2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <net/d80211.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/etherdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/wireless.h>
20 #include <linux/rtnetlink.h>
21 #include <net/iw_handler.h>
22 #include <linux/compiler.h>
23 #include <linux/bitmap.h>
25 #include "ieee80211_common.h"
26 #include "ieee80211_i.h"
27 #include "ieee80211_rate.h"
33 #include "ieee80211_led.h"
35 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
36 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
37 static unsigned char rfc1042_header[] =
38 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
39 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
40 static unsigned char bridge_tunnel_header[] =
41 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
42 /* No encapsulation header if EtherType < 0x600 (=length) */
44 static unsigned char eapol_header[] =
45 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
48 static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len);
50 static int ieee80211_mgmt_start_xmit(struct sk_buff *skb,
51 struct net_device *dev);
53 struct ieee80211_key_conf *
54 ieee80211_key_data2conf(struct ieee80211_local *local,
55 struct ieee80211_key *data)
57 struct ieee80211_key_conf *conf;
59 conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
63 conf->hw_key_idx = data->hw_key_idx;
64 conf->alg = data->alg;
65 conf->keylen = data->keylen;
67 if (data->force_sw_encrypt)
68 conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
69 conf->keyidx = data->keyidx;
70 if (data->default_tx_key)
71 conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
72 if (local->default_wep_only)
73 conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
74 memcpy(conf->key, data->key, data->keylen);
79 struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
80 int idx, size_t key_len, gfp_t flags)
82 struct ieee80211_key *key;
85 key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
89 res = kobject_set_name(&key->kobj, "%d", idx);
91 res = kobject_set_name(&key->kobj, "key");
96 ieee80211_key_sysfs_set_kset(key, sdata ? &sdata->key_kset : NULL);
97 kobject_init(&key->kobj);
101 void ieee80211_key_free(struct ieee80211_key *key)
104 kobject_put(&key->kobj);
107 void ieee80211_key_release(struct kobject *kobj)
109 struct ieee80211_key *key;
111 key = container_of(kobj, struct ieee80211_key, kobj);
112 if (key->alg == ALG_CCMP)
113 ieee80211_aes_key_free(key->u.ccmp.tfm);
117 static int rate_list_match(int *rate_list, int rate)
124 for (i = 0; rate_list[i] >= 0; i++)
125 if (rate_list[i] == rate)
132 void ieee80211_prepare_rates(struct ieee80211_local *local)
136 for (i = 0; i < local->num_curr_rates; i++) {
137 struct ieee80211_rate *rate = &local->curr_rates[i];
139 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
140 IEEE80211_RATE_BASIC);
142 if (local->supp_rates[local->hw.conf.phymode]) {
143 if (!rate_list_match(local->supp_rates
144 [local->hw.conf.phymode],
149 rate->flags |= IEEE80211_RATE_SUPPORTED;
151 /* Use configured basic rate set if it is available. If not,
152 * use defaults that are sane for most cases. */
153 if (local->basic_rates[local->hw.conf.phymode]) {
154 if (rate_list_match(local->basic_rates
155 [local->hw.conf.phymode],
157 rate->flags |= IEEE80211_RATE_BASIC;
158 } else switch (local->hw.conf.phymode) {
159 case MODE_IEEE80211A:
160 if (rate->rate == 60 || rate->rate == 120 ||
162 rate->flags |= IEEE80211_RATE_BASIC;
164 case MODE_IEEE80211B:
165 if (rate->rate == 10 || rate->rate == 20)
166 rate->flags |= IEEE80211_RATE_BASIC;
168 case MODE_ATHEROS_TURBO:
169 if (rate->rate == 120 || rate->rate == 240 ||
171 rate->flags |= IEEE80211_RATE_BASIC;
173 case MODE_IEEE80211G:
174 if (rate->rate == 10 || rate->rate == 20 ||
175 rate->rate == 55 || rate->rate == 110)
176 rate->flags |= IEEE80211_RATE_BASIC;
180 /* Set ERP and MANDATORY flags based on phymode */
181 switch (local->hw.conf.phymode) {
182 case MODE_IEEE80211A:
183 if (rate->rate == 60 || rate->rate == 120 ||
185 rate->flags |= IEEE80211_RATE_MANDATORY;
187 case MODE_IEEE80211B:
188 if (rate->rate == 10)
189 rate->flags |= IEEE80211_RATE_MANDATORY;
191 case MODE_ATHEROS_TURBO:
193 case MODE_IEEE80211G:
194 if (rate->rate == 10 || rate->rate == 20 ||
195 rate->rate == 55 || rate->rate == 110 ||
196 rate->rate == 60 || rate->rate == 120 ||
198 rate->flags |= IEEE80211_RATE_MANDATORY;
199 if (rate->rate != 10 && rate->rate != 20 &&
200 rate->rate != 55 && rate->rate != 110)
201 rate->flags |= IEEE80211_RATE_ERP;
208 static void ieee80211_key_threshold_notify(struct net_device *dev,
209 struct ieee80211_key *key,
210 struct sta_info *sta)
212 struct ieee80211_local *local = dev->ieee80211_ptr;
214 struct ieee80211_msg_key_notification *msg;
216 /* if no one will get it anyway, don't even allocate it.
217 * unlikely because this is only relevant for APs
218 * where the device must be open... */
219 if (unlikely(!local->apdev))
222 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
223 sizeof(struct ieee80211_msg_key_notification));
227 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
228 msg = (struct ieee80211_msg_key_notification *)
229 skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
230 msg->tx_rx_count = key->tx_rx_count;
231 memcpy(msg->ifname, dev->name, IFNAMSIZ);
233 memcpy(msg->addr, sta->addr, ETH_ALEN);
235 memset(msg->addr, 0xff, ETH_ALEN);
237 key->tx_rx_count = 0;
239 ieee80211_rx_mgmt(local, skb, NULL,
240 ieee80211_msg_key_threshold_notification);
244 int ieee80211_get_hdrlen(u16 fc)
248 switch (fc & IEEE80211_FCTL_FTYPE) {
249 case IEEE80211_FTYPE_DATA:
250 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
251 hdrlen = 30; /* Addr4 */
252 if (fc & IEEE80211_STYPE_QOS_DATA)
253 hdrlen += 2; /* QoS Control Field */
255 case IEEE80211_FTYPE_CTL:
256 switch (fc & IEEE80211_FCTL_STYPE) {
257 case IEEE80211_STYPE_CTS:
258 case IEEE80211_STYPE_ACK:
270 EXPORT_SYMBOL(ieee80211_get_hdrlen);
272 int ieee80211_get_hdrlen_from_skb(struct sk_buff *skb)
274 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
277 if (unlikely(skb->len < 10))
279 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
280 if (unlikely(hdrlen > skb->len))
284 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
286 #ifdef CONFIG_D80211_LOWTX_FRAME_DUMP
287 static void ieee80211_dump_frame(const char *ifname, const char *title,
290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
294 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
300 fc = le16_to_cpu(hdr->frame_control);
301 hdrlen = ieee80211_get_hdrlen(fc);
302 if (hdrlen > skb->len)
305 printk(" FC=0x%04x DUR=0x%04x",
306 fc, le16_to_cpu(hdr->duration_id));
308 printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
310 printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
312 printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
314 printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
317 #else /* CONFIG_D80211_LOWTX_FRAME_DUMP */
318 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
322 #endif /* CONFIG_D80211_LOWTX_FRAME_DUMP */
325 static int ieee80211_is_eapol(struct sk_buff *skb)
327 struct ieee80211_hdr *hdr;
331 if (unlikely(skb->len < 10))
334 hdr = (struct ieee80211_hdr *) skb->data;
335 fc = le16_to_cpu(hdr->frame_control);
337 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
340 hdrlen = ieee80211_get_hdrlen(fc);
342 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
343 memcmp(skb->data + hdrlen, eapol_header,
344 sizeof(eapol_header)) == 0))
351 static ieee80211_txrx_result
352 ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
354 struct rate_control_extra extra;
356 memset(&extra, 0, sizeof(extra));
357 extra.mgmt_data = tx->sdata &&
358 tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
359 extra.ethertype = tx->ethertype;
361 extra.endidx = tx->local->num_curr_rates;
363 tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
365 if (unlikely(extra.probe != NULL)) {
366 tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
367 tx->u.tx.probe_last_frag = 1;
368 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
369 tx->u.tx.rate = extra.probe;
371 tx->u.tx.control->alt_retry_rate = -1;
375 if (tx->local->hw.conf.phymode == MODE_IEEE80211G &&
376 tx->local->cts_protect_erp_frames && tx->fragmented &&
378 tx->u.tx.last_frag_rate = tx->u.tx.rate;
379 tx->u.tx.last_frag_rateidx = extra.rateidx;
380 tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
382 tx->u.tx.rate = extra.nonerp;
383 tx->u.tx.control->rateidx = extra.nonerp_idx;
384 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
386 tx->u.tx.last_frag_rate = tx->u.tx.rate;
387 tx->u.tx.last_frag_rateidx = extra.rateidx;
388 tx->u.tx.control->rateidx = extra.rateidx;
390 tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
391 if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
392 tx->local->short_preamble &&
393 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
394 tx->u.tx.short_preamble = 1;
395 tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
398 return TXRX_CONTINUE;
402 static ieee80211_txrx_result
403 ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
406 tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
408 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
410 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
412 else if (tx->sta && tx->sta->key)
413 tx->key = tx->sta->key;
414 else if (tx->sdata->default_key)
415 tx->key = tx->sdata->default_key;
416 else if (tx->sdata->drop_unencrypted &&
417 !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
418 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
424 tx->key->tx_rx_count++;
425 if (unlikely(tx->local->key_tx_rx_threshold &&
426 tx->key->tx_rx_count >
427 tx->local->key_tx_rx_threshold)) {
428 ieee80211_key_threshold_notify(tx->dev, tx->key,
433 return TXRX_CONTINUE;
437 static ieee80211_txrx_result
438 ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
440 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
441 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
442 struct sk_buff **frags, *first, *frag;
445 int frag_threshold = tx->local->fragmentation_threshold;
448 return TXRX_CONTINUE;
452 hdrlen = ieee80211_get_hdrlen(tx->fc);
453 payload_len = first->len - hdrlen;
454 per_fragm = frag_threshold - hdrlen - FCS_LEN;
455 num_fragm = (payload_len + per_fragm - 1) / per_fragm;
457 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
461 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
462 pos = first->data + hdrlen + per_fragm;
463 left = payload_len - per_fragm;
464 for (i = 0; i < num_fragm - 1; i++) {
465 struct ieee80211_hdr *fhdr;
471 /* reserve enough extra head and tail room for possible
473 #define IEEE80211_ENCRYPT_HEADROOM 8
474 #define IEEE80211_ENCRYPT_TAILROOM 12
476 dev_alloc_skb(frag_threshold +
477 IEEE80211_ENCRYPT_HEADROOM +
478 IEEE80211_ENCRYPT_TAILROOM);
481 /* Make sure that all fragments use the same priority so
482 * that they end up using the same TX queue */
483 frag->priority = first->priority;
484 skb_reserve(frag, IEEE80211_ENCRYPT_HEADROOM);
485 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
486 memcpy(fhdr, first->data, hdrlen);
487 if (i == num_fragm - 2)
488 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
489 fhdr->seq_ctrl = cpu_to_le16(i + 1);
490 copylen = left > per_fragm ? per_fragm : left;
491 memcpy(skb_put(frag, copylen), pos, copylen);
496 skb_trim(first, hdrlen + per_fragm);
498 tx->u.tx.num_extra_frag = num_fragm - 1;
499 tx->u.tx.extra_frag = frags;
501 return TXRX_CONTINUE;
504 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
506 for (i = 0; i < num_fragm - 1; i++)
508 dev_kfree_skb(frags[i]);
511 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
516 static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
518 if (tx->key->force_sw_encrypt) {
519 if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
522 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
523 if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
524 if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
533 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
535 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
537 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
538 if (tx->u.tx.extra_frag) {
539 struct ieee80211_hdr *fhdr;
541 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
542 fhdr = (struct ieee80211_hdr *)
543 tx->u.tx.extra_frag[i]->data;
544 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
550 static ieee80211_txrx_result
551 ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
553 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
556 fc = le16_to_cpu(hdr->frame_control);
558 if (!tx->key || tx->key->alg != ALG_WEP ||
559 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
560 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
561 (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
562 return TXRX_CONTINUE;
564 tx->u.tx.control->iv_len = WEP_IV_LEN;
565 tx->u.tx.control->icv_len = WEP_ICV_LEN;
566 ieee80211_tx_set_iswep(tx);
568 if (wep_encrypt_skb(tx, tx->skb) < 0) {
569 I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
573 if (tx->u.tx.extra_frag) {
575 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
576 if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
577 I802_DEBUG_INC(tx->local->
578 tx_handlers_drop_wep);
584 return TXRX_CONTINUE;
588 static inline int ceiling_div(int dividend, int divisor)
590 return ((dividend + divisor - 1) / divisor);
594 static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
595 int rate, int erp, int short_preamble)
599 /* calculate duration (in microseconds, rounded up to next higher
600 * integer if it includes a fractional microsecond) to send frame of
601 * len bytes (does not include FCS) at the given rate. Duration will
604 * rate is in 100 kbps, so divident is multiplied by 10 in the
605 * ceiling_div() operations.
608 if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
609 local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
613 * N_DBPS = DATARATE x 4
614 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
615 * (16 = SIGNAL time, 6 = tail bits)
616 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
619 * 802.11a - 17.5.2: aSIFSTime = 16 usec
620 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
621 * signal ext = 6 usec
623 /* FIX: Atheros Turbo may have different (shorter) duration? */
624 dur = 16; /* SIFS + signal ext */
625 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
626 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
627 dur += 4 * ceiling_div((16 + 8 * (len + 4) + 6) * 10,
628 4 * rate); /* T_SYM x N_SYM */
631 * 802.11b or 802.11g with 802.11b compatibility:
632 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
633 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
635 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
636 * aSIFSTime = 10 usec
637 * aPreambleLength = 144 usec or 72 usec with short preamble
638 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
640 dur = 10; /* aSIFSTime = 10 usec */
641 dur += short_preamble ? (72 + 24) : (144 + 48);
643 dur += ceiling_div(8 * (len + 4) * 10, rate);
650 static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
653 int rate, mrate, erp, dur, i;
654 struct ieee80211_rate *txrate = tx->u.tx.rate;
655 struct ieee80211_local *local = tx->local;
657 erp = txrate->flags & IEEE80211_RATE_ERP;
660 * data and mgmt (except PS Poll):
661 * - during CFP: 32768
662 * - during contention period:
663 * if addr1 is group address: 0
664 * if more fragments = 0 and addr1 is individual address: time to
665 * transmit one ACK plus SIFS
666 * if more fragments = 1 and addr1 is individual address: time to
667 * transmit next fragment plus 2 x ACK plus 3 x SIFS
670 * - control response frame (CTS or ACK) shall be transmitted using the
671 * same rate as the immediately previous frame in the frame exchange
672 * sequence, if this rate belongs to the PHY mandatory rates, or else
673 * at the highest possible rate belonging to the PHY rates in the
677 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
678 /* TODO: These control frames are not currently sent by
679 * 80211.o, but should they be implemented, this function
680 * needs to be updated to support duration field calculation.
682 * RTS: time needed to transmit pending data/mgmt frame plus
683 * one CTS frame plus one ACK frame plus 3 x SIFS
684 * CTS: duration of immediately previous RTS minus time
685 * required to transmit CTS and its SIFS
686 * ACK: 0 if immediately previous directed data/mgmt had
687 * more=0, with more=1 duration in ACK frame is duration
688 * from previous frame minus time needed to transmit ACK
690 * PS Poll: BIT(15) | BIT(14) | aid
696 if (0 /* FIX: data/mgmt during CFP */)
699 if (group_addr) /* Group address as the destination - no ACK */
702 /* Individual destination address:
703 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
704 * CTS and ACK frames shall be transmitted using the highest rate in
705 * basic rate set that is less than or equal to the rate of the
706 * immediately previous frame and that is using the same modulation
707 * (CCK or OFDM). If no basic rate set matches with these requirements,
708 * the highest mandatory rate of the PHY that is less than or equal to
709 * the rate of the previous frame is used.
710 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
713 mrate = 10; /* use 1 Mbps if everything fails */
714 for (i = 0; i < local->num_curr_rates; i++) {
715 struct ieee80211_rate *r = &local->curr_rates[i];
716 if (r->rate > txrate->rate)
719 if (IEEE80211_RATE_MODULATION(txrate->flags) !=
720 IEEE80211_RATE_MODULATION(r->flags))
723 if (r->flags & IEEE80211_RATE_BASIC)
725 else if (r->flags & IEEE80211_RATE_MANDATORY)
729 /* No matching basic rate found; use highest suitable mandatory
734 /* Time needed to transmit ACK
735 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
736 * to closest integer */
738 dur = ieee80211_frame_duration(local, 10, rate, erp,
739 local->short_preamble);
742 /* Frame is fragmented: duration increases with time needed to
743 * transmit next fragment plus ACK and 2 x SIFS. */
744 dur *= 2; /* ACK + SIFS */
746 dur += ieee80211_frame_duration(local, next_frag_len,
748 local->short_preamble);
755 static ieee80211_txrx_result
756 ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
758 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
760 struct ieee80211_tx_control *control = tx->u.tx.control;
762 if (!is_multicast_ether_addr(hdr->addr1)) {
763 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
764 tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
765 control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
766 control->retry_limit =
767 tx->local->long_retry_limit;
769 control->retry_limit =
770 tx->local->short_retry_limit;
773 control->retry_limit = 1;
776 if (tx->fragmented) {
777 /* Do not use multiple retry rates when sending fragmented
779 * TODO: The last fragment could still use multiple retry
781 control->alt_retry_rate = -1;
784 /* Use CTS protection for unicast frames sent using extended rates if
785 * there are associated non-ERP stations and RTS/CTS is not configured
787 if (tx->local->hw.conf.phymode == MODE_IEEE80211G &&
788 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
790 tx->local->cts_protect_erp_frames &&
791 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
792 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
794 /* Setup duration field for the first fragment of the frame. Duration
795 * for remaining fragments will be updated when they are being sent
796 * to low-level driver in ieee80211_tx(). */
797 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
798 tx->fragmented ? tx->u.tx.extra_frag[0]->len :
800 hdr->duration_id = cpu_to_le16(dur);
802 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
803 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
804 struct ieee80211_rate *rate;
805 int erp = tx->u.tx.rate->flags & IEEE80211_RATE_ERP;
807 /* Do not use multiple retry rates when using RTS/CTS */
808 control->alt_retry_rate = -1;
810 /* Use min(data rate, max base rate) as CTS/RTS rate */
811 rate = tx->u.tx.rate;
812 while (rate > tx->local->curr_rates &&
813 !(rate->flags & IEEE80211_RATE_BASIC))
816 if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
817 dur += ieee80211_frame_duration(tx->local, 10,
821 dur += ieee80211_frame_duration(tx->local, tx->skb->len,
822 tx->u.tx.rate->rate, erp,
823 tx->u.tx.short_preamble);
824 control->rts_cts_duration = dur;
825 control->rts_cts_rate = rate->val;
829 tx->sta->tx_packets++;
830 tx->sta->tx_fragments++;
831 tx->sta->tx_bytes += tx->skb->len;
832 if (tx->u.tx.extra_frag) {
834 tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
835 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
837 tx->u.tx.extra_frag[i]->len;
841 tx->local->scan.txrx_count++;
843 return TXRX_CONTINUE;
847 static ieee80211_txrx_result
848 ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
850 #ifdef CONFIG_D80211_VERBOSE_DEBUG
851 struct sk_buff *skb = tx->skb;
852 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
853 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
856 if (unlikely(tx->local->sta_scanning != 0) &&
857 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
858 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
861 if (tx->u.tx.ps_buffered)
862 return TXRX_CONTINUE;
864 sta_flags = tx->sta ? tx->sta->flags : 0;
866 if (likely(tx->u.tx.unicast)) {
867 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
868 tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
869 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
870 #ifdef CONFIG_D80211_VERBOSE_DEBUG
871 printk(KERN_DEBUG "%s: dropped data frame to not "
872 "associated station " MAC_FMT "\n",
873 tx->dev->name, MAC_ARG(hdr->addr1));
874 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
875 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
879 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
880 tx->local->num_sta == 0 &&
881 !tx->local->allow_broadcast_always &&
882 tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
884 * No associated STAs - no need to send multicast
889 return TXRX_CONTINUE;
892 if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
893 !(sta_flags & WLAN_STA_AUTHORIZED))) {
894 #ifdef CONFIG_D80211_DEBUG
895 struct ieee80211_hdr *hdr =
896 (struct ieee80211_hdr *) tx->skb->data;
897 printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
898 " (unauthorized port)\n", tx->dev->name,
899 MAC_ARG(hdr->addr1));
901 I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
905 return TXRX_CONTINUE;
909 /* This function is called whenever the AP is about to exceed the maximum limit
910 * of buffered frames for power saving STAs. This situation should not really
911 * happen often during normal operation, so dropping the oldest buffered packet
912 * from each queue should be OK to make some room for new frames. */
913 static void purge_old_ps_buffers(struct ieee80211_local *local)
915 int total = 0, purged = 0;
917 struct ieee80211_sub_if_data *sdata;
918 struct sta_info *sta;
920 spin_lock_bh(&local->sub_if_lock);
921 list_for_each_entry(sdata, &local->sub_if_list, list) {
922 struct ieee80211_if_ap *ap;
923 if (sdata->dev == local->mdev ||
924 sdata->type != IEEE80211_IF_TYPE_AP)
927 skb = skb_dequeue(&ap->ps_bc_buf);
932 total += skb_queue_len(&ap->ps_bc_buf);
934 spin_unlock_bh(&local->sub_if_lock);
936 spin_lock_bh(&local->sta_lock);
937 list_for_each_entry(sta, &local->sta_list, list) {
938 skb = skb_dequeue(&sta->ps_tx_buf);
943 total += skb_queue_len(&sta->ps_tx_buf);
945 spin_unlock_bh(&local->sta_lock);
947 local->total_ps_buffered = total;
948 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
949 local->mdev->name, purged);
953 static inline ieee80211_txrx_result
954 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
956 /* broadcast/multicast frame */
957 /* If any of the associated stations is in power save mode,
958 * the frame is buffered to be sent after DTIM beacon frame */
959 if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
960 tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
961 tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
962 !(tx->fc & IEEE80211_FCTL_ORDER)) {
963 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
964 purge_old_ps_buffers(tx->local);
965 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
967 if (net_ratelimit()) {
968 printk(KERN_DEBUG "%s: BC TX buffer full - "
969 "dropping the oldest frame\n",
972 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
974 tx->local->total_ps_buffered++;
975 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
979 return TXRX_CONTINUE;
983 static inline ieee80211_txrx_result
984 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
986 struct sta_info *sta = tx->sta;
989 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
990 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
991 return TXRX_CONTINUE;
993 if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
994 struct ieee80211_tx_packet_data *pkt_data;
995 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
996 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
998 MAC_ARG(sta->addr), sta->aid,
999 skb_queue_len(&sta->ps_tx_buf));
1000 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
1001 sta->flags |= WLAN_STA_TIM;
1002 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
1003 purge_old_ps_buffers(tx->local);
1004 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
1005 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
1006 if (net_ratelimit()) {
1007 printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
1008 "buffer full - dropping oldest frame\n",
1009 tx->dev->name, MAC_ARG(sta->addr));
1013 tx->local->total_ps_buffered++;
1014 /* Queue frame to be sent after STA sends an PS Poll frame */
1015 if (skb_queue_empty(&sta->ps_tx_buf)) {
1016 if (tx->local->ops->set_tim)
1017 tx->local->ops->set_tim(local_to_hw(tx->local),
1020 bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
1022 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
1023 pkt_data->jiffies = jiffies;
1024 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
1027 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
1028 else if (unlikely(sta->flags & WLAN_STA_PS)) {
1029 printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
1030 "set -> send frame\n", tx->dev->name,
1031 MAC_ARG(sta->addr));
1033 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
1036 return TXRX_CONTINUE;
1040 static ieee80211_txrx_result
1041 ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
1043 if (unlikely(tx->u.tx.ps_buffered))
1044 return TXRX_CONTINUE;
1046 if (tx->u.tx.unicast)
1047 return ieee80211_tx_h_unicast_ps_buf(tx);
1049 return ieee80211_tx_h_multicast_ps_buf(tx);
1054 __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1055 struct sk_buff *skb,
1056 struct net_device *dev,
1057 struct ieee80211_tx_control *control)
1059 struct ieee80211_local *local = dev->ieee80211_ptr;
1060 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1063 memset(tx, 0, sizeof(*tx));
1065 tx->dev = dev; /* use original interface */
1067 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1068 tx->sta = sta_info_get(local, hdr->addr1);
1069 tx->fc = le16_to_cpu(hdr->frame_control);
1070 control->power_level = local->hw.conf.power_level;
1071 tx->u.tx.control = control;
1072 tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
1073 if (is_multicast_ether_addr(hdr->addr1))
1074 control->flags |= IEEE80211_TXCTL_NO_ACK;
1076 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
1077 tx->fragmented = local->fragmentation_threshold <
1078 IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
1079 skb->len + FCS_LEN > local->fragmentation_threshold &&
1080 (!local->ops->set_frag_threshold);
1082 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1083 else if (tx->sta->clear_dst_mask) {
1084 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1085 tx->sta->clear_dst_mask = 0;
1087 control->antenna_sel = local->hw.conf.antenna_sel;
1088 if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
1089 control->antenna_sel = tx->sta->antenna_sel;
1090 hdrlen = ieee80211_get_hdrlen(tx->fc);
1091 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1092 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1093 tx->ethertype = (pos[0] << 8) | pos[1];
1095 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1099 static int inline is_ieee80211_device(struct net_device *dev,
1100 struct net_device *master)
1102 return (dev->ieee80211_ptr == master->ieee80211_ptr);
1105 /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
1106 * finished with it. */
1107 static void inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1108 struct sk_buff *skb,
1109 struct net_device *mdev,
1110 struct ieee80211_tx_control *control)
1112 struct ieee80211_tx_packet_data *pkt_data;
1113 struct net_device *dev;
1115 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1116 dev = dev_get_by_index(pkt_data->ifindex);
1117 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1121 if (unlikely(!dev)) {
1122 printk(KERN_WARNING "%s: NULL ifindex in pkt_data\n",
1127 __ieee80211_tx_prepare(tx, skb, dev, control);
1130 static inline int __ieee80211_queue_stopped(struct ieee80211_local *local,
1133 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
1136 static inline int __ieee80211_queue_pending(struct ieee80211_local *local,
1139 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
1142 #define IEEE80211_TX_OK 0
1143 #define IEEE80211_TX_AGAIN 1
1144 #define IEEE80211_TX_FRAG_AGAIN 2
1146 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1147 struct ieee80211_txrx_data *tx)
1149 struct ieee80211_tx_control *control = tx->u.tx.control;
1153 ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
1154 ret = local->ops->tx(local_to_hw(local), skb, control);
1156 return IEEE80211_TX_AGAIN;
1157 ieee80211_led_tx(local, 1);
1159 if (tx->u.tx.extra_frag) {
1160 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1161 IEEE80211_TXCTL_USE_CTS_PROTECT |
1162 IEEE80211_TXCTL_CLEAR_DST_MASK |
1163 IEEE80211_TXCTL_FIRST_FRAGMENT);
1164 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1165 if (!tx->u.tx.extra_frag[i])
1167 if (__ieee80211_queue_stopped(local, control->queue))
1168 return IEEE80211_TX_FRAG_AGAIN;
1169 if (i == tx->u.tx.num_extra_frag) {
1170 control->tx_rate = tx->u.tx.last_frag_hwrate;
1171 control->rateidx = tx->u.tx.last_frag_rateidx;
1172 if (tx->u.tx.probe_last_frag)
1174 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1177 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1180 ieee80211_dump_frame(local->mdev->name,
1181 "TX to low-level driver", skb);
1182 ret = local->ops->tx(local_to_hw(local),
1183 tx->u.tx.extra_frag[i],
1186 return IEEE80211_TX_FRAG_AGAIN;
1187 ieee80211_led_tx(local, 1);
1188 tx->u.tx.extra_frag[i] = NULL;
1190 kfree(tx->u.tx.extra_frag);
1191 tx->u.tx.extra_frag = NULL;
1193 return IEEE80211_TX_OK;
1196 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
1197 struct ieee80211_tx_control *control, int mgmt)
1199 struct ieee80211_local *local = dev->ieee80211_ptr;
1200 struct sta_info *sta;
1201 ieee80211_tx_handler *handler;
1202 struct ieee80211_txrx_data tx;
1203 ieee80211_txrx_result res = TXRX_DROP;
1206 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1208 if (unlikely(skb->len < 10)) {
1213 __ieee80211_tx_prepare(&tx, skb, dev, control);
1215 tx.u.tx.mgmt_interface = mgmt;
1217 for (handler = local->tx_handlers; *handler != NULL; handler++) {
1218 res = (*handler)(&tx);
1219 if (res != TXRX_CONTINUE)
1223 skb = tx.skb; /* handlers are allowed to change skb */
1228 if (unlikely(res == TXRX_DROP)) {
1229 I802_DEBUG_INC(local->tx_handlers_drop);
1233 if (unlikely(res == TXRX_QUEUED)) {
1234 I802_DEBUG_INC(local->tx_handlers_queued);
1238 if (tx.u.tx.extra_frag) {
1239 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1241 struct ieee80211_hdr *hdr =
1242 (struct ieee80211_hdr *)
1243 tx.u.tx.extra_frag[i]->data;
1245 if (i + 1 < tx.u.tx.num_extra_frag) {
1246 next_len = tx.u.tx.extra_frag[i + 1]->len;
1249 tx.u.tx.rate = tx.u.tx.last_frag_rate;
1250 tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
1252 dur = ieee80211_duration(&tx, 0, next_len);
1253 hdr->duration_id = cpu_to_le16(dur);
1258 ret = __ieee80211_tx(local, skb, &tx);
1260 struct ieee80211_tx_stored_packet *store =
1261 &local->pending_packet[control->queue];
1263 if (ret == IEEE80211_TX_FRAG_AGAIN)
1265 set_bit(IEEE80211_LINK_STATE_PENDING,
1266 &local->state[control->queue]);
1268 /* When the driver gets out of buffers during sending of
1269 * fragments and calls ieee80211_stop_queue, there is
1270 * a small window between IEEE80211_LINK_STATE_XOFF and
1271 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1272 * gets available in that window (i.e. driver calls
1273 * ieee80211_wake_queue), we would end up with ieee80211_tx
1274 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1275 * continuing transmitting here when that situation is
1276 * possible to have happened. */
1277 if (!__ieee80211_queue_stopped(local, control->queue)) {
1278 clear_bit(IEEE80211_LINK_STATE_PENDING,
1279 &local->state[control->queue]);
1282 memcpy(&store->control, control,
1283 sizeof(struct ieee80211_tx_control));
1285 store->extra_frag = tx.u.tx.extra_frag;
1286 store->num_extra_frag = tx.u.tx.num_extra_frag;
1287 store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
1288 store->last_frag_rateidx = tx.u.tx.last_frag_rateidx;
1289 store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
1296 for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1297 if (tx.u.tx.extra_frag[i])
1298 dev_kfree_skb(tx.u.tx.extra_frag[i]);
1299 kfree(tx.u.tx.extra_frag);
1303 static void ieee80211_tx_pending(unsigned long data)
1305 struct ieee80211_local *local = (struct ieee80211_local *)data;
1306 struct net_device *dev = local->mdev;
1307 struct ieee80211_tx_stored_packet *store;
1308 struct ieee80211_txrx_data tx;
1309 int i, ret, reschedule = 0;
1311 netif_tx_lock_bh(dev);
1312 for (i = 0; i < local->hw.queues; i++) {
1313 if (__ieee80211_queue_stopped(local, i))
1315 if (!__ieee80211_queue_pending(local, i)) {
1319 store = &local->pending_packet[i];
1320 tx.u.tx.control = &store->control;
1321 tx.u.tx.extra_frag = store->extra_frag;
1322 tx.u.tx.num_extra_frag = store->num_extra_frag;
1323 tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
1324 tx.u.tx.last_frag_rateidx = store->last_frag_rateidx;
1325 tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
1326 ret = __ieee80211_tx(local, store->skb, &tx);
1328 if (ret == IEEE80211_TX_FRAG_AGAIN)
1331 clear_bit(IEEE80211_LINK_STATE_PENDING,
1336 netif_tx_unlock_bh(dev);
1338 netif_schedule(dev);
1341 static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1344 struct ieee80211_tx_stored_packet *store;
1346 for (i = 0; i < local->hw.queues; i++) {
1347 if (!__ieee80211_queue_pending(local, i))
1349 store = &local->pending_packet[i];
1350 kfree_skb(store->skb);
1351 for (j = 0; j < store->num_extra_frag; j++)
1352 kfree_skb(store->extra_frag[j]);
1353 kfree(store->extra_frag);
1354 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1358 static int ieee80211_master_start_xmit(struct sk_buff *skb,
1359 struct net_device *dev)
1361 struct ieee80211_tx_control control;
1362 struct ieee80211_tx_packet_data *pkt_data;
1363 struct net_device *odev = NULL;
1364 struct ieee80211_sub_if_data *osdata;
1368 * copy control out of the skb so other people can use skb->cb
1370 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1371 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1373 if (pkt_data->ifindex)
1374 odev = dev_get_by_index(pkt_data->ifindex);
1375 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1379 if (unlikely(!odev)) {
1380 #ifdef CONFIG_D80211_VERBOSE_DEBUG
1381 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1382 "originating device\n", dev->name);
1387 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1389 control.ifindex = odev->ifindex;
1390 control.type = osdata->type;
1391 if (pkt_data->req_tx_status)
1392 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
1393 if (pkt_data->do_not_encrypt)
1394 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1395 if (pkt_data->requeue)
1396 control.flags |= IEEE80211_TXCTL_REQUEUE;
1397 control.queue = pkt_data->queue;
1399 ret = ieee80211_tx(odev, skb, &control,
1400 control.type == IEEE80211_IF_TYPE_MGMT);
1408 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1409 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1410 * @skb: packet to be sent
1411 * @dev: incoming interface
1413 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1414 * not be freed, and caller is responsible for either retrying later or freeing
1417 * This function takes in an Ethernet header and encapsulates it with suitable
1418 * IEEE 802.11 header based on which interface the packet is coming in. The
1419 * encapsulated packet will then be passed to master interface, wlan#.11, for
1420 * transmission (through low-level driver).
1422 static int ieee80211_subif_start_xmit(struct sk_buff *skb,
1423 struct net_device *dev)
1425 struct ieee80211_local *local = dev->ieee80211_ptr;
1426 struct ieee80211_tx_packet_data *pkt_data;
1427 struct ieee80211_sub_if_data *sdata;
1428 int ret = 1, head_need;
1429 u16 ethertype, hdrlen, fc;
1430 struct ieee80211_hdr hdr;
1432 int encaps_len, skip_header_bytes;
1433 int nh_pos, h_pos, no_encrypt = 0;
1434 struct sta_info *sta;
1436 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1437 if (unlikely(skb->len < ETH_HLEN)) {
1438 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1439 dev->name, skb->len);
1444 nh_pos = skb->nh.raw - skb->data;
1445 h_pos = skb->h.raw - skb->data;
1447 /* convert Ethernet header to proper 802.11 header (based on
1448 * operation mode) */
1449 ethertype = (skb->data[12] << 8) | skb->data[13];
1450 /* TODO: handling for 802.1x authorized/unauthorized port */
1451 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1453 if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
1454 sdata->type == IEEE80211_IF_TYPE_VLAN)) {
1455 fc |= IEEE80211_FCTL_FROMDS;
1457 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1458 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1459 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1461 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
1462 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1464 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1465 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1466 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1467 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1469 } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
1470 fc |= IEEE80211_FCTL_TODS;
1472 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1473 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1474 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1476 } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
1478 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1479 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1480 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1487 /* receiver is QoS enabled, use a QoS type frame */
1488 sta = sta_info_get(local, hdr.addr1);
1490 if (sta->flags & WLAN_STA_WME) {
1491 fc |= IEEE80211_STYPE_QOS_DATA;
1497 hdr.frame_control = cpu_to_le16(fc);
1498 hdr.duration_id = 0;
1501 skip_header_bytes = ETH_HLEN;
1502 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1503 encaps_data = bridge_tunnel_header;
1504 encaps_len = sizeof(bridge_tunnel_header);
1505 skip_header_bytes -= 2;
1506 } else if (ethertype >= 0x600) {
1507 encaps_data = rfc1042_header;
1508 encaps_len = sizeof(rfc1042_header);
1509 skip_header_bytes -= 2;
1515 skb_pull(skb, skip_header_bytes);
1516 nh_pos -= skip_header_bytes;
1517 h_pos -= skip_header_bytes;
1519 /* TODO: implement support for fragments so that there is no need to
1520 * reallocate and copy payload; it might be enough to support one
1521 * extra fragment that would be copied in the beginning of the frame
1522 * data.. anyway, it would be nice to include this into skb structure
1525 * There are few options for this:
1526 * use skb->cb as an extra space for 802.11 header
1527 * allocate new buffer if not enough headroom
1528 * make sure that there is enough headroom in every skb by increasing
1529 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1530 * alloc_skb() (net/core/skbuff.c)
1532 head_need = hdrlen + encaps_len + local->hw.extra_tx_headroom;
1533 head_need -= skb_headroom(skb);
1535 /* We are going to modify skb data, so make a copy of it if happens to
1536 * be cloned. This could happen, e.g., with Linux bridge code passing
1537 * us broadcast frames. */
1539 if (head_need > 0 || skb_cloned(skb)) {
1541 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1542 "of headroom\n", dev->name, head_need);
1545 if (skb_cloned(skb))
1546 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1548 I802_DEBUG_INC(local->tx_expand_skb_head);
1549 /* Since we have to reallocate the buffer, make sure that there
1550 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1551 * before payload and 12 after). */
1552 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1554 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1561 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1562 nh_pos += encaps_len;
1563 h_pos += encaps_len;
1565 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1569 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1570 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1571 pkt_data->ifindex = sdata->dev->ifindex;
1572 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1573 pkt_data->do_not_encrypt = no_encrypt;
1575 skb->dev = local->mdev;
1576 sdata->stats.tx_packets++;
1577 sdata->stats.tx_bytes += skb->len;
1579 /* Update skb pointers to various headers since this modified frame
1580 * is going to go through Linux networking code that may potentially
1581 * need things like pointer to IP header. */
1582 skb->mac.raw = skb->data;
1583 skb->nh.raw = skb->data + nh_pos;
1584 skb->h.raw = skb->data + h_pos;
1586 dev_queue_xmit(skb);
1599 * This is the transmit routine for the 802.11 type interfaces
1600 * called by upper layers of the linux networking
1601 * stack when it has a frame to transmit
1604 ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
1606 struct ieee80211_sub_if_data *sdata;
1607 struct ieee80211_tx_packet_data *pkt_data;
1608 struct ieee80211_hdr *hdr;
1611 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1613 if (skb->len < 10) {
1618 hdr = (struct ieee80211_hdr *) skb->data;
1619 fc = le16_to_cpu(hdr->frame_control);
1621 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
1622 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1623 pkt_data->ifindex = sdata->dev->ifindex;
1624 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1626 skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
1627 skb->dev = sdata->local->mdev;
1630 * We're using the protocol field of the the frame control header
1631 * to request TX callback for hostapd. BIT(1) is checked.
1633 if ((fc & BIT(1)) == BIT(1)) {
1634 pkt_data->req_tx_status = 1;
1636 hdr->frame_control = cpu_to_le16(fc);
1639 pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
1641 sdata->stats.tx_packets++;
1642 sdata->stats.tx_bytes += skb->len;
1644 dev_queue_xmit(skb);
1650 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1651 struct ieee80211_if_ap *bss,
1652 struct sk_buff *skb)
1656 int i, have_bits = 0, n1, n2;
1658 /* Generate bitmap for TIM only if there are any STAs in power save
1660 spin_lock_bh(&local->sta_lock);
1661 if (atomic_read(&bss->num_sta_ps) > 0)
1662 /* in the hope that this is faster than
1663 * checking byte-for-byte */
1664 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1665 IEEE80211_MAX_AID+1);
1667 if (bss->dtim_count == 0)
1668 bss->dtim_count = bss->dtim_period - 1;
1672 tim = pos = (u8 *) skb_put(skb, 6);
1673 *pos++ = WLAN_EID_TIM;
1675 *pos++ = bss->dtim_count;
1676 *pos++ = bss->dtim_period;
1678 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1682 /* Find largest even number N1 so that bits numbered 1 through
1683 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1684 * (N2 + 1) x 8 through 2007 are 0. */
1686 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1693 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1700 /* Bitmap control */
1702 /* Part Virt Bitmap */
1703 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1705 tim[1] = n2 - n1 + 4;
1706 skb_put(skb, n2 - n1);
1708 *pos++ = aid0; /* Bitmap control */
1709 *pos++ = 0; /* Part Virt Bitmap */
1711 spin_unlock_bh(&local->sta_lock);
1715 struct sk_buff * ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
1716 struct ieee80211_tx_control *control)
1718 struct ieee80211_local *local = hw_to_local(hw);
1719 struct sk_buff *skb;
1720 struct net_device *bdev;
1721 struct ieee80211_sub_if_data *sdata = NULL;
1722 struct ieee80211_if_ap *ap = NULL;
1723 struct ieee80211_rate *rate;
1724 struct rate_control_extra extra;
1725 u8 *b_head, *b_tail;
1728 bdev = dev_get_by_index(if_id);
1730 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
1735 if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
1737 #ifdef CONFIG_D80211_VERBOSE_DEBUG
1738 if (net_ratelimit())
1739 printk(KERN_DEBUG "no beacon data avail for idx=%d "
1740 "(%s)\n", if_id, bdev ? bdev->name : "N/A");
1741 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
1745 /* Assume we are generating the normal beacon locally */
1746 b_head = ap->beacon_head;
1747 b_tail = ap->beacon_tail;
1748 bh_len = ap->beacon_head_len;
1749 bt_len = ap->beacon_tail_len;
1751 skb = dev_alloc_skb(bh_len + bt_len + 256 /* maximum TIM len */);
1755 memcpy(skb_put(skb, bh_len), b_head, bh_len);
1757 ieee80211_beacon_add_tim(local, ap, skb);
1760 memcpy(skb_put(skb, bt_len), b_tail, bt_len);
1764 memset(&extra, 0, sizeof(extra));
1765 extra.endidx = local->num_curr_rates;
1767 rate = rate_control_get_rate(local, local->mdev, skb, &extra);
1769 if (net_ratelimit()) {
1770 printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
1771 "found\n", local->mdev->name);
1777 control->tx_rate = (local->short_preamble &&
1778 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
1779 rate->val2 : rate->val;
1780 control->antenna_sel = local->hw.conf.antenna_sel;
1781 control->power_level = local->hw.conf.power_level;
1782 control->flags |= IEEE80211_TXCTL_NO_ACK;
1783 control->retry_limit = 1;
1784 control->rts_cts_duration = 0;
1785 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1791 EXPORT_SYMBOL(ieee80211_beacon_get);
1794 ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
1795 struct ieee80211_tx_control *control)
1797 struct ieee80211_local *local = hw_to_local(hw);
1798 struct sk_buff *skb;
1799 struct sta_info *sta;
1800 ieee80211_tx_handler *handler;
1801 struct ieee80211_txrx_data tx;
1802 ieee80211_txrx_result res = TXRX_DROP;
1803 struct net_device *bdev;
1804 struct ieee80211_sub_if_data *sdata;
1805 struct ieee80211_if_ap *bss = NULL;
1807 bdev = dev_get_by_index(if_id);
1809 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
1813 if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
1816 if (bss->dtim_count != 0)
1817 return NULL; /* send buffered bc/mc only after DTIM beacon */
1818 skb = skb_dequeue(&bss->ps_bc_buf);
1819 memset(control, 0, sizeof(*control));
1822 local->total_ps_buffered--;
1824 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1825 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1826 /* more buffered multicast/broadcast frames ==> set MoreData
1827 * flag in IEEE 802.11 header to inform PS STAs */
1828 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1831 ieee80211_tx_prepare(&tx, skb, local->mdev, control);
1833 tx.u.tx.ps_buffered = 1;
1835 for (handler = local->tx_handlers; *handler != NULL; handler++) {
1836 res = (*handler)(&tx);
1837 if (res == TXRX_DROP || res == TXRX_QUEUED)
1841 skb = tx.skb; /* handlers are allowed to change skb */
1843 if (res == TXRX_DROP) {
1844 I802_DEBUG_INC(local->tx_handlers_drop);
1847 } else if (res == TXRX_QUEUED) {
1848 I802_DEBUG_INC(local->tx_handlers_queued);
1857 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
1859 static int __ieee80211_if_config(struct net_device *dev,
1860 struct sk_buff *beacon)
1862 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1863 struct ieee80211_local *local = dev->ieee80211_ptr;
1864 struct ieee80211_if_conf conf;
1866 if (!local->ops->config_interface || !netif_running(dev))
1869 memset(&conf, 0, sizeof(conf));
1870 conf.type = sdata->type;
1871 if (sdata->type == IEEE80211_IF_TYPE_STA ||
1872 sdata->type == IEEE80211_IF_TYPE_IBSS) {
1873 conf.bssid = sdata->u.sta.bssid;
1874 conf.ssid = sdata->u.sta.ssid;
1875 conf.ssid_len = sdata->u.sta.ssid_len;
1876 conf.generic_elem = sdata->u.sta.extra_ie;
1877 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
1878 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
1879 conf.ssid = sdata->u.ap.ssid;
1880 conf.ssid_len = sdata->u.ap.ssid_len;
1881 conf.generic_elem = sdata->u.ap.generic_elem;
1882 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
1883 conf.beacon = beacon;
1885 return local->ops->config_interface(local_to_hw(local),
1886 dev->ifindex, &conf);
1889 int ieee80211_if_config(struct net_device *dev)
1891 return __ieee80211_if_config(dev, NULL);
1894 int ieee80211_if_config_beacon(struct net_device *dev)
1896 struct ieee80211_local *local = dev->ieee80211_ptr;
1897 struct sk_buff *skb;
1899 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
1901 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, NULL);
1904 return __ieee80211_if_config(dev, skb);
1907 int ieee80211_hw_config(struct ieee80211_local *local)
1909 struct ieee80211_hw_mode *mode;
1912 #ifdef CONFIG_D80211_VERBOSE_DEBUG
1913 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
1914 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
1915 local->hw.conf.phymode);
1916 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
1918 if (local->ops->config)
1919 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
1921 list_for_each_entry(mode, &local->modes_list, list) {
1922 if (mode->mode == local->hw.conf.phymode) {
1923 if (local->curr_rates != mode->rates)
1924 rate_control_clear(local);
1925 local->curr_rates = mode->rates;
1926 local->num_curr_rates = mode->num_rates;
1927 ieee80211_prepare_rates(local);
1936 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
1938 /* FIX: what would be proper limits for MTU?
1939 * This interface uses 802.3 frames. */
1940 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
1941 printk(KERN_WARNING "%s: invalid MTU %d\n",
1942 dev->name, new_mtu);
1946 #ifdef CONFIG_D80211_VERBOSE_DEBUG
1947 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
1948 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
1954 static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
1956 /* FIX: what would be proper limits for MTU?
1957 * This interface uses 802.11 frames. */
1958 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
1959 printk(KERN_WARNING "%s: invalid MTU %d\n",
1960 dev->name, new_mtu);
1964 #ifdef CONFIG_D80211_VERBOSE_DEBUG
1965 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
1966 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
1972 static void ieee80211_tx_timeout(struct net_device *dev)
1974 struct ieee80211_local *local = dev->ieee80211_ptr;
1976 printk(KERN_WARNING "%s: resetting interface.\n", dev->name);
1978 if (local->ops->reset(local_to_hw(local)))
1979 printk(KERN_ERR "%s: failed to reset interface.\n", dev->name);
1981 netif_wake_queue(dev);
1985 static int ieee80211_set_mac_address(struct net_device *dev, void *addr)
1987 struct sockaddr *a = addr;
1989 if (netif_running(dev))
1992 memcpy(dev->dev_addr, a->sa_data, ETH_ALEN);
1996 static void ieee80211_set_multicast_list(struct net_device *dev)
1998 struct ieee80211_local *local = dev->ieee80211_ptr;
1999 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2000 unsigned short flags;
2002 if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
2003 if (sdata->allmulti) {
2004 sdata->allmulti = 0;
2005 local->iff_allmultis--;
2007 sdata->allmulti = 1;
2008 local->iff_allmultis++;
2011 if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
2012 if (sdata->promisc) {
2014 local->iff_promiscs--;
2017 local->iff_promiscs++;
2020 if (dev->mc_count != sdata->mc_count) {
2021 local->mc_count = local->mc_count - sdata->mc_count +
2023 sdata->mc_count = dev->mc_count;
2025 if (local->ops->set_multicast_list) {
2026 flags = local->mdev->flags;
2027 if (local->iff_allmultis)
2028 flags |= IFF_ALLMULTI;
2029 if (local->iff_promiscs)
2030 flags |= IFF_PROMISC;
2031 local->ops->set_multicast_list(local_to_hw(local), flags,
2036 struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
2037 struct dev_mc_list *prev,
2040 struct ieee80211_local *local = hw_to_local(hw);
2041 struct ieee80211_sub_if_data *sdata = *ptr;
2042 struct dev_mc_list *mc;
2048 if (!prev || !prev->next) {
2050 sdata = list_entry(sdata->list.next,
2051 struct ieee80211_sub_if_data, list);
2053 sdata = list_entry(local->sub_if_list.next,
2054 struct ieee80211_sub_if_data, list);
2055 if (&sdata->list != &local->sub_if_list)
2056 mc = sdata->dev->mc_list;
2065 EXPORT_SYMBOL(ieee80211_get_mc_list_item);
2067 static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
2069 struct ieee80211_sub_if_data *sdata;
2070 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2071 return &(sdata->stats);
2074 void ieee80211_if_shutdown(struct net_device *dev)
2076 struct ieee80211_local *local = dev->ieee80211_ptr;
2077 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2080 switch (sdata->type) {
2081 case IEEE80211_IF_TYPE_STA:
2082 case IEEE80211_IF_TYPE_IBSS:
2083 sdata->u.sta.state = IEEE80211_DISABLED;
2084 cancel_delayed_work(&sdata->u.sta.work);
2085 if (!local->ops->hw_scan &&
2086 local->scan_dev == sdata->dev) {
2087 local->sta_scanning = 0;
2088 cancel_delayed_work(&local->scan_work);
2089 flush_scheduled_work();
2090 /* see comment in ieee80211_unregister_hw to
2091 * understand why this works */
2092 local->scan_dev = NULL;
2094 flush_scheduled_work();
2099 static inline int identical_mac_addr_allowed(int type1, int type2)
2101 return (type1 == IEEE80211_IF_TYPE_MNTR ||
2102 type2 == IEEE80211_IF_TYPE_MNTR ||
2103 (type1 == IEEE80211_IF_TYPE_AP &&
2104 type2 == IEEE80211_IF_TYPE_WDS) ||
2105 (type1 == IEEE80211_IF_TYPE_WDS &&
2106 (type2 == IEEE80211_IF_TYPE_WDS ||
2107 type2 == IEEE80211_IF_TYPE_AP)) ||
2108 (type1 == IEEE80211_IF_TYPE_AP &&
2109 type2 == IEEE80211_IF_TYPE_VLAN) ||
2110 (type1 == IEEE80211_IF_TYPE_VLAN &&
2111 (type2 == IEEE80211_IF_TYPE_AP ||
2112 type2 == IEEE80211_IF_TYPE_VLAN)));
2115 static int ieee80211_master_open(struct net_device *dev)
2117 struct ieee80211_local *local = dev->ieee80211_ptr;
2118 struct ieee80211_sub_if_data *sdata;
2119 int res = -EOPNOTSUPP;
2121 list_for_each_entry(sdata, &local->sub_if_list, list) {
2122 if (sdata->dev != dev && netif_running(sdata->dev)) {
2124 tasklet_enable(&local->tx_pending_tasklet);
2131 static int ieee80211_master_stop(struct net_device *dev)
2133 struct ieee80211_local *local = dev->ieee80211_ptr;
2134 struct ieee80211_sub_if_data *sdata;
2136 tasklet_disable(&local->tx_pending_tasklet);
2137 list_for_each_entry(sdata, &local->sub_if_list, list) {
2138 if (sdata->dev != dev && netif_running(sdata->dev))
2144 static int ieee80211_mgmt_open(struct net_device *dev)
2146 struct ieee80211_local *local = dev->ieee80211_ptr;
2148 if (!netif_running(local->mdev))
2153 static int ieee80211_mgmt_stop(struct net_device *dev)
2158 /* Check if running monitor interfaces should go to a "soft monitor" mode
2159 * and switch them if necessary. */
2160 static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
2162 struct ieee80211_if_init_conf conf;
2164 if (local->open_count && local->open_count == local->monitors &&
2165 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2166 local->ops->remove_interface) {
2168 conf.type = IEEE80211_IF_TYPE_MNTR;
2169 conf.mac_addr = NULL;
2170 local->ops->remove_interface(local_to_hw(local), &conf);
2174 /* Check if running monitor interfaces should go to a "hard monitor" mode
2175 * and switch them if necessary. */
2176 static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
2178 struct ieee80211_if_init_conf conf;
2180 if (local->open_count && local->open_count == local->monitors &&
2181 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2182 local->ops->add_interface) {
2184 conf.type = IEEE80211_IF_TYPE_MNTR;
2185 conf.mac_addr = NULL;
2186 local->ops->add_interface(local_to_hw(local), &conf);
2190 static int ieee80211_open(struct net_device *dev)
2192 struct ieee80211_sub_if_data *sdata, *nsdata;
2193 struct ieee80211_local *local = dev->ieee80211_ptr;
2194 struct ieee80211_if_init_conf conf;
2197 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2198 list_for_each_entry(nsdata, &local->sub_if_list, list) {
2199 struct net_device *ndev = nsdata->dev;
2201 if (ndev != dev && ndev != local->mdev &&
2202 netif_running(ndev) &&
2203 memcmp(dev->dev_addr, ndev->dev_addr, ETH_ALEN) == 0 &&
2204 !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
2208 if (sdata->type == IEEE80211_IF_TYPE_WDS &&
2209 memcmp(sdata->u.wds.remote_addr, "\0\0\0\0\0\0", ETH_ALEN) == 0)
2212 if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
2213 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2214 /* run the interface in a "soft monitor" mode */
2216 local->open_count++;
2219 ieee80211_start_soft_monitor(local);
2221 if (local->ops->add_interface) {
2222 conf.if_id = dev->ifindex;
2223 conf.type = sdata->type;
2224 conf.mac_addr = dev->dev_addr;
2225 res = local->ops->add_interface(local_to_hw(local), &conf);
2227 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
2228 ieee80211_start_hard_monitor(local);
2232 if (sdata->type != IEEE80211_IF_TYPE_STA)
2234 if (local->open_count > 0)
2238 if (local->open_count == 0) {
2240 if (local->ops->open)
2241 res = local->ops->open(local_to_hw(local));
2243 res = dev_open(local->mdev);
2245 if (local->ops->stop)
2246 local->ops->stop(local_to_hw(local));
2247 } else if (local->apdev)
2248 dev_open(local->apdev);
2251 if (local->ops->remove_interface)
2252 local->ops->remove_interface(local_to_hw(local),
2256 ieee80211_init_scan(local);
2258 local->open_count++;
2260 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
2263 ieee80211_if_config(dev);
2265 netif_start_queue(dev);
2270 static int ieee80211_stop(struct net_device *dev)
2272 struct ieee80211_sub_if_data *sdata;
2273 struct ieee80211_local *local = dev->ieee80211_ptr;
2275 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2277 if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
2278 local->open_count > 1 &&
2279 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2280 /* remove "soft monitor" interface */
2281 local->open_count--;
2286 netif_stop_queue(dev);
2288 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
2291 local->open_count--;
2292 if (local->open_count == 0) {
2293 ieee80211_stop_scan(local);
2294 dev_close(local->mdev);
2296 dev_close(local->apdev);
2297 if (local->ops->stop)
2298 local->ops->stop(local_to_hw(local));
2300 if (local->ops->remove_interface) {
2301 struct ieee80211_if_init_conf conf;
2303 conf.if_id = dev->ifindex;
2304 conf.type = sdata->type;
2305 conf.mac_addr = dev->dev_addr;
2306 local->ops->remove_interface(local_to_hw(local), &conf);
2308 ieee80211_if_shutdown(dev);
2310 ieee80211_start_hard_monitor(local);
2316 static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
2318 memcpy(haddr, skb->mac.raw + 10, ETH_ALEN); /* addr2 */
2322 static inline int ieee80211_bssid_match(u8 *raddr, u8 *addr)
2324 return memcmp(raddr, addr, ETH_ALEN) == 0 ||
2325 is_broadcast_ether_addr(raddr);
2329 static ieee80211_txrx_result
2330 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
2332 struct net_device *dev = rx->dev;
2333 struct ieee80211_local *local = rx->local;
2334 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
2335 u16 fc, hdrlen, ethertype;
2339 struct sk_buff *skb = rx->skb, *skb2;
2340 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2343 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
2344 return TXRX_CONTINUE;
2346 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
2349 hdrlen = ieee80211_get_hdrlen(fc);
2351 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
2353 * IEEE 802.11 address fields:
2354 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
2355 * 0 0 DA SA BSSID n/a
2356 * 0 1 DA BSSID SA n/a
2357 * 1 0 BSSID SA DA n/a
2361 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
2362 case IEEE80211_FCTL_TODS:
2364 memcpy(dst, hdr->addr3, ETH_ALEN);
2365 memcpy(src, hdr->addr2, ETH_ALEN);
2367 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
2368 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
2369 printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
2370 MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
2371 dev->name, MAC_ARG(hdr->addr1),
2372 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
2376 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
2378 memcpy(dst, hdr->addr3, ETH_ALEN);
2379 memcpy(src, hdr->addr4, ETH_ALEN);
2381 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
2382 printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
2383 MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
2385 rx->dev->name, MAC_ARG(hdr->addr1),
2386 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
2387 MAC_ARG(hdr->addr4));
2391 case IEEE80211_FCTL_FROMDS:
2393 memcpy(dst, hdr->addr1, ETH_ALEN);
2394 memcpy(src, hdr->addr3, ETH_ALEN);
2396 if (sdata->type != IEEE80211_IF_TYPE_STA) {
2402 memcpy(dst, hdr->addr1, ETH_ALEN);
2403 memcpy(src, hdr->addr2, ETH_ALEN);
2405 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
2406 if (net_ratelimit()) {
2407 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
2408 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
2410 dev->name, MAC_ARG(hdr->addr1),
2411 MAC_ARG(hdr->addr2),
2412 MAC_ARG(hdr->addr3));
2419 payload = skb->data + hdrlen;
2421 if (unlikely(skb->len - hdrlen < 8)) {
2422 if (net_ratelimit()) {
2423 printk(KERN_DEBUG "%s: RX too short data frame "
2424 "payload\n", dev->name);
2429 ethertype = (payload[6] << 8) | payload[7];
2431 if (likely((memcmp(payload, rfc1042_header, 6) == 0 &&
2432 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
2433 memcmp(payload, bridge_tunnel_header, 6) == 0)) {
2434 /* remove RFC1042 or Bridge-Tunnel encapsulation and
2435 * replace EtherType */
2436 skb_pull(skb, hdrlen + 6);
2437 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
2438 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
2440 struct ethhdr *ehdr;
2442 skb_pull(skb, hdrlen);
2443 len = htons(skb->len);
2444 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
2445 memcpy(ehdr->h_dest, dst, ETH_ALEN);
2446 memcpy(ehdr->h_source, src, ETH_ALEN);
2447 ehdr->h_proto = len;
2453 sdata->stats.rx_packets++;
2454 sdata->stats.rx_bytes += skb->len;
2456 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
2457 || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
2458 if (is_multicast_ether_addr(skb->data)) {
2459 /* send multicast frames both to higher layers in
2460 * local net stack and back to the wireless media */
2461 skb2 = skb_copy(skb, GFP_ATOMIC);
2463 printk(KERN_DEBUG "%s: failed to clone "
2464 "multicast frame\n", dev->name);
2466 struct sta_info *dsta;
2467 dsta = sta_info_get(local, skb->data);
2468 if (dsta && !dsta->dev) {
2469 printk(KERN_DEBUG "Station with null dev "
2471 } else if (dsta && dsta->dev == dev) {
2472 /* Destination station is associated to this
2473 * AP, so send the frame directly to it and
2474 * do not pass the frame to local net stack.
2485 /* deliver to local stack */
2486 skb->protocol = eth_type_trans(skb, dev);
2487 memset(skb->cb, 0, sizeof(skb->cb));
2492 /* send to wireless media */
2493 skb2->protocol = __constant_htons(ETH_P_802_3);
2494 skb2->mac.raw = skb2->nh.raw = skb2->data;
2495 dev_queue_xmit(skb2);
2502 static struct ieee80211_rate *
2503 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
2505 struct ieee80211_hw_mode *mode;
2508 list_for_each_entry(mode, &local->modes_list, list) {
2509 if (mode->mode != phymode)
2511 for (r = 0; r < mode->num_rates; r++) {
2512 struct ieee80211_rate *rate = &mode->rates[r];
2513 if (rate->val == hw_rate ||
2514 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
2515 rate->val2 == hw_rate))
2524 ieee80211_fill_frame_info(struct ieee80211_local *local,
2525 struct ieee80211_frame_info *fi,
2526 struct ieee80211_rx_status *status)
2530 struct ieee80211_rate *rate;
2532 jiffies_to_timespec(status->hosttime, &ts);
2533 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
2535 fi->mactime = cpu_to_be64(status->mactime);
2536 switch (status->phymode) {
2537 case MODE_IEEE80211A:
2538 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
2540 case MODE_IEEE80211B:
2541 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
2543 case MODE_IEEE80211G:
2544 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
2546 case MODE_ATHEROS_TURBO:
2548 htonl(ieee80211_phytype_dsss_dot11_turbo);
2551 fi->phytype = 0xAAAAAAAA;
2554 fi->channel = htonl(status->channel);
2555 rate = ieee80211_get_rate(local, status->phymode,
2558 fi->datarate = htonl(rate->rate);
2559 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
2560 if (status->rate == rate->val)
2561 fi->preamble = htonl(2); /* long */
2562 else if (status->rate == rate->val2)
2563 fi->preamble = htonl(1); /* short */
2565 fi->preamble = htonl(0);
2567 fi->datarate = htonl(0);
2568 fi->preamble = htonl(0);
2571 fi->antenna = htonl(status->antenna);
2572 fi->priority = 0xffffffff; /* no clue */
2573 fi->ssi_type = htonl(ieee80211_ssi_raw);
2574 fi->ssi_signal = htonl(status->ssi);
2575 fi->ssi_noise = 0x00000000;
2578 /* clear everything because we really don't know.
2579 * the msg_type field isn't present on monitor frames
2580 * so we don't know whether it will be present or not,
2581 * but it's ok to not clear it since it'll be assigned
2583 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
2585 fi->ssi_type = htonl(ieee80211_ssi_none);
2587 fi->version = htonl(IEEE80211_FI_VERSION);
2588 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
2591 /* this routine is actually not just for this, but also
2592 * for pushing fake 'management' frames into userspace.
2593 * it shall be replaced by a netlink-based system. */
2595 ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
2596 struct ieee80211_rx_status *status, u32 msg_type)
2598 struct ieee80211_frame_info *fi;
2599 const size_t hlen = sizeof(struct ieee80211_frame_info);
2600 struct ieee80211_sub_if_data *sdata;
2602 skb->dev = local->apdev;
2604 sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
2606 if (skb_headroom(skb) < hlen) {
2607 I802_DEBUG_INC(local->rx_expand_skb_head);
2608 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
2614 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
2616 ieee80211_fill_frame_info(local, fi, status);
2617 fi->msg_type = htonl(msg_type);
2619 sdata->stats.rx_packets++;
2620 sdata->stats.rx_bytes += skb->len;
2622 skb->mac.raw = skb->data;
2623 skb->ip_summed = CHECKSUM_UNNECESSARY;
2624 skb->pkt_type = PACKET_OTHERHOST;
2625 skb->protocol = htons(ETH_P_802_2);
2626 memset(skb->cb, 0, sizeof(skb->cb));
2631 ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
2632 struct ieee80211_rx_status *status)
2634 struct ieee80211_local *local = dev->ieee80211_ptr;
2635 struct ieee80211_frame_info *fi;
2636 struct ieee80211_sub_if_data *sdata;
2637 const size_t hlen = sizeof(struct ieee80211_frame_info)
2638 - sizeof(fi->msg_type);
2642 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2644 if (skb_headroom(skb) < hlen) {
2645 I802_DEBUG_INC(local->rx_expand_skb_head);
2646 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
2652 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
2654 ieee80211_fill_frame_info(local, fi, status);
2655 sdata->stats.rx_packets++;
2656 sdata->stats.rx_bytes += skb->len;
2658 skb->mac.raw = skb->data;
2659 skb->ip_summed = CHECKSUM_UNNECESSARY;
2660 skb->pkt_type = PACKET_OTHERHOST;
2661 skb->protocol = htons(ETH_P_802_2);
2662 memset(skb->cb, 0, sizeof(skb->cb));
2666 int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
2667 int radar, int radar_type)
2669 struct sk_buff *skb;
2670 struct ieee80211_radar_info *msg;
2671 struct ieee80211_local *local = hw_to_local(hw);
2676 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
2677 sizeof(struct ieee80211_radar_info));
2681 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
2683 msg = (struct ieee80211_radar_info *)
2684 skb_put(skb, sizeof(struct ieee80211_radar_info));
2685 msg->channel = channel;
2687 msg->radar_type = radar_type;
2689 ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
2692 EXPORT_SYMBOL(ieee80211_radar_status);
2694 int ieee80211_set_aid_for_sta(struct ieee80211_hw *hw, u8 *peer_address,
2697 struct sk_buff *skb;
2698 struct ieee80211_msg_set_aid_for_sta *msg;
2699 struct ieee80211_local *local = hw_to_local(hw);
2701 /* unlikely because if this event only happens for APs,
2702 * which require an open ap device. */
2703 if (unlikely(!local->apdev))
2706 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
2707 sizeof(struct ieee80211_msg_set_aid_for_sta));
2711 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
2713 msg = (struct ieee80211_msg_set_aid_for_sta *)
2714 skb_put(skb, sizeof(struct ieee80211_msg_set_aid_for_sta));
2715 memcpy(msg->sta_address, peer_address, ETH_ALEN);
2718 ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_set_aid_for_sta);
2721 EXPORT_SYMBOL(ieee80211_set_aid_for_sta);
2723 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
2725 struct ieee80211_sub_if_data *sdata;
2726 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
2729 atomic_inc(&sdata->bss->num_sta_ps);
2730 sta->flags |= WLAN_STA_PS;
2732 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
2733 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
2734 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
2735 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
2739 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
2741 struct ieee80211_local *local = dev->ieee80211_ptr;
2742 struct sk_buff *skb;
2744 struct ieee80211_sub_if_data *sdata;
2745 struct ieee80211_tx_packet_data *pkt_data;
2747 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
2749 atomic_dec(&sdata->bss->num_sta_ps);
2750 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
2752 if (!skb_queue_empty(&sta->ps_tx_buf)) {
2753 if (local->ops->set_tim)
2754 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
2756 bss_tim_clear(local, sdata->bss, sta->aid);
2758 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
2759 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
2760 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
2761 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
2762 /* Send all buffered frames to the station */
2763 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
2764 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
2766 pkt_data->requeue = 1;
2767 dev_queue_xmit(skb);
2769 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
2770 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
2771 local->total_ps_buffered--;
2773 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
2774 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
2775 "since STA not sleeping anymore\n", dev->name,
2776 MAC_ARG(sta->addr), sta->aid);
2777 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
2778 pkt_data->requeue = 1;
2779 dev_queue_xmit(skb);
2786 static ieee80211_txrx_result
2787 ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
2789 struct sk_buff *skb;
2790 int no_pending_pkts;
2792 if (likely(!rx->sta ||
2793 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
2794 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
2795 !rx->u.rx.ra_match))
2796 return TXRX_CONTINUE;
2798 skb = skb_dequeue(&rx->sta->tx_filtered);
2800 skb = skb_dequeue(&rx->sta->ps_tx_buf);
2802 rx->local->total_ps_buffered--;
2804 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
2805 skb_queue_empty(&rx->sta->ps_tx_buf);
2808 struct ieee80211_hdr *hdr =
2809 (struct ieee80211_hdr *) skb->data;
2811 /* tell TX path to send one frame even though the STA may
2812 * still remain is PS mode after this frame exchange */
2813 rx->sta->pspoll = 1;
2815 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
2816 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
2818 MAC_ARG(rx->sta->addr), rx->sta->aid,
2819 skb_queue_len(&rx->sta->ps_tx_buf));
2820 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
2822 /* Use MoreData flag to indicate whether there are more
2823 * buffered frames for this STA */
2824 if (no_pending_pkts) {
2825 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2826 rx->sta->flags &= ~WLAN_STA_TIM;
2828 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2830 dev_queue_xmit(skb);
2832 if (no_pending_pkts) {
2833 if (rx->local->ops->set_tim)
2834 rx->local->ops->set_tim(local_to_hw(rx->local),
2837 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
2839 #ifdef CONFIG_D80211_VERBOSE_PS_DEBUG
2840 } else if (!rx->u.rx.sent_ps_buffered) {
2841 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
2842 "though there is no buffered frames for it\n",
2843 rx->dev->name, MAC_ARG(rx->sta->addr));
2844 #endif /* CONFIG_D80211_VERBOSE_PS_DEBUG */
2848 /* Free PS Poll skb here instead of returning TXRX_DROP that would
2849 * count as an dropped frame. */
2850 dev_kfree_skb(rx->skb);
2856 static inline struct ieee80211_fragment_entry *
2857 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
2858 unsigned int frag, unsigned int seq, int rx_queue,
2859 struct sk_buff **skb)
2861 struct ieee80211_fragment_entry *entry;
2864 idx = sdata->fragment_next;
2865 entry = &sdata->fragments[sdata->fragment_next++];
2866 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
2867 sdata->fragment_next = 0;
2869 if (!skb_queue_empty(&entry->skb_list)) {
2870 #ifdef CONFIG_D80211_DEBUG
2871 struct ieee80211_hdr *hdr =
2872 (struct ieee80211_hdr *) entry->skb_list.next->data;
2873 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
2874 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
2875 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
2876 sdata->dev->name, idx,
2877 jiffies - entry->first_frag_time, entry->seq,
2878 entry->last_frag, MAC_ARG(hdr->addr1),
2879 MAC_ARG(hdr->addr2));
2880 #endif /* CONFIG_D80211_DEBUG */
2881 __skb_queue_purge(&entry->skb_list);
2884 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2886 entry->first_frag_time = jiffies;
2888 entry->rx_queue = rx_queue;
2889 entry->last_frag = frag;
2891 entry->extra_len = 0;
2897 static inline struct ieee80211_fragment_entry *
2898 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
2899 u16 fc, unsigned int frag, unsigned int seq,
2900 int rx_queue, struct ieee80211_hdr *hdr)
2902 struct ieee80211_fragment_entry *entry;
2905 idx = sdata->fragment_next;
2906 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2907 struct ieee80211_hdr *f_hdr;
2912 idx = IEEE80211_FRAGMENT_MAX - 1;
2914 entry = &sdata->fragments[idx];
2915 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2916 entry->rx_queue != rx_queue ||
2917 entry->last_frag + 1 != frag)
2920 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
2921 f_fc = le16_to_cpu(f_hdr->frame_control);
2923 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
2924 memcmp(hdr->addr1, f_hdr->addr1, ETH_ALEN) != 0 ||
2925 memcmp(hdr->addr2, f_hdr->addr2, ETH_ALEN) != 0)
2928 if (entry->first_frag_time + 2 * HZ < jiffies) {
2929 __skb_queue_purge(&entry->skb_list);
2939 static ieee80211_txrx_result
2940 ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
2942 struct ieee80211_hdr *hdr;
2944 unsigned int frag, seq;
2945 struct ieee80211_fragment_entry *entry;
2946 struct sk_buff *skb;
2948 hdr = (struct ieee80211_hdr *) rx->skb->data;
2949 sc = le16_to_cpu(hdr->seq_ctrl);
2950 frag = sc & IEEE80211_SCTL_FRAG;
2952 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
2953 (rx->skb)->len < 24 ||
2954 is_multicast_ether_addr(hdr->addr1))) {
2955 /* not fragmented */
2958 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2960 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2963 /* This is the first fragment of a new frame. */
2964 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
2965 rx->u.rx.queue, &(rx->skb));
2966 if (rx->key && rx->key->alg == ALG_CCMP &&
2967 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
2968 /* Store CCMP PN so that we can verify that the next
2969 * fragment has a sequential PN value. */
2971 memcpy(entry->last_pn,
2972 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
2978 /* This is a fragment for a frame that should already be pending in
2979 * fragment cache. Add this fragment to the end of the pending entry.
2981 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
2982 rx->u.rx.queue, hdr);
2984 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2988 /* Verify that MPDUs within one MSDU have sequential PN values.
2989 * (IEEE 802.11i, 8.3.3.4.5) */
2992 u8 pn[CCMP_PN_LEN], *rpn;
2993 if (!rx->key || rx->key->alg != ALG_CCMP)
2995 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
2996 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
3001 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
3002 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
3003 printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
3004 " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
3005 "(expected %02x%02x%02x%02x%02x%02x)\n",
3006 rx->dev->name, MAC_ARG(hdr->addr2),
3007 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
3008 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
3011 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
3014 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
3015 __skb_queue_tail(&entry->skb_list, rx->skb);
3016 entry->last_frag = frag;
3017 entry->extra_len += rx->skb->len;
3018 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
3023 rx->skb = __skb_dequeue(&entry->skb_list);
3024 if (skb_tailroom(rx->skb) < entry->extra_len) {
3025 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
3026 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
3028 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
3029 __skb_queue_purge(&entry->skb_list);
3033 while ((skb = __skb_dequeue(&entry->skb_list)))
3034 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
3036 /* Complete frame has been reassembled - process it now */
3041 rx->sta->rx_packets++;
3042 if (is_multicast_ether_addr(hdr->addr1))
3043 rx->local->dot11MulticastReceivedFrameCount++;
3045 ieee80211_led_rx(rx->local);
3046 return TXRX_CONTINUE;
3050 static ieee80211_txrx_result
3051 ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
3053 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
3054 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
3058 return TXRX_CONTINUE;
3062 static ieee80211_txrx_result
3063 ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
3065 struct ieee80211_hdr *hdr;
3067 hdr = (struct ieee80211_hdr *) rx->skb->data;
3069 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
3070 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
3071 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
3072 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
3074 if (rx->u.rx.ra_match) {
3075 rx->local->dot11FrameDuplicateCount++;
3076 rx->sta->num_duplicates++;
3080 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
3083 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
3084 rx->skb->len > FCS_LEN)
3085 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
3087 if (unlikely(rx->skb->len < 16)) {
3088 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
3092 if (!rx->u.rx.ra_match)
3093 rx->skb->pkt_type = PACKET_OTHERHOST;
3094 else if (memcmp(rx->dev->dev_addr, hdr->addr1, ETH_ALEN) == 0)
3095 rx->skb->pkt_type = PACKET_HOST;
3096 else if (is_multicast_ether_addr(hdr->addr1)) {
3097 if (is_broadcast_ether_addr(hdr->addr1))
3098 rx->skb->pkt_type = PACKET_BROADCAST;
3100 rx->skb->pkt_type = PACKET_MULTICAST;
3102 rx->skb->pkt_type = PACKET_OTHERHOST;
3104 /* Drop disallowed frame classes based on STA auth/assoc state;
3105 * IEEE 802.11, Chap 5.5.
3107 * 80211.o does filtering only based on association state, i.e., it
3108 * drops Class 3 frames from not associated stations. hostapd sends
3109 * deauth/disassoc frames when needed. In addition, hostapd is
3110 * responsible for filtering on both auth and assoc states.
3112 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
3113 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
3114 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
3115 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
3116 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
3117 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
3118 !(rx->fc & IEEE80211_FCTL_TODS) &&
3119 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
3120 || !rx->u.rx.ra_match) {
3121 /* Drop IBSS frames and frames for other hosts
3126 if (!rx->local->apdev)
3129 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3130 ieee80211_msg_sta_not_assoc);
3134 if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
3139 if (rx->sta && rx->sta->key && always_sta_key) {
3140 rx->key = rx->sta->key;
3142 if (rx->sta && rx->sta->key)
3143 rx->key = rx->sta->key;
3145 rx->key = rx->sdata->default_key;
3147 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3148 rx->fc & IEEE80211_FCTL_PROTECTED) {
3149 int keyidx = ieee80211_wep_get_keyidx(rx->skb);
3151 if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
3152 (!rx->sta || !rx->sta->key || keyidx > 0))
3153 rx->key = rx->sdata->keys[keyidx];
3156 if (!rx->u.rx.ra_match)
3158 printk(KERN_DEBUG "%s: RX WEP frame with "
3159 "unknown keyidx %d (A1=" MAC_FMT " A2="
3160 MAC_FMT " A3=" MAC_FMT ")\n",
3161 rx->dev->name, keyidx,
3162 MAC_ARG(hdr->addr1),
3163 MAC_ARG(hdr->addr2),
3164 MAC_ARG(hdr->addr3));
3165 if (!rx->local->apdev)
3168 rx->local, rx->skb, rx->u.rx.status,
3169 ieee80211_msg_wep_frame_unknown_key);
3175 if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
3176 rx->key->tx_rx_count++;
3177 if (unlikely(rx->local->key_tx_rx_threshold &&
3178 rx->key->tx_rx_count >
3179 rx->local->key_tx_rx_threshold)) {
3180 ieee80211_key_threshold_notify(rx->dev, rx->key,
3185 return TXRX_CONTINUE;
3189 static ieee80211_txrx_result
3190 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
3192 struct sta_info *sta = rx->sta;
3193 struct net_device *dev = rx->dev;
3194 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3197 return TXRX_CONTINUE;
3199 /* Update last_rx only for IBSS packets which are for the current
3200 * BSSID to avoid keeping the current IBSS network alive in cases where
3201 * other STAs are using different BSSID. */
3202 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
3203 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
3204 if (memcmp(bssid, rx->sdata->u.sta.bssid, ETH_ALEN) == 0)
3205 sta->last_rx = jiffies;
3207 if (!is_multicast_ether_addr(hdr->addr1) ||
3208 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
3209 /* Update last_rx only for unicast frames in order to prevent
3210 * the Probe Request frames (the only broadcast frames from a
3211 * STA in infrastructure mode) from keeping a connection alive.
3213 sta->last_rx = jiffies;
3216 if (!rx->u.rx.ra_match)
3217 return TXRX_CONTINUE;
3219 sta->rx_fragments++;
3220 sta->rx_bytes += rx->skb->len;
3221 sta->last_rssi = rx->u.rx.status->ssi;
3222 sta->last_signal = rx->u.rx.status->signal;
3223 sta->last_noise = rx->u.rx.status->noise;
3225 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
3226 /* Change STA power saving mode only in the end of a frame
3227 * exchange sequence */
3228 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
3229 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
3230 else if (!(sta->flags & WLAN_STA_PS) &&
3231 (rx->fc & IEEE80211_FCTL_PM))
3232 ap_sta_ps_start(dev, sta);
3235 /* Drop data::nullfunc frames silently, since they are used only to
3236 * control station power saving mode. */
3237 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3238 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
3239 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
3240 /* Update counter and free packet here to avoid counting this
3241 * as a dropped packed. */
3243 dev_kfree_skb(rx->skb);
3247 return TXRX_CONTINUE;
3251 static ieee80211_txrx_result
3252 ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
3254 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3255 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
3256 !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
3257 return TXRX_CONTINUE;
3259 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
3260 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
3261 rx->key->force_sw_encrypt) {
3262 u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
3264 rx->sta->wep_weak_iv_count++;
3268 return TXRX_CONTINUE;
3272 static ieee80211_txrx_result
3273 ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
3275 /* If the device handles decryption totally, skip this test */
3276 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3277 return TXRX_CONTINUE;
3279 if ((rx->key && rx->key->alg != ALG_WEP) ||
3280 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3281 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3282 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3283 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
3284 return TXRX_CONTINUE;
3287 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
3292 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
3293 rx->key->force_sw_encrypt) {
3294 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
3295 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
3296 "failed\n", rx->dev->name);
3299 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
3300 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
3302 skb_trim(rx->skb, rx->skb->len - 4);
3305 return TXRX_CONTINUE;
3309 static ieee80211_txrx_result
3310 ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
3312 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
3313 rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
3314 /* Pass both encrypted and unencrypted EAPOL frames to user
3315 * space for processing. */
3316 if (!rx->local->apdev)
3318 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3319 ieee80211_msg_normal);
3323 if (unlikely(rx->sdata->ieee802_1x &&
3324 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3325 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3326 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
3327 !ieee80211_is_eapol(rx->skb))) {
3328 #ifdef CONFIG_D80211_DEBUG
3329 struct ieee80211_hdr *hdr =
3330 (struct ieee80211_hdr *) rx->skb->data;
3331 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
3332 " (unauthorized port)\n", rx->dev->name,
3333 MAC_ARG(hdr->addr2));
3334 #endif /* CONFIG_D80211_DEBUG */
3338 return TXRX_CONTINUE;
3342 static ieee80211_txrx_result
3343 ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
3345 /* If the device handles decryption totally, skip this test */
3346 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3347 return TXRX_CONTINUE;
3349 /* Drop unencrypted frames if key is set. */
3350 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
3351 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3352 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3353 (rx->key || rx->sdata->drop_unencrypted) &&
3354 (rx->sdata->eapol == 0 ||
3355 !ieee80211_is_eapol(rx->skb)))) {
3356 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
3357 "encryption\n", rx->dev->name);
3360 return TXRX_CONTINUE;
3364 static ieee80211_txrx_result
3365 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
3367 struct ieee80211_sub_if_data *sdata;
3369 if (!rx->u.rx.ra_match)
3372 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
3373 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
3374 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
3375 !rx->local->user_space_mlme) {
3376 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
3378 /* Management frames are sent to hostapd for processing */
3379 if (!rx->local->apdev)
3381 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3382 ieee80211_msg_normal);
3388 static ieee80211_txrx_result
3389 ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
3391 struct ieee80211_local *local = rx->local;
3392 struct sk_buff *skb = rx->skb;
3394 if (unlikely(local->sta_scanning != 0)) {
3395 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
3399 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
3400 local->scan.txrx_count++;
3401 if (unlikely(local->scan.in_scan != 0 &&
3402 rx->u.rx.status->freq == local->scan.freq)) {
3403 struct ieee80211_hdr *hdr;
3406 local->scan.rx_packets++;
3408 hdr = (struct ieee80211_hdr *) skb->data;
3409 fc = le16_to_cpu(hdr->frame_control);
3411 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
3412 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON &&
3413 rx->dev == local->mdev) {
3414 local->scan.rx_beacon++;
3415 /* Need to trim FCS here because it is normally
3416 * removed only after this passive scan handler. */
3417 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
3418 rx->skb->len > FCS_LEN)
3419 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
3421 if (!rx->local->apdev)
3423 ieee80211_rx_mgmt(rx->local, rx->skb,
3425 ieee80211_msg_passive_scan);
3428 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
3433 if (unlikely(rx->u.rx.in_scan)) {
3434 /* scanning finished during invoking of handlers */
3435 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
3439 return TXRX_CONTINUE;
3443 static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
3450 fc = le16_to_cpu(hdr->frame_control);
3452 switch (fc & IEEE80211_FCTL_FTYPE) {
3453 case IEEE80211_FTYPE_DATA:
3454 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
3455 case IEEE80211_FCTL_TODS:
3457 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
3459 case IEEE80211_FCTL_FROMDS:
3465 case IEEE80211_FTYPE_MGMT:
3467 case IEEE80211_FTYPE_CTL:
3468 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
3477 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
3478 struct ieee80211_hdr *hdr,
3479 struct sta_info *sta,
3480 struct ieee80211_txrx_data *rx)
3484 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
3485 if (rx->skb->len >= hdrlen + 4)
3486 keyidx = rx->skb->data[hdrlen + 3] >> 6;
3490 /* TODO: verify that this is not triggered by fragmented
3491 * frames (hw does not verify MIC for them). */
3492 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
3493 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
3494 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
3497 /* Some hardware versions seem to generate incorrect
3498 * Michael MIC reports; ignore them to avoid triggering
3499 * countermeasures. */
3500 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3501 "error for unknown address " MAC_FMT "\n",
3502 dev->name, MAC_ARG(hdr->addr2));
3506 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
3507 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3508 "error for a frame with no ISWEP flag (src "
3509 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
3513 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3514 rx->sdata->type == IEEE80211_IF_TYPE_AP) {
3515 int keyidx = ieee80211_wep_get_keyidx(rx->skb);
3516 /* AP with Pairwise keys support should never receive Michael
3517 * MIC errors for non-zero keyidx because these are reserved
3518 * for group keys and only the AP is sending real multicast
3521 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
3522 "a frame with non-zero keyidx (%d) (src " MAC_FMT
3523 ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
3528 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3529 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3530 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
3531 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3532 "error for a frame that cannot be encrypted "
3533 "(fc=0x%04x) (src " MAC_FMT ")\n",
3534 dev->name, rx->fc, MAC_ARG(hdr->addr2));
3539 union iwreq_data wrqu;
3540 char *buf = kmalloc(128, GFP_ATOMIC);
3544 /* TODO: needed parameters: count, key type, TSC */
3545 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
3546 "keyid=%d %scast addr=" MAC_FMT ")",
3547 keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
3548 MAC_ARG(hdr->addr2));
3549 memset(&wrqu, 0, sizeof(wrqu));
3550 wrqu.data.length = strlen(buf);
3551 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
3555 /* TODO: consider verifying the MIC error report with software
3556 * implementation if we get too many spurious reports from the
3558 if (!rx->local->apdev)
3560 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3561 ieee80211_msg_michael_mic_failure);
3565 dev_kfree_skb(rx->skb);
3569 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
3570 struct ieee80211_local *local,
3571 ieee80211_rx_handler *handlers,
3572 struct ieee80211_txrx_data *rx,
3573 struct sta_info *sta)
3575 ieee80211_rx_handler *handler;
3576 ieee80211_txrx_result res = TXRX_DROP;
3578 for (handler = handlers; *handler != NULL; handler++) {
3579 res = (*handler)(rx);
3580 if (res != TXRX_CONTINUE) {
3581 if (res == TXRX_DROP) {
3582 I802_DEBUG_INC(local->rx_handlers_drop);
3586 if (res == TXRX_QUEUED)
3587 I802_DEBUG_INC(local->rx_handlers_queued);
3592 if (res == TXRX_DROP) {
3593 dev_kfree_skb(rx->skb);
3598 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
3599 ieee80211_rx_handler *handlers,
3600 struct ieee80211_txrx_data *rx,
3601 struct sta_info *sta)
3603 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
3605 dev_kfree_skb(rx->skb);
3609 * This is the receive path handler. It is called by a low level driver when an
3610 * 802.11 MPDU is received from the hardware.
3612 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3613 struct ieee80211_rx_status *status)
3615 struct ieee80211_local *local = hw_to_local(hw);
3616 struct ieee80211_sub_if_data *sdata;
3617 struct sta_info *sta;
3618 struct ieee80211_hdr *hdr;
3619 struct ieee80211_txrx_data rx;
3623 hdr = (struct ieee80211_hdr *) skb->data;
3624 memset(&rx, 0, sizeof(rx));
3628 rx.u.rx.status = status;
3629 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
3630 type = rx.fc & IEEE80211_FCTL_FTYPE;
3631 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
3632 local->dot11ReceivedFragmentCount++;
3633 multicast = is_multicast_ether_addr(hdr->addr1);
3636 sta = rx.sta = sta_info_get(local, hdr->addr2);
3638 sta = rx.sta = NULL;
3642 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
3645 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
3646 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
3650 if (unlikely(local->sta_scanning || local->scan.in_scan))
3651 rx.u.rx.in_scan = 1;
3653 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
3654 sta) != TXRX_CONTINUE)
3658 if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
3659 !local->iff_promiscs && !multicast) {
3660 rx.u.rx.ra_match = 1;
3661 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
3664 struct ieee80211_sub_if_data *prev = NULL;
3665 struct sk_buff *skb_new;
3666 u8 *bssid = ieee80211_get_bssid(hdr, skb->len);
3668 list_for_each_entry(sdata, &local->sub_if_list, list) {
3669 rx.u.rx.ra_match = 1;
3670 switch (sdata->type) {
3671 case IEEE80211_IF_TYPE_STA:
3674 if (!ieee80211_bssid_match(bssid,
3675 sdata->u.sta.bssid)) {
3676 if (!rx.u.rx.in_scan)
3678 rx.u.rx.ra_match = 0;
3679 } else if (!multicast &&
3680 memcmp(sdata->dev->dev_addr,
3681 hdr->addr1, ETH_ALEN) != 0) {
3682 if (!sdata->promisc)
3684 rx.u.rx.ra_match = 0;
3687 case IEEE80211_IF_TYPE_IBSS:
3690 if (!ieee80211_bssid_match(bssid,
3691 sdata->u.sta.bssid)) {
3692 if (!rx.u.rx.in_scan)
3694 rx.u.rx.ra_match = 0;
3695 } else if (!multicast &&
3696 memcmp(sdata->dev->dev_addr,
3697 hdr->addr1, ETH_ALEN) != 0) {
3698 if (!sdata->promisc)
3700 rx.u.rx.ra_match = 0;
3703 ieee80211_ibss_add_sta(local->mdev,
3706 /* FIXME: call with sdata->dev */
3708 case IEEE80211_IF_TYPE_AP:
3710 if (memcmp(sdata->dev->dev_addr,
3711 hdr->addr1, ETH_ALEN) != 0)
3713 } else if (!ieee80211_bssid_match(bssid,
3714 sdata->dev->dev_addr)) {
3715 if (!rx.u.rx.in_scan)
3717 rx.u.rx.ra_match = 0;
3719 if (sdata->dev == local->mdev &&
3721 /* do not receive anything via
3722 * master device when not scanning */
3725 case IEEE80211_IF_TYPE_WDS:
3727 (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
3729 if (memcmp(sdata->u.wds.remote_addr,
3730 hdr->addr2, ETH_ALEN) != 0)
3736 skb_new = skb_copy(skb, GFP_ATOMIC);
3738 if (net_ratelimit())
3739 printk(KERN_DEBUG "%s: failed to copy "
3740 "multicast frame for %s",
3741 local->mdev->name, prev->dev->name);
3747 ieee80211_invoke_rx_handlers(local,
3757 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
3767 EXPORT_SYMBOL(__ieee80211_rx);
3769 static ieee80211_txrx_result
3770 ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
3772 struct ieee80211_local *local = tx->local;
3773 struct sk_buff *skb = tx->skb;
3774 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
3775 u32 load = 0, hdrtime;
3777 /* TODO: this could be part of tx_status handling, so that the number
3778 * of retries would be known; TX rate should in that case be stored
3779 * somewhere with the packet */
3781 /* Estimate total channel use caused by this frame */
3783 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
3784 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
3786 if (local->hw.conf.phymode == MODE_IEEE80211A ||
3787 local->hw.conf.phymode == MODE_ATHEROS_TURBO ||
3788 local->hw.conf.phymode == MODE_ATHEROS_TURBOG ||
3789 (local->hw.conf.phymode == MODE_IEEE80211G &&
3790 tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
3791 hdrtime = CHAN_UTIL_HDR_SHORT;
3793 hdrtime = CHAN_UTIL_HDR_LONG;
3796 if (!is_multicast_ether_addr(hdr->addr1))
3799 if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
3800 load += 2 * hdrtime;
3801 else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
3804 load += skb->len * tx->u.tx.rate->rate_inv;
3806 if (tx->u.tx.extra_frag) {
3808 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
3809 load += 2 * hdrtime;
3810 load += tx->u.tx.extra_frag[i]->len *
3811 tx->u.tx.rate->rate;
3815 /* Divide channel_use by 8 to avoid wrapping around the counter */
3816 load >>= CHAN_UTIL_SHIFT;
3817 local->channel_use_raw += load;
3819 tx->sta->channel_use_raw += load;
3820 tx->sdata->channel_use_raw += load;
3822 return TXRX_CONTINUE;
3826 static ieee80211_txrx_result
3827 ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
3829 struct ieee80211_local *local = rx->local;
3830 struct sk_buff *skb = rx->skb;
3831 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
3832 u32 load = 0, hdrtime;
3833 struct ieee80211_rate *rate;
3836 /* Estimate total channel use caused by this frame */
3838 if (unlikely(local->num_curr_rates < 0))
3839 return TXRX_CONTINUE;
3841 rate = &local->curr_rates[0];
3842 for (i = 0; i < local->num_curr_rates; i++) {
3843 if (local->curr_rates[i].val == rx->u.rx.status->rate) {
3844 rate = &local->curr_rates[i];
3849 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
3850 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
3852 if (local->hw.conf.phymode == MODE_IEEE80211A ||
3853 local->hw.conf.phymode == MODE_ATHEROS_TURBO ||
3854 local->hw.conf.phymode == MODE_ATHEROS_TURBOG ||
3855 (local->hw.conf.phymode == MODE_IEEE80211G &&
3856 rate->flags & IEEE80211_RATE_ERP))
3857 hdrtime = CHAN_UTIL_HDR_SHORT;
3859 hdrtime = CHAN_UTIL_HDR_LONG;
3862 if (!is_multicast_ether_addr(hdr->addr1))
3865 load += skb->len * rate->rate_inv;
3867 /* Divide channel_use by 8 to avoid wrapping around the counter */
3868 load >>= CHAN_UTIL_SHIFT;
3869 local->channel_use_raw += load;
3871 rx->sta->channel_use_raw += load;
3872 rx->u.rx.load = load;
3874 return TXRX_CONTINUE;
3877 static ieee80211_txrx_result
3878 ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
3880 rx->sdata->channel_use_raw += rx->u.rx.load;
3881 return TXRX_CONTINUE;
3884 static void ieee80211_stat_refresh(unsigned long data)
3886 struct ieee80211_local *local = (struct ieee80211_local *) data;
3887 struct sta_info *sta;
3888 struct ieee80211_sub_if_data *sdata;
3890 if (!local->stat_time)
3893 /* go through all stations */
3894 spin_lock_bh(&local->sta_lock);
3895 list_for_each_entry(sta, &local->sta_list, list) {
3896 sta->channel_use = (sta->channel_use_raw / local->stat_time) /
3898 sta->channel_use_raw = 0;
3900 spin_unlock_bh(&local->sta_lock);
3902 /* go through all subinterfaces */
3903 list_for_each_entry(sdata, &local->sub_if_list, list) {
3904 sdata->channel_use = (sdata->channel_use_raw /
3905 local->stat_time) / CHAN_UTIL_PER_10MS;
3906 sdata->channel_use_raw = 0;
3909 /* hardware interface */
3910 local->channel_use = (local->channel_use_raw /
3911 local->stat_time) / CHAN_UTIL_PER_10MS;
3912 local->channel_use_raw = 0;
3914 local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
3915 add_timer(&local->stat_timer);
3919 /* This is a version of the rx handler that can be called from hard irq
3920 * context. Post the skb on the queue and schedule the tasklet */
3921 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
3922 struct ieee80211_rx_status *status)
3924 struct ieee80211_rx_status *saved;
3925 struct ieee80211_local *local = hw_to_local(hw);
3927 skb->dev = local->mdev;
3928 saved = kmalloc(sizeof(struct ieee80211_rx_status), GFP_ATOMIC);
3929 if (unlikely(!saved)) {
3930 if (net_ratelimit())
3931 printk(KERN_WARNING "%s: Not enough memory, "
3932 "dropping packet", skb->dev->name);
3933 /* should be dev_kfree_skb_irq, but due to this function being
3934 * named _irqsafe instead of just _irq we can't be sure that
3935 * people won't call it from non-irq contexts */
3936 dev_kfree_skb_any(skb);
3939 memcpy(saved, status, sizeof(struct ieee80211_rx_status));
3940 /* copy pointer to saved status into skb->cb for use by tasklet */
3941 memcpy(skb->cb, &saved, sizeof(saved));
3943 skb->pkt_type = ieee80211_rx_msg;
3944 skb_queue_tail(&local->skb_queue, skb);
3945 tasklet_schedule(&local->tasklet);
3947 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
3949 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
3950 struct sk_buff *skb,
3951 struct ieee80211_tx_status *status)
3953 struct ieee80211_local *local = hw_to_local(hw);
3954 struct ieee80211_tx_status *saved;
3957 skb->dev = local->mdev;
3958 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
3959 if (unlikely(!saved)) {
3960 if (net_ratelimit())
3961 printk(KERN_WARNING "%s: Not enough memory, "
3962 "dropping tx status", skb->dev->name);
3963 /* should be dev_kfree_skb_irq, but due to this function being
3964 * named _irqsafe instead of just _irq we can't be sure that
3965 * people won't call it from non-irq contexts */
3966 dev_kfree_skb_any(skb);
3969 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
3970 /* copy pointer to saved status into skb->cb for use by tasklet */
3971 memcpy(skb->cb, &saved, sizeof(saved));
3973 skb->pkt_type = ieee80211_tx_status_msg;
3974 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
3975 &local->skb_queue : &local->skb_queue_unreliable, skb);
3976 tmp = skb_queue_len(&local->skb_queue) +
3977 skb_queue_len(&local->skb_queue_unreliable);
3978 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
3979 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
3980 memcpy(&saved, skb->cb, sizeof(saved));
3982 dev_kfree_skb_irq(skb);
3984 I802_DEBUG_INC(local->tx_status_drop);
3986 tasklet_schedule(&local->tasklet);
3988 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
3990 static void ieee80211_tasklet_handler(unsigned long data)
3992 struct ieee80211_local *local = (struct ieee80211_local *) data;
3993 struct sk_buff *skb;
3994 struct ieee80211_rx_status *rx_status;
3995 struct ieee80211_tx_status *tx_status;
3997 while ((skb = skb_dequeue(&local->skb_queue)) ||
3998 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
3999 switch (skb->pkt_type) {
4000 case ieee80211_rx_msg:
4001 /* get pointer to saved status out of skb->cb */
4002 memcpy(&rx_status, skb->cb, sizeof(rx_status));
4003 /* Clear skb->type in order to not confuse kernel
4006 __ieee80211_rx(local_to_hw(local), skb, rx_status);
4009 case ieee80211_tx_status_msg:
4010 /* get pointer to saved status out of skb->cb */
4011 memcpy(&tx_status, skb->cb, sizeof(tx_status));
4013 ieee80211_tx_status(local_to_hw(local),
4017 default: /* should never get here! */
4018 printk(KERN_ERR "%s: Unknown message type (%d)\n",
4019 local->mdev->name, skb->pkt_type);
4027 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
4028 * make a prepared TX frame (one that has been given to hw) to look like brand
4029 * new IEEE 802.11 frame that is ready to go through TX processing again.
4030 * Also, tx_packet_data in cb is restored from tx_control. */
4031 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
4032 struct ieee80211_key *key,
4033 struct sk_buff *skb,
4034 struct ieee80211_tx_control *control)
4036 int hdrlen, iv_len, mic_len;
4037 struct ieee80211_tx_packet_data *pkt_data;
4039 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
4040 pkt_data->ifindex = control->ifindex;
4041 pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
4042 pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
4043 pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
4044 pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
4045 pkt_data->queue = control->queue;
4047 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
4054 iv_len = WEP_IV_LEN;
4055 mic_len = WEP_ICV_LEN;
4058 iv_len = TKIP_IV_LEN;
4059 mic_len = TKIP_ICV_LEN;
4062 iv_len = CCMP_HDR_LEN;
4063 mic_len = CCMP_MIC_LEN;
4069 if (skb->len >= mic_len && key->force_sw_encrypt)
4070 skb_trim(skb, skb->len - mic_len);
4071 if (skb->len >= iv_len && skb->len > hdrlen) {
4072 memmove(skb->data + iv_len, skb->data, hdrlen);
4073 skb_pull(skb, iv_len);
4078 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4079 u16 fc = le16_to_cpu(hdr->frame_control);
4080 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
4081 fc &= ~IEEE80211_STYPE_QOS_DATA;
4082 hdr->frame_control = cpu_to_le16(fc);
4083 memmove(skb->data + 2, skb->data, hdrlen - 2);
4090 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
4091 struct ieee80211_tx_status *status)
4093 struct sk_buff *skb2;
4094 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4095 struct ieee80211_local *local = hw_to_local(hw);
4101 "%s: ieee80211_tx_status called with NULL status\n",
4107 if (status->excessive_retries) {
4108 struct sta_info *sta;
4109 sta = sta_info_get(local, hdr->addr1);
4111 if (sta->flags & WLAN_STA_PS) {
4112 /* The STA is in power save mode, so assume
4113 * that this TX packet failed because of that.
4115 status->excessive_retries = 0;
4116 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
4122 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
4123 struct sta_info *sta;
4124 sta = sta_info_get(local, hdr->addr1);
4126 sta->tx_filtered_count++;
4128 /* Clear the TX filter mask for this STA when sending
4129 * the next packet. If the STA went to power save mode,
4130 * this will happen when it is waking up for the next
4132 sta->clear_dst_mask = 1;
4134 /* TODO: Is the WLAN_STA_PS flag always set here or is
4135 * the race between RX and TX status causing some
4136 * packets to be filtered out before 80211.o gets an
4137 * update for PS status? This seems to be the case, so
4138 * no changes are likely to be needed. */
4139 if (sta->flags & WLAN_STA_PS &&
4140 skb_queue_len(&sta->tx_filtered) <
4141 STA_MAX_TX_BUFFER) {
4142 ieee80211_remove_tx_extra(local, sta->key,
4145 skb_queue_tail(&sta->tx_filtered, skb);
4146 } else if (!(sta->flags & WLAN_STA_PS) &&
4147 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
4148 /* Software retry the packet once */
4149 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
4150 ieee80211_remove_tx_extra(local, sta->key,
4153 dev_queue_xmit(skb);
4155 if (net_ratelimit()) {
4156 printk(KERN_DEBUG "%s: dropped TX "
4157 "filtered frame queue_len=%d "
4162 !!(sta->flags & WLAN_STA_PS),
4171 /* FIXME: STUPID to call this with both local and local->mdev */
4172 rate_control_tx_status(local, local->mdev, skb, status);
4175 ieee80211_led_tx(local, 0);
4178 * Fragments are passed to low-level drivers as separate skbs, so these
4179 * are actually fragments, not frames. Update frame counters only for
4180 * the first fragment of the frame. */
4182 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
4183 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
4185 if (status->flags & IEEE80211_TX_STATUS_ACK) {
4187 local->dot11TransmittedFrameCount++;
4188 if (is_multicast_ether_addr(hdr->addr1))
4189 local->dot11MulticastTransmittedFrameCount++;
4190 if (status->retry_count > 0)
4191 local->dot11RetryCount++;
4192 if (status->retry_count > 1)
4193 local->dot11MultipleRetryCount++;
4196 /* This counter shall be incremented for an acknowledged MPDU
4197 * with an individual address in the address 1 field or an MPDU
4198 * with a multicast address in the address 1 field of type Data
4200 if (!is_multicast_ether_addr(hdr->addr1) ||
4201 type == IEEE80211_FTYPE_DATA ||
4202 type == IEEE80211_FTYPE_MGMT)
4203 local->dot11TransmittedFragmentCount++;
4206 local->dot11FailedCount++;
4209 if (!(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS)
4210 || unlikely(!local->apdev)) {
4215 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
4216 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
4218 /* skb was the original skb used for TX. Clone it and give the clone
4219 * to netif_rx(). Free original skb. */
4220 skb2 = skb_copy(skb, GFP_ATOMIC);
4228 /* Send frame to hostapd */
4229 ieee80211_rx_mgmt(local, skb, NULL, msg_type);
4231 EXPORT_SYMBOL(ieee80211_tx_status);
4233 /* TODO: implement register/unregister functions for adding TX/RX handlers
4234 * into ordered list */
4236 /* rx_pre handlers don't have dev and sdata fields available in
4237 * ieee80211_txrx_data */
4238 static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
4240 ieee80211_rx_h_parse_qos,
4241 ieee80211_rx_h_load_stats,
4245 static ieee80211_rx_handler ieee80211_rx_handlers[] =
4247 ieee80211_rx_h_if_stats,
4248 ieee80211_rx_h_monitor,
4249 ieee80211_rx_h_passive_scan,
4250 ieee80211_rx_h_check,
4251 ieee80211_rx_h_sta_process,
4252 ieee80211_rx_h_ccmp_decrypt,
4253 ieee80211_rx_h_tkip_decrypt,
4254 ieee80211_rx_h_wep_weak_iv_detection,
4255 ieee80211_rx_h_wep_decrypt,
4256 ieee80211_rx_h_defragment,
4257 ieee80211_rx_h_ps_poll,
4258 ieee80211_rx_h_michael_mic_verify,
4259 /* this must be after decryption - so header is counted in MPDU mic
4260 * must be before pae and data, so QOS_DATA format frames
4261 * are not passed to user space by these functions
4263 ieee80211_rx_h_remove_qos_control,
4264 ieee80211_rx_h_802_1x_pae,
4265 ieee80211_rx_h_drop_unencrypted,
4266 ieee80211_rx_h_data,
4267 ieee80211_rx_h_mgmt,
4271 static ieee80211_tx_handler ieee80211_tx_handlers[] =
4273 ieee80211_tx_h_check_assoc,
4274 ieee80211_tx_h_ps_buf,
4275 ieee80211_tx_h_select_key,
4276 ieee80211_tx_h_michael_mic_add,
4277 ieee80211_tx_h_fragment,
4278 ieee80211_tx_h_tkip_encrypt,
4279 ieee80211_tx_h_ccmp_encrypt,
4280 ieee80211_tx_h_wep_encrypt,
4281 ieee80211_tx_h_rate_ctrl,
4282 ieee80211_tx_h_misc,
4283 ieee80211_tx_h_load_stats,
4288 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
4290 struct ieee80211_local *local = dev->ieee80211_ptr;
4291 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4292 struct sta_info *sta;
4294 /* Remove STA entry for the old peer */
4295 sta = sta_info_get(local, sdata->u.wds.remote_addr);
4298 sta_info_free(sta, 0);
4300 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
4301 "peer " MAC_FMT "\n",
4302 dev->name, MAC_ARG(sdata->u.wds.remote_addr));
4305 /* Update WDS link data */
4306 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
4311 /* Must not be called for mdev and apdev */
4312 void ieee80211_if_setup(struct net_device *dev)
4315 dev->hard_start_xmit = ieee80211_subif_start_xmit;
4316 dev->wireless_handlers =
4317 (struct iw_handler_def *) &ieee80211_iw_handler_def;
4318 dev->do_ioctl = ieee80211_ioctl;
4319 dev->set_mac_address = ieee80211_set_mac_address;
4320 dev->set_multicast_list = ieee80211_set_multicast_list;
4321 dev->change_mtu = ieee80211_change_mtu;
4322 dev->tx_timeout = ieee80211_tx_timeout;
4323 dev->get_stats = ieee80211_get_stats;
4324 dev->open = ieee80211_open;
4325 dev->stop = ieee80211_stop;
4326 dev->tx_queue_len = 0;
4327 dev->uninit = ieee80211_if_reinit;
4328 dev->destructor = ieee80211_if_free;
4331 void ieee80211_if_mgmt_setup(struct net_device *dev)
4334 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
4335 dev->change_mtu = ieee80211_change_mtu_apdev;
4336 dev->get_stats = ieee80211_get_stats;
4337 dev->open = ieee80211_mgmt_open;
4338 dev->stop = ieee80211_mgmt_stop;
4339 dev->type = ARPHRD_IEEE80211_PRISM;
4340 dev->hard_header_parse = header_parse_80211;
4341 dev->tx_queue_len = 0;
4342 dev->uninit = ieee80211_if_reinit;
4343 dev->destructor = ieee80211_if_free;
4346 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
4349 struct rate_control_ref *ref, *old;
4353 if (local->open_count || netif_running(local->mdev) ||
4354 (local->apdev && netif_running(local->apdev)))
4357 ref = rate_control_alloc(name, local);
4359 printk(KERN_WARNING "%s: Failed to select rate control "
4360 "algorithm\n", local->mdev->name);
4363 res = rate_control_add_attrs(ref, &local->class_dev.kobj);
4365 printk(KERN_DEBUG "%s: Failed to register sysfs attributes "
4366 "for rate control\n", local->mdev->name);
4367 rate_control_put(ref);
4371 old = local->rate_ctrl;
4372 local->rate_ctrl = ref;
4374 rate_control_remove_attrs(ref, &local->class_dev.kobj);
4375 rate_control_put(old);
4376 sta_info_flush(local, NULL);
4379 printk(KERN_DEBUG "%s: Selected rate control "
4380 "algorithm '%s'\n", local->mdev->name,
4387 static void rate_control_deinitialize(struct ieee80211_local *local)
4389 struct rate_control_ref *ref;
4391 ref = local->rate_ctrl;
4392 local->rate_ctrl = NULL;
4393 rate_control_remove_attrs(ref, &local->class_dev.kobj);
4394 rate_control_put(ref);
4397 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
4398 const struct ieee80211_ops *ops)
4400 struct net_device *mdev;
4401 struct ieee80211_local *local;
4402 struct ieee80211_sub_if_data *sdata;
4405 local = ieee80211_dev_alloc(GFP_KERNEL);
4411 /* Ensure 32-byte alignment of our private data and hw private data.
4412 * Each net_device is followed by a sub_if_data which is used for
4413 * interface specific information.
4415 * Sample memory map looks something like:
4417 * 0000 *****************
4419 * 0160 *****************
4421 * 0b80 *****************
4423 * 1664 *****************
4425 priv_size = ((sizeof(struct ieee80211_sub_if_data) +
4426 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
4428 mdev = alloc_netdev(priv_size, "wmaster%d", ether_setup);
4430 ieee80211_dev_free(local);
4434 mdev->ieee80211_ptr = local;
4435 local->hw.priv = (char *)mdev->priv +
4436 ((sizeof(struct ieee80211_sub_if_data) +
4437 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4438 local->hw.queues = 1; /* default */
4441 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
4442 local->rx_handlers = ieee80211_rx_handlers;
4443 local->tx_handlers = ieee80211_tx_handlers;
4445 local->bridge_packets = 1;
4447 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
4448 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
4449 local->short_retry_limit = 7;
4450 local->long_retry_limit = 4;
4451 local->hw.conf.radio_enabled = 1;
4452 local->rate_ctrl_num_up = RATE_CONTROL_NUM_UP;
4453 local->rate_ctrl_num_down = RATE_CONTROL_NUM_DOWN;
4455 local->scan.in_scan = 0;
4456 local->enabled_modes = (unsigned int) -1;
4458 init_timer(&local->scan.timer); /* clear it out */
4460 INIT_LIST_HEAD(&local->modes_list);
4462 spin_lock_init(&local->sub_if_lock);
4463 INIT_LIST_HEAD(&local->sub_if_list);
4465 spin_lock_init(&local->generic_lock);
4466 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
4467 init_timer(&local->stat_timer);
4468 local->stat_timer.function = ieee80211_stat_refresh;
4469 local->stat_timer.data = (unsigned long) local;
4470 ieee80211_rx_bss_list_init(mdev);
4472 sta_info_init(local);
4474 mdev->hard_start_xmit = ieee80211_master_start_xmit;
4475 mdev->wireless_handlers =
4476 (struct iw_handler_def *) &ieee80211_iw_master_handler_def;
4477 mdev->do_ioctl = ieee80211_ioctl;
4478 mdev->change_mtu = ieee80211_change_mtu;
4479 mdev->tx_timeout = ieee80211_tx_timeout;
4480 mdev->get_stats = ieee80211_get_stats;
4481 mdev->open = ieee80211_master_open;
4482 mdev->stop = ieee80211_master_stop;
4483 mdev->type = ARPHRD_IEEE80211;
4484 mdev->hard_header_parse = header_parse_80211;
4486 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
4487 sdata->type = IEEE80211_IF_TYPE_AP;
4489 sdata->local = local;
4490 sdata->u.ap.force_unicast_rateidx = -1;
4491 sdata->u.ap.max_ratectrl_rateidx = -1;
4492 ieee80211_if_sdata_init(sdata);
4493 list_add_tail(&sdata->list, &local->sub_if_list);
4495 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
4496 (unsigned long)local);
4497 tasklet_disable(&local->tx_pending_tasklet);
4499 tasklet_init(&local->tasklet,
4500 ieee80211_tasklet_handler,
4501 (unsigned long) local);
4502 skb_queue_head_init(&local->skb_queue);
4503 skb_queue_head_init(&local->skb_queue_unreliable);
4505 return local_to_hw(local);
4507 EXPORT_SYMBOL(ieee80211_alloc_hw);
4509 int ieee80211_register_hw(struct ieee80211_hw *hw)
4511 struct ieee80211_local *local = hw_to_local(hw);
4512 struct net_device *sta_dev;
4515 result = ieee80211_dev_alloc_index(local);
4519 local->class_dev.dev = local->hw.dev;
4521 result = ieee80211_dev_sysfs_add(local);
4525 local->hw.conf.beacon_int = 1000;
4527 result = sta_info_start(local);
4531 if (hw->flags & IEEE80211_HW_FRAGLIST)
4532 local->mdev->features |= NETIF_F_FRAGLIST;
4534 result = dev_alloc_name(local->mdev, local->mdev->name);
4540 memcpy(local->mdev->dev_addr, local->hw.perm_addr, ETH_ALEN);
4541 SET_NETDEV_DEV(local->mdev, local->hw.dev);
4543 result = register_netdevice(local->mdev);
4548 result = sysfs_create_link(&local->class_dev.kobj,
4549 &local->mdev->class_dev.kobj,
4553 goto fail_masterlink;
4555 result = ieee80211_sysfs_add_netdevice(local->mdev);
4561 result = ieee80211_init_rate_ctrl_alg(local, NULL);
4564 printk(KERN_DEBUG "%s: Failed to initialize rate control "
4565 "algorithm\n", local->mdev->name);
4569 result = ieee80211_wep_init(local);
4572 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
4577 /* TODO: add rtnl locking around device creation and qdisc install */
4578 ieee80211_install_qdisc(local->mdev);
4580 /* add one default STA interface */
4582 result = ieee80211_if_add(local->mdev, "wlan%d", 1, &sta_dev);
4584 ieee80211_if_set_type(sta_dev, IEEE80211_IF_TYPE_STA);
4586 local->reg_state = IEEE80211_DEV_REGISTERED;
4589 ieee80211_led_init(local);
4594 rate_control_deinitialize(local);
4596 ieee80211_sysfs_remove_netdevice(local->mdev);
4598 sysfs_remove_link(&local->class_dev.kobj, "master");
4600 unregister_netdev(local->mdev);
4602 sta_info_stop(local);
4604 ieee80211_dev_sysfs_del(local);
4606 ieee80211_dev_free_index(local);
4609 EXPORT_SYMBOL(ieee80211_register_hw);
4611 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
4612 struct ieee80211_hw_mode *mode)
4614 struct ieee80211_local *local = hw_to_local(hw);
4615 struct ieee80211_rate *rate;
4618 INIT_LIST_HEAD(&mode->list);
4619 list_add_tail(&mode->list, &local->modes_list);
4621 local->hw_modes |= (1 << mode->mode);
4622 for (i = 0; i < mode->num_rates; i++) {
4623 rate = &(mode->rates[i]);
4624 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
4627 if (!local->curr_rates) {
4628 /* Default to this mode */
4629 local->hw.conf.phymode = mode->mode;
4630 local->curr_rates = mode->rates;
4631 local->num_curr_rates = mode->num_rates;
4632 ieee80211_prepare_rates(local);
4633 local->hw.conf.freq = mode->channels[0].freq;
4634 local->hw.conf.channel = mode->channels[0].chan;
4635 local->hw.conf.channel_val = mode->channels[0].val;
4638 ieee80211_init_client(local->mdev);
4642 EXPORT_SYMBOL(ieee80211_register_hwmode);
4644 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
4646 struct ieee80211_local *local = hw_to_local(hw);
4647 struct ieee80211_sub_if_data *sdata, *tmp;
4650 tasklet_disable(&local->tasklet);
4651 /* TODO: skb_queue should be empty here, no need to do anything? */
4654 local->reg_state = IEEE80211_DEV_UNREGISTERED;
4656 ieee80211_if_del_mgmt(local);
4658 sysfs_remove_link(&local->class_dev.kobj, "master");
4660 list_for_each_entry_safe(sdata, tmp, &local->sub_if_list, list)
4661 __ieee80211_if_del(local, sdata);
4665 if (local->stat_time)
4666 del_timer_sync(&local->stat_timer);
4667 if (!local->ops->hw_scan && local->scan_dev) {
4668 local->sta_scanning = 0;
4669 cancel_delayed_work(&local->scan_work);
4670 flush_scheduled_work();
4671 /* The scan_work is guaranteed not to be called at this
4672 * point. It is not scheduled and not running now. It can be
4673 * scheduled again only by sta_work (stopped by now) or under
4677 ieee80211_rx_bss_list_deinit(local->mdev);
4678 ieee80211_clear_tx_pending(local);
4679 sta_info_stop(local);
4680 rate_control_deinitialize(local);
4681 ieee80211_dev_sysfs_del(local);
4683 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
4684 kfree(local->supp_rates[i]);
4685 kfree(local->basic_rates[i]);
4688 if (skb_queue_len(&local->skb_queue)
4689 || skb_queue_len(&local->skb_queue_unreliable))
4690 printk(KERN_WARNING "%s: skb_queue not empty\n",
4692 skb_queue_purge(&local->skb_queue);
4693 skb_queue_purge(&local->skb_queue_unreliable);
4695 ieee80211_dev_free_index(local);
4696 ieee80211_wep_free(local);
4697 ieee80211_led_exit(local);
4699 EXPORT_SYMBOL(ieee80211_unregister_hw);
4701 void ieee80211_free_hw(struct ieee80211_hw *hw)
4703 struct ieee80211_local *local = hw_to_local(hw);
4705 ieee80211_if_free(local->mdev);
4706 ieee80211_dev_free(local);
4708 EXPORT_SYMBOL(ieee80211_free_hw);
4710 void ieee80211_release_hw(struct ieee80211_local *local)
4715 /* Perform netif operations on all configured interfaces */
4716 int ieee80211_netif_oper(struct ieee80211_hw *hw, Netif_Oper op)
4718 struct ieee80211_local *local = hw_to_local(hw);
4719 struct net_device *dev = local->mdev;
4723 netif_device_attach(dev);
4726 netif_device_detach(dev);
4729 netif_start_queue(dev);
4734 if (local->scan.in_scan == 0) {
4735 netif_wake_queue(dev);
4737 if (/* FIX: 802.11 qdisc in use */ 1)
4738 __netif_schedule(dev);
4742 case NETIF_IS_STOPPED:
4743 if (netif_queue_stopped(dev))
4746 case NETIF_UPDATE_TX_START:
4747 dev->trans_start = jiffies;
4753 EXPORT_SYMBOL(ieee80211_netif_oper);
4755 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
4757 struct ieee80211_local *local = hw_to_local(hw);
4759 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
4760 &local->state[queue])) {
4761 if (test_bit(IEEE80211_LINK_STATE_PENDING,
4762 &local->state[queue]))
4763 tasklet_schedule(&local->tx_pending_tasklet);
4765 __netif_schedule(local->mdev);
4768 EXPORT_SYMBOL(ieee80211_wake_queue);
4770 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
4772 struct ieee80211_local *local = hw_to_local(hw);
4774 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
4776 EXPORT_SYMBOL(ieee80211_stop_queue);
4778 void ieee80211_start_queues(struct ieee80211_hw *hw)
4780 struct ieee80211_local *local = hw_to_local(hw);
4783 for (i = 0; i < local->hw.queues; i++)
4784 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
4786 EXPORT_SYMBOL(ieee80211_start_queues);
4788 void ieee80211_stop_queues(struct ieee80211_hw *hw)
4790 struct ieee80211_local *local = hw_to_local(hw);
4793 for (i = 0; i < local->hw.queues; i++)
4794 ieee80211_stop_queue(hw, i);
4796 EXPORT_SYMBOL(ieee80211_stop_queues);
4798 struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
4800 struct ieee80211_sub_if_data *sdata;
4801 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4802 return &sdata->stats;
4805 static int __init ieee80211_init(void)
4807 struct sk_buff *skb;
4810 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
4812 if ((ret = ieee80211_sysfs_init())) {
4813 printk(KERN_WARNING "ieee80211_init: sysfs initialization "
4819 ret = ieee80211_wme_register();
4821 printk(KERN_DEBUG "ieee80211_init: failed to "
4822 "initialize WME (err=%d)\n", ret);
4823 ieee80211_sysfs_deinit();
4832 static void __exit ieee80211_exit(void)
4834 ieee80211_wme_unregister();
4835 ieee80211_sysfs_deinit();
4839 module_init(ieee80211_init);
4840 module_exit(ieee80211_exit);
4842 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
4843 MODULE_LICENSE("GPL");