1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
4 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
6 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018 - 2020 Intel Corporation
12 * - Add TSF sync and fix IBSS beacon transmission by adding
13 * competition for "air time" at TBTT
14 * - RX filtering based on filter configuration (data->rx_filter)
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
22 #include <net/mac80211.h>
23 #include <net/ieee80211_radiotap.h>
24 #include <linux/if_arp.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/etherdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/debugfs.h>
29 #include <linux/module.h>
30 #include <linux/ktime.h>
31 #include <net/genetlink.h>
32 #include <net/net_namespace.h>
33 #include <net/netns/generic.h>
34 #include <linux/rhashtable.h>
35 #include <linux/nospec.h>
36 #include <linux/virtio.h>
37 #include <linux/virtio_ids.h>
38 #include <linux/virtio_config.h>
39 #include "mac80211_hwsim.h"
41 #define WARN_QUEUE 100
44 MODULE_AUTHOR("Jouni Malinen");
45 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46 MODULE_LICENSE("GPL");
48 static int radios = 2;
49 module_param(radios, int, 0444);
50 MODULE_PARM_DESC(radios, "Number of simulated radios");
52 static int channels = 1;
53 module_param(channels, int, 0444);
54 MODULE_PARM_DESC(channels, "Number of concurrent channels");
56 static bool paged_rx = false;
57 module_param(paged_rx, bool, 0644);
58 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
60 static bool rctbl = false;
61 module_param(rctbl, bool, 0444);
62 MODULE_PARM_DESC(rctbl, "Handle rate control table");
64 static bool support_p2p_device = true;
65 module_param(support_p2p_device, bool, 0444);
66 MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
69 * enum hwsim_regtest - the type of regulatory tests we offer
71 * These are the different values you can use for the regtest
72 * module parameter. This is useful to help test world roaming
73 * and the driver regulatory_hint() call and combinations of these.
74 * If you want to do specific alpha2 regulatory domain tests simply
75 * use the userspace regulatory request as that will be respected as
76 * well without the need of this module parameter. This is designed
77 * only for testing the driver regulatory request, world roaming
78 * and all possible combinations.
80 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
81 * this is the default value.
82 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
83 * hint, only one driver regulatory hint will be sent as such the
84 * secondary radios are expected to follow.
85 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
86 * request with all radios reporting the same regulatory domain.
87 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
88 * different regulatory domains requests. Expected behaviour is for
89 * an intersection to occur but each device will still use their
90 * respective regulatory requested domains. Subsequent radios will
91 * use the resulting intersection.
92 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
93 * this by using a custom beacon-capable regulatory domain for the first
94 * radio. All other device world roam.
95 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
96 * domain requests. All radios will adhere to this custom world regulatory
98 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
99 * domain requests. The first radio will adhere to the first custom world
100 * regulatory domain, the second one to the second custom world regulatory
101 * domain. All other devices will world roam.
102 * @HWSIM_REGTEST_STRICT_FOLLOW: Used for testing strict regulatory domain
103 * settings, only the first radio will send a regulatory domain request
104 * and use strict settings. The rest of the radios are expected to follow.
105 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
106 * settings. All radios will adhere to this.
107 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
108 * domain settings, combined with secondary driver regulatory domain
109 * settings. The first radio will get a strict regulatory domain setting
110 * using the first driver regulatory request and the second radio will use
111 * non-strict settings using the second driver regulatory request. All
112 * other devices should follow the intersection created between the
114 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
115 * at least 6 radios for a complete test. We will test in this order:
116 * 1 - driver custom world regulatory domain
117 * 2 - second custom world regulatory domain
118 * 3 - first driver regulatory domain request
119 * 4 - second driver regulatory domain request
120 * 5 - strict regulatory domain settings using the third driver regulatory
122 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
123 * regulatory requests.
126 HWSIM_REGTEST_DISABLED = 0,
127 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
128 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
129 HWSIM_REGTEST_DIFF_COUNTRY = 3,
130 HWSIM_REGTEST_WORLD_ROAM = 4,
131 HWSIM_REGTEST_CUSTOM_WORLD = 5,
132 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
133 HWSIM_REGTEST_STRICT_FOLLOW = 7,
134 HWSIM_REGTEST_STRICT_ALL = 8,
135 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
136 HWSIM_REGTEST_ALL = 10,
139 /* Set to one of the HWSIM_REGTEST_* values above */
140 static int regtest = HWSIM_REGTEST_DISABLED;
141 module_param(regtest, int, 0444);
142 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
144 static const char *hwsim_alpha2s[] = {
153 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
157 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
158 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
159 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
160 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
161 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
165 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
169 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
170 REG_RULE(5725-10, 5850+10, 40, 0, 30,
172 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
176 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
177 &hwsim_world_regdom_custom_01,
178 &hwsim_world_regdom_custom_02,
181 struct hwsim_vif_priv {
189 #define HWSIM_VIF_MAGIC 0x69537748
191 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
193 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
194 WARN(vp->magic != HWSIM_VIF_MAGIC,
195 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
196 vif, vp->magic, vif->addr, vif->type, vif->p2p);
199 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
201 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
202 vp->magic = HWSIM_VIF_MAGIC;
205 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
207 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
211 struct hwsim_sta_priv {
215 #define HWSIM_STA_MAGIC 0x6d537749
217 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
219 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
220 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
223 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
225 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
226 sp->magic = HWSIM_STA_MAGIC;
229 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
231 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
235 struct hwsim_chanctx_priv {
239 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
241 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
243 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
244 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
247 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
249 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
250 cp->magic = HWSIM_CHANCTX_MAGIC;
253 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
255 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
259 static unsigned int hwsim_net_id;
261 static DEFINE_IDA(hwsim_netgroup_ida);
268 static inline int hwsim_net_get_netgroup(struct net *net)
270 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
272 return hwsim_net->netgroup;
275 static inline int hwsim_net_set_netgroup(struct net *net)
277 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
279 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
281 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
284 static inline u32 hwsim_net_get_wmediumd(struct net *net)
286 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
288 return hwsim_net->wmediumd;
291 static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
293 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
295 hwsim_net->wmediumd = portid;
298 static struct class *hwsim_class;
300 static struct net_device *hwsim_mon; /* global monitor netdev */
302 #define CHAN2G(_freq) { \
303 .band = NL80211_BAND_2GHZ, \
304 .center_freq = (_freq), \
305 .hw_value = (_freq), \
308 #define CHAN5G(_freq) { \
309 .band = NL80211_BAND_5GHZ, \
310 .center_freq = (_freq), \
311 .hw_value = (_freq), \
314 #define CHAN6G(_freq) { \
315 .band = NL80211_BAND_6GHZ, \
316 .center_freq = (_freq), \
317 .hw_value = (_freq), \
320 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
321 CHAN2G(2412), /* Channel 1 */
322 CHAN2G(2417), /* Channel 2 */
323 CHAN2G(2422), /* Channel 3 */
324 CHAN2G(2427), /* Channel 4 */
325 CHAN2G(2432), /* Channel 5 */
326 CHAN2G(2437), /* Channel 6 */
327 CHAN2G(2442), /* Channel 7 */
328 CHAN2G(2447), /* Channel 8 */
329 CHAN2G(2452), /* Channel 9 */
330 CHAN2G(2457), /* Channel 10 */
331 CHAN2G(2462), /* Channel 11 */
332 CHAN2G(2467), /* Channel 12 */
333 CHAN2G(2472), /* Channel 13 */
334 CHAN2G(2484), /* Channel 14 */
337 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
338 CHAN5G(5180), /* Channel 36 */
339 CHAN5G(5200), /* Channel 40 */
340 CHAN5G(5220), /* Channel 44 */
341 CHAN5G(5240), /* Channel 48 */
343 CHAN5G(5260), /* Channel 52 */
344 CHAN5G(5280), /* Channel 56 */
345 CHAN5G(5300), /* Channel 60 */
346 CHAN5G(5320), /* Channel 64 */
348 CHAN5G(5500), /* Channel 100 */
349 CHAN5G(5520), /* Channel 104 */
350 CHAN5G(5540), /* Channel 108 */
351 CHAN5G(5560), /* Channel 112 */
352 CHAN5G(5580), /* Channel 116 */
353 CHAN5G(5600), /* Channel 120 */
354 CHAN5G(5620), /* Channel 124 */
355 CHAN5G(5640), /* Channel 128 */
356 CHAN5G(5660), /* Channel 132 */
357 CHAN5G(5680), /* Channel 136 */
358 CHAN5G(5700), /* Channel 140 */
360 CHAN5G(5745), /* Channel 149 */
361 CHAN5G(5765), /* Channel 153 */
362 CHAN5G(5785), /* Channel 157 */
363 CHAN5G(5805), /* Channel 161 */
364 CHAN5G(5825), /* Channel 165 */
365 CHAN5G(5845), /* Channel 169 */
367 CHAN5G(5855), /* Channel 171 */
368 CHAN5G(5860), /* Channel 172 */
369 CHAN5G(5865), /* Channel 173 */
370 CHAN5G(5870), /* Channel 174 */
372 CHAN5G(5875), /* Channel 175 */
373 CHAN5G(5880), /* Channel 176 */
374 CHAN5G(5885), /* Channel 177 */
375 CHAN5G(5890), /* Channel 178 */
376 CHAN5G(5895), /* Channel 179 */
377 CHAN5G(5900), /* Channel 180 */
378 CHAN5G(5905), /* Channel 181 */
380 CHAN5G(5910), /* Channel 182 */
381 CHAN5G(5915), /* Channel 183 */
382 CHAN5G(5920), /* Channel 184 */
383 CHAN5G(5925), /* Channel 185 */
386 static const struct ieee80211_channel hwsim_channels_6ghz[] = {
387 CHAN6G(5955), /* Channel 1 */
388 CHAN6G(5975), /* Channel 5 */
389 CHAN6G(5995), /* Channel 9 */
390 CHAN6G(6015), /* Channel 13 */
391 CHAN6G(6035), /* Channel 17 */
392 CHAN6G(6055), /* Channel 21 */
393 CHAN6G(6075), /* Channel 25 */
394 CHAN6G(6095), /* Channel 29 */
395 CHAN6G(6115), /* Channel 33 */
396 CHAN6G(6135), /* Channel 37 */
397 CHAN6G(6155), /* Channel 41 */
398 CHAN6G(6175), /* Channel 45 */
399 CHAN6G(6195), /* Channel 49 */
400 CHAN6G(6215), /* Channel 53 */
401 CHAN6G(6235), /* Channel 57 */
402 CHAN6G(6255), /* Channel 61 */
403 CHAN6G(6275), /* Channel 65 */
404 CHAN6G(6295), /* Channel 69 */
405 CHAN6G(6315), /* Channel 73 */
406 CHAN6G(6335), /* Channel 77 */
407 CHAN6G(6355), /* Channel 81 */
408 CHAN6G(6375), /* Channel 85 */
409 CHAN6G(6395), /* Channel 89 */
410 CHAN6G(6415), /* Channel 93 */
411 CHAN6G(6435), /* Channel 97 */
412 CHAN6G(6455), /* Channel 181 */
413 CHAN6G(6475), /* Channel 105 */
414 CHAN6G(6495), /* Channel 109 */
415 CHAN6G(6515), /* Channel 113 */
416 CHAN6G(6535), /* Channel 117 */
417 CHAN6G(6555), /* Channel 121 */
418 CHAN6G(6575), /* Channel 125 */
419 CHAN6G(6595), /* Channel 129 */
420 CHAN6G(6615), /* Channel 133 */
421 CHAN6G(6635), /* Channel 137 */
422 CHAN6G(6655), /* Channel 141 */
423 CHAN6G(6675), /* Channel 145 */
424 CHAN6G(6695), /* Channel 149 */
425 CHAN6G(6715), /* Channel 153 */
426 CHAN6G(6735), /* Channel 157 */
427 CHAN6G(6755), /* Channel 161 */
428 CHAN6G(6775), /* Channel 165 */
429 CHAN6G(6795), /* Channel 169 */
430 CHAN6G(6815), /* Channel 173 */
431 CHAN6G(6835), /* Channel 177 */
432 CHAN6G(6855), /* Channel 181 */
433 CHAN6G(6875), /* Channel 185 */
434 CHAN6G(6895), /* Channel 189 */
435 CHAN6G(6915), /* Channel 193 */
436 CHAN6G(6935), /* Channel 197 */
437 CHAN6G(6955), /* Channel 201 */
438 CHAN6G(6975), /* Channel 205 */
439 CHAN6G(6995), /* Channel 209 */
440 CHAN6G(7015), /* Channel 213 */
441 CHAN6G(7035), /* Channel 217 */
442 CHAN6G(7055), /* Channel 221 */
443 CHAN6G(7075), /* Channel 225 */
444 CHAN6G(7095), /* Channel 229 */
445 CHAN6G(7115), /* Channel 233 */
448 #define NUM_S1G_CHANS_US 51
449 static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
451 static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
453 .cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
456 S1G_CAP3_MAX_MPDU_LEN,
461 S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
463 .nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
464 /* RX Highest Supported Long GI Data Rate 0:7 */
466 /* RX Highest Supported Long GI Data Rate 0:7 */
467 /* TX S1G MCS Map 0:6 */
469 /* TX S1G MCS Map :7 */
470 /* TX Highest Supported Long GI Data Rate 0:6 */
472 /* TX Highest Supported Long GI Data Rate 7:8 */
473 /* Rx Single spatial stream and S1G-MCS Map for 1MHz */
474 /* Tx Single spatial stream and S1G-MCS Map for 1MHz */
478 static void hwsim_init_s1g_channels(struct ieee80211_channel *channels)
482 for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
483 freq = 902000 + (ch + 1) * 500;
484 channels[ch].band = NL80211_BAND_S1GHZ;
485 channels[ch].center_freq = KHZ_TO_MHZ(freq);
486 channels[ch].freq_offset = freq % 1000;
487 channels[ch].hw_value = ch + 1;
491 static const struct ieee80211_rate hwsim_rates[] = {
493 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
494 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
495 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
506 static const u32 hwsim_ciphers[] = {
507 WLAN_CIPHER_SUITE_WEP40,
508 WLAN_CIPHER_SUITE_WEP104,
509 WLAN_CIPHER_SUITE_TKIP,
510 WLAN_CIPHER_SUITE_CCMP,
511 WLAN_CIPHER_SUITE_CCMP_256,
512 WLAN_CIPHER_SUITE_GCMP,
513 WLAN_CIPHER_SUITE_GCMP_256,
514 WLAN_CIPHER_SUITE_AES_CMAC,
515 WLAN_CIPHER_SUITE_BIP_CMAC_256,
516 WLAN_CIPHER_SUITE_BIP_GMAC_128,
517 WLAN_CIPHER_SUITE_BIP_GMAC_256,
520 #define OUI_QCA 0x001374
521 #define QCA_NL80211_SUBCMD_TEST 1
522 enum qca_nl80211_vendor_subcmds {
523 QCA_WLAN_VENDOR_ATTR_TEST = 8,
524 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
527 static const struct nla_policy
528 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
529 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
532 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
533 struct wireless_dev *wdev,
534 const void *data, int data_len)
537 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
541 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
542 data_len, hwsim_vendor_test_policy, NULL);
545 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
547 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
548 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
550 /* Send a vendor event as a test. Note that this would not normally be
551 * done within a command handler, but rather, based on some other
552 * trigger. For simplicity, this command is used to trigger the event
555 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
557 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
559 /* skb_put() or nla_put() will fill up data within
560 * NL80211_ATTR_VENDOR_DATA.
563 /* Add vendor data */
564 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
566 /* Send the event - this will call nla_nest_end() */
567 cfg80211_vendor_event(skb, GFP_KERNEL);
570 /* Send a response to the command */
571 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
575 /* skb_put() or nla_put() will fill up data within
576 * NL80211_ATTR_VENDOR_DATA
578 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
580 return cfg80211_vendor_cmd_reply(skb);
583 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
585 .info = { .vendor_id = OUI_QCA,
586 .subcmd = QCA_NL80211_SUBCMD_TEST },
587 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
588 .doit = mac80211_hwsim_vendor_cmd_test,
589 .policy = hwsim_vendor_test_policy,
590 .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
594 /* Advertise support vendor specific events */
595 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
596 { .vendor_id = OUI_QCA, .subcmd = 1 },
599 static DEFINE_SPINLOCK(hwsim_radio_lock);
600 static LIST_HEAD(hwsim_radios);
601 static struct rhashtable hwsim_radios_rht;
602 static int hwsim_radio_idx;
603 static int hwsim_radios_generation = 1;
605 static struct platform_driver mac80211_hwsim_driver = {
607 .name = "mac80211_hwsim",
611 struct mac80211_hwsim_data {
612 struct list_head list;
613 struct rhash_head rht;
614 struct ieee80211_hw *hw;
616 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
617 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
618 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
619 struct ieee80211_channel channels_6ghz[ARRAY_SIZE(hwsim_channels_6ghz)];
620 struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
621 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
622 struct ieee80211_iface_combination if_combination;
623 struct ieee80211_iface_limit if_limits[3];
626 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
628 struct mac_address addresses[2];
631 bool destroy_on_close;
634 const struct ieee80211_regdomain *regd;
636 struct ieee80211_channel *tmp_chan;
637 struct ieee80211_channel *roc_chan;
639 struct delayed_work roc_start;
640 struct delayed_work roc_done;
641 struct delayed_work hw_scan;
642 struct cfg80211_scan_request *hw_scan_request;
643 struct ieee80211_vif *hw_scan_vif;
645 u8 scan_addr[ETH_ALEN];
647 struct ieee80211_channel *channel;
648 unsigned long next_start, start, end;
649 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
650 ARRAY_SIZE(hwsim_channels_5ghz) +
651 ARRAY_SIZE(hwsim_channels_6ghz)];
653 struct ieee80211_channel *channel;
654 u64 beacon_int /* beacon interval in us */;
655 unsigned int rx_filter;
656 bool started, idle, scanning;
658 struct hrtimer beacon_timer;
660 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
662 bool ps_poll_pending;
663 struct dentry *debugfs;
665 uintptr_t pending_cookie;
666 struct sk_buff_head pending; /* packets pending */
668 * Only radios in the same group can communicate together (the
669 * channel has to match too). Each bit represents a group. A
670 * radio can be in more than one group.
674 /* group shared by radios created in the same netns */
676 /* wmediumd portid responsible for netgroup of this radio */
679 /* difference between this hw's clock and the real clock, in usecs */
682 /* absolute beacon transmission time. Used to cover up "tx" delay. */
694 static const struct rhashtable_params hwsim_rht_params = {
696 .automatic_shrinking = true,
698 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
699 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
702 struct hwsim_radiotap_hdr {
703 struct ieee80211_radiotap_header hdr;
711 struct hwsim_radiotap_ack_hdr {
712 struct ieee80211_radiotap_header hdr;
719 /* MAC80211_HWSIM netlink family */
720 static struct genl_family hwsim_genl_family;
722 enum hwsim_multicast_groups {
726 static const struct genl_multicast_group hwsim_mcgrps[] = {
727 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
730 /* MAC80211_HWSIM netlink policy */
732 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
733 [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
734 [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
735 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
736 .len = IEEE80211_MAX_DATA_LEN },
737 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
738 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
739 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
740 [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
741 .len = IEEE80211_TX_MAX_RATES *
742 sizeof(struct hwsim_tx_rate)},
743 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
744 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
745 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
746 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
747 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
748 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
749 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
750 [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
751 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
752 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
753 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
754 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
755 [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
756 [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
757 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
758 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
761 #if IS_REACHABLE(CONFIG_VIRTIO)
763 /* MAC80211_HWSIM virtio queues */
764 static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
765 static bool hwsim_virtio_enabled;
766 static DEFINE_SPINLOCK(hwsim_virtio_lock);
768 static void hwsim_virtio_rx_work(struct work_struct *work);
769 static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
771 static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
774 struct scatterlist sg[1];
778 spin_lock_irqsave(&hwsim_virtio_lock, flags);
779 if (!hwsim_virtio_enabled) {
784 sg_init_one(sg, skb->head, skb_end_offset(skb));
785 err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
789 virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
790 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
794 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
799 /* cause a linker error if this ends up being needed */
800 extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
801 struct sk_buff *skb);
802 #define hwsim_virtio_enabled false
805 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
807 struct ieee80211_channel *chan);
809 /* sysfs attributes */
810 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
812 struct mac80211_hwsim_data *data = dat;
813 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
815 struct ieee80211_pspoll *pspoll;
820 wiphy_dbg(data->hw->wiphy,
821 "%s: send PS-Poll to %pM for aid %d\n",
822 __func__, vp->bssid, vp->aid);
824 skb = dev_alloc_skb(sizeof(*pspoll));
827 pspoll = skb_put(skb, sizeof(*pspoll));
828 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
829 IEEE80211_STYPE_PSPOLL |
831 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
832 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
833 memcpy(pspoll->ta, mac, ETH_ALEN);
836 mac80211_hwsim_tx_frame(data->hw, skb,
837 rcu_dereference(vif->chanctx_conf)->def.chan);
841 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
842 struct ieee80211_vif *vif, int ps)
844 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
846 struct ieee80211_hdr *hdr;
851 wiphy_dbg(data->hw->wiphy,
852 "%s: send data::nullfunc to %pM ps=%d\n",
853 __func__, vp->bssid, ps);
855 skb = dev_alloc_skb(sizeof(*hdr));
858 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
859 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
860 IEEE80211_STYPE_NULLFUNC |
861 IEEE80211_FCTL_TODS |
862 (ps ? IEEE80211_FCTL_PM : 0));
863 hdr->duration_id = cpu_to_le16(0);
864 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
865 memcpy(hdr->addr2, mac, ETH_ALEN);
866 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
869 mac80211_hwsim_tx_frame(data->hw, skb,
870 rcu_dereference(vif->chanctx_conf)->def.chan);
875 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
876 struct ieee80211_vif *vif)
878 struct mac80211_hwsim_data *data = dat;
879 hwsim_send_nullfunc(data, mac, vif, 1);
882 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
883 struct ieee80211_vif *vif)
885 struct mac80211_hwsim_data *data = dat;
886 hwsim_send_nullfunc(data, mac, vif, 0);
889 static int hwsim_fops_ps_read(void *dat, u64 *val)
891 struct mac80211_hwsim_data *data = dat;
896 static int hwsim_fops_ps_write(void *dat, u64 val)
898 struct mac80211_hwsim_data *data = dat;
901 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
902 val != PS_MANUAL_POLL)
905 if (val == PS_MANUAL_POLL) {
906 if (data->ps != PS_ENABLED)
909 ieee80211_iterate_active_interfaces_atomic(
910 data->hw, IEEE80211_IFACE_ITER_NORMAL,
911 hwsim_send_ps_poll, data);
919 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
920 ieee80211_iterate_active_interfaces_atomic(
921 data->hw, IEEE80211_IFACE_ITER_NORMAL,
922 hwsim_send_nullfunc_ps, data);
923 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
924 ieee80211_iterate_active_interfaces_atomic(
925 data->hw, IEEE80211_IFACE_ITER_NORMAL,
926 hwsim_send_nullfunc_no_ps, data);
933 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
936 static int hwsim_write_simulate_radar(void *dat, u64 val)
938 struct mac80211_hwsim_data *data = dat;
940 ieee80211_radar_detected(data->hw);
945 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
946 hwsim_write_simulate_radar, "%llu\n");
948 static int hwsim_fops_group_read(void *dat, u64 *val)
950 struct mac80211_hwsim_data *data = dat;
955 static int hwsim_fops_group_write(void *dat, u64 val)
957 struct mac80211_hwsim_data *data = dat;
962 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
963 hwsim_fops_group_read, hwsim_fops_group_write,
966 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
967 struct net_device *dev)
969 /* TODO: allow packet injection */
974 static inline u64 mac80211_hwsim_get_tsf_raw(void)
976 return ktime_to_us(ktime_get_real());
979 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
981 u64 now = mac80211_hwsim_get_tsf_raw();
982 return cpu_to_le64(now + data->tsf_offset);
985 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
986 struct ieee80211_vif *vif)
988 struct mac80211_hwsim_data *data = hw->priv;
989 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
992 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
993 struct ieee80211_vif *vif, u64 tsf)
995 struct mac80211_hwsim_data *data = hw->priv;
996 u64 now = mac80211_hwsim_get_tsf(hw, vif);
997 u32 bcn_int = data->beacon_int;
998 u64 delta = abs(tsf - now);
1000 /* adjust after beaconing with new timestamp at old TBTT */
1002 data->tsf_offset += delta;
1003 data->bcn_delta = do_div(delta, bcn_int);
1005 data->tsf_offset -= delta;
1006 data->bcn_delta = -(s64)do_div(delta, bcn_int);
1010 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
1011 struct sk_buff *tx_skb,
1012 struct ieee80211_channel *chan)
1014 struct mac80211_hwsim_data *data = hw->priv;
1015 struct sk_buff *skb;
1016 struct hwsim_radiotap_hdr *hdr;
1018 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
1019 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
1024 bitrate = txrate->bitrate;
1026 if (!netif_running(hwsim_mon))
1029 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
1033 hdr = skb_push(skb, sizeof(*hdr));
1034 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1035 hdr->hdr.it_pad = 0;
1036 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1037 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1038 (1 << IEEE80211_RADIOTAP_RATE) |
1039 (1 << IEEE80211_RADIOTAP_TSFT) |
1040 (1 << IEEE80211_RADIOTAP_CHANNEL));
1041 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
1043 hdr->rt_rate = bitrate / 5;
1044 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1045 flags = IEEE80211_CHAN_2GHZ;
1046 if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
1047 flags |= IEEE80211_CHAN_OFDM;
1049 flags |= IEEE80211_CHAN_CCK;
1050 hdr->rt_chbitmask = cpu_to_le16(flags);
1052 skb->dev = hwsim_mon;
1053 skb_reset_mac_header(skb);
1054 skb->ip_summed = CHECKSUM_UNNECESSARY;
1055 skb->pkt_type = PACKET_OTHERHOST;
1056 skb->protocol = htons(ETH_P_802_2);
1057 memset(skb->cb, 0, sizeof(skb->cb));
1062 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
1065 struct sk_buff *skb;
1066 struct hwsim_radiotap_ack_hdr *hdr;
1068 struct ieee80211_hdr *hdr11;
1070 if (!netif_running(hwsim_mon))
1073 skb = dev_alloc_skb(100);
1077 hdr = skb_put(skb, sizeof(*hdr));
1078 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1079 hdr->hdr.it_pad = 0;
1080 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1081 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1082 (1 << IEEE80211_RADIOTAP_CHANNEL));
1085 hdr->rt_channel = cpu_to_le16(chan->center_freq);
1086 flags = IEEE80211_CHAN_2GHZ;
1087 hdr->rt_chbitmask = cpu_to_le16(flags);
1089 hdr11 = skb_put(skb, 10);
1090 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1091 IEEE80211_STYPE_ACK);
1092 hdr11->duration_id = cpu_to_le16(0);
1093 memcpy(hdr11->addr1, addr, ETH_ALEN);
1095 skb->dev = hwsim_mon;
1096 skb_reset_mac_header(skb);
1097 skb->ip_summed = CHECKSUM_UNNECESSARY;
1098 skb->pkt_type = PACKET_OTHERHOST;
1099 skb->protocol = htons(ETH_P_802_2);
1100 memset(skb->cb, 0, sizeof(skb->cb));
1104 struct mac80211_hwsim_addr_match_data {
1109 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1110 struct ieee80211_vif *vif)
1112 struct mac80211_hwsim_addr_match_data *md = data;
1114 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
1118 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1121 struct mac80211_hwsim_addr_match_data md = {
1125 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1128 memcpy(md.addr, addr, ETH_ALEN);
1130 ieee80211_iterate_active_interfaces_atomic(data->hw,
1131 IEEE80211_IFACE_ITER_NORMAL,
1132 mac80211_hwsim_addr_iter,
1138 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1139 struct sk_buff *skb)
1147 /* TODO: accept (some) Beacons by default and other frames only
1148 * if pending PS-Poll has been sent */
1150 case PS_MANUAL_POLL:
1151 /* Allow unicast frames to own address if there is a pending
1153 if (data->ps_poll_pending &&
1154 mac80211_hwsim_addr_match(data, skb->data + 4)) {
1155 data->ps_poll_pending = false;
1164 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1165 struct sk_buff *skb, int portid)
1172 for_each_net_rcu(net) {
1173 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1174 res = genlmsg_unicast(net, skb, portid);
1187 static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1188 const u8 *addr, bool add)
1190 struct mac80211_hwsim_data *data = hw->priv;
1191 u32 _portid = READ_ONCE(data->wmediumd);
1192 struct sk_buff *skb;
1195 if (!_portid && !hwsim_virtio_enabled)
1198 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1202 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1203 add ? HWSIM_CMD_ADD_MAC_ADDR :
1204 HWSIM_CMD_DEL_MAC_ADDR);
1206 pr_debug("mac80211_hwsim: problem with msg_head\n");
1207 goto nla_put_failure;
1210 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1211 ETH_ALEN, data->addresses[1].addr))
1212 goto nla_put_failure;
1214 if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1215 goto nla_put_failure;
1217 genlmsg_end(skb, msg_head);
1219 if (hwsim_virtio_enabled)
1220 hwsim_tx_virtio(data, skb);
1222 hwsim_unicast_netgroup(data, skb, _portid);
1228 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1232 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1233 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1234 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1235 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1236 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1237 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1238 if (rate->flags & IEEE80211_TX_RC_MCS)
1239 result |= MAC80211_HWSIM_TX_RC_MCS;
1240 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1241 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1242 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1243 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1244 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1245 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1246 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1247 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1248 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1249 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1250 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1251 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1252 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1253 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1258 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1259 struct sk_buff *my_skb,
1262 struct sk_buff *skb;
1263 struct mac80211_hwsim_data *data = hw->priv;
1264 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1265 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1267 unsigned int hwsim_flags = 0;
1269 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1270 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1273 if (data->ps != PS_DISABLED)
1274 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1275 /* If the queue contains MAX_QUEUE skb's drop some */
1276 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1277 /* Droping until WARN_QUEUE level */
1278 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1279 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1284 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1286 goto nla_put_failure;
1288 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1290 if (msg_head == NULL) {
1291 pr_debug("mac80211_hwsim: problem with msg_head\n");
1292 goto nla_put_failure;
1295 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1296 ETH_ALEN, data->addresses[1].addr))
1297 goto nla_put_failure;
1299 /* We get the skb->data */
1300 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1301 goto nla_put_failure;
1303 /* We get the flags for this transmission, and we translate them to
1306 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1307 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1309 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1310 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1312 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1313 goto nla_put_failure;
1315 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1316 goto nla_put_failure;
1318 /* We get the tx control (rate and retries) info*/
1320 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1321 tx_attempts[i].idx = info->status.rates[i].idx;
1322 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1323 tx_attempts[i].count = info->status.rates[i].count;
1324 tx_attempts_flags[i].flags =
1325 trans_tx_rate_flags_ieee2hwsim(
1326 &info->status.rates[i]);
1329 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1330 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1332 goto nla_put_failure;
1334 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1335 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1337 goto nla_put_failure;
1339 /* We create a cookie to identify this skb */
1340 data->pending_cookie++;
1341 cookie = data->pending_cookie;
1342 info->rate_driver_data[0] = (void *)cookie;
1343 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1344 goto nla_put_failure;
1346 genlmsg_end(skb, msg_head);
1348 if (hwsim_virtio_enabled) {
1349 if (hwsim_tx_virtio(data, skb))
1350 goto err_free_txskb;
1352 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1353 goto err_free_txskb;
1356 /* Enqueue the packet */
1357 skb_queue_tail(&data->pending, my_skb);
1359 data->tx_bytes += my_skb->len;
1365 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1366 ieee80211_free_txskb(hw, my_skb);
1370 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1371 struct ieee80211_channel *c2)
1376 return c1->center_freq == c2->center_freq;
1379 struct tx_iter_data {
1380 struct ieee80211_channel *channel;
1384 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1385 struct ieee80211_vif *vif)
1387 struct tx_iter_data *data = _data;
1389 if (!vif->chanctx_conf)
1392 if (!hwsim_chans_compat(data->channel,
1393 rcu_dereference(vif->chanctx_conf)->def.chan))
1396 data->receive = true;
1399 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1402 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1404 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1405 * (but you should use a valid OUI, not that)
1407 * If anyone wants to 'donate' a radiotap OUI/subns code
1408 * please send a patch removing this #ifdef and changing
1409 * the values accordingly.
1411 #ifdef HWSIM_RADIOTAP_OUI
1412 struct ieee80211_vendor_radiotap *rtap;
1415 * Note that this code requires the headroom in the SKB
1416 * that was allocated earlier.
1418 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1419 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1420 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1421 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1425 * Radiotap vendor namespaces can (and should) also be
1426 * split into fields by using the standard radiotap
1427 * presence bitmap mechanism. Use just BIT(0) here for
1428 * the presence bitmap.
1430 rtap->present = BIT(0);
1431 /* We have 8 bytes of (dummy) data */
1433 /* For testing, also require it to be aligned */
1435 /* And also test that padding works, 4 bytes */
1438 memcpy(rtap->data, "ABCDEFGH", 8);
1439 /* make sure to clear padding, mac80211 doesn't */
1440 memset(rtap->data + 8, 0, 4);
1442 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1446 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1447 struct sk_buff *skb,
1448 struct ieee80211_channel *chan)
1450 struct mac80211_hwsim_data *data = hw->priv, *data2;
1452 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1454 struct ieee80211_rx_status rx_status;
1457 memset(&rx_status, 0, sizeof(rx_status));
1458 rx_status.flag |= RX_FLAG_MACTIME_START;
1459 rx_status.freq = chan->center_freq;
1460 rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1461 rx_status.band = chan->band;
1462 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1463 rx_status.rate_idx =
1464 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1466 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1467 rx_status.encoding = RX_ENC_VHT;
1469 rx_status.rate_idx = info->control.rates[0].idx;
1470 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1471 rx_status.encoding = RX_ENC_HT;
1473 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1474 rx_status.bw = RATE_INFO_BW_40;
1475 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1476 rx_status.bw = RATE_INFO_BW_80;
1477 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1478 rx_status.bw = RATE_INFO_BW_160;
1480 rx_status.bw = RATE_INFO_BW_20;
1481 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1482 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1483 /* TODO: simulate real signal strength (and optional packet loss) */
1484 rx_status.signal = -50;
1485 if (info->control.vif)
1486 rx_status.signal += info->control.vif->bss_conf.txpower;
1488 if (data->ps != PS_DISABLED)
1489 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1491 /* release the skb's source info */
1499 * Get absolute mactime here so all HWs RX at the "same time", and
1500 * absolute TX time for beacon mactime so the timestamp matches.
1501 * Giving beacons a different mactime than non-beacons looks messy, but
1502 * it helps the Toffset be exact and a ~10us mactime discrepancy
1503 * probably doesn't really matter.
1505 if (ieee80211_is_beacon(hdr->frame_control) ||
1506 ieee80211_is_probe_resp(hdr->frame_control)) {
1507 rx_status.boottime_ns = ktime_get_boottime_ns();
1508 now = data->abs_bcn_ts;
1510 now = mac80211_hwsim_get_tsf_raw();
1513 /* Copy skb to all enabled radios that are on the current frequency */
1514 spin_lock(&hwsim_radio_lock);
1515 list_for_each_entry(data2, &hwsim_radios, list) {
1516 struct sk_buff *nskb;
1517 struct tx_iter_data tx_iter_data = {
1525 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1526 !hwsim_ps_rx_ok(data2, skb))
1529 if (!(data->group & data2->group))
1532 if (data->netgroup != data2->netgroup)
1535 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1536 !hwsim_chans_compat(chan, data2->channel)) {
1537 ieee80211_iterate_active_interfaces_atomic(
1538 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1539 mac80211_hwsim_tx_iter, &tx_iter_data);
1540 if (!tx_iter_data.receive)
1545 * reserve some space for our vendor and the normal
1546 * radiotap header, since we're copying anyway
1548 if (skb->len < PAGE_SIZE && paged_rx) {
1549 struct page *page = alloc_page(GFP_ATOMIC);
1554 nskb = dev_alloc_skb(128);
1560 memcpy(page_address(page), skb->data, skb->len);
1561 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1563 nskb = skb_copy(skb, GFP_ATOMIC);
1568 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1571 rx_status.mactime = now + data2->tsf_offset;
1573 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1575 mac80211_hwsim_add_vendor_rtap(nskb);
1578 data2->rx_bytes += nskb->len;
1579 ieee80211_rx_irqsafe(data2->hw, nskb);
1581 spin_unlock(&hwsim_radio_lock);
1586 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1587 struct ieee80211_tx_control *control,
1588 struct sk_buff *skb)
1590 struct mac80211_hwsim_data *data = hw->priv;
1591 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1592 struct ieee80211_hdr *hdr = (void *)skb->data;
1593 struct ieee80211_chanctx_conf *chanctx_conf;
1594 struct ieee80211_channel *channel;
1598 if (WARN_ON(skb->len < 10)) {
1599 /* Should not happen; just a sanity check for addr1 use */
1600 ieee80211_free_txskb(hw, skb);
1604 if (!data->use_chanctx) {
1605 channel = data->channel;
1606 } else if (txi->hw_queue == 4) {
1607 channel = data->tmp_chan;
1609 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1611 channel = chanctx_conf->def.chan;
1616 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1617 ieee80211_free_txskb(hw, skb);
1621 if (data->idle && !data->tmp_chan) {
1622 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1623 ieee80211_free_txskb(hw, skb);
1627 if (txi->control.vif)
1628 hwsim_check_magic(txi->control.vif);
1630 hwsim_check_sta_magic(control->sta);
1632 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1633 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1635 ARRAY_SIZE(txi->control.rates));
1637 if (skb->len >= 24 + 8 &&
1638 ieee80211_is_probe_resp(hdr->frame_control)) {
1639 /* fake header transmission time */
1640 struct ieee80211_mgmt *mgmt;
1641 struct ieee80211_rate *txrate;
1646 mgmt = (struct ieee80211_mgmt *)skb->data;
1647 txrate = ieee80211_get_tx_rate(hw, txi);
1649 bitrate = txrate->bitrate;
1650 ts = mac80211_hwsim_get_tsf_raw();
1651 mgmt->u.probe_resp.timestamp =
1652 cpu_to_le64(ts + data->tsf_offset +
1653 24 * 8 * 10 / bitrate);
1656 mac80211_hwsim_monitor_rx(hw, skb, channel);
1658 /* wmediumd mode check */
1659 _portid = READ_ONCE(data->wmediumd);
1661 if (_portid || hwsim_virtio_enabled)
1662 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1664 /* NO wmediumd detected, perfect medium simulation */
1666 data->tx_bytes += skb->len;
1667 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1669 if (ack && skb->len >= 16)
1670 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1672 ieee80211_tx_info_clear_status(txi);
1674 /* frame was transmitted at most favorable rate at first attempt */
1675 txi->control.rates[0].count = 1;
1676 txi->control.rates[1].idx = -1;
1678 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1679 txi->flags |= IEEE80211_TX_STAT_ACK;
1680 ieee80211_tx_status_irqsafe(hw, skb);
1684 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1686 struct mac80211_hwsim_data *data = hw->priv;
1687 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1688 data->started = true;
1693 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1695 struct mac80211_hwsim_data *data = hw->priv;
1697 data->started = false;
1698 hrtimer_cancel(&data->beacon_timer);
1700 while (!skb_queue_empty(&data->pending))
1701 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1703 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1707 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1708 struct ieee80211_vif *vif)
1710 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1711 __func__, ieee80211_vif_type_p2p(vif),
1713 hwsim_set_magic(vif);
1715 if (vif->type != NL80211_IFTYPE_MONITOR)
1716 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1719 vif->hw_queue[IEEE80211_AC_VO] = 0;
1720 vif->hw_queue[IEEE80211_AC_VI] = 1;
1721 vif->hw_queue[IEEE80211_AC_BE] = 2;
1722 vif->hw_queue[IEEE80211_AC_BK] = 3;
1728 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1729 struct ieee80211_vif *vif,
1730 enum nl80211_iftype newtype,
1733 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1734 wiphy_dbg(hw->wiphy,
1735 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1736 __func__, ieee80211_vif_type_p2p(vif),
1737 newtype, vif->addr);
1738 hwsim_check_magic(vif);
1741 * interface may change from non-AP to AP in
1742 * which case this needs to be set up again
1749 static void mac80211_hwsim_remove_interface(
1750 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1752 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1753 __func__, ieee80211_vif_type_p2p(vif),
1755 hwsim_check_magic(vif);
1756 hwsim_clear_magic(vif);
1757 if (vif->type != NL80211_IFTYPE_MONITOR)
1758 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1761 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1762 struct sk_buff *skb,
1763 struct ieee80211_channel *chan)
1765 struct mac80211_hwsim_data *data = hw->priv;
1766 u32 _pid = READ_ONCE(data->wmediumd);
1768 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1769 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1770 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1772 ARRAY_SIZE(txi->control.rates));
1775 mac80211_hwsim_monitor_rx(hw, skb, chan);
1777 if (_pid || hwsim_virtio_enabled)
1778 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1780 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1784 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1785 struct ieee80211_vif *vif)
1787 struct mac80211_hwsim_data *data = arg;
1788 struct ieee80211_hw *hw = data->hw;
1789 struct ieee80211_tx_info *info;
1790 struct ieee80211_rate *txrate;
1791 struct ieee80211_mgmt *mgmt;
1792 struct sk_buff *skb;
1796 hwsim_check_magic(vif);
1798 if (vif->type != NL80211_IFTYPE_AP &&
1799 vif->type != NL80211_IFTYPE_MESH_POINT &&
1800 vif->type != NL80211_IFTYPE_ADHOC &&
1801 vif->type != NL80211_IFTYPE_OCB)
1804 skb = ieee80211_beacon_get(hw, vif);
1807 info = IEEE80211_SKB_CB(skb);
1808 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1809 ieee80211_get_tx_rates(vif, NULL, skb,
1810 info->control.rates,
1811 ARRAY_SIZE(info->control.rates));
1813 txrate = ieee80211_get_tx_rate(hw, info);
1815 bitrate = txrate->bitrate;
1817 mgmt = (struct ieee80211_mgmt *) skb->data;
1818 /* fake header transmission time */
1819 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1820 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1821 struct ieee80211_ext *ext = (void *) mgmt;
1823 ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1828 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1834 mac80211_hwsim_tx_frame(hw, skb,
1835 rcu_dereference(vif->chanctx_conf)->def.chan);
1837 while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1838 mac80211_hwsim_tx_frame(hw, skb,
1839 rcu_dereference(vif->chanctx_conf)->def.chan);
1842 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1843 ieee80211_csa_finish(vif);
1846 static enum hrtimer_restart
1847 mac80211_hwsim_beacon(struct hrtimer *timer)
1849 struct mac80211_hwsim_data *data =
1850 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1851 struct ieee80211_hw *hw = data->hw;
1852 u64 bcn_int = data->beacon_int;
1855 return HRTIMER_NORESTART;
1857 ieee80211_iterate_active_interfaces_atomic(
1858 hw, IEEE80211_IFACE_ITER_NORMAL,
1859 mac80211_hwsim_beacon_tx, data);
1861 /* beacon at new TBTT + beacon interval */
1862 if (data->bcn_delta) {
1863 bcn_int -= data->bcn_delta;
1864 data->bcn_delta = 0;
1866 hrtimer_forward(&data->beacon_timer, hrtimer_get_expires(timer),
1867 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1868 return HRTIMER_RESTART;
1871 static const char * const hwsim_chanwidths[] = {
1872 [NL80211_CHAN_WIDTH_5] = "ht5",
1873 [NL80211_CHAN_WIDTH_10] = "ht10",
1874 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1875 [NL80211_CHAN_WIDTH_20] = "ht20",
1876 [NL80211_CHAN_WIDTH_40] = "ht40",
1877 [NL80211_CHAN_WIDTH_80] = "vht80",
1878 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1879 [NL80211_CHAN_WIDTH_160] = "vht160",
1880 [NL80211_CHAN_WIDTH_1] = "1MHz",
1881 [NL80211_CHAN_WIDTH_2] = "2MHz",
1882 [NL80211_CHAN_WIDTH_4] = "4MHz",
1883 [NL80211_CHAN_WIDTH_8] = "8MHz",
1884 [NL80211_CHAN_WIDTH_16] = "16MHz",
1887 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1889 struct mac80211_hwsim_data *data = hw->priv;
1890 struct ieee80211_conf *conf = &hw->conf;
1891 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1892 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1893 [IEEE80211_SMPS_OFF] = "off",
1894 [IEEE80211_SMPS_STATIC] = "static",
1895 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1899 if (conf->chandef.chan)
1900 wiphy_dbg(hw->wiphy,
1901 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1903 conf->chandef.chan->center_freq,
1904 conf->chandef.center_freq1,
1905 conf->chandef.center_freq2,
1906 hwsim_chanwidths[conf->chandef.width],
1907 !!(conf->flags & IEEE80211_CONF_IDLE),
1908 !!(conf->flags & IEEE80211_CONF_PS),
1909 smps_modes[conf->smps_mode]);
1911 wiphy_dbg(hw->wiphy,
1912 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1914 !!(conf->flags & IEEE80211_CONF_IDLE),
1915 !!(conf->flags & IEEE80211_CONF_PS),
1916 smps_modes[conf->smps_mode]);
1918 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1920 WARN_ON(conf->chandef.chan && data->use_chanctx);
1922 mutex_lock(&data->mutex);
1923 if (data->scanning && conf->chandef.chan) {
1924 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1925 if (data->survey_data[idx].channel == data->channel) {
1926 data->survey_data[idx].start =
1927 data->survey_data[idx].next_start;
1928 data->survey_data[idx].end = jiffies;
1933 data->channel = conf->chandef.chan;
1935 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1936 if (data->survey_data[idx].channel &&
1937 data->survey_data[idx].channel != data->channel)
1939 data->survey_data[idx].channel = data->channel;
1940 data->survey_data[idx].next_start = jiffies;
1944 data->channel = conf->chandef.chan;
1946 mutex_unlock(&data->mutex);
1948 if (!data->started || !data->beacon_int)
1949 hrtimer_cancel(&data->beacon_timer);
1950 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1951 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1952 u32 bcn_int = data->beacon_int;
1953 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1955 hrtimer_start(&data->beacon_timer,
1956 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1957 HRTIMER_MODE_REL_SOFT);
1964 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1965 unsigned int changed_flags,
1966 unsigned int *total_flags,u64 multicast)
1968 struct mac80211_hwsim_data *data = hw->priv;
1970 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1972 data->rx_filter = 0;
1973 if (*total_flags & FIF_ALLMULTI)
1974 data->rx_filter |= FIF_ALLMULTI;
1975 if (*total_flags & FIF_MCAST_ACTION)
1976 data->rx_filter |= FIF_MCAST_ACTION;
1978 *total_flags = data->rx_filter;
1981 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1982 struct ieee80211_vif *vif)
1984 unsigned int *count = data;
1985 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1991 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1992 struct ieee80211_vif *vif,
1993 struct ieee80211_bss_conf *info,
1996 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1997 struct mac80211_hwsim_data *data = hw->priv;
1999 hwsim_check_magic(vif);
2001 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
2002 __func__, changed, vif->addr);
2004 if (changed & BSS_CHANGED_BSSID) {
2005 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
2006 __func__, info->bssid);
2007 memcpy(vp->bssid, info->bssid, ETH_ALEN);
2010 if (changed & BSS_CHANGED_ASSOC) {
2011 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
2012 info->assoc, info->aid);
2013 vp->assoc = info->assoc;
2014 vp->aid = info->aid;
2017 if (changed & BSS_CHANGED_BEACON_ENABLED) {
2018 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
2019 info->enable_beacon, info->beacon_int);
2020 vp->bcn_en = info->enable_beacon;
2021 if (data->started &&
2022 !hrtimer_is_queued(&data->beacon_timer) &&
2023 info->enable_beacon) {
2024 u64 tsf, until_tbtt;
2026 data->beacon_int = info->beacon_int * 1024;
2027 tsf = mac80211_hwsim_get_tsf(hw, vif);
2028 bcn_int = data->beacon_int;
2029 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2031 hrtimer_start(&data->beacon_timer,
2032 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2033 HRTIMER_MODE_REL_SOFT);
2034 } else if (!info->enable_beacon) {
2035 unsigned int count = 0;
2036 ieee80211_iterate_active_interfaces_atomic(
2037 data->hw, IEEE80211_IFACE_ITER_NORMAL,
2038 mac80211_hwsim_bcn_en_iter, &count);
2039 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
2042 hrtimer_cancel(&data->beacon_timer);
2043 data->beacon_int = 0;
2048 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2049 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
2050 info->use_cts_prot);
2053 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2054 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
2055 info->use_short_preamble);
2058 if (changed & BSS_CHANGED_ERP_SLOT) {
2059 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
2062 if (changed & BSS_CHANGED_HT) {
2063 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
2064 info->ht_operation_mode);
2067 if (changed & BSS_CHANGED_BASIC_RATES) {
2068 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
2069 (unsigned long long) info->basic_rates);
2072 if (changed & BSS_CHANGED_TXPOWER)
2073 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
2076 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2077 struct ieee80211_vif *vif,
2078 struct ieee80211_sta *sta)
2080 hwsim_check_magic(vif);
2081 hwsim_set_sta_magic(sta);
2086 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2087 struct ieee80211_vif *vif,
2088 struct ieee80211_sta *sta)
2090 hwsim_check_magic(vif);
2091 hwsim_clear_sta_magic(sta);
2096 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2097 struct ieee80211_vif *vif,
2098 enum sta_notify_cmd cmd,
2099 struct ieee80211_sta *sta)
2101 hwsim_check_magic(vif);
2104 case STA_NOTIFY_SLEEP:
2105 case STA_NOTIFY_AWAKE:
2106 /* TODO: make good use of these flags */
2109 WARN(1, "Invalid sta notify: %d\n", cmd);
2114 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2115 struct ieee80211_sta *sta,
2118 hwsim_check_sta_magic(sta);
2122 static int mac80211_hwsim_conf_tx(
2123 struct ieee80211_hw *hw,
2124 struct ieee80211_vif *vif, u16 queue,
2125 const struct ieee80211_tx_queue_params *params)
2127 wiphy_dbg(hw->wiphy,
2128 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2130 params->txop, params->cw_min,
2131 params->cw_max, params->aifs);
2135 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2136 struct survey_info *survey)
2138 struct mac80211_hwsim_data *hwsim = hw->priv;
2140 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2143 mutex_lock(&hwsim->mutex);
2144 survey->channel = hwsim->survey_data[idx].channel;
2145 if (!survey->channel) {
2146 mutex_unlock(&hwsim->mutex);
2151 * Magically conjured dummy values --- this is only ok for simulated hardware.
2153 * A real driver which cannot determine real values noise MUST NOT
2154 * report any, especially not a magically conjured ones :-)
2156 survey->filled = SURVEY_INFO_NOISE_DBM |
2158 SURVEY_INFO_TIME_BUSY;
2159 survey->noise = -92;
2161 jiffies_to_msecs(hwsim->survey_data[idx].end -
2162 hwsim->survey_data[idx].start);
2163 /* report 12.5% of channel time is used */
2164 survey->time_busy = survey->time/8;
2165 mutex_unlock(&hwsim->mutex);
2170 #ifdef CONFIG_NL80211_TESTMODE
2172 * This section contains example code for using netlink
2173 * attributes with the testmode command in nl80211.
2176 /* These enums need to be kept in sync with userspace */
2177 enum hwsim_testmode_attr {
2178 __HWSIM_TM_ATTR_INVALID = 0,
2179 HWSIM_TM_ATTR_CMD = 1,
2180 HWSIM_TM_ATTR_PS = 2,
2183 __HWSIM_TM_ATTR_AFTER_LAST,
2184 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
2187 enum hwsim_testmode_cmd {
2188 HWSIM_TM_CMD_SET_PS = 0,
2189 HWSIM_TM_CMD_GET_PS = 1,
2190 HWSIM_TM_CMD_STOP_QUEUES = 2,
2191 HWSIM_TM_CMD_WAKE_QUEUES = 3,
2194 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2195 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2196 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2199 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2200 struct ieee80211_vif *vif,
2201 void *data, int len)
2203 struct mac80211_hwsim_data *hwsim = hw->priv;
2204 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2205 struct sk_buff *skb;
2208 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2209 hwsim_testmode_policy, NULL);
2213 if (!tb[HWSIM_TM_ATTR_CMD])
2216 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2217 case HWSIM_TM_CMD_SET_PS:
2218 if (!tb[HWSIM_TM_ATTR_PS])
2220 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2221 return hwsim_fops_ps_write(hwsim, ps);
2222 case HWSIM_TM_CMD_GET_PS:
2223 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2224 nla_total_size(sizeof(u32)));
2227 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2228 goto nla_put_failure;
2229 return cfg80211_testmode_reply(skb);
2230 case HWSIM_TM_CMD_STOP_QUEUES:
2231 ieee80211_stop_queues(hw);
2233 case HWSIM_TM_CMD_WAKE_QUEUES:
2234 ieee80211_wake_queues(hw);
2246 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2247 struct ieee80211_vif *vif,
2248 struct ieee80211_ampdu_params *params)
2250 struct ieee80211_sta *sta = params->sta;
2251 enum ieee80211_ampdu_mlme_action action = params->action;
2252 u16 tid = params->tid;
2255 case IEEE80211_AMPDU_TX_START:
2256 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2257 case IEEE80211_AMPDU_TX_STOP_CONT:
2258 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2259 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2260 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2262 case IEEE80211_AMPDU_TX_OPERATIONAL:
2264 case IEEE80211_AMPDU_RX_START:
2265 case IEEE80211_AMPDU_RX_STOP:
2274 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2275 struct ieee80211_vif *vif,
2276 u32 queues, bool drop)
2278 /* Not implemented, queues only on kernel side */
2281 static void hw_scan_work(struct work_struct *work)
2283 struct mac80211_hwsim_data *hwsim =
2284 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2285 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2288 mutex_lock(&hwsim->mutex);
2289 if (hwsim->scan_chan_idx >= req->n_channels) {
2290 struct cfg80211_scan_info info = {
2294 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2295 ieee80211_scan_completed(hwsim->hw, &info);
2296 hwsim->hw_scan_request = NULL;
2297 hwsim->hw_scan_vif = NULL;
2298 hwsim->tmp_chan = NULL;
2299 mutex_unlock(&hwsim->mutex);
2300 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2305 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2306 req->channels[hwsim->scan_chan_idx]->center_freq);
2308 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2309 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2310 IEEE80211_CHAN_RADAR) ||
2316 for (i = 0; i < req->n_ssids; i++) {
2317 struct sk_buff *probe;
2318 struct ieee80211_mgmt *mgmt;
2320 probe = ieee80211_probereq_get(hwsim->hw,
2323 req->ssids[i].ssid_len,
2328 mgmt = (struct ieee80211_mgmt *) probe->data;
2329 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2330 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2333 skb_put_data(probe, req->ie, req->ie_len);
2336 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2341 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2342 msecs_to_jiffies(dwell));
2343 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2344 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2345 hwsim->survey_data[hwsim->scan_chan_idx].end =
2346 jiffies + msecs_to_jiffies(dwell);
2347 hwsim->scan_chan_idx++;
2348 mutex_unlock(&hwsim->mutex);
2351 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2352 struct ieee80211_vif *vif,
2353 struct ieee80211_scan_request *hw_req)
2355 struct mac80211_hwsim_data *hwsim = hw->priv;
2356 struct cfg80211_scan_request *req = &hw_req->req;
2358 mutex_lock(&hwsim->mutex);
2359 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2360 mutex_unlock(&hwsim->mutex);
2363 hwsim->hw_scan_request = req;
2364 hwsim->hw_scan_vif = vif;
2365 hwsim->scan_chan_idx = 0;
2366 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2367 get_random_mask_addr(hwsim->scan_addr,
2368 hw_req->req.mac_addr,
2369 hw_req->req.mac_addr_mask);
2371 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2372 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2373 mutex_unlock(&hwsim->mutex);
2375 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2376 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2378 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2383 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2384 struct ieee80211_vif *vif)
2386 struct mac80211_hwsim_data *hwsim = hw->priv;
2387 struct cfg80211_scan_info info = {
2391 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2393 cancel_delayed_work_sync(&hwsim->hw_scan);
2395 mutex_lock(&hwsim->mutex);
2396 ieee80211_scan_completed(hwsim->hw, &info);
2397 hwsim->tmp_chan = NULL;
2398 hwsim->hw_scan_request = NULL;
2399 hwsim->hw_scan_vif = NULL;
2400 mutex_unlock(&hwsim->mutex);
2403 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2404 struct ieee80211_vif *vif,
2407 struct mac80211_hwsim_data *hwsim = hw->priv;
2409 mutex_lock(&hwsim->mutex);
2411 if (hwsim->scanning) {
2412 pr_debug("two hwsim sw_scans detected!\n");
2416 pr_debug("hwsim sw_scan request, prepping stuff\n");
2418 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2419 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2420 hwsim->scanning = true;
2421 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2424 mutex_unlock(&hwsim->mutex);
2427 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2428 struct ieee80211_vif *vif)
2430 struct mac80211_hwsim_data *hwsim = hw->priv;
2432 mutex_lock(&hwsim->mutex);
2434 pr_debug("hwsim sw_scan_complete\n");
2435 hwsim->scanning = false;
2436 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2437 eth_zero_addr(hwsim->scan_addr);
2439 mutex_unlock(&hwsim->mutex);
2442 static void hw_roc_start(struct work_struct *work)
2444 struct mac80211_hwsim_data *hwsim =
2445 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2447 mutex_lock(&hwsim->mutex);
2449 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2450 hwsim->tmp_chan = hwsim->roc_chan;
2451 ieee80211_ready_on_channel(hwsim->hw);
2453 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2454 msecs_to_jiffies(hwsim->roc_duration));
2456 mutex_unlock(&hwsim->mutex);
2459 static void hw_roc_done(struct work_struct *work)
2461 struct mac80211_hwsim_data *hwsim =
2462 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2464 mutex_lock(&hwsim->mutex);
2465 ieee80211_remain_on_channel_expired(hwsim->hw);
2466 hwsim->tmp_chan = NULL;
2467 mutex_unlock(&hwsim->mutex);
2469 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2472 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2473 struct ieee80211_vif *vif,
2474 struct ieee80211_channel *chan,
2476 enum ieee80211_roc_type type)
2478 struct mac80211_hwsim_data *hwsim = hw->priv;
2480 mutex_lock(&hwsim->mutex);
2481 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2482 mutex_unlock(&hwsim->mutex);
2486 hwsim->roc_chan = chan;
2487 hwsim->roc_duration = duration;
2488 mutex_unlock(&hwsim->mutex);
2490 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2491 chan->center_freq, duration);
2492 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2497 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2498 struct ieee80211_vif *vif)
2500 struct mac80211_hwsim_data *hwsim = hw->priv;
2502 cancel_delayed_work_sync(&hwsim->roc_start);
2503 cancel_delayed_work_sync(&hwsim->roc_done);
2505 mutex_lock(&hwsim->mutex);
2506 hwsim->tmp_chan = NULL;
2507 mutex_unlock(&hwsim->mutex);
2509 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2514 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2515 struct ieee80211_chanctx_conf *ctx)
2517 hwsim_set_chanctx_magic(ctx);
2518 wiphy_dbg(hw->wiphy,
2519 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2520 ctx->def.chan->center_freq, ctx->def.width,
2521 ctx->def.center_freq1, ctx->def.center_freq2);
2525 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2526 struct ieee80211_chanctx_conf *ctx)
2528 wiphy_dbg(hw->wiphy,
2529 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2530 ctx->def.chan->center_freq, ctx->def.width,
2531 ctx->def.center_freq1, ctx->def.center_freq2);
2532 hwsim_check_chanctx_magic(ctx);
2533 hwsim_clear_chanctx_magic(ctx);
2536 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2537 struct ieee80211_chanctx_conf *ctx,
2540 hwsim_check_chanctx_magic(ctx);
2541 wiphy_dbg(hw->wiphy,
2542 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2543 ctx->def.chan->center_freq, ctx->def.width,
2544 ctx->def.center_freq1, ctx->def.center_freq2);
2547 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2548 struct ieee80211_vif *vif,
2549 struct ieee80211_chanctx_conf *ctx)
2551 hwsim_check_magic(vif);
2552 hwsim_check_chanctx_magic(ctx);
2557 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2558 struct ieee80211_vif *vif,
2559 struct ieee80211_chanctx_conf *ctx)
2561 hwsim_check_magic(vif);
2562 hwsim_check_chanctx_magic(ctx);
2565 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2576 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2578 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2579 struct ieee80211_vif *vif,
2582 if (sset == ETH_SS_STATS)
2583 memcpy(data, *mac80211_hwsim_gstrings_stats,
2584 sizeof(mac80211_hwsim_gstrings_stats));
2587 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2588 struct ieee80211_vif *vif, int sset)
2590 if (sset == ETH_SS_STATS)
2591 return MAC80211_HWSIM_SSTATS_LEN;
2595 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2596 struct ieee80211_vif *vif,
2597 struct ethtool_stats *stats, u64 *data)
2599 struct mac80211_hwsim_data *ar = hw->priv;
2602 data[i++] = ar->tx_pkts;
2603 data[i++] = ar->tx_bytes;
2604 data[i++] = ar->rx_pkts;
2605 data[i++] = ar->rx_bytes;
2606 data[i++] = ar->tx_dropped;
2607 data[i++] = ar->tx_failed;
2609 data[i++] = ar->group;
2611 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2614 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2619 #define HWSIM_COMMON_OPS \
2620 .tx = mac80211_hwsim_tx, \
2621 .start = mac80211_hwsim_start, \
2622 .stop = mac80211_hwsim_stop, \
2623 .add_interface = mac80211_hwsim_add_interface, \
2624 .change_interface = mac80211_hwsim_change_interface, \
2625 .remove_interface = mac80211_hwsim_remove_interface, \
2626 .config = mac80211_hwsim_config, \
2627 .configure_filter = mac80211_hwsim_configure_filter, \
2628 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2629 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2630 .sta_add = mac80211_hwsim_sta_add, \
2631 .sta_remove = mac80211_hwsim_sta_remove, \
2632 .sta_notify = mac80211_hwsim_sta_notify, \
2633 .set_tim = mac80211_hwsim_set_tim, \
2634 .conf_tx = mac80211_hwsim_conf_tx, \
2635 .get_survey = mac80211_hwsim_get_survey, \
2636 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2637 .ampdu_action = mac80211_hwsim_ampdu_action, \
2638 .flush = mac80211_hwsim_flush, \
2639 .get_tsf = mac80211_hwsim_get_tsf, \
2640 .set_tsf = mac80211_hwsim_set_tsf, \
2641 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2642 .get_et_stats = mac80211_hwsim_get_et_stats, \
2643 .get_et_strings = mac80211_hwsim_get_et_strings,
2645 static const struct ieee80211_ops mac80211_hwsim_ops = {
2647 .sw_scan_start = mac80211_hwsim_sw_scan,
2648 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2651 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2653 .hw_scan = mac80211_hwsim_hw_scan,
2654 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2655 .sw_scan_start = NULL,
2656 .sw_scan_complete = NULL,
2657 .remain_on_channel = mac80211_hwsim_roc,
2658 .cancel_remain_on_channel = mac80211_hwsim_croc,
2659 .add_chanctx = mac80211_hwsim_add_chanctx,
2660 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2661 .change_chanctx = mac80211_hwsim_change_chanctx,
2662 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2663 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2666 struct hwsim_new_radio_params {
2667 unsigned int channels;
2668 const char *reg_alpha2;
2669 const struct ieee80211_regdomain *regd;
2673 bool destroy_on_close;
2676 const u8 *perm_addr;
2682 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2683 struct genl_info *info)
2686 genl_notify(&hwsim_genl_family, mcast_skb, info,
2687 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2689 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2690 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2693 static int append_radio_msg(struct sk_buff *skb, int id,
2694 struct hwsim_new_radio_params *param)
2698 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2702 if (param->channels) {
2703 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2708 if (param->reg_alpha2) {
2709 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2718 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2719 if (hwsim_world_regdom_custom[i] != param->regd)
2722 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2729 if (param->reg_strict) {
2730 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2735 if (param->p2p_device) {
2736 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2741 if (param->use_chanctx) {
2742 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2747 if (param->hwname) {
2748 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2749 strlen(param->hwname), param->hwname);
2757 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2758 struct hwsim_new_radio_params *param)
2760 struct sk_buff *mcast_skb;
2763 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2767 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2768 HWSIM_CMD_NEW_RADIO);
2772 if (append_radio_msg(mcast_skb, id, param) < 0)
2775 genlmsg_end(mcast_skb, data);
2777 hwsim_mcast_config_msg(mcast_skb, info);
2781 nlmsg_free(mcast_skb);
2784 static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2786 /* TODO: should we support other types, e.g., P2P?*/
2787 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2788 BIT(NL80211_IFTYPE_AP),
2793 IEEE80211_HE_MAC_CAP0_HTC_HE,
2795 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2796 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2798 IEEE80211_HE_MAC_CAP2_BSR |
2799 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2800 IEEE80211_HE_MAC_CAP2_ACK_EN,
2802 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2803 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2804 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2806 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2807 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2808 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2809 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2811 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2812 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2813 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2814 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2815 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2817 /* Leave all the other PHY capability bytes
2818 * unset, as DCM, beam forming, RU and PPE
2819 * threshold information are not supported
2822 .he_mcs_nss_supp = {
2823 .rx_mcs_80 = cpu_to_le16(0xfffa),
2824 .tx_mcs_80 = cpu_to_le16(0xfffa),
2825 .rx_mcs_160 = cpu_to_le16(0xffff),
2826 .tx_mcs_160 = cpu_to_le16(0xffff),
2827 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2828 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2832 #ifdef CONFIG_MAC80211_MESH
2834 /* TODO: should we support other types, e.g., IBSS?*/
2835 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2840 IEEE80211_HE_MAC_CAP0_HTC_HE,
2842 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2844 IEEE80211_HE_MAC_CAP2_ACK_EN,
2846 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2847 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2848 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2850 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2851 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2852 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2853 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2854 .phy_cap_info[2] = 0,
2856 /* Leave all the other PHY capability bytes
2857 * unset, as DCM, beam forming, RU and PPE
2858 * threshold information are not supported
2861 .he_mcs_nss_supp = {
2862 .rx_mcs_80 = cpu_to_le16(0xfffa),
2863 .tx_mcs_80 = cpu_to_le16(0xfffa),
2864 .rx_mcs_160 = cpu_to_le16(0xffff),
2865 .tx_mcs_160 = cpu_to_le16(0xffff),
2866 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2867 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2874 static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2876 /* TODO: should we support other types, e.g., P2P?*/
2877 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2878 BIT(NL80211_IFTYPE_AP),
2883 IEEE80211_HE_MAC_CAP0_HTC_HE,
2885 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2886 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2888 IEEE80211_HE_MAC_CAP2_BSR |
2889 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2890 IEEE80211_HE_MAC_CAP2_ACK_EN,
2892 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2893 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2894 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2896 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2897 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2898 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2900 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2901 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2902 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2903 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2905 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2906 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2907 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2908 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2909 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2911 /* Leave all the other PHY capability bytes
2912 * unset, as DCM, beam forming, RU and PPE
2913 * threshold information are not supported
2916 .he_mcs_nss_supp = {
2917 .rx_mcs_80 = cpu_to_le16(0xfffa),
2918 .tx_mcs_80 = cpu_to_le16(0xfffa),
2919 .rx_mcs_160 = cpu_to_le16(0xfffa),
2920 .tx_mcs_160 = cpu_to_le16(0xfffa),
2921 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2922 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2926 #ifdef CONFIG_MAC80211_MESH
2928 /* TODO: should we support other types, e.g., IBSS?*/
2929 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2934 IEEE80211_HE_MAC_CAP0_HTC_HE,
2936 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2938 IEEE80211_HE_MAC_CAP2_ACK_EN,
2940 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2941 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2942 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2944 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2945 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2946 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2948 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2949 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2950 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2951 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2952 .phy_cap_info[2] = 0,
2954 /* Leave all the other PHY capability bytes
2955 * unset, as DCM, beam forming, RU and PPE
2956 * threshold information are not supported
2959 .he_mcs_nss_supp = {
2960 .rx_mcs_80 = cpu_to_le16(0xfffa),
2961 .tx_mcs_80 = cpu_to_le16(0xfffa),
2962 .rx_mcs_160 = cpu_to_le16(0xfffa),
2963 .tx_mcs_160 = cpu_to_le16(0xfffa),
2964 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2965 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2972 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2976 if (sband->band == NL80211_BAND_2GHZ) {
2977 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2978 sband->iftype_data =
2979 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2980 } else if (sband->band == NL80211_BAND_5GHZ) {
2981 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2982 sband->iftype_data =
2983 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2988 sband->n_iftype_data = n_iftype_data;
2991 #ifdef CONFIG_MAC80211_MESH
2992 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2994 #define HWSIM_MESH_BIT 0
2997 #define HWSIM_DEFAULT_IF_LIMIT \
2998 (BIT(NL80211_IFTYPE_STATION) | \
2999 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3000 BIT(NL80211_IFTYPE_AP) | \
3001 BIT(NL80211_IFTYPE_P2P_GO) | \
3004 #define HWSIM_IFTYPE_SUPPORT_MASK \
3005 (BIT(NL80211_IFTYPE_STATION) | \
3006 BIT(NL80211_IFTYPE_AP) | \
3007 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3008 BIT(NL80211_IFTYPE_P2P_GO) | \
3009 BIT(NL80211_IFTYPE_ADHOC) | \
3010 BIT(NL80211_IFTYPE_MESH_POINT) | \
3011 BIT(NL80211_IFTYPE_OCB))
3013 static int mac80211_hwsim_new_radio(struct genl_info *info,
3014 struct hwsim_new_radio_params *param)
3018 struct mac80211_hwsim_data *data;
3019 struct ieee80211_hw *hw;
3020 enum nl80211_band band;
3021 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
3026 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
3029 spin_lock_bh(&hwsim_radio_lock);
3030 idx = hwsim_radio_idx++;
3031 spin_unlock_bh(&hwsim_radio_lock);
3033 if (param->use_chanctx)
3034 ops = &mac80211_hwsim_mchan_ops;
3035 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
3037 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3042 /* ieee80211_alloc_hw_nm may have used a default name */
3043 param->hwname = wiphy_name(hw->wiphy);
3046 net = genl_info_net(info);
3049 wiphy_net_set(hw->wiphy, net);
3054 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3055 if (IS_ERR(data->dev)) {
3057 "mac80211_hwsim: device_create failed (%ld)\n",
3058 PTR_ERR(data->dev));
3060 goto failed_drvdata;
3062 data->dev->driver = &mac80211_hwsim_driver.driver;
3063 err = device_bind_driver(data->dev);
3065 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3070 skb_queue_head_init(&data->pending);
3072 SET_IEEE80211_DEV(hw, data->dev);
3073 if (!param->perm_addr) {
3074 eth_zero_addr(addr);
3078 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3079 /* Why need here second address ? */
3080 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3081 data->addresses[1].addr[0] |= 0x40;
3082 hw->wiphy->n_addresses = 2;
3083 hw->wiphy->addresses = data->addresses;
3084 /* possible address clash is checked at hash table insertion */
3086 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3087 /* compatibility with automatically generated mac addr */
3088 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3089 hw->wiphy->n_addresses = 2;
3090 hw->wiphy->addresses = data->addresses;
3093 data->channels = param->channels;
3094 data->use_chanctx = param->use_chanctx;
3096 data->destroy_on_close = param->destroy_on_close;
3098 data->portid = info->snd_portid;
3100 /* setup interface limits, only on interface types we support */
3101 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3102 data->if_limits[n_limits].max = 1;
3103 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3107 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3108 data->if_limits[n_limits].max = 2048;
3110 * For this case, we may only support a subset of
3111 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3112 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3114 data->if_limits[n_limits].types =
3115 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3119 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3120 data->if_limits[n_limits].max = 1;
3121 data->if_limits[n_limits].types =
3122 BIT(NL80211_IFTYPE_P2P_DEVICE);
3126 if (data->use_chanctx) {
3127 hw->wiphy->max_scan_ssids = 255;
3128 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3129 hw->wiphy->max_remain_on_channel_duration = 1000;
3130 data->if_combination.radar_detect_widths = 0;
3131 data->if_combination.num_different_channels = data->channels;
3133 data->if_combination.num_different_channels = 1;
3134 data->if_combination.radar_detect_widths =
3135 BIT(NL80211_CHAN_WIDTH_5) |
3136 BIT(NL80211_CHAN_WIDTH_10) |
3137 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3138 BIT(NL80211_CHAN_WIDTH_20) |
3139 BIT(NL80211_CHAN_WIDTH_40) |
3140 BIT(NL80211_CHAN_WIDTH_80) |
3141 BIT(NL80211_CHAN_WIDTH_160);
3149 data->if_combination.max_interfaces = 0;
3150 for (i = 0; i < n_limits; i++)
3151 data->if_combination.max_interfaces +=
3152 data->if_limits[i].max;
3154 data->if_combination.n_limits = n_limits;
3155 data->if_combination.limits = data->if_limits;
3158 * If we actually were asked to support combinations,
3159 * advertise them - if there's only a single thing like
3160 * only IBSS then don't advertise it as combinations.
3162 if (data->if_combination.max_interfaces > 1) {
3163 hw->wiphy->iface_combinations = &data->if_combination;
3164 hw->wiphy->n_iface_combinations = 1;
3167 if (param->ciphers) {
3168 memcpy(data->ciphers, param->ciphers,
3169 param->n_ciphers * sizeof(u32));
3170 hw->wiphy->cipher_suites = data->ciphers;
3171 hw->wiphy->n_cipher_suites = param->n_ciphers;
3174 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3175 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3176 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3179 hw->offchannel_tx_hw_queue = 4;
3181 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3182 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3183 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3184 ieee80211_hw_set(hw, QUEUE_CONTROL);
3185 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3186 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3187 ieee80211_hw_set(hw, MFP_CAPABLE);
3188 ieee80211_hw_set(hw, SIGNAL_DBM);
3189 ieee80211_hw_set(hw, SUPPORTS_PS);
3190 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3191 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3192 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3193 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3195 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3196 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3198 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3199 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3200 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3201 WIPHY_FLAG_AP_UAPSD |
3202 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3203 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3204 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3205 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3206 NL80211_FEATURE_STATIC_SMPS |
3207 NL80211_FEATURE_DYNAMIC_SMPS |
3208 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3209 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3210 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3211 wiphy_ext_feature_set(hw->wiphy,
3212 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3213 wiphy_ext_feature_set(hw->wiphy,
3214 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3216 hw->wiphy->interface_modes = param->iftypes;
3218 /* ask mac80211 to reserve space for magic */
3219 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3220 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3221 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3223 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3224 sizeof(hwsim_channels_2ghz));
3225 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3226 sizeof(hwsim_channels_5ghz));
3227 memcpy(data->channels_6ghz, hwsim_channels_6ghz,
3228 sizeof(hwsim_channels_6ghz));
3229 memcpy(data->channels_s1g, hwsim_channels_s1g,
3230 sizeof(hwsim_channels_s1g));
3231 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3233 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3234 struct ieee80211_supported_band *sband = &data->bands[band];
3239 case NL80211_BAND_2GHZ:
3240 sband->channels = data->channels_2ghz;
3241 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3242 sband->bitrates = data->rates;
3243 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3245 case NL80211_BAND_5GHZ:
3246 sband->channels = data->channels_5ghz;
3247 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3248 sband->bitrates = data->rates + 4;
3249 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3251 sband->vht_cap.vht_supported = true;
3252 sband->vht_cap.cap =
3253 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3254 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3255 IEEE80211_VHT_CAP_RXLDPC |
3256 IEEE80211_VHT_CAP_SHORT_GI_80 |
3257 IEEE80211_VHT_CAP_SHORT_GI_160 |
3258 IEEE80211_VHT_CAP_TXSTBC |
3259 IEEE80211_VHT_CAP_RXSTBC_4 |
3260 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3261 sband->vht_cap.vht_mcs.rx_mcs_map =
3262 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3263 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3264 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3265 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3266 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3267 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3268 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3269 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3270 sband->vht_cap.vht_mcs.tx_mcs_map =
3271 sband->vht_cap.vht_mcs.rx_mcs_map;
3273 case NL80211_BAND_S1GHZ:
3274 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3275 sizeof(sband->s1g_cap));
3276 sband->channels = data->channels_s1g;
3277 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3283 sband->ht_cap.ht_supported = true;
3284 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3285 IEEE80211_HT_CAP_GRN_FLD |
3286 IEEE80211_HT_CAP_SGI_20 |
3287 IEEE80211_HT_CAP_SGI_40 |
3288 IEEE80211_HT_CAP_DSSSCCK40;
3289 sband->ht_cap.ampdu_factor = 0x3;
3290 sband->ht_cap.ampdu_density = 0x6;
3291 memset(&sband->ht_cap.mcs, 0,
3292 sizeof(sband->ht_cap.mcs));
3293 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3294 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3295 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3297 mac80211_hwsim_he_capab(sband);
3299 hw->wiphy->bands[band] = sband;
3302 /* By default all radios belong to the first group */
3304 mutex_init(&data->mutex);
3306 data->netgroup = hwsim_net_get_netgroup(net);
3307 data->wmediumd = hwsim_net_get_wmediumd(net);
3309 /* Enable frame retransmissions for lossy channels */
3311 hw->max_rate_tries = 11;
3313 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3314 hw->wiphy->n_vendor_commands =
3315 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3316 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3317 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3319 if (param->reg_strict)
3320 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3322 data->regd = param->regd;
3323 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3324 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3325 /* give the regulatory workqueue a chance to run */
3326 schedule_timeout_interruptible(1);
3330 ieee80211_hw_set(hw, NO_AUTO_VIF);
3332 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3334 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3335 HRTIMER_MODE_ABS_SOFT);
3336 data->beacon_timer.function = mac80211_hwsim_beacon;
3338 err = ieee80211_register_hw(hw);
3340 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3345 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3347 if (param->reg_alpha2) {
3348 data->alpha2[0] = param->reg_alpha2[0];
3349 data->alpha2[1] = param->reg_alpha2[1];
3350 regulatory_hint(hw->wiphy, param->reg_alpha2);
3353 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3354 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3355 debugfs_create_file("group", 0666, data->debugfs, data,
3357 if (!data->use_chanctx)
3358 debugfs_create_file("dfs_simulate_radar", 0222,
3360 data, &hwsim_simulate_radar);
3362 spin_lock_bh(&hwsim_radio_lock);
3363 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3367 GENL_SET_ERR_MSG(info, "perm addr already present");
3368 NL_SET_BAD_ATTR(info->extack,
3369 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3371 spin_unlock_bh(&hwsim_radio_lock);
3372 goto failed_final_insert;
3375 list_add_tail(&data->list, &hwsim_radios);
3376 hwsim_radios_generation++;
3377 spin_unlock_bh(&hwsim_radio_lock);
3379 hwsim_mcast_new_radio(idx, info, param);
3383 failed_final_insert:
3384 debugfs_remove_recursive(data->debugfs);
3385 ieee80211_unregister_hw(data->hw);
3387 device_release_driver(data->dev);
3389 device_unregister(data->dev);
3391 ieee80211_free_hw(hw);
3396 static void hwsim_mcast_del_radio(int id, const char *hwname,
3397 struct genl_info *info)
3399 struct sk_buff *skb;
3403 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3407 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3408 HWSIM_CMD_DEL_RADIO);
3412 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3416 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3421 genlmsg_end(skb, data);
3423 hwsim_mcast_config_msg(skb, info);
3431 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3433 struct genl_info *info)
3435 hwsim_mcast_del_radio(data->idx, hwname, info);
3436 debugfs_remove_recursive(data->debugfs);
3437 ieee80211_unregister_hw(data->hw);
3438 device_release_driver(data->dev);
3439 device_unregister(data->dev);
3440 ieee80211_free_hw(data->hw);
3443 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3444 struct mac80211_hwsim_data *data,
3445 u32 portid, u32 seq,
3446 struct netlink_callback *cb, int flags)
3449 struct hwsim_new_radio_params param = { };
3450 int res = -EMSGSIZE;
3452 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3453 HWSIM_CMD_GET_RADIO);
3458 genl_dump_check_consistent(cb, hdr);
3460 if (data->alpha2[0] && data->alpha2[1])
3461 param.reg_alpha2 = data->alpha2;
3463 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3464 REGULATORY_STRICT_REG);
3465 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3466 BIT(NL80211_IFTYPE_P2P_DEVICE));
3467 param.use_chanctx = data->use_chanctx;
3468 param.regd = data->regd;
3469 param.channels = data->channels;
3470 param.hwname = wiphy_name(data->hw->wiphy);
3472 res = append_radio_msg(skb, data->idx, ¶m);
3476 genlmsg_end(skb, hdr);
3480 genlmsg_cancel(skb, hdr);
3484 static void mac80211_hwsim_free(void)
3486 struct mac80211_hwsim_data *data;
3488 spin_lock_bh(&hwsim_radio_lock);
3489 while ((data = list_first_entry_or_null(&hwsim_radios,
3490 struct mac80211_hwsim_data,
3492 list_del(&data->list);
3493 spin_unlock_bh(&hwsim_radio_lock);
3494 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3496 spin_lock_bh(&hwsim_radio_lock);
3498 spin_unlock_bh(&hwsim_radio_lock);
3499 class_destroy(hwsim_class);
3502 static const struct net_device_ops hwsim_netdev_ops = {
3503 .ndo_start_xmit = hwsim_mon_xmit,
3504 .ndo_set_mac_address = eth_mac_addr,
3505 .ndo_validate_addr = eth_validate_addr,
3508 static void hwsim_mon_setup(struct net_device *dev)
3510 dev->netdev_ops = &hwsim_netdev_ops;
3511 dev->needs_free_netdev = true;
3513 dev->priv_flags |= IFF_NO_QUEUE;
3514 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3515 eth_zero_addr(dev->dev_addr);
3516 dev->dev_addr[0] = 0x12;
3519 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3521 return rhashtable_lookup_fast(&hwsim_radios_rht,
3526 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3528 struct mac80211_hwsim_data *data;
3530 hwsim_net_set_wmediumd(net, portid);
3532 spin_lock_bh(&hwsim_radio_lock);
3533 list_for_each_entry(data, &hwsim_radios, list) {
3534 if (data->netgroup == hwsim_net_get_netgroup(net))
3535 data->wmediumd = portid;
3537 spin_unlock_bh(&hwsim_radio_lock);
3540 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3541 struct genl_info *info)
3544 struct ieee80211_hdr *hdr;
3545 struct mac80211_hwsim_data *data2;
3546 struct ieee80211_tx_info *txi;
3547 struct hwsim_tx_rate *tx_attempts;
3549 struct sk_buff *skb, *tmp;
3551 unsigned int hwsim_flags;
3555 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3556 !info->attrs[HWSIM_ATTR_FLAGS] ||
3557 !info->attrs[HWSIM_ATTR_COOKIE] ||
3558 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3559 !info->attrs[HWSIM_ATTR_TX_INFO])
3562 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3563 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3564 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3566 data2 = get_hwsim_data_ref_from_addr(src);
3570 if (!hwsim_virtio_enabled) {
3571 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3575 if (info->snd_portid != data2->wmediumd)
3579 /* look for the skb matching the cookie passed back from user */
3580 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3583 txi = IEEE80211_SKB_CB(skb);
3584 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3586 if (skb_cookie == ret_skb_cookie) {
3587 skb_unlink(skb, &data2->pending);
3597 /* Tx info received because the frame was broadcasted on user space,
3598 so we get all the necessary info: tx attempts and skb control buff */
3600 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3601 info->attrs[HWSIM_ATTR_TX_INFO]);
3603 /* now send back TX status */
3604 txi = IEEE80211_SKB_CB(skb);
3606 ieee80211_tx_info_clear_status(txi);
3608 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3609 txi->status.rates[i].idx = tx_attempts[i].idx;
3610 txi->status.rates[i].count = tx_attempts[i].count;
3613 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3615 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3616 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3617 if (skb->len >= 16) {
3618 hdr = (struct ieee80211_hdr *) skb->data;
3619 mac80211_hwsim_monitor_ack(data2->channel,
3622 txi->flags |= IEEE80211_TX_STAT_ACK;
3624 ieee80211_tx_status_irqsafe(data2->hw, skb);
3631 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3632 struct genl_info *info)
3634 struct mac80211_hwsim_data *data2;
3635 struct ieee80211_rx_status rx_status;
3636 struct ieee80211_hdr *hdr;
3640 struct sk_buff *skb = NULL;
3642 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3643 !info->attrs[HWSIM_ATTR_FRAME] ||
3644 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3645 !info->attrs[HWSIM_ATTR_SIGNAL])
3648 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3649 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3650 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3652 /* Allocate new skb here */
3653 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3657 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3661 skb_put_data(skb, frame_data, frame_data_len);
3663 data2 = get_hwsim_data_ref_from_addr(dst);
3667 if (!hwsim_virtio_enabled) {
3668 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3672 if (info->snd_portid != data2->wmediumd)
3676 /* check if radio is configured properly */
3678 if (data2->idle || !data2->started)
3681 /* A frame is received from user space */
3682 memset(&rx_status, 0, sizeof(rx_status));
3683 if (info->attrs[HWSIM_ATTR_FREQ]) {
3684 /* throw away off-channel packets, but allow both the temporary
3685 * ("hw" scan/remain-on-channel) and regular channel, since the
3686 * internal datapath also allows this
3688 mutex_lock(&data2->mutex);
3689 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3691 if (rx_status.freq != data2->channel->center_freq &&
3692 (!data2->tmp_chan ||
3693 rx_status.freq != data2->tmp_chan->center_freq)) {
3694 mutex_unlock(&data2->mutex);
3697 mutex_unlock(&data2->mutex);
3699 rx_status.freq = data2->channel->center_freq;
3702 rx_status.band = data2->channel->band;
3703 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3704 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3706 hdr = (void *)skb->data;
3708 if (ieee80211_is_beacon(hdr->frame_control) ||
3709 ieee80211_is_probe_resp(hdr->frame_control))
3710 rx_status.boottime_ns = ktime_get_boottime_ns();
3712 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3714 data2->rx_bytes += skb->len;
3715 ieee80211_rx_irqsafe(data2->hw, skb);
3719 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3725 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3726 struct genl_info *info)
3728 struct net *net = genl_info_net(info);
3729 struct mac80211_hwsim_data *data;
3732 spin_lock_bh(&hwsim_radio_lock);
3733 list_for_each_entry(data, &hwsim_radios, list)
3734 chans = max(chans, data->channels);
3735 spin_unlock_bh(&hwsim_radio_lock);
3737 /* In the future we should revise the userspace API and allow it
3738 * to set a flag that it does support multi-channel, then we can
3739 * let this pass conditionally on the flag.
3740 * For current userspace, prohibit it since it won't work right.
3745 if (hwsim_net_get_wmediumd(net))
3748 hwsim_register_wmediumd(net, info->snd_portid);
3750 pr_debug("mac80211_hwsim: received a REGISTER, "
3751 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3756 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3757 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3761 for (i = 0; i < n_ciphers; i++) {
3765 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3766 if (ciphers[i] == hwsim_ciphers[j]) {
3779 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3781 struct hwsim_new_radio_params param = { 0 };
3782 const char *hwname = NULL;
3785 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3786 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3787 param.channels = channels;
3788 param.destroy_on_close =
3789 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3791 if (info->attrs[HWSIM_ATTR_CHANNELS])
3792 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3794 if (param.channels < 1) {
3795 GENL_SET_ERR_MSG(info, "must have at least one channel");
3799 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3800 GENL_SET_ERR_MSG(info, "too many channels specified");
3804 if (info->attrs[HWSIM_ATTR_NO_VIF])
3805 param.no_vif = true;
3807 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3808 param.use_chanctx = true;
3810 param.use_chanctx = (param.channels > 1);
3812 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3814 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3816 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3817 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3819 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3822 idx = array_index_nospec(idx,
3823 ARRAY_SIZE(hwsim_world_regdom_custom));
3824 param.regd = hwsim_world_regdom_custom[idx];
3827 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3828 if (!is_valid_ether_addr(
3829 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3830 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3831 NL_SET_BAD_ATTR(info->extack,
3832 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3836 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3839 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3841 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3843 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3844 NL_SET_ERR_MSG_ATTR(info->extack,
3845 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3846 "cannot support more iftypes than kernel");
3850 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3853 /* ensure both flag and iftype support is honored */
3854 if (param.p2p_device ||
3855 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3856 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3857 param.p2p_device = true;
3860 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3861 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3864 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3866 if (len % sizeof(u32)) {
3867 NL_SET_ERR_MSG_ATTR(info->extack,
3868 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3869 "bad cipher list length");
3873 param.n_ciphers = len / sizeof(u32);
3875 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3876 NL_SET_ERR_MSG_ATTR(info->extack,
3877 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3878 "too many ciphers specified");
3882 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3883 NL_SET_ERR_MSG_ATTR(info->extack,
3884 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3885 "unsupported ciphers specified");
3890 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3891 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3892 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3896 param.hwname = hwname;
3899 ret = mac80211_hwsim_new_radio(info, ¶m);
3904 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3906 struct mac80211_hwsim_data *data;
3908 const char *hwname = NULL;
3910 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3911 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3912 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3913 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3914 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3921 spin_lock_bh(&hwsim_radio_lock);
3922 list_for_each_entry(data, &hwsim_radios, list) {
3924 if (data->idx != idx)
3928 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3932 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3935 list_del(&data->list);
3936 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3938 hwsim_radios_generation++;
3939 spin_unlock_bh(&hwsim_radio_lock);
3940 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3945 spin_unlock_bh(&hwsim_radio_lock);
3951 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3953 struct mac80211_hwsim_data *data;
3954 struct sk_buff *skb;
3955 int idx, res = -ENODEV;
3957 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3959 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3961 spin_lock_bh(&hwsim_radio_lock);
3962 list_for_each_entry(data, &hwsim_radios, list) {
3963 if (data->idx != idx)
3966 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3969 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3975 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3976 info->snd_seq, NULL, 0);
3982 res = genlmsg_reply(skb, info);
3987 spin_unlock_bh(&hwsim_radio_lock);
3992 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3993 struct netlink_callback *cb)
3995 int last_idx = cb->args[0] - 1;
3996 struct mac80211_hwsim_data *data = NULL;
4000 spin_lock_bh(&hwsim_radio_lock);
4001 cb->seq = hwsim_radios_generation;
4003 if (last_idx >= hwsim_radio_idx-1)
4006 list_for_each_entry(data, &hwsim_radios, list) {
4007 if (data->idx <= last_idx)
4010 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
4013 res = mac80211_hwsim_get_radio(skb, data,
4014 NETLINK_CB(cb->skb).portid,
4015 cb->nlh->nlmsg_seq, cb,
4020 last_idx = data->idx;
4023 cb->args[0] = last_idx + 1;
4025 /* list changed, but no new element sent, set interrupted flag */
4026 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4027 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4028 cb->nlh->nlmsg_seq, &hwsim_genl_family,
4029 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4031 genl_dump_check_consistent(cb, hdr);
4032 genlmsg_end(skb, hdr);
4039 spin_unlock_bh(&hwsim_radio_lock);
4040 return res ?: skb->len;
4043 /* Generic Netlink operations array */
4044 static const struct genl_small_ops hwsim_ops[] = {
4046 .cmd = HWSIM_CMD_REGISTER,
4047 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4048 .doit = hwsim_register_received_nl,
4049 .flags = GENL_UNS_ADMIN_PERM,
4052 .cmd = HWSIM_CMD_FRAME,
4053 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4054 .doit = hwsim_cloned_frame_received_nl,
4057 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4058 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4059 .doit = hwsim_tx_info_frame_received_nl,
4062 .cmd = HWSIM_CMD_NEW_RADIO,
4063 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4064 .doit = hwsim_new_radio_nl,
4065 .flags = GENL_UNS_ADMIN_PERM,
4068 .cmd = HWSIM_CMD_DEL_RADIO,
4069 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4070 .doit = hwsim_del_radio_nl,
4071 .flags = GENL_UNS_ADMIN_PERM,
4074 .cmd = HWSIM_CMD_GET_RADIO,
4075 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4076 .doit = hwsim_get_radio_nl,
4077 .dumpit = hwsim_dump_radio_nl,
4081 static struct genl_family hwsim_genl_family __ro_after_init = {
4082 .name = "MAC80211_HWSIM",
4084 .maxattr = HWSIM_ATTR_MAX,
4085 .policy = hwsim_genl_policy,
4087 .module = THIS_MODULE,
4088 .small_ops = hwsim_ops,
4089 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4090 .mcgrps = hwsim_mcgrps,
4091 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4094 static void remove_user_radios(u32 portid)
4096 struct mac80211_hwsim_data *entry, *tmp;
4099 spin_lock_bh(&hwsim_radio_lock);
4100 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4101 if (entry->destroy_on_close && entry->portid == portid) {
4102 list_move(&entry->list, &list);
4103 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4105 hwsim_radios_generation++;
4108 spin_unlock_bh(&hwsim_radio_lock);
4110 list_for_each_entry_safe(entry, tmp, &list, list) {
4111 list_del(&entry->list);
4112 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4117 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4118 unsigned long state,
4121 struct netlink_notify *notify = _notify;
4123 if (state != NETLINK_URELEASE)
4126 remove_user_radios(notify->portid);
4128 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4129 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4130 " socket, switching to perfect channel medium\n");
4131 hwsim_register_wmediumd(notify->net, 0);
4137 static struct notifier_block hwsim_netlink_notifier = {
4138 .notifier_call = mac80211_hwsim_netlink_notify,
4141 static int __init hwsim_init_netlink(void)
4145 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4147 rc = genl_register_family(&hwsim_genl_family);
4151 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4153 genl_unregister_family(&hwsim_genl_family);
4160 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4164 static __net_init int hwsim_init_net(struct net *net)
4166 return hwsim_net_set_netgroup(net);
4169 static void __net_exit hwsim_exit_net(struct net *net)
4171 struct mac80211_hwsim_data *data, *tmp;
4174 spin_lock_bh(&hwsim_radio_lock);
4175 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4176 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4179 /* Radios created in init_net are returned to init_net. */
4180 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4183 list_move(&data->list, &list);
4184 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4186 hwsim_radios_generation++;
4188 spin_unlock_bh(&hwsim_radio_lock);
4190 list_for_each_entry_safe(data, tmp, &list, list) {
4191 list_del(&data->list);
4192 mac80211_hwsim_del_radio(data,
4193 wiphy_name(data->hw->wiphy),
4197 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4200 static struct pernet_operations hwsim_net_ops = {
4201 .init = hwsim_init_net,
4202 .exit = hwsim_exit_net,
4203 .id = &hwsim_net_id,
4204 .size = sizeof(struct hwsim_net),
4207 static void hwsim_exit_netlink(void)
4209 /* unregister the notifier */
4210 netlink_unregister_notifier(&hwsim_netlink_notifier);
4211 /* unregister the family */
4212 genl_unregister_family(&hwsim_genl_family);
4215 #if IS_REACHABLE(CONFIG_VIRTIO)
4216 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4219 struct sk_buff *skb;
4220 unsigned long flags;
4222 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4223 while ((skb = virtqueue_get_buf(vq, &len)))
4225 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4228 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4230 struct nlmsghdr *nlh;
4231 struct genlmsghdr *gnlh;
4232 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4233 struct genl_info info = {};
4236 nlh = nlmsg_hdr(skb);
4237 gnlh = nlmsg_data(nlh);
4238 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4239 hwsim_genl_policy, NULL);
4241 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4247 switch (gnlh->cmd) {
4248 case HWSIM_CMD_FRAME:
4249 hwsim_cloned_frame_received_nl(skb, &info);
4251 case HWSIM_CMD_TX_INFO_FRAME:
4252 hwsim_tx_info_frame_received_nl(skb, &info);
4255 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4261 static void hwsim_virtio_rx_work(struct work_struct *work)
4263 struct virtqueue *vq;
4265 struct sk_buff *skb;
4266 struct scatterlist sg[1];
4268 unsigned long flags;
4270 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4271 if (!hwsim_virtio_enabled)
4274 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4277 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4279 skb->data = skb->head;
4280 skb_set_tail_pointer(skb, len);
4281 hwsim_virtio_handle_cmd(skb);
4283 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4284 if (!hwsim_virtio_enabled) {
4288 vq = hwsim_vqs[HWSIM_VQ_RX];
4289 sg_init_one(sg, skb->head, skb_end_offset(skb));
4290 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4291 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4295 schedule_work(&hwsim_virtio_rx);
4298 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4301 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4303 schedule_work(&hwsim_virtio_rx);
4306 static int init_vqs(struct virtio_device *vdev)
4308 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4309 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4310 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4312 const char *names[HWSIM_NUM_VQS] = {
4313 [HWSIM_VQ_TX] = "tx",
4314 [HWSIM_VQ_RX] = "rx",
4317 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4318 hwsim_vqs, callbacks, names, NULL);
4321 static int fill_vq(struct virtqueue *vq)
4324 struct sk_buff *skb;
4325 struct scatterlist sg[1];
4327 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4328 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4332 sg_init_one(sg, skb->head, skb_end_offset(skb));
4333 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4343 static void remove_vqs(struct virtio_device *vdev)
4347 vdev->config->reset(vdev);
4349 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4350 struct virtqueue *vq = hwsim_vqs[i];
4351 struct sk_buff *skb;
4353 while ((skb = virtqueue_detach_unused_buf(vq)))
4357 vdev->config->del_vqs(vdev);
4360 static int hwsim_virtio_probe(struct virtio_device *vdev)
4363 unsigned long flags;
4365 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4366 if (hwsim_virtio_enabled) {
4367 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4370 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4372 err = init_vqs(vdev);
4376 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4380 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4381 hwsim_virtio_enabled = true;
4382 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4384 schedule_work(&hwsim_virtio_rx);
4392 static void hwsim_virtio_remove(struct virtio_device *vdev)
4394 hwsim_virtio_enabled = false;
4396 cancel_work_sync(&hwsim_virtio_rx);
4401 /* MAC80211_HWSIM virtio device id table */
4402 static const struct virtio_device_id id_table[] = {
4403 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4406 MODULE_DEVICE_TABLE(virtio, id_table);
4408 static struct virtio_driver virtio_hwsim = {
4409 .driver.name = KBUILD_MODNAME,
4410 .driver.owner = THIS_MODULE,
4411 .id_table = id_table,
4412 .probe = hwsim_virtio_probe,
4413 .remove = hwsim_virtio_remove,
4416 static int hwsim_register_virtio_driver(void)
4418 return register_virtio_driver(&virtio_hwsim);
4421 static void hwsim_unregister_virtio_driver(void)
4423 unregister_virtio_driver(&virtio_hwsim);
4426 static inline int hwsim_register_virtio_driver(void)
4431 static inline void hwsim_unregister_virtio_driver(void)
4436 static int __init init_mac80211_hwsim(void)
4440 if (radios < 0 || radios > 100)
4446 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4450 err = register_pernet_device(&hwsim_net_ops);
4454 err = platform_driver_register(&mac80211_hwsim_driver);
4456 goto out_unregister_pernet;
4458 err = hwsim_init_netlink();
4460 goto out_unregister_driver;
4462 err = hwsim_register_virtio_driver();
4464 goto out_exit_netlink;
4466 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4467 if (IS_ERR(hwsim_class)) {
4468 err = PTR_ERR(hwsim_class);
4469 goto out_exit_virtio;
4472 hwsim_init_s1g_channels(hwsim_channels_s1g);
4474 for (i = 0; i < radios; i++) {
4475 struct hwsim_new_radio_params param = { 0 };
4477 param.channels = channels;
4480 case HWSIM_REGTEST_DIFF_COUNTRY:
4481 if (i < ARRAY_SIZE(hwsim_alpha2s))
4482 param.reg_alpha2 = hwsim_alpha2s[i];
4484 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4486 param.reg_alpha2 = hwsim_alpha2s[0];
4488 case HWSIM_REGTEST_STRICT_ALL:
4489 param.reg_strict = true;
4491 case HWSIM_REGTEST_DRIVER_REG_ALL:
4492 param.reg_alpha2 = hwsim_alpha2s[0];
4494 case HWSIM_REGTEST_WORLD_ROAM:
4496 param.regd = &hwsim_world_regdom_custom_01;
4498 case HWSIM_REGTEST_CUSTOM_WORLD:
4499 param.regd = &hwsim_world_regdom_custom_01;
4501 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4503 param.regd = &hwsim_world_regdom_custom_01;
4505 param.regd = &hwsim_world_regdom_custom_02;
4507 case HWSIM_REGTEST_STRICT_FOLLOW:
4509 param.reg_strict = true;
4510 param.reg_alpha2 = hwsim_alpha2s[0];
4513 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4515 param.reg_strict = true;
4516 param.reg_alpha2 = hwsim_alpha2s[0];
4517 } else if (i == 1) {
4518 param.reg_alpha2 = hwsim_alpha2s[1];
4521 case HWSIM_REGTEST_ALL:
4524 param.regd = &hwsim_world_regdom_custom_01;
4527 param.regd = &hwsim_world_regdom_custom_02;
4530 param.reg_alpha2 = hwsim_alpha2s[0];
4533 param.reg_alpha2 = hwsim_alpha2s[1];
4536 param.reg_strict = true;
4537 param.reg_alpha2 = hwsim_alpha2s[2];
4545 param.p2p_device = support_p2p_device;
4546 param.use_chanctx = channels > 1;
4547 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4548 if (param.p2p_device)
4549 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4551 err = mac80211_hwsim_new_radio(NULL, ¶m);
4553 goto out_free_radios;
4556 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4558 if (hwsim_mon == NULL) {
4560 goto out_free_radios;
4564 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4570 err = register_netdevice(hwsim_mon);
4580 free_netdev(hwsim_mon);
4582 mac80211_hwsim_free();
4584 hwsim_unregister_virtio_driver();
4586 hwsim_exit_netlink();
4587 out_unregister_driver:
4588 platform_driver_unregister(&mac80211_hwsim_driver);
4589 out_unregister_pernet:
4590 unregister_pernet_device(&hwsim_net_ops);
4592 rhashtable_destroy(&hwsim_radios_rht);
4595 module_init(init_mac80211_hwsim);
4597 static void __exit exit_mac80211_hwsim(void)
4599 pr_debug("mac80211_hwsim: unregister radios\n");
4601 hwsim_unregister_virtio_driver();
4602 hwsim_exit_netlink();
4604 mac80211_hwsim_free();
4606 rhashtable_destroy(&hwsim_radios_rht);
4607 unregister_netdev(hwsim_mon);
4608 platform_driver_unregister(&mac80211_hwsim_driver);
4609 unregister_pernet_device(&hwsim_net_ops);
4611 module_exit(exit_mac80211_hwsim);