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