2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
5 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
14 * - Add TSF sync and fix IBSS beacon transmission by adding
15 * competition for "air time" at TBTT
16 * - RX filtering based on filter configuration (data->rx_filter)
19 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
24 #include <net/mac80211.h>
25 #include <net/ieee80211_radiotap.h>
26 #include <linux/if_arp.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/etherdevice.h>
29 #include <linux/platform_device.h>
30 #include <linux/debugfs.h>
31 #include <linux/module.h>
32 #include <linux/ktime.h>
33 #include <net/genetlink.h>
34 #include <net/net_namespace.h>
35 #include <net/netns/generic.h>
36 #include <linux/rhashtable.h>
37 #include <linux/nospec.h>
38 #include "mac80211_hwsim.h"
40 #define WARN_QUEUE 100
43 MODULE_AUTHOR("Jouni Malinen");
44 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
45 MODULE_LICENSE("GPL");
47 static int radios = 2;
48 module_param(radios, int, 0444);
49 MODULE_PARM_DESC(radios, "Number of simulated radios");
51 static int channels = 1;
52 module_param(channels, int, 0444);
53 MODULE_PARM_DESC(channels, "Number of concurrent channels");
55 static bool paged_rx = false;
56 module_param(paged_rx, bool, 0644);
57 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
59 static bool rctbl = false;
60 module_param(rctbl, bool, 0444);
61 MODULE_PARM_DESC(rctbl, "Handle rate control table");
63 static bool support_p2p_device = true;
64 module_param(support_p2p_device, bool, 0444);
65 MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
68 * enum hwsim_regtest - the type of regulatory tests we offer
70 * These are the different values you can use for the regtest
71 * module parameter. This is useful to help test world roaming
72 * and the driver regulatory_hint() call and combinations of these.
73 * If you want to do specific alpha2 regulatory domain tests simply
74 * use the userspace regulatory request as that will be respected as
75 * well without the need of this module parameter. This is designed
76 * only for testing the driver regulatory request, world roaming
77 * and all possible combinations.
79 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
80 * this is the default value.
81 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
82 * hint, only one driver regulatory hint will be sent as such the
83 * secondary radios are expected to follow.
84 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
85 * request with all radios reporting the same regulatory domain.
86 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
87 * different regulatory domains requests. Expected behaviour is for
88 * an intersection to occur but each device will still use their
89 * respective regulatory requested domains. Subsequent radios will
90 * use the resulting intersection.
91 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
92 * this by using a custom beacon-capable regulatory domain for the first
93 * radio. All other device world roam.
94 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
95 * domain requests. All radios will adhere to this custom world regulatory
97 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
98 * domain requests. The first radio will adhere to the first custom world
99 * regulatory domain, the second one to the second custom world regulatory
100 * domain. All other devices will world roam.
101 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
102 * settings, only the first radio will send a regulatory domain request
103 * and use strict settings. The rest of the radios are expected to follow.
104 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
105 * settings. All radios will adhere to this.
106 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
107 * domain settings, combined with secondary driver regulatory domain
108 * settings. The first radio will get a strict regulatory domain setting
109 * using the first driver regulatory request and the second radio will use
110 * non-strict settings using the second driver regulatory request. All
111 * other devices should follow the intersection created between the
113 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
114 * at least 6 radios for a complete test. We will test in this order:
115 * 1 - driver custom world regulatory domain
116 * 2 - second custom world regulatory domain
117 * 3 - first driver regulatory domain request
118 * 4 - second driver regulatory domain request
119 * 5 - strict regulatory domain settings using the third driver regulatory
121 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
122 * regulatory requests.
125 HWSIM_REGTEST_DISABLED = 0,
126 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
127 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
128 HWSIM_REGTEST_DIFF_COUNTRY = 3,
129 HWSIM_REGTEST_WORLD_ROAM = 4,
130 HWSIM_REGTEST_CUSTOM_WORLD = 5,
131 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
132 HWSIM_REGTEST_STRICT_FOLLOW = 7,
133 HWSIM_REGTEST_STRICT_ALL = 8,
134 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
135 HWSIM_REGTEST_ALL = 10,
138 /* Set to one of the HWSIM_REGTEST_* values above */
139 static int regtest = HWSIM_REGTEST_DISABLED;
140 module_param(regtest, int, 0444);
141 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
143 static const char *hwsim_alpha2s[] = {
152 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
156 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
157 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
158 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
159 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
163 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
167 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
168 REG_RULE(5725-10, 5850+10, 40, 0, 30,
173 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
174 &hwsim_world_regdom_custom_01,
175 &hwsim_world_regdom_custom_02,
178 struct hwsim_vif_priv {
186 #define HWSIM_VIF_MAGIC 0x69537748
188 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
190 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
191 WARN(vp->magic != HWSIM_VIF_MAGIC,
192 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
193 vif, vp->magic, vif->addr, vif->type, vif->p2p);
196 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
198 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
199 vp->magic = HWSIM_VIF_MAGIC;
202 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
204 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
208 struct hwsim_sta_priv {
212 #define HWSIM_STA_MAGIC 0x6d537749
214 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
216 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
217 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
220 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
222 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
223 sp->magic = HWSIM_STA_MAGIC;
226 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
228 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
232 struct hwsim_chanctx_priv {
236 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
238 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
240 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
241 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
244 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
246 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
247 cp->magic = HWSIM_CHANCTX_MAGIC;
250 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
252 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
256 static unsigned int hwsim_net_id;
258 static DEFINE_IDA(hwsim_netgroup_ida);
265 static inline int hwsim_net_get_netgroup(struct net *net)
267 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
269 return hwsim_net->netgroup;
272 static inline int hwsim_net_set_netgroup(struct net *net)
274 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
276 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
278 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
281 static inline u32 hwsim_net_get_wmediumd(struct net *net)
283 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
285 return hwsim_net->wmediumd;
288 static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
290 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
292 hwsim_net->wmediumd = portid;
295 static struct class *hwsim_class;
297 static struct net_device *hwsim_mon; /* global monitor netdev */
299 #define CHAN2G(_freq) { \
300 .band = NL80211_BAND_2GHZ, \
301 .center_freq = (_freq), \
302 .hw_value = (_freq), \
306 #define CHAN5G(_freq) { \
307 .band = NL80211_BAND_5GHZ, \
308 .center_freq = (_freq), \
309 .hw_value = (_freq), \
313 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
314 CHAN2G(2412), /* Channel 1 */
315 CHAN2G(2417), /* Channel 2 */
316 CHAN2G(2422), /* Channel 3 */
317 CHAN2G(2427), /* Channel 4 */
318 CHAN2G(2432), /* Channel 5 */
319 CHAN2G(2437), /* Channel 6 */
320 CHAN2G(2442), /* Channel 7 */
321 CHAN2G(2447), /* Channel 8 */
322 CHAN2G(2452), /* Channel 9 */
323 CHAN2G(2457), /* Channel 10 */
324 CHAN2G(2462), /* Channel 11 */
325 CHAN2G(2467), /* Channel 12 */
326 CHAN2G(2472), /* Channel 13 */
327 CHAN2G(2484), /* Channel 14 */
330 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
331 CHAN5G(5180), /* Channel 36 */
332 CHAN5G(5200), /* Channel 40 */
333 CHAN5G(5220), /* Channel 44 */
334 CHAN5G(5240), /* Channel 48 */
336 CHAN5G(5260), /* Channel 52 */
337 CHAN5G(5280), /* Channel 56 */
338 CHAN5G(5300), /* Channel 60 */
339 CHAN5G(5320), /* Channel 64 */
341 CHAN5G(5500), /* Channel 100 */
342 CHAN5G(5520), /* Channel 104 */
343 CHAN5G(5540), /* Channel 108 */
344 CHAN5G(5560), /* Channel 112 */
345 CHAN5G(5580), /* Channel 116 */
346 CHAN5G(5600), /* Channel 120 */
347 CHAN5G(5620), /* Channel 124 */
348 CHAN5G(5640), /* Channel 128 */
349 CHAN5G(5660), /* Channel 132 */
350 CHAN5G(5680), /* Channel 136 */
351 CHAN5G(5700), /* Channel 140 */
353 CHAN5G(5745), /* Channel 149 */
354 CHAN5G(5765), /* Channel 153 */
355 CHAN5G(5785), /* Channel 157 */
356 CHAN5G(5805), /* Channel 161 */
357 CHAN5G(5825), /* Channel 165 */
358 CHAN5G(5845), /* Channel 169 */
361 static const struct ieee80211_rate hwsim_rates[] = {
363 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
364 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
365 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
376 #define OUI_QCA 0x001374
377 #define QCA_NL80211_SUBCMD_TEST 1
378 enum qca_nl80211_vendor_subcmds {
379 QCA_WLAN_VENDOR_ATTR_TEST = 8,
380 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
383 static const struct nla_policy
384 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
385 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
388 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
389 struct wireless_dev *wdev,
390 const void *data, int data_len)
393 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
397 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
398 hwsim_vendor_test_policy, NULL);
401 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
403 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
404 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
406 /* Send a vendor event as a test. Note that this would not normally be
407 * done within a command handler, but rather, based on some other
408 * trigger. For simplicity, this command is used to trigger the event
411 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
413 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
415 /* skb_put() or nla_put() will fill up data within
416 * NL80211_ATTR_VENDOR_DATA.
419 /* Add vendor data */
420 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
422 /* Send the event - this will call nla_nest_end() */
423 cfg80211_vendor_event(skb, GFP_KERNEL);
426 /* Send a response to the command */
427 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
431 /* skb_put() or nla_put() will fill up data within
432 * NL80211_ATTR_VENDOR_DATA
434 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
436 return cfg80211_vendor_cmd_reply(skb);
439 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
441 .info = { .vendor_id = OUI_QCA,
442 .subcmd = QCA_NL80211_SUBCMD_TEST },
443 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
444 .doit = mac80211_hwsim_vendor_cmd_test,
448 /* Advertise support vendor specific events */
449 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
450 { .vendor_id = OUI_QCA, .subcmd = 1 },
453 static const struct ieee80211_iface_limit hwsim_if_limits[] = {
454 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
455 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
456 BIT(NL80211_IFTYPE_P2P_CLIENT) |
457 #ifdef CONFIG_MAC80211_MESH
458 BIT(NL80211_IFTYPE_MESH_POINT) |
460 BIT(NL80211_IFTYPE_AP) |
461 BIT(NL80211_IFTYPE_P2P_GO) },
462 /* must be last, see hwsim_if_comb */
463 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
466 static const struct ieee80211_iface_combination hwsim_if_comb[] = {
468 .limits = hwsim_if_limits,
469 /* remove the last entry which is P2P_DEVICE */
470 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
471 .max_interfaces = 2048,
472 .num_different_channels = 1,
473 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
474 BIT(NL80211_CHAN_WIDTH_20) |
475 BIT(NL80211_CHAN_WIDTH_40) |
476 BIT(NL80211_CHAN_WIDTH_80) |
477 BIT(NL80211_CHAN_WIDTH_160),
481 static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
483 .limits = hwsim_if_limits,
484 .n_limits = ARRAY_SIZE(hwsim_if_limits),
485 .max_interfaces = 2048,
486 .num_different_channels = 1,
487 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
488 BIT(NL80211_CHAN_WIDTH_20) |
489 BIT(NL80211_CHAN_WIDTH_40) |
490 BIT(NL80211_CHAN_WIDTH_80) |
491 BIT(NL80211_CHAN_WIDTH_160),
495 static spinlock_t hwsim_radio_lock;
496 static LIST_HEAD(hwsim_radios);
497 static struct workqueue_struct *hwsim_wq;
498 static struct rhashtable hwsim_radios_rht;
499 static int hwsim_radio_idx;
500 static int hwsim_radios_generation = 1;
502 static struct platform_driver mac80211_hwsim_driver = {
504 .name = "mac80211_hwsim",
508 struct mac80211_hwsim_data {
509 struct list_head list;
510 struct rhash_head rht;
511 struct ieee80211_hw *hw;
513 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
514 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
515 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
516 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
517 struct ieee80211_iface_combination if_combination;
519 struct mac_address addresses[2];
522 bool destroy_on_close;
525 const struct ieee80211_regdomain *regd;
527 struct ieee80211_channel *tmp_chan;
528 struct ieee80211_channel *roc_chan;
530 struct delayed_work roc_start;
531 struct delayed_work roc_done;
532 struct delayed_work hw_scan;
533 struct cfg80211_scan_request *hw_scan_request;
534 struct ieee80211_vif *hw_scan_vif;
536 u8 scan_addr[ETH_ALEN];
538 struct ieee80211_channel *channel;
539 unsigned long next_start, start, end;
540 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
541 ARRAY_SIZE(hwsim_channels_5ghz)];
543 struct ieee80211_channel *channel;
544 u64 beacon_int /* beacon interval in us */;
545 unsigned int rx_filter;
546 bool started, idle, scanning;
548 struct tasklet_hrtimer beacon_timer;
550 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
552 bool ps_poll_pending;
553 struct dentry *debugfs;
555 uintptr_t pending_cookie;
556 struct sk_buff_head pending; /* packets pending */
558 * Only radios in the same group can communicate together (the
559 * channel has to match too). Each bit represents a group. A
560 * radio can be in more than one group.
564 /* group shared by radios created in the same netns */
566 /* wmediumd portid responsible for netgroup of this radio */
569 /* difference between this hw's clock and the real clock, in usecs */
572 /* absolute beacon transmission time. Used to cover up "tx" delay. */
584 static const struct rhashtable_params hwsim_rht_params = {
586 .automatic_shrinking = true,
588 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
589 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
592 struct hwsim_radiotap_hdr {
593 struct ieee80211_radiotap_header hdr;
601 struct hwsim_radiotap_ack_hdr {
602 struct ieee80211_radiotap_header hdr;
609 /* MAC80211_HWSIM netlink family */
610 static struct genl_family hwsim_genl_family;
612 enum hwsim_multicast_groups {
616 static const struct genl_multicast_group hwsim_mcgrps[] = {
617 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
620 /* MAC80211_HWSIM netlink policy */
622 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
623 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
624 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
625 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
626 .len = IEEE80211_MAX_DATA_LEN },
627 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
628 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
629 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
630 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
631 .len = IEEE80211_TX_MAX_RATES *
632 sizeof(struct hwsim_tx_rate)},
633 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
634 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
635 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
636 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
637 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
638 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
639 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
640 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
641 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
642 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
643 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
644 [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
647 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
649 struct ieee80211_channel *chan);
651 /* sysfs attributes */
652 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
654 struct mac80211_hwsim_data *data = dat;
655 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
657 struct ieee80211_pspoll *pspoll;
662 wiphy_dbg(data->hw->wiphy,
663 "%s: send PS-Poll to %pM for aid %d\n",
664 __func__, vp->bssid, vp->aid);
666 skb = dev_alloc_skb(sizeof(*pspoll));
669 pspoll = skb_put(skb, sizeof(*pspoll));
670 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
671 IEEE80211_STYPE_PSPOLL |
673 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
674 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
675 memcpy(pspoll->ta, mac, ETH_ALEN);
678 mac80211_hwsim_tx_frame(data->hw, skb,
679 rcu_dereference(vif->chanctx_conf)->def.chan);
683 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
684 struct ieee80211_vif *vif, int ps)
686 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
688 struct ieee80211_hdr *hdr;
693 wiphy_dbg(data->hw->wiphy,
694 "%s: send data::nullfunc to %pM ps=%d\n",
695 __func__, vp->bssid, ps);
697 skb = dev_alloc_skb(sizeof(*hdr));
700 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
701 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
702 IEEE80211_STYPE_NULLFUNC |
703 IEEE80211_FCTL_TODS |
704 (ps ? IEEE80211_FCTL_PM : 0));
705 hdr->duration_id = cpu_to_le16(0);
706 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
707 memcpy(hdr->addr2, mac, ETH_ALEN);
708 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
711 mac80211_hwsim_tx_frame(data->hw, skb,
712 rcu_dereference(vif->chanctx_conf)->def.chan);
717 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
718 struct ieee80211_vif *vif)
720 struct mac80211_hwsim_data *data = dat;
721 hwsim_send_nullfunc(data, mac, vif, 1);
724 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
725 struct ieee80211_vif *vif)
727 struct mac80211_hwsim_data *data = dat;
728 hwsim_send_nullfunc(data, mac, vif, 0);
731 static int hwsim_fops_ps_read(void *dat, u64 *val)
733 struct mac80211_hwsim_data *data = dat;
738 static int hwsim_fops_ps_write(void *dat, u64 val)
740 struct mac80211_hwsim_data *data = dat;
743 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
744 val != PS_MANUAL_POLL)
747 if (val == PS_MANUAL_POLL) {
748 if (data->ps != PS_ENABLED)
751 ieee80211_iterate_active_interfaces_atomic(
752 data->hw, IEEE80211_IFACE_ITER_NORMAL,
753 hwsim_send_ps_poll, data);
761 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
762 ieee80211_iterate_active_interfaces_atomic(
763 data->hw, IEEE80211_IFACE_ITER_NORMAL,
764 hwsim_send_nullfunc_ps, data);
765 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
766 ieee80211_iterate_active_interfaces_atomic(
767 data->hw, IEEE80211_IFACE_ITER_NORMAL,
768 hwsim_send_nullfunc_no_ps, data);
775 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
778 static int hwsim_write_simulate_radar(void *dat, u64 val)
780 struct mac80211_hwsim_data *data = dat;
782 ieee80211_radar_detected(data->hw);
787 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
788 hwsim_write_simulate_radar, "%llu\n");
790 static int hwsim_fops_group_read(void *dat, u64 *val)
792 struct mac80211_hwsim_data *data = dat;
797 static int hwsim_fops_group_write(void *dat, u64 val)
799 struct mac80211_hwsim_data *data = dat;
804 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
805 hwsim_fops_group_read, hwsim_fops_group_write,
808 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
809 struct net_device *dev)
811 /* TODO: allow packet injection */
816 static inline u64 mac80211_hwsim_get_tsf_raw(void)
818 return ktime_to_us(ktime_get_real());
821 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
823 u64 now = mac80211_hwsim_get_tsf_raw();
824 return cpu_to_le64(now + data->tsf_offset);
827 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
828 struct ieee80211_vif *vif)
830 struct mac80211_hwsim_data *data = hw->priv;
831 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
834 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
835 struct ieee80211_vif *vif, u64 tsf)
837 struct mac80211_hwsim_data *data = hw->priv;
838 u64 now = mac80211_hwsim_get_tsf(hw, vif);
839 u32 bcn_int = data->beacon_int;
840 u64 delta = abs(tsf - now);
842 /* adjust after beaconing with new timestamp at old TBTT */
844 data->tsf_offset += delta;
845 data->bcn_delta = do_div(delta, bcn_int);
847 data->tsf_offset -= delta;
848 data->bcn_delta = -(s64)do_div(delta, bcn_int);
852 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
853 struct sk_buff *tx_skb,
854 struct ieee80211_channel *chan)
856 struct mac80211_hwsim_data *data = hw->priv;
858 struct hwsim_radiotap_hdr *hdr;
860 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
861 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
863 if (WARN_ON(!txrate))
866 if (!netif_running(hwsim_mon))
869 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
873 hdr = skb_push(skb, sizeof(*hdr));
874 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
876 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
877 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
878 (1 << IEEE80211_RADIOTAP_RATE) |
879 (1 << IEEE80211_RADIOTAP_TSFT) |
880 (1 << IEEE80211_RADIOTAP_CHANNEL));
881 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
883 hdr->rt_rate = txrate->bitrate / 5;
884 hdr->rt_channel = cpu_to_le16(chan->center_freq);
885 flags = IEEE80211_CHAN_2GHZ;
886 if (txrate->flags & IEEE80211_RATE_ERP_G)
887 flags |= IEEE80211_CHAN_OFDM;
889 flags |= IEEE80211_CHAN_CCK;
890 hdr->rt_chbitmask = cpu_to_le16(flags);
892 skb->dev = hwsim_mon;
893 skb_reset_mac_header(skb);
894 skb->ip_summed = CHECKSUM_UNNECESSARY;
895 skb->pkt_type = PACKET_OTHERHOST;
896 skb->protocol = htons(ETH_P_802_2);
897 memset(skb->cb, 0, sizeof(skb->cb));
902 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
906 struct hwsim_radiotap_ack_hdr *hdr;
908 struct ieee80211_hdr *hdr11;
910 if (!netif_running(hwsim_mon))
913 skb = dev_alloc_skb(100);
917 hdr = skb_put(skb, sizeof(*hdr));
918 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
920 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
921 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
922 (1 << IEEE80211_RADIOTAP_CHANNEL));
925 hdr->rt_channel = cpu_to_le16(chan->center_freq);
926 flags = IEEE80211_CHAN_2GHZ;
927 hdr->rt_chbitmask = cpu_to_le16(flags);
929 hdr11 = skb_put(skb, 10);
930 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
931 IEEE80211_STYPE_ACK);
932 hdr11->duration_id = cpu_to_le16(0);
933 memcpy(hdr11->addr1, addr, ETH_ALEN);
935 skb->dev = hwsim_mon;
936 skb_reset_mac_header(skb);
937 skb->ip_summed = CHECKSUM_UNNECESSARY;
938 skb->pkt_type = PACKET_OTHERHOST;
939 skb->protocol = htons(ETH_P_802_2);
940 memset(skb->cb, 0, sizeof(skb->cb));
944 struct mac80211_hwsim_addr_match_data {
949 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
950 struct ieee80211_vif *vif)
952 struct mac80211_hwsim_addr_match_data *md = data;
954 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
958 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
961 struct mac80211_hwsim_addr_match_data md = {
965 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
968 memcpy(md.addr, addr, ETH_ALEN);
970 ieee80211_iterate_active_interfaces_atomic(data->hw,
971 IEEE80211_IFACE_ITER_NORMAL,
972 mac80211_hwsim_addr_iter,
978 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
987 /* TODO: accept (some) Beacons by default and other frames only
988 * if pending PS-Poll has been sent */
991 /* Allow unicast frames to own address if there is a pending
993 if (data->ps_poll_pending &&
994 mac80211_hwsim_addr_match(data, skb->data + 4)) {
995 data->ps_poll_pending = false;
1004 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1005 struct sk_buff *skb, int portid)
1012 for_each_net_rcu(net) {
1013 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1014 res = genlmsg_unicast(net, skb, portid);
1027 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1031 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1032 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1033 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1034 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1035 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1036 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1037 if (rate->flags & IEEE80211_TX_RC_MCS)
1038 result |= MAC80211_HWSIM_TX_RC_MCS;
1039 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1040 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1041 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1042 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1043 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1044 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1045 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1046 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1047 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1048 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1049 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1050 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1051 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1052 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1057 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1058 struct sk_buff *my_skb,
1061 struct sk_buff *skb;
1062 struct mac80211_hwsim_data *data = hw->priv;
1063 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1064 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1066 unsigned int hwsim_flags = 0;
1068 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1069 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1072 if (data->ps != PS_DISABLED)
1073 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1074 /* If the queue contains MAX_QUEUE skb's drop some */
1075 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1076 /* Droping until WARN_QUEUE level */
1077 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1078 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1083 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1085 goto nla_put_failure;
1087 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1089 if (msg_head == NULL) {
1090 pr_debug("mac80211_hwsim: problem with msg_head\n");
1091 goto nla_put_failure;
1094 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1095 ETH_ALEN, data->addresses[1].addr))
1096 goto nla_put_failure;
1098 /* We get the skb->data */
1099 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1100 goto nla_put_failure;
1102 /* We get the flags for this transmission, and we translate them to
1105 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1106 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1108 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1109 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1111 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1112 goto nla_put_failure;
1114 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1115 goto nla_put_failure;
1117 /* We get the tx control (rate and retries) info*/
1119 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1120 tx_attempts[i].idx = info->status.rates[i].idx;
1121 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1122 tx_attempts[i].count = info->status.rates[i].count;
1123 tx_attempts_flags[i].flags =
1124 trans_tx_rate_flags_ieee2hwsim(
1125 &info->status.rates[i]);
1128 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1129 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1131 goto nla_put_failure;
1133 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1134 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1136 goto nla_put_failure;
1138 /* We create a cookie to identify this skb */
1139 data->pending_cookie++;
1140 cookie = data->pending_cookie;
1141 info->rate_driver_data[0] = (void *)cookie;
1142 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1143 goto nla_put_failure;
1145 genlmsg_end(skb, msg_head);
1146 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1147 goto err_free_txskb;
1149 /* Enqueue the packet */
1150 skb_queue_tail(&data->pending, my_skb);
1152 data->tx_bytes += my_skb->len;
1158 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1159 ieee80211_free_txskb(hw, my_skb);
1163 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1164 struct ieee80211_channel *c2)
1169 return c1->center_freq == c2->center_freq;
1172 struct tx_iter_data {
1173 struct ieee80211_channel *channel;
1177 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1178 struct ieee80211_vif *vif)
1180 struct tx_iter_data *data = _data;
1182 if (!vif->chanctx_conf)
1185 if (!hwsim_chans_compat(data->channel,
1186 rcu_dereference(vif->chanctx_conf)->def.chan))
1189 data->receive = true;
1192 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1195 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1197 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1198 * (but you should use a valid OUI, not that)
1200 * If anyone wants to 'donate' a radiotap OUI/subns code
1201 * please send a patch removing this #ifdef and changing
1202 * the values accordingly.
1204 #ifdef HWSIM_RADIOTAP_OUI
1205 struct ieee80211_vendor_radiotap *rtap;
1208 * Note that this code requires the headroom in the SKB
1209 * that was allocated earlier.
1211 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1212 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1213 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1214 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1218 * Radiotap vendor namespaces can (and should) also be
1219 * split into fields by using the standard radiotap
1220 * presence bitmap mechanism. Use just BIT(0) here for
1221 * the presence bitmap.
1223 rtap->present = BIT(0);
1224 /* We have 8 bytes of (dummy) data */
1226 /* For testing, also require it to be aligned */
1228 /* And also test that padding works, 4 bytes */
1231 memcpy(rtap->data, "ABCDEFGH", 8);
1232 /* make sure to clear padding, mac80211 doesn't */
1233 memset(rtap->data + 8, 0, 4);
1235 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1239 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1240 struct sk_buff *skb,
1241 struct ieee80211_channel *chan)
1243 struct mac80211_hwsim_data *data = hw->priv, *data2;
1245 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1246 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1247 struct ieee80211_rx_status rx_status;
1250 memset(&rx_status, 0, sizeof(rx_status));
1251 rx_status.flag |= RX_FLAG_MACTIME_START;
1252 rx_status.freq = chan->center_freq;
1253 rx_status.band = chan->band;
1254 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1255 rx_status.rate_idx =
1256 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1258 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1259 rx_status.encoding = RX_ENC_VHT;
1261 rx_status.rate_idx = info->control.rates[0].idx;
1262 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1263 rx_status.encoding = RX_ENC_HT;
1265 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1266 rx_status.bw = RATE_INFO_BW_40;
1267 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1268 rx_status.bw = RATE_INFO_BW_80;
1269 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1270 rx_status.bw = RATE_INFO_BW_160;
1272 rx_status.bw = RATE_INFO_BW_20;
1273 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1274 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1275 /* TODO: simulate real signal strength (and optional packet loss) */
1276 rx_status.signal = -50;
1277 if (info->control.vif)
1278 rx_status.signal += info->control.vif->bss_conf.txpower;
1280 if (data->ps != PS_DISABLED)
1281 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1283 /* release the skb's source info */
1291 * Get absolute mactime here so all HWs RX at the "same time", and
1292 * absolute TX time for beacon mactime so the timestamp matches.
1293 * Giving beacons a different mactime than non-beacons looks messy, but
1294 * it helps the Toffset be exact and a ~10us mactime discrepancy
1295 * probably doesn't really matter.
1297 if (ieee80211_is_beacon(hdr->frame_control) ||
1298 ieee80211_is_probe_resp(hdr->frame_control))
1299 now = data->abs_bcn_ts;
1301 now = mac80211_hwsim_get_tsf_raw();
1303 /* Copy skb to all enabled radios that are on the current frequency */
1304 spin_lock(&hwsim_radio_lock);
1305 list_for_each_entry(data2, &hwsim_radios, list) {
1306 struct sk_buff *nskb;
1307 struct tx_iter_data tx_iter_data = {
1315 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1316 !hwsim_ps_rx_ok(data2, skb))
1319 if (!(data->group & data2->group))
1322 if (data->netgroup != data2->netgroup)
1325 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1326 !hwsim_chans_compat(chan, data2->channel)) {
1327 ieee80211_iterate_active_interfaces_atomic(
1328 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1329 mac80211_hwsim_tx_iter, &tx_iter_data);
1330 if (!tx_iter_data.receive)
1335 * reserve some space for our vendor and the normal
1336 * radiotap header, since we're copying anyway
1338 if (skb->len < PAGE_SIZE && paged_rx) {
1339 struct page *page = alloc_page(GFP_ATOMIC);
1344 nskb = dev_alloc_skb(128);
1350 memcpy(page_address(page), skb->data, skb->len);
1351 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1353 nskb = skb_copy(skb, GFP_ATOMIC);
1358 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1361 rx_status.mactime = now + data2->tsf_offset;
1363 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1365 mac80211_hwsim_add_vendor_rtap(nskb);
1368 data2->rx_bytes += nskb->len;
1369 ieee80211_rx_irqsafe(data2->hw, nskb);
1371 spin_unlock(&hwsim_radio_lock);
1376 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1377 struct ieee80211_tx_control *control,
1378 struct sk_buff *skb)
1380 struct mac80211_hwsim_data *data = hw->priv;
1381 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1382 struct ieee80211_hdr *hdr = (void *)skb->data;
1383 struct ieee80211_chanctx_conf *chanctx_conf;
1384 struct ieee80211_channel *channel;
1388 if (WARN_ON(skb->len < 10)) {
1389 /* Should not happen; just a sanity check for addr1 use */
1390 ieee80211_free_txskb(hw, skb);
1394 if (!data->use_chanctx) {
1395 channel = data->channel;
1396 } else if (txi->hw_queue == 4) {
1397 channel = data->tmp_chan;
1399 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1401 channel = chanctx_conf->def.chan;
1406 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1407 ieee80211_free_txskb(hw, skb);
1411 if (data->idle && !data->tmp_chan) {
1412 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1413 ieee80211_free_txskb(hw, skb);
1417 if (txi->control.vif)
1418 hwsim_check_magic(txi->control.vif);
1420 hwsim_check_sta_magic(control->sta);
1422 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1423 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1425 ARRAY_SIZE(txi->control.rates));
1427 if (skb->len >= 24 + 8 &&
1428 ieee80211_is_probe_resp(hdr->frame_control)) {
1429 /* fake header transmission time */
1430 struct ieee80211_mgmt *mgmt;
1431 struct ieee80211_rate *txrate;
1434 mgmt = (struct ieee80211_mgmt *)skb->data;
1435 txrate = ieee80211_get_tx_rate(hw, txi);
1436 ts = mac80211_hwsim_get_tsf_raw();
1437 mgmt->u.probe_resp.timestamp =
1438 cpu_to_le64(ts + data->tsf_offset +
1439 24 * 8 * 10 / txrate->bitrate);
1442 mac80211_hwsim_monitor_rx(hw, skb, channel);
1444 /* wmediumd mode check */
1445 _portid = READ_ONCE(data->wmediumd);
1448 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1450 /* NO wmediumd detected, perfect medium simulation */
1452 data->tx_bytes += skb->len;
1453 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1455 if (ack && skb->len >= 16)
1456 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1458 ieee80211_tx_info_clear_status(txi);
1460 /* frame was transmitted at most favorable rate at first attempt */
1461 txi->control.rates[0].count = 1;
1462 txi->control.rates[1].idx = -1;
1464 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1465 txi->flags |= IEEE80211_TX_STAT_ACK;
1466 ieee80211_tx_status_irqsafe(hw, skb);
1470 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1472 struct mac80211_hwsim_data *data = hw->priv;
1473 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1474 data->started = true;
1479 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1481 struct mac80211_hwsim_data *data = hw->priv;
1482 data->started = false;
1483 tasklet_hrtimer_cancel(&data->beacon_timer);
1484 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1488 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1489 struct ieee80211_vif *vif)
1491 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1492 __func__, ieee80211_vif_type_p2p(vif),
1494 hwsim_set_magic(vif);
1497 vif->hw_queue[IEEE80211_AC_VO] = 0;
1498 vif->hw_queue[IEEE80211_AC_VI] = 1;
1499 vif->hw_queue[IEEE80211_AC_BE] = 2;
1500 vif->hw_queue[IEEE80211_AC_BK] = 3;
1506 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1507 struct ieee80211_vif *vif,
1508 enum nl80211_iftype newtype,
1511 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1512 wiphy_dbg(hw->wiphy,
1513 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1514 __func__, ieee80211_vif_type_p2p(vif),
1515 newtype, vif->addr);
1516 hwsim_check_magic(vif);
1519 * interface may change from non-AP to AP in
1520 * which case this needs to be set up again
1527 static void mac80211_hwsim_remove_interface(
1528 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1530 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1531 __func__, ieee80211_vif_type_p2p(vif),
1533 hwsim_check_magic(vif);
1534 hwsim_clear_magic(vif);
1537 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1538 struct sk_buff *skb,
1539 struct ieee80211_channel *chan)
1541 struct mac80211_hwsim_data *data = hw->priv;
1542 u32 _pid = READ_ONCE(data->wmediumd);
1544 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1545 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1546 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1548 ARRAY_SIZE(txi->control.rates));
1551 mac80211_hwsim_monitor_rx(hw, skb, chan);
1554 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1556 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1560 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1561 struct ieee80211_vif *vif)
1563 struct mac80211_hwsim_data *data = arg;
1564 struct ieee80211_hw *hw = data->hw;
1565 struct ieee80211_tx_info *info;
1566 struct ieee80211_rate *txrate;
1567 struct ieee80211_mgmt *mgmt;
1568 struct sk_buff *skb;
1570 hwsim_check_magic(vif);
1572 if (vif->type != NL80211_IFTYPE_AP &&
1573 vif->type != NL80211_IFTYPE_MESH_POINT &&
1574 vif->type != NL80211_IFTYPE_ADHOC)
1577 skb = ieee80211_beacon_get(hw, vif);
1580 info = IEEE80211_SKB_CB(skb);
1581 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1582 ieee80211_get_tx_rates(vif, NULL, skb,
1583 info->control.rates,
1584 ARRAY_SIZE(info->control.rates));
1586 txrate = ieee80211_get_tx_rate(hw, info);
1588 mgmt = (struct ieee80211_mgmt *) skb->data;
1589 /* fake header transmission time */
1590 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1591 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1593 24 * 8 * 10 / txrate->bitrate);
1595 mac80211_hwsim_tx_frame(hw, skb,
1596 rcu_dereference(vif->chanctx_conf)->def.chan);
1598 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1599 ieee80211_csa_finish(vif);
1602 static enum hrtimer_restart
1603 mac80211_hwsim_beacon(struct hrtimer *timer)
1605 struct mac80211_hwsim_data *data =
1606 container_of(timer, struct mac80211_hwsim_data,
1607 beacon_timer.timer);
1608 struct ieee80211_hw *hw = data->hw;
1609 u64 bcn_int = data->beacon_int;
1615 ieee80211_iterate_active_interfaces_atomic(
1616 hw, IEEE80211_IFACE_ITER_NORMAL,
1617 mac80211_hwsim_beacon_tx, data);
1619 /* beacon at new TBTT + beacon interval */
1620 if (data->bcn_delta) {
1621 bcn_int -= data->bcn_delta;
1622 data->bcn_delta = 0;
1625 next_bcn = ktime_add(hrtimer_get_expires(timer),
1626 ns_to_ktime(bcn_int * 1000));
1627 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1629 return HRTIMER_NORESTART;
1632 static const char * const hwsim_chanwidths[] = {
1633 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1634 [NL80211_CHAN_WIDTH_20] = "ht20",
1635 [NL80211_CHAN_WIDTH_40] = "ht40",
1636 [NL80211_CHAN_WIDTH_80] = "vht80",
1637 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1638 [NL80211_CHAN_WIDTH_160] = "vht160",
1641 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1643 struct mac80211_hwsim_data *data = hw->priv;
1644 struct ieee80211_conf *conf = &hw->conf;
1645 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1646 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1647 [IEEE80211_SMPS_OFF] = "off",
1648 [IEEE80211_SMPS_STATIC] = "static",
1649 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1653 if (conf->chandef.chan)
1654 wiphy_dbg(hw->wiphy,
1655 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1657 conf->chandef.chan->center_freq,
1658 conf->chandef.center_freq1,
1659 conf->chandef.center_freq2,
1660 hwsim_chanwidths[conf->chandef.width],
1661 !!(conf->flags & IEEE80211_CONF_IDLE),
1662 !!(conf->flags & IEEE80211_CONF_PS),
1663 smps_modes[conf->smps_mode]);
1665 wiphy_dbg(hw->wiphy,
1666 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1668 !!(conf->flags & IEEE80211_CONF_IDLE),
1669 !!(conf->flags & IEEE80211_CONF_PS),
1670 smps_modes[conf->smps_mode]);
1672 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1674 WARN_ON(conf->chandef.chan && data->use_chanctx);
1676 mutex_lock(&data->mutex);
1677 if (data->scanning && conf->chandef.chan) {
1678 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1679 if (data->survey_data[idx].channel == data->channel) {
1680 data->survey_data[idx].start =
1681 data->survey_data[idx].next_start;
1682 data->survey_data[idx].end = jiffies;
1687 data->channel = conf->chandef.chan;
1689 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1690 if (data->survey_data[idx].channel &&
1691 data->survey_data[idx].channel != data->channel)
1693 data->survey_data[idx].channel = data->channel;
1694 data->survey_data[idx].next_start = jiffies;
1698 data->channel = conf->chandef.chan;
1700 mutex_unlock(&data->mutex);
1702 if (!data->started || !data->beacon_int)
1703 tasklet_hrtimer_cancel(&data->beacon_timer);
1704 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
1705 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1706 u32 bcn_int = data->beacon_int;
1707 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1709 tasklet_hrtimer_start(&data->beacon_timer,
1710 ns_to_ktime(until_tbtt * 1000),
1718 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1719 unsigned int changed_flags,
1720 unsigned int *total_flags,u64 multicast)
1722 struct mac80211_hwsim_data *data = hw->priv;
1724 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1726 data->rx_filter = 0;
1727 if (*total_flags & FIF_ALLMULTI)
1728 data->rx_filter |= FIF_ALLMULTI;
1730 *total_flags = data->rx_filter;
1733 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1734 struct ieee80211_vif *vif)
1736 unsigned int *count = data;
1737 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1743 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1744 struct ieee80211_vif *vif,
1745 struct ieee80211_bss_conf *info,
1748 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1749 struct mac80211_hwsim_data *data = hw->priv;
1751 hwsim_check_magic(vif);
1753 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1754 __func__, changed, vif->addr);
1756 if (changed & BSS_CHANGED_BSSID) {
1757 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1758 __func__, info->bssid);
1759 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1762 if (changed & BSS_CHANGED_ASSOC) {
1763 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1764 info->assoc, info->aid);
1765 vp->assoc = info->assoc;
1766 vp->aid = info->aid;
1769 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1770 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1771 info->enable_beacon, info->beacon_int);
1772 vp->bcn_en = info->enable_beacon;
1773 if (data->started &&
1774 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1775 info->enable_beacon) {
1776 u64 tsf, until_tbtt;
1778 data->beacon_int = info->beacon_int * 1024;
1779 tsf = mac80211_hwsim_get_tsf(hw, vif);
1780 bcn_int = data->beacon_int;
1781 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1782 tasklet_hrtimer_start(&data->beacon_timer,
1783 ns_to_ktime(until_tbtt * 1000),
1785 } else if (!info->enable_beacon) {
1786 unsigned int count = 0;
1787 ieee80211_iterate_active_interfaces_atomic(
1788 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1789 mac80211_hwsim_bcn_en_iter, &count);
1790 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1793 tasklet_hrtimer_cancel(&data->beacon_timer);
1794 data->beacon_int = 0;
1799 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1800 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1801 info->use_cts_prot);
1804 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1805 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1806 info->use_short_preamble);
1809 if (changed & BSS_CHANGED_ERP_SLOT) {
1810 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1813 if (changed & BSS_CHANGED_HT) {
1814 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1815 info->ht_operation_mode);
1818 if (changed & BSS_CHANGED_BASIC_RATES) {
1819 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1820 (unsigned long long) info->basic_rates);
1823 if (changed & BSS_CHANGED_TXPOWER)
1824 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1827 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1828 struct ieee80211_vif *vif,
1829 struct ieee80211_sta *sta)
1831 hwsim_check_magic(vif);
1832 hwsim_set_sta_magic(sta);
1837 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1838 struct ieee80211_vif *vif,
1839 struct ieee80211_sta *sta)
1841 hwsim_check_magic(vif);
1842 hwsim_clear_sta_magic(sta);
1847 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1848 struct ieee80211_vif *vif,
1849 enum sta_notify_cmd cmd,
1850 struct ieee80211_sta *sta)
1852 hwsim_check_magic(vif);
1855 case STA_NOTIFY_SLEEP:
1856 case STA_NOTIFY_AWAKE:
1857 /* TODO: make good use of these flags */
1860 WARN(1, "Invalid sta notify: %d\n", cmd);
1865 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1866 struct ieee80211_sta *sta,
1869 hwsim_check_sta_magic(sta);
1873 static int mac80211_hwsim_conf_tx(
1874 struct ieee80211_hw *hw,
1875 struct ieee80211_vif *vif, u16 queue,
1876 const struct ieee80211_tx_queue_params *params)
1878 wiphy_dbg(hw->wiphy,
1879 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1881 params->txop, params->cw_min,
1882 params->cw_max, params->aifs);
1886 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1887 struct survey_info *survey)
1889 struct mac80211_hwsim_data *hwsim = hw->priv;
1891 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
1894 mutex_lock(&hwsim->mutex);
1895 survey->channel = hwsim->survey_data[idx].channel;
1896 if (!survey->channel) {
1897 mutex_unlock(&hwsim->mutex);
1902 * Magically conjured dummy values --- this is only ok for simulated hardware.
1904 * A real driver which cannot determine real values noise MUST NOT
1905 * report any, especially not a magically conjured ones :-)
1907 survey->filled = SURVEY_INFO_NOISE_DBM |
1909 SURVEY_INFO_TIME_BUSY;
1910 survey->noise = -92;
1912 jiffies_to_msecs(hwsim->survey_data[idx].end -
1913 hwsim->survey_data[idx].start);
1914 /* report 12.5% of channel time is used */
1915 survey->time_busy = survey->time/8;
1916 mutex_unlock(&hwsim->mutex);
1921 #ifdef CONFIG_NL80211_TESTMODE
1923 * This section contains example code for using netlink
1924 * attributes with the testmode command in nl80211.
1927 /* These enums need to be kept in sync with userspace */
1928 enum hwsim_testmode_attr {
1929 __HWSIM_TM_ATTR_INVALID = 0,
1930 HWSIM_TM_ATTR_CMD = 1,
1931 HWSIM_TM_ATTR_PS = 2,
1934 __HWSIM_TM_ATTR_AFTER_LAST,
1935 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1938 enum hwsim_testmode_cmd {
1939 HWSIM_TM_CMD_SET_PS = 0,
1940 HWSIM_TM_CMD_GET_PS = 1,
1941 HWSIM_TM_CMD_STOP_QUEUES = 2,
1942 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1945 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1946 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1947 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1950 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1951 struct ieee80211_vif *vif,
1952 void *data, int len)
1954 struct mac80211_hwsim_data *hwsim = hw->priv;
1955 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1956 struct sk_buff *skb;
1959 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1960 hwsim_testmode_policy, NULL);
1964 if (!tb[HWSIM_TM_ATTR_CMD])
1967 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1968 case HWSIM_TM_CMD_SET_PS:
1969 if (!tb[HWSIM_TM_ATTR_PS])
1971 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1972 return hwsim_fops_ps_write(hwsim, ps);
1973 case HWSIM_TM_CMD_GET_PS:
1974 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1975 nla_total_size(sizeof(u32)));
1978 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1979 goto nla_put_failure;
1980 return cfg80211_testmode_reply(skb);
1981 case HWSIM_TM_CMD_STOP_QUEUES:
1982 ieee80211_stop_queues(hw);
1984 case HWSIM_TM_CMD_WAKE_QUEUES:
1985 ieee80211_wake_queues(hw);
1997 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1998 struct ieee80211_vif *vif,
1999 struct ieee80211_ampdu_params *params)
2001 struct ieee80211_sta *sta = params->sta;
2002 enum ieee80211_ampdu_mlme_action action = params->action;
2003 u16 tid = params->tid;
2006 case IEEE80211_AMPDU_TX_START:
2007 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2009 case IEEE80211_AMPDU_TX_STOP_CONT:
2010 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2011 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2012 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2014 case IEEE80211_AMPDU_TX_OPERATIONAL:
2016 case IEEE80211_AMPDU_RX_START:
2017 case IEEE80211_AMPDU_RX_STOP:
2026 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2027 struct ieee80211_vif *vif,
2028 u32 queues, bool drop)
2030 /* Not implemented, queues only on kernel side */
2033 static void hw_scan_work(struct work_struct *work)
2035 struct mac80211_hwsim_data *hwsim =
2036 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2037 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2040 mutex_lock(&hwsim->mutex);
2041 if (hwsim->scan_chan_idx >= req->n_channels) {
2042 struct cfg80211_scan_info info = {
2046 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2047 ieee80211_scan_completed(hwsim->hw, &info);
2048 hwsim->hw_scan_request = NULL;
2049 hwsim->hw_scan_vif = NULL;
2050 hwsim->tmp_chan = NULL;
2051 mutex_unlock(&hwsim->mutex);
2055 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2056 req->channels[hwsim->scan_chan_idx]->center_freq);
2058 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2059 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2060 IEEE80211_CHAN_RADAR) ||
2066 for (i = 0; i < req->n_ssids; i++) {
2067 struct sk_buff *probe;
2068 struct ieee80211_mgmt *mgmt;
2070 probe = ieee80211_probereq_get(hwsim->hw,
2073 req->ssids[i].ssid_len,
2078 mgmt = (struct ieee80211_mgmt *) probe->data;
2079 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2080 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2083 skb_put_data(probe, req->ie, req->ie_len);
2085 if (!ieee80211_tx_prepare_skb(hwsim->hw,
2088 hwsim->tmp_chan->band,
2095 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2100 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2101 msecs_to_jiffies(dwell));
2102 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2103 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2104 hwsim->survey_data[hwsim->scan_chan_idx].end =
2105 jiffies + msecs_to_jiffies(dwell);
2106 hwsim->scan_chan_idx++;
2107 mutex_unlock(&hwsim->mutex);
2110 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2111 struct ieee80211_vif *vif,
2112 struct ieee80211_scan_request *hw_req)
2114 struct mac80211_hwsim_data *hwsim = hw->priv;
2115 struct cfg80211_scan_request *req = &hw_req->req;
2117 mutex_lock(&hwsim->mutex);
2118 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2119 mutex_unlock(&hwsim->mutex);
2122 hwsim->hw_scan_request = req;
2123 hwsim->hw_scan_vif = vif;
2124 hwsim->scan_chan_idx = 0;
2125 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2126 get_random_mask_addr(hwsim->scan_addr,
2127 hw_req->req.mac_addr,
2128 hw_req->req.mac_addr_mask);
2130 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2131 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2132 mutex_unlock(&hwsim->mutex);
2134 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2136 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2141 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2142 struct ieee80211_vif *vif)
2144 struct mac80211_hwsim_data *hwsim = hw->priv;
2145 struct cfg80211_scan_info info = {
2149 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2151 cancel_delayed_work_sync(&hwsim->hw_scan);
2153 mutex_lock(&hwsim->mutex);
2154 ieee80211_scan_completed(hwsim->hw, &info);
2155 hwsim->tmp_chan = NULL;
2156 hwsim->hw_scan_request = NULL;
2157 hwsim->hw_scan_vif = NULL;
2158 mutex_unlock(&hwsim->mutex);
2161 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2162 struct ieee80211_vif *vif,
2165 struct mac80211_hwsim_data *hwsim = hw->priv;
2167 mutex_lock(&hwsim->mutex);
2169 if (hwsim->scanning) {
2170 pr_debug("two hwsim sw_scans detected!\n");
2174 pr_debug("hwsim sw_scan request, prepping stuff\n");
2176 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2177 hwsim->scanning = true;
2178 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2181 mutex_unlock(&hwsim->mutex);
2184 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2185 struct ieee80211_vif *vif)
2187 struct mac80211_hwsim_data *hwsim = hw->priv;
2189 mutex_lock(&hwsim->mutex);
2191 pr_debug("hwsim sw_scan_complete\n");
2192 hwsim->scanning = false;
2193 eth_zero_addr(hwsim->scan_addr);
2195 mutex_unlock(&hwsim->mutex);
2198 static void hw_roc_start(struct work_struct *work)
2200 struct mac80211_hwsim_data *hwsim =
2201 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2203 mutex_lock(&hwsim->mutex);
2205 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2206 hwsim->tmp_chan = hwsim->roc_chan;
2207 ieee80211_ready_on_channel(hwsim->hw);
2209 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2210 msecs_to_jiffies(hwsim->roc_duration));
2212 mutex_unlock(&hwsim->mutex);
2215 static void hw_roc_done(struct work_struct *work)
2217 struct mac80211_hwsim_data *hwsim =
2218 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2220 mutex_lock(&hwsim->mutex);
2221 ieee80211_remain_on_channel_expired(hwsim->hw);
2222 hwsim->tmp_chan = NULL;
2223 mutex_unlock(&hwsim->mutex);
2225 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2228 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2229 struct ieee80211_vif *vif,
2230 struct ieee80211_channel *chan,
2232 enum ieee80211_roc_type type)
2234 struct mac80211_hwsim_data *hwsim = hw->priv;
2236 mutex_lock(&hwsim->mutex);
2237 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2238 mutex_unlock(&hwsim->mutex);
2242 hwsim->roc_chan = chan;
2243 hwsim->roc_duration = duration;
2244 mutex_unlock(&hwsim->mutex);
2246 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2247 chan->center_freq, duration);
2248 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2253 static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2255 struct mac80211_hwsim_data *hwsim = hw->priv;
2257 cancel_delayed_work_sync(&hwsim->roc_start);
2258 cancel_delayed_work_sync(&hwsim->roc_done);
2260 mutex_lock(&hwsim->mutex);
2261 hwsim->tmp_chan = NULL;
2262 mutex_unlock(&hwsim->mutex);
2264 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2269 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2270 struct ieee80211_chanctx_conf *ctx)
2272 hwsim_set_chanctx_magic(ctx);
2273 wiphy_dbg(hw->wiphy,
2274 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2275 ctx->def.chan->center_freq, ctx->def.width,
2276 ctx->def.center_freq1, ctx->def.center_freq2);
2280 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2281 struct ieee80211_chanctx_conf *ctx)
2283 wiphy_dbg(hw->wiphy,
2284 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2285 ctx->def.chan->center_freq, ctx->def.width,
2286 ctx->def.center_freq1, ctx->def.center_freq2);
2287 hwsim_check_chanctx_magic(ctx);
2288 hwsim_clear_chanctx_magic(ctx);
2291 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2292 struct ieee80211_chanctx_conf *ctx,
2295 hwsim_check_chanctx_magic(ctx);
2296 wiphy_dbg(hw->wiphy,
2297 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2298 ctx->def.chan->center_freq, ctx->def.width,
2299 ctx->def.center_freq1, ctx->def.center_freq2);
2302 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2303 struct ieee80211_vif *vif,
2304 struct ieee80211_chanctx_conf *ctx)
2306 hwsim_check_magic(vif);
2307 hwsim_check_chanctx_magic(ctx);
2312 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2313 struct ieee80211_vif *vif,
2314 struct ieee80211_chanctx_conf *ctx)
2316 hwsim_check_magic(vif);
2317 hwsim_check_chanctx_magic(ctx);
2320 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2331 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2333 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2334 struct ieee80211_vif *vif,
2337 if (sset == ETH_SS_STATS)
2338 memcpy(data, *mac80211_hwsim_gstrings_stats,
2339 sizeof(mac80211_hwsim_gstrings_stats));
2342 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2343 struct ieee80211_vif *vif, int sset)
2345 if (sset == ETH_SS_STATS)
2346 return MAC80211_HWSIM_SSTATS_LEN;
2350 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2351 struct ieee80211_vif *vif,
2352 struct ethtool_stats *stats, u64 *data)
2354 struct mac80211_hwsim_data *ar = hw->priv;
2357 data[i++] = ar->tx_pkts;
2358 data[i++] = ar->tx_bytes;
2359 data[i++] = ar->rx_pkts;
2360 data[i++] = ar->rx_bytes;
2361 data[i++] = ar->tx_dropped;
2362 data[i++] = ar->tx_failed;
2364 data[i++] = ar->group;
2366 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2369 #define HWSIM_COMMON_OPS \
2370 .tx = mac80211_hwsim_tx, \
2371 .start = mac80211_hwsim_start, \
2372 .stop = mac80211_hwsim_stop, \
2373 .add_interface = mac80211_hwsim_add_interface, \
2374 .change_interface = mac80211_hwsim_change_interface, \
2375 .remove_interface = mac80211_hwsim_remove_interface, \
2376 .config = mac80211_hwsim_config, \
2377 .configure_filter = mac80211_hwsim_configure_filter, \
2378 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2379 .sta_add = mac80211_hwsim_sta_add, \
2380 .sta_remove = mac80211_hwsim_sta_remove, \
2381 .sta_notify = mac80211_hwsim_sta_notify, \
2382 .set_tim = mac80211_hwsim_set_tim, \
2383 .conf_tx = mac80211_hwsim_conf_tx, \
2384 .get_survey = mac80211_hwsim_get_survey, \
2385 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2386 .ampdu_action = mac80211_hwsim_ampdu_action, \
2387 .flush = mac80211_hwsim_flush, \
2388 .get_tsf = mac80211_hwsim_get_tsf, \
2389 .set_tsf = mac80211_hwsim_set_tsf, \
2390 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2391 .get_et_stats = mac80211_hwsim_get_et_stats, \
2392 .get_et_strings = mac80211_hwsim_get_et_strings,
2394 static const struct ieee80211_ops mac80211_hwsim_ops = {
2396 .sw_scan_start = mac80211_hwsim_sw_scan,
2397 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2400 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2402 .hw_scan = mac80211_hwsim_hw_scan,
2403 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2404 .sw_scan_start = NULL,
2405 .sw_scan_complete = NULL,
2406 .remain_on_channel = mac80211_hwsim_roc,
2407 .cancel_remain_on_channel = mac80211_hwsim_croc,
2408 .add_chanctx = mac80211_hwsim_add_chanctx,
2409 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2410 .change_chanctx = mac80211_hwsim_change_chanctx,
2411 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2412 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2415 struct hwsim_new_radio_params {
2416 unsigned int channels;
2417 const char *reg_alpha2;
2418 const struct ieee80211_regdomain *regd;
2422 bool destroy_on_close;
2425 const u8 *perm_addr;
2428 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2429 struct genl_info *info)
2432 genl_notify(&hwsim_genl_family, mcast_skb, info,
2433 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2435 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2436 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2439 static int append_radio_msg(struct sk_buff *skb, int id,
2440 struct hwsim_new_radio_params *param)
2444 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2448 if (param->channels) {
2449 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2454 if (param->reg_alpha2) {
2455 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2464 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2465 if (hwsim_world_regdom_custom[i] != param->regd)
2468 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2475 if (param->reg_strict) {
2476 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2481 if (param->p2p_device) {
2482 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2487 if (param->use_chanctx) {
2488 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2493 if (param->hwname) {
2494 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2495 strlen(param->hwname), param->hwname);
2503 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2504 struct hwsim_new_radio_params *param)
2506 struct sk_buff *mcast_skb;
2509 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2513 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2514 HWSIM_CMD_NEW_RADIO);
2518 if (append_radio_msg(mcast_skb, id, param) < 0)
2521 genlmsg_end(mcast_skb, data);
2523 hwsim_mcast_config_msg(mcast_skb, info);
2527 nlmsg_free(mcast_skb);
2530 static const struct ieee80211_sband_iftype_data he_capa_2ghz = {
2531 /* TODO: should we support other types, e.g., P2P?*/
2532 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2537 IEEE80211_HE_MAC_CAP0_HTC_HE,
2539 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2540 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
2542 IEEE80211_HE_MAC_CAP2_BSR |
2543 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2544 IEEE80211_HE_MAC_CAP2_ACK_EN,
2546 IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
2547 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2548 IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
2549 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2551 IEEE80211_HE_PHY_CAP0_DUAL_BAND,
2553 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2554 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2555 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2556 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
2558 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2559 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2560 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2561 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2562 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2564 /* Leave all the other PHY capability bytes unset, as
2565 * DCM, beam forming, RU and PPE threshold information
2569 .he_mcs_nss_supp = {
2570 .rx_mcs_80 = cpu_to_le16(0xfffa),
2571 .tx_mcs_80 = cpu_to_le16(0xfffa),
2572 .rx_mcs_160 = cpu_to_le16(0xffff),
2573 .tx_mcs_160 = cpu_to_le16(0xffff),
2574 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2575 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2580 static const struct ieee80211_sband_iftype_data he_capa_5ghz = {
2581 /* TODO: should we support other types, e.g., P2P?*/
2582 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2587 IEEE80211_HE_MAC_CAP0_HTC_HE,
2589 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2590 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
2592 IEEE80211_HE_MAC_CAP2_BSR |
2593 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2594 IEEE80211_HE_MAC_CAP2_ACK_EN,
2596 IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
2597 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2598 IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
2599 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2601 IEEE80211_HE_PHY_CAP0_DUAL_BAND |
2602 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2603 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2604 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2606 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2607 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2608 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2609 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
2611 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2612 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2613 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2614 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2615 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2617 /* Leave all the other PHY capability bytes unset, as
2618 * DCM, beam forming, RU and PPE threshold information
2622 .he_mcs_nss_supp = {
2623 .rx_mcs_80 = cpu_to_le16(0xfffa),
2624 .tx_mcs_80 = cpu_to_le16(0xfffa),
2625 .rx_mcs_160 = cpu_to_le16(0xfffa),
2626 .tx_mcs_160 = cpu_to_le16(0xfffa),
2627 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2628 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2633 static void mac80211_hswim_he_capab(struct ieee80211_supported_band *sband)
2635 if (sband->band == NL80211_BAND_2GHZ)
2636 sband->iftype_data =
2637 (struct ieee80211_sband_iftype_data *)&he_capa_2ghz;
2638 else if (sband->band == NL80211_BAND_5GHZ)
2639 sband->iftype_data =
2640 (struct ieee80211_sband_iftype_data *)&he_capa_5ghz;
2644 sband->n_iftype_data = 1;
2647 static int mac80211_hwsim_new_radio(struct genl_info *info,
2648 struct hwsim_new_radio_params *param)
2652 struct mac80211_hwsim_data *data;
2653 struct ieee80211_hw *hw;
2654 enum nl80211_band band;
2655 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2659 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2662 spin_lock_bh(&hwsim_radio_lock);
2663 idx = hwsim_radio_idx++;
2664 spin_unlock_bh(&hwsim_radio_lock);
2666 if (param->use_chanctx)
2667 ops = &mac80211_hwsim_mchan_ops;
2668 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2670 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2675 /* ieee80211_alloc_hw_nm may have used a default name */
2676 param->hwname = wiphy_name(hw->wiphy);
2679 net = genl_info_net(info);
2682 wiphy_net_set(hw->wiphy, net);
2687 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2688 if (IS_ERR(data->dev)) {
2690 "mac80211_hwsim: device_create failed (%ld)\n",
2691 PTR_ERR(data->dev));
2693 goto failed_drvdata;
2695 data->dev->driver = &mac80211_hwsim_driver.driver;
2696 err = device_bind_driver(data->dev);
2698 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
2703 skb_queue_head_init(&data->pending);
2705 SET_IEEE80211_DEV(hw, data->dev);
2706 if (!param->perm_addr) {
2707 eth_zero_addr(addr);
2711 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2712 /* Why need here second address ? */
2713 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2714 data->addresses[1].addr[0] |= 0x40;
2715 hw->wiphy->n_addresses = 2;
2716 hw->wiphy->addresses = data->addresses;
2717 /* possible address clash is checked at hash table insertion */
2719 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2720 /* compatibility with automatically generated mac addr */
2721 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2722 hw->wiphy->n_addresses = 2;
2723 hw->wiphy->addresses = data->addresses;
2726 data->channels = param->channels;
2727 data->use_chanctx = param->use_chanctx;
2729 data->destroy_on_close = param->destroy_on_close;
2731 data->portid = info->snd_portid;
2733 if (data->use_chanctx) {
2734 hw->wiphy->max_scan_ssids = 255;
2735 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2736 hw->wiphy->max_remain_on_channel_duration = 1000;
2737 hw->wiphy->iface_combinations = &data->if_combination;
2738 if (param->p2p_device)
2739 data->if_combination = hwsim_if_comb_p2p_dev[0];
2741 data->if_combination = hwsim_if_comb[0];
2742 hw->wiphy->n_iface_combinations = 1;
2743 /* For channels > 1 DFS is not allowed */
2744 data->if_combination.radar_detect_widths = 0;
2745 data->if_combination.num_different_channels = data->channels;
2746 } else if (param->p2p_device) {
2747 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2748 hw->wiphy->n_iface_combinations =
2749 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
2751 hw->wiphy->iface_combinations = hwsim_if_comb;
2752 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2755 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
2756 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2757 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2760 hw->offchannel_tx_hw_queue = 4;
2761 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2762 BIT(NL80211_IFTYPE_AP) |
2763 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2764 BIT(NL80211_IFTYPE_P2P_GO) |
2765 BIT(NL80211_IFTYPE_ADHOC) |
2766 BIT(NL80211_IFTYPE_MESH_POINT);
2768 if (param->p2p_device)
2769 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
2771 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2772 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2773 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2774 ieee80211_hw_set(hw, QUEUE_CONTROL);
2775 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2776 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2777 ieee80211_hw_set(hw, MFP_CAPABLE);
2778 ieee80211_hw_set(hw, SIGNAL_DBM);
2779 ieee80211_hw_set(hw, SUPPORTS_PS);
2780 ieee80211_hw_set(hw, TDLS_WIDER_BW);
2782 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
2784 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2785 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2786 WIPHY_FLAG_AP_UAPSD |
2787 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
2788 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2789 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2790 NL80211_FEATURE_STATIC_SMPS |
2791 NL80211_FEATURE_DYNAMIC_SMPS |
2792 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
2793 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
2795 /* ask mac80211 to reserve space for magic */
2796 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2797 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2798 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2800 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2801 sizeof(hwsim_channels_2ghz));
2802 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2803 sizeof(hwsim_channels_5ghz));
2804 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2806 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
2807 struct ieee80211_supported_band *sband = &data->bands[band];
2812 case NL80211_BAND_2GHZ:
2813 sband->channels = data->channels_2ghz;
2814 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2815 sband->bitrates = data->rates;
2816 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2818 case NL80211_BAND_5GHZ:
2819 sband->channels = data->channels_5ghz;
2820 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2821 sband->bitrates = data->rates + 4;
2822 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
2824 sband->vht_cap.vht_supported = true;
2825 sband->vht_cap.cap =
2826 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2827 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2828 IEEE80211_VHT_CAP_RXLDPC |
2829 IEEE80211_VHT_CAP_SHORT_GI_80 |
2830 IEEE80211_VHT_CAP_SHORT_GI_160 |
2831 IEEE80211_VHT_CAP_TXSTBC |
2832 IEEE80211_VHT_CAP_RXSTBC_4 |
2833 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2834 sband->vht_cap.vht_mcs.rx_mcs_map =
2835 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2836 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2837 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2838 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2839 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
2840 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2841 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
2842 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
2843 sband->vht_cap.vht_mcs.tx_mcs_map =
2844 sband->vht_cap.vht_mcs.rx_mcs_map;
2850 sband->ht_cap.ht_supported = true;
2851 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2852 IEEE80211_HT_CAP_GRN_FLD |
2853 IEEE80211_HT_CAP_SGI_20 |
2854 IEEE80211_HT_CAP_SGI_40 |
2855 IEEE80211_HT_CAP_DSSSCCK40;
2856 sband->ht_cap.ampdu_factor = 0x3;
2857 sband->ht_cap.ampdu_density = 0x6;
2858 memset(&sband->ht_cap.mcs, 0,
2859 sizeof(sband->ht_cap.mcs));
2860 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2861 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2862 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2864 mac80211_hswim_he_capab(sband);
2866 hw->wiphy->bands[band] = sband;
2869 /* By default all radios belong to the first group */
2871 mutex_init(&data->mutex);
2873 data->netgroup = hwsim_net_get_netgroup(net);
2874 data->wmediumd = hwsim_net_get_wmediumd(net);
2876 /* Enable frame retransmissions for lossy channels */
2878 hw->max_rate_tries = 11;
2880 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2881 hw->wiphy->n_vendor_commands =
2882 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2883 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2884 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2886 if (param->reg_strict)
2887 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
2889 data->regd = param->regd;
2890 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2891 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
2892 /* give the regulatory workqueue a chance to run */
2893 schedule_timeout_interruptible(1);
2897 ieee80211_hw_set(hw, NO_AUTO_VIF);
2899 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2901 tasklet_hrtimer_init(&data->beacon_timer,
2902 mac80211_hwsim_beacon,
2903 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2905 err = ieee80211_register_hw(hw);
2907 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2912 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
2914 if (param->reg_alpha2) {
2915 data->alpha2[0] = param->reg_alpha2[0];
2916 data->alpha2[1] = param->reg_alpha2[1];
2917 regulatory_hint(hw->wiphy, param->reg_alpha2);
2920 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2921 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2922 debugfs_create_file("group", 0666, data->debugfs, data,
2924 if (!data->use_chanctx)
2925 debugfs_create_file("dfs_simulate_radar", 0222,
2927 data, &hwsim_simulate_radar);
2929 spin_lock_bh(&hwsim_radio_lock);
2930 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2934 GENL_SET_ERR_MSG(info, "perm addr already present");
2935 NL_SET_BAD_ATTR(info->extack,
2936 info->attrs[HWSIM_ATTR_PERM_ADDR]);
2938 spin_unlock_bh(&hwsim_radio_lock);
2939 goto failed_final_insert;
2942 list_add_tail(&data->list, &hwsim_radios);
2943 hwsim_radios_generation++;
2944 spin_unlock_bh(&hwsim_radio_lock);
2946 hwsim_mcast_new_radio(idx, info, param);
2950 failed_final_insert:
2951 debugfs_remove_recursive(data->debugfs);
2952 ieee80211_unregister_hw(data->hw);
2954 device_release_driver(data->dev);
2956 device_unregister(data->dev);
2958 ieee80211_free_hw(hw);
2963 static void hwsim_mcast_del_radio(int id, const char *hwname,
2964 struct genl_info *info)
2966 struct sk_buff *skb;
2970 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2974 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2975 HWSIM_CMD_DEL_RADIO);
2979 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2983 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2988 genlmsg_end(skb, data);
2990 hwsim_mcast_config_msg(skb, info);
2998 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3000 struct genl_info *info)
3002 hwsim_mcast_del_radio(data->idx, hwname, info);
3003 debugfs_remove_recursive(data->debugfs);
3004 ieee80211_unregister_hw(data->hw);
3005 device_release_driver(data->dev);
3006 device_unregister(data->dev);
3007 ieee80211_free_hw(data->hw);
3010 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3011 struct mac80211_hwsim_data *data,
3012 u32 portid, u32 seq,
3013 struct netlink_callback *cb, int flags)
3016 struct hwsim_new_radio_params param = { };
3017 int res = -EMSGSIZE;
3019 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3020 HWSIM_CMD_GET_RADIO);
3025 genl_dump_check_consistent(cb, hdr);
3027 if (data->alpha2[0] && data->alpha2[1])
3028 param.reg_alpha2 = data->alpha2;
3030 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3031 REGULATORY_STRICT_REG);
3032 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3033 BIT(NL80211_IFTYPE_P2P_DEVICE));
3034 param.use_chanctx = data->use_chanctx;
3035 param.regd = data->regd;
3036 param.channels = data->channels;
3037 param.hwname = wiphy_name(data->hw->wiphy);
3039 res = append_radio_msg(skb, data->idx, ¶m);
3043 genlmsg_end(skb, hdr);
3047 genlmsg_cancel(skb, hdr);
3051 static void mac80211_hwsim_free(void)
3053 struct mac80211_hwsim_data *data;
3055 spin_lock_bh(&hwsim_radio_lock);
3056 while ((data = list_first_entry_or_null(&hwsim_radios,
3057 struct mac80211_hwsim_data,
3059 list_del(&data->list);
3060 spin_unlock_bh(&hwsim_radio_lock);
3061 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3063 spin_lock_bh(&hwsim_radio_lock);
3065 spin_unlock_bh(&hwsim_radio_lock);
3066 class_destroy(hwsim_class);
3069 static const struct net_device_ops hwsim_netdev_ops = {
3070 .ndo_start_xmit = hwsim_mon_xmit,
3071 .ndo_set_mac_address = eth_mac_addr,
3072 .ndo_validate_addr = eth_validate_addr,
3075 static void hwsim_mon_setup(struct net_device *dev)
3077 dev->netdev_ops = &hwsim_netdev_ops;
3078 dev->needs_free_netdev = true;
3080 dev->priv_flags |= IFF_NO_QUEUE;
3081 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3082 eth_zero_addr(dev->dev_addr);
3083 dev->dev_addr[0] = 0x12;
3086 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3088 return rhashtable_lookup_fast(&hwsim_radios_rht,
3093 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3095 struct mac80211_hwsim_data *data;
3097 hwsim_net_set_wmediumd(net, portid);
3099 spin_lock_bh(&hwsim_radio_lock);
3100 list_for_each_entry(data, &hwsim_radios, list) {
3101 if (data->netgroup == hwsim_net_get_netgroup(net))
3102 data->wmediumd = portid;
3104 spin_unlock_bh(&hwsim_radio_lock);
3107 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3108 struct genl_info *info)
3111 struct ieee80211_hdr *hdr;
3112 struct mac80211_hwsim_data *data2;
3113 struct ieee80211_tx_info *txi;
3114 struct hwsim_tx_rate *tx_attempts;
3116 struct sk_buff *skb, *tmp;
3118 unsigned int hwsim_flags;
3122 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3123 !info->attrs[HWSIM_ATTR_FLAGS] ||
3124 !info->attrs[HWSIM_ATTR_COOKIE] ||
3125 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3126 !info->attrs[HWSIM_ATTR_TX_INFO])
3129 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3130 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3131 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3133 data2 = get_hwsim_data_ref_from_addr(src);
3137 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3140 if (info->snd_portid != data2->wmediumd)
3143 /* look for the skb matching the cookie passed back from user */
3144 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3147 txi = IEEE80211_SKB_CB(skb);
3148 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3150 if (skb_cookie == ret_skb_cookie) {
3151 skb_unlink(skb, &data2->pending);
3161 /* Tx info received because the frame was broadcasted on user space,
3162 so we get all the necessary info: tx attempts and skb control buff */
3164 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3165 info->attrs[HWSIM_ATTR_TX_INFO]);
3167 /* now send back TX status */
3168 txi = IEEE80211_SKB_CB(skb);
3170 ieee80211_tx_info_clear_status(txi);
3172 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3173 txi->status.rates[i].idx = tx_attempts[i].idx;
3174 txi->status.rates[i].count = tx_attempts[i].count;
3177 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3179 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3180 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3181 if (skb->len >= 16) {
3182 hdr = (struct ieee80211_hdr *) skb->data;
3183 mac80211_hwsim_monitor_ack(data2->channel,
3186 txi->flags |= IEEE80211_TX_STAT_ACK;
3189 if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
3190 txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
3192 ieee80211_tx_status_irqsafe(data2->hw, skb);
3199 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3200 struct genl_info *info)
3202 struct mac80211_hwsim_data *data2;
3203 struct ieee80211_rx_status rx_status;
3207 struct sk_buff *skb = NULL;
3209 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3210 !info->attrs[HWSIM_ATTR_FRAME] ||
3211 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3212 !info->attrs[HWSIM_ATTR_SIGNAL])
3215 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3216 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3217 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3219 /* Allocate new skb here */
3220 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3224 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3228 skb_put_data(skb, frame_data, frame_data_len);
3230 data2 = get_hwsim_data_ref_from_addr(dst);
3234 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3237 if (info->snd_portid != data2->wmediumd)
3240 /* check if radio is configured properly */
3242 if (data2->idle || !data2->started)
3245 /* A frame is received from user space */
3246 memset(&rx_status, 0, sizeof(rx_status));
3247 if (info->attrs[HWSIM_ATTR_FREQ]) {
3248 /* throw away off-channel packets, but allow both the temporary
3249 * ("hw" scan/remain-on-channel) and regular channel, since the
3250 * internal datapath also allows this
3252 mutex_lock(&data2->mutex);
3253 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3255 if (rx_status.freq != data2->channel->center_freq &&
3256 (!data2->tmp_chan ||
3257 rx_status.freq != data2->tmp_chan->center_freq)) {
3258 mutex_unlock(&data2->mutex);
3261 mutex_unlock(&data2->mutex);
3263 rx_status.freq = data2->channel->center_freq;
3266 rx_status.band = data2->channel->band;
3267 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3268 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3270 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3272 data2->rx_bytes += skb->len;
3273 ieee80211_rx_irqsafe(data2->hw, skb);
3277 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3283 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3284 struct genl_info *info)
3286 struct net *net = genl_info_net(info);
3287 struct mac80211_hwsim_data *data;
3290 spin_lock_bh(&hwsim_radio_lock);
3291 list_for_each_entry(data, &hwsim_radios, list)
3292 chans = max(chans, data->channels);
3293 spin_unlock_bh(&hwsim_radio_lock);
3295 /* In the future we should revise the userspace API and allow it
3296 * to set a flag that it does support multi-channel, then we can
3297 * let this pass conditionally on the flag.
3298 * For current userspace, prohibit it since it won't work right.
3303 if (hwsim_net_get_wmediumd(net))
3306 hwsim_register_wmediumd(net, info->snd_portid);
3308 pr_debug("mac80211_hwsim: received a REGISTER, "
3309 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3314 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3316 struct hwsim_new_radio_params param = { 0 };
3317 const char *hwname = NULL;
3320 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3321 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3322 param.channels = channels;
3323 param.destroy_on_close =
3324 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3326 if (info->attrs[HWSIM_ATTR_CHANNELS])
3327 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3329 if (param.channels < 1) {
3330 GENL_SET_ERR_MSG(info, "must have at least one channel");
3334 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3335 GENL_SET_ERR_MSG(info, "too many channels specified");
3339 if (info->attrs[HWSIM_ATTR_NO_VIF])
3340 param.no_vif = true;
3342 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3343 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3344 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3348 param.hwname = hwname;
3351 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3352 param.use_chanctx = true;
3354 param.use_chanctx = (param.channels > 1);
3356 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3358 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3360 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3361 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3363 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3368 idx = array_index_nospec(idx,
3369 ARRAY_SIZE(hwsim_world_regdom_custom));
3370 param.regd = hwsim_world_regdom_custom[idx];
3373 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3374 if (!is_valid_ether_addr(
3375 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3376 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3377 NL_SET_BAD_ATTR(info->extack,
3378 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3384 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3387 ret = mac80211_hwsim_new_radio(info, ¶m);
3392 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3394 struct mac80211_hwsim_data *data;
3396 const char *hwname = NULL;
3398 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3399 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3400 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3401 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3402 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3409 spin_lock_bh(&hwsim_radio_lock);
3410 list_for_each_entry(data, &hwsim_radios, list) {
3412 if (data->idx != idx)
3416 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3420 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3423 list_del(&data->list);
3424 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3426 hwsim_radios_generation++;
3427 spin_unlock_bh(&hwsim_radio_lock);
3428 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3433 spin_unlock_bh(&hwsim_radio_lock);
3439 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3441 struct mac80211_hwsim_data *data;
3442 struct sk_buff *skb;
3443 int idx, res = -ENODEV;
3445 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3447 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3449 spin_lock_bh(&hwsim_radio_lock);
3450 list_for_each_entry(data, &hwsim_radios, list) {
3451 if (data->idx != idx)
3454 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3457 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3463 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3464 info->snd_seq, NULL, 0);
3470 res = genlmsg_reply(skb, info);
3475 spin_unlock_bh(&hwsim_radio_lock);
3480 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3481 struct netlink_callback *cb)
3483 int last_idx = cb->args[0] - 1;
3484 struct mac80211_hwsim_data *data = NULL;
3488 spin_lock_bh(&hwsim_radio_lock);
3489 cb->seq = hwsim_radios_generation;
3491 if (last_idx >= hwsim_radio_idx-1)
3494 list_for_each_entry(data, &hwsim_radios, list) {
3495 if (data->idx <= last_idx)
3498 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3501 res = mac80211_hwsim_get_radio(skb, data,
3502 NETLINK_CB(cb->skb).portid,
3503 cb->nlh->nlmsg_seq, cb,
3508 last_idx = data->idx;
3511 cb->args[0] = last_idx + 1;
3513 /* list changed, but no new element sent, set interrupted flag */
3514 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3515 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3516 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3517 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3519 genl_dump_check_consistent(cb, hdr);
3520 genlmsg_end(skb, hdr);
3527 spin_unlock_bh(&hwsim_radio_lock);
3528 return res ?: skb->len;
3531 /* Generic Netlink operations array */
3532 static const struct genl_ops hwsim_ops[] = {
3534 .cmd = HWSIM_CMD_REGISTER,
3535 .policy = hwsim_genl_policy,
3536 .doit = hwsim_register_received_nl,
3537 .flags = GENL_UNS_ADMIN_PERM,
3540 .cmd = HWSIM_CMD_FRAME,
3541 .policy = hwsim_genl_policy,
3542 .doit = hwsim_cloned_frame_received_nl,
3545 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3546 .policy = hwsim_genl_policy,
3547 .doit = hwsim_tx_info_frame_received_nl,
3550 .cmd = HWSIM_CMD_NEW_RADIO,
3551 .policy = hwsim_genl_policy,
3552 .doit = hwsim_new_radio_nl,
3553 .flags = GENL_UNS_ADMIN_PERM,
3556 .cmd = HWSIM_CMD_DEL_RADIO,
3557 .policy = hwsim_genl_policy,
3558 .doit = hwsim_del_radio_nl,
3559 .flags = GENL_UNS_ADMIN_PERM,
3562 .cmd = HWSIM_CMD_GET_RADIO,
3563 .policy = hwsim_genl_policy,
3564 .doit = hwsim_get_radio_nl,
3565 .dumpit = hwsim_dump_radio_nl,
3569 static struct genl_family hwsim_genl_family __ro_after_init = {
3570 .name = "MAC80211_HWSIM",
3572 .maxattr = HWSIM_ATTR_MAX,
3574 .module = THIS_MODULE,
3576 .n_ops = ARRAY_SIZE(hwsim_ops),
3577 .mcgrps = hwsim_mcgrps,
3578 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3581 static void remove_user_radios(u32 portid)
3583 struct mac80211_hwsim_data *entry, *tmp;
3586 spin_lock_bh(&hwsim_radio_lock);
3587 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3588 if (entry->destroy_on_close && entry->portid == portid) {
3589 list_move(&entry->list, &list);
3590 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3592 hwsim_radios_generation++;
3595 spin_unlock_bh(&hwsim_radio_lock);
3597 list_for_each_entry_safe(entry, tmp, &list, list) {
3598 list_del(&entry->list);
3599 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
3604 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3605 unsigned long state,
3608 struct netlink_notify *notify = _notify;
3610 if (state != NETLINK_URELEASE)
3613 remove_user_radios(notify->portid);
3615 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
3616 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3617 " socket, switching to perfect channel medium\n");
3618 hwsim_register_wmediumd(notify->net, 0);
3624 static struct notifier_block hwsim_netlink_notifier = {
3625 .notifier_call = mac80211_hwsim_netlink_notify,
3628 static int __init hwsim_init_netlink(void)
3632 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3634 rc = genl_register_family(&hwsim_genl_family);
3638 rc = netlink_register_notifier(&hwsim_netlink_notifier);
3640 genl_unregister_family(&hwsim_genl_family);
3647 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3651 static __net_init int hwsim_init_net(struct net *net)
3653 return hwsim_net_set_netgroup(net);
3656 static void __net_exit hwsim_exit_net(struct net *net)
3658 struct mac80211_hwsim_data *data, *tmp;
3661 spin_lock_bh(&hwsim_radio_lock);
3662 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3663 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3666 /* Radios created in init_net are returned to init_net. */
3667 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3670 list_move(&data->list, &list);
3671 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3673 hwsim_radios_generation++;
3675 spin_unlock_bh(&hwsim_radio_lock);
3677 list_for_each_entry_safe(data, tmp, &list, list) {
3678 list_del(&data->list);
3679 mac80211_hwsim_del_radio(data,
3680 wiphy_name(data->hw->wiphy),
3684 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
3687 static struct pernet_operations hwsim_net_ops = {
3688 .init = hwsim_init_net,
3689 .exit = hwsim_exit_net,
3690 .id = &hwsim_net_id,
3691 .size = sizeof(struct hwsim_net),
3694 static void hwsim_exit_netlink(void)
3696 /* unregister the notifier */
3697 netlink_unregister_notifier(&hwsim_netlink_notifier);
3698 /* unregister the family */
3699 genl_unregister_family(&hwsim_genl_family);
3702 static int __init init_mac80211_hwsim(void)
3706 if (radios < 0 || radios > 100)
3712 spin_lock_init(&hwsim_radio_lock);
3714 hwsim_wq = alloc_workqueue("hwsim_wq", 0, 0);
3718 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
3722 err = register_pernet_device(&hwsim_net_ops);
3726 err = platform_driver_register(&mac80211_hwsim_driver);
3728 goto out_unregister_pernet;
3730 err = hwsim_init_netlink();
3732 goto out_unregister_driver;
3734 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
3735 if (IS_ERR(hwsim_class)) {
3736 err = PTR_ERR(hwsim_class);
3737 goto out_exit_netlink;
3740 for (i = 0; i < radios; i++) {
3741 struct hwsim_new_radio_params param = { 0 };
3743 param.channels = channels;
3746 case HWSIM_REGTEST_DIFF_COUNTRY:
3747 if (i < ARRAY_SIZE(hwsim_alpha2s))
3748 param.reg_alpha2 = hwsim_alpha2s[i];
3750 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3752 param.reg_alpha2 = hwsim_alpha2s[0];
3754 case HWSIM_REGTEST_STRICT_ALL:
3755 param.reg_strict = true;
3756 case HWSIM_REGTEST_DRIVER_REG_ALL:
3757 param.reg_alpha2 = hwsim_alpha2s[0];
3759 case HWSIM_REGTEST_WORLD_ROAM:
3761 param.regd = &hwsim_world_regdom_custom_01;
3763 case HWSIM_REGTEST_CUSTOM_WORLD:
3764 param.regd = &hwsim_world_regdom_custom_01;
3766 case HWSIM_REGTEST_CUSTOM_WORLD_2:
3768 param.regd = &hwsim_world_regdom_custom_01;
3770 param.regd = &hwsim_world_regdom_custom_02;
3772 case HWSIM_REGTEST_STRICT_FOLLOW:
3774 param.reg_strict = true;
3775 param.reg_alpha2 = hwsim_alpha2s[0];
3778 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
3780 param.reg_strict = true;
3781 param.reg_alpha2 = hwsim_alpha2s[0];
3782 } else if (i == 1) {
3783 param.reg_alpha2 = hwsim_alpha2s[1];
3786 case HWSIM_REGTEST_ALL:
3789 param.regd = &hwsim_world_regdom_custom_01;
3792 param.regd = &hwsim_world_regdom_custom_02;
3795 param.reg_alpha2 = hwsim_alpha2s[0];
3798 param.reg_alpha2 = hwsim_alpha2s[1];
3801 param.reg_strict = true;
3802 param.reg_alpha2 = hwsim_alpha2s[2];
3810 param.p2p_device = support_p2p_device;
3811 param.use_chanctx = channels > 1;
3813 err = mac80211_hwsim_new_radio(NULL, ¶m);
3815 goto out_free_radios;
3818 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3820 if (hwsim_mon == NULL) {
3822 goto out_free_radios;
3826 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3829 goto out_free_radios;
3832 err = register_netdevice(hwsim_mon);
3842 free_netdev(hwsim_mon);
3844 mac80211_hwsim_free();
3846 hwsim_exit_netlink();
3847 out_unregister_driver:
3848 platform_driver_unregister(&mac80211_hwsim_driver);
3849 out_unregister_pernet:
3850 unregister_pernet_device(&hwsim_net_ops);
3852 rhashtable_destroy(&hwsim_radios_rht);
3854 destroy_workqueue(hwsim_wq);
3857 module_init(init_mac80211_hwsim);
3859 static void __exit exit_mac80211_hwsim(void)
3861 pr_debug("mac80211_hwsim: unregister radios\n");
3863 hwsim_exit_netlink();
3865 mac80211_hwsim_free();
3866 flush_workqueue(hwsim_wq);
3868 rhashtable_destroy(&hwsim_radios_rht);
3869 unregister_netdev(hwsim_mon);
3870 platform_driver_unregister(&mac80211_hwsim_driver);
3871 unregister_pernet_device(&hwsim_net_ops);
3872 destroy_workqueue(hwsim_wq);
3874 module_exit(exit_mac80211_hwsim);