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