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
16 #include <net/mac80211.h>
17 #include <net/ieee80211_radiotap.h>
18 #include <linux/if_arp.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/etherdevice.h>
22 MODULE_AUTHOR("Jouni Malinen");
23 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
24 MODULE_LICENSE("GPL");
26 static int radios = 2;
27 module_param(radios, int, 0444);
28 MODULE_PARM_DESC(radios, "Number of simulated radios");
31 static struct class *hwsim_class;
33 static struct ieee80211_hw **hwsim_radios;
34 static int hwsim_radio_count;
35 static struct net_device *hwsim_mon; /* global monitor netdev */
38 static const struct ieee80211_channel hwsim_channels[] = {
39 { .chan = 1, .freq = 2412, .val = 1 },
40 { .chan = 2, .freq = 2417, .val = 2 },
41 { .chan = 3, .freq = 2422, .val = 3 },
42 { .chan = 4, .freq = 2427, .val = 4 },
43 { .chan = 5, .freq = 2432, .val = 5 },
44 { .chan = 6, .freq = 2437, .val = 6 },
45 { .chan = 7, .freq = 2442, .val = 7 },
46 { .chan = 8, .freq = 2447, .val = 8 },
47 { .chan = 9, .freq = 2452, .val = 9 },
48 { .chan = 10, .freq = 2457, .val = 10 },
49 { .chan = 11, .freq = 2462, .val = 11 },
50 { .chan = 12, .freq = 2467, .val = 12 },
51 { .chan = 13, .freq = 2472, .val = 13 },
52 { .chan = 14, .freq = 2484, .val = 14 },
55 static const struct ieee80211_rate hwsim_rates[] = {
56 { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
57 { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
58 { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
59 { .rate = 110, .val = 110, .val2 = 111,
60 .flags = IEEE80211_RATE_CCK_2 },
61 { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
62 { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
63 { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
64 { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
65 { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
66 { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
67 { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
68 { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
71 struct mac80211_hwsim_data {
73 struct ieee80211_hw_mode modes[1];
74 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
75 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
79 enum ieee80211_phymode phymode;
81 unsigned long beacon_int; /* in jiffies unit */
82 unsigned int rx_filter;
84 struct timer_list beacon_timer;
88 struct hwsim_radiotap_hdr {
89 struct ieee80211_radiotap_header hdr;
94 } __attribute__ ((packed));
97 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
99 /* TODO: allow packet injection */
105 static void mac80211_hwsim_monitor_rx(struct mac80211_hwsim_data *data,
106 struct sk_buff *tx_skb,
107 struct ieee80211_tx_control *control)
110 struct hwsim_radiotap_hdr *hdr;
113 if (!netif_running(hwsim_mon))
116 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
120 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
121 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
123 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
124 hdr->hdr.it_present = __constant_cpu_to_le32(
125 (1 << IEEE80211_RADIOTAP_FLAGS) |
126 (1 << IEEE80211_RADIOTAP_RATE) |
127 (1 << IEEE80211_RADIOTAP_CHANNEL));
129 hdr->rt_rate = control->tx_rate / 5;
130 hdr->rt_channel = data->freq;
131 flags = IEEE80211_CHAN_2GHZ;
132 if (control->rate->flags & IEEE80211_RATE_OFDM)
133 flags |= IEEE80211_CHAN_OFDM;
134 if (control->rate->flags & IEEE80211_RATE_CCK)
135 flags |= IEEE80211_CHAN_CCK;
136 hdr->rt_chbitmask = cpu_to_le16(flags);
138 skb->dev = hwsim_mon;
139 skb_set_mac_header(skb, 0);
140 skb->ip_summed = CHECKSUM_UNNECESSARY;
141 skb->pkt_type = PACKET_OTHERHOST;
142 skb->protocol = __constant_htons(ETH_P_802_2);
143 memset(skb->cb, 0, sizeof(skb->cb));
148 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
149 struct ieee80211_tx_control *control)
151 struct mac80211_hwsim_data *data = hw->priv;
152 struct ieee80211_tx_status tx_status;
153 struct ieee80211_rx_status rx_status;
155 struct ieee80211_hdr *hdr;
157 mac80211_hwsim_monitor_rx(data, skb, control);
160 /* Should not happen; just a sanity check for addr1 use */
165 if (!data->radio_enabled) {
166 printk(KERN_DEBUG "%s: dropped TX frame since radio "
167 "disabled\n", wiphy_name(hw->wiphy));
172 hdr = (struct ieee80211_hdr *) skb->data;
173 if (is_multicast_ether_addr(hdr->addr1))
176 memset(&rx_status, 0, sizeof(rx_status));
177 /* TODO: set mactime */
178 rx_status.freq = data->freq;
179 rx_status.channel = data->channel;
180 rx_status.phymode = data->phymode;
181 rx_status.rate = control->tx_rate;
182 /* TODO: simulate signal strength (and optional packet drop) */
184 /* Copy skb to all enabled radios that are on the current frequency */
185 for (i = 0; i < hwsim_radio_count; i++) {
186 struct mac80211_hwsim_data *data2;
187 struct sk_buff *nskb;
189 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
191 data2 = hwsim_radios[i]->priv;
192 if (!data2->started || !data2->radio_enabled ||
193 data->freq != data2->freq)
196 nskb = skb_copy(skb, GFP_ATOMIC);
200 if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
203 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
206 memset(&tx_status, 0, sizeof(tx_status));
207 memcpy(&tx_status.control, control, sizeof(*control));
208 tx_status.flags = ack ? IEEE80211_TX_STATUS_ACK : 0;
209 ieee80211_tx_status_irqsafe(hw, skb, &tx_status);
214 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
216 struct mac80211_hwsim_data *data = hw->priv;
217 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
223 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
225 struct mac80211_hwsim_data *data = hw->priv;
227 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
231 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
232 struct ieee80211_if_init_conf *conf)
234 DECLARE_MAC_BUF(mac);
235 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
236 wiphy_name(hw->wiphy), __func__, conf->type,
237 print_mac(mac, conf->mac_addr));
242 static void mac80211_hwsim_remove_interface(
243 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
245 DECLARE_MAC_BUF(mac);
246 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
247 wiphy_name(hw->wiphy), __func__, conf->type,
248 print_mac(mac, conf->mac_addr));
252 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
253 struct ieee80211_vif *vif)
255 struct ieee80211_hw *hw = arg;
256 struct mac80211_hwsim_data *data = hw->priv;
257 struct ieee80211_tx_control control;
259 struct ieee80211_rx_status rx_status;
262 if (vif->type != IEEE80211_IF_TYPE_AP)
265 skb = ieee80211_beacon_get(hw, vif, &control);
269 mac80211_hwsim_monitor_rx(data, skb, &control);
271 memset(&rx_status, 0, sizeof(rx_status));
272 /* TODO: set mactime */
273 rx_status.freq = data->freq;
274 rx_status.channel = data->channel;
275 rx_status.phymode = data->phymode;
276 rx_status.rate = control.tx_rate;
277 /* TODO: simulate signal strength (and optional packet drop) */
279 /* Copy skb to all enabled radios that are on the current frequency */
280 for (i = 0; i < hwsim_radio_count; i++) {
281 struct mac80211_hwsim_data *data2;
282 struct sk_buff *nskb;
284 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
286 data2 = hwsim_radios[i]->priv;
287 if (!data2->started || !data2->radio_enabled ||
288 data->freq != data2->freq)
291 nskb = skb_copy(skb, GFP_ATOMIC);
295 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
302 static void mac80211_hwsim_beacon(unsigned long arg)
304 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
305 struct mac80211_hwsim_data *data = hw->priv;
307 if (!data->started || !data->radio_enabled)
310 ieee80211_iterate_active_interfaces(hw, mac80211_hwsim_beacon_tx, hw);
312 data->beacon_timer.expires = jiffies + data->beacon_int;
313 add_timer(&data->beacon_timer);
317 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
318 struct ieee80211_conf *conf)
320 struct mac80211_hwsim_data *data = hw->priv;
322 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
323 wiphy_name(hw->wiphy), __func__,
324 conf->freq, conf->radio_enabled, conf->beacon_int);
326 data->freq = conf->freq;
327 data->channel = conf->channel;
328 data->phymode = conf->phymode;
329 data->radio_enabled = conf->radio_enabled;
330 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
331 if (data->beacon_int < 1)
332 data->beacon_int = 1;
334 if (!data->started || !data->radio_enabled)
335 del_timer(&data->beacon_timer);
337 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
343 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
344 unsigned int changed_flags,
345 unsigned int *total_flags,
347 struct dev_addr_list *mc_list)
349 struct mac80211_hwsim_data *data = hw->priv;
351 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
354 if (*total_flags & FIF_PROMISC_IN_BSS)
355 data->rx_filter |= FIF_PROMISC_IN_BSS;
356 if (*total_flags & FIF_ALLMULTI)
357 data->rx_filter |= FIF_ALLMULTI;
359 *total_flags = data->rx_filter;
364 static const struct ieee80211_ops mac80211_hwsim_ops =
366 .tx = mac80211_hwsim_tx,
367 .start = mac80211_hwsim_start,
368 .stop = mac80211_hwsim_stop,
369 .add_interface = mac80211_hwsim_add_interface,
370 .remove_interface = mac80211_hwsim_remove_interface,
371 .config = mac80211_hwsim_config,
372 .configure_filter = mac80211_hwsim_configure_filter,
376 static void mac80211_hwsim_free(void)
380 for (i = 0; i < hwsim_radio_count; i++) {
381 if (hwsim_radios[i]) {
382 struct mac80211_hwsim_data *data;
383 data = hwsim_radios[i]->priv;
384 ieee80211_unregister_hw(hwsim_radios[i]);
385 if (!IS_ERR(data->dev))
386 device_unregister(data->dev);
387 ieee80211_free_hw(hwsim_radios[i]);
391 class_destroy(hwsim_class);
395 static struct device_driver mac80211_hwsim_driver = {
396 .name = "mac80211_hwsim"
400 static void hwsim_mon_setup(struct net_device *dev)
402 dev->hard_start_xmit = hwsim_mon_xmit;
403 dev->destructor = free_netdev;
405 dev->tx_queue_len = 0;
406 dev->type = ARPHRD_IEEE80211_RADIOTAP;
407 memset(dev->dev_addr, 0, ETH_ALEN);
408 dev->dev_addr[0] = 0x12;
412 static int __init init_mac80211_hwsim(void)
416 struct mac80211_hwsim_data *data;
417 struct ieee80211_hw *hw;
418 DECLARE_MAC_BUF(mac);
420 if (radios < 1 || radios > 65535)
423 hwsim_radio_count = radios;
424 hwsim_radios = kcalloc(hwsim_radio_count,
425 sizeof(struct ieee80211_hw *), GFP_KERNEL);
426 if (hwsim_radios == NULL)
429 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
430 if (IS_ERR(hwsim_class)) {
432 return PTR_ERR(hwsim_class);
435 memset(addr, 0, ETH_ALEN);
438 for (i = 0; i < hwsim_radio_count; i++) {
439 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
441 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
443 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
448 hwsim_radios[i] = hw;
451 data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
452 if (IS_ERR(data->dev)) {
453 printk(KERN_DEBUG "mac80211_hwsim: device_create "
454 "failed (%ld)\n", PTR_ERR(data->dev));
458 data->dev->driver = &mac80211_hwsim_driver;
459 dev_set_drvdata(data->dev, hw);
461 SET_IEEE80211_DEV(hw, data->dev);
464 SET_IEEE80211_PERM_ADDR(hw, addr);
466 hw->channel_change_time = 1;
469 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
470 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
471 data->modes[0].channels = data->channels;
472 data->modes[0].rates = data->rates;
473 data->modes[0].mode = MODE_IEEE80211G;
474 data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
475 data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
477 err = ieee80211_register_hwmode(hw, data->modes);
479 printk(KERN_DEBUG "mac80211_hwsim: "
480 "ieee80211_register_hwmode failed (%d)\n", err);
484 err = ieee80211_register_hw(hw);
486 printk(KERN_DEBUG "mac80211_hwsim: "
487 "ieee80211_register_hw failed (%d)\n", err);
491 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
492 wiphy_name(hw->wiphy),
493 print_mac(mac, hw->wiphy->perm_addr));
495 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
499 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
500 if (hwsim_mon == NULL)
505 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
510 err = register_netdevice(hwsim_mon);
520 free_netdev(hwsim_mon);
523 mac80211_hwsim_free();
528 static void __exit exit_mac80211_hwsim(void)
530 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
533 unregister_netdev(hwsim_mon);
534 mac80211_hwsim_free();
538 module_init(init_mac80211_hwsim);
539 module_exit(exit_mac80211_hwsim);