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 * - periodic Beacon transmission in AP mode
13 * - IBSS mode simulation (Beacon transmission with competion for "air time")
14 * - IEEE 802.11a and 802.11n modes
17 #include <net/mac80211.h>
19 MODULE_AUTHOR("Jouni Malinen");
20 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
21 MODULE_LICENSE("GPL");
23 static int radios = 2;
24 module_param(radios, int, 0444);
25 MODULE_PARM_DESC(radios, "Number of simulated radios");
28 static struct class *hwsim_class;
30 static struct ieee80211_hw **hwsim_radios;
31 static int hwsim_radio_count;
34 static const struct ieee80211_channel hwsim_channels[] = {
35 { .chan = 1, .freq = 2412, .val = 1 },
36 { .chan = 2, .freq = 2417, .val = 2 },
37 { .chan = 3, .freq = 2422, .val = 3 },
38 { .chan = 4, .freq = 2427, .val = 4 },
39 { .chan = 5, .freq = 2432, .val = 5 },
40 { .chan = 6, .freq = 2437, .val = 6 },
41 { .chan = 7, .freq = 2442, .val = 7 },
42 { .chan = 8, .freq = 2447, .val = 8 },
43 { .chan = 9, .freq = 2452, .val = 9 },
44 { .chan = 10, .freq = 2457, .val = 10 },
45 { .chan = 11, .freq = 2462, .val = 11 },
46 { .chan = 12, .freq = 2467, .val = 12 },
47 { .chan = 13, .freq = 2472, .val = 13 },
48 { .chan = 14, .freq = 2484, .val = 14 },
51 static const struct ieee80211_rate hwsim_rates[] = {
52 { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
53 { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
54 { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
55 { .rate = 110, .val = 110, .val2 = 111,
56 .flags = IEEE80211_RATE_CCK_2 },
57 { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
58 { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
59 { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
60 { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
61 { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
62 { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
63 { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
64 { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
67 struct mac80211_hwsim_data {
69 struct ieee80211_hw_mode modes[1];
70 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
71 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
75 enum ieee80211_phymode phymode;
78 unsigned int rx_filter;
82 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
83 struct ieee80211_tx_control *control)
85 struct mac80211_hwsim_data *data = hw->priv;
86 struct ieee80211_tx_status tx_status;
87 struct ieee80211_rx_status rx_status;
90 if (!data->radio_enabled) {
91 printk(KERN_DEBUG "%s: dropped TX frame since radio "
92 "disabled\n", wiphy_name(hw->wiphy));
97 memset(&rx_status, 0, sizeof(rx_status));
98 /* TODO: set mactime */
99 rx_status.freq = data->freq;
100 rx_status.channel = data->channel;
101 rx_status.phymode = data->phymode;
102 rx_status.rate = control->tx_rate;
103 /* TODO: simulate signal strength (and optional packet drop) */
105 /* Copy skb to all enabled radios that are on the current frequency */
106 for (i = 0; i < hwsim_radio_count; i++) {
107 struct mac80211_hwsim_data *data2;
108 struct sk_buff *nskb;
110 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
112 data2 = hwsim_radios[i]->priv;
113 if (!data2->radio_enabled || data->freq != data2->freq)
116 nskb = skb_copy(skb, GFP_KERNEL);
120 ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
123 memset(&tx_status, 0, sizeof(tx_status));
124 memcpy(&tx_status.control, control, sizeof(*control));
125 /* TODO: proper ACK determination */
126 tx_status.flags = IEEE80211_TX_STATUS_ACK;
127 ieee80211_tx_status(hw, skb, &tx_status);
132 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
134 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
139 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
141 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
145 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
146 struct ieee80211_if_init_conf *conf)
148 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
153 static void mac80211_hwsim_remove_interface(
154 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
156 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
160 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
161 struct ieee80211_conf *conf)
163 struct mac80211_hwsim_data *data = hw->priv;
165 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
166 wiphy_name(hw->wiphy), __func__,
167 conf->freq, conf->radio_enabled, conf->beacon_int);
169 data->freq = conf->freq;
170 data->channel = conf->channel;
171 data->phymode = conf->phymode;
172 data->radio_enabled = conf->radio_enabled;
173 data->beacon_int = conf->beacon_int;
179 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
180 unsigned int changed_flags,
181 unsigned int *total_flags,
183 struct dev_addr_list *mc_list)
185 struct mac80211_hwsim_data *data = hw->priv;
187 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
190 if (*total_flags & FIF_PROMISC_IN_BSS)
191 data->rx_filter |= FIF_PROMISC_IN_BSS;
192 if (*total_flags & FIF_ALLMULTI)
193 data->rx_filter |= FIF_ALLMULTI;
195 *total_flags = data->rx_filter;
200 static const struct ieee80211_ops mac80211_hwsim_ops =
202 .tx = mac80211_hwsim_tx,
203 .start = mac80211_hwsim_start,
204 .stop = mac80211_hwsim_stop,
205 .add_interface = mac80211_hwsim_add_interface,
206 .remove_interface = mac80211_hwsim_remove_interface,
207 .config = mac80211_hwsim_config,
208 .configure_filter = mac80211_hwsim_configure_filter,
212 static void mac80211_hwsim_free(void)
216 for (i = 0; i < hwsim_radio_count; i++) {
217 if (hwsim_radios[i]) {
218 struct mac80211_hwsim_data *data;
219 data = hwsim_radios[i]->priv;
220 ieee80211_unregister_hw(hwsim_radios[i]);
221 if (!IS_ERR(data->dev))
222 device_unregister(data->dev);
223 ieee80211_free_hw(hwsim_radios[i]);
227 class_destroy(hwsim_class);
231 static struct device_driver mac80211_hwsim_driver = {
232 .name = "mac80211_hwsim"
236 static int __init init_mac80211_hwsim(void)
240 struct mac80211_hwsim_data *data;
241 struct ieee80211_hw *hw;
242 DECLARE_MAC_BUF(mac);
244 if (radios < 1 || radios > 65535)
247 hwsim_radio_count = radios;
248 hwsim_radios = kcalloc(hwsim_radio_count,
249 sizeof(struct ieee80211_hw *), GFP_KERNEL);
250 if (hwsim_radios == NULL)
253 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
254 if (IS_ERR(hwsim_class)) {
256 return PTR_ERR(hwsim_class);
259 memset(addr, 0, ETH_ALEN);
262 for (i = 0; i < hwsim_radio_count; i++) {
263 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
265 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
267 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
272 hwsim_radios[i] = hw;
275 data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
276 if (IS_ERR(data->dev)) {
277 printk(KERN_DEBUG "mac80211_hwsim: device_create "
278 "failed (%ld)\n", PTR_ERR(data->dev));
282 data->dev->driver = &mac80211_hwsim_driver;
283 dev_set_drvdata(data->dev, hw);
285 SET_IEEE80211_DEV(hw, data->dev);
288 SET_IEEE80211_PERM_ADDR(hw, addr);
290 hw->channel_change_time = 1;
293 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
294 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
295 data->modes[0].channels = data->channels;
296 data->modes[0].rates = data->rates;
297 data->modes[0].mode = MODE_IEEE80211G;
298 data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
299 data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
301 err = ieee80211_register_hwmode(hw, data->modes);
303 printk(KERN_DEBUG "mac80211_hwsim: "
304 "ieee80211_register_hwmode failed (%d)\n", err);
308 err = ieee80211_register_hw(hw);
310 printk(KERN_DEBUG "mac80211_hwsim: "
311 "ieee80211_register_hw failed (%d)\n", err);
315 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
316 wiphy_name(hw->wiphy),
317 print_mac(mac, hw->wiphy->perm_addr));
323 mac80211_hwsim_free();
328 static void __exit exit_mac80211_hwsim(void)
330 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
333 mac80211_hwsim_free();
337 module_init(init_mac80211_hwsim);
338 module_exit(exit_mac80211_hwsim);