Added mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
authorJouni Malinen <j@w1.fi>
Tue, 10 Jun 2008 10:31:25 +0000 (13:31 +0300)
committerJouni Malinen <j@w1.fi>
Tue, 10 Jun 2008 10:31:25 +0000 (13:31 +0300)
mac80211_hwsim is a Linux kernel module that can be used to simulate
arbitrary number of IEEE 802.11 radios for mac80211 on a single
device. It can be used to test most of the mac80211 functionality and
user space tools (e.g., hostapd and wpa_supplicant) in a way that
matches very closely with the normal case of using real WLAN
hardware. From the mac80211 view point, mac80211_hwsim is yet another
hardware driver, i.e., no changes to mac80211 are needed to use this
testing tool.

mac80211_hwsim/Makefile [new file with mode: 0644]
mac80211_hwsim/README [new file with mode: 0644]
mac80211_hwsim/hostapd.conf [new file with mode: 0644]
mac80211_hwsim/mac80211_hwsim.c [new file with mode: 0644]
mac80211_hwsim/wpa_supplicant.conf [new file with mode: 0644]

diff --git a/mac80211_hwsim/Makefile b/mac80211_hwsim/Makefile
new file mode 100644 (file)
index 0000000..1728ad6
--- /dev/null
@@ -0,0 +1,12 @@
+# Determine path to the root directory of the Linux kernel source for the
+# currently running kernel. Version can be hardcoded with environment variable:
+# KVERS=2.6.25-foo make
+# Kernel path can be overrided with KERNEL_PATH argument to make:
+# make KERNEL_PATH=/usr/src/linux
+KVERS ?= $(shell uname -r)
+KERNEL_PATH ?= /lib/modules/$(KVERS)/build
+
+hwsim:
+       $(MAKE) -C $(KERNEL_PATH) SUBDIRS=$(PWD) modules
+
+obj-m += mac80211_hwsim.o
diff --git a/mac80211_hwsim/README b/mac80211_hwsim/README
new file mode 100644 (file)
index 0000000..ff6aaf0
--- /dev/null
@@ -0,0 +1,65 @@
+mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
+Copyright (c) 2008, Jouni Malinen <j@w1.fi>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+
+Introduction
+
+mac80211_hwsim is a Linux kernel module that can be used to simulate
+arbitrary number of IEEE 802.11 radios for mac80211 on a single
+device. It can be used to test most of the mac80211 functionality and
+user space tools (e.g., hostapd and wpa_supplicant) in a way that
+matches very closely with the normal case of using real WLAN
+hardware. From the mac80211 view point, mac80211_hwsim is yet another
+hardware driver, i.e., no changes to mac80211 are needed to use this
+testing tool.
+
+The main goal for mac80211_hwsim is to make it easier for developers
+to test their code and work with new features to mac80211, hostapd,
+and wpa_supplicant. The simulated radios do not have the limitations
+of real hardware, so it is easy to generate an arbitrary test setup
+and always reproduce the same setup for future tests. In addition,
+since all radio operation is simulated, any channel can be used in
+tests regardless of regulatory rules.
+
+mac80211_hwsim kernel module has a parameter 'radios' that can be used
+to select how many radios are simulates (default 2). This allows
+configuration of both very simply setups (e.g., just a single access
+point and a station) or large scale tests (multiple access points with
+hundreds of stations).
+
+mac80211_hwsim works by tracking the current channel of each virtual
+radio and copying all transmitted frames to all other radios that are
+currently enabled and on the same channel as the transmitting
+radio. Software encryption in mac80211 is used so that the frames are
+actually encrypted over the virtual air interface to allow more
+complete testing of encryption.
+
+
+Simple example
+
+This example shows how to use mac80211_hwsim to simulate two radios:
+one to act as an access point and the other as a station that
+associates with the AP. hostapd and wpa_supplicant are used to take
+care of WPA2-PSK authentication. In addition, hostapd is also
+processing access point side of association.
+
+Please note that the currently released Linux kernel (2.6.25) does not
+enable AP mode, so a simple patch is needed to enable AP mode selection:
+http://johannes.sipsolutions.net/patches/kernel/all/LATEST/006-allow-ap-vlan-modes.patch
+
+
+# Build mac80211_hwsim:
+make
+
+# Load the module
+insmod ./mac80211_hwsim.ko
+
+# Run hostapd (AP) for wlan0
+hostapd hostapd.conf
+
+# Run wpa_supplicant (station) for wlan1
+wpa_supplicant -Dwext -iwlan1 -c wpa_supplicant.conf
diff --git a/mac80211_hwsim/hostapd.conf b/mac80211_hwsim/hostapd.conf
new file mode 100644 (file)
index 0000000..08cde7e
--- /dev/null
@@ -0,0 +1,11 @@
+interface=wlan0
+driver=nl80211
+
+hw_mode=g
+channel=1
+ssid=mac80211 test
+
+wpa=2
+wpa_key_mgmt=WPA-PSK
+wpa_pairwise=CCMP
+wpa_passphrase=12345678
diff --git a/mac80211_hwsim/mac80211_hwsim.c b/mac80211_hwsim/mac80211_hwsim.c
new file mode 100644 (file)
index 0000000..35cf43b
--- /dev/null
@@ -0,0 +1,338 @@
+/*
+ * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
+ * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+/*
+ * TODO:
+ * - periodic Beacon transmission in AP mode
+ * - IBSS mode simulation (Beacon transmission with competion for "air time")
+ * - IEEE 802.11a and 802.11n modes
+ */
+
+#include <net/mac80211.h>
+
+MODULE_AUTHOR("Jouni Malinen");
+MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
+MODULE_LICENSE("GPL");
+
+static int radios = 2;
+module_param(radios, int, 0444);
+MODULE_PARM_DESC(radios, "Number of simulated radios");
+
+
+static struct class *hwsim_class;
+
+static struct ieee80211_hw **hwsim_radios;
+static int hwsim_radio_count;
+
+
+static const struct ieee80211_channel hwsim_channels[] = {
+       { .chan = 1, .freq = 2412, .val = 1 },
+       { .chan = 2, .freq = 2417, .val = 2 },
+       { .chan = 3, .freq = 2422, .val = 3 },
+       { .chan = 4, .freq = 2427, .val = 4 },
+       { .chan = 5, .freq = 2432, .val = 5 },
+       { .chan = 6, .freq = 2437, .val = 6 },
+       { .chan = 7, .freq = 2442, .val = 7 },
+       { .chan = 8, .freq = 2447, .val = 8 },
+       { .chan = 9, .freq = 2452, .val = 9 },
+       { .chan = 10, .freq = 2457, .val = 10 },
+       { .chan = 11, .freq = 2462, .val = 11 },
+       { .chan = 12, .freq = 2467, .val = 12 },
+       { .chan = 13, .freq = 2472, .val = 13 },
+       { .chan = 14, .freq = 2484, .val = 14 },
+};
+
+static const struct ieee80211_rate hwsim_rates[] = {
+       { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
+       { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
+       { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
+       { .rate = 110, .val = 110, .val2 = 111,
+         .flags = IEEE80211_RATE_CCK_2 },
+       { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
+       { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
+};
+
+struct mac80211_hwsim_data {
+       struct device *dev;
+       struct ieee80211_hw_mode modes[1];
+       struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
+       struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
+
+       int freq;
+       int channel;
+       enum ieee80211_phymode phymode;
+       int radio_enabled;
+       int beacon_int;
+       unsigned int rx_filter;
+};
+
+
+static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
+                            struct ieee80211_tx_control *control)
+{
+       struct mac80211_hwsim_data *data = hw->priv;
+       struct ieee80211_tx_status tx_status;
+       struct ieee80211_rx_status rx_status;
+       int i;
+
+       if (!data->radio_enabled) {
+               printk(KERN_DEBUG "%s: dropped TX frame since radio "
+                      "disabled\n", wiphy_name(hw->wiphy));
+               dev_kfree_skb(skb);
+               return NETDEV_TX_OK;
+       }
+
+       memset(&rx_status, 0, sizeof(rx_status));
+       /* TODO: set mactime */
+       rx_status.freq = data->freq;
+       rx_status.channel = data->channel;
+       rx_status.phymode = data->phymode;
+       rx_status.rate = control->tx_rate;
+       /* TODO: simulate signal strength (and optional packet drop) */
+
+       /* Copy skb to all enabled radios that are on the current frequency */
+       for (i = 0; i < hwsim_radio_count; i++) {
+               struct mac80211_hwsim_data *data2;
+               struct sk_buff *nskb;
+
+               if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
+                       continue;
+               data2 = hwsim_radios[i]->priv;
+               if (!data2->radio_enabled || data->freq != data2->freq)
+                       continue;
+
+               nskb = skb_copy(skb, GFP_KERNEL);
+               if (nskb == NULL)
+                       continue;
+
+               ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
+       }
+
+       memset(&tx_status, 0, sizeof(tx_status));
+       memcpy(&tx_status.control, control, sizeof(*control));
+       /* TODO: proper ACK determination */
+       tx_status.flags = IEEE80211_TX_STATUS_ACK;
+       ieee80211_tx_status(hw, skb, &tx_status);
+       return NETDEV_TX_OK;
+}
+
+
+static int mac80211_hwsim_start(struct ieee80211_hw *hw)
+{
+       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+       return 0;
+}
+
+
+static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
+{
+       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+}
+
+
+static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
+                                       struct ieee80211_if_init_conf *conf)
+{
+       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+       return 0;
+}
+
+
+static void mac80211_hwsim_remove_interface(
+       struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
+{
+       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+}
+
+
+static int mac80211_hwsim_config(struct ieee80211_hw *hw,
+                                struct ieee80211_conf *conf)
+{
+       struct mac80211_hwsim_data *data = hw->priv;
+
+       printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
+              wiphy_name(hw->wiphy), __func__,
+              conf->freq, conf->radio_enabled, conf->beacon_int);
+
+       data->freq = conf->freq;
+       data->channel = conf->channel;
+       data->phymode = conf->phymode;
+       data->radio_enabled = conf->radio_enabled;
+       data->beacon_int = conf->beacon_int;
+
+       return 0;
+}
+
+
+static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
+                                           unsigned int changed_flags,
+                                           unsigned int *total_flags,
+                                           int mc_count,
+                                           struct dev_addr_list *mc_list)
+{
+       struct mac80211_hwsim_data *data = hw->priv;
+
+       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+
+       data->rx_filter = 0;
+       if (*total_flags & FIF_PROMISC_IN_BSS)
+               data->rx_filter |= FIF_PROMISC_IN_BSS;
+       if (*total_flags & FIF_ALLMULTI)
+               data->rx_filter |= FIF_ALLMULTI;
+
+       *total_flags = data->rx_filter;
+}
+
+
+
+static const struct ieee80211_ops mac80211_hwsim_ops =
+{
+       .tx = mac80211_hwsim_tx,
+       .start = mac80211_hwsim_start,
+       .stop = mac80211_hwsim_stop,
+       .add_interface = mac80211_hwsim_add_interface,
+       .remove_interface = mac80211_hwsim_remove_interface,
+       .config = mac80211_hwsim_config,
+       .configure_filter = mac80211_hwsim_configure_filter,
+};
+
+
+static void mac80211_hwsim_free(void)
+{
+       int i;
+
+       for (i = 0; i < hwsim_radio_count; i++) {
+               if (hwsim_radios[i]) {
+                       struct mac80211_hwsim_data *data;
+                       data = hwsim_radios[i]->priv;
+                       ieee80211_unregister_hw(hwsim_radios[i]);
+                       if (!IS_ERR(data->dev))
+                               device_unregister(data->dev);
+                       ieee80211_free_hw(hwsim_radios[i]);
+               }
+       }
+       kfree(hwsim_radios);
+       class_destroy(hwsim_class);
+}
+
+
+static struct device_driver mac80211_hwsim_driver = {
+       .name = "mac80211_hwsim"
+};
+
+
+static int __init init_mac80211_hwsim(void)
+{
+       int i, err = 0;
+       u8 addr[ETH_ALEN];
+       struct mac80211_hwsim_data *data;
+       struct ieee80211_hw *hw;
+       DECLARE_MAC_BUF(mac);
+
+       if (radios < 1 || radios > 65535)
+               return -EINVAL;
+
+       hwsim_radio_count = radios;
+       hwsim_radios = kcalloc(hwsim_radio_count,
+                              sizeof(struct ieee80211_hw *), GFP_KERNEL);
+       if (hwsim_radios == NULL)
+               return -ENOMEM;
+
+       hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
+       if (IS_ERR(hwsim_class)) {
+               kfree(hwsim_radios);
+               return PTR_ERR(hwsim_class);
+       }
+
+       memset(addr, 0, ETH_ALEN);
+       addr[0] = 0x02;
+
+       for (i = 0; i < hwsim_radio_count; i++) {
+               printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
+                      i);
+               hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
+               if (hw == NULL) {
+                       printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
+                              "failed\n");
+                       err = -ENOMEM;
+                       goto failed;
+               }
+               hwsim_radios[i] = hw;
+
+               data = hw->priv;
+               data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
+               if (IS_ERR(data->dev)) {
+                       printk(KERN_DEBUG "mac80211_hwsim: device_create "
+                              "failed (%ld)\n", PTR_ERR(data->dev));
+                       err = -ENOMEM;
+                       goto failed;
+               }
+               data->dev->driver = &mac80211_hwsim_driver;
+               dev_set_drvdata(data->dev, hw);
+
+               SET_IEEE80211_DEV(hw, data->dev);
+               addr[3] = i >> 8;
+               addr[4] = i;
+               SET_IEEE80211_PERM_ADDR(hw, addr);
+
+               hw->channel_change_time = 1;
+               hw->queues = 1;
+
+               memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
+               memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
+               data->modes[0].channels = data->channels;
+               data->modes[0].rates = data->rates;
+               data->modes[0].mode = MODE_IEEE80211G;
+               data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
+               data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
+
+               err = ieee80211_register_hwmode(hw, data->modes);
+               if (err < 0) {
+                       printk(KERN_DEBUG "mac80211_hwsim: "
+                              "ieee80211_register_hwmode failed (%d)\n", err);
+                       goto failed;
+               }
+
+               err = ieee80211_register_hw(hw);
+               if (err < 0) {
+                       printk(KERN_DEBUG "mac80211_hwsim: "
+                              "ieee80211_register_hw failed (%d)\n", err);
+                       goto failed;
+               }
+
+               printk(KERN_DEBUG "%s: hwaddr %s registered\n",
+                      wiphy_name(hw->wiphy),
+                      print_mac(mac, hw->wiphy->perm_addr));
+       }
+
+       return 0;
+
+failed:
+       mac80211_hwsim_free();
+       return err;
+}
+
+
+static void __exit exit_mac80211_hwsim(void)
+{
+       printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
+              hwsim_radio_count);
+
+       mac80211_hwsim_free();
+}
+
+
+module_init(init_mac80211_hwsim);
+module_exit(exit_mac80211_hwsim);
diff --git a/mac80211_hwsim/wpa_supplicant.conf b/mac80211_hwsim/wpa_supplicant.conf
new file mode 100644 (file)
index 0000000..299128c
--- /dev/null
@@ -0,0 +1,10 @@
+ctrl_interface=/var/run/wpa_supplicant
+
+network={
+       ssid="mac80211 test"
+       psk="12345678"
+       key_mgmt=WPA-PSK
+       proto=WPA2
+       pairwise=CCMP
+       group=CCMP
+}