2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
12 * - IBSS mode simulation (Beacon transmission with competition for "air time")
13 * - IEEE 802.11a and 802.11n modes
14 * - RX filtering based on filter configuration (data->rx_filter)
17 #include <net/mac80211.h>
18 #include <net/ieee80211_radiotap.h>
19 #include <linux/if_arp.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/etherdevice.h>
23 MODULE_AUTHOR("Jouni Malinen");
24 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
25 MODULE_LICENSE("GPL");
27 static int radios = 2;
28 module_param(radios, int, 0444);
29 MODULE_PARM_DESC(radios, "Number of simulated radios");
31 struct hwsim_vif_priv {
35 #define HWSIM_VIF_MAGIC 0x69537748
37 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
39 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
40 WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
43 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
45 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
46 vp->magic = HWSIM_VIF_MAGIC;
49 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
51 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
55 struct hwsim_sta_priv {
59 #define HWSIM_STA_MAGIC 0x6d537748
61 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
63 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
64 WARN_ON(sp->magic != HWSIM_VIF_MAGIC);
67 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
69 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
70 sp->magic = HWSIM_VIF_MAGIC;
73 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
75 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
79 static struct class *hwsim_class;
81 static struct ieee80211_hw **hwsim_radios;
82 static int hwsim_radio_count;
83 static struct net_device *hwsim_mon; /* global monitor netdev */
86 static const struct ieee80211_channel hwsim_channels[] = {
87 { .center_freq = 2412 },
88 { .center_freq = 2417 },
89 { .center_freq = 2422 },
90 { .center_freq = 2427 },
91 { .center_freq = 2432 },
92 { .center_freq = 2437 },
93 { .center_freq = 2442 },
94 { .center_freq = 2447 },
95 { .center_freq = 2452 },
96 { .center_freq = 2457 },
97 { .center_freq = 2462 },
98 { .center_freq = 2467 },
99 { .center_freq = 2472 },
100 { .center_freq = 2484 },
103 static const struct ieee80211_rate hwsim_rates[] = {
105 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
106 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
107 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
118 struct mac80211_hwsim_data {
120 struct ieee80211_supported_band band;
121 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
122 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
124 struct ieee80211_channel *channel;
126 unsigned long beacon_int; /* in jiffies unit */
127 unsigned int rx_filter;
129 struct timer_list beacon_timer;
133 struct hwsim_radiotap_hdr {
134 struct ieee80211_radiotap_header hdr;
139 } __attribute__ ((packed));
142 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
144 /* TODO: allow packet injection */
150 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
151 struct sk_buff *tx_skb)
153 struct mac80211_hwsim_data *data = hw->priv;
155 struct hwsim_radiotap_hdr *hdr;
157 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
158 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
160 if (!netif_running(hwsim_mon))
163 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
167 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
168 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
170 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
171 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
172 (1 << IEEE80211_RADIOTAP_RATE) |
173 (1 << IEEE80211_RADIOTAP_CHANNEL));
175 hdr->rt_rate = txrate->bitrate / 5;
176 hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
177 flags = IEEE80211_CHAN_2GHZ;
178 if (txrate->flags & IEEE80211_RATE_ERP_G)
179 flags |= IEEE80211_CHAN_OFDM;
181 flags |= IEEE80211_CHAN_CCK;
182 hdr->rt_chbitmask = cpu_to_le16(flags);
184 skb->dev = hwsim_mon;
185 skb_set_mac_header(skb, 0);
186 skb->ip_summed = CHECKSUM_UNNECESSARY;
187 skb->pkt_type = PACKET_OTHERHOST;
188 skb->protocol = htons(ETH_P_802_2);
189 memset(skb->cb, 0, sizeof(skb->cb));
194 static int mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
197 struct mac80211_hwsim_data *data = hw->priv;
199 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
200 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
201 struct ieee80211_rx_status rx_status;
203 memset(&rx_status, 0, sizeof(rx_status));
204 /* TODO: set mactime */
205 rx_status.freq = data->channel->center_freq;
206 rx_status.band = data->channel->band;
207 rx_status.rate_idx = info->tx_rate_idx;
208 /* TODO: simulate signal strength (and optional packet drop) */
210 /* Copy skb to all enabled radios that are on the current frequency */
211 for (i = 0; i < hwsim_radio_count; i++) {
212 struct mac80211_hwsim_data *data2;
213 struct sk_buff *nskb;
215 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
217 data2 = hwsim_radios[i]->priv;
218 if (!data2->started || !data2->radio_enabled ||
219 data->channel->center_freq != data2->channel->center_freq)
222 nskb = skb_copy(skb, GFP_ATOMIC);
226 if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
229 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
236 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
238 struct mac80211_hwsim_data *data = hw->priv;
240 struct ieee80211_tx_info *txi;
242 mac80211_hwsim_monitor_rx(hw, skb);
245 /* Should not happen; just a sanity check for addr1 use */
250 if (!data->radio_enabled) {
251 printk(KERN_DEBUG "%s: dropped TX frame since radio "
252 "disabled\n", wiphy_name(hw->wiphy));
257 ack = mac80211_hwsim_tx_frame(hw, skb);
259 txi = IEEE80211_SKB_CB(skb);
261 hwsim_check_magic(txi->control.vif);
262 if (txi->control.sta)
263 hwsim_check_sta_magic(txi->control.sta);
265 memset(&txi->status, 0, sizeof(txi->status));
266 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
268 txi->flags |= IEEE80211_TX_STAT_ACK;
270 txi->status.excessive_retries = 1;
272 ieee80211_tx_status_irqsafe(hw, skb);
277 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
279 struct mac80211_hwsim_data *data = hw->priv;
280 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
286 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
288 struct mac80211_hwsim_data *data = hw->priv;
290 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
294 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
295 struct ieee80211_if_init_conf *conf)
297 DECLARE_MAC_BUF(mac);
298 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
299 wiphy_name(hw->wiphy), __func__, conf->type,
300 print_mac(mac, conf->mac_addr));
301 hwsim_set_magic(conf->vif);
306 static void mac80211_hwsim_remove_interface(
307 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
309 DECLARE_MAC_BUF(mac);
310 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
311 wiphy_name(hw->wiphy), __func__, conf->type,
312 print_mac(mac, conf->mac_addr));
313 hwsim_check_magic(conf->vif);
314 hwsim_clear_magic(conf->vif);
318 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
319 struct ieee80211_vif *vif)
321 struct ieee80211_hw *hw = arg;
323 struct ieee80211_tx_info *info;
325 hwsim_check_magic(vif);
327 if (vif->type != NL80211_IFTYPE_AP)
330 skb = ieee80211_beacon_get(hw, vif);
333 info = IEEE80211_SKB_CB(skb);
335 mac80211_hwsim_monitor_rx(hw, skb);
336 mac80211_hwsim_tx_frame(hw, skb);
341 static void mac80211_hwsim_beacon(unsigned long arg)
343 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
344 struct mac80211_hwsim_data *data = hw->priv;
346 if (!data->started || !data->radio_enabled)
349 ieee80211_iterate_active_interfaces_atomic(
350 hw, mac80211_hwsim_beacon_tx, hw);
352 data->beacon_timer.expires = jiffies + data->beacon_int;
353 add_timer(&data->beacon_timer);
357 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
358 struct ieee80211_conf *conf)
360 struct mac80211_hwsim_data *data = hw->priv;
362 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
363 wiphy_name(hw->wiphy), __func__,
364 conf->channel->center_freq, conf->radio_enabled,
367 data->channel = conf->channel;
368 data->radio_enabled = conf->radio_enabled;
369 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
370 if (data->beacon_int < 1)
371 data->beacon_int = 1;
373 if (!data->started || !data->radio_enabled)
374 del_timer(&data->beacon_timer);
376 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
382 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
383 unsigned int changed_flags,
384 unsigned int *total_flags,
386 struct dev_addr_list *mc_list)
388 struct mac80211_hwsim_data *data = hw->priv;
390 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
393 if (*total_flags & FIF_PROMISC_IN_BSS)
394 data->rx_filter |= FIF_PROMISC_IN_BSS;
395 if (*total_flags & FIF_ALLMULTI)
396 data->rx_filter |= FIF_ALLMULTI;
398 *total_flags = data->rx_filter;
401 static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
402 struct ieee80211_vif *vif,
403 struct ieee80211_if_conf *conf)
405 hwsim_check_magic(vif);
409 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
410 struct ieee80211_vif *vif,
411 struct ieee80211_bss_conf *info,
414 hwsim_check_magic(vif);
417 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
418 struct ieee80211_vif *vif,
419 enum sta_notify_cmd cmd,
420 struct ieee80211_sta *sta)
422 hwsim_check_magic(vif);
425 hwsim_set_sta_magic(sta);
427 case STA_NOTIFY_REMOVE:
428 hwsim_clear_sta_magic(sta);
433 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
434 struct ieee80211_sta *sta,
437 hwsim_check_sta_magic(sta);
441 static const struct ieee80211_ops mac80211_hwsim_ops =
443 .tx = mac80211_hwsim_tx,
444 .start = mac80211_hwsim_start,
445 .stop = mac80211_hwsim_stop,
446 .add_interface = mac80211_hwsim_add_interface,
447 .remove_interface = mac80211_hwsim_remove_interface,
448 .config = mac80211_hwsim_config,
449 .configure_filter = mac80211_hwsim_configure_filter,
450 .config_interface = mac80211_hwsim_config_interface,
451 .bss_info_changed = mac80211_hwsim_bss_info_changed,
452 .sta_notify = mac80211_hwsim_sta_notify,
453 .set_tim = mac80211_hwsim_set_tim,
457 static void mac80211_hwsim_free(void)
461 for (i = 0; i < hwsim_radio_count; i++) {
462 if (hwsim_radios[i]) {
463 struct mac80211_hwsim_data *data;
464 data = hwsim_radios[i]->priv;
465 ieee80211_unregister_hw(hwsim_radios[i]);
466 device_unregister(data->dev);
467 ieee80211_free_hw(hwsim_radios[i]);
471 class_destroy(hwsim_class);
475 static struct device_driver mac80211_hwsim_driver = {
476 .name = "mac80211_hwsim"
480 static void hwsim_mon_setup(struct net_device *dev)
482 dev->hard_start_xmit = hwsim_mon_xmit;
483 dev->destructor = free_netdev;
485 dev->tx_queue_len = 0;
486 dev->type = ARPHRD_IEEE80211_RADIOTAP;
487 memset(dev->dev_addr, 0, ETH_ALEN);
488 dev->dev_addr[0] = 0x12;
492 static int __init init_mac80211_hwsim(void)
496 struct mac80211_hwsim_data *data;
497 struct ieee80211_hw *hw;
498 DECLARE_MAC_BUF(mac);
500 if (radios < 1 || radios > 65535)
503 hwsim_radio_count = radios;
504 hwsim_radios = kcalloc(hwsim_radio_count,
505 sizeof(struct ieee80211_hw *), GFP_KERNEL);
506 if (hwsim_radios == NULL)
509 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
510 if (IS_ERR(hwsim_class)) {
512 return PTR_ERR(hwsim_class);
515 memset(addr, 0, ETH_ALEN);
518 for (i = 0; i < hwsim_radio_count; i++) {
519 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
521 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
523 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
528 hwsim_radios[i] = hw;
531 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
533 if (IS_ERR(data->dev)) {
535 "mac80211_hwsim: device_create_drvdata "
536 "failed (%ld)\n", PTR_ERR(data->dev));
540 data->dev->driver = &mac80211_hwsim_driver;
542 SET_IEEE80211_DEV(hw, data->dev);
545 SET_IEEE80211_PERM_ADDR(hw, addr);
547 hw->channel_change_time = 1;
549 hw->wiphy->interface_modes =
550 BIT(NL80211_IFTYPE_STATION) |
551 BIT(NL80211_IFTYPE_AP);
552 hw->ampdu_queues = 1;
554 /* ask mac80211 to reserve space for magic */
555 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
556 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
558 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
559 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
560 data->band.channels = data->channels;
561 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
562 data->band.bitrates = data->rates;
563 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
564 data->band.ht_info.ht_supported = 1;
565 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
566 IEEE80211_HT_CAP_GRN_FLD |
567 IEEE80211_HT_CAP_SGI_40 |
568 IEEE80211_HT_CAP_DSSSCCK40;
569 data->band.ht_info.ampdu_factor = 0x3;
570 data->band.ht_info.ampdu_density = 0x6;
571 memset(data->band.ht_info.supp_mcs_set, 0,
572 sizeof(data->band.ht_info.supp_mcs_set));
573 data->band.ht_info.supp_mcs_set[0] = 0xff;
574 data->band.ht_info.supp_mcs_set[1] = 0xff;
575 data->band.ht_info.supp_mcs_set[12] =
576 IEEE80211_HT_CAP_MCS_TX_DEFINED;
577 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
579 err = ieee80211_register_hw(hw);
581 printk(KERN_DEBUG "mac80211_hwsim: "
582 "ieee80211_register_hw failed (%d)\n", err);
586 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
587 wiphy_name(hw->wiphy),
588 print_mac(mac, hw->wiphy->perm_addr));
590 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
594 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
595 if (hwsim_mon == NULL)
600 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
605 err = register_netdevice(hwsim_mon);
615 free_netdev(hwsim_mon);
616 mac80211_hwsim_free();
620 device_unregister(data->dev);
622 ieee80211_free_hw(hw);
623 hwsim_radios[i] = NULL;
625 mac80211_hwsim_free();
630 static void __exit exit_mac80211_hwsim(void)
632 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
635 unregister_netdev(hwsim_mon);
636 mac80211_hwsim_free();
640 module_init(init_mac80211_hwsim);
641 module_exit(exit_mac80211_hwsim);