2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/bitmap.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
27 #include "ieee80211_common.h"
28 #include "ieee80211_i.h"
29 #include "ieee80211_rate.h"
33 #include "ieee80211_led.h"
36 #include "debugfs_netdev.h"
39 * For seeing transmitted packets on monitor interfaces
40 * we have a radiotap header too.
42 struct ieee80211_tx_status_rtap_hdr {
43 struct ieee80211_radiotap_header hdr;
46 } __attribute__ ((packed));
48 /* common interface routines */
50 static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
52 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
56 /* must be called under mdev tx lock */
57 static void ieee80211_configure_filter(struct ieee80211_local *local)
59 unsigned int changed_flags;
60 unsigned int new_flags = 0;
62 if (local->iff_promiscs)
63 new_flags |= FIF_PROMISC_IN_BSS;
65 if (local->iff_allmultis)
66 new_flags |= FIF_ALLMULTI;
69 new_flags |= FIF_CONTROL |
71 FIF_BCN_PRBRESP_PROMISC;
73 changed_flags = local->filter_flags ^ new_flags;
78 local->ops->configure_filter(local_to_hw(local),
79 changed_flags, &new_flags,
80 local->mdev->mc_count,
81 local->mdev->mc_list);
83 WARN_ON(new_flags & (1<<31));
85 local->filter_flags = new_flags & ~(1<<31);
88 /* master interface */
90 static int ieee80211_master_open(struct net_device *dev)
92 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
93 struct ieee80211_sub_if_data *sdata;
94 int res = -EOPNOTSUPP;
96 read_lock(&local->sub_if_lock);
97 list_for_each_entry(sdata, &local->sub_if_list, list) {
98 if (sdata->dev != dev && netif_running(sdata->dev)) {
103 read_unlock(&local->sub_if_lock);
107 static int ieee80211_master_stop(struct net_device *dev)
109 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
110 struct ieee80211_sub_if_data *sdata;
112 read_lock(&local->sub_if_lock);
113 list_for_each_entry(sdata, &local->sub_if_list, list)
114 if (sdata->dev != dev && netif_running(sdata->dev))
115 dev_close(sdata->dev);
116 read_unlock(&local->sub_if_lock);
121 static void ieee80211_master_set_multicast_list(struct net_device *dev)
123 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
125 ieee80211_configure_filter(local);
128 /* management interface */
131 ieee80211_fill_frame_info(struct ieee80211_local *local,
132 struct ieee80211_frame_info *fi,
133 struct ieee80211_rx_status *status)
137 struct ieee80211_rate *rate;
139 jiffies_to_timespec(jiffies, &ts);
140 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
142 fi->mactime = cpu_to_be64(status->mactime);
143 switch (status->phymode) {
144 case MODE_IEEE80211A:
145 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
147 case MODE_IEEE80211B:
148 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
150 case MODE_IEEE80211G:
151 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
154 fi->phytype = htonl(0xAAAAAAAA);
157 fi->channel = htonl(status->channel);
158 rate = ieee80211_get_rate(local, status->phymode,
161 fi->datarate = htonl(rate->rate);
162 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
163 if (status->rate == rate->val)
164 fi->preamble = htonl(2); /* long */
165 else if (status->rate == rate->val2)
166 fi->preamble = htonl(1); /* short */
168 fi->preamble = htonl(0);
170 fi->datarate = htonl(0);
171 fi->preamble = htonl(0);
174 fi->antenna = htonl(status->antenna);
175 fi->priority = htonl(0xffffffff); /* no clue */
176 fi->ssi_type = htonl(ieee80211_ssi_raw);
177 fi->ssi_signal = htonl(status->ssi);
178 fi->ssi_noise = 0x00000000;
181 /* clear everything because we really don't know.
182 * the msg_type field isn't present on monitor frames
183 * so we don't know whether it will be present or not,
184 * but it's ok to not clear it since it'll be assigned
186 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
188 fi->ssi_type = htonl(ieee80211_ssi_none);
190 fi->version = htonl(IEEE80211_FI_VERSION);
191 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
194 /* this routine is actually not just for this, but also
195 * for pushing fake 'management' frames into userspace.
196 * it shall be replaced by a netlink-based system. */
198 ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
199 struct ieee80211_rx_status *status, u32 msg_type)
201 struct ieee80211_frame_info *fi;
202 const size_t hlen = sizeof(struct ieee80211_frame_info);
203 struct net_device *dev = local->apdev;
207 if (skb_headroom(skb) < hlen) {
208 I802_DEBUG_INC(local->rx_expand_skb_head);
209 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
215 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
217 ieee80211_fill_frame_info(local, fi, status);
218 fi->msg_type = htonl(msg_type);
220 dev->stats.rx_packets++;
221 dev->stats.rx_bytes += skb->len;
223 skb_set_mac_header(skb, 0);
224 skb->ip_summed = CHECKSUM_UNNECESSARY;
225 skb->pkt_type = PACKET_OTHERHOST;
226 skb->protocol = htons(ETH_P_802_2);
227 memset(skb->cb, 0, sizeof(skb->cb));
231 static int ieee80211_mgmt_open(struct net_device *dev)
233 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
235 if (!netif_running(local->mdev))
240 static int ieee80211_mgmt_stop(struct net_device *dev)
245 static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
247 /* FIX: what would be proper limits for MTU?
248 * This interface uses 802.11 frames. */
249 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
250 printk(KERN_WARNING "%s: invalid MTU %d\n",
255 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
256 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
257 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
262 void ieee80211_if_mgmt_setup(struct net_device *dev)
265 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
266 dev->change_mtu = ieee80211_change_mtu_apdev;
267 dev->open = ieee80211_mgmt_open;
268 dev->stop = ieee80211_mgmt_stop;
269 dev->type = ARPHRD_IEEE80211_PRISM;
270 dev->uninit = ieee80211_if_reinit;
271 dev->destructor = ieee80211_if_free;
274 /* regular interfaces */
276 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
278 /* FIX: what would be proper limits for MTU?
279 * This interface uses 802.3 frames. */
280 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
281 printk(KERN_WARNING "%s: invalid MTU %d\n",
286 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
287 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
288 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
293 static inline int identical_mac_addr_allowed(int type1, int type2)
295 return (type1 == IEEE80211_IF_TYPE_MNTR ||
296 type2 == IEEE80211_IF_TYPE_MNTR ||
297 (type1 == IEEE80211_IF_TYPE_AP &&
298 type2 == IEEE80211_IF_TYPE_WDS) ||
299 (type1 == IEEE80211_IF_TYPE_WDS &&
300 (type2 == IEEE80211_IF_TYPE_WDS ||
301 type2 == IEEE80211_IF_TYPE_AP)) ||
302 (type1 == IEEE80211_IF_TYPE_AP &&
303 type2 == IEEE80211_IF_TYPE_VLAN) ||
304 (type1 == IEEE80211_IF_TYPE_VLAN &&
305 (type2 == IEEE80211_IF_TYPE_AP ||
306 type2 == IEEE80211_IF_TYPE_VLAN)));
309 static int ieee80211_open(struct net_device *dev)
311 struct ieee80211_sub_if_data *sdata, *nsdata;
312 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
313 struct ieee80211_if_init_conf conf;
316 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
317 read_lock(&local->sub_if_lock);
318 list_for_each_entry(nsdata, &local->sub_if_list, list) {
319 struct net_device *ndev = nsdata->dev;
321 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
322 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
323 !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
324 read_unlock(&local->sub_if_lock);
328 read_unlock(&local->sub_if_lock);
330 if (sdata->type == IEEE80211_IF_TYPE_WDS &&
331 is_zero_ether_addr(sdata->u.wds.remote_addr))
334 if (local->open_count == 0) {
336 if (local->ops->start)
337 res = local->ops->start(local_to_hw(local));
342 switch (sdata->type) {
343 case IEEE80211_IF_TYPE_MNTR:
344 /* must be before the call to ieee80211_configure_filter */
346 if (local->monitors == 1) {
347 netif_tx_lock_bh(local->mdev);
348 ieee80211_configure_filter(local);
349 netif_tx_unlock_bh(local->mdev);
351 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
352 ieee80211_hw_config(local);
355 case IEEE80211_IF_TYPE_STA:
356 case IEEE80211_IF_TYPE_IBSS:
357 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
360 conf.if_id = dev->ifindex;
361 conf.type = sdata->type;
362 conf.mac_addr = dev->dev_addr;
363 res = local->ops->add_interface(local_to_hw(local), &conf);
364 if (res && !local->open_count && local->ops->stop)
365 local->ops->stop(local_to_hw(local));
369 ieee80211_if_config(dev);
370 ieee80211_reset_erp_info(dev);
371 ieee80211_enable_keys(sdata);
373 if (sdata->type == IEEE80211_IF_TYPE_STA &&
374 !local->user_space_mlme)
375 netif_carrier_off(dev);
377 netif_carrier_on(dev);
380 if (local->open_count == 0) {
381 res = dev_open(local->mdev);
384 res = dev_open(local->apdev);
387 tasklet_enable(&local->tx_pending_tasklet);
388 tasklet_enable(&local->tasklet);
393 netif_start_queue(dev);
398 static int ieee80211_stop(struct net_device *dev)
400 struct ieee80211_sub_if_data *sdata;
401 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
402 struct ieee80211_if_init_conf conf;
404 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
406 netif_stop_queue(dev);
408 dev_mc_unsync(local->mdev, dev);
412 switch (sdata->type) {
413 case IEEE80211_IF_TYPE_MNTR:
415 if (local->monitors == 0) {
416 netif_tx_lock_bh(local->mdev);
417 ieee80211_configure_filter(local);
418 netif_tx_unlock_bh(local->mdev);
420 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
421 ieee80211_hw_config(local);
424 case IEEE80211_IF_TYPE_STA:
425 case IEEE80211_IF_TYPE_IBSS:
426 sdata->u.sta.state = IEEE80211_DISABLED;
427 del_timer_sync(&sdata->u.sta.timer);
429 * Holding the sub_if_lock for writing here blocks
430 * out the receive path and makes sure it's not
431 * currently processing a packet that may get
432 * added to the queue.
434 write_lock_bh(&local->sub_if_lock);
435 skb_queue_purge(&sdata->u.sta.skb_queue);
436 write_unlock_bh(&local->sub_if_lock);
438 if (!local->ops->hw_scan &&
439 local->scan_dev == sdata->dev) {
440 local->sta_scanning = 0;
441 cancel_delayed_work(&local->scan_work);
443 flush_workqueue(local->hw.workqueue);
446 conf.if_id = dev->ifindex;
447 conf.type = sdata->type;
448 conf.mac_addr = dev->dev_addr;
449 /* disable all keys for as long as this netdev is down */
450 ieee80211_disable_keys(sdata);
451 local->ops->remove_interface(local_to_hw(local), &conf);
454 if (local->open_count == 0) {
455 if (netif_running(local->mdev))
456 dev_close(local->mdev);
459 dev_close(local->apdev);
461 if (local->ops->stop)
462 local->ops->stop(local_to_hw(local));
464 tasklet_disable(&local->tx_pending_tasklet);
465 tasklet_disable(&local->tasklet);
471 static void ieee80211_set_multicast_list(struct net_device *dev)
473 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
474 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
475 int allmulti, promisc, sdata_allmulti, sdata_promisc;
477 allmulti = !!(dev->flags & IFF_ALLMULTI);
478 promisc = !!(dev->flags & IFF_PROMISC);
479 sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
480 sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
482 if (allmulti != sdata_allmulti) {
483 if (dev->flags & IFF_ALLMULTI)
484 local->iff_allmultis++;
486 local->iff_allmultis--;
487 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
490 if (promisc != sdata_promisc) {
491 if (dev->flags & IFF_PROMISC)
492 local->iff_promiscs++;
494 local->iff_promiscs--;
495 sdata->flags ^= IEEE80211_SDATA_PROMISC;
498 dev_mc_sync(local->mdev, dev);
501 static const struct header_ops ieee80211_header_ops = {
502 .create = eth_header,
503 .parse = header_parse_80211,
504 .rebuild = eth_rebuild_header,
505 .cache = eth_header_cache,
506 .cache_update = eth_header_cache_update,
509 /* Must not be called for mdev and apdev */
510 void ieee80211_if_setup(struct net_device *dev)
513 dev->header_ops = &ieee80211_header_ops;
514 dev->hard_start_xmit = ieee80211_subif_start_xmit;
515 dev->wireless_handlers = &ieee80211_iw_handler_def;
516 dev->set_multicast_list = ieee80211_set_multicast_list;
517 dev->change_mtu = ieee80211_change_mtu;
518 dev->open = ieee80211_open;
519 dev->stop = ieee80211_stop;
520 dev->uninit = ieee80211_if_reinit;
521 dev->destructor = ieee80211_if_free;
524 /* WDS specialties */
526 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
528 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
529 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
530 struct sta_info *sta;
531 DECLARE_MAC_BUF(mac);
533 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
536 /* Create STA entry for the new peer */
537 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
542 /* Remove STA entry for the old peer */
543 sta = sta_info_get(local, sdata->u.wds.remote_addr);
548 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
550 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
553 /* Update WDS link data */
554 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
559 /* everything else */
561 static int __ieee80211_if_config(struct net_device *dev,
562 struct sk_buff *beacon,
563 struct ieee80211_tx_control *control)
565 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
566 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
567 struct ieee80211_if_conf conf;
569 if (!local->ops->config_interface || !netif_running(dev))
572 memset(&conf, 0, sizeof(conf));
573 conf.type = sdata->type;
574 if (sdata->type == IEEE80211_IF_TYPE_STA ||
575 sdata->type == IEEE80211_IF_TYPE_IBSS) {
576 conf.bssid = sdata->u.sta.bssid;
577 conf.ssid = sdata->u.sta.ssid;
578 conf.ssid_len = sdata->u.sta.ssid_len;
579 conf.generic_elem = sdata->u.sta.extra_ie;
580 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
581 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
582 conf.ssid = sdata->u.ap.ssid;
583 conf.ssid_len = sdata->u.ap.ssid_len;
584 conf.generic_elem = sdata->u.ap.generic_elem;
585 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
586 conf.beacon = beacon;
587 conf.beacon_control = control;
589 return local->ops->config_interface(local_to_hw(local),
590 dev->ifindex, &conf);
593 int ieee80211_if_config(struct net_device *dev)
595 return __ieee80211_if_config(dev, NULL, NULL);
598 int ieee80211_if_config_beacon(struct net_device *dev)
600 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
601 struct ieee80211_tx_control control;
604 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
606 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
609 return __ieee80211_if_config(dev, skb, &control);
612 int ieee80211_hw_config(struct ieee80211_local *local)
614 struct ieee80211_hw_mode *mode;
615 struct ieee80211_channel *chan;
618 if (local->sta_scanning) {
619 chan = local->scan_channel;
620 mode = local->scan_hw_mode;
622 chan = local->oper_channel;
623 mode = local->oper_hw_mode;
626 local->hw.conf.channel = chan->chan;
627 local->hw.conf.channel_val = chan->val;
628 if (!local->hw.conf.power_level) {
629 local->hw.conf.power_level = chan->power_level;
631 local->hw.conf.power_level = min(chan->power_level,
632 local->hw.conf.power_level);
634 local->hw.conf.freq = chan->freq;
635 local->hw.conf.phymode = mode->mode;
636 local->hw.conf.antenna_max = chan->antenna_max;
637 local->hw.conf.chan = chan;
638 local->hw.conf.mode = mode;
640 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
641 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
642 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
643 local->hw.conf.phymode);
644 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
646 if (local->ops->config)
647 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
652 void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
654 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
655 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
656 if (local->ops->erp_ie_changed)
657 local->ops->erp_ie_changed(local_to_hw(local), changes,
658 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
659 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
662 void ieee80211_reset_erp_info(struct net_device *dev)
664 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
666 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
667 IEEE80211_SDATA_SHORT_PREAMBLE);
668 ieee80211_erp_info_change_notify(dev,
669 IEEE80211_ERP_CHANGE_PROTECTION |
670 IEEE80211_ERP_CHANGE_PREAMBLE);
673 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
675 struct ieee80211_tx_status *status)
677 struct ieee80211_local *local = hw_to_local(hw);
678 struct ieee80211_tx_status *saved;
681 skb->dev = local->mdev;
682 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
683 if (unlikely(!saved)) {
685 printk(KERN_WARNING "%s: Not enough memory, "
686 "dropping tx status", skb->dev->name);
687 /* should be dev_kfree_skb_irq, but due to this function being
688 * named _irqsafe instead of just _irq we can't be sure that
689 * people won't call it from non-irq contexts */
690 dev_kfree_skb_any(skb);
693 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
694 /* copy pointer to saved status into skb->cb for use by tasklet */
695 memcpy(skb->cb, &saved, sizeof(saved));
697 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
698 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
699 &local->skb_queue : &local->skb_queue_unreliable, skb);
700 tmp = skb_queue_len(&local->skb_queue) +
701 skb_queue_len(&local->skb_queue_unreliable);
702 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
703 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
704 memcpy(&saved, skb->cb, sizeof(saved));
706 dev_kfree_skb_irq(skb);
708 I802_DEBUG_INC(local->tx_status_drop);
710 tasklet_schedule(&local->tasklet);
712 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
714 static void ieee80211_tasklet_handler(unsigned long data)
716 struct ieee80211_local *local = (struct ieee80211_local *) data;
718 struct ieee80211_rx_status rx_status;
719 struct ieee80211_tx_status *tx_status;
721 while ((skb = skb_dequeue(&local->skb_queue)) ||
722 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
723 switch (skb->pkt_type) {
724 case IEEE80211_RX_MSG:
725 /* status is in skb->cb */
726 memcpy(&rx_status, skb->cb, sizeof(rx_status));
727 /* Clear skb->type in order to not confuse kernel
730 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
732 case IEEE80211_TX_STATUS_MSG:
733 /* get pointer to saved status out of skb->cb */
734 memcpy(&tx_status, skb->cb, sizeof(tx_status));
736 ieee80211_tx_status(local_to_hw(local),
740 default: /* should never get here! */
741 printk(KERN_ERR "%s: Unknown message type (%d)\n",
742 wiphy_name(local->hw.wiphy), skb->pkt_type);
749 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
750 * make a prepared TX frame (one that has been given to hw) to look like brand
751 * new IEEE 802.11 frame that is ready to go through TX processing again.
752 * Also, tx_packet_data in cb is restored from tx_control. */
753 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
754 struct ieee80211_key *key,
756 struct ieee80211_tx_control *control)
758 int hdrlen, iv_len, mic_len;
759 struct ieee80211_tx_packet_data *pkt_data;
761 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
762 pkt_data->ifindex = control->ifindex;
764 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
765 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
766 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
767 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
768 if (control->flags & IEEE80211_TXCTL_REQUEUE)
769 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
770 if (control->type == IEEE80211_IF_TYPE_MGMT)
771 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
772 pkt_data->queue = control->queue;
774 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
779 switch (key->conf.alg) {
782 mic_len = WEP_ICV_LEN;
785 iv_len = TKIP_IV_LEN;
786 mic_len = TKIP_ICV_LEN;
789 iv_len = CCMP_HDR_LEN;
790 mic_len = CCMP_MIC_LEN;
796 if (skb->len >= mic_len &&
797 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
798 skb_trim(skb, skb->len - mic_len);
799 if (skb->len >= iv_len && skb->len > hdrlen) {
800 memmove(skb->data + iv_len, skb->data, hdrlen);
801 skb_pull(skb, iv_len);
806 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
807 u16 fc = le16_to_cpu(hdr->frame_control);
808 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
809 fc &= ~IEEE80211_STYPE_QOS_DATA;
810 hdr->frame_control = cpu_to_le16(fc);
811 memmove(skb->data + 2, skb->data, hdrlen - 2);
817 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
818 struct ieee80211_tx_status *status)
820 struct sk_buff *skb2;
821 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
822 struct ieee80211_local *local = hw_to_local(hw);
825 struct ieee80211_tx_status_rtap_hdr *rthdr;
826 struct ieee80211_sub_if_data *sdata;
831 "%s: ieee80211_tx_status called with NULL status\n",
832 wiphy_name(local->hw.wiphy));
837 if (status->excessive_retries) {
838 struct sta_info *sta;
839 sta = sta_info_get(local, hdr->addr1);
841 if (sta->flags & WLAN_STA_PS) {
842 /* The STA is in power save mode, so assume
843 * that this TX packet failed because of that.
845 status->excessive_retries = 0;
846 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
852 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
853 struct sta_info *sta;
854 sta = sta_info_get(local, hdr->addr1);
856 sta->tx_filtered_count++;
858 /* Clear the TX filter mask for this STA when sending
859 * the next packet. If the STA went to power save mode,
860 * this will happen when it is waking up for the next
862 sta->clear_dst_mask = 1;
864 /* TODO: Is the WLAN_STA_PS flag always set here or is
865 * the race between RX and TX status causing some
866 * packets to be filtered out before 80211.o gets an
867 * update for PS status? This seems to be the case, so
868 * no changes are likely to be needed. */
869 if (sta->flags & WLAN_STA_PS &&
870 skb_queue_len(&sta->tx_filtered) <
872 ieee80211_remove_tx_extra(local, sta->key,
875 skb_queue_tail(&sta->tx_filtered, skb);
876 } else if (!(sta->flags & WLAN_STA_PS) &&
877 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
878 /* Software retry the packet once */
879 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
880 ieee80211_remove_tx_extra(local, sta->key,
885 if (net_ratelimit()) {
886 printk(KERN_DEBUG "%s: dropped TX "
887 "filtered frame queue_len=%d "
889 wiphy_name(local->hw.wiphy),
892 !!(sta->flags & WLAN_STA_PS),
901 /* FIXME: STUPID to call this with both local and local->mdev */
902 rate_control_tx_status(local, local->mdev, skb, status);
905 ieee80211_led_tx(local, 0);
908 * Fragments are passed to low-level drivers as separate skbs, so these
909 * are actually fragments, not frames. Update frame counters only for
910 * the first fragment of the frame. */
912 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
913 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
915 if (status->flags & IEEE80211_TX_STATUS_ACK) {
917 local->dot11TransmittedFrameCount++;
918 if (is_multicast_ether_addr(hdr->addr1))
919 local->dot11MulticastTransmittedFrameCount++;
920 if (status->retry_count > 0)
921 local->dot11RetryCount++;
922 if (status->retry_count > 1)
923 local->dot11MultipleRetryCount++;
926 /* This counter shall be incremented for an acknowledged MPDU
927 * with an individual address in the address 1 field or an MPDU
928 * with a multicast address in the address 1 field of type Data
930 if (!is_multicast_ether_addr(hdr->addr1) ||
931 type == IEEE80211_FTYPE_DATA ||
932 type == IEEE80211_FTYPE_MGMT)
933 local->dot11TransmittedFragmentCount++;
936 local->dot11FailedCount++;
939 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
940 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
942 /* this was a transmitted frame, but now we want to reuse it */
945 if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
947 if (local->monitors) {
948 skb2 = skb_clone(skb, GFP_ATOMIC);
955 /* Send frame to hostapd */
956 ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
962 if (!local->monitors) {
967 /* send frame to monitor interfaces now */
969 if (skb_headroom(skb) < sizeof(*rthdr)) {
970 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
975 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
976 skb_push(skb, sizeof(*rthdr));
978 memset(rthdr, 0, sizeof(*rthdr));
979 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
980 rthdr->hdr.it_present =
981 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
982 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
984 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
985 !is_multicast_ether_addr(hdr->addr1))
986 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
988 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
989 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
990 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
991 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
992 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
994 rthdr->data_retries = status->retry_count;
996 read_lock(&local->sub_if_lock);
997 monitors = local->monitors;
998 list_for_each_entry(sdata, &local->sub_if_list, list) {
1000 * Using the monitors counter is possibly racy, but
1001 * if the value is wrong we simply either clone the skb
1002 * once too much or forget sending it to one monitor iface
1003 * The latter case isn't nice but fixing the race is much
1006 if (!monitors || !skb)
1009 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
1010 if (!netif_running(sdata->dev))
1014 skb2 = skb_clone(skb, GFP_KERNEL);
1017 skb->dev = sdata->dev;
1018 /* XXX: is this sufficient for BPF? */
1019 skb_set_mac_header(skb, 0);
1020 skb->ip_summed = CHECKSUM_UNNECESSARY;
1021 skb->pkt_type = PACKET_OTHERHOST;
1022 skb->protocol = htons(ETH_P_802_2);
1023 memset(skb->cb, 0, sizeof(skb->cb));
1029 read_unlock(&local->sub_if_lock);
1033 EXPORT_SYMBOL(ieee80211_tx_status);
1035 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1036 const struct ieee80211_ops *ops)
1038 struct net_device *mdev;
1039 struct ieee80211_local *local;
1040 struct ieee80211_sub_if_data *sdata;
1042 struct wiphy *wiphy;
1044 /* Ensure 32-byte alignment of our private data and hw private data.
1045 * We use the wiphy priv data for both our ieee80211_local and for
1046 * the driver's private data
1048 * In memory it'll be like this:
1050 * +-------------------------+
1052 * +-------------------------+
1053 * | struct ieee80211_local |
1054 * +-------------------------+
1055 * | driver's private data |
1056 * +-------------------------+
1059 priv_size = ((sizeof(struct ieee80211_local) +
1060 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1063 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1068 wiphy->privid = mac80211_wiphy_privid;
1070 local = wiphy_priv(wiphy);
1071 local->hw.wiphy = wiphy;
1073 local->hw.priv = (char *)local +
1074 ((sizeof(struct ieee80211_local) +
1075 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1078 BUG_ON(!ops->start);
1080 BUG_ON(!ops->config);
1081 BUG_ON(!ops->add_interface);
1082 BUG_ON(!ops->remove_interface);
1083 BUG_ON(!ops->configure_filter);
1086 /* for now, mdev needs sub_if_data :/ */
1087 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1088 "wmaster%d", ether_setup);
1094 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1095 mdev->ieee80211_ptr = &sdata->wdev;
1096 sdata->wdev.wiphy = wiphy;
1098 local->hw.queues = 1; /* default */
1101 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1102 local->rx_handlers = ieee80211_rx_handlers;
1103 local->tx_handlers = ieee80211_tx_handlers;
1105 local->bridge_packets = 1;
1107 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1108 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1109 local->short_retry_limit = 7;
1110 local->long_retry_limit = 4;
1111 local->hw.conf.radio_enabled = 1;
1113 local->enabled_modes = ~0;
1115 INIT_LIST_HEAD(&local->modes_list);
1117 rwlock_init(&local->sub_if_lock);
1118 INIT_LIST_HEAD(&local->sub_if_list);
1120 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1121 ieee80211_rx_bss_list_init(mdev);
1123 sta_info_init(local);
1125 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1126 mdev->open = ieee80211_master_open;
1127 mdev->stop = ieee80211_master_stop;
1128 mdev->type = ARPHRD_IEEE80211;
1129 mdev->header_ops = &ieee80211_header_ops;
1130 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1132 sdata->type = IEEE80211_IF_TYPE_AP;
1134 sdata->local = local;
1135 sdata->u.ap.force_unicast_rateidx = -1;
1136 sdata->u.ap.max_ratectrl_rateidx = -1;
1137 ieee80211_if_sdata_init(sdata);
1138 list_add_tail(&sdata->list, &local->sub_if_list);
1140 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1141 (unsigned long)local);
1142 tasklet_disable(&local->tx_pending_tasklet);
1144 tasklet_init(&local->tasklet,
1145 ieee80211_tasklet_handler,
1146 (unsigned long) local);
1147 tasklet_disable(&local->tasklet);
1149 skb_queue_head_init(&local->skb_queue);
1150 skb_queue_head_init(&local->skb_queue_unreliable);
1152 return local_to_hw(local);
1154 EXPORT_SYMBOL(ieee80211_alloc_hw);
1156 int ieee80211_register_hw(struct ieee80211_hw *hw)
1158 struct ieee80211_local *local = hw_to_local(hw);
1162 result = wiphy_register(local->hw.wiphy);
1166 name = wiphy_dev(local->hw.wiphy)->driver->name;
1167 local->hw.workqueue = create_singlethread_workqueue(name);
1168 if (!local->hw.workqueue) {
1170 goto fail_workqueue;
1174 * The hardware needs headroom for sending the frame,
1175 * and we need some headroom for passing the frame to monitor
1176 * interfaces, but never both at the same time.
1178 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1179 sizeof(struct ieee80211_tx_status_rtap_hdr));
1181 debugfs_hw_add(local);
1183 local->hw.conf.beacon_int = 1000;
1185 local->wstats_flags |= local->hw.max_rssi ?
1186 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1187 local->wstats_flags |= local->hw.max_signal ?
1188 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1189 local->wstats_flags |= local->hw.max_noise ?
1190 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1191 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1192 local->wstats_flags |= IW_QUAL_DBM;
1194 result = sta_info_start(local);
1199 result = dev_alloc_name(local->mdev, local->mdev->name);
1203 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1204 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1206 result = register_netdevice(local->mdev);
1210 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1212 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1214 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1215 "algorithm\n", wiphy_name(local->hw.wiphy));
1219 result = ieee80211_wep_init(local);
1222 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1223 wiphy_name(local->hw.wiphy));
1227 ieee80211_install_qdisc(local->mdev);
1229 /* add one default STA interface */
1230 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1231 IEEE80211_IF_TYPE_STA);
1233 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1234 wiphy_name(local->hw.wiphy));
1236 local->reg_state = IEEE80211_DEV_REGISTERED;
1239 ieee80211_led_init(local);
1244 rate_control_deinitialize(local);
1246 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1247 unregister_netdevice(local->mdev);
1250 sta_info_stop(local);
1252 debugfs_hw_del(local);
1253 destroy_workqueue(local->hw.workqueue);
1255 wiphy_unregister(local->hw.wiphy);
1258 EXPORT_SYMBOL(ieee80211_register_hw);
1260 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1261 struct ieee80211_hw_mode *mode)
1263 struct ieee80211_local *local = hw_to_local(hw);
1264 struct ieee80211_rate *rate;
1267 INIT_LIST_HEAD(&mode->list);
1268 list_add_tail(&mode->list, &local->modes_list);
1270 local->hw_modes |= (1 << mode->mode);
1271 for (i = 0; i < mode->num_rates; i++) {
1272 rate = &(mode->rates[i]);
1273 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1275 ieee80211_prepare_rates(local, mode);
1277 if (!local->oper_hw_mode) {
1278 /* Default to this mode */
1279 local->hw.conf.phymode = mode->mode;
1280 local->oper_hw_mode = local->scan_hw_mode = mode;
1281 local->oper_channel = local->scan_channel = &mode->channels[0];
1282 local->hw.conf.mode = local->oper_hw_mode;
1283 local->hw.conf.chan = local->oper_channel;
1286 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1287 ieee80211_set_default_regdomain(mode);
1291 EXPORT_SYMBOL(ieee80211_register_hwmode);
1293 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1295 struct ieee80211_local *local = hw_to_local(hw);
1296 struct ieee80211_sub_if_data *sdata, *tmp;
1297 struct list_head tmp_list;
1300 tasklet_kill(&local->tx_pending_tasklet);
1301 tasklet_kill(&local->tasklet);
1305 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1307 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1309 ieee80211_if_del_mgmt(local);
1311 write_lock_bh(&local->sub_if_lock);
1312 list_replace_init(&local->sub_if_list, &tmp_list);
1313 write_unlock_bh(&local->sub_if_lock);
1315 list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
1316 __ieee80211_if_del(local, sdata);
1320 ieee80211_rx_bss_list_deinit(local->mdev);
1321 ieee80211_clear_tx_pending(local);
1322 sta_info_stop(local);
1323 rate_control_deinitialize(local);
1324 debugfs_hw_del(local);
1326 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1327 kfree(local->supp_rates[i]);
1328 kfree(local->basic_rates[i]);
1331 if (skb_queue_len(&local->skb_queue)
1332 || skb_queue_len(&local->skb_queue_unreliable))
1333 printk(KERN_WARNING "%s: skb_queue not empty\n",
1334 wiphy_name(local->hw.wiphy));
1335 skb_queue_purge(&local->skb_queue);
1336 skb_queue_purge(&local->skb_queue_unreliable);
1338 destroy_workqueue(local->hw.workqueue);
1339 wiphy_unregister(local->hw.wiphy);
1340 ieee80211_wep_free(local);
1341 ieee80211_led_exit(local);
1343 EXPORT_SYMBOL(ieee80211_unregister_hw);
1345 void ieee80211_free_hw(struct ieee80211_hw *hw)
1347 struct ieee80211_local *local = hw_to_local(hw);
1349 ieee80211_if_free(local->mdev);
1350 wiphy_free(local->hw.wiphy);
1352 EXPORT_SYMBOL(ieee80211_free_hw);
1354 static int __init ieee80211_init(void)
1356 struct sk_buff *skb;
1359 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1361 ret = ieee80211_wme_register();
1363 printk(KERN_DEBUG "ieee80211_init: failed to "
1364 "initialize WME (err=%d)\n", ret);
1368 ieee80211_debugfs_netdev_init();
1369 ieee80211_regdomain_init();
1374 static void __exit ieee80211_exit(void)
1376 ieee80211_wme_unregister();
1377 ieee80211_debugfs_netdev_exit();
1381 subsys_initcall(ieee80211_init);
1382 module_exit(ieee80211_exit);
1384 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1385 MODULE_LICENSE("GPL");