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;
782 wiphy_dbg(data->hw->wiphy,
783 "%s: send data::nullfunc to %pM ps=%d\n",
784 __func__, vp->bssid, ps);
786 skb = dev_alloc_skb(sizeof(*hdr));
789 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
790 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
791 IEEE80211_STYPE_NULLFUNC |
792 IEEE80211_FCTL_TODS |
793 (ps ? IEEE80211_FCTL_PM : 0));
794 hdr->duration_id = cpu_to_le16(0);
795 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
796 memcpy(hdr->addr2, mac, ETH_ALEN);
797 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
800 mac80211_hwsim_tx_frame(data->hw, skb,
801 rcu_dereference(vif->chanctx_conf)->def.chan);
806 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
807 struct ieee80211_vif *vif)
809 struct mac80211_hwsim_data *data = dat;
810 hwsim_send_nullfunc(data, mac, vif, 1);
813 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
814 struct ieee80211_vif *vif)
816 struct mac80211_hwsim_data *data = dat;
817 hwsim_send_nullfunc(data, mac, vif, 0);
820 static int hwsim_fops_ps_read(void *dat, u64 *val)
822 struct mac80211_hwsim_data *data = dat;
827 static int hwsim_fops_ps_write(void *dat, u64 val)
829 struct mac80211_hwsim_data *data = dat;
832 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
833 val != PS_MANUAL_POLL)
836 if (val == PS_MANUAL_POLL) {
837 if (data->ps != PS_ENABLED)
840 ieee80211_iterate_active_interfaces_atomic(
841 data->hw, IEEE80211_IFACE_ITER_NORMAL,
842 hwsim_send_ps_poll, data);
850 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
851 ieee80211_iterate_active_interfaces_atomic(
852 data->hw, IEEE80211_IFACE_ITER_NORMAL,
853 hwsim_send_nullfunc_ps, data);
854 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
855 ieee80211_iterate_active_interfaces_atomic(
856 data->hw, IEEE80211_IFACE_ITER_NORMAL,
857 hwsim_send_nullfunc_no_ps, data);
864 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
867 static int hwsim_write_simulate_radar(void *dat, u64 val)
869 struct mac80211_hwsim_data *data = dat;
871 ieee80211_radar_detected(data->hw);
876 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
877 hwsim_write_simulate_radar, "%llu\n");
879 static int hwsim_fops_group_read(void *dat, u64 *val)
881 struct mac80211_hwsim_data *data = dat;
886 static int hwsim_fops_group_write(void *dat, u64 val)
888 struct mac80211_hwsim_data *data = dat;
893 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
894 hwsim_fops_group_read, hwsim_fops_group_write,
897 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
898 struct net_device *dev)
900 /* TODO: allow packet injection */
905 static inline u64 mac80211_hwsim_get_tsf_raw(void)
907 return ktime_to_us(ktime_get_real());
910 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
912 u64 now = mac80211_hwsim_get_tsf_raw();
913 return cpu_to_le64(now + data->tsf_offset);
916 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
917 struct ieee80211_vif *vif)
919 struct mac80211_hwsim_data *data = hw->priv;
920 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
923 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
924 struct ieee80211_vif *vif, u64 tsf)
926 struct mac80211_hwsim_data *data = hw->priv;
927 u64 now = mac80211_hwsim_get_tsf(hw, vif);
928 u32 bcn_int = data->beacon_int;
929 u64 delta = abs(tsf - now);
931 /* adjust after beaconing with new timestamp at old TBTT */
933 data->tsf_offset += delta;
934 data->bcn_delta = do_div(delta, bcn_int);
936 data->tsf_offset -= delta;
937 data->bcn_delta = -(s64)do_div(delta, bcn_int);
941 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
942 struct sk_buff *tx_skb,
943 struct ieee80211_channel *chan)
945 struct mac80211_hwsim_data *data = hw->priv;
947 struct hwsim_radiotap_hdr *hdr;
949 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
950 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
955 bitrate = txrate->bitrate;
957 if (!netif_running(hwsim_mon))
960 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
964 hdr = skb_push(skb, sizeof(*hdr));
965 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
967 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
968 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
969 (1 << IEEE80211_RADIOTAP_RATE) |
970 (1 << IEEE80211_RADIOTAP_TSFT) |
971 (1 << IEEE80211_RADIOTAP_CHANNEL));
972 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
974 hdr->rt_rate = bitrate / 5;
975 hdr->rt_channel = cpu_to_le16(chan->center_freq);
976 flags = IEEE80211_CHAN_2GHZ;
977 if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
978 flags |= IEEE80211_CHAN_OFDM;
980 flags |= IEEE80211_CHAN_CCK;
981 hdr->rt_chbitmask = cpu_to_le16(flags);
983 skb->dev = hwsim_mon;
984 skb_reset_mac_header(skb);
985 skb->ip_summed = CHECKSUM_UNNECESSARY;
986 skb->pkt_type = PACKET_OTHERHOST;
987 skb->protocol = htons(ETH_P_802_2);
988 memset(skb->cb, 0, sizeof(skb->cb));
993 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
997 struct hwsim_radiotap_ack_hdr *hdr;
999 struct ieee80211_hdr *hdr11;
1001 if (!netif_running(hwsim_mon))
1004 skb = dev_alloc_skb(100);
1008 hdr = skb_put(skb, sizeof(*hdr));
1009 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1010 hdr->hdr.it_pad = 0;
1011 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1012 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1013 (1 << IEEE80211_RADIOTAP_CHANNEL));
1016 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1017 flags = IEEE80211_CHAN_2GHZ;
1018 hdr->rt_chbitmask = cpu_to_le16(flags);
1020 hdr11 = skb_put(skb, 10);
1021 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1022 IEEE80211_STYPE_ACK);
1023 hdr11->duration_id = cpu_to_le16(0);
1024 memcpy(hdr11->addr1, addr, ETH_ALEN);
1026 skb->dev = hwsim_mon;
1027 skb_reset_mac_header(skb);
1028 skb->ip_summed = CHECKSUM_UNNECESSARY;
1029 skb->pkt_type = PACKET_OTHERHOST;
1030 skb->protocol = htons(ETH_P_802_2);
1031 memset(skb->cb, 0, sizeof(skb->cb));
1035 struct mac80211_hwsim_addr_match_data {
1040 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1041 struct ieee80211_vif *vif)
1043 struct mac80211_hwsim_addr_match_data *md = data;
1045 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
1049 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1052 struct mac80211_hwsim_addr_match_data md = {
1056 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1059 memcpy(md.addr, addr, ETH_ALEN);
1061 ieee80211_iterate_active_interfaces_atomic(data->hw,
1062 IEEE80211_IFACE_ITER_NORMAL,
1063 mac80211_hwsim_addr_iter,
1069 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1070 struct sk_buff *skb)
1078 /* TODO: accept (some) Beacons by default and other frames only
1079 * if pending PS-Poll has been sent */
1081 case PS_MANUAL_POLL:
1082 /* Allow unicast frames to own address if there is a pending
1084 if (data->ps_poll_pending &&
1085 mac80211_hwsim_addr_match(data, skb->data + 4)) {
1086 data->ps_poll_pending = false;
1095 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1096 struct sk_buff *skb, int portid)
1103 for_each_net_rcu(net) {
1104 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1105 res = genlmsg_unicast(net, skb, portid);
1118 static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1119 const u8 *addr, bool add)
1121 struct mac80211_hwsim_data *data = hw->priv;
1122 u32 _portid = READ_ONCE(data->wmediumd);
1123 struct sk_buff *skb;
1126 if (!_portid && !hwsim_virtio_enabled)
1129 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1133 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1134 add ? HWSIM_CMD_ADD_MAC_ADDR :
1135 HWSIM_CMD_DEL_MAC_ADDR);
1137 pr_debug("mac80211_hwsim: problem with msg_head\n");
1138 goto nla_put_failure;
1141 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1142 ETH_ALEN, data->addresses[1].addr))
1143 goto nla_put_failure;
1145 if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1146 goto nla_put_failure;
1148 genlmsg_end(skb, msg_head);
1150 if (hwsim_virtio_enabled)
1151 hwsim_tx_virtio(data, skb);
1153 hwsim_unicast_netgroup(data, skb, _portid);
1159 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1163 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1164 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1165 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1166 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1167 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1168 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1169 if (rate->flags & IEEE80211_TX_RC_MCS)
1170 result |= MAC80211_HWSIM_TX_RC_MCS;
1171 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1172 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1173 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1174 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1175 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1176 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1177 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1178 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1179 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1180 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1181 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1182 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1183 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1184 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1189 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1190 struct sk_buff *my_skb,
1192 struct ieee80211_channel *channel)
1194 struct sk_buff *skb;
1195 struct mac80211_hwsim_data *data = hw->priv;
1196 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1197 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1199 unsigned int hwsim_flags = 0;
1201 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1202 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1205 if (data->ps != PS_DISABLED)
1206 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1207 /* If the queue contains MAX_QUEUE skb's drop some */
1208 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1209 /* Droping until WARN_QUEUE level */
1210 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1211 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1216 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1218 goto nla_put_failure;
1220 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1222 if (msg_head == NULL) {
1223 pr_debug("mac80211_hwsim: problem with msg_head\n");
1224 goto nla_put_failure;
1227 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1228 ETH_ALEN, data->addresses[1].addr))
1229 goto nla_put_failure;
1231 /* We get the skb->data */
1232 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1233 goto nla_put_failure;
1235 /* We get the flags for this transmission, and we translate them to
1238 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1239 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1241 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1242 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1244 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1245 goto nla_put_failure;
1247 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1248 goto nla_put_failure;
1250 /* We get the tx control (rate and retries) info*/
1252 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1253 tx_attempts[i].idx = info->status.rates[i].idx;
1254 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1255 tx_attempts[i].count = info->status.rates[i].count;
1256 tx_attempts_flags[i].flags =
1257 trans_tx_rate_flags_ieee2hwsim(
1258 &info->status.rates[i]);
1261 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1262 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1264 goto nla_put_failure;
1266 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1267 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1269 goto nla_put_failure;
1271 /* We create a cookie to identify this skb */
1272 cookie = atomic_inc_return(&data->pending_cookie);
1273 info->rate_driver_data[0] = (void *)cookie;
1274 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1275 goto nla_put_failure;
1277 genlmsg_end(skb, msg_head);
1279 if (hwsim_virtio_enabled) {
1280 if (hwsim_tx_virtio(data, skb))
1281 goto err_free_txskb;
1283 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1284 goto err_free_txskb;
1287 /* Enqueue the packet */
1288 skb_queue_tail(&data->pending, my_skb);
1290 data->tx_bytes += my_skb->len;
1296 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1297 ieee80211_free_txskb(hw, my_skb);
1301 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1302 struct ieee80211_channel *c2)
1307 return c1->center_freq == c2->center_freq;
1310 struct tx_iter_data {
1311 struct ieee80211_channel *channel;
1315 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1316 struct ieee80211_vif *vif)
1318 struct tx_iter_data *data = _data;
1320 if (!vif->chanctx_conf)
1323 if (!hwsim_chans_compat(data->channel,
1324 rcu_dereference(vif->chanctx_conf)->def.chan))
1327 data->receive = true;
1330 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1333 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1335 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1336 * (but you should use a valid OUI, not that)
1338 * If anyone wants to 'donate' a radiotap OUI/subns code
1339 * please send a patch removing this #ifdef and changing
1340 * the values accordingly.
1342 #ifdef HWSIM_RADIOTAP_OUI
1343 struct ieee80211_vendor_radiotap *rtap;
1346 * Note that this code requires the headroom in the SKB
1347 * that was allocated earlier.
1349 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1350 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1351 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1352 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1356 * Radiotap vendor namespaces can (and should) also be
1357 * split into fields by using the standard radiotap
1358 * presence bitmap mechanism. Use just BIT(0) here for
1359 * the presence bitmap.
1361 rtap->present = BIT(0);
1362 /* We have 8 bytes of (dummy) data */
1364 /* For testing, also require it to be aligned */
1366 /* And also test that padding works, 4 bytes */
1369 memcpy(rtap->data, "ABCDEFGH", 8);
1370 /* make sure to clear padding, mac80211 doesn't */
1371 memset(rtap->data + 8, 0, 4);
1373 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1377 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1378 struct sk_buff *skb,
1379 struct ieee80211_channel *chan)
1381 struct mac80211_hwsim_data *data = hw->priv, *data2;
1383 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1384 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1385 struct ieee80211_rx_status rx_status;
1388 memset(&rx_status, 0, sizeof(rx_status));
1389 rx_status.flag |= RX_FLAG_MACTIME_START;
1390 rx_status.freq = chan->center_freq;
1391 rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1392 rx_status.band = chan->band;
1393 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1394 rx_status.rate_idx =
1395 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1397 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1398 rx_status.encoding = RX_ENC_VHT;
1400 rx_status.rate_idx = info->control.rates[0].idx;
1401 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1402 rx_status.encoding = RX_ENC_HT;
1404 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1405 rx_status.bw = RATE_INFO_BW_40;
1406 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1407 rx_status.bw = RATE_INFO_BW_80;
1408 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1409 rx_status.bw = RATE_INFO_BW_160;
1411 rx_status.bw = RATE_INFO_BW_20;
1412 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1413 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1414 /* TODO: simulate real signal strength (and optional packet loss) */
1415 rx_status.signal = -50;
1416 if (info->control.vif)
1417 rx_status.signal += info->control.vif->bss_conf.txpower;
1419 if (data->ps != PS_DISABLED)
1420 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1422 /* release the skb's source info */
1430 * Get absolute mactime here so all HWs RX at the "same time", and
1431 * absolute TX time for beacon mactime so the timestamp matches.
1432 * Giving beacons a different mactime than non-beacons looks messy, but
1433 * it helps the Toffset be exact and a ~10us mactime discrepancy
1434 * probably doesn't really matter.
1436 if (ieee80211_is_beacon(hdr->frame_control) ||
1437 ieee80211_is_probe_resp(hdr->frame_control)) {
1438 rx_status.boottime_ns = ktime_get_boottime_ns();
1439 now = data->abs_bcn_ts;
1441 now = mac80211_hwsim_get_tsf_raw();
1444 /* Copy skb to all enabled radios that are on the current frequency */
1445 spin_lock(&hwsim_radio_lock);
1446 list_for_each_entry(data2, &hwsim_radios, list) {
1447 struct sk_buff *nskb;
1448 struct tx_iter_data tx_iter_data = {
1456 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1457 !hwsim_ps_rx_ok(data2, skb))
1460 if (!(data->group & data2->group))
1463 if (data->netgroup != data2->netgroup)
1466 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1467 !hwsim_chans_compat(chan, data2->channel)) {
1468 ieee80211_iterate_active_interfaces_atomic(
1469 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1470 mac80211_hwsim_tx_iter, &tx_iter_data);
1471 if (!tx_iter_data.receive)
1476 * reserve some space for our vendor and the normal
1477 * radiotap header, since we're copying anyway
1479 if (skb->len < PAGE_SIZE && paged_rx) {
1480 struct page *page = alloc_page(GFP_ATOMIC);
1485 nskb = dev_alloc_skb(128);
1491 memcpy(page_address(page), skb->data, skb->len);
1492 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1494 nskb = skb_copy(skb, GFP_ATOMIC);
1499 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1502 rx_status.mactime = now + data2->tsf_offset;
1504 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1506 mac80211_hwsim_add_vendor_rtap(nskb);
1509 data2->rx_bytes += nskb->len;
1510 ieee80211_rx_irqsafe(data2->hw, nskb);
1512 spin_unlock(&hwsim_radio_lock);
1517 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1518 struct ieee80211_tx_control *control,
1519 struct sk_buff *skb)
1521 struct mac80211_hwsim_data *data = hw->priv;
1522 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1523 struct ieee80211_hdr *hdr = (void *)skb->data;
1524 struct ieee80211_chanctx_conf *chanctx_conf;
1525 struct ieee80211_channel *channel;
1529 if (WARN_ON(skb->len < 10)) {
1530 /* Should not happen; just a sanity check for addr1 use */
1531 ieee80211_free_txskb(hw, skb);
1535 if (!data->use_chanctx) {
1536 channel = data->channel;
1537 } else if (txi->hw_queue == 4) {
1538 channel = data->tmp_chan;
1540 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1542 channel = chanctx_conf->def.chan;
1547 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1548 ieee80211_free_txskb(hw, skb);
1552 if (data->idle && !data->tmp_chan) {
1553 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1554 ieee80211_free_txskb(hw, skb);
1558 if (txi->control.vif)
1559 hwsim_check_magic(txi->control.vif);
1561 hwsim_check_sta_magic(control->sta);
1563 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1564 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1566 ARRAY_SIZE(txi->control.rates));
1568 if (skb->len >= 24 + 8 &&
1569 ieee80211_is_probe_resp(hdr->frame_control)) {
1570 /* fake header transmission time */
1571 struct ieee80211_mgmt *mgmt;
1572 struct ieee80211_rate *txrate;
1577 mgmt = (struct ieee80211_mgmt *)skb->data;
1578 txrate = ieee80211_get_tx_rate(hw, txi);
1580 bitrate = txrate->bitrate;
1581 ts = mac80211_hwsim_get_tsf_raw();
1582 mgmt->u.probe_resp.timestamp =
1583 cpu_to_le64(ts + data->tsf_offset +
1584 24 * 8 * 10 / bitrate);
1587 mac80211_hwsim_monitor_rx(hw, skb, channel);
1589 /* wmediumd mode check */
1590 _portid = READ_ONCE(data->wmediumd);
1592 if (_portid || hwsim_virtio_enabled)
1593 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1595 /* NO wmediumd detected, perfect medium simulation */
1597 data->tx_bytes += skb->len;
1598 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1600 if (ack && skb->len >= 16)
1601 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1603 ieee80211_tx_info_clear_status(txi);
1605 /* frame was transmitted at most favorable rate at first attempt */
1606 txi->control.rates[0].count = 1;
1607 txi->control.rates[1].idx = -1;
1609 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1610 txi->flags |= IEEE80211_TX_STAT_ACK;
1611 ieee80211_tx_status_irqsafe(hw, skb);
1615 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1617 struct mac80211_hwsim_data *data = hw->priv;
1618 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1619 data->started = true;
1624 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1626 struct mac80211_hwsim_data *data = hw->priv;
1628 data->started = false;
1629 hrtimer_cancel(&data->beacon_timer);
1631 while (!skb_queue_empty(&data->pending))
1632 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1634 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1638 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1639 struct ieee80211_vif *vif)
1641 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1642 __func__, ieee80211_vif_type_p2p(vif),
1644 hwsim_set_magic(vif);
1646 if (vif->type != NL80211_IFTYPE_MONITOR)
1647 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1650 vif->hw_queue[IEEE80211_AC_VO] = 0;
1651 vif->hw_queue[IEEE80211_AC_VI] = 1;
1652 vif->hw_queue[IEEE80211_AC_BE] = 2;
1653 vif->hw_queue[IEEE80211_AC_BK] = 3;
1659 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1660 struct ieee80211_vif *vif,
1661 enum nl80211_iftype newtype,
1664 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1665 wiphy_dbg(hw->wiphy,
1666 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1667 __func__, ieee80211_vif_type_p2p(vif),
1668 newtype, vif->addr);
1669 hwsim_check_magic(vif);
1672 * interface may change from non-AP to AP in
1673 * which case this needs to be set up again
1680 static void mac80211_hwsim_remove_interface(
1681 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1683 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1684 __func__, ieee80211_vif_type_p2p(vif),
1686 hwsim_check_magic(vif);
1687 hwsim_clear_magic(vif);
1688 if (vif->type != NL80211_IFTYPE_MONITOR)
1689 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1692 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1693 struct sk_buff *skb,
1694 struct ieee80211_channel *chan)
1696 struct mac80211_hwsim_data *data = hw->priv;
1697 u32 _pid = READ_ONCE(data->wmediumd);
1699 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1700 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1701 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1703 ARRAY_SIZE(txi->control.rates));
1706 mac80211_hwsim_monitor_rx(hw, skb, chan);
1708 if (_pid || hwsim_virtio_enabled)
1709 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1711 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1715 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1716 struct ieee80211_vif *vif)
1718 struct mac80211_hwsim_data *data = arg;
1719 struct ieee80211_hw *hw = data->hw;
1720 struct ieee80211_tx_info *info;
1721 struct ieee80211_rate *txrate;
1722 struct ieee80211_mgmt *mgmt;
1723 struct sk_buff *skb;
1727 hwsim_check_magic(vif);
1729 if (vif->type != NL80211_IFTYPE_AP &&
1730 vif->type != NL80211_IFTYPE_MESH_POINT &&
1731 vif->type != NL80211_IFTYPE_ADHOC &&
1732 vif->type != NL80211_IFTYPE_OCB)
1735 skb = ieee80211_beacon_get(hw, vif);
1738 info = IEEE80211_SKB_CB(skb);
1739 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1740 ieee80211_get_tx_rates(vif, NULL, skb,
1741 info->control.rates,
1742 ARRAY_SIZE(info->control.rates));
1744 txrate = ieee80211_get_tx_rate(hw, info);
1746 bitrate = txrate->bitrate;
1748 mgmt = (struct ieee80211_mgmt *) skb->data;
1749 /* fake header transmission time */
1750 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1751 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1752 struct ieee80211_ext *ext = (void *) mgmt;
1754 ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1759 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1765 mac80211_hwsim_tx_frame(hw, skb,
1766 rcu_dereference(vif->chanctx_conf)->def.chan);
1768 while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1769 mac80211_hwsim_tx_frame(hw, skb,
1770 rcu_dereference(vif->chanctx_conf)->def.chan);
1773 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1774 ieee80211_csa_finish(vif);
1777 static enum hrtimer_restart
1778 mac80211_hwsim_beacon(struct hrtimer *timer)
1780 struct mac80211_hwsim_data *data =
1781 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1782 struct ieee80211_hw *hw = data->hw;
1783 u64 bcn_int = data->beacon_int;
1786 return HRTIMER_NORESTART;
1788 ieee80211_iterate_active_interfaces_atomic(
1789 hw, IEEE80211_IFACE_ITER_NORMAL,
1790 mac80211_hwsim_beacon_tx, data);
1792 /* beacon at new TBTT + beacon interval */
1793 if (data->bcn_delta) {
1794 bcn_int -= data->bcn_delta;
1795 data->bcn_delta = 0;
1797 hrtimer_forward_now(&data->beacon_timer,
1798 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1799 return HRTIMER_RESTART;
1802 static const char * const hwsim_chanwidths[] = {
1803 [NL80211_CHAN_WIDTH_5] = "ht5",
1804 [NL80211_CHAN_WIDTH_10] = "ht10",
1805 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1806 [NL80211_CHAN_WIDTH_20] = "ht20",
1807 [NL80211_CHAN_WIDTH_40] = "ht40",
1808 [NL80211_CHAN_WIDTH_80] = "vht80",
1809 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1810 [NL80211_CHAN_WIDTH_160] = "vht160",
1811 [NL80211_CHAN_WIDTH_1] = "1MHz",
1812 [NL80211_CHAN_WIDTH_2] = "2MHz",
1813 [NL80211_CHAN_WIDTH_4] = "4MHz",
1814 [NL80211_CHAN_WIDTH_8] = "8MHz",
1815 [NL80211_CHAN_WIDTH_16] = "16MHz",
1818 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1820 struct mac80211_hwsim_data *data = hw->priv;
1821 struct ieee80211_conf *conf = &hw->conf;
1822 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1823 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1824 [IEEE80211_SMPS_OFF] = "off",
1825 [IEEE80211_SMPS_STATIC] = "static",
1826 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1830 if (conf->chandef.chan)
1831 wiphy_dbg(hw->wiphy,
1832 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1834 conf->chandef.chan->center_freq,
1835 conf->chandef.center_freq1,
1836 conf->chandef.center_freq2,
1837 hwsim_chanwidths[conf->chandef.width],
1838 !!(conf->flags & IEEE80211_CONF_IDLE),
1839 !!(conf->flags & IEEE80211_CONF_PS),
1840 smps_modes[conf->smps_mode]);
1842 wiphy_dbg(hw->wiphy,
1843 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1845 !!(conf->flags & IEEE80211_CONF_IDLE),
1846 !!(conf->flags & IEEE80211_CONF_PS),
1847 smps_modes[conf->smps_mode]);
1849 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1851 WARN_ON(conf->chandef.chan && data->use_chanctx);
1853 mutex_lock(&data->mutex);
1854 if (data->scanning && conf->chandef.chan) {
1855 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1856 if (data->survey_data[idx].channel == data->channel) {
1857 data->survey_data[idx].start =
1858 data->survey_data[idx].next_start;
1859 data->survey_data[idx].end = jiffies;
1864 data->channel = conf->chandef.chan;
1866 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1867 if (data->survey_data[idx].channel &&
1868 data->survey_data[idx].channel != data->channel)
1870 data->survey_data[idx].channel = data->channel;
1871 data->survey_data[idx].next_start = jiffies;
1875 data->channel = conf->chandef.chan;
1877 mutex_unlock(&data->mutex);
1879 if (!data->started || !data->beacon_int)
1880 hrtimer_cancel(&data->beacon_timer);
1881 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1882 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1883 u32 bcn_int = data->beacon_int;
1884 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1886 hrtimer_start(&data->beacon_timer,
1887 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1888 HRTIMER_MODE_REL_SOFT);
1895 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1896 unsigned int changed_flags,
1897 unsigned int *total_flags,u64 multicast)
1899 struct mac80211_hwsim_data *data = hw->priv;
1901 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1903 data->rx_filter = 0;
1904 if (*total_flags & FIF_ALLMULTI)
1905 data->rx_filter |= FIF_ALLMULTI;
1906 if (*total_flags & FIF_MCAST_ACTION)
1907 data->rx_filter |= FIF_MCAST_ACTION;
1909 *total_flags = data->rx_filter;
1912 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1913 struct ieee80211_vif *vif)
1915 unsigned int *count = data;
1916 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1922 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1923 struct ieee80211_vif *vif,
1924 struct ieee80211_bss_conf *info,
1927 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1928 struct mac80211_hwsim_data *data = hw->priv;
1930 hwsim_check_magic(vif);
1932 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1933 __func__, changed, vif->addr);
1935 if (changed & BSS_CHANGED_BSSID) {
1936 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1937 __func__, info->bssid);
1938 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1941 if (changed & BSS_CHANGED_ASSOC) {
1942 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1943 info->assoc, info->aid);
1944 vp->assoc = info->assoc;
1945 vp->aid = info->aid;
1948 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1949 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1950 info->enable_beacon, info->beacon_int);
1951 vp->bcn_en = info->enable_beacon;
1952 if (data->started &&
1953 !hrtimer_is_queued(&data->beacon_timer) &&
1954 info->enable_beacon) {
1955 u64 tsf, until_tbtt;
1957 data->beacon_int = info->beacon_int * 1024;
1958 tsf = mac80211_hwsim_get_tsf(hw, vif);
1959 bcn_int = data->beacon_int;
1960 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1962 hrtimer_start(&data->beacon_timer,
1963 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1964 HRTIMER_MODE_REL_SOFT);
1965 } else if (!info->enable_beacon) {
1966 unsigned int count = 0;
1967 ieee80211_iterate_active_interfaces_atomic(
1968 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1969 mac80211_hwsim_bcn_en_iter, &count);
1970 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1973 hrtimer_cancel(&data->beacon_timer);
1974 data->beacon_int = 0;
1979 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1980 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1981 info->use_cts_prot);
1984 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1985 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1986 info->use_short_preamble);
1989 if (changed & BSS_CHANGED_ERP_SLOT) {
1990 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1993 if (changed & BSS_CHANGED_HT) {
1994 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1995 info->ht_operation_mode);
1998 if (changed & BSS_CHANGED_BASIC_RATES) {
1999 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
2000 (unsigned long long) info->basic_rates);
2003 if (changed & BSS_CHANGED_TXPOWER)
2004 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
2007 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2008 struct ieee80211_vif *vif,
2009 struct ieee80211_sta *sta)
2011 hwsim_check_magic(vif);
2012 hwsim_set_sta_magic(sta);
2017 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2018 struct ieee80211_vif *vif,
2019 struct ieee80211_sta *sta)
2021 hwsim_check_magic(vif);
2022 hwsim_clear_sta_magic(sta);
2027 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2028 struct ieee80211_vif *vif,
2029 enum sta_notify_cmd cmd,
2030 struct ieee80211_sta *sta)
2032 hwsim_check_magic(vif);
2035 case STA_NOTIFY_SLEEP:
2036 case STA_NOTIFY_AWAKE:
2037 /* TODO: make good use of these flags */
2040 WARN(1, "Invalid sta notify: %d\n", cmd);
2045 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2046 struct ieee80211_sta *sta,
2049 hwsim_check_sta_magic(sta);
2053 static int mac80211_hwsim_conf_tx(
2054 struct ieee80211_hw *hw,
2055 struct ieee80211_vif *vif, u16 queue,
2056 const struct ieee80211_tx_queue_params *params)
2058 wiphy_dbg(hw->wiphy,
2059 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2061 params->txop, params->cw_min,
2062 params->cw_max, params->aifs);
2066 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2067 struct survey_info *survey)
2069 struct mac80211_hwsim_data *hwsim = hw->priv;
2071 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2074 mutex_lock(&hwsim->mutex);
2075 survey->channel = hwsim->survey_data[idx].channel;
2076 if (!survey->channel) {
2077 mutex_unlock(&hwsim->mutex);
2082 * Magically conjured dummy values --- this is only ok for simulated hardware.
2084 * A real driver which cannot determine real values noise MUST NOT
2085 * report any, especially not a magically conjured ones :-)
2087 survey->filled = SURVEY_INFO_NOISE_DBM |
2089 SURVEY_INFO_TIME_BUSY;
2090 survey->noise = -92;
2092 jiffies_to_msecs(hwsim->survey_data[idx].end -
2093 hwsim->survey_data[idx].start);
2094 /* report 12.5% of channel time is used */
2095 survey->time_busy = survey->time/8;
2096 mutex_unlock(&hwsim->mutex);
2101 #ifdef CONFIG_NL80211_TESTMODE
2103 * This section contains example code for using netlink
2104 * attributes with the testmode command in nl80211.
2107 /* These enums need to be kept in sync with userspace */
2108 enum hwsim_testmode_attr {
2109 __HWSIM_TM_ATTR_INVALID = 0,
2110 HWSIM_TM_ATTR_CMD = 1,
2111 HWSIM_TM_ATTR_PS = 2,
2114 __HWSIM_TM_ATTR_AFTER_LAST,
2115 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
2118 enum hwsim_testmode_cmd {
2119 HWSIM_TM_CMD_SET_PS = 0,
2120 HWSIM_TM_CMD_GET_PS = 1,
2121 HWSIM_TM_CMD_STOP_QUEUES = 2,
2122 HWSIM_TM_CMD_WAKE_QUEUES = 3,
2125 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2126 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2127 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2130 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2131 struct ieee80211_vif *vif,
2132 void *data, int len)
2134 struct mac80211_hwsim_data *hwsim = hw->priv;
2135 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2136 struct sk_buff *skb;
2139 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2140 hwsim_testmode_policy, NULL);
2144 if (!tb[HWSIM_TM_ATTR_CMD])
2147 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2148 case HWSIM_TM_CMD_SET_PS:
2149 if (!tb[HWSIM_TM_ATTR_PS])
2151 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2152 return hwsim_fops_ps_write(hwsim, ps);
2153 case HWSIM_TM_CMD_GET_PS:
2154 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2155 nla_total_size(sizeof(u32)));
2158 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2159 goto nla_put_failure;
2160 return cfg80211_testmode_reply(skb);
2161 case HWSIM_TM_CMD_STOP_QUEUES:
2162 ieee80211_stop_queues(hw);
2164 case HWSIM_TM_CMD_WAKE_QUEUES:
2165 ieee80211_wake_queues(hw);
2177 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2178 struct ieee80211_vif *vif,
2179 struct ieee80211_ampdu_params *params)
2181 struct ieee80211_sta *sta = params->sta;
2182 enum ieee80211_ampdu_mlme_action action = params->action;
2183 u16 tid = params->tid;
2186 case IEEE80211_AMPDU_TX_START:
2187 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2188 case IEEE80211_AMPDU_TX_STOP_CONT:
2189 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2190 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2191 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2193 case IEEE80211_AMPDU_TX_OPERATIONAL:
2195 case IEEE80211_AMPDU_RX_START:
2196 case IEEE80211_AMPDU_RX_STOP:
2205 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2206 struct ieee80211_vif *vif,
2207 u32 queues, bool drop)
2209 /* Not implemented, queues only on kernel side */
2212 static void hw_scan_work(struct work_struct *work)
2214 struct mac80211_hwsim_data *hwsim =
2215 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2216 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2219 mutex_lock(&hwsim->mutex);
2220 if (hwsim->scan_chan_idx >= req->n_channels) {
2221 struct cfg80211_scan_info info = {
2225 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2226 ieee80211_scan_completed(hwsim->hw, &info);
2227 hwsim->hw_scan_request = NULL;
2228 hwsim->hw_scan_vif = NULL;
2229 hwsim->tmp_chan = NULL;
2230 mutex_unlock(&hwsim->mutex);
2231 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2236 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2237 req->channels[hwsim->scan_chan_idx]->center_freq);
2239 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2240 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2241 IEEE80211_CHAN_RADAR) ||
2247 for (i = 0; i < req->n_ssids; i++) {
2248 struct sk_buff *probe;
2249 struct ieee80211_mgmt *mgmt;
2251 probe = ieee80211_probereq_get(hwsim->hw,
2254 req->ssids[i].ssid_len,
2259 mgmt = (struct ieee80211_mgmt *) probe->data;
2260 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2261 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2264 skb_put_data(probe, req->ie, req->ie_len);
2267 if (!ieee80211_tx_prepare_skb(hwsim->hw,
2270 hwsim->tmp_chan->band,
2278 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2284 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2285 msecs_to_jiffies(dwell));
2286 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2287 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2288 hwsim->survey_data[hwsim->scan_chan_idx].end =
2289 jiffies + msecs_to_jiffies(dwell);
2290 hwsim->scan_chan_idx++;
2291 mutex_unlock(&hwsim->mutex);
2294 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2295 struct ieee80211_vif *vif,
2296 struct ieee80211_scan_request *hw_req)
2298 struct mac80211_hwsim_data *hwsim = hw->priv;
2299 struct cfg80211_scan_request *req = &hw_req->req;
2301 mutex_lock(&hwsim->mutex);
2302 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2303 mutex_unlock(&hwsim->mutex);
2306 hwsim->hw_scan_request = req;
2307 hwsim->hw_scan_vif = vif;
2308 hwsim->scan_chan_idx = 0;
2309 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2310 get_random_mask_addr(hwsim->scan_addr,
2311 hw_req->req.mac_addr,
2312 hw_req->req.mac_addr_mask);
2314 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2315 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2316 mutex_unlock(&hwsim->mutex);
2318 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2319 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2321 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2326 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2327 struct ieee80211_vif *vif)
2329 struct mac80211_hwsim_data *hwsim = hw->priv;
2330 struct cfg80211_scan_info info = {
2334 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2336 cancel_delayed_work_sync(&hwsim->hw_scan);
2338 mutex_lock(&hwsim->mutex);
2339 ieee80211_scan_completed(hwsim->hw, &info);
2340 hwsim->tmp_chan = NULL;
2341 hwsim->hw_scan_request = NULL;
2342 hwsim->hw_scan_vif = NULL;
2343 mutex_unlock(&hwsim->mutex);
2346 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2347 struct ieee80211_vif *vif,
2350 struct mac80211_hwsim_data *hwsim = hw->priv;
2352 mutex_lock(&hwsim->mutex);
2354 if (hwsim->scanning) {
2355 pr_debug("two hwsim sw_scans detected!\n");
2359 pr_debug("hwsim sw_scan request, prepping stuff\n");
2361 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2362 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2363 hwsim->scanning = true;
2364 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2367 mutex_unlock(&hwsim->mutex);
2370 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2371 struct ieee80211_vif *vif)
2373 struct mac80211_hwsim_data *hwsim = hw->priv;
2375 mutex_lock(&hwsim->mutex);
2377 pr_debug("hwsim sw_scan_complete\n");
2378 hwsim->scanning = false;
2379 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2380 eth_zero_addr(hwsim->scan_addr);
2382 mutex_unlock(&hwsim->mutex);
2385 static void hw_roc_start(struct work_struct *work)
2387 struct mac80211_hwsim_data *hwsim =
2388 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2390 mutex_lock(&hwsim->mutex);
2392 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2393 hwsim->tmp_chan = hwsim->roc_chan;
2394 ieee80211_ready_on_channel(hwsim->hw);
2396 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2397 msecs_to_jiffies(hwsim->roc_duration));
2399 mutex_unlock(&hwsim->mutex);
2402 static void hw_roc_done(struct work_struct *work)
2404 struct mac80211_hwsim_data *hwsim =
2405 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2407 mutex_lock(&hwsim->mutex);
2408 ieee80211_remain_on_channel_expired(hwsim->hw);
2409 hwsim->tmp_chan = NULL;
2410 mutex_unlock(&hwsim->mutex);
2412 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2415 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2416 struct ieee80211_vif *vif,
2417 struct ieee80211_channel *chan,
2419 enum ieee80211_roc_type type)
2421 struct mac80211_hwsim_data *hwsim = hw->priv;
2423 mutex_lock(&hwsim->mutex);
2424 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2425 mutex_unlock(&hwsim->mutex);
2429 hwsim->roc_chan = chan;
2430 hwsim->roc_duration = duration;
2431 mutex_unlock(&hwsim->mutex);
2433 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2434 chan->center_freq, duration);
2435 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2440 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2441 struct ieee80211_vif *vif)
2443 struct mac80211_hwsim_data *hwsim = hw->priv;
2445 cancel_delayed_work_sync(&hwsim->roc_start);
2446 cancel_delayed_work_sync(&hwsim->roc_done);
2448 mutex_lock(&hwsim->mutex);
2449 hwsim->tmp_chan = NULL;
2450 mutex_unlock(&hwsim->mutex);
2452 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2457 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2458 struct ieee80211_chanctx_conf *ctx)
2460 struct mac80211_hwsim_data *hwsim = hw->priv;
2462 mutex_lock(&hwsim->mutex);
2463 hwsim->chanctx = ctx;
2464 mutex_unlock(&hwsim->mutex);
2465 hwsim_set_chanctx_magic(ctx);
2466 wiphy_dbg(hw->wiphy,
2467 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2468 ctx->def.chan->center_freq, ctx->def.width,
2469 ctx->def.center_freq1, ctx->def.center_freq2);
2473 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2474 struct ieee80211_chanctx_conf *ctx)
2476 struct mac80211_hwsim_data *hwsim = hw->priv;
2478 mutex_lock(&hwsim->mutex);
2479 hwsim->chanctx = NULL;
2480 mutex_unlock(&hwsim->mutex);
2481 wiphy_dbg(hw->wiphy,
2482 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2483 ctx->def.chan->center_freq, ctx->def.width,
2484 ctx->def.center_freq1, ctx->def.center_freq2);
2485 hwsim_check_chanctx_magic(ctx);
2486 hwsim_clear_chanctx_magic(ctx);
2489 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2490 struct ieee80211_chanctx_conf *ctx,
2493 struct mac80211_hwsim_data *hwsim = hw->priv;
2495 mutex_lock(&hwsim->mutex);
2496 hwsim->chanctx = ctx;
2497 mutex_unlock(&hwsim->mutex);
2498 hwsim_check_chanctx_magic(ctx);
2499 wiphy_dbg(hw->wiphy,
2500 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2501 ctx->def.chan->center_freq, ctx->def.width,
2502 ctx->def.center_freq1, ctx->def.center_freq2);
2505 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2506 struct ieee80211_vif *vif,
2507 struct ieee80211_chanctx_conf *ctx)
2509 hwsim_check_magic(vif);
2510 hwsim_check_chanctx_magic(ctx);
2515 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2516 struct ieee80211_vif *vif,
2517 struct ieee80211_chanctx_conf *ctx)
2519 hwsim_check_magic(vif);
2520 hwsim_check_chanctx_magic(ctx);
2523 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2534 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2536 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2537 struct ieee80211_vif *vif,
2540 if (sset == ETH_SS_STATS)
2541 memcpy(data, *mac80211_hwsim_gstrings_stats,
2542 sizeof(mac80211_hwsim_gstrings_stats));
2545 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2546 struct ieee80211_vif *vif, int sset)
2548 if (sset == ETH_SS_STATS)
2549 return MAC80211_HWSIM_SSTATS_LEN;
2553 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2554 struct ieee80211_vif *vif,
2555 struct ethtool_stats *stats, u64 *data)
2557 struct mac80211_hwsim_data *ar = hw->priv;
2560 data[i++] = ar->tx_pkts;
2561 data[i++] = ar->tx_bytes;
2562 data[i++] = ar->rx_pkts;
2563 data[i++] = ar->rx_bytes;
2564 data[i++] = ar->tx_dropped;
2565 data[i++] = ar->tx_failed;
2567 data[i++] = ar->group;
2569 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2572 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2577 #define HWSIM_COMMON_OPS \
2578 .tx = mac80211_hwsim_tx, \
2579 .start = mac80211_hwsim_start, \
2580 .stop = mac80211_hwsim_stop, \
2581 .add_interface = mac80211_hwsim_add_interface, \
2582 .change_interface = mac80211_hwsim_change_interface, \
2583 .remove_interface = mac80211_hwsim_remove_interface, \
2584 .config = mac80211_hwsim_config, \
2585 .configure_filter = mac80211_hwsim_configure_filter, \
2586 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2587 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2588 .sta_add = mac80211_hwsim_sta_add, \
2589 .sta_remove = mac80211_hwsim_sta_remove, \
2590 .sta_notify = mac80211_hwsim_sta_notify, \
2591 .set_tim = mac80211_hwsim_set_tim, \
2592 .conf_tx = mac80211_hwsim_conf_tx, \
2593 .get_survey = mac80211_hwsim_get_survey, \
2594 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2595 .ampdu_action = mac80211_hwsim_ampdu_action, \
2596 .flush = mac80211_hwsim_flush, \
2597 .get_tsf = mac80211_hwsim_get_tsf, \
2598 .set_tsf = mac80211_hwsim_set_tsf, \
2599 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2600 .get_et_stats = mac80211_hwsim_get_et_stats, \
2601 .get_et_strings = mac80211_hwsim_get_et_strings,
2603 static const struct ieee80211_ops mac80211_hwsim_ops = {
2605 .sw_scan_start = mac80211_hwsim_sw_scan,
2606 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2609 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2611 .hw_scan = mac80211_hwsim_hw_scan,
2612 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2613 .sw_scan_start = NULL,
2614 .sw_scan_complete = NULL,
2615 .remain_on_channel = mac80211_hwsim_roc,
2616 .cancel_remain_on_channel = mac80211_hwsim_croc,
2617 .add_chanctx = mac80211_hwsim_add_chanctx,
2618 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2619 .change_chanctx = mac80211_hwsim_change_chanctx,
2620 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2621 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2624 struct hwsim_new_radio_params {
2625 unsigned int channels;
2626 const char *reg_alpha2;
2627 const struct ieee80211_regdomain *regd;
2631 bool destroy_on_close;
2634 const u8 *perm_addr;
2640 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2641 struct genl_info *info)
2644 genl_notify(&hwsim_genl_family, mcast_skb, info,
2645 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2647 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2648 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2651 static int append_radio_msg(struct sk_buff *skb, int id,
2652 struct hwsim_new_radio_params *param)
2656 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2660 if (param->channels) {
2661 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2666 if (param->reg_alpha2) {
2667 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2676 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2677 if (hwsim_world_regdom_custom[i] != param->regd)
2680 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2687 if (param->reg_strict) {
2688 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2693 if (param->p2p_device) {
2694 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2699 if (param->use_chanctx) {
2700 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2705 if (param->hwname) {
2706 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2707 strlen(param->hwname), param->hwname);
2715 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2716 struct hwsim_new_radio_params *param)
2718 struct sk_buff *mcast_skb;
2721 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2725 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2726 HWSIM_CMD_NEW_RADIO);
2730 if (append_radio_msg(mcast_skb, id, param) < 0)
2733 genlmsg_end(mcast_skb, data);
2735 hwsim_mcast_config_msg(mcast_skb, info);
2739 nlmsg_free(mcast_skb);
2742 static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2744 /* TODO: should we support other types, e.g., P2P?*/
2745 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2746 BIT(NL80211_IFTYPE_AP),
2751 IEEE80211_HE_MAC_CAP0_HTC_HE,
2753 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2754 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2756 IEEE80211_HE_MAC_CAP2_BSR |
2757 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2758 IEEE80211_HE_MAC_CAP2_ACK_EN,
2760 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2761 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2762 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2764 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2765 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2766 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2767 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2769 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2770 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2771 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2772 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2773 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2775 /* Leave all the other PHY capability bytes
2776 * unset, as DCM, beam forming, RU and PPE
2777 * threshold information are not supported
2780 .he_mcs_nss_supp = {
2781 .rx_mcs_80 = cpu_to_le16(0xfffa),
2782 .tx_mcs_80 = cpu_to_le16(0xfffa),
2783 .rx_mcs_160 = cpu_to_le16(0xffff),
2784 .tx_mcs_160 = cpu_to_le16(0xffff),
2785 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2786 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2790 #ifdef CONFIG_MAC80211_MESH
2792 /* TODO: should we support other types, e.g., IBSS?*/
2793 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2798 IEEE80211_HE_MAC_CAP0_HTC_HE,
2800 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2802 IEEE80211_HE_MAC_CAP2_ACK_EN,
2804 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2805 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2806 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2808 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2809 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2810 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2811 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2812 .phy_cap_info[2] = 0,
2814 /* Leave all the other PHY capability bytes
2815 * unset, as DCM, beam forming, RU and PPE
2816 * threshold information are not supported
2819 .he_mcs_nss_supp = {
2820 .rx_mcs_80 = cpu_to_le16(0xfffa),
2821 .tx_mcs_80 = cpu_to_le16(0xfffa),
2822 .rx_mcs_160 = cpu_to_le16(0xffff),
2823 .tx_mcs_160 = cpu_to_le16(0xffff),
2824 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2825 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2832 static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2834 /* TODO: should we support other types, e.g., P2P?*/
2835 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2836 BIT(NL80211_IFTYPE_AP),
2841 IEEE80211_HE_MAC_CAP0_HTC_HE,
2843 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2844 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2846 IEEE80211_HE_MAC_CAP2_BSR |
2847 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2848 IEEE80211_HE_MAC_CAP2_ACK_EN,
2850 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2851 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2852 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2854 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2855 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2856 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2858 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2859 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2860 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2861 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2863 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2864 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2865 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2866 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2867 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2869 /* Leave all the other PHY capability bytes
2870 * unset, as DCM, beam forming, RU and PPE
2871 * threshold information are not supported
2874 .he_mcs_nss_supp = {
2875 .rx_mcs_80 = cpu_to_le16(0xfffa),
2876 .tx_mcs_80 = cpu_to_le16(0xfffa),
2877 .rx_mcs_160 = cpu_to_le16(0xfffa),
2878 .tx_mcs_160 = cpu_to_le16(0xfffa),
2879 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2880 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2884 #ifdef CONFIG_MAC80211_MESH
2886 /* TODO: should we support other types, e.g., IBSS?*/
2887 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2892 IEEE80211_HE_MAC_CAP0_HTC_HE,
2894 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2896 IEEE80211_HE_MAC_CAP2_ACK_EN,
2898 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2899 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2900 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2902 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2903 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2904 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2906 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2907 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2908 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2909 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2910 .phy_cap_info[2] = 0,
2912 /* Leave all the other PHY capability bytes
2913 * unset, as DCM, beam forming, RU and PPE
2914 * threshold information are not supported
2917 .he_mcs_nss_supp = {
2918 .rx_mcs_80 = cpu_to_le16(0xfffa),
2919 .tx_mcs_80 = cpu_to_le16(0xfffa),
2920 .rx_mcs_160 = cpu_to_le16(0xfffa),
2921 .tx_mcs_160 = cpu_to_le16(0xfffa),
2922 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2923 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2930 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2934 if (sband->band == NL80211_BAND_2GHZ) {
2935 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2936 sband->iftype_data =
2937 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2938 } else if (sband->band == NL80211_BAND_5GHZ) {
2939 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2940 sband->iftype_data =
2941 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2946 sband->n_iftype_data = n_iftype_data;
2949 #ifdef CONFIG_MAC80211_MESH
2950 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2952 #define HWSIM_MESH_BIT 0
2955 #define HWSIM_DEFAULT_IF_LIMIT \
2956 (BIT(NL80211_IFTYPE_STATION) | \
2957 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2958 BIT(NL80211_IFTYPE_AP) | \
2959 BIT(NL80211_IFTYPE_P2P_GO) | \
2962 #define HWSIM_IFTYPE_SUPPORT_MASK \
2963 (BIT(NL80211_IFTYPE_STATION) | \
2964 BIT(NL80211_IFTYPE_AP) | \
2965 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2966 BIT(NL80211_IFTYPE_P2P_GO) | \
2967 BIT(NL80211_IFTYPE_ADHOC) | \
2968 BIT(NL80211_IFTYPE_MESH_POINT) | \
2969 BIT(NL80211_IFTYPE_OCB))
2971 static int mac80211_hwsim_new_radio(struct genl_info *info,
2972 struct hwsim_new_radio_params *param)
2976 struct mac80211_hwsim_data *data;
2977 struct ieee80211_hw *hw;
2978 enum nl80211_band band;
2979 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2984 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2987 spin_lock_bh(&hwsim_radio_lock);
2988 idx = hwsim_radio_idx++;
2989 spin_unlock_bh(&hwsim_radio_lock);
2991 if (param->use_chanctx)
2992 ops = &mac80211_hwsim_mchan_ops;
2993 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2995 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3000 /* ieee80211_alloc_hw_nm may have used a default name */
3001 param->hwname = wiphy_name(hw->wiphy);
3004 net = genl_info_net(info);
3007 wiphy_net_set(hw->wiphy, net);
3012 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3013 if (IS_ERR(data->dev)) {
3015 "mac80211_hwsim: device_create failed (%ld)\n",
3016 PTR_ERR(data->dev));
3018 goto failed_drvdata;
3020 data->dev->driver = &mac80211_hwsim_driver.driver;
3021 err = device_bind_driver(data->dev);
3023 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3028 skb_queue_head_init(&data->pending);
3030 SET_IEEE80211_DEV(hw, data->dev);
3031 if (!param->perm_addr) {
3032 eth_zero_addr(addr);
3036 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3037 /* Why need here second address ? */
3038 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3039 data->addresses[1].addr[0] |= 0x40;
3040 hw->wiphy->n_addresses = 2;
3041 hw->wiphy->addresses = data->addresses;
3042 /* possible address clash is checked at hash table insertion */
3044 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3045 /* compatibility with automatically generated mac addr */
3046 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3047 hw->wiphy->n_addresses = 2;
3048 hw->wiphy->addresses = data->addresses;
3051 data->channels = param->channels;
3052 data->use_chanctx = param->use_chanctx;
3054 data->destroy_on_close = param->destroy_on_close;
3056 data->portid = info->snd_portid;
3058 /* setup interface limits, only on interface types we support */
3059 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3060 data->if_limits[n_limits].max = 1;
3061 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3065 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3066 data->if_limits[n_limits].max = 2048;
3068 * For this case, we may only support a subset of
3069 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3070 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3072 data->if_limits[n_limits].types =
3073 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3077 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3078 data->if_limits[n_limits].max = 1;
3079 data->if_limits[n_limits].types =
3080 BIT(NL80211_IFTYPE_P2P_DEVICE);
3084 if (data->use_chanctx) {
3085 hw->wiphy->max_scan_ssids = 255;
3086 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3087 hw->wiphy->max_remain_on_channel_duration = 1000;
3088 data->if_combination.radar_detect_widths = 0;
3089 data->if_combination.num_different_channels = data->channels;
3090 data->chanctx = NULL;
3092 data->if_combination.num_different_channels = 1;
3093 data->if_combination.radar_detect_widths =
3094 BIT(NL80211_CHAN_WIDTH_5) |
3095 BIT(NL80211_CHAN_WIDTH_10) |
3096 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3097 BIT(NL80211_CHAN_WIDTH_20) |
3098 BIT(NL80211_CHAN_WIDTH_40) |
3099 BIT(NL80211_CHAN_WIDTH_80) |
3100 BIT(NL80211_CHAN_WIDTH_160);
3108 data->if_combination.max_interfaces = 0;
3109 for (i = 0; i < n_limits; i++)
3110 data->if_combination.max_interfaces +=
3111 data->if_limits[i].max;
3113 data->if_combination.n_limits = n_limits;
3114 data->if_combination.limits = data->if_limits;
3117 * If we actually were asked to support combinations,
3118 * advertise them - if there's only a single thing like
3119 * only IBSS then don't advertise it as combinations.
3121 if (data->if_combination.max_interfaces > 1) {
3122 hw->wiphy->iface_combinations = &data->if_combination;
3123 hw->wiphy->n_iface_combinations = 1;
3126 if (param->ciphers) {
3127 memcpy(data->ciphers, param->ciphers,
3128 param->n_ciphers * sizeof(u32));
3129 hw->wiphy->cipher_suites = data->ciphers;
3130 hw->wiphy->n_cipher_suites = param->n_ciphers;
3133 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3134 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3135 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3138 hw->offchannel_tx_hw_queue = 4;
3140 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3141 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3142 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3143 ieee80211_hw_set(hw, QUEUE_CONTROL);
3144 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3145 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3146 ieee80211_hw_set(hw, MFP_CAPABLE);
3147 ieee80211_hw_set(hw, SIGNAL_DBM);
3148 ieee80211_hw_set(hw, SUPPORTS_PS);
3149 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3150 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3151 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3152 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3154 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3155 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3157 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3158 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3159 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3160 WIPHY_FLAG_AP_UAPSD |
3161 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3162 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3163 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3164 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3165 NL80211_FEATURE_STATIC_SMPS |
3166 NL80211_FEATURE_DYNAMIC_SMPS |
3167 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3168 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3169 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3170 wiphy_ext_feature_set(hw->wiphy,
3171 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3172 wiphy_ext_feature_set(hw->wiphy,
3173 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3175 hw->wiphy->interface_modes = param->iftypes;
3177 /* ask mac80211 to reserve space for magic */
3178 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3179 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3180 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3182 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3183 sizeof(hwsim_channels_2ghz));
3184 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3185 sizeof(hwsim_channels_5ghz));
3186 memcpy(data->channels_s1g, hwsim_channels_s1g,
3187 sizeof(hwsim_channels_s1g));
3188 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3190 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3191 struct ieee80211_supported_band *sband = &data->bands[band];
3196 case NL80211_BAND_2GHZ:
3197 sband->channels = data->channels_2ghz;
3198 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3199 sband->bitrates = data->rates;
3200 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3202 case NL80211_BAND_5GHZ:
3203 sband->channels = data->channels_5ghz;
3204 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3205 sband->bitrates = data->rates + 4;
3206 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3208 sband->vht_cap.vht_supported = true;
3209 sband->vht_cap.cap =
3210 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3211 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3212 IEEE80211_VHT_CAP_RXLDPC |
3213 IEEE80211_VHT_CAP_SHORT_GI_80 |
3214 IEEE80211_VHT_CAP_SHORT_GI_160 |
3215 IEEE80211_VHT_CAP_TXSTBC |
3216 IEEE80211_VHT_CAP_RXSTBC_4 |
3217 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3218 sband->vht_cap.vht_mcs.rx_mcs_map =
3219 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3220 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3221 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3222 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3223 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3224 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3225 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3226 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3227 sband->vht_cap.vht_mcs.tx_mcs_map =
3228 sband->vht_cap.vht_mcs.rx_mcs_map;
3230 case NL80211_BAND_S1GHZ:
3231 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3232 sizeof(sband->s1g_cap));
3233 sband->channels = data->channels_s1g;
3234 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3240 sband->ht_cap.ht_supported = true;
3241 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3242 IEEE80211_HT_CAP_GRN_FLD |
3243 IEEE80211_HT_CAP_SGI_20 |
3244 IEEE80211_HT_CAP_SGI_40 |
3245 IEEE80211_HT_CAP_DSSSCCK40;
3246 sband->ht_cap.ampdu_factor = 0x3;
3247 sband->ht_cap.ampdu_density = 0x6;
3248 memset(&sband->ht_cap.mcs, 0,
3249 sizeof(sband->ht_cap.mcs));
3250 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3251 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3252 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3254 mac80211_hwsim_he_capab(sband);
3256 hw->wiphy->bands[band] = sband;
3259 /* By default all radios belong to the first group */
3261 mutex_init(&data->mutex);
3263 data->netgroup = hwsim_net_get_netgroup(net);
3264 data->wmediumd = hwsim_net_get_wmediumd(net);
3266 /* Enable frame retransmissions for lossy channels */
3268 hw->max_rate_tries = 11;
3270 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3271 hw->wiphy->n_vendor_commands =
3272 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3273 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3274 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3276 if (param->reg_strict)
3277 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3279 data->regd = param->regd;
3280 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3281 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3282 /* give the regulatory workqueue a chance to run */
3283 schedule_timeout_interruptible(1);
3287 ieee80211_hw_set(hw, NO_AUTO_VIF);
3289 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3291 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3292 HRTIMER_MODE_ABS_SOFT);
3293 data->beacon_timer.function = mac80211_hwsim_beacon;
3295 err = ieee80211_register_hw(hw);
3297 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3302 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3304 if (param->reg_alpha2) {
3305 data->alpha2[0] = param->reg_alpha2[0];
3306 data->alpha2[1] = param->reg_alpha2[1];
3307 regulatory_hint(hw->wiphy, param->reg_alpha2);
3310 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3311 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3312 debugfs_create_file("group", 0666, data->debugfs, data,
3314 if (!data->use_chanctx)
3315 debugfs_create_file("dfs_simulate_radar", 0222,
3317 data, &hwsim_simulate_radar);
3319 spin_lock_bh(&hwsim_radio_lock);
3320 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3324 GENL_SET_ERR_MSG(info, "perm addr already present");
3325 NL_SET_BAD_ATTR(info->extack,
3326 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3328 spin_unlock_bh(&hwsim_radio_lock);
3329 goto failed_final_insert;
3332 list_add_tail(&data->list, &hwsim_radios);
3333 hwsim_radios_generation++;
3334 spin_unlock_bh(&hwsim_radio_lock);
3336 hwsim_mcast_new_radio(idx, info, param);
3340 failed_final_insert:
3341 debugfs_remove_recursive(data->debugfs);
3342 ieee80211_unregister_hw(data->hw);
3344 device_release_driver(data->dev);
3346 device_unregister(data->dev);
3348 ieee80211_free_hw(hw);
3353 static void hwsim_mcast_del_radio(int id, const char *hwname,
3354 struct genl_info *info)
3356 struct sk_buff *skb;
3360 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3364 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3365 HWSIM_CMD_DEL_RADIO);
3369 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3373 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3378 genlmsg_end(skb, data);
3380 hwsim_mcast_config_msg(skb, info);
3388 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3390 struct genl_info *info)
3392 hwsim_mcast_del_radio(data->idx, hwname, info);
3393 debugfs_remove_recursive(data->debugfs);
3394 ieee80211_unregister_hw(data->hw);
3395 device_release_driver(data->dev);
3396 device_unregister(data->dev);
3397 ieee80211_free_hw(data->hw);
3400 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3401 struct mac80211_hwsim_data *data,
3402 u32 portid, u32 seq,
3403 struct netlink_callback *cb, int flags)
3406 struct hwsim_new_radio_params param = { };
3407 int res = -EMSGSIZE;
3409 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3410 HWSIM_CMD_GET_RADIO);
3415 genl_dump_check_consistent(cb, hdr);
3417 if (data->alpha2[0] && data->alpha2[1])
3418 param.reg_alpha2 = data->alpha2;
3420 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3421 REGULATORY_STRICT_REG);
3422 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3423 BIT(NL80211_IFTYPE_P2P_DEVICE));
3424 param.use_chanctx = data->use_chanctx;
3425 param.regd = data->regd;
3426 param.channels = data->channels;
3427 param.hwname = wiphy_name(data->hw->wiphy);
3429 res = append_radio_msg(skb, data->idx, ¶m);
3433 genlmsg_end(skb, hdr);
3437 genlmsg_cancel(skb, hdr);
3441 static void mac80211_hwsim_free(void)
3443 struct mac80211_hwsim_data *data;
3445 spin_lock_bh(&hwsim_radio_lock);
3446 while ((data = list_first_entry_or_null(&hwsim_radios,
3447 struct mac80211_hwsim_data,
3449 list_del(&data->list);
3450 spin_unlock_bh(&hwsim_radio_lock);
3451 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3453 spin_lock_bh(&hwsim_radio_lock);
3455 spin_unlock_bh(&hwsim_radio_lock);
3456 class_destroy(hwsim_class);
3459 static const struct net_device_ops hwsim_netdev_ops = {
3460 .ndo_start_xmit = hwsim_mon_xmit,
3461 .ndo_set_mac_address = eth_mac_addr,
3462 .ndo_validate_addr = eth_validate_addr,
3465 static void hwsim_mon_setup(struct net_device *dev)
3467 dev->netdev_ops = &hwsim_netdev_ops;
3468 dev->needs_free_netdev = true;
3470 dev->priv_flags |= IFF_NO_QUEUE;
3471 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3472 eth_zero_addr(dev->dev_addr);
3473 dev->dev_addr[0] = 0x12;
3476 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3478 return rhashtable_lookup_fast(&hwsim_radios_rht,
3483 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3485 struct mac80211_hwsim_data *data;
3487 hwsim_net_set_wmediumd(net, portid);
3489 spin_lock_bh(&hwsim_radio_lock);
3490 list_for_each_entry(data, &hwsim_radios, list) {
3491 if (data->netgroup == hwsim_net_get_netgroup(net))
3492 data->wmediumd = portid;
3494 spin_unlock_bh(&hwsim_radio_lock);
3497 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3498 struct genl_info *info)
3501 struct ieee80211_hdr *hdr;
3502 struct mac80211_hwsim_data *data2;
3503 struct ieee80211_tx_info *txi;
3504 struct hwsim_tx_rate *tx_attempts;
3506 struct sk_buff *skb, *tmp;
3508 unsigned int hwsim_flags;
3510 unsigned long flags;
3513 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3514 !info->attrs[HWSIM_ATTR_FLAGS] ||
3515 !info->attrs[HWSIM_ATTR_COOKIE] ||
3516 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3517 !info->attrs[HWSIM_ATTR_TX_INFO])
3520 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3521 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3522 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3524 data2 = get_hwsim_data_ref_from_addr(src);
3528 if (!hwsim_virtio_enabled) {
3529 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3533 if (info->snd_portid != data2->wmediumd)
3537 /* look for the skb matching the cookie passed back from user */
3538 spin_lock_irqsave(&data2->pending.lock, flags);
3539 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3540 uintptr_t skb_cookie;
3542 txi = IEEE80211_SKB_CB(skb);
3543 skb_cookie = (uintptr_t)txi->rate_driver_data[0];
3545 if (skb_cookie == ret_skb_cookie) {
3546 __skb_unlink(skb, &data2->pending);
3551 spin_unlock_irqrestore(&data2->pending.lock, flags);
3557 /* Tx info received because the frame was broadcasted on user space,
3558 so we get all the necessary info: tx attempts and skb control buff */
3560 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3561 info->attrs[HWSIM_ATTR_TX_INFO]);
3563 /* now send back TX status */
3564 txi = IEEE80211_SKB_CB(skb);
3566 ieee80211_tx_info_clear_status(txi);
3568 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3569 txi->status.rates[i].idx = tx_attempts[i].idx;
3570 txi->status.rates[i].count = tx_attempts[i].count;
3573 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3575 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3576 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3577 if (skb->len >= 16) {
3578 hdr = (struct ieee80211_hdr *) skb->data;
3579 mac80211_hwsim_monitor_ack(data2->channel,
3582 txi->flags |= IEEE80211_TX_STAT_ACK;
3585 if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
3586 txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
3588 ieee80211_tx_status_irqsafe(data2->hw, skb);
3595 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3596 struct genl_info *info)
3598 struct mac80211_hwsim_data *data2;
3599 struct ieee80211_rx_status rx_status;
3600 struct ieee80211_hdr *hdr;
3604 struct sk_buff *skb = NULL;
3605 struct ieee80211_channel *channel = NULL;
3607 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3608 !info->attrs[HWSIM_ATTR_FRAME] ||
3609 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3610 !info->attrs[HWSIM_ATTR_SIGNAL])
3613 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3614 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3615 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3617 /* Allocate new skb here */
3618 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3622 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3626 skb_put_data(skb, frame_data, frame_data_len);
3628 data2 = get_hwsim_data_ref_from_addr(dst);
3632 if (data2->use_chanctx) {
3633 if (data2->tmp_chan)
3634 channel = data2->tmp_chan;
3635 else if (data2->chanctx)
3636 channel = data2->chanctx->def.chan;
3638 channel = data2->channel;
3643 if (!hwsim_virtio_enabled) {
3644 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3648 if (info->snd_portid != data2->wmediumd)
3652 /* check if radio is configured properly */
3654 if ((data2->idle && !data2->tmp_chan) || !data2->started)
3657 /* A frame is received from user space */
3658 memset(&rx_status, 0, sizeof(rx_status));
3659 if (info->attrs[HWSIM_ATTR_FREQ]) {
3660 /* throw away off-channel packets, but allow both the temporary
3661 * ("hw" scan/remain-on-channel) and regular channel, since the
3662 * internal datapath also allows this
3664 mutex_lock(&data2->mutex);
3665 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3667 if (rx_status.freq != channel->center_freq) {
3668 mutex_unlock(&data2->mutex);
3671 mutex_unlock(&data2->mutex);
3673 rx_status.freq = channel->center_freq;
3676 rx_status.band = channel->band;
3677 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3678 if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
3680 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3682 hdr = (void *)skb->data;
3684 if (ieee80211_is_beacon(hdr->frame_control) ||
3685 ieee80211_is_probe_resp(hdr->frame_control))
3686 rx_status.boottime_ns = ktime_get_boottime_ns();
3688 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3690 data2->rx_bytes += skb->len;
3691 ieee80211_rx_irqsafe(data2->hw, skb);
3695 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3701 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3702 struct genl_info *info)
3704 struct net *net = genl_info_net(info);
3705 struct mac80211_hwsim_data *data;
3708 spin_lock_bh(&hwsim_radio_lock);
3709 list_for_each_entry(data, &hwsim_radios, list)
3710 chans = max(chans, data->channels);
3711 spin_unlock_bh(&hwsim_radio_lock);
3713 /* In the future we should revise the userspace API and allow it
3714 * to set a flag that it does support multi-channel, then we can
3715 * let this pass conditionally on the flag.
3716 * For current userspace, prohibit it since it won't work right.
3721 if (hwsim_net_get_wmediumd(net))
3724 hwsim_register_wmediumd(net, info->snd_portid);
3726 pr_debug("mac80211_hwsim: received a REGISTER, "
3727 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3732 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3733 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3737 for (i = 0; i < n_ciphers; i++) {
3741 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3742 if (ciphers[i] == hwsim_ciphers[j]) {
3755 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3757 struct hwsim_new_radio_params param = { 0 };
3758 const char *hwname = NULL;
3761 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3762 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3763 param.channels = channels;
3764 param.destroy_on_close =
3765 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3767 if (info->attrs[HWSIM_ATTR_CHANNELS])
3768 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3770 if (param.channels < 1) {
3771 GENL_SET_ERR_MSG(info, "must have at least one channel");
3775 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3776 GENL_SET_ERR_MSG(info, "too many channels specified");
3780 if (info->attrs[HWSIM_ATTR_NO_VIF])
3781 param.no_vif = true;
3783 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3784 param.use_chanctx = true;
3786 param.use_chanctx = (param.channels > 1);
3788 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3790 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3792 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3793 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3795 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3798 idx = array_index_nospec(idx,
3799 ARRAY_SIZE(hwsim_world_regdom_custom));
3800 param.regd = hwsim_world_regdom_custom[idx];
3803 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3804 if (!is_valid_ether_addr(
3805 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3806 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3807 NL_SET_BAD_ATTR(info->extack,
3808 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3812 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3815 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3817 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3819 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3820 NL_SET_ERR_MSG_ATTR(info->extack,
3821 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3822 "cannot support more iftypes than kernel");
3826 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3829 /* ensure both flag and iftype support is honored */
3830 if (param.p2p_device ||
3831 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3832 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3833 param.p2p_device = true;
3836 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3837 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3840 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3842 if (len % sizeof(u32)) {
3843 NL_SET_ERR_MSG_ATTR(info->extack,
3844 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3845 "bad cipher list length");
3849 param.n_ciphers = len / sizeof(u32);
3851 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3852 NL_SET_ERR_MSG_ATTR(info->extack,
3853 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3854 "too many ciphers specified");
3858 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3859 NL_SET_ERR_MSG_ATTR(info->extack,
3860 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3861 "unsupported ciphers specified");
3866 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3867 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3868 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3872 param.hwname = hwname;
3875 ret = mac80211_hwsim_new_radio(info, ¶m);
3880 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3882 struct mac80211_hwsim_data *data;
3884 const char *hwname = NULL;
3886 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3887 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3888 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3889 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3890 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3897 spin_lock_bh(&hwsim_radio_lock);
3898 list_for_each_entry(data, &hwsim_radios, list) {
3900 if (data->idx != idx)
3904 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3908 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3911 list_del(&data->list);
3912 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3914 hwsim_radios_generation++;
3915 spin_unlock_bh(&hwsim_radio_lock);
3916 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3921 spin_unlock_bh(&hwsim_radio_lock);
3927 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3929 struct mac80211_hwsim_data *data;
3930 struct sk_buff *skb;
3931 int idx, res = -ENODEV;
3933 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3935 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3937 spin_lock_bh(&hwsim_radio_lock);
3938 list_for_each_entry(data, &hwsim_radios, list) {
3939 if (data->idx != idx)
3942 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3945 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3951 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3952 info->snd_seq, NULL, 0);
3958 res = genlmsg_reply(skb, info);
3963 spin_unlock_bh(&hwsim_radio_lock);
3968 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3969 struct netlink_callback *cb)
3971 int last_idx = cb->args[0] - 1;
3972 struct mac80211_hwsim_data *data = NULL;
3976 spin_lock_bh(&hwsim_radio_lock);
3977 cb->seq = hwsim_radios_generation;
3979 if (last_idx >= hwsim_radio_idx-1)
3982 list_for_each_entry(data, &hwsim_radios, list) {
3983 if (data->idx <= last_idx)
3986 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3989 res = mac80211_hwsim_get_radio(skb, data,
3990 NETLINK_CB(cb->skb).portid,
3991 cb->nlh->nlmsg_seq, cb,
3996 last_idx = data->idx;
3999 cb->args[0] = last_idx + 1;
4001 /* list changed, but no new element sent, set interrupted flag */
4002 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4003 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4004 cb->nlh->nlmsg_seq, &hwsim_genl_family,
4005 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4007 genl_dump_check_consistent(cb, hdr);
4008 genlmsg_end(skb, hdr);
4015 spin_unlock_bh(&hwsim_radio_lock);
4016 return res ?: skb->len;
4019 /* Generic Netlink operations array */
4020 static const struct genl_small_ops hwsim_ops[] = {
4022 .cmd = HWSIM_CMD_REGISTER,
4023 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4024 .doit = hwsim_register_received_nl,
4025 .flags = GENL_UNS_ADMIN_PERM,
4028 .cmd = HWSIM_CMD_FRAME,
4029 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4030 .doit = hwsim_cloned_frame_received_nl,
4033 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4034 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4035 .doit = hwsim_tx_info_frame_received_nl,
4038 .cmd = HWSIM_CMD_NEW_RADIO,
4039 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4040 .doit = hwsim_new_radio_nl,
4041 .flags = GENL_UNS_ADMIN_PERM,
4044 .cmd = HWSIM_CMD_DEL_RADIO,
4045 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4046 .doit = hwsim_del_radio_nl,
4047 .flags = GENL_UNS_ADMIN_PERM,
4050 .cmd = HWSIM_CMD_GET_RADIO,
4051 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4052 .doit = hwsim_get_radio_nl,
4053 .dumpit = hwsim_dump_radio_nl,
4057 static struct genl_family hwsim_genl_family __ro_after_init = {
4058 .name = "MAC80211_HWSIM",
4060 .maxattr = HWSIM_ATTR_MAX,
4061 .policy = hwsim_genl_policy,
4063 .module = THIS_MODULE,
4064 .small_ops = hwsim_ops,
4065 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4066 .mcgrps = hwsim_mcgrps,
4067 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4070 static void remove_user_radios(u32 portid)
4072 struct mac80211_hwsim_data *entry, *tmp;
4075 spin_lock_bh(&hwsim_radio_lock);
4076 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4077 if (entry->destroy_on_close && entry->portid == portid) {
4078 list_move(&entry->list, &list);
4079 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4081 hwsim_radios_generation++;
4084 spin_unlock_bh(&hwsim_radio_lock);
4086 list_for_each_entry_safe(entry, tmp, &list, list) {
4087 list_del(&entry->list);
4088 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4093 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4094 unsigned long state,
4097 struct netlink_notify *notify = _notify;
4099 if (state != NETLINK_URELEASE)
4102 remove_user_radios(notify->portid);
4104 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4105 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4106 " socket, switching to perfect channel medium\n");
4107 hwsim_register_wmediumd(notify->net, 0);
4113 static struct notifier_block hwsim_netlink_notifier = {
4114 .notifier_call = mac80211_hwsim_netlink_notify,
4117 static int __init hwsim_init_netlink(void)
4121 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4123 rc = genl_register_family(&hwsim_genl_family);
4127 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4129 genl_unregister_family(&hwsim_genl_family);
4136 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4140 static __net_init int hwsim_init_net(struct net *net)
4142 return hwsim_net_set_netgroup(net);
4145 static void __net_exit hwsim_exit_net(struct net *net)
4147 struct mac80211_hwsim_data *data, *tmp;
4150 spin_lock_bh(&hwsim_radio_lock);
4151 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4152 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4155 /* Radios created in init_net are returned to init_net. */
4156 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4159 list_move(&data->list, &list);
4160 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4162 hwsim_radios_generation++;
4164 spin_unlock_bh(&hwsim_radio_lock);
4166 list_for_each_entry_safe(data, tmp, &list, list) {
4167 list_del(&data->list);
4168 mac80211_hwsim_del_radio(data,
4169 wiphy_name(data->hw->wiphy),
4173 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4176 static struct pernet_operations hwsim_net_ops = {
4177 .init = hwsim_init_net,
4178 .exit = hwsim_exit_net,
4179 .id = &hwsim_net_id,
4180 .size = sizeof(struct hwsim_net),
4183 static void hwsim_exit_netlink(void)
4185 /* unregister the notifier */
4186 netlink_unregister_notifier(&hwsim_netlink_notifier);
4187 /* unregister the family */
4188 genl_unregister_family(&hwsim_genl_family);
4191 #if IS_REACHABLE(CONFIG_VIRTIO)
4192 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4195 struct sk_buff *skb;
4196 unsigned long flags;
4198 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4199 while ((skb = virtqueue_get_buf(vq, &len)))
4201 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4204 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4206 struct nlmsghdr *nlh;
4207 struct genlmsghdr *gnlh;
4208 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4209 struct genl_info info = {};
4212 nlh = nlmsg_hdr(skb);
4213 gnlh = nlmsg_data(nlh);
4215 if (skb->len < nlh->nlmsg_len)
4218 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4219 hwsim_genl_policy, NULL);
4221 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4227 switch (gnlh->cmd) {
4228 case HWSIM_CMD_FRAME:
4229 hwsim_cloned_frame_received_nl(skb, &info);
4231 case HWSIM_CMD_TX_INFO_FRAME:
4232 hwsim_tx_info_frame_received_nl(skb, &info);
4235 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4241 static void hwsim_virtio_rx_work(struct work_struct *work)
4243 struct virtqueue *vq;
4245 struct sk_buff *skb;
4246 struct scatterlist sg[1];
4248 unsigned long flags;
4250 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4251 if (!hwsim_virtio_enabled)
4254 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4257 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4259 skb->data = skb->head;
4260 skb_reset_tail_pointer(skb);
4262 hwsim_virtio_handle_cmd(skb);
4264 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4265 if (!hwsim_virtio_enabled) {
4269 vq = hwsim_vqs[HWSIM_VQ_RX];
4270 sg_init_one(sg, skb->head, skb_end_offset(skb));
4271 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4272 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4276 schedule_work(&hwsim_virtio_rx);
4279 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4282 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4284 schedule_work(&hwsim_virtio_rx);
4287 static int init_vqs(struct virtio_device *vdev)
4289 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4290 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4291 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4293 const char *names[HWSIM_NUM_VQS] = {
4294 [HWSIM_VQ_TX] = "tx",
4295 [HWSIM_VQ_RX] = "rx",
4298 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4299 hwsim_vqs, callbacks, names, NULL);
4302 static int fill_vq(struct virtqueue *vq)
4305 struct sk_buff *skb;
4306 struct scatterlist sg[1];
4308 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4309 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4313 sg_init_one(sg, skb->head, skb_end_offset(skb));
4314 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4324 static void remove_vqs(struct virtio_device *vdev)
4328 vdev->config->reset(vdev);
4330 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4331 struct virtqueue *vq = hwsim_vqs[i];
4332 struct sk_buff *skb;
4334 while ((skb = virtqueue_detach_unused_buf(vq)))
4338 vdev->config->del_vqs(vdev);
4341 static int hwsim_virtio_probe(struct virtio_device *vdev)
4344 unsigned long flags;
4346 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4347 if (hwsim_virtio_enabled) {
4348 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4351 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4353 err = init_vqs(vdev);
4357 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4361 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4362 hwsim_virtio_enabled = true;
4363 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4365 schedule_work(&hwsim_virtio_rx);
4373 static void hwsim_virtio_remove(struct virtio_device *vdev)
4375 hwsim_virtio_enabled = false;
4377 cancel_work_sync(&hwsim_virtio_rx);
4382 /* MAC80211_HWSIM virtio device id table */
4383 static const struct virtio_device_id id_table[] = {
4384 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4387 MODULE_DEVICE_TABLE(virtio, id_table);
4389 static struct virtio_driver virtio_hwsim = {
4390 .driver.name = KBUILD_MODNAME,
4391 .driver.owner = THIS_MODULE,
4392 .id_table = id_table,
4393 .probe = hwsim_virtio_probe,
4394 .remove = hwsim_virtio_remove,
4397 static int hwsim_register_virtio_driver(void)
4399 spin_lock_init(&hwsim_virtio_lock);
4401 return register_virtio_driver(&virtio_hwsim);
4404 static void hwsim_unregister_virtio_driver(void)
4406 unregister_virtio_driver(&virtio_hwsim);
4409 static inline int hwsim_register_virtio_driver(void)
4414 static inline void hwsim_unregister_virtio_driver(void)
4419 static int __init init_mac80211_hwsim(void)
4423 if (radios < 0 || radios > 100)
4429 spin_lock_init(&hwsim_radio_lock);
4431 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4435 err = register_pernet_device(&hwsim_net_ops);
4439 err = platform_driver_register(&mac80211_hwsim_driver);
4441 goto out_unregister_pernet;
4443 err = hwsim_init_netlink();
4445 goto out_unregister_driver;
4447 err = hwsim_register_virtio_driver();
4449 goto out_exit_netlink;
4451 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4452 if (IS_ERR(hwsim_class)) {
4453 err = PTR_ERR(hwsim_class);
4454 goto out_exit_virtio;
4457 hwsim_init_s1g_channels(hwsim_channels_s1g);
4459 for (i = 0; i < radios; i++) {
4460 struct hwsim_new_radio_params param = { 0 };
4462 param.channels = channels;
4465 case HWSIM_REGTEST_DIFF_COUNTRY:
4466 if (i < ARRAY_SIZE(hwsim_alpha2s))
4467 param.reg_alpha2 = hwsim_alpha2s[i];
4469 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4471 param.reg_alpha2 = hwsim_alpha2s[0];
4473 case HWSIM_REGTEST_STRICT_ALL:
4474 param.reg_strict = true;
4476 case HWSIM_REGTEST_DRIVER_REG_ALL:
4477 param.reg_alpha2 = hwsim_alpha2s[0];
4479 case HWSIM_REGTEST_WORLD_ROAM:
4481 param.regd = &hwsim_world_regdom_custom_01;
4483 case HWSIM_REGTEST_CUSTOM_WORLD:
4484 param.regd = &hwsim_world_regdom_custom_01;
4486 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4488 param.regd = &hwsim_world_regdom_custom_01;
4490 param.regd = &hwsim_world_regdom_custom_02;
4492 case HWSIM_REGTEST_STRICT_FOLLOW:
4494 param.reg_strict = true;
4495 param.reg_alpha2 = hwsim_alpha2s[0];
4498 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4500 param.reg_strict = true;
4501 param.reg_alpha2 = hwsim_alpha2s[0];
4502 } else if (i == 1) {
4503 param.reg_alpha2 = hwsim_alpha2s[1];
4506 case HWSIM_REGTEST_ALL:
4509 param.regd = &hwsim_world_regdom_custom_01;
4512 param.regd = &hwsim_world_regdom_custom_02;
4515 param.reg_alpha2 = hwsim_alpha2s[0];
4518 param.reg_alpha2 = hwsim_alpha2s[1];
4521 param.reg_strict = true;
4522 param.reg_alpha2 = hwsim_alpha2s[2];
4530 param.p2p_device = support_p2p_device;
4531 param.use_chanctx = channels > 1;
4532 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4533 if (param.p2p_device)
4534 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4536 err = mac80211_hwsim_new_radio(NULL, ¶m);
4538 goto out_free_radios;
4541 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4543 if (hwsim_mon == NULL) {
4545 goto out_free_radios;
4549 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4555 err = register_netdevice(hwsim_mon);
4565 free_netdev(hwsim_mon);
4567 mac80211_hwsim_free();
4569 hwsim_unregister_virtio_driver();
4571 hwsim_exit_netlink();
4572 out_unregister_driver:
4573 platform_driver_unregister(&mac80211_hwsim_driver);
4574 out_unregister_pernet:
4575 unregister_pernet_device(&hwsim_net_ops);
4577 rhashtable_destroy(&hwsim_radios_rht);
4580 module_init(init_mac80211_hwsim);
4582 static void __exit exit_mac80211_hwsim(void)
4584 pr_debug("mac80211_hwsim: unregister radios\n");
4586 hwsim_unregister_virtio_driver();
4587 hwsim_exit_netlink();
4589 mac80211_hwsim_free();
4591 rhashtable_destroy(&hwsim_radios_rht);
4592 unregister_netdev(hwsim_mon);
4593 platform_driver_unregister(&mac80211_hwsim_driver);
4594 unregister_pernet_device(&hwsim_net_ops);
4596 module_exit(exit_mac80211_hwsim);