mac80211: disable BSSID filtering for mesh interfaces
[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
0e057d73
JB
17#include <linux/list.h>
18#include <linux/spinlock.h>
acc1e7a3
JM
19#include <net/mac80211.h>
20#include <net/ieee80211_radiotap.h>
21#include <linux/if_arp.h>
22#include <linux/rtnetlink.h>
23#include <linux/etherdevice.h>
fc6971d4 24#include <linux/debugfs.h>
acc1e7a3
JM
25
26MODULE_AUTHOR("Jouni Malinen");
27MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
28MODULE_LICENSE("GPL");
29
30static int radios = 2;
31module_param(radios, int, 0444);
32MODULE_PARM_DESC(radios, "Number of simulated radios");
33
8aa21e6f
JB
34struct hwsim_vif_priv {
35 u32 magic;
fc6971d4
JM
36 u8 bssid[ETH_ALEN];
37 bool assoc;
38 u16 aid;
8aa21e6f
JB
39};
40
41#define HWSIM_VIF_MAGIC 0x69537748
42
43static inline void hwsim_check_magic(struct ieee80211_vif *vif)
44{
45 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
46 WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
47}
48
49static inline void hwsim_set_magic(struct ieee80211_vif *vif)
50{
51 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
52 vp->magic = HWSIM_VIF_MAGIC;
53}
54
55static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
56{
57 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
58 vp->magic = 0;
59}
acc1e7a3 60
81c06523
JB
61struct hwsim_sta_priv {
62 u32 magic;
63};
64
65#define HWSIM_STA_MAGIC 0x6d537748
66
67static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
68{
69 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
5d6924ea 70 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
81c06523
JB
71}
72
73static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
74{
75 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
5d6924ea 76 sp->magic = HWSIM_STA_MAGIC;
81c06523
JB
77}
78
79static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
80{
81 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
82 sp->magic = 0;
83}
84
acc1e7a3
JM
85static struct class *hwsim_class;
86
acc1e7a3
JM
87static struct net_device *hwsim_mon; /* global monitor netdev */
88
89
90static const struct ieee80211_channel hwsim_channels[] = {
91 { .center_freq = 2412 },
92 { .center_freq = 2417 },
93 { .center_freq = 2422 },
94 { .center_freq = 2427 },
95 { .center_freq = 2432 },
96 { .center_freq = 2437 },
97 { .center_freq = 2442 },
98 { .center_freq = 2447 },
99 { .center_freq = 2452 },
100 { .center_freq = 2457 },
101 { .center_freq = 2462 },
102 { .center_freq = 2467 },
103 { .center_freq = 2472 },
104 { .center_freq = 2484 },
105};
106
107static const struct ieee80211_rate hwsim_rates[] = {
108 { .bitrate = 10 },
109 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
110 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
111 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
112 { .bitrate = 60 },
113 { .bitrate = 90 },
114 { .bitrate = 120 },
115 { .bitrate = 180 },
116 { .bitrate = 240 },
117 { .bitrate = 360 },
118 { .bitrate = 480 },
119 { .bitrate = 540 }
120};
121
0e057d73
JB
122static spinlock_t hwsim_radio_lock;
123static struct list_head hwsim_radios;
124
acc1e7a3 125struct mac80211_hwsim_data {
0e057d73
JB
126 struct list_head list;
127 struct ieee80211_hw *hw;
acc1e7a3
JM
128 struct device *dev;
129 struct ieee80211_supported_band band;
130 struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
131 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
132
133 struct ieee80211_channel *channel;
134 int radio_enabled;
135 unsigned long beacon_int; /* in jiffies unit */
136 unsigned int rx_filter;
137 int started;
138 struct timer_list beacon_timer;
fc6971d4
JM
139 enum ps_mode {
140 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
141 } ps;
142 bool ps_poll_pending;
143 struct dentry *debugfs;
144 struct dentry *debugfs_ps;
acc1e7a3
JM
145};
146
147
148struct hwsim_radiotap_hdr {
149 struct ieee80211_radiotap_header hdr;
150 u8 rt_flags;
151 u8 rt_rate;
152 __le16 rt_channel;
153 __le16 rt_chbitmask;
154} __attribute__ ((packed));
155
156
157static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
158{
159 /* TODO: allow packet injection */
160 dev_kfree_skb(skb);
161 return 0;
162}
163
164
165static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
166 struct sk_buff *tx_skb)
167{
168 struct mac80211_hwsim_data *data = hw->priv;
169 struct sk_buff *skb;
170 struct hwsim_radiotap_hdr *hdr;
171 u16 flags;
172 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
173 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
174
175 if (!netif_running(hwsim_mon))
176 return;
177
178 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
179 if (skb == NULL)
180 return;
181
182 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
183 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
184 hdr->hdr.it_pad = 0;
185 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
f248f105
JM
186 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
187 (1 << IEEE80211_RADIOTAP_RATE) |
188 (1 << IEEE80211_RADIOTAP_CHANNEL));
acc1e7a3
JM
189 hdr->rt_flags = 0;
190 hdr->rt_rate = txrate->bitrate / 5;
df70b4ac 191 hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
acc1e7a3
JM
192 flags = IEEE80211_CHAN_2GHZ;
193 if (txrate->flags & IEEE80211_RATE_ERP_G)
194 flags |= IEEE80211_CHAN_OFDM;
195 else
196 flags |= IEEE80211_CHAN_CCK;
197 hdr->rt_chbitmask = cpu_to_le16(flags);
198
199 skb->dev = hwsim_mon;
200 skb_set_mac_header(skb, 0);
201 skb->ip_summed = CHECKSUM_UNNECESSARY;
202 skb->pkt_type = PACKET_OTHERHOST;
f248f105 203 skb->protocol = htons(ETH_P_802_2);
acc1e7a3
JM
204 memset(skb->cb, 0, sizeof(skb->cb));
205 netif_rx(skb);
206}
207
208
fc6971d4
JM
209static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
210 struct sk_buff *skb)
211{
212 switch (data->ps) {
213 case PS_DISABLED:
214 return true;
215 case PS_ENABLED:
216 return false;
217 case PS_AUTO_POLL:
218 /* TODO: accept (some) Beacons by default and other frames only
219 * if pending PS-Poll has been sent */
220 return true;
221 case PS_MANUAL_POLL:
222 /* Allow unicast frames to own address if there is a pending
223 * PS-Poll */
224 if (data->ps_poll_pending &&
225 memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
226 ETH_ALEN) == 0) {
227 data->ps_poll_pending = false;
228 return true;
229 }
230 return false;
231 }
232
233 return true;
234}
235
236
0e057d73
JB
237static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
238 struct sk_buff *skb)
acc1e7a3 239{
0e057d73
JB
240 struct mac80211_hwsim_data *data = hw->priv, *data2;
241 bool ack = false;
e36cfdc9 242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
acc1e7a3 243 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e36cfdc9 244 struct ieee80211_rx_status rx_status;
acc1e7a3
JM
245
246 memset(&rx_status, 0, sizeof(rx_status));
247 /* TODO: set mactime */
248 rx_status.freq = data->channel->center_freq;
249 rx_status.band = data->channel->band;
e6a9854b 250 rx_status.rate_idx = info->control.rates[0].idx;
acc1e7a3
JM
251 /* TODO: simulate signal strength (and optional packet drop) */
252
fc6971d4
JM
253 if (data->ps != PS_DISABLED)
254 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
255
acc1e7a3 256 /* Copy skb to all enabled radios that are on the current frequency */
0e057d73
JB
257 spin_lock(&hwsim_radio_lock);
258 list_for_each_entry(data2, &hwsim_radios, list) {
acc1e7a3
JM
259 struct sk_buff *nskb;
260
0e057d73 261 if (data == data2)
acc1e7a3 262 continue;
0e057d73 263
acc1e7a3 264 if (!data2->started || !data2->radio_enabled ||
fc6971d4 265 !hwsim_ps_rx_ok(data2, skb) ||
acc1e7a3
JM
266 data->channel->center_freq != data2->channel->center_freq)
267 continue;
268
269 nskb = skb_copy(skb, GFP_ATOMIC);
270 if (nskb == NULL)
271 continue;
272
0e057d73 273 if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr,
acc1e7a3 274 ETH_ALEN) == 0)
0e057d73
JB
275 ack = true;
276 ieee80211_rx_irqsafe(data2->hw, nskb, &rx_status);
acc1e7a3 277 }
0e057d73 278 spin_unlock(&hwsim_radio_lock);
acc1e7a3 279
e36cfdc9
JM
280 return ack;
281}
282
283
284static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
285{
286 struct mac80211_hwsim_data *data = hw->priv;
0e057d73 287 bool ack;
e36cfdc9
JM
288 struct ieee80211_tx_info *txi;
289
290 mac80211_hwsim_monitor_rx(hw, skb);
291
292 if (skb->len < 10) {
293 /* Should not happen; just a sanity check for addr1 use */
294 dev_kfree_skb(skb);
295 return NETDEV_TX_OK;
296 }
297
298 if (!data->radio_enabled) {
299 printk(KERN_DEBUG "%s: dropped TX frame since radio "
300 "disabled\n", wiphy_name(hw->wiphy));
301 dev_kfree_skb(skb);
302 return NETDEV_TX_OK;
303 }
304
305 ack = mac80211_hwsim_tx_frame(hw, skb);
306
acc1e7a3 307 txi = IEEE80211_SKB_CB(skb);
8aa21e6f 308
25d834e1
JB
309 if (txi->control.vif)
310 hwsim_check_magic(txi->control.vif);
81c06523
JB
311 if (txi->control.sta)
312 hwsim_check_sta_magic(txi->control.sta);
8aa21e6f 313
e6a9854b
JB
314 ieee80211_tx_info_clear_status(txi);
315 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
316 txi->flags |= IEEE80211_TX_STAT_ACK;
acc1e7a3
JM
317 ieee80211_tx_status_irqsafe(hw, skb);
318 return NETDEV_TX_OK;
319}
320
321
322static int mac80211_hwsim_start(struct ieee80211_hw *hw)
323{
324 struct mac80211_hwsim_data *data = hw->priv;
325 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
326 data->started = 1;
327 return 0;
328}
329
330
331static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
332{
333 struct mac80211_hwsim_data *data = hw->priv;
334 data->started = 0;
ab1ef980 335 del_timer(&data->beacon_timer);
acc1e7a3
JM
336 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
337}
338
339
340static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
341 struct ieee80211_if_init_conf *conf)
342{
e174961c 343 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
acc1e7a3 344 wiphy_name(hw->wiphy), __func__, conf->type,
e174961c 345 conf->mac_addr);
8aa21e6f 346 hwsim_set_magic(conf->vif);
acc1e7a3
JM
347 return 0;
348}
349
350
351static void mac80211_hwsim_remove_interface(
352 struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
353{
e174961c 354 printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
acc1e7a3 355 wiphy_name(hw->wiphy), __func__, conf->type,
e174961c 356 conf->mac_addr);
8aa21e6f
JB
357 hwsim_check_magic(conf->vif);
358 hwsim_clear_magic(conf->vif);
acc1e7a3
JM
359}
360
361
362static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
363 struct ieee80211_vif *vif)
364{
365 struct ieee80211_hw *hw = arg;
acc1e7a3 366 struct sk_buff *skb;
acc1e7a3
JM
367 struct ieee80211_tx_info *info;
368
8aa21e6f
JB
369 hwsim_check_magic(vif);
370
05c914fe 371 if (vif->type != NL80211_IFTYPE_AP)
acc1e7a3
JM
372 return;
373
374 skb = ieee80211_beacon_get(hw, vif);
375 if (skb == NULL)
376 return;
377 info = IEEE80211_SKB_CB(skb);
378
379 mac80211_hwsim_monitor_rx(hw, skb);
e36cfdc9 380 mac80211_hwsim_tx_frame(hw, skb);
acc1e7a3
JM
381 dev_kfree_skb(skb);
382}
383
384
385static void mac80211_hwsim_beacon(unsigned long arg)
386{
387 struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
388 struct mac80211_hwsim_data *data = hw->priv;
389
390 if (!data->started || !data->radio_enabled)
391 return;
392
f248f105
JM
393 ieee80211_iterate_active_interfaces_atomic(
394 hw, mac80211_hwsim_beacon_tx, hw);
acc1e7a3
JM
395
396 data->beacon_timer.expires = jiffies + data->beacon_int;
397 add_timer(&data->beacon_timer);
398}
399
400
e8975581 401static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
acc1e7a3
JM
402{
403 struct mac80211_hwsim_data *data = hw->priv;
e8975581 404 struct ieee80211_conf *conf = &hw->conf;
acc1e7a3
JM
405
406 printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
407 wiphy_name(hw->wiphy), __func__,
408 conf->channel->center_freq, conf->radio_enabled,
409 conf->beacon_int);
410
411 data->channel = conf->channel;
412 data->radio_enabled = conf->radio_enabled;
413 data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
414 if (data->beacon_int < 1)
415 data->beacon_int = 1;
416
417 if (!data->started || !data->radio_enabled)
418 del_timer(&data->beacon_timer);
419 else
420 mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
421
422 return 0;
423}
424
425
426static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
427 unsigned int changed_flags,
428 unsigned int *total_flags,
429 int mc_count,
430 struct dev_addr_list *mc_list)
431{
432 struct mac80211_hwsim_data *data = hw->priv;
433
434 printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
435
436 data->rx_filter = 0;
437 if (*total_flags & FIF_PROMISC_IN_BSS)
438 data->rx_filter |= FIF_PROMISC_IN_BSS;
439 if (*total_flags & FIF_ALLMULTI)
440 data->rx_filter |= FIF_ALLMULTI;
441
442 *total_flags = data->rx_filter;
443}
444
8aa21e6f
JB
445static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
446 struct ieee80211_vif *vif,
447 struct ieee80211_if_conf *conf)
448{
fc6971d4
JM
449 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
450
8aa21e6f 451 hwsim_check_magic(vif);
fc6971d4
JM
452 if (conf->changed & IEEE80211_IFCC_BSSID) {
453 DECLARE_MAC_BUF(mac);
454 printk(KERN_DEBUG "%s:%s: BSSID changed: %s\n",
455 wiphy_name(hw->wiphy), __func__,
456 print_mac(mac, conf->bssid));
457 memcpy(vp->bssid, conf->bssid, ETH_ALEN);
458 }
8aa21e6f
JB
459 return 0;
460}
acc1e7a3 461
8aa21e6f
JB
462static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
463 struct ieee80211_vif *vif,
464 struct ieee80211_bss_conf *info,
465 u32 changed)
466{
fc6971d4
JM
467 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
468
8aa21e6f 469 hwsim_check_magic(vif);
fe63bfa3
JM
470
471 printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
472 wiphy_name(hw->wiphy), __func__, changed);
473
474 if (changed & BSS_CHANGED_ASSOC) {
475 printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
476 wiphy_name(hw->wiphy), info->assoc, info->aid);
fc6971d4
JM
477 vp->assoc = info->assoc;
478 vp->aid = info->aid;
fe63bfa3
JM
479 }
480
481 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
482 printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
483 wiphy_name(hw->wiphy), info->use_cts_prot);
484 }
485
486 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
487 printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
488 wiphy_name(hw->wiphy), info->use_short_preamble);
489 }
490
491 if (changed & BSS_CHANGED_ERP_SLOT) {
492 printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
493 wiphy_name(hw->wiphy), info->use_short_slot);
494 }
495
496 if (changed & BSS_CHANGED_HT) {
497 printk(KERN_DEBUG " %s: HT: sec_ch_offs=%d width_40_ok=%d "
498 "op_mode=%d\n",
499 wiphy_name(hw->wiphy),
500 info->ht.secondary_channel_offset,
501 info->ht.width_40_ok, info->ht.operation_mode);
502 }
503
504 if (changed & BSS_CHANGED_BASIC_RATES) {
505 printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
506 wiphy_name(hw->wiphy),
507 (unsigned long long) info->basic_rates);
508 }
8aa21e6f
JB
509}
510
511static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
512 struct ieee80211_vif *vif,
17741cdc
JB
513 enum sta_notify_cmd cmd,
514 struct ieee80211_sta *sta)
8aa21e6f
JB
515{
516 hwsim_check_magic(vif);
81c06523
JB
517 switch (cmd) {
518 case STA_NOTIFY_ADD:
519 hwsim_set_sta_magic(sta);
520 break;
521 case STA_NOTIFY_REMOVE:
522 hwsim_clear_sta_magic(sta);
523 break;
524 }
525}
526
527static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
528 struct ieee80211_sta *sta,
529 bool set)
530{
531 hwsim_check_sta_magic(sta);
532 return 0;
8aa21e6f 533}
acc1e7a3 534
1e898ff8
JM
535static int mac80211_hwsim_conf_tx(
536 struct ieee80211_hw *hw, u16 queue,
537 const struct ieee80211_tx_queue_params *params)
538{
539 printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
540 "aifs=%d)\n",
541 wiphy_name(hw->wiphy), __func__, queue,
542 params->txop, params->cw_min, params->cw_max, params->aifs);
543 return 0;
544}
545
acc1e7a3
JM
546static const struct ieee80211_ops mac80211_hwsim_ops =
547{
548 .tx = mac80211_hwsim_tx,
549 .start = mac80211_hwsim_start,
550 .stop = mac80211_hwsim_stop,
551 .add_interface = mac80211_hwsim_add_interface,
552 .remove_interface = mac80211_hwsim_remove_interface,
553 .config = mac80211_hwsim_config,
554 .configure_filter = mac80211_hwsim_configure_filter,
8aa21e6f
JB
555 .config_interface = mac80211_hwsim_config_interface,
556 .bss_info_changed = mac80211_hwsim_bss_info_changed,
557 .sta_notify = mac80211_hwsim_sta_notify,
81c06523 558 .set_tim = mac80211_hwsim_set_tim,
1e898ff8 559 .conf_tx = mac80211_hwsim_conf_tx,
acc1e7a3
JM
560};
561
562
563static void mac80211_hwsim_free(void)
564{
0e057d73
JB
565 struct list_head tmplist, *i, *tmp;
566 struct mac80211_hwsim_data *data;
567
568 INIT_LIST_HEAD(&tmplist);
569
570 spin_lock_bh(&hwsim_radio_lock);
571 list_for_each_safe(i, tmp, &hwsim_radios)
572 list_move(i, &tmplist);
573 spin_unlock_bh(&hwsim_radio_lock);
574
575 list_for_each_entry(data, &tmplist, list) {
fc6971d4
JM
576 debugfs_remove(data->debugfs_ps);
577 debugfs_remove(data->debugfs);
0e057d73
JB
578 ieee80211_unregister_hw(data->hw);
579 device_unregister(data->dev);
580 ieee80211_free_hw(data->hw);
acc1e7a3 581 }
acc1e7a3
JM
582 class_destroy(hwsim_class);
583}
584
585
586static struct device_driver mac80211_hwsim_driver = {
587 .name = "mac80211_hwsim"
588};
589
590
591static void hwsim_mon_setup(struct net_device *dev)
592{
593 dev->hard_start_xmit = hwsim_mon_xmit;
594 dev->destructor = free_netdev;
595 ether_setup(dev);
596 dev->tx_queue_len = 0;
597 dev->type = ARPHRD_IEEE80211_RADIOTAP;
598 memset(dev->dev_addr, 0, ETH_ALEN);
599 dev->dev_addr[0] = 0x12;
600}
601
602
fc6971d4
JM
603static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
604{
605 struct mac80211_hwsim_data *data = dat;
606 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
607 DECLARE_MAC_BUF(buf);
608 struct sk_buff *skb;
609 struct ieee80211_pspoll *pspoll;
610
611 if (!vp->assoc)
612 return;
613
614 printk(KERN_DEBUG "%s:%s: send PS-Poll to %s for aid %d\n",
615 wiphy_name(data->hw->wiphy), __func__,
616 print_mac(buf, vp->bssid), vp->aid);
617
618 skb = dev_alloc_skb(sizeof(*pspoll));
619 if (!skb)
620 return;
621 pspoll = (void *) skb_put(skb, sizeof(*pspoll));
622 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
623 IEEE80211_STYPE_PSPOLL |
624 IEEE80211_FCTL_PM);
625 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
626 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
627 memcpy(pspoll->ta, mac, ETH_ALEN);
628 if (data->radio_enabled &&
629 !mac80211_hwsim_tx_frame(data->hw, skb))
630 printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
631 dev_kfree_skb(skb);
632}
633
634
635static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
636 struct ieee80211_vif *vif, int ps)
637{
638 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
639 DECLARE_MAC_BUF(buf);
640 struct sk_buff *skb;
641 struct ieee80211_hdr *hdr;
642
643 if (!vp->assoc)
644 return;
645
646 printk(KERN_DEBUG "%s:%s: send data::nullfunc to %s ps=%d\n",
647 wiphy_name(data->hw->wiphy), __func__,
648 print_mac(buf, vp->bssid), ps);
649
650 skb = dev_alloc_skb(sizeof(*hdr));
651 if (!skb)
652 return;
653 hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
654 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
655 IEEE80211_STYPE_NULLFUNC |
656 (ps ? IEEE80211_FCTL_PM : 0));
657 hdr->duration_id = cpu_to_le16(0);
658 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
659 memcpy(hdr->addr2, mac, ETH_ALEN);
660 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
661 if (data->radio_enabled &&
662 !mac80211_hwsim_tx_frame(data->hw, skb))
663 printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
664 dev_kfree_skb(skb);
665}
666
667
668static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
669 struct ieee80211_vif *vif)
670{
671 struct mac80211_hwsim_data *data = dat;
672 hwsim_send_nullfunc(data, mac, vif, 1);
673}
674
675
676static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
677 struct ieee80211_vif *vif)
678{
679 struct mac80211_hwsim_data *data = dat;
680 hwsim_send_nullfunc(data, mac, vif, 0);
681}
682
683
684static int hwsim_fops_ps_read(void *dat, u64 *val)
685{
686 struct mac80211_hwsim_data *data = dat;
687 *val = data->ps;
688 return 0;
689}
690
691static int hwsim_fops_ps_write(void *dat, u64 val)
692{
693 struct mac80211_hwsim_data *data = dat;
694 enum ps_mode old_ps;
695
696 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
697 val != PS_MANUAL_POLL)
698 return -EINVAL;
699
700 old_ps = data->ps;
701 data->ps = val;
702
703 if (val == PS_MANUAL_POLL) {
704 ieee80211_iterate_active_interfaces(data->hw,
705 hwsim_send_ps_poll, data);
706 data->ps_poll_pending = true;
707 } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
708 ieee80211_iterate_active_interfaces(data->hw,
709 hwsim_send_nullfunc_ps,
710 data);
711 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
712 ieee80211_iterate_active_interfaces(data->hw,
713 hwsim_send_nullfunc_no_ps,
714 data);
715 }
716
717 return 0;
718}
719
720DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
721 "%llu\n");
722
723
acc1e7a3
JM
724static int __init init_mac80211_hwsim(void)
725{
726 int i, err = 0;
727 u8 addr[ETH_ALEN];
728 struct mac80211_hwsim_data *data;
729 struct ieee80211_hw *hw;
acc1e7a3 730
0e057d73 731 if (radios < 1 || radios > 100)
acc1e7a3
JM
732 return -EINVAL;
733
0e057d73
JB
734 spin_lock_init(&hwsim_radio_lock);
735 INIT_LIST_HEAD(&hwsim_radios);
acc1e7a3
JM
736
737 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
0e057d73 738 if (IS_ERR(hwsim_class))
acc1e7a3 739 return PTR_ERR(hwsim_class);
acc1e7a3
JM
740
741 memset(addr, 0, ETH_ALEN);
742 addr[0] = 0x02;
743
0e057d73 744 for (i = 0; i < radios; i++) {
acc1e7a3
JM
745 printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
746 i);
747 hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
0e057d73 748 if (!hw) {
acc1e7a3
JM
749 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
750 "failed\n");
751 err = -ENOMEM;
752 goto failed;
753 }
acc1e7a3 754 data = hw->priv;
0e057d73
JB
755 data->hw = hw;
756
6e05d6c4
GKH
757 data->dev = device_create(hwsim_class, NULL, 0, hw,
758 "hwsim%d", i);
acc1e7a3 759 if (IS_ERR(data->dev)) {
e800f17c 760 printk(KERN_DEBUG
6e05d6c4 761 "mac80211_hwsim: device_create "
acc1e7a3
JM
762 "failed (%ld)\n", PTR_ERR(data->dev));
763 err = -ENOMEM;
3a33cc10 764 goto failed_drvdata;
acc1e7a3
JM
765 }
766 data->dev->driver = &mac80211_hwsim_driver;
acc1e7a3
JM
767
768 SET_IEEE80211_DEV(hw, data->dev);
769 addr[3] = i >> 8;
770 addr[4] = i;
771 SET_IEEE80211_PERM_ADDR(hw, addr);
772
773 hw->channel_change_time = 1;
87e8b64e 774 hw->queues = 4;
f59ac048
LR
775 hw->wiphy->interface_modes =
776 BIT(NL80211_IFTYPE_STATION) |
777 BIT(NL80211_IFTYPE_AP);
87e8b64e 778 hw->ampdu_queues = 1;
acc1e7a3 779
8aa21e6f
JB
780 /* ask mac80211 to reserve space for magic */
781 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
81c06523 782 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
8aa21e6f 783
acc1e7a3
JM
784 memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
785 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
786 data->band.channels = data->channels;
787 data->band.n_channels = ARRAY_SIZE(hwsim_channels);
788 data->band.bitrates = data->rates;
789 data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
d9fe60de
JB
790 data->band.ht_cap.ht_supported = true;
791 data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
87e8b64e
JM
792 IEEE80211_HT_CAP_GRN_FLD |
793 IEEE80211_HT_CAP_SGI_40 |
794 IEEE80211_HT_CAP_DSSSCCK40;
d9fe60de
JB
795 data->band.ht_cap.ampdu_factor = 0x3;
796 data->band.ht_cap.ampdu_density = 0x6;
797 memset(&data->band.ht_cap.mcs, 0,
798 sizeof(data->band.ht_cap.mcs));
799 data->band.ht_cap.mcs.rx_mask[0] = 0xff;
800 data->band.ht_cap.mcs.rx_mask[1] = 0xff;
801 data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
acc1e7a3
JM
802 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
803
804 err = ieee80211_register_hw(hw);
805 if (err < 0) {
806 printk(KERN_DEBUG "mac80211_hwsim: "
807 "ieee80211_register_hw failed (%d)\n", err);
3a33cc10 808 goto failed_hw;
acc1e7a3
JM
809 }
810
e174961c 811 printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
acc1e7a3 812 wiphy_name(hw->wiphy),
e174961c 813 hw->wiphy->perm_addr);
acc1e7a3 814
fc6971d4
JM
815 data->debugfs = debugfs_create_dir("hwsim",
816 hw->wiphy->debugfsdir);
817 data->debugfs_ps = debugfs_create_file("ps", 0666,
818 data->debugfs, data,
819 &hwsim_fops_ps);
820
acc1e7a3
JM
821 setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
822 (unsigned long) hw);
0e057d73
JB
823
824 list_add_tail(&data->list, &hwsim_radios);
acc1e7a3
JM
825 }
826
827 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
828 if (hwsim_mon == NULL)
829 goto failed;
830
831 rtnl_lock();
832
833 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3a33cc10 834 if (err < 0)
acc1e7a3 835 goto failed_mon;
3a33cc10 836
acc1e7a3
JM
837
838 err = register_netdevice(hwsim_mon);
839 if (err < 0)
840 goto failed_mon;
841
842 rtnl_unlock();
843
844 return 0;
845
846failed_mon:
847 rtnl_unlock();
848 free_netdev(hwsim_mon);
3a33cc10
IS
849 mac80211_hwsim_free();
850 return err;
acc1e7a3 851
3a33cc10
IS
852failed_hw:
853 device_unregister(data->dev);
854failed_drvdata:
855 ieee80211_free_hw(hw);
acc1e7a3
JM
856failed:
857 mac80211_hwsim_free();
858 return err;
859}
860
861
862static void __exit exit_mac80211_hwsim(void)
863{
0e057d73 864 printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
acc1e7a3
JM
865
866 unregister_netdev(hwsim_mon);
867 mac80211_hwsim_free();
868}
869
870
871module_init(init_mac80211_hwsim);
872module_exit(exit_mac80211_hwsim);
This page took 0.128179 seconds and 5 git commands to generate.