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