blob: bc9da9393760c64b9f36b501e32eeb365b292cb6 [file] [log] [blame]
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001/*
2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 *
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.
8 */
9
10/*
11 * TODO:
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)
15 */
16
Johannes Berg0e057d732008-09-12 00:39:22 +020017#include <linux/list.h>
18#include <linux/spinlock.h>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030019#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>
24
25MODULE_AUTHOR("Jouni Malinen");
26MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
27MODULE_LICENSE("GPL");
28
29static int radios = 2;
30module_param(radios, int, 0444);
31MODULE_PARM_DESC(radios, "Number of simulated radios");
32
Johannes Berg8aa21e62008-09-11 02:16:36 +020033struct hwsim_vif_priv {
34 u32 magic;
35};
36
37#define HWSIM_VIF_MAGIC 0x69537748
38
39static inline void hwsim_check_magic(struct ieee80211_vif *vif)
40{
41 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
42 WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
43}
44
45static inline void hwsim_set_magic(struct ieee80211_vif *vif)
46{
47 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
48 vp->magic = HWSIM_VIF_MAGIC;
49}
50
51static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
52{
53 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
54 vp->magic = 0;
55}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030056
Johannes Berg81c06522008-09-11 02:17:01 +020057struct hwsim_sta_priv {
58 u32 magic;
59};
60
61#define HWSIM_STA_MAGIC 0x6d537748
62
63static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
64{
65 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +020066 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
Johannes Berg81c06522008-09-11 02:17:01 +020067}
68
69static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
70{
71 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +020072 sp->magic = HWSIM_STA_MAGIC;
Johannes Berg81c06522008-09-11 02:17:01 +020073}
74
75static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
76{
77 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
78 sp->magic = 0;
79}
80
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030081static struct class *hwsim_class;
82
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030083static struct net_device *hwsim_mon; /* global monitor netdev */
84
85
86static 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 },
101};
102
103static const struct ieee80211_rate hwsim_rates[] = {
104 { .bitrate = 10 },
105 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
106 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
107 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
108 { .bitrate = 60 },
109 { .bitrate = 90 },
110 { .bitrate = 120 },
111 { .bitrate = 180 },
112 { .bitrate = 240 },
113 { .bitrate = 360 },
114 { .bitrate = 480 },
115 { .bitrate = 540 }
116};
117
Johannes Berg0e057d732008-09-12 00:39:22 +0200118static spinlock_t hwsim_radio_lock;
119static struct list_head hwsim_radios;
120
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300121struct mac80211_hwsim_data {
Johannes Berg0e057d732008-09-12 00:39:22 +0200122 struct list_head list;
123 struct ieee80211_hw *hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300124 struct device *dev;
125 struct ieee80211_supported_band band;
126 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
127 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
128
129 struct ieee80211_channel *channel;
130 int radio_enabled;
131 unsigned long beacon_int; /* in jiffies unit */
132 unsigned int rx_filter;
133 int started;
134 struct timer_list beacon_timer;
135};
136
137
138struct hwsim_radiotap_hdr {
139 struct ieee80211_radiotap_header hdr;
140 u8 rt_flags;
141 u8 rt_rate;
142 __le16 rt_channel;
143 __le16 rt_chbitmask;
144} __attribute__ ((packed));
145
146
147static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
148{
149 /* TODO: allow packet injection */
150 dev_kfree_skb(skb);
151 return 0;
152}
153
154
155static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
156 struct sk_buff *tx_skb)
157{
158 struct mac80211_hwsim_data *data = hw->priv;
159 struct sk_buff *skb;
160 struct hwsim_radiotap_hdr *hdr;
161 u16 flags;
162 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
163 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
164
165 if (!netif_running(hwsim_mon))
166 return;
167
168 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
169 if (skb == NULL)
170 return;
171
172 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
173 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
174 hdr->hdr.it_pad = 0;
175 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
Jouni Malinenf248f102008-06-13 19:44:47 +0300176 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
177 (1 << IEEE80211_RADIOTAP_RATE) |
178 (1 << IEEE80211_RADIOTAP_CHANNEL));
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300179 hdr->rt_flags = 0;
180 hdr->rt_rate = txrate->bitrate / 5;
Johannes Bergdf70b4a2008-07-10 11:56:33 +0200181 hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300182 flags = IEEE80211_CHAN_2GHZ;
183 if (txrate->flags & IEEE80211_RATE_ERP_G)
184 flags |= IEEE80211_CHAN_OFDM;
185 else
186 flags |= IEEE80211_CHAN_CCK;
187 hdr->rt_chbitmask = cpu_to_le16(flags);
188
189 skb->dev = hwsim_mon;
190 skb_set_mac_header(skb, 0);
191 skb->ip_summed = CHECKSUM_UNNECESSARY;
192 skb->pkt_type = PACKET_OTHERHOST;
Jouni Malinenf248f102008-06-13 19:44:47 +0300193 skb->protocol = htons(ETH_P_802_2);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300194 memset(skb->cb, 0, sizeof(skb->cb));
195 netif_rx(skb);
196}
197
198
Johannes Berg0e057d732008-09-12 00:39:22 +0200199static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
200 struct sk_buff *skb)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300201{
Johannes Berg0e057d732008-09-12 00:39:22 +0200202 struct mac80211_hwsim_data *data = hw->priv, *data2;
203 bool ack = false;
Jouni Malinene36cfdc2008-06-13 19:44:48 +0300204 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300205 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +0300206 struct ieee80211_rx_status rx_status;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300207
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;
Johannes Berge6a98542008-10-21 12:40:02 +0200212 rx_status.rate_idx = info->control.rates[0].idx;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300213 /* TODO: simulate signal strength (and optional packet drop) */
214
215 /* Copy skb to all enabled radios that are on the current frequency */
Johannes Berg0e057d732008-09-12 00:39:22 +0200216 spin_lock(&hwsim_radio_lock);
217 list_for_each_entry(data2, &hwsim_radios, list) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300218 struct sk_buff *nskb;
219
Johannes Berg0e057d732008-09-12 00:39:22 +0200220 if (data == data2)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300221 continue;
Johannes Berg0e057d732008-09-12 00:39:22 +0200222
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300223 if (!data2->started || !data2->radio_enabled ||
224 data->channel->center_freq != data2->channel->center_freq)
225 continue;
226
227 nskb = skb_copy(skb, GFP_ATOMIC);
228 if (nskb == NULL)
229 continue;
230
Johannes Berg0e057d732008-09-12 00:39:22 +0200231 if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300232 ETH_ALEN) == 0)
Johannes Berg0e057d732008-09-12 00:39:22 +0200233 ack = true;
234 ieee80211_rx_irqsafe(data2->hw, nskb, &rx_status);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300235 }
Johannes Berg0e057d732008-09-12 00:39:22 +0200236 spin_unlock(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300237
Jouni Malinene36cfdc2008-06-13 19:44:48 +0300238 return ack;
239}
240
241
242static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
243{
244 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berg0e057d732008-09-12 00:39:22 +0200245 bool ack;
Jouni Malinene36cfdc2008-06-13 19:44:48 +0300246 struct ieee80211_tx_info *txi;
247
248 mac80211_hwsim_monitor_rx(hw, skb);
249
250 if (skb->len < 10) {
251 /* Should not happen; just a sanity check for addr1 use */
252 dev_kfree_skb(skb);
253 return NETDEV_TX_OK;
254 }
255
256 if (!data->radio_enabled) {
257 printk(KERN_DEBUG "%s: dropped TX frame since radio "
258 "disabled\n", wiphy_name(hw->wiphy));
259 dev_kfree_skb(skb);
260 return NETDEV_TX_OK;
261 }
262
263 ack = mac80211_hwsim_tx_frame(hw, skb);
264
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300265 txi = IEEE80211_SKB_CB(skb);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200266
Johannes Berg25d834e2008-09-12 22:52:47 +0200267 if (txi->control.vif)
268 hwsim_check_magic(txi->control.vif);
Johannes Berg81c06522008-09-11 02:17:01 +0200269 if (txi->control.sta)
270 hwsim_check_sta_magic(txi->control.sta);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200271
Johannes Berge6a98542008-10-21 12:40:02 +0200272 ieee80211_tx_info_clear_status(txi);
273 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
274 txi->flags |= IEEE80211_TX_STAT_ACK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300275 ieee80211_tx_status_irqsafe(hw, skb);
276 return NETDEV_TX_OK;
277}
278
279
280static int mac80211_hwsim_start(struct ieee80211_hw *hw)
281{
282 struct mac80211_hwsim_data *data = hw->priv;
283 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
284 data->started = 1;
285 return 0;
286}
287
288
289static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
290{
291 struct mac80211_hwsim_data *data = hw->priv;
292 data->started = 0;
Jouni Malinenab1ef982008-10-30 16:59:25 +0200293 del_timer(&data->beacon_timer);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300294 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
295}
296
297
298static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
299 struct ieee80211_if_init_conf *conf)
300{
Johannes Berge1749612008-10-27 15:59:26 -0700301 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300302 wiphy_name(hw->wiphy), __func__, conf->type,
Johannes Berge1749612008-10-27 15:59:26 -0700303 conf->mac_addr);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200304 hwsim_set_magic(conf->vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300305 return 0;
306}
307
308
309static void mac80211_hwsim_remove_interface(
310 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
311{
Johannes Berge1749612008-10-27 15:59:26 -0700312 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300313 wiphy_name(hw->wiphy), __func__, conf->type,
Johannes Berge1749612008-10-27 15:59:26 -0700314 conf->mac_addr);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200315 hwsim_check_magic(conf->vif);
316 hwsim_clear_magic(conf->vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300317}
318
319
320static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
321 struct ieee80211_vif *vif)
322{
323 struct ieee80211_hw *hw = arg;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300324 struct sk_buff *skb;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300325 struct ieee80211_tx_info *info;
326
Johannes Berg8aa21e62008-09-11 02:16:36 +0200327 hwsim_check_magic(vif);
328
Johannes Berg05c914f2008-09-11 00:01:58 +0200329 if (vif->type != NL80211_IFTYPE_AP)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300330 return;
331
332 skb = ieee80211_beacon_get(hw, vif);
333 if (skb == NULL)
334 return;
335 info = IEEE80211_SKB_CB(skb);
336
337 mac80211_hwsim_monitor_rx(hw, skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +0300338 mac80211_hwsim_tx_frame(hw, skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300339 dev_kfree_skb(skb);
340}
341
342
343static void mac80211_hwsim_beacon(unsigned long arg)
344{
345 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
346 struct mac80211_hwsim_data *data = hw->priv;
347
348 if (!data->started || !data->radio_enabled)
349 return;
350
Jouni Malinenf248f102008-06-13 19:44:47 +0300351 ieee80211_iterate_active_interfaces_atomic(
352 hw, mac80211_hwsim_beacon_tx, hw);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300353
354 data->beacon_timer.expires = jiffies + data->beacon_int;
355 add_timer(&data->beacon_timer);
356}
357
358
Johannes Berge8975582008-10-09 12:18:51 +0200359static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300360{
361 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200362 struct ieee80211_conf *conf = &hw->conf;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300363
364 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
365 wiphy_name(hw->wiphy), __func__,
366 conf->channel->center_freq, conf->radio_enabled,
367 conf->beacon_int);
368
369 data->channel = conf->channel;
370 data->radio_enabled = conf->radio_enabled;
371 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
372 if (data->beacon_int < 1)
373 data->beacon_int = 1;
374
375 if (!data->started || !data->radio_enabled)
376 del_timer(&data->beacon_timer);
377 else
378 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
379
380 return 0;
381}
382
383
384static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
385 unsigned int changed_flags,
386 unsigned int *total_flags,
387 int mc_count,
388 struct dev_addr_list *mc_list)
389{
390 struct mac80211_hwsim_data *data = hw->priv;
391
392 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
393
394 data->rx_filter = 0;
395 if (*total_flags & FIF_PROMISC_IN_BSS)
396 data->rx_filter |= FIF_PROMISC_IN_BSS;
397 if (*total_flags & FIF_ALLMULTI)
398 data->rx_filter |= FIF_ALLMULTI;
399
400 *total_flags = data->rx_filter;
401}
402
Johannes Berg8aa21e62008-09-11 02:16:36 +0200403static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
404 struct ieee80211_vif *vif,
405 struct ieee80211_if_conf *conf)
406{
407 hwsim_check_magic(vif);
408 return 0;
409}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300410
Johannes Berg8aa21e62008-09-11 02:16:36 +0200411static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
412 struct ieee80211_vif *vif,
413 struct ieee80211_bss_conf *info,
414 u32 changed)
415{
416 hwsim_check_magic(vif);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +0200417
418 printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
419 wiphy_name(hw->wiphy), __func__, changed);
420
421 if (changed & BSS_CHANGED_ASSOC) {
422 printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
423 wiphy_name(hw->wiphy), info->assoc, info->aid);
424 }
425
426 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
427 printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
428 wiphy_name(hw->wiphy), info->use_cts_prot);
429 }
430
431 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
432 printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
433 wiphy_name(hw->wiphy), info->use_short_preamble);
434 }
435
436 if (changed & BSS_CHANGED_ERP_SLOT) {
437 printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
438 wiphy_name(hw->wiphy), info->use_short_slot);
439 }
440
441 if (changed & BSS_CHANGED_HT) {
442 printk(KERN_DEBUG " %s: HT: sec_ch_offs=%d width_40_ok=%d "
443 "op_mode=%d\n",
444 wiphy_name(hw->wiphy),
445 info->ht.secondary_channel_offset,
446 info->ht.width_40_ok, info->ht.operation_mode);
447 }
448
449 if (changed & BSS_CHANGED_BASIC_RATES) {
450 printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
451 wiphy_name(hw->wiphy),
452 (unsigned long long) info->basic_rates);
453 }
Johannes Berg8aa21e62008-09-11 02:16:36 +0200454}
455
456static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
457 struct ieee80211_vif *vif,
Johannes Berg17741cd2008-09-11 00:02:02 +0200458 enum sta_notify_cmd cmd,
459 struct ieee80211_sta *sta)
Johannes Berg8aa21e62008-09-11 02:16:36 +0200460{
461 hwsim_check_magic(vif);
Johannes Berg81c06522008-09-11 02:17:01 +0200462 switch (cmd) {
463 case STA_NOTIFY_ADD:
464 hwsim_set_sta_magic(sta);
465 break;
466 case STA_NOTIFY_REMOVE:
467 hwsim_clear_sta_magic(sta);
468 break;
469 }
470}
471
472static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
473 struct ieee80211_sta *sta,
474 bool set)
475{
476 hwsim_check_sta_magic(sta);
477 return 0;
Johannes Berg8aa21e62008-09-11 02:16:36 +0200478}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300479
Jouni Malinen1e898ff82008-10-30 16:59:23 +0200480static int mac80211_hwsim_conf_tx(
481 struct ieee80211_hw *hw, u16 queue,
482 const struct ieee80211_tx_queue_params *params)
483{
484 printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
485 "aifs=%d)\n",
486 wiphy_name(hw->wiphy), __func__, queue,
487 params->txop, params->cw_min, params->cw_max, params->aifs);
488 return 0;
489}
490
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300491static const struct ieee80211_ops mac80211_hwsim_ops =
492{
493 .tx = mac80211_hwsim_tx,
494 .start = mac80211_hwsim_start,
495 .stop = mac80211_hwsim_stop,
496 .add_interface = mac80211_hwsim_add_interface,
497 .remove_interface = mac80211_hwsim_remove_interface,
498 .config = mac80211_hwsim_config,
499 .configure_filter = mac80211_hwsim_configure_filter,
Johannes Berg8aa21e62008-09-11 02:16:36 +0200500 .config_interface = mac80211_hwsim_config_interface,
501 .bss_info_changed = mac80211_hwsim_bss_info_changed,
502 .sta_notify = mac80211_hwsim_sta_notify,
Johannes Berg81c06522008-09-11 02:17:01 +0200503 .set_tim = mac80211_hwsim_set_tim,
Jouni Malinen1e898ff82008-10-30 16:59:23 +0200504 .conf_tx = mac80211_hwsim_conf_tx,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300505};
506
507
508static void mac80211_hwsim_free(void)
509{
Johannes Berg0e057d732008-09-12 00:39:22 +0200510 struct list_head tmplist, *i, *tmp;
511 struct mac80211_hwsim_data *data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300512
Johannes Berg0e057d732008-09-12 00:39:22 +0200513 INIT_LIST_HEAD(&tmplist);
514
515 spin_lock_bh(&hwsim_radio_lock);
516 list_for_each_safe(i, tmp, &hwsim_radios)
517 list_move(i, &tmplist);
518 spin_unlock_bh(&hwsim_radio_lock);
519
520 list_for_each_entry(data, &tmplist, list) {
521 ieee80211_unregister_hw(data->hw);
522 device_unregister(data->dev);
523 ieee80211_free_hw(data->hw);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300524 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300525 class_destroy(hwsim_class);
526}
527
528
529static struct device_driver mac80211_hwsim_driver = {
530 .name = "mac80211_hwsim"
531};
532
533
534static void hwsim_mon_setup(struct net_device *dev)
535{
536 dev->hard_start_xmit = hwsim_mon_xmit;
537 dev->destructor = free_netdev;
538 ether_setup(dev);
539 dev->tx_queue_len = 0;
540 dev->type = ARPHRD_IEEE80211_RADIOTAP;
541 memset(dev->dev_addr, 0, ETH_ALEN);
542 dev->dev_addr[0] = 0x12;
543}
544
545
546static int __init init_mac80211_hwsim(void)
547{
548 int i, err = 0;
549 u8 addr[ETH_ALEN];
550 struct mac80211_hwsim_data *data;
551 struct ieee80211_hw *hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300552
Johannes Berg0e057d732008-09-12 00:39:22 +0200553 if (radios < 1 || radios > 100)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300554 return -EINVAL;
555
Johannes Berg0e057d732008-09-12 00:39:22 +0200556 spin_lock_init(&hwsim_radio_lock);
557 INIT_LIST_HEAD(&hwsim_radios);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300558
559 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
Johannes Berg0e057d732008-09-12 00:39:22 +0200560 if (IS_ERR(hwsim_class))
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300561 return PTR_ERR(hwsim_class);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300562
563 memset(addr, 0, ETH_ALEN);
564 addr[0] = 0x02;
565
Johannes Berg0e057d732008-09-12 00:39:22 +0200566 for (i = 0; i < radios; i++) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300567 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
568 i);
569 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
Johannes Berg0e057d732008-09-12 00:39:22 +0200570 if (!hw) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300571 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
572 "failed\n");
573 err = -ENOMEM;
574 goto failed;
575 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300576 data = hw->priv;
Johannes Berg0e057d732008-09-12 00:39:22 +0200577 data->hw = hw;
578
Greg Kroah-Hartman6e05d6c2008-07-21 20:03:34 -0700579 data->dev = device_create(hwsim_class, NULL, 0, hw,
580 "hwsim%d", i);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300581 if (IS_ERR(data->dev)) {
Stephen Rothwelle800f172008-06-16 15:35:10 +1000582 printk(KERN_DEBUG
Greg Kroah-Hartman6e05d6c2008-07-21 20:03:34 -0700583 "mac80211_hwsim: device_create "
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300584 "failed (%ld)\n", PTR_ERR(data->dev));
585 err = -ENOMEM;
Ian Schram3a33cc12008-07-21 13:19:35 -0700586 goto failed_drvdata;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300587 }
588 data->dev->driver = &mac80211_hwsim_driver;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300589
590 SET_IEEE80211_DEV(hw, data->dev);
591 addr[3] = i >> 8;
592 addr[4] = i;
593 SET_IEEE80211_PERM_ADDR(hw, addr);
594
595 hw->channel_change_time = 1;
Jouni Malinen87e8b642008-08-21 21:45:56 +0300596 hw->queues = 4;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700597 hw->wiphy->interface_modes =
598 BIT(NL80211_IFTYPE_STATION) |
599 BIT(NL80211_IFTYPE_AP);
Jouni Malinen87e8b642008-08-21 21:45:56 +0300600 hw->ampdu_queues = 1;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300601
Johannes Berg8aa21e62008-09-11 02:16:36 +0200602 /* ask mac80211 to reserve space for magic */
603 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
Johannes Berg81c06522008-09-11 02:17:01 +0200604 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200605
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300606 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
607 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
608 data->band.channels = data->channels;
609 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
610 data->band.bitrates = data->rates;
611 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200612 data->band.ht_cap.ht_supported = true;
613 data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
Jouni Malinen87e8b642008-08-21 21:45:56 +0300614 IEEE80211_HT_CAP_GRN_FLD |
615 IEEE80211_HT_CAP_SGI_40 |
616 IEEE80211_HT_CAP_DSSSCCK40;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200617 data->band.ht_cap.ampdu_factor = 0x3;
618 data->band.ht_cap.ampdu_density = 0x6;
619 memset(&data->band.ht_cap.mcs, 0,
620 sizeof(data->band.ht_cap.mcs));
621 data->band.ht_cap.mcs.rx_mask[0] = 0xff;
622 data->band.ht_cap.mcs.rx_mask[1] = 0xff;
623 data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300624 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
625
626 err = ieee80211_register_hw(hw);
627 if (err < 0) {
628 printk(KERN_DEBUG "mac80211_hwsim: "
629 "ieee80211_register_hw failed (%d)\n", err);
Ian Schram3a33cc12008-07-21 13:19:35 -0700630 goto failed_hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300631 }
632
Johannes Berge1749612008-10-27 15:59:26 -0700633 printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300634 wiphy_name(hw->wiphy),
Johannes Berge1749612008-10-27 15:59:26 -0700635 hw->wiphy->perm_addr);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300636
637 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
638 (unsigned long) hw);
Johannes Berg0e057d732008-09-12 00:39:22 +0200639
640 list_add_tail(&data->list, &hwsim_radios);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300641 }
642
643 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
644 if (hwsim_mon == NULL)
645 goto failed;
646
647 rtnl_lock();
648
649 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
Ian Schram3a33cc12008-07-21 13:19:35 -0700650 if (err < 0)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300651 goto failed_mon;
Ian Schram3a33cc12008-07-21 13:19:35 -0700652
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300653
654 err = register_netdevice(hwsim_mon);
655 if (err < 0)
656 goto failed_mon;
657
658 rtnl_unlock();
659
660 return 0;
661
662failed_mon:
663 rtnl_unlock();
664 free_netdev(hwsim_mon);
Ian Schram3a33cc12008-07-21 13:19:35 -0700665 mac80211_hwsim_free();
666 return err;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300667
Ian Schram3a33cc12008-07-21 13:19:35 -0700668failed_hw:
669 device_unregister(data->dev);
670failed_drvdata:
671 ieee80211_free_hw(hw);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300672failed:
673 mac80211_hwsim_free();
674 return err;
675}
676
677
678static void __exit exit_mac80211_hwsim(void)
679{
Johannes Berg0e057d732008-09-12 00:39:22 +0200680 printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300681
682 unregister_netdev(hwsim_mon);
683 mac80211_hwsim_free();
684}
685
686
687module_init(init_mac80211_hwsim);
688module_exit(exit_mac80211_hwsim);