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_i.h"
28 #include "ieee80211_rate.h"
32 #include "ieee80211_led.h"
35 #include "debugfs_netdev.h"
38 * For seeing transmitted packets on monitor interfaces
39 * we have a radiotap header too.
41 struct ieee80211_tx_status_rtap_hdr {
42 struct ieee80211_radiotap_header hdr;
45 } __attribute__ ((packed));
47 /* common interface routines */
49 static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
51 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
55 /* must be called under mdev tx lock */
56 static void ieee80211_configure_filter(struct ieee80211_local *local)
58 unsigned int changed_flags;
59 unsigned int new_flags = 0;
61 if (atomic_read(&local->iff_promiscs))
62 new_flags |= FIF_PROMISC_IN_BSS;
64 if (atomic_read(&local->iff_allmultis))
65 new_flags |= FIF_ALLMULTI;
68 new_flags |= FIF_CONTROL |
70 FIF_BCN_PRBRESP_PROMISC;
72 changed_flags = local->filter_flags ^ new_flags;
77 local->ops->configure_filter(local_to_hw(local),
78 changed_flags, &new_flags,
79 local->mdev->mc_count,
80 local->mdev->mc_list);
82 WARN_ON(new_flags & (1<<31));
84 local->filter_flags = new_flags & ~(1<<31);
87 /* master interface */
89 static int ieee80211_master_open(struct net_device *dev)
91 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
92 struct ieee80211_sub_if_data *sdata;
93 int res = -EOPNOTSUPP;
95 /* we hold the RTNL here so can safely walk the list */
96 list_for_each_entry(sdata, &local->interfaces, list) {
97 if (sdata->dev != dev && netif_running(sdata->dev)) {
105 static int ieee80211_master_stop(struct net_device *dev)
107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
108 struct ieee80211_sub_if_data *sdata;
110 /* we hold the RTNL here so can safely walk the list */
111 list_for_each_entry(sdata, &local->interfaces, list)
112 if (sdata->dev != dev && netif_running(sdata->dev))
113 dev_close(sdata->dev);
118 static void ieee80211_master_set_multicast_list(struct net_device *dev)
120 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
122 ieee80211_configure_filter(local);
125 /* regular interfaces */
127 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
129 /* FIX: what would be proper limits for MTU?
130 * This interface uses 802.3 frames. */
131 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
132 printk(KERN_WARNING "%s: invalid MTU %d\n",
137 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
138 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
139 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
144 static inline int identical_mac_addr_allowed(int type1, int type2)
146 return (type1 == IEEE80211_IF_TYPE_MNTR ||
147 type2 == IEEE80211_IF_TYPE_MNTR ||
148 (type1 == IEEE80211_IF_TYPE_AP &&
149 type2 == IEEE80211_IF_TYPE_WDS) ||
150 (type1 == IEEE80211_IF_TYPE_WDS &&
151 (type2 == IEEE80211_IF_TYPE_WDS ||
152 type2 == IEEE80211_IF_TYPE_AP)) ||
153 (type1 == IEEE80211_IF_TYPE_AP &&
154 type2 == IEEE80211_IF_TYPE_VLAN) ||
155 (type1 == IEEE80211_IF_TYPE_VLAN &&
156 (type2 == IEEE80211_IF_TYPE_AP ||
157 type2 == IEEE80211_IF_TYPE_VLAN)));
160 static int ieee80211_open(struct net_device *dev)
162 struct ieee80211_sub_if_data *sdata, *nsdata;
163 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
164 struct ieee80211_if_init_conf conf;
167 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
169 /* we hold the RTNL here so can safely walk the list */
170 list_for_each_entry(nsdata, &local->interfaces, list) {
171 struct net_device *ndev = nsdata->dev;
173 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
174 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
176 * check whether it may have the same address
178 if (!identical_mac_addr_allowed(sdata->type,
183 * can only add VLANs to enabled APs
185 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
186 nsdata->type == IEEE80211_IF_TYPE_AP &&
187 netif_running(nsdata->dev))
188 sdata->u.vlan.ap = nsdata;
192 switch (sdata->type) {
193 case IEEE80211_IF_TYPE_WDS:
194 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
197 case IEEE80211_IF_TYPE_VLAN:
198 if (!sdata->u.vlan.ap)
201 case IEEE80211_IF_TYPE_AP:
202 case IEEE80211_IF_TYPE_STA:
203 case IEEE80211_IF_TYPE_MNTR:
204 case IEEE80211_IF_TYPE_IBSS:
205 /* no special treatment */
207 case IEEE80211_IF_TYPE_INVALID:
213 if (local->open_count == 0) {
215 if (local->ops->start)
216 res = local->ops->start(local_to_hw(local));
221 switch (sdata->type) {
222 case IEEE80211_IF_TYPE_VLAN:
223 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
224 /* no need to tell driver */
226 case IEEE80211_IF_TYPE_MNTR:
227 /* must be before the call to ieee80211_configure_filter */
229 if (local->monitors == 1) {
230 netif_tx_lock_bh(local->mdev);
231 ieee80211_configure_filter(local);
232 netif_tx_unlock_bh(local->mdev);
234 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
235 ieee80211_hw_config(local);
238 case IEEE80211_IF_TYPE_STA:
239 case IEEE80211_IF_TYPE_IBSS:
240 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
243 conf.if_id = dev->ifindex;
244 conf.type = sdata->type;
245 conf.mac_addr = dev->dev_addr;
246 res = local->ops->add_interface(local_to_hw(local), &conf);
247 if (res && !local->open_count && local->ops->stop)
248 local->ops->stop(local_to_hw(local));
252 ieee80211_if_config(dev);
253 ieee80211_reset_erp_info(dev);
254 ieee80211_enable_keys(sdata);
256 if (sdata->type == IEEE80211_IF_TYPE_STA &&
257 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
258 netif_carrier_off(dev);
260 netif_carrier_on(dev);
263 if (local->open_count == 0) {
264 res = dev_open(local->mdev);
266 tasklet_enable(&local->tx_pending_tasklet);
267 tasklet_enable(&local->tasklet);
271 * set_multicast_list will be invoked by the networking core
272 * which will check whether any increments here were done in
273 * error and sync them down to the hardware as filter flags.
275 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
276 atomic_inc(&local->iff_allmultis);
278 if (sdata->flags & IEEE80211_SDATA_PROMISC)
279 atomic_inc(&local->iff_promiscs);
283 netif_start_queue(dev);
288 static int ieee80211_stop(struct net_device *dev)
290 struct ieee80211_sub_if_data *sdata;
291 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
292 struct ieee80211_if_init_conf conf;
294 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
296 netif_stop_queue(dev);
299 * Don't count this interface for promisc/allmulti while it
300 * is down. dev_mc_unsync() will invoke set_multicast_list
301 * on the master interface which will sync these down to the
302 * hardware as filter flags.
304 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
305 atomic_dec(&local->iff_allmultis);
307 if (sdata->flags & IEEE80211_SDATA_PROMISC)
308 atomic_dec(&local->iff_promiscs);
310 dev_mc_unsync(local->mdev, dev);
312 /* down all dependent devices, that is VLANs */
313 if (sdata->type == IEEE80211_IF_TYPE_AP) {
314 struct ieee80211_sub_if_data *vlan, *tmp;
316 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
318 dev_close(vlan->dev);
319 WARN_ON(!list_empty(&sdata->u.ap.vlans));
324 switch (sdata->type) {
325 case IEEE80211_IF_TYPE_VLAN:
326 list_del(&sdata->u.vlan.list);
327 sdata->u.vlan.ap = NULL;
328 /* no need to tell driver */
330 case IEEE80211_IF_TYPE_MNTR:
332 if (local->monitors == 0) {
333 netif_tx_lock_bh(local->mdev);
334 ieee80211_configure_filter(local);
335 netif_tx_unlock_bh(local->mdev);
337 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
338 ieee80211_hw_config(local);
341 case IEEE80211_IF_TYPE_STA:
342 case IEEE80211_IF_TYPE_IBSS:
343 sdata->u.sta.state = IEEE80211_DISABLED;
344 del_timer_sync(&sdata->u.sta.timer);
346 * When we get here, the interface is marked down.
347 * Call synchronize_rcu() to wait for the RX path
348 * should it be using the interface and enqueuing
349 * frames at this very time on another CPU.
352 skb_queue_purge(&sdata->u.sta.skb_queue);
354 if (!local->ops->hw_scan &&
355 local->scan_dev == sdata->dev) {
356 local->sta_scanning = 0;
357 cancel_delayed_work(&local->scan_work);
359 flush_workqueue(local->hw.workqueue);
362 conf.if_id = dev->ifindex;
363 conf.type = sdata->type;
364 conf.mac_addr = dev->dev_addr;
365 /* disable all keys for as long as this netdev is down */
366 ieee80211_disable_keys(sdata);
367 local->ops->remove_interface(local_to_hw(local), &conf);
370 if (local->open_count == 0) {
371 if (netif_running(local->mdev))
372 dev_close(local->mdev);
374 if (local->ops->stop)
375 local->ops->stop(local_to_hw(local));
377 tasklet_disable(&local->tx_pending_tasklet);
378 tasklet_disable(&local->tasklet);
384 static void ieee80211_set_multicast_list(struct net_device *dev)
386 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
387 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
388 int allmulti, promisc, sdata_allmulti, sdata_promisc;
390 allmulti = !!(dev->flags & IFF_ALLMULTI);
391 promisc = !!(dev->flags & IFF_PROMISC);
392 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
393 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
395 if (allmulti != sdata_allmulti) {
396 if (dev->flags & IFF_ALLMULTI)
397 atomic_inc(&local->iff_allmultis);
399 atomic_dec(&local->iff_allmultis);
400 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
403 if (promisc != sdata_promisc) {
404 if (dev->flags & IFF_PROMISC)
405 atomic_inc(&local->iff_promiscs);
407 atomic_dec(&local->iff_promiscs);
408 sdata->flags ^= IEEE80211_SDATA_PROMISC;
411 dev_mc_sync(local->mdev, dev);
414 static const struct header_ops ieee80211_header_ops = {
415 .create = eth_header,
416 .parse = header_parse_80211,
417 .rebuild = eth_rebuild_header,
418 .cache = eth_header_cache,
419 .cache_update = eth_header_cache_update,
422 /* Must not be called for mdev */
423 void ieee80211_if_setup(struct net_device *dev)
426 dev->header_ops = &ieee80211_header_ops;
427 dev->hard_start_xmit = ieee80211_subif_start_xmit;
428 dev->wireless_handlers = &ieee80211_iw_handler_def;
429 dev->set_multicast_list = ieee80211_set_multicast_list;
430 dev->change_mtu = ieee80211_change_mtu;
431 dev->open = ieee80211_open;
432 dev->stop = ieee80211_stop;
433 dev->destructor = ieee80211_if_free;
436 /* WDS specialties */
438 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
440 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
441 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 struct sta_info *sta;
443 DECLARE_MAC_BUF(mac);
445 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
448 /* Create STA entry for the new peer */
449 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
454 /* Remove STA entry for the old peer */
455 sta = sta_info_get(local, sdata->u.wds.remote_addr);
460 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
462 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
465 /* Update WDS link data */
466 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
471 /* everything else */
473 static int __ieee80211_if_config(struct net_device *dev,
474 struct sk_buff *beacon,
475 struct ieee80211_tx_control *control)
477 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
478 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
479 struct ieee80211_if_conf conf;
481 if (!local->ops->config_interface || !netif_running(dev))
484 memset(&conf, 0, sizeof(conf));
485 conf.type = sdata->type;
486 if (sdata->type == IEEE80211_IF_TYPE_STA ||
487 sdata->type == IEEE80211_IF_TYPE_IBSS) {
488 conf.bssid = sdata->u.sta.bssid;
489 conf.ssid = sdata->u.sta.ssid;
490 conf.ssid_len = sdata->u.sta.ssid_len;
491 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
492 conf.ssid = sdata->u.ap.ssid;
493 conf.ssid_len = sdata->u.ap.ssid_len;
494 conf.beacon = beacon;
495 conf.beacon_control = control;
497 return local->ops->config_interface(local_to_hw(local),
498 dev->ifindex, &conf);
501 int ieee80211_if_config(struct net_device *dev)
503 return __ieee80211_if_config(dev, NULL, NULL);
506 int ieee80211_if_config_beacon(struct net_device *dev)
508 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
509 struct ieee80211_tx_control control;
512 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
514 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
517 return __ieee80211_if_config(dev, skb, &control);
520 int ieee80211_hw_config(struct ieee80211_local *local)
522 struct ieee80211_hw_mode *mode;
523 struct ieee80211_channel *chan;
526 if (local->sta_scanning) {
527 chan = local->scan_channel;
528 mode = local->scan_hw_mode;
530 chan = local->oper_channel;
531 mode = local->oper_hw_mode;
534 local->hw.conf.channel = chan->chan;
535 local->hw.conf.channel_val = chan->val;
536 if (!local->hw.conf.power_level) {
537 local->hw.conf.power_level = chan->power_level;
539 local->hw.conf.power_level = min(chan->power_level,
540 local->hw.conf.power_level);
542 local->hw.conf.freq = chan->freq;
543 local->hw.conf.phymode = mode->mode;
544 local->hw.conf.antenna_max = chan->antenna_max;
545 local->hw.conf.chan = chan;
546 local->hw.conf.mode = mode;
548 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
549 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
550 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
551 local->hw.conf.phymode);
552 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
554 if (local->open_count)
555 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
560 void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
562 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
563 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
564 if (local->ops->erp_ie_changed)
565 local->ops->erp_ie_changed(local_to_hw(local), changes,
566 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
567 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
570 void ieee80211_reset_erp_info(struct net_device *dev)
572 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
574 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
575 IEEE80211_SDATA_SHORT_PREAMBLE);
576 ieee80211_erp_info_change_notify(dev,
577 IEEE80211_ERP_CHANGE_PROTECTION |
578 IEEE80211_ERP_CHANGE_PREAMBLE);
581 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
583 struct ieee80211_tx_status *status)
585 struct ieee80211_local *local = hw_to_local(hw);
586 struct ieee80211_tx_status *saved;
589 skb->dev = local->mdev;
590 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
591 if (unlikely(!saved)) {
593 printk(KERN_WARNING "%s: Not enough memory, "
594 "dropping tx status", skb->dev->name);
595 /* should be dev_kfree_skb_irq, but due to this function being
596 * named _irqsafe instead of just _irq we can't be sure that
597 * people won't call it from non-irq contexts */
598 dev_kfree_skb_any(skb);
601 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
602 /* copy pointer to saved status into skb->cb for use by tasklet */
603 memcpy(skb->cb, &saved, sizeof(saved));
605 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
606 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
607 &local->skb_queue : &local->skb_queue_unreliable, skb);
608 tmp = skb_queue_len(&local->skb_queue) +
609 skb_queue_len(&local->skb_queue_unreliable);
610 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
611 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
612 memcpy(&saved, skb->cb, sizeof(saved));
614 dev_kfree_skb_irq(skb);
616 I802_DEBUG_INC(local->tx_status_drop);
618 tasklet_schedule(&local->tasklet);
620 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
622 static void ieee80211_tasklet_handler(unsigned long data)
624 struct ieee80211_local *local = (struct ieee80211_local *) data;
626 struct ieee80211_rx_status rx_status;
627 struct ieee80211_tx_status *tx_status;
629 while ((skb = skb_dequeue(&local->skb_queue)) ||
630 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
631 switch (skb->pkt_type) {
632 case IEEE80211_RX_MSG:
633 /* status is in skb->cb */
634 memcpy(&rx_status, skb->cb, sizeof(rx_status));
635 /* Clear skb->type in order to not confuse kernel
638 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
640 case IEEE80211_TX_STATUS_MSG:
641 /* get pointer to saved status out of skb->cb */
642 memcpy(&tx_status, skb->cb, sizeof(tx_status));
644 ieee80211_tx_status(local_to_hw(local),
648 default: /* should never get here! */
649 printk(KERN_ERR "%s: Unknown message type (%d)\n",
650 wiphy_name(local->hw.wiphy), skb->pkt_type);
657 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
658 * make a prepared TX frame (one that has been given to hw) to look like brand
659 * new IEEE 802.11 frame that is ready to go through TX processing again.
660 * Also, tx_packet_data in cb is restored from tx_control. */
661 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
662 struct ieee80211_key *key,
664 struct ieee80211_tx_control *control)
666 int hdrlen, iv_len, mic_len;
667 struct ieee80211_tx_packet_data *pkt_data;
669 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
670 pkt_data->ifindex = control->ifindex;
672 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
673 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
674 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
675 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
676 if (control->flags & IEEE80211_TXCTL_REQUEUE)
677 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
678 pkt_data->queue = control->queue;
680 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
685 switch (key->conf.alg) {
688 mic_len = WEP_ICV_LEN;
691 iv_len = TKIP_IV_LEN;
692 mic_len = TKIP_ICV_LEN;
695 iv_len = CCMP_HDR_LEN;
696 mic_len = CCMP_MIC_LEN;
702 if (skb->len >= mic_len &&
703 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
704 skb_trim(skb, skb->len - mic_len);
705 if (skb->len >= iv_len && skb->len > hdrlen) {
706 memmove(skb->data + iv_len, skb->data, hdrlen);
707 skb_pull(skb, iv_len);
712 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
713 u16 fc = le16_to_cpu(hdr->frame_control);
714 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
715 fc &= ~IEEE80211_STYPE_QOS_DATA;
716 hdr->frame_control = cpu_to_le16(fc);
717 memmove(skb->data + 2, skb->data, hdrlen - 2);
723 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
724 struct ieee80211_tx_status *status)
726 struct sk_buff *skb2;
727 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
728 struct ieee80211_local *local = hw_to_local(hw);
730 struct ieee80211_tx_status_rtap_hdr *rthdr;
731 struct ieee80211_sub_if_data *sdata;
736 "%s: ieee80211_tx_status called with NULL status\n",
737 wiphy_name(local->hw.wiphy));
742 if (status->excessive_retries) {
743 struct sta_info *sta;
744 sta = sta_info_get(local, hdr->addr1);
746 if (sta->flags & WLAN_STA_PS) {
747 /* The STA is in power save mode, so assume
748 * that this TX packet failed because of that.
750 status->excessive_retries = 0;
751 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
757 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
758 struct sta_info *sta;
759 sta = sta_info_get(local, hdr->addr1);
761 sta->tx_filtered_count++;
763 /* Clear the TX filter mask for this STA when sending
764 * the next packet. If the STA went to power save mode,
765 * this will happen when it is waking up for the next
767 sta->clear_dst_mask = 1;
769 /* TODO: Is the WLAN_STA_PS flag always set here or is
770 * the race between RX and TX status causing some
771 * packets to be filtered out before 80211.o gets an
772 * update for PS status? This seems to be the case, so
773 * no changes are likely to be needed. */
774 if (sta->flags & WLAN_STA_PS &&
775 skb_queue_len(&sta->tx_filtered) <
777 ieee80211_remove_tx_extra(local, sta->key,
780 skb_queue_tail(&sta->tx_filtered, skb);
781 } else if (!(sta->flags & WLAN_STA_PS) &&
782 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
783 /* Software retry the packet once */
784 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
785 ieee80211_remove_tx_extra(local, sta->key,
790 if (net_ratelimit()) {
791 printk(KERN_DEBUG "%s: dropped TX "
792 "filtered frame queue_len=%d "
794 wiphy_name(local->hw.wiphy),
797 !!(sta->flags & WLAN_STA_PS),
806 /* FIXME: STUPID to call this with both local and local->mdev */
807 rate_control_tx_status(local, local->mdev, skb, status);
810 ieee80211_led_tx(local, 0);
813 * Fragments are passed to low-level drivers as separate skbs, so these
814 * are actually fragments, not frames. Update frame counters only for
815 * the first fragment of the frame. */
817 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
818 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
820 if (status->flags & IEEE80211_TX_STATUS_ACK) {
822 local->dot11TransmittedFrameCount++;
823 if (is_multicast_ether_addr(hdr->addr1))
824 local->dot11MulticastTransmittedFrameCount++;
825 if (status->retry_count > 0)
826 local->dot11RetryCount++;
827 if (status->retry_count > 1)
828 local->dot11MultipleRetryCount++;
831 /* This counter shall be incremented for an acknowledged MPDU
832 * with an individual address in the address 1 field or an MPDU
833 * with a multicast address in the address 1 field of type Data
835 if (!is_multicast_ether_addr(hdr->addr1) ||
836 type == IEEE80211_FTYPE_DATA ||
837 type == IEEE80211_FTYPE_MGMT)
838 local->dot11TransmittedFragmentCount++;
841 local->dot11FailedCount++;
844 /* this was a transmitted frame, but now we want to reuse it */
847 if (!local->monitors) {
852 /* send frame to monitor interfaces now */
854 if (skb_headroom(skb) < sizeof(*rthdr)) {
855 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
860 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
861 skb_push(skb, sizeof(*rthdr));
863 memset(rthdr, 0, sizeof(*rthdr));
864 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
865 rthdr->hdr.it_present =
866 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
867 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
869 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
870 !is_multicast_ether_addr(hdr->addr1))
871 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
873 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
874 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
875 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
876 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
877 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
879 rthdr->data_retries = status->retry_count;
882 monitors = local->monitors;
883 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
885 * Using the monitors counter is possibly racy, but
886 * if the value is wrong we simply either clone the skb
887 * once too much or forget sending it to one monitor iface
888 * The latter case isn't nice but fixing the race is much
891 if (!monitors || !skb)
894 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
895 if (!netif_running(sdata->dev))
899 skb2 = skb_clone(skb, GFP_ATOMIC);
902 skb->dev = sdata->dev;
903 /* XXX: is this sufficient for BPF? */
904 skb_set_mac_header(skb, 0);
905 skb->ip_summed = CHECKSUM_UNNECESSARY;
906 skb->pkt_type = PACKET_OTHERHOST;
907 skb->protocol = htons(ETH_P_802_2);
908 memset(skb->cb, 0, sizeof(skb->cb));
918 EXPORT_SYMBOL(ieee80211_tx_status);
920 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
921 const struct ieee80211_ops *ops)
923 struct net_device *mdev;
924 struct ieee80211_local *local;
925 struct ieee80211_sub_if_data *sdata;
929 /* Ensure 32-byte alignment of our private data and hw private data.
930 * We use the wiphy priv data for both our ieee80211_local and for
931 * the driver's private data
933 * In memory it'll be like this:
935 * +-------------------------+
937 * +-------------------------+
938 * | struct ieee80211_local |
939 * +-------------------------+
940 * | driver's private data |
941 * +-------------------------+
944 priv_size = ((sizeof(struct ieee80211_local) +
945 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
948 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
953 wiphy->privid = mac80211_wiphy_privid;
955 local = wiphy_priv(wiphy);
956 local->hw.wiphy = wiphy;
958 local->hw.priv = (char *)local +
959 ((sizeof(struct ieee80211_local) +
960 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
965 BUG_ON(!ops->config);
966 BUG_ON(!ops->add_interface);
967 BUG_ON(!ops->remove_interface);
968 BUG_ON(!ops->configure_filter);
971 /* for now, mdev needs sub_if_data :/ */
972 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
973 "wmaster%d", ether_setup);
979 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
980 mdev->ieee80211_ptr = &sdata->wdev;
981 sdata->wdev.wiphy = wiphy;
983 local->hw.queues = 1; /* default */
986 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
987 local->rx_handlers = ieee80211_rx_handlers;
988 local->tx_handlers = ieee80211_tx_handlers;
990 local->bridge_packets = 1;
992 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
993 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
994 local->short_retry_limit = 7;
995 local->long_retry_limit = 4;
996 local->hw.conf.radio_enabled = 1;
998 local->enabled_modes = ~0;
1000 INIT_LIST_HEAD(&local->modes_list);
1002 INIT_LIST_HEAD(&local->interfaces);
1004 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1005 ieee80211_rx_bss_list_init(mdev);
1007 sta_info_init(local);
1009 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1010 mdev->open = ieee80211_master_open;
1011 mdev->stop = ieee80211_master_stop;
1012 mdev->type = ARPHRD_IEEE80211;
1013 mdev->header_ops = &ieee80211_header_ops;
1014 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1016 sdata->type = IEEE80211_IF_TYPE_AP;
1018 sdata->local = local;
1019 sdata->u.ap.force_unicast_rateidx = -1;
1020 sdata->u.ap.max_ratectrl_rateidx = -1;
1021 ieee80211_if_sdata_init(sdata);
1022 /* no RCU needed since we're still during init phase */
1023 list_add_tail(&sdata->list, &local->interfaces);
1025 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1026 (unsigned long)local);
1027 tasklet_disable(&local->tx_pending_tasklet);
1029 tasklet_init(&local->tasklet,
1030 ieee80211_tasklet_handler,
1031 (unsigned long) local);
1032 tasklet_disable(&local->tasklet);
1034 skb_queue_head_init(&local->skb_queue);
1035 skb_queue_head_init(&local->skb_queue_unreliable);
1037 return local_to_hw(local);
1039 EXPORT_SYMBOL(ieee80211_alloc_hw);
1041 int ieee80211_register_hw(struct ieee80211_hw *hw)
1043 struct ieee80211_local *local = hw_to_local(hw);
1047 result = wiphy_register(local->hw.wiphy);
1051 name = wiphy_dev(local->hw.wiphy)->driver->name;
1052 local->hw.workqueue = create_singlethread_workqueue(name);
1053 if (!local->hw.workqueue) {
1055 goto fail_workqueue;
1059 * The hardware needs headroom for sending the frame,
1060 * and we need some headroom for passing the frame to monitor
1061 * interfaces, but never both at the same time.
1063 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1064 sizeof(struct ieee80211_tx_status_rtap_hdr));
1066 debugfs_hw_add(local);
1068 local->hw.conf.beacon_int = 1000;
1070 local->wstats_flags |= local->hw.max_rssi ?
1071 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1072 local->wstats_flags |= local->hw.max_signal ?
1073 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1074 local->wstats_flags |= local->hw.max_noise ?
1075 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1076 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1077 local->wstats_flags |= IW_QUAL_DBM;
1079 result = sta_info_start(local);
1084 result = dev_alloc_name(local->mdev, local->mdev->name);
1088 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1089 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1091 result = register_netdevice(local->mdev);
1095 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1096 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
1098 result = ieee80211_init_rate_ctrl_alg(local,
1099 hw->rate_control_algorithm);
1101 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1102 "algorithm\n", wiphy_name(local->hw.wiphy));
1106 result = ieee80211_wep_init(local);
1109 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1110 wiphy_name(local->hw.wiphy));
1114 ieee80211_install_qdisc(local->mdev);
1116 /* add one default STA interface */
1117 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1118 IEEE80211_IF_TYPE_STA);
1120 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1121 wiphy_name(local->hw.wiphy));
1123 local->reg_state = IEEE80211_DEV_REGISTERED;
1126 ieee80211_led_init(local);
1131 rate_control_deinitialize(local);
1133 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1134 unregister_netdevice(local->mdev);
1137 sta_info_stop(local);
1139 debugfs_hw_del(local);
1140 destroy_workqueue(local->hw.workqueue);
1142 wiphy_unregister(local->hw.wiphy);
1145 EXPORT_SYMBOL(ieee80211_register_hw);
1147 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1148 struct ieee80211_hw_mode *mode)
1150 struct ieee80211_local *local = hw_to_local(hw);
1151 struct ieee80211_rate *rate;
1154 INIT_LIST_HEAD(&mode->list);
1155 list_add_tail(&mode->list, &local->modes_list);
1157 local->hw_modes |= (1 << mode->mode);
1158 for (i = 0; i < mode->num_rates; i++) {
1159 rate = &(mode->rates[i]);
1160 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1162 ieee80211_prepare_rates(local, mode);
1164 if (!local->oper_hw_mode) {
1165 /* Default to this mode */
1166 local->hw.conf.phymode = mode->mode;
1167 local->oper_hw_mode = local->scan_hw_mode = mode;
1168 local->oper_channel = local->scan_channel = &mode->channels[0];
1169 local->hw.conf.mode = local->oper_hw_mode;
1170 local->hw.conf.chan = local->oper_channel;
1173 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1174 ieee80211_set_default_regdomain(mode);
1178 EXPORT_SYMBOL(ieee80211_register_hwmode);
1180 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1182 struct ieee80211_local *local = hw_to_local(hw);
1183 struct ieee80211_sub_if_data *sdata, *tmp;
1186 tasklet_kill(&local->tx_pending_tasklet);
1187 tasklet_kill(&local->tasklet);
1191 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1193 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1196 * At this point, interface list manipulations are fine
1197 * because the driver cannot be handing us frames any
1198 * more and the tasklet is killed.
1202 * First, we remove all non-master interfaces. Do this because they
1203 * may have bss pointer dependency on the master, and when we free
1204 * the master these would be freed as well, breaking our list
1205 * iteration completely.
1207 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1208 if (sdata->dev == local->mdev)
1210 list_del(&sdata->list);
1211 __ieee80211_if_del(local, sdata);
1214 /* then, finally, remove the master interface */
1215 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
1219 ieee80211_rx_bss_list_deinit(local->mdev);
1220 ieee80211_clear_tx_pending(local);
1221 sta_info_stop(local);
1222 rate_control_deinitialize(local);
1223 debugfs_hw_del(local);
1225 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1226 kfree(local->supp_rates[i]);
1227 kfree(local->basic_rates[i]);
1230 if (skb_queue_len(&local->skb_queue)
1231 || skb_queue_len(&local->skb_queue_unreliable))
1232 printk(KERN_WARNING "%s: skb_queue not empty\n",
1233 wiphy_name(local->hw.wiphy));
1234 skb_queue_purge(&local->skb_queue);
1235 skb_queue_purge(&local->skb_queue_unreliable);
1237 destroy_workqueue(local->hw.workqueue);
1238 wiphy_unregister(local->hw.wiphy);
1239 ieee80211_wep_free(local);
1240 ieee80211_led_exit(local);
1242 EXPORT_SYMBOL(ieee80211_unregister_hw);
1244 void ieee80211_free_hw(struct ieee80211_hw *hw)
1246 struct ieee80211_local *local = hw_to_local(hw);
1248 ieee80211_if_free(local->mdev);
1249 wiphy_free(local->hw.wiphy);
1251 EXPORT_SYMBOL(ieee80211_free_hw);
1253 static int __init ieee80211_init(void)
1255 struct sk_buff *skb;
1258 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1260 #ifdef CONFIG_MAC80211_RCSIMPLE
1261 ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1266 ret = ieee80211_wme_register();
1268 #ifdef CONFIG_MAC80211_RCSIMPLE
1269 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1271 printk(KERN_DEBUG "ieee80211_init: failed to "
1272 "initialize WME (err=%d)\n", ret);
1276 ieee80211_debugfs_netdev_init();
1277 ieee80211_regdomain_init();
1282 static void __exit ieee80211_exit(void)
1284 #ifdef CONFIG_MAC80211_RCSIMPLE
1285 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1288 ieee80211_wme_unregister();
1289 ieee80211_debugfs_netdev_exit();
1293 subsys_initcall(ieee80211_init);
1294 module_exit(ieee80211_exit);
1296 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1297 MODULE_LICENSE("GPL");