Added global monitor interface (hwsim#)
[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 #include <net/ieee80211_radiotap.h>
19 #include <linux/if_arp.h>
20 #include <linux/rtnetlink.h>
21
22 MODULE_AUTHOR("Jouni Malinen");
23 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
24 MODULE_LICENSE("GPL");
25
26 static int radios = 2;
27 module_param(radios, int, 0444);
28 MODULE_PARM_DESC(radios, "Number of simulated radios");
29
30
31 static struct class *hwsim_class;
32
33 static struct ieee80211_hw **hwsim_radios;
34 static int hwsim_radio_count;
35 static struct net_device *hwsim_mon; /* global monitor netdev */
36
37
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 },
53 };
54
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 }
69 };
70
71 struct mac80211_hwsim_data {
72         struct device *dev;
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)];
76
77         int freq;
78         int channel;
79         enum ieee80211_phymode phymode;
80         int radio_enabled;
81         int beacon_int;
82         unsigned int rx_filter;
83 };
84
85
86 struct hwsim_radiotap_hdr {
87         struct ieee80211_radiotap_header hdr;
88         u8 rt_flags;
89         u8 rt_rate;
90         __le16 rt_channel;
91         __le16 rt_chbitmask;
92 } __attribute__ ((packed));
93
94
95 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
96 {
97         /* TODO: allow packet injection */
98         dev_kfree_skb(skb);
99         return 0;
100 }
101
102
103 static void mac80211_hwsim_monitor_rx(struct mac80211_hwsim_data *data,
104                                       struct sk_buff *tx_skb,
105                                       struct ieee80211_tx_control *control)
106 {
107         struct sk_buff *skb;
108         struct hwsim_radiotap_hdr *hdr;
109         u16 flags;
110
111         if (!netif_running(hwsim_mon))
112                 return;
113
114         skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
115         if (skb == NULL)
116                 return;
117
118         hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
119         hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
120         hdr->hdr.it_pad = 0;
121         hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
122         hdr->hdr.it_present = __constant_cpu_to_le32(
123              (1 << IEEE80211_RADIOTAP_FLAGS) |
124              (1 << IEEE80211_RADIOTAP_RATE) |
125              (1 << IEEE80211_RADIOTAP_CHANNEL));
126         hdr->rt_flags = 0;
127         hdr->rt_rate = control->tx_rate / 5;
128         hdr->rt_channel = data->freq;
129         flags = IEEE80211_CHAN_2GHZ;
130         if (control->rate->flags & IEEE80211_RATE_OFDM)
131                 flags |= IEEE80211_CHAN_OFDM;
132         if (control->rate->flags & IEEE80211_RATE_CCK)
133                 flags |= IEEE80211_CHAN_CCK;
134         hdr->rt_chbitmask = cpu_to_le16(flags);
135
136         skb->dev = hwsim_mon;
137         skb_set_mac_header(skb, 0);
138         skb->ip_summed = CHECKSUM_UNNECESSARY;
139         skb->pkt_type = PACKET_OTHERHOST;
140         skb->protocol = __constant_htons(ETH_P_802_2);
141         memset(skb->cb, 0, sizeof(skb->cb));
142         netif_rx(skb);
143 }
144
145
146 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
147                              struct ieee80211_tx_control *control)
148 {
149         struct mac80211_hwsim_data *data = hw->priv;
150         struct ieee80211_tx_status tx_status;
151         struct ieee80211_rx_status rx_status;
152         int i;
153
154         mac80211_hwsim_monitor_rx(data, skb, control);
155
156         if (!data->radio_enabled) {
157                 printk(KERN_DEBUG "%s: dropped TX frame since radio "
158                        "disabled\n", wiphy_name(hw->wiphy));
159                 dev_kfree_skb(skb);
160                 return NETDEV_TX_OK;
161         }
162
163         memset(&rx_status, 0, sizeof(rx_status));
164         /* TODO: set mactime */
165         rx_status.freq = data->freq;
166         rx_status.channel = data->channel;
167         rx_status.phymode = data->phymode;
168         rx_status.rate = control->tx_rate;
169         /* TODO: simulate signal strength (and optional packet drop) */
170
171         /* Copy skb to all enabled radios that are on the current frequency */
172         for (i = 0; i < hwsim_radio_count; i++) {
173                 struct mac80211_hwsim_data *data2;
174                 struct sk_buff *nskb;
175
176                 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
177                         continue;
178                 data2 = hwsim_radios[i]->priv;
179                 if (!data2->radio_enabled || data->freq != data2->freq)
180                         continue;
181
182                 nskb = skb_copy(skb, GFP_ATOMIC);
183                 if (nskb == NULL)
184                         continue;
185
186                 ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
187         }
188
189         memset(&tx_status, 0, sizeof(tx_status));
190         memcpy(&tx_status.control, control, sizeof(*control));
191         /* TODO: proper ACK determination */
192         tx_status.flags = IEEE80211_TX_STATUS_ACK;
193         ieee80211_tx_status(hw, skb, &tx_status);
194         return NETDEV_TX_OK;
195 }
196
197
198 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
199 {
200         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
201         return 0;
202 }
203
204
205 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
206 {
207         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
208 }
209
210
211 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
212                                         struct ieee80211_if_init_conf *conf)
213 {
214         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
215         return 0;
216 }
217
218
219 static void mac80211_hwsim_remove_interface(
220         struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
221 {
222         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
223 }
224
225
226 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
227                                  struct ieee80211_conf *conf)
228 {
229         struct mac80211_hwsim_data *data = hw->priv;
230
231         printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
232                wiphy_name(hw->wiphy), __func__,
233                conf->freq, conf->radio_enabled, conf->beacon_int);
234
235         data->freq = conf->freq;
236         data->channel = conf->channel;
237         data->phymode = conf->phymode;
238         data->radio_enabled = conf->radio_enabled;
239         data->beacon_int = conf->beacon_int;
240
241         return 0;
242 }
243
244
245 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
246                                             unsigned int changed_flags,
247                                             unsigned int *total_flags,
248                                             int mc_count,
249                                             struct dev_addr_list *mc_list)
250 {
251         struct mac80211_hwsim_data *data = hw->priv;
252
253         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
254
255         data->rx_filter = 0;
256         if (*total_flags & FIF_PROMISC_IN_BSS)
257                 data->rx_filter |= FIF_PROMISC_IN_BSS;
258         if (*total_flags & FIF_ALLMULTI)
259                 data->rx_filter |= FIF_ALLMULTI;
260
261         *total_flags = data->rx_filter;
262 }
263
264
265
266 static const struct ieee80211_ops mac80211_hwsim_ops =
267 {
268         .tx = mac80211_hwsim_tx,
269         .start = mac80211_hwsim_start,
270         .stop = mac80211_hwsim_stop,
271         .add_interface = mac80211_hwsim_add_interface,
272         .remove_interface = mac80211_hwsim_remove_interface,
273         .config = mac80211_hwsim_config,
274         .configure_filter = mac80211_hwsim_configure_filter,
275 };
276
277
278 static void mac80211_hwsim_free(void)
279 {
280         int i;
281
282         for (i = 0; i < hwsim_radio_count; i++) {
283                 if (hwsim_radios[i]) {
284                         struct mac80211_hwsim_data *data;
285                         data = hwsim_radios[i]->priv;
286                         ieee80211_unregister_hw(hwsim_radios[i]);
287                         if (!IS_ERR(data->dev))
288                                 device_unregister(data->dev);
289                         ieee80211_free_hw(hwsim_radios[i]);
290                 }
291         }
292         kfree(hwsim_radios);
293         class_destroy(hwsim_class);
294 }
295
296
297 static struct device_driver mac80211_hwsim_driver = {
298         .name = "mac80211_hwsim"
299 };
300
301
302 static void hwsim_mon_setup(struct net_device *dev)
303 {
304         dev->hard_start_xmit = hwsim_mon_xmit;
305         dev->destructor = free_netdev;
306         ether_setup(dev);
307         dev->tx_queue_len = 0;
308         dev->type = ARPHRD_IEEE80211_RADIOTAP;
309         memset(dev->dev_addr, 0, ETH_ALEN);
310         dev->dev_addr[0] = 0x12;
311 }
312
313
314 static int __init init_mac80211_hwsim(void)
315 {
316         int i, err = 0;
317         u8 addr[ETH_ALEN];
318         struct mac80211_hwsim_data *data;
319         struct ieee80211_hw *hw;
320         DECLARE_MAC_BUF(mac);
321
322         if (radios < 1 || radios > 65535)
323                 return -EINVAL;
324
325         hwsim_radio_count = radios;
326         hwsim_radios = kcalloc(hwsim_radio_count,
327                                sizeof(struct ieee80211_hw *), GFP_KERNEL);
328         if (hwsim_radios == NULL)
329                 return -ENOMEM;
330
331         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
332         if (IS_ERR(hwsim_class)) {
333                 kfree(hwsim_radios);
334                 return PTR_ERR(hwsim_class);
335         }
336
337         memset(addr, 0, ETH_ALEN);
338         addr[0] = 0x02;
339
340         for (i = 0; i < hwsim_radio_count; i++) {
341                 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
342                        i);
343                 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
344                 if (hw == NULL) {
345                         printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
346                                "failed\n");
347                         err = -ENOMEM;
348                         goto failed;
349                 }
350                 hwsim_radios[i] = hw;
351
352                 data = hw->priv;
353                 data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
354                 if (IS_ERR(data->dev)) {
355                         printk(KERN_DEBUG "mac80211_hwsim: device_create "
356                                "failed (%ld)\n", PTR_ERR(data->dev));
357                         err = -ENOMEM;
358                         goto failed;
359                 }
360                 data->dev->driver = &mac80211_hwsim_driver;
361                 dev_set_drvdata(data->dev, hw);
362
363                 SET_IEEE80211_DEV(hw, data->dev);
364                 addr[3] = i >> 8;
365                 addr[4] = i;
366                 SET_IEEE80211_PERM_ADDR(hw, addr);
367
368                 hw->channel_change_time = 1;
369                 hw->queues = 1;
370
371                 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
372                 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
373                 data->modes[0].channels = data->channels;
374                 data->modes[0].rates = data->rates;
375                 data->modes[0].mode = MODE_IEEE80211G;
376                 data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
377                 data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
378
379                 err = ieee80211_register_hwmode(hw, data->modes);
380                 if (err < 0) {
381                         printk(KERN_DEBUG "mac80211_hwsim: "
382                                "ieee80211_register_hwmode failed (%d)\n", err);
383                         goto failed;
384                 }
385
386                 err = ieee80211_register_hw(hw);
387                 if (err < 0) {
388                         printk(KERN_DEBUG "mac80211_hwsim: "
389                                "ieee80211_register_hw failed (%d)\n", err);
390                         goto failed;
391                 }
392
393                 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
394                        wiphy_name(hw->wiphy),
395                        print_mac(mac, hw->wiphy->perm_addr));
396         }
397
398         hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
399         if (hwsim_mon == NULL)
400                 goto failed;
401
402         rtnl_lock();
403
404         err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
405         if (err < 0) {
406                 goto failed_mon;
407         }
408
409         err = register_netdevice(hwsim_mon);
410         if (err < 0)
411                 goto failed_mon;
412
413         rtnl_unlock();
414
415         return 0;
416
417 failed_mon:
418         rtnl_unlock();
419         free_netdev(hwsim_mon);
420
421 failed:
422         mac80211_hwsim_free();
423         return err;
424 }
425
426
427 static void __exit exit_mac80211_hwsim(void)
428 {
429         printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
430                hwsim_radio_count);
431
432         unregister_netdev(hwsim_mon);
433         mac80211_hwsim_free();
434 }
435
436
437 module_init(init_mac80211_hwsim);
438 module_exit(exit_mac80211_hwsim);