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 - 2022 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_03 = {
180 REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0),
181 REG_RULE(2484 - 10, 2484 + 10, 40, 0, 20, 0),
182 REG_RULE(5150 - 10, 5240 + 10, 40, 0, 30, 0),
183 REG_RULE(5745 - 10, 5825 + 10, 40, 0, 30, 0),
184 REG_RULE(5855 - 10, 5925 + 10, 40, 0, 33, 0),
185 REG_RULE(5955 - 10, 7125 + 10, 320, 0, 33, 0),
189 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
190 &hwsim_world_regdom_custom_01,
191 &hwsim_world_regdom_custom_02,
192 &hwsim_world_regdom_custom_03,
195 struct hwsim_vif_priv {
203 #define HWSIM_VIF_MAGIC 0x69537748
205 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
207 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
208 WARN(vp->magic != HWSIM_VIF_MAGIC,
209 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
210 vif, vp->magic, vif->addr, vif->type, vif->p2p);
213 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
215 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
216 vp->magic = HWSIM_VIF_MAGIC;
219 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
221 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
225 struct hwsim_sta_priv {
229 #define HWSIM_STA_MAGIC 0x6d537749
231 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
233 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
234 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
237 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
239 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
240 sp->magic = HWSIM_STA_MAGIC;
243 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
245 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
249 struct hwsim_chanctx_priv {
253 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
255 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
257 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
258 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
261 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
263 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
264 cp->magic = HWSIM_CHANCTX_MAGIC;
267 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
269 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
273 static unsigned int hwsim_net_id;
275 static DEFINE_IDA(hwsim_netgroup_ida);
282 static inline int hwsim_net_get_netgroup(struct net *net)
284 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
286 return hwsim_net->netgroup;
289 static inline int hwsim_net_set_netgroup(struct net *net)
291 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
293 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
295 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
298 static inline u32 hwsim_net_get_wmediumd(struct net *net)
300 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
302 return hwsim_net->wmediumd;
305 static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
307 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
309 hwsim_net->wmediumd = portid;
312 static struct class *hwsim_class;
314 static struct net_device *hwsim_mon; /* global monitor netdev */
316 #define CHAN2G(_freq) { \
317 .band = NL80211_BAND_2GHZ, \
318 .center_freq = (_freq), \
319 .hw_value = (_freq), \
322 #define CHAN5G(_freq) { \
323 .band = NL80211_BAND_5GHZ, \
324 .center_freq = (_freq), \
325 .hw_value = (_freq), \
328 #define CHAN6G(_freq) { \
329 .band = NL80211_BAND_6GHZ, \
330 .center_freq = (_freq), \
331 .hw_value = (_freq), \
334 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
335 CHAN2G(2412), /* Channel 1 */
336 CHAN2G(2417), /* Channel 2 */
337 CHAN2G(2422), /* Channel 3 */
338 CHAN2G(2427), /* Channel 4 */
339 CHAN2G(2432), /* Channel 5 */
340 CHAN2G(2437), /* Channel 6 */
341 CHAN2G(2442), /* Channel 7 */
342 CHAN2G(2447), /* Channel 8 */
343 CHAN2G(2452), /* Channel 9 */
344 CHAN2G(2457), /* Channel 10 */
345 CHAN2G(2462), /* Channel 11 */
346 CHAN2G(2467), /* Channel 12 */
347 CHAN2G(2472), /* Channel 13 */
348 CHAN2G(2484), /* Channel 14 */
351 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
352 CHAN5G(5180), /* Channel 36 */
353 CHAN5G(5200), /* Channel 40 */
354 CHAN5G(5220), /* Channel 44 */
355 CHAN5G(5240), /* Channel 48 */
357 CHAN5G(5260), /* Channel 52 */
358 CHAN5G(5280), /* Channel 56 */
359 CHAN5G(5300), /* Channel 60 */
360 CHAN5G(5320), /* Channel 64 */
362 CHAN5G(5500), /* Channel 100 */
363 CHAN5G(5520), /* Channel 104 */
364 CHAN5G(5540), /* Channel 108 */
365 CHAN5G(5560), /* Channel 112 */
366 CHAN5G(5580), /* Channel 116 */
367 CHAN5G(5600), /* Channel 120 */
368 CHAN5G(5620), /* Channel 124 */
369 CHAN5G(5640), /* Channel 128 */
370 CHAN5G(5660), /* Channel 132 */
371 CHAN5G(5680), /* Channel 136 */
372 CHAN5G(5700), /* Channel 140 */
374 CHAN5G(5745), /* Channel 149 */
375 CHAN5G(5765), /* Channel 153 */
376 CHAN5G(5785), /* Channel 157 */
377 CHAN5G(5805), /* Channel 161 */
378 CHAN5G(5825), /* Channel 165 */
379 CHAN5G(5845), /* Channel 169 */
381 CHAN5G(5855), /* Channel 171 */
382 CHAN5G(5860), /* Channel 172 */
383 CHAN5G(5865), /* Channel 173 */
384 CHAN5G(5870), /* Channel 174 */
386 CHAN5G(5875), /* Channel 175 */
387 CHAN5G(5880), /* Channel 176 */
388 CHAN5G(5885), /* Channel 177 */
389 CHAN5G(5890), /* Channel 178 */
390 CHAN5G(5895), /* Channel 179 */
391 CHAN5G(5900), /* Channel 180 */
392 CHAN5G(5905), /* Channel 181 */
394 CHAN5G(5910), /* Channel 182 */
395 CHAN5G(5915), /* Channel 183 */
396 CHAN5G(5920), /* Channel 184 */
397 CHAN5G(5925), /* Channel 185 */
400 static const struct ieee80211_channel hwsim_channels_6ghz[] = {
401 CHAN6G(5955), /* Channel 1 */
402 CHAN6G(5975), /* Channel 5 */
403 CHAN6G(5995), /* Channel 9 */
404 CHAN6G(6015), /* Channel 13 */
405 CHAN6G(6035), /* Channel 17 */
406 CHAN6G(6055), /* Channel 21 */
407 CHAN6G(6075), /* Channel 25 */
408 CHAN6G(6095), /* Channel 29 */
409 CHAN6G(6115), /* Channel 33 */
410 CHAN6G(6135), /* Channel 37 */
411 CHAN6G(6155), /* Channel 41 */
412 CHAN6G(6175), /* Channel 45 */
413 CHAN6G(6195), /* Channel 49 */
414 CHAN6G(6215), /* Channel 53 */
415 CHAN6G(6235), /* Channel 57 */
416 CHAN6G(6255), /* Channel 61 */
417 CHAN6G(6275), /* Channel 65 */
418 CHAN6G(6295), /* Channel 69 */
419 CHAN6G(6315), /* Channel 73 */
420 CHAN6G(6335), /* Channel 77 */
421 CHAN6G(6355), /* Channel 81 */
422 CHAN6G(6375), /* Channel 85 */
423 CHAN6G(6395), /* Channel 89 */
424 CHAN6G(6415), /* Channel 93 */
425 CHAN6G(6435), /* Channel 97 */
426 CHAN6G(6455), /* Channel 181 */
427 CHAN6G(6475), /* Channel 105 */
428 CHAN6G(6495), /* Channel 109 */
429 CHAN6G(6515), /* Channel 113 */
430 CHAN6G(6535), /* Channel 117 */
431 CHAN6G(6555), /* Channel 121 */
432 CHAN6G(6575), /* Channel 125 */
433 CHAN6G(6595), /* Channel 129 */
434 CHAN6G(6615), /* Channel 133 */
435 CHAN6G(6635), /* Channel 137 */
436 CHAN6G(6655), /* Channel 141 */
437 CHAN6G(6675), /* Channel 145 */
438 CHAN6G(6695), /* Channel 149 */
439 CHAN6G(6715), /* Channel 153 */
440 CHAN6G(6735), /* Channel 157 */
441 CHAN6G(6755), /* Channel 161 */
442 CHAN6G(6775), /* Channel 165 */
443 CHAN6G(6795), /* Channel 169 */
444 CHAN6G(6815), /* Channel 173 */
445 CHAN6G(6835), /* Channel 177 */
446 CHAN6G(6855), /* Channel 181 */
447 CHAN6G(6875), /* Channel 185 */
448 CHAN6G(6895), /* Channel 189 */
449 CHAN6G(6915), /* Channel 193 */
450 CHAN6G(6935), /* Channel 197 */
451 CHAN6G(6955), /* Channel 201 */
452 CHAN6G(6975), /* Channel 205 */
453 CHAN6G(6995), /* Channel 209 */
454 CHAN6G(7015), /* Channel 213 */
455 CHAN6G(7035), /* Channel 217 */
456 CHAN6G(7055), /* Channel 221 */
457 CHAN6G(7075), /* Channel 225 */
458 CHAN6G(7095), /* Channel 229 */
459 CHAN6G(7115), /* Channel 233 */
462 #define NUM_S1G_CHANS_US 51
463 static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
465 static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
467 .cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
470 S1G_CAP3_MAX_MPDU_LEN,
475 S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
477 .nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
478 /* RX Highest Supported Long GI Data Rate 0:7 */
480 /* RX Highest Supported Long GI Data Rate 0:7 */
481 /* TX S1G MCS Map 0:6 */
483 /* TX S1G MCS Map :7 */
484 /* TX Highest Supported Long GI Data Rate 0:6 */
486 /* TX Highest Supported Long GI Data Rate 7:8 */
487 /* Rx Single spatial stream and S1G-MCS Map for 1MHz */
488 /* Tx Single spatial stream and S1G-MCS Map for 1MHz */
492 static void hwsim_init_s1g_channels(struct ieee80211_channel *chans)
496 for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
497 freq = 902000 + (ch + 1) * 500;
498 chans[ch].band = NL80211_BAND_S1GHZ;
499 chans[ch].center_freq = KHZ_TO_MHZ(freq);
500 chans[ch].freq_offset = freq % 1000;
501 chans[ch].hw_value = ch + 1;
505 static const struct ieee80211_rate hwsim_rates[] = {
507 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
508 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
509 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
520 #define DEFAULT_RX_RSSI -50
522 static const u32 hwsim_ciphers[] = {
523 WLAN_CIPHER_SUITE_WEP40,
524 WLAN_CIPHER_SUITE_WEP104,
525 WLAN_CIPHER_SUITE_TKIP,
526 WLAN_CIPHER_SUITE_CCMP,
527 WLAN_CIPHER_SUITE_CCMP_256,
528 WLAN_CIPHER_SUITE_GCMP,
529 WLAN_CIPHER_SUITE_GCMP_256,
530 WLAN_CIPHER_SUITE_AES_CMAC,
531 WLAN_CIPHER_SUITE_BIP_CMAC_256,
532 WLAN_CIPHER_SUITE_BIP_GMAC_128,
533 WLAN_CIPHER_SUITE_BIP_GMAC_256,
536 #define OUI_QCA 0x001374
537 #define QCA_NL80211_SUBCMD_TEST 1
538 enum qca_nl80211_vendor_subcmds {
539 QCA_WLAN_VENDOR_ATTR_TEST = 8,
540 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
543 static const struct nla_policy
544 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
545 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
548 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
549 struct wireless_dev *wdev,
550 const void *data, int data_len)
553 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
557 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
558 data_len, hwsim_vendor_test_policy, NULL);
561 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
563 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
564 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
566 /* Send a vendor event as a test. Note that this would not normally be
567 * done within a command handler, but rather, based on some other
568 * trigger. For simplicity, this command is used to trigger the event
571 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
573 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
575 /* skb_put() or nla_put() will fill up data within
576 * NL80211_ATTR_VENDOR_DATA.
579 /* Add vendor data */
580 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
582 /* Send the event - this will call nla_nest_end() */
583 cfg80211_vendor_event(skb, GFP_KERNEL);
586 /* Send a response to the command */
587 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
591 /* skb_put() or nla_put() will fill up data within
592 * NL80211_ATTR_VENDOR_DATA
594 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
596 return cfg80211_vendor_cmd_reply(skb);
599 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
601 .info = { .vendor_id = OUI_QCA,
602 .subcmd = QCA_NL80211_SUBCMD_TEST },
603 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
604 .doit = mac80211_hwsim_vendor_cmd_test,
605 .policy = hwsim_vendor_test_policy,
606 .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
610 /* Advertise support vendor specific events */
611 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
612 { .vendor_id = OUI_QCA, .subcmd = 1 },
615 static DEFINE_SPINLOCK(hwsim_radio_lock);
616 static LIST_HEAD(hwsim_radios);
617 static struct rhashtable hwsim_radios_rht;
618 static int hwsim_radio_idx;
619 static int hwsim_radios_generation = 1;
621 static struct platform_driver mac80211_hwsim_driver = {
623 .name = "mac80211_hwsim",
627 struct mac80211_hwsim_data {
628 struct list_head list;
629 struct rhash_head rht;
630 struct ieee80211_hw *hw;
632 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
633 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
634 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
635 struct ieee80211_channel channels_6ghz[ARRAY_SIZE(hwsim_channels_6ghz)];
636 struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
637 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
638 struct ieee80211_iface_combination if_combination;
639 struct ieee80211_iface_limit if_limits[3];
642 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
644 struct mac_address addresses[2];
645 struct ieee80211_chanctx_conf *chanctx;
648 bool destroy_on_close;
651 const struct ieee80211_regdomain *regd;
653 struct ieee80211_channel *tmp_chan;
654 struct ieee80211_channel *roc_chan;
656 struct delayed_work roc_start;
657 struct delayed_work roc_done;
658 struct delayed_work hw_scan;
659 struct cfg80211_scan_request *hw_scan_request;
660 struct ieee80211_vif *hw_scan_vif;
662 u8 scan_addr[ETH_ALEN];
664 struct ieee80211_channel *channel;
665 unsigned long next_start, start, end;
666 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
667 ARRAY_SIZE(hwsim_channels_5ghz) +
668 ARRAY_SIZE(hwsim_channels_6ghz)];
670 struct ieee80211_channel *channel;
671 enum nl80211_chan_width bw;
672 u64 beacon_int /* beacon interval in us */;
673 unsigned int rx_filter;
674 bool started, idle, scanning;
676 struct hrtimer beacon_timer;
678 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
680 bool ps_poll_pending;
681 struct dentry *debugfs;
683 uintptr_t pending_cookie;
684 struct sk_buff_head pending; /* packets pending */
686 * Only radios in the same group can communicate together (the
687 * channel has to match too). Each bit represents a group. A
688 * radio can be in more than one group.
692 /* group shared by radios created in the same netns */
694 /* wmediumd portid responsible for netgroup of this radio */
697 /* difference between this hw's clock and the real clock, in usecs */
700 /* absolute beacon transmission time. Used to cover up "tx" delay. */
711 /* RSSI in rx status of the receiver */
715 static const struct rhashtable_params hwsim_rht_params = {
717 .automatic_shrinking = true,
719 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
720 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
723 struct hwsim_radiotap_hdr {
724 struct ieee80211_radiotap_header hdr;
732 struct hwsim_radiotap_ack_hdr {
733 struct ieee80211_radiotap_header hdr;
740 /* MAC80211_HWSIM netlink family */
741 static struct genl_family hwsim_genl_family;
743 enum hwsim_multicast_groups {
747 static const struct genl_multicast_group hwsim_mcgrps[] = {
748 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
751 /* MAC80211_HWSIM netlink policy */
753 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
754 [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
755 [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
756 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
757 .len = IEEE80211_MAX_DATA_LEN },
758 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
759 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
760 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
761 [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
762 .len = IEEE80211_TX_MAX_RATES *
763 sizeof(struct hwsim_tx_rate)},
764 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
765 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
766 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
767 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
768 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
769 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
770 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
771 [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
772 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
773 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
774 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
775 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
776 [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
777 [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
778 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
779 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
782 #if IS_REACHABLE(CONFIG_VIRTIO)
784 /* MAC80211_HWSIM virtio queues */
785 static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
786 static bool hwsim_virtio_enabled;
787 static DEFINE_SPINLOCK(hwsim_virtio_lock);
789 static void hwsim_virtio_rx_work(struct work_struct *work);
790 static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
792 static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
795 struct scatterlist sg[1];
799 spin_lock_irqsave(&hwsim_virtio_lock, flags);
800 if (!hwsim_virtio_enabled) {
805 sg_init_one(sg, skb->head, skb_end_offset(skb));
806 err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
810 virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
811 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
815 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
820 /* cause a linker error if this ends up being needed */
821 extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
822 struct sk_buff *skb);
823 #define hwsim_virtio_enabled false
826 static int hwsim_get_chanwidth(enum nl80211_chan_width bw)
829 case NL80211_CHAN_WIDTH_20_NOHT:
830 case NL80211_CHAN_WIDTH_20:
832 case NL80211_CHAN_WIDTH_40:
834 case NL80211_CHAN_WIDTH_80:
836 case NL80211_CHAN_WIDTH_80P80:
837 case NL80211_CHAN_WIDTH_160:
839 case NL80211_CHAN_WIDTH_320:
841 case NL80211_CHAN_WIDTH_5:
843 case NL80211_CHAN_WIDTH_10:
845 case NL80211_CHAN_WIDTH_1:
847 case NL80211_CHAN_WIDTH_2:
849 case NL80211_CHAN_WIDTH_4:
851 case NL80211_CHAN_WIDTH_8:
853 case NL80211_CHAN_WIDTH_16:
860 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
862 struct ieee80211_channel *chan);
864 /* sysfs attributes */
865 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
867 struct mac80211_hwsim_data *data = dat;
868 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
870 struct ieee80211_pspoll *pspoll;
875 wiphy_dbg(data->hw->wiphy,
876 "%s: send PS-Poll to %pM for aid %d\n",
877 __func__, vp->bssid, vp->aid);
879 skb = dev_alloc_skb(sizeof(*pspoll));
882 pspoll = skb_put(skb, sizeof(*pspoll));
883 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
884 IEEE80211_STYPE_PSPOLL |
886 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
887 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
888 memcpy(pspoll->ta, mac, ETH_ALEN);
891 mac80211_hwsim_tx_frame(data->hw, skb,
892 rcu_dereference(vif->chanctx_conf)->def.chan);
896 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
897 struct ieee80211_vif *vif, int ps)
899 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
901 struct ieee80211_hdr *hdr;
906 wiphy_dbg(data->hw->wiphy,
907 "%s: send data::nullfunc to %pM ps=%d\n",
908 __func__, vp->bssid, ps);
910 skb = dev_alloc_skb(sizeof(*hdr));
913 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
914 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
915 IEEE80211_STYPE_NULLFUNC |
916 IEEE80211_FCTL_TODS |
917 (ps ? IEEE80211_FCTL_PM : 0));
918 hdr->duration_id = cpu_to_le16(0);
919 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
920 memcpy(hdr->addr2, mac, ETH_ALEN);
921 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
924 mac80211_hwsim_tx_frame(data->hw, skb,
925 rcu_dereference(vif->chanctx_conf)->def.chan);
930 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
931 struct ieee80211_vif *vif)
933 struct mac80211_hwsim_data *data = dat;
934 hwsim_send_nullfunc(data, mac, vif, 1);
937 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
938 struct ieee80211_vif *vif)
940 struct mac80211_hwsim_data *data = dat;
941 hwsim_send_nullfunc(data, mac, vif, 0);
944 static int hwsim_fops_ps_read(void *dat, u64 *val)
946 struct mac80211_hwsim_data *data = dat;
951 static int hwsim_fops_ps_write(void *dat, u64 val)
953 struct mac80211_hwsim_data *data = dat;
956 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
957 val != PS_MANUAL_POLL)
960 if (val == PS_MANUAL_POLL) {
961 if (data->ps != PS_ENABLED)
964 ieee80211_iterate_active_interfaces_atomic(
965 data->hw, IEEE80211_IFACE_ITER_NORMAL,
966 hwsim_send_ps_poll, data);
974 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
975 ieee80211_iterate_active_interfaces_atomic(
976 data->hw, IEEE80211_IFACE_ITER_NORMAL,
977 hwsim_send_nullfunc_ps, data);
978 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
979 ieee80211_iterate_active_interfaces_atomic(
980 data->hw, IEEE80211_IFACE_ITER_NORMAL,
981 hwsim_send_nullfunc_no_ps, data);
988 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
991 static int hwsim_write_simulate_radar(void *dat, u64 val)
993 struct mac80211_hwsim_data *data = dat;
995 ieee80211_radar_detected(data->hw);
1000 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
1001 hwsim_write_simulate_radar, "%llu\n");
1003 static int hwsim_fops_group_read(void *dat, u64 *val)
1005 struct mac80211_hwsim_data *data = dat;
1010 static int hwsim_fops_group_write(void *dat, u64 val)
1012 struct mac80211_hwsim_data *data = dat;
1017 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
1018 hwsim_fops_group_read, hwsim_fops_group_write,
1021 static int hwsim_fops_rx_rssi_read(void *dat, u64 *val)
1023 struct mac80211_hwsim_data *data = dat;
1024 *val = data->rx_rssi;
1028 static int hwsim_fops_rx_rssi_write(void *dat, u64 val)
1030 struct mac80211_hwsim_data *data = dat;
1031 int rssi = (int)val;
1033 if (rssi >= 0 || rssi < -100)
1036 data->rx_rssi = rssi;
1040 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_rx_rssi,
1041 hwsim_fops_rx_rssi_read, hwsim_fops_rx_rssi_write,
1044 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
1045 struct net_device *dev)
1047 /* TODO: allow packet injection */
1049 return NETDEV_TX_OK;
1052 static inline u64 mac80211_hwsim_get_tsf_raw(void)
1054 return ktime_to_us(ktime_get_real());
1057 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
1059 u64 now = mac80211_hwsim_get_tsf_raw();
1060 return cpu_to_le64(now + data->tsf_offset);
1063 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
1064 struct ieee80211_vif *vif)
1066 struct mac80211_hwsim_data *data = hw->priv;
1067 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
1070 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
1071 struct ieee80211_vif *vif, u64 tsf)
1073 struct mac80211_hwsim_data *data = hw->priv;
1074 u64 now = mac80211_hwsim_get_tsf(hw, vif);
1075 u32 bcn_int = data->beacon_int;
1076 u64 delta = abs(tsf - now);
1078 /* adjust after beaconing with new timestamp at old TBTT */
1080 data->tsf_offset += delta;
1081 data->bcn_delta = do_div(delta, bcn_int);
1083 data->tsf_offset -= delta;
1084 data->bcn_delta = -(s64)do_div(delta, bcn_int);
1088 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
1089 struct sk_buff *tx_skb,
1090 struct ieee80211_channel *chan)
1092 struct mac80211_hwsim_data *data = hw->priv;
1093 struct sk_buff *skb;
1094 struct hwsim_radiotap_hdr *hdr;
1096 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
1097 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
1102 bitrate = txrate->bitrate;
1104 if (!netif_running(hwsim_mon))
1107 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
1111 hdr = skb_push(skb, sizeof(*hdr));
1112 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1113 hdr->hdr.it_pad = 0;
1114 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1115 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1116 (1 << IEEE80211_RADIOTAP_RATE) |
1117 (1 << IEEE80211_RADIOTAP_TSFT) |
1118 (1 << IEEE80211_RADIOTAP_CHANNEL));
1119 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
1121 hdr->rt_rate = bitrate / 5;
1122 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1123 flags = IEEE80211_CHAN_2GHZ;
1124 if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
1125 flags |= IEEE80211_CHAN_OFDM;
1127 flags |= IEEE80211_CHAN_CCK;
1128 hdr->rt_chbitmask = cpu_to_le16(flags);
1130 skb->dev = hwsim_mon;
1131 skb_reset_mac_header(skb);
1132 skb->ip_summed = CHECKSUM_UNNECESSARY;
1133 skb->pkt_type = PACKET_OTHERHOST;
1134 skb->protocol = htons(ETH_P_802_2);
1135 memset(skb->cb, 0, sizeof(skb->cb));
1140 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
1143 struct sk_buff *skb;
1144 struct hwsim_radiotap_ack_hdr *hdr;
1146 struct ieee80211_hdr *hdr11;
1148 if (!netif_running(hwsim_mon))
1151 skb = dev_alloc_skb(100);
1155 hdr = skb_put(skb, sizeof(*hdr));
1156 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1157 hdr->hdr.it_pad = 0;
1158 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1159 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1160 (1 << IEEE80211_RADIOTAP_CHANNEL));
1163 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1164 flags = IEEE80211_CHAN_2GHZ;
1165 hdr->rt_chbitmask = cpu_to_le16(flags);
1167 hdr11 = skb_put(skb, 10);
1168 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1169 IEEE80211_STYPE_ACK);
1170 hdr11->duration_id = cpu_to_le16(0);
1171 memcpy(hdr11->addr1, addr, ETH_ALEN);
1173 skb->dev = hwsim_mon;
1174 skb_reset_mac_header(skb);
1175 skb->ip_summed = CHECKSUM_UNNECESSARY;
1176 skb->pkt_type = PACKET_OTHERHOST;
1177 skb->protocol = htons(ETH_P_802_2);
1178 memset(skb->cb, 0, sizeof(skb->cb));
1182 struct mac80211_hwsim_addr_match_data {
1187 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1188 struct ieee80211_vif *vif)
1190 struct mac80211_hwsim_addr_match_data *md = data;
1192 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
1196 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1199 struct mac80211_hwsim_addr_match_data md = {
1203 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1206 memcpy(md.addr, addr, ETH_ALEN);
1208 ieee80211_iterate_active_interfaces_atomic(data->hw,
1209 IEEE80211_IFACE_ITER_NORMAL,
1210 mac80211_hwsim_addr_iter,
1216 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1217 struct sk_buff *skb)
1225 /* TODO: accept (some) Beacons by default and other frames only
1226 * if pending PS-Poll has been sent */
1228 case PS_MANUAL_POLL:
1229 /* Allow unicast frames to own address if there is a pending
1231 if (data->ps_poll_pending &&
1232 mac80211_hwsim_addr_match(data, skb->data + 4)) {
1233 data->ps_poll_pending = false;
1242 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1243 struct sk_buff *skb, int portid)
1250 for_each_net_rcu(net) {
1251 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1252 res = genlmsg_unicast(net, skb, portid);
1265 static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1266 const u8 *addr, bool add)
1268 struct mac80211_hwsim_data *data = hw->priv;
1269 u32 _portid = READ_ONCE(data->wmediumd);
1270 struct sk_buff *skb;
1273 if (!_portid && !hwsim_virtio_enabled)
1276 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1280 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1281 add ? HWSIM_CMD_ADD_MAC_ADDR :
1282 HWSIM_CMD_DEL_MAC_ADDR);
1284 pr_debug("mac80211_hwsim: problem with msg_head\n");
1285 goto nla_put_failure;
1288 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1289 ETH_ALEN, data->addresses[1].addr))
1290 goto nla_put_failure;
1292 if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1293 goto nla_put_failure;
1295 genlmsg_end(skb, msg_head);
1297 if (hwsim_virtio_enabled)
1298 hwsim_tx_virtio(data, skb);
1300 hwsim_unicast_netgroup(data, skb, _portid);
1306 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1310 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1311 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1312 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1313 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1314 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1315 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1316 if (rate->flags & IEEE80211_TX_RC_MCS)
1317 result |= MAC80211_HWSIM_TX_RC_MCS;
1318 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1319 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1320 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1321 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1322 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1323 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1324 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1325 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1326 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1327 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1328 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1329 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1330 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1331 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1336 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1337 struct sk_buff *my_skb,
1339 struct ieee80211_channel *channel)
1341 struct sk_buff *skb;
1342 struct mac80211_hwsim_data *data = hw->priv;
1343 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1344 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1346 unsigned int hwsim_flags = 0;
1348 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1349 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1352 if (data->ps != PS_DISABLED)
1353 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1354 /* If the queue contains MAX_QUEUE skb's drop some */
1355 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1356 /* Dropping until WARN_QUEUE level */
1357 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1358 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1363 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1365 goto nla_put_failure;
1367 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1369 if (msg_head == NULL) {
1370 pr_debug("mac80211_hwsim: problem with msg_head\n");
1371 goto nla_put_failure;
1374 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1375 ETH_ALEN, data->addresses[1].addr))
1376 goto nla_put_failure;
1378 /* We get the skb->data */
1379 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1380 goto nla_put_failure;
1382 /* We get the flags for this transmission, and we translate them to
1385 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1386 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1388 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1389 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1391 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1392 goto nla_put_failure;
1394 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1395 goto nla_put_failure;
1397 /* We get the tx control (rate and retries) info*/
1399 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1400 tx_attempts[i].idx = info->status.rates[i].idx;
1401 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1402 tx_attempts[i].count = info->status.rates[i].count;
1403 tx_attempts_flags[i].flags =
1404 trans_tx_rate_flags_ieee2hwsim(
1405 &info->status.rates[i]);
1408 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1409 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1411 goto nla_put_failure;
1413 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1414 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1416 goto nla_put_failure;
1418 /* We create a cookie to identify this skb */
1419 data->pending_cookie++;
1420 cookie = data->pending_cookie;
1421 info->rate_driver_data[0] = (void *)cookie;
1422 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1423 goto nla_put_failure;
1425 genlmsg_end(skb, msg_head);
1427 if (hwsim_virtio_enabled) {
1428 if (hwsim_tx_virtio(data, skb))
1429 goto err_free_txskb;
1431 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1432 goto err_free_txskb;
1435 /* Enqueue the packet */
1436 skb_queue_tail(&data->pending, my_skb);
1438 data->tx_bytes += my_skb->len;
1444 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1445 ieee80211_free_txskb(hw, my_skb);
1449 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1450 struct ieee80211_channel *c2)
1455 return c1->center_freq == c2->center_freq;
1458 struct tx_iter_data {
1459 struct ieee80211_channel *channel;
1463 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1464 struct ieee80211_vif *vif)
1466 struct tx_iter_data *data = _data;
1468 if (!vif->chanctx_conf)
1471 if (!hwsim_chans_compat(data->channel,
1472 rcu_dereference(vif->chanctx_conf)->def.chan))
1475 data->receive = true;
1478 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1481 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1483 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1484 * (but you should use a valid OUI, not that)
1486 * If anyone wants to 'donate' a radiotap OUI/subns code
1487 * please send a patch removing this #ifdef and changing
1488 * the values accordingly.
1490 #ifdef HWSIM_RADIOTAP_OUI
1491 struct ieee80211_vendor_radiotap *rtap;
1494 * Note that this code requires the headroom in the SKB
1495 * that was allocated earlier.
1497 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1498 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1499 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1500 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1504 * Radiotap vendor namespaces can (and should) also be
1505 * split into fields by using the standard radiotap
1506 * presence bitmap mechanism. Use just BIT(0) here for
1507 * the presence bitmap.
1509 rtap->present = BIT(0);
1510 /* We have 8 bytes of (dummy) data */
1512 /* For testing, also require it to be aligned */
1514 /* And also test that padding works, 4 bytes */
1517 memcpy(rtap->data, "ABCDEFGH", 8);
1518 /* make sure to clear padding, mac80211 doesn't */
1519 memset(rtap->data + 8, 0, 4);
1521 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1525 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1526 struct sk_buff *skb,
1527 struct ieee80211_channel *chan)
1529 struct mac80211_hwsim_data *data = hw->priv, *data2;
1531 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1532 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1533 struct ieee80211_rx_status rx_status;
1536 memset(&rx_status, 0, sizeof(rx_status));
1537 rx_status.flag |= RX_FLAG_MACTIME_START;
1538 rx_status.freq = chan->center_freq;
1539 rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1540 rx_status.band = chan->band;
1541 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1542 rx_status.rate_idx =
1543 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1545 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1546 rx_status.encoding = RX_ENC_VHT;
1548 rx_status.rate_idx = info->control.rates[0].idx;
1549 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1550 rx_status.encoding = RX_ENC_HT;
1552 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1553 rx_status.bw = RATE_INFO_BW_40;
1554 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1555 rx_status.bw = RATE_INFO_BW_80;
1556 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1557 rx_status.bw = RATE_INFO_BW_160;
1559 rx_status.bw = RATE_INFO_BW_20;
1560 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1561 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1562 /* TODO: simulate optional packet loss */
1563 rx_status.signal = data->rx_rssi;
1564 if (info->control.vif)
1565 rx_status.signal += info->control.vif->bss_conf.txpower;
1567 if (data->ps != PS_DISABLED)
1568 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1570 /* release the skb's source info */
1578 * Get absolute mactime here so all HWs RX at the "same time", and
1579 * absolute TX time for beacon mactime so the timestamp matches.
1580 * Giving beacons a different mactime than non-beacons looks messy, but
1581 * it helps the Toffset be exact and a ~10us mactime discrepancy
1582 * probably doesn't really matter.
1584 if (ieee80211_is_beacon(hdr->frame_control) ||
1585 ieee80211_is_probe_resp(hdr->frame_control)) {
1586 rx_status.boottime_ns = ktime_get_boottime_ns();
1587 now = data->abs_bcn_ts;
1589 now = mac80211_hwsim_get_tsf_raw();
1592 /* Copy skb to all enabled radios that are on the current frequency */
1593 spin_lock(&hwsim_radio_lock);
1594 list_for_each_entry(data2, &hwsim_radios, list) {
1595 struct sk_buff *nskb;
1596 struct tx_iter_data tx_iter_data = {
1604 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1605 !hwsim_ps_rx_ok(data2, skb))
1608 if (!(data->group & data2->group))
1611 if (data->netgroup != data2->netgroup)
1614 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1615 !hwsim_chans_compat(chan, data2->channel)) {
1616 ieee80211_iterate_active_interfaces_atomic(
1617 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1618 mac80211_hwsim_tx_iter, &tx_iter_data);
1619 if (!tx_iter_data.receive)
1624 * reserve some space for our vendor and the normal
1625 * radiotap header, since we're copying anyway
1627 if (skb->len < PAGE_SIZE && paged_rx) {
1628 struct page *page = alloc_page(GFP_ATOMIC);
1633 nskb = dev_alloc_skb(128);
1639 memcpy(page_address(page), skb->data, skb->len);
1640 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1642 nskb = skb_copy(skb, GFP_ATOMIC);
1647 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1650 rx_status.mactime = now + data2->tsf_offset;
1652 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1654 mac80211_hwsim_add_vendor_rtap(nskb);
1657 data2->rx_bytes += nskb->len;
1658 ieee80211_rx_irqsafe(data2->hw, nskb);
1660 spin_unlock(&hwsim_radio_lock);
1665 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1666 struct ieee80211_tx_control *control,
1667 struct sk_buff *skb)
1669 struct mac80211_hwsim_data *data = hw->priv;
1670 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1671 struct ieee80211_hdr *hdr = (void *)skb->data;
1672 struct ieee80211_chanctx_conf *chanctx_conf;
1673 struct ieee80211_channel *channel;
1675 enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
1678 if (WARN_ON(skb->len < 10)) {
1679 /* Should not happen; just a sanity check for addr1 use */
1680 ieee80211_free_txskb(hw, skb);
1684 if (!data->use_chanctx) {
1685 channel = data->channel;
1687 } else if (txi->hw_queue == 4) {
1688 channel = data->tmp_chan;
1690 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1692 channel = chanctx_conf->def.chan;
1693 confbw = chanctx_conf->def.width;
1699 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1700 ieee80211_free_txskb(hw, skb);
1704 if (data->idle && !data->tmp_chan) {
1705 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1706 ieee80211_free_txskb(hw, skb);
1710 if (txi->control.vif)
1711 hwsim_check_magic(txi->control.vif);
1713 hwsim_check_sta_magic(control->sta);
1715 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1716 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1718 ARRAY_SIZE(txi->control.rates));
1720 for (i = 0; i < ARRAY_SIZE(txi->control.rates); i++) {
1721 u16 rflags = txi->control.rates[i].flags;
1722 /* initialize to data->bw for 5/10 MHz handling */
1723 enum nl80211_chan_width bw = data->bw;
1725 if (txi->control.rates[i].idx == -1)
1728 if (rflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1729 bw = NL80211_CHAN_WIDTH_40;
1730 else if (rflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1731 bw = NL80211_CHAN_WIDTH_80;
1732 else if (rflags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1733 bw = NL80211_CHAN_WIDTH_160;
1735 if (WARN_ON(hwsim_get_chanwidth(bw) > hwsim_get_chanwidth(confbw)))
1739 if (skb->len >= 24 + 8 &&
1740 ieee80211_is_probe_resp(hdr->frame_control)) {
1741 /* fake header transmission time */
1742 struct ieee80211_mgmt *mgmt;
1743 struct ieee80211_rate *txrate;
1748 mgmt = (struct ieee80211_mgmt *)skb->data;
1749 txrate = ieee80211_get_tx_rate(hw, txi);
1751 bitrate = txrate->bitrate;
1752 ts = mac80211_hwsim_get_tsf_raw();
1753 mgmt->u.probe_resp.timestamp =
1754 cpu_to_le64(ts + data->tsf_offset +
1755 24 * 8 * 10 / bitrate);
1758 mac80211_hwsim_monitor_rx(hw, skb, channel);
1760 /* wmediumd mode check */
1761 _portid = READ_ONCE(data->wmediumd);
1763 if (_portid || hwsim_virtio_enabled)
1764 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1766 /* NO wmediumd detected, perfect medium simulation */
1768 data->tx_bytes += skb->len;
1769 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1771 if (ack && skb->len >= 16)
1772 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1774 ieee80211_tx_info_clear_status(txi);
1776 /* frame was transmitted at most favorable rate at first attempt */
1777 txi->control.rates[0].count = 1;
1778 txi->control.rates[1].idx = -1;
1780 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1781 txi->flags |= IEEE80211_TX_STAT_ACK;
1782 ieee80211_tx_status_irqsafe(hw, skb);
1786 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1788 struct mac80211_hwsim_data *data = hw->priv;
1789 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1790 data->started = true;
1795 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1797 struct mac80211_hwsim_data *data = hw->priv;
1799 data->started = false;
1800 hrtimer_cancel(&data->beacon_timer);
1802 while (!skb_queue_empty(&data->pending))
1803 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1805 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1809 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1810 struct ieee80211_vif *vif)
1812 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1813 __func__, ieee80211_vif_type_p2p(vif),
1815 hwsim_set_magic(vif);
1817 if (vif->type != NL80211_IFTYPE_MONITOR)
1818 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1821 vif->hw_queue[IEEE80211_AC_VO] = 0;
1822 vif->hw_queue[IEEE80211_AC_VI] = 1;
1823 vif->hw_queue[IEEE80211_AC_BE] = 2;
1824 vif->hw_queue[IEEE80211_AC_BK] = 3;
1830 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1831 struct ieee80211_vif *vif,
1832 enum nl80211_iftype newtype,
1835 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1836 wiphy_dbg(hw->wiphy,
1837 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1838 __func__, ieee80211_vif_type_p2p(vif),
1839 newtype, vif->addr);
1840 hwsim_check_magic(vif);
1843 * interface may change from non-AP to AP in
1844 * which case this needs to be set up again
1851 static void mac80211_hwsim_remove_interface(
1852 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1854 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1855 __func__, ieee80211_vif_type_p2p(vif),
1857 hwsim_check_magic(vif);
1858 hwsim_clear_magic(vif);
1859 if (vif->type != NL80211_IFTYPE_MONITOR)
1860 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1863 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1864 struct sk_buff *skb,
1865 struct ieee80211_channel *chan)
1867 struct mac80211_hwsim_data *data = hw->priv;
1868 u32 _pid = READ_ONCE(data->wmediumd);
1870 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1871 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1872 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1874 ARRAY_SIZE(txi->control.rates));
1877 mac80211_hwsim_monitor_rx(hw, skb, chan);
1879 if (_pid || hwsim_virtio_enabled)
1880 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1883 data->tx_bytes += skb->len;
1884 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1888 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1889 struct ieee80211_vif *vif)
1891 struct mac80211_hwsim_data *data = arg;
1892 struct ieee80211_hw *hw = data->hw;
1893 struct ieee80211_tx_info *info;
1894 struct ieee80211_rate *txrate;
1895 struct ieee80211_mgmt *mgmt;
1896 struct sk_buff *skb;
1900 hwsim_check_magic(vif);
1902 if (vif->type != NL80211_IFTYPE_AP &&
1903 vif->type != NL80211_IFTYPE_MESH_POINT &&
1904 vif->type != NL80211_IFTYPE_ADHOC &&
1905 vif->type != NL80211_IFTYPE_OCB)
1908 skb = ieee80211_beacon_get(hw, vif);
1911 info = IEEE80211_SKB_CB(skb);
1912 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1913 ieee80211_get_tx_rates(vif, NULL, skb,
1914 info->control.rates,
1915 ARRAY_SIZE(info->control.rates));
1917 txrate = ieee80211_get_tx_rate(hw, info);
1919 bitrate = txrate->bitrate;
1921 mgmt = (struct ieee80211_mgmt *) skb->data;
1922 /* fake header transmission time */
1923 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1924 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1925 struct ieee80211_ext *ext = (void *) mgmt;
1927 ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1932 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1938 mac80211_hwsim_tx_frame(hw, skb,
1939 rcu_dereference(vif->chanctx_conf)->def.chan);
1941 while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1942 mac80211_hwsim_tx_frame(hw, skb,
1943 rcu_dereference(vif->chanctx_conf)->def.chan);
1946 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1947 ieee80211_csa_finish(vif);
1950 static enum hrtimer_restart
1951 mac80211_hwsim_beacon(struct hrtimer *timer)
1953 struct mac80211_hwsim_data *data =
1954 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1955 struct ieee80211_hw *hw = data->hw;
1956 u64 bcn_int = data->beacon_int;
1959 return HRTIMER_NORESTART;
1961 ieee80211_iterate_active_interfaces_atomic(
1962 hw, IEEE80211_IFACE_ITER_NORMAL,
1963 mac80211_hwsim_beacon_tx, data);
1965 /* beacon at new TBTT + beacon interval */
1966 if (data->bcn_delta) {
1967 bcn_int -= data->bcn_delta;
1968 data->bcn_delta = 0;
1970 hrtimer_forward_now(&data->beacon_timer,
1971 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1972 return HRTIMER_RESTART;
1975 static const char * const hwsim_chanwidths[] = {
1976 [NL80211_CHAN_WIDTH_5] = "ht5",
1977 [NL80211_CHAN_WIDTH_10] = "ht10",
1978 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1979 [NL80211_CHAN_WIDTH_20] = "ht20",
1980 [NL80211_CHAN_WIDTH_40] = "ht40",
1981 [NL80211_CHAN_WIDTH_80] = "vht80",
1982 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1983 [NL80211_CHAN_WIDTH_160] = "vht160",
1984 [NL80211_CHAN_WIDTH_1] = "1MHz",
1985 [NL80211_CHAN_WIDTH_2] = "2MHz",
1986 [NL80211_CHAN_WIDTH_4] = "4MHz",
1987 [NL80211_CHAN_WIDTH_8] = "8MHz",
1988 [NL80211_CHAN_WIDTH_16] = "16MHz",
1991 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1993 struct mac80211_hwsim_data *data = hw->priv;
1994 struct ieee80211_conf *conf = &hw->conf;
1995 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1996 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1997 [IEEE80211_SMPS_OFF] = "off",
1998 [IEEE80211_SMPS_STATIC] = "static",
1999 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
2003 if (conf->chandef.chan)
2004 wiphy_dbg(hw->wiphy,
2005 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
2007 conf->chandef.chan->center_freq,
2008 conf->chandef.center_freq1,
2009 conf->chandef.center_freq2,
2010 hwsim_chanwidths[conf->chandef.width],
2011 !!(conf->flags & IEEE80211_CONF_IDLE),
2012 !!(conf->flags & IEEE80211_CONF_PS),
2013 smps_modes[conf->smps_mode]);
2015 wiphy_dbg(hw->wiphy,
2016 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
2018 !!(conf->flags & IEEE80211_CONF_IDLE),
2019 !!(conf->flags & IEEE80211_CONF_PS),
2020 smps_modes[conf->smps_mode]);
2022 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
2024 WARN_ON(conf->chandef.chan && data->use_chanctx);
2026 mutex_lock(&data->mutex);
2027 if (data->scanning && conf->chandef.chan) {
2028 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2029 if (data->survey_data[idx].channel == data->channel) {
2030 data->survey_data[idx].start =
2031 data->survey_data[idx].next_start;
2032 data->survey_data[idx].end = jiffies;
2037 data->channel = conf->chandef.chan;
2038 data->bw = conf->chandef.width;
2040 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2041 if (data->survey_data[idx].channel &&
2042 data->survey_data[idx].channel != data->channel)
2044 data->survey_data[idx].channel = data->channel;
2045 data->survey_data[idx].next_start = jiffies;
2049 data->channel = conf->chandef.chan;
2050 data->bw = conf->chandef.width;
2052 mutex_unlock(&data->mutex);
2054 if (!data->started || !data->beacon_int)
2055 hrtimer_cancel(&data->beacon_timer);
2056 else if (!hrtimer_is_queued(&data->beacon_timer)) {
2057 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
2058 u32 bcn_int = data->beacon_int;
2059 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2061 hrtimer_start(&data->beacon_timer,
2062 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2063 HRTIMER_MODE_REL_SOFT);
2070 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
2071 unsigned int changed_flags,
2072 unsigned int *total_flags,u64 multicast)
2074 struct mac80211_hwsim_data *data = hw->priv;
2076 wiphy_dbg(hw->wiphy, "%s\n", __func__);
2078 data->rx_filter = 0;
2079 if (*total_flags & FIF_ALLMULTI)
2080 data->rx_filter |= FIF_ALLMULTI;
2081 if (*total_flags & FIF_MCAST_ACTION)
2082 data->rx_filter |= FIF_MCAST_ACTION;
2084 *total_flags = data->rx_filter;
2087 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
2088 struct ieee80211_vif *vif)
2090 unsigned int *count = data;
2091 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2097 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
2098 struct ieee80211_vif *vif,
2099 struct ieee80211_bss_conf *info,
2102 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2103 struct mac80211_hwsim_data *data = hw->priv;
2105 hwsim_check_magic(vif);
2107 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
2108 __func__, changed, vif->addr);
2110 if (changed & BSS_CHANGED_BSSID) {
2111 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
2112 __func__, info->bssid);
2113 memcpy(vp->bssid, info->bssid, ETH_ALEN);
2116 if (changed & BSS_CHANGED_ASSOC) {
2117 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
2118 info->assoc, info->aid);
2119 vp->assoc = info->assoc;
2120 vp->aid = info->aid;
2123 if (changed & BSS_CHANGED_BEACON_ENABLED) {
2124 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
2125 info->enable_beacon, info->beacon_int);
2126 vp->bcn_en = info->enable_beacon;
2127 if (data->started &&
2128 !hrtimer_is_queued(&data->beacon_timer) &&
2129 info->enable_beacon) {
2130 u64 tsf, until_tbtt;
2132 data->beacon_int = info->beacon_int * 1024;
2133 tsf = mac80211_hwsim_get_tsf(hw, vif);
2134 bcn_int = data->beacon_int;
2135 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2137 hrtimer_start(&data->beacon_timer,
2138 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2139 HRTIMER_MODE_REL_SOFT);
2140 } else if (!info->enable_beacon) {
2141 unsigned int count = 0;
2142 ieee80211_iterate_active_interfaces_atomic(
2143 data->hw, IEEE80211_IFACE_ITER_NORMAL,
2144 mac80211_hwsim_bcn_en_iter, &count);
2145 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
2148 hrtimer_cancel(&data->beacon_timer);
2149 data->beacon_int = 0;
2154 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2155 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
2156 info->use_cts_prot);
2159 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2160 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
2161 info->use_short_preamble);
2164 if (changed & BSS_CHANGED_ERP_SLOT) {
2165 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
2168 if (changed & BSS_CHANGED_HT) {
2169 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
2170 info->ht_operation_mode);
2173 if (changed & BSS_CHANGED_BASIC_RATES) {
2174 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
2175 (unsigned long long) info->basic_rates);
2178 if (changed & BSS_CHANGED_TXPOWER)
2179 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
2183 mac80211_hwsim_sta_rc_update(struct ieee80211_hw *hw,
2184 struct ieee80211_vif *vif,
2185 struct ieee80211_sta *sta,
2188 struct mac80211_hwsim_data *data = hw->priv;
2190 enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
2192 switch (sta->deflink.bandwidth) {
2193 #define C(_bw) case IEEE80211_STA_RX_BW_##_bw: bw = _bw; break
2202 if (!data->use_chanctx) {
2205 struct ieee80211_chanctx_conf *chanctx_conf;
2208 chanctx_conf = rcu_dereference(vif->chanctx_conf);
2210 if (!WARN_ON(!chanctx_conf))
2211 confbw = chanctx_conf->def.width;
2215 WARN(bw > hwsim_get_chanwidth(confbw),
2216 "intf %pM: bad STA %pM bandwidth %d MHz (%d) > channel config %d MHz (%d)\n",
2217 vif->addr, sta->addr, bw, sta->deflink.bandwidth,
2218 hwsim_get_chanwidth(data->bw), data->bw);
2221 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2222 struct ieee80211_vif *vif,
2223 struct ieee80211_sta *sta)
2225 hwsim_check_magic(vif);
2226 hwsim_set_sta_magic(sta);
2227 mac80211_hwsim_sta_rc_update(hw, vif, sta, 0);
2232 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2233 struct ieee80211_vif *vif,
2234 struct ieee80211_sta *sta)
2236 hwsim_check_magic(vif);
2237 hwsim_clear_sta_magic(sta);
2242 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2243 struct ieee80211_vif *vif,
2244 enum sta_notify_cmd cmd,
2245 struct ieee80211_sta *sta)
2247 hwsim_check_magic(vif);
2250 case STA_NOTIFY_SLEEP:
2251 case STA_NOTIFY_AWAKE:
2252 /* TODO: make good use of these flags */
2255 WARN(1, "Invalid sta notify: %d\n", cmd);
2260 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2261 struct ieee80211_sta *sta,
2264 hwsim_check_sta_magic(sta);
2268 static int mac80211_hwsim_conf_tx(
2269 struct ieee80211_hw *hw,
2270 struct ieee80211_vif *vif, u16 queue,
2271 const struct ieee80211_tx_queue_params *params)
2273 wiphy_dbg(hw->wiphy,
2274 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2276 params->txop, params->cw_min,
2277 params->cw_max, params->aifs);
2281 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2282 struct survey_info *survey)
2284 struct mac80211_hwsim_data *hwsim = hw->priv;
2286 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2289 mutex_lock(&hwsim->mutex);
2290 survey->channel = hwsim->survey_data[idx].channel;
2291 if (!survey->channel) {
2292 mutex_unlock(&hwsim->mutex);
2297 * Magically conjured dummy values --- this is only ok for simulated hardware.
2299 * A real driver which cannot determine real values noise MUST NOT
2300 * report any, especially not a magically conjured ones :-)
2302 survey->filled = SURVEY_INFO_NOISE_DBM |
2304 SURVEY_INFO_TIME_BUSY;
2305 survey->noise = -92;
2307 jiffies_to_msecs(hwsim->survey_data[idx].end -
2308 hwsim->survey_data[idx].start);
2309 /* report 12.5% of channel time is used */
2310 survey->time_busy = survey->time/8;
2311 mutex_unlock(&hwsim->mutex);
2316 #ifdef CONFIG_NL80211_TESTMODE
2318 * This section contains example code for using netlink
2319 * attributes with the testmode command in nl80211.
2322 /* These enums need to be kept in sync with userspace */
2323 enum hwsim_testmode_attr {
2324 __HWSIM_TM_ATTR_INVALID = 0,
2325 HWSIM_TM_ATTR_CMD = 1,
2326 HWSIM_TM_ATTR_PS = 2,
2329 __HWSIM_TM_ATTR_AFTER_LAST,
2330 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
2333 enum hwsim_testmode_cmd {
2334 HWSIM_TM_CMD_SET_PS = 0,
2335 HWSIM_TM_CMD_GET_PS = 1,
2336 HWSIM_TM_CMD_STOP_QUEUES = 2,
2337 HWSIM_TM_CMD_WAKE_QUEUES = 3,
2340 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2341 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2342 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2345 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2346 struct ieee80211_vif *vif,
2347 void *data, int len)
2349 struct mac80211_hwsim_data *hwsim = hw->priv;
2350 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2351 struct sk_buff *skb;
2354 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2355 hwsim_testmode_policy, NULL);
2359 if (!tb[HWSIM_TM_ATTR_CMD])
2362 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2363 case HWSIM_TM_CMD_SET_PS:
2364 if (!tb[HWSIM_TM_ATTR_PS])
2366 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2367 return hwsim_fops_ps_write(hwsim, ps);
2368 case HWSIM_TM_CMD_GET_PS:
2369 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2370 nla_total_size(sizeof(u32)));
2373 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2374 goto nla_put_failure;
2375 return cfg80211_testmode_reply(skb);
2376 case HWSIM_TM_CMD_STOP_QUEUES:
2377 ieee80211_stop_queues(hw);
2379 case HWSIM_TM_CMD_WAKE_QUEUES:
2380 ieee80211_wake_queues(hw);
2392 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2393 struct ieee80211_vif *vif,
2394 struct ieee80211_ampdu_params *params)
2396 struct ieee80211_sta *sta = params->sta;
2397 enum ieee80211_ampdu_mlme_action action = params->action;
2398 u16 tid = params->tid;
2401 case IEEE80211_AMPDU_TX_START:
2402 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2403 case IEEE80211_AMPDU_TX_STOP_CONT:
2404 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2405 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2406 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2408 case IEEE80211_AMPDU_TX_OPERATIONAL:
2410 case IEEE80211_AMPDU_RX_START:
2411 case IEEE80211_AMPDU_RX_STOP:
2420 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2421 struct ieee80211_vif *vif,
2422 u32 queues, bool drop)
2424 /* Not implemented, queues only on kernel side */
2427 static void hw_scan_work(struct work_struct *work)
2429 struct mac80211_hwsim_data *hwsim =
2430 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2431 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2434 mutex_lock(&hwsim->mutex);
2435 if (hwsim->scan_chan_idx >= req->n_channels) {
2436 struct cfg80211_scan_info info = {
2440 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2441 ieee80211_scan_completed(hwsim->hw, &info);
2442 hwsim->hw_scan_request = NULL;
2443 hwsim->hw_scan_vif = NULL;
2444 hwsim->tmp_chan = NULL;
2445 mutex_unlock(&hwsim->mutex);
2446 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2451 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2452 req->channels[hwsim->scan_chan_idx]->center_freq);
2454 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2455 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2456 IEEE80211_CHAN_RADAR) ||
2462 for (i = 0; i < req->n_ssids; i++) {
2463 struct sk_buff *probe;
2464 struct ieee80211_mgmt *mgmt;
2466 probe = ieee80211_probereq_get(hwsim->hw,
2469 req->ssids[i].ssid_len,
2474 mgmt = (struct ieee80211_mgmt *) probe->data;
2475 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2476 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2479 skb_put_data(probe, req->ie, req->ie_len);
2482 if (!ieee80211_tx_prepare_skb(hwsim->hw,
2485 hwsim->tmp_chan->band,
2493 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2499 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2500 msecs_to_jiffies(dwell));
2501 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2502 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2503 hwsim->survey_data[hwsim->scan_chan_idx].end =
2504 jiffies + msecs_to_jiffies(dwell);
2505 hwsim->scan_chan_idx++;
2506 mutex_unlock(&hwsim->mutex);
2509 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2510 struct ieee80211_vif *vif,
2511 struct ieee80211_scan_request *hw_req)
2513 struct mac80211_hwsim_data *hwsim = hw->priv;
2514 struct cfg80211_scan_request *req = &hw_req->req;
2516 mutex_lock(&hwsim->mutex);
2517 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2518 mutex_unlock(&hwsim->mutex);
2521 hwsim->hw_scan_request = req;
2522 hwsim->hw_scan_vif = vif;
2523 hwsim->scan_chan_idx = 0;
2524 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2525 get_random_mask_addr(hwsim->scan_addr,
2526 hw_req->req.mac_addr,
2527 hw_req->req.mac_addr_mask);
2529 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2530 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2531 mutex_unlock(&hwsim->mutex);
2533 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2534 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2536 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2541 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2542 struct ieee80211_vif *vif)
2544 struct mac80211_hwsim_data *hwsim = hw->priv;
2545 struct cfg80211_scan_info info = {
2549 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2551 cancel_delayed_work_sync(&hwsim->hw_scan);
2553 mutex_lock(&hwsim->mutex);
2554 ieee80211_scan_completed(hwsim->hw, &info);
2555 hwsim->tmp_chan = NULL;
2556 hwsim->hw_scan_request = NULL;
2557 hwsim->hw_scan_vif = NULL;
2558 mutex_unlock(&hwsim->mutex);
2561 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2562 struct ieee80211_vif *vif,
2565 struct mac80211_hwsim_data *hwsim = hw->priv;
2567 mutex_lock(&hwsim->mutex);
2569 if (hwsim->scanning) {
2570 pr_debug("two hwsim sw_scans detected!\n");
2574 pr_debug("hwsim sw_scan request, prepping stuff\n");
2576 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2577 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2578 hwsim->scanning = true;
2579 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2582 mutex_unlock(&hwsim->mutex);
2585 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2586 struct ieee80211_vif *vif)
2588 struct mac80211_hwsim_data *hwsim = hw->priv;
2590 mutex_lock(&hwsim->mutex);
2592 pr_debug("hwsim sw_scan_complete\n");
2593 hwsim->scanning = false;
2594 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2595 eth_zero_addr(hwsim->scan_addr);
2597 mutex_unlock(&hwsim->mutex);
2600 static void hw_roc_start(struct work_struct *work)
2602 struct mac80211_hwsim_data *hwsim =
2603 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2605 mutex_lock(&hwsim->mutex);
2607 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2608 hwsim->tmp_chan = hwsim->roc_chan;
2609 ieee80211_ready_on_channel(hwsim->hw);
2611 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2612 msecs_to_jiffies(hwsim->roc_duration));
2614 mutex_unlock(&hwsim->mutex);
2617 static void hw_roc_done(struct work_struct *work)
2619 struct mac80211_hwsim_data *hwsim =
2620 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2622 mutex_lock(&hwsim->mutex);
2623 ieee80211_remain_on_channel_expired(hwsim->hw);
2624 hwsim->tmp_chan = NULL;
2625 mutex_unlock(&hwsim->mutex);
2627 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2630 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2631 struct ieee80211_vif *vif,
2632 struct ieee80211_channel *chan,
2634 enum ieee80211_roc_type type)
2636 struct mac80211_hwsim_data *hwsim = hw->priv;
2638 mutex_lock(&hwsim->mutex);
2639 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2640 mutex_unlock(&hwsim->mutex);
2644 hwsim->roc_chan = chan;
2645 hwsim->roc_duration = duration;
2646 mutex_unlock(&hwsim->mutex);
2648 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2649 chan->center_freq, duration);
2650 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2655 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2656 struct ieee80211_vif *vif)
2658 struct mac80211_hwsim_data *hwsim = hw->priv;
2660 cancel_delayed_work_sync(&hwsim->roc_start);
2661 cancel_delayed_work_sync(&hwsim->roc_done);
2663 mutex_lock(&hwsim->mutex);
2664 hwsim->tmp_chan = NULL;
2665 mutex_unlock(&hwsim->mutex);
2667 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2672 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2673 struct ieee80211_chanctx_conf *ctx)
2675 struct mac80211_hwsim_data *hwsim = hw->priv;
2677 mutex_lock(&hwsim->mutex);
2678 hwsim->chanctx = ctx;
2679 mutex_unlock(&hwsim->mutex);
2680 hwsim_set_chanctx_magic(ctx);
2681 wiphy_dbg(hw->wiphy,
2682 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2683 ctx->def.chan->center_freq, ctx->def.width,
2684 ctx->def.center_freq1, ctx->def.center_freq2);
2688 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2689 struct ieee80211_chanctx_conf *ctx)
2691 struct mac80211_hwsim_data *hwsim = hw->priv;
2693 mutex_lock(&hwsim->mutex);
2694 hwsim->chanctx = NULL;
2695 mutex_unlock(&hwsim->mutex);
2696 wiphy_dbg(hw->wiphy,
2697 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2698 ctx->def.chan->center_freq, ctx->def.width,
2699 ctx->def.center_freq1, ctx->def.center_freq2);
2700 hwsim_check_chanctx_magic(ctx);
2701 hwsim_clear_chanctx_magic(ctx);
2704 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2705 struct ieee80211_chanctx_conf *ctx,
2708 struct mac80211_hwsim_data *hwsim = hw->priv;
2710 mutex_lock(&hwsim->mutex);
2711 hwsim->chanctx = ctx;
2712 mutex_unlock(&hwsim->mutex);
2713 hwsim_check_chanctx_magic(ctx);
2714 wiphy_dbg(hw->wiphy,
2715 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2716 ctx->def.chan->center_freq, ctx->def.width,
2717 ctx->def.center_freq1, ctx->def.center_freq2);
2720 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2721 struct ieee80211_vif *vif,
2722 struct ieee80211_chanctx_conf *ctx)
2724 hwsim_check_magic(vif);
2725 hwsim_check_chanctx_magic(ctx);
2730 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2731 struct ieee80211_vif *vif,
2732 struct ieee80211_chanctx_conf *ctx)
2734 hwsim_check_magic(vif);
2735 hwsim_check_chanctx_magic(ctx);
2738 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2749 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2751 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2752 struct ieee80211_vif *vif,
2755 if (sset == ETH_SS_STATS)
2756 memcpy(data, *mac80211_hwsim_gstrings_stats,
2757 sizeof(mac80211_hwsim_gstrings_stats));
2760 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2761 struct ieee80211_vif *vif, int sset)
2763 if (sset == ETH_SS_STATS)
2764 return MAC80211_HWSIM_SSTATS_LEN;
2768 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2769 struct ieee80211_vif *vif,
2770 struct ethtool_stats *stats, u64 *data)
2772 struct mac80211_hwsim_data *ar = hw->priv;
2775 data[i++] = ar->tx_pkts;
2776 data[i++] = ar->tx_bytes;
2777 data[i++] = ar->rx_pkts;
2778 data[i++] = ar->rx_bytes;
2779 data[i++] = ar->tx_dropped;
2780 data[i++] = ar->tx_failed;
2782 data[i++] = ar->group;
2784 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2787 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2792 #define HWSIM_COMMON_OPS \
2793 .tx = mac80211_hwsim_tx, \
2794 .start = mac80211_hwsim_start, \
2795 .stop = mac80211_hwsim_stop, \
2796 .add_interface = mac80211_hwsim_add_interface, \
2797 .change_interface = mac80211_hwsim_change_interface, \
2798 .remove_interface = mac80211_hwsim_remove_interface, \
2799 .config = mac80211_hwsim_config, \
2800 .configure_filter = mac80211_hwsim_configure_filter, \
2801 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2802 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2803 .sta_add = mac80211_hwsim_sta_add, \
2804 .sta_remove = mac80211_hwsim_sta_remove, \
2805 .sta_notify = mac80211_hwsim_sta_notify, \
2806 .sta_rc_update = mac80211_hwsim_sta_rc_update, \
2807 .set_tim = mac80211_hwsim_set_tim, \
2808 .conf_tx = mac80211_hwsim_conf_tx, \
2809 .get_survey = mac80211_hwsim_get_survey, \
2810 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2811 .ampdu_action = mac80211_hwsim_ampdu_action, \
2812 .flush = mac80211_hwsim_flush, \
2813 .get_tsf = mac80211_hwsim_get_tsf, \
2814 .set_tsf = mac80211_hwsim_set_tsf, \
2815 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2816 .get_et_stats = mac80211_hwsim_get_et_stats, \
2817 .get_et_strings = mac80211_hwsim_get_et_strings,
2819 static const struct ieee80211_ops mac80211_hwsim_ops = {
2821 .sw_scan_start = mac80211_hwsim_sw_scan,
2822 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2825 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2827 .hw_scan = mac80211_hwsim_hw_scan,
2828 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2829 .sw_scan_start = NULL,
2830 .sw_scan_complete = NULL,
2831 .remain_on_channel = mac80211_hwsim_roc,
2832 .cancel_remain_on_channel = mac80211_hwsim_croc,
2833 .add_chanctx = mac80211_hwsim_add_chanctx,
2834 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2835 .change_chanctx = mac80211_hwsim_change_chanctx,
2836 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2837 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2840 struct hwsim_new_radio_params {
2841 unsigned int channels;
2842 const char *reg_alpha2;
2843 const struct ieee80211_regdomain *regd;
2847 bool destroy_on_close;
2850 const u8 *perm_addr;
2856 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2857 struct genl_info *info)
2860 genl_notify(&hwsim_genl_family, mcast_skb, info,
2861 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2863 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2864 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2867 static int append_radio_msg(struct sk_buff *skb, int id,
2868 struct hwsim_new_radio_params *param)
2872 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2876 if (param->channels) {
2877 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2882 if (param->reg_alpha2) {
2883 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2892 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2893 if (hwsim_world_regdom_custom[i] != param->regd)
2896 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2903 if (param->reg_strict) {
2904 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2909 if (param->p2p_device) {
2910 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2915 if (param->use_chanctx) {
2916 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2921 if (param->hwname) {
2922 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2923 strlen(param->hwname), param->hwname);
2931 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2932 struct hwsim_new_radio_params *param)
2934 struct sk_buff *mcast_skb;
2937 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2941 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2942 HWSIM_CMD_NEW_RADIO);
2946 if (append_radio_msg(mcast_skb, id, param) < 0)
2949 genlmsg_end(mcast_skb, data);
2951 hwsim_mcast_config_msg(mcast_skb, info);
2955 nlmsg_free(mcast_skb);
2958 static const struct ieee80211_sband_iftype_data sband_capa_2ghz[] = {
2960 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2961 BIT(NL80211_IFTYPE_AP),
2966 IEEE80211_HE_MAC_CAP0_HTC_HE,
2968 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2969 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2971 IEEE80211_HE_MAC_CAP2_BSR |
2972 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2973 IEEE80211_HE_MAC_CAP2_ACK_EN,
2975 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2976 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2977 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2979 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2980 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2981 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2982 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2984 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2985 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2986 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2987 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2988 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2990 /* Leave all the other PHY capability bytes
2991 * unset, as DCM, beam forming, RU and PPE
2992 * threshold information are not supported
2995 .he_mcs_nss_supp = {
2996 .rx_mcs_80 = cpu_to_le16(0xfffa),
2997 .tx_mcs_80 = cpu_to_le16(0xfffa),
2998 .rx_mcs_160 = cpu_to_le16(0xffff),
2999 .tx_mcs_160 = cpu_to_le16(0xffff),
3000 .rx_mcs_80p80 = cpu_to_le16(0xffff),
3001 .tx_mcs_80p80 = cpu_to_le16(0xffff),
3008 IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3009 IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3010 IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3012 IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3013 IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3014 IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3015 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3016 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
3018 IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3019 IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3020 IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3021 IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3022 IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3023 IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3024 IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3026 IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3027 IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3028 IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3029 IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3030 IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3032 IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3033 IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3034 IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3035 IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3036 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3037 IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3039 IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3040 IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
3042 IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
3045 /* For all MCS and bandwidth, set 8 NSS for both Tx and
3048 .eht_mcs_nss_supp = {
3050 * Since B0, B1, B2 and B3 are not set in
3051 * the supported channel width set field in the
3052 * HE PHY capabilities information field the
3053 * device is a 20MHz only device on 2.4GHz band.
3056 .rx_tx_mcs7_max_nss = 0x88,
3057 .rx_tx_mcs9_max_nss = 0x88,
3058 .rx_tx_mcs11_max_nss = 0x88,
3059 .rx_tx_mcs13_max_nss = 0x88,
3062 /* PPE threshold information is not supported */
3065 #ifdef CONFIG_MAC80211_MESH
3067 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3072 IEEE80211_HE_MAC_CAP0_HTC_HE,
3074 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3076 IEEE80211_HE_MAC_CAP2_ACK_EN,
3078 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3079 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3080 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3082 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3083 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3084 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3085 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3086 .phy_cap_info[2] = 0,
3088 /* Leave all the other PHY capability bytes
3089 * unset, as DCM, beam forming, RU and PPE
3090 * threshold information are not supported
3093 .he_mcs_nss_supp = {
3094 .rx_mcs_80 = cpu_to_le16(0xfffa),
3095 .tx_mcs_80 = cpu_to_le16(0xfffa),
3096 .rx_mcs_160 = cpu_to_le16(0xffff),
3097 .tx_mcs_160 = cpu_to_le16(0xffff),
3098 .rx_mcs_80p80 = cpu_to_le16(0xffff),
3099 .tx_mcs_80p80 = cpu_to_le16(0xffff),
3106 static const struct ieee80211_sband_iftype_data sband_capa_5ghz[] = {
3108 /* TODO: should we support other types, e.g., P2P?*/
3109 .types_mask = BIT(NL80211_IFTYPE_STATION) |
3110 BIT(NL80211_IFTYPE_AP),
3115 IEEE80211_HE_MAC_CAP0_HTC_HE,
3117 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
3118 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3120 IEEE80211_HE_MAC_CAP2_BSR |
3121 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
3122 IEEE80211_HE_MAC_CAP2_ACK_EN,
3124 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3125 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3126 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3128 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3129 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3130 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3132 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3133 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3134 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3135 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3137 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3138 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3139 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3140 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3141 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
3143 /* Leave all the other PHY capability bytes
3144 * unset, as DCM, beam forming, RU and PPE
3145 * threshold information are not supported
3148 .he_mcs_nss_supp = {
3149 .rx_mcs_80 = cpu_to_le16(0xfffa),
3150 .tx_mcs_80 = cpu_to_le16(0xfffa),
3151 .rx_mcs_160 = cpu_to_le16(0xfffa),
3152 .tx_mcs_160 = cpu_to_le16(0xfffa),
3153 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3154 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3161 IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3162 IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3163 IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3165 IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3166 IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3167 IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3168 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3169 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
3170 IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
3172 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
3173 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
3175 IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
3176 IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
3178 IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3179 IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3180 IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3181 IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3182 IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3183 IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3184 IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3186 IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3187 IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3188 IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3189 IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3190 IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3192 IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3193 IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3194 IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3195 IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3196 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3197 IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3199 IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3200 IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
3202 IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
3203 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
3204 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
3205 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
3206 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
3209 /* For all MCS and bandwidth, set 8 NSS for both Tx and
3212 .eht_mcs_nss_supp = {
3214 * As B1 and B2 are set in the supported
3215 * channel width set field in the HE PHY
3216 * capabilities information field include all
3217 * the following MCS/NSS.
3220 .rx_tx_mcs9_max_nss = 0x88,
3221 .rx_tx_mcs11_max_nss = 0x88,
3222 .rx_tx_mcs13_max_nss = 0x88,
3225 .rx_tx_mcs9_max_nss = 0x88,
3226 .rx_tx_mcs11_max_nss = 0x88,
3227 .rx_tx_mcs13_max_nss = 0x88,
3230 /* PPE threshold information is not supported */
3233 #ifdef CONFIG_MAC80211_MESH
3235 /* TODO: should we support other types, e.g., IBSS?*/
3236 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3241 IEEE80211_HE_MAC_CAP0_HTC_HE,
3243 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3245 IEEE80211_HE_MAC_CAP2_ACK_EN,
3247 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3248 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3249 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3251 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3252 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3253 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3255 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3256 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3257 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3258 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3259 .phy_cap_info[2] = 0,
3261 /* Leave all the other PHY capability bytes
3262 * unset, as DCM, beam forming, RU and PPE
3263 * threshold information are not supported
3266 .he_mcs_nss_supp = {
3267 .rx_mcs_80 = cpu_to_le16(0xfffa),
3268 .tx_mcs_80 = cpu_to_le16(0xfffa),
3269 .rx_mcs_160 = cpu_to_le16(0xfffa),
3270 .tx_mcs_160 = cpu_to_le16(0xfffa),
3271 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3272 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3279 static const struct ieee80211_sband_iftype_data sband_capa_6ghz[] = {
3281 /* TODO: should we support other types, e.g., P2P?*/
3282 .types_mask = BIT(NL80211_IFTYPE_STATION) |
3283 BIT(NL80211_IFTYPE_AP),
3285 .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
3286 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
3287 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
3288 IEEE80211_HE_6GHZ_CAP_SM_PS |
3289 IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
3290 IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
3291 IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
3297 IEEE80211_HE_MAC_CAP0_HTC_HE,
3299 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
3300 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3302 IEEE80211_HE_MAC_CAP2_BSR |
3303 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
3304 IEEE80211_HE_MAC_CAP2_ACK_EN,
3306 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3307 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3308 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3310 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3311 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3312 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3314 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3315 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3316 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3317 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3319 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3320 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3321 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3322 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3323 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
3325 /* Leave all the other PHY capability bytes
3326 * unset, as DCM, beam forming, RU and PPE
3327 * threshold information are not supported
3330 .he_mcs_nss_supp = {
3331 .rx_mcs_80 = cpu_to_le16(0xfffa),
3332 .tx_mcs_80 = cpu_to_le16(0xfffa),
3333 .rx_mcs_160 = cpu_to_le16(0xfffa),
3334 .tx_mcs_160 = cpu_to_le16(0xfffa),
3335 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3336 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3343 IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3344 IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3345 IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3347 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
3348 IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3349 IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3350 IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3351 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3352 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
3353 IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
3355 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
3356 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
3357 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
3359 IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
3360 IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
3361 IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
3363 IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3364 IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3365 IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3366 IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3367 IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3368 IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3369 IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3371 IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3372 IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3373 IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3374 IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3375 IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3377 IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3378 IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3379 IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3380 IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3381 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3382 IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3384 IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3385 IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
3386 IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
3388 IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
3389 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
3390 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
3391 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
3392 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
3393 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
3394 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
3397 /* For all MCS and bandwidth, set 8 NSS for both Tx and
3400 .eht_mcs_nss_supp = {
3402 * As B1 and B2 are set in the supported
3403 * channel width set field in the HE PHY
3404 * capabilities information field and 320MHz in
3405 * 6GHz is supported include all the following
3409 .rx_tx_mcs9_max_nss = 0x88,
3410 .rx_tx_mcs11_max_nss = 0x88,
3411 .rx_tx_mcs13_max_nss = 0x88,
3414 .rx_tx_mcs9_max_nss = 0x88,
3415 .rx_tx_mcs11_max_nss = 0x88,
3416 .rx_tx_mcs13_max_nss = 0x88,
3419 .rx_tx_mcs9_max_nss = 0x88,
3420 .rx_tx_mcs11_max_nss = 0x88,
3421 .rx_tx_mcs13_max_nss = 0x88,
3424 /* PPE threshold information is not supported */
3427 #ifdef CONFIG_MAC80211_MESH
3429 /* TODO: should we support other types, e.g., IBSS?*/
3430 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3432 .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
3433 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
3434 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
3435 IEEE80211_HE_6GHZ_CAP_SM_PS |
3436 IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
3437 IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
3438 IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
3444 IEEE80211_HE_MAC_CAP0_HTC_HE,
3446 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3448 IEEE80211_HE_MAC_CAP2_ACK_EN,
3450 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3451 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3452 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3454 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3455 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3456 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3458 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3459 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3460 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3461 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3462 .phy_cap_info[2] = 0,
3464 /* Leave all the other PHY capability bytes
3465 * unset, as DCM, beam forming, RU and PPE
3466 * threshold information are not supported
3469 .he_mcs_nss_supp = {
3470 .rx_mcs_80 = cpu_to_le16(0xfffa),
3471 .tx_mcs_80 = cpu_to_le16(0xfffa),
3472 .rx_mcs_160 = cpu_to_le16(0xfffa),
3473 .tx_mcs_160 = cpu_to_le16(0xfffa),
3474 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3475 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3482 static void mac80211_hwsim_sband_capab(struct ieee80211_supported_band *sband)
3486 if (sband->band == NL80211_BAND_2GHZ) {
3487 n_iftype_data = ARRAY_SIZE(sband_capa_2ghz);
3488 sband->iftype_data =
3489 (struct ieee80211_sband_iftype_data *)sband_capa_2ghz;
3490 } else if (sband->band == NL80211_BAND_5GHZ) {
3491 n_iftype_data = ARRAY_SIZE(sband_capa_5ghz);
3492 sband->iftype_data =
3493 (struct ieee80211_sband_iftype_data *)sband_capa_5ghz;
3494 } else if (sband->band == NL80211_BAND_6GHZ) {
3495 n_iftype_data = ARRAY_SIZE(sband_capa_6ghz);
3496 sband->iftype_data =
3497 (struct ieee80211_sband_iftype_data *)sband_capa_6ghz;
3502 sband->n_iftype_data = n_iftype_data;
3505 #ifdef CONFIG_MAC80211_MESH
3506 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
3508 #define HWSIM_MESH_BIT 0
3511 #define HWSIM_DEFAULT_IF_LIMIT \
3512 (BIT(NL80211_IFTYPE_STATION) | \
3513 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3514 BIT(NL80211_IFTYPE_AP) | \
3515 BIT(NL80211_IFTYPE_P2P_GO) | \
3518 #define HWSIM_IFTYPE_SUPPORT_MASK \
3519 (BIT(NL80211_IFTYPE_STATION) | \
3520 BIT(NL80211_IFTYPE_AP) | \
3521 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3522 BIT(NL80211_IFTYPE_P2P_GO) | \
3523 BIT(NL80211_IFTYPE_ADHOC) | \
3524 BIT(NL80211_IFTYPE_MESH_POINT) | \
3525 BIT(NL80211_IFTYPE_OCB))
3527 static int mac80211_hwsim_new_radio(struct genl_info *info,
3528 struct hwsim_new_radio_params *param)
3532 struct mac80211_hwsim_data *data;
3533 struct ieee80211_hw *hw;
3534 enum nl80211_band band;
3535 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
3540 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
3543 spin_lock_bh(&hwsim_radio_lock);
3544 idx = hwsim_radio_idx++;
3545 spin_unlock_bh(&hwsim_radio_lock);
3547 if (param->use_chanctx)
3548 ops = &mac80211_hwsim_mchan_ops;
3549 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
3551 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3556 /* ieee80211_alloc_hw_nm may have used a default name */
3557 param->hwname = wiphy_name(hw->wiphy);
3560 net = genl_info_net(info);
3563 wiphy_net_set(hw->wiphy, net);
3568 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3569 if (IS_ERR(data->dev)) {
3571 "mac80211_hwsim: device_create failed (%ld)\n",
3572 PTR_ERR(data->dev));
3574 goto failed_drvdata;
3576 data->dev->driver = &mac80211_hwsim_driver.driver;
3577 err = device_bind_driver(data->dev);
3579 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3584 skb_queue_head_init(&data->pending);
3586 SET_IEEE80211_DEV(hw, data->dev);
3587 if (!param->perm_addr) {
3588 eth_zero_addr(addr);
3592 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3593 /* Why need here second address ? */
3594 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3595 data->addresses[1].addr[0] |= 0x40;
3596 hw->wiphy->n_addresses = 2;
3597 hw->wiphy->addresses = data->addresses;
3598 /* possible address clash is checked at hash table insertion */
3600 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3601 /* compatibility with automatically generated mac addr */
3602 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3603 hw->wiphy->n_addresses = 2;
3604 hw->wiphy->addresses = data->addresses;
3607 data->channels = param->channels;
3608 data->use_chanctx = param->use_chanctx;
3610 data->destroy_on_close = param->destroy_on_close;
3612 data->portid = info->snd_portid;
3614 /* setup interface limits, only on interface types we support */
3615 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3616 data->if_limits[n_limits].max = 1;
3617 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3621 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3622 data->if_limits[n_limits].max = 2048;
3624 * For this case, we may only support a subset of
3625 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3626 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3628 data->if_limits[n_limits].types =
3629 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3633 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3634 data->if_limits[n_limits].max = 1;
3635 data->if_limits[n_limits].types =
3636 BIT(NL80211_IFTYPE_P2P_DEVICE);
3640 if (data->use_chanctx) {
3641 hw->wiphy->max_scan_ssids = 255;
3642 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3643 hw->wiphy->max_remain_on_channel_duration = 1000;
3644 data->if_combination.radar_detect_widths = 0;
3645 data->if_combination.num_different_channels = data->channels;
3646 data->chanctx = NULL;
3648 data->if_combination.num_different_channels = 1;
3649 data->if_combination.radar_detect_widths =
3650 BIT(NL80211_CHAN_WIDTH_5) |
3651 BIT(NL80211_CHAN_WIDTH_10) |
3652 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3653 BIT(NL80211_CHAN_WIDTH_20) |
3654 BIT(NL80211_CHAN_WIDTH_40) |
3655 BIT(NL80211_CHAN_WIDTH_80) |
3656 BIT(NL80211_CHAN_WIDTH_160);
3664 data->if_combination.max_interfaces = 0;
3665 for (i = 0; i < n_limits; i++)
3666 data->if_combination.max_interfaces +=
3667 data->if_limits[i].max;
3669 data->if_combination.n_limits = n_limits;
3670 data->if_combination.limits = data->if_limits;
3673 * If we actually were asked to support combinations,
3674 * advertise them - if there's only a single thing like
3675 * only IBSS then don't advertise it as combinations.
3677 if (data->if_combination.max_interfaces > 1) {
3678 hw->wiphy->iface_combinations = &data->if_combination;
3679 hw->wiphy->n_iface_combinations = 1;
3682 if (param->ciphers) {
3683 memcpy(data->ciphers, param->ciphers,
3684 param->n_ciphers * sizeof(u32));
3685 hw->wiphy->cipher_suites = data->ciphers;
3686 hw->wiphy->n_cipher_suites = param->n_ciphers;
3689 data->rx_rssi = DEFAULT_RX_RSSI;
3691 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3692 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3693 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3696 hw->offchannel_tx_hw_queue = 4;
3698 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3699 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3700 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3701 ieee80211_hw_set(hw, QUEUE_CONTROL);
3702 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3703 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3704 ieee80211_hw_set(hw, MFP_CAPABLE);
3705 ieee80211_hw_set(hw, SIGNAL_DBM);
3706 ieee80211_hw_set(hw, SUPPORTS_PS);
3707 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3708 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3709 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3710 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3712 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3713 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3715 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3716 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3717 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3718 WIPHY_FLAG_AP_UAPSD |
3719 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3720 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3721 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3722 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3723 NL80211_FEATURE_STATIC_SMPS |
3724 NL80211_FEATURE_DYNAMIC_SMPS |
3725 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3726 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3727 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3728 wiphy_ext_feature_set(hw->wiphy,
3729 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3730 wiphy_ext_feature_set(hw->wiphy,
3731 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3733 hw->wiphy->interface_modes = param->iftypes;
3735 /* ask mac80211 to reserve space for magic */
3736 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3737 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3738 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3740 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3741 sizeof(hwsim_channels_2ghz));
3742 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3743 sizeof(hwsim_channels_5ghz));
3744 memcpy(data->channels_6ghz, hwsim_channels_6ghz,
3745 sizeof(hwsim_channels_6ghz));
3746 memcpy(data->channels_s1g, hwsim_channels_s1g,
3747 sizeof(hwsim_channels_s1g));
3748 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3750 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3751 struct ieee80211_supported_band *sband = &data->bands[band];
3756 case NL80211_BAND_2GHZ:
3757 sband->channels = data->channels_2ghz;
3758 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3759 sband->bitrates = data->rates;
3760 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3762 case NL80211_BAND_5GHZ:
3763 sband->channels = data->channels_5ghz;
3764 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3765 sband->bitrates = data->rates + 4;
3766 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3768 sband->vht_cap.vht_supported = true;
3769 sband->vht_cap.cap =
3770 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3771 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3772 IEEE80211_VHT_CAP_RXLDPC |
3773 IEEE80211_VHT_CAP_SHORT_GI_80 |
3774 IEEE80211_VHT_CAP_SHORT_GI_160 |
3775 IEEE80211_VHT_CAP_TXSTBC |
3776 IEEE80211_VHT_CAP_RXSTBC_4 |
3777 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3778 sband->vht_cap.vht_mcs.rx_mcs_map =
3779 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3780 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3781 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3782 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3783 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3784 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3785 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3786 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3787 sband->vht_cap.vht_mcs.tx_mcs_map =
3788 sband->vht_cap.vht_mcs.rx_mcs_map;
3790 case NL80211_BAND_6GHZ:
3791 sband->channels = data->channels_6ghz;
3792 sband->n_channels = ARRAY_SIZE(hwsim_channels_6ghz);
3793 sband->bitrates = data->rates + 4;
3794 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3796 case NL80211_BAND_S1GHZ:
3797 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3798 sizeof(sband->s1g_cap));
3799 sband->channels = data->channels_s1g;
3800 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3806 if (band != NL80211_BAND_6GHZ){
3807 sband->ht_cap.ht_supported = true;
3808 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3809 IEEE80211_HT_CAP_GRN_FLD |
3810 IEEE80211_HT_CAP_SGI_20 |
3811 IEEE80211_HT_CAP_SGI_40 |
3812 IEEE80211_HT_CAP_DSSSCCK40;
3813 sband->ht_cap.ampdu_factor = 0x3;
3814 sband->ht_cap.ampdu_density = 0x6;
3815 memset(&sband->ht_cap.mcs, 0,
3816 sizeof(sband->ht_cap.mcs));
3817 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3818 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3819 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3822 mac80211_hwsim_sband_capab(sband);
3824 hw->wiphy->bands[band] = sband;
3827 /* By default all radios belong to the first group */
3829 mutex_init(&data->mutex);
3831 data->netgroup = hwsim_net_get_netgroup(net);
3832 data->wmediumd = hwsim_net_get_wmediumd(net);
3834 /* Enable frame retransmissions for lossy channels */
3836 hw->max_rate_tries = 11;
3838 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3839 hw->wiphy->n_vendor_commands =
3840 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3841 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3842 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3844 if (param->reg_strict)
3845 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3847 data->regd = param->regd;
3848 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3849 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3850 /* give the regulatory workqueue a chance to run */
3851 schedule_timeout_interruptible(1);
3855 ieee80211_hw_set(hw, NO_AUTO_VIF);
3857 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3859 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3860 HRTIMER_MODE_ABS_SOFT);
3861 data->beacon_timer.function = mac80211_hwsim_beacon;
3863 err = ieee80211_register_hw(hw);
3865 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3870 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3872 if (param->reg_alpha2) {
3873 data->alpha2[0] = param->reg_alpha2[0];
3874 data->alpha2[1] = param->reg_alpha2[1];
3875 regulatory_hint(hw->wiphy, param->reg_alpha2);
3878 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3879 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3880 debugfs_create_file("group", 0666, data->debugfs, data,
3882 debugfs_create_file("rx_rssi", 0666, data->debugfs, data,
3883 &hwsim_fops_rx_rssi);
3884 if (!data->use_chanctx)
3885 debugfs_create_file("dfs_simulate_radar", 0222,
3887 data, &hwsim_simulate_radar);
3889 spin_lock_bh(&hwsim_radio_lock);
3890 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3894 GENL_SET_ERR_MSG(info, "perm addr already present");
3895 NL_SET_BAD_ATTR(info->extack,
3896 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3898 spin_unlock_bh(&hwsim_radio_lock);
3899 goto failed_final_insert;
3902 list_add_tail(&data->list, &hwsim_radios);
3903 hwsim_radios_generation++;
3904 spin_unlock_bh(&hwsim_radio_lock);
3906 hwsim_mcast_new_radio(idx, info, param);
3910 failed_final_insert:
3911 debugfs_remove_recursive(data->debugfs);
3912 ieee80211_unregister_hw(data->hw);
3914 device_release_driver(data->dev);
3916 device_unregister(data->dev);
3918 ieee80211_free_hw(hw);
3923 static void hwsim_mcast_del_radio(int id, const char *hwname,
3924 struct genl_info *info)
3926 struct sk_buff *skb;
3930 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3934 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3935 HWSIM_CMD_DEL_RADIO);
3939 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3943 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3948 genlmsg_end(skb, data);
3950 hwsim_mcast_config_msg(skb, info);
3958 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3960 struct genl_info *info)
3962 hwsim_mcast_del_radio(data->idx, hwname, info);
3963 debugfs_remove_recursive(data->debugfs);
3964 ieee80211_unregister_hw(data->hw);
3965 device_release_driver(data->dev);
3966 device_unregister(data->dev);
3967 ieee80211_free_hw(data->hw);
3970 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3971 struct mac80211_hwsim_data *data,
3972 u32 portid, u32 seq,
3973 struct netlink_callback *cb, int flags)
3976 struct hwsim_new_radio_params param = { };
3977 int res = -EMSGSIZE;
3979 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3980 HWSIM_CMD_GET_RADIO);
3985 genl_dump_check_consistent(cb, hdr);
3987 if (data->alpha2[0] && data->alpha2[1])
3988 param.reg_alpha2 = data->alpha2;
3990 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3991 REGULATORY_STRICT_REG);
3992 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3993 BIT(NL80211_IFTYPE_P2P_DEVICE));
3994 param.use_chanctx = data->use_chanctx;
3995 param.regd = data->regd;
3996 param.channels = data->channels;
3997 param.hwname = wiphy_name(data->hw->wiphy);
3999 res = append_radio_msg(skb, data->idx, ¶m);
4003 genlmsg_end(skb, hdr);
4007 genlmsg_cancel(skb, hdr);
4011 static void mac80211_hwsim_free(void)
4013 struct mac80211_hwsim_data *data;
4015 spin_lock_bh(&hwsim_radio_lock);
4016 while ((data = list_first_entry_or_null(&hwsim_radios,
4017 struct mac80211_hwsim_data,
4019 list_del(&data->list);
4020 spin_unlock_bh(&hwsim_radio_lock);
4021 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
4023 spin_lock_bh(&hwsim_radio_lock);
4025 spin_unlock_bh(&hwsim_radio_lock);
4026 class_destroy(hwsim_class);
4029 static const struct net_device_ops hwsim_netdev_ops = {
4030 .ndo_start_xmit = hwsim_mon_xmit,
4031 .ndo_set_mac_address = eth_mac_addr,
4032 .ndo_validate_addr = eth_validate_addr,
4035 static void hwsim_mon_setup(struct net_device *dev)
4039 dev->netdev_ops = &hwsim_netdev_ops;
4040 dev->needs_free_netdev = true;
4042 dev->priv_flags |= IFF_NO_QUEUE;
4043 dev->type = ARPHRD_IEEE80211_RADIOTAP;
4044 eth_zero_addr(addr);
4046 eth_hw_addr_set(dev, addr);
4049 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
4051 return rhashtable_lookup_fast(&hwsim_radios_rht,
4056 static void hwsim_register_wmediumd(struct net *net, u32 portid)
4058 struct mac80211_hwsim_data *data;
4060 hwsim_net_set_wmediumd(net, portid);
4062 spin_lock_bh(&hwsim_radio_lock);
4063 list_for_each_entry(data, &hwsim_radios, list) {
4064 if (data->netgroup == hwsim_net_get_netgroup(net))
4065 data->wmediumd = portid;
4067 spin_unlock_bh(&hwsim_radio_lock);
4070 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
4071 struct genl_info *info)
4074 struct ieee80211_hdr *hdr;
4075 struct mac80211_hwsim_data *data2;
4076 struct ieee80211_tx_info *txi;
4077 struct hwsim_tx_rate *tx_attempts;
4079 struct sk_buff *skb, *tmp;
4081 unsigned int hwsim_flags;
4085 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
4086 !info->attrs[HWSIM_ATTR_FLAGS] ||
4087 !info->attrs[HWSIM_ATTR_COOKIE] ||
4088 !info->attrs[HWSIM_ATTR_SIGNAL] ||
4089 !info->attrs[HWSIM_ATTR_TX_INFO])
4092 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
4093 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
4094 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
4096 data2 = get_hwsim_data_ref_from_addr(src);
4100 if (!hwsim_virtio_enabled) {
4101 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
4105 if (info->snd_portid != data2->wmediumd)
4109 /* look for the skb matching the cookie passed back from user */
4110 skb_queue_walk_safe(&data2->pending, skb, tmp) {
4113 txi = IEEE80211_SKB_CB(skb);
4114 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
4116 if (skb_cookie == ret_skb_cookie) {
4117 skb_unlink(skb, &data2->pending);
4127 /* Tx info received because the frame was broadcasted on user space,
4128 so we get all the necessary info: tx attempts and skb control buff */
4130 tx_attempts = (struct hwsim_tx_rate *)nla_data(
4131 info->attrs[HWSIM_ATTR_TX_INFO]);
4133 /* now send back TX status */
4134 txi = IEEE80211_SKB_CB(skb);
4136 ieee80211_tx_info_clear_status(txi);
4138 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
4139 txi->status.rates[i].idx = tx_attempts[i].idx;
4140 txi->status.rates[i].count = tx_attempts[i].count;
4143 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
4145 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
4146 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
4147 if (skb->len >= 16) {
4148 hdr = (struct ieee80211_hdr *) skb->data;
4149 mac80211_hwsim_monitor_ack(data2->channel,
4152 txi->flags |= IEEE80211_TX_STAT_ACK;
4155 if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
4156 txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
4158 ieee80211_tx_status_irqsafe(data2->hw, skb);
4165 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
4166 struct genl_info *info)
4168 struct mac80211_hwsim_data *data2;
4169 struct ieee80211_rx_status rx_status;
4170 struct ieee80211_hdr *hdr;
4174 struct sk_buff *skb = NULL;
4175 struct ieee80211_channel *channel = NULL;
4177 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
4178 !info->attrs[HWSIM_ATTR_FRAME] ||
4179 !info->attrs[HWSIM_ATTR_RX_RATE] ||
4180 !info->attrs[HWSIM_ATTR_SIGNAL])
4183 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
4184 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
4185 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
4187 /* Allocate new skb here */
4188 skb = alloc_skb(frame_data_len, GFP_KERNEL);
4192 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
4196 skb_put_data(skb, frame_data, frame_data_len);
4198 data2 = get_hwsim_data_ref_from_addr(dst);
4202 if (data2->use_chanctx) {
4203 if (data2->tmp_chan)
4204 channel = data2->tmp_chan;
4205 else if (data2->chanctx)
4206 channel = data2->chanctx->def.chan;
4208 channel = data2->channel;
4213 if (!hwsim_virtio_enabled) {
4214 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
4218 if (info->snd_portid != data2->wmediumd)
4222 /* check if radio is configured properly */
4224 if ((data2->idle && !data2->tmp_chan) || !data2->started)
4227 /* A frame is received from user space */
4228 memset(&rx_status, 0, sizeof(rx_status));
4229 if (info->attrs[HWSIM_ATTR_FREQ]) {
4230 /* throw away off-channel packets, but allow both the temporary
4231 * ("hw" scan/remain-on-channel) and regular channel, since the
4232 * internal datapath also allows this
4234 mutex_lock(&data2->mutex);
4235 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
4237 if (rx_status.freq != channel->center_freq) {
4238 mutex_unlock(&data2->mutex);
4241 mutex_unlock(&data2->mutex);
4243 rx_status.freq = channel->center_freq;
4246 rx_status.band = channel->band;
4247 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
4248 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
4250 hdr = (void *)skb->data;
4252 if (ieee80211_is_beacon(hdr->frame_control) ||
4253 ieee80211_is_probe_resp(hdr->frame_control))
4254 rx_status.boottime_ns = ktime_get_boottime_ns();
4256 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
4258 data2->rx_bytes += skb->len;
4259 ieee80211_rx_irqsafe(data2->hw, skb);
4263 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4269 static int hwsim_register_received_nl(struct sk_buff *skb_2,
4270 struct genl_info *info)
4272 struct net *net = genl_info_net(info);
4273 struct mac80211_hwsim_data *data;
4276 spin_lock_bh(&hwsim_radio_lock);
4277 list_for_each_entry(data, &hwsim_radios, list)
4278 chans = max(chans, data->channels);
4279 spin_unlock_bh(&hwsim_radio_lock);
4281 /* In the future we should revise the userspace API and allow it
4282 * to set a flag that it does support multi-channel, then we can
4283 * let this pass conditionally on the flag.
4284 * For current userspace, prohibit it since it won't work right.
4289 if (hwsim_net_get_wmediumd(net))
4292 hwsim_register_wmediumd(net, info->snd_portid);
4294 pr_debug("mac80211_hwsim: received a REGISTER, "
4295 "switching to wmediumd mode with pid %d\n", info->snd_portid);
4300 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
4301 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
4305 for (i = 0; i < n_ciphers; i++) {
4309 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
4310 if (ciphers[i] == hwsim_ciphers[j]) {
4323 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
4325 struct hwsim_new_radio_params param = { 0 };
4326 const char *hwname = NULL;
4329 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
4330 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
4331 param.channels = channels;
4332 param.destroy_on_close =
4333 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
4335 if (info->attrs[HWSIM_ATTR_CHANNELS])
4336 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
4338 if (param.channels < 1) {
4339 GENL_SET_ERR_MSG(info, "must have at least one channel");
4343 if (info->attrs[HWSIM_ATTR_NO_VIF])
4344 param.no_vif = true;
4346 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
4347 param.use_chanctx = true;
4349 param.use_chanctx = (param.channels > 1);
4351 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
4353 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
4355 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
4356 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
4358 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
4361 idx = array_index_nospec(idx,
4362 ARRAY_SIZE(hwsim_world_regdom_custom));
4363 param.regd = hwsim_world_regdom_custom[idx];
4366 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
4367 if (!is_valid_ether_addr(
4368 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
4369 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
4370 NL_SET_BAD_ATTR(info->extack,
4371 info->attrs[HWSIM_ATTR_PERM_ADDR]);
4375 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
4378 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
4380 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
4382 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
4383 NL_SET_ERR_MSG_ATTR(info->extack,
4384 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
4385 "cannot support more iftypes than kernel");
4389 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4392 /* ensure both flag and iftype support is honored */
4393 if (param.p2p_device ||
4394 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
4395 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4396 param.p2p_device = true;
4399 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
4400 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
4403 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
4405 if (len % sizeof(u32)) {
4406 NL_SET_ERR_MSG_ATTR(info->extack,
4407 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4408 "bad cipher list length");
4412 param.n_ciphers = len / sizeof(u32);
4414 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
4415 NL_SET_ERR_MSG_ATTR(info->extack,
4416 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4417 "too many ciphers specified");
4421 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
4422 NL_SET_ERR_MSG_ATTR(info->extack,
4423 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4424 "unsupported ciphers specified");
4429 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
4430 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4431 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4435 param.hwname = hwname;
4438 ret = mac80211_hwsim_new_radio(info, ¶m);
4443 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
4445 struct mac80211_hwsim_data *data;
4447 const char *hwname = NULL;
4449 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
4450 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
4451 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
4452 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4453 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4460 spin_lock_bh(&hwsim_radio_lock);
4461 list_for_each_entry(data, &hwsim_radios, list) {
4463 if (data->idx != idx)
4467 strcmp(hwname, wiphy_name(data->hw->wiphy)))
4471 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
4474 list_del(&data->list);
4475 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4477 hwsim_radios_generation++;
4478 spin_unlock_bh(&hwsim_radio_lock);
4479 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
4484 spin_unlock_bh(&hwsim_radio_lock);
4490 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
4492 struct mac80211_hwsim_data *data;
4493 struct sk_buff *skb;
4494 int idx, res = -ENODEV;
4496 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
4498 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
4500 spin_lock_bh(&hwsim_radio_lock);
4501 list_for_each_entry(data, &hwsim_radios, list) {
4502 if (data->idx != idx)
4505 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
4508 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
4514 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
4515 info->snd_seq, NULL, 0);
4521 res = genlmsg_reply(skb, info);
4526 spin_unlock_bh(&hwsim_radio_lock);
4531 static int hwsim_dump_radio_nl(struct sk_buff *skb,
4532 struct netlink_callback *cb)
4534 int last_idx = cb->args[0] - 1;
4535 struct mac80211_hwsim_data *data = NULL;
4539 spin_lock_bh(&hwsim_radio_lock);
4540 cb->seq = hwsim_radios_generation;
4542 if (last_idx >= hwsim_radio_idx-1)
4545 list_for_each_entry(data, &hwsim_radios, list) {
4546 if (data->idx <= last_idx)
4549 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
4552 res = mac80211_hwsim_get_radio(skb, data,
4553 NETLINK_CB(cb->skb).portid,
4554 cb->nlh->nlmsg_seq, cb,
4559 last_idx = data->idx;
4562 cb->args[0] = last_idx + 1;
4564 /* list changed, but no new element sent, set interrupted flag */
4565 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4566 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4567 cb->nlh->nlmsg_seq, &hwsim_genl_family,
4568 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4570 genl_dump_check_consistent(cb, hdr);
4571 genlmsg_end(skb, hdr);
4578 spin_unlock_bh(&hwsim_radio_lock);
4579 return res ?: skb->len;
4582 /* Generic Netlink operations array */
4583 static const struct genl_small_ops hwsim_ops[] = {
4585 .cmd = HWSIM_CMD_REGISTER,
4586 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4587 .doit = hwsim_register_received_nl,
4588 .flags = GENL_UNS_ADMIN_PERM,
4591 .cmd = HWSIM_CMD_FRAME,
4592 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4593 .doit = hwsim_cloned_frame_received_nl,
4596 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4597 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4598 .doit = hwsim_tx_info_frame_received_nl,
4601 .cmd = HWSIM_CMD_NEW_RADIO,
4602 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4603 .doit = hwsim_new_radio_nl,
4604 .flags = GENL_UNS_ADMIN_PERM,
4607 .cmd = HWSIM_CMD_DEL_RADIO,
4608 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4609 .doit = hwsim_del_radio_nl,
4610 .flags = GENL_UNS_ADMIN_PERM,
4613 .cmd = HWSIM_CMD_GET_RADIO,
4614 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4615 .doit = hwsim_get_radio_nl,
4616 .dumpit = hwsim_dump_radio_nl,
4620 static struct genl_family hwsim_genl_family __ro_after_init = {
4621 .name = "MAC80211_HWSIM",
4623 .maxattr = HWSIM_ATTR_MAX,
4624 .policy = hwsim_genl_policy,
4626 .module = THIS_MODULE,
4627 .small_ops = hwsim_ops,
4628 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4629 .mcgrps = hwsim_mcgrps,
4630 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4633 static void remove_user_radios(u32 portid)
4635 struct mac80211_hwsim_data *entry, *tmp;
4638 spin_lock_bh(&hwsim_radio_lock);
4639 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4640 if (entry->destroy_on_close && entry->portid == portid) {
4641 list_move(&entry->list, &list);
4642 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4644 hwsim_radios_generation++;
4647 spin_unlock_bh(&hwsim_radio_lock);
4649 list_for_each_entry_safe(entry, tmp, &list, list) {
4650 list_del(&entry->list);
4651 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4656 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4657 unsigned long state,
4660 struct netlink_notify *notify = _notify;
4662 if (state != NETLINK_URELEASE)
4665 remove_user_radios(notify->portid);
4667 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4668 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4669 " socket, switching to perfect channel medium\n");
4670 hwsim_register_wmediumd(notify->net, 0);
4676 static struct notifier_block hwsim_netlink_notifier = {
4677 .notifier_call = mac80211_hwsim_netlink_notify,
4680 static int __init hwsim_init_netlink(void)
4684 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4686 rc = genl_register_family(&hwsim_genl_family);
4690 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4692 genl_unregister_family(&hwsim_genl_family);
4699 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4703 static __net_init int hwsim_init_net(struct net *net)
4705 return hwsim_net_set_netgroup(net);
4708 static void __net_exit hwsim_exit_net(struct net *net)
4710 struct mac80211_hwsim_data *data, *tmp;
4713 spin_lock_bh(&hwsim_radio_lock);
4714 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4715 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4718 /* Radios created in init_net are returned to init_net. */
4719 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4722 list_move(&data->list, &list);
4723 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4725 hwsim_radios_generation++;
4727 spin_unlock_bh(&hwsim_radio_lock);
4729 list_for_each_entry_safe(data, tmp, &list, list) {
4730 list_del(&data->list);
4731 mac80211_hwsim_del_radio(data,
4732 wiphy_name(data->hw->wiphy),
4736 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4739 static struct pernet_operations hwsim_net_ops = {
4740 .init = hwsim_init_net,
4741 .exit = hwsim_exit_net,
4742 .id = &hwsim_net_id,
4743 .size = sizeof(struct hwsim_net),
4746 static void hwsim_exit_netlink(void)
4748 /* unregister the notifier */
4749 netlink_unregister_notifier(&hwsim_netlink_notifier);
4750 /* unregister the family */
4751 genl_unregister_family(&hwsim_genl_family);
4754 #if IS_REACHABLE(CONFIG_VIRTIO)
4755 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4758 struct sk_buff *skb;
4759 unsigned long flags;
4761 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4762 while ((skb = virtqueue_get_buf(vq, &len)))
4764 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4767 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4769 struct nlmsghdr *nlh;
4770 struct genlmsghdr *gnlh;
4771 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4772 struct genl_info info = {};
4775 nlh = nlmsg_hdr(skb);
4776 gnlh = nlmsg_data(nlh);
4777 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4778 hwsim_genl_policy, NULL);
4780 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4786 switch (gnlh->cmd) {
4787 case HWSIM_CMD_FRAME:
4788 hwsim_cloned_frame_received_nl(skb, &info);
4790 case HWSIM_CMD_TX_INFO_FRAME:
4791 hwsim_tx_info_frame_received_nl(skb, &info);
4794 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4800 static void hwsim_virtio_rx_work(struct work_struct *work)
4802 struct virtqueue *vq;
4804 struct sk_buff *skb;
4805 struct scatterlist sg[1];
4807 unsigned long flags;
4809 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4810 if (!hwsim_virtio_enabled)
4813 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4816 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4818 skb->data = skb->head;
4819 skb_set_tail_pointer(skb, len);
4820 hwsim_virtio_handle_cmd(skb);
4822 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4823 if (!hwsim_virtio_enabled) {
4827 vq = hwsim_vqs[HWSIM_VQ_RX];
4828 sg_init_one(sg, skb->head, skb_end_offset(skb));
4829 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4830 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4834 schedule_work(&hwsim_virtio_rx);
4837 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4840 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4842 schedule_work(&hwsim_virtio_rx);
4845 static int init_vqs(struct virtio_device *vdev)
4847 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4848 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4849 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4851 const char *names[HWSIM_NUM_VQS] = {
4852 [HWSIM_VQ_TX] = "tx",
4853 [HWSIM_VQ_RX] = "rx",
4856 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4857 hwsim_vqs, callbacks, names, NULL);
4860 static int fill_vq(struct virtqueue *vq)
4863 struct sk_buff *skb;
4864 struct scatterlist sg[1];
4866 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4867 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4871 sg_init_one(sg, skb->head, skb_end_offset(skb));
4872 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4882 static void remove_vqs(struct virtio_device *vdev)
4886 virtio_reset_device(vdev);
4888 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4889 struct virtqueue *vq = hwsim_vqs[i];
4890 struct sk_buff *skb;
4892 while ((skb = virtqueue_detach_unused_buf(vq)))
4896 vdev->config->del_vqs(vdev);
4899 static int hwsim_virtio_probe(struct virtio_device *vdev)
4902 unsigned long flags;
4904 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4905 if (hwsim_virtio_enabled) {
4906 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4909 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4911 err = init_vqs(vdev);
4915 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4919 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4920 hwsim_virtio_enabled = true;
4921 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4923 schedule_work(&hwsim_virtio_rx);
4931 static void hwsim_virtio_remove(struct virtio_device *vdev)
4933 hwsim_virtio_enabled = false;
4935 cancel_work_sync(&hwsim_virtio_rx);
4940 /* MAC80211_HWSIM virtio device id table */
4941 static const struct virtio_device_id id_table[] = {
4942 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4945 MODULE_DEVICE_TABLE(virtio, id_table);
4947 static struct virtio_driver virtio_hwsim = {
4948 .driver.name = KBUILD_MODNAME,
4949 .driver.owner = THIS_MODULE,
4950 .id_table = id_table,
4951 .probe = hwsim_virtio_probe,
4952 .remove = hwsim_virtio_remove,
4955 static int hwsim_register_virtio_driver(void)
4957 return register_virtio_driver(&virtio_hwsim);
4960 static void hwsim_unregister_virtio_driver(void)
4962 unregister_virtio_driver(&virtio_hwsim);
4965 static inline int hwsim_register_virtio_driver(void)
4970 static inline void hwsim_unregister_virtio_driver(void)
4975 static int __init init_mac80211_hwsim(void)
4979 if (radios < 0 || radios > 100)
4985 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4989 err = register_pernet_device(&hwsim_net_ops);
4993 err = platform_driver_register(&mac80211_hwsim_driver);
4995 goto out_unregister_pernet;
4997 err = hwsim_init_netlink();
4999 goto out_unregister_driver;
5001 err = hwsim_register_virtio_driver();
5003 goto out_exit_netlink;
5005 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
5006 if (IS_ERR(hwsim_class)) {
5007 err = PTR_ERR(hwsim_class);
5008 goto out_exit_virtio;
5011 hwsim_init_s1g_channels(hwsim_channels_s1g);
5013 for (i = 0; i < radios; i++) {
5014 struct hwsim_new_radio_params param = { 0 };
5016 param.channels = channels;
5019 case HWSIM_REGTEST_DIFF_COUNTRY:
5020 if (i < ARRAY_SIZE(hwsim_alpha2s))
5021 param.reg_alpha2 = hwsim_alpha2s[i];
5023 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
5025 param.reg_alpha2 = hwsim_alpha2s[0];
5027 case HWSIM_REGTEST_STRICT_ALL:
5028 param.reg_strict = true;
5030 case HWSIM_REGTEST_DRIVER_REG_ALL:
5031 param.reg_alpha2 = hwsim_alpha2s[0];
5033 case HWSIM_REGTEST_WORLD_ROAM:
5035 param.regd = &hwsim_world_regdom_custom_01;
5037 case HWSIM_REGTEST_CUSTOM_WORLD:
5038 param.regd = &hwsim_world_regdom_custom_01;
5040 case HWSIM_REGTEST_CUSTOM_WORLD_2:
5042 param.regd = &hwsim_world_regdom_custom_01;
5044 param.regd = &hwsim_world_regdom_custom_02;
5046 case HWSIM_REGTEST_STRICT_FOLLOW:
5048 param.reg_strict = true;
5049 param.reg_alpha2 = hwsim_alpha2s[0];
5052 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
5054 param.reg_strict = true;
5055 param.reg_alpha2 = hwsim_alpha2s[0];
5056 } else if (i == 1) {
5057 param.reg_alpha2 = hwsim_alpha2s[1];
5060 case HWSIM_REGTEST_ALL:
5063 param.regd = &hwsim_world_regdom_custom_01;
5066 param.regd = &hwsim_world_regdom_custom_02;
5069 param.reg_alpha2 = hwsim_alpha2s[0];
5072 param.reg_alpha2 = hwsim_alpha2s[1];
5075 param.reg_strict = true;
5076 param.reg_alpha2 = hwsim_alpha2s[2];
5084 param.p2p_device = support_p2p_device;
5085 param.use_chanctx = channels > 1;
5086 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
5087 if (param.p2p_device)
5088 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
5090 err = mac80211_hwsim_new_radio(NULL, ¶m);
5092 goto out_free_radios;
5095 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
5097 if (hwsim_mon == NULL) {
5099 goto out_free_radios;
5103 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
5109 err = register_netdevice(hwsim_mon);
5119 free_netdev(hwsim_mon);
5121 mac80211_hwsim_free();
5123 hwsim_unregister_virtio_driver();
5125 hwsim_exit_netlink();
5126 out_unregister_driver:
5127 platform_driver_unregister(&mac80211_hwsim_driver);
5128 out_unregister_pernet:
5129 unregister_pernet_device(&hwsim_net_ops);
5131 rhashtable_destroy(&hwsim_radios_rht);
5134 module_init(init_mac80211_hwsim);
5136 static void __exit exit_mac80211_hwsim(void)
5138 pr_debug("mac80211_hwsim: unregister radios\n");
5140 hwsim_unregister_virtio_driver();
5141 hwsim_exit_netlink();
5143 mac80211_hwsim_free();
5145 rhashtable_destroy(&hwsim_radios_rht);
5146 unregister_netdev(hwsim_mon);
5147 platform_driver_unregister(&mac80211_hwsim_driver);
5148 unregister_pernet_device(&hwsim_net_ops);
5150 module_exit(exit_mac80211_hwsim);