1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
4 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
6 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018 - 2020 Intel Corporation
12 * - Add TSF sync and fix IBSS beacon transmission by adding
13 * competition for "air time" at TBTT
14 * - RX filtering based on filter configuration (data->rx_filter)
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
22 #include <net/mac80211.h>
23 #include <net/ieee80211_radiotap.h>
24 #include <linux/if_arp.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/etherdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/debugfs.h>
29 #include <linux/module.h>
30 #include <linux/ktime.h>
31 #include <net/genetlink.h>
32 #include <net/net_namespace.h>
33 #include <net/netns/generic.h>
34 #include <linux/rhashtable.h>
35 #include <linux/nospec.h>
36 #include <linux/virtio.h>
37 #include <linux/virtio_ids.h>
38 #include <linux/virtio_config.h>
39 #include "mac80211_hwsim.h"
41 #define WARN_QUEUE 100
44 MODULE_AUTHOR("Jouni Malinen");
45 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46 MODULE_LICENSE("GPL");
48 static int radios
= 2;
49 module_param(radios
, int, 0444);
50 MODULE_PARM_DESC(radios
, "Number of simulated radios");
52 static int channels
= 1;
53 module_param(channels
, int, 0444);
54 MODULE_PARM_DESC(channels
, "Number of concurrent channels");
56 static bool paged_rx
= false;
57 module_param(paged_rx
, bool, 0644);
58 MODULE_PARM_DESC(paged_rx
, "Use paged SKBs for RX instead of linear ones");
60 static bool rctbl
= false;
61 module_param(rctbl
, bool, 0444);
62 MODULE_PARM_DESC(rctbl
, "Handle rate control table");
64 static bool support_p2p_device
= true;
65 module_param(support_p2p_device
, bool, 0444);
66 MODULE_PARM_DESC(support_p2p_device
, "Support P2P-Device interface type");
69 * enum hwsim_regtest - the type of regulatory tests we offer
71 * These are the different values you can use for the regtest
72 * module parameter. This is useful to help test world roaming
73 * and the driver regulatory_hint() call and combinations of these.
74 * If you want to do specific alpha2 regulatory domain tests simply
75 * use the userspace regulatory request as that will be respected as
76 * well without the need of this module parameter. This is designed
77 * only for testing the driver regulatory request, world roaming
78 * and all possible combinations.
80 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
81 * this is the default value.
82 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
83 * hint, only one driver regulatory hint will be sent as such the
84 * secondary radios are expected to follow.
85 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
86 * request with all radios reporting the same regulatory domain.
87 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
88 * different regulatory domains requests. Expected behaviour is for
89 * an intersection to occur but each device will still use their
90 * respective regulatory requested domains. Subsequent radios will
91 * use the resulting intersection.
92 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
93 * this by using a custom beacon-capable regulatory domain for the first
94 * radio. All other device world roam.
95 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
96 * domain requests. All radios will adhere to this custom world regulatory
98 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
99 * domain requests. The first radio will adhere to the first custom world
100 * regulatory domain, the second one to the second custom world regulatory
101 * domain. All other devices will world roam.
102 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
103 * settings, only the first radio will send a regulatory domain request
104 * and use strict settings. The rest of the radios are expected to follow.
105 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
106 * settings. All radios will adhere to this.
107 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
108 * domain settings, combined with secondary driver regulatory domain
109 * settings. The first radio will get a strict regulatory domain setting
110 * using the first driver regulatory request and the second radio will use
111 * non-strict settings using the second driver regulatory request. All
112 * other devices should follow the intersection created between the
114 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
115 * at least 6 radios for a complete test. We will test in this order:
116 * 1 - driver custom world regulatory domain
117 * 2 - second custom world regulatory domain
118 * 3 - first driver regulatory domain request
119 * 4 - second driver regulatory domain request
120 * 5 - strict regulatory domain settings using the third driver regulatory
122 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
123 * regulatory requests.
126 HWSIM_REGTEST_DISABLED
= 0,
127 HWSIM_REGTEST_DRIVER_REG_FOLLOW
= 1,
128 HWSIM_REGTEST_DRIVER_REG_ALL
= 2,
129 HWSIM_REGTEST_DIFF_COUNTRY
= 3,
130 HWSIM_REGTEST_WORLD_ROAM
= 4,
131 HWSIM_REGTEST_CUSTOM_WORLD
= 5,
132 HWSIM_REGTEST_CUSTOM_WORLD_2
= 6,
133 HWSIM_REGTEST_STRICT_FOLLOW
= 7,
134 HWSIM_REGTEST_STRICT_ALL
= 8,
135 HWSIM_REGTEST_STRICT_AND_DRIVER_REG
= 9,
136 HWSIM_REGTEST_ALL
= 10,
139 /* Set to one of the HWSIM_REGTEST_* values above */
140 static int regtest
= HWSIM_REGTEST_DISABLED
;
141 module_param(regtest
, int, 0444);
142 MODULE_PARM_DESC(regtest
, "The type of regulatory test we want to run");
144 static const char *hwsim_alpha2s
[] = {
153 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01
= {
157 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
158 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
159 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
160 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
161 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
165 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02
= {
169 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
170 REG_RULE(5725-10, 5850+10, 40, 0, 30,
172 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
176 static const struct ieee80211_regdomain
*hwsim_world_regdom_custom
[] = {
177 &hwsim_world_regdom_custom_01
,
178 &hwsim_world_regdom_custom_02
,
181 struct hwsim_vif_priv
{
189 #define HWSIM_VIF_MAGIC 0x69537748
191 static inline void hwsim_check_magic(struct ieee80211_vif
*vif
)
193 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
194 WARN(vp
->magic
!= HWSIM_VIF_MAGIC
,
195 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
196 vif
, vp
->magic
, vif
->addr
, vif
->type
, vif
->p2p
);
199 static inline void hwsim_set_magic(struct ieee80211_vif
*vif
)
201 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
202 vp
->magic
= HWSIM_VIF_MAGIC
;
205 static inline void hwsim_clear_magic(struct ieee80211_vif
*vif
)
207 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
211 struct hwsim_sta_priv
{
215 #define HWSIM_STA_MAGIC 0x6d537749
217 static inline void hwsim_check_sta_magic(struct ieee80211_sta
*sta
)
219 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
220 WARN_ON(sp
->magic
!= HWSIM_STA_MAGIC
);
223 static inline void hwsim_set_sta_magic(struct ieee80211_sta
*sta
)
225 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
226 sp
->magic
= HWSIM_STA_MAGIC
;
229 static inline void hwsim_clear_sta_magic(struct ieee80211_sta
*sta
)
231 struct hwsim_sta_priv
*sp
= (void *)sta
->drv_priv
;
235 struct hwsim_chanctx_priv
{
239 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
241 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
243 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
244 WARN_ON(cp
->magic
!= HWSIM_CHANCTX_MAGIC
);
247 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
249 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
250 cp
->magic
= HWSIM_CHANCTX_MAGIC
;
253 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf
*c
)
255 struct hwsim_chanctx_priv
*cp
= (void *)c
->drv_priv
;
259 static unsigned int hwsim_net_id
;
261 static DEFINE_IDA(hwsim_netgroup_ida
);
268 static inline int hwsim_net_get_netgroup(struct net
*net
)
270 struct hwsim_net
*hwsim_net
= net_generic(net
, hwsim_net_id
);
272 return hwsim_net
->netgroup
;
275 static inline int hwsim_net_set_netgroup(struct net
*net
)
277 struct hwsim_net
*hwsim_net
= net_generic(net
, hwsim_net_id
);
279 hwsim_net
->netgroup
= ida_simple_get(&hwsim_netgroup_ida
,
281 return hwsim_net
->netgroup
>= 0 ? 0 : -ENOMEM
;
284 static inline u32
hwsim_net_get_wmediumd(struct net
*net
)
286 struct hwsim_net
*hwsim_net
= net_generic(net
, hwsim_net_id
);
288 return hwsim_net
->wmediumd
;
291 static inline void hwsim_net_set_wmediumd(struct net
*net
, u32 portid
)
293 struct hwsim_net
*hwsim_net
= net_generic(net
, hwsim_net_id
);
295 hwsim_net
->wmediumd
= portid
;
298 static struct class *hwsim_class
;
300 static struct net_device
*hwsim_mon
; /* global monitor netdev */
302 #define CHAN2G(_freq) { \
303 .band = NL80211_BAND_2GHZ, \
304 .center_freq = (_freq), \
305 .hw_value = (_freq), \
308 #define CHAN5G(_freq) { \
309 .band = NL80211_BAND_5GHZ, \
310 .center_freq = (_freq), \
311 .hw_value = (_freq), \
314 static const struct ieee80211_channel hwsim_channels_2ghz
[] = {
315 CHAN2G(2412), /* Channel 1 */
316 CHAN2G(2417), /* Channel 2 */
317 CHAN2G(2422), /* Channel 3 */
318 CHAN2G(2427), /* Channel 4 */
319 CHAN2G(2432), /* Channel 5 */
320 CHAN2G(2437), /* Channel 6 */
321 CHAN2G(2442), /* Channel 7 */
322 CHAN2G(2447), /* Channel 8 */
323 CHAN2G(2452), /* Channel 9 */
324 CHAN2G(2457), /* Channel 10 */
325 CHAN2G(2462), /* Channel 11 */
326 CHAN2G(2467), /* Channel 12 */
327 CHAN2G(2472), /* Channel 13 */
328 CHAN2G(2484), /* Channel 14 */
331 static const struct ieee80211_channel hwsim_channels_5ghz
[] = {
332 CHAN5G(5180), /* Channel 36 */
333 CHAN5G(5200), /* Channel 40 */
334 CHAN5G(5220), /* Channel 44 */
335 CHAN5G(5240), /* Channel 48 */
337 CHAN5G(5260), /* Channel 52 */
338 CHAN5G(5280), /* Channel 56 */
339 CHAN5G(5300), /* Channel 60 */
340 CHAN5G(5320), /* Channel 64 */
342 CHAN5G(5500), /* Channel 100 */
343 CHAN5G(5520), /* Channel 104 */
344 CHAN5G(5540), /* Channel 108 */
345 CHAN5G(5560), /* Channel 112 */
346 CHAN5G(5580), /* Channel 116 */
347 CHAN5G(5600), /* Channel 120 */
348 CHAN5G(5620), /* Channel 124 */
349 CHAN5G(5640), /* Channel 128 */
350 CHAN5G(5660), /* Channel 132 */
351 CHAN5G(5680), /* Channel 136 */
352 CHAN5G(5700), /* Channel 140 */
354 CHAN5G(5745), /* Channel 149 */
355 CHAN5G(5765), /* Channel 153 */
356 CHAN5G(5785), /* Channel 157 */
357 CHAN5G(5805), /* Channel 161 */
358 CHAN5G(5825), /* Channel 165 */
359 CHAN5G(5845), /* Channel 169 */
361 CHAN5G(5855), /* Channel 171 */
362 CHAN5G(5860), /* Channel 172 */
363 CHAN5G(5865), /* Channel 173 */
364 CHAN5G(5870), /* Channel 174 */
366 CHAN5G(5875), /* Channel 175 */
367 CHAN5G(5880), /* Channel 176 */
368 CHAN5G(5885), /* Channel 177 */
369 CHAN5G(5890), /* Channel 178 */
370 CHAN5G(5895), /* Channel 179 */
371 CHAN5G(5900), /* Channel 180 */
372 CHAN5G(5905), /* Channel 181 */
374 CHAN5G(5910), /* Channel 182 */
375 CHAN5G(5915), /* Channel 183 */
376 CHAN5G(5920), /* Channel 184 */
377 CHAN5G(5925), /* Channel 185 */
380 static const struct ieee80211_rate hwsim_rates
[] = {
382 { .bitrate
= 20, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
383 { .bitrate
= 55, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
384 { .bitrate
= 110, .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
395 static const u32 hwsim_ciphers
[] = {
396 WLAN_CIPHER_SUITE_WEP40
,
397 WLAN_CIPHER_SUITE_WEP104
,
398 WLAN_CIPHER_SUITE_TKIP
,
399 WLAN_CIPHER_SUITE_CCMP
,
400 WLAN_CIPHER_SUITE_CCMP_256
,
401 WLAN_CIPHER_SUITE_GCMP
,
402 WLAN_CIPHER_SUITE_GCMP_256
,
403 WLAN_CIPHER_SUITE_AES_CMAC
,
404 WLAN_CIPHER_SUITE_BIP_CMAC_256
,
405 WLAN_CIPHER_SUITE_BIP_GMAC_128
,
406 WLAN_CIPHER_SUITE_BIP_GMAC_256
,
409 #define OUI_QCA 0x001374
410 #define QCA_NL80211_SUBCMD_TEST 1
411 enum qca_nl80211_vendor_subcmds
{
412 QCA_WLAN_VENDOR_ATTR_TEST
= 8,
413 QCA_WLAN_VENDOR_ATTR_MAX
= QCA_WLAN_VENDOR_ATTR_TEST
416 static const struct nla_policy
417 hwsim_vendor_test_policy
[QCA_WLAN_VENDOR_ATTR_MAX
+ 1] = {
418 [QCA_WLAN_VENDOR_ATTR_MAX
] = { .type
= NLA_U32
},
421 static int mac80211_hwsim_vendor_cmd_test(struct wiphy
*wiphy
,
422 struct wireless_dev
*wdev
,
423 const void *data
, int data_len
)
426 struct nlattr
*tb
[QCA_WLAN_VENDOR_ATTR_MAX
+ 1];
430 err
= nla_parse_deprecated(tb
, QCA_WLAN_VENDOR_ATTR_MAX
, data
,
431 data_len
, hwsim_vendor_test_policy
, NULL
);
434 if (!tb
[QCA_WLAN_VENDOR_ATTR_TEST
])
436 val
= nla_get_u32(tb
[QCA_WLAN_VENDOR_ATTR_TEST
]);
437 wiphy_dbg(wiphy
, "%s: test=%u\n", __func__
, val
);
439 /* Send a vendor event as a test. Note that this would not normally be
440 * done within a command handler, but rather, based on some other
441 * trigger. For simplicity, this command is used to trigger the event
444 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
446 skb
= cfg80211_vendor_event_alloc(wiphy
, wdev
, 100, 0, GFP_KERNEL
);
448 /* skb_put() or nla_put() will fill up data within
449 * NL80211_ATTR_VENDOR_DATA.
452 /* Add vendor data */
453 nla_put_u32(skb
, QCA_WLAN_VENDOR_ATTR_TEST
, val
+ 1);
455 /* Send the event - this will call nla_nest_end() */
456 cfg80211_vendor_event(skb
, GFP_KERNEL
);
459 /* Send a response to the command */
460 skb
= cfg80211_vendor_cmd_alloc_reply_skb(wiphy
, 10);
464 /* skb_put() or nla_put() will fill up data within
465 * NL80211_ATTR_VENDOR_DATA
467 nla_put_u32(skb
, QCA_WLAN_VENDOR_ATTR_TEST
, val
+ 2);
469 return cfg80211_vendor_cmd_reply(skb
);
472 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands
[] = {
474 .info
= { .vendor_id
= OUI_QCA
,
475 .subcmd
= QCA_NL80211_SUBCMD_TEST
},
476 .flags
= WIPHY_VENDOR_CMD_NEED_NETDEV
,
477 .doit
= mac80211_hwsim_vendor_cmd_test
,
478 .policy
= hwsim_vendor_test_policy
,
479 .maxattr
= QCA_WLAN_VENDOR_ATTR_MAX
,
483 /* Advertise support vendor specific events */
484 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events
[] = {
485 { .vendor_id
= OUI_QCA
, .subcmd
= 1 },
488 static spinlock_t hwsim_radio_lock
;
489 static LIST_HEAD(hwsim_radios
);
490 static struct rhashtable hwsim_radios_rht
;
491 static int hwsim_radio_idx
;
492 static int hwsim_radios_generation
= 1;
494 static struct platform_driver mac80211_hwsim_driver
= {
496 .name
= "mac80211_hwsim",
500 struct mac80211_hwsim_data
{
501 struct list_head list
;
502 struct rhash_head rht
;
503 struct ieee80211_hw
*hw
;
505 struct ieee80211_supported_band bands
[NUM_NL80211_BANDS
];
506 struct ieee80211_channel channels_2ghz
[ARRAY_SIZE(hwsim_channels_2ghz
)];
507 struct ieee80211_channel channels_5ghz
[ARRAY_SIZE(hwsim_channels_5ghz
)];
508 struct ieee80211_rate rates
[ARRAY_SIZE(hwsim_rates
)];
509 struct ieee80211_iface_combination if_combination
;
510 struct ieee80211_iface_limit if_limits
[3];
513 u32 ciphers
[ARRAY_SIZE(hwsim_ciphers
)];
515 struct mac_address addresses
[2];
518 bool destroy_on_close
;
521 const struct ieee80211_regdomain
*regd
;
523 struct ieee80211_channel
*tmp_chan
;
524 struct ieee80211_channel
*roc_chan
;
526 struct delayed_work roc_start
;
527 struct delayed_work roc_done
;
528 struct delayed_work hw_scan
;
529 struct cfg80211_scan_request
*hw_scan_request
;
530 struct ieee80211_vif
*hw_scan_vif
;
532 u8 scan_addr
[ETH_ALEN
];
534 struct ieee80211_channel
*channel
;
535 unsigned long next_start
, start
, end
;
536 } survey_data
[ARRAY_SIZE(hwsim_channels_2ghz
) +
537 ARRAY_SIZE(hwsim_channels_5ghz
)];
539 struct ieee80211_channel
*channel
;
540 u64 beacon_int
/* beacon interval in us */;
541 unsigned int rx_filter
;
542 bool started
, idle
, scanning
;
544 struct hrtimer beacon_timer
;
546 PS_DISABLED
, PS_ENABLED
, PS_AUTO_POLL
, PS_MANUAL_POLL
548 bool ps_poll_pending
;
549 struct dentry
*debugfs
;
551 uintptr_t pending_cookie
;
552 struct sk_buff_head pending
; /* packets pending */
554 * Only radios in the same group can communicate together (the
555 * channel has to match too). Each bit represents a group. A
556 * radio can be in more than one group.
560 /* group shared by radios created in the same netns */
562 /* wmediumd portid responsible for netgroup of this radio */
565 /* difference between this hw's clock and the real clock, in usecs */
568 /* absolute beacon transmission time. Used to cover up "tx" delay. */
580 static const struct rhashtable_params hwsim_rht_params
= {
582 .automatic_shrinking
= true,
584 .key_offset
= offsetof(struct mac80211_hwsim_data
, addresses
[1]),
585 .head_offset
= offsetof(struct mac80211_hwsim_data
, rht
),
588 struct hwsim_radiotap_hdr
{
589 struct ieee80211_radiotap_header hdr
;
597 struct hwsim_radiotap_ack_hdr
{
598 struct ieee80211_radiotap_header hdr
;
605 /* MAC80211_HWSIM netlink family */
606 static struct genl_family hwsim_genl_family
;
608 enum hwsim_multicast_groups
{
612 static const struct genl_multicast_group hwsim_mcgrps
[] = {
613 [HWSIM_MCGRP_CONFIG
] = { .name
= "config", },
616 /* MAC80211_HWSIM netlink policy */
618 static const struct nla_policy hwsim_genl_policy
[HWSIM_ATTR_MAX
+ 1] = {
619 [HWSIM_ATTR_ADDR_RECEIVER
] = NLA_POLICY_ETH_ADDR_COMPAT
,
620 [HWSIM_ATTR_ADDR_TRANSMITTER
] = NLA_POLICY_ETH_ADDR_COMPAT
,
621 [HWSIM_ATTR_FRAME
] = { .type
= NLA_BINARY
,
622 .len
= IEEE80211_MAX_DATA_LEN
},
623 [HWSIM_ATTR_FLAGS
] = { .type
= NLA_U32
},
624 [HWSIM_ATTR_RX_RATE
] = { .type
= NLA_U32
},
625 [HWSIM_ATTR_SIGNAL
] = { .type
= NLA_U32
},
626 [HWSIM_ATTR_TX_INFO
] = { .type
= NLA_BINARY
,
627 .len
= IEEE80211_TX_MAX_RATES
*
628 sizeof(struct hwsim_tx_rate
)},
629 [HWSIM_ATTR_COOKIE
] = { .type
= NLA_U64
},
630 [HWSIM_ATTR_CHANNELS
] = { .type
= NLA_U32
},
631 [HWSIM_ATTR_RADIO_ID
] = { .type
= NLA_U32
},
632 [HWSIM_ATTR_REG_HINT_ALPHA2
] = { .type
= NLA_STRING
, .len
= 2 },
633 [HWSIM_ATTR_REG_CUSTOM_REG
] = { .type
= NLA_U32
},
634 [HWSIM_ATTR_REG_STRICT_REG
] = { .type
= NLA_FLAG
},
635 [HWSIM_ATTR_SUPPORT_P2P_DEVICE
] = { .type
= NLA_FLAG
},
636 [HWSIM_ATTR_USE_CHANCTX
] = { .type
= NLA_FLAG
},
637 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE
] = { .type
= NLA_FLAG
},
638 [HWSIM_ATTR_RADIO_NAME
] = { .type
= NLA_STRING
},
639 [HWSIM_ATTR_NO_VIF
] = { .type
= NLA_FLAG
},
640 [HWSIM_ATTR_FREQ
] = { .type
= NLA_U32
},
641 [HWSIM_ATTR_TX_INFO_FLAGS
] = { .type
= NLA_BINARY
},
642 [HWSIM_ATTR_PERM_ADDR
] = NLA_POLICY_ETH_ADDR_COMPAT
,
643 [HWSIM_ATTR_IFTYPE_SUPPORT
] = { .type
= NLA_U32
},
644 [HWSIM_ATTR_CIPHER_SUPPORT
] = { .type
= NLA_BINARY
},
647 #if IS_REACHABLE(CONFIG_VIRTIO)
649 /* MAC80211_HWSIM virtio queues */
650 static struct virtqueue
*hwsim_vqs
[HWSIM_NUM_VQS
];
651 static bool hwsim_virtio_enabled
;
652 static spinlock_t hwsim_virtio_lock
;
654 static void hwsim_virtio_rx_work(struct work_struct
*work
);
655 static DECLARE_WORK(hwsim_virtio_rx
, hwsim_virtio_rx_work
);
657 static int hwsim_tx_virtio(struct mac80211_hwsim_data
*data
,
660 struct scatterlist sg
[1];
664 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
665 if (!hwsim_virtio_enabled
) {
670 sg_init_one(sg
, skb
->head
, skb_end_offset(skb
));
671 err
= virtqueue_add_outbuf(hwsim_vqs
[HWSIM_VQ_TX
], sg
, 1, skb
,
675 virtqueue_kick(hwsim_vqs
[HWSIM_VQ_TX
]);
676 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
680 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
685 /* cause a linker error if this ends up being needed */
686 extern int hwsim_tx_virtio(struct mac80211_hwsim_data
*data
,
687 struct sk_buff
*skb
);
688 #define hwsim_virtio_enabled false
691 static void mac80211_hwsim_tx_frame(struct ieee80211_hw
*hw
,
693 struct ieee80211_channel
*chan
);
695 /* sysfs attributes */
696 static void hwsim_send_ps_poll(void *dat
, u8
*mac
, struct ieee80211_vif
*vif
)
698 struct mac80211_hwsim_data
*data
= dat
;
699 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
701 struct ieee80211_pspoll
*pspoll
;
706 wiphy_dbg(data
->hw
->wiphy
,
707 "%s: send PS-Poll to %pM for aid %d\n",
708 __func__
, vp
->bssid
, vp
->aid
);
710 skb
= dev_alloc_skb(sizeof(*pspoll
));
713 pspoll
= skb_put(skb
, sizeof(*pspoll
));
714 pspoll
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_CTL
|
715 IEEE80211_STYPE_PSPOLL
|
717 pspoll
->aid
= cpu_to_le16(0xc000 | vp
->aid
);
718 memcpy(pspoll
->bssid
, vp
->bssid
, ETH_ALEN
);
719 memcpy(pspoll
->ta
, mac
, ETH_ALEN
);
722 mac80211_hwsim_tx_frame(data
->hw
, skb
,
723 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
727 static void hwsim_send_nullfunc(struct mac80211_hwsim_data
*data
, u8
*mac
,
728 struct ieee80211_vif
*vif
, int ps
)
730 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
732 struct ieee80211_hdr
*hdr
;
737 wiphy_dbg(data
->hw
->wiphy
,
738 "%s: send data::nullfunc to %pM ps=%d\n",
739 __func__
, vp
->bssid
, ps
);
741 skb
= dev_alloc_skb(sizeof(*hdr
));
744 hdr
= skb_put(skb
, sizeof(*hdr
) - ETH_ALEN
);
745 hdr
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_DATA
|
746 IEEE80211_STYPE_NULLFUNC
|
747 IEEE80211_FCTL_TODS
|
748 (ps
? IEEE80211_FCTL_PM
: 0));
749 hdr
->duration_id
= cpu_to_le16(0);
750 memcpy(hdr
->addr1
, vp
->bssid
, ETH_ALEN
);
751 memcpy(hdr
->addr2
, mac
, ETH_ALEN
);
752 memcpy(hdr
->addr3
, vp
->bssid
, ETH_ALEN
);
755 mac80211_hwsim_tx_frame(data
->hw
, skb
,
756 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
761 static void hwsim_send_nullfunc_ps(void *dat
, u8
*mac
,
762 struct ieee80211_vif
*vif
)
764 struct mac80211_hwsim_data
*data
= dat
;
765 hwsim_send_nullfunc(data
, mac
, vif
, 1);
768 static void hwsim_send_nullfunc_no_ps(void *dat
, u8
*mac
,
769 struct ieee80211_vif
*vif
)
771 struct mac80211_hwsim_data
*data
= dat
;
772 hwsim_send_nullfunc(data
, mac
, vif
, 0);
775 static int hwsim_fops_ps_read(void *dat
, u64
*val
)
777 struct mac80211_hwsim_data
*data
= dat
;
782 static int hwsim_fops_ps_write(void *dat
, u64 val
)
784 struct mac80211_hwsim_data
*data
= dat
;
787 if (val
!= PS_DISABLED
&& val
!= PS_ENABLED
&& val
!= PS_AUTO_POLL
&&
788 val
!= PS_MANUAL_POLL
)
791 if (val
== PS_MANUAL_POLL
) {
792 if (data
->ps
!= PS_ENABLED
)
795 ieee80211_iterate_active_interfaces_atomic(
796 data
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
797 hwsim_send_ps_poll
, data
);
805 if (old_ps
== PS_DISABLED
&& val
!= PS_DISABLED
) {
806 ieee80211_iterate_active_interfaces_atomic(
807 data
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
808 hwsim_send_nullfunc_ps
, data
);
809 } else if (old_ps
!= PS_DISABLED
&& val
== PS_DISABLED
) {
810 ieee80211_iterate_active_interfaces_atomic(
811 data
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
812 hwsim_send_nullfunc_no_ps
, data
);
819 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps
, hwsim_fops_ps_read
, hwsim_fops_ps_write
,
822 static int hwsim_write_simulate_radar(void *dat
, u64 val
)
824 struct mac80211_hwsim_data
*data
= dat
;
826 ieee80211_radar_detected(data
->hw
);
831 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar
, NULL
,
832 hwsim_write_simulate_radar
, "%llu\n");
834 static int hwsim_fops_group_read(void *dat
, u64
*val
)
836 struct mac80211_hwsim_data
*data
= dat
;
841 static int hwsim_fops_group_write(void *dat
, u64 val
)
843 struct mac80211_hwsim_data
*data
= dat
;
848 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group
,
849 hwsim_fops_group_read
, hwsim_fops_group_write
,
852 static netdev_tx_t
hwsim_mon_xmit(struct sk_buff
*skb
,
853 struct net_device
*dev
)
855 /* TODO: allow packet injection */
860 static inline u64
mac80211_hwsim_get_tsf_raw(void)
862 return ktime_to_us(ktime_get_real());
865 static __le64
__mac80211_hwsim_get_tsf(struct mac80211_hwsim_data
*data
)
867 u64 now
= mac80211_hwsim_get_tsf_raw();
868 return cpu_to_le64(now
+ data
->tsf_offset
);
871 static u64
mac80211_hwsim_get_tsf(struct ieee80211_hw
*hw
,
872 struct ieee80211_vif
*vif
)
874 struct mac80211_hwsim_data
*data
= hw
->priv
;
875 return le64_to_cpu(__mac80211_hwsim_get_tsf(data
));
878 static void mac80211_hwsim_set_tsf(struct ieee80211_hw
*hw
,
879 struct ieee80211_vif
*vif
, u64 tsf
)
881 struct mac80211_hwsim_data
*data
= hw
->priv
;
882 u64 now
= mac80211_hwsim_get_tsf(hw
, vif
);
883 u32 bcn_int
= data
->beacon_int
;
884 u64 delta
= abs(tsf
- now
);
886 /* adjust after beaconing with new timestamp at old TBTT */
888 data
->tsf_offset
+= delta
;
889 data
->bcn_delta
= do_div(delta
, bcn_int
);
891 data
->tsf_offset
-= delta
;
892 data
->bcn_delta
= -(s64
)do_div(delta
, bcn_int
);
896 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw
*hw
,
897 struct sk_buff
*tx_skb
,
898 struct ieee80211_channel
*chan
)
900 struct mac80211_hwsim_data
*data
= hw
->priv
;
902 struct hwsim_radiotap_hdr
*hdr
;
904 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(tx_skb
);
905 struct ieee80211_rate
*txrate
= ieee80211_get_tx_rate(hw
, info
);
907 if (WARN_ON(!txrate
))
910 if (!netif_running(hwsim_mon
))
913 skb
= skb_copy_expand(tx_skb
, sizeof(*hdr
), 0, GFP_ATOMIC
);
917 hdr
= skb_push(skb
, sizeof(*hdr
));
918 hdr
->hdr
.it_version
= PKTHDR_RADIOTAP_VERSION
;
920 hdr
->hdr
.it_len
= cpu_to_le16(sizeof(*hdr
));
921 hdr
->hdr
.it_present
= cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS
) |
922 (1 << IEEE80211_RADIOTAP_RATE
) |
923 (1 << IEEE80211_RADIOTAP_TSFT
) |
924 (1 << IEEE80211_RADIOTAP_CHANNEL
));
925 hdr
->rt_tsft
= __mac80211_hwsim_get_tsf(data
);
927 hdr
->rt_rate
= txrate
->bitrate
/ 5;
928 hdr
->rt_channel
= cpu_to_le16(chan
->center_freq
);
929 flags
= IEEE80211_CHAN_2GHZ
;
930 if (txrate
->flags
& IEEE80211_RATE_ERP_G
)
931 flags
|= IEEE80211_CHAN_OFDM
;
933 flags
|= IEEE80211_CHAN_CCK
;
934 hdr
->rt_chbitmask
= cpu_to_le16(flags
);
936 skb
->dev
= hwsim_mon
;
937 skb_reset_mac_header(skb
);
938 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
939 skb
->pkt_type
= PACKET_OTHERHOST
;
940 skb
->protocol
= htons(ETH_P_802_2
);
941 memset(skb
->cb
, 0, sizeof(skb
->cb
));
946 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel
*chan
,
950 struct hwsim_radiotap_ack_hdr
*hdr
;
952 struct ieee80211_hdr
*hdr11
;
954 if (!netif_running(hwsim_mon
))
957 skb
= dev_alloc_skb(100);
961 hdr
= skb_put(skb
, sizeof(*hdr
));
962 hdr
->hdr
.it_version
= PKTHDR_RADIOTAP_VERSION
;
964 hdr
->hdr
.it_len
= cpu_to_le16(sizeof(*hdr
));
965 hdr
->hdr
.it_present
= cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS
) |
966 (1 << IEEE80211_RADIOTAP_CHANNEL
));
969 hdr
->rt_channel
= cpu_to_le16(chan
->center_freq
);
970 flags
= IEEE80211_CHAN_2GHZ
;
971 hdr
->rt_chbitmask
= cpu_to_le16(flags
);
973 hdr11
= skb_put(skb
, 10);
974 hdr11
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_CTL
|
975 IEEE80211_STYPE_ACK
);
976 hdr11
->duration_id
= cpu_to_le16(0);
977 memcpy(hdr11
->addr1
, addr
, ETH_ALEN
);
979 skb
->dev
= hwsim_mon
;
980 skb_reset_mac_header(skb
);
981 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
982 skb
->pkt_type
= PACKET_OTHERHOST
;
983 skb
->protocol
= htons(ETH_P_802_2
);
984 memset(skb
->cb
, 0, sizeof(skb
->cb
));
988 struct mac80211_hwsim_addr_match_data
{
993 static void mac80211_hwsim_addr_iter(void *data
, u8
*mac
,
994 struct ieee80211_vif
*vif
)
996 struct mac80211_hwsim_addr_match_data
*md
= data
;
998 if (memcmp(mac
, md
->addr
, ETH_ALEN
) == 0)
1002 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data
*data
,
1005 struct mac80211_hwsim_addr_match_data md
= {
1009 if (data
->scanning
&& memcmp(addr
, data
->scan_addr
, ETH_ALEN
) == 0)
1012 memcpy(md
.addr
, addr
, ETH_ALEN
);
1014 ieee80211_iterate_active_interfaces_atomic(data
->hw
,
1015 IEEE80211_IFACE_ITER_NORMAL
,
1016 mac80211_hwsim_addr_iter
,
1022 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data
*data
,
1023 struct sk_buff
*skb
)
1031 /* TODO: accept (some) Beacons by default and other frames only
1032 * if pending PS-Poll has been sent */
1034 case PS_MANUAL_POLL
:
1035 /* Allow unicast frames to own address if there is a pending
1037 if (data
->ps_poll_pending
&&
1038 mac80211_hwsim_addr_match(data
, skb
->data
+ 4)) {
1039 data
->ps_poll_pending
= false;
1048 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data
*data
,
1049 struct sk_buff
*skb
, int portid
)
1056 for_each_net_rcu(net
) {
1057 if (data
->netgroup
== hwsim_net_get_netgroup(net
)) {
1058 res
= genlmsg_unicast(net
, skb
, portid
);
1071 static inline u16
trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate
*rate
)
1075 if (rate
->flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
1076 result
|= MAC80211_HWSIM_TX_RC_USE_RTS_CTS
;
1077 if (rate
->flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)
1078 result
|= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT
;
1079 if (rate
->flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
1080 result
|= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE
;
1081 if (rate
->flags
& IEEE80211_TX_RC_MCS
)
1082 result
|= MAC80211_HWSIM_TX_RC_MCS
;
1083 if (rate
->flags
& IEEE80211_TX_RC_GREEN_FIELD
)
1084 result
|= MAC80211_HWSIM_TX_RC_GREEN_FIELD
;
1085 if (rate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
1086 result
|= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH
;
1087 if (rate
->flags
& IEEE80211_TX_RC_DUP_DATA
)
1088 result
|= MAC80211_HWSIM_TX_RC_DUP_DATA
;
1089 if (rate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
1090 result
|= MAC80211_HWSIM_TX_RC_SHORT_GI
;
1091 if (rate
->flags
& IEEE80211_TX_RC_VHT_MCS
)
1092 result
|= MAC80211_HWSIM_TX_RC_VHT_MCS
;
1093 if (rate
->flags
& IEEE80211_TX_RC_80_MHZ_WIDTH
)
1094 result
|= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH
;
1095 if (rate
->flags
& IEEE80211_TX_RC_160_MHZ_WIDTH
)
1096 result
|= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH
;
1101 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw
*hw
,
1102 struct sk_buff
*my_skb
,
1105 struct sk_buff
*skb
;
1106 struct mac80211_hwsim_data
*data
= hw
->priv
;
1107 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) my_skb
->data
;
1108 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(my_skb
);
1110 unsigned int hwsim_flags
= 0;
1112 struct hwsim_tx_rate tx_attempts
[IEEE80211_TX_MAX_RATES
];
1113 struct hwsim_tx_rate_flag tx_attempts_flags
[IEEE80211_TX_MAX_RATES
];
1116 if (data
->ps
!= PS_DISABLED
)
1117 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
1118 /* If the queue contains MAX_QUEUE skb's drop some */
1119 if (skb_queue_len(&data
->pending
) >= MAX_QUEUE
) {
1120 /* Droping until WARN_QUEUE level */
1121 while (skb_queue_len(&data
->pending
) >= WARN_QUEUE
) {
1122 ieee80211_free_txskb(hw
, skb_dequeue(&data
->pending
));
1127 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1129 goto nla_put_failure
;
1131 msg_head
= genlmsg_put(skb
, 0, 0, &hwsim_genl_family
, 0,
1133 if (msg_head
== NULL
) {
1134 pr_debug("mac80211_hwsim: problem with msg_head\n");
1135 goto nla_put_failure
;
1138 if (nla_put(skb
, HWSIM_ATTR_ADDR_TRANSMITTER
,
1139 ETH_ALEN
, data
->addresses
[1].addr
))
1140 goto nla_put_failure
;
1142 /* We get the skb->data */
1143 if (nla_put(skb
, HWSIM_ATTR_FRAME
, my_skb
->len
, my_skb
->data
))
1144 goto nla_put_failure
;
1146 /* We get the flags for this transmission, and we translate them to
1149 if (info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
)
1150 hwsim_flags
|= HWSIM_TX_CTL_REQ_TX_STATUS
;
1152 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1153 hwsim_flags
|= HWSIM_TX_CTL_NO_ACK
;
1155 if (nla_put_u32(skb
, HWSIM_ATTR_FLAGS
, hwsim_flags
))
1156 goto nla_put_failure
;
1158 if (nla_put_u32(skb
, HWSIM_ATTR_FREQ
, data
->channel
->center_freq
))
1159 goto nla_put_failure
;
1161 /* We get the tx control (rate and retries) info*/
1163 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
1164 tx_attempts
[i
].idx
= info
->status
.rates
[i
].idx
;
1165 tx_attempts_flags
[i
].idx
= info
->status
.rates
[i
].idx
;
1166 tx_attempts
[i
].count
= info
->status
.rates
[i
].count
;
1167 tx_attempts_flags
[i
].flags
=
1168 trans_tx_rate_flags_ieee2hwsim(
1169 &info
->status
.rates
[i
]);
1172 if (nla_put(skb
, HWSIM_ATTR_TX_INFO
,
1173 sizeof(struct hwsim_tx_rate
)*IEEE80211_TX_MAX_RATES
,
1175 goto nla_put_failure
;
1177 if (nla_put(skb
, HWSIM_ATTR_TX_INFO_FLAGS
,
1178 sizeof(struct hwsim_tx_rate_flag
) * IEEE80211_TX_MAX_RATES
,
1180 goto nla_put_failure
;
1182 /* We create a cookie to identify this skb */
1183 data
->pending_cookie
++;
1184 cookie
= data
->pending_cookie
;
1185 info
->rate_driver_data
[0] = (void *)cookie
;
1186 if (nla_put_u64_64bit(skb
, HWSIM_ATTR_COOKIE
, cookie
, HWSIM_ATTR_PAD
))
1187 goto nla_put_failure
;
1189 genlmsg_end(skb
, msg_head
);
1191 if (hwsim_virtio_enabled
) {
1192 if (hwsim_tx_virtio(data
, skb
))
1193 goto err_free_txskb
;
1195 if (hwsim_unicast_netgroup(data
, skb
, dst_portid
))
1196 goto err_free_txskb
;
1199 /* Enqueue the packet */
1200 skb_queue_tail(&data
->pending
, my_skb
);
1202 data
->tx_bytes
+= my_skb
->len
;
1208 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__
);
1209 ieee80211_free_txskb(hw
, my_skb
);
1213 static bool hwsim_chans_compat(struct ieee80211_channel
*c1
,
1214 struct ieee80211_channel
*c2
)
1219 return c1
->center_freq
== c2
->center_freq
;
1222 struct tx_iter_data
{
1223 struct ieee80211_channel
*channel
;
1227 static void mac80211_hwsim_tx_iter(void *_data
, u8
*addr
,
1228 struct ieee80211_vif
*vif
)
1230 struct tx_iter_data
*data
= _data
;
1232 if (!vif
->chanctx_conf
)
1235 if (!hwsim_chans_compat(data
->channel
,
1236 rcu_dereference(vif
->chanctx_conf
)->def
.chan
))
1239 data
->receive
= true;
1242 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff
*skb
)
1245 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1247 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1248 * (but you should use a valid OUI, not that)
1250 * If anyone wants to 'donate' a radiotap OUI/subns code
1251 * please send a patch removing this #ifdef and changing
1252 * the values accordingly.
1254 #ifdef HWSIM_RADIOTAP_OUI
1255 struct ieee80211_vendor_radiotap
*rtap
;
1258 * Note that this code requires the headroom in the SKB
1259 * that was allocated earlier.
1261 rtap
= skb_push(skb
, sizeof(*rtap
) + 8 + 4);
1262 rtap
->oui
[0] = HWSIM_RADIOTAP_OUI
[0];
1263 rtap
->oui
[1] = HWSIM_RADIOTAP_OUI
[1];
1264 rtap
->oui
[2] = HWSIM_RADIOTAP_OUI
[2];
1268 * Radiotap vendor namespaces can (and should) also be
1269 * split into fields by using the standard radiotap
1270 * presence bitmap mechanism. Use just BIT(0) here for
1271 * the presence bitmap.
1273 rtap
->present
= BIT(0);
1274 /* We have 8 bytes of (dummy) data */
1276 /* For testing, also require it to be aligned */
1278 /* And also test that padding works, 4 bytes */
1281 memcpy(rtap
->data
, "ABCDEFGH", 8);
1282 /* make sure to clear padding, mac80211 doesn't */
1283 memset(rtap
->data
+ 8, 0, 4);
1285 IEEE80211_SKB_RXCB(skb
)->flag
|= RX_FLAG_RADIOTAP_VENDOR_DATA
;
1289 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw
*hw
,
1290 struct sk_buff
*skb
,
1291 struct ieee80211_channel
*chan
)
1293 struct mac80211_hwsim_data
*data
= hw
->priv
, *data2
;
1295 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
1296 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1297 struct ieee80211_rx_status rx_status
;
1300 memset(&rx_status
, 0, sizeof(rx_status
));
1301 rx_status
.flag
|= RX_FLAG_MACTIME_START
;
1302 rx_status
.freq
= chan
->center_freq
;
1303 rx_status
.band
= chan
->band
;
1304 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_VHT_MCS
) {
1305 rx_status
.rate_idx
=
1306 ieee80211_rate_get_vht_mcs(&info
->control
.rates
[0]);
1308 ieee80211_rate_get_vht_nss(&info
->control
.rates
[0]);
1309 rx_status
.encoding
= RX_ENC_VHT
;
1311 rx_status
.rate_idx
= info
->control
.rates
[0].idx
;
1312 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_MCS
)
1313 rx_status
.encoding
= RX_ENC_HT
;
1315 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
1316 rx_status
.bw
= RATE_INFO_BW_40
;
1317 else if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_80_MHZ_WIDTH
)
1318 rx_status
.bw
= RATE_INFO_BW_80
;
1319 else if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_160_MHZ_WIDTH
)
1320 rx_status
.bw
= RATE_INFO_BW_160
;
1322 rx_status
.bw
= RATE_INFO_BW_20
;
1323 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_SHORT_GI
)
1324 rx_status
.enc_flags
|= RX_ENC_FLAG_SHORT_GI
;
1325 /* TODO: simulate real signal strength (and optional packet loss) */
1326 rx_status
.signal
= -50;
1327 if (info
->control
.vif
)
1328 rx_status
.signal
+= info
->control
.vif
->bss_conf
.txpower
;
1330 if (data
->ps
!= PS_DISABLED
)
1331 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
1333 /* release the skb's source info */
1341 * Get absolute mactime here so all HWs RX at the "same time", and
1342 * absolute TX time for beacon mactime so the timestamp matches.
1343 * Giving beacons a different mactime than non-beacons looks messy, but
1344 * it helps the Toffset be exact and a ~10us mactime discrepancy
1345 * probably doesn't really matter.
1347 if (ieee80211_is_beacon(hdr
->frame_control
) ||
1348 ieee80211_is_probe_resp(hdr
->frame_control
)) {
1349 rx_status
.boottime_ns
= ktime_get_boottime_ns();
1350 now
= data
->abs_bcn_ts
;
1352 now
= mac80211_hwsim_get_tsf_raw();
1355 /* Copy skb to all enabled radios that are on the current frequency */
1356 spin_lock(&hwsim_radio_lock
);
1357 list_for_each_entry(data2
, &hwsim_radios
, list
) {
1358 struct sk_buff
*nskb
;
1359 struct tx_iter_data tx_iter_data
= {
1367 if (!data2
->started
|| (data2
->idle
&& !data2
->tmp_chan
) ||
1368 !hwsim_ps_rx_ok(data2
, skb
))
1371 if (!(data
->group
& data2
->group
))
1374 if (data
->netgroup
!= data2
->netgroup
)
1377 if (!hwsim_chans_compat(chan
, data2
->tmp_chan
) &&
1378 !hwsim_chans_compat(chan
, data2
->channel
)) {
1379 ieee80211_iterate_active_interfaces_atomic(
1380 data2
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
1381 mac80211_hwsim_tx_iter
, &tx_iter_data
);
1382 if (!tx_iter_data
.receive
)
1387 * reserve some space for our vendor and the normal
1388 * radiotap header, since we're copying anyway
1390 if (skb
->len
< PAGE_SIZE
&& paged_rx
) {
1391 struct page
*page
= alloc_page(GFP_ATOMIC
);
1396 nskb
= dev_alloc_skb(128);
1402 memcpy(page_address(page
), skb
->data
, skb
->len
);
1403 skb_add_rx_frag(nskb
, 0, page
, 0, skb
->len
, skb
->len
);
1405 nskb
= skb_copy(skb
, GFP_ATOMIC
);
1410 if (mac80211_hwsim_addr_match(data2
, hdr
->addr1
))
1413 rx_status
.mactime
= now
+ data2
->tsf_offset
;
1415 memcpy(IEEE80211_SKB_RXCB(nskb
), &rx_status
, sizeof(rx_status
));
1417 mac80211_hwsim_add_vendor_rtap(nskb
);
1420 data2
->rx_bytes
+= nskb
->len
;
1421 ieee80211_rx_irqsafe(data2
->hw
, nskb
);
1423 spin_unlock(&hwsim_radio_lock
);
1428 static void mac80211_hwsim_tx(struct ieee80211_hw
*hw
,
1429 struct ieee80211_tx_control
*control
,
1430 struct sk_buff
*skb
)
1432 struct mac80211_hwsim_data
*data
= hw
->priv
;
1433 struct ieee80211_tx_info
*txi
= IEEE80211_SKB_CB(skb
);
1434 struct ieee80211_hdr
*hdr
= (void *)skb
->data
;
1435 struct ieee80211_chanctx_conf
*chanctx_conf
;
1436 struct ieee80211_channel
*channel
;
1440 if (WARN_ON(skb
->len
< 10)) {
1441 /* Should not happen; just a sanity check for addr1 use */
1442 ieee80211_free_txskb(hw
, skb
);
1446 if (!data
->use_chanctx
) {
1447 channel
= data
->channel
;
1448 } else if (txi
->hw_queue
== 4) {
1449 channel
= data
->tmp_chan
;
1451 chanctx_conf
= rcu_dereference(txi
->control
.vif
->chanctx_conf
);
1453 channel
= chanctx_conf
->def
.chan
;
1458 if (WARN(!channel
, "TX w/o channel - queue = %d\n", txi
->hw_queue
)) {
1459 ieee80211_free_txskb(hw
, skb
);
1463 if (data
->idle
&& !data
->tmp_chan
) {
1464 wiphy_dbg(hw
->wiphy
, "Trying to TX when idle - reject\n");
1465 ieee80211_free_txskb(hw
, skb
);
1469 if (txi
->control
.vif
)
1470 hwsim_check_magic(txi
->control
.vif
);
1472 hwsim_check_sta_magic(control
->sta
);
1474 if (ieee80211_hw_check(hw
, SUPPORTS_RC_TABLE
))
1475 ieee80211_get_tx_rates(txi
->control
.vif
, control
->sta
, skb
,
1477 ARRAY_SIZE(txi
->control
.rates
));
1479 if (skb
->len
>= 24 + 8 &&
1480 ieee80211_is_probe_resp(hdr
->frame_control
)) {
1481 /* fake header transmission time */
1482 struct ieee80211_mgmt
*mgmt
;
1483 struct ieee80211_rate
*txrate
;
1486 mgmt
= (struct ieee80211_mgmt
*)skb
->data
;
1487 txrate
= ieee80211_get_tx_rate(hw
, txi
);
1488 ts
= mac80211_hwsim_get_tsf_raw();
1489 mgmt
->u
.probe_resp
.timestamp
=
1490 cpu_to_le64(ts
+ data
->tsf_offset
+
1491 24 * 8 * 10 / txrate
->bitrate
);
1494 mac80211_hwsim_monitor_rx(hw
, skb
, channel
);
1496 /* wmediumd mode check */
1497 _portid
= READ_ONCE(data
->wmediumd
);
1499 if (_portid
|| hwsim_virtio_enabled
)
1500 return mac80211_hwsim_tx_frame_nl(hw
, skb
, _portid
);
1502 /* NO wmediumd detected, perfect medium simulation */
1504 data
->tx_bytes
+= skb
->len
;
1505 ack
= mac80211_hwsim_tx_frame_no_nl(hw
, skb
, channel
);
1507 if (ack
&& skb
->len
>= 16)
1508 mac80211_hwsim_monitor_ack(channel
, hdr
->addr2
);
1510 ieee80211_tx_info_clear_status(txi
);
1512 /* frame was transmitted at most favorable rate at first attempt */
1513 txi
->control
.rates
[0].count
= 1;
1514 txi
->control
.rates
[1].idx
= -1;
1516 if (!(txi
->flags
& IEEE80211_TX_CTL_NO_ACK
) && ack
)
1517 txi
->flags
|= IEEE80211_TX_STAT_ACK
;
1518 ieee80211_tx_status_irqsafe(hw
, skb
);
1522 static int mac80211_hwsim_start(struct ieee80211_hw
*hw
)
1524 struct mac80211_hwsim_data
*data
= hw
->priv
;
1525 wiphy_dbg(hw
->wiphy
, "%s\n", __func__
);
1526 data
->started
= true;
1531 static void mac80211_hwsim_stop(struct ieee80211_hw
*hw
)
1533 struct mac80211_hwsim_data
*data
= hw
->priv
;
1534 data
->started
= false;
1535 hrtimer_cancel(&data
->beacon_timer
);
1536 wiphy_dbg(hw
->wiphy
, "%s\n", __func__
);
1540 static int mac80211_hwsim_add_interface(struct ieee80211_hw
*hw
,
1541 struct ieee80211_vif
*vif
)
1543 wiphy_dbg(hw
->wiphy
, "%s (type=%d mac_addr=%pM)\n",
1544 __func__
, ieee80211_vif_type_p2p(vif
),
1546 hwsim_set_magic(vif
);
1549 vif
->hw_queue
[IEEE80211_AC_VO
] = 0;
1550 vif
->hw_queue
[IEEE80211_AC_VI
] = 1;
1551 vif
->hw_queue
[IEEE80211_AC_BE
] = 2;
1552 vif
->hw_queue
[IEEE80211_AC_BK
] = 3;
1558 static int mac80211_hwsim_change_interface(struct ieee80211_hw
*hw
,
1559 struct ieee80211_vif
*vif
,
1560 enum nl80211_iftype newtype
,
1563 newtype
= ieee80211_iftype_p2p(newtype
, newp2p
);
1564 wiphy_dbg(hw
->wiphy
,
1565 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1566 __func__
, ieee80211_vif_type_p2p(vif
),
1567 newtype
, vif
->addr
);
1568 hwsim_check_magic(vif
);
1571 * interface may change from non-AP to AP in
1572 * which case this needs to be set up again
1579 static void mac80211_hwsim_remove_interface(
1580 struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
1582 wiphy_dbg(hw
->wiphy
, "%s (type=%d mac_addr=%pM)\n",
1583 __func__
, ieee80211_vif_type_p2p(vif
),
1585 hwsim_check_magic(vif
);
1586 hwsim_clear_magic(vif
);
1589 static void mac80211_hwsim_tx_frame(struct ieee80211_hw
*hw
,
1590 struct sk_buff
*skb
,
1591 struct ieee80211_channel
*chan
)
1593 struct mac80211_hwsim_data
*data
= hw
->priv
;
1594 u32 _pid
= READ_ONCE(data
->wmediumd
);
1596 if (ieee80211_hw_check(hw
, SUPPORTS_RC_TABLE
)) {
1597 struct ieee80211_tx_info
*txi
= IEEE80211_SKB_CB(skb
);
1598 ieee80211_get_tx_rates(txi
->control
.vif
, NULL
, skb
,
1600 ARRAY_SIZE(txi
->control
.rates
));
1603 mac80211_hwsim_monitor_rx(hw
, skb
, chan
);
1605 if (_pid
|| hwsim_virtio_enabled
)
1606 return mac80211_hwsim_tx_frame_nl(hw
, skb
, _pid
);
1608 mac80211_hwsim_tx_frame_no_nl(hw
, skb
, chan
);
1612 static void mac80211_hwsim_beacon_tx(void *arg
, u8
*mac
,
1613 struct ieee80211_vif
*vif
)
1615 struct mac80211_hwsim_data
*data
= arg
;
1616 struct ieee80211_hw
*hw
= data
->hw
;
1617 struct ieee80211_tx_info
*info
;
1618 struct ieee80211_rate
*txrate
;
1619 struct ieee80211_mgmt
*mgmt
;
1620 struct sk_buff
*skb
;
1622 hwsim_check_magic(vif
);
1624 if (vif
->type
!= NL80211_IFTYPE_AP
&&
1625 vif
->type
!= NL80211_IFTYPE_MESH_POINT
&&
1626 vif
->type
!= NL80211_IFTYPE_ADHOC
&&
1627 vif
->type
!= NL80211_IFTYPE_OCB
)
1630 skb
= ieee80211_beacon_get(hw
, vif
);
1633 info
= IEEE80211_SKB_CB(skb
);
1634 if (ieee80211_hw_check(hw
, SUPPORTS_RC_TABLE
))
1635 ieee80211_get_tx_rates(vif
, NULL
, skb
,
1636 info
->control
.rates
,
1637 ARRAY_SIZE(info
->control
.rates
));
1639 txrate
= ieee80211_get_tx_rate(hw
, info
);
1641 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
1642 /* fake header transmission time */
1643 data
->abs_bcn_ts
= mac80211_hwsim_get_tsf_raw();
1644 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(data
->abs_bcn_ts
+
1646 24 * 8 * 10 / txrate
->bitrate
);
1648 mac80211_hwsim_tx_frame(hw
, skb
,
1649 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
1651 while ((skb
= ieee80211_get_buffered_bc(hw
, vif
)) != NULL
) {
1652 mac80211_hwsim_tx_frame(hw
, skb
,
1653 rcu_dereference(vif
->chanctx_conf
)->def
.chan
);
1656 if (vif
->csa_active
&& ieee80211_csa_is_complete(vif
))
1657 ieee80211_csa_finish(vif
);
1660 static enum hrtimer_restart
1661 mac80211_hwsim_beacon(struct hrtimer
*timer
)
1663 struct mac80211_hwsim_data
*data
=
1664 container_of(timer
, struct mac80211_hwsim_data
, beacon_timer
);
1665 struct ieee80211_hw
*hw
= data
->hw
;
1666 u64 bcn_int
= data
->beacon_int
;
1669 return HRTIMER_NORESTART
;
1671 ieee80211_iterate_active_interfaces_atomic(
1672 hw
, IEEE80211_IFACE_ITER_NORMAL
,
1673 mac80211_hwsim_beacon_tx
, data
);
1675 /* beacon at new TBTT + beacon interval */
1676 if (data
->bcn_delta
) {
1677 bcn_int
-= data
->bcn_delta
;
1678 data
->bcn_delta
= 0;
1680 hrtimer_forward(&data
->beacon_timer
, hrtimer_get_expires(timer
),
1681 ns_to_ktime(bcn_int
* NSEC_PER_USEC
));
1682 return HRTIMER_RESTART
;
1685 static const char * const hwsim_chanwidths
[] = {
1686 [NL80211_CHAN_WIDTH_5
] = "ht5",
1687 [NL80211_CHAN_WIDTH_10
] = "ht10",
1688 [NL80211_CHAN_WIDTH_20_NOHT
] = "noht",
1689 [NL80211_CHAN_WIDTH_20
] = "ht20",
1690 [NL80211_CHAN_WIDTH_40
] = "ht40",
1691 [NL80211_CHAN_WIDTH_80
] = "vht80",
1692 [NL80211_CHAN_WIDTH_80P80
] = "vht80p80",
1693 [NL80211_CHAN_WIDTH_160
] = "vht160",
1696 static int mac80211_hwsim_config(struct ieee80211_hw
*hw
, u32 changed
)
1698 struct mac80211_hwsim_data
*data
= hw
->priv
;
1699 struct ieee80211_conf
*conf
= &hw
->conf
;
1700 static const char *smps_modes
[IEEE80211_SMPS_NUM_MODES
] = {
1701 [IEEE80211_SMPS_AUTOMATIC
] = "auto",
1702 [IEEE80211_SMPS_OFF
] = "off",
1703 [IEEE80211_SMPS_STATIC
] = "static",
1704 [IEEE80211_SMPS_DYNAMIC
] = "dynamic",
1708 if (conf
->chandef
.chan
)
1709 wiphy_dbg(hw
->wiphy
,
1710 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1712 conf
->chandef
.chan
->center_freq
,
1713 conf
->chandef
.center_freq1
,
1714 conf
->chandef
.center_freq2
,
1715 hwsim_chanwidths
[conf
->chandef
.width
],
1716 !!(conf
->flags
& IEEE80211_CONF_IDLE
),
1717 !!(conf
->flags
& IEEE80211_CONF_PS
),
1718 smps_modes
[conf
->smps_mode
]);
1720 wiphy_dbg(hw
->wiphy
,
1721 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1723 !!(conf
->flags
& IEEE80211_CONF_IDLE
),
1724 !!(conf
->flags
& IEEE80211_CONF_PS
),
1725 smps_modes
[conf
->smps_mode
]);
1727 data
->idle
= !!(conf
->flags
& IEEE80211_CONF_IDLE
);
1729 WARN_ON(conf
->chandef
.chan
&& data
->use_chanctx
);
1731 mutex_lock(&data
->mutex
);
1732 if (data
->scanning
&& conf
->chandef
.chan
) {
1733 for (idx
= 0; idx
< ARRAY_SIZE(data
->survey_data
); idx
++) {
1734 if (data
->survey_data
[idx
].channel
== data
->channel
) {
1735 data
->survey_data
[idx
].start
=
1736 data
->survey_data
[idx
].next_start
;
1737 data
->survey_data
[idx
].end
= jiffies
;
1742 data
->channel
= conf
->chandef
.chan
;
1744 for (idx
= 0; idx
< ARRAY_SIZE(data
->survey_data
); idx
++) {
1745 if (data
->survey_data
[idx
].channel
&&
1746 data
->survey_data
[idx
].channel
!= data
->channel
)
1748 data
->survey_data
[idx
].channel
= data
->channel
;
1749 data
->survey_data
[idx
].next_start
= jiffies
;
1753 data
->channel
= conf
->chandef
.chan
;
1755 mutex_unlock(&data
->mutex
);
1757 if (!data
->started
|| !data
->beacon_int
)
1758 hrtimer_cancel(&data
->beacon_timer
);
1759 else if (!hrtimer_is_queued(&data
->beacon_timer
)) {
1760 u64 tsf
= mac80211_hwsim_get_tsf(hw
, NULL
);
1761 u32 bcn_int
= data
->beacon_int
;
1762 u64 until_tbtt
= bcn_int
- do_div(tsf
, bcn_int
);
1764 hrtimer_start(&data
->beacon_timer
,
1765 ns_to_ktime(until_tbtt
* NSEC_PER_USEC
),
1766 HRTIMER_MODE_REL_SOFT
);
1773 static void mac80211_hwsim_configure_filter(struct ieee80211_hw
*hw
,
1774 unsigned int changed_flags
,
1775 unsigned int *total_flags
,u64 multicast
)
1777 struct mac80211_hwsim_data
*data
= hw
->priv
;
1779 wiphy_dbg(hw
->wiphy
, "%s\n", __func__
);
1781 data
->rx_filter
= 0;
1782 if (*total_flags
& FIF_ALLMULTI
)
1783 data
->rx_filter
|= FIF_ALLMULTI
;
1785 *total_flags
= data
->rx_filter
;
1788 static void mac80211_hwsim_bcn_en_iter(void *data
, u8
*mac
,
1789 struct ieee80211_vif
*vif
)
1791 unsigned int *count
= data
;
1792 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
1798 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw
*hw
,
1799 struct ieee80211_vif
*vif
,
1800 struct ieee80211_bss_conf
*info
,
1803 struct hwsim_vif_priv
*vp
= (void *)vif
->drv_priv
;
1804 struct mac80211_hwsim_data
*data
= hw
->priv
;
1806 hwsim_check_magic(vif
);
1808 wiphy_dbg(hw
->wiphy
, "%s(changed=0x%x vif->addr=%pM)\n",
1809 __func__
, changed
, vif
->addr
);
1811 if (changed
& BSS_CHANGED_BSSID
) {
1812 wiphy_dbg(hw
->wiphy
, "%s: BSSID changed: %pM\n",
1813 __func__
, info
->bssid
);
1814 memcpy(vp
->bssid
, info
->bssid
, ETH_ALEN
);
1817 if (changed
& BSS_CHANGED_ASSOC
) {
1818 wiphy_dbg(hw
->wiphy
, " ASSOC: assoc=%d aid=%d\n",
1819 info
->assoc
, info
->aid
);
1820 vp
->assoc
= info
->assoc
;
1821 vp
->aid
= info
->aid
;
1824 if (changed
& BSS_CHANGED_BEACON_ENABLED
) {
1825 wiphy_dbg(hw
->wiphy
, " BCN EN: %d (BI=%u)\n",
1826 info
->enable_beacon
, info
->beacon_int
);
1827 vp
->bcn_en
= info
->enable_beacon
;
1828 if (data
->started
&&
1829 !hrtimer_is_queued(&data
->beacon_timer
) &&
1830 info
->enable_beacon
) {
1831 u64 tsf
, until_tbtt
;
1833 data
->beacon_int
= info
->beacon_int
* 1024;
1834 tsf
= mac80211_hwsim_get_tsf(hw
, vif
);
1835 bcn_int
= data
->beacon_int
;
1836 until_tbtt
= bcn_int
- do_div(tsf
, bcn_int
);
1838 hrtimer_start(&data
->beacon_timer
,
1839 ns_to_ktime(until_tbtt
* NSEC_PER_USEC
),
1840 HRTIMER_MODE_REL_SOFT
);
1841 } else if (!info
->enable_beacon
) {
1842 unsigned int count
= 0;
1843 ieee80211_iterate_active_interfaces_atomic(
1844 data
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
1845 mac80211_hwsim_bcn_en_iter
, &count
);
1846 wiphy_dbg(hw
->wiphy
, " beaconing vifs remaining: %u",
1849 hrtimer_cancel(&data
->beacon_timer
);
1850 data
->beacon_int
= 0;
1855 if (changed
& BSS_CHANGED_ERP_CTS_PROT
) {
1856 wiphy_dbg(hw
->wiphy
, " ERP_CTS_PROT: %d\n",
1857 info
->use_cts_prot
);
1860 if (changed
& BSS_CHANGED_ERP_PREAMBLE
) {
1861 wiphy_dbg(hw
->wiphy
, " ERP_PREAMBLE: %d\n",
1862 info
->use_short_preamble
);
1865 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1866 wiphy_dbg(hw
->wiphy
, " ERP_SLOT: %d\n", info
->use_short_slot
);
1869 if (changed
& BSS_CHANGED_HT
) {
1870 wiphy_dbg(hw
->wiphy
, " HT: op_mode=0x%x\n",
1871 info
->ht_operation_mode
);
1874 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1875 wiphy_dbg(hw
->wiphy
, " BASIC_RATES: 0x%llx\n",
1876 (unsigned long long) info
->basic_rates
);
1879 if (changed
& BSS_CHANGED_TXPOWER
)
1880 wiphy_dbg(hw
->wiphy
, " TX Power: %d dBm\n", info
->txpower
);
1883 static int mac80211_hwsim_sta_add(struct ieee80211_hw
*hw
,
1884 struct ieee80211_vif
*vif
,
1885 struct ieee80211_sta
*sta
)
1887 hwsim_check_magic(vif
);
1888 hwsim_set_sta_magic(sta
);
1893 static int mac80211_hwsim_sta_remove(struct ieee80211_hw
*hw
,
1894 struct ieee80211_vif
*vif
,
1895 struct ieee80211_sta
*sta
)
1897 hwsim_check_magic(vif
);
1898 hwsim_clear_sta_magic(sta
);
1903 static void mac80211_hwsim_sta_notify(struct ieee80211_hw
*hw
,
1904 struct ieee80211_vif
*vif
,
1905 enum sta_notify_cmd cmd
,
1906 struct ieee80211_sta
*sta
)
1908 hwsim_check_magic(vif
);
1911 case STA_NOTIFY_SLEEP
:
1912 case STA_NOTIFY_AWAKE
:
1913 /* TODO: make good use of these flags */
1916 WARN(1, "Invalid sta notify: %d\n", cmd
);
1921 static int mac80211_hwsim_set_tim(struct ieee80211_hw
*hw
,
1922 struct ieee80211_sta
*sta
,
1925 hwsim_check_sta_magic(sta
);
1929 static int mac80211_hwsim_conf_tx(
1930 struct ieee80211_hw
*hw
,
1931 struct ieee80211_vif
*vif
, u16 queue
,
1932 const struct ieee80211_tx_queue_params
*params
)
1934 wiphy_dbg(hw
->wiphy
,
1935 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1937 params
->txop
, params
->cw_min
,
1938 params
->cw_max
, params
->aifs
);
1942 static int mac80211_hwsim_get_survey(struct ieee80211_hw
*hw
, int idx
,
1943 struct survey_info
*survey
)
1945 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
1947 if (idx
< 0 || idx
>= ARRAY_SIZE(hwsim
->survey_data
))
1950 mutex_lock(&hwsim
->mutex
);
1951 survey
->channel
= hwsim
->survey_data
[idx
].channel
;
1952 if (!survey
->channel
) {
1953 mutex_unlock(&hwsim
->mutex
);
1958 * Magically conjured dummy values --- this is only ok for simulated hardware.
1960 * A real driver which cannot determine real values noise MUST NOT
1961 * report any, especially not a magically conjured ones :-)
1963 survey
->filled
= SURVEY_INFO_NOISE_DBM
|
1965 SURVEY_INFO_TIME_BUSY
;
1966 survey
->noise
= -92;
1968 jiffies_to_msecs(hwsim
->survey_data
[idx
].end
-
1969 hwsim
->survey_data
[idx
].start
);
1970 /* report 12.5% of channel time is used */
1971 survey
->time_busy
= survey
->time
/8;
1972 mutex_unlock(&hwsim
->mutex
);
1977 #ifdef CONFIG_NL80211_TESTMODE
1979 * This section contains example code for using netlink
1980 * attributes with the testmode command in nl80211.
1983 /* These enums need to be kept in sync with userspace */
1984 enum hwsim_testmode_attr
{
1985 __HWSIM_TM_ATTR_INVALID
= 0,
1986 HWSIM_TM_ATTR_CMD
= 1,
1987 HWSIM_TM_ATTR_PS
= 2,
1990 __HWSIM_TM_ATTR_AFTER_LAST
,
1991 HWSIM_TM_ATTR_MAX
= __HWSIM_TM_ATTR_AFTER_LAST
- 1
1994 enum hwsim_testmode_cmd
{
1995 HWSIM_TM_CMD_SET_PS
= 0,
1996 HWSIM_TM_CMD_GET_PS
= 1,
1997 HWSIM_TM_CMD_STOP_QUEUES
= 2,
1998 HWSIM_TM_CMD_WAKE_QUEUES
= 3,
2001 static const struct nla_policy hwsim_testmode_policy
[HWSIM_TM_ATTR_MAX
+ 1] = {
2002 [HWSIM_TM_ATTR_CMD
] = { .type
= NLA_U32
},
2003 [HWSIM_TM_ATTR_PS
] = { .type
= NLA_U32
},
2006 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw
*hw
,
2007 struct ieee80211_vif
*vif
,
2008 void *data
, int len
)
2010 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2011 struct nlattr
*tb
[HWSIM_TM_ATTR_MAX
+ 1];
2012 struct sk_buff
*skb
;
2015 err
= nla_parse_deprecated(tb
, HWSIM_TM_ATTR_MAX
, data
, len
,
2016 hwsim_testmode_policy
, NULL
);
2020 if (!tb
[HWSIM_TM_ATTR_CMD
])
2023 switch (nla_get_u32(tb
[HWSIM_TM_ATTR_CMD
])) {
2024 case HWSIM_TM_CMD_SET_PS
:
2025 if (!tb
[HWSIM_TM_ATTR_PS
])
2027 ps
= nla_get_u32(tb
[HWSIM_TM_ATTR_PS
]);
2028 return hwsim_fops_ps_write(hwsim
, ps
);
2029 case HWSIM_TM_CMD_GET_PS
:
2030 skb
= cfg80211_testmode_alloc_reply_skb(hw
->wiphy
,
2031 nla_total_size(sizeof(u32
)));
2034 if (nla_put_u32(skb
, HWSIM_TM_ATTR_PS
, hwsim
->ps
))
2035 goto nla_put_failure
;
2036 return cfg80211_testmode_reply(skb
);
2037 case HWSIM_TM_CMD_STOP_QUEUES
:
2038 ieee80211_stop_queues(hw
);
2040 case HWSIM_TM_CMD_WAKE_QUEUES
:
2041 ieee80211_wake_queues(hw
);
2053 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw
*hw
,
2054 struct ieee80211_vif
*vif
,
2055 struct ieee80211_ampdu_params
*params
)
2057 struct ieee80211_sta
*sta
= params
->sta
;
2058 enum ieee80211_ampdu_mlme_action action
= params
->action
;
2059 u16 tid
= params
->tid
;
2062 case IEEE80211_AMPDU_TX_START
:
2063 return IEEE80211_AMPDU_TX_START_IMMEDIATE
;
2064 case IEEE80211_AMPDU_TX_STOP_CONT
:
2065 case IEEE80211_AMPDU_TX_STOP_FLUSH
:
2066 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
:
2067 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
2069 case IEEE80211_AMPDU_TX_OPERATIONAL
:
2071 case IEEE80211_AMPDU_RX_START
:
2072 case IEEE80211_AMPDU_RX_STOP
:
2081 static void mac80211_hwsim_flush(struct ieee80211_hw
*hw
,
2082 struct ieee80211_vif
*vif
,
2083 u32 queues
, bool drop
)
2085 /* Not implemented, queues only on kernel side */
2088 static void hw_scan_work(struct work_struct
*work
)
2090 struct mac80211_hwsim_data
*hwsim
=
2091 container_of(work
, struct mac80211_hwsim_data
, hw_scan
.work
);
2092 struct cfg80211_scan_request
*req
= hwsim
->hw_scan_request
;
2095 mutex_lock(&hwsim
->mutex
);
2096 if (hwsim
->scan_chan_idx
>= req
->n_channels
) {
2097 struct cfg80211_scan_info info
= {
2101 wiphy_dbg(hwsim
->hw
->wiphy
, "hw scan complete\n");
2102 ieee80211_scan_completed(hwsim
->hw
, &info
);
2103 hwsim
->hw_scan_request
= NULL
;
2104 hwsim
->hw_scan_vif
= NULL
;
2105 hwsim
->tmp_chan
= NULL
;
2106 mutex_unlock(&hwsim
->mutex
);
2110 wiphy_dbg(hwsim
->hw
->wiphy
, "hw scan %d MHz\n",
2111 req
->channels
[hwsim
->scan_chan_idx
]->center_freq
);
2113 hwsim
->tmp_chan
= req
->channels
[hwsim
->scan_chan_idx
];
2114 if (hwsim
->tmp_chan
->flags
& (IEEE80211_CHAN_NO_IR
|
2115 IEEE80211_CHAN_RADAR
) ||
2121 for (i
= 0; i
< req
->n_ssids
; i
++) {
2122 struct sk_buff
*probe
;
2123 struct ieee80211_mgmt
*mgmt
;
2125 probe
= ieee80211_probereq_get(hwsim
->hw
,
2128 req
->ssids
[i
].ssid_len
,
2133 mgmt
= (struct ieee80211_mgmt
*) probe
->data
;
2134 memcpy(mgmt
->da
, req
->bssid
, ETH_ALEN
);
2135 memcpy(mgmt
->bssid
, req
->bssid
, ETH_ALEN
);
2138 skb_put_data(probe
, req
->ie
, req
->ie_len
);
2141 mac80211_hwsim_tx_frame(hwsim
->hw
, probe
,
2146 ieee80211_queue_delayed_work(hwsim
->hw
, &hwsim
->hw_scan
,
2147 msecs_to_jiffies(dwell
));
2148 hwsim
->survey_data
[hwsim
->scan_chan_idx
].channel
= hwsim
->tmp_chan
;
2149 hwsim
->survey_data
[hwsim
->scan_chan_idx
].start
= jiffies
;
2150 hwsim
->survey_data
[hwsim
->scan_chan_idx
].end
=
2151 jiffies
+ msecs_to_jiffies(dwell
);
2152 hwsim
->scan_chan_idx
++;
2153 mutex_unlock(&hwsim
->mutex
);
2156 static int mac80211_hwsim_hw_scan(struct ieee80211_hw
*hw
,
2157 struct ieee80211_vif
*vif
,
2158 struct ieee80211_scan_request
*hw_req
)
2160 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2161 struct cfg80211_scan_request
*req
= &hw_req
->req
;
2163 mutex_lock(&hwsim
->mutex
);
2164 if (WARN_ON(hwsim
->tmp_chan
|| hwsim
->hw_scan_request
)) {
2165 mutex_unlock(&hwsim
->mutex
);
2168 hwsim
->hw_scan_request
= req
;
2169 hwsim
->hw_scan_vif
= vif
;
2170 hwsim
->scan_chan_idx
= 0;
2171 if (req
->flags
& NL80211_SCAN_FLAG_RANDOM_ADDR
)
2172 get_random_mask_addr(hwsim
->scan_addr
,
2173 hw_req
->req
.mac_addr
,
2174 hw_req
->req
.mac_addr_mask
);
2176 memcpy(hwsim
->scan_addr
, vif
->addr
, ETH_ALEN
);
2177 memset(hwsim
->survey_data
, 0, sizeof(hwsim
->survey_data
));
2178 mutex_unlock(&hwsim
->mutex
);
2180 wiphy_dbg(hw
->wiphy
, "hwsim hw_scan request\n");
2182 ieee80211_queue_delayed_work(hwsim
->hw
, &hwsim
->hw_scan
, 0);
2187 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw
*hw
,
2188 struct ieee80211_vif
*vif
)
2190 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2191 struct cfg80211_scan_info info
= {
2195 wiphy_dbg(hw
->wiphy
, "hwsim cancel_hw_scan\n");
2197 cancel_delayed_work_sync(&hwsim
->hw_scan
);
2199 mutex_lock(&hwsim
->mutex
);
2200 ieee80211_scan_completed(hwsim
->hw
, &info
);
2201 hwsim
->tmp_chan
= NULL
;
2202 hwsim
->hw_scan_request
= NULL
;
2203 hwsim
->hw_scan_vif
= NULL
;
2204 mutex_unlock(&hwsim
->mutex
);
2207 static void mac80211_hwsim_sw_scan(struct ieee80211_hw
*hw
,
2208 struct ieee80211_vif
*vif
,
2211 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2213 mutex_lock(&hwsim
->mutex
);
2215 if (hwsim
->scanning
) {
2216 pr_debug("two hwsim sw_scans detected!\n");
2220 pr_debug("hwsim sw_scan request, prepping stuff\n");
2222 memcpy(hwsim
->scan_addr
, mac_addr
, ETH_ALEN
);
2223 hwsim
->scanning
= true;
2224 memset(hwsim
->survey_data
, 0, sizeof(hwsim
->survey_data
));
2227 mutex_unlock(&hwsim
->mutex
);
2230 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw
*hw
,
2231 struct ieee80211_vif
*vif
)
2233 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2235 mutex_lock(&hwsim
->mutex
);
2237 pr_debug("hwsim sw_scan_complete\n");
2238 hwsim
->scanning
= false;
2239 eth_zero_addr(hwsim
->scan_addr
);
2241 mutex_unlock(&hwsim
->mutex
);
2244 static void hw_roc_start(struct work_struct
*work
)
2246 struct mac80211_hwsim_data
*hwsim
=
2247 container_of(work
, struct mac80211_hwsim_data
, roc_start
.work
);
2249 mutex_lock(&hwsim
->mutex
);
2251 wiphy_dbg(hwsim
->hw
->wiphy
, "hwsim ROC begins\n");
2252 hwsim
->tmp_chan
= hwsim
->roc_chan
;
2253 ieee80211_ready_on_channel(hwsim
->hw
);
2255 ieee80211_queue_delayed_work(hwsim
->hw
, &hwsim
->roc_done
,
2256 msecs_to_jiffies(hwsim
->roc_duration
));
2258 mutex_unlock(&hwsim
->mutex
);
2261 static void hw_roc_done(struct work_struct
*work
)
2263 struct mac80211_hwsim_data
*hwsim
=
2264 container_of(work
, struct mac80211_hwsim_data
, roc_done
.work
);
2266 mutex_lock(&hwsim
->mutex
);
2267 ieee80211_remain_on_channel_expired(hwsim
->hw
);
2268 hwsim
->tmp_chan
= NULL
;
2269 mutex_unlock(&hwsim
->mutex
);
2271 wiphy_dbg(hwsim
->hw
->wiphy
, "hwsim ROC expired\n");
2274 static int mac80211_hwsim_roc(struct ieee80211_hw
*hw
,
2275 struct ieee80211_vif
*vif
,
2276 struct ieee80211_channel
*chan
,
2278 enum ieee80211_roc_type type
)
2280 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2282 mutex_lock(&hwsim
->mutex
);
2283 if (WARN_ON(hwsim
->tmp_chan
|| hwsim
->hw_scan_request
)) {
2284 mutex_unlock(&hwsim
->mutex
);
2288 hwsim
->roc_chan
= chan
;
2289 hwsim
->roc_duration
= duration
;
2290 mutex_unlock(&hwsim
->mutex
);
2292 wiphy_dbg(hw
->wiphy
, "hwsim ROC (%d MHz, %d ms)\n",
2293 chan
->center_freq
, duration
);
2294 ieee80211_queue_delayed_work(hw
, &hwsim
->roc_start
, HZ
/50);
2299 static int mac80211_hwsim_croc(struct ieee80211_hw
*hw
,
2300 struct ieee80211_vif
*vif
)
2302 struct mac80211_hwsim_data
*hwsim
= hw
->priv
;
2304 cancel_delayed_work_sync(&hwsim
->roc_start
);
2305 cancel_delayed_work_sync(&hwsim
->roc_done
);
2307 mutex_lock(&hwsim
->mutex
);
2308 hwsim
->tmp_chan
= NULL
;
2309 mutex_unlock(&hwsim
->mutex
);
2311 wiphy_dbg(hw
->wiphy
, "hwsim ROC canceled\n");
2316 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw
*hw
,
2317 struct ieee80211_chanctx_conf
*ctx
)
2319 hwsim_set_chanctx_magic(ctx
);
2320 wiphy_dbg(hw
->wiphy
,
2321 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2322 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
2323 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
2327 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw
*hw
,
2328 struct ieee80211_chanctx_conf
*ctx
)
2330 wiphy_dbg(hw
->wiphy
,
2331 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2332 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
2333 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
2334 hwsim_check_chanctx_magic(ctx
);
2335 hwsim_clear_chanctx_magic(ctx
);
2338 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw
*hw
,
2339 struct ieee80211_chanctx_conf
*ctx
,
2342 hwsim_check_chanctx_magic(ctx
);
2343 wiphy_dbg(hw
->wiphy
,
2344 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2345 ctx
->def
.chan
->center_freq
, ctx
->def
.width
,
2346 ctx
->def
.center_freq1
, ctx
->def
.center_freq2
);
2349 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw
*hw
,
2350 struct ieee80211_vif
*vif
,
2351 struct ieee80211_chanctx_conf
*ctx
)
2353 hwsim_check_magic(vif
);
2354 hwsim_check_chanctx_magic(ctx
);
2359 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw
*hw
,
2360 struct ieee80211_vif
*vif
,
2361 struct ieee80211_chanctx_conf
*ctx
)
2363 hwsim_check_magic(vif
);
2364 hwsim_check_chanctx_magic(ctx
);
2367 static const char mac80211_hwsim_gstrings_stats
[][ETH_GSTRING_LEN
] = {
2378 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2380 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw
*hw
,
2381 struct ieee80211_vif
*vif
,
2384 if (sset
== ETH_SS_STATS
)
2385 memcpy(data
, *mac80211_hwsim_gstrings_stats
,
2386 sizeof(mac80211_hwsim_gstrings_stats
));
2389 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw
*hw
,
2390 struct ieee80211_vif
*vif
, int sset
)
2392 if (sset
== ETH_SS_STATS
)
2393 return MAC80211_HWSIM_SSTATS_LEN
;
2397 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw
*hw
,
2398 struct ieee80211_vif
*vif
,
2399 struct ethtool_stats
*stats
, u64
*data
)
2401 struct mac80211_hwsim_data
*ar
= hw
->priv
;
2404 data
[i
++] = ar
->tx_pkts
;
2405 data
[i
++] = ar
->tx_bytes
;
2406 data
[i
++] = ar
->rx_pkts
;
2407 data
[i
++] = ar
->rx_bytes
;
2408 data
[i
++] = ar
->tx_dropped
;
2409 data
[i
++] = ar
->tx_failed
;
2411 data
[i
++] = ar
->group
;
2413 WARN_ON(i
!= MAC80211_HWSIM_SSTATS_LEN
);
2416 #define HWSIM_COMMON_OPS \
2417 .tx = mac80211_hwsim_tx, \
2418 .start = mac80211_hwsim_start, \
2419 .stop = mac80211_hwsim_stop, \
2420 .add_interface = mac80211_hwsim_add_interface, \
2421 .change_interface = mac80211_hwsim_change_interface, \
2422 .remove_interface = mac80211_hwsim_remove_interface, \
2423 .config = mac80211_hwsim_config, \
2424 .configure_filter = mac80211_hwsim_configure_filter, \
2425 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2426 .sta_add = mac80211_hwsim_sta_add, \
2427 .sta_remove = mac80211_hwsim_sta_remove, \
2428 .sta_notify = mac80211_hwsim_sta_notify, \
2429 .set_tim = mac80211_hwsim_set_tim, \
2430 .conf_tx = mac80211_hwsim_conf_tx, \
2431 .get_survey = mac80211_hwsim_get_survey, \
2432 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2433 .ampdu_action = mac80211_hwsim_ampdu_action, \
2434 .flush = mac80211_hwsim_flush, \
2435 .get_tsf = mac80211_hwsim_get_tsf, \
2436 .set_tsf = mac80211_hwsim_set_tsf, \
2437 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2438 .get_et_stats = mac80211_hwsim_get_et_stats, \
2439 .get_et_strings = mac80211_hwsim_get_et_strings,
2441 static const struct ieee80211_ops mac80211_hwsim_ops
= {
2443 .sw_scan_start
= mac80211_hwsim_sw_scan
,
2444 .sw_scan_complete
= mac80211_hwsim_sw_scan_complete
,
2447 static const struct ieee80211_ops mac80211_hwsim_mchan_ops
= {
2449 .hw_scan
= mac80211_hwsim_hw_scan
,
2450 .cancel_hw_scan
= mac80211_hwsim_cancel_hw_scan
,
2451 .sw_scan_start
= NULL
,
2452 .sw_scan_complete
= NULL
,
2453 .remain_on_channel
= mac80211_hwsim_roc
,
2454 .cancel_remain_on_channel
= mac80211_hwsim_croc
,
2455 .add_chanctx
= mac80211_hwsim_add_chanctx
,
2456 .remove_chanctx
= mac80211_hwsim_remove_chanctx
,
2457 .change_chanctx
= mac80211_hwsim_change_chanctx
,
2458 .assign_vif_chanctx
= mac80211_hwsim_assign_vif_chanctx
,
2459 .unassign_vif_chanctx
= mac80211_hwsim_unassign_vif_chanctx
,
2462 struct hwsim_new_radio_params
{
2463 unsigned int channels
;
2464 const char *reg_alpha2
;
2465 const struct ieee80211_regdomain
*regd
;
2469 bool destroy_on_close
;
2472 const u8
*perm_addr
;
2478 static void hwsim_mcast_config_msg(struct sk_buff
*mcast_skb
,
2479 struct genl_info
*info
)
2482 genl_notify(&hwsim_genl_family
, mcast_skb
, info
,
2483 HWSIM_MCGRP_CONFIG
, GFP_KERNEL
);
2485 genlmsg_multicast(&hwsim_genl_family
, mcast_skb
, 0,
2486 HWSIM_MCGRP_CONFIG
, GFP_KERNEL
);
2489 static int append_radio_msg(struct sk_buff
*skb
, int id
,
2490 struct hwsim_new_radio_params
*param
)
2494 ret
= nla_put_u32(skb
, HWSIM_ATTR_RADIO_ID
, id
);
2498 if (param
->channels
) {
2499 ret
= nla_put_u32(skb
, HWSIM_ATTR_CHANNELS
, param
->channels
);
2504 if (param
->reg_alpha2
) {
2505 ret
= nla_put(skb
, HWSIM_ATTR_REG_HINT_ALPHA2
, 2,
2514 for (i
= 0; i
< ARRAY_SIZE(hwsim_world_regdom_custom
); i
++) {
2515 if (hwsim_world_regdom_custom
[i
] != param
->regd
)
2518 ret
= nla_put_u32(skb
, HWSIM_ATTR_REG_CUSTOM_REG
, i
);
2525 if (param
->reg_strict
) {
2526 ret
= nla_put_flag(skb
, HWSIM_ATTR_REG_STRICT_REG
);
2531 if (param
->p2p_device
) {
2532 ret
= nla_put_flag(skb
, HWSIM_ATTR_SUPPORT_P2P_DEVICE
);
2537 if (param
->use_chanctx
) {
2538 ret
= nla_put_flag(skb
, HWSIM_ATTR_USE_CHANCTX
);
2543 if (param
->hwname
) {
2544 ret
= nla_put(skb
, HWSIM_ATTR_RADIO_NAME
,
2545 strlen(param
->hwname
), param
->hwname
);
2553 static void hwsim_mcast_new_radio(int id
, struct genl_info
*info
,
2554 struct hwsim_new_radio_params
*param
)
2556 struct sk_buff
*mcast_skb
;
2559 mcast_skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2563 data
= genlmsg_put(mcast_skb
, 0, 0, &hwsim_genl_family
, 0,
2564 HWSIM_CMD_NEW_RADIO
);
2568 if (append_radio_msg(mcast_skb
, id
, param
) < 0)
2571 genlmsg_end(mcast_skb
, data
);
2573 hwsim_mcast_config_msg(mcast_skb
, info
);
2577 nlmsg_free(mcast_skb
);
2580 static const struct ieee80211_sband_iftype_data he_capa_2ghz
[] = {
2582 /* TODO: should we support other types, e.g., P2P?*/
2583 .types_mask
= BIT(NL80211_IFTYPE_STATION
) |
2584 BIT(NL80211_IFTYPE_AP
),
2589 IEEE80211_HE_MAC_CAP0_HTC_HE
,
2591 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US
|
2592 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8
,
2594 IEEE80211_HE_MAC_CAP2_BSR
|
2595 IEEE80211_HE_MAC_CAP2_MU_CASCADING
|
2596 IEEE80211_HE_MAC_CAP2_ACK_EN
,
2598 IEEE80211_HE_MAC_CAP3_OMI_CONTROL
|
2599 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2
,
2600 .mac_cap_info
[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU
,
2602 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK
|
2603 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A
|
2604 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD
|
2605 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS
,
2607 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US
|
2608 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ
|
2609 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ
|
2610 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO
|
2611 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO
,
2613 /* Leave all the other PHY capability bytes
2614 * unset, as DCM, beam forming, RU and PPE
2615 * threshold information are not supported
2618 .he_mcs_nss_supp
= {
2619 .rx_mcs_80
= cpu_to_le16(0xfffa),
2620 .tx_mcs_80
= cpu_to_le16(0xfffa),
2621 .rx_mcs_160
= cpu_to_le16(0xffff),
2622 .tx_mcs_160
= cpu_to_le16(0xffff),
2623 .rx_mcs_80p80
= cpu_to_le16(0xffff),
2624 .tx_mcs_80p80
= cpu_to_le16(0xffff),
2628 #ifdef CONFIG_MAC80211_MESH
2630 /* TODO: should we support other types, e.g., IBSS?*/
2631 .types_mask
= BIT(NL80211_IFTYPE_MESH_POINT
),
2636 IEEE80211_HE_MAC_CAP0_HTC_HE
,
2638 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8
,
2640 IEEE80211_HE_MAC_CAP2_ACK_EN
,
2642 IEEE80211_HE_MAC_CAP3_OMI_CONTROL
|
2643 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2
,
2644 .mac_cap_info
[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU
,
2646 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK
|
2647 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A
|
2648 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD
|
2649 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS
,
2650 .phy_cap_info
[2] = 0,
2652 /* Leave all the other PHY capability bytes
2653 * unset, as DCM, beam forming, RU and PPE
2654 * threshold information are not supported
2657 .he_mcs_nss_supp
= {
2658 .rx_mcs_80
= cpu_to_le16(0xfffa),
2659 .tx_mcs_80
= cpu_to_le16(0xfffa),
2660 .rx_mcs_160
= cpu_to_le16(0xffff),
2661 .tx_mcs_160
= cpu_to_le16(0xffff),
2662 .rx_mcs_80p80
= cpu_to_le16(0xffff),
2663 .tx_mcs_80p80
= cpu_to_le16(0xffff),
2670 static const struct ieee80211_sband_iftype_data he_capa_5ghz
[] = {
2672 /* TODO: should we support other types, e.g., P2P?*/
2673 .types_mask
= BIT(NL80211_IFTYPE_STATION
) |
2674 BIT(NL80211_IFTYPE_AP
),
2679 IEEE80211_HE_MAC_CAP0_HTC_HE
,
2681 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US
|
2682 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8
,
2684 IEEE80211_HE_MAC_CAP2_BSR
|
2685 IEEE80211_HE_MAC_CAP2_MU_CASCADING
|
2686 IEEE80211_HE_MAC_CAP2_ACK_EN
,
2688 IEEE80211_HE_MAC_CAP3_OMI_CONTROL
|
2689 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2
,
2690 .mac_cap_info
[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU
,
2692 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G
|
2693 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G
|
2694 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G
,
2696 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK
|
2697 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A
|
2698 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD
|
2699 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS
,
2701 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US
|
2702 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ
|
2703 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ
|
2704 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO
|
2705 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO
,
2707 /* Leave all the other PHY capability bytes
2708 * unset, as DCM, beam forming, RU and PPE
2709 * threshold information are not supported
2712 .he_mcs_nss_supp
= {
2713 .rx_mcs_80
= cpu_to_le16(0xfffa),
2714 .tx_mcs_80
= cpu_to_le16(0xfffa),
2715 .rx_mcs_160
= cpu_to_le16(0xfffa),
2716 .tx_mcs_160
= cpu_to_le16(0xfffa),
2717 .rx_mcs_80p80
= cpu_to_le16(0xfffa),
2718 .tx_mcs_80p80
= cpu_to_le16(0xfffa),
2722 #ifdef CONFIG_MAC80211_MESH
2724 /* TODO: should we support other types, e.g., IBSS?*/
2725 .types_mask
= BIT(NL80211_IFTYPE_MESH_POINT
),
2730 IEEE80211_HE_MAC_CAP0_HTC_HE
,
2732 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8
,
2734 IEEE80211_HE_MAC_CAP2_ACK_EN
,
2736 IEEE80211_HE_MAC_CAP3_OMI_CONTROL
|
2737 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2
,
2738 .mac_cap_info
[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU
,
2740 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G
|
2741 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G
|
2742 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G
,
2744 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK
|
2745 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A
|
2746 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD
|
2747 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS
,
2748 .phy_cap_info
[2] = 0,
2750 /* Leave all the other PHY capability bytes
2751 * unset, as DCM, beam forming, RU and PPE
2752 * threshold information are not supported
2755 .he_mcs_nss_supp
= {
2756 .rx_mcs_80
= cpu_to_le16(0xfffa),
2757 .tx_mcs_80
= cpu_to_le16(0xfffa),
2758 .rx_mcs_160
= cpu_to_le16(0xfffa),
2759 .tx_mcs_160
= cpu_to_le16(0xfffa),
2760 .rx_mcs_80p80
= cpu_to_le16(0xfffa),
2761 .tx_mcs_80p80
= cpu_to_le16(0xfffa),
2768 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band
*sband
)
2772 if (sband
->band
== NL80211_BAND_2GHZ
) {
2773 n_iftype_data
= ARRAY_SIZE(he_capa_2ghz
);
2774 sband
->iftype_data
=
2775 (struct ieee80211_sband_iftype_data
*)he_capa_2ghz
;
2776 } else if (sband
->band
== NL80211_BAND_5GHZ
) {
2777 n_iftype_data
= ARRAY_SIZE(he_capa_5ghz
);
2778 sband
->iftype_data
=
2779 (struct ieee80211_sband_iftype_data
*)he_capa_5ghz
;
2784 sband
->n_iftype_data
= n_iftype_data
;
2787 #ifdef CONFIG_MAC80211_MESH
2788 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2790 #define HWSIM_MESH_BIT 0
2793 #define HWSIM_DEFAULT_IF_LIMIT \
2794 (BIT(NL80211_IFTYPE_STATION) | \
2795 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2796 BIT(NL80211_IFTYPE_AP) | \
2797 BIT(NL80211_IFTYPE_P2P_GO) | \
2800 #define HWSIM_IFTYPE_SUPPORT_MASK \
2801 (BIT(NL80211_IFTYPE_STATION) | \
2802 BIT(NL80211_IFTYPE_AP) | \
2803 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2804 BIT(NL80211_IFTYPE_P2P_GO) | \
2805 BIT(NL80211_IFTYPE_ADHOC) | \
2806 BIT(NL80211_IFTYPE_MESH_POINT) | \
2807 BIT(NL80211_IFTYPE_OCB))
2809 static int mac80211_hwsim_new_radio(struct genl_info
*info
,
2810 struct hwsim_new_radio_params
*param
)
2814 struct mac80211_hwsim_data
*data
;
2815 struct ieee80211_hw
*hw
;
2816 enum nl80211_band band
;
2817 const struct ieee80211_ops
*ops
= &mac80211_hwsim_ops
;
2822 if (WARN_ON(param
->channels
> 1 && !param
->use_chanctx
))
2825 spin_lock_bh(&hwsim_radio_lock
);
2826 idx
= hwsim_radio_idx
++;
2827 spin_unlock_bh(&hwsim_radio_lock
);
2829 if (param
->use_chanctx
)
2830 ops
= &mac80211_hwsim_mchan_ops
;
2831 hw
= ieee80211_alloc_hw_nm(sizeof(*data
), ops
, param
->hwname
);
2833 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2838 /* ieee80211_alloc_hw_nm may have used a default name */
2839 param
->hwname
= wiphy_name(hw
->wiphy
);
2842 net
= genl_info_net(info
);
2845 wiphy_net_set(hw
->wiphy
, net
);
2850 data
->dev
= device_create(hwsim_class
, NULL
, 0, hw
, "hwsim%d", idx
);
2851 if (IS_ERR(data
->dev
)) {
2853 "mac80211_hwsim: device_create failed (%ld)\n",
2854 PTR_ERR(data
->dev
));
2856 goto failed_drvdata
;
2858 data
->dev
->driver
= &mac80211_hwsim_driver
.driver
;
2859 err
= device_bind_driver(data
->dev
);
2861 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
2866 skb_queue_head_init(&data
->pending
);
2868 SET_IEEE80211_DEV(hw
, data
->dev
);
2869 if (!param
->perm_addr
) {
2870 eth_zero_addr(addr
);
2874 memcpy(data
->addresses
[0].addr
, addr
, ETH_ALEN
);
2875 /* Why need here second address ? */
2876 memcpy(data
->addresses
[1].addr
, addr
, ETH_ALEN
);
2877 data
->addresses
[1].addr
[0] |= 0x40;
2878 hw
->wiphy
->n_addresses
= 2;
2879 hw
->wiphy
->addresses
= data
->addresses
;
2880 /* possible address clash is checked at hash table insertion */
2882 memcpy(data
->addresses
[0].addr
, param
->perm_addr
, ETH_ALEN
);
2883 /* compatibility with automatically generated mac addr */
2884 memcpy(data
->addresses
[1].addr
, param
->perm_addr
, ETH_ALEN
);
2885 hw
->wiphy
->n_addresses
= 2;
2886 hw
->wiphy
->addresses
= data
->addresses
;
2889 data
->channels
= param
->channels
;
2890 data
->use_chanctx
= param
->use_chanctx
;
2892 data
->destroy_on_close
= param
->destroy_on_close
;
2894 data
->portid
= info
->snd_portid
;
2896 /* setup interface limits, only on interface types we support */
2897 if (param
->iftypes
& BIT(NL80211_IFTYPE_ADHOC
)) {
2898 data
->if_limits
[n_limits
].max
= 1;
2899 data
->if_limits
[n_limits
].types
= BIT(NL80211_IFTYPE_ADHOC
);
2903 if (param
->iftypes
& HWSIM_DEFAULT_IF_LIMIT
) {
2904 data
->if_limits
[n_limits
].max
= 2048;
2906 * For this case, we may only support a subset of
2907 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
2908 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
2910 data
->if_limits
[n_limits
].types
=
2911 HWSIM_DEFAULT_IF_LIMIT
& param
->iftypes
;
2915 if (param
->iftypes
& BIT(NL80211_IFTYPE_P2P_DEVICE
)) {
2916 data
->if_limits
[n_limits
].max
= 1;
2917 data
->if_limits
[n_limits
].types
=
2918 BIT(NL80211_IFTYPE_P2P_DEVICE
);
2922 if (data
->use_chanctx
) {
2923 hw
->wiphy
->max_scan_ssids
= 255;
2924 hw
->wiphy
->max_scan_ie_len
= IEEE80211_MAX_DATA_LEN
;
2925 hw
->wiphy
->max_remain_on_channel_duration
= 1000;
2926 data
->if_combination
.radar_detect_widths
= 0;
2927 data
->if_combination
.num_different_channels
= data
->channels
;
2929 data
->if_combination
.num_different_channels
= 1;
2930 data
->if_combination
.radar_detect_widths
=
2931 BIT(NL80211_CHAN_WIDTH_5
) |
2932 BIT(NL80211_CHAN_WIDTH_10
) |
2933 BIT(NL80211_CHAN_WIDTH_20_NOHT
) |
2934 BIT(NL80211_CHAN_WIDTH_20
) |
2935 BIT(NL80211_CHAN_WIDTH_40
) |
2936 BIT(NL80211_CHAN_WIDTH_80
) |
2937 BIT(NL80211_CHAN_WIDTH_160
);
2945 data
->if_combination
.max_interfaces
= 0;
2946 for (i
= 0; i
< n_limits
; i
++)
2947 data
->if_combination
.max_interfaces
+=
2948 data
->if_limits
[i
].max
;
2950 data
->if_combination
.n_limits
= n_limits
;
2951 data
->if_combination
.limits
= data
->if_limits
;
2954 * If we actually were asked to support combinations,
2955 * advertise them - if there's only a single thing like
2956 * only IBSS then don't advertise it as combinations.
2958 if (data
->if_combination
.max_interfaces
> 1) {
2959 hw
->wiphy
->iface_combinations
= &data
->if_combination
;
2960 hw
->wiphy
->n_iface_combinations
= 1;
2963 if (param
->ciphers
) {
2964 memcpy(data
->ciphers
, param
->ciphers
,
2965 param
->n_ciphers
* sizeof(u32
));
2966 hw
->wiphy
->cipher_suites
= data
->ciphers
;
2967 hw
->wiphy
->n_cipher_suites
= param
->n_ciphers
;
2970 INIT_DELAYED_WORK(&data
->roc_start
, hw_roc_start
);
2971 INIT_DELAYED_WORK(&data
->roc_done
, hw_roc_done
);
2972 INIT_DELAYED_WORK(&data
->hw_scan
, hw_scan_work
);
2975 hw
->offchannel_tx_hw_queue
= 4;
2977 ieee80211_hw_set(hw
, SUPPORT_FAST_XMIT
);
2978 ieee80211_hw_set(hw
, CHANCTX_STA_CSA
);
2979 ieee80211_hw_set(hw
, SUPPORTS_HT_CCK_RATES
);
2980 ieee80211_hw_set(hw
, QUEUE_CONTROL
);
2981 ieee80211_hw_set(hw
, WANT_MONITOR_VIF
);
2982 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
2983 ieee80211_hw_set(hw
, MFP_CAPABLE
);
2984 ieee80211_hw_set(hw
, SIGNAL_DBM
);
2985 ieee80211_hw_set(hw
, SUPPORTS_PS
);
2986 ieee80211_hw_set(hw
, REPORTS_TX_ACK_STATUS
);
2987 ieee80211_hw_set(hw
, HOST_BROADCAST_PS_BUFFERING
);
2988 ieee80211_hw_set(hw
, PS_NULLFUNC_STACK
);
2989 ieee80211_hw_set(hw
, TDLS_WIDER_BW
);
2991 ieee80211_hw_set(hw
, SUPPORTS_RC_TABLE
);
2992 ieee80211_hw_set(hw
, SUPPORTS_MULTI_BSSID
);
2994 hw
->wiphy
->flags
&= ~WIPHY_FLAG_PS_ON_BY_DEFAULT
;
2995 hw
->wiphy
->flags
|= WIPHY_FLAG_SUPPORTS_TDLS
|
2996 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL
|
2997 WIPHY_FLAG_AP_UAPSD
|
2998 WIPHY_FLAG_HAS_CHANNEL_SWITCH
;
2999 hw
->wiphy
->features
|= NL80211_FEATURE_ACTIVE_MONITOR
|
3000 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE
|
3001 NL80211_FEATURE_STATIC_SMPS
|
3002 NL80211_FEATURE_DYNAMIC_SMPS
|
3003 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR
;
3004 wiphy_ext_feature_set(hw
->wiphy
, NL80211_EXT_FEATURE_VHT_IBSS
);
3005 wiphy_ext_feature_set(hw
->wiphy
, NL80211_EXT_FEATURE_BEACON_PROTECTION
);
3007 hw
->wiphy
->interface_modes
= param
->iftypes
;
3009 /* ask mac80211 to reserve space for magic */
3010 hw
->vif_data_size
= sizeof(struct hwsim_vif_priv
);
3011 hw
->sta_data_size
= sizeof(struct hwsim_sta_priv
);
3012 hw
->chanctx_data_size
= sizeof(struct hwsim_chanctx_priv
);
3014 memcpy(data
->channels_2ghz
, hwsim_channels_2ghz
,
3015 sizeof(hwsim_channels_2ghz
));
3016 memcpy(data
->channels_5ghz
, hwsim_channels_5ghz
,
3017 sizeof(hwsim_channels_5ghz
));
3018 memcpy(data
->rates
, hwsim_rates
, sizeof(hwsim_rates
));
3020 for (band
= NL80211_BAND_2GHZ
; band
< NUM_NL80211_BANDS
; band
++) {
3021 struct ieee80211_supported_band
*sband
= &data
->bands
[band
];
3026 case NL80211_BAND_2GHZ
:
3027 sband
->channels
= data
->channels_2ghz
;
3028 sband
->n_channels
= ARRAY_SIZE(hwsim_channels_2ghz
);
3029 sband
->bitrates
= data
->rates
;
3030 sband
->n_bitrates
= ARRAY_SIZE(hwsim_rates
);
3032 case NL80211_BAND_5GHZ
:
3033 sband
->channels
= data
->channels_5ghz
;
3034 sband
->n_channels
= ARRAY_SIZE(hwsim_channels_5ghz
);
3035 sband
->bitrates
= data
->rates
+ 4;
3036 sband
->n_bitrates
= ARRAY_SIZE(hwsim_rates
) - 4;
3038 sband
->vht_cap
.vht_supported
= true;
3039 sband
->vht_cap
.cap
=
3040 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454
|
3041 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ
|
3042 IEEE80211_VHT_CAP_RXLDPC
|
3043 IEEE80211_VHT_CAP_SHORT_GI_80
|
3044 IEEE80211_VHT_CAP_SHORT_GI_160
|
3045 IEEE80211_VHT_CAP_TXSTBC
|
3046 IEEE80211_VHT_CAP_RXSTBC_4
|
3047 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK
;
3048 sband
->vht_cap
.vht_mcs
.rx_mcs_map
=
3049 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9
<< 0 |
3050 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 2 |
3051 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 4 |
3052 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 6 |
3053 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 8 |
3054 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 10 |
3055 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 12 |
3056 IEEE80211_VHT_MCS_SUPPORT_0_9
<< 14);
3057 sband
->vht_cap
.vht_mcs
.tx_mcs_map
=
3058 sband
->vht_cap
.vht_mcs
.rx_mcs_map
;
3064 sband
->ht_cap
.ht_supported
= true;
3065 sband
->ht_cap
.cap
= IEEE80211_HT_CAP_SUP_WIDTH_20_40
|
3066 IEEE80211_HT_CAP_GRN_FLD
|
3067 IEEE80211_HT_CAP_SGI_20
|
3068 IEEE80211_HT_CAP_SGI_40
|
3069 IEEE80211_HT_CAP_DSSSCCK40
;
3070 sband
->ht_cap
.ampdu_factor
= 0x3;
3071 sband
->ht_cap
.ampdu_density
= 0x6;
3072 memset(&sband
->ht_cap
.mcs
, 0,
3073 sizeof(sband
->ht_cap
.mcs
));
3074 sband
->ht_cap
.mcs
.rx_mask
[0] = 0xff;
3075 sband
->ht_cap
.mcs
.rx_mask
[1] = 0xff;
3076 sband
->ht_cap
.mcs
.tx_params
= IEEE80211_HT_MCS_TX_DEFINED
;
3078 mac80211_hwsim_he_capab(sband
);
3080 hw
->wiphy
->bands
[band
] = sband
;
3083 /* By default all radios belong to the first group */
3085 mutex_init(&data
->mutex
);
3087 data
->netgroup
= hwsim_net_get_netgroup(net
);
3088 data
->wmediumd
= hwsim_net_get_wmediumd(net
);
3090 /* Enable frame retransmissions for lossy channels */
3092 hw
->max_rate_tries
= 11;
3094 hw
->wiphy
->vendor_commands
= mac80211_hwsim_vendor_commands
;
3095 hw
->wiphy
->n_vendor_commands
=
3096 ARRAY_SIZE(mac80211_hwsim_vendor_commands
);
3097 hw
->wiphy
->vendor_events
= mac80211_hwsim_vendor_events
;
3098 hw
->wiphy
->n_vendor_events
= ARRAY_SIZE(mac80211_hwsim_vendor_events
);
3100 if (param
->reg_strict
)
3101 hw
->wiphy
->regulatory_flags
|= REGULATORY_STRICT_REG
;
3103 data
->regd
= param
->regd
;
3104 hw
->wiphy
->regulatory_flags
|= REGULATORY_CUSTOM_REG
;
3105 wiphy_apply_custom_regulatory(hw
->wiphy
, param
->regd
);
3106 /* give the regulatory workqueue a chance to run */
3107 schedule_timeout_interruptible(1);
3111 ieee80211_hw_set(hw
, NO_AUTO_VIF
);
3113 wiphy_ext_feature_set(hw
->wiphy
, NL80211_EXT_FEATURE_CQM_RSSI_LIST
);
3115 hrtimer_init(&data
->beacon_timer
, CLOCK_MONOTONIC
,
3116 HRTIMER_MODE_ABS_SOFT
);
3117 data
->beacon_timer
.function
= mac80211_hwsim_beacon
;
3119 err
= ieee80211_register_hw(hw
);
3121 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3126 wiphy_dbg(hw
->wiphy
, "hwaddr %pM registered\n", hw
->wiphy
->perm_addr
);
3128 if (param
->reg_alpha2
) {
3129 data
->alpha2
[0] = param
->reg_alpha2
[0];
3130 data
->alpha2
[1] = param
->reg_alpha2
[1];
3131 regulatory_hint(hw
->wiphy
, param
->reg_alpha2
);
3134 data
->debugfs
= debugfs_create_dir("hwsim", hw
->wiphy
->debugfsdir
);
3135 debugfs_create_file("ps", 0666, data
->debugfs
, data
, &hwsim_fops_ps
);
3136 debugfs_create_file("group", 0666, data
->debugfs
, data
,
3138 if (!data
->use_chanctx
)
3139 debugfs_create_file("dfs_simulate_radar", 0222,
3141 data
, &hwsim_simulate_radar
);
3143 spin_lock_bh(&hwsim_radio_lock
);
3144 err
= rhashtable_insert_fast(&hwsim_radios_rht
, &data
->rht
,
3148 GENL_SET_ERR_MSG(info
, "perm addr already present");
3149 NL_SET_BAD_ATTR(info
->extack
,
3150 info
->attrs
[HWSIM_ATTR_PERM_ADDR
]);
3152 spin_unlock_bh(&hwsim_radio_lock
);
3153 goto failed_final_insert
;
3156 list_add_tail(&data
->list
, &hwsim_radios
);
3157 hwsim_radios_generation
++;
3158 spin_unlock_bh(&hwsim_radio_lock
);
3160 hwsim_mcast_new_radio(idx
, info
, param
);
3164 failed_final_insert
:
3165 debugfs_remove_recursive(data
->debugfs
);
3166 ieee80211_unregister_hw(data
->hw
);
3168 device_release_driver(data
->dev
);
3170 device_unregister(data
->dev
);
3172 ieee80211_free_hw(hw
);
3177 static void hwsim_mcast_del_radio(int id
, const char *hwname
,
3178 struct genl_info
*info
)
3180 struct sk_buff
*skb
;
3184 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3188 data
= genlmsg_put(skb
, 0, 0, &hwsim_genl_family
, 0,
3189 HWSIM_CMD_DEL_RADIO
);
3193 ret
= nla_put_u32(skb
, HWSIM_ATTR_RADIO_ID
, id
);
3197 ret
= nla_put(skb
, HWSIM_ATTR_RADIO_NAME
, strlen(hwname
),
3202 genlmsg_end(skb
, data
);
3204 hwsim_mcast_config_msg(skb
, info
);
3212 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data
*data
,
3214 struct genl_info
*info
)
3216 hwsim_mcast_del_radio(data
->idx
, hwname
, info
);
3217 debugfs_remove_recursive(data
->debugfs
);
3218 ieee80211_unregister_hw(data
->hw
);
3219 device_release_driver(data
->dev
);
3220 device_unregister(data
->dev
);
3221 ieee80211_free_hw(data
->hw
);
3224 static int mac80211_hwsim_get_radio(struct sk_buff
*skb
,
3225 struct mac80211_hwsim_data
*data
,
3226 u32 portid
, u32 seq
,
3227 struct netlink_callback
*cb
, int flags
)
3230 struct hwsim_new_radio_params param
= { };
3231 int res
= -EMSGSIZE
;
3233 hdr
= genlmsg_put(skb
, portid
, seq
, &hwsim_genl_family
, flags
,
3234 HWSIM_CMD_GET_RADIO
);
3239 genl_dump_check_consistent(cb
, hdr
);
3241 if (data
->alpha2
[0] && data
->alpha2
[1])
3242 param
.reg_alpha2
= data
->alpha2
;
3244 param
.reg_strict
= !!(data
->hw
->wiphy
->regulatory_flags
&
3245 REGULATORY_STRICT_REG
);
3246 param
.p2p_device
= !!(data
->hw
->wiphy
->interface_modes
&
3247 BIT(NL80211_IFTYPE_P2P_DEVICE
));
3248 param
.use_chanctx
= data
->use_chanctx
;
3249 param
.regd
= data
->regd
;
3250 param
.channels
= data
->channels
;
3251 param
.hwname
= wiphy_name(data
->hw
->wiphy
);
3253 res
= append_radio_msg(skb
, data
->idx
, ¶m
);
3257 genlmsg_end(skb
, hdr
);
3261 genlmsg_cancel(skb
, hdr
);
3265 static void mac80211_hwsim_free(void)
3267 struct mac80211_hwsim_data
*data
;
3269 spin_lock_bh(&hwsim_radio_lock
);
3270 while ((data
= list_first_entry_or_null(&hwsim_radios
,
3271 struct mac80211_hwsim_data
,
3273 list_del(&data
->list
);
3274 spin_unlock_bh(&hwsim_radio_lock
);
3275 mac80211_hwsim_del_radio(data
, wiphy_name(data
->hw
->wiphy
),
3277 spin_lock_bh(&hwsim_radio_lock
);
3279 spin_unlock_bh(&hwsim_radio_lock
);
3280 class_destroy(hwsim_class
);
3283 static const struct net_device_ops hwsim_netdev_ops
= {
3284 .ndo_start_xmit
= hwsim_mon_xmit
,
3285 .ndo_set_mac_address
= eth_mac_addr
,
3286 .ndo_validate_addr
= eth_validate_addr
,
3289 static void hwsim_mon_setup(struct net_device
*dev
)
3291 dev
->netdev_ops
= &hwsim_netdev_ops
;
3292 dev
->needs_free_netdev
= true;
3294 dev
->priv_flags
|= IFF_NO_QUEUE
;
3295 dev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
3296 eth_zero_addr(dev
->dev_addr
);
3297 dev
->dev_addr
[0] = 0x12;
3300 static struct mac80211_hwsim_data
*get_hwsim_data_ref_from_addr(const u8
*addr
)
3302 return rhashtable_lookup_fast(&hwsim_radios_rht
,
3307 static void hwsim_register_wmediumd(struct net
*net
, u32 portid
)
3309 struct mac80211_hwsim_data
*data
;
3311 hwsim_net_set_wmediumd(net
, portid
);
3313 spin_lock_bh(&hwsim_radio_lock
);
3314 list_for_each_entry(data
, &hwsim_radios
, list
) {
3315 if (data
->netgroup
== hwsim_net_get_netgroup(net
))
3316 data
->wmediumd
= portid
;
3318 spin_unlock_bh(&hwsim_radio_lock
);
3321 static int hwsim_tx_info_frame_received_nl(struct sk_buff
*skb_2
,
3322 struct genl_info
*info
)
3325 struct ieee80211_hdr
*hdr
;
3326 struct mac80211_hwsim_data
*data2
;
3327 struct ieee80211_tx_info
*txi
;
3328 struct hwsim_tx_rate
*tx_attempts
;
3330 struct sk_buff
*skb
, *tmp
;
3332 unsigned int hwsim_flags
;
3336 if (!info
->attrs
[HWSIM_ATTR_ADDR_TRANSMITTER
] ||
3337 !info
->attrs
[HWSIM_ATTR_FLAGS
] ||
3338 !info
->attrs
[HWSIM_ATTR_COOKIE
] ||
3339 !info
->attrs
[HWSIM_ATTR_SIGNAL
] ||
3340 !info
->attrs
[HWSIM_ATTR_TX_INFO
])
3343 src
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_ADDR_TRANSMITTER
]);
3344 hwsim_flags
= nla_get_u32(info
->attrs
[HWSIM_ATTR_FLAGS
]);
3345 ret_skb_cookie
= nla_get_u64(info
->attrs
[HWSIM_ATTR_COOKIE
]);
3347 data2
= get_hwsim_data_ref_from_addr(src
);
3351 if (!hwsim_virtio_enabled
) {
3352 if (hwsim_net_get_netgroup(genl_info_net(info
)) !=
3356 if (info
->snd_portid
!= data2
->wmediumd
)
3360 /* look for the skb matching the cookie passed back from user */
3361 skb_queue_walk_safe(&data2
->pending
, skb
, tmp
) {
3364 txi
= IEEE80211_SKB_CB(skb
);
3365 skb_cookie
= (u64
)(uintptr_t)txi
->rate_driver_data
[0];
3367 if (skb_cookie
== ret_skb_cookie
) {
3368 skb_unlink(skb
, &data2
->pending
);
3378 /* Tx info received because the frame was broadcasted on user space,
3379 so we get all the necessary info: tx attempts and skb control buff */
3381 tx_attempts
= (struct hwsim_tx_rate
*)nla_data(
3382 info
->attrs
[HWSIM_ATTR_TX_INFO
]);
3384 /* now send back TX status */
3385 txi
= IEEE80211_SKB_CB(skb
);
3387 ieee80211_tx_info_clear_status(txi
);
3389 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
3390 txi
->status
.rates
[i
].idx
= tx_attempts
[i
].idx
;
3391 txi
->status
.rates
[i
].count
= tx_attempts
[i
].count
;
3394 txi
->status
.ack_signal
= nla_get_u32(info
->attrs
[HWSIM_ATTR_SIGNAL
]);
3396 if (!(hwsim_flags
& HWSIM_TX_CTL_NO_ACK
) &&
3397 (hwsim_flags
& HWSIM_TX_STAT_ACK
)) {
3398 if (skb
->len
>= 16) {
3399 hdr
= (struct ieee80211_hdr
*) skb
->data
;
3400 mac80211_hwsim_monitor_ack(data2
->channel
,
3403 txi
->flags
|= IEEE80211_TX_STAT_ACK
;
3405 ieee80211_tx_status_irqsafe(data2
->hw
, skb
);
3412 static int hwsim_cloned_frame_received_nl(struct sk_buff
*skb_2
,
3413 struct genl_info
*info
)
3415 struct mac80211_hwsim_data
*data2
;
3416 struct ieee80211_rx_status rx_status
;
3417 struct ieee80211_hdr
*hdr
;
3421 struct sk_buff
*skb
= NULL
;
3423 if (!info
->attrs
[HWSIM_ATTR_ADDR_RECEIVER
] ||
3424 !info
->attrs
[HWSIM_ATTR_FRAME
] ||
3425 !info
->attrs
[HWSIM_ATTR_RX_RATE
] ||
3426 !info
->attrs
[HWSIM_ATTR_SIGNAL
])
3429 dst
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_ADDR_RECEIVER
]);
3430 frame_data_len
= nla_len(info
->attrs
[HWSIM_ATTR_FRAME
]);
3431 frame_data
= (void *)nla_data(info
->attrs
[HWSIM_ATTR_FRAME
]);
3433 /* Allocate new skb here */
3434 skb
= alloc_skb(frame_data_len
, GFP_KERNEL
);
3438 if (frame_data_len
> IEEE80211_MAX_DATA_LEN
)
3442 skb_put_data(skb
, frame_data
, frame_data_len
);
3444 data2
= get_hwsim_data_ref_from_addr(dst
);
3448 if (!hwsim_virtio_enabled
) {
3449 if (hwsim_net_get_netgroup(genl_info_net(info
)) !=
3453 if (info
->snd_portid
!= data2
->wmediumd
)
3457 /* check if radio is configured properly */
3459 if (data2
->idle
|| !data2
->started
)
3462 /* A frame is received from user space */
3463 memset(&rx_status
, 0, sizeof(rx_status
));
3464 if (info
->attrs
[HWSIM_ATTR_FREQ
]) {
3465 /* throw away off-channel packets, but allow both the temporary
3466 * ("hw" scan/remain-on-channel) and regular channel, since the
3467 * internal datapath also allows this
3469 mutex_lock(&data2
->mutex
);
3470 rx_status
.freq
= nla_get_u32(info
->attrs
[HWSIM_ATTR_FREQ
]);
3472 if (rx_status
.freq
!= data2
->channel
->center_freq
&&
3473 (!data2
->tmp_chan
||
3474 rx_status
.freq
!= data2
->tmp_chan
->center_freq
)) {
3475 mutex_unlock(&data2
->mutex
);
3478 mutex_unlock(&data2
->mutex
);
3480 rx_status
.freq
= data2
->channel
->center_freq
;
3483 rx_status
.band
= data2
->channel
->band
;
3484 rx_status
.rate_idx
= nla_get_u32(info
->attrs
[HWSIM_ATTR_RX_RATE
]);
3485 rx_status
.signal
= nla_get_u32(info
->attrs
[HWSIM_ATTR_SIGNAL
]);
3487 hdr
= (void *)skb
->data
;
3489 if (ieee80211_is_beacon(hdr
->frame_control
) ||
3490 ieee80211_is_probe_resp(hdr
->frame_control
))
3491 rx_status
.boottime_ns
= ktime_get_boottime_ns();
3493 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
3495 data2
->rx_bytes
+= skb
->len
;
3496 ieee80211_rx_irqsafe(data2
->hw
, skb
);
3500 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__
);
3506 static int hwsim_register_received_nl(struct sk_buff
*skb_2
,
3507 struct genl_info
*info
)
3509 struct net
*net
= genl_info_net(info
);
3510 struct mac80211_hwsim_data
*data
;
3513 spin_lock_bh(&hwsim_radio_lock
);
3514 list_for_each_entry(data
, &hwsim_radios
, list
)
3515 chans
= max(chans
, data
->channels
);
3516 spin_unlock_bh(&hwsim_radio_lock
);
3518 /* In the future we should revise the userspace API and allow it
3519 * to set a flag that it does support multi-channel, then we can
3520 * let this pass conditionally on the flag.
3521 * For current userspace, prohibit it since it won't work right.
3526 if (hwsim_net_get_wmediumd(net
))
3529 hwsim_register_wmediumd(net
, info
->snd_portid
);
3531 pr_debug("mac80211_hwsim: received a REGISTER, "
3532 "switching to wmediumd mode with pid %d\n", info
->snd_portid
);
3537 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3538 static bool hwsim_known_ciphers(const u32
*ciphers
, int n_ciphers
)
3542 for (i
= 0; i
< n_ciphers
; i
++) {
3546 for (j
= 0; j
< ARRAY_SIZE(hwsim_ciphers
); j
++) {
3547 if (ciphers
[i
] == hwsim_ciphers
[j
]) {
3560 static int hwsim_new_radio_nl(struct sk_buff
*msg
, struct genl_info
*info
)
3562 struct hwsim_new_radio_params param
= { 0 };
3563 const char *hwname
= NULL
;
3566 param
.reg_strict
= info
->attrs
[HWSIM_ATTR_REG_STRICT_REG
];
3567 param
.p2p_device
= info
->attrs
[HWSIM_ATTR_SUPPORT_P2P_DEVICE
];
3568 param
.channels
= channels
;
3569 param
.destroy_on_close
=
3570 info
->attrs
[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE
];
3572 if (info
->attrs
[HWSIM_ATTR_CHANNELS
])
3573 param
.channels
= nla_get_u32(info
->attrs
[HWSIM_ATTR_CHANNELS
]);
3575 if (param
.channels
< 1) {
3576 GENL_SET_ERR_MSG(info
, "must have at least one channel");
3580 if (param
.channels
> CFG80211_MAX_NUM_DIFFERENT_CHANNELS
) {
3581 GENL_SET_ERR_MSG(info
, "too many channels specified");
3585 if (info
->attrs
[HWSIM_ATTR_NO_VIF
])
3586 param
.no_vif
= true;
3588 if (info
->attrs
[HWSIM_ATTR_USE_CHANCTX
])
3589 param
.use_chanctx
= true;
3591 param
.use_chanctx
= (param
.channels
> 1);
3593 if (info
->attrs
[HWSIM_ATTR_REG_HINT_ALPHA2
])
3595 nla_data(info
->attrs
[HWSIM_ATTR_REG_HINT_ALPHA2
]);
3597 if (info
->attrs
[HWSIM_ATTR_REG_CUSTOM_REG
]) {
3598 u32 idx
= nla_get_u32(info
->attrs
[HWSIM_ATTR_REG_CUSTOM_REG
]);
3600 if (idx
>= ARRAY_SIZE(hwsim_world_regdom_custom
))
3603 idx
= array_index_nospec(idx
,
3604 ARRAY_SIZE(hwsim_world_regdom_custom
));
3605 param
.regd
= hwsim_world_regdom_custom
[idx
];
3608 if (info
->attrs
[HWSIM_ATTR_PERM_ADDR
]) {
3609 if (!is_valid_ether_addr(
3610 nla_data(info
->attrs
[HWSIM_ATTR_PERM_ADDR
]))) {
3611 GENL_SET_ERR_MSG(info
,"MAC is no valid source addr");
3612 NL_SET_BAD_ATTR(info
->extack
,
3613 info
->attrs
[HWSIM_ATTR_PERM_ADDR
]);
3617 param
.perm_addr
= nla_data(info
->attrs
[HWSIM_ATTR_PERM_ADDR
]);
3620 if (info
->attrs
[HWSIM_ATTR_IFTYPE_SUPPORT
]) {
3622 nla_get_u32(info
->attrs
[HWSIM_ATTR_IFTYPE_SUPPORT
]);
3624 if (param
.iftypes
& ~HWSIM_IFTYPE_SUPPORT_MASK
) {
3625 NL_SET_ERR_MSG_ATTR(info
->extack
,
3626 info
->attrs
[HWSIM_ATTR_IFTYPE_SUPPORT
],
3627 "cannot support more iftypes than kernel");
3631 param
.iftypes
= HWSIM_IFTYPE_SUPPORT_MASK
;
3634 /* ensure both flag and iftype support is honored */
3635 if (param
.p2p_device
||
3636 param
.iftypes
& BIT(NL80211_IFTYPE_P2P_DEVICE
)) {
3637 param
.iftypes
|= BIT(NL80211_IFTYPE_P2P_DEVICE
);
3638 param
.p2p_device
= true;
3641 if (info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
]) {
3642 u32 len
= nla_len(info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
]);
3645 nla_data(info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
]);
3647 if (len
% sizeof(u32
)) {
3648 NL_SET_ERR_MSG_ATTR(info
->extack
,
3649 info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
],
3650 "bad cipher list length");
3654 param
.n_ciphers
= len
/ sizeof(u32
);
3656 if (param
.n_ciphers
> ARRAY_SIZE(hwsim_ciphers
)) {
3657 NL_SET_ERR_MSG_ATTR(info
->extack
,
3658 info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
],
3659 "too many ciphers specified");
3663 if (!hwsim_known_ciphers(param
.ciphers
, param
.n_ciphers
)) {
3664 NL_SET_ERR_MSG_ATTR(info
->extack
,
3665 info
->attrs
[HWSIM_ATTR_CIPHER_SUPPORT
],
3666 "unsupported ciphers specified");
3671 if (info
->attrs
[HWSIM_ATTR_RADIO_NAME
]) {
3672 hwname
= kasprintf(GFP_KERNEL
, "%.*s",
3673 nla_len(info
->attrs
[HWSIM_ATTR_RADIO_NAME
]),
3674 (char *)nla_data(info
->attrs
[HWSIM_ATTR_RADIO_NAME
]));
3677 param
.hwname
= hwname
;
3680 ret
= mac80211_hwsim_new_radio(info
, ¶m
);
3685 static int hwsim_del_radio_nl(struct sk_buff
*msg
, struct genl_info
*info
)
3687 struct mac80211_hwsim_data
*data
;
3689 const char *hwname
= NULL
;
3691 if (info
->attrs
[HWSIM_ATTR_RADIO_ID
]) {
3692 idx
= nla_get_u32(info
->attrs
[HWSIM_ATTR_RADIO_ID
]);
3693 } else if (info
->attrs
[HWSIM_ATTR_RADIO_NAME
]) {
3694 hwname
= kasprintf(GFP_KERNEL
, "%.*s",
3695 nla_len(info
->attrs
[HWSIM_ATTR_RADIO_NAME
]),
3696 (char *)nla_data(info
->attrs
[HWSIM_ATTR_RADIO_NAME
]));
3702 spin_lock_bh(&hwsim_radio_lock
);
3703 list_for_each_entry(data
, &hwsim_radios
, list
) {
3705 if (data
->idx
!= idx
)
3709 strcmp(hwname
, wiphy_name(data
->hw
->wiphy
)))
3713 if (!net_eq(wiphy_net(data
->hw
->wiphy
), genl_info_net(info
)))
3716 list_del(&data
->list
);
3717 rhashtable_remove_fast(&hwsim_radios_rht
, &data
->rht
,
3719 hwsim_radios_generation
++;
3720 spin_unlock_bh(&hwsim_radio_lock
);
3721 mac80211_hwsim_del_radio(data
, wiphy_name(data
->hw
->wiphy
),
3726 spin_unlock_bh(&hwsim_radio_lock
);
3732 static int hwsim_get_radio_nl(struct sk_buff
*msg
, struct genl_info
*info
)
3734 struct mac80211_hwsim_data
*data
;
3735 struct sk_buff
*skb
;
3736 int idx
, res
= -ENODEV
;
3738 if (!info
->attrs
[HWSIM_ATTR_RADIO_ID
])
3740 idx
= nla_get_u32(info
->attrs
[HWSIM_ATTR_RADIO_ID
]);
3742 spin_lock_bh(&hwsim_radio_lock
);
3743 list_for_each_entry(data
, &hwsim_radios
, list
) {
3744 if (data
->idx
!= idx
)
3747 if (!net_eq(wiphy_net(data
->hw
->wiphy
), genl_info_net(info
)))
3750 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
3756 res
= mac80211_hwsim_get_radio(skb
, data
, info
->snd_portid
,
3757 info
->snd_seq
, NULL
, 0);
3763 res
= genlmsg_reply(skb
, info
);
3768 spin_unlock_bh(&hwsim_radio_lock
);
3773 static int hwsim_dump_radio_nl(struct sk_buff
*skb
,
3774 struct netlink_callback
*cb
)
3776 int last_idx
= cb
->args
[0] - 1;
3777 struct mac80211_hwsim_data
*data
= NULL
;
3781 spin_lock_bh(&hwsim_radio_lock
);
3782 cb
->seq
= hwsim_radios_generation
;
3784 if (last_idx
>= hwsim_radio_idx
-1)
3787 list_for_each_entry(data
, &hwsim_radios
, list
) {
3788 if (data
->idx
<= last_idx
)
3791 if (!net_eq(wiphy_net(data
->hw
->wiphy
), sock_net(skb
->sk
)))
3794 res
= mac80211_hwsim_get_radio(skb
, data
,
3795 NETLINK_CB(cb
->skb
).portid
,
3796 cb
->nlh
->nlmsg_seq
, cb
,
3801 last_idx
= data
->idx
;
3804 cb
->args
[0] = last_idx
+ 1;
3806 /* list changed, but no new element sent, set interrupted flag */
3807 if (skb
->len
== 0 && cb
->prev_seq
&& cb
->seq
!= cb
->prev_seq
) {
3808 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
3809 cb
->nlh
->nlmsg_seq
, &hwsim_genl_family
,
3810 NLM_F_MULTI
, HWSIM_CMD_GET_RADIO
);
3812 genl_dump_check_consistent(cb
, hdr
);
3813 genlmsg_end(skb
, hdr
);
3820 spin_unlock_bh(&hwsim_radio_lock
);
3821 return res
?: skb
->len
;
3824 /* Generic Netlink operations array */
3825 static const struct genl_ops hwsim_ops
[] = {
3827 .cmd
= HWSIM_CMD_REGISTER
,
3828 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3829 .doit
= hwsim_register_received_nl
,
3830 .flags
= GENL_UNS_ADMIN_PERM
,
3833 .cmd
= HWSIM_CMD_FRAME
,
3834 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3835 .doit
= hwsim_cloned_frame_received_nl
,
3838 .cmd
= HWSIM_CMD_TX_INFO_FRAME
,
3839 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3840 .doit
= hwsim_tx_info_frame_received_nl
,
3843 .cmd
= HWSIM_CMD_NEW_RADIO
,
3844 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3845 .doit
= hwsim_new_radio_nl
,
3846 .flags
= GENL_UNS_ADMIN_PERM
,
3849 .cmd
= HWSIM_CMD_DEL_RADIO
,
3850 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3851 .doit
= hwsim_del_radio_nl
,
3852 .flags
= GENL_UNS_ADMIN_PERM
,
3855 .cmd
= HWSIM_CMD_GET_RADIO
,
3856 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
3857 .doit
= hwsim_get_radio_nl
,
3858 .dumpit
= hwsim_dump_radio_nl
,
3862 static struct genl_family hwsim_genl_family __ro_after_init
= {
3863 .name
= "MAC80211_HWSIM",
3865 .maxattr
= HWSIM_ATTR_MAX
,
3866 .policy
= hwsim_genl_policy
,
3868 .module
= THIS_MODULE
,
3870 .n_ops
= ARRAY_SIZE(hwsim_ops
),
3871 .mcgrps
= hwsim_mcgrps
,
3872 .n_mcgrps
= ARRAY_SIZE(hwsim_mcgrps
),
3875 static void remove_user_radios(u32 portid
)
3877 struct mac80211_hwsim_data
*entry
, *tmp
;
3880 spin_lock_bh(&hwsim_radio_lock
);
3881 list_for_each_entry_safe(entry
, tmp
, &hwsim_radios
, list
) {
3882 if (entry
->destroy_on_close
&& entry
->portid
== portid
) {
3883 list_move(&entry
->list
, &list
);
3884 rhashtable_remove_fast(&hwsim_radios_rht
, &entry
->rht
,
3886 hwsim_radios_generation
++;
3889 spin_unlock_bh(&hwsim_radio_lock
);
3891 list_for_each_entry_safe(entry
, tmp
, &list
, list
) {
3892 list_del(&entry
->list
);
3893 mac80211_hwsim_del_radio(entry
, wiphy_name(entry
->hw
->wiphy
),
3898 static int mac80211_hwsim_netlink_notify(struct notifier_block
*nb
,
3899 unsigned long state
,
3902 struct netlink_notify
*notify
= _notify
;
3904 if (state
!= NETLINK_URELEASE
)
3907 remove_user_radios(notify
->portid
);
3909 if (notify
->portid
== hwsim_net_get_wmediumd(notify
->net
)) {
3910 printk(KERN_INFO
"mac80211_hwsim: wmediumd released netlink"
3911 " socket, switching to perfect channel medium\n");
3912 hwsim_register_wmediumd(notify
->net
, 0);
3918 static struct notifier_block hwsim_netlink_notifier
= {
3919 .notifier_call
= mac80211_hwsim_netlink_notify
,
3922 static int __init
hwsim_init_netlink(void)
3926 printk(KERN_INFO
"mac80211_hwsim: initializing netlink\n");
3928 rc
= genl_register_family(&hwsim_genl_family
);
3932 rc
= netlink_register_notifier(&hwsim_netlink_notifier
);
3934 genl_unregister_family(&hwsim_genl_family
);
3941 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__
);
3945 static __net_init
int hwsim_init_net(struct net
*net
)
3947 return hwsim_net_set_netgroup(net
);
3950 static void __net_exit
hwsim_exit_net(struct net
*net
)
3952 struct mac80211_hwsim_data
*data
, *tmp
;
3955 spin_lock_bh(&hwsim_radio_lock
);
3956 list_for_each_entry_safe(data
, tmp
, &hwsim_radios
, list
) {
3957 if (!net_eq(wiphy_net(data
->hw
->wiphy
), net
))
3960 /* Radios created in init_net are returned to init_net. */
3961 if (data
->netgroup
== hwsim_net_get_netgroup(&init_net
))
3964 list_move(&data
->list
, &list
);
3965 rhashtable_remove_fast(&hwsim_radios_rht
, &data
->rht
,
3967 hwsim_radios_generation
++;
3969 spin_unlock_bh(&hwsim_radio_lock
);
3971 list_for_each_entry_safe(data
, tmp
, &list
, list
) {
3972 list_del(&data
->list
);
3973 mac80211_hwsim_del_radio(data
,
3974 wiphy_name(data
->hw
->wiphy
),
3978 ida_simple_remove(&hwsim_netgroup_ida
, hwsim_net_get_netgroup(net
));
3981 static struct pernet_operations hwsim_net_ops
= {
3982 .init
= hwsim_init_net
,
3983 .exit
= hwsim_exit_net
,
3984 .id
= &hwsim_net_id
,
3985 .size
= sizeof(struct hwsim_net
),
3988 static void hwsim_exit_netlink(void)
3990 /* unregister the notifier */
3991 netlink_unregister_notifier(&hwsim_netlink_notifier
);
3992 /* unregister the family */
3993 genl_unregister_family(&hwsim_genl_family
);
3996 #if IS_REACHABLE(CONFIG_VIRTIO)
3997 static void hwsim_virtio_tx_done(struct virtqueue
*vq
)
4000 struct sk_buff
*skb
;
4001 unsigned long flags
;
4003 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
4004 while ((skb
= virtqueue_get_buf(vq
, &len
)))
4006 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4009 static int hwsim_virtio_handle_cmd(struct sk_buff
*skb
)
4011 struct nlmsghdr
*nlh
;
4012 struct genlmsghdr
*gnlh
;
4013 struct nlattr
*tb
[HWSIM_ATTR_MAX
+ 1];
4014 struct genl_info info
= {};
4017 nlh
= nlmsg_hdr(skb
);
4018 gnlh
= nlmsg_data(nlh
);
4019 err
= genlmsg_parse(nlh
, &hwsim_genl_family
, tb
, HWSIM_ATTR_MAX
,
4020 hwsim_genl_policy
, NULL
);
4022 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err
);
4028 switch (gnlh
->cmd
) {
4029 case HWSIM_CMD_FRAME
:
4030 hwsim_cloned_frame_received_nl(skb
, &info
);
4032 case HWSIM_CMD_TX_INFO_FRAME
:
4033 hwsim_tx_info_frame_received_nl(skb
, &info
);
4036 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh
->cmd
);
4042 static void hwsim_virtio_rx_work(struct work_struct
*work
)
4044 struct virtqueue
*vq
;
4046 struct sk_buff
*skb
;
4047 struct scatterlist sg
[1];
4049 unsigned long flags
;
4051 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
4052 if (!hwsim_virtio_enabled
)
4055 skb
= virtqueue_get_buf(hwsim_vqs
[HWSIM_VQ_RX
], &len
);
4058 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4060 skb
->data
= skb
->head
;
4061 skb_set_tail_pointer(skb
, len
);
4062 hwsim_virtio_handle_cmd(skb
);
4064 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
4065 if (!hwsim_virtio_enabled
) {
4069 vq
= hwsim_vqs
[HWSIM_VQ_RX
];
4070 sg_init_one(sg
, skb
->head
, skb_end_offset(skb
));
4071 err
= virtqueue_add_inbuf(vq
, sg
, 1, skb
, GFP_KERNEL
);
4072 if (WARN(err
, "virtqueue_add_inbuf returned %d\n", err
))
4076 schedule_work(&hwsim_virtio_rx
);
4079 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4082 static void hwsim_virtio_rx_done(struct virtqueue
*vq
)
4084 schedule_work(&hwsim_virtio_rx
);
4087 static int init_vqs(struct virtio_device
*vdev
)
4089 vq_callback_t
*callbacks
[HWSIM_NUM_VQS
] = {
4090 [HWSIM_VQ_TX
] = hwsim_virtio_tx_done
,
4091 [HWSIM_VQ_RX
] = hwsim_virtio_rx_done
,
4093 const char *names
[HWSIM_NUM_VQS
] = {
4094 [HWSIM_VQ_TX
] = "tx",
4095 [HWSIM_VQ_RX
] = "rx",
4098 return virtio_find_vqs(vdev
, HWSIM_NUM_VQS
,
4099 hwsim_vqs
, callbacks
, names
, NULL
);
4102 static int fill_vq(struct virtqueue
*vq
)
4105 struct sk_buff
*skb
;
4106 struct scatterlist sg
[1];
4108 for (i
= 0; i
< virtqueue_get_vring_size(vq
); i
++) {
4109 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4113 sg_init_one(sg
, skb
->head
, skb_end_offset(skb
));
4114 err
= virtqueue_add_inbuf(vq
, sg
, 1, skb
, GFP_KERNEL
);
4124 static void remove_vqs(struct virtio_device
*vdev
)
4128 vdev
->config
->reset(vdev
);
4130 for (i
= 0; i
< ARRAY_SIZE(hwsim_vqs
); i
++) {
4131 struct virtqueue
*vq
= hwsim_vqs
[i
];
4132 struct sk_buff
*skb
;
4134 while ((skb
= virtqueue_detach_unused_buf(vq
)))
4138 vdev
->config
->del_vqs(vdev
);
4141 static int hwsim_virtio_probe(struct virtio_device
*vdev
)
4144 unsigned long flags
;
4146 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
4147 if (hwsim_virtio_enabled
) {
4148 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4151 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4153 err
= init_vqs(vdev
);
4157 err
= fill_vq(hwsim_vqs
[HWSIM_VQ_RX
]);
4161 spin_lock_irqsave(&hwsim_virtio_lock
, flags
);
4162 hwsim_virtio_enabled
= true;
4163 spin_unlock_irqrestore(&hwsim_virtio_lock
, flags
);
4165 schedule_work(&hwsim_virtio_rx
);
4173 static void hwsim_virtio_remove(struct virtio_device
*vdev
)
4175 hwsim_virtio_enabled
= false;
4177 cancel_work_sync(&hwsim_virtio_rx
);
4182 /* MAC80211_HWSIM virtio device id table */
4183 static const struct virtio_device_id id_table
[] = {
4184 { VIRTIO_ID_MAC80211_HWSIM
, VIRTIO_DEV_ANY_ID
},
4187 MODULE_DEVICE_TABLE(virtio
, id_table
);
4189 static struct virtio_driver virtio_hwsim
= {
4190 .driver
.name
= KBUILD_MODNAME
,
4191 .driver
.owner
= THIS_MODULE
,
4192 .id_table
= id_table
,
4193 .probe
= hwsim_virtio_probe
,
4194 .remove
= hwsim_virtio_remove
,
4197 static int hwsim_register_virtio_driver(void)
4199 spin_lock_init(&hwsim_virtio_lock
);
4201 return register_virtio_driver(&virtio_hwsim
);
4204 static void hwsim_unregister_virtio_driver(void)
4206 unregister_virtio_driver(&virtio_hwsim
);
4209 static inline int hwsim_register_virtio_driver(void)
4214 static inline void hwsim_unregister_virtio_driver(void)
4219 static int __init
init_mac80211_hwsim(void)
4223 if (radios
< 0 || radios
> 100)
4229 spin_lock_init(&hwsim_radio_lock
);
4231 err
= rhashtable_init(&hwsim_radios_rht
, &hwsim_rht_params
);
4235 err
= register_pernet_device(&hwsim_net_ops
);
4239 err
= platform_driver_register(&mac80211_hwsim_driver
);
4241 goto out_unregister_pernet
;
4243 err
= hwsim_init_netlink();
4245 goto out_unregister_driver
;
4247 err
= hwsim_register_virtio_driver();
4249 goto out_exit_netlink
;
4251 hwsim_class
= class_create(THIS_MODULE
, "mac80211_hwsim");
4252 if (IS_ERR(hwsim_class
)) {
4253 err
= PTR_ERR(hwsim_class
);
4254 goto out_exit_virtio
;
4257 for (i
= 0; i
< radios
; i
++) {
4258 struct hwsim_new_radio_params param
= { 0 };
4260 param
.channels
= channels
;
4263 case HWSIM_REGTEST_DIFF_COUNTRY
:
4264 if (i
< ARRAY_SIZE(hwsim_alpha2s
))
4265 param
.reg_alpha2
= hwsim_alpha2s
[i
];
4267 case HWSIM_REGTEST_DRIVER_REG_FOLLOW
:
4269 param
.reg_alpha2
= hwsim_alpha2s
[0];
4271 case HWSIM_REGTEST_STRICT_ALL
:
4272 param
.reg_strict
= true;
4274 case HWSIM_REGTEST_DRIVER_REG_ALL
:
4275 param
.reg_alpha2
= hwsim_alpha2s
[0];
4277 case HWSIM_REGTEST_WORLD_ROAM
:
4279 param
.regd
= &hwsim_world_regdom_custom_01
;
4281 case HWSIM_REGTEST_CUSTOM_WORLD
:
4282 param
.regd
= &hwsim_world_regdom_custom_01
;
4284 case HWSIM_REGTEST_CUSTOM_WORLD_2
:
4286 param
.regd
= &hwsim_world_regdom_custom_01
;
4288 param
.regd
= &hwsim_world_regdom_custom_02
;
4290 case HWSIM_REGTEST_STRICT_FOLLOW
:
4292 param
.reg_strict
= true;
4293 param
.reg_alpha2
= hwsim_alpha2s
[0];
4296 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG
:
4298 param
.reg_strict
= true;
4299 param
.reg_alpha2
= hwsim_alpha2s
[0];
4300 } else if (i
== 1) {
4301 param
.reg_alpha2
= hwsim_alpha2s
[1];
4304 case HWSIM_REGTEST_ALL
:
4307 param
.regd
= &hwsim_world_regdom_custom_01
;
4310 param
.regd
= &hwsim_world_regdom_custom_02
;
4313 param
.reg_alpha2
= hwsim_alpha2s
[0];
4316 param
.reg_alpha2
= hwsim_alpha2s
[1];
4319 param
.reg_strict
= true;
4320 param
.reg_alpha2
= hwsim_alpha2s
[2];
4328 param
.p2p_device
= support_p2p_device
;
4329 param
.use_chanctx
= channels
> 1;
4330 param
.iftypes
= HWSIM_IFTYPE_SUPPORT_MASK
;
4331 if (param
.p2p_device
)
4332 param
.iftypes
|= BIT(NL80211_IFTYPE_P2P_DEVICE
);
4334 err
= mac80211_hwsim_new_radio(NULL
, ¶m
);
4336 goto out_free_radios
;
4339 hwsim_mon
= alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN
,
4341 if (hwsim_mon
== NULL
) {
4343 goto out_free_radios
;
4347 err
= dev_alloc_name(hwsim_mon
, hwsim_mon
->name
);
4353 err
= register_netdevice(hwsim_mon
);
4363 free_netdev(hwsim_mon
);
4365 mac80211_hwsim_free();
4367 hwsim_unregister_virtio_driver();
4369 hwsim_exit_netlink();
4370 out_unregister_driver
:
4371 platform_driver_unregister(&mac80211_hwsim_driver
);
4372 out_unregister_pernet
:
4373 unregister_pernet_device(&hwsim_net_ops
);
4375 rhashtable_destroy(&hwsim_radios_rht
);
4378 module_init(init_mac80211_hwsim
);
4380 static void __exit
exit_mac80211_hwsim(void)
4382 pr_debug("mac80211_hwsim: unregister radios\n");
4384 hwsim_unregister_virtio_driver();
4385 hwsim_exit_netlink();
4387 mac80211_hwsim_free();
4389 rhashtable_destroy(&hwsim_radios_rht
);
4390 unregister_netdev(hwsim_mon
);
4391 platform_driver_unregister(&mac80211_hwsim_driver
);
4392 unregister_pernet_device(&hwsim_net_ops
);
4394 module_exit(exit_mac80211_hwsim
);