2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
15 * - RX filtering based on filter configuration (data->rx_filter)
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <linux/if_arp.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/etherdevice.h>
28 #include <linux/platform_device.h>
29 #include <linux/debugfs.h>
30 #include <linux/module.h>
31 #include <linux/ktime.h>
32 #include <net/genetlink.h>
33 #include "mac80211_hwsim.h"
35 #define WARN_QUEUE 100
38 MODULE_AUTHOR("Jouni Malinen");
39 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
40 MODULE_LICENSE("GPL");
42 static u32 wmediumd_portid
;
44 static int radios
= 2;
45 module_param(radios
, int, 0444);
46 MODULE_PARM_DESC(radios
, "Number of simulated radios");
48 static int channels
= 1;
49 module_param(channels
, int, 0444);
50 MODULE_PARM_DESC(channels
, "Number of concurrent channels");
52 static bool paged_rx
= false;
53 module_param(paged_rx
, bool, 0644);
54 MODULE_PARM_DESC(paged_rx
, "Use paged SKBs for RX instead of linear ones");
56 static bool rctbl
= false;
57 module_param(rctbl
, bool, 0444);
58 MODULE_PARM_DESC(rctbl
, "Handle rate control table");
60 struct hwsim_vif_priv
{
68 #define HWSIM_VIF_MAGIC 0x69537748
70 static inline void hwsim_check_magic(struct ieee80211_vif
*vif
)
72 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
73 WARN(vp
->magic
!= HWSIM_VIF_MAGIC
,
74 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
75 vif
, vp
->magic
, vif
->addr
, vif
->type
, vif
->p2p
);
78 static inline void hwsim_set_magic(struct ieee80211_vif
*vif
)
80 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
81 vp
->magic
= HWSIM_VIF_MAGIC
;
84 static inline void hwsim_clear_magic(struct ieee80211_vif
*vif
)
86 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
90 struct hwsim_sta_priv
{
94 #define HWSIM_STA_MAGIC 0x6d537749
96 static inline void hwsim_check_sta_magic(struct ieee80211_sta
*sta
)
98 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
99 WARN_ON(sp
->magic
!= HWSIM_STA_MAGIC
);
102 static inline void hwsim_set_sta_magic(struct ieee80211_sta
*sta
)
104 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
105 sp
->magic
= HWSIM_STA_MAGIC
;
108 static inline void hwsim_clear_sta_magic(struct ieee80211_sta
*sta
)
110 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
114 struct hwsim_chanctx_priv
{
118 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
120 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
122 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
123 WARN_ON(cp
->magic
!= HWSIM_CHANCTX_MAGIC
);
126 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
128 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
129 cp
->magic
= HWSIM_CHANCTX_MAGIC
;
132 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
134 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
138 static struct class *hwsim_class
;
140 static struct net_device
*hwsim_mon
; /* global monitor netdev */
142 #define CHAN2G(_freq) { \
143 .band = IEEE80211_BAND_2GHZ, \
144 .center_freq = (_freq), \
145 .hw_value = (_freq), \
149 #define CHAN5G(_freq) { \
150 .band = IEEE80211_BAND_5GHZ, \
151 .center_freq = (_freq), \
152 .hw_value = (_freq), \
156 static const struct ieee80211_channel hwsim_channels_2ghz
[] = {
157 CHAN2G(2412), /* Channel 1 */
158 CHAN2G(2417), /* Channel 2 */
159 CHAN2G(2422), /* Channel 3 */
160 CHAN2G(2427), /* Channel 4 */
161 CHAN2G(2432), /* Channel 5 */
162 CHAN2G(2437), /* Channel 6 */
163 CHAN2G(2442), /* Channel 7 */
164 CHAN2G(2447), /* Channel 8 */
165 CHAN2G(2452), /* Channel 9 */
166 CHAN2G(2457), /* Channel 10 */
167 CHAN2G(2462), /* Channel 11 */
168 CHAN2G(2467), /* Channel 12 */
169 CHAN2G(2472), /* Channel 13 */
170 CHAN2G(2484), /* Channel 14 */
173 static const struct ieee80211_channel hwsim_channels_5ghz
[] = {
174 CHAN5G(5180), /* Channel 36 */
175 CHAN5G(5200), /* Channel 40 */
176 CHAN5G(5220), /* Channel 44 */
177 CHAN5G(5240), /* Channel 48 */
179 CHAN5G(5260), /* Channel 52 */
180 CHAN5G(5280), /* Channel 56 */
181 CHAN5G(5300), /* Channel 60 */
182 CHAN5G(5320), /* Channel 64 */
184 CHAN5G(5500), /* Channel 100 */
185 CHAN5G(5520), /* Channel 104 */
186 CHAN5G(5540), /* Channel 108 */
187 CHAN5G(5560), /* Channel 112 */
188 CHAN5G(5580), /* Channel 116 */
189 CHAN5G(5600), /* Channel 120 */
190 CHAN5G(5620), /* Channel 124 */
191 CHAN5G(5640), /* Channel 128 */
192 CHAN5G(5660), /* Channel 132 */
193 CHAN5G(5680), /* Channel 136 */
194 CHAN5G(5700), /* Channel 140 */
196 CHAN5G(5745), /* Channel 149 */
197 CHAN5G(5765), /* Channel 153 */
198 CHAN5G(5785), /* Channel 157 */
199 CHAN5G(5805), /* Channel 161 */
200 CHAN5G(5825), /* Channel 165 */
203 static const struct ieee80211_rate hwsim_rates
[] = {
205 { .bitrate
= 20, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
206 { .bitrate
= 55, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
207 { .bitrate
= 110, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
218 static const struct ieee80211_iface_limit hwsim_if_limits
[] = {
219 { .max
= 1, .types
= BIT(NL80211_IFTYPE_ADHOC
) },
220 { .max
= 2048, .types
= BIT(NL80211_IFTYPE_STATION
) |
221 BIT(NL80211_IFTYPE_P2P_CLIENT
) |
222 #ifdef CONFIG_MAC80211_MESH
223 BIT(NL80211_IFTYPE_MESH_POINT
) |
225 BIT(NL80211_IFTYPE_AP
) |
226 BIT(NL80211_IFTYPE_P2P_GO
) },
227 { .max
= 1, .types
= BIT(NL80211_IFTYPE_P2P_DEVICE
) },
230 static const struct ieee80211_iface_limit hwsim_if_dfs_limits
[] = {
231 { .max
= 8, .types
= BIT(NL80211_IFTYPE_AP
) },
234 static const struct ieee80211_iface_combination hwsim_if_comb
[] = {
236 .limits
= hwsim_if_limits
,
237 .n_limits
= ARRAY_SIZE(hwsim_if_limits
),
238 .max_interfaces
= 2048,
239 .num_different_channels
= 1,
242 .limits
= hwsim_if_dfs_limits
,
243 .n_limits
= ARRAY_SIZE(hwsim_if_dfs_limits
),
245 .num_different_channels
= 1,
246 .radar_detect_widths
= BIT(NL80211_CHAN_WIDTH_20_NOHT
) |
247 BIT(NL80211_CHAN_WIDTH_20
) |
248 BIT(NL80211_CHAN_WIDTH_40
) |
249 BIT(NL80211_CHAN_WIDTH_80
) |
250 BIT(NL80211_CHAN_WIDTH_160
),
254 static spinlock_t hwsim_radio_lock
;
255 static struct list_head hwsim_radios
;
256 static int hwsim_radio_idx
;
258 static struct platform_driver mac80211_hwsim_driver
= {
260 .name
= "mac80211_hwsim",
261 .owner
= THIS_MODULE
,
265 struct mac80211_hwsim_data
{
266 struct list_head list
;
267 struct ieee80211_hw
*hw
;
269 struct ieee80211_supported_band bands
[IEEE80211_NUM_BANDS
];
270 struct ieee80211_channel channels_2ghz
[ARRAY_SIZE(hwsim_channels_2ghz
)];
271 struct ieee80211_channel channels_5ghz
[ARRAY_SIZE(hwsim_channels_5ghz
)];
272 struct ieee80211_rate rates
[ARRAY_SIZE(hwsim_rates
)];
273 struct ieee80211_iface_combination if_combination
;
275 struct mac_address addresses
[2];
278 struct ieee80211_channel
*tmp_chan
;
279 struct delayed_work roc_done
;
280 struct delayed_work hw_scan
;
281 struct cfg80211_scan_request
*hw_scan_request
;
282 struct ieee80211_vif
*hw_scan_vif
;
285 struct ieee80211_channel
*channel
;
286 u64 beacon_int
/* beacon interval in us */;
287 unsigned int rx_filter
;
288 bool started
, idle
, scanning
;
290 struct tasklet_hrtimer beacon_timer
;
292 PS_DISABLED
, PS_ENABLED
, PS_AUTO_POLL
, PS_MANUAL_POLL
294 bool ps_poll_pending
;
295 struct dentry
*debugfs
;
297 struct sk_buff_head pending
; /* packets pending */
299 * Only radios in the same group can communicate together (the
300 * channel has to match too). Each bit represents a group. A
301 * radio can be in more then one group.
307 /* difference between this hw's clock and the real clock, in usecs */
310 /* absolute beacon transmission time. Used to cover up "tx" delay. */
315 struct hwsim_radiotap_hdr
{
316 struct ieee80211_radiotap_header hdr
;
324 struct hwsim_radiotap_ack_hdr
{
325 struct ieee80211_radiotap_header hdr
;
332 /* MAC80211_HWSIM netlinf family */
333 static struct genl_family hwsim_genl_family
= {
334 .id
= GENL_ID_GENERATE
,
336 .name
= "MAC80211_HWSIM",
338 .maxattr
= HWSIM_ATTR_MAX
,
341 /* MAC80211_HWSIM netlink policy */
343 static struct nla_policy hwsim_genl_policy
[HWSIM_ATTR_MAX
+ 1] = {
344 [HWSIM_ATTR_ADDR_RECEIVER
] = { .type
= NLA_UNSPEC
, .len
= ETH_ALEN
},
345 [HWSIM_ATTR_ADDR_TRANSMITTER
] = { .type
= NLA_UNSPEC
, .len
= ETH_ALEN
},
346 [HWSIM_ATTR_FRAME
] = { .type
= NLA_BINARY
,
347 .len
= IEEE80211_MAX_DATA_LEN
},
348 [HWSIM_ATTR_FLAGS
] = { .type
= NLA_U32
},
349 [HWSIM_ATTR_RX_RATE
] = { .type
= NLA_U32
},
350 [HWSIM_ATTR_SIGNAL
] = { .type
= NLA_U32
},
351 [HWSIM_ATTR_TX_INFO
] = { .type
= NLA_UNSPEC
,
352 .len
= IEEE80211_TX_MAX_RATES
*
353 sizeof(struct hwsim_tx_rate
)},
354 [HWSIM_ATTR_COOKIE
] = { .type
= NLA_U64
},
357 static void mac80211_hwsim_tx_frame(struct ieee80211_hw
*hw
,
359 struct ieee80211_channel
*chan
);
361 /* sysfs attributes */
362 static void hwsim_send_ps_poll(void *dat
, u8
*mac
, struct ieee80211_vif
*vif
)
364 struct mac80211_hwsim_data
*data
= dat
;
365 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
367 struct ieee80211_pspoll
*pspoll
;
372 wiphy_debug(data
->hw
->wiphy
,
373 "%s: send PS-Poll to %pM for aid %d\n",
374 __func__
, vp
->bssid
, vp
->aid
);
376 skb
= dev_alloc_skb(sizeof(*pspoll
));
379 pspoll
= (void *) skb_put(skb
, sizeof(*pspoll
));
380 pspoll
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_CTL
|
381 IEEE80211_STYPE_PSPOLL
|
383 pspoll
->aid
= cpu_to_le16(0xc000 | vp
->aid
);
384 memcpy(pspoll
->bssid
, vp
->bssid
, ETH_ALEN
);
385 memcpy(pspoll
->ta
, mac
, ETH_ALEN
);
388 mac80211_hwsim_tx_frame(data
->hw
, skb
,
389 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
393 static void hwsim_send_nullfunc(struct mac80211_hwsim_data
*data
, u8
*mac
,
394 struct ieee80211_vif
*vif
, int ps
)
396 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
398 struct ieee80211_hdr
*hdr
;
403 wiphy_debug(data
->hw
->wiphy
,
404 "%s: send data::nullfunc to %pM ps=%d\n",
405 __func__
, vp
->bssid
, ps
);
407 skb
= dev_alloc_skb(sizeof(*hdr
));
410 hdr
= (void *) skb_put(skb
, sizeof(*hdr
) - ETH_ALEN
);
411 hdr
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_DATA
|
412 IEEE80211_STYPE_NULLFUNC
|
413 (ps
? IEEE80211_FCTL_PM
: 0));
414 hdr
->duration_id
= cpu_to_le16(0);
415 memcpy(hdr
->addr1
, vp
->bssid
, ETH_ALEN
);
416 memcpy(hdr
->addr2
, mac
, ETH_ALEN
);
417 memcpy(hdr
->addr3
, vp
->bssid
, ETH_ALEN
);
420 mac80211_hwsim_tx_frame(data
->hw
, skb
,
421 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
426 static void hwsim_send_nullfunc_ps(void *dat
, u8
*mac
,
427 struct ieee80211_vif
*vif
)
429 struct mac80211_hwsim_data
*data
= dat
;
430 hwsim_send_nullfunc(data
, mac
, vif
, 1);
433 static void hwsim_send_nullfunc_no_ps(void *dat
, u8
*mac
,
434 struct ieee80211_vif
*vif
)
436 struct mac80211_hwsim_data
*data
= dat
;
437 hwsim_send_nullfunc(data
, mac
, vif
, 0);
440 static int hwsim_fops_ps_read(void *dat
, u64
*val
)
442 struct mac80211_hwsim_data
*data
= dat
;
447 static int hwsim_fops_ps_write(void *dat
, u64 val
)
449 struct mac80211_hwsim_data
*data
= dat
;
452 if (val
!= PS_DISABLED
&& val
!= PS_ENABLED
&& val
!= PS_AUTO_POLL
&&
453 val
!= PS_MANUAL_POLL
)
459 if (val
== PS_MANUAL_POLL
) {
460 ieee80211_iterate_active_interfaces(data
->hw
,
461 IEEE80211_IFACE_ITER_NORMAL
,
462 hwsim_send_ps_poll
, data
);
463 data
->ps_poll_pending
= true;
464 } else if (old_ps
== PS_DISABLED
&& val
!= PS_DISABLED
) {
465 ieee80211_iterate_active_interfaces(data
->hw
,
466 IEEE80211_IFACE_ITER_NORMAL
,
467 hwsim_send_nullfunc_ps
,
469 } else if (old_ps
!= PS_DISABLED
&& val
== PS_DISABLED
) {
470 ieee80211_iterate_active_interfaces(data
->hw
,
471 IEEE80211_IFACE_ITER_NORMAL
,
472 hwsim_send_nullfunc_no_ps
,
479 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps
, hwsim_fops_ps_read
, hwsim_fops_ps_write
,
482 static int hwsim_write_simulate_radar(void *dat
, u64 val
)
484 struct mac80211_hwsim_data
*data
= dat
;
486 ieee80211_radar_detected(data
->hw
);
491 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar
, NULL
,
492 hwsim_write_simulate_radar
, "%llu\n");
494 static int hwsim_fops_group_read(void *dat
, u64
*val
)
496 struct mac80211_hwsim_data
*data
= dat
;
501 static int hwsim_fops_group_write(void *dat
, u64 val
)
503 struct mac80211_hwsim_data
*data
= dat
;
508 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group
,
509 hwsim_fops_group_read
, hwsim_fops_group_write
,
512 static netdev_tx_t
hwsim_mon_xmit(struct sk_buff
*skb
,
513 struct net_device
*dev
)
515 /* TODO: allow packet injection */
520 static inline u64
mac80211_hwsim_get_tsf_raw(void)
522 return ktime_to_us(ktime_get_real());
525 static __le64
__mac80211_hwsim_get_tsf(struct mac80211_hwsim_data
*data
)
527 u64 now
= mac80211_hwsim_get_tsf_raw();
528 return cpu_to_le64(now
+ data
->tsf_offset
);
531 static u64
mac80211_hwsim_get_tsf(struct ieee80211_hw
*hw
,
532 struct ieee80211_vif
*vif
)
534 struct mac80211_hwsim_data
*data
= hw
->priv
;
535 return le64_to_cpu(__mac80211_hwsim_get_tsf(data
));
538 static void mac80211_hwsim_set_tsf(struct ieee80211_hw
*hw
,
539 struct ieee80211_vif
*vif
, u64 tsf
)
541 struct mac80211_hwsim_data
*data
= hw
->priv
;
542 u64 now
= mac80211_hwsim_get_tsf(hw
, vif
);
543 u32 bcn_int
= data
->beacon_int
;
544 s64 delta
= tsf
- now
;
546 data
->tsf_offset
+= delta
;
547 /* adjust after beaconing with new timestamp at old TBTT */
548 data
->bcn_delta
= do_div(delta
, bcn_int
);
551 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw
*hw
,
552 struct sk_buff
*tx_skb
,
553 struct ieee80211_channel
*chan
)
555 struct mac80211_hwsim_data
*data
= hw
->priv
;
557 struct hwsim_radiotap_hdr
*hdr
;
559 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(tx_skb
);
560 struct ieee80211_rate
*txrate
= ieee80211_get_tx_rate(hw
, info
);
562 if (!netif_running(hwsim_mon
))
565 skb
= skb_copy_expand(tx_skb
, sizeof(*hdr
), 0, GFP_ATOMIC
);
569 hdr
= (struct hwsim_radiotap_hdr
*) skb_push(skb
, sizeof(*hdr
));
570 hdr
->hdr
.it_version
= PKTHDR_RADIOTAP_VERSION
;
572 hdr
->hdr
.it_len
= cpu_to_le16(sizeof(*hdr
));
573 hdr
->hdr
.it_present
= cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS
) |
574 (1 << IEEE80211_RADIOTAP_RATE
) |
575 (1 << IEEE80211_RADIOTAP_TSFT
) |
576 (1 << IEEE80211_RADIOTAP_CHANNEL
));
577 hdr
->rt_tsft
= __mac80211_hwsim_get_tsf(data
);
579 hdr
->rt_rate
= txrate
->bitrate
/ 5;
580 hdr
->rt_channel
= cpu_to_le16(chan
->center_freq
);
581 flags
= IEEE80211_CHAN_2GHZ
;
582 if (txrate
->flags
& IEEE80211_RATE_ERP_G
)
583 flags
|= IEEE80211_CHAN_OFDM
;
585 flags
|= IEEE80211_CHAN_CCK
;
586 hdr
->rt_chbitmask
= cpu_to_le16(flags
);
588 skb
->dev
= hwsim_mon
;
589 skb_set_mac_header(skb
, 0);
590 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
591 skb
->pkt_type
= PACKET_OTHERHOST
;
592 skb
->protocol
= htons(ETH_P_802_2
);
593 memset(skb
->cb
, 0, sizeof(skb
->cb
));
598 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel
*chan
,
602 struct hwsim_radiotap_ack_hdr
*hdr
;
604 struct ieee80211_hdr
*hdr11
;
606 if (!netif_running(hwsim_mon
))
609 skb
= dev_alloc_skb(100);
613 hdr
= (struct hwsim_radiotap_ack_hdr
*) skb_put(skb
, sizeof(*hdr
));
614 hdr
->hdr
.it_version
= PKTHDR_RADIOTAP_VERSION
;
616 hdr
->hdr
.it_len
= cpu_to_le16(sizeof(*hdr
));
617 hdr
->hdr
.it_present
= cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS
) |
618 (1 << IEEE80211_RADIOTAP_CHANNEL
));
621 hdr
->rt_channel
= cpu_to_le16(chan
->center_freq
);
622 flags
= IEEE80211_CHAN_2GHZ
;
623 hdr
->rt_chbitmask
= cpu_to_le16(flags
);
625 hdr11
= (struct ieee80211_hdr
*) skb_put(skb
, 10);
626 hdr11
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_CTL
|
627 IEEE80211_STYPE_ACK
);
628 hdr11
->duration_id
= cpu_to_le16(0);
629 memcpy(hdr11
->addr1
, addr
, ETH_ALEN
);
631 skb
->dev
= hwsim_mon
;
632 skb_set_mac_header(skb
, 0);
633 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
634 skb
->pkt_type
= PACKET_OTHERHOST
;
635 skb
->protocol
= htons(ETH_P_802_2
);
636 memset(skb
->cb
, 0, sizeof(skb
->cb
));
641 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data
*data
,
650 /* TODO: accept (some) Beacons by default and other frames only
651 * if pending PS-Poll has been sent */
654 /* Allow unicast frames to own address if there is a pending
656 if (data
->ps_poll_pending
&&
657 memcmp(data
->hw
->wiphy
->perm_addr
, skb
->data
+ 4,
659 data
->ps_poll_pending
= false;
669 struct mac80211_hwsim_addr_match_data
{
674 static void mac80211_hwsim_addr_iter(void *data
, u8
*mac
,
675 struct ieee80211_vif
*vif
)
677 struct mac80211_hwsim_addr_match_data
*md
= data
;
678 if (memcmp(mac
, md
->addr
, ETH_ALEN
) == 0)
683 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data
*data
,
686 struct mac80211_hwsim_addr_match_data md
;
688 if (memcmp(addr
, data
->hw
->wiphy
->perm_addr
, ETH_ALEN
) == 0)
693 ieee80211_iterate_active_interfaces_atomic(data
->hw
,
694 IEEE80211_IFACE_ITER_NORMAL
,
695 mac80211_hwsim_addr_iter
,
701 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw
*hw
,
702 struct sk_buff
*my_skb
,
706 struct mac80211_hwsim_data
*data
= hw
->priv
;
707 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) my_skb
->data
;
708 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(my_skb
);
710 unsigned int hwsim_flags
= 0;
712 struct hwsim_tx_rate tx_attempts
[IEEE80211_TX_MAX_RATES
];
714 if (data
->ps
!= PS_DISABLED
)
715 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
716 /* If the queue contains MAX_QUEUE skb's drop some */
717 if (skb_queue_len(&data
->pending
) >= MAX_QUEUE
) {
718 /* Droping until WARN_QUEUE level */
719 while (skb_queue_len(&data
->pending
) >= WARN_QUEUE
)
720 skb_dequeue(&data
->pending
);
723 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
725 goto nla_put_failure
;
727 msg_head
= genlmsg_put(skb
, 0, 0, &hwsim_genl_family
, 0,
729 if (msg_head
== NULL
) {
730 printk(KERN_DEBUG
"mac80211_hwsim: problem with msg_head\n");
731 goto nla_put_failure
;
734 if (nla_put(skb
, HWSIM_ATTR_ADDR_TRANSMITTER
,
735 ETH_ALEN
, data
->addresses
[1].addr
))
736 goto nla_put_failure
;
738 /* We get the skb->data */
739 if (nla_put(skb
, HWSIM_ATTR_FRAME
, my_skb
->len
, my_skb
->data
))
740 goto nla_put_failure
;
742 /* We get the flags for this transmission, and we translate them to
745 if (info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
)
746 hwsim_flags
|= HWSIM_TX_CTL_REQ_TX_STATUS
;
748 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
749 hwsim_flags
|= HWSIM_TX_CTL_NO_ACK
;
751 if (nla_put_u32(skb
, HWSIM_ATTR_FLAGS
, hwsim_flags
))
752 goto nla_put_failure
;
754 /* We get the tx control (rate and retries) info*/
756 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
757 tx_attempts
[i
].idx
= info
->status
.rates
[i
].idx
;
758 tx_attempts
[i
].count
= info
->status
.rates
[i
].count
;
761 if (nla_put(skb
, HWSIM_ATTR_TX_INFO
,
762 sizeof(struct hwsim_tx_rate
)*IEEE80211_TX_MAX_RATES
,
764 goto nla_put_failure
;
766 /* We create a cookie to identify this skb */
767 if (nla_put_u64(skb
, HWSIM_ATTR_COOKIE
, (unsigned long) my_skb
))
768 goto nla_put_failure
;
770 genlmsg_end(skb
, msg_head
);
771 genlmsg_unicast(&init_net
, skb
, dst_portid
);
773 /* Enqueue the packet */
774 skb_queue_tail(&data
->pending
, my_skb
);
778 printk(KERN_DEBUG
"mac80211_hwsim: error occurred in %s\n", __func__
);
781 static bool hwsim_chans_compat(struct ieee80211_channel
*c1
,
782 struct ieee80211_channel
*c2
)
787 return c1
->center_freq
== c2
->center_freq
;
790 struct tx_iter_data
{
791 struct ieee80211_channel
*channel
;
795 static void mac80211_hwsim_tx_iter(void *_data
, u8
*addr
,
796 struct ieee80211_vif
*vif
)
798 struct tx_iter_data
*data
= _data
;
800 if (!vif
->chanctx_conf
)
803 if (!hwsim_chans_compat(data
->channel
,
804 rcu_dereference(vif
->chanctx_conf
)->def
.chan
))
807 data
->receive
= true;
810 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw
*hw
,
812 struct ieee80211_channel
*chan
)
814 struct mac80211_hwsim_data
*data
= hw
->priv
, *data2
;
816 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
817 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
818 struct ieee80211_rx_status rx_status
;
821 memset(&rx_status
, 0, sizeof(rx_status
));
822 rx_status
.flag
|= RX_FLAG_MACTIME_START
;
823 rx_status
.freq
= chan
->center_freq
;
824 rx_status
.band
= chan
->band
;
825 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_VHT_MCS
) {
827 ieee80211_rate_get_vht_mcs(&info
->control
.rates
[0]);
829 ieee80211_rate_get_vht_nss(&info
->control
.rates
[0]);
830 rx_status
.flag
|= RX_FLAG_VHT
;
832 rx_status
.rate_idx
= info
->control
.rates
[0].idx
;
833 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_MCS
)
834 rx_status
.flag
|= RX_FLAG_HT
;
836 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
837 rx_status
.flag
|= RX_FLAG_40MHZ
;
838 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_SHORT_GI
)
839 rx_status
.flag
|= RX_FLAG_SHORT_GI
;
840 /* TODO: simulate real signal strength (and optional packet loss) */
841 rx_status
.signal
= data
->power_level
- 50;
843 if (data
->ps
!= PS_DISABLED
)
844 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
846 /* release the skb's source info */
854 * Get absolute mactime here so all HWs RX at the "same time", and
855 * absolute TX time for beacon mactime so the timestamp matches.
856 * Giving beacons a different mactime than non-beacons looks messy, but
857 * it helps the Toffset be exact and a ~10us mactime discrepancy
858 * probably doesn't really matter.
860 if (ieee80211_is_beacon(hdr
->frame_control
) ||
861 ieee80211_is_probe_resp(hdr
->frame_control
))
862 now
= data
->abs_bcn_ts
;
864 now
= mac80211_hwsim_get_tsf_raw();
866 /* Copy skb to all enabled radios that are on the current frequency */
867 spin_lock(&hwsim_radio_lock
);
868 list_for_each_entry(data2
, &hwsim_radios
, list
) {
869 struct sk_buff
*nskb
;
870 struct tx_iter_data tx_iter_data
= {
878 if (!data2
->started
|| (data2
->idle
&& !data2
->tmp_chan
) ||
879 !hwsim_ps_rx_ok(data2
, skb
))
882 if (!(data
->group
& data2
->group
))
885 if (!hwsim_chans_compat(chan
, data2
->tmp_chan
) &&
886 !hwsim_chans_compat(chan
, data2
->channel
)) {
887 ieee80211_iterate_active_interfaces_atomic(
888 data2
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
889 mac80211_hwsim_tx_iter
, &tx_iter_data
);
890 if (!tx_iter_data
.receive
)
895 * reserve some space for our vendor and the normal
896 * radiotap header, since we're copying anyway
898 if (skb
->len
< PAGE_SIZE
&& paged_rx
) {
899 struct page
*page
= alloc_page(GFP_ATOMIC
);
904 nskb
= dev_alloc_skb(128);
910 memcpy(page_address(page
), skb
->data
, skb
->len
);
911 skb_add_rx_frag(nskb
, 0, page
, 0, skb
->len
, skb
->len
);
913 nskb
= skb_copy(skb
, GFP_ATOMIC
);
918 if (mac80211_hwsim_addr_match(data2
, hdr
->addr1
))
921 rx_status
.mactime
= now
+ data2
->tsf_offset
;
924 * Don't enable this code by default as the OUI 00:00:00
925 * is registered to Xerox so we shouldn't use it here, it
926 * might find its way into pcap files.
927 * Note that this code requires the headroom in the SKB
928 * that was allocated earlier.
930 rx_status
.vendor_radiotap_oui
[0] = 0x00;
931 rx_status
.vendor_radiotap_oui
[1] = 0x00;
932 rx_status
.vendor_radiotap_oui
[2] = 0x00;
933 rx_status
.vendor_radiotap_subns
= 127;
935 * Radiotap vendor namespaces can (and should) also be
936 * split into fields by using the standard radiotap
937 * presence bitmap mechanism. Use just BIT(0) here for
938 * the presence bitmap.
940 rx_status
.vendor_radiotap_bitmap
= BIT(0);
941 /* We have 8 bytes of (dummy) data */
942 rx_status
.vendor_radiotap_len
= 8;
943 /* For testing, also require it to be aligned */
944 rx_status
.vendor_radiotap_align
= 8;
946 memcpy(skb_push(nskb
, 8), "ABCDEFGH", 8);
949 memcpy(IEEE80211_SKB_RXCB(nskb
), &rx_status
, sizeof(rx_status
));
950 ieee80211_rx_irqsafe(data2
->hw
, nskb
);
952 spin_unlock(&hwsim_radio_lock
);
957 static void mac80211_hwsim_tx(struct ieee80211_hw
*hw
,
958 struct ieee80211_tx_control
*control
,
961 struct mac80211_hwsim_data
*data
= hw
->priv
;
962 struct ieee80211_tx_info
*txi
= IEEE80211_SKB_CB(skb
);
963 struct ieee80211_chanctx_conf
*chanctx_conf
;
964 struct ieee80211_channel
*channel
;
968 if (WARN_ON(skb
->len
< 10)) {
969 /* Should not happen; just a sanity check for addr1 use */
970 ieee80211_free_txskb(hw
, skb
);
974 if (data
->channels
== 1) {
975 channel
= data
->channel
;
976 } else if (txi
->hw_queue
== 4) {
977 channel
= data
->tmp_chan
;
979 chanctx_conf
= rcu_dereference(txi
->control
.vif
->chanctx_conf
);
981 channel
= chanctx_conf
->def
.chan
;
986 if (WARN(!channel
, "TX w/o channel - queue = %d\n", txi
->hw_queue
)) {
987 ieee80211_free_txskb(hw
, skb
);
991 if (data
->idle
&& !data
->tmp_chan
) {
992 wiphy_debug(hw
->wiphy
, "Trying to TX when idle - reject\n");
993 ieee80211_free_txskb(hw
, skb
);
997 if (txi
->control
.vif
)
998 hwsim_check_magic(txi
->control
.vif
);
1000 hwsim_check_sta_magic(control
->sta
);
1002 if (hw
->flags
& IEEE80211_HW_SUPPORTS_RC_TABLE
)
1003 ieee80211_get_tx_rates(txi
->control
.vif
, control
->sta
, skb
,
1005 ARRAY_SIZE(txi
->control
.rates
));
1007 txi
->rate_driver_data
[0] = channel
;
1008 mac80211_hwsim_monitor_rx(hw
, skb
, channel
);
1010 /* wmediumd mode check */
1011 _portid
= ACCESS_ONCE(wmediumd_portid
);
1014 return mac80211_hwsim_tx_frame_nl(hw
, skb
, _portid
);
1016 /* NO wmediumd detected, perfect medium simulation */
1017 ack
= mac80211_hwsim_tx_frame_no_nl(hw
, skb
, channel
);
1019 if (ack
&& skb
->len
>= 16) {
1020 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
1021 mac80211_hwsim_monitor_ack(channel
, hdr
->addr2
);
1024 ieee80211_tx_info_clear_status(txi
);
1026 /* frame was transmitted at most favorable rate at first attempt */
1027 txi
->control
.rates
[0].count
= 1;
1028 txi
->control
.rates
[1].idx
= -1;
1030 if (!(txi
->flags
& IEEE80211_TX_CTL_NO_ACK
) && ack
)
1031 txi
->flags
|= IEEE80211_TX_STAT_ACK
;
1032 ieee80211_tx_status_irqsafe(hw
, skb
);
1036 static int mac80211_hwsim_start(struct ieee80211_hw
*hw
)
1038 struct mac80211_hwsim_data
*data
= hw
->priv
;
1039 wiphy_debug(hw
->wiphy
, "%s\n", __func__
);
1040 data
->started
= true;
1045 static void mac80211_hwsim_stop(struct ieee80211_hw
*hw
)
1047 struct mac80211_hwsim_data
*data
= hw
->priv
;
1048 data
->started
= false;
1049 tasklet_hrtimer_cancel(&data
->beacon_timer
);
1050 wiphy_debug(hw
->wiphy
, "%s\n", __func__
);
1054 static int mac80211_hwsim_add_interface(struct ieee80211_hw
*hw
,
1055 struct ieee80211_vif
*vif
)
1057 wiphy_debug(hw
->wiphy
, "%s (type=%d mac_addr=%pM)\n",
1058 __func__
, ieee80211_vif_type_p2p(vif
),
1060 hwsim_set_magic(vif
);
1063 vif
->hw_queue
[IEEE80211_AC_VO
] = 0;
1064 vif
->hw_queue
[IEEE80211_AC_VI
] = 1;
1065 vif
->hw_queue
[IEEE80211_AC_BE
] = 2;
1066 vif
->hw_queue
[IEEE80211_AC_BK
] = 3;
1072 static int mac80211_hwsim_change_interface(struct ieee80211_hw
*hw
,
1073 struct ieee80211_vif
*vif
,
1074 enum nl80211_iftype newtype
,
1077 newtype
= ieee80211_iftype_p2p(newtype
, newp2p
);
1078 wiphy_debug(hw
->wiphy
,
1079 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1080 __func__
, ieee80211_vif_type_p2p(vif
),
1081 newtype
, vif
->addr
);
1082 hwsim_check_magic(vif
);
1085 * interface may change from non-AP to AP in
1086 * which case this needs to be set up again
1093 static void mac80211_hwsim_remove_interface(
1094 struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
1096 wiphy_debug(hw
->wiphy
, "%s (type=%d mac_addr=%pM)\n",
1097 __func__
, ieee80211_vif_type_p2p(vif
),
1099 hwsim_check_magic(vif
);
1100 hwsim_clear_magic(vif
);
1103 static void mac80211_hwsim_tx_frame(struct ieee80211_hw
*hw
,
1104 struct sk_buff
*skb
,
1105 struct ieee80211_channel
*chan
)
1107 u32 _pid
= ACCESS_ONCE(wmediumd_portid
);
1109 if (hw
->flags
& IEEE80211_HW_SUPPORTS_RC_TABLE
) {
1110 struct ieee80211_tx_info
*txi
= IEEE80211_SKB_CB(skb
);
1111 ieee80211_get_tx_rates(txi
->control
.vif
, NULL
, skb
,
1113 ARRAY_SIZE(txi
->control
.rates
));
1116 mac80211_hwsim_monitor_rx(hw
, skb
, chan
);
1119 return mac80211_hwsim_tx_frame_nl(hw
, skb
, _pid
);
1121 mac80211_hwsim_tx_frame_no_nl(hw
, skb
, chan
);
1125 static void mac80211_hwsim_beacon_tx(void *arg
, u8
*mac
,
1126 struct ieee80211_vif
*vif
)
1128 struct mac80211_hwsim_data
*data
= arg
;
1129 struct ieee80211_hw
*hw
= data
->hw
;
1130 struct ieee80211_tx_info
*info
;
1131 struct ieee80211_rate
*txrate
;
1132 struct ieee80211_mgmt
*mgmt
;
1133 struct sk_buff
*skb
;
1135 hwsim_check_magic(vif
);
1137 if (vif
->type
!= NL80211_IFTYPE_AP
&&
1138 vif
->type
!= NL80211_IFTYPE_MESH_POINT
&&
1139 vif
->type
!= NL80211_IFTYPE_ADHOC
)
1142 skb
= ieee80211_beacon_get(hw
, vif
);
1145 info
= IEEE80211_SKB_CB(skb
);
1146 if (hw
->flags
& IEEE80211_HW_SUPPORTS_RC_TABLE
)
1147 ieee80211_get_tx_rates(vif
, NULL
, skb
,
1148 info
->control
.rates
,
1149 ARRAY_SIZE(info
->control
.rates
));
1151 txrate
= ieee80211_get_tx_rate(hw
, info
);
1153 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
1154 /* fake header transmission time */
1155 data
->abs_bcn_ts
= mac80211_hwsim_get_tsf_raw();
1156 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(data
->abs_bcn_ts
+
1158 24 * 8 * 10 / txrate
->bitrate
);
1160 mac80211_hwsim_tx_frame(hw
, skb
,
1161 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
1164 static enum hrtimer_restart
1165 mac80211_hwsim_beacon(struct hrtimer
*timer
)
1167 struct mac80211_hwsim_data
*data
=
1168 container_of(timer
, struct mac80211_hwsim_data
,
1169 beacon_timer
.timer
);
1170 struct ieee80211_hw
*hw
= data
->hw
;
1171 u64 bcn_int
= data
->beacon_int
;
1177 ieee80211_iterate_active_interfaces_atomic(
1178 hw
, IEEE80211_IFACE_ITER_NORMAL
,
1179 mac80211_hwsim_beacon_tx
, data
);
1181 /* beacon at new TBTT + beacon interval */
1182 if (data
->bcn_delta
) {
1183 bcn_int
-= data
->bcn_delta
;
1184 data
->bcn_delta
= 0;
1187 next_bcn
= ktime_add(hrtimer_get_expires(timer
),
1188 ns_to_ktime(bcn_int
* 1000));
1189 tasklet_hrtimer_start(&data
->beacon_timer
, next_bcn
, HRTIMER_MODE_ABS
);
1191 return HRTIMER_NORESTART
;
1194 static const char * const hwsim_chanwidths
[] = {
1195 [NL80211_CHAN_WIDTH_20_NOHT
] = "noht",
1196 [NL80211_CHAN_WIDTH_20
] = "ht20",
1197 [NL80211_CHAN_WIDTH_40
] = "ht40",
1198 [NL80211_CHAN_WIDTH_80
] = "vht80",
1199 [NL80211_CHAN_WIDTH_80P80
] = "vht80p80",
1200 [NL80211_CHAN_WIDTH_160
] = "vht160",
1203 static int mac80211_hwsim_config(struct ieee80211_hw
*hw
, u32 changed
)
1205 struct mac80211_hwsim_data
*data
= hw
->priv
;
1206 struct ieee80211_conf
*conf
= &hw
->conf
;
1207 static const char *smps_modes
[IEEE80211_SMPS_NUM_MODES
] = {
1208 [IEEE80211_SMPS_AUTOMATIC
] = "auto",
1209 [IEEE80211_SMPS_OFF
] = "off",
1210 [IEEE80211_SMPS_STATIC
] = "static",
1211 [IEEE80211_SMPS_DYNAMIC
] = "dynamic",
1214 if (conf
->chandef
.chan
)
1215 wiphy_debug(hw
->wiphy
,
1216 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1218 conf
->chandef
.chan
->center_freq
,
1219 conf
->chandef
.center_freq1
,
1220 conf
->chandef
.center_freq2
,
1221 hwsim_chanwidths
[conf
->chandef
.width
],
1222 !!(conf
->flags
& IEEE80211_CONF_IDLE
),
1223 !!(conf
->flags
& IEEE80211_CONF_PS
),
1224 smps_modes
[conf
->smps_mode
]);
1226 wiphy_debug(hw
->wiphy
,
1227 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1229 !!(conf
->flags
& IEEE80211_CONF_IDLE
),
1230 !!(conf
->flags
& IEEE80211_CONF_PS
),
1231 smps_modes
[conf
->smps_mode
]);
1233 data
->idle
= !!(conf
->flags
& IEEE80211_CONF_IDLE
);
1235 data
->channel
= conf
->chandef
.chan
;
1237 WARN_ON(data
->channel
&& data
->channels
> 1);
1239 data
->power_level
= conf
->power_level
;
1240 if (!data
->started
|| !data
->beacon_int
)
1241 tasklet_hrtimer_cancel(&data
->beacon_timer
);
1242 else if (!hrtimer_is_queued(&data
->beacon_timer
.timer
)) {
1243 u64 tsf
= mac80211_hwsim_get_tsf(hw
, NULL
);
1244 u32 bcn_int
= data
->beacon_int
;
1245 u64 until_tbtt
= bcn_int
- do_div(tsf
, bcn_int
);
1247 tasklet_hrtimer_start(&data
->beacon_timer
,
1248 ns_to_ktime(until_tbtt
* 1000),
1256 static void mac80211_hwsim_configure_filter(struct ieee80211_hw
*hw
,
1257 unsigned int changed_flags
,
1258 unsigned int *total_flags
,u64 multicast
)
1260 struct mac80211_hwsim_data
*data
= hw
->priv
;
1262 wiphy_debug(hw
->wiphy
, "%s\n", __func__
);
1264 data
->rx_filter
= 0;
1265 if (*total_flags
& FIF_PROMISC_IN_BSS
)
1266 data
->rx_filter
|= FIF_PROMISC_IN_BSS
;
1267 if (*total_flags
& FIF_ALLMULTI
)
1268 data
->rx_filter
|= FIF_ALLMULTI
;
1270 *total_flags
= data
->rx_filter
;
1273 static void mac80211_hwsim_bcn_en_iter(void *data
, u8
*mac
,
1274 struct ieee80211_vif
*vif
)
1276 unsigned int *count
= data
;
1277 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
1283 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw
*hw
,
1284 struct ieee80211_vif
*vif
,
1285 struct ieee80211_bss_conf
*info
,
1288 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
1289 struct mac80211_hwsim_data
*data
= hw
->priv
;
1291 hwsim_check_magic(vif
);
1293 wiphy_debug(hw
->wiphy
, "%s(changed=0x%x vif->addr=%pM)\n",
1294 __func__
, changed
, vif
->addr
);
1296 if (changed
& BSS_CHANGED_BSSID
) {
1297 wiphy_debug(hw
->wiphy
, "%s: BSSID changed: %pM\n",
1298 __func__
, info
->bssid
);
1299 memcpy(vp
->bssid
, info
->bssid
, ETH_ALEN
);
1302 if (changed
& BSS_CHANGED_ASSOC
) {
1303 wiphy_debug(hw
->wiphy
, " ASSOC: assoc=%d aid=%d\n",
1304 info
->assoc
, info
->aid
);
1305 vp
->assoc
= info
->assoc
;
1306 vp
->aid
= info
->aid
;
1309 if (changed
& BSS_CHANGED_BEACON_INT
) {
1310 wiphy_debug(hw
->wiphy
, " BCNINT: %d\n", info
->beacon_int
);
1311 data
->beacon_int
= info
->beacon_int
* 1024;
1314 if (changed
& BSS_CHANGED_BEACON_ENABLED
) {
1315 wiphy_debug(hw
->wiphy
, " BCN EN: %d\n", info
->enable_beacon
);
1316 vp
->bcn_en
= info
->enable_beacon
;
1317 if (data
->started
&&
1318 !hrtimer_is_queued(&data
->beacon_timer
.timer
) &&
1319 info
->enable_beacon
) {
1320 u64 tsf
, until_tbtt
;
1322 if (WARN_ON(!data
->beacon_int
))
1323 data
->beacon_int
= 1000 * 1024;
1324 tsf
= mac80211_hwsim_get_tsf(hw
, vif
);
1325 bcn_int
= data
->beacon_int
;
1326 until_tbtt
= bcn_int
- do_div(tsf
, bcn_int
);
1327 tasklet_hrtimer_start(&data
->beacon_timer
,
1328 ns_to_ktime(until_tbtt
* 1000),
1330 } else if (!info
->enable_beacon
) {
1331 unsigned int count
= 0;
1332 ieee80211_iterate_active_interfaces_atomic(
1333 data
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
1334 mac80211_hwsim_bcn_en_iter
, &count
);
1335 wiphy_debug(hw
->wiphy
, " beaconing vifs remaining: %u",
1338 tasklet_hrtimer_cancel(&data
->beacon_timer
);
1342 if (changed
& BSS_CHANGED_ERP_CTS_PROT
) {
1343 wiphy_debug(hw
->wiphy
, " ERP_CTS_PROT: %d\n",
1344 info
->use_cts_prot
);
1347 if (changed
& BSS_CHANGED_ERP_PREAMBLE
) {
1348 wiphy_debug(hw
->wiphy
, " ERP_PREAMBLE: %d\n",
1349 info
->use_short_preamble
);
1352 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1353 wiphy_debug(hw
->wiphy
, " ERP_SLOT: %d\n", info
->use_short_slot
);
1356 if (changed
& BSS_CHANGED_HT
) {
1357 wiphy_debug(hw
->wiphy
, " HT: op_mode=0x%x\n",
1358 info
->ht_operation_mode
);
1361 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1362 wiphy_debug(hw
->wiphy
, " BASIC_RATES: 0x%llx\n",
1363 (unsigned long long) info
->basic_rates
);
1366 if (changed
& BSS_CHANGED_TXPOWER
)
1367 wiphy_debug(hw
->wiphy
, " TX Power: %d dBm\n", info
->txpower
);
1370 static int mac80211_hwsim_sta_add(struct ieee80211_hw
*hw
,
1371 struct ieee80211_vif
*vif
,
1372 struct ieee80211_sta
*sta
)
1374 hwsim_check_magic(vif
);
1375 hwsim_set_sta_magic(sta
);
1380 static int mac80211_hwsim_sta_remove(struct ieee80211_hw
*hw
,
1381 struct ieee80211_vif
*vif
,
1382 struct ieee80211_sta
*sta
)
1384 hwsim_check_magic(vif
);
1385 hwsim_clear_sta_magic(sta
);
1390 static void mac80211_hwsim_sta_notify(struct ieee80211_hw
*hw
,
1391 struct ieee80211_vif
*vif
,
1392 enum sta_notify_cmd cmd
,
1393 struct ieee80211_sta
*sta
)
1395 hwsim_check_magic(vif
);
1398 case STA_NOTIFY_SLEEP
:
1399 case STA_NOTIFY_AWAKE
:
1400 /* TODO: make good use of these flags */
1403 WARN(1, "Invalid sta notify: %d\n", cmd
);
1408 static int mac80211_hwsim_set_tim(struct ieee80211_hw
*hw
,
1409 struct ieee80211_sta
*sta
,
1412 hwsim_check_sta_magic(sta
);
1416 static int mac80211_hwsim_conf_tx(
1417 struct ieee80211_hw
*hw
,
1418 struct ieee80211_vif
*vif
, u16 queue
,
1419 const struct ieee80211_tx_queue_params
*params
)
1421 wiphy_debug(hw
->wiphy
,
1422 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1424 params
->txop
, params
->cw_min
,
1425 params
->cw_max
, params
->aifs
);
1429 static int mac80211_hwsim_get_survey(
1430 struct ieee80211_hw
*hw
, int idx
,
1431 struct survey_info
*survey
)
1433 struct ieee80211_conf
*conf
= &hw
->conf
;
1435 wiphy_debug(hw
->wiphy
, "%s (idx=%d)\n", __func__
, idx
);
1440 /* Current channel */
1441 survey
->channel
= conf
->chandef
.chan
;
1444 * Magically conjured noise level --- this is only ok for simulated hardware.
1446 * A real driver which cannot determine the real channel noise MUST NOT
1447 * report any noise, especially not a magically conjured one :-)
1449 survey
->filled
= SURVEY_INFO_NOISE_DBM
;
1450 survey
->noise
= -92;
1455 #ifdef CONFIG_NL80211_TESTMODE
1457 * This section contains example code for using netlink
1458 * attributes with the testmode command in nl80211.
1461 /* These enums need to be kept in sync with userspace */
1462 enum hwsim_testmode_attr
{
1463 __HWSIM_TM_ATTR_INVALID
= 0,
1464 HWSIM_TM_ATTR_CMD
= 1,
1465 HWSIM_TM_ATTR_PS
= 2,
1468 __HWSIM_TM_ATTR_AFTER_LAST
,
1469 HWSIM_TM_ATTR_MAX
= __HWSIM_TM_ATTR_AFTER_LAST
- 1
1472 enum hwsim_testmode_cmd
{
1473 HWSIM_TM_CMD_SET_PS
= 0,
1474 HWSIM_TM_CMD_GET_PS
= 1,
1475 HWSIM_TM_CMD_STOP_QUEUES
= 2,
1476 HWSIM_TM_CMD_WAKE_QUEUES
= 3,
1479 static const struct nla_policy hwsim_testmode_policy
[HWSIM_TM_ATTR_MAX
+ 1] = {
1480 [HWSIM_TM_ATTR_CMD
] = { .type
= NLA_U32
},
1481 [HWSIM_TM_ATTR_PS
] = { .type
= NLA_U32
},
1484 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw
*hw
,
1485 struct ieee80211_vif
*vif
,
1486 void *data
, int len
)
1488 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1489 struct nlattr
*tb
[HWSIM_TM_ATTR_MAX
+ 1];
1490 struct sk_buff
*skb
;
1493 err
= nla_parse(tb
, HWSIM_TM_ATTR_MAX
, data
, len
,
1494 hwsim_testmode_policy
);
1498 if (!tb
[HWSIM_TM_ATTR_CMD
])
1501 switch (nla_get_u32(tb
[HWSIM_TM_ATTR_CMD
])) {
1502 case HWSIM_TM_CMD_SET_PS
:
1503 if (!tb
[HWSIM_TM_ATTR_PS
])
1505 ps
= nla_get_u32(tb
[HWSIM_TM_ATTR_PS
]);
1506 return hwsim_fops_ps_write(hwsim
, ps
);
1507 case HWSIM_TM_CMD_GET_PS
:
1508 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
,
1509 nla_total_size(sizeof(u32
)));
1512 if (nla_put_u32(skb
, HWSIM_TM_ATTR_PS
, hwsim
->ps
))
1513 goto nla_put_failure
;
1514 return cfg80211_testmode_reply(skb
);
1515 case HWSIM_TM_CMD_STOP_QUEUES
:
1516 ieee80211_stop_queues(hw
);
1518 case HWSIM_TM_CMD_WAKE_QUEUES
:
1519 ieee80211_wake_queues(hw
);
1531 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw
*hw
,
1532 struct ieee80211_vif
*vif
,
1533 enum ieee80211_ampdu_mlme_action action
,
1534 struct ieee80211_sta
*sta
, u16 tid
, u16
*ssn
,
1538 case IEEE80211_AMPDU_TX_START
:
1539 ieee80211_start_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
1541 case IEEE80211_AMPDU_TX_STOP_CONT
:
1542 case IEEE80211_AMPDU_TX_STOP_FLUSH
:
1543 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
:
1544 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
1546 case IEEE80211_AMPDU_TX_OPERATIONAL
:
1548 case IEEE80211_AMPDU_RX_START
:
1549 case IEEE80211_AMPDU_RX_STOP
:
1558 static void mac80211_hwsim_flush(struct ieee80211_hw
*hw
, u32 queues
, bool drop
)
1560 /* Not implemented, queues only on kernel side */
1563 static void hw_scan_work(struct work_struct
*work
)
1565 struct mac80211_hwsim_data
*hwsim
=
1566 container_of(work
, struct mac80211_hwsim_data
, hw_scan
.work
);
1567 struct cfg80211_scan_request
*req
= hwsim
->hw_scan_request
;
1570 mutex_lock(&hwsim
->mutex
);
1571 if (hwsim
->scan_chan_idx
>= req
->n_channels
) {
1572 wiphy_debug(hwsim
->hw
->wiphy
, "hw scan complete\n");
1573 ieee80211_scan_completed(hwsim
->hw
, false);
1574 hwsim
->hw_scan_request
= NULL
;
1575 hwsim
->hw_scan_vif
= NULL
;
1576 hwsim
->tmp_chan
= NULL
;
1577 mutex_unlock(&hwsim
->mutex
);
1581 wiphy_debug(hwsim
->hw
->wiphy
, "hw scan %d MHz\n",
1582 req
->channels
[hwsim
->scan_chan_idx
]->center_freq
);
1584 hwsim
->tmp_chan
= req
->channels
[hwsim
->scan_chan_idx
];
1585 if (hwsim
->tmp_chan
->flags
& IEEE80211_CHAN_NO_IR
||
1591 for (i
= 0; i
< req
->n_ssids
; i
++) {
1592 struct sk_buff
*probe
;
1594 probe
= ieee80211_probereq_get(hwsim
->hw
,
1597 req
->ssids
[i
].ssid_len
,
1603 memcpy(skb_put(probe
, req
->ie_len
), req
->ie
,
1607 mac80211_hwsim_tx_frame(hwsim
->hw
, probe
,
1612 ieee80211_queue_delayed_work(hwsim
->hw
, &hwsim
->hw_scan
,
1613 msecs_to_jiffies(dwell
));
1614 hwsim
->scan_chan_idx
++;
1615 mutex_unlock(&hwsim
->mutex
);
1618 static int mac80211_hwsim_hw_scan(struct ieee80211_hw
*hw
,
1619 struct ieee80211_vif
*vif
,
1620 struct cfg80211_scan_request
*req
)
1622 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1624 mutex_lock(&hwsim
->mutex
);
1625 if (WARN_ON(hwsim
->tmp_chan
|| hwsim
->hw_scan_request
)) {
1626 mutex_unlock(&hwsim
->mutex
);
1629 hwsim
->hw_scan_request
= req
;
1630 hwsim
->hw_scan_vif
= vif
;
1631 hwsim
->scan_chan_idx
= 0;
1632 mutex_unlock(&hwsim
->mutex
);
1634 wiphy_debug(hw
->wiphy
, "hwsim hw_scan request\n");
1636 ieee80211_queue_delayed_work(hwsim
->hw
, &hwsim
->hw_scan
, 0);
1641 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw
*hw
,
1642 struct ieee80211_vif
*vif
)
1644 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1646 wiphy_debug(hw
->wiphy
, "hwsim cancel_hw_scan\n");
1648 cancel_delayed_work_sync(&hwsim
->hw_scan
);
1650 mutex_lock(&hwsim
->mutex
);
1651 ieee80211_scan_completed(hwsim
->hw
, true);
1652 hwsim
->tmp_chan
= NULL
;
1653 hwsim
->hw_scan_request
= NULL
;
1654 hwsim
->hw_scan_vif
= NULL
;
1655 mutex_unlock(&hwsim
->mutex
);
1658 static void mac80211_hwsim_sw_scan(struct ieee80211_hw
*hw
)
1660 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1662 mutex_lock(&hwsim
->mutex
);
1664 if (hwsim
->scanning
) {
1665 printk(KERN_DEBUG
"two hwsim sw_scans detected!\n");
1669 printk(KERN_DEBUG
"hwsim sw_scan request, prepping stuff\n");
1670 hwsim
->scanning
= true;
1673 mutex_unlock(&hwsim
->mutex
);
1676 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw
*hw
)
1678 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1680 mutex_lock(&hwsim
->mutex
);
1682 printk(KERN_DEBUG
"hwsim sw_scan_complete\n");
1683 hwsim
->scanning
= false;
1685 mutex_unlock(&hwsim
->mutex
);
1688 static void hw_roc_done(struct work_struct
*work
)
1690 struct mac80211_hwsim_data
*hwsim
=
1691 container_of(work
, struct mac80211_hwsim_data
, roc_done
.work
);
1693 mutex_lock(&hwsim
->mutex
);
1694 ieee80211_remain_on_channel_expired(hwsim
->hw
);
1695 hwsim
->tmp_chan
= NULL
;
1696 mutex_unlock(&hwsim
->mutex
);
1698 wiphy_debug(hwsim
->hw
->wiphy
, "hwsim ROC expired\n");
1701 static int mac80211_hwsim_roc(struct ieee80211_hw
*hw
,
1702 struct ieee80211_vif
*vif
,
1703 struct ieee80211_channel
*chan
,
1705 enum ieee80211_roc_type type
)
1707 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1709 mutex_lock(&hwsim
->mutex
);
1710 if (WARN_ON(hwsim
->tmp_chan
|| hwsim
->hw_scan_request
)) {
1711 mutex_unlock(&hwsim
->mutex
);
1715 hwsim
->tmp_chan
= chan
;
1716 mutex_unlock(&hwsim
->mutex
);
1718 wiphy_debug(hw
->wiphy
, "hwsim ROC (%d MHz, %d ms)\n",
1719 chan
->center_freq
, duration
);
1721 ieee80211_ready_on_channel(hw
);
1723 ieee80211_queue_delayed_work(hw
, &hwsim
->roc_done
,
1724 msecs_to_jiffies(duration
));
1728 static int mac80211_hwsim_croc(struct ieee80211_hw
*hw
)
1730 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1732 cancel_delayed_work_sync(&hwsim
->roc_done
);
1734 mutex_lock(&hwsim
->mutex
);
1735 hwsim
->tmp_chan
= NULL
;
1736 mutex_unlock(&hwsim
->mutex
);
1738 wiphy_debug(hw
->wiphy
, "hwsim ROC canceled\n");
1743 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw
*hw
,
1744 struct ieee80211_chanctx_conf
*ctx
)
1746 hwsim_set_chanctx_magic(ctx
);
1747 wiphy_debug(hw
->wiphy
,
1748 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1749 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
1750 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
1754 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw
*hw
,
1755 struct ieee80211_chanctx_conf
*ctx
)
1757 wiphy_debug(hw
->wiphy
,
1758 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1759 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
1760 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
1761 hwsim_check_chanctx_magic(ctx
);
1762 hwsim_clear_chanctx_magic(ctx
);
1765 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw
*hw
,
1766 struct ieee80211_chanctx_conf
*ctx
,
1769 hwsim_check_chanctx_magic(ctx
);
1770 wiphy_debug(hw
->wiphy
,
1771 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1772 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
1773 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
1776 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw
*hw
,
1777 struct ieee80211_vif
*vif
,
1778 struct ieee80211_chanctx_conf
*ctx
)
1780 hwsim_check_magic(vif
);
1781 hwsim_check_chanctx_magic(ctx
);
1786 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw
*hw
,
1787 struct ieee80211_vif
*vif
,
1788 struct ieee80211_chanctx_conf
*ctx
)
1790 hwsim_check_magic(vif
);
1791 hwsim_check_chanctx_magic(ctx
);
1794 static const struct ieee80211_ops mac80211_hwsim_ops
= {
1795 .tx
= mac80211_hwsim_tx
,
1796 .start
= mac80211_hwsim_start
,
1797 .stop
= mac80211_hwsim_stop
,
1798 .add_interface
= mac80211_hwsim_add_interface
,
1799 .change_interface
= mac80211_hwsim_change_interface
,
1800 .remove_interface
= mac80211_hwsim_remove_interface
,
1801 .config
= mac80211_hwsim_config
,
1802 .configure_filter
= mac80211_hwsim_configure_filter
,
1803 .bss_info_changed
= mac80211_hwsim_bss_info_changed
,
1804 .sta_add
= mac80211_hwsim_sta_add
,
1805 .sta_remove
= mac80211_hwsim_sta_remove
,
1806 .sta_notify
= mac80211_hwsim_sta_notify
,
1807 .set_tim
= mac80211_hwsim_set_tim
,
1808 .conf_tx
= mac80211_hwsim_conf_tx
,
1809 .get_survey
= mac80211_hwsim_get_survey
,
1810 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd
)
1811 .ampdu_action
= mac80211_hwsim_ampdu_action
,
1812 .sw_scan_start
= mac80211_hwsim_sw_scan
,
1813 .sw_scan_complete
= mac80211_hwsim_sw_scan_complete
,
1814 .flush
= mac80211_hwsim_flush
,
1815 .get_tsf
= mac80211_hwsim_get_tsf
,
1816 .set_tsf
= mac80211_hwsim_set_tsf
,
1819 static struct ieee80211_ops mac80211_hwsim_mchan_ops
;
1821 static int __init
mac80211_hwsim_create_radio(void)
1825 struct mac80211_hwsim_data
*data
;
1826 struct ieee80211_hw
*hw
;
1827 enum ieee80211_band band
;
1828 const struct ieee80211_ops
*ops
= &mac80211_hwsim_ops
;
1831 spin_lock_bh(&hwsim_radio_lock
);
1832 idx
= hwsim_radio_idx
++;
1833 spin_unlock_bh(&hwsim_radio_lock
);
1836 ops
= &mac80211_hwsim_mchan_ops
;
1837 hw
= ieee80211_alloc_hw(sizeof(*data
), ops
);
1839 printk(KERN_DEBUG
"mac80211_hwsim: ieee80211_alloc_hw failed\n");
1846 data
->dev
= device_create(hwsim_class
, NULL
, 0, hw
, "hwsim%d", idx
);
1847 if (IS_ERR(data
->dev
)) {
1849 "mac80211_hwsim: device_create failed (%ld)\n",
1850 PTR_ERR(data
->dev
));
1852 goto failed_drvdata
;
1854 data
->dev
->driver
= &mac80211_hwsim_driver
.driver
;
1855 err
= device_bind_driver(data
->dev
);
1857 printk(KERN_DEBUG
"mac80211_hwsim: device_bind_driver failed (%d)\n",
1862 skb_queue_head_init(&data
->pending
);
1864 SET_IEEE80211_DEV(hw
, data
->dev
);
1865 memset(addr
, 0, ETH_ALEN
);
1869 memcpy(data
->addresses
[0].addr
, addr
, ETH_ALEN
);
1870 memcpy(data
->addresses
[1].addr
, addr
, ETH_ALEN
);
1871 data
->addresses
[1].addr
[0] |= 0x40;
1872 hw
->wiphy
->n_addresses
= 2;
1873 hw
->wiphy
->addresses
= data
->addresses
;
1875 data
->channels
= channels
;
1877 if (data
->channels
> 1) {
1878 hw
->wiphy
->max_scan_ssids
= 255;
1879 hw
->wiphy
->max_scan_ie_len
= IEEE80211_MAX_DATA_LEN
;
1880 hw
->wiphy
->max_remain_on_channel_duration
= 1000;
1881 /* For channels > 1 DFS is not allowed */
1882 hw
->wiphy
->n_iface_combinations
= 1;
1883 hw
->wiphy
->iface_combinations
= &data
->if_combination
;
1884 data
->if_combination
= hwsim_if_comb
[0];
1885 data
->if_combination
.num_different_channels
= data
->channels
;
1887 hw
->wiphy
->iface_combinations
= hwsim_if_comb
;
1888 hw
->wiphy
->n_iface_combinations
= ARRAY_SIZE(hwsim_if_comb
);
1891 INIT_DELAYED_WORK(&data
->roc_done
, hw_roc_done
);
1892 INIT_DELAYED_WORK(&data
->hw_scan
, hw_scan_work
);
1895 hw
->offchannel_tx_hw_queue
= 4;
1896 hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
) |
1897 BIT(NL80211_IFTYPE_AP
) |
1898 BIT(NL80211_IFTYPE_P2P_CLIENT
) |
1899 BIT(NL80211_IFTYPE_P2P_GO
) |
1900 BIT(NL80211_IFTYPE_ADHOC
) |
1901 BIT(NL80211_IFTYPE_MESH_POINT
) |
1902 BIT(NL80211_IFTYPE_P2P_DEVICE
);
1904 hw
->flags
= IEEE80211_HW_MFP_CAPABLE
|
1905 IEEE80211_HW_SIGNAL_DBM
|
1906 IEEE80211_HW_SUPPORTS_STATIC_SMPS
|
1907 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS
|
1908 IEEE80211_HW_AMPDU_AGGREGATION
|
1909 IEEE80211_HW_WANT_MONITOR_VIF
|
1910 IEEE80211_HW_QUEUE_CONTROL
|
1911 IEEE80211_HW_SUPPORTS_HT_CCK_RATES
;
1913 hw
->flags
|= IEEE80211_HW_SUPPORTS_RC_TABLE
;
1915 hw
->wiphy
->flags
|= WIPHY_FLAG_SUPPORTS_TDLS
|
1916 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL
|
1917 WIPHY_FLAG_AP_UAPSD
;
1918 hw
->wiphy
->features
|= NL80211_FEATURE_ACTIVE_MONITOR
;
1920 /* ask mac80211 to reserve space for magic */
1921 hw
->vif_data_size
= sizeof(struct hwsim_vif_priv
);
1922 hw
->sta_data_size
= sizeof(struct hwsim_sta_priv
);
1923 hw
->chanctx_data_size
= sizeof(struct hwsim_chanctx_priv
);
1925 memcpy(data
->channels_2ghz
, hwsim_channels_2ghz
,
1926 sizeof(hwsim_channels_2ghz
));
1927 memcpy(data
->channels_5ghz
, hwsim_channels_5ghz
,
1928 sizeof(hwsim_channels_5ghz
));
1929 memcpy(data
->rates
, hwsim_rates
, sizeof(hwsim_rates
));
1931 for (band
= IEEE80211_BAND_2GHZ
; band
< IEEE80211_NUM_BANDS
; band
++) {
1932 struct ieee80211_supported_band
*sband
= &data
->bands
[band
];
1934 case IEEE80211_BAND_2GHZ
:
1935 sband
->channels
= data
->channels_2ghz
;
1936 sband
->n_channels
= ARRAY_SIZE(hwsim_channels_2ghz
);
1937 sband
->bitrates
= data
->rates
;
1938 sband
->n_bitrates
= ARRAY_SIZE(hwsim_rates
);
1940 case IEEE80211_BAND_5GHZ
:
1941 sband
->channels
= data
->channels_5ghz
;
1942 sband
->n_channels
= ARRAY_SIZE(hwsim_channels_5ghz
);
1943 sband
->bitrates
= data
->rates
+ 4;
1944 sband
->n_bitrates
= ARRAY_SIZE(hwsim_rates
) - 4;
1950 sband
->ht_cap
.ht_supported
= true;
1951 sband
->ht_cap
.cap
= IEEE80211_HT_CAP_SUP_WIDTH_20_40
|
1952 IEEE80211_HT_CAP_GRN_FLD
|
1953 IEEE80211_HT_CAP_SGI_40
|
1954 IEEE80211_HT_CAP_DSSSCCK40
;
1955 sband
->ht_cap
.ampdu_factor
= 0x3;
1956 sband
->ht_cap
.ampdu_density
= 0x6;
1957 memset(&sband
->ht_cap
.mcs
, 0,
1958 sizeof(sband
->ht_cap
.mcs
));
1959 sband
->ht_cap
.mcs
.rx_mask
[0] = 0xff;
1960 sband
->ht_cap
.mcs
.rx_mask
[1] = 0xff;
1961 sband
->ht_cap
.mcs
.tx_params
= IEEE80211_HT_MCS_TX_DEFINED
;
1963 hw
->wiphy
->bands
[band
] = sband
;
1965 sband
->vht_cap
.vht_supported
= true;
1966 sband
->vht_cap
.cap
=
1967 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454
|
1968 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ
|
1969 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ
|
1970 IEEE80211_VHT_CAP_RXLDPC
|
1971 IEEE80211_VHT_CAP_SHORT_GI_80
|
1972 IEEE80211_VHT_CAP_SHORT_GI_160
|
1973 IEEE80211_VHT_CAP_TXSTBC
|
1974 IEEE80211_VHT_CAP_RXSTBC_1
|
1975 IEEE80211_VHT_CAP_RXSTBC_2
|
1976 IEEE80211_VHT_CAP_RXSTBC_3
|
1977 IEEE80211_VHT_CAP_RXSTBC_4
|
1978 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK
;
1979 sband
->vht_cap
.vht_mcs
.rx_mcs_map
=
1980 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8
<< 0 |
1981 IEEE80211_VHT_MCS_SUPPORT_0_8
<< 2 |
1982 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 4 |
1983 IEEE80211_VHT_MCS_SUPPORT_0_8
<< 6 |
1984 IEEE80211_VHT_MCS_SUPPORT_0_8
<< 8 |
1985 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 10 |
1986 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 12 |
1987 IEEE80211_VHT_MCS_SUPPORT_0_8
<< 14);
1988 sband
->vht_cap
.vht_mcs
.tx_mcs_map
=
1989 sband
->vht_cap
.vht_mcs
.rx_mcs_map
;
1992 /* By default all radios belong to the first group */
1994 mutex_init(&data
->mutex
);
1996 /* Enable frame retransmissions for lossy channels */
1998 hw
->max_rate_tries
= 11;
2000 err
= ieee80211_register_hw(hw
);
2002 printk(KERN_DEBUG
"mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2007 wiphy_debug(hw
->wiphy
, "hwaddr %pM registered\n", hw
->wiphy
->perm_addr
);
2009 data
->debugfs
= debugfs_create_dir("hwsim", hw
->wiphy
->debugfsdir
);
2010 debugfs_create_file("ps", 0666, data
->debugfs
, data
, &hwsim_fops_ps
);
2011 debugfs_create_file("group", 0666, data
->debugfs
, data
,
2013 if (data
->channels
== 1)
2014 debugfs_create_file("dfs_simulate_radar", 0222,
2016 data
, &hwsim_simulate_radar
);
2018 tasklet_hrtimer_init(&data
->beacon_timer
,
2019 mac80211_hwsim_beacon
,
2020 CLOCK_MONOTONIC_RAW
, HRTIMER_MODE_ABS
);
2022 spin_lock_bh(&hwsim_radio_lock
);
2023 list_add_tail(&data
->list
, &hwsim_radios
);
2024 spin_unlock_bh(&hwsim_radio_lock
);
2029 device_unregister(data
->dev
);
2031 ieee80211_free_hw(hw
);
2036 static void mac80211_hwsim_destroy_radio(struct mac80211_hwsim_data
*data
)
2038 debugfs_remove_recursive(data
->debugfs
);
2039 ieee80211_unregister_hw(data
->hw
);
2040 device_release_driver(data
->dev
);
2041 device_unregister(data
->dev
);
2042 ieee80211_free_hw(data
->hw
);
2045 static void mac80211_hwsim_free(void)
2047 struct mac80211_hwsim_data
*data
;
2049 spin_lock_bh(&hwsim_radio_lock
);
2050 while ((data
= list_first_entry_or_null(&hwsim_radios
,
2051 struct mac80211_hwsim_data
,
2053 list_del(&data
->list
);
2054 spin_unlock_bh(&hwsim_radio_lock
);
2055 mac80211_hwsim_destroy_radio(data
);
2056 spin_lock_bh(&hwsim_radio_lock
);
2058 spin_unlock_bh(&hwsim_radio_lock
);
2059 class_destroy(hwsim_class
);
2062 static const struct net_device_ops hwsim_netdev_ops
= {
2063 .ndo_start_xmit
= hwsim_mon_xmit
,
2064 .ndo_change_mtu
= eth_change_mtu
,
2065 .ndo_set_mac_address
= eth_mac_addr
,
2066 .ndo_validate_addr
= eth_validate_addr
,
2069 static void hwsim_mon_setup(struct net_device
*dev
)
2071 dev
->netdev_ops
= &hwsim_netdev_ops
;
2072 dev
->destructor
= free_netdev
;
2074 dev
->tx_queue_len
= 0;
2075 dev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
2076 memset(dev
->dev_addr
, 0, ETH_ALEN
);
2077 dev
->dev_addr
[0] = 0x12;
2080 static struct mac80211_hwsim_data
*get_hwsim_data_ref_from_addr(const u8
*addr
)
2082 struct mac80211_hwsim_data
*data
;
2083 bool _found
= false;
2085 spin_lock_bh(&hwsim_radio_lock
);
2086 list_for_each_entry(data
, &hwsim_radios
, list
) {
2087 if (memcmp(data
->addresses
[1].addr
, addr
, ETH_ALEN
) == 0) {
2092 spin_unlock_bh(&hwsim_radio_lock
);
2100 static int hwsim_tx_info_frame_received_nl(struct sk_buff
*skb_2
,
2101 struct genl_info
*info
)
2104 struct ieee80211_hdr
*hdr
;
2105 struct mac80211_hwsim_data
*data2
;
2106 struct ieee80211_tx_info
*txi
;
2107 struct hwsim_tx_rate
*tx_attempts
;
2108 unsigned long ret_skb_ptr
;
2109 struct sk_buff
*skb
, *tmp
;
2111 unsigned int hwsim_flags
;
2115 if (info
->snd_portid
!= wmediumd_portid
)
2118 if (!info
->attrs
[HWSIM_ATTR_ADDR_TRANSMITTER
] ||
2119 !info
->attrs
[HWSIM_ATTR_FLAGS
] ||
2120 !info
->attrs
[HWSIM_ATTR_COOKIE
] ||
2121 !info
->attrs
[HWSIM_ATTR_TX_INFO
])
2124 src
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_ADDR_TRANSMITTER
]);
2125 hwsim_flags
= nla_get_u32(info
->attrs
[HWSIM_ATTR_FLAGS
]);
2126 ret_skb_ptr
= nla_get_u64(info
->attrs
[HWSIM_ATTR_COOKIE
]);
2128 data2
= get_hwsim_data_ref_from_addr(src
);
2132 /* look for the skb matching the cookie passed back from user */
2133 skb_queue_walk_safe(&data2
->pending
, skb
, tmp
) {
2134 if ((unsigned long)skb
== ret_skb_ptr
) {
2135 skb_unlink(skb
, &data2
->pending
);
2145 /* Tx info received because the frame was broadcasted on user space,
2146 so we get all the necessary info: tx attempts and skb control buff */
2148 tx_attempts
= (struct hwsim_tx_rate
*)nla_data(
2149 info
->attrs
[HWSIM_ATTR_TX_INFO
]);
2151 /* now send back TX status */
2152 txi
= IEEE80211_SKB_CB(skb
);
2154 ieee80211_tx_info_clear_status(txi
);
2156 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
2157 txi
->status
.rates
[i
].idx
= tx_attempts
[i
].idx
;
2158 txi
->status
.rates
[i
].count
= tx_attempts
[i
].count
;
2159 /*txi->status.rates[i].flags = 0;*/
2162 txi
->status
.ack_signal
= nla_get_u32(info
->attrs
[HWSIM_ATTR_SIGNAL
]);
2164 if (!(hwsim_flags
& HWSIM_TX_CTL_NO_ACK
) &&
2165 (hwsim_flags
& HWSIM_TX_STAT_ACK
)) {
2166 if (skb
->len
>= 16) {
2167 hdr
= (struct ieee80211_hdr
*) skb
->data
;
2168 mac80211_hwsim_monitor_ack(txi
->rate_driver_data
[0],
2171 txi
->flags
|= IEEE80211_TX_STAT_ACK
;
2173 ieee80211_tx_status_irqsafe(data2
->hw
, skb
);
2180 static int hwsim_cloned_frame_received_nl(struct sk_buff
*skb_2
,
2181 struct genl_info
*info
)
2184 struct mac80211_hwsim_data
*data2
;
2185 struct ieee80211_rx_status rx_status
;
2189 struct sk_buff
*skb
= NULL
;
2191 if (info
->snd_portid
!= wmediumd_portid
)
2194 if (!info
->attrs
[HWSIM_ATTR_ADDR_RECEIVER
] ||
2195 !info
->attrs
[HWSIM_ATTR_FRAME
] ||
2196 !info
->attrs
[HWSIM_ATTR_RX_RATE
] ||
2197 !info
->attrs
[HWSIM_ATTR_SIGNAL
])
2200 dst
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_ADDR_RECEIVER
]);
2201 frame_data_len
= nla_len(info
->attrs
[HWSIM_ATTR_FRAME
]);
2202 frame_data
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_FRAME
]);
2204 /* Allocate new skb here */
2205 skb
= alloc_skb(frame_data_len
, GFP_KERNEL
);
2209 if (frame_data_len
> IEEE80211_MAX_DATA_LEN
)
2213 memcpy(skb_put(skb
, frame_data_len
), frame_data
, frame_data_len
);
2215 data2
= get_hwsim_data_ref_from_addr(dst
);
2219 /* check if radio is configured properly */
2221 if (data2
->idle
|| !data2
->started
)
2224 /* A frame is received from user space */
2225 memset(&rx_status
, 0, sizeof(rx_status
));
2226 rx_status
.freq
= data2
->channel
->center_freq
;
2227 rx_status
.band
= data2
->channel
->band
;
2228 rx_status
.rate_idx
= nla_get_u32(info
->attrs
[HWSIM_ATTR_RX_RATE
]);
2229 rx_status
.signal
= nla_get_u32(info
->attrs
[HWSIM_ATTR_SIGNAL
]);
2231 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
2232 ieee80211_rx_irqsafe(data2
->hw
, skb
);
2236 printk(KERN_DEBUG
"mac80211_hwsim: error occurred in %s\n", __func__
);
2243 static int hwsim_register_received_nl(struct sk_buff
*skb_2
,
2244 struct genl_info
*info
)
2246 if (wmediumd_portid
)
2249 wmediumd_portid
= info
->snd_portid
;
2251 printk(KERN_DEBUG
"mac80211_hwsim: received a REGISTER, "
2252 "switching to wmediumd mode with pid %d\n", info
->snd_portid
);
2257 /* Generic Netlink operations array */
2258 static const struct genl_ops hwsim_ops
[] = {
2260 .cmd
= HWSIM_CMD_REGISTER
,
2261 .policy
= hwsim_genl_policy
,
2262 .doit
= hwsim_register_received_nl
,
2263 .flags
= GENL_ADMIN_PERM
,
2266 .cmd
= HWSIM_CMD_FRAME
,
2267 .policy
= hwsim_genl_policy
,
2268 .doit
= hwsim_cloned_frame_received_nl
,
2271 .cmd
= HWSIM_CMD_TX_INFO_FRAME
,
2272 .policy
= hwsim_genl_policy
,
2273 .doit
= hwsim_tx_info_frame_received_nl
,
2277 static int mac80211_hwsim_netlink_notify(struct notifier_block
*nb
,
2278 unsigned long state
,
2281 struct netlink_notify
*notify
= _notify
;
2283 if (state
!= NETLINK_URELEASE
)
2286 if (notify
->portid
== wmediumd_portid
) {
2287 printk(KERN_INFO
"mac80211_hwsim: wmediumd released netlink"
2288 " socket, switching to perfect channel medium\n");
2289 wmediumd_portid
= 0;
2295 static struct notifier_block hwsim_netlink_notifier
= {
2296 .notifier_call
= mac80211_hwsim_netlink_notify
,
2299 static int hwsim_init_netlink(void)
2303 /* userspace test API hasn't been adjusted for multi-channel */
2307 printk(KERN_INFO
"mac80211_hwsim: initializing netlink\n");
2309 rc
= genl_register_family_with_ops(&hwsim_genl_family
, hwsim_ops
);
2313 rc
= netlink_register_notifier(&hwsim_netlink_notifier
);
2320 printk(KERN_DEBUG
"mac80211_hwsim: error occurred in %s\n", __func__
);
2324 static void hwsim_exit_netlink(void)
2326 /* userspace test API hasn't been adjusted for multi-channel */
2330 /* unregister the notifier */
2331 netlink_unregister_notifier(&hwsim_netlink_notifier
);
2332 /* unregister the family */
2333 genl_unregister_family(&hwsim_genl_family
);
2336 static int __init
init_mac80211_hwsim(void)
2340 if (radios
< 1 || radios
> 100)
2346 mac80211_hwsim_mchan_ops
= mac80211_hwsim_ops
;
2347 mac80211_hwsim_mchan_ops
.hw_scan
= mac80211_hwsim_hw_scan
;
2348 mac80211_hwsim_mchan_ops
.cancel_hw_scan
= mac80211_hwsim_cancel_hw_scan
;
2349 mac80211_hwsim_mchan_ops
.sw_scan_start
= NULL
;
2350 mac80211_hwsim_mchan_ops
.sw_scan_complete
= NULL
;
2351 mac80211_hwsim_mchan_ops
.remain_on_channel
= mac80211_hwsim_roc
;
2352 mac80211_hwsim_mchan_ops
.cancel_remain_on_channel
= mac80211_hwsim_croc
;
2353 mac80211_hwsim_mchan_ops
.add_chanctx
= mac80211_hwsim_add_chanctx
;
2354 mac80211_hwsim_mchan_ops
.remove_chanctx
= mac80211_hwsim_remove_chanctx
;
2355 mac80211_hwsim_mchan_ops
.change_chanctx
= mac80211_hwsim_change_chanctx
;
2356 mac80211_hwsim_mchan_ops
.assign_vif_chanctx
=
2357 mac80211_hwsim_assign_vif_chanctx
;
2358 mac80211_hwsim_mchan_ops
.unassign_vif_chanctx
=
2359 mac80211_hwsim_unassign_vif_chanctx
;
2361 spin_lock_init(&hwsim_radio_lock
);
2362 INIT_LIST_HEAD(&hwsim_radios
);
2364 err
= platform_driver_register(&mac80211_hwsim_driver
);
2368 hwsim_class
= class_create(THIS_MODULE
, "mac80211_hwsim");
2369 if (IS_ERR(hwsim_class
)) {
2370 err
= PTR_ERR(hwsim_class
);
2371 goto out_unregister_driver
;
2374 for (i
= 0; i
< radios
; i
++) {
2375 err
= mac80211_hwsim_create_radio();
2377 goto out_free_radios
;
2380 hwsim_mon
= alloc_netdev(0, "hwsim%d", hwsim_mon_setup
);
2381 if (hwsim_mon
== NULL
) {
2383 goto out_free_radios
;
2387 err
= dev_alloc_name(hwsim_mon
, hwsim_mon
->name
);
2390 goto out_free_radios
;
2393 err
= register_netdevice(hwsim_mon
);
2400 err
= hwsim_init_netlink();
2407 free_netdev(hwsim_mon
);
2409 mac80211_hwsim_free();
2410 out_unregister_driver
:
2411 platform_driver_unregister(&mac80211_hwsim_driver
);
2414 module_init(init_mac80211_hwsim
);
2416 static void __exit
exit_mac80211_hwsim(void)
2418 printk(KERN_DEBUG
"mac80211_hwsim: unregister radios\n");
2420 hwsim_exit_netlink();
2422 mac80211_hwsim_free();
2423 unregister_netdev(hwsim_mon
);
2424 platform_driver_unregister(&mac80211_hwsim_driver
);
2426 module_exit(exit_mac80211_hwsim
);