]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/mac80211_hwsim.c
da8aa83481f7bafd3d825ba24448103b6e77f7d8
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / 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  * - 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
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 struct hwsim_vif_priv {
32         u32 magic;
33 };
34
35 #define HWSIM_VIF_MAGIC 0x69537748
36
37 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
38 {
39         struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
40         WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
41 }
42
43 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
44 {
45         struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
46         vp->magic = HWSIM_VIF_MAGIC;
47 }
48
49 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
50 {
51         struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
52         vp->magic = 0;
53 }
54
55 struct hwsim_sta_priv {
56         u32 magic;
57 };
58
59 #define HWSIM_STA_MAGIC 0x6d537748
60
61 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
62 {
63         struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
64         WARN_ON(sp->magic != HWSIM_VIF_MAGIC);
65 }
66
67 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
68 {
69         struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
70         sp->magic = HWSIM_VIF_MAGIC;
71 }
72
73 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
74 {
75         struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
76         sp->magic = 0;
77 }
78
79 static struct class *hwsim_class;
80
81 static struct ieee80211_hw **hwsim_radios;
82 static int hwsim_radio_count;
83 static struct net_device *hwsim_mon; /* global monitor netdev */
84
85
86 static 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
103 static 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
118 struct mac80211_hwsim_data {
119         struct device *dev;
120         struct ieee80211_supported_band band;
121         struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
122         struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
123
124         struct ieee80211_channel *channel;
125         int radio_enabled;
126         unsigned long beacon_int; /* in jiffies unit */
127         unsigned int rx_filter;
128         int started;
129         struct timer_list beacon_timer;
130 };
131
132
133 struct hwsim_radiotap_hdr {
134         struct ieee80211_radiotap_header hdr;
135         u8 rt_flags;
136         u8 rt_rate;
137         __le16 rt_channel;
138         __le16 rt_chbitmask;
139 } __attribute__ ((packed));
140
141
142 static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
143 {
144         /* TODO: allow packet injection */
145         dev_kfree_skb(skb);
146         return 0;
147 }
148
149
150 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
151                                       struct sk_buff *tx_skb)
152 {
153         struct mac80211_hwsim_data *data = hw->priv;
154         struct sk_buff *skb;
155         struct hwsim_radiotap_hdr *hdr;
156         u16 flags;
157         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
158         struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
159
160         if (!netif_running(hwsim_mon))
161                 return;
162
163         skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
164         if (skb == NULL)
165                 return;
166
167         hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
168         hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
169         hdr->hdr.it_pad = 0;
170         hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
171         hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
172                                           (1 << IEEE80211_RADIOTAP_RATE) |
173                                           (1 << IEEE80211_RADIOTAP_CHANNEL));
174         hdr->rt_flags = 0;
175         hdr->rt_rate = txrate->bitrate / 5;
176         hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
177         flags = IEEE80211_CHAN_2GHZ;
178         if (txrate->flags & IEEE80211_RATE_ERP_G)
179                 flags |= IEEE80211_CHAN_OFDM;
180         else
181                 flags |= IEEE80211_CHAN_CCK;
182         hdr->rt_chbitmask = cpu_to_le16(flags);
183
184         skb->dev = hwsim_mon;
185         skb_set_mac_header(skb, 0);
186         skb->ip_summed = CHECKSUM_UNNECESSARY;
187         skb->pkt_type = PACKET_OTHERHOST;
188         skb->protocol = htons(ETH_P_802_2);
189         memset(skb->cb, 0, sizeof(skb->cb));
190         netif_rx(skb);
191 }
192
193
194 static int mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
195                                    struct sk_buff *skb)
196 {
197         struct mac80211_hwsim_data *data = hw->priv;
198         int i, ack = 0;
199         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
200         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
201         struct ieee80211_rx_status rx_status;
202
203         memset(&rx_status, 0, sizeof(rx_status));
204         /* TODO: set mactime */
205         rx_status.freq = data->channel->center_freq;
206         rx_status.band = data->channel->band;
207         rx_status.rate_idx = info->tx_rate_idx;
208         /* TODO: simulate signal strength (and optional packet drop) */
209
210         /* Copy skb to all enabled radios that are on the current frequency */
211         for (i = 0; i < hwsim_radio_count; i++) {
212                 struct mac80211_hwsim_data *data2;
213                 struct sk_buff *nskb;
214
215                 if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
216                         continue;
217                 data2 = hwsim_radios[i]->priv;
218                 if (!data2->started || !data2->radio_enabled ||
219                     data->channel->center_freq != data2->channel->center_freq)
220                         continue;
221
222                 nskb = skb_copy(skb, GFP_ATOMIC);
223                 if (nskb == NULL)
224                         continue;
225
226                 if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
227                            ETH_ALEN) == 0)
228                         ack = 1;
229                 ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
230         }
231
232         return ack;
233 }
234
235
236 static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
237 {
238         struct mac80211_hwsim_data *data = hw->priv;
239         int ack;
240         struct ieee80211_tx_info *txi;
241
242         mac80211_hwsim_monitor_rx(hw, skb);
243
244         if (skb->len < 10) {
245                 /* Should not happen; just a sanity check for addr1 use */
246                 dev_kfree_skb(skb);
247                 return NETDEV_TX_OK;
248         }
249
250         if (!data->radio_enabled) {
251                 printk(KERN_DEBUG "%s: dropped TX frame since radio "
252                        "disabled\n", wiphy_name(hw->wiphy));
253                 dev_kfree_skb(skb);
254                 return NETDEV_TX_OK;
255         }
256
257         ack = mac80211_hwsim_tx_frame(hw, skb);
258
259         txi = IEEE80211_SKB_CB(skb);
260
261         hwsim_check_magic(txi->control.vif);
262         if (txi->control.sta)
263                 hwsim_check_sta_magic(txi->control.sta);
264
265         memset(&txi->status, 0, sizeof(txi->status));
266         if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
267                 if (ack)
268                         txi->flags |= IEEE80211_TX_STAT_ACK;
269                 else
270                         txi->status.excessive_retries = 1;
271         }
272         ieee80211_tx_status_irqsafe(hw, skb);
273         return NETDEV_TX_OK;
274 }
275
276
277 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
278 {
279         struct mac80211_hwsim_data *data = hw->priv;
280         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
281         data->started = 1;
282         return 0;
283 }
284
285
286 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
287 {
288         struct mac80211_hwsim_data *data = hw->priv;
289         data->started = 0;
290         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
291 }
292
293
294 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
295                                         struct ieee80211_if_init_conf *conf)
296 {
297         DECLARE_MAC_BUF(mac);
298         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
299                wiphy_name(hw->wiphy), __func__, conf->type,
300                print_mac(mac, conf->mac_addr));
301         hwsim_set_magic(conf->vif);
302         return 0;
303 }
304
305
306 static void mac80211_hwsim_remove_interface(
307         struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
308 {
309         DECLARE_MAC_BUF(mac);
310         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
311                wiphy_name(hw->wiphy), __func__, conf->type,
312                print_mac(mac, conf->mac_addr));
313         hwsim_check_magic(conf->vif);
314         hwsim_clear_magic(conf->vif);
315 }
316
317
318 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
319                                      struct ieee80211_vif *vif)
320 {
321         struct ieee80211_hw *hw = arg;
322         struct sk_buff *skb;
323         struct ieee80211_tx_info *info;
324
325         hwsim_check_magic(vif);
326
327         if (vif->type != NL80211_IFTYPE_AP)
328                 return;
329
330         skb = ieee80211_beacon_get(hw, vif);
331         if (skb == NULL)
332                 return;
333         info = IEEE80211_SKB_CB(skb);
334
335         mac80211_hwsim_monitor_rx(hw, skb);
336         mac80211_hwsim_tx_frame(hw, skb);
337         dev_kfree_skb(skb);
338 }
339
340
341 static void mac80211_hwsim_beacon(unsigned long arg)
342 {
343         struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
344         struct mac80211_hwsim_data *data = hw->priv;
345
346         if (!data->started || !data->radio_enabled)
347                 return;
348
349         ieee80211_iterate_active_interfaces_atomic(
350                 hw, mac80211_hwsim_beacon_tx, hw);
351
352         data->beacon_timer.expires = jiffies + data->beacon_int;
353         add_timer(&data->beacon_timer);
354 }
355
356
357 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
358                                  struct ieee80211_conf *conf)
359 {
360         struct mac80211_hwsim_data *data = hw->priv;
361
362         printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
363                wiphy_name(hw->wiphy), __func__,
364                conf->channel->center_freq, conf->radio_enabled,
365                conf->beacon_int);
366
367         data->channel = conf->channel;
368         data->radio_enabled = conf->radio_enabled;
369         data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
370         if (data->beacon_int < 1)
371                 data->beacon_int = 1;
372
373         if (!data->started || !data->radio_enabled)
374                 del_timer(&data->beacon_timer);
375         else
376                 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
377
378         return 0;
379 }
380
381
382 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
383                                             unsigned int changed_flags,
384                                             unsigned int *total_flags,
385                                             int mc_count,
386                                             struct dev_addr_list *mc_list)
387 {
388         struct mac80211_hwsim_data *data = hw->priv;
389
390         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
391
392         data->rx_filter = 0;
393         if (*total_flags & FIF_PROMISC_IN_BSS)
394                 data->rx_filter |= FIF_PROMISC_IN_BSS;
395         if (*total_flags & FIF_ALLMULTI)
396                 data->rx_filter |= FIF_ALLMULTI;
397
398         *total_flags = data->rx_filter;
399 }
400
401 static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
402                                            struct ieee80211_vif *vif,
403                                            struct ieee80211_if_conf *conf)
404 {
405         hwsim_check_magic(vif);
406         return 0;
407 }
408
409 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
410                                             struct ieee80211_vif *vif,
411                                             struct ieee80211_bss_conf *info,
412                                             u32 changed)
413 {
414         hwsim_check_magic(vif);
415 }
416
417 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
418                                       struct ieee80211_vif *vif,
419                                       enum sta_notify_cmd cmd,
420                                       struct ieee80211_sta *sta)
421 {
422         hwsim_check_magic(vif);
423         switch (cmd) {
424         case STA_NOTIFY_ADD:
425                 hwsim_set_sta_magic(sta);
426                 break;
427         case STA_NOTIFY_REMOVE:
428                 hwsim_clear_sta_magic(sta);
429                 break;
430         }
431 }
432
433 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
434                                   struct ieee80211_sta *sta,
435                                   bool set)
436 {
437         hwsim_check_sta_magic(sta);
438         return 0;
439 }
440
441 static const struct ieee80211_ops mac80211_hwsim_ops =
442 {
443         .tx = mac80211_hwsim_tx,
444         .start = mac80211_hwsim_start,
445         .stop = mac80211_hwsim_stop,
446         .add_interface = mac80211_hwsim_add_interface,
447         .remove_interface = mac80211_hwsim_remove_interface,
448         .config = mac80211_hwsim_config,
449         .configure_filter = mac80211_hwsim_configure_filter,
450         .config_interface = mac80211_hwsim_config_interface,
451         .bss_info_changed = mac80211_hwsim_bss_info_changed,
452         .sta_notify = mac80211_hwsim_sta_notify,
453         .set_tim = mac80211_hwsim_set_tim,
454 };
455
456
457 static void mac80211_hwsim_free(void)
458 {
459         int i;
460
461         for (i = 0; i < hwsim_radio_count; i++) {
462                 if (hwsim_radios[i]) {
463                         struct mac80211_hwsim_data *data;
464                         data = hwsim_radios[i]->priv;
465                         ieee80211_unregister_hw(hwsim_radios[i]);
466                         device_unregister(data->dev);
467                         ieee80211_free_hw(hwsim_radios[i]);
468                 }
469         }
470         kfree(hwsim_radios);
471         class_destroy(hwsim_class);
472 }
473
474
475 static struct device_driver mac80211_hwsim_driver = {
476         .name = "mac80211_hwsim"
477 };
478
479
480 static void hwsim_mon_setup(struct net_device *dev)
481 {
482         dev->hard_start_xmit = hwsim_mon_xmit;
483         dev->destructor = free_netdev;
484         ether_setup(dev);
485         dev->tx_queue_len = 0;
486         dev->type = ARPHRD_IEEE80211_RADIOTAP;
487         memset(dev->dev_addr, 0, ETH_ALEN);
488         dev->dev_addr[0] = 0x12;
489 }
490
491
492 static int __init init_mac80211_hwsim(void)
493 {
494         int i, err = 0;
495         u8 addr[ETH_ALEN];
496         struct mac80211_hwsim_data *data;
497         struct ieee80211_hw *hw;
498         DECLARE_MAC_BUF(mac);
499
500         if (radios < 1 || radios > 65535)
501                 return -EINVAL;
502
503         hwsim_radio_count = radios;
504         hwsim_radios = kcalloc(hwsim_radio_count,
505                                sizeof(struct ieee80211_hw *), GFP_KERNEL);
506         if (hwsim_radios == NULL)
507                 return -ENOMEM;
508
509         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
510         if (IS_ERR(hwsim_class)) {
511                 kfree(hwsim_radios);
512                 return PTR_ERR(hwsim_class);
513         }
514
515         memset(addr, 0, ETH_ALEN);
516         addr[0] = 0x02;
517
518         for (i = 0; i < hwsim_radio_count; i++) {
519                 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
520                        i);
521                 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
522                 if (hw == NULL) {
523                         printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
524                                "failed\n");
525                         err = -ENOMEM;
526                         goto failed;
527                 }
528                 hwsim_radios[i] = hw;
529
530                 data = hw->priv;
531                 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
532                                                 "hwsim%d", i);
533                 if (IS_ERR(data->dev)) {
534                         printk(KERN_DEBUG
535                                "mac80211_hwsim: device_create_drvdata "
536                                "failed (%ld)\n", PTR_ERR(data->dev));
537                         err = -ENOMEM;
538                         goto failed_drvdata;
539                 }
540                 data->dev->driver = &mac80211_hwsim_driver;
541
542                 SET_IEEE80211_DEV(hw, data->dev);
543                 addr[3] = i >> 8;
544                 addr[4] = i;
545                 SET_IEEE80211_PERM_ADDR(hw, addr);
546
547                 hw->channel_change_time = 1;
548                 hw->queues = 4;
549                 hw->wiphy->interface_modes =
550                         BIT(NL80211_IFTYPE_STATION) |
551                         BIT(NL80211_IFTYPE_AP);
552                 hw->ampdu_queues = 1;
553
554                 /* ask mac80211 to reserve space for magic */
555                 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
556                 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
557
558                 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
559                 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
560                 data->band.channels = data->channels;
561                 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
562                 data->band.bitrates = data->rates;
563                 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
564                 data->band.ht_info.ht_supported = 1;
565                 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
566                         IEEE80211_HT_CAP_GRN_FLD |
567                         IEEE80211_HT_CAP_SGI_40 |
568                         IEEE80211_HT_CAP_DSSSCCK40;
569                 data->band.ht_info.ampdu_factor = 0x3;
570                 data->band.ht_info.ampdu_density = 0x6;
571                 memset(data->band.ht_info.supp_mcs_set, 0,
572                        sizeof(data->band.ht_info.supp_mcs_set));
573                 data->band.ht_info.supp_mcs_set[0] = 0xff;
574                 data->band.ht_info.supp_mcs_set[1] = 0xff;
575                 data->band.ht_info.supp_mcs_set[12] =
576                         IEEE80211_HT_CAP_MCS_TX_DEFINED;
577                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
578
579                 err = ieee80211_register_hw(hw);
580                 if (err < 0) {
581                         printk(KERN_DEBUG "mac80211_hwsim: "
582                                "ieee80211_register_hw failed (%d)\n", err);
583                         goto failed_hw;
584                 }
585
586                 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
587                        wiphy_name(hw->wiphy),
588                        print_mac(mac, hw->wiphy->perm_addr));
589
590                 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
591                             (unsigned long) hw);
592         }
593
594         hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
595         if (hwsim_mon == NULL)
596                 goto failed;
597
598         rtnl_lock();
599
600         err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
601         if (err < 0)
602                 goto failed_mon;
603
604
605         err = register_netdevice(hwsim_mon);
606         if (err < 0)
607                 goto failed_mon;
608
609         rtnl_unlock();
610
611         return 0;
612
613 failed_mon:
614         rtnl_unlock();
615         free_netdev(hwsim_mon);
616         mac80211_hwsim_free();
617         return err;
618
619 failed_hw:
620         device_unregister(data->dev);
621 failed_drvdata:
622         ieee80211_free_hw(hw);
623         hwsim_radios[i] = NULL;
624 failed:
625         mac80211_hwsim_free();
626         return err;
627 }
628
629
630 static void __exit exit_mac80211_hwsim(void)
631 {
632         printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
633                hwsim_radio_count);
634
635         unregister_netdev(hwsim_mon);
636         mac80211_hwsim_free();
637 }
638
639
640 module_init(init_mac80211_hwsim);
641 module_exit(exit_mac80211_hwsim);