2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <net/iw_handler.h>
24 #include <linux/compiler.h>
25 #include <linux/bitmap.h>
26 #include <net/cfg80211.h>
27 #include <asm/unaligned.h>
29 #include "ieee80211_common.h"
30 #include "ieee80211_i.h"
31 #include "ieee80211_rate.h"
37 #include "ieee80211_led.h"
38 #include "ieee80211_cfg.h"
40 #include "debugfs_netdev.h"
41 #include "debugfs_key.h"
43 /* privid for wiphys to determine whether they belong to us or not */
44 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
46 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
47 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
48 static const unsigned char rfc1042_header[] =
49 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
51 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
52 static const unsigned char bridge_tunnel_header[] =
53 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
55 /* No encapsulation header if EtherType < 0x600 (=length) */
56 static const unsigned char eapol_header[] =
57 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
61 * For seeing transmitted packets on monitor interfaces
62 * we have a radiotap header too.
64 struct ieee80211_tx_status_rtap_hdr {
65 struct ieee80211_radiotap_header hdr;
68 } __attribute__ ((packed));
71 static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
72 struct ieee80211_hdr *hdr)
74 /* Set the sequence number for this frame. */
75 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
77 /* Increase the sequence number. */
78 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
81 struct ieee80211_key_conf *
82 ieee80211_key_data2conf(struct ieee80211_local *local,
83 const struct ieee80211_key *data)
85 struct ieee80211_key_conf *conf;
87 conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
91 conf->hw_key_idx = data->hw_key_idx;
92 conf->alg = data->alg;
93 conf->keylen = data->keylen;
95 if (data->force_sw_encrypt)
96 conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
97 conf->keyidx = data->keyidx;
98 if (data->default_tx_key)
99 conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
100 if (local->default_wep_only)
101 conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
102 memcpy(conf->key, data->key, data->keylen);
107 struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
108 int idx, size_t key_len, gfp_t flags)
110 struct ieee80211_key *key;
112 key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
115 kref_init(&key->kref);
119 static void ieee80211_key_release(struct kref *kref)
121 struct ieee80211_key *key;
123 key = container_of(kref, struct ieee80211_key, kref);
124 if (key->alg == ALG_CCMP)
125 ieee80211_aes_key_free(key->u.ccmp.tfm);
126 ieee80211_debugfs_key_remove(key);
130 void ieee80211_key_free(struct ieee80211_key *key)
133 kref_put(&key->kref, ieee80211_key_release);
136 static int rate_list_match(const int *rate_list, int rate)
143 for (i = 0; rate_list[i] >= 0; i++)
144 if (rate_list[i] == rate)
151 void ieee80211_prepare_rates(struct ieee80211_local *local,
152 struct ieee80211_hw_mode *mode)
156 for (i = 0; i < mode->num_rates; i++) {
157 struct ieee80211_rate *rate = &mode->rates[i];
159 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
160 IEEE80211_RATE_BASIC);
162 if (local->supp_rates[mode->mode]) {
163 if (!rate_list_match(local->supp_rates[mode->mode],
168 rate->flags |= IEEE80211_RATE_SUPPORTED;
170 /* Use configured basic rate set if it is available. If not,
171 * use defaults that are sane for most cases. */
172 if (local->basic_rates[mode->mode]) {
173 if (rate_list_match(local->basic_rates[mode->mode],
175 rate->flags |= IEEE80211_RATE_BASIC;
176 } else switch (mode->mode) {
177 case MODE_IEEE80211A:
178 if (rate->rate == 60 || rate->rate == 120 ||
180 rate->flags |= IEEE80211_RATE_BASIC;
182 case MODE_IEEE80211B:
183 if (rate->rate == 10 || rate->rate == 20)
184 rate->flags |= IEEE80211_RATE_BASIC;
186 case MODE_ATHEROS_TURBO:
187 if (rate->rate == 120 || rate->rate == 240 ||
189 rate->flags |= IEEE80211_RATE_BASIC;
191 case MODE_IEEE80211G:
192 if (rate->rate == 10 || rate->rate == 20 ||
193 rate->rate == 55 || rate->rate == 110)
194 rate->flags |= IEEE80211_RATE_BASIC;
198 /* Set ERP and MANDATORY flags based on phymode */
199 switch (mode->mode) {
200 case MODE_IEEE80211A:
201 if (rate->rate == 60 || rate->rate == 120 ||
203 rate->flags |= IEEE80211_RATE_MANDATORY;
205 case MODE_IEEE80211B:
206 if (rate->rate == 10)
207 rate->flags |= IEEE80211_RATE_MANDATORY;
209 case MODE_ATHEROS_TURBO:
211 case MODE_IEEE80211G:
212 if (rate->rate == 10 || rate->rate == 20 ||
213 rate->rate == 55 || rate->rate == 110 ||
214 rate->rate == 60 || rate->rate == 120 ||
216 rate->flags |= IEEE80211_RATE_MANDATORY;
219 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
220 rate->flags |= IEEE80211_RATE_ERP;
225 static void ieee80211_key_threshold_notify(struct net_device *dev,
226 struct ieee80211_key *key,
227 struct sta_info *sta)
229 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
231 struct ieee80211_msg_key_notification *msg;
233 /* if no one will get it anyway, don't even allocate it.
234 * unlikely because this is only relevant for APs
235 * where the device must be open... */
236 if (unlikely(!local->apdev))
239 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
240 sizeof(struct ieee80211_msg_key_notification));
244 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
245 msg = (struct ieee80211_msg_key_notification *)
246 skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
247 msg->tx_rx_count = key->tx_rx_count;
248 memcpy(msg->ifname, dev->name, IFNAMSIZ);
250 memcpy(msg->addr, sta->addr, ETH_ALEN);
252 memset(msg->addr, 0xff, ETH_ALEN);
254 key->tx_rx_count = 0;
256 ieee80211_rx_mgmt(local, skb, NULL,
257 ieee80211_msg_key_threshold_notification);
261 static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
268 fc = le16_to_cpu(hdr->frame_control);
270 switch (fc & IEEE80211_FCTL_FTYPE) {
271 case IEEE80211_FTYPE_DATA:
272 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
273 case IEEE80211_FCTL_TODS:
275 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
277 case IEEE80211_FCTL_FROMDS:
283 case IEEE80211_FTYPE_MGMT:
285 case IEEE80211_FTYPE_CTL:
286 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
295 int ieee80211_get_hdrlen(u16 fc)
299 switch (fc & IEEE80211_FCTL_FTYPE) {
300 case IEEE80211_FTYPE_DATA:
301 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
302 hdrlen = 30; /* Addr4 */
304 * The QoS Control field is two bytes and its presence is
305 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
306 * hdrlen if that bit is set.
307 * This works by masking out the bit and shifting it to
308 * bit position 1 so the result has the value 0 or 2.
310 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
311 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
313 case IEEE80211_FTYPE_CTL:
315 * ACK and CTS are 10 bytes, all others 16. To see how
316 * to get this condition consider
317 * subtype mask: 0b0000000011110000 (0x00F0)
318 * ACK subtype: 0b0000000011010000 (0x00D0)
319 * CTS subtype: 0b0000000011000000 (0x00C0)
320 * bits that matter: ^^^ (0x00E0)
321 * value of those: 0b0000000011000000 (0x00C0)
323 if ((fc & 0xE0) == 0xC0)
332 EXPORT_SYMBOL(ieee80211_get_hdrlen);
334 int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
336 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
339 if (unlikely(skb->len < 10))
341 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
342 if (unlikely(hdrlen > skb->len))
346 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
348 static int ieee80211_get_radiotap_len(struct sk_buff *skb)
350 struct ieee80211_radiotap_header *hdr =
351 (struct ieee80211_radiotap_header *) skb->data;
353 return le16_to_cpu(hdr->it_len);
356 #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
357 static void ieee80211_dump_frame(const char *ifname, const char *title,
358 const struct sk_buff *skb)
360 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
364 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
370 fc = le16_to_cpu(hdr->frame_control);
371 hdrlen = ieee80211_get_hdrlen(fc);
372 if (hdrlen > skb->len)
375 printk(" FC=0x%04x DUR=0x%04x",
376 fc, le16_to_cpu(hdr->duration_id));
378 printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
380 printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
382 printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
384 printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
387 #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
388 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
392 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
395 static int ieee80211_is_eapol(const struct sk_buff *skb)
397 const struct ieee80211_hdr *hdr;
401 if (unlikely(skb->len < 10))
404 hdr = (const struct ieee80211_hdr *) skb->data;
405 fc = le16_to_cpu(hdr->frame_control);
407 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
410 hdrlen = ieee80211_get_hdrlen(fc);
412 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
413 memcmp(skb->data + hdrlen, eapol_header,
414 sizeof(eapol_header)) == 0))
421 static ieee80211_txrx_result
422 ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
424 struct rate_control_extra extra;
426 memset(&extra, 0, sizeof(extra));
427 extra.mode = tx->u.tx.mode;
428 extra.mgmt_data = tx->sdata &&
429 tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
430 extra.ethertype = tx->ethertype;
432 tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
434 if (unlikely(extra.probe != NULL)) {
435 tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
436 tx->u.tx.probe_last_frag = 1;
437 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
438 tx->u.tx.rate = extra.probe;
440 tx->u.tx.control->alt_retry_rate = -1;
444 if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
445 tx->local->cts_protect_erp_frames && tx->fragmented &&
447 tx->u.tx.last_frag_rate = tx->u.tx.rate;
448 tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
450 tx->u.tx.rate = extra.nonerp;
451 tx->u.tx.control->rate = extra.nonerp;
452 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
454 tx->u.tx.last_frag_rate = tx->u.tx.rate;
455 tx->u.tx.control->rate = tx->u.tx.rate;
457 tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
458 if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
459 tx->local->short_preamble &&
460 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
461 tx->u.tx.short_preamble = 1;
462 tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
465 return TXRX_CONTINUE;
469 static ieee80211_txrx_result
470 ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
473 tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
475 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
477 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
479 else if (tx->sta && tx->sta->key)
480 tx->key = tx->sta->key;
481 else if (tx->sdata->default_key)
482 tx->key = tx->sdata->default_key;
483 else if (tx->sdata->drop_unencrypted &&
484 !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
485 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
491 tx->key->tx_rx_count++;
492 if (unlikely(tx->local->key_tx_rx_threshold &&
493 tx->key->tx_rx_count >
494 tx->local->key_tx_rx_threshold)) {
495 ieee80211_key_threshold_notify(tx->dev, tx->key,
500 return TXRX_CONTINUE;
504 static ieee80211_txrx_result
505 ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
508 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
509 struct sk_buff **frags, *first, *frag;
513 int frag_threshold = tx->local->fragmentation_threshold;
516 return TXRX_CONTINUE;
520 hdrlen = ieee80211_get_hdrlen(tx->fc);
521 payload_len = first->len - hdrlen;
522 per_fragm = frag_threshold - hdrlen - FCS_LEN;
523 num_fragm = (payload_len + per_fragm - 1) / per_fragm;
525 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
529 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
530 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
531 pos = first->data + hdrlen + per_fragm;
532 left = payload_len - per_fragm;
533 for (i = 0; i < num_fragm - 1; i++) {
534 struct ieee80211_hdr *fhdr;
540 /* reserve enough extra head and tail room for possible
543 dev_alloc_skb(tx->local->tx_headroom +
545 IEEE80211_ENCRYPT_HEADROOM +
546 IEEE80211_ENCRYPT_TAILROOM);
549 /* Make sure that all fragments use the same priority so
550 * that they end up using the same TX queue */
551 frag->priority = first->priority;
552 skb_reserve(frag, tx->local->tx_headroom +
553 IEEE80211_ENCRYPT_HEADROOM);
554 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
555 memcpy(fhdr, first->data, hdrlen);
556 if (i == num_fragm - 2)
557 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
558 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
559 copylen = left > per_fragm ? per_fragm : left;
560 memcpy(skb_put(frag, copylen), pos, copylen);
565 skb_trim(first, hdrlen + per_fragm);
567 tx->u.tx.num_extra_frag = num_fragm - 1;
568 tx->u.tx.extra_frag = frags;
570 return TXRX_CONTINUE;
573 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
575 for (i = 0; i < num_fragm - 1; i++)
577 dev_kfree_skb(frags[i]);
580 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
585 static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
587 if (tx->key->force_sw_encrypt) {
588 if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
591 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
592 if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
593 if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
602 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
604 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
606 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
607 if (tx->u.tx.extra_frag) {
608 struct ieee80211_hdr *fhdr;
610 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
611 fhdr = (struct ieee80211_hdr *)
612 tx->u.tx.extra_frag[i]->data;
613 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
619 static ieee80211_txrx_result
620 ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
622 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
625 fc = le16_to_cpu(hdr->frame_control);
627 if (!tx->key || tx->key->alg != ALG_WEP ||
628 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
629 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
630 (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
631 return TXRX_CONTINUE;
633 tx->u.tx.control->iv_len = WEP_IV_LEN;
634 tx->u.tx.control->icv_len = WEP_ICV_LEN;
635 ieee80211_tx_set_iswep(tx);
637 if (wep_encrypt_skb(tx, tx->skb) < 0) {
638 I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
642 if (tx->u.tx.extra_frag) {
644 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
645 if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
646 I802_DEBUG_INC(tx->local->
647 tx_handlers_drop_wep);
653 return TXRX_CONTINUE;
657 static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
658 int rate, int erp, int short_preamble)
662 /* calculate duration (in microseconds, rounded up to next higher
663 * integer if it includes a fractional microsecond) to send frame of
664 * len bytes (does not include FCS) at the given rate. Duration will
667 * rate is in 100 kbps, so divident is multiplied by 10 in the
668 * DIV_ROUND_UP() operations.
671 if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
672 local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
676 * N_DBPS = DATARATE x 4
677 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
678 * (16 = SIGNAL time, 6 = tail bits)
679 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
682 * 802.11a - 17.5.2: aSIFSTime = 16 usec
683 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
684 * signal ext = 6 usec
686 /* FIX: Atheros Turbo may have different (shorter) duration? */
687 dur = 16; /* SIFS + signal ext */
688 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
689 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
690 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
691 4 * rate); /* T_SYM x N_SYM */
694 * 802.11b or 802.11g with 802.11b compatibility:
695 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
696 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
698 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
699 * aSIFSTime = 10 usec
700 * aPreambleLength = 144 usec or 72 usec with short preamble
701 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
703 dur = 10; /* aSIFSTime = 10 usec */
704 dur += short_preamble ? (72 + 24) : (144 + 48);
706 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
713 /* Exported duration function for driver use */
714 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
715 size_t frame_len, int rate)
717 struct ieee80211_local *local = hw_to_local(hw);
721 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
722 dur = ieee80211_frame_duration(local, frame_len, rate,
723 erp, local->short_preamble);
725 return cpu_to_le16(dur);
727 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
730 static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
733 int rate, mrate, erp, dur, i;
734 struct ieee80211_rate *txrate = tx->u.tx.rate;
735 struct ieee80211_local *local = tx->local;
736 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
738 erp = txrate->flags & IEEE80211_RATE_ERP;
741 * data and mgmt (except PS Poll):
742 * - during CFP: 32768
743 * - during contention period:
744 * if addr1 is group address: 0
745 * if more fragments = 0 and addr1 is individual address: time to
746 * transmit one ACK plus SIFS
747 * if more fragments = 1 and addr1 is individual address: time to
748 * transmit next fragment plus 2 x ACK plus 3 x SIFS
751 * - control response frame (CTS or ACK) shall be transmitted using the
752 * same rate as the immediately previous frame in the frame exchange
753 * sequence, if this rate belongs to the PHY mandatory rates, or else
754 * at the highest possible rate belonging to the PHY rates in the
758 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
759 /* TODO: These control frames are not currently sent by
760 * 80211.o, but should they be implemented, this function
761 * needs to be updated to support duration field calculation.
763 * RTS: time needed to transmit pending data/mgmt frame plus
764 * one CTS frame plus one ACK frame plus 3 x SIFS
765 * CTS: duration of immediately previous RTS minus time
766 * required to transmit CTS and its SIFS
767 * ACK: 0 if immediately previous directed data/mgmt had
768 * more=0, with more=1 duration in ACK frame is duration
769 * from previous frame minus time needed to transmit ACK
771 * PS Poll: BIT(15) | BIT(14) | aid
777 if (0 /* FIX: data/mgmt during CFP */)
780 if (group_addr) /* Group address as the destination - no ACK */
783 /* Individual destination address:
784 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
785 * CTS and ACK frames shall be transmitted using the highest rate in
786 * basic rate set that is less than or equal to the rate of the
787 * immediately previous frame and that is using the same modulation
788 * (CCK or OFDM). If no basic rate set matches with these requirements,
789 * the highest mandatory rate of the PHY that is less than or equal to
790 * the rate of the previous frame is used.
791 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
794 mrate = 10; /* use 1 Mbps if everything fails */
795 for (i = 0; i < mode->num_rates; i++) {
796 struct ieee80211_rate *r = &mode->rates[i];
797 if (r->rate > txrate->rate)
800 if (IEEE80211_RATE_MODULATION(txrate->flags) !=
801 IEEE80211_RATE_MODULATION(r->flags))
804 if (r->flags & IEEE80211_RATE_BASIC)
806 else if (r->flags & IEEE80211_RATE_MANDATORY)
810 /* No matching basic rate found; use highest suitable mandatory
815 /* Time needed to transmit ACK
816 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
817 * to closest integer */
819 dur = ieee80211_frame_duration(local, 10, rate, erp,
820 local->short_preamble);
823 /* Frame is fragmented: duration increases with time needed to
824 * transmit next fragment plus ACK and 2 x SIFS. */
825 dur *= 2; /* ACK + SIFS */
827 dur += ieee80211_frame_duration(local, next_frag_len,
829 local->short_preamble);
836 static ieee80211_txrx_result
837 ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
839 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
841 struct ieee80211_tx_control *control = tx->u.tx.control;
842 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
844 if (!is_multicast_ether_addr(hdr->addr1)) {
845 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
846 tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
847 control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
848 control->retry_limit =
849 tx->local->long_retry_limit;
851 control->retry_limit =
852 tx->local->short_retry_limit;
855 control->retry_limit = 1;
858 if (tx->fragmented) {
859 /* Do not use multiple retry rates when sending fragmented
861 * TODO: The last fragment could still use multiple retry
863 control->alt_retry_rate = -1;
866 /* Use CTS protection for unicast frames sent using extended rates if
867 * there are associated non-ERP stations and RTS/CTS is not configured
869 if (mode->mode == MODE_IEEE80211G &&
870 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
872 tx->local->cts_protect_erp_frames &&
873 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
874 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
876 /* Setup duration field for the first fragment of the frame. Duration
877 * for remaining fragments will be updated when they are being sent
878 * to low-level driver in ieee80211_tx(). */
879 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
880 tx->fragmented ? tx->u.tx.extra_frag[0]->len :
882 hdr->duration_id = cpu_to_le16(dur);
884 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
885 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
886 struct ieee80211_rate *rate;
888 /* Do not use multiple retry rates when using RTS/CTS */
889 control->alt_retry_rate = -1;
891 /* Use min(data rate, max base rate) as CTS/RTS rate */
892 rate = tx->u.tx.rate;
893 while (rate > mode->rates &&
894 !(rate->flags & IEEE80211_RATE_BASIC))
897 control->rts_cts_rate = rate->val;
898 control->rts_rate = rate;
902 tx->sta->tx_packets++;
903 tx->sta->tx_fragments++;
904 tx->sta->tx_bytes += tx->skb->len;
905 if (tx->u.tx.extra_frag) {
907 tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
908 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
910 tx->u.tx.extra_frag[i]->len;
915 return TXRX_CONTINUE;
919 static ieee80211_txrx_result
920 ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
922 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
923 struct sk_buff *skb = tx->skb;
924 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
925 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
928 if (unlikely(tx->local->sta_scanning != 0) &&
929 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
930 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
933 if (tx->u.tx.ps_buffered)
934 return TXRX_CONTINUE;
936 sta_flags = tx->sta ? tx->sta->flags : 0;
938 if (likely(tx->u.tx.unicast)) {
939 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
940 tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
941 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
942 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
943 printk(KERN_DEBUG "%s: dropped data frame to not "
944 "associated station " MAC_FMT "\n",
945 tx->dev->name, MAC_ARG(hdr->addr1));
946 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
947 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
951 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
952 tx->local->num_sta == 0 &&
953 !tx->local->allow_broadcast_always &&
954 tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
956 * No associated STAs - no need to send multicast
961 return TXRX_CONTINUE;
964 if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
965 !(sta_flags & WLAN_STA_AUTHORIZED))) {
966 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
967 printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
968 " (unauthorized port)\n", tx->dev->name,
969 MAC_ARG(hdr->addr1));
971 I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
975 return TXRX_CONTINUE;
978 static ieee80211_txrx_result
979 ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
981 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
983 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
984 ieee80211_include_sequence(tx->sdata, hdr);
986 return TXRX_CONTINUE;
989 /* This function is called whenever the AP is about to exceed the maximum limit
990 * of buffered frames for power saving STAs. This situation should not really
991 * happen often during normal operation, so dropping the oldest buffered packet
992 * from each queue should be OK to make some room for new frames. */
993 static void purge_old_ps_buffers(struct ieee80211_local *local)
995 int total = 0, purged = 0;
997 struct ieee80211_sub_if_data *sdata;
998 struct sta_info *sta;
1000 read_lock(&local->sub_if_lock);
1001 list_for_each_entry(sdata, &local->sub_if_list, list) {
1002 struct ieee80211_if_ap *ap;
1003 if (sdata->dev == local->mdev ||
1004 sdata->type != IEEE80211_IF_TYPE_AP)
1007 skb = skb_dequeue(&ap->ps_bc_buf);
1012 total += skb_queue_len(&ap->ps_bc_buf);
1014 read_unlock(&local->sub_if_lock);
1016 spin_lock_bh(&local->sta_lock);
1017 list_for_each_entry(sta, &local->sta_list, list) {
1018 skb = skb_dequeue(&sta->ps_tx_buf);
1023 total += skb_queue_len(&sta->ps_tx_buf);
1025 spin_unlock_bh(&local->sta_lock);
1027 local->total_ps_buffered = total;
1028 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
1029 local->mdev->name, purged);
1033 static inline ieee80211_txrx_result
1034 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
1036 /* broadcast/multicast frame */
1037 /* If any of the associated stations is in power save mode,
1038 * the frame is buffered to be sent after DTIM beacon frame */
1039 if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
1040 tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
1041 tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
1042 !(tx->fc & IEEE80211_FCTL_ORDER)) {
1043 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
1044 purge_old_ps_buffers(tx->local);
1045 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
1047 if (net_ratelimit()) {
1048 printk(KERN_DEBUG "%s: BC TX buffer full - "
1049 "dropping the oldest frame\n",
1052 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
1054 tx->local->total_ps_buffered++;
1055 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
1059 return TXRX_CONTINUE;
1063 static inline ieee80211_txrx_result
1064 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
1066 struct sta_info *sta = tx->sta;
1068 if (unlikely(!sta ||
1069 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
1070 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
1071 return TXRX_CONTINUE;
1073 if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
1074 struct ieee80211_tx_packet_data *pkt_data;
1075 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1076 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
1078 MAC_ARG(sta->addr), sta->aid,
1079 skb_queue_len(&sta->ps_tx_buf));
1080 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1081 sta->flags |= WLAN_STA_TIM;
1082 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
1083 purge_old_ps_buffers(tx->local);
1084 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
1085 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
1086 if (net_ratelimit()) {
1087 printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
1088 "buffer full - dropping oldest frame\n",
1089 tx->dev->name, MAC_ARG(sta->addr));
1093 tx->local->total_ps_buffered++;
1094 /* Queue frame to be sent after STA sends an PS Poll frame */
1095 if (skb_queue_empty(&sta->ps_tx_buf)) {
1096 if (tx->local->ops->set_tim)
1097 tx->local->ops->set_tim(local_to_hw(tx->local),
1100 bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
1102 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
1103 pkt_data->jiffies = jiffies;
1104 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
1107 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1108 else if (unlikely(sta->flags & WLAN_STA_PS)) {
1109 printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
1110 "set -> send frame\n", tx->dev->name,
1111 MAC_ARG(sta->addr));
1113 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1116 return TXRX_CONTINUE;
1120 static ieee80211_txrx_result
1121 ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
1123 if (unlikely(tx->u.tx.ps_buffered))
1124 return TXRX_CONTINUE;
1126 if (tx->u.tx.unicast)
1127 return ieee80211_tx_h_unicast_ps_buf(tx);
1129 return ieee80211_tx_h_multicast_ps_buf(tx);
1134 * deal with packet injection down monitor interface
1135 * with Radiotap Header -- only called for monitor mode interface
1138 static ieee80211_txrx_result
1139 __ieee80211_parse_tx_radiotap(
1140 struct ieee80211_txrx_data *tx,
1141 struct sk_buff *skb, struct ieee80211_tx_control *control)
1144 * this is the moment to interpret and discard the radiotap header that
1145 * must be at the start of the packet injected in Monitor mode
1147 * Need to take some care with endian-ness since radiotap
1148 * args are little-endian
1151 struct ieee80211_radiotap_iterator iterator;
1152 struct ieee80211_radiotap_header *rthdr =
1153 (struct ieee80211_radiotap_header *) skb->data;
1154 struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
1155 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
1158 * default control situation for all injected packets
1159 * FIXME: this does not suit all usage cases, expand to allow control
1162 control->retry_limit = 1; /* no retry */
1163 control->key_idx = -1; /* no encryption key */
1164 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1165 IEEE80211_TXCTL_USE_CTS_PROTECT);
1166 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
1167 IEEE80211_TXCTL_NO_ACK;
1168 control->antenna_sel_tx = 0; /* default to default antenna */
1171 * for every radiotap entry that is present
1172 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1173 * entries present, or -EINVAL on error)
1179 ret = ieee80211_radiotap_iterator_next(&iterator);
1184 /* see if this argument is something we can use */
1185 switch (iterator.this_arg_index) {
1187 * You must take care when dereferencing iterator.this_arg
1188 * for multibyte types... the pointer is not aligned. Use
1189 * get_unaligned((type *)iterator.this_arg) to dereference
1190 * iterator.this_arg for type "type" safely on all arches.
1192 case IEEE80211_RADIOTAP_RATE:
1194 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
1195 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
1197 target_rate = (*iterator.this_arg) * 5;
1198 for (i = 0; i < mode->num_rates; i++) {
1199 struct ieee80211_rate *r = &mode->rates[i];
1201 if (r->rate > target_rate)
1206 if (r->flags & IEEE80211_RATE_PREAMBLE2)
1207 control->tx_rate = r->val2;
1209 control->tx_rate = r->val;
1211 /* end on exact match */
1212 if (r->rate == target_rate)
1213 i = mode->num_rates;
1217 case IEEE80211_RADIOTAP_ANTENNA:
1219 * radiotap uses 0 for 1st ant, mac80211 is 1 for
1222 control->antenna_sel_tx = (*iterator.this_arg) + 1;
1225 case IEEE80211_RADIOTAP_DBM_TX_POWER:
1226 control->power_level = *iterator.this_arg;
1229 case IEEE80211_RADIOTAP_FLAGS:
1230 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1232 * this indicates that the skb we have been
1233 * handed has the 32-bit FCS CRC at the end...
1234 * we should react to that by snipping it off
1235 * because it will be recomputed and added
1238 if (skb->len < (iterator.max_length + FCS_LEN))
1241 skb_trim(skb, skb->len - FCS_LEN);
1250 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1254 * remove the radiotap header
1255 * iterator->max_length was sanity-checked against
1256 * skb->len by iterator init
1258 skb_pull(skb, iterator.max_length);
1260 return TXRX_CONTINUE;
1264 static ieee80211_txrx_result inline
1265 __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1266 struct sk_buff *skb,
1267 struct net_device *dev,
1268 struct ieee80211_tx_control *control)
1270 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1271 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1272 struct ieee80211_sub_if_data *sdata;
1273 ieee80211_txrx_result res = TXRX_CONTINUE;
1277 memset(tx, 0, sizeof(*tx));
1279 tx->dev = dev; /* use original interface */
1281 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1282 tx->sta = sta_info_get(local, hdr->addr1);
1283 tx->fc = le16_to_cpu(hdr->frame_control);
1286 * set defaults for things that can be set by
1287 * injected radiotap headers
1289 control->power_level = local->hw.conf.power_level;
1290 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1291 if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
1292 control->antenna_sel_tx = tx->sta->antenna_sel_tx;
1294 /* process and remove the injection radiotap header */
1295 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1296 if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
1297 if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
1302 * we removed the radiotap header after this point,
1303 * we filled control with what we could use
1304 * set to the actual ieee header now
1306 hdr = (struct ieee80211_hdr *) skb->data;
1307 res = TXRX_QUEUED; /* indication it was monitor packet */
1310 tx->u.tx.control = control;
1311 tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
1312 if (is_multicast_ether_addr(hdr->addr1))
1313 control->flags |= IEEE80211_TXCTL_NO_ACK;
1315 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
1316 tx->fragmented = local->fragmentation_threshold <
1317 IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
1318 skb->len + FCS_LEN > local->fragmentation_threshold &&
1319 (!local->ops->set_frag_threshold);
1321 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1322 else if (tx->sta->clear_dst_mask) {
1323 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1324 tx->sta->clear_dst_mask = 0;
1326 hdrlen = ieee80211_get_hdrlen(tx->fc);
1327 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1328 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1329 tx->ethertype = (pos[0] << 8) | pos[1];
1331 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1336 static int inline is_ieee80211_device(struct net_device *dev,
1337 struct net_device *master)
1339 return (wdev_priv(dev->ieee80211_ptr) ==
1340 wdev_priv(master->ieee80211_ptr));
1343 /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
1344 * finished with it. */
1345 static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1346 struct sk_buff *skb,
1347 struct net_device *mdev,
1348 struct ieee80211_tx_control *control)
1350 struct ieee80211_tx_packet_data *pkt_data;
1351 struct net_device *dev;
1353 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1354 dev = dev_get_by_index(pkt_data->ifindex);
1355 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1361 __ieee80211_tx_prepare(tx, skb, dev, control);
1365 static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
1368 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
1371 static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
1374 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
1377 #define IEEE80211_TX_OK 0
1378 #define IEEE80211_TX_AGAIN 1
1379 #define IEEE80211_TX_FRAG_AGAIN 2
1381 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1382 struct ieee80211_txrx_data *tx)
1384 struct ieee80211_tx_control *control = tx->u.tx.control;
1387 if (!ieee80211_qdisc_installed(local->mdev) &&
1388 __ieee80211_queue_stopped(local, 0)) {
1389 netif_stop_queue(local->mdev);
1390 return IEEE80211_TX_AGAIN;
1393 ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
1394 ret = local->ops->tx(local_to_hw(local), skb, control);
1396 return IEEE80211_TX_AGAIN;
1397 local->mdev->trans_start = jiffies;
1398 ieee80211_led_tx(local, 1);
1400 if (tx->u.tx.extra_frag) {
1401 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1402 IEEE80211_TXCTL_USE_CTS_PROTECT |
1403 IEEE80211_TXCTL_CLEAR_DST_MASK |
1404 IEEE80211_TXCTL_FIRST_FRAGMENT);
1405 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1406 if (!tx->u.tx.extra_frag[i])
1408 if (__ieee80211_queue_stopped(local, control->queue))
1409 return IEEE80211_TX_FRAG_AGAIN;
1410 if (i == tx->u.tx.num_extra_frag) {
1411 control->tx_rate = tx->u.tx.last_frag_hwrate;
1412 control->rate = tx->u.tx.last_frag_rate;
1413 if (tx->u.tx.probe_last_frag)
1415 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1418 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1421 ieee80211_dump_frame(local->mdev->name,
1422 "TX to low-level driver",
1423 tx->u.tx.extra_frag[i]);
1424 ret = local->ops->tx(local_to_hw(local),
1425 tx->u.tx.extra_frag[i],
1428 return IEEE80211_TX_FRAG_AGAIN;
1429 local->mdev->trans_start = jiffies;
1430 ieee80211_led_tx(local, 1);
1431 tx->u.tx.extra_frag[i] = NULL;
1433 kfree(tx->u.tx.extra_frag);
1434 tx->u.tx.extra_frag = NULL;
1436 return IEEE80211_TX_OK;
1439 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
1440 struct ieee80211_tx_control *control, int mgmt)
1442 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1443 struct sta_info *sta;
1444 ieee80211_tx_handler *handler;
1445 struct ieee80211_txrx_data tx;
1446 ieee80211_txrx_result res = TXRX_DROP, res_prepare;
1449 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1451 if (unlikely(skb->len < 10)) {
1456 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1458 if (res_prepare == TXRX_DROP) {
1464 tx.u.tx.mgmt_interface = mgmt;
1465 tx.u.tx.mode = local->hw.conf.mode;
1467 if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
1468 res = TXRX_CONTINUE;
1470 for (handler = local->tx_handlers; *handler != NULL;
1472 res = (*handler)(&tx);
1473 if (res != TXRX_CONTINUE)
1478 skb = tx.skb; /* handlers are allowed to change skb */
1483 if (unlikely(res == TXRX_DROP)) {
1484 I802_DEBUG_INC(local->tx_handlers_drop);
1488 if (unlikely(res == TXRX_QUEUED)) {
1489 I802_DEBUG_INC(local->tx_handlers_queued);
1493 if (tx.u.tx.extra_frag) {
1494 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1496 struct ieee80211_hdr *hdr =
1497 (struct ieee80211_hdr *)
1498 tx.u.tx.extra_frag[i]->data;
1500 if (i + 1 < tx.u.tx.num_extra_frag) {
1501 next_len = tx.u.tx.extra_frag[i + 1]->len;
1504 tx.u.tx.rate = tx.u.tx.last_frag_rate;
1505 tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
1507 dur = ieee80211_duration(&tx, 0, next_len);
1508 hdr->duration_id = cpu_to_le16(dur);
1513 ret = __ieee80211_tx(local, skb, &tx);
1515 struct ieee80211_tx_stored_packet *store =
1516 &local->pending_packet[control->queue];
1518 if (ret == IEEE80211_TX_FRAG_AGAIN)
1520 set_bit(IEEE80211_LINK_STATE_PENDING,
1521 &local->state[control->queue]);
1523 /* When the driver gets out of buffers during sending of
1524 * fragments and calls ieee80211_stop_queue, there is
1525 * a small window between IEEE80211_LINK_STATE_XOFF and
1526 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1527 * gets available in that window (i.e. driver calls
1528 * ieee80211_wake_queue), we would end up with ieee80211_tx
1529 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1530 * continuing transmitting here when that situation is
1531 * possible to have happened. */
1532 if (!__ieee80211_queue_stopped(local, control->queue)) {
1533 clear_bit(IEEE80211_LINK_STATE_PENDING,
1534 &local->state[control->queue]);
1537 memcpy(&store->control, control,
1538 sizeof(struct ieee80211_tx_control));
1540 store->extra_frag = tx.u.tx.extra_frag;
1541 store->num_extra_frag = tx.u.tx.num_extra_frag;
1542 store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
1543 store->last_frag_rate = tx.u.tx.last_frag_rate;
1544 store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
1551 for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1552 if (tx.u.tx.extra_frag[i])
1553 dev_kfree_skb(tx.u.tx.extra_frag[i]);
1554 kfree(tx.u.tx.extra_frag);
1558 static void ieee80211_tx_pending(unsigned long data)
1560 struct ieee80211_local *local = (struct ieee80211_local *)data;
1561 struct net_device *dev = local->mdev;
1562 struct ieee80211_tx_stored_packet *store;
1563 struct ieee80211_txrx_data tx;
1564 int i, ret, reschedule = 0;
1566 netif_tx_lock_bh(dev);
1567 for (i = 0; i < local->hw.queues; i++) {
1568 if (__ieee80211_queue_stopped(local, i))
1570 if (!__ieee80211_queue_pending(local, i)) {
1574 store = &local->pending_packet[i];
1575 tx.u.tx.control = &store->control;
1576 tx.u.tx.extra_frag = store->extra_frag;
1577 tx.u.tx.num_extra_frag = store->num_extra_frag;
1578 tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
1579 tx.u.tx.last_frag_rate = store->last_frag_rate;
1580 tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
1581 ret = __ieee80211_tx(local, store->skb, &tx);
1583 if (ret == IEEE80211_TX_FRAG_AGAIN)
1586 clear_bit(IEEE80211_LINK_STATE_PENDING,
1591 netif_tx_unlock_bh(dev);
1593 if (!ieee80211_qdisc_installed(dev)) {
1594 if (!__ieee80211_queue_stopped(local, 0))
1595 netif_wake_queue(dev);
1597 netif_schedule(dev);
1601 static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1604 struct ieee80211_tx_stored_packet *store;
1606 for (i = 0; i < local->hw.queues; i++) {
1607 if (!__ieee80211_queue_pending(local, i))
1609 store = &local->pending_packet[i];
1610 kfree_skb(store->skb);
1611 for (j = 0; j < store->num_extra_frag; j++)
1612 kfree_skb(store->extra_frag[j]);
1613 kfree(store->extra_frag);
1614 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1618 static int ieee80211_master_start_xmit(struct sk_buff *skb,
1619 struct net_device *dev)
1621 struct ieee80211_tx_control control;
1622 struct ieee80211_tx_packet_data *pkt_data;
1623 struct net_device *odev = NULL;
1624 struct ieee80211_sub_if_data *osdata;
1629 * copy control out of the skb so other people can use skb->cb
1631 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1632 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1634 if (pkt_data->ifindex)
1635 odev = dev_get_by_index(pkt_data->ifindex);
1636 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1640 if (unlikely(!odev)) {
1641 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1642 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1643 "originating device\n", dev->name);
1648 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1650 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1651 if (skb_headroom(skb) < headroom) {
1652 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1658 control.ifindex = odev->ifindex;
1659 control.type = osdata->type;
1660 if (pkt_data->req_tx_status)
1661 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
1662 if (pkt_data->do_not_encrypt)
1663 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1664 if (pkt_data->requeue)
1665 control.flags |= IEEE80211_TXCTL_REQUEUE;
1666 control.queue = pkt_data->queue;
1668 ret = ieee80211_tx(odev, skb, &control,
1669 control.type == IEEE80211_IF_TYPE_MGMT);
1676 int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1677 struct net_device *dev)
1679 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1680 struct ieee80211_tx_packet_data *pkt_data;
1681 struct ieee80211_radiotap_header *prthdr =
1682 (struct ieee80211_radiotap_header *)skb->data;
1686 * there must be a radiotap header at the
1687 * start in this case
1689 if (unlikely(prthdr->it_version)) {
1690 /* only version 0 is supported */
1692 return NETDEV_TX_OK;
1695 skb->dev = local->mdev;
1697 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1698 memset(pkt_data, 0, sizeof(*pkt_data));
1699 pkt_data->ifindex = dev->ifindex;
1700 pkt_data->mgmt_iface = 0;
1701 pkt_data->do_not_encrypt = 1;
1703 /* above needed because we set skb device to master */
1706 * fix up the pointers accounting for the radiotap
1707 * header still being in there. We are being given
1708 * a precooked IEEE80211 header so no need for
1711 len = le16_to_cpu(get_unaligned(&prthdr->it_len));
1712 skb_set_mac_header(skb, len);
1713 skb_set_network_header(skb, len + sizeof(struct ieee80211_hdr));
1714 skb_set_transport_header(skb, len + sizeof(struct ieee80211_hdr));
1717 * pass the radiotap header up to
1718 * the next stage intact
1720 dev_queue_xmit(skb);
1722 return NETDEV_TX_OK;
1727 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1728 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1729 * @skb: packet to be sent
1730 * @dev: incoming interface
1732 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1733 * not be freed, and caller is responsible for either retrying later or freeing
1736 * This function takes in an Ethernet header and encapsulates it with suitable
1737 * IEEE 802.11 header based on which interface the packet is coming in. The
1738 * encapsulated packet will then be passed to master interface, wlan#.11, for
1739 * transmission (through low-level driver).
1741 int ieee80211_subif_start_xmit(struct sk_buff *skb,
1742 struct net_device *dev)
1744 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1745 struct ieee80211_tx_packet_data *pkt_data;
1746 struct ieee80211_sub_if_data *sdata;
1747 int ret = 1, head_need;
1748 u16 ethertype, hdrlen, fc;
1749 struct ieee80211_hdr hdr;
1750 const u8 *encaps_data;
1751 int encaps_len, skip_header_bytes;
1752 int nh_pos, h_pos, no_encrypt = 0;
1753 struct sta_info *sta;
1755 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1756 if (unlikely(skb->len < ETH_HLEN)) {
1757 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1758 dev->name, skb->len);
1763 nh_pos = skb_network_header(skb) - skb->data;
1764 h_pos = skb_transport_header(skb) - skb->data;
1766 /* convert Ethernet header to proper 802.11 header (based on
1767 * operation mode) */
1768 ethertype = (skb->data[12] << 8) | skb->data[13];
1769 /* TODO: handling for 802.1x authorized/unauthorized port */
1770 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1772 if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
1773 sdata->type == IEEE80211_IF_TYPE_VLAN)) {
1774 fc |= IEEE80211_FCTL_FROMDS;
1776 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1777 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1778 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1780 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
1781 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1783 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1784 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1785 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1786 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1788 } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
1789 fc |= IEEE80211_FCTL_TODS;
1791 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1792 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1793 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1795 } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
1797 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1798 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1799 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1806 /* receiver is QoS enabled, use a QoS type frame */
1807 sta = sta_info_get(local, hdr.addr1);
1809 if (sta->flags & WLAN_STA_WME) {
1810 fc |= IEEE80211_STYPE_QOS_DATA;
1816 hdr.frame_control = cpu_to_le16(fc);
1817 hdr.duration_id = 0;
1820 skip_header_bytes = ETH_HLEN;
1821 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1822 encaps_data = bridge_tunnel_header;
1823 encaps_len = sizeof(bridge_tunnel_header);
1824 skip_header_bytes -= 2;
1825 } else if (ethertype >= 0x600) {
1826 encaps_data = rfc1042_header;
1827 encaps_len = sizeof(rfc1042_header);
1828 skip_header_bytes -= 2;
1834 skb_pull(skb, skip_header_bytes);
1835 nh_pos -= skip_header_bytes;
1836 h_pos -= skip_header_bytes;
1838 /* TODO: implement support for fragments so that there is no need to
1839 * reallocate and copy payload; it might be enough to support one
1840 * extra fragment that would be copied in the beginning of the frame
1841 * data.. anyway, it would be nice to include this into skb structure
1844 * There are few options for this:
1845 * use skb->cb as an extra space for 802.11 header
1846 * allocate new buffer if not enough headroom
1847 * make sure that there is enough headroom in every skb by increasing
1848 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1849 * alloc_skb() (net/core/skbuff.c)
1851 head_need = hdrlen + encaps_len + local->tx_headroom;
1852 head_need -= skb_headroom(skb);
1854 /* We are going to modify skb data, so make a copy of it if happens to
1855 * be cloned. This could happen, e.g., with Linux bridge code passing
1856 * us broadcast frames. */
1858 if (head_need > 0 || skb_cloned(skb)) {
1860 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1861 "of headroom\n", dev->name, head_need);
1864 if (skb_cloned(skb))
1865 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1867 I802_DEBUG_INC(local->tx_expand_skb_head);
1868 /* Since we have to reallocate the buffer, make sure that there
1869 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1870 * before payload and 12 after). */
1871 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1873 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1880 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1881 nh_pos += encaps_len;
1882 h_pos += encaps_len;
1884 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1888 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1889 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1890 pkt_data->ifindex = dev->ifindex;
1891 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1892 pkt_data->do_not_encrypt = no_encrypt;
1894 skb->dev = local->mdev;
1895 sdata->stats.tx_packets++;
1896 sdata->stats.tx_bytes += skb->len;
1898 /* Update skb pointers to various headers since this modified frame
1899 * is going to go through Linux networking code that may potentially
1900 * need things like pointer to IP header. */
1901 skb_set_mac_header(skb, 0);
1902 skb_set_network_header(skb, nh_pos);
1903 skb_set_transport_header(skb, h_pos);
1905 dev->trans_start = jiffies;
1906 dev_queue_xmit(skb);
1919 * This is the transmit routine for the 802.11 type interfaces
1920 * called by upper layers of the linux networking
1921 * stack when it has a frame to transmit
1924 ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
1926 struct ieee80211_sub_if_data *sdata;
1927 struct ieee80211_tx_packet_data *pkt_data;
1928 struct ieee80211_hdr *hdr;
1931 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1933 if (skb->len < 10) {
1938 if (skb_headroom(skb) < sdata->local->tx_headroom) {
1939 if (pskb_expand_head(skb, sdata->local->tx_headroom,
1946 hdr = (struct ieee80211_hdr *) skb->data;
1947 fc = le16_to_cpu(hdr->frame_control);
1949 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
1950 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1951 pkt_data->ifindex = sdata->dev->ifindex;
1952 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1954 skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
1955 skb->dev = sdata->local->mdev;
1958 * We're using the protocol field of the the frame control header
1959 * to request TX callback for hostapd. BIT(1) is checked.
1961 if ((fc & BIT(1)) == BIT(1)) {
1962 pkt_data->req_tx_status = 1;
1964 hdr->frame_control = cpu_to_le16(fc);
1967 pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
1969 sdata->stats.tx_packets++;
1970 sdata->stats.tx_bytes += skb->len;
1972 dev_queue_xmit(skb);
1978 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1979 struct ieee80211_if_ap *bss,
1980 struct sk_buff *skb)
1984 int i, have_bits = 0, n1, n2;
1986 /* Generate bitmap for TIM only if there are any STAs in power save
1988 spin_lock_bh(&local->sta_lock);
1989 if (atomic_read(&bss->num_sta_ps) > 0)
1990 /* in the hope that this is faster than
1991 * checking byte-for-byte */
1992 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1993 IEEE80211_MAX_AID+1);
1995 if (bss->dtim_count == 0)
1996 bss->dtim_count = bss->dtim_period - 1;
2000 tim = pos = (u8 *) skb_put(skb, 6);
2001 *pos++ = WLAN_EID_TIM;
2003 *pos++ = bss->dtim_count;
2004 *pos++ = bss->dtim_period;
2006 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
2010 /* Find largest even number N1 so that bits numbered 1 through
2011 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2012 * (N2 + 1) x 8 through 2007 are 0. */
2014 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2021 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2028 /* Bitmap control */
2030 /* Part Virt Bitmap */
2031 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
2033 tim[1] = n2 - n1 + 4;
2034 skb_put(skb, n2 - n1);
2036 *pos++ = aid0; /* Bitmap control */
2037 *pos++ = 0; /* Part Virt Bitmap */
2039 spin_unlock_bh(&local->sta_lock);
2043 struct sk_buff * ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
2044 struct ieee80211_tx_control *control)
2046 struct ieee80211_local *local = hw_to_local(hw);
2047 struct sk_buff *skb;
2048 struct net_device *bdev;
2049 struct ieee80211_sub_if_data *sdata = NULL;
2050 struct ieee80211_if_ap *ap = NULL;
2051 struct ieee80211_rate *rate;
2052 struct rate_control_extra extra;
2053 u8 *b_head, *b_tail;
2056 bdev = dev_get_by_index(if_id);
2058 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
2063 if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
2065 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2066 if (net_ratelimit())
2067 printk(KERN_DEBUG "no beacon data avail for idx=%d "
2068 "(%s)\n", if_id, bdev ? bdev->name : "N/A");
2069 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2073 /* Assume we are generating the normal beacon locally */
2074 b_head = ap->beacon_head;
2075 b_tail = ap->beacon_tail;
2076 bh_len = ap->beacon_head_len;
2077 bt_len = ap->beacon_tail_len;
2079 skb = dev_alloc_skb(local->tx_headroom +
2080 bh_len + bt_len + 256 /* maximum TIM len */);
2084 skb_reserve(skb, local->tx_headroom);
2085 memcpy(skb_put(skb, bh_len), b_head, bh_len);
2087 ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
2089 ieee80211_beacon_add_tim(local, ap, skb);
2092 memcpy(skb_put(skb, bt_len), b_tail, bt_len);
2096 memset(&extra, 0, sizeof(extra));
2097 extra.mode = local->oper_hw_mode;
2099 rate = rate_control_get_rate(local, local->mdev, skb, &extra);
2101 if (net_ratelimit()) {
2102 printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
2103 "found\n", local->mdev->name);
2109 control->tx_rate = (local->short_preamble &&
2110 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2111 rate->val2 : rate->val;
2112 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2113 control->power_level = local->hw.conf.power_level;
2114 control->flags |= IEEE80211_TXCTL_NO_ACK;
2115 control->retry_limit = 1;
2116 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
2122 EXPORT_SYMBOL(ieee80211_beacon_get);
2124 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
2126 const struct ieee80211_tx_control *frame_txctl)
2128 struct ieee80211_local *local = hw_to_local(hw);
2129 struct ieee80211_rate *rate;
2130 int short_preamble = local->short_preamble;
2134 rate = frame_txctl->rts_rate;
2135 erp = !!(rate->flags & IEEE80211_RATE_ERP);
2138 dur = ieee80211_frame_duration(local, 10, rate->rate,
2139 erp, short_preamble);
2140 /* Data frame duration */
2141 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
2142 erp, short_preamble);
2144 dur += ieee80211_frame_duration(local, 10, rate->rate,
2145 erp, short_preamble);
2147 return cpu_to_le16(dur);
2149 EXPORT_SYMBOL(ieee80211_rts_duration);
2152 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
2154 const struct ieee80211_tx_control *frame_txctl)
2156 struct ieee80211_local *local = hw_to_local(hw);
2157 struct ieee80211_rate *rate;
2158 int short_preamble = local->short_preamble;
2162 rate = frame_txctl->rts_rate;
2163 erp = !!(rate->flags & IEEE80211_RATE_ERP);
2165 /* Data frame duration */
2166 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
2167 erp, short_preamble);
2168 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
2170 dur += ieee80211_frame_duration(local, 10, rate->rate,
2171 erp, short_preamble);
2174 return cpu_to_le16(dur);
2176 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
2178 void ieee80211_rts_get(struct ieee80211_hw *hw,
2179 const void *frame, size_t frame_len,
2180 const struct ieee80211_tx_control *frame_txctl,
2181 struct ieee80211_rts *rts)
2183 const struct ieee80211_hdr *hdr = frame;
2186 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
2187 rts->frame_control = cpu_to_le16(fctl);
2188 rts->duration = ieee80211_rts_duration(hw, frame_len, frame_txctl);
2189 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2190 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2192 EXPORT_SYMBOL(ieee80211_rts_get);
2194 void ieee80211_ctstoself_get(struct ieee80211_hw *hw,
2195 const void *frame, size_t frame_len,
2196 const struct ieee80211_tx_control *frame_txctl,
2197 struct ieee80211_cts *cts)
2199 const struct ieee80211_hdr *hdr = frame;
2202 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
2203 cts->frame_control = cpu_to_le16(fctl);
2204 cts->duration = ieee80211_ctstoself_duration(hw, frame_len, frame_txctl);
2205 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2207 EXPORT_SYMBOL(ieee80211_ctstoself_get);
2210 ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
2211 struct ieee80211_tx_control *control)
2213 struct ieee80211_local *local = hw_to_local(hw);
2214 struct sk_buff *skb;
2215 struct sta_info *sta;
2216 ieee80211_tx_handler *handler;
2217 struct ieee80211_txrx_data tx;
2218 ieee80211_txrx_result res = TXRX_DROP;
2219 struct net_device *bdev;
2220 struct ieee80211_sub_if_data *sdata;
2221 struct ieee80211_if_ap *bss = NULL;
2223 bdev = dev_get_by_index(if_id);
2225 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
2229 if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
2232 if (bss->dtim_count != 0)
2233 return NULL; /* send buffered bc/mc only after DTIM beacon */
2234 memset(control, 0, sizeof(*control));
2236 skb = skb_dequeue(&bss->ps_bc_buf);
2239 local->total_ps_buffered--;
2241 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2242 struct ieee80211_hdr *hdr =
2243 (struct ieee80211_hdr *) skb->data;
2244 /* more buffered multicast/broadcast frames ==> set
2245 * MoreData flag in IEEE 802.11 header to inform PS
2247 hdr->frame_control |=
2248 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2251 if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
2253 dev_kfree_skb_any(skb);
2256 tx.u.tx.ps_buffered = 1;
2258 for (handler = local->tx_handlers; *handler != NULL; handler++) {
2259 res = (*handler)(&tx);
2260 if (res == TXRX_DROP || res == TXRX_QUEUED)
2264 skb = tx.skb; /* handlers are allowed to change skb */
2266 if (res == TXRX_DROP) {
2267 I802_DEBUG_INC(local->tx_handlers_drop);
2270 } else if (res == TXRX_QUEUED) {
2271 I802_DEBUG_INC(local->tx_handlers_queued);
2280 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
2282 static int __ieee80211_if_config(struct net_device *dev,
2283 struct sk_buff *beacon,
2284 struct ieee80211_tx_control *control)
2286 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2287 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2288 struct ieee80211_if_conf conf;
2289 static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2291 if (!local->ops->config_interface || !netif_running(dev))
2294 memset(&conf, 0, sizeof(conf));
2295 conf.type = sdata->type;
2296 if (sdata->type == IEEE80211_IF_TYPE_STA ||
2297 sdata->type == IEEE80211_IF_TYPE_IBSS) {
2298 if (local->sta_scanning &&
2299 local->scan_dev == dev)
2300 conf.bssid = scan_bssid;
2302 conf.bssid = sdata->u.sta.bssid;
2303 conf.ssid = sdata->u.sta.ssid;
2304 conf.ssid_len = sdata->u.sta.ssid_len;
2305 conf.generic_elem = sdata->u.sta.extra_ie;
2306 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
2307 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
2308 conf.ssid = sdata->u.ap.ssid;
2309 conf.ssid_len = sdata->u.ap.ssid_len;
2310 conf.generic_elem = sdata->u.ap.generic_elem;
2311 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
2312 conf.beacon = beacon;
2313 conf.beacon_control = control;
2315 return local->ops->config_interface(local_to_hw(local),
2316 dev->ifindex, &conf);
2319 int ieee80211_if_config(struct net_device *dev)
2321 return __ieee80211_if_config(dev, NULL, NULL);
2324 int ieee80211_if_config_beacon(struct net_device *dev)
2326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2327 struct ieee80211_tx_control control;
2328 struct sk_buff *skb;
2330 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
2332 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
2335 return __ieee80211_if_config(dev, skb, &control);
2338 int ieee80211_hw_config(struct ieee80211_local *local)
2340 struct ieee80211_hw_mode *mode;
2341 struct ieee80211_channel *chan;
2344 if (local->sta_scanning) {
2345 chan = local->scan_channel;
2346 mode = local->scan_hw_mode;
2348 chan = local->oper_channel;
2349 mode = local->oper_hw_mode;
2352 local->hw.conf.channel = chan->chan;
2353 local->hw.conf.channel_val = chan->val;
2354 local->hw.conf.power_level = chan->power_level;
2355 local->hw.conf.freq = chan->freq;
2356 local->hw.conf.phymode = mode->mode;
2357 local->hw.conf.antenna_max = chan->antenna_max;
2358 local->hw.conf.chan = chan;
2359 local->hw.conf.mode = mode;
2361 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2362 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
2363 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
2364 local->hw.conf.phymode);
2365 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2367 if (local->ops->config)
2368 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
2374 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
2376 /* FIX: what would be proper limits for MTU?
2377 * This interface uses 802.3 frames. */
2378 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
2379 printk(KERN_WARNING "%s: invalid MTU %d\n",
2380 dev->name, new_mtu);
2384 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2385 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
2386 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2392 static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
2394 /* FIX: what would be proper limits for MTU?
2395 * This interface uses 802.11 frames. */
2396 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
2397 printk(KERN_WARNING "%s: invalid MTU %d\n",
2398 dev->name, new_mtu);
2402 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2403 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
2404 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2409 enum netif_tx_lock_class {
2414 static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
2416 spin_lock_nested(&dev->_xmit_lock, subclass);
2417 dev->xmit_lock_owner = smp_processor_id();
2420 static void ieee80211_set_multicast_list(struct net_device *dev)
2422 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2423 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2424 unsigned short flags;
2426 netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
2427 if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
2428 if (sdata->allmulti) {
2429 sdata->allmulti = 0;
2430 local->iff_allmultis--;
2432 sdata->allmulti = 1;
2433 local->iff_allmultis++;
2436 if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
2437 if (sdata->promisc) {
2439 local->iff_promiscs--;
2442 local->iff_promiscs++;
2445 if (dev->mc_count != sdata->mc_count) {
2446 local->mc_count = local->mc_count - sdata->mc_count +
2448 sdata->mc_count = dev->mc_count;
2450 if (local->ops->set_multicast_list) {
2451 flags = local->mdev->flags;
2452 if (local->iff_allmultis)
2453 flags |= IFF_ALLMULTI;
2454 if (local->iff_promiscs)
2455 flags |= IFF_PROMISC;
2456 read_lock(&local->sub_if_lock);
2457 local->ops->set_multicast_list(local_to_hw(local), flags,
2459 read_unlock(&local->sub_if_lock);
2461 netif_tx_unlock(local->mdev);
2464 struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
2465 struct dev_mc_list *prev,
2468 struct ieee80211_local *local = hw_to_local(hw);
2469 struct ieee80211_sub_if_data *sdata = *ptr;
2470 struct dev_mc_list *mc;
2476 if (!prev || !prev->next) {
2478 sdata = list_entry(sdata->list.next,
2479 struct ieee80211_sub_if_data, list);
2481 sdata = list_entry(local->sub_if_list.next,
2482 struct ieee80211_sub_if_data, list);
2483 if (&sdata->list != &local->sub_if_list)
2484 mc = sdata->dev->mc_list;
2493 EXPORT_SYMBOL(ieee80211_get_mc_list_item);
2495 static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
2497 struct ieee80211_sub_if_data *sdata;
2498 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2499 return &(sdata->stats);
2502 static void ieee80211_if_shutdown(struct net_device *dev)
2504 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2505 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2508 switch (sdata->type) {
2509 case IEEE80211_IF_TYPE_STA:
2510 case IEEE80211_IF_TYPE_IBSS:
2511 sdata->u.sta.state = IEEE80211_DISABLED;
2512 del_timer_sync(&sdata->u.sta.timer);
2513 skb_queue_purge(&sdata->u.sta.skb_queue);
2514 if (!local->ops->hw_scan &&
2515 local->scan_dev == sdata->dev) {
2516 local->sta_scanning = 0;
2517 cancel_delayed_work(&local->scan_work);
2519 flush_workqueue(local->hw.workqueue);
2524 static inline int identical_mac_addr_allowed(int type1, int type2)
2526 return (type1 == IEEE80211_IF_TYPE_MNTR ||
2527 type2 == IEEE80211_IF_TYPE_MNTR ||
2528 (type1 == IEEE80211_IF_TYPE_AP &&
2529 type2 == IEEE80211_IF_TYPE_WDS) ||
2530 (type1 == IEEE80211_IF_TYPE_WDS &&
2531 (type2 == IEEE80211_IF_TYPE_WDS ||
2532 type2 == IEEE80211_IF_TYPE_AP)) ||
2533 (type1 == IEEE80211_IF_TYPE_AP &&
2534 type2 == IEEE80211_IF_TYPE_VLAN) ||
2535 (type1 == IEEE80211_IF_TYPE_VLAN &&
2536 (type2 == IEEE80211_IF_TYPE_AP ||
2537 type2 == IEEE80211_IF_TYPE_VLAN)));
2540 static int ieee80211_master_open(struct net_device *dev)
2542 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2543 struct ieee80211_sub_if_data *sdata;
2544 int res = -EOPNOTSUPP;
2546 read_lock(&local->sub_if_lock);
2547 list_for_each_entry(sdata, &local->sub_if_list, list) {
2548 if (sdata->dev != dev && netif_running(sdata->dev)) {
2553 read_unlock(&local->sub_if_lock);
2557 static int ieee80211_master_stop(struct net_device *dev)
2559 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2560 struct ieee80211_sub_if_data *sdata;
2562 read_lock(&local->sub_if_lock);
2563 list_for_each_entry(sdata, &local->sub_if_list, list)
2564 if (sdata->dev != dev && netif_running(sdata->dev))
2565 dev_close(sdata->dev);
2566 read_unlock(&local->sub_if_lock);
2571 static int ieee80211_mgmt_open(struct net_device *dev)
2573 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2575 if (!netif_running(local->mdev))
2580 static int ieee80211_mgmt_stop(struct net_device *dev)
2585 /* Check if running monitor interfaces should go to a "soft monitor" mode
2586 * and switch them if necessary. */
2587 static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
2589 struct ieee80211_if_init_conf conf;
2591 if (local->open_count && local->open_count == local->monitors &&
2592 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2593 local->ops->remove_interface) {
2595 conf.type = IEEE80211_IF_TYPE_MNTR;
2596 conf.mac_addr = NULL;
2597 local->ops->remove_interface(local_to_hw(local), &conf);
2601 /* Check if running monitor interfaces should go to a "hard monitor" mode
2602 * and switch them if necessary. */
2603 static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
2605 struct ieee80211_if_init_conf conf;
2607 if (local->open_count && local->open_count == local->monitors &&
2608 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2609 local->ops->add_interface) {
2611 conf.type = IEEE80211_IF_TYPE_MNTR;
2612 conf.mac_addr = NULL;
2613 local->ops->add_interface(local_to_hw(local), &conf);
2617 static int ieee80211_open(struct net_device *dev)
2619 struct ieee80211_sub_if_data *sdata, *nsdata;
2620 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2621 struct ieee80211_if_init_conf conf;
2624 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2625 read_lock(&local->sub_if_lock);
2626 list_for_each_entry(nsdata, &local->sub_if_list, list) {
2627 struct net_device *ndev = nsdata->dev;
2629 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
2630 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
2631 !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
2632 read_unlock(&local->sub_if_lock);
2636 read_unlock(&local->sub_if_lock);
2638 if (sdata->type == IEEE80211_IF_TYPE_WDS &&
2639 is_zero_ether_addr(sdata->u.wds.remote_addr))
2642 if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
2643 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2644 /* run the interface in a "soft monitor" mode */
2646 local->open_count++;
2647 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
2650 ieee80211_start_soft_monitor(local);
2652 if (local->ops->add_interface) {
2653 conf.if_id = dev->ifindex;
2654 conf.type = sdata->type;
2655 conf.mac_addr = dev->dev_addr;
2656 res = local->ops->add_interface(local_to_hw(local), &conf);
2658 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
2659 ieee80211_start_hard_monitor(local);
2663 if (sdata->type != IEEE80211_IF_TYPE_STA)
2665 if (local->open_count > 0)
2669 if (local->open_count == 0) {
2671 tasklet_enable(&local->tx_pending_tasklet);
2672 tasklet_enable(&local->tasklet);
2673 if (local->ops->open)
2674 res = local->ops->open(local_to_hw(local));
2676 res = dev_open(local->mdev);
2678 if (local->ops->stop)
2679 local->ops->stop(local_to_hw(local));
2681 res = ieee80211_hw_config(local);
2682 if (res && local->ops->stop)
2683 local->ops->stop(local_to_hw(local));
2684 else if (!res && local->apdev)
2685 dev_open(local->apdev);
2689 if (local->ops->remove_interface)
2690 local->ops->remove_interface(local_to_hw(local),
2695 local->open_count++;
2697 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
2699 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
2701 ieee80211_if_config(dev);
2703 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2704 !local->user_space_mlme)
2705 netif_carrier_off(dev);
2707 netif_carrier_on(dev);
2709 netif_start_queue(dev);
2714 static int ieee80211_stop(struct net_device *dev)
2716 struct ieee80211_sub_if_data *sdata;
2717 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2719 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2721 if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
2722 local->open_count > 1 &&
2723 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2724 /* remove "soft monitor" interface */
2725 local->open_count--;
2727 if (!local->monitors)
2728 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
2732 netif_stop_queue(dev);
2733 ieee80211_if_shutdown(dev);
2735 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
2737 if (!local->monitors)
2738 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
2741 local->open_count--;
2742 if (local->open_count == 0) {
2743 if (netif_running(local->mdev))
2744 dev_close(local->mdev);
2746 dev_close(local->apdev);
2747 if (local->ops->stop)
2748 local->ops->stop(local_to_hw(local));
2749 tasklet_disable(&local->tx_pending_tasklet);
2750 tasklet_disable(&local->tasklet);
2752 if (local->ops->remove_interface) {
2753 struct ieee80211_if_init_conf conf;
2755 conf.if_id = dev->ifindex;
2756 conf.type = sdata->type;
2757 conf.mac_addr = dev->dev_addr;
2758 local->ops->remove_interface(local_to_hw(local), &conf);
2761 ieee80211_start_hard_monitor(local);
2767 static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
2769 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
2773 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
2775 return compare_ether_addr(raddr, addr) == 0 ||
2776 is_broadcast_ether_addr(raddr);
2780 static ieee80211_txrx_result
2781 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
2783 struct net_device *dev = rx->dev;
2784 struct ieee80211_local *local = rx->local;
2785 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
2786 u16 fc, hdrlen, ethertype;
2790 struct sk_buff *skb = rx->skb, *skb2;
2791 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2794 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
2795 return TXRX_CONTINUE;
2797 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
2800 hdrlen = ieee80211_get_hdrlen(fc);
2802 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
2804 * IEEE 802.11 address fields:
2805 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
2806 * 0 0 DA SA BSSID n/a
2807 * 0 1 DA BSSID SA n/a
2808 * 1 0 BSSID SA DA n/a
2812 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
2813 case IEEE80211_FCTL_TODS:
2815 memcpy(dst, hdr->addr3, ETH_ALEN);
2816 memcpy(src, hdr->addr2, ETH_ALEN);
2818 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
2819 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
2820 printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
2821 MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
2822 dev->name, MAC_ARG(hdr->addr1),
2823 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
2827 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
2829 memcpy(dst, hdr->addr3, ETH_ALEN);
2830 memcpy(src, hdr->addr4, ETH_ALEN);
2832 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
2833 printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
2834 MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
2836 rx->dev->name, MAC_ARG(hdr->addr1),
2837 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
2838 MAC_ARG(hdr->addr4));
2842 case IEEE80211_FCTL_FROMDS:
2844 memcpy(dst, hdr->addr1, ETH_ALEN);
2845 memcpy(src, hdr->addr3, ETH_ALEN);
2847 if (sdata->type != IEEE80211_IF_TYPE_STA) {
2853 memcpy(dst, hdr->addr1, ETH_ALEN);
2854 memcpy(src, hdr->addr2, ETH_ALEN);
2856 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
2857 if (net_ratelimit()) {
2858 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
2859 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
2861 dev->name, MAC_ARG(hdr->addr1),
2862 MAC_ARG(hdr->addr2),
2863 MAC_ARG(hdr->addr3));
2870 payload = skb->data + hdrlen;
2872 if (unlikely(skb->len - hdrlen < 8)) {
2873 if (net_ratelimit()) {
2874 printk(KERN_DEBUG "%s: RX too short data frame "
2875 "payload\n", dev->name);
2880 ethertype = (payload[6] << 8) | payload[7];
2882 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
2883 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
2884 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
2885 /* remove RFC1042 or Bridge-Tunnel encapsulation and
2886 * replace EtherType */
2887 skb_pull(skb, hdrlen + 6);
2888 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
2889 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
2891 struct ethhdr *ehdr;
2893 skb_pull(skb, hdrlen);
2894 len = htons(skb->len);
2895 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
2896 memcpy(ehdr->h_dest, dst, ETH_ALEN);
2897 memcpy(ehdr->h_source, src, ETH_ALEN);
2898 ehdr->h_proto = len;
2904 sdata->stats.rx_packets++;
2905 sdata->stats.rx_bytes += skb->len;
2907 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
2908 || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
2909 if (is_multicast_ether_addr(skb->data)) {
2910 /* send multicast frames both to higher layers in
2911 * local net stack and back to the wireless media */
2912 skb2 = skb_copy(skb, GFP_ATOMIC);
2914 printk(KERN_DEBUG "%s: failed to clone "
2915 "multicast frame\n", dev->name);
2917 struct sta_info *dsta;
2918 dsta = sta_info_get(local, skb->data);
2919 if (dsta && !dsta->dev) {
2920 printk(KERN_DEBUG "Station with null dev "
2922 } else if (dsta && dsta->dev == dev) {
2923 /* Destination station is associated to this
2924 * AP, so send the frame directly to it and
2925 * do not pass the frame to local net stack.
2936 /* deliver to local stack */
2937 skb->protocol = eth_type_trans(skb, dev);
2938 memset(skb->cb, 0, sizeof(skb->cb));
2943 /* send to wireless media */
2944 skb2->protocol = __constant_htons(ETH_P_802_3);
2945 skb_set_network_header(skb2, 0);
2946 skb_set_mac_header(skb2, 0);
2947 dev_queue_xmit(skb2);
2954 static struct ieee80211_rate *
2955 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
2957 struct ieee80211_hw_mode *mode;
2960 list_for_each_entry(mode, &local->modes_list, list) {
2961 if (mode->mode != phymode)
2963 for (r = 0; r < mode->num_rates; r++) {
2964 struct ieee80211_rate *rate = &mode->rates[r];
2965 if (rate->val == hw_rate ||
2966 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
2967 rate->val2 == hw_rate))
2976 ieee80211_fill_frame_info(struct ieee80211_local *local,
2977 struct ieee80211_frame_info *fi,
2978 struct ieee80211_rx_status *status)
2982 struct ieee80211_rate *rate;
2984 jiffies_to_timespec(jiffies, &ts);
2985 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
2987 fi->mactime = cpu_to_be64(status->mactime);
2988 switch (status->phymode) {
2989 case MODE_IEEE80211A:
2990 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
2992 case MODE_IEEE80211B:
2993 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
2995 case MODE_IEEE80211G:
2996 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
2998 case MODE_ATHEROS_TURBO:
3000 htonl(ieee80211_phytype_dsss_dot11_turbo);
3003 fi->phytype = htonl(0xAAAAAAAA);
3006 fi->channel = htonl(status->channel);
3007 rate = ieee80211_get_rate(local, status->phymode,
3010 fi->datarate = htonl(rate->rate);
3011 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
3012 if (status->rate == rate->val)
3013 fi->preamble = htonl(2); /* long */
3014 else if (status->rate == rate->val2)
3015 fi->preamble = htonl(1); /* short */
3017 fi->preamble = htonl(0);
3019 fi->datarate = htonl(0);
3020 fi->preamble = htonl(0);
3023 fi->antenna = htonl(status->antenna);
3024 fi->priority = htonl(0xffffffff); /* no clue */
3025 fi->ssi_type = htonl(ieee80211_ssi_raw);
3026 fi->ssi_signal = htonl(status->ssi);
3027 fi->ssi_noise = 0x00000000;
3030 /* clear everything because we really don't know.
3031 * the msg_type field isn't present on monitor frames
3032 * so we don't know whether it will be present or not,
3033 * but it's ok to not clear it since it'll be assigned
3035 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
3037 fi->ssi_type = htonl(ieee80211_ssi_none);
3039 fi->version = htonl(IEEE80211_FI_VERSION);
3040 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
3043 /* this routine is actually not just for this, but also
3044 * for pushing fake 'management' frames into userspace.
3045 * it shall be replaced by a netlink-based system. */
3047 ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
3048 struct ieee80211_rx_status *status, u32 msg_type)
3050 struct ieee80211_frame_info *fi;
3051 const size_t hlen = sizeof(struct ieee80211_frame_info);
3052 struct ieee80211_sub_if_data *sdata;
3054 skb->dev = local->apdev;
3056 sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
3058 if (skb_headroom(skb) < hlen) {
3059 I802_DEBUG_INC(local->rx_expand_skb_head);
3060 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
3066 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
3068 ieee80211_fill_frame_info(local, fi, status);
3069 fi->msg_type = htonl(msg_type);
3071 sdata->stats.rx_packets++;
3072 sdata->stats.rx_bytes += skb->len;
3074 skb_set_mac_header(skb, 0);
3075 skb->ip_summed = CHECKSUM_UNNECESSARY;
3076 skb->pkt_type = PACKET_OTHERHOST;
3077 skb->protocol = htons(ETH_P_802_2);
3078 memset(skb->cb, 0, sizeof(skb->cb));
3083 ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
3084 struct ieee80211_rx_status *status)
3086 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3087 struct ieee80211_sub_if_data *sdata;
3088 struct ieee80211_rate *rate;
3089 struct ieee80211_rtap_hdr {
3090 struct ieee80211_radiotap_header hdr;
3096 } __attribute__ ((packed)) *rthdr;
3100 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3102 if (status->flag & RX_FLAG_RADIOTAP)
3105 if (skb_headroom(skb) < sizeof(*rthdr)) {
3106 I802_DEBUG_INC(local->rx_expand_skb_head);
3107 if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
3113 rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
3114 memset(rthdr, 0, sizeof(*rthdr));
3115 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
3116 rthdr->hdr.it_present =
3117 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
3118 (1 << IEEE80211_RADIOTAP_RATE) |
3119 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3120 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
3121 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
3122 IEEE80211_RADIOTAP_F_FCS : 0;
3123 rate = ieee80211_get_rate(local, status->phymode, status->rate);
3125 rthdr->rate = rate->rate / 5;
3126 rthdr->chan_freq = cpu_to_le16(status->freq);
3128 status->phymode == MODE_IEEE80211A ?
3129 cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
3130 cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
3131 rthdr->antsignal = status->ssi;
3134 sdata->stats.rx_packets++;
3135 sdata->stats.rx_bytes += skb->len;
3137 skb_set_mac_header(skb, 0);
3138 skb->ip_summed = CHECKSUM_UNNECESSARY;
3139 skb->pkt_type = PACKET_OTHERHOST;
3140 skb->protocol = htons(ETH_P_802_2);
3141 memset(skb->cb, 0, sizeof(skb->cb));
3145 int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
3146 int radar, int radar_type)
3148 struct sk_buff *skb;
3149 struct ieee80211_radar_info *msg;
3150 struct ieee80211_local *local = hw_to_local(hw);
3155 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
3156 sizeof(struct ieee80211_radar_info));
3160 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
3162 msg = (struct ieee80211_radar_info *)
3163 skb_put(skb, sizeof(struct ieee80211_radar_info));
3164 msg->channel = channel;
3166 msg->radar_type = radar_type;
3168 ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
3171 EXPORT_SYMBOL(ieee80211_radar_status);
3174 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
3176 struct ieee80211_sub_if_data *sdata;
3177 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
3180 atomic_inc(&sdata->bss->num_sta_ps);
3181 sta->flags |= WLAN_STA_PS;
3183 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3184 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
3185 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
3186 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3190 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
3192 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3193 struct sk_buff *skb;
3195 struct ieee80211_sub_if_data *sdata;
3196 struct ieee80211_tx_packet_data *pkt_data;
3198 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
3200 atomic_dec(&sdata->bss->num_sta_ps);
3201 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
3203 if (!skb_queue_empty(&sta->ps_tx_buf)) {
3204 if (local->ops->set_tim)
3205 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
3207 bss_tim_clear(local, sdata->bss, sta->aid);
3209 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3210 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
3211 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
3212 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3213 /* Send all buffered frames to the station */
3214 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
3215 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
3217 pkt_data->requeue = 1;
3218 dev_queue_xmit(skb);
3220 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
3221 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
3222 local->total_ps_buffered--;
3224 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3225 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
3226 "since STA not sleeping anymore\n", dev->name,
3227 MAC_ARG(sta->addr), sta->aid);
3228 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3229 pkt_data->requeue = 1;
3230 dev_queue_xmit(skb);
3237 static ieee80211_txrx_result
3238 ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
3240 struct sk_buff *skb;
3241 int no_pending_pkts;
3243 if (likely(!rx->sta ||
3244 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
3245 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
3246 !rx->u.rx.ra_match))
3247 return TXRX_CONTINUE;
3249 skb = skb_dequeue(&rx->sta->tx_filtered);
3251 skb = skb_dequeue(&rx->sta->ps_tx_buf);
3253 rx->local->total_ps_buffered--;
3255 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
3256 skb_queue_empty(&rx->sta->ps_tx_buf);
3259 struct ieee80211_hdr *hdr =
3260 (struct ieee80211_hdr *) skb->data;
3262 /* tell TX path to send one frame even though the STA may
3263 * still remain is PS mode after this frame exchange */
3264 rx->sta->pspoll = 1;
3266 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3267 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
3269 MAC_ARG(rx->sta->addr), rx->sta->aid,
3270 skb_queue_len(&rx->sta->ps_tx_buf));
3271 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3273 /* Use MoreData flag to indicate whether there are more
3274 * buffered frames for this STA */
3275 if (no_pending_pkts) {
3276 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
3277 rx->sta->flags &= ~WLAN_STA_TIM;
3279 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
3281 dev_queue_xmit(skb);
3283 if (no_pending_pkts) {
3284 if (rx->local->ops->set_tim)
3285 rx->local->ops->set_tim(local_to_hw(rx->local),
3288 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
3290 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3291 } else if (!rx->u.rx.sent_ps_buffered) {
3292 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
3293 "though there is no buffered frames for it\n",
3294 rx->dev->name, MAC_ARG(rx->sta->addr));
3295 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3299 /* Free PS Poll skb here instead of returning TXRX_DROP that would
3300 * count as an dropped frame. */
3301 dev_kfree_skb(rx->skb);
3307 static inline struct ieee80211_fragment_entry *
3308 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
3309 unsigned int frag, unsigned int seq, int rx_queue,
3310 struct sk_buff **skb)
3312 struct ieee80211_fragment_entry *entry;
3315 idx = sdata->fragment_next;
3316 entry = &sdata->fragments[sdata->fragment_next++];
3317 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
3318 sdata->fragment_next = 0;
3320 if (!skb_queue_empty(&entry->skb_list)) {
3321 #ifdef CONFIG_MAC80211_DEBUG
3322 struct ieee80211_hdr *hdr =
3323 (struct ieee80211_hdr *) entry->skb_list.next->data;
3324 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
3325 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
3326 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
3327 sdata->dev->name, idx,
3328 jiffies - entry->first_frag_time, entry->seq,
3329 entry->last_frag, MAC_ARG(hdr->addr1),
3330 MAC_ARG(hdr->addr2));
3331 #endif /* CONFIG_MAC80211_DEBUG */
3332 __skb_queue_purge(&entry->skb_list);
3335 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
3337 entry->first_frag_time = jiffies;
3339 entry->rx_queue = rx_queue;
3340 entry->last_frag = frag;
3342 entry->extra_len = 0;
3348 static inline struct ieee80211_fragment_entry *
3349 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
3350 u16 fc, unsigned int frag, unsigned int seq,
3351 int rx_queue, struct ieee80211_hdr *hdr)
3353 struct ieee80211_fragment_entry *entry;
3356 idx = sdata->fragment_next;
3357 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
3358 struct ieee80211_hdr *f_hdr;
3363 idx = IEEE80211_FRAGMENT_MAX - 1;
3365 entry = &sdata->fragments[idx];
3366 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
3367 entry->rx_queue != rx_queue ||
3368 entry->last_frag + 1 != frag)
3371 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
3372 f_fc = le16_to_cpu(f_hdr->frame_control);
3374 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
3375 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
3376 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
3379 if (entry->first_frag_time + 2 * HZ < jiffies) {
3380 __skb_queue_purge(&entry->skb_list);
3390 static ieee80211_txrx_result
3391 ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
3393 struct ieee80211_hdr *hdr;
3395 unsigned int frag, seq;
3396 struct ieee80211_fragment_entry *entry;
3397 struct sk_buff *skb;
3399 hdr = (struct ieee80211_hdr *) rx->skb->data;
3400 sc = le16_to_cpu(hdr->seq_ctrl);
3401 frag = sc & IEEE80211_SCTL_FRAG;
3403 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
3404 (rx->skb)->len < 24 ||
3405 is_multicast_ether_addr(hdr->addr1))) {
3406 /* not fragmented */
3409 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
3411 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3414 /* This is the first fragment of a new frame. */
3415 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
3416 rx->u.rx.queue, &(rx->skb));
3417 if (rx->key && rx->key->alg == ALG_CCMP &&
3418 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
3419 /* Store CCMP PN so that we can verify that the next
3420 * fragment has a sequential PN value. */
3422 memcpy(entry->last_pn,
3423 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
3429 /* This is a fragment for a frame that should already be pending in
3430 * fragment cache. Add this fragment to the end of the pending entry.
3432 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
3433 rx->u.rx.queue, hdr);
3435 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
3439 /* Verify that MPDUs within one MSDU have sequential PN values.
3440 * (IEEE 802.11i, 8.3.3.4.5) */
3443 u8 pn[CCMP_PN_LEN], *rpn;
3444 if (!rx->key || rx->key->alg != ALG_CCMP)
3446 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
3447 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
3452 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
3453 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
3454 printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
3455 " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
3456 "(expected %02x%02x%02x%02x%02x%02x)\n",
3457 rx->dev->name, MAC_ARG(hdr->addr2),
3458 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
3459 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
3462 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
3465 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
3466 __skb_queue_tail(&entry->skb_list, rx->skb);
3467 entry->last_frag = frag;
3468 entry->extra_len += rx->skb->len;
3469 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
3474 rx->skb = __skb_dequeue(&entry->skb_list);
3475 if (skb_tailroom(rx->skb) < entry->extra_len) {
3476 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
3477 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
3479 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
3480 __skb_queue_purge(&entry->skb_list);
3484 while ((skb = __skb_dequeue(&entry->skb_list))) {
3485 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
3489 /* Complete frame has been reassembled - process it now */
3494 rx->sta->rx_packets++;
3495 if (is_multicast_ether_addr(hdr->addr1))
3496 rx->local->dot11MulticastReceivedFrameCount++;
3498 ieee80211_led_rx(rx->local);
3499 return TXRX_CONTINUE;
3503 static ieee80211_txrx_result
3504 ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
3506 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
3507 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
3511 if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
3512 skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb));
3514 return TXRX_CONTINUE;
3518 static ieee80211_txrx_result
3519 ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
3521 struct ieee80211_hdr *hdr;
3523 hdr = (struct ieee80211_hdr *) rx->skb->data;
3525 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
3526 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
3527 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
3528 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
3530 if (rx->u.rx.ra_match) {
3531 rx->local->dot11FrameDuplicateCount++;
3532 rx->sta->num_duplicates++;
3536 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
3539 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
3540 rx->skb->len > FCS_LEN)
3541 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
3543 if (unlikely(rx->skb->len < 16)) {
3544 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
3548 if (!rx->u.rx.ra_match)
3549 rx->skb->pkt_type = PACKET_OTHERHOST;
3550 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
3551 rx->skb->pkt_type = PACKET_HOST;
3552 else if (is_multicast_ether_addr(hdr->addr1)) {
3553 if (is_broadcast_ether_addr(hdr->addr1))
3554 rx->skb->pkt_type = PACKET_BROADCAST;
3556 rx->skb->pkt_type = PACKET_MULTICAST;
3558 rx->skb->pkt_type = PACKET_OTHERHOST;
3560 /* Drop disallowed frame classes based on STA auth/assoc state;
3561 * IEEE 802.11, Chap 5.5.
3563 * 80211.o does filtering only based on association state, i.e., it
3564 * drops Class 3 frames from not associated stations. hostapd sends
3565 * deauth/disassoc frames when needed. In addition, hostapd is
3566 * responsible for filtering on both auth and assoc states.
3568 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
3569 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
3570 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
3571 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
3572 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
3573 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
3574 !(rx->fc & IEEE80211_FCTL_TODS) &&
3575 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
3576 || !rx->u.rx.ra_match) {
3577 /* Drop IBSS frames and frames for other hosts
3582 if (!rx->local->apdev)
3585 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3586 ieee80211_msg_sta_not_assoc);
3590 if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
3595 if (rx->sta && rx->sta->key && always_sta_key) {
3596 rx->key = rx->sta->key;
3598 if (rx->sta && rx->sta->key)
3599 rx->key = rx->sta->key;
3601 rx->key = rx->sdata->default_key;
3603 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3604 rx->fc & IEEE80211_FCTL_PROTECTED) {
3605 int keyidx = ieee80211_wep_get_keyidx(rx->skb);
3607 if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
3608 (!rx->sta || !rx->sta->key || keyidx > 0))
3609 rx->key = rx->sdata->keys[keyidx];
3612 if (!rx->u.rx.ra_match)
3614 printk(KERN_DEBUG "%s: RX WEP frame with "
3615 "unknown keyidx %d (A1=" MAC_FMT " A2="
3616 MAC_FMT " A3=" MAC_FMT ")\n",
3617 rx->dev->name, keyidx,
3618 MAC_ARG(hdr->addr1),
3619 MAC_ARG(hdr->addr2),
3620 MAC_ARG(hdr->addr3));
3621 if (!rx->local->apdev)
3624 rx->local, rx->skb, rx->u.rx.status,
3625 ieee80211_msg_wep_frame_unknown_key);
3631 if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
3632 rx->key->tx_rx_count++;
3633 if (unlikely(rx->local->key_tx_rx_threshold &&
3634 rx->key->tx_rx_count >
3635 rx->local->key_tx_rx_threshold)) {
3636 ieee80211_key_threshold_notify(rx->dev, rx->key,
3641 return TXRX_CONTINUE;
3645 static ieee80211_txrx_result
3646 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
3648 struct sta_info *sta = rx->sta;
3649 struct net_device *dev = rx->dev;
3650 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3653 return TXRX_CONTINUE;
3655 /* Update last_rx only for IBSS packets which are for the current
3656 * BSSID to avoid keeping the current IBSS network alive in cases where
3657 * other STAs are using different BSSID. */
3658 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
3659 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
3660 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
3661 sta->last_rx = jiffies;
3663 if (!is_multicast_ether_addr(hdr->addr1) ||
3664 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
3665 /* Update last_rx only for unicast frames in order to prevent
3666 * the Probe Request frames (the only broadcast frames from a
3667 * STA in infrastructure mode) from keeping a connection alive.
3669 sta->last_rx = jiffies;
3672 if (!rx->u.rx.ra_match)
3673 return TXRX_CONTINUE;
3675 sta->rx_fragments++;
3676 sta->rx_bytes += rx->skb->len;
3677 sta->last_rssi = (sta->last_rssi * 15 +
3678 rx->u.rx.status->ssi) / 16;
3679 sta->last_signal = (sta->last_signal * 15 +
3680 rx->u.rx.status->signal) / 16;
3681 sta->last_noise = (sta->last_noise * 15 +
3682 rx->u.rx.status->noise) / 16;
3684 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
3685 /* Change STA power saving mode only in the end of a frame
3686 * exchange sequence */
3687 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
3688 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
3689 else if (!(sta->flags & WLAN_STA_PS) &&
3690 (rx->fc & IEEE80211_FCTL_PM))
3691 ap_sta_ps_start(dev, sta);
3694 /* Drop data::nullfunc frames silently, since they are used only to
3695 * control station power saving mode. */
3696 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3697 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
3698 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
3699 /* Update counter and free packet here to avoid counting this
3700 * as a dropped packed. */
3702 dev_kfree_skb(rx->skb);
3706 return TXRX_CONTINUE;
3707 } /* ieee80211_rx_h_sta_process */
3710 static ieee80211_txrx_result
3711 ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
3713 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3714 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
3715 !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
3716 return TXRX_CONTINUE;
3718 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
3719 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
3720 rx->key->force_sw_encrypt) {
3721 u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
3723 rx->sta->wep_weak_iv_count++;
3727 return TXRX_CONTINUE;
3731 static ieee80211_txrx_result
3732 ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
3734 /* If the device handles decryption totally, skip this test */
3735 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3736 return TXRX_CONTINUE;
3738 if ((rx->key && rx->key->alg != ALG_WEP) ||
3739 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3740 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3741 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3742 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
3743 return TXRX_CONTINUE;
3746 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
3751 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
3752 rx->key->force_sw_encrypt) {
3753 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
3754 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
3755 "failed\n", rx->dev->name);
3758 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
3759 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
3761 skb_trim(rx->skb, rx->skb->len - 4);
3764 return TXRX_CONTINUE;
3768 static ieee80211_txrx_result
3769 ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
3771 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
3772 rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
3773 /* Pass both encrypted and unencrypted EAPOL frames to user
3774 * space for processing. */
3775 if (!rx->local->apdev)
3777 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3778 ieee80211_msg_normal);
3782 if (unlikely(rx->sdata->ieee802_1x &&
3783 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3784 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3785 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
3786 !ieee80211_is_eapol(rx->skb))) {
3787 #ifdef CONFIG_MAC80211_DEBUG
3788 struct ieee80211_hdr *hdr =
3789 (struct ieee80211_hdr *) rx->skb->data;
3790 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
3791 " (unauthorized port)\n", rx->dev->name,
3792 MAC_ARG(hdr->addr2));
3793 #endif /* CONFIG_MAC80211_DEBUG */
3797 return TXRX_CONTINUE;
3801 static ieee80211_txrx_result
3802 ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
3804 /* If the device handles decryption totally, skip this test */
3805 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3806 return TXRX_CONTINUE;
3808 /* Drop unencrypted frames if key is set. */
3809 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
3810 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3811 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3812 (rx->key || rx->sdata->drop_unencrypted) &&
3813 (rx->sdata->eapol == 0 ||
3814 !ieee80211_is_eapol(rx->skb)))) {
3815 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
3816 "encryption\n", rx->dev->name);
3819 return TXRX_CONTINUE;
3823 static ieee80211_txrx_result
3824 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
3826 struct ieee80211_sub_if_data *sdata;
3828 if (!rx->u.rx.ra_match)
3831 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
3832 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
3833 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
3834 !rx->local->user_space_mlme) {
3835 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
3837 /* Management frames are sent to hostapd for processing */
3838 if (!rx->local->apdev)
3840 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3841 ieee80211_msg_normal);
3847 static ieee80211_txrx_result
3848 ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
3850 struct ieee80211_local *local = rx->local;
3851 struct sk_buff *skb = rx->skb;
3853 if (unlikely(local->sta_scanning != 0)) {
3854 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
3858 if (unlikely(rx->u.rx.in_scan)) {
3859 /* scanning finished during invoking of handlers */
3860 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
3864 return TXRX_CONTINUE;
3868 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
3869 struct ieee80211_hdr *hdr,
3870 struct sta_info *sta,
3871 struct ieee80211_txrx_data *rx)
3875 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
3876 if (rx->skb->len >= hdrlen + 4)
3877 keyidx = rx->skb->data[hdrlen + 3] >> 6;
3881 /* TODO: verify that this is not triggered by fragmented
3882 * frames (hw does not verify MIC for them). */
3883 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
3884 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
3885 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
3888 /* Some hardware versions seem to generate incorrect
3889 * Michael MIC reports; ignore them to avoid triggering
3890 * countermeasures. */
3891 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3892 "error for unknown address " MAC_FMT "\n",
3893 dev->name, MAC_ARG(hdr->addr2));
3897 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
3898 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3899 "error for a frame with no ISWEP flag (src "
3900 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
3904 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3905 rx->sdata->type == IEEE80211_IF_TYPE_AP) {
3906 keyidx = ieee80211_wep_get_keyidx(rx->skb);
3907 /* AP with Pairwise keys support should never receive Michael
3908 * MIC errors for non-zero keyidx because these are reserved
3909 * for group keys and only the AP is sending real multicast
3912 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
3913 "a frame with non-zero keyidx (%d) (src " MAC_FMT
3914 ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
3919 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3920 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3921 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
3922 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3923 "error for a frame that cannot be encrypted "
3924 "(fc=0x%04x) (src " MAC_FMT ")\n",
3925 dev->name, rx->fc, MAC_ARG(hdr->addr2));
3930 union iwreq_data wrqu;
3931 char *buf = kmalloc(128, GFP_ATOMIC);
3935 /* TODO: needed parameters: count, key type, TSC */
3936 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
3937 "keyid=%d %scast addr=" MAC_FMT ")",
3938 keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
3939 MAC_ARG(hdr->addr2));
3940 memset(&wrqu, 0, sizeof(wrqu));
3941 wrqu.data.length = strlen(buf);
3942 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
3946 /* TODO: consider verifying the MIC error report with software
3947 * implementation if we get too many spurious reports from the
3949 if (!rx->local->apdev)
3951 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3952 ieee80211_msg_michael_mic_failure);
3956 dev_kfree_skb(rx->skb);
3960 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
3961 struct ieee80211_local *local,
3962 ieee80211_rx_handler *handlers,
3963 struct ieee80211_txrx_data *rx,
3964 struct sta_info *sta)
3966 ieee80211_rx_handler *handler;
3967 ieee80211_txrx_result res = TXRX_DROP;
3969 for (handler = handlers; *handler != NULL; handler++) {
3970 res = (*handler)(rx);
3971 if (res != TXRX_CONTINUE) {
3972 if (res == TXRX_DROP) {
3973 I802_DEBUG_INC(local->rx_handlers_drop);
3977 if (res == TXRX_QUEUED)
3978 I802_DEBUG_INC(local->rx_handlers_queued);
3983 if (res == TXRX_DROP) {
3984 dev_kfree_skb(rx->skb);
3989 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
3990 ieee80211_rx_handler *handlers,
3991 struct ieee80211_txrx_data *rx,
3992 struct sta_info *sta)
3994 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
3996 dev_kfree_skb(rx->skb);
4000 * This is the receive path handler. It is called by a low level driver when an
4001 * 802.11 MPDU is received from the hardware.
4003 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
4004 struct ieee80211_rx_status *status)
4006 struct ieee80211_local *local = hw_to_local(hw);
4007 struct ieee80211_sub_if_data *sdata;
4008 struct sta_info *sta;
4009 struct ieee80211_hdr *hdr;
4010 struct ieee80211_txrx_data rx;
4013 int radiotap_len = 0;
4015 if (status->flag & RX_FLAG_RADIOTAP) {
4016 radiotap_len = ieee80211_get_radiotap_len(skb);
4017 skb_pull(skb, radiotap_len);
4020 hdr = (struct ieee80211_hdr *) skb->data;
4021 memset(&rx, 0, sizeof(rx));
4025 rx.u.rx.status = status;
4026 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
4027 type = rx.fc & IEEE80211_FCTL_FTYPE;
4028 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
4029 local->dot11ReceivedFragmentCount++;
4030 multicast = is_multicast_ether_addr(hdr->addr1);
4033 sta = rx.sta = sta_info_get(local, hdr->addr2);
4035 sta = rx.sta = NULL;
4039 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
4042 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
4043 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
4047 if (unlikely(local->sta_scanning))
4048 rx.u.rx.in_scan = 1;
4050 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
4051 sta) != TXRX_CONTINUE)
4055 skb_push(skb, radiotap_len);
4056 if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
4057 !local->iff_promiscs && !multicast) {
4058 rx.u.rx.ra_match = 1;
4059 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
4062 struct ieee80211_sub_if_data *prev = NULL;
4063 struct sk_buff *skb_new;
4064 u8 *bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
4066 read_lock(&local->sub_if_lock);
4067 list_for_each_entry(sdata, &local->sub_if_list, list) {
4068 rx.u.rx.ra_match = 1;
4069 switch (sdata->type) {
4070 case IEEE80211_IF_TYPE_STA:
4073 if (!ieee80211_bssid_match(bssid,
4074 sdata->u.sta.bssid)) {
4075 if (!rx.u.rx.in_scan)
4077 rx.u.rx.ra_match = 0;
4078 } else if (!multicast &&
4079 compare_ether_addr(sdata->dev->dev_addr,
4081 if (!sdata->promisc)
4083 rx.u.rx.ra_match = 0;
4086 case IEEE80211_IF_TYPE_IBSS:
4089 if (!ieee80211_bssid_match(bssid,
4090 sdata->u.sta.bssid)) {
4091 if (!rx.u.rx.in_scan)
4093 rx.u.rx.ra_match = 0;
4094 } else if (!multicast &&
4095 compare_ether_addr(sdata->dev->dev_addr,
4097 if (!sdata->promisc)
4099 rx.u.rx.ra_match = 0;
4102 ieee80211_ibss_add_sta(sdata->dev,
4106 case IEEE80211_IF_TYPE_AP:
4108 if (compare_ether_addr(sdata->dev->dev_addr,
4111 } else if (!ieee80211_bssid_match(bssid,
4112 sdata->dev->dev_addr)) {
4113 if (!rx.u.rx.in_scan)
4115 rx.u.rx.ra_match = 0;
4117 if (sdata->dev == local->mdev &&
4119 /* do not receive anything via
4120 * master device when not scanning */
4123 case IEEE80211_IF_TYPE_WDS:
4125 (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
4127 if (compare_ether_addr(sdata->u.wds.remote_addr,
4134 skb_new = skb_copy(skb, GFP_ATOMIC);
4136 if (net_ratelimit())
4137 printk(KERN_DEBUG "%s: failed to copy "
4138 "multicast frame for %s",
4139 local->mdev->name, prev->dev->name);
4145 ieee80211_invoke_rx_handlers(local,
4155 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
4159 read_unlock(&local->sub_if_lock);
4166 EXPORT_SYMBOL(__ieee80211_rx);
4168 static ieee80211_txrx_result
4169 ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
4171 struct ieee80211_local *local = tx->local;
4172 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
4173 struct sk_buff *skb = tx->skb;
4174 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4175 u32 load = 0, hdrtime;
4177 /* TODO: this could be part of tx_status handling, so that the number
4178 * of retries would be known; TX rate should in that case be stored
4179 * somewhere with the packet */
4181 /* Estimate total channel use caused by this frame */
4183 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
4184 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
4186 if (mode->mode == MODE_IEEE80211A ||
4187 mode->mode == MODE_ATHEROS_TURBO ||
4188 mode->mode == MODE_ATHEROS_TURBOG ||
4189 (mode->mode == MODE_IEEE80211G &&
4190 tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
4191 hdrtime = CHAN_UTIL_HDR_SHORT;
4193 hdrtime = CHAN_UTIL_HDR_LONG;
4196 if (!is_multicast_ether_addr(hdr->addr1))
4199 if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
4200 load += 2 * hdrtime;
4201 else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
4204 load += skb->len * tx->u.tx.rate->rate_inv;
4206 if (tx->u.tx.extra_frag) {
4208 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
4209 load += 2 * hdrtime;
4210 load += tx->u.tx.extra_frag[i]->len *
4211 tx->u.tx.rate->rate;
4215 /* Divide channel_use by 8 to avoid wrapping around the counter */
4216 load >>= CHAN_UTIL_SHIFT;
4217 local->channel_use_raw += load;
4219 tx->sta->channel_use_raw += load;
4220 tx->sdata->channel_use_raw += load;
4222 return TXRX_CONTINUE;
4226 static ieee80211_txrx_result
4227 ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
4229 struct ieee80211_local *local = rx->local;
4230 struct sk_buff *skb = rx->skb;
4231 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4232 u32 load = 0, hdrtime;
4233 struct ieee80211_rate *rate;
4234 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
4237 /* Estimate total channel use caused by this frame */
4239 if (unlikely(mode->num_rates < 0))
4240 return TXRX_CONTINUE;
4242 rate = &mode->rates[0];
4243 for (i = 0; i < mode->num_rates; i++) {
4244 if (mode->rates[i].val == rx->u.rx.status->rate) {
4245 rate = &mode->rates[i];
4250 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
4251 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
4253 if (mode->mode == MODE_IEEE80211A ||
4254 mode->mode == MODE_ATHEROS_TURBO ||
4255 mode->mode == MODE_ATHEROS_TURBOG ||
4256 (mode->mode == MODE_IEEE80211G &&
4257 rate->flags & IEEE80211_RATE_ERP))
4258 hdrtime = CHAN_UTIL_HDR_SHORT;
4260 hdrtime = CHAN_UTIL_HDR_LONG;
4263 if (!is_multicast_ether_addr(hdr->addr1))
4266 load += skb->len * rate->rate_inv;
4268 /* Divide channel_use by 8 to avoid wrapping around the counter */
4269 load >>= CHAN_UTIL_SHIFT;
4270 local->channel_use_raw += load;
4272 rx->sta->channel_use_raw += load;
4273 rx->u.rx.load = load;
4275 return TXRX_CONTINUE;
4278 static ieee80211_txrx_result
4279 ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
4281 rx->sdata->channel_use_raw += rx->u.rx.load;
4282 return TXRX_CONTINUE;
4285 static void ieee80211_stat_refresh(unsigned long data)
4287 struct ieee80211_local *local = (struct ieee80211_local *) data;
4288 struct sta_info *sta;
4289 struct ieee80211_sub_if_data *sdata;
4291 if (!local->stat_time)
4294 /* go through all stations */
4295 spin_lock_bh(&local->sta_lock);
4296 list_for_each_entry(sta, &local->sta_list, list) {
4297 sta->channel_use = (sta->channel_use_raw / local->stat_time) /
4299 sta->channel_use_raw = 0;
4301 spin_unlock_bh(&local->sta_lock);
4303 /* go through all subinterfaces */
4304 read_lock(&local->sub_if_lock);
4305 list_for_each_entry(sdata, &local->sub_if_list, list) {
4306 sdata->channel_use = (sdata->channel_use_raw /
4307 local->stat_time) / CHAN_UTIL_PER_10MS;
4308 sdata->channel_use_raw = 0;
4310 read_unlock(&local->sub_if_lock);
4312 /* hardware interface */
4313 local->channel_use = (local->channel_use_raw /
4314 local->stat_time) / CHAN_UTIL_PER_10MS;
4315 local->channel_use_raw = 0;
4317 local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
4318 add_timer(&local->stat_timer);
4322 /* This is a version of the rx handler that can be called from hard irq
4323 * context. Post the skb on the queue and schedule the tasklet */
4324 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
4325 struct ieee80211_rx_status *status)
4327 struct ieee80211_local *local = hw_to_local(hw);
4329 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4331 skb->dev = local->mdev;
4332 /* copy status into skb->cb for use by tasklet */
4333 memcpy(skb->cb, status, sizeof(*status));
4334 skb->pkt_type = IEEE80211_RX_MSG;
4335 skb_queue_tail(&local->skb_queue, skb);
4336 tasklet_schedule(&local->tasklet);
4338 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
4340 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
4341 struct sk_buff *skb,
4342 struct ieee80211_tx_status *status)
4344 struct ieee80211_local *local = hw_to_local(hw);
4345 struct ieee80211_tx_status *saved;
4348 skb->dev = local->mdev;
4349 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
4350 if (unlikely(!saved)) {
4351 if (net_ratelimit())
4352 printk(KERN_WARNING "%s: Not enough memory, "
4353 "dropping tx status", skb->dev->name);
4354 /* should be dev_kfree_skb_irq, but due to this function being
4355 * named _irqsafe instead of just _irq we can't be sure that
4356 * people won't call it from non-irq contexts */
4357 dev_kfree_skb_any(skb);
4360 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
4361 /* copy pointer to saved status into skb->cb for use by tasklet */
4362 memcpy(skb->cb, &saved, sizeof(saved));
4364 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
4365 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
4366 &local->skb_queue : &local->skb_queue_unreliable, skb);
4367 tmp = skb_queue_len(&local->skb_queue) +
4368 skb_queue_len(&local->skb_queue_unreliable);
4369 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
4370 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
4371 memcpy(&saved, skb->cb, sizeof(saved));
4373 dev_kfree_skb_irq(skb);
4375 I802_DEBUG_INC(local->tx_status_drop);
4377 tasklet_schedule(&local->tasklet);
4379 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
4381 static void ieee80211_tasklet_handler(unsigned long data)
4383 struct ieee80211_local *local = (struct ieee80211_local *) data;
4384 struct sk_buff *skb;
4385 struct ieee80211_rx_status rx_status;
4386 struct ieee80211_tx_status *tx_status;
4388 while ((skb = skb_dequeue(&local->skb_queue)) ||
4389 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
4390 switch (skb->pkt_type) {
4391 case IEEE80211_RX_MSG:
4392 /* status is in skb->cb */
4393 memcpy(&rx_status, skb->cb, sizeof(rx_status));
4394 /* Clear skb->type in order to not confuse kernel
4397 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
4399 case IEEE80211_TX_STATUS_MSG:
4400 /* get pointer to saved status out of skb->cb */
4401 memcpy(&tx_status, skb->cb, sizeof(tx_status));
4403 ieee80211_tx_status(local_to_hw(local),
4407 default: /* should never get here! */
4408 printk(KERN_ERR "%s: Unknown message type (%d)\n",
4409 local->mdev->name, skb->pkt_type);
4417 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
4418 * make a prepared TX frame (one that has been given to hw) to look like brand
4419 * new IEEE 802.11 frame that is ready to go through TX processing again.
4420 * Also, tx_packet_data in cb is restored from tx_control. */
4421 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
4422 struct ieee80211_key *key,
4423 struct sk_buff *skb,
4424 struct ieee80211_tx_control *control)
4426 int hdrlen, iv_len, mic_len;
4427 struct ieee80211_tx_packet_data *pkt_data;
4429 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
4430 pkt_data->ifindex = control->ifindex;
4431 pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
4432 pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
4433 pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
4434 pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
4435 pkt_data->queue = control->queue;
4437 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
4444 iv_len = WEP_IV_LEN;
4445 mic_len = WEP_ICV_LEN;
4448 iv_len = TKIP_IV_LEN;
4449 mic_len = TKIP_ICV_LEN;
4452 iv_len = CCMP_HDR_LEN;
4453 mic_len = CCMP_MIC_LEN;
4459 if (skb->len >= mic_len && key->force_sw_encrypt)
4460 skb_trim(skb, skb->len - mic_len);
4461 if (skb->len >= iv_len && skb->len > hdrlen) {
4462 memmove(skb->data + iv_len, skb->data, hdrlen);
4463 skb_pull(skb, iv_len);
4468 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4469 u16 fc = le16_to_cpu(hdr->frame_control);
4470 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
4471 fc &= ~IEEE80211_STYPE_QOS_DATA;
4472 hdr->frame_control = cpu_to_le16(fc);
4473 memmove(skb->data + 2, skb->data, hdrlen - 2);
4480 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
4481 struct ieee80211_tx_status *status)
4483 struct sk_buff *skb2;
4484 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4485 struct ieee80211_local *local = hw_to_local(hw);
4488 struct ieee80211_tx_status_rtap_hdr *rthdr;
4489 struct ieee80211_sub_if_data *sdata;
4494 "%s: ieee80211_tx_status called with NULL status\n",
4500 if (status->excessive_retries) {
4501 struct sta_info *sta;
4502 sta = sta_info_get(local, hdr->addr1);
4504 if (sta->flags & WLAN_STA_PS) {
4505 /* The STA is in power save mode, so assume
4506 * that this TX packet failed because of that.
4508 status->excessive_retries = 0;
4509 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
4515 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
4516 struct sta_info *sta;
4517 sta = sta_info_get(local, hdr->addr1);
4519 sta->tx_filtered_count++;
4521 /* Clear the TX filter mask for this STA when sending
4522 * the next packet. If the STA went to power save mode,
4523 * this will happen when it is waking up for the next
4525 sta->clear_dst_mask = 1;
4527 /* TODO: Is the WLAN_STA_PS flag always set here or is
4528 * the race between RX and TX status causing some
4529 * packets to be filtered out before 80211.o gets an
4530 * update for PS status? This seems to be the case, so
4531 * no changes are likely to be needed. */
4532 if (sta->flags & WLAN_STA_PS &&
4533 skb_queue_len(&sta->tx_filtered) <
4534 STA_MAX_TX_BUFFER) {
4535 ieee80211_remove_tx_extra(local, sta->key,
4538 skb_queue_tail(&sta->tx_filtered, skb);
4539 } else if (!(sta->flags & WLAN_STA_PS) &&
4540 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
4541 /* Software retry the packet once */
4542 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
4543 ieee80211_remove_tx_extra(local, sta->key,
4546 dev_queue_xmit(skb);
4548 if (net_ratelimit()) {
4549 printk(KERN_DEBUG "%s: dropped TX "
4550 "filtered frame queue_len=%d "
4555 !!(sta->flags & WLAN_STA_PS),
4564 /* FIXME: STUPID to call this with both local and local->mdev */
4565 rate_control_tx_status(local, local->mdev, skb, status);
4568 ieee80211_led_tx(local, 0);
4571 * Fragments are passed to low-level drivers as separate skbs, so these
4572 * are actually fragments, not frames. Update frame counters only for
4573 * the first fragment of the frame. */
4575 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
4576 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
4578 if (status->flags & IEEE80211_TX_STATUS_ACK) {
4580 local->dot11TransmittedFrameCount++;
4581 if (is_multicast_ether_addr(hdr->addr1))
4582 local->dot11MulticastTransmittedFrameCount++;
4583 if (status->retry_count > 0)
4584 local->dot11RetryCount++;
4585 if (status->retry_count > 1)
4586 local->dot11MultipleRetryCount++;
4589 /* This counter shall be incremented for an acknowledged MPDU
4590 * with an individual address in the address 1 field or an MPDU
4591 * with a multicast address in the address 1 field of type Data
4593 if (!is_multicast_ether_addr(hdr->addr1) ||
4594 type == IEEE80211_FTYPE_DATA ||
4595 type == IEEE80211_FTYPE_MGMT)
4596 local->dot11TransmittedFragmentCount++;
4599 local->dot11FailedCount++;
4602 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
4603 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
4605 /* this was a transmitted frame, but now we want to reuse it */
4608 if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
4610 if (local->monitors) {
4611 skb2 = skb_clone(skb, GFP_ATOMIC);
4618 /* Send frame to hostapd */
4619 ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
4625 if (!local->monitors) {
4630 /* send frame to monitor interfaces now */
4632 if (skb_headroom(skb) < sizeof(*rthdr)) {
4633 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
4638 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
4639 skb_push(skb, sizeof(*rthdr));
4641 memset(rthdr, 0, sizeof(*rthdr));
4642 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
4643 rthdr->hdr.it_present =
4644 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
4645 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
4647 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
4648 !is_multicast_ether_addr(hdr->addr1))
4649 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
4651 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
4652 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
4653 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
4654 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
4655 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
4657 rthdr->data_retries = status->retry_count;
4659 read_lock(&local->sub_if_lock);
4660 monitors = local->monitors;
4661 list_for_each_entry(sdata, &local->sub_if_list, list) {
4663 * Using the monitors counter is possibly racy, but
4664 * if the value is wrong we simply either clone the skb
4665 * once too much or forget sending it to one monitor iface
4666 * The latter case isn't nice but fixing the race is much
4669 if (!monitors || !skb)
4672 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
4673 if (!netif_running(sdata->dev))
4677 skb2 = skb_clone(skb, GFP_KERNEL);
4680 skb->dev = sdata->dev;
4681 /* XXX: is this sufficient for BPF? */
4682 skb_set_mac_header(skb, 0);
4683 skb->ip_summed = CHECKSUM_UNNECESSARY;
4684 skb->pkt_type = PACKET_OTHERHOST;
4685 skb->protocol = htons(ETH_P_802_2);
4686 memset(skb->cb, 0, sizeof(skb->cb));
4693 read_unlock(&local->sub_if_lock);
4697 EXPORT_SYMBOL(ieee80211_tx_status);
4699 /* TODO: implement register/unregister functions for adding TX/RX handlers
4700 * into ordered list */
4702 /* rx_pre handlers don't have dev and sdata fields available in
4703 * ieee80211_txrx_data */
4704 static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
4706 ieee80211_rx_h_parse_qos,
4707 ieee80211_rx_h_load_stats,
4711 static ieee80211_rx_handler ieee80211_rx_handlers[] =
4713 ieee80211_rx_h_if_stats,
4714 ieee80211_rx_h_monitor,
4715 ieee80211_rx_h_passive_scan,
4716 ieee80211_rx_h_check,
4717 ieee80211_rx_h_sta_process,
4718 ieee80211_rx_h_ccmp_decrypt,
4719 ieee80211_rx_h_tkip_decrypt,
4720 ieee80211_rx_h_wep_weak_iv_detection,
4721 ieee80211_rx_h_wep_decrypt,
4722 ieee80211_rx_h_defragment,
4723 ieee80211_rx_h_ps_poll,
4724 ieee80211_rx_h_michael_mic_verify,
4725 /* this must be after decryption - so header is counted in MPDU mic
4726 * must be before pae and data, so QOS_DATA format frames
4727 * are not passed to user space by these functions
4729 ieee80211_rx_h_remove_qos_control,
4730 ieee80211_rx_h_802_1x_pae,
4731 ieee80211_rx_h_drop_unencrypted,
4732 ieee80211_rx_h_data,
4733 ieee80211_rx_h_mgmt,
4737 static ieee80211_tx_handler ieee80211_tx_handlers[] =
4739 ieee80211_tx_h_check_assoc,
4740 ieee80211_tx_h_sequence,
4741 ieee80211_tx_h_ps_buf,
4742 ieee80211_tx_h_select_key,
4743 ieee80211_tx_h_michael_mic_add,
4744 ieee80211_tx_h_fragment,
4745 ieee80211_tx_h_tkip_encrypt,
4746 ieee80211_tx_h_ccmp_encrypt,
4747 ieee80211_tx_h_wep_encrypt,
4748 ieee80211_tx_h_rate_ctrl,
4749 ieee80211_tx_h_misc,
4750 ieee80211_tx_h_load_stats,
4755 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
4757 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4758 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4759 struct sta_info *sta;
4761 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
4764 /* Create STA entry for the new peer */
4765 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
4770 /* Remove STA entry for the old peer */
4771 sta = sta_info_get(local, sdata->u.wds.remote_addr);
4774 sta_info_free(sta, 0);
4776 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
4777 "peer " MAC_FMT "\n",
4778 dev->name, MAC_ARG(sdata->u.wds.remote_addr));
4781 /* Update WDS link data */
4782 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
4787 /* Must not be called for mdev and apdev */
4788 void ieee80211_if_setup(struct net_device *dev)
4791 dev->hard_start_xmit = ieee80211_subif_start_xmit;
4792 dev->wireless_handlers = &ieee80211_iw_handler_def;
4793 dev->set_multicast_list = ieee80211_set_multicast_list;
4794 dev->change_mtu = ieee80211_change_mtu;
4795 dev->get_stats = ieee80211_get_stats;
4796 dev->open = ieee80211_open;
4797 dev->stop = ieee80211_stop;
4798 dev->uninit = ieee80211_if_reinit;
4799 dev->destructor = ieee80211_if_free;
4802 void ieee80211_if_mgmt_setup(struct net_device *dev)
4805 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
4806 dev->change_mtu = ieee80211_change_mtu_apdev;
4807 dev->get_stats = ieee80211_get_stats;
4808 dev->open = ieee80211_mgmt_open;
4809 dev->stop = ieee80211_mgmt_stop;
4810 dev->type = ARPHRD_IEEE80211_PRISM;
4811 dev->hard_header_parse = header_parse_80211;
4812 dev->uninit = ieee80211_if_reinit;
4813 dev->destructor = ieee80211_if_free;
4816 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
4819 struct rate_control_ref *ref, *old;
4822 if (local->open_count || netif_running(local->mdev) ||
4823 (local->apdev && netif_running(local->apdev)))
4826 ref = rate_control_alloc(name, local);
4828 printk(KERN_WARNING "%s: Failed to select rate control "
4829 "algorithm\n", local->mdev->name);
4833 old = local->rate_ctrl;
4834 local->rate_ctrl = ref;
4836 rate_control_put(old);
4837 sta_info_flush(local, NULL);
4840 printk(KERN_DEBUG "%s: Selected rate control "
4841 "algorithm '%s'\n", local->mdev->name,
4848 static void rate_control_deinitialize(struct ieee80211_local *local)
4850 struct rate_control_ref *ref;
4852 ref = local->rate_ctrl;
4853 local->rate_ctrl = NULL;
4854 rate_control_put(ref);
4857 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
4858 const struct ieee80211_ops *ops)
4860 struct net_device *mdev;
4861 struct ieee80211_local *local;
4862 struct ieee80211_sub_if_data *sdata;
4864 struct wiphy *wiphy;
4866 /* Ensure 32-byte alignment of our private data and hw private data.
4867 * We use the wiphy priv data for both our ieee80211_local and for
4868 * the driver's private data
4870 * In memory it'll be like this:
4872 * +-------------------------+
4874 * +-------------------------+
4875 * | struct ieee80211_local |
4876 * +-------------------------+
4877 * | driver's private data |
4878 * +-------------------------+
4881 priv_size = ((sizeof(struct ieee80211_local) +
4882 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
4885 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
4890 wiphy->privid = mac80211_wiphy_privid;
4892 local = wiphy_priv(wiphy);
4893 local->hw.wiphy = wiphy;
4895 local->hw.priv = (char *)local +
4896 ((sizeof(struct ieee80211_local) +
4897 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4901 /* for now, mdev needs sub_if_data :/ */
4902 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
4903 "wmaster%d", ether_setup);
4909 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
4910 mdev->ieee80211_ptr = &sdata->wdev;
4911 sdata->wdev.wiphy = wiphy;
4913 local->hw.queues = 1; /* default */
4916 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
4917 local->rx_handlers = ieee80211_rx_handlers;
4918 local->tx_handlers = ieee80211_tx_handlers;
4920 local->bridge_packets = 1;
4922 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
4923 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
4924 local->short_retry_limit = 7;
4925 local->long_retry_limit = 4;
4926 local->hw.conf.radio_enabled = 1;
4928 local->enabled_modes = (unsigned int) -1;
4930 INIT_LIST_HEAD(&local->modes_list);
4932 rwlock_init(&local->sub_if_lock);
4933 INIT_LIST_HEAD(&local->sub_if_list);
4935 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
4936 init_timer(&local->stat_timer);
4937 local->stat_timer.function = ieee80211_stat_refresh;
4938 local->stat_timer.data = (unsigned long) local;
4939 ieee80211_rx_bss_list_init(mdev);
4941 sta_info_init(local);
4943 mdev->hard_start_xmit = ieee80211_master_start_xmit;
4944 mdev->open = ieee80211_master_open;
4945 mdev->stop = ieee80211_master_stop;
4946 mdev->type = ARPHRD_IEEE80211;
4947 mdev->hard_header_parse = header_parse_80211;
4949 sdata->type = IEEE80211_IF_TYPE_AP;
4951 sdata->local = local;
4952 sdata->u.ap.force_unicast_rateidx = -1;
4953 sdata->u.ap.max_ratectrl_rateidx = -1;
4954 ieee80211_if_sdata_init(sdata);
4955 list_add_tail(&sdata->list, &local->sub_if_list);
4957 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
4958 (unsigned long)local);
4959 tasklet_disable(&local->tx_pending_tasklet);
4961 tasklet_init(&local->tasklet,
4962 ieee80211_tasklet_handler,
4963 (unsigned long) local);
4964 tasklet_disable(&local->tasklet);
4966 skb_queue_head_init(&local->skb_queue);
4967 skb_queue_head_init(&local->skb_queue_unreliable);
4969 return local_to_hw(local);
4971 EXPORT_SYMBOL(ieee80211_alloc_hw);
4973 int ieee80211_register_hw(struct ieee80211_hw *hw)
4975 struct ieee80211_local *local = hw_to_local(hw);
4979 result = wiphy_register(local->hw.wiphy);
4983 name = wiphy_dev(local->hw.wiphy)->driver->name;
4984 local->hw.workqueue = create_singlethread_workqueue(name);
4985 if (!local->hw.workqueue) {
4987 goto fail_workqueue;
4991 * The hardware needs headroom for sending the frame,
4992 * and we need some headroom for passing the frame to monitor
4993 * interfaces, but never both at the same time.
4995 local->tx_headroom = max(local->hw.extra_tx_headroom,
4996 sizeof(struct ieee80211_tx_status_rtap_hdr));
4998 debugfs_hw_add(local);
5000 local->hw.conf.beacon_int = 1000;
5002 local->wstats_flags |= local->hw.max_rssi ?
5003 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
5004 local->wstats_flags |= local->hw.max_signal ?
5005 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
5006 local->wstats_flags |= local->hw.max_noise ?
5007 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
5008 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
5009 local->wstats_flags |= IW_QUAL_DBM;
5011 result = sta_info_start(local);
5016 result = dev_alloc_name(local->mdev, local->mdev->name);
5020 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
5021 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
5023 result = register_netdevice(local->mdev);
5027 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
5029 result = ieee80211_init_rate_ctrl_alg(local, NULL);
5031 printk(KERN_DEBUG "%s: Failed to initialize rate control "
5032 "algorithm\n", local->mdev->name);
5036 result = ieee80211_wep_init(local);
5039 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
5044 ieee80211_install_qdisc(local->mdev);
5046 /* add one default STA interface */
5047 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
5048 IEEE80211_IF_TYPE_STA);
5050 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
5053 local->reg_state = IEEE80211_DEV_REGISTERED;
5056 ieee80211_led_init(local);
5061 rate_control_deinitialize(local);
5063 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
5064 unregister_netdevice(local->mdev);
5067 sta_info_stop(local);
5069 debugfs_hw_del(local);
5070 destroy_workqueue(local->hw.workqueue);
5072 wiphy_unregister(local->hw.wiphy);
5075 EXPORT_SYMBOL(ieee80211_register_hw);
5077 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
5078 struct ieee80211_hw_mode *mode)
5080 struct ieee80211_local *local = hw_to_local(hw);
5081 struct ieee80211_rate *rate;
5084 INIT_LIST_HEAD(&mode->list);
5085 list_add_tail(&mode->list, &local->modes_list);
5087 local->hw_modes |= (1 << mode->mode);
5088 for (i = 0; i < mode->num_rates; i++) {
5089 rate = &(mode->rates[i]);
5090 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
5092 ieee80211_prepare_rates(local, mode);
5094 if (!local->oper_hw_mode) {
5095 /* Default to this mode */
5096 local->hw.conf.phymode = mode->mode;
5097 local->oper_hw_mode = local->scan_hw_mode = mode;
5098 local->oper_channel = local->scan_channel = &mode->channels[0];
5099 local->hw.conf.mode = local->oper_hw_mode;
5100 local->hw.conf.chan = local->oper_channel;
5103 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
5104 ieee80211_init_client(local->mdev);
5108 EXPORT_SYMBOL(ieee80211_register_hwmode);
5110 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
5112 struct ieee80211_local *local = hw_to_local(hw);
5113 struct ieee80211_sub_if_data *sdata, *tmp;
5114 struct list_head tmp_list;
5117 tasklet_kill(&local->tx_pending_tasklet);
5118 tasklet_kill(&local->tasklet);
5122 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
5124 local->reg_state = IEEE80211_DEV_UNREGISTERED;
5126 ieee80211_if_del_mgmt(local);
5128 write_lock_bh(&local->sub_if_lock);
5129 list_replace_init(&local->sub_if_list, &tmp_list);
5130 write_unlock_bh(&local->sub_if_lock);
5132 list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
5133 __ieee80211_if_del(local, sdata);
5137 if (local->stat_time)
5138 del_timer_sync(&local->stat_timer);
5140 ieee80211_rx_bss_list_deinit(local->mdev);
5141 ieee80211_clear_tx_pending(local);
5142 sta_info_stop(local);
5143 rate_control_deinitialize(local);
5144 debugfs_hw_del(local);
5146 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
5147 kfree(local->supp_rates[i]);
5148 kfree(local->basic_rates[i]);
5151 if (skb_queue_len(&local->skb_queue)
5152 || skb_queue_len(&local->skb_queue_unreliable))
5153 printk(KERN_WARNING "%s: skb_queue not empty\n",
5155 skb_queue_purge(&local->skb_queue);
5156 skb_queue_purge(&local->skb_queue_unreliable);
5158 destroy_workqueue(local->hw.workqueue);
5159 wiphy_unregister(local->hw.wiphy);
5160 ieee80211_wep_free(local);
5161 ieee80211_led_exit(local);
5163 EXPORT_SYMBOL(ieee80211_unregister_hw);
5165 void ieee80211_free_hw(struct ieee80211_hw *hw)
5167 struct ieee80211_local *local = hw_to_local(hw);
5169 ieee80211_if_free(local->mdev);
5170 wiphy_free(local->hw.wiphy);
5172 EXPORT_SYMBOL(ieee80211_free_hw);
5174 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
5176 struct ieee80211_local *local = hw_to_local(hw);
5178 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
5179 &local->state[queue])) {
5180 if (test_bit(IEEE80211_LINK_STATE_PENDING,
5181 &local->state[queue]))
5182 tasklet_schedule(&local->tx_pending_tasklet);
5184 if (!ieee80211_qdisc_installed(local->mdev)) {
5186 netif_wake_queue(local->mdev);
5188 __netif_schedule(local->mdev);
5191 EXPORT_SYMBOL(ieee80211_wake_queue);
5193 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
5195 struct ieee80211_local *local = hw_to_local(hw);
5197 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
5198 netif_stop_queue(local->mdev);
5199 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
5201 EXPORT_SYMBOL(ieee80211_stop_queue);
5203 void ieee80211_start_queues(struct ieee80211_hw *hw)
5205 struct ieee80211_local *local = hw_to_local(hw);
5208 for (i = 0; i < local->hw.queues; i++)
5209 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
5210 if (!ieee80211_qdisc_installed(local->mdev))
5211 netif_start_queue(local->mdev);
5213 EXPORT_SYMBOL(ieee80211_start_queues);
5215 void ieee80211_stop_queues(struct ieee80211_hw *hw)
5219 for (i = 0; i < hw->queues; i++)
5220 ieee80211_stop_queue(hw, i);
5222 EXPORT_SYMBOL(ieee80211_stop_queues);
5224 void ieee80211_wake_queues(struct ieee80211_hw *hw)
5228 for (i = 0; i < hw->queues; i++)
5229 ieee80211_wake_queue(hw, i);
5231 EXPORT_SYMBOL(ieee80211_wake_queues);
5233 struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
5235 struct ieee80211_sub_if_data *sdata;
5236 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5237 return &sdata->stats;
5240 static int __init ieee80211_init(void)
5242 struct sk_buff *skb;
5245 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
5247 ret = ieee80211_wme_register();
5249 printk(KERN_DEBUG "ieee80211_init: failed to "
5250 "initialize WME (err=%d)\n", ret);
5254 ieee80211_debugfs_netdev_init();
5260 static void __exit ieee80211_exit(void)
5262 ieee80211_wme_unregister();
5263 ieee80211_debugfs_netdev_exit();
5267 module_init(ieee80211_init);
5268 module_exit(ieee80211_exit);
5270 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
5271 MODULE_LICENSE("GPL");