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 if (txi->control.vif)
262 hwsim_check_magic(txi->control.vif);
263 if (txi->control.sta)
264 hwsim_check_sta_magic(txi->control.sta);
266 memset(&txi->status, 0, sizeof(txi->status));
267 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
269 txi->flags |= IEEE80211_TX_STAT_ACK;
271 txi->status.excessive_retries = 1;
273 ieee80211_tx_status_irqsafe(hw, skb);
278 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
280 struct mac80211_hwsim_data *data = hw->priv;
281 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
287 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
289 struct mac80211_hwsim_data *data = hw->priv;
291 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
295 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
296 struct ieee80211_if_init_conf *conf)
298 DECLARE_MAC_BUF(mac);
299 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
300 wiphy_name(hw->wiphy), __func__, conf->type,
301 print_mac(mac, conf->mac_addr));
302 hwsim_set_magic(conf->vif);
307 static void mac80211_hwsim_remove_interface(
308 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
310 DECLARE_MAC_BUF(mac);
311 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
312 wiphy_name(hw->wiphy), __func__, conf->type,
313 print_mac(mac, conf->mac_addr));
314 hwsim_check_magic(conf->vif);
315 hwsim_clear_magic(conf->vif);
319 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
320 struct ieee80211_vif *vif)
322 struct ieee80211_hw *hw = arg;
324 struct ieee80211_tx_info *info;
326 hwsim_check_magic(vif);
328 if (vif->type != NL80211_IFTYPE_AP)
331 skb = ieee80211_beacon_get(hw, vif);
334 info = IEEE80211_SKB_CB(skb);
336 mac80211_hwsim_monitor_rx(hw, skb);
337 mac80211_hwsim_tx_frame(hw, skb);
342 static void mac80211_hwsim_beacon(unsigned long arg)
344 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
345 struct mac80211_hwsim_data *data = hw->priv;
347 if (!data->started || !data->radio_enabled)
350 ieee80211_iterate_active_interfaces_atomic(
351 hw, mac80211_hwsim_beacon_tx, hw);
353 data->beacon_timer.expires = jiffies + data->beacon_int;
354 add_timer(&data->beacon_timer);
358 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
359 struct ieee80211_conf *conf)
361 struct mac80211_hwsim_data *data = hw->priv;
363 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
364 wiphy_name(hw->wiphy), __func__,
365 conf->channel->center_freq, conf->radio_enabled,
368 data->channel = conf->channel;
369 data->radio_enabled = conf->radio_enabled;
370 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
371 if (data->beacon_int < 1)
372 data->beacon_int = 1;
374 if (!data->started || !data->radio_enabled)
375 del_timer(&data->beacon_timer);
377 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
383 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
384 unsigned int changed_flags,
385 unsigned int *total_flags,
387 struct dev_addr_list *mc_list)
389 struct mac80211_hwsim_data *data = hw->priv;
391 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
394 if (*total_flags & FIF_PROMISC_IN_BSS)
395 data->rx_filter |= FIF_PROMISC_IN_BSS;
396 if (*total_flags & FIF_ALLMULTI)
397 data->rx_filter |= FIF_ALLMULTI;
399 *total_flags = data->rx_filter;
402 static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
403 struct ieee80211_vif *vif,
404 struct ieee80211_if_conf *conf)
406 hwsim_check_magic(vif);
410 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
411 struct ieee80211_vif *vif,
412 struct ieee80211_bss_conf *info,
415 hwsim_check_magic(vif);
418 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
419 struct ieee80211_vif *vif,
420 enum sta_notify_cmd cmd,
421 struct ieee80211_sta *sta)
423 hwsim_check_magic(vif);
426 hwsim_set_sta_magic(sta);
428 case STA_NOTIFY_REMOVE:
429 hwsim_clear_sta_magic(sta);
434 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
435 struct ieee80211_sta *sta,
438 hwsim_check_sta_magic(sta);
442 static const struct ieee80211_ops mac80211_hwsim_ops =
444 .tx = mac80211_hwsim_tx,
445 .start = mac80211_hwsim_start,
446 .stop = mac80211_hwsim_stop,
447 .add_interface = mac80211_hwsim_add_interface,
448 .remove_interface = mac80211_hwsim_remove_interface,
449 .config = mac80211_hwsim_config,
450 .configure_filter = mac80211_hwsim_configure_filter,
451 .config_interface = mac80211_hwsim_config_interface,
452 .bss_info_changed = mac80211_hwsim_bss_info_changed,
453 .sta_notify = mac80211_hwsim_sta_notify,
454 .set_tim = mac80211_hwsim_set_tim,
458 static void mac80211_hwsim_free(void)
462 for (i = 0; i < hwsim_radio_count; i++) {
463 if (hwsim_radios[i]) {
464 struct mac80211_hwsim_data *data;
465 data = hwsim_radios[i]->priv;
466 ieee80211_unregister_hw(hwsim_radios[i]);
467 device_unregister(data->dev);
468 ieee80211_free_hw(hwsim_radios[i]);
472 class_destroy(hwsim_class);
476 static struct device_driver mac80211_hwsim_driver = {
477 .name = "mac80211_hwsim"
481 static void hwsim_mon_setup(struct net_device *dev)
483 dev->hard_start_xmit = hwsim_mon_xmit;
484 dev->destructor = free_netdev;
486 dev->tx_queue_len = 0;
487 dev->type = ARPHRD_IEEE80211_RADIOTAP;
488 memset(dev->dev_addr, 0, ETH_ALEN);
489 dev->dev_addr[0] = 0x12;
493 static int __init init_mac80211_hwsim(void)
497 struct mac80211_hwsim_data *data;
498 struct ieee80211_hw *hw;
499 DECLARE_MAC_BUF(mac);
501 if (radios < 1 || radios > 65535)
504 hwsim_radio_count = radios;
505 hwsim_radios = kcalloc(hwsim_radio_count,
506 sizeof(struct ieee80211_hw *), GFP_KERNEL);
507 if (hwsim_radios == NULL)
510 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
511 if (IS_ERR(hwsim_class)) {
513 return PTR_ERR(hwsim_class);
516 memset(addr, 0, ETH_ALEN);
519 for (i = 0; i < hwsim_radio_count; i++) {
520 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
522 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
524 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
529 hwsim_radios[i] = hw;
532 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
534 if (IS_ERR(data->dev)) {
536 "mac80211_hwsim: device_create_drvdata "
537 "failed (%ld)\n", PTR_ERR(data->dev));
541 data->dev->driver = &mac80211_hwsim_driver;
543 SET_IEEE80211_DEV(hw, data->dev);
546 SET_IEEE80211_PERM_ADDR(hw, addr);
548 hw->channel_change_time = 1;
550 hw->wiphy->interface_modes =
551 BIT(NL80211_IFTYPE_STATION) |
552 BIT(NL80211_IFTYPE_AP);
553 hw->ampdu_queues = 1;
555 /* ask mac80211 to reserve space for magic */
556 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
557 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
559 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
560 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
561 data->band.channels = data->channels;
562 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
563 data->band.bitrates = data->rates;
564 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
565 data->band.ht_info.ht_supported = 1;
566 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
567 IEEE80211_HT_CAP_GRN_FLD |
568 IEEE80211_HT_CAP_SGI_40 |
569 IEEE80211_HT_CAP_DSSSCCK40;
570 data->band.ht_info.ampdu_factor = 0x3;
571 data->band.ht_info.ampdu_density = 0x6;
572 memset(data->band.ht_info.supp_mcs_set, 0,
573 sizeof(data->band.ht_info.supp_mcs_set));
574 data->band.ht_info.supp_mcs_set[0] = 0xff;
575 data->band.ht_info.supp_mcs_set[1] = 0xff;
576 data->band.ht_info.supp_mcs_set[12] =
577 IEEE80211_HT_CAP_MCS_TX_DEFINED;
578 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
580 err = ieee80211_register_hw(hw);
582 printk(KERN_DEBUG "mac80211_hwsim: "
583 "ieee80211_register_hw failed (%d)\n", err);
587 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
588 wiphy_name(hw->wiphy),
589 print_mac(mac, hw->wiphy->perm_addr));
591 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
595 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
596 if (hwsim_mon == NULL)
601 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
606 err = register_netdevice(hwsim_mon);
616 free_netdev(hwsim_mon);
617 mac80211_hwsim_free();
621 device_unregister(data->dev);
623 ieee80211_free_hw(hw);
624 hwsim_radios[i] = NULL;
626 mac80211_hwsim_free();
631 static void __exit exit_mac80211_hwsim(void)
633 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
636 unregister_netdev(hwsim_mon);
637 mac80211_hwsim_free();
641 module_init(init_mac80211_hwsim);
642 module_exit(exit_mac80211_hwsim);