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 static struct class *hwsim_class;
57 static struct ieee80211_hw **hwsim_radios;
58 static int hwsim_radio_count;
59 static struct net_device *hwsim_mon; /* global monitor netdev */
62 static const struct ieee80211_channel hwsim_channels[] = {
63 { .center_freq = 2412 },
64 { .center_freq = 2417 },
65 { .center_freq = 2422 },
66 { .center_freq = 2427 },
67 { .center_freq = 2432 },
68 { .center_freq = 2437 },
69 { .center_freq = 2442 },
70 { .center_freq = 2447 },
71 { .center_freq = 2452 },
72 { .center_freq = 2457 },
73 { .center_freq = 2462 },
74 { .center_freq = 2467 },
75 { .center_freq = 2472 },
76 { .center_freq = 2484 },
79 static const struct ieee80211_rate hwsim_rates[] = {
81 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
82 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
83 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
94 struct mac80211_hwsim_data {
96 struct ieee80211_supported_band band;
97 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
98 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
100 struct ieee80211_channel *channel;
102 unsigned long beacon_int; /* in jiffies unit */
103 unsigned int rx_filter;
105 struct timer_list beacon_timer;
109 struct hwsim_radiotap_hdr {
110 struct ieee80211_radiotap_header hdr;
115 } __attribute__ ((packed));
118 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
120 /* TODO: allow packet injection */
126 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
127 struct sk_buff *tx_skb)
129 struct mac80211_hwsim_data *data = hw->priv;
131 struct hwsim_radiotap_hdr *hdr;
133 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
134 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
136 if (!netif_running(hwsim_mon))
139 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
143 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
144 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
146 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
147 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
148 (1 << IEEE80211_RADIOTAP_RATE) |
149 (1 << IEEE80211_RADIOTAP_CHANNEL));
151 hdr->rt_rate = txrate->bitrate / 5;
152 hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
153 flags = IEEE80211_CHAN_2GHZ;
154 if (txrate->flags & IEEE80211_RATE_ERP_G)
155 flags |= IEEE80211_CHAN_OFDM;
157 flags |= IEEE80211_CHAN_CCK;
158 hdr->rt_chbitmask = cpu_to_le16(flags);
160 skb->dev = hwsim_mon;
161 skb_set_mac_header(skb, 0);
162 skb->ip_summed = CHECKSUM_UNNECESSARY;
163 skb->pkt_type = PACKET_OTHERHOST;
164 skb->protocol = htons(ETH_P_802_2);
165 memset(skb->cb, 0, sizeof(skb->cb));
170 static int mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
173 struct mac80211_hwsim_data *data = hw->priv;
175 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
176 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
177 struct ieee80211_rx_status rx_status;
179 memset(&rx_status, 0, sizeof(rx_status));
180 /* TODO: set mactime */
181 rx_status.freq = data->channel->center_freq;
182 rx_status.band = data->channel->band;
183 rx_status.rate_idx = info->tx_rate_idx;
184 /* TODO: simulate signal strength (and optional packet drop) */
186 /* Copy skb to all enabled radios that are on the current frequency */
187 for (i = 0; i < hwsim_radio_count; i++) {
188 struct mac80211_hwsim_data *data2;
189 struct sk_buff *nskb;
191 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
193 data2 = hwsim_radios[i]->priv;
194 if (!data2->started || !data2->radio_enabled ||
195 data->channel->center_freq != data2->channel->center_freq)
198 nskb = skb_copy(skb, GFP_ATOMIC);
202 if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
205 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
212 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
214 struct mac80211_hwsim_data *data = hw->priv;
216 struct ieee80211_tx_info *txi;
218 mac80211_hwsim_monitor_rx(hw, skb);
221 /* Should not happen; just a sanity check for addr1 use */
226 if (!data->radio_enabled) {
227 printk(KERN_DEBUG "%s: dropped TX frame since radio "
228 "disabled\n", wiphy_name(hw->wiphy));
233 ack = mac80211_hwsim_tx_frame(hw, skb);
235 txi = IEEE80211_SKB_CB(skb);
237 hwsim_check_magic(txi->control.vif);
239 memset(&txi->status, 0, sizeof(txi->status));
240 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
242 txi->flags |= IEEE80211_TX_STAT_ACK;
244 txi->status.excessive_retries = 1;
246 ieee80211_tx_status_irqsafe(hw, skb);
251 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
253 struct mac80211_hwsim_data *data = hw->priv;
254 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
260 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
262 struct mac80211_hwsim_data *data = hw->priv;
264 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
268 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
269 struct ieee80211_if_init_conf *conf)
271 DECLARE_MAC_BUF(mac);
272 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
273 wiphy_name(hw->wiphy), __func__, conf->type,
274 print_mac(mac, conf->mac_addr));
275 hwsim_set_magic(conf->vif);
280 static void mac80211_hwsim_remove_interface(
281 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
283 DECLARE_MAC_BUF(mac);
284 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
285 wiphy_name(hw->wiphy), __func__, conf->type,
286 print_mac(mac, conf->mac_addr));
287 hwsim_check_magic(conf->vif);
288 hwsim_clear_magic(conf->vif);
292 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
293 struct ieee80211_vif *vif)
295 struct ieee80211_hw *hw = arg;
297 struct ieee80211_tx_info *info;
299 hwsim_check_magic(vif);
301 if (vif->type != NL80211_IFTYPE_AP)
304 skb = ieee80211_beacon_get(hw, vif);
307 info = IEEE80211_SKB_CB(skb);
309 mac80211_hwsim_monitor_rx(hw, skb);
310 mac80211_hwsim_tx_frame(hw, skb);
315 static void mac80211_hwsim_beacon(unsigned long arg)
317 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
318 struct mac80211_hwsim_data *data = hw->priv;
320 if (!data->started || !data->radio_enabled)
323 ieee80211_iterate_active_interfaces_atomic(
324 hw, mac80211_hwsim_beacon_tx, hw);
326 data->beacon_timer.expires = jiffies + data->beacon_int;
327 add_timer(&data->beacon_timer);
331 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
332 struct ieee80211_conf *conf)
334 struct mac80211_hwsim_data *data = hw->priv;
336 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
337 wiphy_name(hw->wiphy), __func__,
338 conf->channel->center_freq, conf->radio_enabled,
341 data->channel = conf->channel;
342 data->radio_enabled = conf->radio_enabled;
343 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
344 if (data->beacon_int < 1)
345 data->beacon_int = 1;
347 if (!data->started || !data->radio_enabled)
348 del_timer(&data->beacon_timer);
350 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
356 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
357 unsigned int changed_flags,
358 unsigned int *total_flags,
360 struct dev_addr_list *mc_list)
362 struct mac80211_hwsim_data *data = hw->priv;
364 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
367 if (*total_flags & FIF_PROMISC_IN_BSS)
368 data->rx_filter |= FIF_PROMISC_IN_BSS;
369 if (*total_flags & FIF_ALLMULTI)
370 data->rx_filter |= FIF_ALLMULTI;
372 *total_flags = data->rx_filter;
375 static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
376 struct ieee80211_vif *vif,
377 struct ieee80211_if_conf *conf)
379 hwsim_check_magic(vif);
383 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
384 struct ieee80211_vif *vif,
385 struct ieee80211_bss_conf *info,
388 hwsim_check_magic(vif);
391 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
392 struct ieee80211_vif *vif,
393 enum sta_notify_cmd cmd,
394 struct ieee80211_sta *sta)
396 hwsim_check_magic(vif);
399 static const struct ieee80211_ops mac80211_hwsim_ops =
401 .tx = mac80211_hwsim_tx,
402 .start = mac80211_hwsim_start,
403 .stop = mac80211_hwsim_stop,
404 .add_interface = mac80211_hwsim_add_interface,
405 .remove_interface = mac80211_hwsim_remove_interface,
406 .config = mac80211_hwsim_config,
407 .configure_filter = mac80211_hwsim_configure_filter,
408 .config_interface = mac80211_hwsim_config_interface,
409 .bss_info_changed = mac80211_hwsim_bss_info_changed,
410 .sta_notify = mac80211_hwsim_sta_notify,
414 static void mac80211_hwsim_free(void)
418 for (i = 0; i < hwsim_radio_count; i++) {
419 if (hwsim_radios[i]) {
420 struct mac80211_hwsim_data *data;
421 data = hwsim_radios[i]->priv;
422 ieee80211_unregister_hw(hwsim_radios[i]);
423 device_unregister(data->dev);
424 ieee80211_free_hw(hwsim_radios[i]);
428 class_destroy(hwsim_class);
432 static struct device_driver mac80211_hwsim_driver = {
433 .name = "mac80211_hwsim"
437 static void hwsim_mon_setup(struct net_device *dev)
439 dev->hard_start_xmit = hwsim_mon_xmit;
440 dev->destructor = free_netdev;
442 dev->tx_queue_len = 0;
443 dev->type = ARPHRD_IEEE80211_RADIOTAP;
444 memset(dev->dev_addr, 0, ETH_ALEN);
445 dev->dev_addr[0] = 0x12;
449 static int __init init_mac80211_hwsim(void)
453 struct mac80211_hwsim_data *data;
454 struct ieee80211_hw *hw;
455 DECLARE_MAC_BUF(mac);
457 if (radios < 1 || radios > 65535)
460 hwsim_radio_count = radios;
461 hwsim_radios = kcalloc(hwsim_radio_count,
462 sizeof(struct ieee80211_hw *), GFP_KERNEL);
463 if (hwsim_radios == NULL)
466 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
467 if (IS_ERR(hwsim_class)) {
469 return PTR_ERR(hwsim_class);
472 memset(addr, 0, ETH_ALEN);
475 for (i = 0; i < hwsim_radio_count; i++) {
476 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
478 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
480 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
485 hwsim_radios[i] = hw;
488 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
490 if (IS_ERR(data->dev)) {
492 "mac80211_hwsim: device_create_drvdata "
493 "failed (%ld)\n", PTR_ERR(data->dev));
497 data->dev->driver = &mac80211_hwsim_driver;
499 SET_IEEE80211_DEV(hw, data->dev);
502 SET_IEEE80211_PERM_ADDR(hw, addr);
504 hw->channel_change_time = 1;
506 hw->wiphy->interface_modes =
507 BIT(NL80211_IFTYPE_STATION) |
508 BIT(NL80211_IFTYPE_AP);
509 hw->ampdu_queues = 1;
511 /* ask mac80211 to reserve space for magic */
512 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
514 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
515 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
516 data->band.channels = data->channels;
517 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
518 data->band.bitrates = data->rates;
519 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
520 data->band.ht_info.ht_supported = 1;
521 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
522 IEEE80211_HT_CAP_GRN_FLD |
523 IEEE80211_HT_CAP_SGI_40 |
524 IEEE80211_HT_CAP_DSSSCCK40;
525 data->band.ht_info.ampdu_factor = 0x3;
526 data->band.ht_info.ampdu_density = 0x6;
527 memset(data->band.ht_info.supp_mcs_set, 0,
528 sizeof(data->band.ht_info.supp_mcs_set));
529 data->band.ht_info.supp_mcs_set[0] = 0xff;
530 data->band.ht_info.supp_mcs_set[1] = 0xff;
531 data->band.ht_info.supp_mcs_set[12] =
532 IEEE80211_HT_CAP_MCS_TX_DEFINED;
533 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
535 err = ieee80211_register_hw(hw);
537 printk(KERN_DEBUG "mac80211_hwsim: "
538 "ieee80211_register_hw failed (%d)\n", err);
542 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
543 wiphy_name(hw->wiphy),
544 print_mac(mac, hw->wiphy->perm_addr));
546 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
550 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
551 if (hwsim_mon == NULL)
556 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
561 err = register_netdevice(hwsim_mon);
571 free_netdev(hwsim_mon);
572 mac80211_hwsim_free();
576 device_unregister(data->dev);
578 ieee80211_free_hw(hw);
579 hwsim_radios[i] = NULL;
581 mac80211_hwsim_free();
586 static void __exit exit_mac80211_hwsim(void)
588 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
591 unregister_netdev(hwsim_mon);
592 mac80211_hwsim_free();
596 module_init(init_mac80211_hwsim);
597 module_exit(exit_mac80211_hwsim);