Added support for AP mode Beacon transmission
[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 competition 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 #include <linux/etherdevice.h>
22
23 MODULE_AUTHOR("Jouni Malinen");
24 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
25 MODULE_LICENSE("GPL");
26
27 static int radios = 2;
28 module_param(radios, int, 0444);
29 MODULE_PARM_DESC(radios, "Number of simulated radios");
30
31
32 static struct class *hwsim_class;
33
34 static struct ieee80211_hw **hwsim_radios;
35 static int hwsim_radio_count;
36 static struct net_device *hwsim_mon; /* global monitor netdev */
37
38
39 static const struct ieee80211_channel hwsim_channels[] = {
40         { .chan = 1, .freq = 2412, .val = 1 },
41         { .chan = 2, .freq = 2417, .val = 2 },
42         { .chan = 3, .freq = 2422, .val = 3 },
43         { .chan = 4, .freq = 2427, .val = 4 },
44         { .chan = 5, .freq = 2432, .val = 5 },
45         { .chan = 6, .freq = 2437, .val = 6 },
46         { .chan = 7, .freq = 2442, .val = 7 },
47         { .chan = 8, .freq = 2447, .val = 8 },
48         { .chan = 9, .freq = 2452, .val = 9 },
49         { .chan = 10, .freq = 2457, .val = 10 },
50         { .chan = 11, .freq = 2462, .val = 11 },
51         { .chan = 12, .freq = 2467, .val = 12 },
52         { .chan = 13, .freq = 2472, .val = 13 },
53         { .chan = 14, .freq = 2484, .val = 14 },
54 };
55
56 static const struct ieee80211_rate hwsim_rates[] = {
57         { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
58         { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
59         { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
60         { .rate = 110, .val = 110, .val2 = 111,
61           .flags = IEEE80211_RATE_CCK_2 },
62         { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
63         { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
64         { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
65         { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
66         { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
67         { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
68         { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
69         { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
70 };
71
72 struct mac80211_hwsim_data {
73         struct device *dev;
74         struct ieee80211_hw_mode modes[1];
75         struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
76         struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
77
78         int freq;
79         int channel;
80         enum ieee80211_phymode phymode;
81         int radio_enabled;
82         unsigned long beacon_int; /* in jiffies unit */
83         unsigned int rx_filter;
84         int started;
85         struct timer_list beacon_timer;
86 };
87
88
89 struct hwsim_radiotap_hdr {
90         struct ieee80211_radiotap_header hdr;
91         u8 rt_flags;
92         u8 rt_rate;
93         __le16 rt_channel;
94         __le16 rt_chbitmask;
95 } __attribute__ ((packed));
96
97
98 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
99 {
100         /* TODO: allow packet injection */
101         dev_kfree_skb(skb);
102         return 0;
103 }
104
105
106 static void mac80211_hwsim_monitor_rx(struct mac80211_hwsim_data *data,
107                                       struct sk_buff *tx_skb,
108                                       struct ieee80211_tx_control *control)
109 {
110         struct sk_buff *skb;
111         struct hwsim_radiotap_hdr *hdr;
112         u16 flags;
113
114         if (!netif_running(hwsim_mon))
115                 return;
116
117         skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
118         if (skb == NULL)
119                 return;
120
121         hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
122         hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
123         hdr->hdr.it_pad = 0;
124         hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
125         hdr->hdr.it_present = __constant_cpu_to_le32(
126              (1 << IEEE80211_RADIOTAP_FLAGS) |
127              (1 << IEEE80211_RADIOTAP_RATE) |
128              (1 << IEEE80211_RADIOTAP_CHANNEL));
129         hdr->rt_flags = 0;
130         hdr->rt_rate = control->tx_rate / 5;
131         hdr->rt_channel = data->freq;
132         flags = IEEE80211_CHAN_2GHZ;
133         if (control->rate->flags & IEEE80211_RATE_OFDM)
134                 flags |= IEEE80211_CHAN_OFDM;
135         if (control->rate->flags & IEEE80211_RATE_CCK)
136                 flags |= IEEE80211_CHAN_CCK;
137         hdr->rt_chbitmask = cpu_to_le16(flags);
138
139         skb->dev = hwsim_mon;
140         skb_set_mac_header(skb, 0);
141         skb->ip_summed = CHECKSUM_UNNECESSARY;
142         skb->pkt_type = PACKET_OTHERHOST;
143         skb->protocol = __constant_htons(ETH_P_802_2);
144         memset(skb->cb, 0, sizeof(skb->cb));
145         netif_rx(skb);
146 }
147
148
149 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
150                              struct ieee80211_tx_control *control)
151 {
152         struct mac80211_hwsim_data *data = hw->priv;
153         struct ieee80211_tx_status tx_status;
154         struct ieee80211_rx_status rx_status;
155         int i, ack = 0;
156         struct ieee80211_hdr *hdr;
157
158         mac80211_hwsim_monitor_rx(data, skb, control);
159
160         if (skb->len < 10) {
161                 /* Should not happen; just a sanity check for addr1 use */
162                 dev_kfree_skb(skb);
163                 return NETDEV_TX_OK;
164         }
165
166         if (!data->radio_enabled) {
167                 printk(KERN_DEBUG "%s: dropped TX frame since radio "
168                        "disabled\n", wiphy_name(hw->wiphy));
169                 dev_kfree_skb(skb);
170                 return NETDEV_TX_OK;
171         }
172
173         hdr = (struct ieee80211_hdr *) skb->data;
174         if (is_multicast_ether_addr(hdr->addr1))
175                 ack = 1;
176
177         memset(&rx_status, 0, sizeof(rx_status));
178         /* TODO: set mactime */
179         rx_status.freq = data->freq;
180         rx_status.channel = data->channel;
181         rx_status.phymode = data->phymode;
182         rx_status.rate = control->tx_rate;
183         /* TODO: simulate signal strength (and optional packet drop) */
184
185         /* Copy skb to all enabled radios that are on the current frequency */
186         for (i = 0; i < hwsim_radio_count; i++) {
187                 struct mac80211_hwsim_data *data2;
188                 struct sk_buff *nskb;
189
190                 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
191                         continue;
192                 data2 = hwsim_radios[i]->priv;
193                 if (!data2->started || !data2->radio_enabled ||
194                     data->freq != data2->freq)
195                         continue;
196
197                 nskb = skb_copy(skb, GFP_ATOMIC);
198                 if (nskb == NULL)
199                         continue;
200
201                 if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
202                            ETH_ALEN) == 0)
203                         ack = 1;
204                 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
205         }
206
207         memset(&tx_status, 0, sizeof(tx_status));
208         memcpy(&tx_status.control, control, sizeof(*control));
209         tx_status.flags = ack ? IEEE80211_TX_STATUS_ACK : 0;
210         ieee80211_tx_status_irqsafe(hw, skb, &tx_status);
211         return NETDEV_TX_OK;
212 }
213
214
215 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
216 {
217         struct mac80211_hwsim_data *data = hw->priv;
218         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
219         data->started = 1;
220         return 0;
221 }
222
223
224 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
225 {
226         struct mac80211_hwsim_data *data = hw->priv;
227         data->started = 0;
228         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
229 }
230
231
232 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
233                                         struct ieee80211_if_init_conf *conf)
234 {
235         DECLARE_MAC_BUF(mac);
236         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
237                wiphy_name(hw->wiphy), __func__, conf->type,
238                print_mac(mac, conf->mac_addr));
239         return 0;
240 }
241
242
243 static void mac80211_hwsim_remove_interface(
244         struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
245 {
246         DECLARE_MAC_BUF(mac);
247         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
248                wiphy_name(hw->wiphy), __func__, conf->type,
249                print_mac(mac, conf->mac_addr));
250 }
251
252
253 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
254                                      struct ieee80211_vif *vif)
255 {
256         struct ieee80211_hw *hw = arg;
257         struct mac80211_hwsim_data *data = hw->priv;
258         struct ieee80211_tx_control control;
259         struct sk_buff *skb;
260         struct ieee80211_rx_status rx_status;
261         int i;
262
263         if (vif->type != IEEE80211_IF_TYPE_AP)
264                 return;
265
266         skb = ieee80211_beacon_get(hw, vif, &control);
267         if (skb == NULL)
268                 return;
269
270         mac80211_hwsim_monitor_rx(data, skb, &control);
271
272         memset(&rx_status, 0, sizeof(rx_status));
273         /* TODO: set mactime */
274         rx_status.freq = data->freq;
275         rx_status.channel = data->channel;
276         rx_status.phymode = data->phymode;
277         rx_status.rate = control.tx_rate;
278         /* TODO: simulate signal strength (and optional packet drop) */
279
280         /* Copy skb to all enabled radios that are on the current frequency */
281         for (i = 0; i < hwsim_radio_count; i++) {
282                 struct mac80211_hwsim_data *data2;
283                 struct sk_buff *nskb;
284
285                 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
286                         continue;
287                 data2 = hwsim_radios[i]->priv;
288                 if (!data2->started || !data2->radio_enabled ||
289                     data->freq != data2->freq)
290                         continue;
291
292                 nskb = skb_copy(skb, GFP_ATOMIC);
293                 if (nskb == NULL)
294                         continue;
295
296                 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
297         }
298
299         dev_kfree_skb(skb);
300 }
301
302
303 static void mac80211_hwsim_beacon(unsigned long arg)
304 {
305         struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
306         struct mac80211_hwsim_data *data = hw->priv;
307
308         if (!data->started || !data->radio_enabled)
309                 return;
310
311         ieee80211_iterate_active_interfaces(hw, mac80211_hwsim_beacon_tx, hw);
312
313         data->beacon_timer.expires = jiffies + data->beacon_int;
314         add_timer(&data->beacon_timer);
315 }
316
317
318 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
319                                  struct ieee80211_conf *conf)
320 {
321         struct mac80211_hwsim_data *data = hw->priv;
322
323         printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
324                wiphy_name(hw->wiphy), __func__,
325                conf->freq, conf->radio_enabled, conf->beacon_int);
326
327         data->freq = conf->freq;
328         data->channel = conf->channel;
329         data->phymode = conf->phymode;
330         data->radio_enabled = conf->radio_enabled;
331         data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
332         if (data->beacon_int < 1)
333                 data->beacon_int = 1;
334
335         if (!data->started || !data->radio_enabled)
336                 del_timer(&data->beacon_timer);
337         else
338                 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
339
340         return 0;
341 }
342
343
344 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
345                                             unsigned int changed_flags,
346                                             unsigned int *total_flags,
347                                             int mc_count,
348                                             struct dev_addr_list *mc_list)
349 {
350         struct mac80211_hwsim_data *data = hw->priv;
351
352         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
353
354         data->rx_filter = 0;
355         if (*total_flags & FIF_PROMISC_IN_BSS)
356                 data->rx_filter |= FIF_PROMISC_IN_BSS;
357         if (*total_flags & FIF_ALLMULTI)
358                 data->rx_filter |= FIF_ALLMULTI;
359
360         *total_flags = data->rx_filter;
361 }
362
363
364
365 static const struct ieee80211_ops mac80211_hwsim_ops =
366 {
367         .tx = mac80211_hwsim_tx,
368         .start = mac80211_hwsim_start,
369         .stop = mac80211_hwsim_stop,
370         .add_interface = mac80211_hwsim_add_interface,
371         .remove_interface = mac80211_hwsim_remove_interface,
372         .config = mac80211_hwsim_config,
373         .configure_filter = mac80211_hwsim_configure_filter,
374 };
375
376
377 static void mac80211_hwsim_free(void)
378 {
379         int i;
380
381         for (i = 0; i < hwsim_radio_count; i++) {
382                 if (hwsim_radios[i]) {
383                         struct mac80211_hwsim_data *data;
384                         data = hwsim_radios[i]->priv;
385                         ieee80211_unregister_hw(hwsim_radios[i]);
386                         if (!IS_ERR(data->dev))
387                                 device_unregister(data->dev);
388                         ieee80211_free_hw(hwsim_radios[i]);
389                 }
390         }
391         kfree(hwsim_radios);
392         class_destroy(hwsim_class);
393 }
394
395
396 static struct device_driver mac80211_hwsim_driver = {
397         .name = "mac80211_hwsim"
398 };
399
400
401 static void hwsim_mon_setup(struct net_device *dev)
402 {
403         dev->hard_start_xmit = hwsim_mon_xmit;
404         dev->destructor = free_netdev;
405         ether_setup(dev);
406         dev->tx_queue_len = 0;
407         dev->type = ARPHRD_IEEE80211_RADIOTAP;
408         memset(dev->dev_addr, 0, ETH_ALEN);
409         dev->dev_addr[0] = 0x12;
410 }
411
412
413 static int __init init_mac80211_hwsim(void)
414 {
415         int i, err = 0;
416         u8 addr[ETH_ALEN];
417         struct mac80211_hwsim_data *data;
418         struct ieee80211_hw *hw;
419         DECLARE_MAC_BUF(mac);
420
421         if (radios < 1 || radios > 65535)
422                 return -EINVAL;
423
424         hwsim_radio_count = radios;
425         hwsim_radios = kcalloc(hwsim_radio_count,
426                                sizeof(struct ieee80211_hw *), GFP_KERNEL);
427         if (hwsim_radios == NULL)
428                 return -ENOMEM;
429
430         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
431         if (IS_ERR(hwsim_class)) {
432                 kfree(hwsim_radios);
433                 return PTR_ERR(hwsim_class);
434         }
435
436         memset(addr, 0, ETH_ALEN);
437         addr[0] = 0x02;
438
439         for (i = 0; i < hwsim_radio_count; i++) {
440                 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
441                        i);
442                 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
443                 if (hw == NULL) {
444                         printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
445                                "failed\n");
446                         err = -ENOMEM;
447                         goto failed;
448                 }
449                 hwsim_radios[i] = hw;
450
451                 data = hw->priv;
452                 data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
453                 if (IS_ERR(data->dev)) {
454                         printk(KERN_DEBUG "mac80211_hwsim: device_create "
455                                "failed (%ld)\n", PTR_ERR(data->dev));
456                         err = -ENOMEM;
457                         goto failed;
458                 }
459                 data->dev->driver = &mac80211_hwsim_driver;
460                 dev_set_drvdata(data->dev, hw);
461
462                 SET_IEEE80211_DEV(hw, data->dev);
463                 addr[3] = i >> 8;
464                 addr[4] = i;
465                 SET_IEEE80211_PERM_ADDR(hw, addr);
466
467                 hw->channel_change_time = 1;
468                 hw->queues = 1;
469
470                 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
471                 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
472                 data->modes[0].channels = data->channels;
473                 data->modes[0].rates = data->rates;
474                 data->modes[0].mode = MODE_IEEE80211G;
475                 data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
476                 data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
477
478                 err = ieee80211_register_hwmode(hw, data->modes);
479                 if (err < 0) {
480                         printk(KERN_DEBUG "mac80211_hwsim: "
481                                "ieee80211_register_hwmode failed (%d)\n", err);
482                         goto failed;
483                 }
484
485                 err = ieee80211_register_hw(hw);
486                 if (err < 0) {
487                         printk(KERN_DEBUG "mac80211_hwsim: "
488                                "ieee80211_register_hw failed (%d)\n", err);
489                         goto failed;
490                 }
491
492                 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
493                        wiphy_name(hw->wiphy),
494                        print_mac(mac, hw->wiphy->perm_addr));
495
496                 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
497                             (unsigned long) hw);
498         }
499
500         hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
501         if (hwsim_mon == NULL)
502                 goto failed;
503
504         rtnl_lock();
505
506         err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
507         if (err < 0) {
508                 goto failed_mon;
509         }
510
511         err = register_netdevice(hwsim_mon);
512         if (err < 0)
513                 goto failed_mon;
514
515         rtnl_unlock();
516
517         return 0;
518
519 failed_mon:
520         rtnl_unlock();
521         free_netdev(hwsim_mon);
522
523 failed:
524         mac80211_hwsim_free();
525         return err;
526 }
527
528
529 static void __exit exit_mac80211_hwsim(void)
530 {
531         printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
532                hwsim_radio_count);
533
534         unregister_netdev(hwsim_mon);
535         mac80211_hwsim_free();
536 }
537
538
539 module_init(init_mac80211_hwsim);
540 module_exit(exit_mac80211_hwsim);