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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
15 * - RX filtering based on filter configuration (data->rx_filter)
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <linux/if_arp.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/etherdevice.h>
28 #include <linux/platform_device.h>
29 #include <linux/debugfs.h>
30 #include <linux/module.h>
31 #include <linux/ktime.h>
32 #include <net/genetlink.h>
33 #include <net/net_namespace.h>
34 #include <net/netns/generic.h>
35 #include "mac80211_hwsim.h"
37 #define WARN_QUEUE 100
40 MODULE_AUTHOR("Jouni Malinen");
41 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
42 MODULE_LICENSE("GPL");
44 static u32 wmediumd_portid;
46 static int radios = 2;
47 module_param(radios, int, 0444);
48 MODULE_PARM_DESC(radios, "Number of simulated radios");
50 static int channels = 1;
51 module_param(channels, int, 0444);
52 MODULE_PARM_DESC(channels, "Number of concurrent channels");
54 static bool paged_rx = false;
55 module_param(paged_rx, bool, 0644);
56 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
58 static bool rctbl = false;
59 module_param(rctbl, bool, 0444);
60 MODULE_PARM_DESC(rctbl, "Handle rate control table");
62 static bool support_p2p_device = true;
63 module_param(support_p2p_device, bool, 0444);
64 MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
67 * enum hwsim_regtest - the type of regulatory tests we offer
69 * These are the different values you can use for the regtest
70 * module parameter. This is useful to help test world roaming
71 * and the driver regulatory_hint() call and combinations of these.
72 * If you want to do specific alpha2 regulatory domain tests simply
73 * use the userspace regulatory request as that will be respected as
74 * well without the need of this module parameter. This is designed
75 * only for testing the driver regulatory request, world roaming
76 * and all possible combinations.
78 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
79 * this is the default value.
80 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
81 * hint, only one driver regulatory hint will be sent as such the
82 * secondary radios are expected to follow.
83 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
84 * request with all radios reporting the same regulatory domain.
85 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
86 * different regulatory domains requests. Expected behaviour is for
87 * an intersection to occur but each device will still use their
88 * respective regulatory requested domains. Subsequent radios will
89 * use the resulting intersection.
90 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
91 * this by using a custom beacon-capable regulatory domain for the first
92 * radio. All other device world roam.
93 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
94 * domain requests. All radios will adhere to this custom world regulatory
96 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
97 * domain requests. The first radio will adhere to the first custom world
98 * regulatory domain, the second one to the second custom world regulatory
99 * domain. All other devices will world roam.
100 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
101 * settings, only the first radio will send a regulatory domain request
102 * and use strict settings. The rest of the radios are expected to follow.
103 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
104 * settings. All radios will adhere to this.
105 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
106 * domain settings, combined with secondary driver regulatory domain
107 * settings. The first radio will get a strict regulatory domain setting
108 * using the first driver regulatory request and the second radio will use
109 * non-strict settings using the second driver regulatory request. All
110 * other devices should follow the intersection created between the
112 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
113 * at least 6 radios for a complete test. We will test in this order:
114 * 1 - driver custom world regulatory domain
115 * 2 - second custom world regulatory domain
116 * 3 - first driver regulatory domain request
117 * 4 - second driver regulatory domain request
118 * 5 - strict regulatory domain settings using the third driver regulatory
120 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
121 * regulatory requests.
124 HWSIM_REGTEST_DISABLED = 0,
125 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
126 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
127 HWSIM_REGTEST_DIFF_COUNTRY = 3,
128 HWSIM_REGTEST_WORLD_ROAM = 4,
129 HWSIM_REGTEST_CUSTOM_WORLD = 5,
130 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
131 HWSIM_REGTEST_STRICT_FOLLOW = 7,
132 HWSIM_REGTEST_STRICT_ALL = 8,
133 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
134 HWSIM_REGTEST_ALL = 10,
137 /* Set to one of the HWSIM_REGTEST_* values above */
138 static int regtest = HWSIM_REGTEST_DISABLED;
139 module_param(regtest, int, 0444);
140 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
142 static const char *hwsim_alpha2s[] = {
151 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
155 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
156 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
157 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
158 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
162 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
166 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
167 REG_RULE(5725-10, 5850+10, 40, 0, 30,
172 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
173 &hwsim_world_regdom_custom_01,
174 &hwsim_world_regdom_custom_02,
177 struct hwsim_vif_priv {
185 #define HWSIM_VIF_MAGIC 0x69537748
187 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
189 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
190 WARN(vp->magic != HWSIM_VIF_MAGIC,
191 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
192 vif, vp->magic, vif->addr, vif->type, vif->p2p);
195 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
197 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
198 vp->magic = HWSIM_VIF_MAGIC;
201 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
203 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
207 struct hwsim_sta_priv {
211 #define HWSIM_STA_MAGIC 0x6d537749
213 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
215 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
216 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
219 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
221 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
222 sp->magic = HWSIM_STA_MAGIC;
225 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
227 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
231 struct hwsim_chanctx_priv {
235 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
237 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
239 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
240 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
243 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
245 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
246 cp->magic = HWSIM_CHANCTX_MAGIC;
249 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
251 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
255 static unsigned int hwsim_net_id;
257 static int hwsim_netgroup;
263 static inline int hwsim_net_get_netgroup(struct net *net)
265 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
267 return hwsim_net->netgroup;
270 static inline void hwsim_net_set_netgroup(struct net *net)
272 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
274 hwsim_net->netgroup = hwsim_netgroup++;
277 static struct class *hwsim_class;
279 static struct net_device *hwsim_mon; /* global monitor netdev */
281 #define CHAN2G(_freq) { \
282 .band = NL80211_BAND_2GHZ, \
283 .center_freq = (_freq), \
284 .hw_value = (_freq), \
288 #define CHAN5G(_freq) { \
289 .band = NL80211_BAND_5GHZ, \
290 .center_freq = (_freq), \
291 .hw_value = (_freq), \
295 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
296 CHAN2G(2412), /* Channel 1 */
297 CHAN2G(2417), /* Channel 2 */
298 CHAN2G(2422), /* Channel 3 */
299 CHAN2G(2427), /* Channel 4 */
300 CHAN2G(2432), /* Channel 5 */
301 CHAN2G(2437), /* Channel 6 */
302 CHAN2G(2442), /* Channel 7 */
303 CHAN2G(2447), /* Channel 8 */
304 CHAN2G(2452), /* Channel 9 */
305 CHAN2G(2457), /* Channel 10 */
306 CHAN2G(2462), /* Channel 11 */
307 CHAN2G(2467), /* Channel 12 */
308 CHAN2G(2472), /* Channel 13 */
309 CHAN2G(2484), /* Channel 14 */
312 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
313 CHAN5G(5180), /* Channel 36 */
314 CHAN5G(5200), /* Channel 40 */
315 CHAN5G(5220), /* Channel 44 */
316 CHAN5G(5240), /* Channel 48 */
318 CHAN5G(5260), /* Channel 52 */
319 CHAN5G(5280), /* Channel 56 */
320 CHAN5G(5300), /* Channel 60 */
321 CHAN5G(5320), /* Channel 64 */
323 CHAN5G(5500), /* Channel 100 */
324 CHAN5G(5520), /* Channel 104 */
325 CHAN5G(5540), /* Channel 108 */
326 CHAN5G(5560), /* Channel 112 */
327 CHAN5G(5580), /* Channel 116 */
328 CHAN5G(5600), /* Channel 120 */
329 CHAN5G(5620), /* Channel 124 */
330 CHAN5G(5640), /* Channel 128 */
331 CHAN5G(5660), /* Channel 132 */
332 CHAN5G(5680), /* Channel 136 */
333 CHAN5G(5700), /* Channel 140 */
335 CHAN5G(5745), /* Channel 149 */
336 CHAN5G(5765), /* Channel 153 */
337 CHAN5G(5785), /* Channel 157 */
338 CHAN5G(5805), /* Channel 161 */
339 CHAN5G(5825), /* Channel 165 */
342 static const struct ieee80211_rate hwsim_rates[] = {
344 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
345 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
346 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
357 #define OUI_QCA 0x001374
358 #define QCA_NL80211_SUBCMD_TEST 1
359 enum qca_nl80211_vendor_subcmds {
360 QCA_WLAN_VENDOR_ATTR_TEST = 8,
361 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
364 static const struct nla_policy
365 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
366 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
369 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
370 struct wireless_dev *wdev,
371 const void *data, int data_len)
374 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
378 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
379 hwsim_vendor_test_policy);
382 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
384 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
385 wiphy_debug(wiphy, "%s: test=%u\n", __func__, val);
387 /* Send a vendor event as a test. Note that this would not normally be
388 * done within a command handler, but rather, based on some other
389 * trigger. For simplicity, this command is used to trigger the event
392 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
394 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
396 /* skb_put() or nla_put() will fill up data within
397 * NL80211_ATTR_VENDOR_DATA.
400 /* Add vendor data */
401 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
403 /* Send the event - this will call nla_nest_end() */
404 cfg80211_vendor_event(skb, GFP_KERNEL);
407 /* Send a response to the command */
408 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
412 /* skb_put() or nla_put() will fill up data within
413 * NL80211_ATTR_VENDOR_DATA
415 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
417 return cfg80211_vendor_cmd_reply(skb);
420 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
422 .info = { .vendor_id = OUI_QCA,
423 .subcmd = QCA_NL80211_SUBCMD_TEST },
424 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
425 .doit = mac80211_hwsim_vendor_cmd_test,
429 /* Advertise support vendor specific events */
430 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
431 { .vendor_id = OUI_QCA, .subcmd = 1 },
434 static const struct ieee80211_iface_limit hwsim_if_limits[] = {
435 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
436 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
437 BIT(NL80211_IFTYPE_P2P_CLIENT) |
438 #ifdef CONFIG_MAC80211_MESH
439 BIT(NL80211_IFTYPE_MESH_POINT) |
441 BIT(NL80211_IFTYPE_AP) |
442 BIT(NL80211_IFTYPE_P2P_GO) },
443 /* must be last, see hwsim_if_comb */
444 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
447 static const struct ieee80211_iface_limit hwsim_if_dfs_limits[] = {
448 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
451 static const struct ieee80211_iface_combination hwsim_if_comb[] = {
453 .limits = hwsim_if_limits,
454 /* remove the last entry which is P2P_DEVICE */
455 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
456 .max_interfaces = 2048,
457 .num_different_channels = 1,
460 .limits = hwsim_if_dfs_limits,
461 .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits),
463 .num_different_channels = 1,
464 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
465 BIT(NL80211_CHAN_WIDTH_20) |
466 BIT(NL80211_CHAN_WIDTH_40) |
467 BIT(NL80211_CHAN_WIDTH_80) |
468 BIT(NL80211_CHAN_WIDTH_160),
472 static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
474 .limits = hwsim_if_limits,
475 .n_limits = ARRAY_SIZE(hwsim_if_limits),
476 .max_interfaces = 2048,
477 .num_different_channels = 1,
480 .limits = hwsim_if_dfs_limits,
481 .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits),
483 .num_different_channels = 1,
484 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
485 BIT(NL80211_CHAN_WIDTH_20) |
486 BIT(NL80211_CHAN_WIDTH_40) |
487 BIT(NL80211_CHAN_WIDTH_80) |
488 BIT(NL80211_CHAN_WIDTH_160),
492 static spinlock_t hwsim_radio_lock;
493 static struct list_head hwsim_radios;
494 static int hwsim_radio_idx;
496 static struct platform_driver mac80211_hwsim_driver = {
498 .name = "mac80211_hwsim",
502 struct mac80211_hwsim_data {
503 struct list_head list;
504 struct ieee80211_hw *hw;
506 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
507 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
508 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
509 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
510 struct ieee80211_iface_combination if_combination;
512 struct mac_address addresses[2];
515 bool destroy_on_close;
516 struct work_struct destroy_work;
519 const struct ieee80211_regdomain *regd;
521 struct ieee80211_channel *tmp_chan;
522 struct ieee80211_channel *roc_chan;
524 struct delayed_work roc_start;
525 struct delayed_work roc_done;
526 struct delayed_work hw_scan;
527 struct cfg80211_scan_request *hw_scan_request;
528 struct ieee80211_vif *hw_scan_vif;
530 u8 scan_addr[ETH_ALEN];
532 struct ieee80211_channel *channel;
533 u64 beacon_int /* beacon interval in us */;
534 unsigned int rx_filter;
535 bool started, idle, scanning;
537 struct tasklet_hrtimer beacon_timer;
539 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
541 bool ps_poll_pending;
542 struct dentry *debugfs;
544 uintptr_t pending_cookie;
545 struct sk_buff_head pending; /* packets pending */
547 * Only radios in the same group can communicate together (the
548 * channel has to match too). Each bit represents a group. A
549 * radio can be in more than one group.
553 /* group shared by radios created in the same netns */
558 /* difference between this hw's clock and the real clock, in usecs */
561 /* absolute beacon transmission time. Used to cover up "tx" delay. */
574 struct hwsim_radiotap_hdr {
575 struct ieee80211_radiotap_header hdr;
583 struct hwsim_radiotap_ack_hdr {
584 struct ieee80211_radiotap_header hdr;
591 /* MAC80211_HWSIM netlinf family */
592 static struct genl_family hwsim_genl_family = {
593 .id = GENL_ID_GENERATE,
595 .name = "MAC80211_HWSIM",
597 .maxattr = HWSIM_ATTR_MAX,
601 enum hwsim_multicast_groups {
605 static const struct genl_multicast_group hwsim_mcgrps[] = {
606 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
609 /* MAC80211_HWSIM netlink policy */
611 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
612 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
613 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
614 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
615 .len = IEEE80211_MAX_DATA_LEN },
616 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
617 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
618 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
619 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
620 .len = IEEE80211_TX_MAX_RATES *
621 sizeof(struct hwsim_tx_rate)},
622 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
623 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
624 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
625 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
626 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
627 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
628 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
629 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
630 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
631 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
632 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
635 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
637 struct ieee80211_channel *chan);
639 /* sysfs attributes */
640 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
642 struct mac80211_hwsim_data *data = dat;
643 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
645 struct ieee80211_pspoll *pspoll;
650 wiphy_debug(data->hw->wiphy,
651 "%s: send PS-Poll to %pM for aid %d\n",
652 __func__, vp->bssid, vp->aid);
654 skb = dev_alloc_skb(sizeof(*pspoll));
657 pspoll = (void *) skb_put(skb, sizeof(*pspoll));
658 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
659 IEEE80211_STYPE_PSPOLL |
661 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
662 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
663 memcpy(pspoll->ta, mac, ETH_ALEN);
666 mac80211_hwsim_tx_frame(data->hw, skb,
667 rcu_dereference(vif->chanctx_conf)->def.chan);
671 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
672 struct ieee80211_vif *vif, int ps)
674 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
676 struct ieee80211_hdr *hdr;
681 wiphy_debug(data->hw->wiphy,
682 "%s: send data::nullfunc to %pM ps=%d\n",
683 __func__, vp->bssid, ps);
685 skb = dev_alloc_skb(sizeof(*hdr));
688 hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
689 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
690 IEEE80211_STYPE_NULLFUNC |
691 (ps ? IEEE80211_FCTL_PM : 0));
692 hdr->duration_id = cpu_to_le16(0);
693 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
694 memcpy(hdr->addr2, mac, ETH_ALEN);
695 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
698 mac80211_hwsim_tx_frame(data->hw, skb,
699 rcu_dereference(vif->chanctx_conf)->def.chan);
704 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
705 struct ieee80211_vif *vif)
707 struct mac80211_hwsim_data *data = dat;
708 hwsim_send_nullfunc(data, mac, vif, 1);
711 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
712 struct ieee80211_vif *vif)
714 struct mac80211_hwsim_data *data = dat;
715 hwsim_send_nullfunc(data, mac, vif, 0);
718 static int hwsim_fops_ps_read(void *dat, u64 *val)
720 struct mac80211_hwsim_data *data = dat;
725 static int hwsim_fops_ps_write(void *dat, u64 val)
727 struct mac80211_hwsim_data *data = dat;
730 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
731 val != PS_MANUAL_POLL)
738 if (val == PS_MANUAL_POLL) {
739 ieee80211_iterate_active_interfaces_atomic(
740 data->hw, IEEE80211_IFACE_ITER_NORMAL,
741 hwsim_send_ps_poll, data);
742 data->ps_poll_pending = true;
743 } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
744 ieee80211_iterate_active_interfaces_atomic(
745 data->hw, IEEE80211_IFACE_ITER_NORMAL,
746 hwsim_send_nullfunc_ps, data);
747 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
748 ieee80211_iterate_active_interfaces_atomic(
749 data->hw, IEEE80211_IFACE_ITER_NORMAL,
750 hwsim_send_nullfunc_no_ps, data);
757 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
760 static int hwsim_write_simulate_radar(void *dat, u64 val)
762 struct mac80211_hwsim_data *data = dat;
764 ieee80211_radar_detected(data->hw);
769 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
770 hwsim_write_simulate_radar, "%llu\n");
772 static int hwsim_fops_group_read(void *dat, u64 *val)
774 struct mac80211_hwsim_data *data = dat;
779 static int hwsim_fops_group_write(void *dat, u64 val)
781 struct mac80211_hwsim_data *data = dat;
786 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
787 hwsim_fops_group_read, hwsim_fops_group_write,
790 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
791 struct net_device *dev)
793 /* TODO: allow packet injection */
798 static inline u64 mac80211_hwsim_get_tsf_raw(void)
800 return ktime_to_us(ktime_get_real());
803 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
805 u64 now = mac80211_hwsim_get_tsf_raw();
806 return cpu_to_le64(now + data->tsf_offset);
809 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
810 struct ieee80211_vif *vif)
812 struct mac80211_hwsim_data *data = hw->priv;
813 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
816 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
817 struct ieee80211_vif *vif, u64 tsf)
819 struct mac80211_hwsim_data *data = hw->priv;
820 u64 now = mac80211_hwsim_get_tsf(hw, vif);
821 u32 bcn_int = data->beacon_int;
822 u64 delta = abs(tsf - now);
824 /* adjust after beaconing with new timestamp at old TBTT */
826 data->tsf_offset += delta;
827 data->bcn_delta = do_div(delta, bcn_int);
829 data->tsf_offset -= delta;
830 data->bcn_delta = -do_div(delta, bcn_int);
834 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
835 struct sk_buff *tx_skb,
836 struct ieee80211_channel *chan)
838 struct mac80211_hwsim_data *data = hw->priv;
840 struct hwsim_radiotap_hdr *hdr;
842 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
843 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
845 if (WARN_ON(!txrate))
848 if (!netif_running(hwsim_mon))
851 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
855 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
856 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
858 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
859 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
860 (1 << IEEE80211_RADIOTAP_RATE) |
861 (1 << IEEE80211_RADIOTAP_TSFT) |
862 (1 << IEEE80211_RADIOTAP_CHANNEL));
863 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
865 hdr->rt_rate = txrate->bitrate / 5;
866 hdr->rt_channel = cpu_to_le16(chan->center_freq);
867 flags = IEEE80211_CHAN_2GHZ;
868 if (txrate->flags & IEEE80211_RATE_ERP_G)
869 flags |= IEEE80211_CHAN_OFDM;
871 flags |= IEEE80211_CHAN_CCK;
872 hdr->rt_chbitmask = cpu_to_le16(flags);
874 skb->dev = hwsim_mon;
875 skb_reset_mac_header(skb);
876 skb->ip_summed = CHECKSUM_UNNECESSARY;
877 skb->pkt_type = PACKET_OTHERHOST;
878 skb->protocol = htons(ETH_P_802_2);
879 memset(skb->cb, 0, sizeof(skb->cb));
884 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
888 struct hwsim_radiotap_ack_hdr *hdr;
890 struct ieee80211_hdr *hdr11;
892 if (!netif_running(hwsim_mon))
895 skb = dev_alloc_skb(100);
899 hdr = (struct hwsim_radiotap_ack_hdr *) skb_put(skb, sizeof(*hdr));
900 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
902 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
903 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
904 (1 << IEEE80211_RADIOTAP_CHANNEL));
907 hdr->rt_channel = cpu_to_le16(chan->center_freq);
908 flags = IEEE80211_CHAN_2GHZ;
909 hdr->rt_chbitmask = cpu_to_le16(flags);
911 hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
912 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
913 IEEE80211_STYPE_ACK);
914 hdr11->duration_id = cpu_to_le16(0);
915 memcpy(hdr11->addr1, addr, ETH_ALEN);
917 skb->dev = hwsim_mon;
918 skb_reset_mac_header(skb);
919 skb->ip_summed = CHECKSUM_UNNECESSARY;
920 skb->pkt_type = PACKET_OTHERHOST;
921 skb->protocol = htons(ETH_P_802_2);
922 memset(skb->cb, 0, sizeof(skb->cb));
926 struct mac80211_hwsim_addr_match_data {
931 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
932 struct ieee80211_vif *vif)
934 struct mac80211_hwsim_addr_match_data *md = data;
936 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
940 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
943 struct mac80211_hwsim_addr_match_data md = {
947 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
950 memcpy(md.addr, addr, ETH_ALEN);
952 ieee80211_iterate_active_interfaces_atomic(data->hw,
953 IEEE80211_IFACE_ITER_NORMAL,
954 mac80211_hwsim_addr_iter,
960 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
969 /* TODO: accept (some) Beacons by default and other frames only
970 * if pending PS-Poll has been sent */
973 /* Allow unicast frames to own address if there is a pending
975 if (data->ps_poll_pending &&
976 mac80211_hwsim_addr_match(data, skb->data + 4)) {
977 data->ps_poll_pending = false;
986 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
987 struct sk_buff *my_skb,
991 struct mac80211_hwsim_data *data = hw->priv;
992 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
993 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
995 unsigned int hwsim_flags = 0;
997 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1000 if (data->ps != PS_DISABLED)
1001 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1002 /* If the queue contains MAX_QUEUE skb's drop some */
1003 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1004 /* Droping until WARN_QUEUE level */
1005 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1006 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1011 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1013 goto nla_put_failure;
1015 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1017 if (msg_head == NULL) {
1018 printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
1019 goto nla_put_failure;
1022 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1023 ETH_ALEN, data->addresses[1].addr))
1024 goto nla_put_failure;
1026 /* We get the skb->data */
1027 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1028 goto nla_put_failure;
1030 /* We get the flags for this transmission, and we translate them to
1033 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1034 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1036 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1037 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1039 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1040 goto nla_put_failure;
1042 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1043 goto nla_put_failure;
1045 /* We get the tx control (rate and retries) info*/
1047 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1048 tx_attempts[i].idx = info->status.rates[i].idx;
1049 tx_attempts[i].count = info->status.rates[i].count;
1052 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1053 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1055 goto nla_put_failure;
1057 /* We create a cookie to identify this skb */
1058 data->pending_cookie++;
1059 cookie = data->pending_cookie;
1060 info->rate_driver_data[0] = (void *)cookie;
1061 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1062 goto nla_put_failure;
1064 genlmsg_end(skb, msg_head);
1065 if (genlmsg_unicast(&init_net, skb, dst_portid))
1066 goto err_free_txskb;
1068 /* Enqueue the packet */
1069 skb_queue_tail(&data->pending, my_skb);
1071 data->tx_bytes += my_skb->len;
1077 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
1078 ieee80211_free_txskb(hw, my_skb);
1082 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1083 struct ieee80211_channel *c2)
1088 return c1->center_freq == c2->center_freq;
1091 struct tx_iter_data {
1092 struct ieee80211_channel *channel;
1096 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1097 struct ieee80211_vif *vif)
1099 struct tx_iter_data *data = _data;
1101 if (!vif->chanctx_conf)
1104 if (!hwsim_chans_compat(data->channel,
1105 rcu_dereference(vif->chanctx_conf)->def.chan))
1108 data->receive = true;
1111 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1114 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1116 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1117 * (but you should use a valid OUI, not that)
1119 * If anyone wants to 'donate' a radiotap OUI/subns code
1120 * please send a patch removing this #ifdef and changing
1121 * the values accordingly.
1123 #ifdef HWSIM_RADIOTAP_OUI
1124 struct ieee80211_vendor_radiotap *rtap;
1127 * Note that this code requires the headroom in the SKB
1128 * that was allocated earlier.
1130 rtap = (void *)skb_push(skb, sizeof(*rtap) + 8 + 4);
1131 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1132 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1133 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1137 * Radiotap vendor namespaces can (and should) also be
1138 * split into fields by using the standard radiotap
1139 * presence bitmap mechanism. Use just BIT(0) here for
1140 * the presence bitmap.
1142 rtap->present = BIT(0);
1143 /* We have 8 bytes of (dummy) data */
1145 /* For testing, also require it to be aligned */
1147 /* And also test that padding works, 4 bytes */
1150 memcpy(rtap->data, "ABCDEFGH", 8);
1151 /* make sure to clear padding, mac80211 doesn't */
1152 memset(rtap->data + 8, 0, 4);
1154 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1158 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1159 struct sk_buff *skb,
1160 struct ieee80211_channel *chan)
1162 struct mac80211_hwsim_data *data = hw->priv, *data2;
1164 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1165 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1166 struct ieee80211_rx_status rx_status;
1169 memset(&rx_status, 0, sizeof(rx_status));
1170 rx_status.flag |= RX_FLAG_MACTIME_START;
1171 rx_status.freq = chan->center_freq;
1172 rx_status.band = chan->band;
1173 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1174 rx_status.rate_idx =
1175 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1177 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1178 rx_status.flag |= RX_FLAG_VHT;
1180 rx_status.rate_idx = info->control.rates[0].idx;
1181 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1182 rx_status.flag |= RX_FLAG_HT;
1184 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1185 rx_status.flag |= RX_FLAG_40MHZ;
1186 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1187 rx_status.flag |= RX_FLAG_SHORT_GI;
1188 /* TODO: simulate real signal strength (and optional packet loss) */
1189 rx_status.signal = data->power_level - 50;
1191 if (data->ps != PS_DISABLED)
1192 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1194 /* release the skb's source info */
1202 * Get absolute mactime here so all HWs RX at the "same time", and
1203 * absolute TX time for beacon mactime so the timestamp matches.
1204 * Giving beacons a different mactime than non-beacons looks messy, but
1205 * it helps the Toffset be exact and a ~10us mactime discrepancy
1206 * probably doesn't really matter.
1208 if (ieee80211_is_beacon(hdr->frame_control) ||
1209 ieee80211_is_probe_resp(hdr->frame_control))
1210 now = data->abs_bcn_ts;
1212 now = mac80211_hwsim_get_tsf_raw();
1214 /* Copy skb to all enabled radios that are on the current frequency */
1215 spin_lock(&hwsim_radio_lock);
1216 list_for_each_entry(data2, &hwsim_radios, list) {
1217 struct sk_buff *nskb;
1218 struct tx_iter_data tx_iter_data = {
1226 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1227 !hwsim_ps_rx_ok(data2, skb))
1230 if (!(data->group & data2->group))
1233 if (data->netgroup != data2->netgroup)
1236 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1237 !hwsim_chans_compat(chan, data2->channel)) {
1238 ieee80211_iterate_active_interfaces_atomic(
1239 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1240 mac80211_hwsim_tx_iter, &tx_iter_data);
1241 if (!tx_iter_data.receive)
1246 * reserve some space for our vendor and the normal
1247 * radiotap header, since we're copying anyway
1249 if (skb->len < PAGE_SIZE && paged_rx) {
1250 struct page *page = alloc_page(GFP_ATOMIC);
1255 nskb = dev_alloc_skb(128);
1261 memcpy(page_address(page), skb->data, skb->len);
1262 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1264 nskb = skb_copy(skb, GFP_ATOMIC);
1269 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1272 rx_status.mactime = now + data2->tsf_offset;
1274 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1276 mac80211_hwsim_add_vendor_rtap(nskb);
1279 data2->rx_bytes += nskb->len;
1280 ieee80211_rx_irqsafe(data2->hw, nskb);
1282 spin_unlock(&hwsim_radio_lock);
1287 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1288 struct ieee80211_tx_control *control,
1289 struct sk_buff *skb)
1291 struct mac80211_hwsim_data *data = hw->priv;
1292 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1293 struct ieee80211_hdr *hdr = (void *)skb->data;
1294 struct ieee80211_chanctx_conf *chanctx_conf;
1295 struct ieee80211_channel *channel;
1299 if (WARN_ON(skb->len < 10)) {
1300 /* Should not happen; just a sanity check for addr1 use */
1301 ieee80211_free_txskb(hw, skb);
1305 if (!data->use_chanctx) {
1306 channel = data->channel;
1307 } else if (txi->hw_queue == 4) {
1308 channel = data->tmp_chan;
1310 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1312 channel = chanctx_conf->def.chan;
1317 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1318 ieee80211_free_txskb(hw, skb);
1322 if (data->idle && !data->tmp_chan) {
1323 wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
1324 ieee80211_free_txskb(hw, skb);
1328 if (txi->control.vif)
1329 hwsim_check_magic(txi->control.vif);
1331 hwsim_check_sta_magic(control->sta);
1333 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1334 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1336 ARRAY_SIZE(txi->control.rates));
1338 txi->rate_driver_data[0] = channel;
1340 if (skb->len >= 24 + 8 &&
1341 ieee80211_is_probe_resp(hdr->frame_control)) {
1342 /* fake header transmission time */
1343 struct ieee80211_mgmt *mgmt;
1344 struct ieee80211_rate *txrate;
1347 mgmt = (struct ieee80211_mgmt *)skb->data;
1348 txrate = ieee80211_get_tx_rate(hw, txi);
1349 ts = mac80211_hwsim_get_tsf_raw();
1350 mgmt->u.probe_resp.timestamp =
1351 cpu_to_le64(ts + data->tsf_offset +
1352 24 * 8 * 10 / txrate->bitrate);
1355 mac80211_hwsim_monitor_rx(hw, skb, channel);
1357 /* wmediumd mode check */
1358 _portid = ACCESS_ONCE(wmediumd_portid);
1361 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1363 /* NO wmediumd detected, perfect medium simulation */
1365 data->tx_bytes += skb->len;
1366 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1368 if (ack && skb->len >= 16)
1369 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1371 ieee80211_tx_info_clear_status(txi);
1373 /* frame was transmitted at most favorable rate at first attempt */
1374 txi->control.rates[0].count = 1;
1375 txi->control.rates[1].idx = -1;
1377 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1378 txi->flags |= IEEE80211_TX_STAT_ACK;
1379 ieee80211_tx_status_irqsafe(hw, skb);
1383 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1385 struct mac80211_hwsim_data *data = hw->priv;
1386 wiphy_debug(hw->wiphy, "%s\n", __func__);
1387 data->started = true;
1392 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1394 struct mac80211_hwsim_data *data = hw->priv;
1395 data->started = false;
1396 tasklet_hrtimer_cancel(&data->beacon_timer);
1397 wiphy_debug(hw->wiphy, "%s\n", __func__);
1401 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1402 struct ieee80211_vif *vif)
1404 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1405 __func__, ieee80211_vif_type_p2p(vif),
1407 hwsim_set_magic(vif);
1410 vif->hw_queue[IEEE80211_AC_VO] = 0;
1411 vif->hw_queue[IEEE80211_AC_VI] = 1;
1412 vif->hw_queue[IEEE80211_AC_BE] = 2;
1413 vif->hw_queue[IEEE80211_AC_BK] = 3;
1419 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1420 struct ieee80211_vif *vif,
1421 enum nl80211_iftype newtype,
1424 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1425 wiphy_debug(hw->wiphy,
1426 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1427 __func__, ieee80211_vif_type_p2p(vif),
1428 newtype, vif->addr);
1429 hwsim_check_magic(vif);
1432 * interface may change from non-AP to AP in
1433 * which case this needs to be set up again
1440 static void mac80211_hwsim_remove_interface(
1441 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1443 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1444 __func__, ieee80211_vif_type_p2p(vif),
1446 hwsim_check_magic(vif);
1447 hwsim_clear_magic(vif);
1450 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1451 struct sk_buff *skb,
1452 struct ieee80211_channel *chan)
1454 u32 _pid = ACCESS_ONCE(wmediumd_portid);
1456 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1457 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1458 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1460 ARRAY_SIZE(txi->control.rates));
1463 mac80211_hwsim_monitor_rx(hw, skb, chan);
1466 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1468 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1472 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1473 struct ieee80211_vif *vif)
1475 struct mac80211_hwsim_data *data = arg;
1476 struct ieee80211_hw *hw = data->hw;
1477 struct ieee80211_tx_info *info;
1478 struct ieee80211_rate *txrate;
1479 struct ieee80211_mgmt *mgmt;
1480 struct sk_buff *skb;
1482 hwsim_check_magic(vif);
1484 if (vif->type != NL80211_IFTYPE_AP &&
1485 vif->type != NL80211_IFTYPE_MESH_POINT &&
1486 vif->type != NL80211_IFTYPE_ADHOC)
1489 skb = ieee80211_beacon_get(hw, vif);
1492 info = IEEE80211_SKB_CB(skb);
1493 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1494 ieee80211_get_tx_rates(vif, NULL, skb,
1495 info->control.rates,
1496 ARRAY_SIZE(info->control.rates));
1498 txrate = ieee80211_get_tx_rate(hw, info);
1500 mgmt = (struct ieee80211_mgmt *) skb->data;
1501 /* fake header transmission time */
1502 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1503 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1505 24 * 8 * 10 / txrate->bitrate);
1507 mac80211_hwsim_tx_frame(hw, skb,
1508 rcu_dereference(vif->chanctx_conf)->def.chan);
1510 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1511 ieee80211_csa_finish(vif);
1514 static enum hrtimer_restart
1515 mac80211_hwsim_beacon(struct hrtimer *timer)
1517 struct mac80211_hwsim_data *data =
1518 container_of(timer, struct mac80211_hwsim_data,
1519 beacon_timer.timer);
1520 struct ieee80211_hw *hw = data->hw;
1521 u64 bcn_int = data->beacon_int;
1527 ieee80211_iterate_active_interfaces_atomic(
1528 hw, IEEE80211_IFACE_ITER_NORMAL,
1529 mac80211_hwsim_beacon_tx, data);
1531 /* beacon at new TBTT + beacon interval */
1532 if (data->bcn_delta) {
1533 bcn_int -= data->bcn_delta;
1534 data->bcn_delta = 0;
1537 next_bcn = ktime_add(hrtimer_get_expires(timer),
1538 ns_to_ktime(bcn_int * 1000));
1539 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1541 return HRTIMER_NORESTART;
1544 static const char * const hwsim_chanwidths[] = {
1545 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1546 [NL80211_CHAN_WIDTH_20] = "ht20",
1547 [NL80211_CHAN_WIDTH_40] = "ht40",
1548 [NL80211_CHAN_WIDTH_80] = "vht80",
1549 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1550 [NL80211_CHAN_WIDTH_160] = "vht160",
1553 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1555 struct mac80211_hwsim_data *data = hw->priv;
1556 struct ieee80211_conf *conf = &hw->conf;
1557 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1558 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1559 [IEEE80211_SMPS_OFF] = "off",
1560 [IEEE80211_SMPS_STATIC] = "static",
1561 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1564 if (conf->chandef.chan)
1565 wiphy_debug(hw->wiphy,
1566 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1568 conf->chandef.chan->center_freq,
1569 conf->chandef.center_freq1,
1570 conf->chandef.center_freq2,
1571 hwsim_chanwidths[conf->chandef.width],
1572 !!(conf->flags & IEEE80211_CONF_IDLE),
1573 !!(conf->flags & IEEE80211_CONF_PS),
1574 smps_modes[conf->smps_mode]);
1576 wiphy_debug(hw->wiphy,
1577 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1579 !!(conf->flags & IEEE80211_CONF_IDLE),
1580 !!(conf->flags & IEEE80211_CONF_PS),
1581 smps_modes[conf->smps_mode]);
1583 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1585 data->channel = conf->chandef.chan;
1587 WARN_ON(data->channel && data->use_chanctx);
1589 data->power_level = conf->power_level;
1590 if (!data->started || !data->beacon_int)
1591 tasklet_hrtimer_cancel(&data->beacon_timer);
1592 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
1593 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1594 u32 bcn_int = data->beacon_int;
1595 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1597 tasklet_hrtimer_start(&data->beacon_timer,
1598 ns_to_ktime(until_tbtt * 1000),
1606 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1607 unsigned int changed_flags,
1608 unsigned int *total_flags,u64 multicast)
1610 struct mac80211_hwsim_data *data = hw->priv;
1612 wiphy_debug(hw->wiphy, "%s\n", __func__);
1614 data->rx_filter = 0;
1615 if (*total_flags & FIF_ALLMULTI)
1616 data->rx_filter |= FIF_ALLMULTI;
1618 *total_flags = data->rx_filter;
1621 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1622 struct ieee80211_vif *vif)
1624 unsigned int *count = data;
1625 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1631 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1632 struct ieee80211_vif *vif,
1633 struct ieee80211_bss_conf *info,
1636 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1637 struct mac80211_hwsim_data *data = hw->priv;
1639 hwsim_check_magic(vif);
1641 wiphy_debug(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1642 __func__, changed, vif->addr);
1644 if (changed & BSS_CHANGED_BSSID) {
1645 wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
1646 __func__, info->bssid);
1647 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1650 if (changed & BSS_CHANGED_ASSOC) {
1651 wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1652 info->assoc, info->aid);
1653 vp->assoc = info->assoc;
1654 vp->aid = info->aid;
1657 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1658 wiphy_debug(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1659 info->enable_beacon, info->beacon_int);
1660 vp->bcn_en = info->enable_beacon;
1661 if (data->started &&
1662 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1663 info->enable_beacon) {
1664 u64 tsf, until_tbtt;
1666 data->beacon_int = info->beacon_int * 1024;
1667 tsf = mac80211_hwsim_get_tsf(hw, vif);
1668 bcn_int = data->beacon_int;
1669 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1670 tasklet_hrtimer_start(&data->beacon_timer,
1671 ns_to_ktime(until_tbtt * 1000),
1673 } else if (!info->enable_beacon) {
1674 unsigned int count = 0;
1675 ieee80211_iterate_active_interfaces_atomic(
1676 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1677 mac80211_hwsim_bcn_en_iter, &count);
1678 wiphy_debug(hw->wiphy, " beaconing vifs remaining: %u",
1681 tasklet_hrtimer_cancel(&data->beacon_timer);
1682 data->beacon_int = 0;
1687 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1688 wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
1689 info->use_cts_prot);
1692 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1693 wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
1694 info->use_short_preamble);
1697 if (changed & BSS_CHANGED_ERP_SLOT) {
1698 wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1701 if (changed & BSS_CHANGED_HT) {
1702 wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
1703 info->ht_operation_mode);
1706 if (changed & BSS_CHANGED_BASIC_RATES) {
1707 wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1708 (unsigned long long) info->basic_rates);
1711 if (changed & BSS_CHANGED_TXPOWER)
1712 wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1715 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1716 struct ieee80211_vif *vif,
1717 struct ieee80211_sta *sta)
1719 hwsim_check_magic(vif);
1720 hwsim_set_sta_magic(sta);
1725 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1726 struct ieee80211_vif *vif,
1727 struct ieee80211_sta *sta)
1729 hwsim_check_magic(vif);
1730 hwsim_clear_sta_magic(sta);
1735 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1736 struct ieee80211_vif *vif,
1737 enum sta_notify_cmd cmd,
1738 struct ieee80211_sta *sta)
1740 hwsim_check_magic(vif);
1743 case STA_NOTIFY_SLEEP:
1744 case STA_NOTIFY_AWAKE:
1745 /* TODO: make good use of these flags */
1748 WARN(1, "Invalid sta notify: %d\n", cmd);
1753 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1754 struct ieee80211_sta *sta,
1757 hwsim_check_sta_magic(sta);
1761 static int mac80211_hwsim_conf_tx(
1762 struct ieee80211_hw *hw,
1763 struct ieee80211_vif *vif, u16 queue,
1764 const struct ieee80211_tx_queue_params *params)
1766 wiphy_debug(hw->wiphy,
1767 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1769 params->txop, params->cw_min,
1770 params->cw_max, params->aifs);
1774 static int mac80211_hwsim_get_survey(
1775 struct ieee80211_hw *hw, int idx,
1776 struct survey_info *survey)
1778 struct ieee80211_conf *conf = &hw->conf;
1780 wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
1785 /* Current channel */
1786 survey->channel = conf->chandef.chan;
1789 * Magically conjured noise level --- this is only ok for simulated hardware.
1791 * A real driver which cannot determine the real channel noise MUST NOT
1792 * report any noise, especially not a magically conjured one :-)
1794 survey->filled = SURVEY_INFO_NOISE_DBM;
1795 survey->noise = -92;
1800 #ifdef CONFIG_NL80211_TESTMODE
1802 * This section contains example code for using netlink
1803 * attributes with the testmode command in nl80211.
1806 /* These enums need to be kept in sync with userspace */
1807 enum hwsim_testmode_attr {
1808 __HWSIM_TM_ATTR_INVALID = 0,
1809 HWSIM_TM_ATTR_CMD = 1,
1810 HWSIM_TM_ATTR_PS = 2,
1813 __HWSIM_TM_ATTR_AFTER_LAST,
1814 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1817 enum hwsim_testmode_cmd {
1818 HWSIM_TM_CMD_SET_PS = 0,
1819 HWSIM_TM_CMD_GET_PS = 1,
1820 HWSIM_TM_CMD_STOP_QUEUES = 2,
1821 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1824 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1825 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1826 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1829 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1830 struct ieee80211_vif *vif,
1831 void *data, int len)
1833 struct mac80211_hwsim_data *hwsim = hw->priv;
1834 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1835 struct sk_buff *skb;
1838 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1839 hwsim_testmode_policy);
1843 if (!tb[HWSIM_TM_ATTR_CMD])
1846 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1847 case HWSIM_TM_CMD_SET_PS:
1848 if (!tb[HWSIM_TM_ATTR_PS])
1850 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1851 return hwsim_fops_ps_write(hwsim, ps);
1852 case HWSIM_TM_CMD_GET_PS:
1853 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1854 nla_total_size(sizeof(u32)));
1857 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1858 goto nla_put_failure;
1859 return cfg80211_testmode_reply(skb);
1860 case HWSIM_TM_CMD_STOP_QUEUES:
1861 ieee80211_stop_queues(hw);
1863 case HWSIM_TM_CMD_WAKE_QUEUES:
1864 ieee80211_wake_queues(hw);
1876 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1877 struct ieee80211_vif *vif,
1878 struct ieee80211_ampdu_params *params)
1880 struct ieee80211_sta *sta = params->sta;
1881 enum ieee80211_ampdu_mlme_action action = params->action;
1882 u16 tid = params->tid;
1885 case IEEE80211_AMPDU_TX_START:
1886 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1888 case IEEE80211_AMPDU_TX_STOP_CONT:
1889 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1890 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1891 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1893 case IEEE80211_AMPDU_TX_OPERATIONAL:
1895 case IEEE80211_AMPDU_RX_START:
1896 case IEEE80211_AMPDU_RX_STOP:
1905 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
1906 struct ieee80211_vif *vif,
1907 u32 queues, bool drop)
1909 /* Not implemented, queues only on kernel side */
1912 static void hw_scan_work(struct work_struct *work)
1914 struct mac80211_hwsim_data *hwsim =
1915 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
1916 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
1919 mutex_lock(&hwsim->mutex);
1920 if (hwsim->scan_chan_idx >= req->n_channels) {
1921 wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
1922 ieee80211_scan_completed(hwsim->hw, false);
1923 hwsim->hw_scan_request = NULL;
1924 hwsim->hw_scan_vif = NULL;
1925 hwsim->tmp_chan = NULL;
1926 mutex_unlock(&hwsim->mutex);
1930 wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
1931 req->channels[hwsim->scan_chan_idx]->center_freq);
1933 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
1934 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
1935 IEEE80211_CHAN_RADAR) ||
1941 for (i = 0; i < req->n_ssids; i++) {
1942 struct sk_buff *probe;
1943 struct ieee80211_mgmt *mgmt;
1945 probe = ieee80211_probereq_get(hwsim->hw,
1948 req->ssids[i].ssid_len,
1953 mgmt = (struct ieee80211_mgmt *) probe->data;
1954 memcpy(mgmt->da, req->bssid, ETH_ALEN);
1955 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
1958 memcpy(skb_put(probe, req->ie_len), req->ie,
1962 mac80211_hwsim_tx_frame(hwsim->hw, probe,
1967 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
1968 msecs_to_jiffies(dwell));
1969 hwsim->scan_chan_idx++;
1970 mutex_unlock(&hwsim->mutex);
1973 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
1974 struct ieee80211_vif *vif,
1975 struct ieee80211_scan_request *hw_req)
1977 struct mac80211_hwsim_data *hwsim = hw->priv;
1978 struct cfg80211_scan_request *req = &hw_req->req;
1980 mutex_lock(&hwsim->mutex);
1981 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
1982 mutex_unlock(&hwsim->mutex);
1985 hwsim->hw_scan_request = req;
1986 hwsim->hw_scan_vif = vif;
1987 hwsim->scan_chan_idx = 0;
1988 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
1989 get_random_mask_addr(hwsim->scan_addr,
1990 hw_req->req.mac_addr,
1991 hw_req->req.mac_addr_mask);
1993 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
1994 mutex_unlock(&hwsim->mutex);
1996 wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
1998 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2003 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2004 struct ieee80211_vif *vif)
2006 struct mac80211_hwsim_data *hwsim = hw->priv;
2008 wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
2010 cancel_delayed_work_sync(&hwsim->hw_scan);
2012 mutex_lock(&hwsim->mutex);
2013 ieee80211_scan_completed(hwsim->hw, true);
2014 hwsim->tmp_chan = NULL;
2015 hwsim->hw_scan_request = NULL;
2016 hwsim->hw_scan_vif = NULL;
2017 mutex_unlock(&hwsim->mutex);
2020 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2021 struct ieee80211_vif *vif,
2024 struct mac80211_hwsim_data *hwsim = hw->priv;
2026 mutex_lock(&hwsim->mutex);
2028 if (hwsim->scanning) {
2029 printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
2033 printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
2035 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2036 hwsim->scanning = true;
2039 mutex_unlock(&hwsim->mutex);
2042 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2043 struct ieee80211_vif *vif)
2045 struct mac80211_hwsim_data *hwsim = hw->priv;
2047 mutex_lock(&hwsim->mutex);
2049 printk(KERN_DEBUG "hwsim sw_scan_complete\n");
2050 hwsim->scanning = false;
2051 eth_zero_addr(hwsim->scan_addr);
2053 mutex_unlock(&hwsim->mutex);
2056 static void hw_roc_start(struct work_struct *work)
2058 struct mac80211_hwsim_data *hwsim =
2059 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2061 mutex_lock(&hwsim->mutex);
2063 wiphy_debug(hwsim->hw->wiphy, "hwsim ROC begins\n");
2064 hwsim->tmp_chan = hwsim->roc_chan;
2065 ieee80211_ready_on_channel(hwsim->hw);
2067 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2068 msecs_to_jiffies(hwsim->roc_duration));
2070 mutex_unlock(&hwsim->mutex);
2073 static void hw_roc_done(struct work_struct *work)
2075 struct mac80211_hwsim_data *hwsim =
2076 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2078 mutex_lock(&hwsim->mutex);
2079 ieee80211_remain_on_channel_expired(hwsim->hw);
2080 hwsim->tmp_chan = NULL;
2081 mutex_unlock(&hwsim->mutex);
2083 wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
2086 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2087 struct ieee80211_vif *vif,
2088 struct ieee80211_channel *chan,
2090 enum ieee80211_roc_type type)
2092 struct mac80211_hwsim_data *hwsim = hw->priv;
2094 mutex_lock(&hwsim->mutex);
2095 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2096 mutex_unlock(&hwsim->mutex);
2100 hwsim->roc_chan = chan;
2101 hwsim->roc_duration = duration;
2102 mutex_unlock(&hwsim->mutex);
2104 wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2105 chan->center_freq, duration);
2106 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2111 static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2113 struct mac80211_hwsim_data *hwsim = hw->priv;
2115 cancel_delayed_work_sync(&hwsim->roc_start);
2116 cancel_delayed_work_sync(&hwsim->roc_done);
2118 mutex_lock(&hwsim->mutex);
2119 hwsim->tmp_chan = NULL;
2120 mutex_unlock(&hwsim->mutex);
2122 wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
2127 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2128 struct ieee80211_chanctx_conf *ctx)
2130 hwsim_set_chanctx_magic(ctx);
2131 wiphy_debug(hw->wiphy,
2132 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2133 ctx->def.chan->center_freq, ctx->def.width,
2134 ctx->def.center_freq1, ctx->def.center_freq2);
2138 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2139 struct ieee80211_chanctx_conf *ctx)
2141 wiphy_debug(hw->wiphy,
2142 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2143 ctx->def.chan->center_freq, ctx->def.width,
2144 ctx->def.center_freq1, ctx->def.center_freq2);
2145 hwsim_check_chanctx_magic(ctx);
2146 hwsim_clear_chanctx_magic(ctx);
2149 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2150 struct ieee80211_chanctx_conf *ctx,
2153 hwsim_check_chanctx_magic(ctx);
2154 wiphy_debug(hw->wiphy,
2155 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2156 ctx->def.chan->center_freq, ctx->def.width,
2157 ctx->def.center_freq1, ctx->def.center_freq2);
2160 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2161 struct ieee80211_vif *vif,
2162 struct ieee80211_chanctx_conf *ctx)
2164 hwsim_check_magic(vif);
2165 hwsim_check_chanctx_magic(ctx);
2170 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2171 struct ieee80211_vif *vif,
2172 struct ieee80211_chanctx_conf *ctx)
2174 hwsim_check_magic(vif);
2175 hwsim_check_chanctx_magic(ctx);
2178 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2190 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2192 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2193 struct ieee80211_vif *vif,
2196 if (sset == ETH_SS_STATS)
2197 memcpy(data, *mac80211_hwsim_gstrings_stats,
2198 sizeof(mac80211_hwsim_gstrings_stats));
2201 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2202 struct ieee80211_vif *vif, int sset)
2204 if (sset == ETH_SS_STATS)
2205 return MAC80211_HWSIM_SSTATS_LEN;
2209 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2210 struct ieee80211_vif *vif,
2211 struct ethtool_stats *stats, u64 *data)
2213 struct mac80211_hwsim_data *ar = hw->priv;
2216 data[i++] = ar->tx_pkts;
2217 data[i++] = ar->tx_bytes;
2218 data[i++] = ar->rx_pkts;
2219 data[i++] = ar->rx_bytes;
2220 data[i++] = ar->tx_dropped;
2221 data[i++] = ar->tx_failed;
2223 data[i++] = ar->group;
2224 data[i++] = ar->power_level;
2226 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2229 static const struct ieee80211_ops mac80211_hwsim_ops = {
2230 .tx = mac80211_hwsim_tx,
2231 .start = mac80211_hwsim_start,
2232 .stop = mac80211_hwsim_stop,
2233 .add_interface = mac80211_hwsim_add_interface,
2234 .change_interface = mac80211_hwsim_change_interface,
2235 .remove_interface = mac80211_hwsim_remove_interface,
2236 .config = mac80211_hwsim_config,
2237 .configure_filter = mac80211_hwsim_configure_filter,
2238 .bss_info_changed = mac80211_hwsim_bss_info_changed,
2239 .sta_add = mac80211_hwsim_sta_add,
2240 .sta_remove = mac80211_hwsim_sta_remove,
2241 .sta_notify = mac80211_hwsim_sta_notify,
2242 .set_tim = mac80211_hwsim_set_tim,
2243 .conf_tx = mac80211_hwsim_conf_tx,
2244 .get_survey = mac80211_hwsim_get_survey,
2245 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
2246 .ampdu_action = mac80211_hwsim_ampdu_action,
2247 .sw_scan_start = mac80211_hwsim_sw_scan,
2248 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2249 .flush = mac80211_hwsim_flush,
2250 .get_tsf = mac80211_hwsim_get_tsf,
2251 .set_tsf = mac80211_hwsim_set_tsf,
2252 .get_et_sset_count = mac80211_hwsim_get_et_sset_count,
2253 .get_et_stats = mac80211_hwsim_get_et_stats,
2254 .get_et_strings = mac80211_hwsim_get_et_strings,
2257 static struct ieee80211_ops mac80211_hwsim_mchan_ops;
2259 struct hwsim_new_radio_params {
2260 unsigned int channels;
2261 const char *reg_alpha2;
2262 const struct ieee80211_regdomain *regd;
2266 bool destroy_on_close;
2271 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2272 struct genl_info *info)
2275 genl_notify(&hwsim_genl_family, mcast_skb, info,
2276 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2278 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2279 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2282 static int append_radio_msg(struct sk_buff *skb, int id,
2283 struct hwsim_new_radio_params *param)
2287 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2291 if (param->channels) {
2292 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2297 if (param->reg_alpha2) {
2298 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2307 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2308 if (hwsim_world_regdom_custom[i] != param->regd)
2311 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2318 if (param->reg_strict) {
2319 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2324 if (param->p2p_device) {
2325 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2330 if (param->use_chanctx) {
2331 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2336 if (param->hwname) {
2337 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2338 strlen(param->hwname), param->hwname);
2346 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2347 struct hwsim_new_radio_params *param)
2349 struct sk_buff *mcast_skb;
2352 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2356 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2357 HWSIM_CMD_NEW_RADIO);
2361 if (append_radio_msg(mcast_skb, id, param) < 0)
2364 genlmsg_end(mcast_skb, data);
2366 hwsim_mcast_config_msg(mcast_skb, info);
2370 genlmsg_cancel(mcast_skb, data);
2371 nlmsg_free(mcast_skb);
2374 static int mac80211_hwsim_new_radio(struct genl_info *info,
2375 struct hwsim_new_radio_params *param)
2379 struct mac80211_hwsim_data *data;
2380 struct ieee80211_hw *hw;
2381 enum nl80211_band band;
2382 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2386 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2389 spin_lock_bh(&hwsim_radio_lock);
2390 idx = hwsim_radio_idx++;
2391 spin_unlock_bh(&hwsim_radio_lock);
2393 if (param->use_chanctx)
2394 ops = &mac80211_hwsim_mchan_ops;
2395 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2397 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw failed\n");
2403 net = genl_info_net(info);
2406 wiphy_net_set(hw->wiphy, net);
2411 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2412 if (IS_ERR(data->dev)) {
2414 "mac80211_hwsim: device_create failed (%ld)\n",
2415 PTR_ERR(data->dev));
2417 goto failed_drvdata;
2419 data->dev->driver = &mac80211_hwsim_driver.driver;
2420 err = device_bind_driver(data->dev);
2422 printk(KERN_DEBUG "mac80211_hwsim: device_bind_driver failed (%d)\n",
2427 skb_queue_head_init(&data->pending);
2429 SET_IEEE80211_DEV(hw, data->dev);
2430 eth_zero_addr(addr);
2434 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2435 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2436 data->addresses[1].addr[0] |= 0x40;
2437 hw->wiphy->n_addresses = 2;
2438 hw->wiphy->addresses = data->addresses;
2440 data->channels = param->channels;
2441 data->use_chanctx = param->use_chanctx;
2443 data->destroy_on_close = param->destroy_on_close;
2445 data->portid = info->snd_portid;
2447 if (data->use_chanctx) {
2448 hw->wiphy->max_scan_ssids = 255;
2449 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2450 hw->wiphy->max_remain_on_channel_duration = 1000;
2451 /* For channels > 1 DFS is not allowed */
2452 hw->wiphy->n_iface_combinations = 1;
2453 hw->wiphy->iface_combinations = &data->if_combination;
2454 if (param->p2p_device)
2455 data->if_combination = hwsim_if_comb_p2p_dev[0];
2457 data->if_combination = hwsim_if_comb[0];
2458 data->if_combination.num_different_channels = data->channels;
2459 } else if (param->p2p_device) {
2460 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2461 hw->wiphy->n_iface_combinations =
2462 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
2464 hw->wiphy->iface_combinations = hwsim_if_comb;
2465 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2468 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
2469 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2470 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2473 hw->offchannel_tx_hw_queue = 4;
2474 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2475 BIT(NL80211_IFTYPE_AP) |
2476 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2477 BIT(NL80211_IFTYPE_P2P_GO) |
2478 BIT(NL80211_IFTYPE_ADHOC) |
2479 BIT(NL80211_IFTYPE_MESH_POINT);
2481 if (param->p2p_device)
2482 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
2484 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2485 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2486 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2487 ieee80211_hw_set(hw, QUEUE_CONTROL);
2488 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2489 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2490 ieee80211_hw_set(hw, MFP_CAPABLE);
2491 ieee80211_hw_set(hw, SIGNAL_DBM);
2492 ieee80211_hw_set(hw, TDLS_WIDER_BW);
2494 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
2496 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2497 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2498 WIPHY_FLAG_AP_UAPSD |
2499 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
2500 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2501 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2502 NL80211_FEATURE_STATIC_SMPS |
2503 NL80211_FEATURE_DYNAMIC_SMPS |
2504 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
2505 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
2507 /* ask mac80211 to reserve space for magic */
2508 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2509 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2510 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2512 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2513 sizeof(hwsim_channels_2ghz));
2514 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2515 sizeof(hwsim_channels_5ghz));
2516 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2518 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
2519 struct ieee80211_supported_band *sband = &data->bands[band];
2521 case NL80211_BAND_2GHZ:
2522 sband->channels = data->channels_2ghz;
2523 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2524 sband->bitrates = data->rates;
2525 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2527 case NL80211_BAND_5GHZ:
2528 sband->channels = data->channels_5ghz;
2529 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2530 sband->bitrates = data->rates + 4;
2531 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
2533 sband->vht_cap.vht_supported = true;
2534 sband->vht_cap.cap =
2535 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2536 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2537 IEEE80211_VHT_CAP_RXLDPC |
2538 IEEE80211_VHT_CAP_SHORT_GI_80 |
2539 IEEE80211_VHT_CAP_SHORT_GI_160 |
2540 IEEE80211_VHT_CAP_TXSTBC |
2541 IEEE80211_VHT_CAP_RXSTBC_1 |
2542 IEEE80211_VHT_CAP_RXSTBC_2 |
2543 IEEE80211_VHT_CAP_RXSTBC_3 |
2544 IEEE80211_VHT_CAP_RXSTBC_4 |
2545 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2546 sband->vht_cap.vht_mcs.rx_mcs_map =
2547 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2548 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2549 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2550 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2551 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
2552 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2553 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
2554 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
2555 sband->vht_cap.vht_mcs.tx_mcs_map =
2556 sband->vht_cap.vht_mcs.rx_mcs_map;
2562 sband->ht_cap.ht_supported = true;
2563 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2564 IEEE80211_HT_CAP_GRN_FLD |
2565 IEEE80211_HT_CAP_SGI_20 |
2566 IEEE80211_HT_CAP_SGI_40 |
2567 IEEE80211_HT_CAP_DSSSCCK40;
2568 sband->ht_cap.ampdu_factor = 0x3;
2569 sband->ht_cap.ampdu_density = 0x6;
2570 memset(&sband->ht_cap.mcs, 0,
2571 sizeof(sband->ht_cap.mcs));
2572 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2573 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2574 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2576 hw->wiphy->bands[band] = sband;
2579 /* By default all radios belong to the first group */
2581 mutex_init(&data->mutex);
2583 data->netgroup = hwsim_net_get_netgroup(net);
2585 /* Enable frame retransmissions for lossy channels */
2587 hw->max_rate_tries = 11;
2589 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2590 hw->wiphy->n_vendor_commands =
2591 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2592 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2593 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2595 if (param->reg_strict)
2596 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
2598 data->regd = param->regd;
2599 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2600 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
2601 /* give the regulatory workqueue a chance to run */
2602 schedule_timeout_interruptible(1);
2606 ieee80211_hw_set(hw, NO_AUTO_VIF);
2608 err = ieee80211_register_hw(hw);
2610 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2615 wiphy_debug(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
2617 if (param->reg_alpha2) {
2618 data->alpha2[0] = param->reg_alpha2[0];
2619 data->alpha2[1] = param->reg_alpha2[1];
2620 regulatory_hint(hw->wiphy, param->reg_alpha2);
2623 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2624 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2625 debugfs_create_file("group", 0666, data->debugfs, data,
2627 if (!data->use_chanctx)
2628 debugfs_create_file("dfs_simulate_radar", 0222,
2630 data, &hwsim_simulate_radar);
2632 tasklet_hrtimer_init(&data->beacon_timer,
2633 mac80211_hwsim_beacon,
2634 CLOCK_MONOTONIC_RAW, HRTIMER_MODE_ABS);
2636 spin_lock_bh(&hwsim_radio_lock);
2637 list_add_tail(&data->list, &hwsim_radios);
2638 spin_unlock_bh(&hwsim_radio_lock);
2641 hwsim_mcast_new_radio(idx, info, param);
2646 device_release_driver(data->dev);
2648 device_unregister(data->dev);
2650 ieee80211_free_hw(hw);
2655 static void hwsim_mcast_del_radio(int id, const char *hwname,
2656 struct genl_info *info)
2658 struct sk_buff *skb;
2662 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2666 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2667 HWSIM_CMD_DEL_RADIO);
2671 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2675 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2680 genlmsg_end(skb, data);
2682 hwsim_mcast_config_msg(skb, info);
2690 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2692 struct genl_info *info)
2694 hwsim_mcast_del_radio(data->idx, hwname, info);
2695 debugfs_remove_recursive(data->debugfs);
2696 ieee80211_unregister_hw(data->hw);
2697 device_release_driver(data->dev);
2698 device_unregister(data->dev);
2699 ieee80211_free_hw(data->hw);
2702 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
2703 struct mac80211_hwsim_data *data,
2704 u32 portid, u32 seq,
2705 struct netlink_callback *cb, int flags)
2708 struct hwsim_new_radio_params param = { };
2709 int res = -EMSGSIZE;
2711 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
2712 HWSIM_CMD_GET_RADIO);
2717 genl_dump_check_consistent(cb, hdr, &hwsim_genl_family);
2719 if (data->alpha2[0] && data->alpha2[1])
2720 param.reg_alpha2 = data->alpha2;
2722 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
2723 REGULATORY_STRICT_REG);
2724 param.p2p_device = !!(data->hw->wiphy->interface_modes &
2725 BIT(NL80211_IFTYPE_P2P_DEVICE));
2726 param.use_chanctx = data->use_chanctx;
2727 param.regd = data->regd;
2728 param.channels = data->channels;
2729 param.hwname = wiphy_name(data->hw->wiphy);
2731 res = append_radio_msg(skb, data->idx, ¶m);
2735 genlmsg_end(skb, hdr);
2739 genlmsg_cancel(skb, hdr);
2743 static void mac80211_hwsim_free(void)
2745 struct mac80211_hwsim_data *data;
2747 spin_lock_bh(&hwsim_radio_lock);
2748 while ((data = list_first_entry_or_null(&hwsim_radios,
2749 struct mac80211_hwsim_data,
2751 list_del(&data->list);
2752 spin_unlock_bh(&hwsim_radio_lock);
2753 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
2755 spin_lock_bh(&hwsim_radio_lock);
2757 spin_unlock_bh(&hwsim_radio_lock);
2758 class_destroy(hwsim_class);
2761 static const struct net_device_ops hwsim_netdev_ops = {
2762 .ndo_start_xmit = hwsim_mon_xmit,
2763 .ndo_change_mtu = eth_change_mtu,
2764 .ndo_set_mac_address = eth_mac_addr,
2765 .ndo_validate_addr = eth_validate_addr,
2768 static void hwsim_mon_setup(struct net_device *dev)
2770 dev->netdev_ops = &hwsim_netdev_ops;
2771 dev->destructor = free_netdev;
2773 dev->priv_flags |= IFF_NO_QUEUE;
2774 dev->type = ARPHRD_IEEE80211_RADIOTAP;
2775 eth_zero_addr(dev->dev_addr);
2776 dev->dev_addr[0] = 0x12;
2779 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
2781 struct mac80211_hwsim_data *data;
2782 bool _found = false;
2784 spin_lock_bh(&hwsim_radio_lock);
2785 list_for_each_entry(data, &hwsim_radios, list) {
2786 if (memcmp(data->addresses[1].addr, addr, ETH_ALEN) == 0) {
2791 spin_unlock_bh(&hwsim_radio_lock);
2799 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2800 struct genl_info *info)
2803 struct ieee80211_hdr *hdr;
2804 struct mac80211_hwsim_data *data2;
2805 struct ieee80211_tx_info *txi;
2806 struct hwsim_tx_rate *tx_attempts;
2808 struct sk_buff *skb, *tmp;
2810 unsigned int hwsim_flags;
2814 if (info->snd_portid != wmediumd_portid)
2817 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
2818 !info->attrs[HWSIM_ATTR_FLAGS] ||
2819 !info->attrs[HWSIM_ATTR_COOKIE] ||
2820 !info->attrs[HWSIM_ATTR_SIGNAL] ||
2821 !info->attrs[HWSIM_ATTR_TX_INFO])
2824 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
2825 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
2826 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
2828 data2 = get_hwsim_data_ref_from_addr(src);
2832 /* look for the skb matching the cookie passed back from user */
2833 skb_queue_walk_safe(&data2->pending, skb, tmp) {
2836 txi = IEEE80211_SKB_CB(skb);
2837 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
2839 if (skb_cookie == ret_skb_cookie) {
2840 skb_unlink(skb, &data2->pending);
2850 /* Tx info received because the frame was broadcasted on user space,
2851 so we get all the necessary info: tx attempts and skb control buff */
2853 tx_attempts = (struct hwsim_tx_rate *)nla_data(
2854 info->attrs[HWSIM_ATTR_TX_INFO]);
2856 /* now send back TX status */
2857 txi = IEEE80211_SKB_CB(skb);
2859 ieee80211_tx_info_clear_status(txi);
2861 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2862 txi->status.rates[i].idx = tx_attempts[i].idx;
2863 txi->status.rates[i].count = tx_attempts[i].count;
2864 /*txi->status.rates[i].flags = 0;*/
2867 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2869 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
2870 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
2871 if (skb->len >= 16) {
2872 hdr = (struct ieee80211_hdr *) skb->data;
2873 mac80211_hwsim_monitor_ack(data2->channel,
2876 txi->flags |= IEEE80211_TX_STAT_ACK;
2878 ieee80211_tx_status_irqsafe(data2->hw, skb);
2885 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
2886 struct genl_info *info)
2888 struct mac80211_hwsim_data *data2;
2889 struct ieee80211_rx_status rx_status;
2893 struct sk_buff *skb = NULL;
2895 if (info->snd_portid != wmediumd_portid)
2898 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
2899 !info->attrs[HWSIM_ATTR_FRAME] ||
2900 !info->attrs[HWSIM_ATTR_RX_RATE] ||
2901 !info->attrs[HWSIM_ATTR_SIGNAL])
2904 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
2905 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
2906 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
2908 /* Allocate new skb here */
2909 skb = alloc_skb(frame_data_len, GFP_KERNEL);
2913 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
2917 memcpy(skb_put(skb, frame_data_len), frame_data, frame_data_len);
2919 data2 = get_hwsim_data_ref_from_addr(dst);
2923 /* check if radio is configured properly */
2925 if (data2->idle || !data2->started)
2928 /* A frame is received from user space */
2929 memset(&rx_status, 0, sizeof(rx_status));
2930 if (info->attrs[HWSIM_ATTR_FREQ]) {
2931 /* throw away off-channel packets, but allow both the temporary
2932 * ("hw" scan/remain-on-channel) and regular channel, since the
2933 * internal datapath also allows this
2935 mutex_lock(&data2->mutex);
2936 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
2938 if (rx_status.freq != data2->channel->center_freq &&
2939 (!data2->tmp_chan ||
2940 rx_status.freq != data2->tmp_chan->center_freq)) {
2941 mutex_unlock(&data2->mutex);
2944 mutex_unlock(&data2->mutex);
2946 rx_status.freq = data2->channel->center_freq;
2949 rx_status.band = data2->channel->band;
2950 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
2951 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2953 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
2955 data2->rx_bytes += skb->len;
2956 ieee80211_rx_irqsafe(data2->hw, skb);
2960 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
2966 static int hwsim_register_received_nl(struct sk_buff *skb_2,
2967 struct genl_info *info)
2969 struct mac80211_hwsim_data *data;
2972 spin_lock_bh(&hwsim_radio_lock);
2973 list_for_each_entry(data, &hwsim_radios, list)
2974 chans = max(chans, data->channels);
2975 spin_unlock_bh(&hwsim_radio_lock);
2977 /* In the future we should revise the userspace API and allow it
2978 * to set a flag that it does support multi-channel, then we can
2979 * let this pass conditionally on the flag.
2980 * For current userspace, prohibit it since it won't work right.
2985 if (wmediumd_portid)
2988 wmediumd_portid = info->snd_portid;
2990 printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
2991 "switching to wmediumd mode with pid %d\n", info->snd_portid);
2996 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
2998 struct hwsim_new_radio_params param = { 0 };
3000 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3001 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3002 param.channels = channels;
3003 param.destroy_on_close =
3004 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3006 if (info->attrs[HWSIM_ATTR_CHANNELS])
3007 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3009 if (info->attrs[HWSIM_ATTR_NO_VIF])
3010 param.no_vif = true;
3012 if (info->attrs[HWSIM_ATTR_RADIO_NAME])
3013 param.hwname = nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]);
3015 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3016 param.use_chanctx = true;
3018 param.use_chanctx = (param.channels > 1);
3020 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3022 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3024 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3025 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3027 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3029 param.regd = hwsim_world_regdom_custom[idx];
3032 return mac80211_hwsim_new_radio(info, ¶m);
3035 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3037 struct mac80211_hwsim_data *data;
3039 const char *hwname = NULL;
3041 if (info->attrs[HWSIM_ATTR_RADIO_ID])
3042 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3043 else if (info->attrs[HWSIM_ATTR_RADIO_NAME])
3044 hwname = (void *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]);
3048 spin_lock_bh(&hwsim_radio_lock);
3049 list_for_each_entry(data, &hwsim_radios, list) {
3051 if (data->idx != idx)
3054 if (strcmp(hwname, wiphy_name(data->hw->wiphy)))
3058 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3061 list_del(&data->list);
3062 spin_unlock_bh(&hwsim_radio_lock);
3063 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3067 spin_unlock_bh(&hwsim_radio_lock);
3072 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3074 struct mac80211_hwsim_data *data;
3075 struct sk_buff *skb;
3076 int idx, res = -ENODEV;
3078 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3080 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3082 spin_lock_bh(&hwsim_radio_lock);
3083 list_for_each_entry(data, &hwsim_radios, list) {
3084 if (data->idx != idx)
3087 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3090 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3096 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3097 info->snd_seq, NULL, 0);
3103 genlmsg_reply(skb, info);
3108 spin_unlock_bh(&hwsim_radio_lock);
3113 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3114 struct netlink_callback *cb)
3116 int idx = cb->args[0];
3117 struct mac80211_hwsim_data *data = NULL;
3120 spin_lock_bh(&hwsim_radio_lock);
3122 if (idx == hwsim_radio_idx)
3125 list_for_each_entry(data, &hwsim_radios, list) {
3126 if (data->idx < idx)
3129 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3132 res = mac80211_hwsim_get_radio(skb, data,
3133 NETLINK_CB(cb->skb).portid,
3134 cb->nlh->nlmsg_seq, cb,
3139 idx = data->idx + 1;
3145 spin_unlock_bh(&hwsim_radio_lock);
3149 /* Generic Netlink operations array */
3150 static const struct genl_ops hwsim_ops[] = {
3152 .cmd = HWSIM_CMD_REGISTER,
3153 .policy = hwsim_genl_policy,
3154 .doit = hwsim_register_received_nl,
3155 .flags = GENL_ADMIN_PERM,
3158 .cmd = HWSIM_CMD_FRAME,
3159 .policy = hwsim_genl_policy,
3160 .doit = hwsim_cloned_frame_received_nl,
3163 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3164 .policy = hwsim_genl_policy,
3165 .doit = hwsim_tx_info_frame_received_nl,
3168 .cmd = HWSIM_CMD_NEW_RADIO,
3169 .policy = hwsim_genl_policy,
3170 .doit = hwsim_new_radio_nl,
3171 .flags = GENL_UNS_ADMIN_PERM,
3174 .cmd = HWSIM_CMD_DEL_RADIO,
3175 .policy = hwsim_genl_policy,
3176 .doit = hwsim_del_radio_nl,
3177 .flags = GENL_UNS_ADMIN_PERM,
3180 .cmd = HWSIM_CMD_GET_RADIO,
3181 .policy = hwsim_genl_policy,
3182 .doit = hwsim_get_radio_nl,
3183 .dumpit = hwsim_dump_radio_nl,
3187 static void destroy_radio(struct work_struct *work)
3189 struct mac80211_hwsim_data *data =
3190 container_of(work, struct mac80211_hwsim_data, destroy_work);
3192 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
3195 static void remove_user_radios(u32 portid)
3197 struct mac80211_hwsim_data *entry, *tmp;
3199 spin_lock_bh(&hwsim_radio_lock);
3200 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3201 if (entry->destroy_on_close && entry->portid == portid) {
3202 list_del(&entry->list);
3203 INIT_WORK(&entry->destroy_work, destroy_radio);
3204 schedule_work(&entry->destroy_work);
3207 spin_unlock_bh(&hwsim_radio_lock);
3210 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3211 unsigned long state,
3214 struct netlink_notify *notify = _notify;
3216 if (state != NETLINK_URELEASE)
3219 remove_user_radios(notify->portid);
3221 if (notify->portid == wmediumd_portid) {
3222 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3223 " socket, switching to perfect channel medium\n");
3224 wmediumd_portid = 0;
3230 static struct notifier_block hwsim_netlink_notifier = {
3231 .notifier_call = mac80211_hwsim_netlink_notify,
3234 static int hwsim_init_netlink(void)
3238 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3240 rc = genl_register_family_with_ops_groups(&hwsim_genl_family,
3246 rc = netlink_register_notifier(&hwsim_netlink_notifier);
3248 genl_unregister_family(&hwsim_genl_family);
3255 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
3259 static __net_init int hwsim_init_net(struct net *net)
3261 hwsim_net_set_netgroup(net);
3266 static void __net_exit hwsim_exit_net(struct net *net)
3268 struct mac80211_hwsim_data *data, *tmp;
3270 spin_lock_bh(&hwsim_radio_lock);
3271 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3272 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3275 /* Radios created in init_net are returned to init_net. */
3276 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3279 list_del(&data->list);
3280 INIT_WORK(&data->destroy_work, destroy_radio);
3281 schedule_work(&data->destroy_work);
3283 spin_unlock_bh(&hwsim_radio_lock);
3286 static struct pernet_operations hwsim_net_ops = {
3287 .init = hwsim_init_net,
3288 .exit = hwsim_exit_net,
3289 .id = &hwsim_net_id,
3290 .size = sizeof(struct hwsim_net),
3293 static void hwsim_exit_netlink(void)
3295 /* unregister the notifier */
3296 netlink_unregister_notifier(&hwsim_netlink_notifier);
3297 /* unregister the family */
3298 genl_unregister_family(&hwsim_genl_family);
3301 static int __init init_mac80211_hwsim(void)
3305 if (radios < 0 || radios > 100)
3311 mac80211_hwsim_mchan_ops = mac80211_hwsim_ops;
3312 mac80211_hwsim_mchan_ops.hw_scan = mac80211_hwsim_hw_scan;
3313 mac80211_hwsim_mchan_ops.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan;
3314 mac80211_hwsim_mchan_ops.sw_scan_start = NULL;
3315 mac80211_hwsim_mchan_ops.sw_scan_complete = NULL;
3316 mac80211_hwsim_mchan_ops.remain_on_channel = mac80211_hwsim_roc;
3317 mac80211_hwsim_mchan_ops.cancel_remain_on_channel = mac80211_hwsim_croc;
3318 mac80211_hwsim_mchan_ops.add_chanctx = mac80211_hwsim_add_chanctx;
3319 mac80211_hwsim_mchan_ops.remove_chanctx = mac80211_hwsim_remove_chanctx;
3320 mac80211_hwsim_mchan_ops.change_chanctx = mac80211_hwsim_change_chanctx;
3321 mac80211_hwsim_mchan_ops.assign_vif_chanctx =
3322 mac80211_hwsim_assign_vif_chanctx;
3323 mac80211_hwsim_mchan_ops.unassign_vif_chanctx =
3324 mac80211_hwsim_unassign_vif_chanctx;
3326 spin_lock_init(&hwsim_radio_lock);
3327 INIT_LIST_HEAD(&hwsim_radios);
3329 err = register_pernet_device(&hwsim_net_ops);
3333 err = platform_driver_register(&mac80211_hwsim_driver);
3335 goto out_unregister_pernet;
3337 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
3338 if (IS_ERR(hwsim_class)) {
3339 err = PTR_ERR(hwsim_class);
3340 goto out_unregister_driver;
3343 err = hwsim_init_netlink();
3345 goto out_unregister_driver;
3347 for (i = 0; i < radios; i++) {
3348 struct hwsim_new_radio_params param = { 0 };
3350 param.channels = channels;
3353 case HWSIM_REGTEST_DIFF_COUNTRY:
3354 if (i < ARRAY_SIZE(hwsim_alpha2s))
3355 param.reg_alpha2 = hwsim_alpha2s[i];
3357 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3359 param.reg_alpha2 = hwsim_alpha2s[0];
3361 case HWSIM_REGTEST_STRICT_ALL:
3362 param.reg_strict = true;
3363 case HWSIM_REGTEST_DRIVER_REG_ALL:
3364 param.reg_alpha2 = hwsim_alpha2s[0];
3366 case HWSIM_REGTEST_WORLD_ROAM:
3368 param.regd = &hwsim_world_regdom_custom_01;
3370 case HWSIM_REGTEST_CUSTOM_WORLD:
3371 param.regd = &hwsim_world_regdom_custom_01;
3373 case HWSIM_REGTEST_CUSTOM_WORLD_2:
3375 param.regd = &hwsim_world_regdom_custom_01;
3377 param.regd = &hwsim_world_regdom_custom_02;
3379 case HWSIM_REGTEST_STRICT_FOLLOW:
3381 param.reg_strict = true;
3382 param.reg_alpha2 = hwsim_alpha2s[0];
3385 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
3387 param.reg_strict = true;
3388 param.reg_alpha2 = hwsim_alpha2s[0];
3389 } else if (i == 1) {
3390 param.reg_alpha2 = hwsim_alpha2s[1];
3393 case HWSIM_REGTEST_ALL:
3396 param.regd = &hwsim_world_regdom_custom_01;
3399 param.regd = &hwsim_world_regdom_custom_02;
3402 param.reg_alpha2 = hwsim_alpha2s[0];
3405 param.reg_alpha2 = hwsim_alpha2s[1];
3408 param.reg_strict = true;
3409 param.reg_alpha2 = hwsim_alpha2s[2];
3417 param.p2p_device = support_p2p_device;
3418 param.use_chanctx = channels > 1;
3420 err = mac80211_hwsim_new_radio(NULL, ¶m);
3422 goto out_free_radios;
3425 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3427 if (hwsim_mon == NULL) {
3429 goto out_free_radios;
3433 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3436 goto out_free_radios;
3439 err = register_netdevice(hwsim_mon);
3449 free_netdev(hwsim_mon);
3451 mac80211_hwsim_free();
3452 out_unregister_driver:
3453 platform_driver_unregister(&mac80211_hwsim_driver);
3454 out_unregister_pernet:
3455 unregister_pernet_device(&hwsim_net_ops);
3458 module_init(init_mac80211_hwsim);
3460 static void __exit exit_mac80211_hwsim(void)
3462 printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
3464 hwsim_exit_netlink();
3466 mac80211_hwsim_free();
3467 unregister_netdev(hwsim_mon);
3468 platform_driver_unregister(&mac80211_hwsim_driver);
3469 unregister_pernet_device(&hwsim_net_ops);
3471 module_exit(exit_mac80211_hwsim);