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);
318 read_lock(&local->sub_if_lock);
319 list_for_each_entry(nsdata, &local->sub_if_list, list) {
320 struct net_device *ndev = nsdata->dev;
322 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
323 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
325 * check whether it may have the same address
327 if (!identical_mac_addr_allowed(sdata->type,
329 read_unlock(&local->sub_if_lock);
334 * can only add VLANs to enabled APs
336 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
337 nsdata->type == IEEE80211_IF_TYPE_AP &&
338 netif_running(nsdata->dev))
339 sdata->u.vlan.ap = nsdata;
342 read_unlock(&local->sub_if_lock);
344 switch (sdata->type) {
345 case IEEE80211_IF_TYPE_WDS:
346 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
349 case IEEE80211_IF_TYPE_VLAN:
350 if (!sdata->u.vlan.ap)
355 if (local->open_count == 0) {
357 if (local->ops->start)
358 res = local->ops->start(local_to_hw(local));
363 switch (sdata->type) {
364 case IEEE80211_IF_TYPE_VLAN:
365 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
366 /* no need to tell driver */
368 case IEEE80211_IF_TYPE_MNTR:
369 /* must be before the call to ieee80211_configure_filter */
371 if (local->monitors == 1) {
372 netif_tx_lock_bh(local->mdev);
373 ieee80211_configure_filter(local);
374 netif_tx_unlock_bh(local->mdev);
376 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
377 ieee80211_hw_config(local);
380 case IEEE80211_IF_TYPE_STA:
381 case IEEE80211_IF_TYPE_IBSS:
382 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
385 conf.if_id = dev->ifindex;
386 conf.type = sdata->type;
387 conf.mac_addr = dev->dev_addr;
388 res = local->ops->add_interface(local_to_hw(local), &conf);
389 if (res && !local->open_count && local->ops->stop)
390 local->ops->stop(local_to_hw(local));
394 ieee80211_if_config(dev);
395 ieee80211_reset_erp_info(dev);
396 ieee80211_enable_keys(sdata);
398 if (sdata->type == IEEE80211_IF_TYPE_STA &&
399 !local->user_space_mlme)
400 netif_carrier_off(dev);
402 netif_carrier_on(dev);
405 if (local->open_count == 0) {
406 res = dev_open(local->mdev);
409 res = dev_open(local->apdev);
412 tasklet_enable(&local->tx_pending_tasklet);
413 tasklet_enable(&local->tasklet);
418 netif_start_queue(dev);
423 static int ieee80211_stop(struct net_device *dev)
425 struct ieee80211_sub_if_data *sdata;
426 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
427 struct ieee80211_if_init_conf conf;
429 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
431 netif_stop_queue(dev);
433 dev_mc_unsync(local->mdev, dev);
435 /* down all dependent devices, that is VLANs */
436 if (sdata->type == IEEE80211_IF_TYPE_AP) {
437 struct ieee80211_sub_if_data *vlan, *tmp;
439 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
441 dev_close(vlan->dev);
442 WARN_ON(!list_empty(&sdata->u.ap.vlans));
447 switch (sdata->type) {
448 case IEEE80211_IF_TYPE_VLAN:
449 list_del(&sdata->u.vlan.list);
450 sdata->u.vlan.ap = NULL;
451 /* no need to tell driver */
453 case IEEE80211_IF_TYPE_MNTR:
455 if (local->monitors == 0) {
456 netif_tx_lock_bh(local->mdev);
457 ieee80211_configure_filter(local);
458 netif_tx_unlock_bh(local->mdev);
460 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
461 ieee80211_hw_config(local);
464 case IEEE80211_IF_TYPE_STA:
465 case IEEE80211_IF_TYPE_IBSS:
466 sdata->u.sta.state = IEEE80211_DISABLED;
467 del_timer_sync(&sdata->u.sta.timer);
469 * Holding the sub_if_lock for writing here blocks
470 * out the receive path and makes sure it's not
471 * currently processing a packet that may get
472 * added to the queue.
474 write_lock_bh(&local->sub_if_lock);
475 skb_queue_purge(&sdata->u.sta.skb_queue);
476 write_unlock_bh(&local->sub_if_lock);
478 if (!local->ops->hw_scan &&
479 local->scan_dev == sdata->dev) {
480 local->sta_scanning = 0;
481 cancel_delayed_work(&local->scan_work);
483 flush_workqueue(local->hw.workqueue);
486 conf.if_id = dev->ifindex;
487 conf.type = sdata->type;
488 conf.mac_addr = dev->dev_addr;
489 /* disable all keys for as long as this netdev is down */
490 ieee80211_disable_keys(sdata);
491 local->ops->remove_interface(local_to_hw(local), &conf);
494 if (local->open_count == 0) {
495 if (netif_running(local->mdev))
496 dev_close(local->mdev);
499 dev_close(local->apdev);
501 if (local->ops->stop)
502 local->ops->stop(local_to_hw(local));
504 tasklet_disable(&local->tx_pending_tasklet);
505 tasklet_disable(&local->tasklet);
511 static void ieee80211_set_multicast_list(struct net_device *dev)
513 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
514 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
515 int allmulti, promisc, sdata_allmulti, sdata_promisc;
517 allmulti = !!(dev->flags & IFF_ALLMULTI);
518 promisc = !!(dev->flags & IFF_PROMISC);
519 sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
520 sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
522 if (allmulti != sdata_allmulti) {
523 if (dev->flags & IFF_ALLMULTI)
524 local->iff_allmultis++;
526 local->iff_allmultis--;
527 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
530 if (promisc != sdata_promisc) {
531 if (dev->flags & IFF_PROMISC)
532 local->iff_promiscs++;
534 local->iff_promiscs--;
535 sdata->flags ^= IEEE80211_SDATA_PROMISC;
538 dev_mc_sync(local->mdev, dev);
541 static const struct header_ops ieee80211_header_ops = {
542 .create = eth_header,
543 .parse = header_parse_80211,
544 .rebuild = eth_rebuild_header,
545 .cache = eth_header_cache,
546 .cache_update = eth_header_cache_update,
549 /* Must not be called for mdev and apdev */
550 void ieee80211_if_setup(struct net_device *dev)
553 dev->header_ops = &ieee80211_header_ops;
554 dev->hard_start_xmit = ieee80211_subif_start_xmit;
555 dev->wireless_handlers = &ieee80211_iw_handler_def;
556 dev->set_multicast_list = ieee80211_set_multicast_list;
557 dev->change_mtu = ieee80211_change_mtu;
558 dev->open = ieee80211_open;
559 dev->stop = ieee80211_stop;
560 dev->uninit = ieee80211_if_reinit;
561 dev->destructor = ieee80211_if_free;
564 /* WDS specialties */
566 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
568 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
569 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
570 struct sta_info *sta;
571 DECLARE_MAC_BUF(mac);
573 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
576 /* Create STA entry for the new peer */
577 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
582 /* Remove STA entry for the old peer */
583 sta = sta_info_get(local, sdata->u.wds.remote_addr);
588 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
590 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
593 /* Update WDS link data */
594 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
599 /* everything else */
601 static int __ieee80211_if_config(struct net_device *dev,
602 struct sk_buff *beacon,
603 struct ieee80211_tx_control *control)
605 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
606 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
607 struct ieee80211_if_conf conf;
609 if (!local->ops->config_interface || !netif_running(dev))
612 memset(&conf, 0, sizeof(conf));
613 conf.type = sdata->type;
614 if (sdata->type == IEEE80211_IF_TYPE_STA ||
615 sdata->type == IEEE80211_IF_TYPE_IBSS) {
616 conf.bssid = sdata->u.sta.bssid;
617 conf.ssid = sdata->u.sta.ssid;
618 conf.ssid_len = sdata->u.sta.ssid_len;
619 conf.generic_elem = sdata->u.sta.extra_ie;
620 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
621 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
622 conf.ssid = sdata->u.ap.ssid;
623 conf.ssid_len = sdata->u.ap.ssid_len;
624 conf.generic_elem = sdata->u.ap.generic_elem;
625 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
626 conf.beacon = beacon;
627 conf.beacon_control = control;
629 return local->ops->config_interface(local_to_hw(local),
630 dev->ifindex, &conf);
633 int ieee80211_if_config(struct net_device *dev)
635 return __ieee80211_if_config(dev, NULL, NULL);
638 int ieee80211_if_config_beacon(struct net_device *dev)
640 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
641 struct ieee80211_tx_control control;
644 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
646 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
649 return __ieee80211_if_config(dev, skb, &control);
652 int ieee80211_hw_config(struct ieee80211_local *local)
654 struct ieee80211_hw_mode *mode;
655 struct ieee80211_channel *chan;
658 if (local->sta_scanning) {
659 chan = local->scan_channel;
660 mode = local->scan_hw_mode;
662 chan = local->oper_channel;
663 mode = local->oper_hw_mode;
666 local->hw.conf.channel = chan->chan;
667 local->hw.conf.channel_val = chan->val;
668 if (!local->hw.conf.power_level) {
669 local->hw.conf.power_level = chan->power_level;
671 local->hw.conf.power_level = min(chan->power_level,
672 local->hw.conf.power_level);
674 local->hw.conf.freq = chan->freq;
675 local->hw.conf.phymode = mode->mode;
676 local->hw.conf.antenna_max = chan->antenna_max;
677 local->hw.conf.chan = chan;
678 local->hw.conf.mode = mode;
680 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
681 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
682 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
683 local->hw.conf.phymode);
684 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
686 if (local->ops->config)
687 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
692 void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
694 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
695 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
696 if (local->ops->erp_ie_changed)
697 local->ops->erp_ie_changed(local_to_hw(local), changes,
698 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
699 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
702 void ieee80211_reset_erp_info(struct net_device *dev)
704 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
706 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
707 IEEE80211_SDATA_SHORT_PREAMBLE);
708 ieee80211_erp_info_change_notify(dev,
709 IEEE80211_ERP_CHANGE_PROTECTION |
710 IEEE80211_ERP_CHANGE_PREAMBLE);
713 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
715 struct ieee80211_tx_status *status)
717 struct ieee80211_local *local = hw_to_local(hw);
718 struct ieee80211_tx_status *saved;
721 skb->dev = local->mdev;
722 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
723 if (unlikely(!saved)) {
725 printk(KERN_WARNING "%s: Not enough memory, "
726 "dropping tx status", skb->dev->name);
727 /* should be dev_kfree_skb_irq, but due to this function being
728 * named _irqsafe instead of just _irq we can't be sure that
729 * people won't call it from non-irq contexts */
730 dev_kfree_skb_any(skb);
733 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
734 /* copy pointer to saved status into skb->cb for use by tasklet */
735 memcpy(skb->cb, &saved, sizeof(saved));
737 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
738 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
739 &local->skb_queue : &local->skb_queue_unreliable, skb);
740 tmp = skb_queue_len(&local->skb_queue) +
741 skb_queue_len(&local->skb_queue_unreliable);
742 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
743 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
744 memcpy(&saved, skb->cb, sizeof(saved));
746 dev_kfree_skb_irq(skb);
748 I802_DEBUG_INC(local->tx_status_drop);
750 tasklet_schedule(&local->tasklet);
752 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
754 static void ieee80211_tasklet_handler(unsigned long data)
756 struct ieee80211_local *local = (struct ieee80211_local *) data;
758 struct ieee80211_rx_status rx_status;
759 struct ieee80211_tx_status *tx_status;
761 while ((skb = skb_dequeue(&local->skb_queue)) ||
762 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
763 switch (skb->pkt_type) {
764 case IEEE80211_RX_MSG:
765 /* status is in skb->cb */
766 memcpy(&rx_status, skb->cb, sizeof(rx_status));
767 /* Clear skb->type in order to not confuse kernel
770 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
772 case IEEE80211_TX_STATUS_MSG:
773 /* get pointer to saved status out of skb->cb */
774 memcpy(&tx_status, skb->cb, sizeof(tx_status));
776 ieee80211_tx_status(local_to_hw(local),
780 default: /* should never get here! */
781 printk(KERN_ERR "%s: Unknown message type (%d)\n",
782 wiphy_name(local->hw.wiphy), skb->pkt_type);
789 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
790 * make a prepared TX frame (one that has been given to hw) to look like brand
791 * new IEEE 802.11 frame that is ready to go through TX processing again.
792 * Also, tx_packet_data in cb is restored from tx_control. */
793 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
794 struct ieee80211_key *key,
796 struct ieee80211_tx_control *control)
798 int hdrlen, iv_len, mic_len;
799 struct ieee80211_tx_packet_data *pkt_data;
801 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
802 pkt_data->ifindex = control->ifindex;
804 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
805 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
806 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
807 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
808 if (control->flags & IEEE80211_TXCTL_REQUEUE)
809 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
810 if (control->type == IEEE80211_IF_TYPE_MGMT)
811 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
812 pkt_data->queue = control->queue;
814 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
819 switch (key->conf.alg) {
822 mic_len = WEP_ICV_LEN;
825 iv_len = TKIP_IV_LEN;
826 mic_len = TKIP_ICV_LEN;
829 iv_len = CCMP_HDR_LEN;
830 mic_len = CCMP_MIC_LEN;
836 if (skb->len >= mic_len &&
837 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
838 skb_trim(skb, skb->len - mic_len);
839 if (skb->len >= iv_len && skb->len > hdrlen) {
840 memmove(skb->data + iv_len, skb->data, hdrlen);
841 skb_pull(skb, iv_len);
846 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
847 u16 fc = le16_to_cpu(hdr->frame_control);
848 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
849 fc &= ~IEEE80211_STYPE_QOS_DATA;
850 hdr->frame_control = cpu_to_le16(fc);
851 memmove(skb->data + 2, skb->data, hdrlen - 2);
857 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
858 struct ieee80211_tx_status *status)
860 struct sk_buff *skb2;
861 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
862 struct ieee80211_local *local = hw_to_local(hw);
865 struct ieee80211_tx_status_rtap_hdr *rthdr;
866 struct ieee80211_sub_if_data *sdata;
871 "%s: ieee80211_tx_status called with NULL status\n",
872 wiphy_name(local->hw.wiphy));
877 if (status->excessive_retries) {
878 struct sta_info *sta;
879 sta = sta_info_get(local, hdr->addr1);
881 if (sta->flags & WLAN_STA_PS) {
882 /* The STA is in power save mode, so assume
883 * that this TX packet failed because of that.
885 status->excessive_retries = 0;
886 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
892 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
893 struct sta_info *sta;
894 sta = sta_info_get(local, hdr->addr1);
896 sta->tx_filtered_count++;
898 /* Clear the TX filter mask for this STA when sending
899 * the next packet. If the STA went to power save mode,
900 * this will happen when it is waking up for the next
902 sta->clear_dst_mask = 1;
904 /* TODO: Is the WLAN_STA_PS flag always set here or is
905 * the race between RX and TX status causing some
906 * packets to be filtered out before 80211.o gets an
907 * update for PS status? This seems to be the case, so
908 * no changes are likely to be needed. */
909 if (sta->flags & WLAN_STA_PS &&
910 skb_queue_len(&sta->tx_filtered) <
912 ieee80211_remove_tx_extra(local, sta->key,
915 skb_queue_tail(&sta->tx_filtered, skb);
916 } else if (!(sta->flags & WLAN_STA_PS) &&
917 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
918 /* Software retry the packet once */
919 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
920 ieee80211_remove_tx_extra(local, sta->key,
925 if (net_ratelimit()) {
926 printk(KERN_DEBUG "%s: dropped TX "
927 "filtered frame queue_len=%d "
929 wiphy_name(local->hw.wiphy),
932 !!(sta->flags & WLAN_STA_PS),
941 /* FIXME: STUPID to call this with both local and local->mdev */
942 rate_control_tx_status(local, local->mdev, skb, status);
945 ieee80211_led_tx(local, 0);
948 * Fragments are passed to low-level drivers as separate skbs, so these
949 * are actually fragments, not frames. Update frame counters only for
950 * the first fragment of the frame. */
952 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
953 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
955 if (status->flags & IEEE80211_TX_STATUS_ACK) {
957 local->dot11TransmittedFrameCount++;
958 if (is_multicast_ether_addr(hdr->addr1))
959 local->dot11MulticastTransmittedFrameCount++;
960 if (status->retry_count > 0)
961 local->dot11RetryCount++;
962 if (status->retry_count > 1)
963 local->dot11MultipleRetryCount++;
966 /* This counter shall be incremented for an acknowledged MPDU
967 * with an individual address in the address 1 field or an MPDU
968 * with a multicast address in the address 1 field of type Data
970 if (!is_multicast_ether_addr(hdr->addr1) ||
971 type == IEEE80211_FTYPE_DATA ||
972 type == IEEE80211_FTYPE_MGMT)
973 local->dot11TransmittedFragmentCount++;
976 local->dot11FailedCount++;
979 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
980 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
982 /* this was a transmitted frame, but now we want to reuse it */
985 if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
987 if (local->monitors) {
988 skb2 = skb_clone(skb, GFP_ATOMIC);
995 /* Send frame to hostapd */
996 ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
1002 if (!local->monitors) {
1007 /* send frame to monitor interfaces now */
1009 if (skb_headroom(skb) < sizeof(*rthdr)) {
1010 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1015 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1016 skb_push(skb, sizeof(*rthdr));
1018 memset(rthdr, 0, sizeof(*rthdr));
1019 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1020 rthdr->hdr.it_present =
1021 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1022 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1024 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1025 !is_multicast_ether_addr(hdr->addr1))
1026 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1028 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1029 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1030 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1031 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1032 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1034 rthdr->data_retries = status->retry_count;
1036 read_lock(&local->sub_if_lock);
1037 monitors = local->monitors;
1038 list_for_each_entry(sdata, &local->sub_if_list, list) {
1040 * Using the monitors counter is possibly racy, but
1041 * if the value is wrong we simply either clone the skb
1042 * once too much or forget sending it to one monitor iface
1043 * The latter case isn't nice but fixing the race is much
1046 if (!monitors || !skb)
1049 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
1050 if (!netif_running(sdata->dev))
1054 skb2 = skb_clone(skb, GFP_KERNEL);
1057 skb->dev = sdata->dev;
1058 /* XXX: is this sufficient for BPF? */
1059 skb_set_mac_header(skb, 0);
1060 skb->ip_summed = CHECKSUM_UNNECESSARY;
1061 skb->pkt_type = PACKET_OTHERHOST;
1062 skb->protocol = htons(ETH_P_802_2);
1063 memset(skb->cb, 0, sizeof(skb->cb));
1069 read_unlock(&local->sub_if_lock);
1073 EXPORT_SYMBOL(ieee80211_tx_status);
1075 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1076 const struct ieee80211_ops *ops)
1078 struct net_device *mdev;
1079 struct ieee80211_local *local;
1080 struct ieee80211_sub_if_data *sdata;
1082 struct wiphy *wiphy;
1084 /* Ensure 32-byte alignment of our private data and hw private data.
1085 * We use the wiphy priv data for both our ieee80211_local and for
1086 * the driver's private data
1088 * In memory it'll be like this:
1090 * +-------------------------+
1092 * +-------------------------+
1093 * | struct ieee80211_local |
1094 * +-------------------------+
1095 * | driver's private data |
1096 * +-------------------------+
1099 priv_size = ((sizeof(struct ieee80211_local) +
1100 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1103 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1108 wiphy->privid = mac80211_wiphy_privid;
1110 local = wiphy_priv(wiphy);
1111 local->hw.wiphy = wiphy;
1113 local->hw.priv = (char *)local +
1114 ((sizeof(struct ieee80211_local) +
1115 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1118 BUG_ON(!ops->start);
1120 BUG_ON(!ops->config);
1121 BUG_ON(!ops->add_interface);
1122 BUG_ON(!ops->remove_interface);
1123 BUG_ON(!ops->configure_filter);
1126 /* for now, mdev needs sub_if_data :/ */
1127 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1128 "wmaster%d", ether_setup);
1134 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1135 mdev->ieee80211_ptr = &sdata->wdev;
1136 sdata->wdev.wiphy = wiphy;
1138 local->hw.queues = 1; /* default */
1141 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1142 local->rx_handlers = ieee80211_rx_handlers;
1143 local->tx_handlers = ieee80211_tx_handlers;
1145 local->bridge_packets = 1;
1147 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1148 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1149 local->short_retry_limit = 7;
1150 local->long_retry_limit = 4;
1151 local->hw.conf.radio_enabled = 1;
1153 local->enabled_modes = ~0;
1155 INIT_LIST_HEAD(&local->modes_list);
1157 rwlock_init(&local->sub_if_lock);
1158 INIT_LIST_HEAD(&local->sub_if_list);
1160 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1161 ieee80211_rx_bss_list_init(mdev);
1163 sta_info_init(local);
1165 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1166 mdev->open = ieee80211_master_open;
1167 mdev->stop = ieee80211_master_stop;
1168 mdev->type = ARPHRD_IEEE80211;
1169 mdev->header_ops = &ieee80211_header_ops;
1170 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1172 sdata->type = IEEE80211_IF_TYPE_AP;
1174 sdata->local = local;
1175 sdata->u.ap.force_unicast_rateidx = -1;
1176 sdata->u.ap.max_ratectrl_rateidx = -1;
1177 ieee80211_if_sdata_init(sdata);
1178 list_add_tail(&sdata->list, &local->sub_if_list);
1180 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1181 (unsigned long)local);
1182 tasklet_disable(&local->tx_pending_tasklet);
1184 tasklet_init(&local->tasklet,
1185 ieee80211_tasklet_handler,
1186 (unsigned long) local);
1187 tasklet_disable(&local->tasklet);
1189 skb_queue_head_init(&local->skb_queue);
1190 skb_queue_head_init(&local->skb_queue_unreliable);
1192 return local_to_hw(local);
1194 EXPORT_SYMBOL(ieee80211_alloc_hw);
1196 int ieee80211_register_hw(struct ieee80211_hw *hw)
1198 struct ieee80211_local *local = hw_to_local(hw);
1202 result = wiphy_register(local->hw.wiphy);
1206 name = wiphy_dev(local->hw.wiphy)->driver->name;
1207 local->hw.workqueue = create_singlethread_workqueue(name);
1208 if (!local->hw.workqueue) {
1210 goto fail_workqueue;
1214 * The hardware needs headroom for sending the frame,
1215 * and we need some headroom for passing the frame to monitor
1216 * interfaces, but never both at the same time.
1218 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1219 sizeof(struct ieee80211_tx_status_rtap_hdr));
1221 debugfs_hw_add(local);
1223 local->hw.conf.beacon_int = 1000;
1225 local->wstats_flags |= local->hw.max_rssi ?
1226 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1227 local->wstats_flags |= local->hw.max_signal ?
1228 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1229 local->wstats_flags |= local->hw.max_noise ?
1230 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1231 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1232 local->wstats_flags |= IW_QUAL_DBM;
1234 result = sta_info_start(local);
1239 result = dev_alloc_name(local->mdev, local->mdev->name);
1243 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1244 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1246 result = register_netdevice(local->mdev);
1250 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1252 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1254 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1255 "algorithm\n", wiphy_name(local->hw.wiphy));
1259 result = ieee80211_wep_init(local);
1262 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1263 wiphy_name(local->hw.wiphy));
1267 ieee80211_install_qdisc(local->mdev);
1269 /* add one default STA interface */
1270 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1271 IEEE80211_IF_TYPE_STA);
1273 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1274 wiphy_name(local->hw.wiphy));
1276 local->reg_state = IEEE80211_DEV_REGISTERED;
1279 ieee80211_led_init(local);
1284 rate_control_deinitialize(local);
1286 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1287 unregister_netdevice(local->mdev);
1290 sta_info_stop(local);
1292 debugfs_hw_del(local);
1293 destroy_workqueue(local->hw.workqueue);
1295 wiphy_unregister(local->hw.wiphy);
1298 EXPORT_SYMBOL(ieee80211_register_hw);
1300 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1301 struct ieee80211_hw_mode *mode)
1303 struct ieee80211_local *local = hw_to_local(hw);
1304 struct ieee80211_rate *rate;
1307 INIT_LIST_HEAD(&mode->list);
1308 list_add_tail(&mode->list, &local->modes_list);
1310 local->hw_modes |= (1 << mode->mode);
1311 for (i = 0; i < mode->num_rates; i++) {
1312 rate = &(mode->rates[i]);
1313 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1315 ieee80211_prepare_rates(local, mode);
1317 if (!local->oper_hw_mode) {
1318 /* Default to this mode */
1319 local->hw.conf.phymode = mode->mode;
1320 local->oper_hw_mode = local->scan_hw_mode = mode;
1321 local->oper_channel = local->scan_channel = &mode->channels[0];
1322 local->hw.conf.mode = local->oper_hw_mode;
1323 local->hw.conf.chan = local->oper_channel;
1326 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1327 ieee80211_set_default_regdomain(mode);
1331 EXPORT_SYMBOL(ieee80211_register_hwmode);
1333 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1335 struct ieee80211_local *local = hw_to_local(hw);
1336 struct ieee80211_sub_if_data *sdata, *tmp;
1337 struct list_head tmp_list;
1340 tasklet_kill(&local->tx_pending_tasklet);
1341 tasklet_kill(&local->tasklet);
1345 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1347 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1349 ieee80211_if_del_mgmt(local);
1351 write_lock_bh(&local->sub_if_lock);
1352 list_replace_init(&local->sub_if_list, &tmp_list);
1353 write_unlock_bh(&local->sub_if_lock);
1355 list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
1356 __ieee80211_if_del(local, sdata);
1360 ieee80211_rx_bss_list_deinit(local->mdev);
1361 ieee80211_clear_tx_pending(local);
1362 sta_info_stop(local);
1363 rate_control_deinitialize(local);
1364 debugfs_hw_del(local);
1366 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1367 kfree(local->supp_rates[i]);
1368 kfree(local->basic_rates[i]);
1371 if (skb_queue_len(&local->skb_queue)
1372 || skb_queue_len(&local->skb_queue_unreliable))
1373 printk(KERN_WARNING "%s: skb_queue not empty\n",
1374 wiphy_name(local->hw.wiphy));
1375 skb_queue_purge(&local->skb_queue);
1376 skb_queue_purge(&local->skb_queue_unreliable);
1378 destroy_workqueue(local->hw.workqueue);
1379 wiphy_unregister(local->hw.wiphy);
1380 ieee80211_wep_free(local);
1381 ieee80211_led_exit(local);
1383 EXPORT_SYMBOL(ieee80211_unregister_hw);
1385 void ieee80211_free_hw(struct ieee80211_hw *hw)
1387 struct ieee80211_local *local = hw_to_local(hw);
1389 ieee80211_if_free(local->mdev);
1390 wiphy_free(local->hw.wiphy);
1392 EXPORT_SYMBOL(ieee80211_free_hw);
1394 static int __init ieee80211_init(void)
1396 struct sk_buff *skb;
1399 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1401 ret = ieee80211_wme_register();
1403 printk(KERN_DEBUG "ieee80211_init: failed to "
1404 "initialize WME (err=%d)\n", ret);
1408 ieee80211_debugfs_netdev_init();
1409 ieee80211_regdomain_init();
1414 static void __exit ieee80211_exit(void)
1416 ieee80211_wme_unregister();
1417 ieee80211_debugfs_netdev_exit();
1421 subsys_initcall(ieee80211_init);
1422 module_exit(ieee80211_exit);
1424 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1425 MODULE_LICENSE("GPL");