staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / net / wireless / mac80211_hwsim.c
blob1068757ec42e4784942e69c00161ae4e1ee16548
1 /*
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.
13 * TODO:
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>
22 #include <net/dst.h>
23 #include <net/xfrm.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
41 #define MAX_QUEUE 200
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");
67 /**
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
96 * domain.
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
112 * first two.
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
120 * domain request
121 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
122 * regulatory requests.
124 enum hwsim_regtest {
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[] = {
144 "FI",
145 "AL",
146 "US",
147 "DE",
148 "JP",
149 "AL",
152 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
153 .n_reg_rules = 4,
154 .alpha2 = "99",
155 .reg_rules = {
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 = {
164 .n_reg_rules = 2,
165 .alpha2 = "99",
166 .reg_rules = {
167 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
168 REG_RULE(5725-10, 5850+10, 40, 0, 30,
169 NL80211_RRF_NO_IR),
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 {
179 u32 magic;
180 u8 bssid[ETH_ALEN];
181 bool assoc;
182 bool bcn_en;
183 u16 aid;
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;
205 vp->magic = 0;
208 struct hwsim_sta_priv {
209 u32 magic;
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;
229 sp->magic = 0;
232 struct hwsim_chanctx_priv {
233 u32 magic;
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;
253 cp->magic = 0;
256 static unsigned int hwsim_net_id;
258 static DEFINE_IDA(hwsim_netgroup_ida);
260 struct hwsim_net {
261 int netgroup;
262 u32 wmediumd;
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,
277 0, 0, GFP_KERNEL);
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), \
303 .max_power = 20, \
306 #define CHAN5G(_freq) { \
307 .band = NL80211_BAND_5GHZ, \
308 .center_freq = (_freq), \
309 .hw_value = (_freq), \
310 .max_power = 20, \
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[] = {
362 { .bitrate = 10 },
363 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
364 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
365 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
366 { .bitrate = 60 },
367 { .bitrate = 90 },
368 { .bitrate = 120 },
369 { .bitrate = 180 },
370 { .bitrate = 240 },
371 { .bitrate = 360 },
372 { .bitrate = 480 },
373 { .bitrate = 540 }
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)
392 struct sk_buff *skb;
393 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
394 int err;
395 u32 val;
397 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
398 hwsim_vendor_test_policy, NULL);
399 if (err)
400 return err;
401 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
402 return -EINVAL;
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
409 * here.
411 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
413 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
414 if (skb) {
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);
428 if (!skb)
429 return -ENOMEM;
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) |
459 #endif
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 = {
503 .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;
512 struct device *dev;
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];
520 int channels, idx;
521 bool use_chanctx;
522 bool destroy_on_close;
523 struct work_struct destroy_work;
524 u32 portid;
525 char alpha2[2];
526 const struct ieee80211_regdomain *regd;
528 struct ieee80211_channel *tmp_chan;
529 struct ieee80211_channel *roc_chan;
530 u32 roc_duration;
531 struct delayed_work roc_start;
532 struct delayed_work roc_done;
533 struct delayed_work hw_scan;
534 struct cfg80211_scan_request *hw_scan_request;
535 struct ieee80211_vif *hw_scan_vif;
536 int scan_chan_idx;
537 u8 scan_addr[ETH_ALEN];
538 struct {
539 struct ieee80211_channel *channel;
540 unsigned long next_start, start, end;
541 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
542 ARRAY_SIZE(hwsim_channels_5ghz)];
544 struct ieee80211_channel *channel;
545 u64 beacon_int /* beacon interval in us */;
546 unsigned int rx_filter;
547 bool started, idle, scanning;
548 struct mutex mutex;
549 struct tasklet_hrtimer beacon_timer;
550 enum ps_mode {
551 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
552 } ps;
553 bool ps_poll_pending;
554 struct dentry *debugfs;
556 uintptr_t pending_cookie;
557 struct sk_buff_head pending; /* packets pending */
559 * Only radios in the same group can communicate together (the
560 * channel has to match too). Each bit represents a group. A
561 * radio can be in more than one group.
563 u64 group;
565 /* group shared by radios created in the same netns */
566 int netgroup;
567 /* wmediumd portid responsible for netgroup of this radio */
568 u32 wmediumd;
570 /* difference between this hw's clock and the real clock, in usecs */
571 s64 tsf_offset;
572 s64 bcn_delta;
573 /* absolute beacon transmission time. Used to cover up "tx" delay. */
574 u64 abs_bcn_ts;
576 /* Stats */
577 u64 tx_pkts;
578 u64 rx_pkts;
579 u64 tx_bytes;
580 u64 rx_bytes;
581 u64 tx_dropped;
582 u64 tx_failed;
585 static const struct rhashtable_params hwsim_rht_params = {
586 .nelem_hint = 2,
587 .automatic_shrinking = true,
588 .key_len = ETH_ALEN,
589 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
590 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
593 struct hwsim_radiotap_hdr {
594 struct ieee80211_radiotap_header hdr;
595 __le64 rt_tsft;
596 u8 rt_flags;
597 u8 rt_rate;
598 __le16 rt_channel;
599 __le16 rt_chbitmask;
600 } __packed;
602 struct hwsim_radiotap_ack_hdr {
603 struct ieee80211_radiotap_header hdr;
604 u8 rt_flags;
605 u8 pad;
606 __le16 rt_channel;
607 __le16 rt_chbitmask;
608 } __packed;
610 /* MAC80211_HWSIM netlink family */
611 static struct genl_family hwsim_genl_family;
613 enum hwsim_multicast_groups {
614 HWSIM_MCGRP_CONFIG,
617 static const struct genl_multicast_group hwsim_mcgrps[] = {
618 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
621 /* MAC80211_HWSIM netlink policy */
623 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
624 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
625 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
626 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
627 .len = IEEE80211_MAX_DATA_LEN },
628 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
629 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
630 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
631 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
632 .len = IEEE80211_TX_MAX_RATES *
633 sizeof(struct hwsim_tx_rate)},
634 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
635 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
636 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
637 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
638 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
639 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
640 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
641 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
642 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
643 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
644 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
645 [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
648 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
649 struct sk_buff *skb,
650 struct ieee80211_channel *chan);
652 /* sysfs attributes */
653 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
655 struct mac80211_hwsim_data *data = dat;
656 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
657 struct sk_buff *skb;
658 struct ieee80211_pspoll *pspoll;
660 if (!vp->assoc)
661 return;
663 wiphy_dbg(data->hw->wiphy,
664 "%s: send PS-Poll to %pM for aid %d\n",
665 __func__, vp->bssid, vp->aid);
667 skb = dev_alloc_skb(sizeof(*pspoll));
668 if (!skb)
669 return;
670 pspoll = skb_put(skb, sizeof(*pspoll));
671 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
672 IEEE80211_STYPE_PSPOLL |
673 IEEE80211_FCTL_PM);
674 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
675 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
676 memcpy(pspoll->ta, mac, ETH_ALEN);
678 rcu_read_lock();
679 mac80211_hwsim_tx_frame(data->hw, skb,
680 rcu_dereference(vif->chanctx_conf)->def.chan);
681 rcu_read_unlock();
684 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
685 struct ieee80211_vif *vif, int ps)
687 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
688 struct sk_buff *skb;
689 struct ieee80211_hdr *hdr;
691 if (!vp->assoc)
692 return;
694 wiphy_dbg(data->hw->wiphy,
695 "%s: send data::nullfunc to %pM ps=%d\n",
696 __func__, vp->bssid, ps);
698 skb = dev_alloc_skb(sizeof(*hdr));
699 if (!skb)
700 return;
701 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
702 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
703 IEEE80211_STYPE_NULLFUNC |
704 IEEE80211_FCTL_TODS |
705 (ps ? IEEE80211_FCTL_PM : 0));
706 hdr->duration_id = cpu_to_le16(0);
707 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
708 memcpy(hdr->addr2, mac, ETH_ALEN);
709 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
711 rcu_read_lock();
712 mac80211_hwsim_tx_frame(data->hw, skb,
713 rcu_dereference(vif->chanctx_conf)->def.chan);
714 rcu_read_unlock();
718 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
719 struct ieee80211_vif *vif)
721 struct mac80211_hwsim_data *data = dat;
722 hwsim_send_nullfunc(data, mac, vif, 1);
725 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
726 struct ieee80211_vif *vif)
728 struct mac80211_hwsim_data *data = dat;
729 hwsim_send_nullfunc(data, mac, vif, 0);
732 static int hwsim_fops_ps_read(void *dat, u64 *val)
734 struct mac80211_hwsim_data *data = dat;
735 *val = data->ps;
736 return 0;
739 static int hwsim_fops_ps_write(void *dat, u64 val)
741 struct mac80211_hwsim_data *data = dat;
742 enum ps_mode old_ps;
744 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
745 val != PS_MANUAL_POLL)
746 return -EINVAL;
748 if (val == PS_MANUAL_POLL) {
749 if (data->ps != PS_ENABLED)
750 return -EINVAL;
751 local_bh_disable();
752 ieee80211_iterate_active_interfaces_atomic(
753 data->hw, IEEE80211_IFACE_ITER_NORMAL,
754 hwsim_send_ps_poll, data);
755 local_bh_enable();
756 return 0;
758 old_ps = data->ps;
759 data->ps = val;
761 local_bh_disable();
762 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
763 ieee80211_iterate_active_interfaces_atomic(
764 data->hw, IEEE80211_IFACE_ITER_NORMAL,
765 hwsim_send_nullfunc_ps, data);
766 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
767 ieee80211_iterate_active_interfaces_atomic(
768 data->hw, IEEE80211_IFACE_ITER_NORMAL,
769 hwsim_send_nullfunc_no_ps, data);
771 local_bh_enable();
773 return 0;
776 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
777 "%llu\n");
779 static int hwsim_write_simulate_radar(void *dat, u64 val)
781 struct mac80211_hwsim_data *data = dat;
783 ieee80211_radar_detected(data->hw);
785 return 0;
788 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
789 hwsim_write_simulate_radar, "%llu\n");
791 static int hwsim_fops_group_read(void *dat, u64 *val)
793 struct mac80211_hwsim_data *data = dat;
794 *val = data->group;
795 return 0;
798 static int hwsim_fops_group_write(void *dat, u64 val)
800 struct mac80211_hwsim_data *data = dat;
801 data->group = val;
802 return 0;
805 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
806 hwsim_fops_group_read, hwsim_fops_group_write,
807 "%llx\n");
809 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
810 struct net_device *dev)
812 /* TODO: allow packet injection */
813 dev_kfree_skb(skb);
814 return NETDEV_TX_OK;
817 static inline u64 mac80211_hwsim_get_tsf_raw(void)
819 return ktime_to_us(ktime_get_real());
822 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
824 u64 now = mac80211_hwsim_get_tsf_raw();
825 return cpu_to_le64(now + data->tsf_offset);
828 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
829 struct ieee80211_vif *vif)
831 struct mac80211_hwsim_data *data = hw->priv;
832 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
835 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
836 struct ieee80211_vif *vif, u64 tsf)
838 struct mac80211_hwsim_data *data = hw->priv;
839 u64 now = mac80211_hwsim_get_tsf(hw, vif);
840 u32 bcn_int = data->beacon_int;
841 u64 delta = abs(tsf - now);
843 /* adjust after beaconing with new timestamp at old TBTT */
844 if (tsf > now) {
845 data->tsf_offset += delta;
846 data->bcn_delta = do_div(delta, bcn_int);
847 } else {
848 data->tsf_offset -= delta;
849 data->bcn_delta = -(s64)do_div(delta, bcn_int);
853 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
854 struct sk_buff *tx_skb,
855 struct ieee80211_channel *chan)
857 struct mac80211_hwsim_data *data = hw->priv;
858 struct sk_buff *skb;
859 struct hwsim_radiotap_hdr *hdr;
860 u16 flags;
861 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
862 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
864 if (WARN_ON(!txrate))
865 return;
867 if (!netif_running(hwsim_mon))
868 return;
870 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
871 if (skb == NULL)
872 return;
874 hdr = skb_push(skb, sizeof(*hdr));
875 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
876 hdr->hdr.it_pad = 0;
877 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
878 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
879 (1 << IEEE80211_RADIOTAP_RATE) |
880 (1 << IEEE80211_RADIOTAP_TSFT) |
881 (1 << IEEE80211_RADIOTAP_CHANNEL));
882 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
883 hdr->rt_flags = 0;
884 hdr->rt_rate = txrate->bitrate / 5;
885 hdr->rt_channel = cpu_to_le16(chan->center_freq);
886 flags = IEEE80211_CHAN_2GHZ;
887 if (txrate->flags & IEEE80211_RATE_ERP_G)
888 flags |= IEEE80211_CHAN_OFDM;
889 else
890 flags |= IEEE80211_CHAN_CCK;
891 hdr->rt_chbitmask = cpu_to_le16(flags);
893 skb->dev = hwsim_mon;
894 skb_reset_mac_header(skb);
895 skb->ip_summed = CHECKSUM_UNNECESSARY;
896 skb->pkt_type = PACKET_OTHERHOST;
897 skb->protocol = htons(ETH_P_802_2);
898 memset(skb->cb, 0, sizeof(skb->cb));
899 netif_rx(skb);
903 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
904 const u8 *addr)
906 struct sk_buff *skb;
907 struct hwsim_radiotap_ack_hdr *hdr;
908 u16 flags;
909 struct ieee80211_hdr *hdr11;
911 if (!netif_running(hwsim_mon))
912 return;
914 skb = dev_alloc_skb(100);
915 if (skb == NULL)
916 return;
918 hdr = skb_put(skb, sizeof(*hdr));
919 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
920 hdr->hdr.it_pad = 0;
921 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
922 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
923 (1 << IEEE80211_RADIOTAP_CHANNEL));
924 hdr->rt_flags = 0;
925 hdr->pad = 0;
926 hdr->rt_channel = cpu_to_le16(chan->center_freq);
927 flags = IEEE80211_CHAN_2GHZ;
928 hdr->rt_chbitmask = cpu_to_le16(flags);
930 hdr11 = skb_put(skb, 10);
931 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
932 IEEE80211_STYPE_ACK);
933 hdr11->duration_id = cpu_to_le16(0);
934 memcpy(hdr11->addr1, addr, ETH_ALEN);
936 skb->dev = hwsim_mon;
937 skb_reset_mac_header(skb);
938 skb->ip_summed = CHECKSUM_UNNECESSARY;
939 skb->pkt_type = PACKET_OTHERHOST;
940 skb->protocol = htons(ETH_P_802_2);
941 memset(skb->cb, 0, sizeof(skb->cb));
942 netif_rx(skb);
945 struct mac80211_hwsim_addr_match_data {
946 u8 addr[ETH_ALEN];
947 bool ret;
950 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
951 struct ieee80211_vif *vif)
953 struct mac80211_hwsim_addr_match_data *md = data;
955 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
956 md->ret = true;
959 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
960 const u8 *addr)
962 struct mac80211_hwsim_addr_match_data md = {
963 .ret = false,
966 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
967 return true;
969 memcpy(md.addr, addr, ETH_ALEN);
971 ieee80211_iterate_active_interfaces_atomic(data->hw,
972 IEEE80211_IFACE_ITER_NORMAL,
973 mac80211_hwsim_addr_iter,
974 &md);
976 return md.ret;
979 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
980 struct sk_buff *skb)
982 switch (data->ps) {
983 case PS_DISABLED:
984 return true;
985 case PS_ENABLED:
986 return false;
987 case PS_AUTO_POLL:
988 /* TODO: accept (some) Beacons by default and other frames only
989 * if pending PS-Poll has been sent */
990 return true;
991 case PS_MANUAL_POLL:
992 /* Allow unicast frames to own address if there is a pending
993 * PS-Poll */
994 if (data->ps_poll_pending &&
995 mac80211_hwsim_addr_match(data, skb->data + 4)) {
996 data->ps_poll_pending = false;
997 return true;
999 return false;
1002 return true;
1005 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1006 struct sk_buff *skb, int portid)
1008 struct net *net;
1009 bool found = false;
1010 int res = -ENOENT;
1012 rcu_read_lock();
1013 for_each_net_rcu(net) {
1014 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1015 res = genlmsg_unicast(net, skb, portid);
1016 found = true;
1017 break;
1020 rcu_read_unlock();
1022 if (!found)
1023 nlmsg_free(skb);
1025 return res;
1028 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1030 u16 result = 0;
1032 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1033 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1034 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1035 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1036 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1037 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1038 if (rate->flags & IEEE80211_TX_RC_MCS)
1039 result |= MAC80211_HWSIM_TX_RC_MCS;
1040 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1041 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1042 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1043 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1044 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1045 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1046 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1047 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1048 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1049 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1050 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1051 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1052 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1053 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1055 return result;
1058 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1059 struct sk_buff *my_skb,
1060 int dst_portid)
1062 struct sk_buff *skb;
1063 struct mac80211_hwsim_data *data = hw->priv;
1064 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1065 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1066 void *msg_head;
1067 unsigned int hwsim_flags = 0;
1068 int i;
1069 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1070 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1071 uintptr_t cookie;
1073 if (data->ps != PS_DISABLED)
1074 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1075 /* If the queue contains MAX_QUEUE skb's drop some */
1076 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1077 /* Droping until WARN_QUEUE level */
1078 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1079 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1080 data->tx_dropped++;
1084 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1085 if (skb == NULL)
1086 goto nla_put_failure;
1088 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1089 HWSIM_CMD_FRAME);
1090 if (msg_head == NULL) {
1091 pr_debug("mac80211_hwsim: problem with msg_head\n");
1092 goto nla_put_failure;
1095 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1096 ETH_ALEN, data->addresses[1].addr))
1097 goto nla_put_failure;
1099 /* We get the skb->data */
1100 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1101 goto nla_put_failure;
1103 /* We get the flags for this transmission, and we translate them to
1104 wmediumd flags */
1106 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1107 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1109 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1110 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1112 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1113 goto nla_put_failure;
1115 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1116 goto nla_put_failure;
1118 /* We get the tx control (rate and retries) info*/
1120 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1121 tx_attempts[i].idx = info->status.rates[i].idx;
1122 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1123 tx_attempts[i].count = info->status.rates[i].count;
1124 tx_attempts_flags[i].flags =
1125 trans_tx_rate_flags_ieee2hwsim(
1126 &info->status.rates[i]);
1129 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1130 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1131 tx_attempts))
1132 goto nla_put_failure;
1134 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1135 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1136 tx_attempts_flags))
1137 goto nla_put_failure;
1139 /* We create a cookie to identify this skb */
1140 data->pending_cookie++;
1141 cookie = data->pending_cookie;
1142 info->rate_driver_data[0] = (void *)cookie;
1143 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1144 goto nla_put_failure;
1146 genlmsg_end(skb, msg_head);
1147 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1148 goto err_free_txskb;
1150 /* Enqueue the packet */
1151 skb_queue_tail(&data->pending, my_skb);
1152 data->tx_pkts++;
1153 data->tx_bytes += my_skb->len;
1154 return;
1156 nla_put_failure:
1157 nlmsg_free(skb);
1158 err_free_txskb:
1159 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1160 ieee80211_free_txskb(hw, my_skb);
1161 data->tx_failed++;
1164 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1165 struct ieee80211_channel *c2)
1167 if (!c1 || !c2)
1168 return false;
1170 return c1->center_freq == c2->center_freq;
1173 struct tx_iter_data {
1174 struct ieee80211_channel *channel;
1175 bool receive;
1178 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1179 struct ieee80211_vif *vif)
1181 struct tx_iter_data *data = _data;
1183 if (!vif->chanctx_conf)
1184 return;
1186 if (!hwsim_chans_compat(data->channel,
1187 rcu_dereference(vif->chanctx_conf)->def.chan))
1188 return;
1190 data->receive = true;
1193 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1196 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1197 * e.g. like this:
1198 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1199 * (but you should use a valid OUI, not that)
1201 * If anyone wants to 'donate' a radiotap OUI/subns code
1202 * please send a patch removing this #ifdef and changing
1203 * the values accordingly.
1205 #ifdef HWSIM_RADIOTAP_OUI
1206 struct ieee80211_vendor_radiotap *rtap;
1209 * Note that this code requires the headroom in the SKB
1210 * that was allocated earlier.
1212 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1213 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1214 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1215 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1216 rtap->subns = 127;
1219 * Radiotap vendor namespaces can (and should) also be
1220 * split into fields by using the standard radiotap
1221 * presence bitmap mechanism. Use just BIT(0) here for
1222 * the presence bitmap.
1224 rtap->present = BIT(0);
1225 /* We have 8 bytes of (dummy) data */
1226 rtap->len = 8;
1227 /* For testing, also require it to be aligned */
1228 rtap->align = 8;
1229 /* And also test that padding works, 4 bytes */
1230 rtap->pad = 4;
1231 /* push the data */
1232 memcpy(rtap->data, "ABCDEFGH", 8);
1233 /* make sure to clear padding, mac80211 doesn't */
1234 memset(rtap->data + 8, 0, 4);
1236 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1237 #endif
1240 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1241 struct sk_buff *skb,
1242 struct ieee80211_channel *chan)
1244 struct mac80211_hwsim_data *data = hw->priv, *data2;
1245 bool ack = false;
1246 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1247 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1248 struct ieee80211_rx_status rx_status;
1249 u64 now;
1251 memset(&rx_status, 0, sizeof(rx_status));
1252 rx_status.flag |= RX_FLAG_MACTIME_START;
1253 rx_status.freq = chan->center_freq;
1254 rx_status.band = chan->band;
1255 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1256 rx_status.rate_idx =
1257 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1258 rx_status.nss =
1259 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1260 rx_status.encoding = RX_ENC_VHT;
1261 } else {
1262 rx_status.rate_idx = info->control.rates[0].idx;
1263 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1264 rx_status.encoding = RX_ENC_HT;
1266 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1267 rx_status.bw = RATE_INFO_BW_40;
1268 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1269 rx_status.bw = RATE_INFO_BW_80;
1270 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1271 rx_status.bw = RATE_INFO_BW_160;
1272 else
1273 rx_status.bw = RATE_INFO_BW_20;
1274 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1275 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1276 /* TODO: simulate real signal strength (and optional packet loss) */
1277 rx_status.signal = -50;
1278 if (info->control.vif)
1279 rx_status.signal += info->control.vif->bss_conf.txpower;
1281 if (data->ps != PS_DISABLED)
1282 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1284 /* release the skb's source info */
1285 skb_orphan(skb);
1286 skb_dst_drop(skb);
1287 skb->mark = 0;
1288 secpath_reset(skb);
1289 nf_reset(skb);
1292 * Get absolute mactime here so all HWs RX at the "same time", and
1293 * absolute TX time for beacon mactime so the timestamp matches.
1294 * Giving beacons a different mactime than non-beacons looks messy, but
1295 * it helps the Toffset be exact and a ~10us mactime discrepancy
1296 * probably doesn't really matter.
1298 if (ieee80211_is_beacon(hdr->frame_control) ||
1299 ieee80211_is_probe_resp(hdr->frame_control))
1300 now = data->abs_bcn_ts;
1301 else
1302 now = mac80211_hwsim_get_tsf_raw();
1304 /* Copy skb to all enabled radios that are on the current frequency */
1305 spin_lock(&hwsim_radio_lock);
1306 list_for_each_entry(data2, &hwsim_radios, list) {
1307 struct sk_buff *nskb;
1308 struct tx_iter_data tx_iter_data = {
1309 .receive = false,
1310 .channel = chan,
1313 if (data == data2)
1314 continue;
1316 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1317 !hwsim_ps_rx_ok(data2, skb))
1318 continue;
1320 if (!(data->group & data2->group))
1321 continue;
1323 if (data->netgroup != data2->netgroup)
1324 continue;
1326 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1327 !hwsim_chans_compat(chan, data2->channel)) {
1328 ieee80211_iterate_active_interfaces_atomic(
1329 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1330 mac80211_hwsim_tx_iter, &tx_iter_data);
1331 if (!tx_iter_data.receive)
1332 continue;
1336 * reserve some space for our vendor and the normal
1337 * radiotap header, since we're copying anyway
1339 if (skb->len < PAGE_SIZE && paged_rx) {
1340 struct page *page = alloc_page(GFP_ATOMIC);
1342 if (!page)
1343 continue;
1345 nskb = dev_alloc_skb(128);
1346 if (!nskb) {
1347 __free_page(page);
1348 continue;
1351 memcpy(page_address(page), skb->data, skb->len);
1352 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1353 } else {
1354 nskb = skb_copy(skb, GFP_ATOMIC);
1355 if (!nskb)
1356 continue;
1359 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1360 ack = true;
1362 rx_status.mactime = now + data2->tsf_offset;
1364 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1366 mac80211_hwsim_add_vendor_rtap(nskb);
1368 data2->rx_pkts++;
1369 data2->rx_bytes += nskb->len;
1370 ieee80211_rx_irqsafe(data2->hw, nskb);
1372 spin_unlock(&hwsim_radio_lock);
1374 return ack;
1377 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1378 struct ieee80211_tx_control *control,
1379 struct sk_buff *skb)
1381 struct mac80211_hwsim_data *data = hw->priv;
1382 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1383 struct ieee80211_hdr *hdr = (void *)skb->data;
1384 struct ieee80211_chanctx_conf *chanctx_conf;
1385 struct ieee80211_channel *channel;
1386 bool ack;
1387 u32 _portid;
1389 if (WARN_ON(skb->len < 10)) {
1390 /* Should not happen; just a sanity check for addr1 use */
1391 ieee80211_free_txskb(hw, skb);
1392 return;
1395 if (!data->use_chanctx) {
1396 channel = data->channel;
1397 } else if (txi->hw_queue == 4) {
1398 channel = data->tmp_chan;
1399 } else {
1400 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1401 if (chanctx_conf)
1402 channel = chanctx_conf->def.chan;
1403 else
1404 channel = NULL;
1407 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1408 ieee80211_free_txskb(hw, skb);
1409 return;
1412 if (data->idle && !data->tmp_chan) {
1413 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1414 ieee80211_free_txskb(hw, skb);
1415 return;
1418 if (txi->control.vif)
1419 hwsim_check_magic(txi->control.vif);
1420 if (control->sta)
1421 hwsim_check_sta_magic(control->sta);
1423 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1424 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1425 txi->control.rates,
1426 ARRAY_SIZE(txi->control.rates));
1428 if (skb->len >= 24 + 8 &&
1429 ieee80211_is_probe_resp(hdr->frame_control)) {
1430 /* fake header transmission time */
1431 struct ieee80211_mgmt *mgmt;
1432 struct ieee80211_rate *txrate;
1433 u64 ts;
1435 mgmt = (struct ieee80211_mgmt *)skb->data;
1436 txrate = ieee80211_get_tx_rate(hw, txi);
1437 ts = mac80211_hwsim_get_tsf_raw();
1438 mgmt->u.probe_resp.timestamp =
1439 cpu_to_le64(ts + data->tsf_offset +
1440 24 * 8 * 10 / txrate->bitrate);
1443 mac80211_hwsim_monitor_rx(hw, skb, channel);
1445 /* wmediumd mode check */
1446 _portid = READ_ONCE(data->wmediumd);
1448 if (_portid)
1449 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1451 /* NO wmediumd detected, perfect medium simulation */
1452 data->tx_pkts++;
1453 data->tx_bytes += skb->len;
1454 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1456 if (ack && skb->len >= 16)
1457 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1459 ieee80211_tx_info_clear_status(txi);
1461 /* frame was transmitted at most favorable rate at first attempt */
1462 txi->control.rates[0].count = 1;
1463 txi->control.rates[1].idx = -1;
1465 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1466 txi->flags |= IEEE80211_TX_STAT_ACK;
1467 ieee80211_tx_status_irqsafe(hw, skb);
1471 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1473 struct mac80211_hwsim_data *data = hw->priv;
1474 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1475 data->started = true;
1476 return 0;
1480 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1482 struct mac80211_hwsim_data *data = hw->priv;
1483 data->started = false;
1484 tasklet_hrtimer_cancel(&data->beacon_timer);
1485 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1489 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1490 struct ieee80211_vif *vif)
1492 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1493 __func__, ieee80211_vif_type_p2p(vif),
1494 vif->addr);
1495 hwsim_set_magic(vif);
1497 vif->cab_queue = 0;
1498 vif->hw_queue[IEEE80211_AC_VO] = 0;
1499 vif->hw_queue[IEEE80211_AC_VI] = 1;
1500 vif->hw_queue[IEEE80211_AC_BE] = 2;
1501 vif->hw_queue[IEEE80211_AC_BK] = 3;
1503 return 0;
1507 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1508 struct ieee80211_vif *vif,
1509 enum nl80211_iftype newtype,
1510 bool newp2p)
1512 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1513 wiphy_dbg(hw->wiphy,
1514 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1515 __func__, ieee80211_vif_type_p2p(vif),
1516 newtype, vif->addr);
1517 hwsim_check_magic(vif);
1520 * interface may change from non-AP to AP in
1521 * which case this needs to be set up again
1523 vif->cab_queue = 0;
1525 return 0;
1528 static void mac80211_hwsim_remove_interface(
1529 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1531 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1532 __func__, ieee80211_vif_type_p2p(vif),
1533 vif->addr);
1534 hwsim_check_magic(vif);
1535 hwsim_clear_magic(vif);
1538 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1539 struct sk_buff *skb,
1540 struct ieee80211_channel *chan)
1542 struct mac80211_hwsim_data *data = hw->priv;
1543 u32 _pid = READ_ONCE(data->wmediumd);
1545 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1546 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1547 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1548 txi->control.rates,
1549 ARRAY_SIZE(txi->control.rates));
1552 mac80211_hwsim_monitor_rx(hw, skb, chan);
1554 if (_pid)
1555 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1557 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1558 dev_kfree_skb(skb);
1561 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1562 struct ieee80211_vif *vif)
1564 struct mac80211_hwsim_data *data = arg;
1565 struct ieee80211_hw *hw = data->hw;
1566 struct ieee80211_tx_info *info;
1567 struct ieee80211_rate *txrate;
1568 struct ieee80211_mgmt *mgmt;
1569 struct sk_buff *skb;
1571 hwsim_check_magic(vif);
1573 if (vif->type != NL80211_IFTYPE_AP &&
1574 vif->type != NL80211_IFTYPE_MESH_POINT &&
1575 vif->type != NL80211_IFTYPE_ADHOC)
1576 return;
1578 skb = ieee80211_beacon_get(hw, vif);
1579 if (skb == NULL)
1580 return;
1581 info = IEEE80211_SKB_CB(skb);
1582 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1583 ieee80211_get_tx_rates(vif, NULL, skb,
1584 info->control.rates,
1585 ARRAY_SIZE(info->control.rates));
1587 txrate = ieee80211_get_tx_rate(hw, info);
1589 mgmt = (struct ieee80211_mgmt *) skb->data;
1590 /* fake header transmission time */
1591 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1592 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1593 data->tsf_offset +
1594 24 * 8 * 10 / txrate->bitrate);
1596 mac80211_hwsim_tx_frame(hw, skb,
1597 rcu_dereference(vif->chanctx_conf)->def.chan);
1599 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1600 ieee80211_csa_finish(vif);
1603 static enum hrtimer_restart
1604 mac80211_hwsim_beacon(struct hrtimer *timer)
1606 struct mac80211_hwsim_data *data =
1607 container_of(timer, struct mac80211_hwsim_data,
1608 beacon_timer.timer);
1609 struct ieee80211_hw *hw = data->hw;
1610 u64 bcn_int = data->beacon_int;
1611 ktime_t next_bcn;
1613 if (!data->started)
1614 goto out;
1616 ieee80211_iterate_active_interfaces_atomic(
1617 hw, IEEE80211_IFACE_ITER_NORMAL,
1618 mac80211_hwsim_beacon_tx, data);
1620 /* beacon at new TBTT + beacon interval */
1621 if (data->bcn_delta) {
1622 bcn_int -= data->bcn_delta;
1623 data->bcn_delta = 0;
1626 next_bcn = ktime_add(hrtimer_get_expires(timer),
1627 ns_to_ktime(bcn_int * 1000));
1628 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1629 out:
1630 return HRTIMER_NORESTART;
1633 static const char * const hwsim_chanwidths[] = {
1634 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1635 [NL80211_CHAN_WIDTH_20] = "ht20",
1636 [NL80211_CHAN_WIDTH_40] = "ht40",
1637 [NL80211_CHAN_WIDTH_80] = "vht80",
1638 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1639 [NL80211_CHAN_WIDTH_160] = "vht160",
1642 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1644 struct mac80211_hwsim_data *data = hw->priv;
1645 struct ieee80211_conf *conf = &hw->conf;
1646 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1647 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1648 [IEEE80211_SMPS_OFF] = "off",
1649 [IEEE80211_SMPS_STATIC] = "static",
1650 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1652 int idx;
1654 if (conf->chandef.chan)
1655 wiphy_dbg(hw->wiphy,
1656 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1657 __func__,
1658 conf->chandef.chan->center_freq,
1659 conf->chandef.center_freq1,
1660 conf->chandef.center_freq2,
1661 hwsim_chanwidths[conf->chandef.width],
1662 !!(conf->flags & IEEE80211_CONF_IDLE),
1663 !!(conf->flags & IEEE80211_CONF_PS),
1664 smps_modes[conf->smps_mode]);
1665 else
1666 wiphy_dbg(hw->wiphy,
1667 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1668 __func__,
1669 !!(conf->flags & IEEE80211_CONF_IDLE),
1670 !!(conf->flags & IEEE80211_CONF_PS),
1671 smps_modes[conf->smps_mode]);
1673 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1675 WARN_ON(conf->chandef.chan && data->use_chanctx);
1677 mutex_lock(&data->mutex);
1678 if (data->scanning && conf->chandef.chan) {
1679 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1680 if (data->survey_data[idx].channel == data->channel) {
1681 data->survey_data[idx].start =
1682 data->survey_data[idx].next_start;
1683 data->survey_data[idx].end = jiffies;
1684 break;
1688 data->channel = conf->chandef.chan;
1690 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1691 if (data->survey_data[idx].channel &&
1692 data->survey_data[idx].channel != data->channel)
1693 continue;
1694 data->survey_data[idx].channel = data->channel;
1695 data->survey_data[idx].next_start = jiffies;
1696 break;
1698 } else {
1699 data->channel = conf->chandef.chan;
1701 mutex_unlock(&data->mutex);
1703 if (!data->started || !data->beacon_int)
1704 tasklet_hrtimer_cancel(&data->beacon_timer);
1705 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
1706 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1707 u32 bcn_int = data->beacon_int;
1708 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1710 tasklet_hrtimer_start(&data->beacon_timer,
1711 ns_to_ktime(until_tbtt * 1000),
1712 HRTIMER_MODE_REL);
1715 return 0;
1719 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1720 unsigned int changed_flags,
1721 unsigned int *total_flags,u64 multicast)
1723 struct mac80211_hwsim_data *data = hw->priv;
1725 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1727 data->rx_filter = 0;
1728 if (*total_flags & FIF_ALLMULTI)
1729 data->rx_filter |= FIF_ALLMULTI;
1731 *total_flags = data->rx_filter;
1734 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1735 struct ieee80211_vif *vif)
1737 unsigned int *count = data;
1738 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1740 if (vp->bcn_en)
1741 (*count)++;
1744 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1745 struct ieee80211_vif *vif,
1746 struct ieee80211_bss_conf *info,
1747 u32 changed)
1749 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1750 struct mac80211_hwsim_data *data = hw->priv;
1752 hwsim_check_magic(vif);
1754 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1755 __func__, changed, vif->addr);
1757 if (changed & BSS_CHANGED_BSSID) {
1758 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1759 __func__, info->bssid);
1760 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1763 if (changed & BSS_CHANGED_ASSOC) {
1764 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1765 info->assoc, info->aid);
1766 vp->assoc = info->assoc;
1767 vp->aid = info->aid;
1770 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1771 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1772 info->enable_beacon, info->beacon_int);
1773 vp->bcn_en = info->enable_beacon;
1774 if (data->started &&
1775 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1776 info->enable_beacon) {
1777 u64 tsf, until_tbtt;
1778 u32 bcn_int;
1779 data->beacon_int = info->beacon_int * 1024;
1780 tsf = mac80211_hwsim_get_tsf(hw, vif);
1781 bcn_int = data->beacon_int;
1782 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1783 tasklet_hrtimer_start(&data->beacon_timer,
1784 ns_to_ktime(until_tbtt * 1000),
1785 HRTIMER_MODE_REL);
1786 } else if (!info->enable_beacon) {
1787 unsigned int count = 0;
1788 ieee80211_iterate_active_interfaces_atomic(
1789 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1790 mac80211_hwsim_bcn_en_iter, &count);
1791 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1792 count);
1793 if (count == 0) {
1794 tasklet_hrtimer_cancel(&data->beacon_timer);
1795 data->beacon_int = 0;
1800 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1801 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1802 info->use_cts_prot);
1805 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1806 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1807 info->use_short_preamble);
1810 if (changed & BSS_CHANGED_ERP_SLOT) {
1811 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1814 if (changed & BSS_CHANGED_HT) {
1815 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1816 info->ht_operation_mode);
1819 if (changed & BSS_CHANGED_BASIC_RATES) {
1820 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1821 (unsigned long long) info->basic_rates);
1824 if (changed & BSS_CHANGED_TXPOWER)
1825 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1828 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1829 struct ieee80211_vif *vif,
1830 struct ieee80211_sta *sta)
1832 hwsim_check_magic(vif);
1833 hwsim_set_sta_magic(sta);
1835 return 0;
1838 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1839 struct ieee80211_vif *vif,
1840 struct ieee80211_sta *sta)
1842 hwsim_check_magic(vif);
1843 hwsim_clear_sta_magic(sta);
1845 return 0;
1848 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1849 struct ieee80211_vif *vif,
1850 enum sta_notify_cmd cmd,
1851 struct ieee80211_sta *sta)
1853 hwsim_check_magic(vif);
1855 switch (cmd) {
1856 case STA_NOTIFY_SLEEP:
1857 case STA_NOTIFY_AWAKE:
1858 /* TODO: make good use of these flags */
1859 break;
1860 default:
1861 WARN(1, "Invalid sta notify: %d\n", cmd);
1862 break;
1866 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1867 struct ieee80211_sta *sta,
1868 bool set)
1870 hwsim_check_sta_magic(sta);
1871 return 0;
1874 static int mac80211_hwsim_conf_tx(
1875 struct ieee80211_hw *hw,
1876 struct ieee80211_vif *vif, u16 queue,
1877 const struct ieee80211_tx_queue_params *params)
1879 wiphy_dbg(hw->wiphy,
1880 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1881 __func__, queue,
1882 params->txop, params->cw_min,
1883 params->cw_max, params->aifs);
1884 return 0;
1887 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1888 struct survey_info *survey)
1890 struct mac80211_hwsim_data *hwsim = hw->priv;
1892 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
1893 return -ENOENT;
1895 mutex_lock(&hwsim->mutex);
1896 survey->channel = hwsim->survey_data[idx].channel;
1897 if (!survey->channel) {
1898 mutex_unlock(&hwsim->mutex);
1899 return -ENOENT;
1903 * Magically conjured dummy values --- this is only ok for simulated hardware.
1905 * A real driver which cannot determine real values noise MUST NOT
1906 * report any, especially not a magically conjured ones :-)
1908 survey->filled = SURVEY_INFO_NOISE_DBM |
1909 SURVEY_INFO_TIME |
1910 SURVEY_INFO_TIME_BUSY;
1911 survey->noise = -92;
1912 survey->time =
1913 jiffies_to_msecs(hwsim->survey_data[idx].end -
1914 hwsim->survey_data[idx].start);
1915 /* report 12.5% of channel time is used */
1916 survey->time_busy = survey->time/8;
1917 mutex_unlock(&hwsim->mutex);
1919 return 0;
1922 #ifdef CONFIG_NL80211_TESTMODE
1924 * This section contains example code for using netlink
1925 * attributes with the testmode command in nl80211.
1928 /* These enums need to be kept in sync with userspace */
1929 enum hwsim_testmode_attr {
1930 __HWSIM_TM_ATTR_INVALID = 0,
1931 HWSIM_TM_ATTR_CMD = 1,
1932 HWSIM_TM_ATTR_PS = 2,
1934 /* keep last */
1935 __HWSIM_TM_ATTR_AFTER_LAST,
1936 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1939 enum hwsim_testmode_cmd {
1940 HWSIM_TM_CMD_SET_PS = 0,
1941 HWSIM_TM_CMD_GET_PS = 1,
1942 HWSIM_TM_CMD_STOP_QUEUES = 2,
1943 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1946 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1947 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1948 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1951 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1952 struct ieee80211_vif *vif,
1953 void *data, int len)
1955 struct mac80211_hwsim_data *hwsim = hw->priv;
1956 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1957 struct sk_buff *skb;
1958 int err, ps;
1960 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1961 hwsim_testmode_policy, NULL);
1962 if (err)
1963 return err;
1965 if (!tb[HWSIM_TM_ATTR_CMD])
1966 return -EINVAL;
1968 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1969 case HWSIM_TM_CMD_SET_PS:
1970 if (!tb[HWSIM_TM_ATTR_PS])
1971 return -EINVAL;
1972 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1973 return hwsim_fops_ps_write(hwsim, ps);
1974 case HWSIM_TM_CMD_GET_PS:
1975 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1976 nla_total_size(sizeof(u32)));
1977 if (!skb)
1978 return -ENOMEM;
1979 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1980 goto nla_put_failure;
1981 return cfg80211_testmode_reply(skb);
1982 case HWSIM_TM_CMD_STOP_QUEUES:
1983 ieee80211_stop_queues(hw);
1984 return 0;
1985 case HWSIM_TM_CMD_WAKE_QUEUES:
1986 ieee80211_wake_queues(hw);
1987 return 0;
1988 default:
1989 return -EOPNOTSUPP;
1992 nla_put_failure:
1993 kfree_skb(skb);
1994 return -ENOBUFS;
1996 #endif
1998 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1999 struct ieee80211_vif *vif,
2000 struct ieee80211_ampdu_params *params)
2002 struct ieee80211_sta *sta = params->sta;
2003 enum ieee80211_ampdu_mlme_action action = params->action;
2004 u16 tid = params->tid;
2006 switch (action) {
2007 case IEEE80211_AMPDU_TX_START:
2008 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2009 break;
2010 case IEEE80211_AMPDU_TX_STOP_CONT:
2011 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2012 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2013 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2014 break;
2015 case IEEE80211_AMPDU_TX_OPERATIONAL:
2016 break;
2017 case IEEE80211_AMPDU_RX_START:
2018 case IEEE80211_AMPDU_RX_STOP:
2019 break;
2020 default:
2021 return -EOPNOTSUPP;
2024 return 0;
2027 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2028 struct ieee80211_vif *vif,
2029 u32 queues, bool drop)
2031 /* Not implemented, queues only on kernel side */
2034 static void hw_scan_work(struct work_struct *work)
2036 struct mac80211_hwsim_data *hwsim =
2037 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2038 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2039 int dwell, i;
2041 mutex_lock(&hwsim->mutex);
2042 if (hwsim->scan_chan_idx >= req->n_channels) {
2043 struct cfg80211_scan_info info = {
2044 .aborted = false,
2047 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2048 ieee80211_scan_completed(hwsim->hw, &info);
2049 hwsim->hw_scan_request = NULL;
2050 hwsim->hw_scan_vif = NULL;
2051 hwsim->tmp_chan = NULL;
2052 mutex_unlock(&hwsim->mutex);
2053 return;
2056 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2057 req->channels[hwsim->scan_chan_idx]->center_freq);
2059 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2060 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2061 IEEE80211_CHAN_RADAR) ||
2062 !req->n_ssids) {
2063 dwell = 120;
2064 } else {
2065 dwell = 30;
2066 /* send probes */
2067 for (i = 0; i < req->n_ssids; i++) {
2068 struct sk_buff *probe;
2069 struct ieee80211_mgmt *mgmt;
2071 probe = ieee80211_probereq_get(hwsim->hw,
2072 hwsim->scan_addr,
2073 req->ssids[i].ssid,
2074 req->ssids[i].ssid_len,
2075 req->ie_len);
2076 if (!probe)
2077 continue;
2079 mgmt = (struct ieee80211_mgmt *) probe->data;
2080 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2081 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2083 if (req->ie_len)
2084 skb_put_data(probe, req->ie, req->ie_len);
2086 local_bh_disable();
2087 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2088 hwsim->tmp_chan);
2089 local_bh_enable();
2092 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2093 msecs_to_jiffies(dwell));
2094 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2095 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2096 hwsim->survey_data[hwsim->scan_chan_idx].end =
2097 jiffies + msecs_to_jiffies(dwell);
2098 hwsim->scan_chan_idx++;
2099 mutex_unlock(&hwsim->mutex);
2102 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2103 struct ieee80211_vif *vif,
2104 struct ieee80211_scan_request *hw_req)
2106 struct mac80211_hwsim_data *hwsim = hw->priv;
2107 struct cfg80211_scan_request *req = &hw_req->req;
2109 mutex_lock(&hwsim->mutex);
2110 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2111 mutex_unlock(&hwsim->mutex);
2112 return -EBUSY;
2114 hwsim->hw_scan_request = req;
2115 hwsim->hw_scan_vif = vif;
2116 hwsim->scan_chan_idx = 0;
2117 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2118 get_random_mask_addr(hwsim->scan_addr,
2119 hw_req->req.mac_addr,
2120 hw_req->req.mac_addr_mask);
2121 else
2122 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2123 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2124 mutex_unlock(&hwsim->mutex);
2126 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2128 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2130 return 0;
2133 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2134 struct ieee80211_vif *vif)
2136 struct mac80211_hwsim_data *hwsim = hw->priv;
2137 struct cfg80211_scan_info info = {
2138 .aborted = true,
2141 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2143 cancel_delayed_work_sync(&hwsim->hw_scan);
2145 mutex_lock(&hwsim->mutex);
2146 ieee80211_scan_completed(hwsim->hw, &info);
2147 hwsim->tmp_chan = NULL;
2148 hwsim->hw_scan_request = NULL;
2149 hwsim->hw_scan_vif = NULL;
2150 mutex_unlock(&hwsim->mutex);
2153 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2154 struct ieee80211_vif *vif,
2155 const u8 *mac_addr)
2157 struct mac80211_hwsim_data *hwsim = hw->priv;
2159 mutex_lock(&hwsim->mutex);
2161 if (hwsim->scanning) {
2162 pr_debug("two hwsim sw_scans detected!\n");
2163 goto out;
2166 pr_debug("hwsim sw_scan request, prepping stuff\n");
2168 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2169 hwsim->scanning = true;
2170 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2172 out:
2173 mutex_unlock(&hwsim->mutex);
2176 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2177 struct ieee80211_vif *vif)
2179 struct mac80211_hwsim_data *hwsim = hw->priv;
2181 mutex_lock(&hwsim->mutex);
2183 pr_debug("hwsim sw_scan_complete\n");
2184 hwsim->scanning = false;
2185 eth_zero_addr(hwsim->scan_addr);
2187 mutex_unlock(&hwsim->mutex);
2190 static void hw_roc_start(struct work_struct *work)
2192 struct mac80211_hwsim_data *hwsim =
2193 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2195 mutex_lock(&hwsim->mutex);
2197 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2198 hwsim->tmp_chan = hwsim->roc_chan;
2199 ieee80211_ready_on_channel(hwsim->hw);
2201 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2202 msecs_to_jiffies(hwsim->roc_duration));
2204 mutex_unlock(&hwsim->mutex);
2207 static void hw_roc_done(struct work_struct *work)
2209 struct mac80211_hwsim_data *hwsim =
2210 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2212 mutex_lock(&hwsim->mutex);
2213 ieee80211_remain_on_channel_expired(hwsim->hw);
2214 hwsim->tmp_chan = NULL;
2215 mutex_unlock(&hwsim->mutex);
2217 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2220 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2221 struct ieee80211_vif *vif,
2222 struct ieee80211_channel *chan,
2223 int duration,
2224 enum ieee80211_roc_type type)
2226 struct mac80211_hwsim_data *hwsim = hw->priv;
2228 mutex_lock(&hwsim->mutex);
2229 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2230 mutex_unlock(&hwsim->mutex);
2231 return -EBUSY;
2234 hwsim->roc_chan = chan;
2235 hwsim->roc_duration = duration;
2236 mutex_unlock(&hwsim->mutex);
2238 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2239 chan->center_freq, duration);
2240 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2242 return 0;
2245 static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2247 struct mac80211_hwsim_data *hwsim = hw->priv;
2249 cancel_delayed_work_sync(&hwsim->roc_start);
2250 cancel_delayed_work_sync(&hwsim->roc_done);
2252 mutex_lock(&hwsim->mutex);
2253 hwsim->tmp_chan = NULL;
2254 mutex_unlock(&hwsim->mutex);
2256 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2258 return 0;
2261 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2262 struct ieee80211_chanctx_conf *ctx)
2264 hwsim_set_chanctx_magic(ctx);
2265 wiphy_dbg(hw->wiphy,
2266 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2267 ctx->def.chan->center_freq, ctx->def.width,
2268 ctx->def.center_freq1, ctx->def.center_freq2);
2269 return 0;
2272 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2273 struct ieee80211_chanctx_conf *ctx)
2275 wiphy_dbg(hw->wiphy,
2276 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2277 ctx->def.chan->center_freq, ctx->def.width,
2278 ctx->def.center_freq1, ctx->def.center_freq2);
2279 hwsim_check_chanctx_magic(ctx);
2280 hwsim_clear_chanctx_magic(ctx);
2283 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2284 struct ieee80211_chanctx_conf *ctx,
2285 u32 changed)
2287 hwsim_check_chanctx_magic(ctx);
2288 wiphy_dbg(hw->wiphy,
2289 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2290 ctx->def.chan->center_freq, ctx->def.width,
2291 ctx->def.center_freq1, ctx->def.center_freq2);
2294 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2295 struct ieee80211_vif *vif,
2296 struct ieee80211_chanctx_conf *ctx)
2298 hwsim_check_magic(vif);
2299 hwsim_check_chanctx_magic(ctx);
2301 return 0;
2304 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2305 struct ieee80211_vif *vif,
2306 struct ieee80211_chanctx_conf *ctx)
2308 hwsim_check_magic(vif);
2309 hwsim_check_chanctx_magic(ctx);
2312 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2313 "tx_pkts_nic",
2314 "tx_bytes_nic",
2315 "rx_pkts_nic",
2316 "rx_bytes_nic",
2317 "d_tx_dropped",
2318 "d_tx_failed",
2319 "d_ps_mode",
2320 "d_group",
2323 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2325 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2326 struct ieee80211_vif *vif,
2327 u32 sset, u8 *data)
2329 if (sset == ETH_SS_STATS)
2330 memcpy(data, *mac80211_hwsim_gstrings_stats,
2331 sizeof(mac80211_hwsim_gstrings_stats));
2334 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2335 struct ieee80211_vif *vif, int sset)
2337 if (sset == ETH_SS_STATS)
2338 return MAC80211_HWSIM_SSTATS_LEN;
2339 return 0;
2342 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2343 struct ieee80211_vif *vif,
2344 struct ethtool_stats *stats, u64 *data)
2346 struct mac80211_hwsim_data *ar = hw->priv;
2347 int i = 0;
2349 data[i++] = ar->tx_pkts;
2350 data[i++] = ar->tx_bytes;
2351 data[i++] = ar->rx_pkts;
2352 data[i++] = ar->rx_bytes;
2353 data[i++] = ar->tx_dropped;
2354 data[i++] = ar->tx_failed;
2355 data[i++] = ar->ps;
2356 data[i++] = ar->group;
2358 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2361 #define HWSIM_COMMON_OPS \
2362 .tx = mac80211_hwsim_tx, \
2363 .start = mac80211_hwsim_start, \
2364 .stop = mac80211_hwsim_stop, \
2365 .add_interface = mac80211_hwsim_add_interface, \
2366 .change_interface = mac80211_hwsim_change_interface, \
2367 .remove_interface = mac80211_hwsim_remove_interface, \
2368 .config = mac80211_hwsim_config, \
2369 .configure_filter = mac80211_hwsim_configure_filter, \
2370 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2371 .sta_add = mac80211_hwsim_sta_add, \
2372 .sta_remove = mac80211_hwsim_sta_remove, \
2373 .sta_notify = mac80211_hwsim_sta_notify, \
2374 .set_tim = mac80211_hwsim_set_tim, \
2375 .conf_tx = mac80211_hwsim_conf_tx, \
2376 .get_survey = mac80211_hwsim_get_survey, \
2377 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2378 .ampdu_action = mac80211_hwsim_ampdu_action, \
2379 .flush = mac80211_hwsim_flush, \
2380 .get_tsf = mac80211_hwsim_get_tsf, \
2381 .set_tsf = mac80211_hwsim_set_tsf, \
2382 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2383 .get_et_stats = mac80211_hwsim_get_et_stats, \
2384 .get_et_strings = mac80211_hwsim_get_et_strings,
2386 static const struct ieee80211_ops mac80211_hwsim_ops = {
2387 HWSIM_COMMON_OPS
2388 .sw_scan_start = mac80211_hwsim_sw_scan,
2389 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2392 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2393 HWSIM_COMMON_OPS
2394 .hw_scan = mac80211_hwsim_hw_scan,
2395 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2396 .sw_scan_start = NULL,
2397 .sw_scan_complete = NULL,
2398 .remain_on_channel = mac80211_hwsim_roc,
2399 .cancel_remain_on_channel = mac80211_hwsim_croc,
2400 .add_chanctx = mac80211_hwsim_add_chanctx,
2401 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2402 .change_chanctx = mac80211_hwsim_change_chanctx,
2403 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2404 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2407 struct hwsim_new_radio_params {
2408 unsigned int channels;
2409 const char *reg_alpha2;
2410 const struct ieee80211_regdomain *regd;
2411 bool reg_strict;
2412 bool p2p_device;
2413 bool use_chanctx;
2414 bool destroy_on_close;
2415 const char *hwname;
2416 bool no_vif;
2417 const u8 *perm_addr;
2420 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2421 struct genl_info *info)
2423 if (info)
2424 genl_notify(&hwsim_genl_family, mcast_skb, info,
2425 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2426 else
2427 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2428 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2431 static int append_radio_msg(struct sk_buff *skb, int id,
2432 struct hwsim_new_radio_params *param)
2434 int ret;
2436 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2437 if (ret < 0)
2438 return ret;
2440 if (param->channels) {
2441 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2442 if (ret < 0)
2443 return ret;
2446 if (param->reg_alpha2) {
2447 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2448 param->reg_alpha2);
2449 if (ret < 0)
2450 return ret;
2453 if (param->regd) {
2454 int i;
2456 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2457 if (hwsim_world_regdom_custom[i] != param->regd)
2458 continue;
2460 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2461 if (ret < 0)
2462 return ret;
2463 break;
2467 if (param->reg_strict) {
2468 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2469 if (ret < 0)
2470 return ret;
2473 if (param->p2p_device) {
2474 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2475 if (ret < 0)
2476 return ret;
2479 if (param->use_chanctx) {
2480 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2481 if (ret < 0)
2482 return ret;
2485 if (param->hwname) {
2486 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2487 strlen(param->hwname), param->hwname);
2488 if (ret < 0)
2489 return ret;
2492 return 0;
2495 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2496 struct hwsim_new_radio_params *param)
2498 struct sk_buff *mcast_skb;
2499 void *data;
2501 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2502 if (!mcast_skb)
2503 return;
2505 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2506 HWSIM_CMD_NEW_RADIO);
2507 if (!data)
2508 goto out_err;
2510 if (append_radio_msg(mcast_skb, id, param) < 0)
2511 goto out_err;
2513 genlmsg_end(mcast_skb, data);
2515 hwsim_mcast_config_msg(mcast_skb, info);
2516 return;
2518 out_err:
2519 nlmsg_free(mcast_skb);
2522 static const struct ieee80211_sband_iftype_data he_capa_2ghz = {
2523 /* TODO: should we support other types, e.g., P2P?*/
2524 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2525 .he_cap = {
2526 .has_he = true,
2527 .he_cap_elem = {
2528 .mac_cap_info[0] =
2529 IEEE80211_HE_MAC_CAP0_HTC_HE,
2530 .mac_cap_info[1] =
2531 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2532 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
2533 .mac_cap_info[2] =
2534 IEEE80211_HE_MAC_CAP2_BSR |
2535 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2536 IEEE80211_HE_MAC_CAP2_ACK_EN,
2537 .mac_cap_info[3] =
2538 IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
2539 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2540 IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
2541 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2542 .phy_cap_info[0] =
2543 IEEE80211_HE_PHY_CAP0_DUAL_BAND,
2544 .phy_cap_info[1] =
2545 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2546 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2547 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2548 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
2549 .phy_cap_info[2] =
2550 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2551 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2552 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2553 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2554 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2556 /* Leave all the other PHY capability bytes unset, as
2557 * DCM, beam forming, RU and PPE threshold information
2558 * are not supported
2561 .he_mcs_nss_supp = {
2562 .rx_mcs_80 = cpu_to_le16(0xfffa),
2563 .tx_mcs_80 = cpu_to_le16(0xfffa),
2564 .rx_mcs_160 = cpu_to_le16(0xffff),
2565 .tx_mcs_160 = cpu_to_le16(0xffff),
2566 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2567 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2572 static const struct ieee80211_sband_iftype_data he_capa_5ghz = {
2573 /* TODO: should we support other types, e.g., P2P?*/
2574 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2575 .he_cap = {
2576 .has_he = true,
2577 .he_cap_elem = {
2578 .mac_cap_info[0] =
2579 IEEE80211_HE_MAC_CAP0_HTC_HE,
2580 .mac_cap_info[1] =
2581 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2582 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_QOS_8,
2583 .mac_cap_info[2] =
2584 IEEE80211_HE_MAC_CAP2_BSR |
2585 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2586 IEEE80211_HE_MAC_CAP2_ACK_EN,
2587 .mac_cap_info[3] =
2588 IEEE80211_HE_MAC_CAP3_GRP_ADDR_MULTI_STA_BA_DL_MU |
2589 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2590 IEEE80211_HE_MAC_CAP3_MAX_A_AMPDU_LEN_EXP_VHT_2,
2591 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2592 .phy_cap_info[0] =
2593 IEEE80211_HE_PHY_CAP0_DUAL_BAND |
2594 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2595 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2596 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2597 .phy_cap_info[1] =
2598 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2599 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2600 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2601 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_MAX_NSTS,
2602 .phy_cap_info[2] =
2603 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2604 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2605 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2606 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2607 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2609 /* Leave all the other PHY capability bytes unset, as
2610 * DCM, beam forming, RU and PPE threshold information
2611 * are not supported
2614 .he_mcs_nss_supp = {
2615 .rx_mcs_80 = cpu_to_le16(0xfffa),
2616 .tx_mcs_80 = cpu_to_le16(0xfffa),
2617 .rx_mcs_160 = cpu_to_le16(0xfffa),
2618 .tx_mcs_160 = cpu_to_le16(0xfffa),
2619 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2620 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2625 static void mac80211_hswim_he_capab(struct ieee80211_supported_band *sband)
2627 if (sband->band == NL80211_BAND_2GHZ)
2628 sband->iftype_data =
2629 (struct ieee80211_sband_iftype_data *)&he_capa_2ghz;
2630 else if (sband->band == NL80211_BAND_5GHZ)
2631 sband->iftype_data =
2632 (struct ieee80211_sband_iftype_data *)&he_capa_5ghz;
2633 else
2634 return;
2636 sband->n_iftype_data = 1;
2639 static int mac80211_hwsim_new_radio(struct genl_info *info,
2640 struct hwsim_new_radio_params *param)
2642 int err;
2643 u8 addr[ETH_ALEN];
2644 struct mac80211_hwsim_data *data;
2645 struct ieee80211_hw *hw;
2646 enum nl80211_band band;
2647 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2648 struct net *net;
2649 int idx;
2651 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2652 return -EINVAL;
2654 spin_lock_bh(&hwsim_radio_lock);
2655 idx = hwsim_radio_idx++;
2656 spin_unlock_bh(&hwsim_radio_lock);
2658 if (param->use_chanctx)
2659 ops = &mac80211_hwsim_mchan_ops;
2660 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2661 if (!hw) {
2662 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2663 err = -ENOMEM;
2664 goto failed;
2667 /* ieee80211_alloc_hw_nm may have used a default name */
2668 param->hwname = wiphy_name(hw->wiphy);
2670 if (info)
2671 net = genl_info_net(info);
2672 else
2673 net = &init_net;
2674 wiphy_net_set(hw->wiphy, net);
2676 data = hw->priv;
2677 data->hw = hw;
2679 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2680 if (IS_ERR(data->dev)) {
2681 printk(KERN_DEBUG
2682 "mac80211_hwsim: device_create failed (%ld)\n",
2683 PTR_ERR(data->dev));
2684 err = -ENOMEM;
2685 goto failed_drvdata;
2687 data->dev->driver = &mac80211_hwsim_driver.driver;
2688 err = device_bind_driver(data->dev);
2689 if (err != 0) {
2690 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
2691 err);
2692 goto failed_bind;
2695 skb_queue_head_init(&data->pending);
2697 SET_IEEE80211_DEV(hw, data->dev);
2698 if (!param->perm_addr) {
2699 eth_zero_addr(addr);
2700 addr[0] = 0x02;
2701 addr[3] = idx >> 8;
2702 addr[4] = idx;
2703 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2704 /* Why need here second address ? */
2705 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2706 data->addresses[1].addr[0] |= 0x40;
2707 hw->wiphy->n_addresses = 2;
2708 hw->wiphy->addresses = data->addresses;
2709 /* possible address clash is checked at hash table insertion */
2710 } else {
2711 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2712 /* compatibility with automatically generated mac addr */
2713 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2714 hw->wiphy->n_addresses = 2;
2715 hw->wiphy->addresses = data->addresses;
2718 data->channels = param->channels;
2719 data->use_chanctx = param->use_chanctx;
2720 data->idx = idx;
2721 data->destroy_on_close = param->destroy_on_close;
2722 if (info)
2723 data->portid = info->snd_portid;
2725 if (data->use_chanctx) {
2726 hw->wiphy->max_scan_ssids = 255;
2727 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2728 hw->wiphy->max_remain_on_channel_duration = 1000;
2729 hw->wiphy->iface_combinations = &data->if_combination;
2730 if (param->p2p_device)
2731 data->if_combination = hwsim_if_comb_p2p_dev[0];
2732 else
2733 data->if_combination = hwsim_if_comb[0];
2734 hw->wiphy->n_iface_combinations = 1;
2735 /* For channels > 1 DFS is not allowed */
2736 data->if_combination.radar_detect_widths = 0;
2737 data->if_combination.num_different_channels = data->channels;
2738 } else if (param->p2p_device) {
2739 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2740 hw->wiphy->n_iface_combinations =
2741 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
2742 } else {
2743 hw->wiphy->iface_combinations = hwsim_if_comb;
2744 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2747 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
2748 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2749 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2751 hw->queues = 5;
2752 hw->offchannel_tx_hw_queue = 4;
2753 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2754 BIT(NL80211_IFTYPE_AP) |
2755 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2756 BIT(NL80211_IFTYPE_P2P_GO) |
2757 BIT(NL80211_IFTYPE_ADHOC) |
2758 BIT(NL80211_IFTYPE_MESH_POINT);
2760 if (param->p2p_device)
2761 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
2763 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2764 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2765 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2766 ieee80211_hw_set(hw, QUEUE_CONTROL);
2767 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2768 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2769 ieee80211_hw_set(hw, MFP_CAPABLE);
2770 ieee80211_hw_set(hw, SIGNAL_DBM);
2771 ieee80211_hw_set(hw, SUPPORTS_PS);
2772 ieee80211_hw_set(hw, TDLS_WIDER_BW);
2773 if (rctbl)
2774 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
2776 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2777 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2778 WIPHY_FLAG_AP_UAPSD |
2779 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
2780 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2781 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2782 NL80211_FEATURE_STATIC_SMPS |
2783 NL80211_FEATURE_DYNAMIC_SMPS |
2784 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
2785 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
2787 /* ask mac80211 to reserve space for magic */
2788 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2789 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2790 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2792 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2793 sizeof(hwsim_channels_2ghz));
2794 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2795 sizeof(hwsim_channels_5ghz));
2796 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2798 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
2799 struct ieee80211_supported_band *sband = &data->bands[band];
2801 sband->band = band;
2803 switch (band) {
2804 case NL80211_BAND_2GHZ:
2805 sband->channels = data->channels_2ghz;
2806 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2807 sband->bitrates = data->rates;
2808 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2809 break;
2810 case NL80211_BAND_5GHZ:
2811 sband->channels = data->channels_5ghz;
2812 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2813 sband->bitrates = data->rates + 4;
2814 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
2816 sband->vht_cap.vht_supported = true;
2817 sband->vht_cap.cap =
2818 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2819 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2820 IEEE80211_VHT_CAP_RXLDPC |
2821 IEEE80211_VHT_CAP_SHORT_GI_80 |
2822 IEEE80211_VHT_CAP_SHORT_GI_160 |
2823 IEEE80211_VHT_CAP_TXSTBC |
2824 IEEE80211_VHT_CAP_RXSTBC_4 |
2825 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2826 sband->vht_cap.vht_mcs.rx_mcs_map =
2827 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2828 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2829 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2830 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2831 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
2832 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2833 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
2834 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
2835 sband->vht_cap.vht_mcs.tx_mcs_map =
2836 sband->vht_cap.vht_mcs.rx_mcs_map;
2837 break;
2838 default:
2839 continue;
2842 sband->ht_cap.ht_supported = true;
2843 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2844 IEEE80211_HT_CAP_GRN_FLD |
2845 IEEE80211_HT_CAP_SGI_20 |
2846 IEEE80211_HT_CAP_SGI_40 |
2847 IEEE80211_HT_CAP_DSSSCCK40;
2848 sband->ht_cap.ampdu_factor = 0x3;
2849 sband->ht_cap.ampdu_density = 0x6;
2850 memset(&sband->ht_cap.mcs, 0,
2851 sizeof(sband->ht_cap.mcs));
2852 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2853 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2854 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2856 mac80211_hswim_he_capab(sband);
2858 hw->wiphy->bands[band] = sband;
2861 /* By default all radios belong to the first group */
2862 data->group = 1;
2863 mutex_init(&data->mutex);
2865 data->netgroup = hwsim_net_get_netgroup(net);
2866 data->wmediumd = hwsim_net_get_wmediumd(net);
2868 /* Enable frame retransmissions for lossy channels */
2869 hw->max_rates = 4;
2870 hw->max_rate_tries = 11;
2872 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2873 hw->wiphy->n_vendor_commands =
2874 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2875 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2876 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2878 if (param->reg_strict)
2879 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
2880 if (param->regd) {
2881 data->regd = param->regd;
2882 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2883 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
2884 /* give the regulatory workqueue a chance to run */
2885 schedule_timeout_interruptible(1);
2888 if (param->no_vif)
2889 ieee80211_hw_set(hw, NO_AUTO_VIF);
2891 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2893 err = ieee80211_register_hw(hw);
2894 if (err < 0) {
2895 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2896 err);
2897 goto failed_hw;
2900 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
2902 if (param->reg_alpha2) {
2903 data->alpha2[0] = param->reg_alpha2[0];
2904 data->alpha2[1] = param->reg_alpha2[1];
2905 regulatory_hint(hw->wiphy, param->reg_alpha2);
2908 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2909 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2910 debugfs_create_file("group", 0666, data->debugfs, data,
2911 &hwsim_fops_group);
2912 if (!data->use_chanctx)
2913 debugfs_create_file("dfs_simulate_radar", 0222,
2914 data->debugfs,
2915 data, &hwsim_simulate_radar);
2917 tasklet_hrtimer_init(&data->beacon_timer,
2918 mac80211_hwsim_beacon,
2919 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2921 spin_lock_bh(&hwsim_radio_lock);
2922 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2923 hwsim_rht_params);
2924 if (err < 0) {
2925 if (info) {
2926 GENL_SET_ERR_MSG(info, "perm addr already present");
2927 NL_SET_BAD_ATTR(info->extack,
2928 info->attrs[HWSIM_ATTR_PERM_ADDR]);
2930 spin_unlock_bh(&hwsim_radio_lock);
2931 goto failed_final_insert;
2934 list_add_tail(&data->list, &hwsim_radios);
2935 hwsim_radios_generation++;
2936 spin_unlock_bh(&hwsim_radio_lock);
2938 if (idx > 0)
2939 hwsim_mcast_new_radio(idx, info, param);
2941 return idx;
2943 failed_final_insert:
2944 debugfs_remove_recursive(data->debugfs);
2945 ieee80211_unregister_hw(data->hw);
2946 failed_hw:
2947 device_release_driver(data->dev);
2948 failed_bind:
2949 device_unregister(data->dev);
2950 failed_drvdata:
2951 ieee80211_free_hw(hw);
2952 failed:
2953 return err;
2956 static void hwsim_mcast_del_radio(int id, const char *hwname,
2957 struct genl_info *info)
2959 struct sk_buff *skb;
2960 void *data;
2961 int ret;
2963 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2964 if (!skb)
2965 return;
2967 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2968 HWSIM_CMD_DEL_RADIO);
2969 if (!data)
2970 goto error;
2972 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2973 if (ret < 0)
2974 goto error;
2976 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2977 hwname);
2978 if (ret < 0)
2979 goto error;
2981 genlmsg_end(skb, data);
2983 hwsim_mcast_config_msg(skb, info);
2985 return;
2987 error:
2988 nlmsg_free(skb);
2991 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2992 const char *hwname,
2993 struct genl_info *info)
2995 hwsim_mcast_del_radio(data->idx, hwname, info);
2996 debugfs_remove_recursive(data->debugfs);
2997 ieee80211_unregister_hw(data->hw);
2998 device_release_driver(data->dev);
2999 device_unregister(data->dev);
3000 ieee80211_free_hw(data->hw);
3003 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3004 struct mac80211_hwsim_data *data,
3005 u32 portid, u32 seq,
3006 struct netlink_callback *cb, int flags)
3008 void *hdr;
3009 struct hwsim_new_radio_params param = { };
3010 int res = -EMSGSIZE;
3012 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3013 HWSIM_CMD_GET_RADIO);
3014 if (!hdr)
3015 return -EMSGSIZE;
3017 if (cb)
3018 genl_dump_check_consistent(cb, hdr);
3020 if (data->alpha2[0] && data->alpha2[1])
3021 param.reg_alpha2 = data->alpha2;
3023 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3024 REGULATORY_STRICT_REG);
3025 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3026 BIT(NL80211_IFTYPE_P2P_DEVICE));
3027 param.use_chanctx = data->use_chanctx;
3028 param.regd = data->regd;
3029 param.channels = data->channels;
3030 param.hwname = wiphy_name(data->hw->wiphy);
3032 res = append_radio_msg(skb, data->idx, &param);
3033 if (res < 0)
3034 goto out_err;
3036 genlmsg_end(skb, hdr);
3037 return 0;
3039 out_err:
3040 genlmsg_cancel(skb, hdr);
3041 return res;
3044 static void mac80211_hwsim_free(void)
3046 struct mac80211_hwsim_data *data;
3048 spin_lock_bh(&hwsim_radio_lock);
3049 while ((data = list_first_entry_or_null(&hwsim_radios,
3050 struct mac80211_hwsim_data,
3051 list))) {
3052 list_del(&data->list);
3053 spin_unlock_bh(&hwsim_radio_lock);
3054 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3055 NULL);
3056 spin_lock_bh(&hwsim_radio_lock);
3058 spin_unlock_bh(&hwsim_radio_lock);
3059 class_destroy(hwsim_class);
3062 static const struct net_device_ops hwsim_netdev_ops = {
3063 .ndo_start_xmit = hwsim_mon_xmit,
3064 .ndo_set_mac_address = eth_mac_addr,
3065 .ndo_validate_addr = eth_validate_addr,
3068 static void hwsim_mon_setup(struct net_device *dev)
3070 dev->netdev_ops = &hwsim_netdev_ops;
3071 dev->needs_free_netdev = true;
3072 ether_setup(dev);
3073 dev->priv_flags |= IFF_NO_QUEUE;
3074 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3075 eth_zero_addr(dev->dev_addr);
3076 dev->dev_addr[0] = 0x12;
3079 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3081 return rhashtable_lookup_fast(&hwsim_radios_rht,
3082 addr,
3083 hwsim_rht_params);
3086 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3088 struct mac80211_hwsim_data *data;
3090 hwsim_net_set_wmediumd(net, portid);
3092 spin_lock_bh(&hwsim_radio_lock);
3093 list_for_each_entry(data, &hwsim_radios, list) {
3094 if (data->netgroup == hwsim_net_get_netgroup(net))
3095 data->wmediumd = portid;
3097 spin_unlock_bh(&hwsim_radio_lock);
3100 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3101 struct genl_info *info)
3104 struct ieee80211_hdr *hdr;
3105 struct mac80211_hwsim_data *data2;
3106 struct ieee80211_tx_info *txi;
3107 struct hwsim_tx_rate *tx_attempts;
3108 u64 ret_skb_cookie;
3109 struct sk_buff *skb, *tmp;
3110 const u8 *src;
3111 unsigned int hwsim_flags;
3112 int i;
3113 bool found = false;
3115 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3116 !info->attrs[HWSIM_ATTR_FLAGS] ||
3117 !info->attrs[HWSIM_ATTR_COOKIE] ||
3118 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3119 !info->attrs[HWSIM_ATTR_TX_INFO])
3120 goto out;
3122 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3123 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3124 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3126 data2 = get_hwsim_data_ref_from_addr(src);
3127 if (!data2)
3128 goto out;
3130 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3131 goto out;
3133 if (info->snd_portid != data2->wmediumd)
3134 goto out;
3136 /* look for the skb matching the cookie passed back from user */
3137 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3138 u64 skb_cookie;
3140 txi = IEEE80211_SKB_CB(skb);
3141 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3143 if (skb_cookie == ret_skb_cookie) {
3144 skb_unlink(skb, &data2->pending);
3145 found = true;
3146 break;
3150 /* not found */
3151 if (!found)
3152 goto out;
3154 /* Tx info received because the frame was broadcasted on user space,
3155 so we get all the necessary info: tx attempts and skb control buff */
3157 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3158 info->attrs[HWSIM_ATTR_TX_INFO]);
3160 /* now send back TX status */
3161 txi = IEEE80211_SKB_CB(skb);
3163 ieee80211_tx_info_clear_status(txi);
3165 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3166 txi->status.rates[i].idx = tx_attempts[i].idx;
3167 txi->status.rates[i].count = tx_attempts[i].count;
3170 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3172 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3173 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3174 if (skb->len >= 16) {
3175 hdr = (struct ieee80211_hdr *) skb->data;
3176 mac80211_hwsim_monitor_ack(data2->channel,
3177 hdr->addr2);
3179 txi->flags |= IEEE80211_TX_STAT_ACK;
3181 ieee80211_tx_status_irqsafe(data2->hw, skb);
3182 return 0;
3183 out:
3184 return -EINVAL;
3188 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3189 struct genl_info *info)
3191 struct mac80211_hwsim_data *data2;
3192 struct ieee80211_rx_status rx_status;
3193 const u8 *dst;
3194 int frame_data_len;
3195 void *frame_data;
3196 struct sk_buff *skb = NULL;
3198 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3199 !info->attrs[HWSIM_ATTR_FRAME] ||
3200 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3201 !info->attrs[HWSIM_ATTR_SIGNAL])
3202 goto out;
3204 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3205 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3206 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3208 /* Allocate new skb here */
3209 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3210 if (skb == NULL)
3211 goto err;
3213 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3214 goto err;
3216 /* Copy the data */
3217 skb_put_data(skb, frame_data, frame_data_len);
3219 data2 = get_hwsim_data_ref_from_addr(dst);
3220 if (!data2)
3221 goto out;
3223 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3224 goto out;
3226 if (info->snd_portid != data2->wmediumd)
3227 goto out;
3229 /* check if radio is configured properly */
3231 if (data2->idle || !data2->started)
3232 goto out;
3234 /* A frame is received from user space */
3235 memset(&rx_status, 0, sizeof(rx_status));
3236 if (info->attrs[HWSIM_ATTR_FREQ]) {
3237 /* throw away off-channel packets, but allow both the temporary
3238 * ("hw" scan/remain-on-channel) and regular channel, since the
3239 * internal datapath also allows this
3241 mutex_lock(&data2->mutex);
3242 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3244 if (rx_status.freq != data2->channel->center_freq &&
3245 (!data2->tmp_chan ||
3246 rx_status.freq != data2->tmp_chan->center_freq)) {
3247 mutex_unlock(&data2->mutex);
3248 goto out;
3250 mutex_unlock(&data2->mutex);
3251 } else {
3252 rx_status.freq = data2->channel->center_freq;
3255 rx_status.band = data2->channel->band;
3256 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3257 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3259 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3260 data2->rx_pkts++;
3261 data2->rx_bytes += skb->len;
3262 ieee80211_rx_irqsafe(data2->hw, skb);
3264 return 0;
3265 err:
3266 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3267 out:
3268 dev_kfree_skb(skb);
3269 return -EINVAL;
3272 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3273 struct genl_info *info)
3275 struct net *net = genl_info_net(info);
3276 struct mac80211_hwsim_data *data;
3277 int chans = 1;
3279 spin_lock_bh(&hwsim_radio_lock);
3280 list_for_each_entry(data, &hwsim_radios, list)
3281 chans = max(chans, data->channels);
3282 spin_unlock_bh(&hwsim_radio_lock);
3284 /* In the future we should revise the userspace API and allow it
3285 * to set a flag that it does support multi-channel, then we can
3286 * let this pass conditionally on the flag.
3287 * For current userspace, prohibit it since it won't work right.
3289 if (chans > 1)
3290 return -EOPNOTSUPP;
3292 if (hwsim_net_get_wmediumd(net))
3293 return -EBUSY;
3295 hwsim_register_wmediumd(net, info->snd_portid);
3297 pr_debug("mac80211_hwsim: received a REGISTER, "
3298 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3300 return 0;
3303 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3305 struct hwsim_new_radio_params param = { 0 };
3306 const char *hwname = NULL;
3307 int ret;
3309 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3310 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3311 param.channels = channels;
3312 param.destroy_on_close =
3313 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3315 if (info->attrs[HWSIM_ATTR_CHANNELS])
3316 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3318 if (param.channels < 1) {
3319 GENL_SET_ERR_MSG(info, "must have at least one channel");
3320 return -EINVAL;
3323 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3324 GENL_SET_ERR_MSG(info, "too many channels specified");
3325 return -EINVAL;
3328 if (info->attrs[HWSIM_ATTR_NO_VIF])
3329 param.no_vif = true;
3331 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3332 hwname = kasprintf(GFP_KERNEL, "%.*s",
3333 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3334 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3335 if (!hwname)
3336 return -ENOMEM;
3337 param.hwname = hwname;
3340 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3341 param.use_chanctx = true;
3342 else
3343 param.use_chanctx = (param.channels > 1);
3345 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3346 param.reg_alpha2 =
3347 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3349 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3350 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3352 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3353 kfree(hwname);
3354 return -EINVAL;
3357 idx = array_index_nospec(idx,
3358 ARRAY_SIZE(hwsim_world_regdom_custom));
3359 param.regd = hwsim_world_regdom_custom[idx];
3362 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3363 if (!is_valid_ether_addr(
3364 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3365 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3366 NL_SET_BAD_ATTR(info->extack,
3367 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3368 kfree(hwname);
3369 return -EINVAL;
3373 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3376 ret = mac80211_hwsim_new_radio(info, &param);
3377 kfree(hwname);
3378 return ret;
3381 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3383 struct mac80211_hwsim_data *data;
3384 s64 idx = -1;
3385 const char *hwname = NULL;
3387 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3388 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3389 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3390 hwname = kasprintf(GFP_KERNEL, "%.*s",
3391 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3392 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3393 if (!hwname)
3394 return -ENOMEM;
3395 } else
3396 return -EINVAL;
3398 spin_lock_bh(&hwsim_radio_lock);
3399 list_for_each_entry(data, &hwsim_radios, list) {
3400 if (idx >= 0) {
3401 if (data->idx != idx)
3402 continue;
3403 } else {
3404 if (!hwname ||
3405 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3406 continue;
3409 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3410 continue;
3412 list_del(&data->list);
3413 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3414 hwsim_rht_params);
3415 hwsim_radios_generation++;
3416 spin_unlock_bh(&hwsim_radio_lock);
3417 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3418 info);
3419 kfree(hwname);
3420 return 0;
3422 spin_unlock_bh(&hwsim_radio_lock);
3424 kfree(hwname);
3425 return -ENODEV;
3428 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3430 struct mac80211_hwsim_data *data;
3431 struct sk_buff *skb;
3432 int idx, res = -ENODEV;
3434 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3435 return -EINVAL;
3436 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3438 spin_lock_bh(&hwsim_radio_lock);
3439 list_for_each_entry(data, &hwsim_radios, list) {
3440 if (data->idx != idx)
3441 continue;
3443 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3444 continue;
3446 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3447 if (!skb) {
3448 res = -ENOMEM;
3449 goto out_err;
3452 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3453 info->snd_seq, NULL, 0);
3454 if (res < 0) {
3455 nlmsg_free(skb);
3456 goto out_err;
3459 genlmsg_reply(skb, info);
3460 break;
3463 out_err:
3464 spin_unlock_bh(&hwsim_radio_lock);
3466 return res;
3469 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3470 struct netlink_callback *cb)
3472 int last_idx = cb->args[0] - 1;
3473 struct mac80211_hwsim_data *data = NULL;
3474 int res = 0;
3475 void *hdr;
3477 spin_lock_bh(&hwsim_radio_lock);
3478 cb->seq = hwsim_radios_generation;
3480 if (last_idx >= hwsim_radio_idx-1)
3481 goto done;
3483 list_for_each_entry(data, &hwsim_radios, list) {
3484 if (data->idx <= last_idx)
3485 continue;
3487 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3488 continue;
3490 res = mac80211_hwsim_get_radio(skb, data,
3491 NETLINK_CB(cb->skb).portid,
3492 cb->nlh->nlmsg_seq, cb,
3493 NLM_F_MULTI);
3494 if (res < 0)
3495 break;
3497 last_idx = data->idx;
3500 cb->args[0] = last_idx + 1;
3502 /* list changed, but no new element sent, set interrupted flag */
3503 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3504 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3505 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3506 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3507 if (!hdr)
3508 res = -EMSGSIZE;
3509 genl_dump_check_consistent(cb, hdr);
3510 genlmsg_end(skb, hdr);
3513 done:
3514 spin_unlock_bh(&hwsim_radio_lock);
3515 return res ?: skb->len;
3518 /* Generic Netlink operations array */
3519 static const struct genl_ops hwsim_ops[] = {
3521 .cmd = HWSIM_CMD_REGISTER,
3522 .policy = hwsim_genl_policy,
3523 .doit = hwsim_register_received_nl,
3524 .flags = GENL_UNS_ADMIN_PERM,
3527 .cmd = HWSIM_CMD_FRAME,
3528 .policy = hwsim_genl_policy,
3529 .doit = hwsim_cloned_frame_received_nl,
3532 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3533 .policy = hwsim_genl_policy,
3534 .doit = hwsim_tx_info_frame_received_nl,
3537 .cmd = HWSIM_CMD_NEW_RADIO,
3538 .policy = hwsim_genl_policy,
3539 .doit = hwsim_new_radio_nl,
3540 .flags = GENL_UNS_ADMIN_PERM,
3543 .cmd = HWSIM_CMD_DEL_RADIO,
3544 .policy = hwsim_genl_policy,
3545 .doit = hwsim_del_radio_nl,
3546 .flags = GENL_UNS_ADMIN_PERM,
3549 .cmd = HWSIM_CMD_GET_RADIO,
3550 .policy = hwsim_genl_policy,
3551 .doit = hwsim_get_radio_nl,
3552 .dumpit = hwsim_dump_radio_nl,
3556 static struct genl_family hwsim_genl_family __ro_after_init = {
3557 .name = "MAC80211_HWSIM",
3558 .version = 1,
3559 .maxattr = HWSIM_ATTR_MAX,
3560 .netnsok = true,
3561 .module = THIS_MODULE,
3562 .ops = hwsim_ops,
3563 .n_ops = ARRAY_SIZE(hwsim_ops),
3564 .mcgrps = hwsim_mcgrps,
3565 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3568 static void destroy_radio(struct work_struct *work)
3570 struct mac80211_hwsim_data *data =
3571 container_of(work, struct mac80211_hwsim_data, destroy_work);
3573 hwsim_radios_generation++;
3574 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
3577 static void remove_user_radios(u32 portid)
3579 struct mac80211_hwsim_data *entry, *tmp;
3581 spin_lock_bh(&hwsim_radio_lock);
3582 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3583 if (entry->destroy_on_close && entry->portid == portid) {
3584 list_del(&entry->list);
3585 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3586 hwsim_rht_params);
3587 INIT_WORK(&entry->destroy_work, destroy_radio);
3588 queue_work(hwsim_wq, &entry->destroy_work);
3591 spin_unlock_bh(&hwsim_radio_lock);
3594 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3595 unsigned long state,
3596 void *_notify)
3598 struct netlink_notify *notify = _notify;
3600 if (state != NETLINK_URELEASE)
3601 return NOTIFY_DONE;
3603 remove_user_radios(notify->portid);
3605 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
3606 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3607 " socket, switching to perfect channel medium\n");
3608 hwsim_register_wmediumd(notify->net, 0);
3610 return NOTIFY_DONE;
3614 static struct notifier_block hwsim_netlink_notifier = {
3615 .notifier_call = mac80211_hwsim_netlink_notify,
3618 static int __init hwsim_init_netlink(void)
3620 int rc;
3622 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3624 rc = genl_register_family(&hwsim_genl_family);
3625 if (rc)
3626 goto failure;
3628 rc = netlink_register_notifier(&hwsim_netlink_notifier);
3629 if (rc) {
3630 genl_unregister_family(&hwsim_genl_family);
3631 goto failure;
3634 return 0;
3636 failure:
3637 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3638 return -EINVAL;
3641 static __net_init int hwsim_init_net(struct net *net)
3643 return hwsim_net_set_netgroup(net);
3646 static void __net_exit hwsim_exit_net(struct net *net)
3648 struct mac80211_hwsim_data *data, *tmp;
3650 spin_lock_bh(&hwsim_radio_lock);
3651 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3652 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3653 continue;
3655 /* Radios created in init_net are returned to init_net. */
3656 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3657 continue;
3659 list_del(&data->list);
3660 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3661 hwsim_rht_params);
3662 hwsim_radios_generation++;
3663 spin_unlock_bh(&hwsim_radio_lock);
3664 mac80211_hwsim_del_radio(data,
3665 wiphy_name(data->hw->wiphy),
3666 NULL);
3667 spin_lock_bh(&hwsim_radio_lock);
3669 spin_unlock_bh(&hwsim_radio_lock);
3671 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
3674 static struct pernet_operations hwsim_net_ops = {
3675 .init = hwsim_init_net,
3676 .exit = hwsim_exit_net,
3677 .id = &hwsim_net_id,
3678 .size = sizeof(struct hwsim_net),
3681 static void hwsim_exit_netlink(void)
3683 /* unregister the notifier */
3684 netlink_unregister_notifier(&hwsim_netlink_notifier);
3685 /* unregister the family */
3686 genl_unregister_family(&hwsim_genl_family);
3689 static int __init init_mac80211_hwsim(void)
3691 int i, err;
3693 if (radios < 0 || radios > 100)
3694 return -EINVAL;
3696 if (channels < 1)
3697 return -EINVAL;
3699 spin_lock_init(&hwsim_radio_lock);
3701 hwsim_wq = alloc_workqueue("hwsim_wq", 0, 0);
3702 if (!hwsim_wq)
3703 return -ENOMEM;
3705 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
3706 if (err)
3707 goto out_free_wq;
3709 err = register_pernet_device(&hwsim_net_ops);
3710 if (err)
3711 goto out_free_rht;
3713 err = platform_driver_register(&mac80211_hwsim_driver);
3714 if (err)
3715 goto out_unregister_pernet;
3717 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
3718 if (IS_ERR(hwsim_class)) {
3719 err = PTR_ERR(hwsim_class);
3720 goto out_unregister_driver;
3723 err = hwsim_init_netlink();
3724 if (err < 0)
3725 goto out_unregister_driver;
3727 for (i = 0; i < radios; i++) {
3728 struct hwsim_new_radio_params param = { 0 };
3730 param.channels = channels;
3732 switch (regtest) {
3733 case HWSIM_REGTEST_DIFF_COUNTRY:
3734 if (i < ARRAY_SIZE(hwsim_alpha2s))
3735 param.reg_alpha2 = hwsim_alpha2s[i];
3736 break;
3737 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3738 if (!i)
3739 param.reg_alpha2 = hwsim_alpha2s[0];
3740 break;
3741 case HWSIM_REGTEST_STRICT_ALL:
3742 param.reg_strict = true;
3743 case HWSIM_REGTEST_DRIVER_REG_ALL:
3744 param.reg_alpha2 = hwsim_alpha2s[0];
3745 break;
3746 case HWSIM_REGTEST_WORLD_ROAM:
3747 if (i == 0)
3748 param.regd = &hwsim_world_regdom_custom_01;
3749 break;
3750 case HWSIM_REGTEST_CUSTOM_WORLD:
3751 param.regd = &hwsim_world_regdom_custom_01;
3752 break;
3753 case HWSIM_REGTEST_CUSTOM_WORLD_2:
3754 if (i == 0)
3755 param.regd = &hwsim_world_regdom_custom_01;
3756 else if (i == 1)
3757 param.regd = &hwsim_world_regdom_custom_02;
3758 break;
3759 case HWSIM_REGTEST_STRICT_FOLLOW:
3760 if (i == 0) {
3761 param.reg_strict = true;
3762 param.reg_alpha2 = hwsim_alpha2s[0];
3764 break;
3765 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
3766 if (i == 0) {
3767 param.reg_strict = true;
3768 param.reg_alpha2 = hwsim_alpha2s[0];
3769 } else if (i == 1) {
3770 param.reg_alpha2 = hwsim_alpha2s[1];
3772 break;
3773 case HWSIM_REGTEST_ALL:
3774 switch (i) {
3775 case 0:
3776 param.regd = &hwsim_world_regdom_custom_01;
3777 break;
3778 case 1:
3779 param.regd = &hwsim_world_regdom_custom_02;
3780 break;
3781 case 2:
3782 param.reg_alpha2 = hwsim_alpha2s[0];
3783 break;
3784 case 3:
3785 param.reg_alpha2 = hwsim_alpha2s[1];
3786 break;
3787 case 4:
3788 param.reg_strict = true;
3789 param.reg_alpha2 = hwsim_alpha2s[2];
3790 break;
3792 break;
3793 default:
3794 break;
3797 param.p2p_device = support_p2p_device;
3798 param.use_chanctx = channels > 1;
3800 err = mac80211_hwsim_new_radio(NULL, &param);
3801 if (err < 0)
3802 goto out_free_radios;
3805 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3806 hwsim_mon_setup);
3807 if (hwsim_mon == NULL) {
3808 err = -ENOMEM;
3809 goto out_free_radios;
3812 rtnl_lock();
3813 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3814 if (err < 0) {
3815 rtnl_unlock();
3816 goto out_free_radios;
3819 err = register_netdevice(hwsim_mon);
3820 if (err < 0) {
3821 rtnl_unlock();
3822 goto out_free_mon;
3824 rtnl_unlock();
3826 return 0;
3828 out_free_mon:
3829 free_netdev(hwsim_mon);
3830 out_free_radios:
3831 mac80211_hwsim_free();
3832 out_unregister_driver:
3833 platform_driver_unregister(&mac80211_hwsim_driver);
3834 out_unregister_pernet:
3835 unregister_pernet_device(&hwsim_net_ops);
3836 out_free_rht:
3837 rhashtable_destroy(&hwsim_radios_rht);
3838 out_free_wq:
3839 destroy_workqueue(hwsim_wq);
3840 return err;
3842 module_init(init_mac80211_hwsim);
3844 static void __exit exit_mac80211_hwsim(void)
3846 pr_debug("mac80211_hwsim: unregister radios\n");
3848 hwsim_exit_netlink();
3850 mac80211_hwsim_free();
3851 flush_workqueue(hwsim_wq);
3853 rhashtable_destroy(&hwsim_radios_rht);
3854 unregister_netdev(hwsim_mon);
3855 platform_driver_unregister(&mac80211_hwsim_driver);
3856 unregister_pernet_device(&hwsim_net_ops);
3857 destroy_workqueue(hwsim_wq);
3859 module_exit(exit_mac80211_hwsim);