]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/mac80211_hwsim.c
mac80211: fix virtual interfaces vs. injection
[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         if (txi->control.vif)
262                 hwsim_check_magic(txi->control.vif);
263         if (txi->control.sta)
264                 hwsim_check_sta_magic(txi->control.sta);
265
266         memset(&txi->status, 0, sizeof(txi->status));
267         if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
268                 if (ack)
269                         txi->flags |= IEEE80211_TX_STAT_ACK;
270                 else
271                         txi->status.excessive_retries = 1;
272         }
273         ieee80211_tx_status_irqsafe(hw, skb);
274         return NETDEV_TX_OK;
275 }
276
277
278 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
279 {
280         struct mac80211_hwsim_data *data = hw->priv;
281         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
282         data->started = 1;
283         return 0;
284 }
285
286
287 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
288 {
289         struct mac80211_hwsim_data *data = hw->priv;
290         data->started = 0;
291         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
292 }
293
294
295 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
296                                         struct ieee80211_if_init_conf *conf)
297 {
298         DECLARE_MAC_BUF(mac);
299         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
300                wiphy_name(hw->wiphy), __func__, conf->type,
301                print_mac(mac, conf->mac_addr));
302         hwsim_set_magic(conf->vif);
303         return 0;
304 }
305
306
307 static void mac80211_hwsim_remove_interface(
308         struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
309 {
310         DECLARE_MAC_BUF(mac);
311         printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
312                wiphy_name(hw->wiphy), __func__, conf->type,
313                print_mac(mac, conf->mac_addr));
314         hwsim_check_magic(conf->vif);
315         hwsim_clear_magic(conf->vif);
316 }
317
318
319 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
320                                      struct ieee80211_vif *vif)
321 {
322         struct ieee80211_hw *hw = arg;
323         struct sk_buff *skb;
324         struct ieee80211_tx_info *info;
325
326         hwsim_check_magic(vif);
327
328         if (vif->type != NL80211_IFTYPE_AP)
329                 return;
330
331         skb = ieee80211_beacon_get(hw, vif);
332         if (skb == NULL)
333                 return;
334         info = IEEE80211_SKB_CB(skb);
335
336         mac80211_hwsim_monitor_rx(hw, skb);
337         mac80211_hwsim_tx_frame(hw, skb);
338         dev_kfree_skb(skb);
339 }
340
341
342 static void mac80211_hwsim_beacon(unsigned long arg)
343 {
344         struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
345         struct mac80211_hwsim_data *data = hw->priv;
346
347         if (!data->started || !data->radio_enabled)
348                 return;
349
350         ieee80211_iterate_active_interfaces_atomic(
351                 hw, mac80211_hwsim_beacon_tx, hw);
352
353         data->beacon_timer.expires = jiffies + data->beacon_int;
354         add_timer(&data->beacon_timer);
355 }
356
357
358 static int mac80211_hwsim_config(struct ieee80211_hw *hw,
359                                  struct ieee80211_conf *conf)
360 {
361         struct mac80211_hwsim_data *data = hw->priv;
362
363         printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
364                wiphy_name(hw->wiphy), __func__,
365                conf->channel->center_freq, conf->radio_enabled,
366                conf->beacon_int);
367
368         data->channel = conf->channel;
369         data->radio_enabled = conf->radio_enabled;
370         data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
371         if (data->beacon_int < 1)
372                 data->beacon_int = 1;
373
374         if (!data->started || !data->radio_enabled)
375                 del_timer(&data->beacon_timer);
376         else
377                 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
378
379         return 0;
380 }
381
382
383 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
384                                             unsigned int changed_flags,
385                                             unsigned int *total_flags,
386                                             int mc_count,
387                                             struct dev_addr_list *mc_list)
388 {
389         struct mac80211_hwsim_data *data = hw->priv;
390
391         printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
392
393         data->rx_filter = 0;
394         if (*total_flags & FIF_PROMISC_IN_BSS)
395                 data->rx_filter |= FIF_PROMISC_IN_BSS;
396         if (*total_flags & FIF_ALLMULTI)
397                 data->rx_filter |= FIF_ALLMULTI;
398
399         *total_flags = data->rx_filter;
400 }
401
402 static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
403                                            struct ieee80211_vif *vif,
404                                            struct ieee80211_if_conf *conf)
405 {
406         hwsim_check_magic(vif);
407         return 0;
408 }
409
410 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
411                                             struct ieee80211_vif *vif,
412                                             struct ieee80211_bss_conf *info,
413                                             u32 changed)
414 {
415         hwsim_check_magic(vif);
416 }
417
418 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
419                                       struct ieee80211_vif *vif,
420                                       enum sta_notify_cmd cmd,
421                                       struct ieee80211_sta *sta)
422 {
423         hwsim_check_magic(vif);
424         switch (cmd) {
425         case STA_NOTIFY_ADD:
426                 hwsim_set_sta_magic(sta);
427                 break;
428         case STA_NOTIFY_REMOVE:
429                 hwsim_clear_sta_magic(sta);
430                 break;
431         }
432 }
433
434 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
435                                   struct ieee80211_sta *sta,
436                                   bool set)
437 {
438         hwsim_check_sta_magic(sta);
439         return 0;
440 }
441
442 static const struct ieee80211_ops mac80211_hwsim_ops =
443 {
444         .tx = mac80211_hwsim_tx,
445         .start = mac80211_hwsim_start,
446         .stop = mac80211_hwsim_stop,
447         .add_interface = mac80211_hwsim_add_interface,
448         .remove_interface = mac80211_hwsim_remove_interface,
449         .config = mac80211_hwsim_config,
450         .configure_filter = mac80211_hwsim_configure_filter,
451         .config_interface = mac80211_hwsim_config_interface,
452         .bss_info_changed = mac80211_hwsim_bss_info_changed,
453         .sta_notify = mac80211_hwsim_sta_notify,
454         .set_tim = mac80211_hwsim_set_tim,
455 };
456
457
458 static void mac80211_hwsim_free(void)
459 {
460         int i;
461
462         for (i = 0; i < hwsim_radio_count; i++) {
463                 if (hwsim_radios[i]) {
464                         struct mac80211_hwsim_data *data;
465                         data = hwsim_radios[i]->priv;
466                         ieee80211_unregister_hw(hwsim_radios[i]);
467                         device_unregister(data->dev);
468                         ieee80211_free_hw(hwsim_radios[i]);
469                 }
470         }
471         kfree(hwsim_radios);
472         class_destroy(hwsim_class);
473 }
474
475
476 static struct device_driver mac80211_hwsim_driver = {
477         .name = "mac80211_hwsim"
478 };
479
480
481 static void hwsim_mon_setup(struct net_device *dev)
482 {
483         dev->hard_start_xmit = hwsim_mon_xmit;
484         dev->destructor = free_netdev;
485         ether_setup(dev);
486         dev->tx_queue_len = 0;
487         dev->type = ARPHRD_IEEE80211_RADIOTAP;
488         memset(dev->dev_addr, 0, ETH_ALEN);
489         dev->dev_addr[0] = 0x12;
490 }
491
492
493 static int __init init_mac80211_hwsim(void)
494 {
495         int i, err = 0;
496         u8 addr[ETH_ALEN];
497         struct mac80211_hwsim_data *data;
498         struct ieee80211_hw *hw;
499         DECLARE_MAC_BUF(mac);
500
501         if (radios < 1 || radios > 65535)
502                 return -EINVAL;
503
504         hwsim_radio_count = radios;
505         hwsim_radios = kcalloc(hwsim_radio_count,
506                                sizeof(struct ieee80211_hw *), GFP_KERNEL);
507         if (hwsim_radios == NULL)
508                 return -ENOMEM;
509
510         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
511         if (IS_ERR(hwsim_class)) {
512                 kfree(hwsim_radios);
513                 return PTR_ERR(hwsim_class);
514         }
515
516         memset(addr, 0, ETH_ALEN);
517         addr[0] = 0x02;
518
519         for (i = 0; i < hwsim_radio_count; i++) {
520                 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
521                        i);
522                 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
523                 if (hw == NULL) {
524                         printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
525                                "failed\n");
526                         err = -ENOMEM;
527                         goto failed;
528                 }
529                 hwsim_radios[i] = hw;
530
531                 data = hw->priv;
532                 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
533                                                 "hwsim%d", i);
534                 if (IS_ERR(data->dev)) {
535                         printk(KERN_DEBUG
536                                "mac80211_hwsim: device_create_drvdata "
537                                "failed (%ld)\n", PTR_ERR(data->dev));
538                         err = -ENOMEM;
539                         goto failed_drvdata;
540                 }
541                 data->dev->driver = &mac80211_hwsim_driver;
542
543                 SET_IEEE80211_DEV(hw, data->dev);
544                 addr[3] = i >> 8;
545                 addr[4] = i;
546                 SET_IEEE80211_PERM_ADDR(hw, addr);
547
548                 hw->channel_change_time = 1;
549                 hw->queues = 4;
550                 hw->wiphy->interface_modes =
551                         BIT(NL80211_IFTYPE_STATION) |
552                         BIT(NL80211_IFTYPE_AP);
553                 hw->ampdu_queues = 1;
554
555                 /* ask mac80211 to reserve space for magic */
556                 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
557                 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
558
559                 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
560                 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
561                 data->band.channels = data->channels;
562                 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
563                 data->band.bitrates = data->rates;
564                 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
565                 data->band.ht_info.ht_supported = 1;
566                 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
567                         IEEE80211_HT_CAP_GRN_FLD |
568                         IEEE80211_HT_CAP_SGI_40 |
569                         IEEE80211_HT_CAP_DSSSCCK40;
570                 data->band.ht_info.ampdu_factor = 0x3;
571                 data->band.ht_info.ampdu_density = 0x6;
572                 memset(data->band.ht_info.supp_mcs_set, 0,
573                        sizeof(data->band.ht_info.supp_mcs_set));
574                 data->band.ht_info.supp_mcs_set[0] = 0xff;
575                 data->band.ht_info.supp_mcs_set[1] = 0xff;
576                 data->band.ht_info.supp_mcs_set[12] =
577                         IEEE80211_HT_CAP_MCS_TX_DEFINED;
578                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
579
580                 err = ieee80211_register_hw(hw);
581                 if (err < 0) {
582                         printk(KERN_DEBUG "mac80211_hwsim: "
583                                "ieee80211_register_hw failed (%d)\n", err);
584                         goto failed_hw;
585                 }
586
587                 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
588                        wiphy_name(hw->wiphy),
589                        print_mac(mac, hw->wiphy->perm_addr));
590
591                 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
592                             (unsigned long) hw);
593         }
594
595         hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
596         if (hwsim_mon == NULL)
597                 goto failed;
598
599         rtnl_lock();
600
601         err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
602         if (err < 0)
603                 goto failed_mon;
604
605
606         err = register_netdevice(hwsim_mon);
607         if (err < 0)
608                 goto failed_mon;
609
610         rtnl_unlock();
611
612         return 0;
613
614 failed_mon:
615         rtnl_unlock();
616         free_netdev(hwsim_mon);
617         mac80211_hwsim_free();
618         return err;
619
620 failed_hw:
621         device_unregister(data->dev);
622 failed_drvdata:
623         ieee80211_free_hw(hw);
624         hwsim_radios[i] = NULL;
625 failed:
626         mac80211_hwsim_free();
627         return err;
628 }
629
630
631 static void __exit exit_mac80211_hwsim(void)
632 {
633         printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
634                hwsim_radio_count);
635
636         unregister_netdev(hwsim_mon);
637         mac80211_hwsim_free();
638 }
639
640
641 module_init(init_mac80211_hwsim);
642 module_exit(exit_mac80211_hwsim);