RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / mac80211 / ieee80211.c
blobf0ca22874848a8efa5b4fa26d2667cc42ef5299e
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <net/iw_handler.h>
24 #include <linux/compiler.h>
25 #include <linux/bitmap.h>
26 #include <net/cfg80211.h>
27 #include <asm/unaligned.h>
29 #include "ieee80211_common.h"
30 #include "ieee80211_i.h"
31 #include "ieee80211_rate.h"
32 #include "wep.h"
33 #include "wpa.h"
34 #include "tkip.h"
35 #include "wme.h"
36 #include "aes_ccm.h"
37 #include "ieee80211_led.h"
38 #include "ieee80211_cfg.h"
39 #include "debugfs.h"
40 #include "debugfs_netdev.h"
41 #include "debugfs_key.h"
43 /* privid for wiphys to determine whether they belong to us or not */
44 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
46 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
47 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
48 static const unsigned char rfc1042_header[] =
49 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
51 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
52 static const unsigned char bridge_tunnel_header[] =
53 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
55 /* No encapsulation header if EtherType < 0x600 (=length) */
56 static const unsigned char eapol_header[] =
57 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
60 static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
61 struct ieee80211_hdr *hdr)
63 /* Set the sequence number for this frame. */
64 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
66 /* Increase the sequence number. */
67 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
70 struct ieee80211_key_conf *
71 ieee80211_key_data2conf(struct ieee80211_local *local,
72 const struct ieee80211_key *data)
74 struct ieee80211_key_conf *conf;
76 conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
77 if (!conf)
78 return NULL;
80 conf->hw_key_idx = data->hw_key_idx;
81 conf->alg = data->alg;
82 conf->keylen = data->keylen;
83 conf->flags = 0;
84 if (data->force_sw_encrypt)
85 conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
86 conf->keyidx = data->keyidx;
87 if (data->default_tx_key)
88 conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
89 if (local->default_wep_only)
90 conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
91 memcpy(conf->key, data->key, data->keylen);
93 return conf;
96 struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
97 int idx, size_t key_len, gfp_t flags)
99 struct ieee80211_key *key;
101 key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
102 if (!key)
103 return NULL;
104 kref_init(&key->kref);
105 return key;
108 static void ieee80211_key_release(struct kref *kref)
110 struct ieee80211_key *key;
112 key = container_of(kref, struct ieee80211_key, kref);
113 if (key->alg == ALG_CCMP)
114 ieee80211_aes_key_free(key->u.ccmp.tfm);
115 ieee80211_debugfs_key_remove(key);
116 kfree(key);
119 void ieee80211_key_free(struct ieee80211_key *key)
121 if (key)
122 kref_put(&key->kref, ieee80211_key_release);
125 static int rate_list_match(const int *rate_list, int rate)
127 int i;
129 if (!rate_list)
130 return 0;
132 for (i = 0; rate_list[i] >= 0; i++)
133 if (rate_list[i] == rate)
134 return 1;
136 return 0;
140 void ieee80211_prepare_rates(struct ieee80211_local *local,
141 struct ieee80211_hw_mode *mode)
143 int i;
145 for (i = 0; i < mode->num_rates; i++) {
146 struct ieee80211_rate *rate = &mode->rates[i];
148 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
149 IEEE80211_RATE_BASIC);
151 if (local->supp_rates[mode->mode]) {
152 if (!rate_list_match(local->supp_rates[mode->mode],
153 rate->rate))
154 continue;
157 rate->flags |= IEEE80211_RATE_SUPPORTED;
159 /* Use configured basic rate set if it is available. If not,
160 * use defaults that are sane for most cases. */
161 if (local->basic_rates[mode->mode]) {
162 if (rate_list_match(local->basic_rates[mode->mode],
163 rate->rate))
164 rate->flags |= IEEE80211_RATE_BASIC;
165 } else switch (mode->mode) {
166 case MODE_IEEE80211A:
167 if (rate->rate == 60 || rate->rate == 120 ||
168 rate->rate == 240)
169 rate->flags |= IEEE80211_RATE_BASIC;
170 break;
171 case MODE_IEEE80211B:
172 if (rate->rate == 10 || rate->rate == 20)
173 rate->flags |= IEEE80211_RATE_BASIC;
174 break;
175 case MODE_ATHEROS_TURBO:
176 if (rate->rate == 120 || rate->rate == 240 ||
177 rate->rate == 480)
178 rate->flags |= IEEE80211_RATE_BASIC;
179 break;
180 case MODE_IEEE80211G:
181 if (rate->rate == 10 || rate->rate == 20 ||
182 rate->rate == 55 || rate->rate == 110)
183 rate->flags |= IEEE80211_RATE_BASIC;
184 break;
187 /* Set ERP and MANDATORY flags based on phymode */
188 switch (mode->mode) {
189 case MODE_IEEE80211A:
190 if (rate->rate == 60 || rate->rate == 120 ||
191 rate->rate == 240)
192 rate->flags |= IEEE80211_RATE_MANDATORY;
193 break;
194 case MODE_IEEE80211B:
195 if (rate->rate == 10)
196 rate->flags |= IEEE80211_RATE_MANDATORY;
197 break;
198 case MODE_ATHEROS_TURBO:
199 break;
200 case MODE_IEEE80211G:
201 if (rate->rate == 10 || rate->rate == 20 ||
202 rate->rate == 55 || rate->rate == 110 ||
203 rate->rate == 60 || rate->rate == 120 ||
204 rate->rate == 240)
205 rate->flags |= IEEE80211_RATE_MANDATORY;
206 break;
208 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
209 rate->flags |= IEEE80211_RATE_ERP;
214 static void ieee80211_key_threshold_notify(struct net_device *dev,
215 struct ieee80211_key *key,
216 struct sta_info *sta)
218 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
219 struct sk_buff *skb;
220 struct ieee80211_msg_key_notification *msg;
222 /* if no one will get it anyway, don't even allocate it.
223 * unlikely because this is only relevant for APs
224 * where the device must be open... */
225 if (unlikely(!local->apdev))
226 return;
228 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
229 sizeof(struct ieee80211_msg_key_notification));
230 if (!skb)
231 return;
233 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
234 msg = (struct ieee80211_msg_key_notification *)
235 skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
236 msg->tx_rx_count = key->tx_rx_count;
237 memcpy(msg->ifname, dev->name, IFNAMSIZ);
238 if (sta)
239 memcpy(msg->addr, sta->addr, ETH_ALEN);
240 else
241 memset(msg->addr, 0xff, ETH_ALEN);
243 key->tx_rx_count = 0;
245 ieee80211_rx_mgmt(local, skb, NULL,
246 ieee80211_msg_key_threshold_notification);
250 static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
252 u16 fc;
254 if (len < 24)
255 return NULL;
257 fc = le16_to_cpu(hdr->frame_control);
259 switch (fc & IEEE80211_FCTL_FTYPE) {
260 case IEEE80211_FTYPE_DATA:
261 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
262 case IEEE80211_FCTL_TODS:
263 return hdr->addr1;
264 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
265 return NULL;
266 case IEEE80211_FCTL_FROMDS:
267 return hdr->addr2;
268 case 0:
269 return hdr->addr3;
271 break;
272 case IEEE80211_FTYPE_MGMT:
273 return hdr->addr3;
274 case IEEE80211_FTYPE_CTL:
275 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
276 return hdr->addr1;
277 else
278 return NULL;
281 return NULL;
284 int ieee80211_get_hdrlen(u16 fc)
286 int hdrlen = 24;
288 switch (fc & IEEE80211_FCTL_FTYPE) {
289 case IEEE80211_FTYPE_DATA:
290 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
291 hdrlen = 30; /* Addr4 */
293 * The QoS Control field is two bytes and its presence is
294 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
295 * hdrlen if that bit is set.
296 * This works by masking out the bit and shifting it to
297 * bit position 1 so the result has the value 0 or 2.
299 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
300 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
301 break;
302 case IEEE80211_FTYPE_CTL:
304 * ACK and CTS are 10 bytes, all others 16. To see how
305 * to get this condition consider
306 * subtype mask: 0b0000000011110000 (0x00F0)
307 * ACK subtype: 0b0000000011010000 (0x00D0)
308 * CTS subtype: 0b0000000011000000 (0x00C0)
309 * bits that matter: ^^^ (0x00E0)
310 * value of those: 0b0000000011000000 (0x00C0)
312 if ((fc & 0xE0) == 0xC0)
313 hdrlen = 10;
314 else
315 hdrlen = 16;
316 break;
319 return hdrlen;
321 EXPORT_SYMBOL(ieee80211_get_hdrlen);
323 int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
325 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
326 int hdrlen;
328 if (unlikely(skb->len < 10))
329 return 0;
330 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
331 if (unlikely(hdrlen > skb->len))
332 return 0;
333 return hdrlen;
335 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
337 static int ieee80211_get_radiotap_len(struct sk_buff *skb)
339 struct ieee80211_radiotap_header *hdr =
340 (struct ieee80211_radiotap_header *) skb->data;
342 return le16_to_cpu(get_unaligned(&hdr->it_len));
345 #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
346 static void ieee80211_dump_frame(const char *ifname, const char *title,
347 const struct sk_buff *skb)
349 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
350 u16 fc;
351 int hdrlen;
353 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
354 if (skb->len < 4) {
355 printk("\n");
356 return;
359 fc = le16_to_cpu(hdr->frame_control);
360 hdrlen = ieee80211_get_hdrlen(fc);
361 if (hdrlen > skb->len)
362 hdrlen = skb->len;
363 if (hdrlen >= 4)
364 printk(" FC=0x%04x DUR=0x%04x",
365 fc, le16_to_cpu(hdr->duration_id));
366 if (hdrlen >= 10)
367 printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
368 if (hdrlen >= 16)
369 printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
370 if (hdrlen >= 24)
371 printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
372 if (hdrlen >= 30)
373 printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
374 printk("\n");
376 #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
377 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
378 struct sk_buff *skb)
381 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
384 static int ieee80211_is_eapol(const struct sk_buff *skb)
386 const struct ieee80211_hdr *hdr;
387 u16 fc;
388 int hdrlen;
390 if (unlikely(skb->len < 10))
391 return 0;
393 hdr = (const struct ieee80211_hdr *) skb->data;
394 fc = le16_to_cpu(hdr->frame_control);
396 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
397 return 0;
399 hdrlen = ieee80211_get_hdrlen(fc);
401 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
402 memcmp(skb->data + hdrlen, eapol_header,
403 sizeof(eapol_header)) == 0))
404 return 1;
406 return 0;
410 static ieee80211_txrx_result
411 ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
413 struct rate_control_extra extra;
415 memset(&extra, 0, sizeof(extra));
416 extra.mode = tx->u.tx.mode;
417 extra.mgmt_data = tx->sdata &&
418 tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
419 extra.ethertype = tx->ethertype;
421 tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
422 &extra);
423 if (unlikely(extra.probe != NULL)) {
424 tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
425 tx->u.tx.probe_last_frag = 1;
426 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
427 tx->u.tx.rate = extra.probe;
428 } else {
429 tx->u.tx.control->alt_retry_rate = -1;
431 if (!tx->u.tx.rate)
432 return TXRX_DROP;
433 if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
434 tx->local->cts_protect_erp_frames && tx->fragmented &&
435 extra.nonerp) {
436 tx->u.tx.last_frag_rate = tx->u.tx.rate;
437 tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
439 tx->u.tx.rate = extra.nonerp;
440 tx->u.tx.control->rate = extra.nonerp;
441 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
442 } else {
443 tx->u.tx.last_frag_rate = tx->u.tx.rate;
444 tx->u.tx.control->rate = tx->u.tx.rate;
446 tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
447 if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
448 tx->local->short_preamble &&
449 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
450 tx->u.tx.short_preamble = 1;
451 tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
454 return TXRX_CONTINUE;
458 static ieee80211_txrx_result
459 ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
461 if (tx->sta)
462 tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
463 else
464 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
466 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
467 tx->key = NULL;
468 else if (tx->sta && tx->sta->key)
469 tx->key = tx->sta->key;
470 else if (tx->sdata->default_key)
471 tx->key = tx->sdata->default_key;
472 else if (tx->sdata->drop_unencrypted &&
473 !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
474 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
475 return TXRX_DROP;
476 } else
477 tx->key = NULL;
479 if (tx->key) {
480 tx->key->tx_rx_count++;
481 if (unlikely(tx->local->key_tx_rx_threshold &&
482 tx->key->tx_rx_count >
483 tx->local->key_tx_rx_threshold)) {
484 ieee80211_key_threshold_notify(tx->dev, tx->key,
485 tx->sta);
489 return TXRX_CONTINUE;
493 static ieee80211_txrx_result
494 ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
496 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
497 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
498 struct sk_buff **frags, *first, *frag;
499 int i;
500 u16 seq;
501 u8 *pos;
502 int frag_threshold = tx->local->fragmentation_threshold;
504 if (!tx->fragmented)
505 return TXRX_CONTINUE;
507 first = tx->skb;
509 hdrlen = ieee80211_get_hdrlen(tx->fc);
510 payload_len = first->len - hdrlen;
511 per_fragm = frag_threshold - hdrlen - FCS_LEN;
512 num_fragm = (payload_len + per_fragm - 1) / per_fragm;
514 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
515 if (!frags)
516 goto fail;
518 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
519 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
520 pos = first->data + hdrlen + per_fragm;
521 left = payload_len - per_fragm;
522 for (i = 0; i < num_fragm - 1; i++) {
523 struct ieee80211_hdr *fhdr;
524 size_t copylen;
526 if (left <= 0)
527 goto fail;
529 /* reserve enough extra head and tail room for possible
530 * encryption */
531 frag = frags[i] =
532 dev_alloc_skb(tx->local->hw.extra_tx_headroom +
533 frag_threshold +
534 IEEE80211_ENCRYPT_HEADROOM +
535 IEEE80211_ENCRYPT_TAILROOM);
536 if (!frag)
537 goto fail;
538 /* Make sure that all fragments use the same priority so
539 * that they end up using the same TX queue */
540 frag->priority = first->priority;
541 skb_reserve(frag, tx->local->hw.extra_tx_headroom +
542 IEEE80211_ENCRYPT_HEADROOM);
543 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
544 memcpy(fhdr, first->data, hdrlen);
545 if (i == num_fragm - 2)
546 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
547 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
548 copylen = left > per_fragm ? per_fragm : left;
549 memcpy(skb_put(frag, copylen), pos, copylen);
551 pos += copylen;
552 left -= copylen;
554 skb_trim(first, hdrlen + per_fragm);
556 tx->u.tx.num_extra_frag = num_fragm - 1;
557 tx->u.tx.extra_frag = frags;
559 return TXRX_CONTINUE;
561 fail:
562 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
563 if (frags) {
564 for (i = 0; i < num_fragm - 1; i++)
565 if (frags[i])
566 dev_kfree_skb(frags[i]);
567 kfree(frags);
569 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
570 return TXRX_DROP;
574 static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
576 if (tx->key->force_sw_encrypt) {
577 if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
578 return -1;
579 } else {
580 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
581 if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
582 if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
583 NULL)
584 return -1;
587 return 0;
591 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
593 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
595 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
596 if (tx->u.tx.extra_frag) {
597 struct ieee80211_hdr *fhdr;
598 int i;
599 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
600 fhdr = (struct ieee80211_hdr *)
601 tx->u.tx.extra_frag[i]->data;
602 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
608 static ieee80211_txrx_result
609 ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
611 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
612 u16 fc;
614 fc = le16_to_cpu(hdr->frame_control);
616 if (!tx->key || tx->key->alg != ALG_WEP ||
617 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
618 ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
619 (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
620 return TXRX_CONTINUE;
622 tx->u.tx.control->iv_len = WEP_IV_LEN;
623 tx->u.tx.control->icv_len = WEP_ICV_LEN;
624 ieee80211_tx_set_iswep(tx);
626 if (wep_encrypt_skb(tx, tx->skb) < 0) {
627 I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
628 return TXRX_DROP;
631 if (tx->u.tx.extra_frag) {
632 int i;
633 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
634 if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
635 I802_DEBUG_INC(tx->local->
636 tx_handlers_drop_wep);
637 return TXRX_DROP;
642 return TXRX_CONTINUE;
646 static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
647 int rate, int erp, int short_preamble)
649 int dur;
651 /* calculate duration (in microseconds, rounded up to next higher
652 * integer if it includes a fractional microsecond) to send frame of
653 * len bytes (does not include FCS) at the given rate. Duration will
654 * also include SIFS.
656 * rate is in 100 kbps, so divident is multiplied by 10 in the
657 * DIV_ROUND_UP() operations.
660 if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
661 local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
663 * OFDM:
665 * N_DBPS = DATARATE x 4
666 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
667 * (16 = SIGNAL time, 6 = tail bits)
668 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
670 * T_SYM = 4 usec
671 * 802.11a - 17.5.2: aSIFSTime = 16 usec
672 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
673 * signal ext = 6 usec
675 /* FIX: Atheros Turbo may have different (shorter) duration? */
676 dur = 16; /* SIFS + signal ext */
677 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
678 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
679 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
680 4 * rate); /* T_SYM x N_SYM */
681 } else {
683 * 802.11b or 802.11g with 802.11b compatibility:
684 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
685 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
687 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
688 * aSIFSTime = 10 usec
689 * aPreambleLength = 144 usec or 72 usec with short preamble
690 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
692 dur = 10; /* aSIFSTime = 10 usec */
693 dur += short_preamble ? (72 + 24) : (144 + 48);
695 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
698 return dur;
702 /* Exported duration function for driver use */
703 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
704 size_t frame_len, int rate)
706 struct ieee80211_local *local = hw_to_local(hw);
707 u16 dur;
708 int erp;
710 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
711 dur = ieee80211_frame_duration(local, frame_len, rate,
712 erp, local->short_preamble);
714 return cpu_to_le16(dur);
716 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
719 static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
720 int next_frag_len)
722 int rate, mrate, erp, dur, i;
723 struct ieee80211_rate *txrate = tx->u.tx.rate;
724 struct ieee80211_local *local = tx->local;
725 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
727 erp = txrate->flags & IEEE80211_RATE_ERP;
730 * data and mgmt (except PS Poll):
731 * - during CFP: 32768
732 * - during contention period:
733 * if addr1 is group address: 0
734 * if more fragments = 0 and addr1 is individual address: time to
735 * transmit one ACK plus SIFS
736 * if more fragments = 1 and addr1 is individual address: time to
737 * transmit next fragment plus 2 x ACK plus 3 x SIFS
739 * IEEE 802.11, 9.6:
740 * - control response frame (CTS or ACK) shall be transmitted using the
741 * same rate as the immediately previous frame in the frame exchange
742 * sequence, if this rate belongs to the PHY mandatory rates, or else
743 * at the highest possible rate belonging to the PHY rates in the
744 * BSSBasicRateSet
747 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
748 /* TODO: These control frames are not currently sent by
749 * 80211.o, but should they be implemented, this function
750 * needs to be updated to support duration field calculation.
752 * RTS: time needed to transmit pending data/mgmt frame plus
753 * one CTS frame plus one ACK frame plus 3 x SIFS
754 * CTS: duration of immediately previous RTS minus time
755 * required to transmit CTS and its SIFS
756 * ACK: 0 if immediately previous directed data/mgmt had
757 * more=0, with more=1 duration in ACK frame is duration
758 * from previous frame minus time needed to transmit ACK
759 * and its SIFS
760 * PS Poll: BIT(15) | BIT(14) | aid
762 return 0;
765 /* data/mgmt */
766 if (0 /* FIX: data/mgmt during CFP */)
767 return 32768;
769 if (group_addr) /* Group address as the destination - no ACK */
770 return 0;
772 /* Individual destination address:
773 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
774 * CTS and ACK frames shall be transmitted using the highest rate in
775 * basic rate set that is less than or equal to the rate of the
776 * immediately previous frame and that is using the same modulation
777 * (CCK or OFDM). If no basic rate set matches with these requirements,
778 * the highest mandatory rate of the PHY that is less than or equal to
779 * the rate of the previous frame is used.
780 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
782 rate = -1;
783 mrate = 10; /* use 1 Mbps if everything fails */
784 for (i = 0; i < mode->num_rates; i++) {
785 struct ieee80211_rate *r = &mode->rates[i];
786 if (r->rate > txrate->rate)
787 break;
789 if (IEEE80211_RATE_MODULATION(txrate->flags) !=
790 IEEE80211_RATE_MODULATION(r->flags))
791 continue;
793 if (r->flags & IEEE80211_RATE_BASIC)
794 rate = r->rate;
795 else if (r->flags & IEEE80211_RATE_MANDATORY)
796 mrate = r->rate;
798 if (rate == -1) {
799 /* No matching basic rate found; use highest suitable mandatory
800 * PHY rate */
801 rate = mrate;
804 /* Time needed to transmit ACK
805 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
806 * to closest integer */
808 dur = ieee80211_frame_duration(local, 10, rate, erp,
809 local->short_preamble);
811 if (next_frag_len) {
812 /* Frame is fragmented: duration increases with time needed to
813 * transmit next fragment plus ACK and 2 x SIFS. */
814 dur *= 2; /* ACK + SIFS */
815 /* next fragment */
816 dur += ieee80211_frame_duration(local, next_frag_len,
817 txrate->rate, erp,
818 local->short_preamble);
821 return dur;
825 static ieee80211_txrx_result
826 ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
828 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
829 u16 dur;
830 struct ieee80211_tx_control *control = tx->u.tx.control;
831 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
833 if (!is_multicast_ether_addr(hdr->addr1)) {
834 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
835 tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
836 control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
837 control->retry_limit =
838 tx->local->long_retry_limit;
839 } else {
840 control->retry_limit =
841 tx->local->short_retry_limit;
843 } else {
844 control->retry_limit = 1;
847 if (tx->fragmented) {
848 /* Do not use multiple retry rates when sending fragmented
849 * frames.
850 * TODO: The last fragment could still use multiple retry
851 * rates. */
852 control->alt_retry_rate = -1;
855 /* Use CTS protection for unicast frames sent using extended rates if
856 * there are associated non-ERP stations and RTS/CTS is not configured
857 * for the frame. */
858 if (mode->mode == MODE_IEEE80211G &&
859 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
860 tx->u.tx.unicast &&
861 tx->local->cts_protect_erp_frames &&
862 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
863 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
865 /* Setup duration field for the first fragment of the frame. Duration
866 * for remaining fragments will be updated when they are being sent
867 * to low-level driver in ieee80211_tx(). */
868 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
869 tx->fragmented ? tx->u.tx.extra_frag[0]->len :
871 hdr->duration_id = cpu_to_le16(dur);
873 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
874 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
875 struct ieee80211_rate *rate;
877 /* Do not use multiple retry rates when using RTS/CTS */
878 control->alt_retry_rate = -1;
880 /* Use min(data rate, max base rate) as CTS/RTS rate */
881 rate = tx->u.tx.rate;
882 while (rate > mode->rates &&
883 !(rate->flags & IEEE80211_RATE_BASIC))
884 rate--;
886 control->rts_cts_rate = rate->val;
887 control->rts_rate = rate;
890 if (tx->sta) {
891 tx->sta->tx_packets++;
892 tx->sta->tx_fragments++;
893 tx->sta->tx_bytes += tx->skb->len;
894 if (tx->u.tx.extra_frag) {
895 int i;
896 tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
897 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
898 tx->sta->tx_bytes +=
899 tx->u.tx.extra_frag[i]->len;
904 return TXRX_CONTINUE;
908 static ieee80211_txrx_result
909 ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
911 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
912 struct sk_buff *skb = tx->skb;
913 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
914 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
915 u32 sta_flags;
917 if (unlikely(tx->local->sta_scanning != 0) &&
918 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
919 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
920 return TXRX_DROP;
922 if (tx->u.tx.ps_buffered)
923 return TXRX_CONTINUE;
925 sta_flags = tx->sta ? tx->sta->flags : 0;
927 if (likely(tx->u.tx.unicast)) {
928 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
929 tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
930 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
931 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
932 printk(KERN_DEBUG "%s: dropped data frame to not "
933 "associated station " MAC_FMT "\n",
934 tx->dev->name, MAC_ARG(hdr->addr1));
935 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
936 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
937 return TXRX_DROP;
939 } else {
940 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
941 tx->local->num_sta == 0 &&
942 !tx->local->allow_broadcast_always &&
943 tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
945 * No associated STAs - no need to send multicast
946 * frames.
948 return TXRX_DROP;
950 return TXRX_CONTINUE;
953 if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
954 !(sta_flags & WLAN_STA_AUTHORIZED))) {
955 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
956 printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
957 " (unauthorized port)\n", tx->dev->name,
958 MAC_ARG(hdr->addr1));
959 #endif
960 I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
961 return TXRX_DROP;
964 return TXRX_CONTINUE;
967 static ieee80211_txrx_result
968 ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
970 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
972 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
973 ieee80211_include_sequence(tx->sdata, hdr);
975 return TXRX_CONTINUE;
978 /* This function is called whenever the AP is about to exceed the maximum limit
979 * of buffered frames for power saving STAs. This situation should not really
980 * happen often during normal operation, so dropping the oldest buffered packet
981 * from each queue should be OK to make some room for new frames. */
982 static void purge_old_ps_buffers(struct ieee80211_local *local)
984 int total = 0, purged = 0;
985 struct sk_buff *skb;
986 struct ieee80211_sub_if_data *sdata;
987 struct sta_info *sta;
989 read_lock(&local->sub_if_lock);
990 list_for_each_entry(sdata, &local->sub_if_list, list) {
991 struct ieee80211_if_ap *ap;
992 if (sdata->dev == local->mdev ||
993 sdata->type != IEEE80211_IF_TYPE_AP)
994 continue;
995 ap = &sdata->u.ap;
996 skb = skb_dequeue(&ap->ps_bc_buf);
997 if (skb) {
998 purged++;
999 dev_kfree_skb(skb);
1001 total += skb_queue_len(&ap->ps_bc_buf);
1003 read_unlock(&local->sub_if_lock);
1005 spin_lock_bh(&local->sta_lock);
1006 list_for_each_entry(sta, &local->sta_list, list) {
1007 skb = skb_dequeue(&sta->ps_tx_buf);
1008 if (skb) {
1009 purged++;
1010 dev_kfree_skb(skb);
1012 total += skb_queue_len(&sta->ps_tx_buf);
1014 spin_unlock_bh(&local->sta_lock);
1016 local->total_ps_buffered = total;
1017 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
1018 local->mdev->name, purged);
1022 static inline ieee80211_txrx_result
1023 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
1025 /* broadcast/multicast frame */
1026 /* If any of the associated stations is in power save mode,
1027 * the frame is buffered to be sent after DTIM beacon frame */
1028 if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
1029 tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
1030 tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
1031 !(tx->fc & IEEE80211_FCTL_ORDER)) {
1032 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
1033 purge_old_ps_buffers(tx->local);
1034 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
1035 AP_MAX_BC_BUFFER) {
1036 if (net_ratelimit()) {
1037 printk(KERN_DEBUG "%s: BC TX buffer full - "
1038 "dropping the oldest frame\n",
1039 tx->dev->name);
1041 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
1042 } else
1043 tx->local->total_ps_buffered++;
1044 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
1045 return TXRX_QUEUED;
1048 return TXRX_CONTINUE;
1052 static inline ieee80211_txrx_result
1053 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
1055 struct sta_info *sta = tx->sta;
1057 if (unlikely(!sta ||
1058 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
1059 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
1060 return TXRX_CONTINUE;
1062 if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
1063 struct ieee80211_tx_packet_data *pkt_data;
1064 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1065 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
1066 "before %d)\n",
1067 MAC_ARG(sta->addr), sta->aid,
1068 skb_queue_len(&sta->ps_tx_buf));
1069 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1070 sta->flags |= WLAN_STA_TIM;
1071 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
1072 purge_old_ps_buffers(tx->local);
1073 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
1074 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
1075 if (net_ratelimit()) {
1076 printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
1077 "buffer full - dropping oldest frame\n",
1078 tx->dev->name, MAC_ARG(sta->addr));
1080 dev_kfree_skb(old);
1081 } else
1082 tx->local->total_ps_buffered++;
1083 /* Queue frame to be sent after STA sends an PS Poll frame */
1084 if (skb_queue_empty(&sta->ps_tx_buf)) {
1085 if (tx->local->ops->set_tim)
1086 tx->local->ops->set_tim(local_to_hw(tx->local),
1087 sta->aid, 1);
1088 if (tx->sdata->bss)
1089 bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
1091 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
1092 pkt_data->jiffies = jiffies;
1093 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
1094 return TXRX_QUEUED;
1096 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1097 else if (unlikely(sta->flags & WLAN_STA_PS)) {
1098 printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
1099 "set -> send frame\n", tx->dev->name,
1100 MAC_ARG(sta->addr));
1102 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1103 sta->pspoll = 0;
1105 return TXRX_CONTINUE;
1109 static ieee80211_txrx_result
1110 ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
1112 if (unlikely(tx->u.tx.ps_buffered))
1113 return TXRX_CONTINUE;
1115 if (tx->u.tx.unicast)
1116 return ieee80211_tx_h_unicast_ps_buf(tx);
1117 else
1118 return ieee80211_tx_h_multicast_ps_buf(tx);
1122 static void inline
1123 __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1124 struct sk_buff *skb,
1125 struct net_device *dev,
1126 struct ieee80211_tx_control *control)
1128 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1129 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1130 int hdrlen;
1132 memset(tx, 0, sizeof(*tx));
1133 tx->skb = skb;
1134 tx->dev = dev; /* use original interface */
1135 tx->local = local;
1136 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1137 tx->sta = sta_info_get(local, hdr->addr1);
1138 tx->fc = le16_to_cpu(hdr->frame_control);
1139 control->power_level = local->hw.conf.power_level;
1140 tx->u.tx.control = control;
1141 tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
1142 if (is_multicast_ether_addr(hdr->addr1))
1143 control->flags |= IEEE80211_TXCTL_NO_ACK;
1144 else
1145 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
1146 tx->fragmented = local->fragmentation_threshold <
1147 IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
1148 skb->len + FCS_LEN > local->fragmentation_threshold &&
1149 (!local->ops->set_frag_threshold);
1150 if (!tx->sta)
1151 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1152 else if (tx->sta->clear_dst_mask) {
1153 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1154 tx->sta->clear_dst_mask = 0;
1156 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1157 if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
1158 control->antenna_sel_tx = tx->sta->antenna_sel_tx;
1159 hdrlen = ieee80211_get_hdrlen(tx->fc);
1160 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1161 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1162 tx->ethertype = (pos[0] << 8) | pos[1];
1164 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1168 static int inline is_ieee80211_device(struct net_device *dev,
1169 struct net_device *master)
1171 return (wdev_priv(dev->ieee80211_ptr) ==
1172 wdev_priv(master->ieee80211_ptr));
1175 /* Device in tx->dev has a reference added; use dev_put(tx->dev) when
1176 * finished with it. */
1177 static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1178 struct sk_buff *skb,
1179 struct net_device *mdev,
1180 struct ieee80211_tx_control *control)
1182 struct ieee80211_tx_packet_data *pkt_data;
1183 struct net_device *dev;
1185 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1186 dev = dev_get_by_index(pkt_data->ifindex);
1187 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1188 dev_put(dev);
1189 dev = NULL;
1191 if (unlikely(!dev))
1192 return -ENODEV;
1193 __ieee80211_tx_prepare(tx, skb, dev, control);
1194 return 0;
1197 static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
1198 int queue)
1200 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
1203 static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
1204 int queue)
1206 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
1209 #define IEEE80211_TX_OK 0
1210 #define IEEE80211_TX_AGAIN 1
1211 #define IEEE80211_TX_FRAG_AGAIN 2
1213 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1214 struct ieee80211_txrx_data *tx)
1216 struct ieee80211_tx_control *control = tx->u.tx.control;
1217 int ret, i;
1219 if (!ieee80211_qdisc_installed(local->mdev) &&
1220 __ieee80211_queue_stopped(local, 0)) {
1221 netif_stop_queue(local->mdev);
1222 return IEEE80211_TX_AGAIN;
1224 if (skb) {
1225 ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
1226 ret = local->ops->tx(local_to_hw(local), skb, control);
1227 if (ret)
1228 return IEEE80211_TX_AGAIN;
1229 local->mdev->trans_start = jiffies;
1230 ieee80211_led_tx(local, 1);
1232 if (tx->u.tx.extra_frag) {
1233 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1234 IEEE80211_TXCTL_USE_CTS_PROTECT |
1235 IEEE80211_TXCTL_CLEAR_DST_MASK |
1236 IEEE80211_TXCTL_FIRST_FRAGMENT);
1237 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1238 if (!tx->u.tx.extra_frag[i])
1239 continue;
1240 if (__ieee80211_queue_stopped(local, control->queue))
1241 return IEEE80211_TX_FRAG_AGAIN;
1242 if (i == tx->u.tx.num_extra_frag) {
1243 control->tx_rate = tx->u.tx.last_frag_hwrate;
1244 control->rate = tx->u.tx.last_frag_rate;
1245 if (tx->u.tx.probe_last_frag)
1246 control->flags |=
1247 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1248 else
1249 control->flags &=
1250 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1253 ieee80211_dump_frame(local->mdev->name,
1254 "TX to low-level driver",
1255 tx->u.tx.extra_frag[i]);
1256 ret = local->ops->tx(local_to_hw(local),
1257 tx->u.tx.extra_frag[i],
1258 control);
1259 if (ret)
1260 return IEEE80211_TX_FRAG_AGAIN;
1261 local->mdev->trans_start = jiffies;
1262 ieee80211_led_tx(local, 1);
1263 tx->u.tx.extra_frag[i] = NULL;
1265 kfree(tx->u.tx.extra_frag);
1266 tx->u.tx.extra_frag = NULL;
1268 return IEEE80211_TX_OK;
1271 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
1272 struct ieee80211_tx_control *control, int mgmt)
1274 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1275 struct sta_info *sta;
1276 ieee80211_tx_handler *handler;
1277 struct ieee80211_txrx_data tx;
1278 ieee80211_txrx_result res = TXRX_DROP;
1279 int ret, i;
1281 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1283 if (unlikely(skb->len < 10)) {
1284 dev_kfree_skb(skb);
1285 return 0;
1288 __ieee80211_tx_prepare(&tx, skb, dev, control);
1289 sta = tx.sta;
1290 tx.u.tx.mgmt_interface = mgmt;
1291 tx.u.tx.mode = local->hw.conf.mode;
1293 for (handler = local->tx_handlers; *handler != NULL; handler++) {
1294 res = (*handler)(&tx);
1295 if (res != TXRX_CONTINUE)
1296 break;
1299 skb = tx.skb; /* handlers are allowed to change skb */
1301 if (sta)
1302 sta_info_put(sta);
1304 if (unlikely(res == TXRX_DROP)) {
1305 I802_DEBUG_INC(local->tx_handlers_drop);
1306 goto drop;
1309 if (unlikely(res == TXRX_QUEUED)) {
1310 I802_DEBUG_INC(local->tx_handlers_queued);
1311 return 0;
1314 if (tx.u.tx.extra_frag) {
1315 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1316 int next_len, dur;
1317 struct ieee80211_hdr *hdr =
1318 (struct ieee80211_hdr *)
1319 tx.u.tx.extra_frag[i]->data;
1321 if (i + 1 < tx.u.tx.num_extra_frag) {
1322 next_len = tx.u.tx.extra_frag[i + 1]->len;
1323 } else {
1324 next_len = 0;
1325 tx.u.tx.rate = tx.u.tx.last_frag_rate;
1326 tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
1328 dur = ieee80211_duration(&tx, 0, next_len);
1329 hdr->duration_id = cpu_to_le16(dur);
1333 retry:
1334 ret = __ieee80211_tx(local, skb, &tx);
1335 if (ret) {
1336 struct ieee80211_tx_stored_packet *store =
1337 &local->pending_packet[control->queue];
1339 if (ret == IEEE80211_TX_FRAG_AGAIN)
1340 skb = NULL;
1341 set_bit(IEEE80211_LINK_STATE_PENDING,
1342 &local->state[control->queue]);
1343 smp_mb();
1344 /* When the driver gets out of buffers during sending of
1345 * fragments and calls ieee80211_stop_queue, there is
1346 * a small window between IEEE80211_LINK_STATE_XOFF and
1347 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1348 * gets available in that window (i.e. driver calls
1349 * ieee80211_wake_queue), we would end up with ieee80211_tx
1350 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1351 * continuing transmitting here when that situation is
1352 * possible to have happened. */
1353 if (!__ieee80211_queue_stopped(local, control->queue)) {
1354 clear_bit(IEEE80211_LINK_STATE_PENDING,
1355 &local->state[control->queue]);
1356 goto retry;
1358 memcpy(&store->control, control,
1359 sizeof(struct ieee80211_tx_control));
1360 store->skb = skb;
1361 store->extra_frag = tx.u.tx.extra_frag;
1362 store->num_extra_frag = tx.u.tx.num_extra_frag;
1363 store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
1364 store->last_frag_rate = tx.u.tx.last_frag_rate;
1365 store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
1367 return 0;
1369 drop:
1370 if (skb)
1371 dev_kfree_skb(skb);
1372 for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1373 if (tx.u.tx.extra_frag[i])
1374 dev_kfree_skb(tx.u.tx.extra_frag[i]);
1375 kfree(tx.u.tx.extra_frag);
1376 return 0;
1379 static void ieee80211_tx_pending(unsigned long data)
1381 struct ieee80211_local *local = (struct ieee80211_local *)data;
1382 struct net_device *dev = local->mdev;
1383 struct ieee80211_tx_stored_packet *store;
1384 struct ieee80211_txrx_data tx;
1385 int i, ret, reschedule = 0;
1387 netif_tx_lock_bh(dev);
1388 for (i = 0; i < local->hw.queues; i++) {
1389 if (__ieee80211_queue_stopped(local, i))
1390 continue;
1391 if (!__ieee80211_queue_pending(local, i)) {
1392 reschedule = 1;
1393 continue;
1395 store = &local->pending_packet[i];
1396 tx.u.tx.control = &store->control;
1397 tx.u.tx.extra_frag = store->extra_frag;
1398 tx.u.tx.num_extra_frag = store->num_extra_frag;
1399 tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
1400 tx.u.tx.last_frag_rate = store->last_frag_rate;
1401 tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
1402 ret = __ieee80211_tx(local, store->skb, &tx);
1403 if (ret) {
1404 if (ret == IEEE80211_TX_FRAG_AGAIN)
1405 store->skb = NULL;
1406 } else {
1407 clear_bit(IEEE80211_LINK_STATE_PENDING,
1408 &local->state[i]);
1409 reschedule = 1;
1412 netif_tx_unlock_bh(dev);
1413 if (reschedule) {
1414 if (!ieee80211_qdisc_installed(dev)) {
1415 if (!__ieee80211_queue_stopped(local, 0))
1416 netif_wake_queue(dev);
1417 } else
1418 netif_schedule(dev);
1422 static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1424 int i, j;
1425 struct ieee80211_tx_stored_packet *store;
1427 for (i = 0; i < local->hw.queues; i++) {
1428 if (!__ieee80211_queue_pending(local, i))
1429 continue;
1430 store = &local->pending_packet[i];
1431 kfree_skb(store->skb);
1432 for (j = 0; j < store->num_extra_frag; j++)
1433 kfree_skb(store->extra_frag[j]);
1434 kfree(store->extra_frag);
1435 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1439 static int ieee80211_master_start_xmit(struct sk_buff *skb,
1440 struct net_device *dev)
1442 struct ieee80211_tx_control control;
1443 struct ieee80211_tx_packet_data *pkt_data;
1444 struct net_device *odev = NULL;
1445 struct ieee80211_sub_if_data *osdata;
1446 int headroom;
1447 int ret;
1450 * copy control out of the skb so other people can use skb->cb
1452 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1453 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1455 if (pkt_data->ifindex)
1456 odev = dev_get_by_index(pkt_data->ifindex);
1457 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1458 dev_put(odev);
1459 odev = NULL;
1461 if (unlikely(!odev)) {
1462 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1463 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1464 "originating device\n", dev->name);
1465 #endif
1466 dev_kfree_skb(skb);
1467 return 0;
1469 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1471 headroom = osdata->local->hw.extra_tx_headroom +
1472 IEEE80211_ENCRYPT_HEADROOM;
1473 if (skb_headroom(skb) < headroom) {
1474 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1475 dev_kfree_skb(skb);
1476 return 0;
1480 control.ifindex = odev->ifindex;
1481 control.type = osdata->type;
1482 if (pkt_data->req_tx_status)
1483 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
1484 if (pkt_data->do_not_encrypt)
1485 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1486 if (pkt_data->requeue)
1487 control.flags |= IEEE80211_TXCTL_REQUEUE;
1488 control.queue = pkt_data->queue;
1490 ret = ieee80211_tx(odev, skb, &control,
1491 control.type == IEEE80211_IF_TYPE_MGMT);
1492 dev_put(odev);
1494 return ret;
1499 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1500 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1501 * @skb: packet to be sent
1502 * @dev: incoming interface
1504 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1505 * not be freed, and caller is responsible for either retrying later or freeing
1506 * skb).
1508 * This function takes in an Ethernet header and encapsulates it with suitable
1509 * IEEE 802.11 header based on which interface the packet is coming in. The
1510 * encapsulated packet will then be passed to master interface, wlan#.11, for
1511 * transmission (through low-level driver).
1513 static int ieee80211_subif_start_xmit(struct sk_buff *skb,
1514 struct net_device *dev)
1516 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1517 struct ieee80211_tx_packet_data *pkt_data;
1518 struct ieee80211_sub_if_data *sdata;
1519 int ret = 1, head_need;
1520 u16 ethertype, hdrlen, fc;
1521 struct ieee80211_hdr hdr;
1522 const u8 *encaps_data;
1523 int encaps_len, skip_header_bytes;
1524 int nh_pos, h_pos, no_encrypt = 0;
1525 struct sta_info *sta;
1527 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1528 if (unlikely(skb->len < ETH_HLEN)) {
1529 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1530 dev->name, skb->len);
1531 ret = 0;
1532 goto fail;
1535 nh_pos = skb_network_header(skb) - skb->data;
1536 h_pos = skb_transport_header(skb) - skb->data;
1538 /* convert Ethernet header to proper 802.11 header (based on
1539 * operation mode) */
1540 ethertype = (skb->data[12] << 8) | skb->data[13];
1541 /* TODO: handling for 802.1x authorized/unauthorized port */
1542 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1544 if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
1545 sdata->type == IEEE80211_IF_TYPE_VLAN)) {
1546 fc |= IEEE80211_FCTL_FROMDS;
1547 /* DA BSSID SA */
1548 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1549 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1550 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1551 hdrlen = 24;
1552 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
1553 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1554 /* RA TA DA SA */
1555 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1556 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1557 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1558 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1559 hdrlen = 30;
1560 } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
1561 fc |= IEEE80211_FCTL_TODS;
1562 /* BSSID SA DA */
1563 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1564 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1565 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1566 hdrlen = 24;
1567 } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
1568 /* DA SA BSSID */
1569 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1570 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1571 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1572 hdrlen = 24;
1573 } else {
1574 ret = 0;
1575 goto fail;
1578 /* receiver is QoS enabled, use a QoS type frame */
1579 sta = sta_info_get(local, hdr.addr1);
1580 if (sta) {
1581 if (sta->flags & WLAN_STA_WME) {
1582 fc |= IEEE80211_STYPE_QOS_DATA;
1583 hdrlen += 2;
1585 sta_info_put(sta);
1588 hdr.frame_control = cpu_to_le16(fc);
1589 hdr.duration_id = 0;
1590 hdr.seq_ctrl = 0;
1592 skip_header_bytes = ETH_HLEN;
1593 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1594 encaps_data = bridge_tunnel_header;
1595 encaps_len = sizeof(bridge_tunnel_header);
1596 skip_header_bytes -= 2;
1597 } else if (ethertype >= 0x600) {
1598 encaps_data = rfc1042_header;
1599 encaps_len = sizeof(rfc1042_header);
1600 skip_header_bytes -= 2;
1601 } else {
1602 encaps_data = NULL;
1603 encaps_len = 0;
1606 skb_pull(skb, skip_header_bytes);
1607 nh_pos -= skip_header_bytes;
1608 h_pos -= skip_header_bytes;
1610 /* TODO: implement support for fragments so that there is no need to
1611 * reallocate and copy payload; it might be enough to support one
1612 * extra fragment that would be copied in the beginning of the frame
1613 * data.. anyway, it would be nice to include this into skb structure
1614 * somehow
1616 * There are few options for this:
1617 * use skb->cb as an extra space for 802.11 header
1618 * allocate new buffer if not enough headroom
1619 * make sure that there is enough headroom in every skb by increasing
1620 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1621 * alloc_skb() (net/core/skbuff.c)
1623 head_need = hdrlen + encaps_len + local->hw.extra_tx_headroom;
1624 head_need -= skb_headroom(skb);
1626 /* We are going to modify skb data, so make a copy of it if happens to
1627 * be cloned. This could happen, e.g., with Linux bridge code passing
1628 * us broadcast frames. */
1630 if (head_need > 0 || skb_cloned(skb)) {
1631 #if 0
1632 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1633 "of headroom\n", dev->name, head_need);
1634 #endif
1636 if (skb_cloned(skb))
1637 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1638 else
1639 I802_DEBUG_INC(local->tx_expand_skb_head);
1640 /* Since we have to reallocate the buffer, make sure that there
1641 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1642 * before payload and 12 after). */
1643 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1644 12, GFP_ATOMIC)) {
1645 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1646 "\n", dev->name);
1647 goto fail;
1651 if (encaps_data) {
1652 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1653 nh_pos += encaps_len;
1654 h_pos += encaps_len;
1656 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1657 nh_pos += hdrlen;
1658 h_pos += hdrlen;
1660 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1661 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1662 pkt_data->ifindex = sdata->dev->ifindex;
1663 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1664 pkt_data->do_not_encrypt = no_encrypt;
1666 skb->dev = local->mdev;
1667 sdata->stats.tx_packets++;
1668 sdata->stats.tx_bytes += skb->len;
1670 /* Update skb pointers to various headers since this modified frame
1671 * is going to go through Linux networking code that may potentially
1672 * need things like pointer to IP header. */
1673 skb_set_mac_header(skb, 0);
1674 skb_set_network_header(skb, nh_pos);
1675 skb_set_transport_header(skb, h_pos);
1677 dev->trans_start = jiffies;
1678 dev_queue_xmit(skb);
1680 return 0;
1682 fail:
1683 if (!ret)
1684 dev_kfree_skb(skb);
1686 return ret;
1691 * This is the transmit routine for the 802.11 type interfaces
1692 * called by upper layers of the linux networking
1693 * stack when it has a frame to transmit
1695 static int
1696 ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
1698 struct ieee80211_sub_if_data *sdata;
1699 struct ieee80211_tx_packet_data *pkt_data;
1700 struct ieee80211_hdr *hdr;
1701 u16 fc;
1703 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1705 if (skb->len < 10) {
1706 dev_kfree_skb(skb);
1707 return 0;
1710 if (skb_headroom(skb) < sdata->local->hw.extra_tx_headroom) {
1711 if (pskb_expand_head(skb,
1712 sdata->local->hw.extra_tx_headroom, 0, GFP_ATOMIC)) {
1713 dev_kfree_skb(skb);
1714 return 0;
1718 hdr = (struct ieee80211_hdr *) skb->data;
1719 fc = le16_to_cpu(hdr->frame_control);
1721 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
1722 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1723 pkt_data->ifindex = sdata->dev->ifindex;
1724 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
1726 skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
1727 skb->dev = sdata->local->mdev;
1730 * We're using the protocol field of the the frame control header
1731 * to request TX callback for hostapd. BIT(1) is checked.
1733 if ((fc & BIT(1)) == BIT(1)) {
1734 pkt_data->req_tx_status = 1;
1735 fc &= ~BIT(1);
1736 hdr->frame_control = cpu_to_le16(fc);
1739 pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
1741 sdata->stats.tx_packets++;
1742 sdata->stats.tx_bytes += skb->len;
1744 dev_queue_xmit(skb);
1746 return 0;
1750 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1751 struct ieee80211_if_ap *bss,
1752 struct sk_buff *skb)
1754 u8 *pos, *tim;
1755 int aid0 = 0;
1756 int i, have_bits = 0, n1, n2;
1758 /* Generate bitmap for TIM only if there are any STAs in power save
1759 * mode. */
1760 spin_lock_bh(&local->sta_lock);
1761 if (atomic_read(&bss->num_sta_ps) > 0)
1762 /* in the hope that this is faster than
1763 * checking byte-for-byte */
1764 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1765 IEEE80211_MAX_AID+1);
1767 if (bss->dtim_count == 0)
1768 bss->dtim_count = bss->dtim_period - 1;
1769 else
1770 bss->dtim_count--;
1772 tim = pos = (u8 *) skb_put(skb, 6);
1773 *pos++ = WLAN_EID_TIM;
1774 *pos++ = 4;
1775 *pos++ = bss->dtim_count;
1776 *pos++ = bss->dtim_period;
1778 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1779 aid0 = 1;
1781 if (have_bits) {
1782 /* Find largest even number N1 so that bits numbered 1 through
1783 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1784 * (N2 + 1) x 8 through 2007 are 0. */
1785 n1 = 0;
1786 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1787 if (bss->tim[i]) {
1788 n1 = i & 0xfe;
1789 break;
1792 n2 = n1;
1793 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1794 if (bss->tim[i]) {
1795 n2 = i;
1796 break;
1800 /* Bitmap control */
1801 *pos++ = n1 | aid0;
1802 /* Part Virt Bitmap */
1803 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1805 tim[1] = n2 - n1 + 4;
1806 skb_put(skb, n2 - n1);
1807 } else {
1808 *pos++ = aid0; /* Bitmap control */
1809 *pos++ = 0; /* Part Virt Bitmap */
1811 spin_unlock_bh(&local->sta_lock);
1815 struct sk_buff * ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
1816 struct ieee80211_tx_control *control)
1818 struct ieee80211_local *local = hw_to_local(hw);
1819 struct sk_buff *skb;
1820 struct net_device *bdev;
1821 struct ieee80211_sub_if_data *sdata = NULL;
1822 struct ieee80211_if_ap *ap = NULL;
1823 struct ieee80211_rate *rate;
1824 struct rate_control_extra extra;
1825 u8 *b_head, *b_tail;
1826 int bh_len, bt_len;
1828 bdev = dev_get_by_index(if_id);
1829 if (bdev) {
1830 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
1831 ap = &sdata->u.ap;
1832 dev_put(bdev);
1835 if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
1836 !ap->beacon_head) {
1837 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1838 if (net_ratelimit())
1839 printk(KERN_DEBUG "no beacon data avail for idx=%d "
1840 "(%s)\n", if_id, bdev ? bdev->name : "N/A");
1841 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
1842 return NULL;
1845 /* Assume we are generating the normal beacon locally */
1846 b_head = ap->beacon_head;
1847 b_tail = ap->beacon_tail;
1848 bh_len = ap->beacon_head_len;
1849 bt_len = ap->beacon_tail_len;
1851 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1852 bh_len + bt_len + 256 /* maximum TIM len */);
1853 if (!skb)
1854 return NULL;
1856 skb_reserve(skb, local->hw.extra_tx_headroom);
1857 memcpy(skb_put(skb, bh_len), b_head, bh_len);
1859 ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
1861 ieee80211_beacon_add_tim(local, ap, skb);
1863 if (b_tail) {
1864 memcpy(skb_put(skb, bt_len), b_tail, bt_len);
1867 if (control) {
1868 memset(&extra, 0, sizeof(extra));
1869 extra.mode = local->oper_hw_mode;
1871 rate = rate_control_get_rate(local, local->mdev, skb, &extra);
1872 if (!rate) {
1873 if (net_ratelimit()) {
1874 printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
1875 "found\n", local->mdev->name);
1877 dev_kfree_skb(skb);
1878 return NULL;
1881 control->tx_rate = (local->short_preamble &&
1882 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
1883 rate->val2 : rate->val;
1884 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1885 control->power_level = local->hw.conf.power_level;
1886 control->flags |= IEEE80211_TXCTL_NO_ACK;
1887 control->retry_limit = 1;
1888 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1891 ap->num_beacons++;
1892 return skb;
1894 EXPORT_SYMBOL(ieee80211_beacon_get);
1896 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
1897 size_t frame_len,
1898 const struct ieee80211_tx_control *frame_txctl)
1900 struct ieee80211_local *local = hw_to_local(hw);
1901 struct ieee80211_rate *rate;
1902 int short_preamble = local->short_preamble;
1903 int erp;
1904 u16 dur;
1906 rate = frame_txctl->rts_rate;
1907 erp = !!(rate->flags & IEEE80211_RATE_ERP);
1909 /* CTS duration */
1910 dur = ieee80211_frame_duration(local, 10, rate->rate,
1911 erp, short_preamble);
1912 /* Data frame duration */
1913 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
1914 erp, short_preamble);
1915 /* ACK duration */
1916 dur += ieee80211_frame_duration(local, 10, rate->rate,
1917 erp, short_preamble);
1919 return cpu_to_le16(dur);
1921 EXPORT_SYMBOL(ieee80211_rts_duration);
1924 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
1925 size_t frame_len,
1926 const struct ieee80211_tx_control *frame_txctl)
1928 struct ieee80211_local *local = hw_to_local(hw);
1929 struct ieee80211_rate *rate;
1930 int short_preamble = local->short_preamble;
1931 int erp;
1932 u16 dur;
1934 rate = frame_txctl->rts_rate;
1935 erp = !!(rate->flags & IEEE80211_RATE_ERP);
1937 /* Data frame duration */
1938 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
1939 erp, short_preamble);
1940 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
1941 /* ACK duration */
1942 dur += ieee80211_frame_duration(local, 10, rate->rate,
1943 erp, short_preamble);
1946 return cpu_to_le16(dur);
1948 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
1950 void ieee80211_rts_get(struct ieee80211_hw *hw,
1951 const void *frame, size_t frame_len,
1952 const struct ieee80211_tx_control *frame_txctl,
1953 struct ieee80211_rts *rts)
1955 const struct ieee80211_hdr *hdr = frame;
1956 u16 fctl;
1958 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1959 rts->frame_control = cpu_to_le16(fctl);
1960 rts->duration = ieee80211_rts_duration(hw, frame_len, frame_txctl);
1961 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1962 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1964 EXPORT_SYMBOL(ieee80211_rts_get);
1966 void ieee80211_ctstoself_get(struct ieee80211_hw *hw,
1967 const void *frame, size_t frame_len,
1968 const struct ieee80211_tx_control *frame_txctl,
1969 struct ieee80211_cts *cts)
1971 const struct ieee80211_hdr *hdr = frame;
1972 u16 fctl;
1974 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1975 cts->frame_control = cpu_to_le16(fctl);
1976 cts->duration = ieee80211_ctstoself_duration(hw, frame_len, frame_txctl);
1977 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1979 EXPORT_SYMBOL(ieee80211_ctstoself_get);
1981 struct sk_buff *
1982 ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
1983 struct ieee80211_tx_control *control)
1985 struct ieee80211_local *local = hw_to_local(hw);
1986 struct sk_buff *skb;
1987 struct sta_info *sta;
1988 ieee80211_tx_handler *handler;
1989 struct ieee80211_txrx_data tx;
1990 ieee80211_txrx_result res = TXRX_DROP;
1991 struct net_device *bdev;
1992 struct ieee80211_sub_if_data *sdata;
1993 struct ieee80211_if_ap *bss = NULL;
1995 bdev = dev_get_by_index(if_id);
1996 if (bdev) {
1997 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
1998 bss = &sdata->u.ap;
1999 dev_put(bdev);
2001 if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
2002 return NULL;
2004 if (bss->dtim_count != 0)
2005 return NULL; /* send buffered bc/mc only after DTIM beacon */
2006 memset(control, 0, sizeof(*control));
2007 while (1) {
2008 skb = skb_dequeue(&bss->ps_bc_buf);
2009 if (!skb)
2010 return NULL;
2011 local->total_ps_buffered--;
2013 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2014 struct ieee80211_hdr *hdr =
2015 (struct ieee80211_hdr *) skb->data;
2016 /* more buffered multicast/broadcast frames ==> set
2017 * MoreData flag in IEEE 802.11 header to inform PS
2018 * STAs */
2019 hdr->frame_control |=
2020 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2023 if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
2024 break;
2025 dev_kfree_skb_any(skb);
2027 sta = tx.sta;
2028 tx.u.tx.ps_buffered = 1;
2030 for (handler = local->tx_handlers; *handler != NULL; handler++) {
2031 res = (*handler)(&tx);
2032 if (res == TXRX_DROP || res == TXRX_QUEUED)
2033 break;
2035 dev_put(tx.dev);
2036 skb = tx.skb; /* handlers are allowed to change skb */
2038 if (res == TXRX_DROP) {
2039 I802_DEBUG_INC(local->tx_handlers_drop);
2040 dev_kfree_skb(skb);
2041 skb = NULL;
2042 } else if (res == TXRX_QUEUED) {
2043 I802_DEBUG_INC(local->tx_handlers_queued);
2044 skb = NULL;
2047 if (sta)
2048 sta_info_put(sta);
2050 return skb;
2052 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
2054 static int __ieee80211_if_config(struct net_device *dev,
2055 struct sk_buff *beacon,
2056 struct ieee80211_tx_control *control)
2058 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2060 struct ieee80211_if_conf conf;
2061 static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2063 if (!local->ops->config_interface || !netif_running(dev))
2064 return 0;
2066 memset(&conf, 0, sizeof(conf));
2067 conf.type = sdata->type;
2068 if (sdata->type == IEEE80211_IF_TYPE_STA ||
2069 sdata->type == IEEE80211_IF_TYPE_IBSS) {
2070 if (local->sta_scanning &&
2071 local->scan_dev == dev)
2072 conf.bssid = scan_bssid;
2073 else
2074 conf.bssid = sdata->u.sta.bssid;
2075 conf.ssid = sdata->u.sta.ssid;
2076 conf.ssid_len = sdata->u.sta.ssid_len;
2077 conf.generic_elem = sdata->u.sta.extra_ie;
2078 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
2079 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
2080 conf.ssid = sdata->u.ap.ssid;
2081 conf.ssid_len = sdata->u.ap.ssid_len;
2082 conf.generic_elem = sdata->u.ap.generic_elem;
2083 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
2084 conf.beacon = beacon;
2085 conf.beacon_control = control;
2087 return local->ops->config_interface(local_to_hw(local),
2088 dev->ifindex, &conf);
2091 int ieee80211_if_config(struct net_device *dev)
2093 return __ieee80211_if_config(dev, NULL, NULL);
2096 int ieee80211_if_config_beacon(struct net_device *dev)
2098 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2099 struct ieee80211_tx_control control;
2100 struct sk_buff *skb;
2102 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
2103 return 0;
2104 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
2105 if (!skb)
2106 return -ENOMEM;
2107 return __ieee80211_if_config(dev, skb, &control);
2110 int ieee80211_hw_config(struct ieee80211_local *local)
2112 struct ieee80211_hw_mode *mode;
2113 struct ieee80211_channel *chan;
2114 int ret = 0;
2116 if (local->sta_scanning) {
2117 chan = local->scan_channel;
2118 mode = local->scan_hw_mode;
2119 } else {
2120 chan = local->oper_channel;
2121 mode = local->oper_hw_mode;
2124 local->hw.conf.channel = chan->chan;
2125 local->hw.conf.channel_val = chan->val;
2126 local->hw.conf.power_level = chan->power_level;
2127 local->hw.conf.freq = chan->freq;
2128 local->hw.conf.phymode = mode->mode;
2129 local->hw.conf.antenna_max = chan->antenna_max;
2130 local->hw.conf.chan = chan;
2131 local->hw.conf.mode = mode;
2133 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2134 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
2135 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
2136 local->hw.conf.phymode);
2137 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2139 if (local->ops->config)
2140 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
2142 return ret;
2146 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
2148 /* FIX: what would be proper limits for MTU?
2149 * This interface uses 802.3 frames. */
2150 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
2151 printk(KERN_WARNING "%s: invalid MTU %d\n",
2152 dev->name, new_mtu);
2153 return -EINVAL;
2156 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2157 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
2158 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2159 dev->mtu = new_mtu;
2160 return 0;
2164 static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
2166 /* FIX: what would be proper limits for MTU?
2167 * This interface uses 802.11 frames. */
2168 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
2169 printk(KERN_WARNING "%s: invalid MTU %d\n",
2170 dev->name, new_mtu);
2171 return -EINVAL;
2174 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2175 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
2176 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
2177 dev->mtu = new_mtu;
2178 return 0;
2181 enum netif_tx_lock_class {
2182 TX_LOCK_NORMAL,
2183 TX_LOCK_MASTER,
2186 static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
2188 spin_lock_nested(&dev->_xmit_lock, subclass);
2189 dev->xmit_lock_owner = smp_processor_id();
2192 static void ieee80211_set_multicast_list(struct net_device *dev)
2194 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2195 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2196 unsigned short flags;
2198 netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
2199 if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
2200 if (sdata->allmulti) {
2201 sdata->allmulti = 0;
2202 local->iff_allmultis--;
2203 } else {
2204 sdata->allmulti = 1;
2205 local->iff_allmultis++;
2208 if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
2209 if (sdata->promisc) {
2210 sdata->promisc = 0;
2211 local->iff_promiscs--;
2212 } else {
2213 sdata->promisc = 1;
2214 local->iff_promiscs++;
2217 if (dev->mc_count != sdata->mc_count) {
2218 local->mc_count = local->mc_count - sdata->mc_count +
2219 dev->mc_count;
2220 sdata->mc_count = dev->mc_count;
2222 if (local->ops->set_multicast_list) {
2223 flags = local->mdev->flags;
2224 if (local->iff_allmultis)
2225 flags |= IFF_ALLMULTI;
2226 if (local->iff_promiscs)
2227 flags |= IFF_PROMISC;
2228 read_lock(&local->sub_if_lock);
2229 local->ops->set_multicast_list(local_to_hw(local), flags,
2230 local->mc_count);
2231 read_unlock(&local->sub_if_lock);
2233 netif_tx_unlock(local->mdev);
2236 struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
2237 struct dev_mc_list *prev,
2238 void **ptr)
2240 struct ieee80211_local *local = hw_to_local(hw);
2241 struct ieee80211_sub_if_data *sdata = *ptr;
2242 struct dev_mc_list *mc;
2244 if (!prev) {
2245 WARN_ON(sdata);
2246 sdata = NULL;
2248 if (!prev || !prev->next) {
2249 if (sdata)
2250 sdata = list_entry(sdata->list.next,
2251 struct ieee80211_sub_if_data, list);
2252 else
2253 sdata = list_entry(local->sub_if_list.next,
2254 struct ieee80211_sub_if_data, list);
2255 if (&sdata->list != &local->sub_if_list)
2256 mc = sdata->dev->mc_list;
2257 else
2258 mc = NULL;
2259 } else
2260 mc = prev->next;
2262 *ptr = sdata;
2263 return mc;
2265 EXPORT_SYMBOL(ieee80211_get_mc_list_item);
2267 static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
2269 struct ieee80211_sub_if_data *sdata;
2270 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2271 return &(sdata->stats);
2274 static void ieee80211_if_shutdown(struct net_device *dev)
2276 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2277 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2279 ASSERT_RTNL();
2280 switch (sdata->type) {
2281 case IEEE80211_IF_TYPE_STA:
2282 case IEEE80211_IF_TYPE_IBSS:
2283 sdata->u.sta.state = IEEE80211_DISABLED;
2284 del_timer_sync(&sdata->u.sta.timer);
2285 skb_queue_purge(&sdata->u.sta.skb_queue);
2286 if (!local->ops->hw_scan &&
2287 local->scan_dev == sdata->dev) {
2288 local->sta_scanning = 0;
2289 cancel_delayed_work(&local->scan_work);
2291 flush_workqueue(local->hw.workqueue);
2292 break;
2296 static inline int identical_mac_addr_allowed(int type1, int type2)
2298 return (type1 == IEEE80211_IF_TYPE_MNTR ||
2299 type2 == IEEE80211_IF_TYPE_MNTR ||
2300 (type1 == IEEE80211_IF_TYPE_AP &&
2301 type2 == IEEE80211_IF_TYPE_WDS) ||
2302 (type1 == IEEE80211_IF_TYPE_WDS &&
2303 (type2 == IEEE80211_IF_TYPE_WDS ||
2304 type2 == IEEE80211_IF_TYPE_AP)) ||
2305 (type1 == IEEE80211_IF_TYPE_AP &&
2306 type2 == IEEE80211_IF_TYPE_VLAN) ||
2307 (type1 == IEEE80211_IF_TYPE_VLAN &&
2308 (type2 == IEEE80211_IF_TYPE_AP ||
2309 type2 == IEEE80211_IF_TYPE_VLAN)));
2312 static int ieee80211_master_open(struct net_device *dev)
2314 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2315 struct ieee80211_sub_if_data *sdata;
2316 int res = -EOPNOTSUPP;
2318 read_lock(&local->sub_if_lock);
2319 list_for_each_entry(sdata, &local->sub_if_list, list) {
2320 if (sdata->dev != dev && netif_running(sdata->dev)) {
2321 res = 0;
2322 break;
2325 read_unlock(&local->sub_if_lock);
2326 return res;
2329 static int ieee80211_master_stop(struct net_device *dev)
2331 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2332 struct ieee80211_sub_if_data *sdata;
2334 read_lock(&local->sub_if_lock);
2335 list_for_each_entry(sdata, &local->sub_if_list, list)
2336 if (sdata->dev != dev && netif_running(sdata->dev))
2337 dev_close(sdata->dev);
2338 read_unlock(&local->sub_if_lock);
2340 return 0;
2343 static int ieee80211_mgmt_open(struct net_device *dev)
2345 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2347 if (!netif_running(local->mdev))
2348 return -EOPNOTSUPP;
2349 return 0;
2352 static int ieee80211_mgmt_stop(struct net_device *dev)
2354 return 0;
2357 /* Check if running monitor interfaces should go to a "soft monitor" mode
2358 * and switch them if necessary. */
2359 static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
2361 struct ieee80211_if_init_conf conf;
2363 if (local->open_count && local->open_count == local->monitors &&
2364 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2365 local->ops->remove_interface) {
2366 conf.if_id = -1;
2367 conf.type = IEEE80211_IF_TYPE_MNTR;
2368 conf.mac_addr = NULL;
2369 local->ops->remove_interface(local_to_hw(local), &conf);
2373 /* Check if running monitor interfaces should go to a "hard monitor" mode
2374 * and switch them if necessary. */
2375 static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
2377 struct ieee80211_if_init_conf conf;
2379 if (local->open_count && local->open_count == local->monitors &&
2380 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
2381 local->ops->add_interface) {
2382 conf.if_id = -1;
2383 conf.type = IEEE80211_IF_TYPE_MNTR;
2384 conf.mac_addr = NULL;
2385 local->ops->add_interface(local_to_hw(local), &conf);
2389 static int ieee80211_open(struct net_device *dev)
2391 struct ieee80211_sub_if_data *sdata, *nsdata;
2392 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2393 struct ieee80211_if_init_conf conf;
2394 int res;
2396 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2397 read_lock(&local->sub_if_lock);
2398 list_for_each_entry(nsdata, &local->sub_if_list, list) {
2399 struct net_device *ndev = nsdata->dev;
2401 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
2402 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
2403 !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
2404 read_unlock(&local->sub_if_lock);
2405 return -ENOTUNIQ;
2408 read_unlock(&local->sub_if_lock);
2410 if (sdata->type == IEEE80211_IF_TYPE_WDS &&
2411 is_zero_ether_addr(sdata->u.wds.remote_addr))
2412 return -ENOLINK;
2414 if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
2415 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2416 /* run the interface in a "soft monitor" mode */
2417 local->monitors++;
2418 local->open_count++;
2419 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
2420 return 0;
2422 ieee80211_start_soft_monitor(local);
2424 if (local->ops->add_interface) {
2425 conf.if_id = dev->ifindex;
2426 conf.type = sdata->type;
2427 conf.mac_addr = dev->dev_addr;
2428 res = local->ops->add_interface(local_to_hw(local), &conf);
2429 if (res) {
2430 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
2431 ieee80211_start_hard_monitor(local);
2432 return res;
2434 } else {
2435 if (sdata->type != IEEE80211_IF_TYPE_STA)
2436 return -EOPNOTSUPP;
2437 if (local->open_count > 0)
2438 return -ENOBUFS;
2441 if (local->open_count == 0) {
2442 res = 0;
2443 tasklet_enable(&local->tx_pending_tasklet);
2444 tasklet_enable(&local->tasklet);
2445 if (local->ops->open)
2446 res = local->ops->open(local_to_hw(local));
2447 if (res == 0) {
2448 res = dev_open(local->mdev);
2449 if (res) {
2450 if (local->ops->stop)
2451 local->ops->stop(local_to_hw(local));
2452 } else {
2453 res = ieee80211_hw_config(local);
2454 if (res && local->ops->stop)
2455 local->ops->stop(local_to_hw(local));
2456 else if (!res && local->apdev)
2457 dev_open(local->apdev);
2460 if (res) {
2461 if (local->ops->remove_interface)
2462 local->ops->remove_interface(local_to_hw(local),
2463 &conf);
2464 return res;
2467 local->open_count++;
2469 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
2470 local->monitors++;
2471 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
2472 } else
2473 ieee80211_if_config(dev);
2475 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2476 !local->user_space_mlme)
2477 netif_carrier_off(dev);
2478 else
2479 netif_carrier_on(dev);
2481 netif_start_queue(dev);
2482 return 0;
2486 static int ieee80211_stop(struct net_device *dev)
2488 struct ieee80211_sub_if_data *sdata;
2489 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2491 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2493 if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
2494 local->open_count > 1 &&
2495 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
2496 /* remove "soft monitor" interface */
2497 local->open_count--;
2498 local->monitors--;
2499 if (!local->monitors)
2500 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
2501 return 0;
2504 netif_stop_queue(dev);
2505 ieee80211_if_shutdown(dev);
2507 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
2508 local->monitors--;
2509 if (!local->monitors)
2510 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
2513 local->open_count--;
2514 if (local->open_count == 0) {
2515 if (netif_running(local->mdev))
2516 dev_close(local->mdev);
2517 if (local->apdev)
2518 dev_close(local->apdev);
2519 if (local->ops->stop)
2520 local->ops->stop(local_to_hw(local));
2521 tasklet_disable(&local->tx_pending_tasklet);
2522 tasklet_disable(&local->tasklet);
2524 if (local->ops->remove_interface) {
2525 struct ieee80211_if_init_conf conf;
2527 conf.if_id = dev->ifindex;
2528 conf.type = sdata->type;
2529 conf.mac_addr = dev->dev_addr;
2530 local->ops->remove_interface(local_to_hw(local), &conf);
2533 ieee80211_start_hard_monitor(local);
2535 return 0;
2539 static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
2541 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
2542 return ETH_ALEN;
2545 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
2547 return compare_ether_addr(raddr, addr) == 0 ||
2548 is_broadcast_ether_addr(raddr);
2552 static ieee80211_txrx_result
2553 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
2555 struct net_device *dev = rx->dev;
2556 struct ieee80211_local *local = rx->local;
2557 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
2558 u16 fc, hdrlen, ethertype;
2559 u8 *payload;
2560 u8 dst[ETH_ALEN];
2561 u8 src[ETH_ALEN];
2562 struct sk_buff *skb = rx->skb, *skb2;
2563 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2565 fc = rx->fc;
2566 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
2567 return TXRX_CONTINUE;
2569 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
2570 return TXRX_DROP;
2572 hdrlen = ieee80211_get_hdrlen(fc);
2574 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
2575 * header
2576 * IEEE 802.11 address fields:
2577 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
2578 * 0 0 DA SA BSSID n/a
2579 * 0 1 DA BSSID SA n/a
2580 * 1 0 BSSID SA DA n/a
2581 * 1 1 RA TA DA SA
2584 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
2585 case IEEE80211_FCTL_TODS:
2586 /* BSSID SA DA */
2587 memcpy(dst, hdr->addr3, ETH_ALEN);
2588 memcpy(src, hdr->addr2, ETH_ALEN);
2590 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
2591 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
2592 printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
2593 MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
2594 dev->name, MAC_ARG(hdr->addr1),
2595 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
2596 return TXRX_DROP;
2598 break;
2599 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
2600 /* RA TA DA SA */
2601 memcpy(dst, hdr->addr3, ETH_ALEN);
2602 memcpy(src, hdr->addr4, ETH_ALEN);
2604 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
2605 printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
2606 MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
2607 MAC_FMT ")\n",
2608 rx->dev->name, MAC_ARG(hdr->addr1),
2609 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
2610 MAC_ARG(hdr->addr4));
2611 return TXRX_DROP;
2613 break;
2614 case IEEE80211_FCTL_FROMDS:
2615 /* DA BSSID SA */
2616 memcpy(dst, hdr->addr1, ETH_ALEN);
2617 memcpy(src, hdr->addr3, ETH_ALEN);
2619 if (sdata->type != IEEE80211_IF_TYPE_STA ||
2620 (is_multicast_ether_addr(dst) &&
2621 !compare_ether_addr(src, dev->dev_addr)))
2622 return TXRX_DROP;
2623 break;
2624 case 0:
2625 /* DA SA BSSID */
2626 memcpy(dst, hdr->addr1, ETH_ALEN);
2627 memcpy(src, hdr->addr2, ETH_ALEN);
2629 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
2630 if (net_ratelimit()) {
2631 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
2632 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
2633 ")\n",
2634 dev->name, MAC_ARG(hdr->addr1),
2635 MAC_ARG(hdr->addr2),
2636 MAC_ARG(hdr->addr3));
2638 return TXRX_DROP;
2640 break;
2643 payload = skb->data + hdrlen;
2645 if (unlikely(skb->len - hdrlen < 8)) {
2646 if (net_ratelimit()) {
2647 printk(KERN_DEBUG "%s: RX too short data frame "
2648 "payload\n", dev->name);
2650 return TXRX_DROP;
2653 ethertype = (payload[6] << 8) | payload[7];
2655 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
2656 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
2657 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
2658 /* remove RFC1042 or Bridge-Tunnel encapsulation and
2659 * replace EtherType */
2660 skb_pull(skb, hdrlen + 6);
2661 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
2662 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
2663 } else {
2664 struct ethhdr *ehdr;
2665 __be16 len;
2666 skb_pull(skb, hdrlen);
2667 len = htons(skb->len);
2668 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
2669 memcpy(ehdr->h_dest, dst, ETH_ALEN);
2670 memcpy(ehdr->h_source, src, ETH_ALEN);
2671 ehdr->h_proto = len;
2673 skb->dev = dev;
2675 skb2 = NULL;
2677 sdata->stats.rx_packets++;
2678 sdata->stats.rx_bytes += skb->len;
2680 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
2681 || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
2682 if (is_multicast_ether_addr(skb->data)) {
2683 /* send multicast frames both to higher layers in
2684 * local net stack and back to the wireless media */
2685 skb2 = skb_copy(skb, GFP_ATOMIC);
2686 if (!skb2)
2687 printk(KERN_DEBUG "%s: failed to clone "
2688 "multicast frame\n", dev->name);
2689 } else {
2690 struct sta_info *dsta;
2691 dsta = sta_info_get(local, skb->data);
2692 if (dsta && !dsta->dev) {
2693 printk(KERN_DEBUG "Station with null dev "
2694 "structure!\n");
2695 } else if (dsta && dsta->dev == dev) {
2696 /* Destination station is associated to this
2697 * AP, so send the frame directly to it and
2698 * do not pass the frame to local net stack.
2700 skb2 = skb;
2701 skb = NULL;
2703 if (dsta)
2704 sta_info_put(dsta);
2708 if (skb) {
2709 /* deliver to local stack */
2710 skb->protocol = eth_type_trans(skb, dev);
2711 memset(skb->cb, 0, sizeof(skb->cb));
2712 netif_rx(skb);
2715 if (skb2) {
2716 /* send to wireless media */
2717 skb2->protocol = __constant_htons(ETH_P_802_3);
2718 skb_set_network_header(skb2, 0);
2719 skb_set_mac_header(skb2, 0);
2720 dev_queue_xmit(skb2);
2723 return TXRX_QUEUED;
2727 static struct ieee80211_rate *
2728 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
2730 struct ieee80211_hw_mode *mode;
2731 int r;
2733 list_for_each_entry(mode, &local->modes_list, list) {
2734 if (mode->mode != phymode)
2735 continue;
2736 for (r = 0; r < mode->num_rates; r++) {
2737 struct ieee80211_rate *rate = &mode->rates[r];
2738 if (rate->val == hw_rate ||
2739 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
2740 rate->val2 == hw_rate))
2741 return rate;
2745 return NULL;
2748 static void
2749 ieee80211_fill_frame_info(struct ieee80211_local *local,
2750 struct ieee80211_frame_info *fi,
2751 struct ieee80211_rx_status *status)
2753 if (status) {
2754 struct timespec ts;
2755 struct ieee80211_rate *rate;
2757 jiffies_to_timespec(jiffies, &ts);
2758 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
2759 ts.tv_nsec / 1000);
2760 fi->mactime = cpu_to_be64(status->mactime);
2761 switch (status->phymode) {
2762 case MODE_IEEE80211A:
2763 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
2764 break;
2765 case MODE_IEEE80211B:
2766 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
2767 break;
2768 case MODE_IEEE80211G:
2769 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
2770 break;
2771 case MODE_ATHEROS_TURBO:
2772 fi->phytype =
2773 htonl(ieee80211_phytype_dsss_dot11_turbo);
2774 break;
2775 default:
2776 fi->phytype = htonl(0xAAAAAAAA);
2777 break;
2779 fi->channel = htonl(status->channel);
2780 rate = ieee80211_get_rate(local, status->phymode,
2781 status->rate);
2782 if (rate) {
2783 fi->datarate = htonl(rate->rate);
2784 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
2785 if (status->rate == rate->val)
2786 fi->preamble = htonl(2); /* long */
2787 else if (status->rate == rate->val2)
2788 fi->preamble = htonl(1); /* short */
2789 } else
2790 fi->preamble = htonl(0);
2791 } else {
2792 fi->datarate = htonl(0);
2793 fi->preamble = htonl(0);
2796 fi->antenna = htonl(status->antenna);
2797 fi->priority = htonl(0xffffffff); /* no clue */
2798 fi->ssi_type = htonl(ieee80211_ssi_raw);
2799 fi->ssi_signal = htonl(status->ssi);
2800 fi->ssi_noise = 0x00000000;
2801 fi->encoding = 0;
2802 } else {
2803 /* clear everything because we really don't know.
2804 * the msg_type field isn't present on monitor frames
2805 * so we don't know whether it will be present or not,
2806 * but it's ok to not clear it since it'll be assigned
2807 * anyway */
2808 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
2810 fi->ssi_type = htonl(ieee80211_ssi_none);
2812 fi->version = htonl(IEEE80211_FI_VERSION);
2813 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
2816 /* this routine is actually not just for this, but also
2817 * for pushing fake 'management' frames into userspace.
2818 * it shall be replaced by a netlink-based system. */
2819 void
2820 ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
2821 struct ieee80211_rx_status *status, u32 msg_type)
2823 struct ieee80211_frame_info *fi;
2824 const size_t hlen = sizeof(struct ieee80211_frame_info);
2825 struct ieee80211_sub_if_data *sdata;
2827 skb->dev = local->apdev;
2829 sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
2831 if (skb_headroom(skb) < hlen) {
2832 I802_DEBUG_INC(local->rx_expand_skb_head);
2833 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
2834 dev_kfree_skb(skb);
2835 return;
2839 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
2841 ieee80211_fill_frame_info(local, fi, status);
2842 fi->msg_type = htonl(msg_type);
2844 sdata->stats.rx_packets++;
2845 sdata->stats.rx_bytes += skb->len;
2847 skb_set_mac_header(skb, 0);
2848 skb->ip_summed = CHECKSUM_UNNECESSARY;
2849 skb->pkt_type = PACKET_OTHERHOST;
2850 skb->protocol = htons(ETH_P_802_2);
2851 memset(skb->cb, 0, sizeof(skb->cb));
2852 netif_rx(skb);
2855 static void
2856 ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
2857 struct ieee80211_rx_status *status)
2859 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2860 struct ieee80211_sub_if_data *sdata;
2861 struct ieee80211_rate *rate;
2862 struct ieee80211_rtap_hdr {
2863 struct ieee80211_radiotap_header hdr;
2864 u8 flags;
2865 u8 rate;
2866 __le16 chan_freq;
2867 __le16 chan_flags;
2868 u8 antsignal;
2869 } __attribute__ ((packed)) *rthdr;
2871 skb->dev = dev;
2873 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2875 if (status->flag & RX_FLAG_RADIOTAP)
2876 goto out;
2878 if (skb_headroom(skb) < sizeof(*rthdr)) {
2879 I802_DEBUG_INC(local->rx_expand_skb_head);
2880 if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
2881 dev_kfree_skb(skb);
2882 return;
2886 rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
2887 memset(rthdr, 0, sizeof(*rthdr));
2888 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2889 rthdr->hdr.it_present =
2890 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
2891 (1 << IEEE80211_RADIOTAP_RATE) |
2892 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2893 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
2894 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
2895 IEEE80211_RADIOTAP_F_FCS : 0;
2896 rate = ieee80211_get_rate(local, status->phymode, status->rate);
2897 if (rate)
2898 rthdr->rate = rate->rate / 5;
2899 rthdr->chan_freq = cpu_to_le16(status->freq);
2900 rthdr->chan_flags =
2901 status->phymode == MODE_IEEE80211A ?
2902 cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
2903 cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
2904 rthdr->antsignal = status->ssi;
2906 out:
2907 sdata->stats.rx_packets++;
2908 sdata->stats.rx_bytes += skb->len;
2910 skb_set_mac_header(skb, 0);
2911 skb->ip_summed = CHECKSUM_UNNECESSARY;
2912 skb->pkt_type = PACKET_OTHERHOST;
2913 skb->protocol = htons(ETH_P_802_2);
2914 memset(skb->cb, 0, sizeof(skb->cb));
2915 netif_rx(skb);
2918 int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
2919 int radar, int radar_type)
2921 struct sk_buff *skb;
2922 struct ieee80211_radar_info *msg;
2923 struct ieee80211_local *local = hw_to_local(hw);
2925 if (!local->apdev)
2926 return 0;
2928 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
2929 sizeof(struct ieee80211_radar_info));
2931 if (!skb)
2932 return -ENOMEM;
2933 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
2935 msg = (struct ieee80211_radar_info *)
2936 skb_put(skb, sizeof(struct ieee80211_radar_info));
2937 msg->channel = channel;
2938 msg->radar = radar;
2939 msg->radar_type = radar_type;
2941 ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
2942 return 0;
2944 EXPORT_SYMBOL(ieee80211_radar_status);
2946 int ieee80211_set_aid_for_sta(struct ieee80211_hw *hw, u8 *peer_address,
2947 u16 aid)
2949 struct sk_buff *skb;
2950 struct ieee80211_msg_set_aid_for_sta *msg;
2951 struct ieee80211_local *local = hw_to_local(hw);
2953 /* unlikely because if this event only happens for APs,
2954 * which require an open ap device. */
2955 if (unlikely(!local->apdev))
2956 return 0;
2958 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
2959 sizeof(struct ieee80211_msg_set_aid_for_sta));
2961 if (!skb)
2962 return -ENOMEM;
2963 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
2965 msg = (struct ieee80211_msg_set_aid_for_sta *)
2966 skb_put(skb, sizeof(struct ieee80211_msg_set_aid_for_sta));
2967 memcpy(msg->sta_address, peer_address, ETH_ALEN);
2968 msg->aid = aid;
2970 ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_set_aid_for_sta);
2971 return 0;
2973 EXPORT_SYMBOL(ieee80211_set_aid_for_sta);
2975 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
2977 struct ieee80211_sub_if_data *sdata;
2978 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
2980 if (sdata->bss)
2981 atomic_inc(&sdata->bss->num_sta_ps);
2982 sta->flags |= WLAN_STA_PS;
2983 sta->pspoll = 0;
2984 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
2985 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
2986 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
2987 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
2991 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
2993 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2994 struct sk_buff *skb;
2995 int sent = 0;
2996 struct ieee80211_sub_if_data *sdata;
2997 struct ieee80211_tx_packet_data *pkt_data;
2999 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
3000 if (sdata->bss)
3001 atomic_dec(&sdata->bss->num_sta_ps);
3002 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
3003 sta->pspoll = 0;
3004 if (!skb_queue_empty(&sta->ps_tx_buf)) {
3005 if (local->ops->set_tim)
3006 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
3007 if (sdata->bss)
3008 bss_tim_clear(local, sdata->bss, sta->aid);
3010 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3011 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
3012 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
3013 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3014 /* Send all buffered frames to the station */
3015 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
3016 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
3017 sent++;
3018 pkt_data->requeue = 1;
3019 dev_queue_xmit(skb);
3021 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
3022 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
3023 local->total_ps_buffered--;
3024 sent++;
3025 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3026 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
3027 "since STA not sleeping anymore\n", dev->name,
3028 MAC_ARG(sta->addr), sta->aid);
3029 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3030 pkt_data->requeue = 1;
3031 dev_queue_xmit(skb);
3034 return sent;
3038 static ieee80211_txrx_result
3039 ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
3041 struct sk_buff *skb;
3042 int no_pending_pkts;
3044 if (likely(!rx->sta ||
3045 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
3046 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
3047 !rx->u.rx.ra_match))
3048 return TXRX_CONTINUE;
3050 skb = skb_dequeue(&rx->sta->tx_filtered);
3051 if (!skb) {
3052 skb = skb_dequeue(&rx->sta->ps_tx_buf);
3053 if (skb)
3054 rx->local->total_ps_buffered--;
3056 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
3057 skb_queue_empty(&rx->sta->ps_tx_buf);
3059 if (skb) {
3060 struct ieee80211_hdr *hdr =
3061 (struct ieee80211_hdr *) skb->data;
3063 /* tell TX path to send one frame even though the STA may
3064 * still remain is PS mode after this frame exchange */
3065 rx->sta->pspoll = 1;
3067 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3068 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
3069 "after %d)\n",
3070 MAC_ARG(rx->sta->addr), rx->sta->aid,
3071 skb_queue_len(&rx->sta->ps_tx_buf));
3072 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3074 /* Use MoreData flag to indicate whether there are more
3075 * buffered frames for this STA */
3076 if (no_pending_pkts) {
3077 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
3078 rx->sta->flags &= ~WLAN_STA_TIM;
3079 } else
3080 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
3082 dev_queue_xmit(skb);
3084 if (no_pending_pkts) {
3085 if (rx->local->ops->set_tim)
3086 rx->local->ops->set_tim(local_to_hw(rx->local),
3087 rx->sta->aid, 0);
3088 if (rx->sdata->bss)
3089 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
3091 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
3092 } else if (!rx->u.rx.sent_ps_buffered) {
3093 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
3094 "though there is no buffered frames for it\n",
3095 rx->dev->name, MAC_ARG(rx->sta->addr));
3096 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
3100 /* Free PS Poll skb here instead of returning TXRX_DROP that would
3101 * count as an dropped frame. */
3102 dev_kfree_skb(rx->skb);
3104 return TXRX_QUEUED;
3108 static inline struct ieee80211_fragment_entry *
3109 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
3110 unsigned int frag, unsigned int seq, int rx_queue,
3111 struct sk_buff **skb)
3113 struct ieee80211_fragment_entry *entry;
3114 int idx;
3116 idx = sdata->fragment_next;
3117 entry = &sdata->fragments[sdata->fragment_next++];
3118 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
3119 sdata->fragment_next = 0;
3121 if (!skb_queue_empty(&entry->skb_list)) {
3122 #ifdef CONFIG_MAC80211_DEBUG
3123 struct ieee80211_hdr *hdr =
3124 (struct ieee80211_hdr *) entry->skb_list.next->data;
3125 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
3126 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
3127 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
3128 sdata->dev->name, idx,
3129 jiffies - entry->first_frag_time, entry->seq,
3130 entry->last_frag, MAC_ARG(hdr->addr1),
3131 MAC_ARG(hdr->addr2));
3132 #endif /* CONFIG_MAC80211_DEBUG */
3133 __skb_queue_purge(&entry->skb_list);
3136 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
3137 *skb = NULL;
3138 entry->first_frag_time = jiffies;
3139 entry->seq = seq;
3140 entry->rx_queue = rx_queue;
3141 entry->last_frag = frag;
3142 entry->ccmp = 0;
3143 entry->extra_len = 0;
3145 return entry;
3149 static inline struct ieee80211_fragment_entry *
3150 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
3151 u16 fc, unsigned int frag, unsigned int seq,
3152 int rx_queue, struct ieee80211_hdr *hdr)
3154 struct ieee80211_fragment_entry *entry;
3155 int i, idx;
3157 idx = sdata->fragment_next;
3158 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
3159 struct ieee80211_hdr *f_hdr;
3160 u16 f_fc;
3162 idx--;
3163 if (idx < 0)
3164 idx = IEEE80211_FRAGMENT_MAX - 1;
3166 entry = &sdata->fragments[idx];
3167 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
3168 entry->rx_queue != rx_queue ||
3169 entry->last_frag + 1 != frag)
3170 continue;
3172 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
3173 f_fc = le16_to_cpu(f_hdr->frame_control);
3175 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
3176 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
3177 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
3178 continue;
3180 if (entry->first_frag_time + 2 * HZ < jiffies) {
3181 __skb_queue_purge(&entry->skb_list);
3182 continue;
3184 return entry;
3187 return NULL;
3191 static ieee80211_txrx_result
3192 ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
3194 struct ieee80211_hdr *hdr;
3195 u16 sc;
3196 unsigned int frag, seq;
3197 struct ieee80211_fragment_entry *entry;
3198 struct sk_buff *skb;
3200 hdr = (struct ieee80211_hdr *) rx->skb->data;
3201 sc = le16_to_cpu(hdr->seq_ctrl);
3202 frag = sc & IEEE80211_SCTL_FRAG;
3204 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
3205 (rx->skb)->len < 24 ||
3206 is_multicast_ether_addr(hdr->addr1))) {
3207 /* not fragmented */
3208 goto out;
3210 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
3212 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3214 if (frag == 0) {
3215 /* This is the first fragment of a new frame. */
3216 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
3217 rx->u.rx.queue, &(rx->skb));
3218 if (rx->key && rx->key->alg == ALG_CCMP &&
3219 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
3220 /* Store CCMP PN so that we can verify that the next
3221 * fragment has a sequential PN value. */
3222 entry->ccmp = 1;
3223 memcpy(entry->last_pn,
3224 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
3225 CCMP_PN_LEN);
3227 return TXRX_QUEUED;
3230 /* This is a fragment for a frame that should already be pending in
3231 * fragment cache. Add this fragment to the end of the pending entry.
3233 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
3234 rx->u.rx.queue, hdr);
3235 if (!entry) {
3236 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
3237 return TXRX_DROP;
3240 /* Verify that MPDUs within one MSDU have sequential PN values.
3241 * (IEEE 802.11i, 8.3.3.4.5) */
3242 if (entry->ccmp) {
3243 int i;
3244 u8 pn[CCMP_PN_LEN], *rpn;
3245 if (!rx->key || rx->key->alg != ALG_CCMP)
3246 return TXRX_DROP;
3247 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
3248 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
3249 pn[i]++;
3250 if (pn[i])
3251 break;
3253 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
3254 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
3255 printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
3256 " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
3257 "(expected %02x%02x%02x%02x%02x%02x)\n",
3258 rx->dev->name, MAC_ARG(hdr->addr2),
3259 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
3260 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
3261 return TXRX_DROP;
3263 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
3266 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
3267 __skb_queue_tail(&entry->skb_list, rx->skb);
3268 entry->last_frag = frag;
3269 entry->extra_len += rx->skb->len;
3270 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
3271 rx->skb = NULL;
3272 return TXRX_QUEUED;
3275 rx->skb = __skb_dequeue(&entry->skb_list);
3276 if (skb_tailroom(rx->skb) < entry->extra_len) {
3277 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
3278 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
3279 GFP_ATOMIC))) {
3280 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
3281 __skb_queue_purge(&entry->skb_list);
3282 return TXRX_DROP;
3285 while ((skb = __skb_dequeue(&entry->skb_list))) {
3286 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
3287 dev_kfree_skb(skb);
3290 /* Complete frame has been reassembled - process it now */
3291 rx->fragmented = 1;
3293 out:
3294 if (rx->sta)
3295 rx->sta->rx_packets++;
3296 if (is_multicast_ether_addr(hdr->addr1))
3297 rx->local->dot11MulticastReceivedFrameCount++;
3298 else
3299 ieee80211_led_rx(rx->local);
3300 return TXRX_CONTINUE;
3304 static ieee80211_txrx_result
3305 ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
3307 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
3308 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
3309 return TXRX_QUEUED;
3312 if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
3313 skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb));
3315 return TXRX_CONTINUE;
3319 static ieee80211_txrx_result
3320 ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
3322 struct ieee80211_hdr *hdr;
3323 int always_sta_key;
3324 hdr = (struct ieee80211_hdr *) rx->skb->data;
3326 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
3327 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
3328 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
3329 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
3330 hdr->seq_ctrl)) {
3331 if (rx->u.rx.ra_match) {
3332 rx->local->dot11FrameDuplicateCount++;
3333 rx->sta->num_duplicates++;
3335 return TXRX_DROP;
3336 } else
3337 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
3340 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
3341 rx->skb->len > FCS_LEN)
3342 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
3344 if (unlikely(rx->skb->len < 16)) {
3345 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
3346 return TXRX_DROP;
3349 if (!rx->u.rx.ra_match)
3350 rx->skb->pkt_type = PACKET_OTHERHOST;
3351 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
3352 rx->skb->pkt_type = PACKET_HOST;
3353 else if (is_multicast_ether_addr(hdr->addr1)) {
3354 if (is_broadcast_ether_addr(hdr->addr1))
3355 rx->skb->pkt_type = PACKET_BROADCAST;
3356 else
3357 rx->skb->pkt_type = PACKET_MULTICAST;
3358 } else
3359 rx->skb->pkt_type = PACKET_OTHERHOST;
3361 /* Drop disallowed frame classes based on STA auth/assoc state;
3362 * IEEE 802.11, Chap 5.5.
3364 * 80211.o does filtering only based on association state, i.e., it
3365 * drops Class 3 frames from not associated stations. hostapd sends
3366 * deauth/disassoc frames when needed. In addition, hostapd is
3367 * responsible for filtering on both auth and assoc states.
3369 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
3370 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
3371 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
3372 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
3373 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
3374 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
3375 !(rx->fc & IEEE80211_FCTL_TODS) &&
3376 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
3377 || !rx->u.rx.ra_match) {
3378 /* Drop IBSS frames and frames for other hosts
3379 * silently. */
3380 return TXRX_DROP;
3383 if (!rx->local->apdev)
3384 return TXRX_DROP;
3386 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3387 ieee80211_msg_sta_not_assoc);
3388 return TXRX_QUEUED;
3391 if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
3392 always_sta_key = 0;
3393 else
3394 always_sta_key = 1;
3396 if (rx->sta && rx->sta->key && always_sta_key) {
3397 rx->key = rx->sta->key;
3398 } else {
3399 if (rx->sta && rx->sta->key)
3400 rx->key = rx->sta->key;
3401 else
3402 rx->key = rx->sdata->default_key;
3404 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3405 rx->fc & IEEE80211_FCTL_PROTECTED) {
3406 int keyidx = ieee80211_wep_get_keyidx(rx->skb);
3408 if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
3409 (!rx->sta || !rx->sta->key || keyidx > 0))
3410 rx->key = rx->sdata->keys[keyidx];
3412 if (!rx->key) {
3413 if (!rx->u.rx.ra_match)
3414 return TXRX_DROP;
3415 printk(KERN_DEBUG "%s: RX WEP frame with "
3416 "unknown keyidx %d (A1=" MAC_FMT " A2="
3417 MAC_FMT " A3=" MAC_FMT ")\n",
3418 rx->dev->name, keyidx,
3419 MAC_ARG(hdr->addr1),
3420 MAC_ARG(hdr->addr2),
3421 MAC_ARG(hdr->addr3));
3422 if (!rx->local->apdev)
3423 return TXRX_DROP;
3424 ieee80211_rx_mgmt(
3425 rx->local, rx->skb, rx->u.rx.status,
3426 ieee80211_msg_wep_frame_unknown_key);
3427 return TXRX_QUEUED;
3432 if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
3433 rx->key->tx_rx_count++;
3434 if (unlikely(rx->local->key_tx_rx_threshold &&
3435 rx->key->tx_rx_count >
3436 rx->local->key_tx_rx_threshold)) {
3437 ieee80211_key_threshold_notify(rx->dev, rx->key,
3438 rx->sta);
3442 return TXRX_CONTINUE;
3446 static ieee80211_txrx_result
3447 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
3449 struct sta_info *sta = rx->sta;
3450 struct net_device *dev = rx->dev;
3451 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3453 if (!sta)
3454 return TXRX_CONTINUE;
3456 /* Update last_rx only for IBSS packets which are for the current
3457 * BSSID to avoid keeping the current IBSS network alive in cases where
3458 * other STAs are using different BSSID. */
3459 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
3460 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
3461 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
3462 sta->last_rx = jiffies;
3463 } else
3464 if (!is_multicast_ether_addr(hdr->addr1) ||
3465 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
3466 /* Update last_rx only for unicast frames in order to prevent
3467 * the Probe Request frames (the only broadcast frames from a
3468 * STA in infrastructure mode) from keeping a connection alive.
3470 sta->last_rx = jiffies;
3473 if (!rx->u.rx.ra_match)
3474 return TXRX_CONTINUE;
3476 sta->rx_fragments++;
3477 sta->rx_bytes += rx->skb->len;
3478 sta->last_rssi = (sta->last_rssi * 15 +
3479 rx->u.rx.status->ssi) / 16;
3480 sta->last_signal = (sta->last_signal * 15 +
3481 rx->u.rx.status->signal) / 16;
3482 sta->last_noise = (sta->last_noise * 15 +
3483 rx->u.rx.status->noise) / 16;
3485 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
3486 /* Change STA power saving mode only in the end of a frame
3487 * exchange sequence */
3488 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
3489 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
3490 else if (!(sta->flags & WLAN_STA_PS) &&
3491 (rx->fc & IEEE80211_FCTL_PM))
3492 ap_sta_ps_start(dev, sta);
3495 /* Drop data::nullfunc frames silently, since they are used only to
3496 * control station power saving mode. */
3497 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3498 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
3499 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
3500 /* Update counter and free packet here to avoid counting this
3501 * as a dropped packed. */
3502 sta->rx_packets++;
3503 dev_kfree_skb(rx->skb);
3504 return TXRX_QUEUED;
3507 return TXRX_CONTINUE;
3508 } /* ieee80211_rx_h_sta_process */
3511 static ieee80211_txrx_result
3512 ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
3514 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3515 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
3516 !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
3517 return TXRX_CONTINUE;
3519 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
3520 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
3521 rx->key->force_sw_encrypt) {
3522 u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
3523 if (iv) {
3524 rx->sta->wep_weak_iv_count++;
3528 return TXRX_CONTINUE;
3532 static ieee80211_txrx_result
3533 ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
3535 /* If the device handles decryption totally, skip this test */
3536 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3537 return TXRX_CONTINUE;
3539 if ((rx->key && rx->key->alg != ALG_WEP) ||
3540 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
3541 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3542 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3543 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
3544 return TXRX_CONTINUE;
3546 if (!rx->key) {
3547 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
3548 rx->dev->name);
3549 return TXRX_DROP;
3552 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
3553 rx->key->force_sw_encrypt) {
3554 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
3555 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
3556 "failed\n", rx->dev->name);
3557 return TXRX_DROP;
3559 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
3560 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
3561 /* remove ICV */
3562 skb_trim(rx->skb, rx->skb->len - 4);
3565 return TXRX_CONTINUE;
3569 static ieee80211_txrx_result
3570 ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
3572 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
3573 rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
3574 /* Pass both encrypted and unencrypted EAPOL frames to user
3575 * space for processing. */
3576 if (!rx->local->apdev)
3577 return TXRX_DROP;
3578 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3579 ieee80211_msg_normal);
3580 return TXRX_QUEUED;
3583 if (unlikely(rx->sdata->ieee802_1x &&
3584 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3585 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3586 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
3587 !ieee80211_is_eapol(rx->skb))) {
3588 #ifdef CONFIG_MAC80211_DEBUG
3589 struct ieee80211_hdr *hdr =
3590 (struct ieee80211_hdr *) rx->skb->data;
3591 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
3592 " (unauthorized port)\n", rx->dev->name,
3593 MAC_ARG(hdr->addr2));
3594 #endif /* CONFIG_MAC80211_DEBUG */
3595 return TXRX_DROP;
3598 return TXRX_CONTINUE;
3602 static ieee80211_txrx_result
3603 ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
3605 /* If the device handles decryption totally, skip this test */
3606 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
3607 return TXRX_CONTINUE;
3609 /* Drop unencrypted frames if key is set. */
3610 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
3611 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
3612 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
3613 (rx->key || rx->sdata->drop_unencrypted) &&
3614 (rx->sdata->eapol == 0 ||
3615 !ieee80211_is_eapol(rx->skb)))) {
3616 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
3617 "encryption\n", rx->dev->name);
3618 return TXRX_DROP;
3620 return TXRX_CONTINUE;
3624 static ieee80211_txrx_result
3625 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
3627 struct ieee80211_sub_if_data *sdata;
3629 if (!rx->u.rx.ra_match)
3630 return TXRX_DROP;
3632 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
3633 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
3634 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
3635 !rx->local->user_space_mlme) {
3636 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
3637 } else {
3638 /* Management frames are sent to hostapd for processing */
3639 if (!rx->local->apdev)
3640 return TXRX_DROP;
3641 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3642 ieee80211_msg_normal);
3644 return TXRX_QUEUED;
3648 static ieee80211_txrx_result
3649 ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
3651 struct ieee80211_local *local = rx->local;
3652 struct sk_buff *skb = rx->skb;
3654 if (unlikely(local->sta_scanning != 0)) {
3655 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
3656 return TXRX_QUEUED;
3659 if (unlikely(rx->u.rx.in_scan)) {
3660 /* scanning finished during invoking of handlers */
3661 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
3662 return TXRX_DROP;
3665 return TXRX_CONTINUE;
3669 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
3670 struct ieee80211_hdr *hdr,
3671 struct sta_info *sta,
3672 struct ieee80211_txrx_data *rx)
3674 int keyidx, hdrlen;
3676 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
3677 if (rx->skb->len >= hdrlen + 4)
3678 keyidx = rx->skb->data[hdrlen + 3] >> 6;
3679 else
3680 keyidx = -1;
3682 /* TODO: verify that this is not triggered by fragmented
3683 * frames (hw does not verify MIC for them). */
3684 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
3685 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
3686 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
3688 if (!sta) {
3689 /* Some hardware versions seem to generate incorrect
3690 * Michael MIC reports; ignore them to avoid triggering
3691 * countermeasures. */
3692 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3693 "error for unknown address " MAC_FMT "\n",
3694 dev->name, MAC_ARG(hdr->addr2));
3695 goto ignore;
3698 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
3699 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3700 "error for a frame with no ISWEP flag (src "
3701 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
3702 goto ignore;
3705 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
3706 rx->sdata->type == IEEE80211_IF_TYPE_AP) {
3707 keyidx = ieee80211_wep_get_keyidx(rx->skb);
3708 /* AP with Pairwise keys support should never receive Michael
3709 * MIC errors for non-zero keyidx because these are reserved
3710 * for group keys and only the AP is sending real multicast
3711 * frames in BSS. */
3712 if (keyidx) {
3713 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
3714 "a frame with non-zero keyidx (%d) (src " MAC_FMT
3715 ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
3716 goto ignore;
3720 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
3721 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
3722 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
3723 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
3724 "error for a frame that cannot be encrypted "
3725 "(fc=0x%04x) (src " MAC_FMT ")\n",
3726 dev->name, rx->fc, MAC_ARG(hdr->addr2));
3727 goto ignore;
3730 do {
3731 union iwreq_data wrqu;
3732 char *buf = kmalloc(128, GFP_ATOMIC);
3733 if (!buf)
3734 break;
3736 /* TODO: needed parameters: count, key type, TSC */
3737 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
3738 "keyid=%d %scast addr=" MAC_FMT ")",
3739 keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
3740 MAC_ARG(hdr->addr2));
3741 memset(&wrqu, 0, sizeof(wrqu));
3742 wrqu.data.length = strlen(buf);
3743 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
3744 kfree(buf);
3745 } while (0);
3747 /* TODO: consider verifying the MIC error report with software
3748 * implementation if we get too many spurious reports from the
3749 * hardware. */
3750 if (!rx->local->apdev)
3751 goto ignore;
3752 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
3753 ieee80211_msg_michael_mic_failure);
3754 return;
3756 ignore:
3757 dev_kfree_skb(rx->skb);
3758 rx->skb = NULL;
3761 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
3762 struct ieee80211_local *local,
3763 ieee80211_rx_handler *handlers,
3764 struct ieee80211_txrx_data *rx,
3765 struct sta_info *sta)
3767 ieee80211_rx_handler *handler;
3768 ieee80211_txrx_result res = TXRX_DROP;
3770 for (handler = handlers; *handler != NULL; handler++) {
3771 res = (*handler)(rx);
3772 if (res != TXRX_CONTINUE) {
3773 if (res == TXRX_DROP) {
3774 I802_DEBUG_INC(local->rx_handlers_drop);
3775 if (sta)
3776 sta->rx_dropped++;
3778 if (res == TXRX_QUEUED)
3779 I802_DEBUG_INC(local->rx_handlers_queued);
3780 break;
3784 if (res == TXRX_DROP) {
3785 dev_kfree_skb(rx->skb);
3787 return res;
3790 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
3791 ieee80211_rx_handler *handlers,
3792 struct ieee80211_txrx_data *rx,
3793 struct sta_info *sta)
3795 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
3796 TXRX_CONTINUE)
3797 dev_kfree_skb(rx->skb);
3801 * This is the receive path handler. It is called by a low level driver when an
3802 * 802.11 MPDU is received from the hardware.
3804 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3805 struct ieee80211_rx_status *status)
3807 struct ieee80211_local *local = hw_to_local(hw);
3808 struct ieee80211_sub_if_data *sdata;
3809 struct sta_info *sta;
3810 struct ieee80211_hdr *hdr;
3811 struct ieee80211_txrx_data rx;
3812 u16 type;
3813 int multicast;
3814 int radiotap_len = 0;
3816 if (status->flag & RX_FLAG_RADIOTAP) {
3817 radiotap_len = ieee80211_get_radiotap_len(skb);
3818 skb_pull(skb, radiotap_len);
3821 hdr = (struct ieee80211_hdr *) skb->data;
3822 memset(&rx, 0, sizeof(rx));
3823 rx.skb = skb;
3824 rx.local = local;
3826 rx.u.rx.status = status;
3827 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
3828 type = rx.fc & IEEE80211_FCTL_FTYPE;
3829 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
3830 local->dot11ReceivedFragmentCount++;
3831 multicast = is_multicast_ether_addr(hdr->addr1);
3833 if (skb->len >= 16)
3834 sta = rx.sta = sta_info_get(local, hdr->addr2);
3835 else
3836 sta = rx.sta = NULL;
3838 if (sta) {
3839 rx.dev = sta->dev;
3840 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
3843 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
3844 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
3845 goto end;
3848 if (unlikely(local->sta_scanning))
3849 rx.u.rx.in_scan = 1;
3851 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
3852 sta) != TXRX_CONTINUE)
3853 goto end;
3854 skb = rx.skb;
3856 skb_push(skb, radiotap_len);
3857 if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
3858 !local->iff_promiscs && !multicast) {
3859 rx.u.rx.ra_match = 1;
3860 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
3861 sta);
3862 } else {
3863 struct ieee80211_sub_if_data *prev = NULL;
3864 struct sk_buff *skb_new;
3865 u8 *bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
3867 read_lock(&local->sub_if_lock);
3868 list_for_each_entry(sdata, &local->sub_if_list, list) {
3869 rx.u.rx.ra_match = 1;
3870 switch (sdata->type) {
3871 case IEEE80211_IF_TYPE_STA:
3872 if (!bssid)
3873 continue;
3874 if (!ieee80211_bssid_match(bssid,
3875 sdata->u.sta.bssid)) {
3876 if (!rx.u.rx.in_scan)
3877 continue;
3878 rx.u.rx.ra_match = 0;
3879 } else if (!multicast &&
3880 compare_ether_addr(sdata->dev->dev_addr,
3881 hdr->addr1) != 0) {
3882 if (!sdata->promisc)
3883 continue;
3884 rx.u.rx.ra_match = 0;
3886 break;
3887 case IEEE80211_IF_TYPE_IBSS:
3888 if (!bssid)
3889 continue;
3890 if (!ieee80211_bssid_match(bssid,
3891 sdata->u.sta.bssid)) {
3892 if (!rx.u.rx.in_scan)
3893 continue;
3894 rx.u.rx.ra_match = 0;
3895 } else if (!multicast &&
3896 compare_ether_addr(sdata->dev->dev_addr,
3897 hdr->addr1) != 0) {
3898 if (!sdata->promisc)
3899 continue;
3900 rx.u.rx.ra_match = 0;
3901 } else if (!sta)
3902 sta = rx.sta =
3903 ieee80211_ibss_add_sta(sdata->dev,
3904 skb, bssid,
3905 hdr->addr2);
3906 break;
3907 case IEEE80211_IF_TYPE_AP:
3908 if (!bssid) {
3909 if (compare_ether_addr(sdata->dev->dev_addr,
3910 hdr->addr1) != 0)
3911 continue;
3912 } else if (!ieee80211_bssid_match(bssid,
3913 sdata->dev->dev_addr)) {
3914 if (!rx.u.rx.in_scan)
3915 continue;
3916 rx.u.rx.ra_match = 0;
3918 if (sdata->dev == local->mdev &&
3919 !rx.u.rx.in_scan)
3920 /* do not receive anything via
3921 * master device when not scanning */
3922 continue;
3923 break;
3924 case IEEE80211_IF_TYPE_WDS:
3925 if (bssid ||
3926 (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
3927 continue;
3928 if (compare_ether_addr(sdata->u.wds.remote_addr,
3929 hdr->addr2) != 0)
3930 continue;
3931 break;
3934 if (prev) {
3935 skb_new = skb_copy(skb, GFP_ATOMIC);
3936 if (!skb_new) {
3937 if (net_ratelimit())
3938 printk(KERN_DEBUG "%s: failed to copy "
3939 "multicast frame for %s",
3940 local->mdev->name, prev->dev->name);
3941 continue;
3943 rx.skb = skb_new;
3944 rx.dev = prev->dev;
3945 rx.sdata = prev;
3946 ieee80211_invoke_rx_handlers(local,
3947 local->rx_handlers,
3948 &rx, sta);
3950 prev = sdata;
3952 if (prev) {
3953 rx.skb = skb;
3954 rx.dev = prev->dev;
3955 rx.sdata = prev;
3956 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
3957 &rx, sta);
3958 } else
3959 dev_kfree_skb(skb);
3960 read_unlock(&local->sub_if_lock);
3963 end:
3964 if (sta)
3965 sta_info_put(sta);
3967 EXPORT_SYMBOL(__ieee80211_rx);
3969 static ieee80211_txrx_result
3970 ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
3972 struct ieee80211_local *local = tx->local;
3973 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
3974 struct sk_buff *skb = tx->skb;
3975 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
3976 u32 load = 0, hdrtime;
3978 /* TODO: this could be part of tx_status handling, so that the number
3979 * of retries would be known; TX rate should in that case be stored
3980 * somewhere with the packet */
3982 /* Estimate total channel use caused by this frame */
3984 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
3985 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
3987 if (mode->mode == MODE_IEEE80211A ||
3988 mode->mode == MODE_ATHEROS_TURBO ||
3989 mode->mode == MODE_ATHEROS_TURBOG ||
3990 (mode->mode == MODE_IEEE80211G &&
3991 tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
3992 hdrtime = CHAN_UTIL_HDR_SHORT;
3993 else
3994 hdrtime = CHAN_UTIL_HDR_LONG;
3996 load = hdrtime;
3997 if (!is_multicast_ether_addr(hdr->addr1))
3998 load += hdrtime;
4000 if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
4001 load += 2 * hdrtime;
4002 else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
4003 load += hdrtime;
4005 load += skb->len * tx->u.tx.rate->rate_inv;
4007 if (tx->u.tx.extra_frag) {
4008 int i;
4009 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
4010 load += 2 * hdrtime;
4011 load += tx->u.tx.extra_frag[i]->len *
4012 tx->u.tx.rate->rate;
4016 /* Divide channel_use by 8 to avoid wrapping around the counter */
4017 load >>= CHAN_UTIL_SHIFT;
4018 local->channel_use_raw += load;
4019 if (tx->sta)
4020 tx->sta->channel_use_raw += load;
4021 tx->sdata->channel_use_raw += load;
4023 return TXRX_CONTINUE;
4027 static ieee80211_txrx_result
4028 ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
4030 struct ieee80211_local *local = rx->local;
4031 struct sk_buff *skb = rx->skb;
4032 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4033 u32 load = 0, hdrtime;
4034 struct ieee80211_rate *rate;
4035 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
4036 int i;
4038 /* Estimate total channel use caused by this frame */
4040 if (unlikely(mode->num_rates < 0))
4041 return TXRX_CONTINUE;
4043 rate = &mode->rates[0];
4044 for (i = 0; i < mode->num_rates; i++) {
4045 if (mode->rates[i].val == rx->u.rx.status->rate) {
4046 rate = &mode->rates[i];
4047 break;
4051 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
4052 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
4054 if (mode->mode == MODE_IEEE80211A ||
4055 mode->mode == MODE_ATHEROS_TURBO ||
4056 mode->mode == MODE_ATHEROS_TURBOG ||
4057 (mode->mode == MODE_IEEE80211G &&
4058 rate->flags & IEEE80211_RATE_ERP))
4059 hdrtime = CHAN_UTIL_HDR_SHORT;
4060 else
4061 hdrtime = CHAN_UTIL_HDR_LONG;
4063 load = hdrtime;
4064 if (!is_multicast_ether_addr(hdr->addr1))
4065 load += hdrtime;
4067 load += skb->len * rate->rate_inv;
4069 /* Divide channel_use by 8 to avoid wrapping around the counter */
4070 load >>= CHAN_UTIL_SHIFT;
4071 local->channel_use_raw += load;
4072 if (rx->sta)
4073 rx->sta->channel_use_raw += load;
4074 rx->u.rx.load = load;
4076 return TXRX_CONTINUE;
4079 static ieee80211_txrx_result
4080 ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
4082 rx->sdata->channel_use_raw += rx->u.rx.load;
4083 return TXRX_CONTINUE;
4086 static void ieee80211_stat_refresh(unsigned long data)
4088 struct ieee80211_local *local = (struct ieee80211_local *) data;
4089 struct sta_info *sta;
4090 struct ieee80211_sub_if_data *sdata;
4092 if (!local->stat_time)
4093 return;
4095 /* go through all stations */
4096 spin_lock_bh(&local->sta_lock);
4097 list_for_each_entry(sta, &local->sta_list, list) {
4098 sta->channel_use = (sta->channel_use_raw / local->stat_time) /
4099 CHAN_UTIL_PER_10MS;
4100 sta->channel_use_raw = 0;
4102 spin_unlock_bh(&local->sta_lock);
4104 /* go through all subinterfaces */
4105 read_lock(&local->sub_if_lock);
4106 list_for_each_entry(sdata, &local->sub_if_list, list) {
4107 sdata->channel_use = (sdata->channel_use_raw /
4108 local->stat_time) / CHAN_UTIL_PER_10MS;
4109 sdata->channel_use_raw = 0;
4111 read_unlock(&local->sub_if_lock);
4113 /* hardware interface */
4114 local->channel_use = (local->channel_use_raw /
4115 local->stat_time) / CHAN_UTIL_PER_10MS;
4116 local->channel_use_raw = 0;
4118 local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
4119 add_timer(&local->stat_timer);
4123 /* This is a version of the rx handler that can be called from hard irq
4124 * context. Post the skb on the queue and schedule the tasklet */
4125 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
4126 struct ieee80211_rx_status *status)
4128 struct ieee80211_local *local = hw_to_local(hw);
4130 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4132 skb->dev = local->mdev;
4133 /* copy status into skb->cb for use by tasklet */
4134 memcpy(skb->cb, status, sizeof(*status));
4135 skb->pkt_type = IEEE80211_RX_MSG;
4136 skb_queue_tail(&local->skb_queue, skb);
4137 tasklet_schedule(&local->tasklet);
4139 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
4141 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
4142 struct sk_buff *skb,
4143 struct ieee80211_tx_status *status)
4145 struct ieee80211_local *local = hw_to_local(hw);
4146 struct ieee80211_tx_status *saved;
4147 int tmp;
4149 skb->dev = local->mdev;
4150 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
4151 if (unlikely(!saved)) {
4152 if (net_ratelimit())
4153 printk(KERN_WARNING "%s: Not enough memory, "
4154 "dropping tx status", skb->dev->name);
4155 /* should be dev_kfree_skb_irq, but due to this function being
4156 * named _irqsafe instead of just _irq we can't be sure that
4157 * people won't call it from non-irq contexts */
4158 dev_kfree_skb_any(skb);
4159 return;
4161 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
4162 /* copy pointer to saved status into skb->cb for use by tasklet */
4163 memcpy(skb->cb, &saved, sizeof(saved));
4165 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
4166 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
4167 &local->skb_queue : &local->skb_queue_unreliable, skb);
4168 tmp = skb_queue_len(&local->skb_queue) +
4169 skb_queue_len(&local->skb_queue_unreliable);
4170 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
4171 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
4172 memcpy(&saved, skb->cb, sizeof(saved));
4173 kfree(saved);
4174 dev_kfree_skb_irq(skb);
4175 tmp--;
4176 I802_DEBUG_INC(local->tx_status_drop);
4178 tasklet_schedule(&local->tasklet);
4180 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
4182 static void ieee80211_tasklet_handler(unsigned long data)
4184 struct ieee80211_local *local = (struct ieee80211_local *) data;
4185 struct sk_buff *skb;
4186 struct ieee80211_rx_status rx_status;
4187 struct ieee80211_tx_status *tx_status;
4189 while ((skb = skb_dequeue(&local->skb_queue)) ||
4190 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
4191 switch (skb->pkt_type) {
4192 case IEEE80211_RX_MSG:
4193 /* status is in skb->cb */
4194 memcpy(&rx_status, skb->cb, sizeof(rx_status));
4195 /* Clear skb->type in order to not confuse kernel
4196 * netstack. */
4197 skb->pkt_type = 0;
4198 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
4199 break;
4200 case IEEE80211_TX_STATUS_MSG:
4201 /* get pointer to saved status out of skb->cb */
4202 memcpy(&tx_status, skb->cb, sizeof(tx_status));
4203 skb->pkt_type = 0;
4204 ieee80211_tx_status(local_to_hw(local),
4205 skb, tx_status);
4206 kfree(tx_status);
4207 break;
4208 default: /* should never get here! */
4209 printk(KERN_ERR "%s: Unknown message type (%d)\n",
4210 local->mdev->name, skb->pkt_type);
4211 dev_kfree_skb(skb);
4212 break;
4218 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
4219 * make a prepared TX frame (one that has been given to hw) to look like brand
4220 * new IEEE 802.11 frame that is ready to go through TX processing again.
4221 * Also, tx_packet_data in cb is restored from tx_control. */
4222 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
4223 struct ieee80211_key *key,
4224 struct sk_buff *skb,
4225 struct ieee80211_tx_control *control)
4227 int hdrlen, iv_len, mic_len;
4228 struct ieee80211_tx_packet_data *pkt_data;
4230 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
4231 pkt_data->ifindex = control->ifindex;
4232 pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
4233 pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
4234 pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
4235 pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
4236 pkt_data->queue = control->queue;
4238 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
4240 if (!key)
4241 goto no_key;
4243 switch (key->alg) {
4244 case ALG_WEP:
4245 iv_len = WEP_IV_LEN;
4246 mic_len = WEP_ICV_LEN;
4247 break;
4248 case ALG_TKIP:
4249 iv_len = TKIP_IV_LEN;
4250 mic_len = TKIP_ICV_LEN;
4251 break;
4252 case ALG_CCMP:
4253 iv_len = CCMP_HDR_LEN;
4254 mic_len = CCMP_MIC_LEN;
4255 break;
4256 default:
4257 goto no_key;
4260 if (skb->len >= mic_len && key->force_sw_encrypt)
4261 skb_trim(skb, skb->len - mic_len);
4262 if (skb->len >= iv_len && skb->len > hdrlen) {
4263 memmove(skb->data + iv_len, skb->data, hdrlen);
4264 skb_pull(skb, iv_len);
4267 no_key:
4269 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4270 u16 fc = le16_to_cpu(hdr->frame_control);
4271 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
4272 fc &= ~IEEE80211_STYPE_QOS_DATA;
4273 hdr->frame_control = cpu_to_le16(fc);
4274 memmove(skb->data + 2, skb->data, hdrlen - 2);
4275 skb_pull(skb, 2);
4281 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
4282 struct ieee80211_tx_status *status)
4284 struct sk_buff *skb2;
4285 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
4286 struct ieee80211_local *local = hw_to_local(hw);
4287 u16 frag, type;
4288 u32 msg_type;
4290 if (!status) {
4291 printk(KERN_ERR
4292 "%s: ieee80211_tx_status called with NULL status\n",
4293 local->mdev->name);
4294 dev_kfree_skb(skb);
4295 return;
4298 if (status->excessive_retries) {
4299 struct sta_info *sta;
4300 sta = sta_info_get(local, hdr->addr1);
4301 if (sta) {
4302 if (sta->flags & WLAN_STA_PS) {
4303 /* The STA is in power save mode, so assume
4304 * that this TX packet failed because of that.
4306 status->excessive_retries = 0;
4307 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
4309 sta_info_put(sta);
4313 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
4314 struct sta_info *sta;
4315 sta = sta_info_get(local, hdr->addr1);
4316 if (sta) {
4317 sta->tx_filtered_count++;
4319 /* Clear the TX filter mask for this STA when sending
4320 * the next packet. If the STA went to power save mode,
4321 * this will happen when it is waking up for the next
4322 * time. */
4323 sta->clear_dst_mask = 1;
4325 /* TODO: Is the WLAN_STA_PS flag always set here or is
4326 * the race between RX and TX status causing some
4327 * packets to be filtered out before 80211.o gets an
4328 * update for PS status? This seems to be the case, so
4329 * no changes are likely to be needed. */
4330 if (sta->flags & WLAN_STA_PS &&
4331 skb_queue_len(&sta->tx_filtered) <
4332 STA_MAX_TX_BUFFER) {
4333 ieee80211_remove_tx_extra(local, sta->key,
4334 skb,
4335 &status->control);
4336 skb_queue_tail(&sta->tx_filtered, skb);
4337 } else if (!(sta->flags & WLAN_STA_PS) &&
4338 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
4339 /* Software retry the packet once */
4340 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
4341 ieee80211_remove_tx_extra(local, sta->key,
4342 skb,
4343 &status->control);
4344 dev_queue_xmit(skb);
4345 } else {
4346 if (net_ratelimit()) {
4347 printk(KERN_DEBUG "%s: dropped TX "
4348 "filtered frame queue_len=%d "
4349 "PS=%d @%lu\n",
4350 local->mdev->name,
4351 skb_queue_len(
4352 &sta->tx_filtered),
4353 !!(sta->flags & WLAN_STA_PS),
4354 jiffies);
4356 dev_kfree_skb(skb);
4358 sta_info_put(sta);
4359 return;
4361 } else {
4362 /* FIXME: STUPID to call this with both local and local->mdev */
4363 rate_control_tx_status(local, local->mdev, skb, status);
4366 ieee80211_led_tx(local, 0);
4368 /* SNMP counters
4369 * Fragments are passed to low-level drivers as separate skbs, so these
4370 * are actually fragments, not frames. Update frame counters only for
4371 * the first fragment of the frame. */
4373 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
4374 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
4376 if (status->flags & IEEE80211_TX_STATUS_ACK) {
4377 if (frag == 0) {
4378 local->dot11TransmittedFrameCount++;
4379 if (is_multicast_ether_addr(hdr->addr1))
4380 local->dot11MulticastTransmittedFrameCount++;
4381 if (status->retry_count > 0)
4382 local->dot11RetryCount++;
4383 if (status->retry_count > 1)
4384 local->dot11MultipleRetryCount++;
4387 /* This counter shall be incremented for an acknowledged MPDU
4388 * with an individual address in the address 1 field or an MPDU
4389 * with a multicast address in the address 1 field of type Data
4390 * or Management. */
4391 if (!is_multicast_ether_addr(hdr->addr1) ||
4392 type == IEEE80211_FTYPE_DATA ||
4393 type == IEEE80211_FTYPE_MGMT)
4394 local->dot11TransmittedFragmentCount++;
4395 } else {
4396 if (frag == 0)
4397 local->dot11FailedCount++;
4400 if (!(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS)
4401 || unlikely(!local->apdev)) {
4402 dev_kfree_skb(skb);
4403 return;
4406 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
4407 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
4409 /* skb was the original skb used for TX. Clone it and give the clone
4410 * to netif_rx(). Free original skb. */
4411 skb2 = skb_copy(skb, GFP_ATOMIC);
4412 if (!skb2) {
4413 dev_kfree_skb(skb);
4414 return;
4416 dev_kfree_skb(skb);
4417 skb = skb2;
4419 /* Send frame to hostapd */
4420 ieee80211_rx_mgmt(local, skb, NULL, msg_type);
4422 EXPORT_SYMBOL(ieee80211_tx_status);
4424 /* TODO: implement register/unregister functions for adding TX/RX handlers
4425 * into ordered list */
4427 /* rx_pre handlers don't have dev and sdata fields available in
4428 * ieee80211_txrx_data */
4429 static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
4431 ieee80211_rx_h_parse_qos,
4432 ieee80211_rx_h_load_stats,
4433 NULL
4436 static ieee80211_rx_handler ieee80211_rx_handlers[] =
4438 ieee80211_rx_h_if_stats,
4439 ieee80211_rx_h_monitor,
4440 ieee80211_rx_h_passive_scan,
4441 ieee80211_rx_h_check,
4442 ieee80211_rx_h_sta_process,
4443 ieee80211_rx_h_ccmp_decrypt,
4444 ieee80211_rx_h_tkip_decrypt,
4445 ieee80211_rx_h_wep_weak_iv_detection,
4446 ieee80211_rx_h_wep_decrypt,
4447 ieee80211_rx_h_defragment,
4448 ieee80211_rx_h_ps_poll,
4449 ieee80211_rx_h_michael_mic_verify,
4450 /* this must be after decryption - so header is counted in MPDU mic
4451 * must be before pae and data, so QOS_DATA format frames
4452 * are not passed to user space by these functions
4454 ieee80211_rx_h_remove_qos_control,
4455 ieee80211_rx_h_802_1x_pae,
4456 ieee80211_rx_h_drop_unencrypted,
4457 ieee80211_rx_h_data,
4458 ieee80211_rx_h_mgmt,
4459 NULL
4462 static ieee80211_tx_handler ieee80211_tx_handlers[] =
4464 ieee80211_tx_h_check_assoc,
4465 ieee80211_tx_h_sequence,
4466 ieee80211_tx_h_ps_buf,
4467 ieee80211_tx_h_select_key,
4468 ieee80211_tx_h_michael_mic_add,
4469 ieee80211_tx_h_fragment,
4470 ieee80211_tx_h_tkip_encrypt,
4471 ieee80211_tx_h_ccmp_encrypt,
4472 ieee80211_tx_h_wep_encrypt,
4473 ieee80211_tx_h_rate_ctrl,
4474 ieee80211_tx_h_misc,
4475 ieee80211_tx_h_load_stats,
4476 NULL
4480 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
4482 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4483 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4484 struct sta_info *sta;
4486 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
4487 return 0;
4489 /* Create STA entry for the new peer */
4490 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
4491 if (!sta)
4492 return -ENOMEM;
4493 sta_info_put(sta);
4495 /* Remove STA entry for the old peer */
4496 sta = sta_info_get(local, sdata->u.wds.remote_addr);
4497 if (sta) {
4498 sta_info_put(sta);
4499 sta_info_free(sta, 0);
4500 } else {
4501 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
4502 "peer " MAC_FMT "\n",
4503 dev->name, MAC_ARG(sdata->u.wds.remote_addr));
4506 /* Update WDS link data */
4507 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
4509 return 0;
4512 /* Must not be called for mdev and apdev */
4513 void ieee80211_if_setup(struct net_device *dev)
4515 ether_setup(dev);
4516 dev->hard_start_xmit = ieee80211_subif_start_xmit;
4517 dev->wireless_handlers = &ieee80211_iw_handler_def;
4518 dev->set_multicast_list = ieee80211_set_multicast_list;
4519 dev->change_mtu = ieee80211_change_mtu;
4520 dev->get_stats = ieee80211_get_stats;
4521 dev->open = ieee80211_open;
4522 dev->stop = ieee80211_stop;
4523 dev->uninit = ieee80211_if_reinit;
4524 dev->destructor = ieee80211_if_free;
4527 void ieee80211_if_mgmt_setup(struct net_device *dev)
4529 ether_setup(dev);
4530 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
4531 dev->change_mtu = ieee80211_change_mtu_apdev;
4532 dev->get_stats = ieee80211_get_stats;
4533 dev->open = ieee80211_mgmt_open;
4534 dev->stop = ieee80211_mgmt_stop;
4535 dev->type = ARPHRD_IEEE80211_PRISM;
4536 dev->hard_header_parse = header_parse_80211;
4537 dev->uninit = ieee80211_if_reinit;
4538 dev->destructor = ieee80211_if_free;
4541 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
4542 const char *name)
4544 struct rate_control_ref *ref, *old;
4546 ASSERT_RTNL();
4547 if (local->open_count || netif_running(local->mdev) ||
4548 (local->apdev && netif_running(local->apdev)))
4549 return -EBUSY;
4551 ref = rate_control_alloc(name, local);
4552 if (!ref) {
4553 printk(KERN_WARNING "%s: Failed to select rate control "
4554 "algorithm\n", local->mdev->name);
4555 return -ENOENT;
4558 old = local->rate_ctrl;
4559 local->rate_ctrl = ref;
4560 if (old) {
4561 rate_control_put(old);
4562 sta_info_flush(local, NULL);
4565 printk(KERN_DEBUG "%s: Selected rate control "
4566 "algorithm '%s'\n", local->mdev->name,
4567 ref->ops->name);
4570 return 0;
4573 static void rate_control_deinitialize(struct ieee80211_local *local)
4575 struct rate_control_ref *ref;
4577 ref = local->rate_ctrl;
4578 local->rate_ctrl = NULL;
4579 rate_control_put(ref);
4582 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
4583 const struct ieee80211_ops *ops)
4585 struct net_device *mdev;
4586 struct ieee80211_local *local;
4587 struct ieee80211_sub_if_data *sdata;
4588 int priv_size;
4589 struct wiphy *wiphy;
4591 /* Ensure 32-byte alignment of our private data and hw private data.
4592 * We use the wiphy priv data for both our ieee80211_local and for
4593 * the driver's private data
4595 * In memory it'll be like this:
4597 * +-------------------------+
4598 * | struct wiphy |
4599 * +-------------------------+
4600 * | struct ieee80211_local |
4601 * +-------------------------+
4602 * | driver's private data |
4603 * +-------------------------+
4606 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
4608 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
4610 if (!wiphy)
4611 return NULL;
4613 wiphy->privid = mac80211_wiphy_privid;
4615 local = wiphy_priv(wiphy);
4616 local->hw.wiphy = wiphy;
4618 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
4620 local->ops = ops;
4622 /* for now, mdev needs sub_if_data :/ */
4623 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
4624 "wmaster%d", ether_setup);
4625 if (!mdev) {
4626 wiphy_free(wiphy);
4627 return NULL;
4630 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
4631 mdev->ieee80211_ptr = &sdata->wdev;
4632 sdata->wdev.wiphy = wiphy;
4634 local->hw.queues = 1; /* default */
4636 local->mdev = mdev;
4637 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
4638 local->rx_handlers = ieee80211_rx_handlers;
4639 local->tx_handlers = ieee80211_tx_handlers;
4641 local->bridge_packets = 1;
4643 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
4644 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
4645 local->short_retry_limit = 7;
4646 local->long_retry_limit = 4;
4647 local->hw.conf.radio_enabled = 1;
4648 local->rate_ctrl_num_up = RATE_CONTROL_NUM_UP;
4649 local->rate_ctrl_num_down = RATE_CONTROL_NUM_DOWN;
4651 local->enabled_modes = (unsigned int) -1;
4653 INIT_LIST_HEAD(&local->modes_list);
4655 rwlock_init(&local->sub_if_lock);
4656 INIT_LIST_HEAD(&local->sub_if_list);
4658 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
4659 init_timer(&local->stat_timer);
4660 local->stat_timer.function = ieee80211_stat_refresh;
4661 local->stat_timer.data = (unsigned long) local;
4662 ieee80211_rx_bss_list_init(mdev);
4664 sta_info_init(local);
4666 mdev->hard_start_xmit = ieee80211_master_start_xmit;
4667 mdev->open = ieee80211_master_open;
4668 mdev->stop = ieee80211_master_stop;
4669 mdev->type = ARPHRD_IEEE80211;
4670 mdev->hard_header_parse = header_parse_80211;
4672 sdata->type = IEEE80211_IF_TYPE_AP;
4673 sdata->dev = mdev;
4674 sdata->local = local;
4675 sdata->u.ap.force_unicast_rateidx = -1;
4676 sdata->u.ap.max_ratectrl_rateidx = -1;
4677 ieee80211_if_sdata_init(sdata);
4678 list_add_tail(&sdata->list, &local->sub_if_list);
4680 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
4681 (unsigned long)local);
4682 tasklet_disable(&local->tx_pending_tasklet);
4684 tasklet_init(&local->tasklet,
4685 ieee80211_tasklet_handler,
4686 (unsigned long) local);
4687 tasklet_disable(&local->tasklet);
4689 skb_queue_head_init(&local->skb_queue);
4690 skb_queue_head_init(&local->skb_queue_unreliable);
4692 return local_to_hw(local);
4694 EXPORT_SYMBOL(ieee80211_alloc_hw);
4696 int ieee80211_register_hw(struct ieee80211_hw *hw)
4698 struct ieee80211_local *local = hw_to_local(hw);
4699 const char *name;
4700 int result;
4702 result = wiphy_register(local->hw.wiphy);
4703 if (result < 0)
4704 return result;
4706 name = wiphy_dev(local->hw.wiphy)->driver->name;
4707 local->hw.workqueue = create_singlethread_workqueue(name);
4708 if (!local->hw.workqueue) {
4709 result = -ENOMEM;
4710 goto fail_workqueue;
4713 debugfs_hw_add(local);
4715 local->hw.conf.beacon_int = 1000;
4717 local->wstats_flags |= local->hw.max_rssi ?
4718 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
4719 local->wstats_flags |= local->hw.max_signal ?
4720 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
4721 local->wstats_flags |= local->hw.max_noise ?
4722 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
4723 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
4724 local->wstats_flags |= IW_QUAL_DBM;
4726 result = sta_info_start(local);
4727 if (result < 0)
4728 goto fail_sta_info;
4730 rtnl_lock();
4731 result = dev_alloc_name(local->mdev, local->mdev->name);
4732 if (result < 0)
4733 goto fail_dev;
4735 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
4736 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
4738 result = register_netdevice(local->mdev);
4739 if (result < 0)
4740 goto fail_dev;
4742 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
4744 result = ieee80211_init_rate_ctrl_alg(local, NULL);
4745 if (result < 0) {
4746 printk(KERN_DEBUG "%s: Failed to initialize rate control "
4747 "algorithm\n", local->mdev->name);
4748 goto fail_rate;
4751 result = ieee80211_wep_init(local);
4753 if (result < 0) {
4754 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
4755 local->mdev->name);
4756 goto fail_wep;
4759 ieee80211_install_qdisc(local->mdev);
4761 /* add one default STA interface */
4762 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
4763 IEEE80211_IF_TYPE_STA);
4764 if (result)
4765 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
4766 local->mdev->name);
4768 local->reg_state = IEEE80211_DEV_REGISTERED;
4769 rtnl_unlock();
4771 ieee80211_led_init(local);
4773 return 0;
4775 fail_wep:
4776 rate_control_deinitialize(local);
4777 fail_rate:
4778 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
4779 unregister_netdevice(local->mdev);
4780 fail_dev:
4781 rtnl_unlock();
4782 sta_info_stop(local);
4783 fail_sta_info:
4784 debugfs_hw_del(local);
4785 destroy_workqueue(local->hw.workqueue);
4786 fail_workqueue:
4787 wiphy_unregister(local->hw.wiphy);
4788 return result;
4790 EXPORT_SYMBOL(ieee80211_register_hw);
4792 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
4793 struct ieee80211_hw_mode *mode)
4795 struct ieee80211_local *local = hw_to_local(hw);
4796 struct ieee80211_rate *rate;
4797 int i;
4799 INIT_LIST_HEAD(&mode->list);
4800 list_add_tail(&mode->list, &local->modes_list);
4802 local->hw_modes |= (1 << mode->mode);
4803 for (i = 0; i < mode->num_rates; i++) {
4804 rate = &(mode->rates[i]);
4805 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
4807 ieee80211_prepare_rates(local, mode);
4809 if (!local->oper_hw_mode) {
4810 /* Default to this mode */
4811 local->hw.conf.phymode = mode->mode;
4812 local->oper_hw_mode = local->scan_hw_mode = mode;
4813 local->oper_channel = local->scan_channel = &mode->channels[0];
4814 local->hw.conf.mode = local->oper_hw_mode;
4815 local->hw.conf.chan = local->oper_channel;
4818 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
4819 ieee80211_init_client(local->mdev);
4821 return 0;
4823 EXPORT_SYMBOL(ieee80211_register_hwmode);
4825 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
4827 struct ieee80211_local *local = hw_to_local(hw);
4828 struct ieee80211_sub_if_data *sdata, *tmp;
4829 struct list_head tmp_list;
4830 int i;
4832 tasklet_kill(&local->tx_pending_tasklet);
4833 tasklet_kill(&local->tasklet);
4835 rtnl_lock();
4837 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
4839 local->reg_state = IEEE80211_DEV_UNREGISTERED;
4840 if (local->apdev)
4841 ieee80211_if_del_mgmt(local);
4843 write_lock_bh(&local->sub_if_lock);
4844 list_replace_init(&local->sub_if_list, &tmp_list);
4845 write_unlock_bh(&local->sub_if_lock);
4847 list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
4848 __ieee80211_if_del(local, sdata);
4850 rtnl_unlock();
4852 if (local->stat_time)
4853 del_timer_sync(&local->stat_timer);
4855 ieee80211_rx_bss_list_deinit(local->mdev);
4856 ieee80211_clear_tx_pending(local);
4857 sta_info_stop(local);
4858 rate_control_deinitialize(local);
4859 debugfs_hw_del(local);
4861 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
4862 kfree(local->supp_rates[i]);
4863 kfree(local->basic_rates[i]);
4866 if (skb_queue_len(&local->skb_queue)
4867 || skb_queue_len(&local->skb_queue_unreliable))
4868 printk(KERN_WARNING "%s: skb_queue not empty\n",
4869 local->mdev->name);
4870 skb_queue_purge(&local->skb_queue);
4871 skb_queue_purge(&local->skb_queue_unreliable);
4873 destroy_workqueue(local->hw.workqueue);
4874 wiphy_unregister(local->hw.wiphy);
4875 ieee80211_wep_free(local);
4876 ieee80211_led_exit(local);
4878 EXPORT_SYMBOL(ieee80211_unregister_hw);
4880 void ieee80211_free_hw(struct ieee80211_hw *hw)
4882 struct ieee80211_local *local = hw_to_local(hw);
4884 ieee80211_if_free(local->mdev);
4885 wiphy_free(local->hw.wiphy);
4887 EXPORT_SYMBOL(ieee80211_free_hw);
4889 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
4891 struct ieee80211_local *local = hw_to_local(hw);
4893 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
4894 &local->state[queue])) {
4895 if (test_bit(IEEE80211_LINK_STATE_PENDING,
4896 &local->state[queue]))
4897 tasklet_schedule(&local->tx_pending_tasklet);
4898 else
4899 if (!ieee80211_qdisc_installed(local->mdev)) {
4900 if (queue == 0)
4901 netif_wake_queue(local->mdev);
4902 } else
4903 __netif_schedule(local->mdev);
4906 EXPORT_SYMBOL(ieee80211_wake_queue);
4908 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
4910 struct ieee80211_local *local = hw_to_local(hw);
4912 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
4913 netif_stop_queue(local->mdev);
4914 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
4916 EXPORT_SYMBOL(ieee80211_stop_queue);
4918 void ieee80211_start_queues(struct ieee80211_hw *hw)
4920 struct ieee80211_local *local = hw_to_local(hw);
4921 int i;
4923 for (i = 0; i < local->hw.queues; i++)
4924 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
4925 if (!ieee80211_qdisc_installed(local->mdev))
4926 netif_start_queue(local->mdev);
4928 EXPORT_SYMBOL(ieee80211_start_queues);
4930 void ieee80211_stop_queues(struct ieee80211_hw *hw)
4932 int i;
4934 for (i = 0; i < hw->queues; i++)
4935 ieee80211_stop_queue(hw, i);
4937 EXPORT_SYMBOL(ieee80211_stop_queues);
4939 void ieee80211_wake_queues(struct ieee80211_hw *hw)
4941 int i;
4943 for (i = 0; i < hw->queues; i++)
4944 ieee80211_wake_queue(hw, i);
4946 EXPORT_SYMBOL(ieee80211_wake_queues);
4948 struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
4950 struct ieee80211_sub_if_data *sdata;
4951 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4952 return &sdata->stats;
4955 static int __init ieee80211_init(void)
4957 struct sk_buff *skb;
4958 int ret;
4960 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
4962 ret = ieee80211_wme_register();
4963 if (ret) {
4964 printk(KERN_DEBUG "ieee80211_init: failed to "
4965 "initialize WME (err=%d)\n", ret);
4966 return ret;
4969 ieee80211_debugfs_netdev_init();
4971 return 0;
4975 static void __exit ieee80211_exit(void)
4977 ieee80211_wme_unregister();
4978 ieee80211_debugfs_netdev_exit();
4982 module_init(ieee80211_init);
4983 module_exit(ieee80211_exit);
4985 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
4986 MODULE_LICENSE("GPL");