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;
1696 data->started = false;
1697 hrtimer_cancel(&data->beacon_timer);
1698 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1702 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1703 struct ieee80211_vif *vif)
1705 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1706 __func__, ieee80211_vif_type_p2p(vif),
1708 hwsim_set_magic(vif);
1710 if (vif->type != NL80211_IFTYPE_MONITOR)
1711 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1714 vif->hw_queue[IEEE80211_AC_VO] = 0;
1715 vif->hw_queue[IEEE80211_AC_VI] = 1;
1716 vif->hw_queue[IEEE80211_AC_BE] = 2;
1717 vif->hw_queue[IEEE80211_AC_BK] = 3;
1723 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1724 struct ieee80211_vif *vif,
1725 enum nl80211_iftype newtype,
1728 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1729 wiphy_dbg(hw->wiphy,
1730 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1731 __func__, ieee80211_vif_type_p2p(vif),
1732 newtype, vif->addr);
1733 hwsim_check_magic(vif);
1736 * interface may change from non-AP to AP in
1737 * which case this needs to be set up again
1744 static void mac80211_hwsim_remove_interface(
1745 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1747 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1748 __func__, ieee80211_vif_type_p2p(vif),
1750 hwsim_check_magic(vif);
1751 hwsim_clear_magic(vif);
1752 if (vif->type != NL80211_IFTYPE_MONITOR)
1753 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1756 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1757 struct sk_buff *skb,
1758 struct ieee80211_channel *chan)
1760 struct mac80211_hwsim_data *data = hw->priv;
1761 u32 _pid = READ_ONCE(data->wmediumd);
1763 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1764 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1765 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1767 ARRAY_SIZE(txi->control.rates));
1770 mac80211_hwsim_monitor_rx(hw, skb, chan);
1772 if (_pid || hwsim_virtio_enabled)
1773 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1775 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1779 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1780 struct ieee80211_vif *vif)
1782 struct mac80211_hwsim_data *data = arg;
1783 struct ieee80211_hw *hw = data->hw;
1784 struct ieee80211_tx_info *info;
1785 struct ieee80211_rate *txrate;
1786 struct ieee80211_mgmt *mgmt;
1787 struct sk_buff *skb;
1791 hwsim_check_magic(vif);
1793 if (vif->type != NL80211_IFTYPE_AP &&
1794 vif->type != NL80211_IFTYPE_MESH_POINT &&
1795 vif->type != NL80211_IFTYPE_ADHOC &&
1796 vif->type != NL80211_IFTYPE_OCB)
1799 skb = ieee80211_beacon_get(hw, vif);
1802 info = IEEE80211_SKB_CB(skb);
1803 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1804 ieee80211_get_tx_rates(vif, NULL, skb,
1805 info->control.rates,
1806 ARRAY_SIZE(info->control.rates));
1808 txrate = ieee80211_get_tx_rate(hw, info);
1810 bitrate = txrate->bitrate;
1812 mgmt = (struct ieee80211_mgmt *) skb->data;
1813 /* fake header transmission time */
1814 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1815 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1816 struct ieee80211_ext *ext = (void *) mgmt;
1818 ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1823 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1829 mac80211_hwsim_tx_frame(hw, skb,
1830 rcu_dereference(vif->chanctx_conf)->def.chan);
1832 while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1833 mac80211_hwsim_tx_frame(hw, skb,
1834 rcu_dereference(vif->chanctx_conf)->def.chan);
1837 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1838 ieee80211_csa_finish(vif);
1841 static enum hrtimer_restart
1842 mac80211_hwsim_beacon(struct hrtimer *timer)
1844 struct mac80211_hwsim_data *data =
1845 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1846 struct ieee80211_hw *hw = data->hw;
1847 u64 bcn_int = data->beacon_int;
1850 return HRTIMER_NORESTART;
1852 ieee80211_iterate_active_interfaces_atomic(
1853 hw, IEEE80211_IFACE_ITER_NORMAL,
1854 mac80211_hwsim_beacon_tx, data);
1856 /* beacon at new TBTT + beacon interval */
1857 if (data->bcn_delta) {
1858 bcn_int -= data->bcn_delta;
1859 data->bcn_delta = 0;
1861 hrtimer_forward(&data->beacon_timer, hrtimer_get_expires(timer),
1862 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1863 return HRTIMER_RESTART;
1866 static const char * const hwsim_chanwidths[] = {
1867 [NL80211_CHAN_WIDTH_5] = "ht5",
1868 [NL80211_CHAN_WIDTH_10] = "ht10",
1869 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1870 [NL80211_CHAN_WIDTH_20] = "ht20",
1871 [NL80211_CHAN_WIDTH_40] = "ht40",
1872 [NL80211_CHAN_WIDTH_80] = "vht80",
1873 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1874 [NL80211_CHAN_WIDTH_160] = "vht160",
1875 [NL80211_CHAN_WIDTH_1] = "1MHz",
1876 [NL80211_CHAN_WIDTH_2] = "2MHz",
1877 [NL80211_CHAN_WIDTH_4] = "4MHz",
1878 [NL80211_CHAN_WIDTH_8] = "8MHz",
1879 [NL80211_CHAN_WIDTH_16] = "16MHz",
1882 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1884 struct mac80211_hwsim_data *data = hw->priv;
1885 struct ieee80211_conf *conf = &hw->conf;
1886 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1887 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1888 [IEEE80211_SMPS_OFF] = "off",
1889 [IEEE80211_SMPS_STATIC] = "static",
1890 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1894 if (conf->chandef.chan)
1895 wiphy_dbg(hw->wiphy,
1896 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1898 conf->chandef.chan->center_freq,
1899 conf->chandef.center_freq1,
1900 conf->chandef.center_freq2,
1901 hwsim_chanwidths[conf->chandef.width],
1902 !!(conf->flags & IEEE80211_CONF_IDLE),
1903 !!(conf->flags & IEEE80211_CONF_PS),
1904 smps_modes[conf->smps_mode]);
1906 wiphy_dbg(hw->wiphy,
1907 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1909 !!(conf->flags & IEEE80211_CONF_IDLE),
1910 !!(conf->flags & IEEE80211_CONF_PS),
1911 smps_modes[conf->smps_mode]);
1913 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1915 WARN_ON(conf->chandef.chan && data->use_chanctx);
1917 mutex_lock(&data->mutex);
1918 if (data->scanning && conf->chandef.chan) {
1919 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1920 if (data->survey_data[idx].channel == data->channel) {
1921 data->survey_data[idx].start =
1922 data->survey_data[idx].next_start;
1923 data->survey_data[idx].end = jiffies;
1928 data->channel = conf->chandef.chan;
1930 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1931 if (data->survey_data[idx].channel &&
1932 data->survey_data[idx].channel != data->channel)
1934 data->survey_data[idx].channel = data->channel;
1935 data->survey_data[idx].next_start = jiffies;
1939 data->channel = conf->chandef.chan;
1941 mutex_unlock(&data->mutex);
1943 if (!data->started || !data->beacon_int)
1944 hrtimer_cancel(&data->beacon_timer);
1945 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1946 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1947 u32 bcn_int = data->beacon_int;
1948 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1950 hrtimer_start(&data->beacon_timer,
1951 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1952 HRTIMER_MODE_REL_SOFT);
1959 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1960 unsigned int changed_flags,
1961 unsigned int *total_flags,u64 multicast)
1963 struct mac80211_hwsim_data *data = hw->priv;
1965 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1967 data->rx_filter = 0;
1968 if (*total_flags & FIF_ALLMULTI)
1969 data->rx_filter |= FIF_ALLMULTI;
1970 if (*total_flags & FIF_MCAST_ACTION)
1971 data->rx_filter |= FIF_MCAST_ACTION;
1973 *total_flags = data->rx_filter;
1976 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1977 struct ieee80211_vif *vif)
1979 unsigned int *count = data;
1980 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1986 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1987 struct ieee80211_vif *vif,
1988 struct ieee80211_bss_conf *info,
1991 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1992 struct mac80211_hwsim_data *data = hw->priv;
1994 hwsim_check_magic(vif);
1996 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1997 __func__, changed, vif->addr);
1999 if (changed & BSS_CHANGED_BSSID) {
2000 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
2001 __func__, info->bssid);
2002 memcpy(vp->bssid, info->bssid, ETH_ALEN);
2005 if (changed & BSS_CHANGED_ASSOC) {
2006 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
2007 info->assoc, info->aid);
2008 vp->assoc = info->assoc;
2009 vp->aid = info->aid;
2012 if (changed & BSS_CHANGED_BEACON_ENABLED) {
2013 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
2014 info->enable_beacon, info->beacon_int);
2015 vp->bcn_en = info->enable_beacon;
2016 if (data->started &&
2017 !hrtimer_is_queued(&data->beacon_timer) &&
2018 info->enable_beacon) {
2019 u64 tsf, until_tbtt;
2021 data->beacon_int = info->beacon_int * 1024;
2022 tsf = mac80211_hwsim_get_tsf(hw, vif);
2023 bcn_int = data->beacon_int;
2024 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2026 hrtimer_start(&data->beacon_timer,
2027 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2028 HRTIMER_MODE_REL_SOFT);
2029 } else if (!info->enable_beacon) {
2030 unsigned int count = 0;
2031 ieee80211_iterate_active_interfaces_atomic(
2032 data->hw, IEEE80211_IFACE_ITER_NORMAL,
2033 mac80211_hwsim_bcn_en_iter, &count);
2034 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
2037 hrtimer_cancel(&data->beacon_timer);
2038 data->beacon_int = 0;
2043 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2044 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
2045 info->use_cts_prot);
2048 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2049 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
2050 info->use_short_preamble);
2053 if (changed & BSS_CHANGED_ERP_SLOT) {
2054 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
2057 if (changed & BSS_CHANGED_HT) {
2058 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
2059 info->ht_operation_mode);
2062 if (changed & BSS_CHANGED_BASIC_RATES) {
2063 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
2064 (unsigned long long) info->basic_rates);
2067 if (changed & BSS_CHANGED_TXPOWER)
2068 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
2071 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2072 struct ieee80211_vif *vif,
2073 struct ieee80211_sta *sta)
2075 hwsim_check_magic(vif);
2076 hwsim_set_sta_magic(sta);
2081 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2082 struct ieee80211_vif *vif,
2083 struct ieee80211_sta *sta)
2085 hwsim_check_magic(vif);
2086 hwsim_clear_sta_magic(sta);
2091 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2092 struct ieee80211_vif *vif,
2093 enum sta_notify_cmd cmd,
2094 struct ieee80211_sta *sta)
2096 hwsim_check_magic(vif);
2099 case STA_NOTIFY_SLEEP:
2100 case STA_NOTIFY_AWAKE:
2101 /* TODO: make good use of these flags */
2104 WARN(1, "Invalid sta notify: %d\n", cmd);
2109 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2110 struct ieee80211_sta *sta,
2113 hwsim_check_sta_magic(sta);
2117 static int mac80211_hwsim_conf_tx(
2118 struct ieee80211_hw *hw,
2119 struct ieee80211_vif *vif, u16 queue,
2120 const struct ieee80211_tx_queue_params *params)
2122 wiphy_dbg(hw->wiphy,
2123 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2125 params->txop, params->cw_min,
2126 params->cw_max, params->aifs);
2130 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2131 struct survey_info *survey)
2133 struct mac80211_hwsim_data *hwsim = hw->priv;
2135 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2138 mutex_lock(&hwsim->mutex);
2139 survey->channel = hwsim->survey_data[idx].channel;
2140 if (!survey->channel) {
2141 mutex_unlock(&hwsim->mutex);
2146 * Magically conjured dummy values --- this is only ok for simulated hardware.
2148 * A real driver which cannot determine real values noise MUST NOT
2149 * report any, especially not a magically conjured ones :-)
2151 survey->filled = SURVEY_INFO_NOISE_DBM |
2153 SURVEY_INFO_TIME_BUSY;
2154 survey->noise = -92;
2156 jiffies_to_msecs(hwsim->survey_data[idx].end -
2157 hwsim->survey_data[idx].start);
2158 /* report 12.5% of channel time is used */
2159 survey->time_busy = survey->time/8;
2160 mutex_unlock(&hwsim->mutex);
2165 #ifdef CONFIG_NL80211_TESTMODE
2167 * This section contains example code for using netlink
2168 * attributes with the testmode command in nl80211.
2171 /* These enums need to be kept in sync with userspace */
2172 enum hwsim_testmode_attr {
2173 __HWSIM_TM_ATTR_INVALID = 0,
2174 HWSIM_TM_ATTR_CMD = 1,
2175 HWSIM_TM_ATTR_PS = 2,
2178 __HWSIM_TM_ATTR_AFTER_LAST,
2179 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
2182 enum hwsim_testmode_cmd {
2183 HWSIM_TM_CMD_SET_PS = 0,
2184 HWSIM_TM_CMD_GET_PS = 1,
2185 HWSIM_TM_CMD_STOP_QUEUES = 2,
2186 HWSIM_TM_CMD_WAKE_QUEUES = 3,
2189 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2190 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2191 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2194 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2195 struct ieee80211_vif *vif,
2196 void *data, int len)
2198 struct mac80211_hwsim_data *hwsim = hw->priv;
2199 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2200 struct sk_buff *skb;
2203 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2204 hwsim_testmode_policy, NULL);
2208 if (!tb[HWSIM_TM_ATTR_CMD])
2211 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2212 case HWSIM_TM_CMD_SET_PS:
2213 if (!tb[HWSIM_TM_ATTR_PS])
2215 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2216 return hwsim_fops_ps_write(hwsim, ps);
2217 case HWSIM_TM_CMD_GET_PS:
2218 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2219 nla_total_size(sizeof(u32)));
2222 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2223 goto nla_put_failure;
2224 return cfg80211_testmode_reply(skb);
2225 case HWSIM_TM_CMD_STOP_QUEUES:
2226 ieee80211_stop_queues(hw);
2228 case HWSIM_TM_CMD_WAKE_QUEUES:
2229 ieee80211_wake_queues(hw);
2241 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2242 struct ieee80211_vif *vif,
2243 struct ieee80211_ampdu_params *params)
2245 struct ieee80211_sta *sta = params->sta;
2246 enum ieee80211_ampdu_mlme_action action = params->action;
2247 u16 tid = params->tid;
2250 case IEEE80211_AMPDU_TX_START:
2251 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2252 case IEEE80211_AMPDU_TX_STOP_CONT:
2253 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2254 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2255 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2257 case IEEE80211_AMPDU_TX_OPERATIONAL:
2259 case IEEE80211_AMPDU_RX_START:
2260 case IEEE80211_AMPDU_RX_STOP:
2269 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2270 struct ieee80211_vif *vif,
2271 u32 queues, bool drop)
2273 /* Not implemented, queues only on kernel side */
2276 static void hw_scan_work(struct work_struct *work)
2278 struct mac80211_hwsim_data *hwsim =
2279 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2280 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2283 mutex_lock(&hwsim->mutex);
2284 if (hwsim->scan_chan_idx >= req->n_channels) {
2285 struct cfg80211_scan_info info = {
2289 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2290 ieee80211_scan_completed(hwsim->hw, &info);
2291 hwsim->hw_scan_request = NULL;
2292 hwsim->hw_scan_vif = NULL;
2293 hwsim->tmp_chan = NULL;
2294 mutex_unlock(&hwsim->mutex);
2295 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2300 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2301 req->channels[hwsim->scan_chan_idx]->center_freq);
2303 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2304 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2305 IEEE80211_CHAN_RADAR) ||
2311 for (i = 0; i < req->n_ssids; i++) {
2312 struct sk_buff *probe;
2313 struct ieee80211_mgmt *mgmt;
2315 probe = ieee80211_probereq_get(hwsim->hw,
2318 req->ssids[i].ssid_len,
2323 mgmt = (struct ieee80211_mgmt *) probe->data;
2324 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2325 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2328 skb_put_data(probe, req->ie, req->ie_len);
2331 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2336 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2337 msecs_to_jiffies(dwell));
2338 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2339 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2340 hwsim->survey_data[hwsim->scan_chan_idx].end =
2341 jiffies + msecs_to_jiffies(dwell);
2342 hwsim->scan_chan_idx++;
2343 mutex_unlock(&hwsim->mutex);
2346 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2347 struct ieee80211_vif *vif,
2348 struct ieee80211_scan_request *hw_req)
2350 struct mac80211_hwsim_data *hwsim = hw->priv;
2351 struct cfg80211_scan_request *req = &hw_req->req;
2353 mutex_lock(&hwsim->mutex);
2354 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2355 mutex_unlock(&hwsim->mutex);
2358 hwsim->hw_scan_request = req;
2359 hwsim->hw_scan_vif = vif;
2360 hwsim->scan_chan_idx = 0;
2361 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2362 get_random_mask_addr(hwsim->scan_addr,
2363 hw_req->req.mac_addr,
2364 hw_req->req.mac_addr_mask);
2366 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2367 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2368 mutex_unlock(&hwsim->mutex);
2370 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2371 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2373 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2378 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2379 struct ieee80211_vif *vif)
2381 struct mac80211_hwsim_data *hwsim = hw->priv;
2382 struct cfg80211_scan_info info = {
2386 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2388 cancel_delayed_work_sync(&hwsim->hw_scan);
2390 mutex_lock(&hwsim->mutex);
2391 ieee80211_scan_completed(hwsim->hw, &info);
2392 hwsim->tmp_chan = NULL;
2393 hwsim->hw_scan_request = NULL;
2394 hwsim->hw_scan_vif = NULL;
2395 mutex_unlock(&hwsim->mutex);
2398 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2399 struct ieee80211_vif *vif,
2402 struct mac80211_hwsim_data *hwsim = hw->priv;
2404 mutex_lock(&hwsim->mutex);
2406 if (hwsim->scanning) {
2407 pr_debug("two hwsim sw_scans detected!\n");
2411 pr_debug("hwsim sw_scan request, prepping stuff\n");
2413 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2414 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2415 hwsim->scanning = true;
2416 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2419 mutex_unlock(&hwsim->mutex);
2422 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2423 struct ieee80211_vif *vif)
2425 struct mac80211_hwsim_data *hwsim = hw->priv;
2427 mutex_lock(&hwsim->mutex);
2429 pr_debug("hwsim sw_scan_complete\n");
2430 hwsim->scanning = false;
2431 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2432 eth_zero_addr(hwsim->scan_addr);
2434 mutex_unlock(&hwsim->mutex);
2437 static void hw_roc_start(struct work_struct *work)
2439 struct mac80211_hwsim_data *hwsim =
2440 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2442 mutex_lock(&hwsim->mutex);
2444 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2445 hwsim->tmp_chan = hwsim->roc_chan;
2446 ieee80211_ready_on_channel(hwsim->hw);
2448 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2449 msecs_to_jiffies(hwsim->roc_duration));
2451 mutex_unlock(&hwsim->mutex);
2454 static void hw_roc_done(struct work_struct *work)
2456 struct mac80211_hwsim_data *hwsim =
2457 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2459 mutex_lock(&hwsim->mutex);
2460 ieee80211_remain_on_channel_expired(hwsim->hw);
2461 hwsim->tmp_chan = NULL;
2462 mutex_unlock(&hwsim->mutex);
2464 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2467 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2468 struct ieee80211_vif *vif,
2469 struct ieee80211_channel *chan,
2471 enum ieee80211_roc_type type)
2473 struct mac80211_hwsim_data *hwsim = hw->priv;
2475 mutex_lock(&hwsim->mutex);
2476 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2477 mutex_unlock(&hwsim->mutex);
2481 hwsim->roc_chan = chan;
2482 hwsim->roc_duration = duration;
2483 mutex_unlock(&hwsim->mutex);
2485 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2486 chan->center_freq, duration);
2487 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2492 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2493 struct ieee80211_vif *vif)
2495 struct mac80211_hwsim_data *hwsim = hw->priv;
2497 cancel_delayed_work_sync(&hwsim->roc_start);
2498 cancel_delayed_work_sync(&hwsim->roc_done);
2500 mutex_lock(&hwsim->mutex);
2501 hwsim->tmp_chan = NULL;
2502 mutex_unlock(&hwsim->mutex);
2504 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2509 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2510 struct ieee80211_chanctx_conf *ctx)
2512 hwsim_set_chanctx_magic(ctx);
2513 wiphy_dbg(hw->wiphy,
2514 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2515 ctx->def.chan->center_freq, ctx->def.width,
2516 ctx->def.center_freq1, ctx->def.center_freq2);
2520 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2521 struct ieee80211_chanctx_conf *ctx)
2523 wiphy_dbg(hw->wiphy,
2524 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2525 ctx->def.chan->center_freq, ctx->def.width,
2526 ctx->def.center_freq1, ctx->def.center_freq2);
2527 hwsim_check_chanctx_magic(ctx);
2528 hwsim_clear_chanctx_magic(ctx);
2531 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2532 struct ieee80211_chanctx_conf *ctx,
2535 hwsim_check_chanctx_magic(ctx);
2536 wiphy_dbg(hw->wiphy,
2537 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2538 ctx->def.chan->center_freq, ctx->def.width,
2539 ctx->def.center_freq1, ctx->def.center_freq2);
2542 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2543 struct ieee80211_vif *vif,
2544 struct ieee80211_chanctx_conf *ctx)
2546 hwsim_check_magic(vif);
2547 hwsim_check_chanctx_magic(ctx);
2552 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2553 struct ieee80211_vif *vif,
2554 struct ieee80211_chanctx_conf *ctx)
2556 hwsim_check_magic(vif);
2557 hwsim_check_chanctx_magic(ctx);
2560 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2571 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2573 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2574 struct ieee80211_vif *vif,
2577 if (sset == ETH_SS_STATS)
2578 memcpy(data, *mac80211_hwsim_gstrings_stats,
2579 sizeof(mac80211_hwsim_gstrings_stats));
2582 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2583 struct ieee80211_vif *vif, int sset)
2585 if (sset == ETH_SS_STATS)
2586 return MAC80211_HWSIM_SSTATS_LEN;
2590 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2591 struct ieee80211_vif *vif,
2592 struct ethtool_stats *stats, u64 *data)
2594 struct mac80211_hwsim_data *ar = hw->priv;
2597 data[i++] = ar->tx_pkts;
2598 data[i++] = ar->tx_bytes;
2599 data[i++] = ar->rx_pkts;
2600 data[i++] = ar->rx_bytes;
2601 data[i++] = ar->tx_dropped;
2602 data[i++] = ar->tx_failed;
2604 data[i++] = ar->group;
2606 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2609 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2614 #define HWSIM_COMMON_OPS \
2615 .tx = mac80211_hwsim_tx, \
2616 .start = mac80211_hwsim_start, \
2617 .stop = mac80211_hwsim_stop, \
2618 .add_interface = mac80211_hwsim_add_interface, \
2619 .change_interface = mac80211_hwsim_change_interface, \
2620 .remove_interface = mac80211_hwsim_remove_interface, \
2621 .config = mac80211_hwsim_config, \
2622 .configure_filter = mac80211_hwsim_configure_filter, \
2623 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2624 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2625 .sta_add = mac80211_hwsim_sta_add, \
2626 .sta_remove = mac80211_hwsim_sta_remove, \
2627 .sta_notify = mac80211_hwsim_sta_notify, \
2628 .set_tim = mac80211_hwsim_set_tim, \
2629 .conf_tx = mac80211_hwsim_conf_tx, \
2630 .get_survey = mac80211_hwsim_get_survey, \
2631 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2632 .ampdu_action = mac80211_hwsim_ampdu_action, \
2633 .flush = mac80211_hwsim_flush, \
2634 .get_tsf = mac80211_hwsim_get_tsf, \
2635 .set_tsf = mac80211_hwsim_set_tsf, \
2636 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2637 .get_et_stats = mac80211_hwsim_get_et_stats, \
2638 .get_et_strings = mac80211_hwsim_get_et_strings,
2640 static const struct ieee80211_ops mac80211_hwsim_ops = {
2642 .sw_scan_start = mac80211_hwsim_sw_scan,
2643 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2646 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2648 .hw_scan = mac80211_hwsim_hw_scan,
2649 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2650 .sw_scan_start = NULL,
2651 .sw_scan_complete = NULL,
2652 .remain_on_channel = mac80211_hwsim_roc,
2653 .cancel_remain_on_channel = mac80211_hwsim_croc,
2654 .add_chanctx = mac80211_hwsim_add_chanctx,
2655 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2656 .change_chanctx = mac80211_hwsim_change_chanctx,
2657 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2658 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2661 struct hwsim_new_radio_params {
2662 unsigned int channels;
2663 const char *reg_alpha2;
2664 const struct ieee80211_regdomain *regd;
2668 bool destroy_on_close;
2671 const u8 *perm_addr;
2677 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2678 struct genl_info *info)
2681 genl_notify(&hwsim_genl_family, mcast_skb, info,
2682 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2684 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2685 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2688 static int append_radio_msg(struct sk_buff *skb, int id,
2689 struct hwsim_new_radio_params *param)
2693 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2697 if (param->channels) {
2698 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2703 if (param->reg_alpha2) {
2704 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2713 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2714 if (hwsim_world_regdom_custom[i] != param->regd)
2717 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2724 if (param->reg_strict) {
2725 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2730 if (param->p2p_device) {
2731 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2736 if (param->use_chanctx) {
2737 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2742 if (param->hwname) {
2743 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2744 strlen(param->hwname), param->hwname);
2752 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2753 struct hwsim_new_radio_params *param)
2755 struct sk_buff *mcast_skb;
2758 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2762 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2763 HWSIM_CMD_NEW_RADIO);
2767 if (append_radio_msg(mcast_skb, id, param) < 0)
2770 genlmsg_end(mcast_skb, data);
2772 hwsim_mcast_config_msg(mcast_skb, info);
2776 nlmsg_free(mcast_skb);
2779 static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2781 /* TODO: should we support other types, e.g., P2P?*/
2782 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2783 BIT(NL80211_IFTYPE_AP),
2788 IEEE80211_HE_MAC_CAP0_HTC_HE,
2790 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2791 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2793 IEEE80211_HE_MAC_CAP2_BSR |
2794 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2795 IEEE80211_HE_MAC_CAP2_ACK_EN,
2797 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2798 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2799 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2801 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2802 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2803 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2804 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2806 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2807 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2808 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2809 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2810 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2812 /* Leave all the other PHY capability bytes
2813 * unset, as DCM, beam forming, RU and PPE
2814 * threshold information are not supported
2817 .he_mcs_nss_supp = {
2818 .rx_mcs_80 = cpu_to_le16(0xfffa),
2819 .tx_mcs_80 = cpu_to_le16(0xfffa),
2820 .rx_mcs_160 = cpu_to_le16(0xffff),
2821 .tx_mcs_160 = cpu_to_le16(0xffff),
2822 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2823 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2827 #ifdef CONFIG_MAC80211_MESH
2829 /* TODO: should we support other types, e.g., IBSS?*/
2830 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2835 IEEE80211_HE_MAC_CAP0_HTC_HE,
2837 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2839 IEEE80211_HE_MAC_CAP2_ACK_EN,
2841 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2842 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2843 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2845 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2846 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2847 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2848 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2849 .phy_cap_info[2] = 0,
2851 /* Leave all the other PHY capability bytes
2852 * unset, as DCM, beam forming, RU and PPE
2853 * threshold information are not supported
2856 .he_mcs_nss_supp = {
2857 .rx_mcs_80 = cpu_to_le16(0xfffa),
2858 .tx_mcs_80 = cpu_to_le16(0xfffa),
2859 .rx_mcs_160 = cpu_to_le16(0xffff),
2860 .tx_mcs_160 = cpu_to_le16(0xffff),
2861 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2862 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2869 static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2871 /* TODO: should we support other types, e.g., P2P?*/
2872 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2873 BIT(NL80211_IFTYPE_AP),
2878 IEEE80211_HE_MAC_CAP0_HTC_HE,
2880 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2881 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2883 IEEE80211_HE_MAC_CAP2_BSR |
2884 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2885 IEEE80211_HE_MAC_CAP2_ACK_EN,
2887 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2888 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2889 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2891 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2892 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2893 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2895 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2896 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2897 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2898 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2900 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2901 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2902 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2903 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2904 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2906 /* Leave all the other PHY capability bytes
2907 * unset, as DCM, beam forming, RU and PPE
2908 * threshold information are not supported
2911 .he_mcs_nss_supp = {
2912 .rx_mcs_80 = cpu_to_le16(0xfffa),
2913 .tx_mcs_80 = cpu_to_le16(0xfffa),
2914 .rx_mcs_160 = cpu_to_le16(0xfffa),
2915 .tx_mcs_160 = cpu_to_le16(0xfffa),
2916 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2917 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2921 #ifdef CONFIG_MAC80211_MESH
2923 /* TODO: should we support other types, e.g., IBSS?*/
2924 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2929 IEEE80211_HE_MAC_CAP0_HTC_HE,
2931 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2933 IEEE80211_HE_MAC_CAP2_ACK_EN,
2935 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2936 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2937 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2939 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2940 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2941 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2943 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2944 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2945 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2946 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2947 .phy_cap_info[2] = 0,
2949 /* Leave all the other PHY capability bytes
2950 * unset, as DCM, beam forming, RU and PPE
2951 * threshold information are not supported
2954 .he_mcs_nss_supp = {
2955 .rx_mcs_80 = cpu_to_le16(0xfffa),
2956 .tx_mcs_80 = cpu_to_le16(0xfffa),
2957 .rx_mcs_160 = cpu_to_le16(0xfffa),
2958 .tx_mcs_160 = cpu_to_le16(0xfffa),
2959 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2960 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2967 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2971 if (sband->band == NL80211_BAND_2GHZ) {
2972 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2973 sband->iftype_data =
2974 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2975 } else if (sband->band == NL80211_BAND_5GHZ) {
2976 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2977 sband->iftype_data =
2978 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2983 sband->n_iftype_data = n_iftype_data;
2986 #ifdef CONFIG_MAC80211_MESH
2987 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2989 #define HWSIM_MESH_BIT 0
2992 #define HWSIM_DEFAULT_IF_LIMIT \
2993 (BIT(NL80211_IFTYPE_STATION) | \
2994 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2995 BIT(NL80211_IFTYPE_AP) | \
2996 BIT(NL80211_IFTYPE_P2P_GO) | \
2999 #define HWSIM_IFTYPE_SUPPORT_MASK \
3000 (BIT(NL80211_IFTYPE_STATION) | \
3001 BIT(NL80211_IFTYPE_AP) | \
3002 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3003 BIT(NL80211_IFTYPE_P2P_GO) | \
3004 BIT(NL80211_IFTYPE_ADHOC) | \
3005 BIT(NL80211_IFTYPE_MESH_POINT) | \
3006 BIT(NL80211_IFTYPE_OCB))
3008 static int mac80211_hwsim_new_radio(struct genl_info *info,
3009 struct hwsim_new_radio_params *param)
3013 struct mac80211_hwsim_data *data;
3014 struct ieee80211_hw *hw;
3015 enum nl80211_band band;
3016 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
3021 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
3024 spin_lock_bh(&hwsim_radio_lock);
3025 idx = hwsim_radio_idx++;
3026 spin_unlock_bh(&hwsim_radio_lock);
3028 if (param->use_chanctx)
3029 ops = &mac80211_hwsim_mchan_ops;
3030 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
3032 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3037 /* ieee80211_alloc_hw_nm may have used a default name */
3038 param->hwname = wiphy_name(hw->wiphy);
3041 net = genl_info_net(info);
3044 wiphy_net_set(hw->wiphy, net);
3049 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3050 if (IS_ERR(data->dev)) {
3052 "mac80211_hwsim: device_create failed (%ld)\n",
3053 PTR_ERR(data->dev));
3055 goto failed_drvdata;
3057 data->dev->driver = &mac80211_hwsim_driver.driver;
3058 err = device_bind_driver(data->dev);
3060 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3065 skb_queue_head_init(&data->pending);
3067 SET_IEEE80211_DEV(hw, data->dev);
3068 if (!param->perm_addr) {
3069 eth_zero_addr(addr);
3073 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3074 /* Why need here second address ? */
3075 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3076 data->addresses[1].addr[0] |= 0x40;
3077 hw->wiphy->n_addresses = 2;
3078 hw->wiphy->addresses = data->addresses;
3079 /* possible address clash is checked at hash table insertion */
3081 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3082 /* compatibility with automatically generated mac addr */
3083 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3084 hw->wiphy->n_addresses = 2;
3085 hw->wiphy->addresses = data->addresses;
3088 data->channels = param->channels;
3089 data->use_chanctx = param->use_chanctx;
3091 data->destroy_on_close = param->destroy_on_close;
3093 data->portid = info->snd_portid;
3095 /* setup interface limits, only on interface types we support */
3096 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3097 data->if_limits[n_limits].max = 1;
3098 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3102 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3103 data->if_limits[n_limits].max = 2048;
3105 * For this case, we may only support a subset of
3106 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3107 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3109 data->if_limits[n_limits].types =
3110 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3114 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3115 data->if_limits[n_limits].max = 1;
3116 data->if_limits[n_limits].types =
3117 BIT(NL80211_IFTYPE_P2P_DEVICE);
3121 if (data->use_chanctx) {
3122 hw->wiphy->max_scan_ssids = 255;
3123 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3124 hw->wiphy->max_remain_on_channel_duration = 1000;
3125 data->if_combination.radar_detect_widths = 0;
3126 data->if_combination.num_different_channels = data->channels;
3128 data->if_combination.num_different_channels = 1;
3129 data->if_combination.radar_detect_widths =
3130 BIT(NL80211_CHAN_WIDTH_5) |
3131 BIT(NL80211_CHAN_WIDTH_10) |
3132 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3133 BIT(NL80211_CHAN_WIDTH_20) |
3134 BIT(NL80211_CHAN_WIDTH_40) |
3135 BIT(NL80211_CHAN_WIDTH_80) |
3136 BIT(NL80211_CHAN_WIDTH_160);
3144 data->if_combination.max_interfaces = 0;
3145 for (i = 0; i < n_limits; i++)
3146 data->if_combination.max_interfaces +=
3147 data->if_limits[i].max;
3149 data->if_combination.n_limits = n_limits;
3150 data->if_combination.limits = data->if_limits;
3153 * If we actually were asked to support combinations,
3154 * advertise them - if there's only a single thing like
3155 * only IBSS then don't advertise it as combinations.
3157 if (data->if_combination.max_interfaces > 1) {
3158 hw->wiphy->iface_combinations = &data->if_combination;
3159 hw->wiphy->n_iface_combinations = 1;
3162 if (param->ciphers) {
3163 memcpy(data->ciphers, param->ciphers,
3164 param->n_ciphers * sizeof(u32));
3165 hw->wiphy->cipher_suites = data->ciphers;
3166 hw->wiphy->n_cipher_suites = param->n_ciphers;
3169 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3170 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3171 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3174 hw->offchannel_tx_hw_queue = 4;
3176 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3177 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3178 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3179 ieee80211_hw_set(hw, QUEUE_CONTROL);
3180 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3181 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3182 ieee80211_hw_set(hw, MFP_CAPABLE);
3183 ieee80211_hw_set(hw, SIGNAL_DBM);
3184 ieee80211_hw_set(hw, SUPPORTS_PS);
3185 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3186 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3187 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3188 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3190 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3191 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3193 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3194 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3195 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3196 WIPHY_FLAG_AP_UAPSD |
3197 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3198 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3199 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3200 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3201 NL80211_FEATURE_STATIC_SMPS |
3202 NL80211_FEATURE_DYNAMIC_SMPS |
3203 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3204 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3205 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3206 wiphy_ext_feature_set(hw->wiphy,
3207 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3208 wiphy_ext_feature_set(hw->wiphy,
3209 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3211 hw->wiphy->interface_modes = param->iftypes;
3213 /* ask mac80211 to reserve space for magic */
3214 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3215 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3216 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3218 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3219 sizeof(hwsim_channels_2ghz));
3220 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3221 sizeof(hwsim_channels_5ghz));
3222 memcpy(data->channels_6ghz, hwsim_channels_6ghz,
3223 sizeof(hwsim_channels_6ghz));
3224 memcpy(data->channels_s1g, hwsim_channels_s1g,
3225 sizeof(hwsim_channels_s1g));
3226 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3228 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3229 struct ieee80211_supported_band *sband = &data->bands[band];
3234 case NL80211_BAND_2GHZ:
3235 sband->channels = data->channels_2ghz;
3236 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3237 sband->bitrates = data->rates;
3238 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3240 case NL80211_BAND_5GHZ:
3241 sband->channels = data->channels_5ghz;
3242 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3243 sband->bitrates = data->rates + 4;
3244 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3246 sband->vht_cap.vht_supported = true;
3247 sband->vht_cap.cap =
3248 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3249 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3250 IEEE80211_VHT_CAP_RXLDPC |
3251 IEEE80211_VHT_CAP_SHORT_GI_80 |
3252 IEEE80211_VHT_CAP_SHORT_GI_160 |
3253 IEEE80211_VHT_CAP_TXSTBC |
3254 IEEE80211_VHT_CAP_RXSTBC_4 |
3255 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3256 sband->vht_cap.vht_mcs.rx_mcs_map =
3257 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3258 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3259 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3260 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3261 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3262 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3263 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3264 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3265 sband->vht_cap.vht_mcs.tx_mcs_map =
3266 sband->vht_cap.vht_mcs.rx_mcs_map;
3268 case NL80211_BAND_S1GHZ:
3269 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3270 sizeof(sband->s1g_cap));
3271 sband->channels = data->channels_s1g;
3272 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3278 sband->ht_cap.ht_supported = true;
3279 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3280 IEEE80211_HT_CAP_GRN_FLD |
3281 IEEE80211_HT_CAP_SGI_20 |
3282 IEEE80211_HT_CAP_SGI_40 |
3283 IEEE80211_HT_CAP_DSSSCCK40;
3284 sband->ht_cap.ampdu_factor = 0x3;
3285 sband->ht_cap.ampdu_density = 0x6;
3286 memset(&sband->ht_cap.mcs, 0,
3287 sizeof(sband->ht_cap.mcs));
3288 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3289 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3290 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3292 mac80211_hwsim_he_capab(sband);
3294 hw->wiphy->bands[band] = sband;
3297 /* By default all radios belong to the first group */
3299 mutex_init(&data->mutex);
3301 data->netgroup = hwsim_net_get_netgroup(net);
3302 data->wmediumd = hwsim_net_get_wmediumd(net);
3304 /* Enable frame retransmissions for lossy channels */
3306 hw->max_rate_tries = 11;
3308 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3309 hw->wiphy->n_vendor_commands =
3310 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3311 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3312 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3314 if (param->reg_strict)
3315 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3317 data->regd = param->regd;
3318 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3319 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3320 /* give the regulatory workqueue a chance to run */
3321 schedule_timeout_interruptible(1);
3325 ieee80211_hw_set(hw, NO_AUTO_VIF);
3327 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3329 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3330 HRTIMER_MODE_ABS_SOFT);
3331 data->beacon_timer.function = mac80211_hwsim_beacon;
3333 err = ieee80211_register_hw(hw);
3335 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3340 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3342 if (param->reg_alpha2) {
3343 data->alpha2[0] = param->reg_alpha2[0];
3344 data->alpha2[1] = param->reg_alpha2[1];
3345 regulatory_hint(hw->wiphy, param->reg_alpha2);
3348 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3349 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3350 debugfs_create_file("group", 0666, data->debugfs, data,
3352 if (!data->use_chanctx)
3353 debugfs_create_file("dfs_simulate_radar", 0222,
3355 data, &hwsim_simulate_radar);
3357 spin_lock_bh(&hwsim_radio_lock);
3358 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3362 GENL_SET_ERR_MSG(info, "perm addr already present");
3363 NL_SET_BAD_ATTR(info->extack,
3364 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3366 spin_unlock_bh(&hwsim_radio_lock);
3367 goto failed_final_insert;
3370 list_add_tail(&data->list, &hwsim_radios);
3371 hwsim_radios_generation++;
3372 spin_unlock_bh(&hwsim_radio_lock);
3374 hwsim_mcast_new_radio(idx, info, param);
3378 failed_final_insert:
3379 debugfs_remove_recursive(data->debugfs);
3380 ieee80211_unregister_hw(data->hw);
3382 device_release_driver(data->dev);
3384 device_unregister(data->dev);
3386 ieee80211_free_hw(hw);
3391 static void hwsim_mcast_del_radio(int id, const char *hwname,
3392 struct genl_info *info)
3394 struct sk_buff *skb;
3398 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3402 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3403 HWSIM_CMD_DEL_RADIO);
3407 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3411 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3416 genlmsg_end(skb, data);
3418 hwsim_mcast_config_msg(skb, info);
3426 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3428 struct genl_info *info)
3430 hwsim_mcast_del_radio(data->idx, hwname, info);
3431 debugfs_remove_recursive(data->debugfs);
3432 ieee80211_unregister_hw(data->hw);
3433 device_release_driver(data->dev);
3434 device_unregister(data->dev);
3435 ieee80211_free_hw(data->hw);
3438 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3439 struct mac80211_hwsim_data *data,
3440 u32 portid, u32 seq,
3441 struct netlink_callback *cb, int flags)
3444 struct hwsim_new_radio_params param = { };
3445 int res = -EMSGSIZE;
3447 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3448 HWSIM_CMD_GET_RADIO);
3453 genl_dump_check_consistent(cb, hdr);
3455 if (data->alpha2[0] && data->alpha2[1])
3456 param.reg_alpha2 = data->alpha2;
3458 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3459 REGULATORY_STRICT_REG);
3460 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3461 BIT(NL80211_IFTYPE_P2P_DEVICE));
3462 param.use_chanctx = data->use_chanctx;
3463 param.regd = data->regd;
3464 param.channels = data->channels;
3465 param.hwname = wiphy_name(data->hw->wiphy);
3467 res = append_radio_msg(skb, data->idx, ¶m);
3471 genlmsg_end(skb, hdr);
3475 genlmsg_cancel(skb, hdr);
3479 static void mac80211_hwsim_free(void)
3481 struct mac80211_hwsim_data *data;
3483 spin_lock_bh(&hwsim_radio_lock);
3484 while ((data = list_first_entry_or_null(&hwsim_radios,
3485 struct mac80211_hwsim_data,
3487 list_del(&data->list);
3488 spin_unlock_bh(&hwsim_radio_lock);
3489 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3491 spin_lock_bh(&hwsim_radio_lock);
3493 spin_unlock_bh(&hwsim_radio_lock);
3494 class_destroy(hwsim_class);
3497 static const struct net_device_ops hwsim_netdev_ops = {
3498 .ndo_start_xmit = hwsim_mon_xmit,
3499 .ndo_set_mac_address = eth_mac_addr,
3500 .ndo_validate_addr = eth_validate_addr,
3503 static void hwsim_mon_setup(struct net_device *dev)
3505 dev->netdev_ops = &hwsim_netdev_ops;
3506 dev->needs_free_netdev = true;
3508 dev->priv_flags |= IFF_NO_QUEUE;
3509 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3510 eth_zero_addr(dev->dev_addr);
3511 dev->dev_addr[0] = 0x12;
3514 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3516 return rhashtable_lookup_fast(&hwsim_radios_rht,
3521 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3523 struct mac80211_hwsim_data *data;
3525 hwsim_net_set_wmediumd(net, portid);
3527 spin_lock_bh(&hwsim_radio_lock);
3528 list_for_each_entry(data, &hwsim_radios, list) {
3529 if (data->netgroup == hwsim_net_get_netgroup(net))
3530 data->wmediumd = portid;
3532 spin_unlock_bh(&hwsim_radio_lock);
3535 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3536 struct genl_info *info)
3539 struct ieee80211_hdr *hdr;
3540 struct mac80211_hwsim_data *data2;
3541 struct ieee80211_tx_info *txi;
3542 struct hwsim_tx_rate *tx_attempts;
3544 struct sk_buff *skb, *tmp;
3546 unsigned int hwsim_flags;
3550 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3551 !info->attrs[HWSIM_ATTR_FLAGS] ||
3552 !info->attrs[HWSIM_ATTR_COOKIE] ||
3553 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3554 !info->attrs[HWSIM_ATTR_TX_INFO])
3557 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3558 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3559 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3561 data2 = get_hwsim_data_ref_from_addr(src);
3565 if (!hwsim_virtio_enabled) {
3566 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3570 if (info->snd_portid != data2->wmediumd)
3574 /* look for the skb matching the cookie passed back from user */
3575 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3578 txi = IEEE80211_SKB_CB(skb);
3579 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3581 if (skb_cookie == ret_skb_cookie) {
3582 skb_unlink(skb, &data2->pending);
3592 /* Tx info received because the frame was broadcasted on user space,
3593 so we get all the necessary info: tx attempts and skb control buff */
3595 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3596 info->attrs[HWSIM_ATTR_TX_INFO]);
3598 /* now send back TX status */
3599 txi = IEEE80211_SKB_CB(skb);
3601 ieee80211_tx_info_clear_status(txi);
3603 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3604 txi->status.rates[i].idx = tx_attempts[i].idx;
3605 txi->status.rates[i].count = tx_attempts[i].count;
3608 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3610 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3611 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3612 if (skb->len >= 16) {
3613 hdr = (struct ieee80211_hdr *) skb->data;
3614 mac80211_hwsim_monitor_ack(data2->channel,
3617 txi->flags |= IEEE80211_TX_STAT_ACK;
3619 ieee80211_tx_status_irqsafe(data2->hw, skb);
3626 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3627 struct genl_info *info)
3629 struct mac80211_hwsim_data *data2;
3630 struct ieee80211_rx_status rx_status;
3631 struct ieee80211_hdr *hdr;
3635 struct sk_buff *skb = NULL;
3637 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3638 !info->attrs[HWSIM_ATTR_FRAME] ||
3639 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3640 !info->attrs[HWSIM_ATTR_SIGNAL])
3643 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3644 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3645 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3647 /* Allocate new skb here */
3648 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3652 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3656 skb_put_data(skb, frame_data, frame_data_len);
3658 data2 = get_hwsim_data_ref_from_addr(dst);
3662 if (!hwsim_virtio_enabled) {
3663 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3667 if (info->snd_portid != data2->wmediumd)
3671 /* check if radio is configured properly */
3673 if (data2->idle || !data2->started)
3676 /* A frame is received from user space */
3677 memset(&rx_status, 0, sizeof(rx_status));
3678 if (info->attrs[HWSIM_ATTR_FREQ]) {
3679 /* throw away off-channel packets, but allow both the temporary
3680 * ("hw" scan/remain-on-channel) and regular channel, since the
3681 * internal datapath also allows this
3683 mutex_lock(&data2->mutex);
3684 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3686 if (rx_status.freq != data2->channel->center_freq &&
3687 (!data2->tmp_chan ||
3688 rx_status.freq != data2->tmp_chan->center_freq)) {
3689 mutex_unlock(&data2->mutex);
3692 mutex_unlock(&data2->mutex);
3694 rx_status.freq = data2->channel->center_freq;
3697 rx_status.band = data2->channel->band;
3698 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3699 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3701 hdr = (void *)skb->data;
3703 if (ieee80211_is_beacon(hdr->frame_control) ||
3704 ieee80211_is_probe_resp(hdr->frame_control))
3705 rx_status.boottime_ns = ktime_get_boottime_ns();
3707 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3709 data2->rx_bytes += skb->len;
3710 ieee80211_rx_irqsafe(data2->hw, skb);
3714 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3720 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3721 struct genl_info *info)
3723 struct net *net = genl_info_net(info);
3724 struct mac80211_hwsim_data *data;
3727 spin_lock_bh(&hwsim_radio_lock);
3728 list_for_each_entry(data, &hwsim_radios, list)
3729 chans = max(chans, data->channels);
3730 spin_unlock_bh(&hwsim_radio_lock);
3732 /* In the future we should revise the userspace API and allow it
3733 * to set a flag that it does support multi-channel, then we can
3734 * let this pass conditionally on the flag.
3735 * For current userspace, prohibit it since it won't work right.
3740 if (hwsim_net_get_wmediumd(net))
3743 hwsim_register_wmediumd(net, info->snd_portid);
3745 pr_debug("mac80211_hwsim: received a REGISTER, "
3746 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3751 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3752 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3756 for (i = 0; i < n_ciphers; i++) {
3760 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3761 if (ciphers[i] == hwsim_ciphers[j]) {
3774 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3776 struct hwsim_new_radio_params param = { 0 };
3777 const char *hwname = NULL;
3780 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3781 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3782 param.channels = channels;
3783 param.destroy_on_close =
3784 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3786 if (info->attrs[HWSIM_ATTR_CHANNELS])
3787 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3789 if (param.channels < 1) {
3790 GENL_SET_ERR_MSG(info, "must have at least one channel");
3794 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3795 GENL_SET_ERR_MSG(info, "too many channels specified");
3799 if (info->attrs[HWSIM_ATTR_NO_VIF])
3800 param.no_vif = true;
3802 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3803 param.use_chanctx = true;
3805 param.use_chanctx = (param.channels > 1);
3807 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3809 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3811 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3812 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3814 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3817 idx = array_index_nospec(idx,
3818 ARRAY_SIZE(hwsim_world_regdom_custom));
3819 param.regd = hwsim_world_regdom_custom[idx];
3822 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3823 if (!is_valid_ether_addr(
3824 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3825 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3826 NL_SET_BAD_ATTR(info->extack,
3827 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3831 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3834 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3836 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3838 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3839 NL_SET_ERR_MSG_ATTR(info->extack,
3840 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3841 "cannot support more iftypes than kernel");
3845 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3848 /* ensure both flag and iftype support is honored */
3849 if (param.p2p_device ||
3850 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3851 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3852 param.p2p_device = true;
3855 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3856 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3859 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3861 if (len % sizeof(u32)) {
3862 NL_SET_ERR_MSG_ATTR(info->extack,
3863 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3864 "bad cipher list length");
3868 param.n_ciphers = len / sizeof(u32);
3870 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3871 NL_SET_ERR_MSG_ATTR(info->extack,
3872 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3873 "too many ciphers specified");
3877 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3878 NL_SET_ERR_MSG_ATTR(info->extack,
3879 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3880 "unsupported ciphers specified");
3885 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3886 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3887 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3891 param.hwname = hwname;
3894 ret = mac80211_hwsim_new_radio(info, ¶m);
3899 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3901 struct mac80211_hwsim_data *data;
3903 const char *hwname = NULL;
3905 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3906 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3907 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3908 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3909 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3916 spin_lock_bh(&hwsim_radio_lock);
3917 list_for_each_entry(data, &hwsim_radios, list) {
3919 if (data->idx != idx)
3923 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3927 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3930 list_del(&data->list);
3931 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3933 hwsim_radios_generation++;
3934 spin_unlock_bh(&hwsim_radio_lock);
3935 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3940 spin_unlock_bh(&hwsim_radio_lock);
3946 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3948 struct mac80211_hwsim_data *data;
3949 struct sk_buff *skb;
3950 int idx, res = -ENODEV;
3952 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3954 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3956 spin_lock_bh(&hwsim_radio_lock);
3957 list_for_each_entry(data, &hwsim_radios, list) {
3958 if (data->idx != idx)
3961 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3964 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3970 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3971 info->snd_seq, NULL, 0);
3977 res = genlmsg_reply(skb, info);
3982 spin_unlock_bh(&hwsim_radio_lock);
3987 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3988 struct netlink_callback *cb)
3990 int last_idx = cb->args[0] - 1;
3991 struct mac80211_hwsim_data *data = NULL;
3995 spin_lock_bh(&hwsim_radio_lock);
3996 cb->seq = hwsim_radios_generation;
3998 if (last_idx >= hwsim_radio_idx-1)
4001 list_for_each_entry(data, &hwsim_radios, list) {
4002 if (data->idx <= last_idx)
4005 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
4008 res = mac80211_hwsim_get_radio(skb, data,
4009 NETLINK_CB(cb->skb).portid,
4010 cb->nlh->nlmsg_seq, cb,
4015 last_idx = data->idx;
4018 cb->args[0] = last_idx + 1;
4020 /* list changed, but no new element sent, set interrupted flag */
4021 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4022 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4023 cb->nlh->nlmsg_seq, &hwsim_genl_family,
4024 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4026 genl_dump_check_consistent(cb, hdr);
4027 genlmsg_end(skb, hdr);
4034 spin_unlock_bh(&hwsim_radio_lock);
4035 return res ?: skb->len;
4038 /* Generic Netlink operations array */
4039 static const struct genl_small_ops hwsim_ops[] = {
4041 .cmd = HWSIM_CMD_REGISTER,
4042 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4043 .doit = hwsim_register_received_nl,
4044 .flags = GENL_UNS_ADMIN_PERM,
4047 .cmd = HWSIM_CMD_FRAME,
4048 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4049 .doit = hwsim_cloned_frame_received_nl,
4052 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4053 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4054 .doit = hwsim_tx_info_frame_received_nl,
4057 .cmd = HWSIM_CMD_NEW_RADIO,
4058 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4059 .doit = hwsim_new_radio_nl,
4060 .flags = GENL_UNS_ADMIN_PERM,
4063 .cmd = HWSIM_CMD_DEL_RADIO,
4064 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4065 .doit = hwsim_del_radio_nl,
4066 .flags = GENL_UNS_ADMIN_PERM,
4069 .cmd = HWSIM_CMD_GET_RADIO,
4070 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4071 .doit = hwsim_get_radio_nl,
4072 .dumpit = hwsim_dump_radio_nl,
4076 static struct genl_family hwsim_genl_family __ro_after_init = {
4077 .name = "MAC80211_HWSIM",
4079 .maxattr = HWSIM_ATTR_MAX,
4080 .policy = hwsim_genl_policy,
4082 .module = THIS_MODULE,
4083 .small_ops = hwsim_ops,
4084 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4085 .mcgrps = hwsim_mcgrps,
4086 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4089 static void remove_user_radios(u32 portid)
4091 struct mac80211_hwsim_data *entry, *tmp;
4094 spin_lock_bh(&hwsim_radio_lock);
4095 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4096 if (entry->destroy_on_close && entry->portid == portid) {
4097 list_move(&entry->list, &list);
4098 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4100 hwsim_radios_generation++;
4103 spin_unlock_bh(&hwsim_radio_lock);
4105 list_for_each_entry_safe(entry, tmp, &list, list) {
4106 list_del(&entry->list);
4107 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4112 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4113 unsigned long state,
4116 struct netlink_notify *notify = _notify;
4118 if (state != NETLINK_URELEASE)
4121 remove_user_radios(notify->portid);
4123 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4124 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4125 " socket, switching to perfect channel medium\n");
4126 hwsim_register_wmediumd(notify->net, 0);
4132 static struct notifier_block hwsim_netlink_notifier = {
4133 .notifier_call = mac80211_hwsim_netlink_notify,
4136 static int __init hwsim_init_netlink(void)
4140 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4142 rc = genl_register_family(&hwsim_genl_family);
4146 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4148 genl_unregister_family(&hwsim_genl_family);
4155 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4159 static __net_init int hwsim_init_net(struct net *net)
4161 return hwsim_net_set_netgroup(net);
4164 static void __net_exit hwsim_exit_net(struct net *net)
4166 struct mac80211_hwsim_data *data, *tmp;
4169 spin_lock_bh(&hwsim_radio_lock);
4170 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4171 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4174 /* Radios created in init_net are returned to init_net. */
4175 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4178 list_move(&data->list, &list);
4179 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4181 hwsim_radios_generation++;
4183 spin_unlock_bh(&hwsim_radio_lock);
4185 list_for_each_entry_safe(data, tmp, &list, list) {
4186 list_del(&data->list);
4187 mac80211_hwsim_del_radio(data,
4188 wiphy_name(data->hw->wiphy),
4192 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4195 static struct pernet_operations hwsim_net_ops = {
4196 .init = hwsim_init_net,
4197 .exit = hwsim_exit_net,
4198 .id = &hwsim_net_id,
4199 .size = sizeof(struct hwsim_net),
4202 static void hwsim_exit_netlink(void)
4204 /* unregister the notifier */
4205 netlink_unregister_notifier(&hwsim_netlink_notifier);
4206 /* unregister the family */
4207 genl_unregister_family(&hwsim_genl_family);
4210 #if IS_REACHABLE(CONFIG_VIRTIO)
4211 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4214 struct sk_buff *skb;
4215 unsigned long flags;
4217 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4218 while ((skb = virtqueue_get_buf(vq, &len)))
4220 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4223 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4225 struct nlmsghdr *nlh;
4226 struct genlmsghdr *gnlh;
4227 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4228 struct genl_info info = {};
4231 nlh = nlmsg_hdr(skb);
4232 gnlh = nlmsg_data(nlh);
4233 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4234 hwsim_genl_policy, NULL);
4236 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4242 switch (gnlh->cmd) {
4243 case HWSIM_CMD_FRAME:
4244 hwsim_cloned_frame_received_nl(skb, &info);
4246 case HWSIM_CMD_TX_INFO_FRAME:
4247 hwsim_tx_info_frame_received_nl(skb, &info);
4250 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4256 static void hwsim_virtio_rx_work(struct work_struct *work)
4258 struct virtqueue *vq;
4260 struct sk_buff *skb;
4261 struct scatterlist sg[1];
4263 unsigned long flags;
4265 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4266 if (!hwsim_virtio_enabled)
4269 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4272 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4274 skb->data = skb->head;
4275 skb_set_tail_pointer(skb, len);
4276 hwsim_virtio_handle_cmd(skb);
4278 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4279 if (!hwsim_virtio_enabled) {
4283 vq = hwsim_vqs[HWSIM_VQ_RX];
4284 sg_init_one(sg, skb->head, skb_end_offset(skb));
4285 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4286 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4290 schedule_work(&hwsim_virtio_rx);
4293 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4296 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4298 schedule_work(&hwsim_virtio_rx);
4301 static int init_vqs(struct virtio_device *vdev)
4303 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4304 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4305 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4307 const char *names[HWSIM_NUM_VQS] = {
4308 [HWSIM_VQ_TX] = "tx",
4309 [HWSIM_VQ_RX] = "rx",
4312 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4313 hwsim_vqs, callbacks, names, NULL);
4316 static int fill_vq(struct virtqueue *vq)
4319 struct sk_buff *skb;
4320 struct scatterlist sg[1];
4322 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4323 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4327 sg_init_one(sg, skb->head, skb_end_offset(skb));
4328 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4338 static void remove_vqs(struct virtio_device *vdev)
4342 vdev->config->reset(vdev);
4344 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4345 struct virtqueue *vq = hwsim_vqs[i];
4346 struct sk_buff *skb;
4348 while ((skb = virtqueue_detach_unused_buf(vq)))
4352 vdev->config->del_vqs(vdev);
4355 static int hwsim_virtio_probe(struct virtio_device *vdev)
4358 unsigned long flags;
4360 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4361 if (hwsim_virtio_enabled) {
4362 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4365 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4367 err = init_vqs(vdev);
4371 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4375 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4376 hwsim_virtio_enabled = true;
4377 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4379 schedule_work(&hwsim_virtio_rx);
4387 static void hwsim_virtio_remove(struct virtio_device *vdev)
4389 hwsim_virtio_enabled = false;
4391 cancel_work_sync(&hwsim_virtio_rx);
4396 /* MAC80211_HWSIM virtio device id table */
4397 static const struct virtio_device_id id_table[] = {
4398 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4401 MODULE_DEVICE_TABLE(virtio, id_table);
4403 static struct virtio_driver virtio_hwsim = {
4404 .driver.name = KBUILD_MODNAME,
4405 .driver.owner = THIS_MODULE,
4406 .id_table = id_table,
4407 .probe = hwsim_virtio_probe,
4408 .remove = hwsim_virtio_remove,
4411 static int hwsim_register_virtio_driver(void)
4413 return register_virtio_driver(&virtio_hwsim);
4416 static void hwsim_unregister_virtio_driver(void)
4418 unregister_virtio_driver(&virtio_hwsim);
4421 static inline int hwsim_register_virtio_driver(void)
4426 static inline void hwsim_unregister_virtio_driver(void)
4431 static int __init init_mac80211_hwsim(void)
4435 if (radios < 0 || radios > 100)
4441 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4445 err = register_pernet_device(&hwsim_net_ops);
4449 err = platform_driver_register(&mac80211_hwsim_driver);
4451 goto out_unregister_pernet;
4453 err = hwsim_init_netlink();
4455 goto out_unregister_driver;
4457 err = hwsim_register_virtio_driver();
4459 goto out_exit_netlink;
4461 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4462 if (IS_ERR(hwsim_class)) {
4463 err = PTR_ERR(hwsim_class);
4464 goto out_exit_virtio;
4467 hwsim_init_s1g_channels(hwsim_channels_s1g);
4469 for (i = 0; i < radios; i++) {
4470 struct hwsim_new_radio_params param = { 0 };
4472 param.channels = channels;
4475 case HWSIM_REGTEST_DIFF_COUNTRY:
4476 if (i < ARRAY_SIZE(hwsim_alpha2s))
4477 param.reg_alpha2 = hwsim_alpha2s[i];
4479 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4481 param.reg_alpha2 = hwsim_alpha2s[0];
4483 case HWSIM_REGTEST_STRICT_ALL:
4484 param.reg_strict = true;
4486 case HWSIM_REGTEST_DRIVER_REG_ALL:
4487 param.reg_alpha2 = hwsim_alpha2s[0];
4489 case HWSIM_REGTEST_WORLD_ROAM:
4491 param.regd = &hwsim_world_regdom_custom_01;
4493 case HWSIM_REGTEST_CUSTOM_WORLD:
4494 param.regd = &hwsim_world_regdom_custom_01;
4496 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4498 param.regd = &hwsim_world_regdom_custom_01;
4500 param.regd = &hwsim_world_regdom_custom_02;
4502 case HWSIM_REGTEST_STRICT_FOLLOW:
4504 param.reg_strict = true;
4505 param.reg_alpha2 = hwsim_alpha2s[0];
4508 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4510 param.reg_strict = true;
4511 param.reg_alpha2 = hwsim_alpha2s[0];
4512 } else if (i == 1) {
4513 param.reg_alpha2 = hwsim_alpha2s[1];
4516 case HWSIM_REGTEST_ALL:
4519 param.regd = &hwsim_world_regdom_custom_01;
4522 param.regd = &hwsim_world_regdom_custom_02;
4525 param.reg_alpha2 = hwsim_alpha2s[0];
4528 param.reg_alpha2 = hwsim_alpha2s[1];
4531 param.reg_strict = true;
4532 param.reg_alpha2 = hwsim_alpha2s[2];
4540 param.p2p_device = support_p2p_device;
4541 param.use_chanctx = channels > 1;
4542 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4543 if (param.p2p_device)
4544 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4546 err = mac80211_hwsim_new_radio(NULL, ¶m);
4548 goto out_free_radios;
4551 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4553 if (hwsim_mon == NULL) {
4555 goto out_free_radios;
4559 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4565 err = register_netdevice(hwsim_mon);
4575 free_netdev(hwsim_mon);
4577 mac80211_hwsim_free();
4579 hwsim_unregister_virtio_driver();
4581 hwsim_exit_netlink();
4582 out_unregister_driver:
4583 platform_driver_unregister(&mac80211_hwsim_driver);
4584 out_unregister_pernet:
4585 unregister_pernet_device(&hwsim_net_ops);
4587 rhashtable_destroy(&hwsim_radios_rht);
4590 module_init(init_mac80211_hwsim);
4592 static void __exit exit_mac80211_hwsim(void)
4594 pr_debug("mac80211_hwsim: unregister radios\n");
4596 hwsim_unregister_virtio_driver();
4597 hwsim_exit_netlink();
4599 mac80211_hwsim_free();
4601 rhashtable_destroy(&hwsim_radios_rht);
4602 unregister_netdev(hwsim_mon);
4603 platform_driver_unregister(&mac80211_hwsim_driver);
4604 unregister_pernet_device(&hwsim_net_ops);
4606 module_exit(exit_mac80211_hwsim);