bccb2f45f741dccf6dadd377f9d164a51d6ec261
[libeap.git] / mac80211_hwsim / mac80211_hwsim.c
1 /*
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  * - 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
15  */
16
17 #include <net/mac80211.h>
18
19 MODULE_AUTHOR("Jouni Malinen");
20 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
21 MODULE_LICENSE("GPL");
22
23 static int radios = 2;
24 module_param(radios, int, 0444);
25 MODULE_PARM_DESC(radios, "Number of simulated radios");
26
27
28 static struct class *hwsim_class;
29
30 static struct ieee80211_hw **hwsim_radios;
31 static int hwsim_radio_count;
32
33
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 },
49 };
50
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 }
65 };
66
67 struct mac80211_hwsim_data {
68         struct device *dev;
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)];
72
73         int freq;
74         int channel;
75         enum ieee80211_phymode phymode;
76         int radio_enabled;
77         int beacon_int;
78         unsigned int rx_filter;
79 };
80
81
82 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
83                              struct ieee80211_tx_control *control)
84 {
85         struct mac80211_hwsim_data *data = hw->priv;
86         struct ieee80211_tx_status tx_status;
87         struct ieee80211_rx_status rx_status;
88         int i;
89
90         if (!data->radio_enabled) {
91                 printk(KERN_DEBUG "%s: dropped TX frame since radio "
92                        "disabled\n", wiphy_name(hw->wiphy));
93                 dev_kfree_skb(skb);
94                 return NETDEV_TX_OK;
95         }
96
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) */
104
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;
109
110                 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
111                         continue;
112                 data2 = hwsim_radios[i]->priv;
113                 if (!data2->radio_enabled || data->freq != data2->freq)
114                         continue;
115
116                 nskb = skb_copy(skb, GFP_ATOMIC);
117                 if (nskb == NULL)
118                         continue;
119
120                 ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
121         }
122
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);
128         return NETDEV_TX_OK;
129 }
130
131
132 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
133 {
134         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
135         return 0;
136 }
137
138
139 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
140 {
141         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
142 }
143
144
145 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
146                                         struct ieee80211_if_init_conf *conf)
147 {
148         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
149         return 0;
150 }
151
152
153 static void mac80211_hwsim_remove_interface(
154         struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
155 {
156         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
157 }
158
159
160 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
161                                  struct ieee80211_conf *conf)
162 {
163         struct mac80211_hwsim_data *data = hw->priv;
164
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);
168
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;
174
175         return 0;
176 }
177
178
179 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
180                                             unsigned int changed_flags,
181                                             unsigned int *total_flags,
182                                             int mc_count,
183                                             struct dev_addr_list *mc_list)
184 {
185         struct mac80211_hwsim_data *data = hw->priv;
186
187         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
188
189         data->rx_filter = 0;
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;
194
195         *total_flags = data->rx_filter;
196 }
197
198
199
200 static const struct ieee80211_ops mac80211_hwsim_ops =
201 {
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,
209 };
210
211
212 static void mac80211_hwsim_free(void)
213 {
214         int i;
215
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]);
224                 }
225         }
226         kfree(hwsim_radios);
227         class_destroy(hwsim_class);
228 }
229
230
231 static struct device_driver mac80211_hwsim_driver = {
232         .name = "mac80211_hwsim"
233 };
234
235
236 static int __init init_mac80211_hwsim(void)
237 {
238         int i, err = 0;
239         u8 addr[ETH_ALEN];
240         struct mac80211_hwsim_data *data;
241         struct ieee80211_hw *hw;
242         DECLARE_MAC_BUF(mac);
243
244         if (radios < 1 || radios > 65535)
245                 return -EINVAL;
246
247         hwsim_radio_count = radios;
248         hwsim_radios = kcalloc(hwsim_radio_count,
249                                sizeof(struct ieee80211_hw *), GFP_KERNEL);
250         if (hwsim_radios == NULL)
251                 return -ENOMEM;
252
253         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
254         if (IS_ERR(hwsim_class)) {
255                 kfree(hwsim_radios);
256                 return PTR_ERR(hwsim_class);
257         }
258
259         memset(addr, 0, ETH_ALEN);
260         addr[0] = 0x02;
261
262         for (i = 0; i < hwsim_radio_count; i++) {
263                 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
264                        i);
265                 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
266                 if (hw == NULL) {
267                         printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
268                                "failed\n");
269                         err = -ENOMEM;
270                         goto failed;
271                 }
272                 hwsim_radios[i] = hw;
273
274                 data = hw->priv;
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));
279                         err = -ENOMEM;
280                         goto failed;
281                 }
282                 data->dev->driver = &mac80211_hwsim_driver;
283                 dev_set_drvdata(data->dev, hw);
284
285                 SET_IEEE80211_DEV(hw, data->dev);
286                 addr[3] = i >> 8;
287                 addr[4] = i;
288                 SET_IEEE80211_PERM_ADDR(hw, addr);
289
290                 hw->channel_change_time = 1;
291                 hw->queues = 1;
292
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);
300
301                 err = ieee80211_register_hwmode(hw, data->modes);
302                 if (err < 0) {
303                         printk(KERN_DEBUG "mac80211_hwsim: "
304                                "ieee80211_register_hwmode failed (%d)\n", err);
305                         goto failed;
306                 }
307
308                 err = ieee80211_register_hw(hw);
309                 if (err < 0) {
310                         printk(KERN_DEBUG "mac80211_hwsim: "
311                                "ieee80211_register_hw failed (%d)\n", err);
312                         goto failed;
313                 }
314
315                 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
316                        wiphy_name(hw->wiphy),
317                        print_mac(mac, hw->wiphy->perm_addr));
318         }
319
320         return 0;
321
322 failed:
323         mac80211_hwsim_free();
324         return err;
325 }
326
327
328 static void __exit exit_mac80211_hwsim(void)
329 {
330         printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
331                hwsim_radio_count);
332
333         mac80211_hwsim_free();
334 }
335
336
337 module_init(init_mac80211_hwsim);
338 module_exit(exit_mac80211_hwsim);