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 <linux/list.h>
18 #include <linux/spinlock.h>
19 #include <net/mac80211.h>
20 #include <net/ieee80211_radiotap.h>
21 #include <linux/if_arp.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/etherdevice.h>
25 MODULE_AUTHOR("Jouni Malinen");
26 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
27 MODULE_LICENSE("GPL");
29 static int radios
= 2;
30 module_param(radios
, int, 0444);
31 MODULE_PARM_DESC(radios
, "Number of simulated radios");
33 struct hwsim_vif_priv
{
37 #define HWSIM_VIF_MAGIC 0x69537748
39 static inline void hwsim_check_magic(struct ieee80211_vif
*vif
)
41 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
42 WARN_ON(vp
->magic
!= HWSIM_VIF_MAGIC
);
45 static inline void hwsim_set_magic(struct ieee80211_vif
*vif
)
47 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
48 vp
->magic
= HWSIM_VIF_MAGIC
;
51 static inline void hwsim_clear_magic(struct ieee80211_vif
*vif
)
53 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
57 struct hwsim_sta_priv
{
61 #define HWSIM_STA_MAGIC 0x6d537748
63 static inline void hwsim_check_sta_magic(struct ieee80211_sta
*sta
)
65 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
66 WARN_ON(sp
->magic
!= HWSIM_STA_MAGIC
);
69 static inline void hwsim_set_sta_magic(struct ieee80211_sta
*sta
)
71 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
72 sp
->magic
= HWSIM_STA_MAGIC
;
75 static inline void hwsim_clear_sta_magic(struct ieee80211_sta
*sta
)
77 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
81 static struct class *hwsim_class
;
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 static spinlock_t hwsim_radio_lock
;
119 static struct list_head hwsim_radios
;
121 struct mac80211_hwsim_data
{
122 struct list_head list
;
123 struct ieee80211_hw
*hw
;
125 struct ieee80211_supported_band band
;
126 struct ieee80211_channel channels
[ARRAY_SIZE(hwsim_channels
)];
127 struct ieee80211_rate rates
[ARRAY_SIZE(hwsim_rates
)];
129 struct ieee80211_channel
*channel
;
131 unsigned long beacon_int
; /* in jiffies unit */
132 unsigned int rx_filter
;
134 struct timer_list beacon_timer
;
138 struct hwsim_radiotap_hdr
{
139 struct ieee80211_radiotap_header hdr
;
144 } __attribute__ ((packed
));
147 static int hwsim_mon_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
149 /* TODO: allow packet injection */
155 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw
*hw
,
156 struct sk_buff
*tx_skb
)
158 struct mac80211_hwsim_data
*data
= hw
->priv
;
160 struct hwsim_radiotap_hdr
*hdr
;
162 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(tx_skb
);
163 struct ieee80211_rate
*txrate
= ieee80211_get_tx_rate(hw
, info
);
165 if (!netif_running(hwsim_mon
))
168 skb
= skb_copy_expand(tx_skb
, sizeof(*hdr
), 0, GFP_ATOMIC
);
172 hdr
= (struct hwsim_radiotap_hdr
*) skb_push(skb
, sizeof(*hdr
));
173 hdr
->hdr
.it_version
= PKTHDR_RADIOTAP_VERSION
;
175 hdr
->hdr
.it_len
= cpu_to_le16(sizeof(*hdr
));
176 hdr
->hdr
.it_present
= cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS
) |
177 (1 << IEEE80211_RADIOTAP_RATE
) |
178 (1 << IEEE80211_RADIOTAP_CHANNEL
));
180 hdr
->rt_rate
= txrate
->bitrate
/ 5;
181 hdr
->rt_channel
= cpu_to_le16(data
->channel
->center_freq
);
182 flags
= IEEE80211_CHAN_2GHZ
;
183 if (txrate
->flags
& IEEE80211_RATE_ERP_G
)
184 flags
|= IEEE80211_CHAN_OFDM
;
186 flags
|= IEEE80211_CHAN_CCK
;
187 hdr
->rt_chbitmask
= cpu_to_le16(flags
);
189 skb
->dev
= hwsim_mon
;
190 skb_set_mac_header(skb
, 0);
191 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
192 skb
->pkt_type
= PACKET_OTHERHOST
;
193 skb
->protocol
= htons(ETH_P_802_2
);
194 memset(skb
->cb
, 0, sizeof(skb
->cb
));
199 static bool mac80211_hwsim_tx_frame(struct ieee80211_hw
*hw
,
202 struct mac80211_hwsim_data
*data
= hw
->priv
, *data2
;
204 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
205 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
206 struct ieee80211_rx_status rx_status
;
208 memset(&rx_status
, 0, sizeof(rx_status
));
209 /* TODO: set mactime */
210 rx_status
.freq
= data
->channel
->center_freq
;
211 rx_status
.band
= data
->channel
->band
;
212 rx_status
.rate_idx
= info
->tx_rate_idx
;
213 /* TODO: simulate signal strength (and optional packet drop) */
215 /* Copy skb to all enabled radios that are on the current frequency */
216 spin_lock(&hwsim_radio_lock
);
217 list_for_each_entry(data2
, &hwsim_radios
, list
) {
218 struct sk_buff
*nskb
;
223 if (!data2
->started
|| !data2
->radio_enabled
||
224 data
->channel
->center_freq
!= data2
->channel
->center_freq
)
227 nskb
= skb_copy(skb
, GFP_ATOMIC
);
231 if (memcmp(hdr
->addr1
, data2
->hw
->wiphy
->perm_addr
,
234 ieee80211_rx_irqsafe(data2
->hw
, nskb
, &rx_status
);
236 spin_unlock(&hwsim_radio_lock
);
242 static int mac80211_hwsim_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
244 struct mac80211_hwsim_data
*data
= hw
->priv
;
246 struct ieee80211_tx_info
*txi
;
248 mac80211_hwsim_monitor_rx(hw
, skb
);
251 /* Should not happen; just a sanity check for addr1 use */
256 if (!data
->radio_enabled
) {
257 printk(KERN_DEBUG
"%s: dropped TX frame since radio "
258 "disabled\n", wiphy_name(hw
->wiphy
));
263 ack
= mac80211_hwsim_tx_frame(hw
, skb
);
265 txi
= IEEE80211_SKB_CB(skb
);
267 if (txi
->control
.vif
)
268 hwsim_check_magic(txi
->control
.vif
);
269 if (txi
->control
.sta
)
270 hwsim_check_sta_magic(txi
->control
.sta
);
272 memset(&txi
->status
, 0, sizeof(txi
->status
));
273 if (!(txi
->flags
& IEEE80211_TX_CTL_NO_ACK
)) {
275 txi
->flags
|= IEEE80211_TX_STAT_ACK
;
277 txi
->status
.excessive_retries
= 1;
279 ieee80211_tx_status_irqsafe(hw
, skb
);
284 static int mac80211_hwsim_start(struct ieee80211_hw
*hw
)
286 struct mac80211_hwsim_data
*data
= hw
->priv
;
287 printk(KERN_DEBUG
"%s:%s\n", wiphy_name(hw
->wiphy
), __func__
);
293 static void mac80211_hwsim_stop(struct ieee80211_hw
*hw
)
295 struct mac80211_hwsim_data
*data
= hw
->priv
;
297 printk(KERN_DEBUG
"%s:%s\n", wiphy_name(hw
->wiphy
), __func__
);
301 static int mac80211_hwsim_add_interface(struct ieee80211_hw
*hw
,
302 struct ieee80211_if_init_conf
*conf
)
304 printk(KERN_DEBUG
"%s:%s (type=%d mac_addr=%pM)\n",
305 wiphy_name(hw
->wiphy
), __func__
, conf
->type
,
307 hwsim_set_magic(conf
->vif
);
312 static void mac80211_hwsim_remove_interface(
313 struct ieee80211_hw
*hw
, struct ieee80211_if_init_conf
*conf
)
315 printk(KERN_DEBUG
"%s:%s (type=%d mac_addr=%pM)\n",
316 wiphy_name(hw
->wiphy
), __func__
, conf
->type
,
318 hwsim_check_magic(conf
->vif
);
319 hwsim_clear_magic(conf
->vif
);
323 static void mac80211_hwsim_beacon_tx(void *arg
, u8
*mac
,
324 struct ieee80211_vif
*vif
)
326 struct ieee80211_hw
*hw
= arg
;
328 struct ieee80211_tx_info
*info
;
330 hwsim_check_magic(vif
);
332 if (vif
->type
!= NL80211_IFTYPE_AP
)
335 skb
= ieee80211_beacon_get(hw
, vif
);
338 info
= IEEE80211_SKB_CB(skb
);
340 mac80211_hwsim_monitor_rx(hw
, skb
);
341 mac80211_hwsim_tx_frame(hw
, skb
);
346 static void mac80211_hwsim_beacon(unsigned long arg
)
348 struct ieee80211_hw
*hw
= (struct ieee80211_hw
*) arg
;
349 struct mac80211_hwsim_data
*data
= hw
->priv
;
351 if (!data
->started
|| !data
->radio_enabled
)
354 ieee80211_iterate_active_interfaces_atomic(
355 hw
, mac80211_hwsim_beacon_tx
, hw
);
357 data
->beacon_timer
.expires
= jiffies
+ data
->beacon_int
;
358 add_timer(&data
->beacon_timer
);
362 static int mac80211_hwsim_config(struct ieee80211_hw
*hw
,
363 struct ieee80211_conf
*conf
)
365 struct mac80211_hwsim_data
*data
= hw
->priv
;
367 printk(KERN_DEBUG
"%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
368 wiphy_name(hw
->wiphy
), __func__
,
369 conf
->channel
->center_freq
, conf
->radio_enabled
,
372 data
->channel
= conf
->channel
;
373 data
->radio_enabled
= conf
->radio_enabled
;
374 data
->beacon_int
= 1024 * conf
->beacon_int
/ 1000 * HZ
/ 1000;
375 if (data
->beacon_int
< 1)
376 data
->beacon_int
= 1;
378 if (!data
->started
|| !data
->radio_enabled
)
379 del_timer(&data
->beacon_timer
);
381 mod_timer(&data
->beacon_timer
, jiffies
+ data
->beacon_int
);
387 static void mac80211_hwsim_configure_filter(struct ieee80211_hw
*hw
,
388 unsigned int changed_flags
,
389 unsigned int *total_flags
,
391 struct dev_addr_list
*mc_list
)
393 struct mac80211_hwsim_data
*data
= hw
->priv
;
395 printk(KERN_DEBUG
"%s:%s\n", wiphy_name(hw
->wiphy
), __func__
);
398 if (*total_flags
& FIF_PROMISC_IN_BSS
)
399 data
->rx_filter
|= FIF_PROMISC_IN_BSS
;
400 if (*total_flags
& FIF_ALLMULTI
)
401 data
->rx_filter
|= FIF_ALLMULTI
;
403 *total_flags
= data
->rx_filter
;
406 static int mac80211_hwsim_config_interface(struct ieee80211_hw
*hw
,
407 struct ieee80211_vif
*vif
,
408 struct ieee80211_if_conf
*conf
)
410 hwsim_check_magic(vif
);
414 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw
*hw
,
415 struct ieee80211_vif
*vif
,
416 struct ieee80211_bss_conf
*info
,
419 hwsim_check_magic(vif
);
422 static void mac80211_hwsim_sta_notify(struct ieee80211_hw
*hw
,
423 struct ieee80211_vif
*vif
,
424 enum sta_notify_cmd cmd
,
425 struct ieee80211_sta
*sta
)
427 hwsim_check_magic(vif
);
430 hwsim_set_sta_magic(sta
);
432 case STA_NOTIFY_REMOVE
:
433 hwsim_clear_sta_magic(sta
);
438 static int mac80211_hwsim_set_tim(struct ieee80211_hw
*hw
,
439 struct ieee80211_sta
*sta
,
442 hwsim_check_sta_magic(sta
);
446 static const struct ieee80211_ops mac80211_hwsim_ops
=
448 .tx
= mac80211_hwsim_tx
,
449 .start
= mac80211_hwsim_start
,
450 .stop
= mac80211_hwsim_stop
,
451 .add_interface
= mac80211_hwsim_add_interface
,
452 .remove_interface
= mac80211_hwsim_remove_interface
,
453 .config
= mac80211_hwsim_config
,
454 .configure_filter
= mac80211_hwsim_configure_filter
,
455 .config_interface
= mac80211_hwsim_config_interface
,
456 .bss_info_changed
= mac80211_hwsim_bss_info_changed
,
457 .sta_notify
= mac80211_hwsim_sta_notify
,
458 .set_tim
= mac80211_hwsim_set_tim
,
462 static void mac80211_hwsim_free(void)
464 struct list_head tmplist
, *i
, *tmp
;
465 struct mac80211_hwsim_data
*data
;
467 INIT_LIST_HEAD(&tmplist
);
469 spin_lock_bh(&hwsim_radio_lock
);
470 list_for_each_safe(i
, tmp
, &hwsim_radios
)
471 list_move(i
, &tmplist
);
472 spin_unlock_bh(&hwsim_radio_lock
);
474 list_for_each_entry(data
, &tmplist
, list
) {
475 ieee80211_unregister_hw(data
->hw
);
476 device_unregister(data
->dev
);
477 ieee80211_free_hw(data
->hw
);
479 class_destroy(hwsim_class
);
483 static struct device_driver mac80211_hwsim_driver
= {
484 .name
= "mac80211_hwsim"
488 static void hwsim_mon_setup(struct net_device
*dev
)
490 dev
->hard_start_xmit
= hwsim_mon_xmit
;
491 dev
->destructor
= free_netdev
;
493 dev
->tx_queue_len
= 0;
494 dev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
495 memset(dev
->dev_addr
, 0, ETH_ALEN
);
496 dev
->dev_addr
[0] = 0x12;
500 static int __init
init_mac80211_hwsim(void)
504 struct mac80211_hwsim_data
*data
;
505 struct ieee80211_hw
*hw
;
507 if (radios
< 1 || radios
> 100)
510 spin_lock_init(&hwsim_radio_lock
);
511 INIT_LIST_HEAD(&hwsim_radios
);
513 hwsim_class
= class_create(THIS_MODULE
, "mac80211_hwsim");
514 if (IS_ERR(hwsim_class
))
515 return PTR_ERR(hwsim_class
);
517 memset(addr
, 0, ETH_ALEN
);
520 for (i
= 0; i
< radios
; i
++) {
521 printk(KERN_DEBUG
"mac80211_hwsim: Initializing radio %d\n",
523 hw
= ieee80211_alloc_hw(sizeof(*data
), &mac80211_hwsim_ops
);
525 printk(KERN_DEBUG
"mac80211_hwsim: ieee80211_alloc_hw "
533 data
->dev
= device_create(hwsim_class
, NULL
, 0, hw
,
535 if (IS_ERR(data
->dev
)) {
537 "mac80211_hwsim: device_create "
538 "failed (%ld)\n", PTR_ERR(data
->dev
));
542 data
->dev
->driver
= &mac80211_hwsim_driver
;
544 SET_IEEE80211_DEV(hw
, data
->dev
);
547 SET_IEEE80211_PERM_ADDR(hw
, addr
);
549 hw
->channel_change_time
= 1;
551 hw
->wiphy
->interface_modes
=
552 BIT(NL80211_IFTYPE_STATION
) |
553 BIT(NL80211_IFTYPE_AP
);
554 hw
->ampdu_queues
= 1;
556 /* ask mac80211 to reserve space for magic */
557 hw
->vif_data_size
= sizeof(struct hwsim_vif_priv
);
558 hw
->sta_data_size
= sizeof(struct hwsim_sta_priv
);
560 memcpy(data
->channels
, hwsim_channels
, sizeof(hwsim_channels
));
561 memcpy(data
->rates
, hwsim_rates
, sizeof(hwsim_rates
));
562 data
->band
.channels
= data
->channels
;
563 data
->band
.n_channels
= ARRAY_SIZE(hwsim_channels
);
564 data
->band
.bitrates
= data
->rates
;
565 data
->band
.n_bitrates
= ARRAY_SIZE(hwsim_rates
);
566 data
->band
.ht_cap
.ht_supported
= true;
567 data
->band
.ht_cap
.cap
= IEEE80211_HT_CAP_SUP_WIDTH_20_40
|
568 IEEE80211_HT_CAP_GRN_FLD
|
569 IEEE80211_HT_CAP_SGI_40
|
570 IEEE80211_HT_CAP_DSSSCCK40
;
571 data
->band
.ht_cap
.ampdu_factor
= 0x3;
572 data
->band
.ht_cap
.ampdu_density
= 0x6;
573 memset(&data
->band
.ht_cap
.mcs
, 0,
574 sizeof(data
->band
.ht_cap
.mcs
));
575 data
->band
.ht_cap
.mcs
.rx_mask
[0] = 0xff;
576 data
->band
.ht_cap
.mcs
.rx_mask
[1] = 0xff;
577 data
->band
.ht_cap
.mcs
.tx_params
= IEEE80211_HT_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 %pM registered\n",
588 wiphy_name(hw
->wiphy
),
589 hw
->wiphy
->perm_addr
);
591 setup_timer(&data
->beacon_timer
, mac80211_hwsim_beacon
,
594 list_add_tail(&data
->list
, &hwsim_radios
);
597 hwsim_mon
= alloc_netdev(0, "hwsim%d", hwsim_mon_setup
);
598 if (hwsim_mon
== NULL
)
603 err
= dev_alloc_name(hwsim_mon
, hwsim_mon
->name
);
608 err
= register_netdevice(hwsim_mon
);
618 free_netdev(hwsim_mon
);
619 mac80211_hwsim_free();
623 device_unregister(data
->dev
);
625 ieee80211_free_hw(hw
);
627 mac80211_hwsim_free();
632 static void __exit
exit_mac80211_hwsim(void)
634 printk(KERN_DEBUG
"mac80211_hwsim: unregister radios\n");
636 unregister_netdev(hwsim_mon
);
637 mac80211_hwsim_free();
641 module_init(init_mac80211_hwsim
);
642 module_exit(exit_mac80211_hwsim
);