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 #define NUM_S1G_CHANS_US 51
381 static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
383 static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
385 .cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
388 S1G_CAP3_MAX_MPDU_LEN,
393 S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
395 .nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
396 /* RX Highest Supported Long GI Data Rate 0:7 */
398 /* RX Highest Supported Long GI Data Rate 0:7 */
399 /* TX S1G MCS Map 0:6 */
401 /* TX S1G MCS Map :7 */
402 /* TX Highest Supported Long GI Data Rate 0:6 */
404 /* TX Highest Supported Long GI Data Rate 7:8 */
405 /* Rx Single spatial stream and S1G-MCS Map for 1MHz */
406 /* Tx Single spatial stream and S1G-MCS Map for 1MHz */
410 static void hwsim_init_s1g_channels(struct ieee80211_channel *channels)
414 for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
415 freq = 902000 + (ch + 1) * 500;
416 channels[ch].band = NL80211_BAND_S1GHZ;
417 channels[ch].center_freq = KHZ_TO_MHZ(freq);
418 channels[ch].freq_offset = freq % 1000;
419 channels[ch].hw_value = ch + 1;
423 static const struct ieee80211_rate hwsim_rates[] = {
425 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
426 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
427 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
438 static const u32 hwsim_ciphers[] = {
439 WLAN_CIPHER_SUITE_WEP40,
440 WLAN_CIPHER_SUITE_WEP104,
441 WLAN_CIPHER_SUITE_TKIP,
442 WLAN_CIPHER_SUITE_CCMP,
443 WLAN_CIPHER_SUITE_CCMP_256,
444 WLAN_CIPHER_SUITE_GCMP,
445 WLAN_CIPHER_SUITE_GCMP_256,
446 WLAN_CIPHER_SUITE_AES_CMAC,
447 WLAN_CIPHER_SUITE_BIP_CMAC_256,
448 WLAN_CIPHER_SUITE_BIP_GMAC_128,
449 WLAN_CIPHER_SUITE_BIP_GMAC_256,
452 #define OUI_QCA 0x001374
453 #define QCA_NL80211_SUBCMD_TEST 1
454 enum qca_nl80211_vendor_subcmds {
455 QCA_WLAN_VENDOR_ATTR_TEST = 8,
456 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
459 static const struct nla_policy
460 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
461 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
464 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
465 struct wireless_dev *wdev,
466 const void *data, int data_len)
469 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
473 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
474 data_len, hwsim_vendor_test_policy, NULL);
477 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
479 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
480 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
482 /* Send a vendor event as a test. Note that this would not normally be
483 * done within a command handler, but rather, based on some other
484 * trigger. For simplicity, this command is used to trigger the event
487 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
489 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
491 /* skb_put() or nla_put() will fill up data within
492 * NL80211_ATTR_VENDOR_DATA.
495 /* Add vendor data */
496 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
498 /* Send the event - this will call nla_nest_end() */
499 cfg80211_vendor_event(skb, GFP_KERNEL);
502 /* Send a response to the command */
503 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
507 /* skb_put() or nla_put() will fill up data within
508 * NL80211_ATTR_VENDOR_DATA
510 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
512 return cfg80211_vendor_cmd_reply(skb);
515 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
517 .info = { .vendor_id = OUI_QCA,
518 .subcmd = QCA_NL80211_SUBCMD_TEST },
519 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
520 .doit = mac80211_hwsim_vendor_cmd_test,
521 .policy = hwsim_vendor_test_policy,
522 .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
526 /* Advertise support vendor specific events */
527 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
528 { .vendor_id = OUI_QCA, .subcmd = 1 },
531 static spinlock_t hwsim_radio_lock;
532 static LIST_HEAD(hwsim_radios);
533 static struct rhashtable hwsim_radios_rht;
534 static int hwsim_radio_idx;
535 static int hwsim_radios_generation = 1;
537 static struct platform_driver mac80211_hwsim_driver = {
539 .name = "mac80211_hwsim",
543 struct mac80211_hwsim_data {
544 struct list_head list;
545 struct rhash_head rht;
546 struct ieee80211_hw *hw;
548 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
549 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
550 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
551 struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
552 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
553 struct ieee80211_iface_combination if_combination;
554 struct ieee80211_iface_limit if_limits[3];
557 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
559 struct mac_address addresses[2];
560 struct ieee80211_chanctx_conf *chanctx;
563 bool destroy_on_close;
566 const struct ieee80211_regdomain *regd;
568 struct ieee80211_channel *tmp_chan;
569 struct ieee80211_channel *roc_chan;
571 struct delayed_work roc_start;
572 struct delayed_work roc_done;
573 struct delayed_work hw_scan;
574 struct cfg80211_scan_request *hw_scan_request;
575 struct ieee80211_vif *hw_scan_vif;
577 u8 scan_addr[ETH_ALEN];
579 struct ieee80211_channel *channel;
580 unsigned long next_start, start, end;
581 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
582 ARRAY_SIZE(hwsim_channels_5ghz)];
584 struct ieee80211_channel *channel;
585 u64 beacon_int /* beacon interval in us */;
586 unsigned int rx_filter;
587 bool started, idle, scanning;
589 struct hrtimer beacon_timer;
591 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
593 bool ps_poll_pending;
594 struct dentry *debugfs;
596 atomic_t pending_cookie;
597 struct sk_buff_head pending; /* packets pending */
599 * Only radios in the same group can communicate together (the
600 * channel has to match too). Each bit represents a group. A
601 * radio can be in more than one group.
605 /* group shared by radios created in the same netns */
607 /* wmediumd portid responsible for netgroup of this radio */
610 /* difference between this hw's clock and the real clock, in usecs */
613 /* absolute beacon transmission time. Used to cover up "tx" delay. */
625 static const struct rhashtable_params hwsim_rht_params = {
627 .automatic_shrinking = true,
629 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
630 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
633 struct hwsim_radiotap_hdr {
634 struct ieee80211_radiotap_header hdr;
642 struct hwsim_radiotap_ack_hdr {
643 struct ieee80211_radiotap_header hdr;
650 /* MAC80211_HWSIM netlink family */
651 static struct genl_family hwsim_genl_family;
653 enum hwsim_multicast_groups {
657 static const struct genl_multicast_group hwsim_mcgrps[] = {
658 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
661 /* MAC80211_HWSIM netlink policy */
663 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
664 [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
665 [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
666 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
667 .len = IEEE80211_MAX_DATA_LEN },
668 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
669 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
670 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
671 [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
672 .len = IEEE80211_TX_MAX_RATES *
673 sizeof(struct hwsim_tx_rate)},
674 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
675 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
676 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
677 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
678 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
679 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
680 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
681 [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
682 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
683 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
684 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
685 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
686 [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
687 [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
688 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
689 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
692 #if IS_REACHABLE(CONFIG_VIRTIO)
694 /* MAC80211_HWSIM virtio queues */
695 static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
696 static bool hwsim_virtio_enabled;
697 static spinlock_t hwsim_virtio_lock;
699 static void hwsim_virtio_rx_work(struct work_struct *work);
700 static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
702 static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
705 struct scatterlist sg[1];
709 spin_lock_irqsave(&hwsim_virtio_lock, flags);
710 if (!hwsim_virtio_enabled) {
715 sg_init_one(sg, skb->head, skb_end_offset(skb));
716 err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
720 virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
721 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
725 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
730 /* cause a linker error if this ends up being needed */
731 extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
732 struct sk_buff *skb);
733 #define hwsim_virtio_enabled false
736 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
738 struct ieee80211_channel *chan);
740 /* sysfs attributes */
741 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
743 struct mac80211_hwsim_data *data = dat;
744 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
746 struct ieee80211_pspoll *pspoll;
751 wiphy_dbg(data->hw->wiphy,
752 "%s: send PS-Poll to %pM for aid %d\n",
753 __func__, vp->bssid, vp->aid);
755 skb = dev_alloc_skb(sizeof(*pspoll));
758 pspoll = skb_put(skb, sizeof(*pspoll));
759 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
760 IEEE80211_STYPE_PSPOLL |
762 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
763 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
764 memcpy(pspoll->ta, mac, ETH_ALEN);
767 mac80211_hwsim_tx_frame(data->hw, skb,
768 rcu_dereference(vif->chanctx_conf)->def.chan);
772 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
773 struct ieee80211_vif *vif, int ps)
775 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
777 struct ieee80211_hdr *hdr;
778 struct ieee80211_tx_info *cb;
783 wiphy_dbg(data->hw->wiphy,
784 "%s: send data::nullfunc to %pM ps=%d\n",
785 __func__, vp->bssid, ps);
787 skb = dev_alloc_skb(sizeof(*hdr));
790 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
791 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
792 IEEE80211_STYPE_NULLFUNC |
793 IEEE80211_FCTL_TODS |
794 (ps ? IEEE80211_FCTL_PM : 0));
795 hdr->duration_id = cpu_to_le16(0);
796 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
797 memcpy(hdr->addr2, mac, ETH_ALEN);
798 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
800 cb = IEEE80211_SKB_CB(skb);
801 cb->control.rates[0].count = 1;
802 cb->control.rates[1].idx = -1;
805 mac80211_hwsim_tx_frame(data->hw, skb,
806 rcu_dereference(vif->chanctx_conf)->def.chan);
811 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
812 struct ieee80211_vif *vif)
814 struct mac80211_hwsim_data *data = dat;
815 hwsim_send_nullfunc(data, mac, vif, 1);
818 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
819 struct ieee80211_vif *vif)
821 struct mac80211_hwsim_data *data = dat;
822 hwsim_send_nullfunc(data, mac, vif, 0);
825 static int hwsim_fops_ps_read(void *dat, u64 *val)
827 struct mac80211_hwsim_data *data = dat;
832 static int hwsim_fops_ps_write(void *dat, u64 val)
834 struct mac80211_hwsim_data *data = dat;
837 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
838 val != PS_MANUAL_POLL)
841 if (val == PS_MANUAL_POLL) {
842 if (data->ps != PS_ENABLED)
845 ieee80211_iterate_active_interfaces_atomic(
846 data->hw, IEEE80211_IFACE_ITER_NORMAL,
847 hwsim_send_ps_poll, data);
855 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
856 ieee80211_iterate_active_interfaces_atomic(
857 data->hw, IEEE80211_IFACE_ITER_NORMAL,
858 hwsim_send_nullfunc_ps, data);
859 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
860 ieee80211_iterate_active_interfaces_atomic(
861 data->hw, IEEE80211_IFACE_ITER_NORMAL,
862 hwsim_send_nullfunc_no_ps, data);
869 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
872 static int hwsim_write_simulate_radar(void *dat, u64 val)
874 struct mac80211_hwsim_data *data = dat;
876 ieee80211_radar_detected(data->hw);
881 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
882 hwsim_write_simulate_radar, "%llu\n");
884 static int hwsim_fops_group_read(void *dat, u64 *val)
886 struct mac80211_hwsim_data *data = dat;
891 static int hwsim_fops_group_write(void *dat, u64 val)
893 struct mac80211_hwsim_data *data = dat;
898 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
899 hwsim_fops_group_read, hwsim_fops_group_write,
902 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
903 struct net_device *dev)
905 /* TODO: allow packet injection */
910 static inline u64 mac80211_hwsim_get_tsf_raw(void)
912 return ktime_to_us(ktime_get_real());
915 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
917 u64 now = mac80211_hwsim_get_tsf_raw();
918 return cpu_to_le64(now + data->tsf_offset);
921 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
922 struct ieee80211_vif *vif)
924 struct mac80211_hwsim_data *data = hw->priv;
925 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
928 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
929 struct ieee80211_vif *vif, u64 tsf)
931 struct mac80211_hwsim_data *data = hw->priv;
932 u64 now = mac80211_hwsim_get_tsf(hw, vif);
933 u32 bcn_int = data->beacon_int;
934 u64 delta = abs(tsf - now);
936 /* adjust after beaconing with new timestamp at old TBTT */
938 data->tsf_offset += delta;
939 data->bcn_delta = do_div(delta, bcn_int);
941 data->tsf_offset -= delta;
942 data->bcn_delta = -(s64)do_div(delta, bcn_int);
946 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
947 struct sk_buff *tx_skb,
948 struct ieee80211_channel *chan)
950 struct mac80211_hwsim_data *data = hw->priv;
952 struct hwsim_radiotap_hdr *hdr;
954 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
955 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
960 bitrate = txrate->bitrate;
962 if (!netif_running(hwsim_mon))
965 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
969 hdr = skb_push(skb, sizeof(*hdr));
970 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
972 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
973 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
974 (1 << IEEE80211_RADIOTAP_RATE) |
975 (1 << IEEE80211_RADIOTAP_TSFT) |
976 (1 << IEEE80211_RADIOTAP_CHANNEL));
977 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
979 hdr->rt_rate = bitrate / 5;
980 hdr->rt_channel = cpu_to_le16(chan->center_freq);
981 flags = IEEE80211_CHAN_2GHZ;
982 if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
983 flags |= IEEE80211_CHAN_OFDM;
985 flags |= IEEE80211_CHAN_CCK;
986 hdr->rt_chbitmask = cpu_to_le16(flags);
988 skb->dev = hwsim_mon;
989 skb_reset_mac_header(skb);
990 skb->ip_summed = CHECKSUM_UNNECESSARY;
991 skb->pkt_type = PACKET_OTHERHOST;
992 skb->protocol = htons(ETH_P_802_2);
993 memset(skb->cb, 0, sizeof(skb->cb));
998 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
1001 struct sk_buff *skb;
1002 struct hwsim_radiotap_ack_hdr *hdr;
1004 struct ieee80211_hdr *hdr11;
1006 if (!netif_running(hwsim_mon))
1009 skb = dev_alloc_skb(100);
1013 hdr = skb_put(skb, sizeof(*hdr));
1014 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1015 hdr->hdr.it_pad = 0;
1016 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1017 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1018 (1 << IEEE80211_RADIOTAP_CHANNEL));
1021 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1022 flags = IEEE80211_CHAN_2GHZ;
1023 hdr->rt_chbitmask = cpu_to_le16(flags);
1025 hdr11 = skb_put(skb, 10);
1026 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1027 IEEE80211_STYPE_ACK);
1028 hdr11->duration_id = cpu_to_le16(0);
1029 memcpy(hdr11->addr1, addr, ETH_ALEN);
1031 skb->dev = hwsim_mon;
1032 skb_reset_mac_header(skb);
1033 skb->ip_summed = CHECKSUM_UNNECESSARY;
1034 skb->pkt_type = PACKET_OTHERHOST;
1035 skb->protocol = htons(ETH_P_802_2);
1036 memset(skb->cb, 0, sizeof(skb->cb));
1040 struct mac80211_hwsim_addr_match_data {
1045 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1046 struct ieee80211_vif *vif)
1048 struct mac80211_hwsim_addr_match_data *md = data;
1050 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
1054 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1057 struct mac80211_hwsim_addr_match_data md = {
1061 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1064 memcpy(md.addr, addr, ETH_ALEN);
1066 ieee80211_iterate_active_interfaces_atomic(data->hw,
1067 IEEE80211_IFACE_ITER_NORMAL,
1068 mac80211_hwsim_addr_iter,
1074 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1075 struct sk_buff *skb)
1083 /* TODO: accept (some) Beacons by default and other frames only
1084 * if pending PS-Poll has been sent */
1086 case PS_MANUAL_POLL:
1087 /* Allow unicast frames to own address if there is a pending
1089 if (data->ps_poll_pending &&
1090 mac80211_hwsim_addr_match(data, skb->data + 4)) {
1091 data->ps_poll_pending = false;
1100 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1101 struct sk_buff *skb, int portid)
1108 for_each_net_rcu(net) {
1109 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1110 res = genlmsg_unicast(net, skb, portid);
1123 static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1124 const u8 *addr, bool add)
1126 struct mac80211_hwsim_data *data = hw->priv;
1127 u32 _portid = READ_ONCE(data->wmediumd);
1128 struct sk_buff *skb;
1131 if (!_portid && !hwsim_virtio_enabled)
1134 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1138 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1139 add ? HWSIM_CMD_ADD_MAC_ADDR :
1140 HWSIM_CMD_DEL_MAC_ADDR);
1142 pr_debug("mac80211_hwsim: problem with msg_head\n");
1143 goto nla_put_failure;
1146 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1147 ETH_ALEN, data->addresses[1].addr))
1148 goto nla_put_failure;
1150 if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1151 goto nla_put_failure;
1153 genlmsg_end(skb, msg_head);
1155 if (hwsim_virtio_enabled)
1156 hwsim_tx_virtio(data, skb);
1158 hwsim_unicast_netgroup(data, skb, _portid);
1164 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1168 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1169 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1170 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1171 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1172 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1173 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1174 if (rate->flags & IEEE80211_TX_RC_MCS)
1175 result |= MAC80211_HWSIM_TX_RC_MCS;
1176 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1177 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1178 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1179 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1180 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1181 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1182 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1183 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1184 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1185 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1186 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1187 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1188 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1189 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1194 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1195 struct sk_buff *my_skb,
1197 struct ieee80211_channel *channel)
1199 struct sk_buff *skb;
1200 struct mac80211_hwsim_data *data = hw->priv;
1201 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1202 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1204 unsigned int hwsim_flags = 0;
1206 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1207 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1210 if (data->ps != PS_DISABLED)
1211 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1212 /* If the queue contains MAX_QUEUE skb's drop some */
1213 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1214 /* Droping until WARN_QUEUE level */
1215 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1216 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1221 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1223 goto nla_put_failure;
1225 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1227 if (msg_head == NULL) {
1228 pr_debug("mac80211_hwsim: problem with msg_head\n");
1229 goto nla_put_failure;
1232 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1233 ETH_ALEN, data->addresses[1].addr))
1234 goto nla_put_failure;
1236 /* We get the skb->data */
1237 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1238 goto nla_put_failure;
1240 /* We get the flags for this transmission, and we translate them to
1243 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1244 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1246 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1247 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1249 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1250 goto nla_put_failure;
1252 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1253 goto nla_put_failure;
1255 /* We get the tx control (rate and retries) info*/
1257 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1258 tx_attempts[i].idx = info->status.rates[i].idx;
1259 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1260 tx_attempts[i].count = info->status.rates[i].count;
1261 tx_attempts_flags[i].flags =
1262 trans_tx_rate_flags_ieee2hwsim(
1263 &info->status.rates[i]);
1266 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1267 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1269 goto nla_put_failure;
1271 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1272 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1274 goto nla_put_failure;
1276 /* We create a cookie to identify this skb */
1277 cookie = atomic_inc_return(&data->pending_cookie);
1278 info->rate_driver_data[0] = (void *)cookie;
1279 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1280 goto nla_put_failure;
1282 genlmsg_end(skb, msg_head);
1284 if (hwsim_virtio_enabled) {
1285 if (hwsim_tx_virtio(data, skb))
1286 goto err_free_txskb;
1288 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1289 goto err_free_txskb;
1292 /* Enqueue the packet */
1293 skb_queue_tail(&data->pending, my_skb);
1295 data->tx_bytes += my_skb->len;
1301 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1302 ieee80211_free_txskb(hw, my_skb);
1306 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1307 struct ieee80211_channel *c2)
1312 return c1->center_freq == c2->center_freq;
1315 struct tx_iter_data {
1316 struct ieee80211_channel *channel;
1320 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1321 struct ieee80211_vif *vif)
1323 struct tx_iter_data *data = _data;
1325 if (!vif->chanctx_conf)
1328 if (!hwsim_chans_compat(data->channel,
1329 rcu_dereference(vif->chanctx_conf)->def.chan))
1332 data->receive = true;
1335 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1338 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1340 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1341 * (but you should use a valid OUI, not that)
1343 * If anyone wants to 'donate' a radiotap OUI/subns code
1344 * please send a patch removing this #ifdef and changing
1345 * the values accordingly.
1347 #ifdef HWSIM_RADIOTAP_OUI
1348 struct ieee80211_vendor_radiotap *rtap;
1351 * Note that this code requires the headroom in the SKB
1352 * that was allocated earlier.
1354 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1355 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1356 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1357 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1361 * Radiotap vendor namespaces can (and should) also be
1362 * split into fields by using the standard radiotap
1363 * presence bitmap mechanism. Use just BIT(0) here for
1364 * the presence bitmap.
1366 rtap->present = BIT(0);
1367 /* We have 8 bytes of (dummy) data */
1369 /* For testing, also require it to be aligned */
1371 /* And also test that padding works, 4 bytes */
1374 memcpy(rtap->data, "ABCDEFGH", 8);
1375 /* make sure to clear padding, mac80211 doesn't */
1376 memset(rtap->data + 8, 0, 4);
1378 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1382 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1383 struct sk_buff *skb,
1384 struct ieee80211_channel *chan)
1386 struct mac80211_hwsim_data *data = hw->priv, *data2;
1388 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1389 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1390 struct ieee80211_rx_status rx_status;
1393 memset(&rx_status, 0, sizeof(rx_status));
1394 rx_status.flag |= RX_FLAG_MACTIME_START;
1395 rx_status.freq = chan->center_freq;
1396 rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1397 rx_status.band = chan->band;
1398 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1399 rx_status.rate_idx =
1400 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1402 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1403 rx_status.encoding = RX_ENC_VHT;
1405 rx_status.rate_idx = info->control.rates[0].idx;
1406 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1407 rx_status.encoding = RX_ENC_HT;
1409 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1410 rx_status.bw = RATE_INFO_BW_40;
1411 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1412 rx_status.bw = RATE_INFO_BW_80;
1413 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1414 rx_status.bw = RATE_INFO_BW_160;
1416 rx_status.bw = RATE_INFO_BW_20;
1417 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1418 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1419 /* TODO: simulate real signal strength (and optional packet loss) */
1420 rx_status.signal = -50;
1421 if (info->control.vif)
1422 rx_status.signal += info->control.vif->bss_conf.txpower;
1424 if (data->ps != PS_DISABLED)
1425 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1427 /* release the skb's source info */
1435 * Get absolute mactime here so all HWs RX at the "same time", and
1436 * absolute TX time for beacon mactime so the timestamp matches.
1437 * Giving beacons a different mactime than non-beacons looks messy, but
1438 * it helps the Toffset be exact and a ~10us mactime discrepancy
1439 * probably doesn't really matter.
1441 if (ieee80211_is_beacon(hdr->frame_control) ||
1442 ieee80211_is_probe_resp(hdr->frame_control)) {
1443 rx_status.boottime_ns = ktime_get_boottime_ns();
1444 now = data->abs_bcn_ts;
1446 now = mac80211_hwsim_get_tsf_raw();
1449 /* Copy skb to all enabled radios that are on the current frequency */
1450 spin_lock(&hwsim_radio_lock);
1451 list_for_each_entry(data2, &hwsim_radios, list) {
1452 struct sk_buff *nskb;
1453 struct tx_iter_data tx_iter_data = {
1461 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1462 !hwsim_ps_rx_ok(data2, skb))
1465 if (!(data->group & data2->group))
1468 if (data->netgroup != data2->netgroup)
1471 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1472 !hwsim_chans_compat(chan, data2->channel)) {
1473 ieee80211_iterate_active_interfaces_atomic(
1474 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1475 mac80211_hwsim_tx_iter, &tx_iter_data);
1476 if (!tx_iter_data.receive)
1481 * reserve some space for our vendor and the normal
1482 * radiotap header, since we're copying anyway
1484 if (skb->len < PAGE_SIZE && paged_rx) {
1485 struct page *page = alloc_page(GFP_ATOMIC);
1490 nskb = dev_alloc_skb(128);
1496 memcpy(page_address(page), skb->data, skb->len);
1497 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1499 nskb = skb_copy(skb, GFP_ATOMIC);
1504 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1507 rx_status.mactime = now + data2->tsf_offset;
1509 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1511 mac80211_hwsim_add_vendor_rtap(nskb);
1514 data2->rx_bytes += nskb->len;
1515 ieee80211_rx_irqsafe(data2->hw, nskb);
1517 spin_unlock(&hwsim_radio_lock);
1522 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1523 struct ieee80211_tx_control *control,
1524 struct sk_buff *skb)
1526 struct mac80211_hwsim_data *data = hw->priv;
1527 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1528 struct ieee80211_hdr *hdr = (void *)skb->data;
1529 struct ieee80211_chanctx_conf *chanctx_conf;
1530 struct ieee80211_channel *channel;
1534 if (WARN_ON(skb->len < 10)) {
1535 /* Should not happen; just a sanity check for addr1 use */
1536 ieee80211_free_txskb(hw, skb);
1540 if (!data->use_chanctx) {
1541 channel = data->channel;
1542 } else if (txi->hw_queue == 4) {
1543 channel = data->tmp_chan;
1545 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1547 channel = chanctx_conf->def.chan;
1552 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1553 ieee80211_free_txskb(hw, skb);
1557 if (data->idle && !data->tmp_chan) {
1558 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1559 ieee80211_free_txskb(hw, skb);
1563 if (txi->control.vif)
1564 hwsim_check_magic(txi->control.vif);
1566 hwsim_check_sta_magic(control->sta);
1568 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1569 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1571 ARRAY_SIZE(txi->control.rates));
1573 if (skb->len >= 24 + 8 &&
1574 ieee80211_is_probe_resp(hdr->frame_control)) {
1575 /* fake header transmission time */
1576 struct ieee80211_mgmt *mgmt;
1577 struct ieee80211_rate *txrate;
1582 mgmt = (struct ieee80211_mgmt *)skb->data;
1583 txrate = ieee80211_get_tx_rate(hw, txi);
1585 bitrate = txrate->bitrate;
1586 ts = mac80211_hwsim_get_tsf_raw();
1587 mgmt->u.probe_resp.timestamp =
1588 cpu_to_le64(ts + data->tsf_offset +
1589 24 * 8 * 10 / bitrate);
1592 mac80211_hwsim_monitor_rx(hw, skb, channel);
1594 /* wmediumd mode check */
1595 _portid = READ_ONCE(data->wmediumd);
1597 if (_portid || hwsim_virtio_enabled)
1598 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1600 /* NO wmediumd detected, perfect medium simulation */
1602 data->tx_bytes += skb->len;
1603 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1605 if (ack && skb->len >= 16)
1606 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1608 ieee80211_tx_info_clear_status(txi);
1610 /* frame was transmitted at most favorable rate at first attempt */
1611 txi->control.rates[0].count = 1;
1612 txi->control.rates[1].idx = -1;
1614 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1615 txi->flags |= IEEE80211_TX_STAT_ACK;
1616 ieee80211_tx_status_irqsafe(hw, skb);
1620 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1622 struct mac80211_hwsim_data *data = hw->priv;
1623 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1624 data->started = true;
1629 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1631 struct mac80211_hwsim_data *data = hw->priv;
1633 data->started = false;
1634 hrtimer_cancel(&data->beacon_timer);
1636 while (!skb_queue_empty(&data->pending))
1637 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1639 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1643 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1644 struct ieee80211_vif *vif)
1646 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1647 __func__, ieee80211_vif_type_p2p(vif),
1649 hwsim_set_magic(vif);
1651 if (vif->type != NL80211_IFTYPE_MONITOR)
1652 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1655 vif->hw_queue[IEEE80211_AC_VO] = 0;
1656 vif->hw_queue[IEEE80211_AC_VI] = 1;
1657 vif->hw_queue[IEEE80211_AC_BE] = 2;
1658 vif->hw_queue[IEEE80211_AC_BK] = 3;
1664 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1665 struct ieee80211_vif *vif,
1666 enum nl80211_iftype newtype,
1669 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1670 wiphy_dbg(hw->wiphy,
1671 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1672 __func__, ieee80211_vif_type_p2p(vif),
1673 newtype, vif->addr);
1674 hwsim_check_magic(vif);
1677 * interface may change from non-AP to AP in
1678 * which case this needs to be set up again
1685 static void mac80211_hwsim_remove_interface(
1686 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1688 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1689 __func__, ieee80211_vif_type_p2p(vif),
1691 hwsim_check_magic(vif);
1692 hwsim_clear_magic(vif);
1693 if (vif->type != NL80211_IFTYPE_MONITOR)
1694 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1697 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1698 struct sk_buff *skb,
1699 struct ieee80211_channel *chan)
1701 struct mac80211_hwsim_data *data = hw->priv;
1702 u32 _pid = READ_ONCE(data->wmediumd);
1704 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1705 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1706 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1708 ARRAY_SIZE(txi->control.rates));
1711 mac80211_hwsim_monitor_rx(hw, skb, chan);
1713 if (_pid || hwsim_virtio_enabled)
1714 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1716 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1720 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1721 struct ieee80211_vif *vif)
1723 struct mac80211_hwsim_data *data = arg;
1724 struct ieee80211_hw *hw = data->hw;
1725 struct ieee80211_tx_info *info;
1726 struct ieee80211_rate *txrate;
1727 struct ieee80211_mgmt *mgmt;
1728 struct sk_buff *skb;
1732 hwsim_check_magic(vif);
1734 if (vif->type != NL80211_IFTYPE_AP &&
1735 vif->type != NL80211_IFTYPE_MESH_POINT &&
1736 vif->type != NL80211_IFTYPE_ADHOC &&
1737 vif->type != NL80211_IFTYPE_OCB)
1740 skb = ieee80211_beacon_get(hw, vif);
1743 info = IEEE80211_SKB_CB(skb);
1744 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1745 ieee80211_get_tx_rates(vif, NULL, skb,
1746 info->control.rates,
1747 ARRAY_SIZE(info->control.rates));
1749 txrate = ieee80211_get_tx_rate(hw, info);
1751 bitrate = txrate->bitrate;
1753 mgmt = (struct ieee80211_mgmt *) skb->data;
1754 /* fake header transmission time */
1755 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1756 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1757 struct ieee80211_ext *ext = (void *) mgmt;
1759 ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1764 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1770 mac80211_hwsim_tx_frame(hw, skb,
1771 rcu_dereference(vif->chanctx_conf)->def.chan);
1773 while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1774 mac80211_hwsim_tx_frame(hw, skb,
1775 rcu_dereference(vif->chanctx_conf)->def.chan);
1778 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1779 ieee80211_csa_finish(vif);
1782 static enum hrtimer_restart
1783 mac80211_hwsim_beacon(struct hrtimer *timer)
1785 struct mac80211_hwsim_data *data =
1786 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1787 struct ieee80211_hw *hw = data->hw;
1788 u64 bcn_int = data->beacon_int;
1791 return HRTIMER_NORESTART;
1793 ieee80211_iterate_active_interfaces_atomic(
1794 hw, IEEE80211_IFACE_ITER_NORMAL,
1795 mac80211_hwsim_beacon_tx, data);
1797 /* beacon at new TBTT + beacon interval */
1798 if (data->bcn_delta) {
1799 bcn_int -= data->bcn_delta;
1800 data->bcn_delta = 0;
1802 hrtimer_forward_now(&data->beacon_timer,
1803 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1804 return HRTIMER_RESTART;
1807 static const char * const hwsim_chanwidths[] = {
1808 [NL80211_CHAN_WIDTH_5] = "ht5",
1809 [NL80211_CHAN_WIDTH_10] = "ht10",
1810 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1811 [NL80211_CHAN_WIDTH_20] = "ht20",
1812 [NL80211_CHAN_WIDTH_40] = "ht40",
1813 [NL80211_CHAN_WIDTH_80] = "vht80",
1814 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1815 [NL80211_CHAN_WIDTH_160] = "vht160",
1816 [NL80211_CHAN_WIDTH_1] = "1MHz",
1817 [NL80211_CHAN_WIDTH_2] = "2MHz",
1818 [NL80211_CHAN_WIDTH_4] = "4MHz",
1819 [NL80211_CHAN_WIDTH_8] = "8MHz",
1820 [NL80211_CHAN_WIDTH_16] = "16MHz",
1823 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1825 struct mac80211_hwsim_data *data = hw->priv;
1826 struct ieee80211_conf *conf = &hw->conf;
1827 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1828 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1829 [IEEE80211_SMPS_OFF] = "off",
1830 [IEEE80211_SMPS_STATIC] = "static",
1831 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1835 if (conf->chandef.chan)
1836 wiphy_dbg(hw->wiphy,
1837 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1839 conf->chandef.chan->center_freq,
1840 conf->chandef.center_freq1,
1841 conf->chandef.center_freq2,
1842 hwsim_chanwidths[conf->chandef.width],
1843 !!(conf->flags & IEEE80211_CONF_IDLE),
1844 !!(conf->flags & IEEE80211_CONF_PS),
1845 smps_modes[conf->smps_mode]);
1847 wiphy_dbg(hw->wiphy,
1848 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1850 !!(conf->flags & IEEE80211_CONF_IDLE),
1851 !!(conf->flags & IEEE80211_CONF_PS),
1852 smps_modes[conf->smps_mode]);
1854 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1856 WARN_ON(conf->chandef.chan && data->use_chanctx);
1858 mutex_lock(&data->mutex);
1859 if (data->scanning && conf->chandef.chan) {
1860 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1861 if (data->survey_data[idx].channel == data->channel) {
1862 data->survey_data[idx].start =
1863 data->survey_data[idx].next_start;
1864 data->survey_data[idx].end = jiffies;
1869 data->channel = conf->chandef.chan;
1871 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1872 if (data->survey_data[idx].channel &&
1873 data->survey_data[idx].channel != data->channel)
1875 data->survey_data[idx].channel = data->channel;
1876 data->survey_data[idx].next_start = jiffies;
1880 data->channel = conf->chandef.chan;
1882 mutex_unlock(&data->mutex);
1884 if (!data->started || !data->beacon_int)
1885 hrtimer_cancel(&data->beacon_timer);
1886 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1887 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1888 u32 bcn_int = data->beacon_int;
1889 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1891 hrtimer_start(&data->beacon_timer,
1892 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1893 HRTIMER_MODE_REL_SOFT);
1900 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1901 unsigned int changed_flags,
1902 unsigned int *total_flags,u64 multicast)
1904 struct mac80211_hwsim_data *data = hw->priv;
1906 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1908 data->rx_filter = 0;
1909 if (*total_flags & FIF_ALLMULTI)
1910 data->rx_filter |= FIF_ALLMULTI;
1911 if (*total_flags & FIF_MCAST_ACTION)
1912 data->rx_filter |= FIF_MCAST_ACTION;
1914 *total_flags = data->rx_filter;
1917 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1918 struct ieee80211_vif *vif)
1920 unsigned int *count = data;
1921 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1927 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1928 struct ieee80211_vif *vif,
1929 struct ieee80211_bss_conf *info,
1932 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1933 struct mac80211_hwsim_data *data = hw->priv;
1935 hwsim_check_magic(vif);
1937 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1938 __func__, changed, vif->addr);
1940 if (changed & BSS_CHANGED_BSSID) {
1941 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1942 __func__, info->bssid);
1943 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1946 if (changed & BSS_CHANGED_ASSOC) {
1947 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1948 info->assoc, info->aid);
1949 vp->assoc = info->assoc;
1950 vp->aid = info->aid;
1953 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1954 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1955 info->enable_beacon, info->beacon_int);
1956 vp->bcn_en = info->enable_beacon;
1957 if (data->started &&
1958 !hrtimer_is_queued(&data->beacon_timer) &&
1959 info->enable_beacon) {
1960 u64 tsf, until_tbtt;
1962 data->beacon_int = info->beacon_int * 1024;
1963 tsf = mac80211_hwsim_get_tsf(hw, vif);
1964 bcn_int = data->beacon_int;
1965 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1967 hrtimer_start(&data->beacon_timer,
1968 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1969 HRTIMER_MODE_REL_SOFT);
1970 } else if (!info->enable_beacon) {
1971 unsigned int count = 0;
1972 ieee80211_iterate_active_interfaces_atomic(
1973 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1974 mac80211_hwsim_bcn_en_iter, &count);
1975 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1978 hrtimer_cancel(&data->beacon_timer);
1979 data->beacon_int = 0;
1984 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1985 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1986 info->use_cts_prot);
1989 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1990 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1991 info->use_short_preamble);
1994 if (changed & BSS_CHANGED_ERP_SLOT) {
1995 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1998 if (changed & BSS_CHANGED_HT) {
1999 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
2000 info->ht_operation_mode);
2003 if (changed & BSS_CHANGED_BASIC_RATES) {
2004 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
2005 (unsigned long long) info->basic_rates);
2008 if (changed & BSS_CHANGED_TXPOWER)
2009 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
2012 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2013 struct ieee80211_vif *vif,
2014 struct ieee80211_sta *sta)
2016 hwsim_check_magic(vif);
2017 hwsim_set_sta_magic(sta);
2022 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2023 struct ieee80211_vif *vif,
2024 struct ieee80211_sta *sta)
2026 hwsim_check_magic(vif);
2027 hwsim_clear_sta_magic(sta);
2032 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2033 struct ieee80211_vif *vif,
2034 enum sta_notify_cmd cmd,
2035 struct ieee80211_sta *sta)
2037 hwsim_check_magic(vif);
2040 case STA_NOTIFY_SLEEP:
2041 case STA_NOTIFY_AWAKE:
2042 /* TODO: make good use of these flags */
2045 WARN(1, "Invalid sta notify: %d\n", cmd);
2050 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2051 struct ieee80211_sta *sta,
2054 hwsim_check_sta_magic(sta);
2058 static int mac80211_hwsim_conf_tx(
2059 struct ieee80211_hw *hw,
2060 struct ieee80211_vif *vif, u16 queue,
2061 const struct ieee80211_tx_queue_params *params)
2063 wiphy_dbg(hw->wiphy,
2064 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2066 params->txop, params->cw_min,
2067 params->cw_max, params->aifs);
2071 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2072 struct survey_info *survey)
2074 struct mac80211_hwsim_data *hwsim = hw->priv;
2076 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2079 mutex_lock(&hwsim->mutex);
2080 survey->channel = hwsim->survey_data[idx].channel;
2081 if (!survey->channel) {
2082 mutex_unlock(&hwsim->mutex);
2087 * Magically conjured dummy values --- this is only ok for simulated hardware.
2089 * A real driver which cannot determine real values noise MUST NOT
2090 * report any, especially not a magically conjured ones :-)
2092 survey->filled = SURVEY_INFO_NOISE_DBM |
2094 SURVEY_INFO_TIME_BUSY;
2095 survey->noise = -92;
2097 jiffies_to_msecs(hwsim->survey_data[idx].end -
2098 hwsim->survey_data[idx].start);
2099 /* report 12.5% of channel time is used */
2100 survey->time_busy = survey->time/8;
2101 mutex_unlock(&hwsim->mutex);
2106 #ifdef CONFIG_NL80211_TESTMODE
2108 * This section contains example code for using netlink
2109 * attributes with the testmode command in nl80211.
2112 /* These enums need to be kept in sync with userspace */
2113 enum hwsim_testmode_attr {
2114 __HWSIM_TM_ATTR_INVALID = 0,
2115 HWSIM_TM_ATTR_CMD = 1,
2116 HWSIM_TM_ATTR_PS = 2,
2119 __HWSIM_TM_ATTR_AFTER_LAST,
2120 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
2123 enum hwsim_testmode_cmd {
2124 HWSIM_TM_CMD_SET_PS = 0,
2125 HWSIM_TM_CMD_GET_PS = 1,
2126 HWSIM_TM_CMD_STOP_QUEUES = 2,
2127 HWSIM_TM_CMD_WAKE_QUEUES = 3,
2130 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2131 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2132 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2135 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2136 struct ieee80211_vif *vif,
2137 void *data, int len)
2139 struct mac80211_hwsim_data *hwsim = hw->priv;
2140 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2141 struct sk_buff *skb;
2144 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2145 hwsim_testmode_policy, NULL);
2149 if (!tb[HWSIM_TM_ATTR_CMD])
2152 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2153 case HWSIM_TM_CMD_SET_PS:
2154 if (!tb[HWSIM_TM_ATTR_PS])
2156 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2157 return hwsim_fops_ps_write(hwsim, ps);
2158 case HWSIM_TM_CMD_GET_PS:
2159 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2160 nla_total_size(sizeof(u32)));
2163 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2164 goto nla_put_failure;
2165 return cfg80211_testmode_reply(skb);
2166 case HWSIM_TM_CMD_STOP_QUEUES:
2167 ieee80211_stop_queues(hw);
2169 case HWSIM_TM_CMD_WAKE_QUEUES:
2170 ieee80211_wake_queues(hw);
2182 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2183 struct ieee80211_vif *vif,
2184 struct ieee80211_ampdu_params *params)
2186 struct ieee80211_sta *sta = params->sta;
2187 enum ieee80211_ampdu_mlme_action action = params->action;
2188 u16 tid = params->tid;
2191 case IEEE80211_AMPDU_TX_START:
2192 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2193 case IEEE80211_AMPDU_TX_STOP_CONT:
2194 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2195 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2196 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2198 case IEEE80211_AMPDU_TX_OPERATIONAL:
2200 case IEEE80211_AMPDU_RX_START:
2201 case IEEE80211_AMPDU_RX_STOP:
2210 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2211 struct ieee80211_vif *vif,
2212 u32 queues, bool drop)
2214 /* Not implemented, queues only on kernel side */
2217 static void hw_scan_work(struct work_struct *work)
2219 struct mac80211_hwsim_data *hwsim =
2220 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2221 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2224 mutex_lock(&hwsim->mutex);
2225 if (hwsim->scan_chan_idx >= req->n_channels) {
2226 struct cfg80211_scan_info info = {
2230 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2231 ieee80211_scan_completed(hwsim->hw, &info);
2232 hwsim->hw_scan_request = NULL;
2233 hwsim->hw_scan_vif = NULL;
2234 hwsim->tmp_chan = NULL;
2235 mutex_unlock(&hwsim->mutex);
2236 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2241 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2242 req->channels[hwsim->scan_chan_idx]->center_freq);
2244 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2245 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2246 IEEE80211_CHAN_RADAR) ||
2252 for (i = 0; i < req->n_ssids; i++) {
2253 struct sk_buff *probe;
2254 struct ieee80211_mgmt *mgmt;
2256 probe = ieee80211_probereq_get(hwsim->hw,
2259 req->ssids[i].ssid_len,
2264 mgmt = (struct ieee80211_mgmt *) probe->data;
2265 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2266 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2269 skb_put_data(probe, req->ie, req->ie_len);
2272 if (!ieee80211_tx_prepare_skb(hwsim->hw,
2275 hwsim->tmp_chan->band,
2283 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2289 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2290 msecs_to_jiffies(dwell));
2291 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2292 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2293 hwsim->survey_data[hwsim->scan_chan_idx].end =
2294 jiffies + msecs_to_jiffies(dwell);
2295 hwsim->scan_chan_idx++;
2296 mutex_unlock(&hwsim->mutex);
2299 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2300 struct ieee80211_vif *vif,
2301 struct ieee80211_scan_request *hw_req)
2303 struct mac80211_hwsim_data *hwsim = hw->priv;
2304 struct cfg80211_scan_request *req = &hw_req->req;
2306 mutex_lock(&hwsim->mutex);
2307 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2308 mutex_unlock(&hwsim->mutex);
2311 hwsim->hw_scan_request = req;
2312 hwsim->hw_scan_vif = vif;
2313 hwsim->scan_chan_idx = 0;
2314 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2315 get_random_mask_addr(hwsim->scan_addr,
2316 hw_req->req.mac_addr,
2317 hw_req->req.mac_addr_mask);
2319 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2320 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2321 mutex_unlock(&hwsim->mutex);
2323 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2324 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2326 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2331 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2332 struct ieee80211_vif *vif)
2334 struct mac80211_hwsim_data *hwsim = hw->priv;
2335 struct cfg80211_scan_info info = {
2339 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2341 cancel_delayed_work_sync(&hwsim->hw_scan);
2343 mutex_lock(&hwsim->mutex);
2344 ieee80211_scan_completed(hwsim->hw, &info);
2345 hwsim->tmp_chan = NULL;
2346 hwsim->hw_scan_request = NULL;
2347 hwsim->hw_scan_vif = NULL;
2348 mutex_unlock(&hwsim->mutex);
2351 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2352 struct ieee80211_vif *vif,
2355 struct mac80211_hwsim_data *hwsim = hw->priv;
2357 mutex_lock(&hwsim->mutex);
2359 if (hwsim->scanning) {
2360 pr_debug("two hwsim sw_scans detected!\n");
2364 pr_debug("hwsim sw_scan request, prepping stuff\n");
2366 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2367 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2368 hwsim->scanning = true;
2369 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2372 mutex_unlock(&hwsim->mutex);
2375 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2376 struct ieee80211_vif *vif)
2378 struct mac80211_hwsim_data *hwsim = hw->priv;
2380 mutex_lock(&hwsim->mutex);
2382 pr_debug("hwsim sw_scan_complete\n");
2383 hwsim->scanning = false;
2384 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2385 eth_zero_addr(hwsim->scan_addr);
2387 mutex_unlock(&hwsim->mutex);
2390 static void hw_roc_start(struct work_struct *work)
2392 struct mac80211_hwsim_data *hwsim =
2393 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2395 mutex_lock(&hwsim->mutex);
2397 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2398 hwsim->tmp_chan = hwsim->roc_chan;
2399 ieee80211_ready_on_channel(hwsim->hw);
2401 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2402 msecs_to_jiffies(hwsim->roc_duration));
2404 mutex_unlock(&hwsim->mutex);
2407 static void hw_roc_done(struct work_struct *work)
2409 struct mac80211_hwsim_data *hwsim =
2410 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2412 mutex_lock(&hwsim->mutex);
2413 ieee80211_remain_on_channel_expired(hwsim->hw);
2414 hwsim->tmp_chan = NULL;
2415 mutex_unlock(&hwsim->mutex);
2417 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2420 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2421 struct ieee80211_vif *vif,
2422 struct ieee80211_channel *chan,
2424 enum ieee80211_roc_type type)
2426 struct mac80211_hwsim_data *hwsim = hw->priv;
2428 mutex_lock(&hwsim->mutex);
2429 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2430 mutex_unlock(&hwsim->mutex);
2434 hwsim->roc_chan = chan;
2435 hwsim->roc_duration = duration;
2436 mutex_unlock(&hwsim->mutex);
2438 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2439 chan->center_freq, duration);
2440 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2445 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2446 struct ieee80211_vif *vif)
2448 struct mac80211_hwsim_data *hwsim = hw->priv;
2450 cancel_delayed_work_sync(&hwsim->roc_start);
2451 cancel_delayed_work_sync(&hwsim->roc_done);
2453 mutex_lock(&hwsim->mutex);
2454 hwsim->tmp_chan = NULL;
2455 mutex_unlock(&hwsim->mutex);
2457 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2462 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2463 struct ieee80211_chanctx_conf *ctx)
2465 struct mac80211_hwsim_data *hwsim = hw->priv;
2467 mutex_lock(&hwsim->mutex);
2468 hwsim->chanctx = ctx;
2469 mutex_unlock(&hwsim->mutex);
2470 hwsim_set_chanctx_magic(ctx);
2471 wiphy_dbg(hw->wiphy,
2472 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2473 ctx->def.chan->center_freq, ctx->def.width,
2474 ctx->def.center_freq1, ctx->def.center_freq2);
2478 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2479 struct ieee80211_chanctx_conf *ctx)
2481 struct mac80211_hwsim_data *hwsim = hw->priv;
2483 mutex_lock(&hwsim->mutex);
2484 hwsim->chanctx = NULL;
2485 mutex_unlock(&hwsim->mutex);
2486 wiphy_dbg(hw->wiphy,
2487 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2488 ctx->def.chan->center_freq, ctx->def.width,
2489 ctx->def.center_freq1, ctx->def.center_freq2);
2490 hwsim_check_chanctx_magic(ctx);
2491 hwsim_clear_chanctx_magic(ctx);
2494 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2495 struct ieee80211_chanctx_conf *ctx,
2498 struct mac80211_hwsim_data *hwsim = hw->priv;
2500 mutex_lock(&hwsim->mutex);
2501 hwsim->chanctx = ctx;
2502 mutex_unlock(&hwsim->mutex);
2503 hwsim_check_chanctx_magic(ctx);
2504 wiphy_dbg(hw->wiphy,
2505 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2506 ctx->def.chan->center_freq, ctx->def.width,
2507 ctx->def.center_freq1, ctx->def.center_freq2);
2510 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2511 struct ieee80211_vif *vif,
2512 struct ieee80211_chanctx_conf *ctx)
2514 hwsim_check_magic(vif);
2515 hwsim_check_chanctx_magic(ctx);
2520 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2521 struct ieee80211_vif *vif,
2522 struct ieee80211_chanctx_conf *ctx)
2524 hwsim_check_magic(vif);
2525 hwsim_check_chanctx_magic(ctx);
2528 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2539 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2541 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2542 struct ieee80211_vif *vif,
2545 if (sset == ETH_SS_STATS)
2546 memcpy(data, mac80211_hwsim_gstrings_stats,
2547 sizeof(mac80211_hwsim_gstrings_stats));
2550 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2551 struct ieee80211_vif *vif, int sset)
2553 if (sset == ETH_SS_STATS)
2554 return MAC80211_HWSIM_SSTATS_LEN;
2558 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2559 struct ieee80211_vif *vif,
2560 struct ethtool_stats *stats, u64 *data)
2562 struct mac80211_hwsim_data *ar = hw->priv;
2565 data[i++] = ar->tx_pkts;
2566 data[i++] = ar->tx_bytes;
2567 data[i++] = ar->rx_pkts;
2568 data[i++] = ar->rx_bytes;
2569 data[i++] = ar->tx_dropped;
2570 data[i++] = ar->tx_failed;
2572 data[i++] = ar->group;
2574 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2577 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2582 #define HWSIM_COMMON_OPS \
2583 .tx = mac80211_hwsim_tx, \
2584 .start = mac80211_hwsim_start, \
2585 .stop = mac80211_hwsim_stop, \
2586 .add_interface = mac80211_hwsim_add_interface, \
2587 .change_interface = mac80211_hwsim_change_interface, \
2588 .remove_interface = mac80211_hwsim_remove_interface, \
2589 .config = mac80211_hwsim_config, \
2590 .configure_filter = mac80211_hwsim_configure_filter, \
2591 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2592 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2593 .sta_add = mac80211_hwsim_sta_add, \
2594 .sta_remove = mac80211_hwsim_sta_remove, \
2595 .sta_notify = mac80211_hwsim_sta_notify, \
2596 .set_tim = mac80211_hwsim_set_tim, \
2597 .conf_tx = mac80211_hwsim_conf_tx, \
2598 .get_survey = mac80211_hwsim_get_survey, \
2599 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2600 .ampdu_action = mac80211_hwsim_ampdu_action, \
2601 .flush = mac80211_hwsim_flush, \
2602 .get_tsf = mac80211_hwsim_get_tsf, \
2603 .set_tsf = mac80211_hwsim_set_tsf, \
2604 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2605 .get_et_stats = mac80211_hwsim_get_et_stats, \
2606 .get_et_strings = mac80211_hwsim_get_et_strings,
2608 static const struct ieee80211_ops mac80211_hwsim_ops = {
2610 .sw_scan_start = mac80211_hwsim_sw_scan,
2611 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2614 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2616 .hw_scan = mac80211_hwsim_hw_scan,
2617 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2618 .sw_scan_start = NULL,
2619 .sw_scan_complete = NULL,
2620 .remain_on_channel = mac80211_hwsim_roc,
2621 .cancel_remain_on_channel = mac80211_hwsim_croc,
2622 .add_chanctx = mac80211_hwsim_add_chanctx,
2623 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2624 .change_chanctx = mac80211_hwsim_change_chanctx,
2625 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2626 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2629 struct hwsim_new_radio_params {
2630 unsigned int channels;
2631 const char *reg_alpha2;
2632 const struct ieee80211_regdomain *regd;
2636 bool destroy_on_close;
2639 const u8 *perm_addr;
2645 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2646 struct genl_info *info)
2649 genl_notify(&hwsim_genl_family, mcast_skb, info,
2650 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2652 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2653 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2656 static int append_radio_msg(struct sk_buff *skb, int id,
2657 struct hwsim_new_radio_params *param)
2661 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2665 if (param->channels) {
2666 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2671 if (param->reg_alpha2) {
2672 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2681 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2682 if (hwsim_world_regdom_custom[i] != param->regd)
2685 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2692 if (param->reg_strict) {
2693 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2698 if (param->p2p_device) {
2699 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2704 if (param->use_chanctx) {
2705 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2710 if (param->hwname) {
2711 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2712 strlen(param->hwname), param->hwname);
2720 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2721 struct hwsim_new_radio_params *param)
2723 struct sk_buff *mcast_skb;
2726 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2730 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2731 HWSIM_CMD_NEW_RADIO);
2735 if (append_radio_msg(mcast_skb, id, param) < 0)
2738 genlmsg_end(mcast_skb, data);
2740 hwsim_mcast_config_msg(mcast_skb, info);
2744 nlmsg_free(mcast_skb);
2747 static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2749 /* TODO: should we support other types, e.g., P2P?*/
2750 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2751 BIT(NL80211_IFTYPE_AP),
2756 IEEE80211_HE_MAC_CAP0_HTC_HE,
2758 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2759 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2761 IEEE80211_HE_MAC_CAP2_BSR |
2762 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2763 IEEE80211_HE_MAC_CAP2_ACK_EN,
2765 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2766 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2767 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2769 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2770 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2771 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2772 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2774 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2775 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2776 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2777 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2778 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2780 /* Leave all the other PHY capability bytes
2781 * unset, as DCM, beam forming, RU and PPE
2782 * threshold information are not supported
2785 .he_mcs_nss_supp = {
2786 .rx_mcs_80 = cpu_to_le16(0xfffa),
2787 .tx_mcs_80 = cpu_to_le16(0xfffa),
2788 .rx_mcs_160 = cpu_to_le16(0xffff),
2789 .tx_mcs_160 = cpu_to_le16(0xffff),
2790 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2791 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2795 #ifdef CONFIG_MAC80211_MESH
2797 /* TODO: should we support other types, e.g., IBSS?*/
2798 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2803 IEEE80211_HE_MAC_CAP0_HTC_HE,
2805 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2807 IEEE80211_HE_MAC_CAP2_ACK_EN,
2809 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2810 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2811 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2813 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2814 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2815 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2816 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2817 .phy_cap_info[2] = 0,
2819 /* Leave all the other PHY capability bytes
2820 * unset, as DCM, beam forming, RU and PPE
2821 * threshold information are not supported
2824 .he_mcs_nss_supp = {
2825 .rx_mcs_80 = cpu_to_le16(0xfffa),
2826 .tx_mcs_80 = cpu_to_le16(0xfffa),
2827 .rx_mcs_160 = cpu_to_le16(0xffff),
2828 .tx_mcs_160 = cpu_to_le16(0xffff),
2829 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2830 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2837 static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2839 /* TODO: should we support other types, e.g., P2P?*/
2840 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2841 BIT(NL80211_IFTYPE_AP),
2846 IEEE80211_HE_MAC_CAP0_HTC_HE,
2848 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2849 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2851 IEEE80211_HE_MAC_CAP2_BSR |
2852 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2853 IEEE80211_HE_MAC_CAP2_ACK_EN,
2855 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2856 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2857 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2859 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2860 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2861 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2863 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2864 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2865 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2866 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2868 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2869 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2870 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2871 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2872 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2874 /* Leave all the other PHY capability bytes
2875 * unset, as DCM, beam forming, RU and PPE
2876 * threshold information are not supported
2879 .he_mcs_nss_supp = {
2880 .rx_mcs_80 = cpu_to_le16(0xfffa),
2881 .tx_mcs_80 = cpu_to_le16(0xfffa),
2882 .rx_mcs_160 = cpu_to_le16(0xfffa),
2883 .tx_mcs_160 = cpu_to_le16(0xfffa),
2884 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2885 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2889 #ifdef CONFIG_MAC80211_MESH
2891 /* TODO: should we support other types, e.g., IBSS?*/
2892 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2897 IEEE80211_HE_MAC_CAP0_HTC_HE,
2899 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2901 IEEE80211_HE_MAC_CAP2_ACK_EN,
2903 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2904 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2905 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2907 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2908 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2909 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2911 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2912 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2913 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2914 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2915 .phy_cap_info[2] = 0,
2917 /* Leave all the other PHY capability bytes
2918 * unset, as DCM, beam forming, RU and PPE
2919 * threshold information are not supported
2922 .he_mcs_nss_supp = {
2923 .rx_mcs_80 = cpu_to_le16(0xfffa),
2924 .tx_mcs_80 = cpu_to_le16(0xfffa),
2925 .rx_mcs_160 = cpu_to_le16(0xfffa),
2926 .tx_mcs_160 = cpu_to_le16(0xfffa),
2927 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2928 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2935 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2939 if (sband->band == NL80211_BAND_2GHZ) {
2940 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2941 sband->iftype_data =
2942 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2943 } else if (sband->band == NL80211_BAND_5GHZ) {
2944 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2945 sband->iftype_data =
2946 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2951 sband->n_iftype_data = n_iftype_data;
2954 #ifdef CONFIG_MAC80211_MESH
2955 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2957 #define HWSIM_MESH_BIT 0
2960 #define HWSIM_DEFAULT_IF_LIMIT \
2961 (BIT(NL80211_IFTYPE_STATION) | \
2962 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2963 BIT(NL80211_IFTYPE_AP) | \
2964 BIT(NL80211_IFTYPE_P2P_GO) | \
2967 #define HWSIM_IFTYPE_SUPPORT_MASK \
2968 (BIT(NL80211_IFTYPE_STATION) | \
2969 BIT(NL80211_IFTYPE_AP) | \
2970 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2971 BIT(NL80211_IFTYPE_P2P_GO) | \
2972 BIT(NL80211_IFTYPE_ADHOC) | \
2973 BIT(NL80211_IFTYPE_MESH_POINT) | \
2974 BIT(NL80211_IFTYPE_OCB))
2976 static int mac80211_hwsim_new_radio(struct genl_info *info,
2977 struct hwsim_new_radio_params *param)
2981 struct mac80211_hwsim_data *data;
2982 struct ieee80211_hw *hw;
2983 enum nl80211_band band;
2984 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2989 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2992 spin_lock_bh(&hwsim_radio_lock);
2993 idx = hwsim_radio_idx++;
2994 spin_unlock_bh(&hwsim_radio_lock);
2996 if (param->use_chanctx)
2997 ops = &mac80211_hwsim_mchan_ops;
2998 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
3000 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3005 /* ieee80211_alloc_hw_nm may have used a default name */
3006 param->hwname = wiphy_name(hw->wiphy);
3009 net = genl_info_net(info);
3012 wiphy_net_set(hw->wiphy, net);
3017 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3018 if (IS_ERR(data->dev)) {
3020 "mac80211_hwsim: device_create failed (%ld)\n",
3021 PTR_ERR(data->dev));
3023 goto failed_drvdata;
3025 data->dev->driver = &mac80211_hwsim_driver.driver;
3026 err = device_bind_driver(data->dev);
3028 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3033 skb_queue_head_init(&data->pending);
3035 SET_IEEE80211_DEV(hw, data->dev);
3036 if (!param->perm_addr) {
3037 eth_zero_addr(addr);
3041 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3042 /* Why need here second address ? */
3043 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3044 data->addresses[1].addr[0] |= 0x40;
3045 hw->wiphy->n_addresses = 2;
3046 hw->wiphy->addresses = data->addresses;
3047 /* possible address clash is checked at hash table insertion */
3049 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3050 /* compatibility with automatically generated mac addr */
3051 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3052 hw->wiphy->n_addresses = 2;
3053 hw->wiphy->addresses = data->addresses;
3056 data->channels = param->channels;
3057 data->use_chanctx = param->use_chanctx;
3059 data->destroy_on_close = param->destroy_on_close;
3061 data->portid = info->snd_portid;
3063 /* setup interface limits, only on interface types we support */
3064 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3065 data->if_limits[n_limits].max = 1;
3066 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3070 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3071 data->if_limits[n_limits].max = 2048;
3073 * For this case, we may only support a subset of
3074 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3075 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3077 data->if_limits[n_limits].types =
3078 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3082 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3083 data->if_limits[n_limits].max = 1;
3084 data->if_limits[n_limits].types =
3085 BIT(NL80211_IFTYPE_P2P_DEVICE);
3089 if (data->use_chanctx) {
3090 hw->wiphy->max_scan_ssids = 255;
3091 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3092 hw->wiphy->max_remain_on_channel_duration = 1000;
3093 data->if_combination.radar_detect_widths = 0;
3094 data->if_combination.num_different_channels = data->channels;
3095 data->chanctx = NULL;
3097 data->if_combination.num_different_channels = 1;
3098 data->if_combination.radar_detect_widths =
3099 BIT(NL80211_CHAN_WIDTH_5) |
3100 BIT(NL80211_CHAN_WIDTH_10) |
3101 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3102 BIT(NL80211_CHAN_WIDTH_20) |
3103 BIT(NL80211_CHAN_WIDTH_40) |
3104 BIT(NL80211_CHAN_WIDTH_80) |
3105 BIT(NL80211_CHAN_WIDTH_160);
3113 data->if_combination.max_interfaces = 0;
3114 for (i = 0; i < n_limits; i++)
3115 data->if_combination.max_interfaces +=
3116 data->if_limits[i].max;
3118 data->if_combination.n_limits = n_limits;
3119 data->if_combination.limits = data->if_limits;
3122 * If we actually were asked to support combinations,
3123 * advertise them - if there's only a single thing like
3124 * only IBSS then don't advertise it as combinations.
3126 if (data->if_combination.max_interfaces > 1) {
3127 hw->wiphy->iface_combinations = &data->if_combination;
3128 hw->wiphy->n_iface_combinations = 1;
3131 if (param->ciphers) {
3132 memcpy(data->ciphers, param->ciphers,
3133 param->n_ciphers * sizeof(u32));
3134 hw->wiphy->cipher_suites = data->ciphers;
3135 hw->wiphy->n_cipher_suites = param->n_ciphers;
3138 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3139 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3140 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3143 hw->offchannel_tx_hw_queue = 4;
3145 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3146 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3147 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3148 ieee80211_hw_set(hw, QUEUE_CONTROL);
3149 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3150 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3151 ieee80211_hw_set(hw, MFP_CAPABLE);
3152 ieee80211_hw_set(hw, SIGNAL_DBM);
3153 ieee80211_hw_set(hw, SUPPORTS_PS);
3154 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3155 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3156 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3157 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3159 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3160 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3162 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3163 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3164 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3165 WIPHY_FLAG_AP_UAPSD |
3166 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3167 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3168 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3169 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3170 NL80211_FEATURE_STATIC_SMPS |
3171 NL80211_FEATURE_DYNAMIC_SMPS |
3172 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3173 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3174 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3175 wiphy_ext_feature_set(hw->wiphy,
3176 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3177 wiphy_ext_feature_set(hw->wiphy,
3178 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3180 hw->wiphy->interface_modes = param->iftypes;
3182 /* ask mac80211 to reserve space for magic */
3183 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3184 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3185 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3187 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3188 sizeof(hwsim_channels_2ghz));
3189 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3190 sizeof(hwsim_channels_5ghz));
3191 memcpy(data->channels_s1g, hwsim_channels_s1g,
3192 sizeof(hwsim_channels_s1g));
3193 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3195 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3196 struct ieee80211_supported_band *sband = &data->bands[band];
3201 case NL80211_BAND_2GHZ:
3202 sband->channels = data->channels_2ghz;
3203 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3204 sband->bitrates = data->rates;
3205 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3207 case NL80211_BAND_5GHZ:
3208 sband->channels = data->channels_5ghz;
3209 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3210 sband->bitrates = data->rates + 4;
3211 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3213 sband->vht_cap.vht_supported = true;
3214 sband->vht_cap.cap =
3215 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3216 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3217 IEEE80211_VHT_CAP_RXLDPC |
3218 IEEE80211_VHT_CAP_SHORT_GI_80 |
3219 IEEE80211_VHT_CAP_SHORT_GI_160 |
3220 IEEE80211_VHT_CAP_TXSTBC |
3221 IEEE80211_VHT_CAP_RXSTBC_4 |
3222 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3223 sband->vht_cap.vht_mcs.rx_mcs_map =
3224 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3225 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3226 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3227 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3228 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3229 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3230 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3231 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3232 sband->vht_cap.vht_mcs.tx_mcs_map =
3233 sband->vht_cap.vht_mcs.rx_mcs_map;
3235 case NL80211_BAND_S1GHZ:
3236 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3237 sizeof(sband->s1g_cap));
3238 sband->channels = data->channels_s1g;
3239 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3245 sband->ht_cap.ht_supported = true;
3246 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3247 IEEE80211_HT_CAP_GRN_FLD |
3248 IEEE80211_HT_CAP_SGI_20 |
3249 IEEE80211_HT_CAP_SGI_40 |
3250 IEEE80211_HT_CAP_DSSSCCK40;
3251 sband->ht_cap.ampdu_factor = 0x3;
3252 sband->ht_cap.ampdu_density = 0x6;
3253 memset(&sband->ht_cap.mcs, 0,
3254 sizeof(sband->ht_cap.mcs));
3255 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3256 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3257 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3259 mac80211_hwsim_he_capab(sband);
3261 hw->wiphy->bands[band] = sband;
3264 /* By default all radios belong to the first group */
3266 mutex_init(&data->mutex);
3268 data->netgroup = hwsim_net_get_netgroup(net);
3269 data->wmediumd = hwsim_net_get_wmediumd(net);
3271 /* Enable frame retransmissions for lossy channels */
3273 hw->max_rate_tries = 11;
3275 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3276 hw->wiphy->n_vendor_commands =
3277 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3278 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3279 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3281 if (param->reg_strict)
3282 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3284 data->regd = param->regd;
3285 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3286 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3287 /* give the regulatory workqueue a chance to run */
3288 schedule_timeout_interruptible(1);
3292 ieee80211_hw_set(hw, NO_AUTO_VIF);
3294 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3296 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3297 HRTIMER_MODE_ABS_SOFT);
3298 data->beacon_timer.function = mac80211_hwsim_beacon;
3300 err = ieee80211_register_hw(hw);
3302 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3307 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3309 if (param->reg_alpha2) {
3310 data->alpha2[0] = param->reg_alpha2[0];
3311 data->alpha2[1] = param->reg_alpha2[1];
3312 regulatory_hint(hw->wiphy, param->reg_alpha2);
3315 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3316 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3317 debugfs_create_file("group", 0666, data->debugfs, data,
3319 if (!data->use_chanctx)
3320 debugfs_create_file("dfs_simulate_radar", 0222,
3322 data, &hwsim_simulate_radar);
3324 spin_lock_bh(&hwsim_radio_lock);
3325 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3329 GENL_SET_ERR_MSG(info, "perm addr already present");
3330 NL_SET_BAD_ATTR(info->extack,
3331 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3333 spin_unlock_bh(&hwsim_radio_lock);
3334 goto failed_final_insert;
3337 list_add_tail(&data->list, &hwsim_radios);
3338 hwsim_radios_generation++;
3339 spin_unlock_bh(&hwsim_radio_lock);
3341 hwsim_mcast_new_radio(idx, info, param);
3345 failed_final_insert:
3346 debugfs_remove_recursive(data->debugfs);
3347 ieee80211_unregister_hw(data->hw);
3349 device_release_driver(data->dev);
3351 device_unregister(data->dev);
3353 ieee80211_free_hw(hw);
3358 static void hwsim_mcast_del_radio(int id, const char *hwname,
3359 struct genl_info *info)
3361 struct sk_buff *skb;
3365 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3369 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3370 HWSIM_CMD_DEL_RADIO);
3374 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3378 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3383 genlmsg_end(skb, data);
3385 hwsim_mcast_config_msg(skb, info);
3393 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3395 struct genl_info *info)
3397 hwsim_mcast_del_radio(data->idx, hwname, info);
3398 debugfs_remove_recursive(data->debugfs);
3399 ieee80211_unregister_hw(data->hw);
3400 device_release_driver(data->dev);
3401 device_unregister(data->dev);
3402 ieee80211_free_hw(data->hw);
3405 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3406 struct mac80211_hwsim_data *data,
3407 u32 portid, u32 seq,
3408 struct netlink_callback *cb, int flags)
3411 struct hwsim_new_radio_params param = { };
3412 int res = -EMSGSIZE;
3414 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3415 HWSIM_CMD_GET_RADIO);
3420 genl_dump_check_consistent(cb, hdr);
3422 if (data->alpha2[0] && data->alpha2[1])
3423 param.reg_alpha2 = data->alpha2;
3425 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3426 REGULATORY_STRICT_REG);
3427 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3428 BIT(NL80211_IFTYPE_P2P_DEVICE));
3429 param.use_chanctx = data->use_chanctx;
3430 param.regd = data->regd;
3431 param.channels = data->channels;
3432 param.hwname = wiphy_name(data->hw->wiphy);
3434 res = append_radio_msg(skb, data->idx, ¶m);
3438 genlmsg_end(skb, hdr);
3442 genlmsg_cancel(skb, hdr);
3446 static void mac80211_hwsim_free(void)
3448 struct mac80211_hwsim_data *data;
3450 spin_lock_bh(&hwsim_radio_lock);
3451 while ((data = list_first_entry_or_null(&hwsim_radios,
3452 struct mac80211_hwsim_data,
3454 list_del(&data->list);
3455 spin_unlock_bh(&hwsim_radio_lock);
3456 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3458 spin_lock_bh(&hwsim_radio_lock);
3460 spin_unlock_bh(&hwsim_radio_lock);
3461 class_destroy(hwsim_class);
3464 static const struct net_device_ops hwsim_netdev_ops = {
3465 .ndo_start_xmit = hwsim_mon_xmit,
3466 .ndo_set_mac_address = eth_mac_addr,
3467 .ndo_validate_addr = eth_validate_addr,
3470 static void hwsim_mon_setup(struct net_device *dev)
3472 dev->netdev_ops = &hwsim_netdev_ops;
3473 dev->needs_free_netdev = true;
3475 dev->priv_flags |= IFF_NO_QUEUE;
3476 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3477 eth_zero_addr(dev->dev_addr);
3478 dev->dev_addr[0] = 0x12;
3481 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3483 return rhashtable_lookup_fast(&hwsim_radios_rht,
3488 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3490 struct mac80211_hwsim_data *data;
3492 hwsim_net_set_wmediumd(net, portid);
3494 spin_lock_bh(&hwsim_radio_lock);
3495 list_for_each_entry(data, &hwsim_radios, list) {
3496 if (data->netgroup == hwsim_net_get_netgroup(net))
3497 data->wmediumd = portid;
3499 spin_unlock_bh(&hwsim_radio_lock);
3502 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3503 struct genl_info *info)
3506 struct ieee80211_hdr *hdr;
3507 struct mac80211_hwsim_data *data2;
3508 struct ieee80211_tx_info *txi;
3509 struct hwsim_tx_rate *tx_attempts;
3511 struct sk_buff *skb, *tmp;
3513 unsigned int hwsim_flags;
3515 unsigned long flags;
3518 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3519 !info->attrs[HWSIM_ATTR_FLAGS] ||
3520 !info->attrs[HWSIM_ATTR_COOKIE] ||
3521 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3522 !info->attrs[HWSIM_ATTR_TX_INFO])
3525 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3526 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3527 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3529 data2 = get_hwsim_data_ref_from_addr(src);
3533 if (!hwsim_virtio_enabled) {
3534 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3538 if (info->snd_portid != data2->wmediumd)
3542 /* look for the skb matching the cookie passed back from user */
3543 spin_lock_irqsave(&data2->pending.lock, flags);
3544 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3545 uintptr_t skb_cookie;
3547 txi = IEEE80211_SKB_CB(skb);
3548 skb_cookie = (uintptr_t)txi->rate_driver_data[0];
3550 if (skb_cookie == ret_skb_cookie) {
3551 __skb_unlink(skb, &data2->pending);
3556 spin_unlock_irqrestore(&data2->pending.lock, flags);
3562 /* Tx info received because the frame was broadcasted on user space,
3563 so we get all the necessary info: tx attempts and skb control buff */
3565 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3566 info->attrs[HWSIM_ATTR_TX_INFO]);
3568 /* now send back TX status */
3569 txi = IEEE80211_SKB_CB(skb);
3571 ieee80211_tx_info_clear_status(txi);
3573 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3574 txi->status.rates[i].idx = tx_attempts[i].idx;
3575 txi->status.rates[i].count = tx_attempts[i].count;
3578 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3580 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3581 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3582 if (skb->len >= 16) {
3583 hdr = (struct ieee80211_hdr *) skb->data;
3584 mac80211_hwsim_monitor_ack(data2->channel,
3587 txi->flags |= IEEE80211_TX_STAT_ACK;
3590 if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
3591 txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
3593 ieee80211_tx_status_irqsafe(data2->hw, skb);
3600 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3601 struct genl_info *info)
3603 struct mac80211_hwsim_data *data2;
3604 struct ieee80211_rx_status rx_status;
3605 struct ieee80211_hdr *hdr;
3609 struct sk_buff *skb = NULL;
3610 struct ieee80211_channel *channel = NULL;
3612 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3613 !info->attrs[HWSIM_ATTR_FRAME] ||
3614 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3615 !info->attrs[HWSIM_ATTR_SIGNAL])
3618 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3619 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3620 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3622 if (frame_data_len < sizeof(struct ieee80211_hdr_3addr) ||
3623 frame_data_len > IEEE80211_MAX_DATA_LEN)
3626 /* Allocate new skb here */
3627 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3632 skb_put_data(skb, frame_data, frame_data_len);
3634 data2 = get_hwsim_data_ref_from_addr(dst);
3638 if (data2->use_chanctx) {
3639 if (data2->tmp_chan)
3640 channel = data2->tmp_chan;
3641 else if (data2->chanctx)
3642 channel = data2->chanctx->def.chan;
3644 channel = data2->channel;
3649 if (!hwsim_virtio_enabled) {
3650 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3654 if (info->snd_portid != data2->wmediumd)
3658 /* check if radio is configured properly */
3660 if ((data2->idle && !data2->tmp_chan) || !data2->started)
3663 /* A frame is received from user space */
3664 memset(&rx_status, 0, sizeof(rx_status));
3665 if (info->attrs[HWSIM_ATTR_FREQ]) {
3666 /* throw away off-channel packets, but allow both the temporary
3667 * ("hw" scan/remain-on-channel) and regular channel, since the
3668 * internal datapath also allows this
3670 mutex_lock(&data2->mutex);
3671 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3673 if (rx_status.freq != channel->center_freq) {
3674 mutex_unlock(&data2->mutex);
3677 mutex_unlock(&data2->mutex);
3679 rx_status.freq = channel->center_freq;
3682 rx_status.band = channel->band;
3683 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3684 if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
3686 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3688 hdr = (void *)skb->data;
3690 if (ieee80211_is_beacon(hdr->frame_control) ||
3691 ieee80211_is_probe_resp(hdr->frame_control))
3692 rx_status.boottime_ns = ktime_get_boottime_ns();
3694 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3696 data2->rx_bytes += skb->len;
3697 ieee80211_rx_irqsafe(data2->hw, skb);
3701 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3707 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3708 struct genl_info *info)
3710 struct net *net = genl_info_net(info);
3711 struct mac80211_hwsim_data *data;
3714 spin_lock_bh(&hwsim_radio_lock);
3715 list_for_each_entry(data, &hwsim_radios, list)
3716 chans = max(chans, data->channels);
3717 spin_unlock_bh(&hwsim_radio_lock);
3719 /* In the future we should revise the userspace API and allow it
3720 * to set a flag that it does support multi-channel, then we can
3721 * let this pass conditionally on the flag.
3722 * For current userspace, prohibit it since it won't work right.
3727 if (hwsim_net_get_wmediumd(net))
3730 hwsim_register_wmediumd(net, info->snd_portid);
3732 pr_debug("mac80211_hwsim: received a REGISTER, "
3733 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3738 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3739 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3743 for (i = 0; i < n_ciphers; i++) {
3747 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3748 if (ciphers[i] == hwsim_ciphers[j]) {
3761 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3763 struct hwsim_new_radio_params param = { 0 };
3764 const char *hwname = NULL;
3767 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3768 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3769 param.channels = channels;
3770 param.destroy_on_close =
3771 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3773 if (info->attrs[HWSIM_ATTR_CHANNELS])
3774 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3776 if (param.channels < 1) {
3777 GENL_SET_ERR_MSG(info, "must have at least one channel");
3781 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3782 GENL_SET_ERR_MSG(info, "too many channels specified");
3786 if (info->attrs[HWSIM_ATTR_NO_VIF])
3787 param.no_vif = true;
3789 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3790 param.use_chanctx = true;
3792 param.use_chanctx = (param.channels > 1);
3794 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3796 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3798 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3799 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3801 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3804 idx = array_index_nospec(idx,
3805 ARRAY_SIZE(hwsim_world_regdom_custom));
3806 param.regd = hwsim_world_regdom_custom[idx];
3809 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3810 if (!is_valid_ether_addr(
3811 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3812 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3813 NL_SET_BAD_ATTR(info->extack,
3814 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3818 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3821 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3823 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3825 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3826 NL_SET_ERR_MSG_ATTR(info->extack,
3827 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3828 "cannot support more iftypes than kernel");
3832 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3835 /* ensure both flag and iftype support is honored */
3836 if (param.p2p_device ||
3837 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3838 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3839 param.p2p_device = true;
3842 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3843 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3846 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3848 if (len % sizeof(u32)) {
3849 NL_SET_ERR_MSG_ATTR(info->extack,
3850 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3851 "bad cipher list length");
3855 param.n_ciphers = len / sizeof(u32);
3857 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3858 NL_SET_ERR_MSG_ATTR(info->extack,
3859 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3860 "too many ciphers specified");
3864 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3865 NL_SET_ERR_MSG_ATTR(info->extack,
3866 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3867 "unsupported ciphers specified");
3872 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3873 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3874 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3878 param.hwname = hwname;
3881 ret = mac80211_hwsim_new_radio(info, ¶m);
3886 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3888 struct mac80211_hwsim_data *data;
3890 const char *hwname = NULL;
3892 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3893 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3894 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3895 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3896 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3903 spin_lock_bh(&hwsim_radio_lock);
3904 list_for_each_entry(data, &hwsim_radios, list) {
3906 if (data->idx != idx)
3910 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3914 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3917 list_del(&data->list);
3918 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3920 hwsim_radios_generation++;
3921 spin_unlock_bh(&hwsim_radio_lock);
3922 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3927 spin_unlock_bh(&hwsim_radio_lock);
3933 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3935 struct mac80211_hwsim_data *data;
3936 struct sk_buff *skb;
3937 int idx, res = -ENODEV;
3939 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3941 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3943 spin_lock_bh(&hwsim_radio_lock);
3944 list_for_each_entry(data, &hwsim_radios, list) {
3945 if (data->idx != idx)
3948 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3951 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3957 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3958 info->snd_seq, NULL, 0);
3964 res = genlmsg_reply(skb, info);
3969 spin_unlock_bh(&hwsim_radio_lock);
3974 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3975 struct netlink_callback *cb)
3977 int last_idx = cb->args[0] - 1;
3978 struct mac80211_hwsim_data *data = NULL;
3982 spin_lock_bh(&hwsim_radio_lock);
3983 cb->seq = hwsim_radios_generation;
3985 if (last_idx >= hwsim_radio_idx-1)
3988 list_for_each_entry(data, &hwsim_radios, list) {
3989 if (data->idx <= last_idx)
3992 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3995 res = mac80211_hwsim_get_radio(skb, data,
3996 NETLINK_CB(cb->skb).portid,
3997 cb->nlh->nlmsg_seq, cb,
4002 last_idx = data->idx;
4005 cb->args[0] = last_idx + 1;
4007 /* list changed, but no new element sent, set interrupted flag */
4008 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4009 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4010 cb->nlh->nlmsg_seq, &hwsim_genl_family,
4011 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4013 genl_dump_check_consistent(cb, hdr);
4014 genlmsg_end(skb, hdr);
4021 spin_unlock_bh(&hwsim_radio_lock);
4022 return res ?: skb->len;
4025 /* Generic Netlink operations array */
4026 static const struct genl_small_ops hwsim_ops[] = {
4028 .cmd = HWSIM_CMD_REGISTER,
4029 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4030 .doit = hwsim_register_received_nl,
4031 .flags = GENL_UNS_ADMIN_PERM,
4034 .cmd = HWSIM_CMD_FRAME,
4035 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4036 .doit = hwsim_cloned_frame_received_nl,
4039 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4040 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4041 .doit = hwsim_tx_info_frame_received_nl,
4044 .cmd = HWSIM_CMD_NEW_RADIO,
4045 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4046 .doit = hwsim_new_radio_nl,
4047 .flags = GENL_UNS_ADMIN_PERM,
4050 .cmd = HWSIM_CMD_DEL_RADIO,
4051 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4052 .doit = hwsim_del_radio_nl,
4053 .flags = GENL_UNS_ADMIN_PERM,
4056 .cmd = HWSIM_CMD_GET_RADIO,
4057 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4058 .doit = hwsim_get_radio_nl,
4059 .dumpit = hwsim_dump_radio_nl,
4063 static struct genl_family hwsim_genl_family __ro_after_init = {
4064 .name = "MAC80211_HWSIM",
4066 .maxattr = HWSIM_ATTR_MAX,
4067 .policy = hwsim_genl_policy,
4069 .module = THIS_MODULE,
4070 .small_ops = hwsim_ops,
4071 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4072 .mcgrps = hwsim_mcgrps,
4073 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4076 static void remove_user_radios(u32 portid)
4078 struct mac80211_hwsim_data *entry, *tmp;
4081 spin_lock_bh(&hwsim_radio_lock);
4082 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4083 if (entry->destroy_on_close && entry->portid == portid) {
4084 list_move(&entry->list, &list);
4085 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4087 hwsim_radios_generation++;
4090 spin_unlock_bh(&hwsim_radio_lock);
4092 list_for_each_entry_safe(entry, tmp, &list, list) {
4093 list_del(&entry->list);
4094 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4099 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4100 unsigned long state,
4103 struct netlink_notify *notify = _notify;
4105 if (state != NETLINK_URELEASE)
4108 remove_user_radios(notify->portid);
4110 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4111 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4112 " socket, switching to perfect channel medium\n");
4113 hwsim_register_wmediumd(notify->net, 0);
4119 static struct notifier_block hwsim_netlink_notifier = {
4120 .notifier_call = mac80211_hwsim_netlink_notify,
4123 static int __init hwsim_init_netlink(void)
4127 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4129 rc = genl_register_family(&hwsim_genl_family);
4133 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4135 genl_unregister_family(&hwsim_genl_family);
4142 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4146 static __net_init int hwsim_init_net(struct net *net)
4148 return hwsim_net_set_netgroup(net);
4151 static void __net_exit hwsim_exit_net(struct net *net)
4153 struct mac80211_hwsim_data *data, *tmp;
4156 spin_lock_bh(&hwsim_radio_lock);
4157 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4158 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4161 /* Radios created in init_net are returned to init_net. */
4162 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4165 list_move(&data->list, &list);
4166 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4168 hwsim_radios_generation++;
4170 spin_unlock_bh(&hwsim_radio_lock);
4172 list_for_each_entry_safe(data, tmp, &list, list) {
4173 list_del(&data->list);
4174 mac80211_hwsim_del_radio(data,
4175 wiphy_name(data->hw->wiphy),
4179 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4182 static struct pernet_operations hwsim_net_ops = {
4183 .init = hwsim_init_net,
4184 .exit = hwsim_exit_net,
4185 .id = &hwsim_net_id,
4186 .size = sizeof(struct hwsim_net),
4189 static void hwsim_exit_netlink(void)
4191 /* unregister the notifier */
4192 netlink_unregister_notifier(&hwsim_netlink_notifier);
4193 /* unregister the family */
4194 genl_unregister_family(&hwsim_genl_family);
4197 #if IS_REACHABLE(CONFIG_VIRTIO)
4198 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4201 struct sk_buff *skb;
4202 unsigned long flags;
4204 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4205 while ((skb = virtqueue_get_buf(vq, &len)))
4207 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4210 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4212 struct nlmsghdr *nlh;
4213 struct genlmsghdr *gnlh;
4214 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4215 struct genl_info info = {};
4218 nlh = nlmsg_hdr(skb);
4219 gnlh = nlmsg_data(nlh);
4221 if (skb->len < nlh->nlmsg_len)
4224 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4225 hwsim_genl_policy, NULL);
4227 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4233 switch (gnlh->cmd) {
4234 case HWSIM_CMD_FRAME:
4235 hwsim_cloned_frame_received_nl(skb, &info);
4237 case HWSIM_CMD_TX_INFO_FRAME:
4238 hwsim_tx_info_frame_received_nl(skb, &info);
4241 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4247 static void hwsim_virtio_rx_work(struct work_struct *work)
4249 struct virtqueue *vq;
4251 struct sk_buff *skb;
4252 struct scatterlist sg[1];
4254 unsigned long flags;
4256 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4257 if (!hwsim_virtio_enabled)
4260 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4263 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4265 skb->data = skb->head;
4266 skb_reset_tail_pointer(skb);
4268 hwsim_virtio_handle_cmd(skb);
4270 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4271 if (!hwsim_virtio_enabled) {
4275 vq = hwsim_vqs[HWSIM_VQ_RX];
4276 sg_init_one(sg, skb->head, skb_end_offset(skb));
4277 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4278 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4282 schedule_work(&hwsim_virtio_rx);
4285 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4288 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4290 schedule_work(&hwsim_virtio_rx);
4293 static int init_vqs(struct virtio_device *vdev)
4295 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4296 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4297 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4299 const char *names[HWSIM_NUM_VQS] = {
4300 [HWSIM_VQ_TX] = "tx",
4301 [HWSIM_VQ_RX] = "rx",
4304 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4305 hwsim_vqs, callbacks, names, NULL);
4308 static int fill_vq(struct virtqueue *vq)
4311 struct sk_buff *skb;
4312 struct scatterlist sg[1];
4314 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4315 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4319 sg_init_one(sg, skb->head, skb_end_offset(skb));
4320 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4330 static void remove_vqs(struct virtio_device *vdev)
4334 vdev->config->reset(vdev);
4336 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4337 struct virtqueue *vq = hwsim_vqs[i];
4338 struct sk_buff *skb;
4340 while ((skb = virtqueue_detach_unused_buf(vq)))
4344 vdev->config->del_vqs(vdev);
4347 static int hwsim_virtio_probe(struct virtio_device *vdev)
4350 unsigned long flags;
4352 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4353 if (hwsim_virtio_enabled) {
4354 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4357 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4359 err = init_vqs(vdev);
4363 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4367 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4368 hwsim_virtio_enabled = true;
4369 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4371 schedule_work(&hwsim_virtio_rx);
4379 static void hwsim_virtio_remove(struct virtio_device *vdev)
4381 hwsim_virtio_enabled = false;
4383 cancel_work_sync(&hwsim_virtio_rx);
4388 /* MAC80211_HWSIM virtio device id table */
4389 static const struct virtio_device_id id_table[] = {
4390 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4393 MODULE_DEVICE_TABLE(virtio, id_table);
4395 static struct virtio_driver virtio_hwsim = {
4396 .driver.name = KBUILD_MODNAME,
4397 .driver.owner = THIS_MODULE,
4398 .id_table = id_table,
4399 .probe = hwsim_virtio_probe,
4400 .remove = hwsim_virtio_remove,
4403 static int hwsim_register_virtio_driver(void)
4405 spin_lock_init(&hwsim_virtio_lock);
4407 return register_virtio_driver(&virtio_hwsim);
4410 static void hwsim_unregister_virtio_driver(void)
4412 unregister_virtio_driver(&virtio_hwsim);
4415 static inline int hwsim_register_virtio_driver(void)
4420 static inline void hwsim_unregister_virtio_driver(void)
4425 static int __init init_mac80211_hwsim(void)
4429 if (radios < 0 || radios > 100)
4435 spin_lock_init(&hwsim_radio_lock);
4437 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4441 err = register_pernet_device(&hwsim_net_ops);
4445 err = platform_driver_register(&mac80211_hwsim_driver);
4447 goto out_unregister_pernet;
4449 err = hwsim_init_netlink();
4451 goto out_unregister_driver;
4453 err = hwsim_register_virtio_driver();
4455 goto out_exit_netlink;
4457 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4458 if (IS_ERR(hwsim_class)) {
4459 err = PTR_ERR(hwsim_class);
4460 goto out_exit_virtio;
4463 hwsim_init_s1g_channels(hwsim_channels_s1g);
4465 for (i = 0; i < radios; i++) {
4466 struct hwsim_new_radio_params param = { 0 };
4468 param.channels = channels;
4471 case HWSIM_REGTEST_DIFF_COUNTRY:
4472 if (i < ARRAY_SIZE(hwsim_alpha2s))
4473 param.reg_alpha2 = hwsim_alpha2s[i];
4475 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4477 param.reg_alpha2 = hwsim_alpha2s[0];
4479 case HWSIM_REGTEST_STRICT_ALL:
4480 param.reg_strict = true;
4482 case HWSIM_REGTEST_DRIVER_REG_ALL:
4483 param.reg_alpha2 = hwsim_alpha2s[0];
4485 case HWSIM_REGTEST_WORLD_ROAM:
4487 param.regd = &hwsim_world_regdom_custom_01;
4489 case HWSIM_REGTEST_CUSTOM_WORLD:
4490 param.regd = &hwsim_world_regdom_custom_01;
4492 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4494 param.regd = &hwsim_world_regdom_custom_01;
4496 param.regd = &hwsim_world_regdom_custom_02;
4498 case HWSIM_REGTEST_STRICT_FOLLOW:
4500 param.reg_strict = true;
4501 param.reg_alpha2 = hwsim_alpha2s[0];
4504 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4506 param.reg_strict = true;
4507 param.reg_alpha2 = hwsim_alpha2s[0];
4508 } else if (i == 1) {
4509 param.reg_alpha2 = hwsim_alpha2s[1];
4512 case HWSIM_REGTEST_ALL:
4515 param.regd = &hwsim_world_regdom_custom_01;
4518 param.regd = &hwsim_world_regdom_custom_02;
4521 param.reg_alpha2 = hwsim_alpha2s[0];
4524 param.reg_alpha2 = hwsim_alpha2s[1];
4527 param.reg_strict = true;
4528 param.reg_alpha2 = hwsim_alpha2s[2];
4536 param.p2p_device = support_p2p_device;
4537 param.use_chanctx = channels > 1;
4538 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4539 if (param.p2p_device)
4540 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4542 err = mac80211_hwsim_new_radio(NULL, ¶m);
4544 goto out_free_radios;
4547 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4549 if (hwsim_mon == NULL) {
4551 goto out_free_radios;
4555 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4561 err = register_netdevice(hwsim_mon);
4571 free_netdev(hwsim_mon);
4573 mac80211_hwsim_free();
4575 hwsim_unregister_virtio_driver();
4577 hwsim_exit_netlink();
4578 out_unregister_driver:
4579 platform_driver_unregister(&mac80211_hwsim_driver);
4580 out_unregister_pernet:
4581 unregister_pernet_device(&hwsim_net_ops);
4583 rhashtable_destroy(&hwsim_radios_rht);
4586 module_init(init_mac80211_hwsim);
4588 static void __exit exit_mac80211_hwsim(void)
4590 pr_debug("mac80211_hwsim: unregister radios\n");
4592 hwsim_unregister_virtio_driver();
4593 hwsim_exit_netlink();
4595 mac80211_hwsim_free();
4597 rhashtable_destroy(&hwsim_radios_rht);
4598 unregister_netdev(hwsim_mon);
4599 platform_driver_unregister(&mac80211_hwsim_driver);
4600 unregister_pernet_device(&hwsim_net_ops);
4602 module_exit(exit_mac80211_hwsim);