mac80211 hwsim: verify vif pointers
[deliverable/linux.git] / drivers / net / wireless / mac80211_hwsim.c
CommitLineData
acc1e7a3
JM
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
23MODULE_AUTHOR("Jouni Malinen");
24MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
25MODULE_LICENSE("GPL");
26
27static int radios = 2;
28module_param(radios, int, 0444);
29MODULE_PARM_DESC(radios, "Number of simulated radios");
30
8aa21e6f
JB
31struct hwsim_vif_priv {
32 u32 magic;
33};
34
35#define HWSIM_VIF_MAGIC 0x69537748
36
37static 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
43static 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
49static 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}
acc1e7a3
JM
54
55static struct class *hwsim_class;
56
57static struct ieee80211_hw **hwsim_radios;
58static int hwsim_radio_count;
59static struct net_device *hwsim_mon; /* global monitor netdev */
60
61
62static 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
79static 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
94struct 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
109struct 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
118static 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
126static 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));
f248f105
JM
147 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
148 (1 << IEEE80211_RADIOTAP_RATE) |
149 (1 << IEEE80211_RADIOTAP_CHANNEL));
acc1e7a3
JM
150 hdr->rt_flags = 0;
151 hdr->rt_rate = txrate->bitrate / 5;
df70b4ac 152 hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
acc1e7a3
JM
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;
f248f105 164 skb->protocol = htons(ETH_P_802_2);
acc1e7a3
JM
165 memset(skb->cb, 0, sizeof(skb->cb));
166 netif_rx(skb);
167}
168
169
e36cfdc9
JM
170static int mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
171 struct sk_buff *skb)
acc1e7a3
JM
172{
173 struct mac80211_hwsim_data *data = hw->priv;
acc1e7a3 174 int i, ack = 0;
e36cfdc9 175 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
acc1e7a3 176 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e36cfdc9 177 struct ieee80211_rx_status rx_status;
acc1e7a3
JM
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
e36cfdc9
JM
208 return ack;
209}
210
211
212static 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
acc1e7a3 235 txi = IEEE80211_SKB_CB(skb);
8aa21e6f
JB
236
237 hwsim_check_magic(txi->control.vif);
238
acc1e7a3
JM
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
251static 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
260static 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
268static 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));
8aa21e6f 275 hwsim_set_magic(conf->vif);
acc1e7a3
JM
276 return 0;
277}
278
279
280static 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));
8aa21e6f
JB
287 hwsim_check_magic(conf->vif);
288 hwsim_clear_magic(conf->vif);
acc1e7a3
JM
289}
290
291
292static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
293 struct ieee80211_vif *vif)
294{
295 struct ieee80211_hw *hw = arg;
acc1e7a3 296 struct sk_buff *skb;
acc1e7a3
JM
297 struct ieee80211_tx_info *info;
298
8aa21e6f
JB
299 hwsim_check_magic(vif);
300
05c914fe 301 if (vif->type != NL80211_IFTYPE_AP)
acc1e7a3
JM
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);
e36cfdc9 310 mac80211_hwsim_tx_frame(hw, skb);
acc1e7a3
JM
311 dev_kfree_skb(skb);
312}
313
314
315static 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
f248f105
JM
323 ieee80211_iterate_active_interfaces_atomic(
324 hw, mac80211_hwsim_beacon_tx, hw);
acc1e7a3
JM
325
326 data->beacon_timer.expires = jiffies + data->beacon_int;
327 add_timer(&data->beacon_timer);
328}
329
330
331static 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
356static 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
8aa21e6f
JB
375static 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}
acc1e7a3 382
8aa21e6f
JB
383static 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
391static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
392 struct ieee80211_vif *vif,
393 enum sta_notify_cmd cmd, const u8 *addr)
394{
395 hwsim_check_magic(vif);
396}
acc1e7a3
JM
397
398static const struct ieee80211_ops mac80211_hwsim_ops =
399{
400 .tx = mac80211_hwsim_tx,
401 .start = mac80211_hwsim_start,
402 .stop = mac80211_hwsim_stop,
403 .add_interface = mac80211_hwsim_add_interface,
404 .remove_interface = mac80211_hwsim_remove_interface,
405 .config = mac80211_hwsim_config,
406 .configure_filter = mac80211_hwsim_configure_filter,
8aa21e6f
JB
407 .config_interface = mac80211_hwsim_config_interface,
408 .bss_info_changed = mac80211_hwsim_bss_info_changed,
409 .sta_notify = mac80211_hwsim_sta_notify,
acc1e7a3
JM
410};
411
412
413static void mac80211_hwsim_free(void)
414{
415 int i;
416
417 for (i = 0; i < hwsim_radio_count; i++) {
418 if (hwsim_radios[i]) {
419 struct mac80211_hwsim_data *data;
420 data = hwsim_radios[i]->priv;
421 ieee80211_unregister_hw(hwsim_radios[i]);
3a33cc10 422 device_unregister(data->dev);
acc1e7a3
JM
423 ieee80211_free_hw(hwsim_radios[i]);
424 }
425 }
426 kfree(hwsim_radios);
427 class_destroy(hwsim_class);
428}
429
430
431static struct device_driver mac80211_hwsim_driver = {
432 .name = "mac80211_hwsim"
433};
434
435
436static void hwsim_mon_setup(struct net_device *dev)
437{
438 dev->hard_start_xmit = hwsim_mon_xmit;
439 dev->destructor = free_netdev;
440 ether_setup(dev);
441 dev->tx_queue_len = 0;
442 dev->type = ARPHRD_IEEE80211_RADIOTAP;
443 memset(dev->dev_addr, 0, ETH_ALEN);
444 dev->dev_addr[0] = 0x12;
445}
446
447
448static int __init init_mac80211_hwsim(void)
449{
450 int i, err = 0;
451 u8 addr[ETH_ALEN];
452 struct mac80211_hwsim_data *data;
453 struct ieee80211_hw *hw;
454 DECLARE_MAC_BUF(mac);
455
456 if (radios < 1 || radios > 65535)
457 return -EINVAL;
458
459 hwsim_radio_count = radios;
460 hwsim_radios = kcalloc(hwsim_radio_count,
461 sizeof(struct ieee80211_hw *), GFP_KERNEL);
462 if (hwsim_radios == NULL)
463 return -ENOMEM;
464
465 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
466 if (IS_ERR(hwsim_class)) {
467 kfree(hwsim_radios);
468 return PTR_ERR(hwsim_class);
469 }
470
471 memset(addr, 0, ETH_ALEN);
472 addr[0] = 0x02;
473
474 for (i = 0; i < hwsim_radio_count; i++) {
475 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
476 i);
477 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
478 if (hw == NULL) {
479 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
480 "failed\n");
481 err = -ENOMEM;
482 goto failed;
483 }
484 hwsim_radios[i] = hw;
485
486 data = hw->priv;
e800f17c
SR
487 data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
488 "hwsim%d", i);
acc1e7a3 489 if (IS_ERR(data->dev)) {
e800f17c
SR
490 printk(KERN_DEBUG
491 "mac80211_hwsim: device_create_drvdata "
acc1e7a3
JM
492 "failed (%ld)\n", PTR_ERR(data->dev));
493 err = -ENOMEM;
3a33cc10 494 goto failed_drvdata;
acc1e7a3
JM
495 }
496 data->dev->driver = &mac80211_hwsim_driver;
acc1e7a3
JM
497
498 SET_IEEE80211_DEV(hw, data->dev);
499 addr[3] = i >> 8;
500 addr[4] = i;
501 SET_IEEE80211_PERM_ADDR(hw, addr);
502
503 hw->channel_change_time = 1;
87e8b64e 504 hw->queues = 4;
f59ac048
LR
505 hw->wiphy->interface_modes =
506 BIT(NL80211_IFTYPE_STATION) |
507 BIT(NL80211_IFTYPE_AP);
87e8b64e 508 hw->ampdu_queues = 1;
acc1e7a3 509
8aa21e6f
JB
510 /* ask mac80211 to reserve space for magic */
511 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
512
acc1e7a3
JM
513 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
514 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
515 data->band.channels = data->channels;
516 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
517 data->band.bitrates = data->rates;
518 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
87e8b64e
JM
519 data->band.ht_info.ht_supported = 1;
520 data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
521 IEEE80211_HT_CAP_GRN_FLD |
522 IEEE80211_HT_CAP_SGI_40 |
523 IEEE80211_HT_CAP_DSSSCCK40;
524 data->band.ht_info.ampdu_factor = 0x3;
525 data->band.ht_info.ampdu_density = 0x6;
526 memset(data->band.ht_info.supp_mcs_set, 0,
527 sizeof(data->band.ht_info.supp_mcs_set));
528 data->band.ht_info.supp_mcs_set[0] = 0xff;
529 data->band.ht_info.supp_mcs_set[1] = 0xff;
530 data->band.ht_info.supp_mcs_set[12] =
531 IEEE80211_HT_CAP_MCS_TX_DEFINED;
acc1e7a3
JM
532 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
533
534 err = ieee80211_register_hw(hw);
535 if (err < 0) {
536 printk(KERN_DEBUG "mac80211_hwsim: "
537 "ieee80211_register_hw failed (%d)\n", err);
3a33cc10 538 goto failed_hw;
acc1e7a3
JM
539 }
540
541 printk(KERN_DEBUG "%s: hwaddr %s registered\n",
542 wiphy_name(hw->wiphy),
543 print_mac(mac, hw->wiphy->perm_addr));
544
545 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
546 (unsigned long) hw);
547 }
548
549 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
550 if (hwsim_mon == NULL)
551 goto failed;
552
553 rtnl_lock();
554
555 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3a33cc10 556 if (err < 0)
acc1e7a3 557 goto failed_mon;
3a33cc10 558
acc1e7a3
JM
559
560 err = register_netdevice(hwsim_mon);
561 if (err < 0)
562 goto failed_mon;
563
564 rtnl_unlock();
565
566 return 0;
567
568failed_mon:
569 rtnl_unlock();
570 free_netdev(hwsim_mon);
3a33cc10
IS
571 mac80211_hwsim_free();
572 return err;
acc1e7a3 573
3a33cc10
IS
574failed_hw:
575 device_unregister(data->dev);
576failed_drvdata:
577 ieee80211_free_hw(hw);
0b06b2ae 578 hwsim_radios[i] = NULL;
acc1e7a3
JM
579failed:
580 mac80211_hwsim_free();
581 return err;
582}
583
584
585static void __exit exit_mac80211_hwsim(void)
586{
587 printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
588 hwsim_radio_count);
589
590 unregister_netdev(hwsim_mon);
591 mac80211_hwsim_free();
592}
593
594
595module_init(init_mac80211_hwsim);
596module_exit(exit_mac80211_hwsim);
This page took 0.082259 seconds and 5 git commands to generate.