Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / mac80211_hwsim.c
blob20af54378cc0593c2772938929685d1c5bd12720
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>
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.
9 */
12 * TODO:
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>
21 #include <net/dst.h>
22 #include <net/xfrm.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 <linux/rhashtable.h>
36 #include "mac80211_hwsim.h"
38 #define WARN_QUEUE 100
39 #define MAX_QUEUE 200
41 MODULE_AUTHOR("Jouni Malinen");
42 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
43 MODULE_LICENSE("GPL");
45 static int radios = 2;
46 module_param(radios, int, 0444);
47 MODULE_PARM_DESC(radios, "Number of simulated radios");
49 static int channels = 1;
50 module_param(channels, int, 0444);
51 MODULE_PARM_DESC(channels, "Number of concurrent channels");
53 static bool paged_rx = false;
54 module_param(paged_rx, bool, 0644);
55 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
57 static bool rctbl = false;
58 module_param(rctbl, bool, 0444);
59 MODULE_PARM_DESC(rctbl, "Handle rate control table");
61 static bool support_p2p_device = true;
62 module_param(support_p2p_device, bool, 0444);
63 MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
65 /**
66 * enum hwsim_regtest - the type of regulatory tests we offer
68 * These are the different values you can use for the regtest
69 * module parameter. This is useful to help test world roaming
70 * and the driver regulatory_hint() call and combinations of these.
71 * If you want to do specific alpha2 regulatory domain tests simply
72 * use the userspace regulatory request as that will be respected as
73 * well without the need of this module parameter. This is designed
74 * only for testing the driver regulatory request, world roaming
75 * and all possible combinations.
77 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
78 * this is the default value.
79 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
80 * hint, only one driver regulatory hint will be sent as such the
81 * secondary radios are expected to follow.
82 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
83 * request with all radios reporting the same regulatory domain.
84 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
85 * different regulatory domains requests. Expected behaviour is for
86 * an intersection to occur but each device will still use their
87 * respective regulatory requested domains. Subsequent radios will
88 * use the resulting intersection.
89 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
90 * this by using a custom beacon-capable regulatory domain for the first
91 * radio. All other device world roam.
92 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
93 * domain requests. All radios will adhere to this custom world regulatory
94 * domain.
95 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
96 * domain requests. The first radio will adhere to the first custom world
97 * regulatory domain, the second one to the second custom world regulatory
98 * domain. All other devices will world roam.
99 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
100 * settings, only the first radio will send a regulatory domain request
101 * and use strict settings. The rest of the radios are expected to follow.
102 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
103 * settings. All radios will adhere to this.
104 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
105 * domain settings, combined with secondary driver regulatory domain
106 * settings. The first radio will get a strict regulatory domain setting
107 * using the first driver regulatory request and the second radio will use
108 * non-strict settings using the second driver regulatory request. All
109 * other devices should follow the intersection created between the
110 * first two.
111 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
112 * at least 6 radios for a complete test. We will test in this order:
113 * 1 - driver custom world regulatory domain
114 * 2 - second custom world regulatory domain
115 * 3 - first driver regulatory domain request
116 * 4 - second driver regulatory domain request
117 * 5 - strict regulatory domain settings using the third driver regulatory
118 * domain request
119 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
120 * regulatory requests.
122 enum hwsim_regtest {
123 HWSIM_REGTEST_DISABLED = 0,
124 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
125 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
126 HWSIM_REGTEST_DIFF_COUNTRY = 3,
127 HWSIM_REGTEST_WORLD_ROAM = 4,
128 HWSIM_REGTEST_CUSTOM_WORLD = 5,
129 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
130 HWSIM_REGTEST_STRICT_FOLLOW = 7,
131 HWSIM_REGTEST_STRICT_ALL = 8,
132 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
133 HWSIM_REGTEST_ALL = 10,
136 /* Set to one of the HWSIM_REGTEST_* values above */
137 static int regtest = HWSIM_REGTEST_DISABLED;
138 module_param(regtest, int, 0444);
139 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
141 static const char *hwsim_alpha2s[] = {
142 "FI",
143 "AL",
144 "US",
145 "DE",
146 "JP",
147 "AL",
150 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
151 .n_reg_rules = 4,
152 .alpha2 = "99",
153 .reg_rules = {
154 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
155 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
156 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
157 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
161 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
162 .n_reg_rules = 2,
163 .alpha2 = "99",
164 .reg_rules = {
165 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
166 REG_RULE(5725-10, 5850+10, 40, 0, 30,
167 NL80211_RRF_NO_IR),
171 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
172 &hwsim_world_regdom_custom_01,
173 &hwsim_world_regdom_custom_02,
176 struct hwsim_vif_priv {
177 u32 magic;
178 u8 bssid[ETH_ALEN];
179 bool assoc;
180 bool bcn_en;
181 u16 aid;
184 #define HWSIM_VIF_MAGIC 0x69537748
186 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
188 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
189 WARN(vp->magic != HWSIM_VIF_MAGIC,
190 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
191 vif, vp->magic, vif->addr, vif->type, vif->p2p);
194 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
196 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
197 vp->magic = HWSIM_VIF_MAGIC;
200 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
202 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
203 vp->magic = 0;
206 struct hwsim_sta_priv {
207 u32 magic;
210 #define HWSIM_STA_MAGIC 0x6d537749
212 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
214 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
215 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
218 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
220 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
221 sp->magic = HWSIM_STA_MAGIC;
224 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
226 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
227 sp->magic = 0;
230 struct hwsim_chanctx_priv {
231 u32 magic;
234 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
236 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
238 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
239 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
242 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
244 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
245 cp->magic = HWSIM_CHANCTX_MAGIC;
248 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
250 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
251 cp->magic = 0;
254 static unsigned int hwsim_net_id;
256 static int hwsim_netgroup;
258 struct hwsim_net {
259 int netgroup;
260 u32 wmediumd;
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 inline u32 hwsim_net_get_wmediumd(struct net *net)
279 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
281 return hwsim_net->wmediumd;
284 static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
286 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
288 hwsim_net->wmediumd = portid;
291 static struct class *hwsim_class;
293 static struct net_device *hwsim_mon; /* global monitor netdev */
295 #define CHAN2G(_freq) { \
296 .band = NL80211_BAND_2GHZ, \
297 .center_freq = (_freq), \
298 .hw_value = (_freq), \
299 .max_power = 20, \
302 #define CHAN5G(_freq) { \
303 .band = NL80211_BAND_5GHZ, \
304 .center_freq = (_freq), \
305 .hw_value = (_freq), \
306 .max_power = 20, \
309 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
310 CHAN2G(2412), /* Channel 1 */
311 CHAN2G(2417), /* Channel 2 */
312 CHAN2G(2422), /* Channel 3 */
313 CHAN2G(2427), /* Channel 4 */
314 CHAN2G(2432), /* Channel 5 */
315 CHAN2G(2437), /* Channel 6 */
316 CHAN2G(2442), /* Channel 7 */
317 CHAN2G(2447), /* Channel 8 */
318 CHAN2G(2452), /* Channel 9 */
319 CHAN2G(2457), /* Channel 10 */
320 CHAN2G(2462), /* Channel 11 */
321 CHAN2G(2467), /* Channel 12 */
322 CHAN2G(2472), /* Channel 13 */
323 CHAN2G(2484), /* Channel 14 */
326 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
327 CHAN5G(5180), /* Channel 36 */
328 CHAN5G(5200), /* Channel 40 */
329 CHAN5G(5220), /* Channel 44 */
330 CHAN5G(5240), /* Channel 48 */
332 CHAN5G(5260), /* Channel 52 */
333 CHAN5G(5280), /* Channel 56 */
334 CHAN5G(5300), /* Channel 60 */
335 CHAN5G(5320), /* Channel 64 */
337 CHAN5G(5500), /* Channel 100 */
338 CHAN5G(5520), /* Channel 104 */
339 CHAN5G(5540), /* Channel 108 */
340 CHAN5G(5560), /* Channel 112 */
341 CHAN5G(5580), /* Channel 116 */
342 CHAN5G(5600), /* Channel 120 */
343 CHAN5G(5620), /* Channel 124 */
344 CHAN5G(5640), /* Channel 128 */
345 CHAN5G(5660), /* Channel 132 */
346 CHAN5G(5680), /* Channel 136 */
347 CHAN5G(5700), /* Channel 140 */
349 CHAN5G(5745), /* Channel 149 */
350 CHAN5G(5765), /* Channel 153 */
351 CHAN5G(5785), /* Channel 157 */
352 CHAN5G(5805), /* Channel 161 */
353 CHAN5G(5825), /* Channel 165 */
354 CHAN5G(5845), /* Channel 169 */
357 static const struct ieee80211_rate hwsim_rates[] = {
358 { .bitrate = 10 },
359 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
360 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
361 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
362 { .bitrate = 60 },
363 { .bitrate = 90 },
364 { .bitrate = 120 },
365 { .bitrate = 180 },
366 { .bitrate = 240 },
367 { .bitrate = 360 },
368 { .bitrate = 480 },
369 { .bitrate = 540 }
372 #define OUI_QCA 0x001374
373 #define QCA_NL80211_SUBCMD_TEST 1
374 enum qca_nl80211_vendor_subcmds {
375 QCA_WLAN_VENDOR_ATTR_TEST = 8,
376 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
379 static const struct nla_policy
380 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
381 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
384 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
385 struct wireless_dev *wdev,
386 const void *data, int data_len)
388 struct sk_buff *skb;
389 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
390 int err;
391 u32 val;
393 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
394 hwsim_vendor_test_policy, NULL);
395 if (err)
396 return err;
397 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
398 return -EINVAL;
399 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
400 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
402 /* Send a vendor event as a test. Note that this would not normally be
403 * done within a command handler, but rather, based on some other
404 * trigger. For simplicity, this command is used to trigger the event
405 * here.
407 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
409 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
410 if (skb) {
411 /* skb_put() or nla_put() will fill up data within
412 * NL80211_ATTR_VENDOR_DATA.
415 /* Add vendor data */
416 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
418 /* Send the event - this will call nla_nest_end() */
419 cfg80211_vendor_event(skb, GFP_KERNEL);
422 /* Send a response to the command */
423 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
424 if (!skb)
425 return -ENOMEM;
427 /* skb_put() or nla_put() will fill up data within
428 * NL80211_ATTR_VENDOR_DATA
430 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
432 return cfg80211_vendor_cmd_reply(skb);
435 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
437 .info = { .vendor_id = OUI_QCA,
438 .subcmd = QCA_NL80211_SUBCMD_TEST },
439 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
440 .doit = mac80211_hwsim_vendor_cmd_test,
444 /* Advertise support vendor specific events */
445 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
446 { .vendor_id = OUI_QCA, .subcmd = 1 },
449 static const struct ieee80211_iface_limit hwsim_if_limits[] = {
450 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
451 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
452 BIT(NL80211_IFTYPE_P2P_CLIENT) |
453 #ifdef CONFIG_MAC80211_MESH
454 BIT(NL80211_IFTYPE_MESH_POINT) |
455 #endif
456 BIT(NL80211_IFTYPE_AP) |
457 BIT(NL80211_IFTYPE_P2P_GO) },
458 /* must be last, see hwsim_if_comb */
459 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
462 static const struct ieee80211_iface_combination hwsim_if_comb[] = {
464 .limits = hwsim_if_limits,
465 /* remove the last entry which is P2P_DEVICE */
466 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
467 .max_interfaces = 2048,
468 .num_different_channels = 1,
469 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
470 BIT(NL80211_CHAN_WIDTH_20) |
471 BIT(NL80211_CHAN_WIDTH_40) |
472 BIT(NL80211_CHAN_WIDTH_80) |
473 BIT(NL80211_CHAN_WIDTH_160),
477 static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
479 .limits = hwsim_if_limits,
480 .n_limits = ARRAY_SIZE(hwsim_if_limits),
481 .max_interfaces = 2048,
482 .num_different_channels = 1,
483 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
484 BIT(NL80211_CHAN_WIDTH_20) |
485 BIT(NL80211_CHAN_WIDTH_40) |
486 BIT(NL80211_CHAN_WIDTH_80) |
487 BIT(NL80211_CHAN_WIDTH_160),
491 static spinlock_t hwsim_radio_lock;
492 static LIST_HEAD(hwsim_radios);
493 static struct workqueue_struct *hwsim_wq;
494 static struct rhashtable hwsim_radios_rht;
495 static int hwsim_radio_idx;
497 static struct platform_driver mac80211_hwsim_driver = {
498 .driver = {
499 .name = "mac80211_hwsim",
503 struct mac80211_hwsim_data {
504 struct list_head list;
505 struct rhash_head rht;
506 struct ieee80211_hw *hw;
507 struct device *dev;
508 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
509 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
510 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
511 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
512 struct ieee80211_iface_combination if_combination;
514 struct mac_address addresses[2];
515 int channels, idx;
516 bool use_chanctx;
517 bool destroy_on_close;
518 struct work_struct destroy_work;
519 u32 portid;
520 char alpha2[2];
521 const struct ieee80211_regdomain *regd;
523 struct ieee80211_channel *tmp_chan;
524 struct ieee80211_channel *roc_chan;
525 u32 roc_duration;
526 struct delayed_work roc_start;
527 struct delayed_work roc_done;
528 struct delayed_work hw_scan;
529 struct cfg80211_scan_request *hw_scan_request;
530 struct ieee80211_vif *hw_scan_vif;
531 int scan_chan_idx;
532 u8 scan_addr[ETH_ALEN];
533 struct {
534 struct ieee80211_channel *channel;
535 unsigned long next_start, start, end;
536 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
537 ARRAY_SIZE(hwsim_channels_5ghz)];
539 struct ieee80211_channel *channel;
540 u64 beacon_int /* beacon interval in us */;
541 unsigned int rx_filter;
542 bool started, idle, scanning;
543 struct mutex mutex;
544 struct tasklet_hrtimer beacon_timer;
545 enum ps_mode {
546 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
547 } ps;
548 bool ps_poll_pending;
549 struct dentry *debugfs;
551 uintptr_t pending_cookie;
552 struct sk_buff_head pending; /* packets pending */
554 * Only radios in the same group can communicate together (the
555 * channel has to match too). Each bit represents a group. A
556 * radio can be in more than one group.
558 u64 group;
560 /* group shared by radios created in the same netns */
561 int netgroup;
562 /* wmediumd portid responsible for netgroup of this radio */
563 u32 wmediumd;
565 /* difference between this hw's clock and the real clock, in usecs */
566 s64 tsf_offset;
567 s64 bcn_delta;
568 /* absolute beacon transmission time. Used to cover up "tx" delay. */
569 u64 abs_bcn_ts;
571 /* Stats */
572 u64 tx_pkts;
573 u64 rx_pkts;
574 u64 tx_bytes;
575 u64 rx_bytes;
576 u64 tx_dropped;
577 u64 tx_failed;
580 static const struct rhashtable_params hwsim_rht_params = {
581 .nelem_hint = 2,
582 .automatic_shrinking = true,
583 .key_len = ETH_ALEN,
584 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
585 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
588 struct hwsim_radiotap_hdr {
589 struct ieee80211_radiotap_header hdr;
590 __le64 rt_tsft;
591 u8 rt_flags;
592 u8 rt_rate;
593 __le16 rt_channel;
594 __le16 rt_chbitmask;
595 } __packed;
597 struct hwsim_radiotap_ack_hdr {
598 struct ieee80211_radiotap_header hdr;
599 u8 rt_flags;
600 u8 pad;
601 __le16 rt_channel;
602 __le16 rt_chbitmask;
603 } __packed;
605 /* MAC80211_HWSIM netlink family */
606 static struct genl_family hwsim_genl_family;
608 enum hwsim_multicast_groups {
609 HWSIM_MCGRP_CONFIG,
612 static const struct genl_multicast_group hwsim_mcgrps[] = {
613 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
616 /* MAC80211_HWSIM netlink policy */
618 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
619 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
620 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
621 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
622 .len = IEEE80211_MAX_DATA_LEN },
623 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
624 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
625 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
626 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
627 .len = IEEE80211_TX_MAX_RATES *
628 sizeof(struct hwsim_tx_rate)},
629 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
630 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
631 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
632 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
633 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
634 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
635 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
636 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
637 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
638 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
639 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
642 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
643 struct sk_buff *skb,
644 struct ieee80211_channel *chan);
646 /* sysfs attributes */
647 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
649 struct mac80211_hwsim_data *data = dat;
650 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
651 struct sk_buff *skb;
652 struct ieee80211_pspoll *pspoll;
654 if (!vp->assoc)
655 return;
657 wiphy_dbg(data->hw->wiphy,
658 "%s: send PS-Poll to %pM for aid %d\n",
659 __func__, vp->bssid, vp->aid);
661 skb = dev_alloc_skb(sizeof(*pspoll));
662 if (!skb)
663 return;
664 pspoll = skb_put(skb, sizeof(*pspoll));
665 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
666 IEEE80211_STYPE_PSPOLL |
667 IEEE80211_FCTL_PM);
668 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
669 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
670 memcpy(pspoll->ta, mac, ETH_ALEN);
672 rcu_read_lock();
673 mac80211_hwsim_tx_frame(data->hw, skb,
674 rcu_dereference(vif->chanctx_conf)->def.chan);
675 rcu_read_unlock();
678 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
679 struct ieee80211_vif *vif, int ps)
681 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
682 struct sk_buff *skb;
683 struct ieee80211_hdr *hdr;
685 if (!vp->assoc)
686 return;
688 wiphy_dbg(data->hw->wiphy,
689 "%s: send data::nullfunc to %pM ps=%d\n",
690 __func__, vp->bssid, ps);
692 skb = dev_alloc_skb(sizeof(*hdr));
693 if (!skb)
694 return;
695 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
696 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
697 IEEE80211_STYPE_NULLFUNC |
698 IEEE80211_FCTL_TODS |
699 (ps ? IEEE80211_FCTL_PM : 0));
700 hdr->duration_id = cpu_to_le16(0);
701 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
702 memcpy(hdr->addr2, mac, ETH_ALEN);
703 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
705 rcu_read_lock();
706 mac80211_hwsim_tx_frame(data->hw, skb,
707 rcu_dereference(vif->chanctx_conf)->def.chan);
708 rcu_read_unlock();
712 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
713 struct ieee80211_vif *vif)
715 struct mac80211_hwsim_data *data = dat;
716 hwsim_send_nullfunc(data, mac, vif, 1);
719 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
720 struct ieee80211_vif *vif)
722 struct mac80211_hwsim_data *data = dat;
723 hwsim_send_nullfunc(data, mac, vif, 0);
726 static int hwsim_fops_ps_read(void *dat, u64 *val)
728 struct mac80211_hwsim_data *data = dat;
729 *val = data->ps;
730 return 0;
733 static int hwsim_fops_ps_write(void *dat, u64 val)
735 struct mac80211_hwsim_data *data = dat;
736 enum ps_mode old_ps;
738 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
739 val != PS_MANUAL_POLL)
740 return -EINVAL;
742 if (val == PS_MANUAL_POLL) {
743 if (data->ps != PS_ENABLED)
744 return -EINVAL;
745 local_bh_disable();
746 ieee80211_iterate_active_interfaces_atomic(
747 data->hw, IEEE80211_IFACE_ITER_NORMAL,
748 hwsim_send_ps_poll, data);
749 local_bh_enable();
750 return 0;
752 old_ps = data->ps;
753 data->ps = val;
755 local_bh_disable();
756 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
757 ieee80211_iterate_active_interfaces_atomic(
758 data->hw, IEEE80211_IFACE_ITER_NORMAL,
759 hwsim_send_nullfunc_ps, data);
760 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
761 ieee80211_iterate_active_interfaces_atomic(
762 data->hw, IEEE80211_IFACE_ITER_NORMAL,
763 hwsim_send_nullfunc_no_ps, data);
765 local_bh_enable();
767 return 0;
770 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
771 "%llu\n");
773 static int hwsim_write_simulate_radar(void *dat, u64 val)
775 struct mac80211_hwsim_data *data = dat;
777 ieee80211_radar_detected(data->hw);
779 return 0;
782 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
783 hwsim_write_simulate_radar, "%llu\n");
785 static int hwsim_fops_group_read(void *dat, u64 *val)
787 struct mac80211_hwsim_data *data = dat;
788 *val = data->group;
789 return 0;
792 static int hwsim_fops_group_write(void *dat, u64 val)
794 struct mac80211_hwsim_data *data = dat;
795 data->group = val;
796 return 0;
799 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
800 hwsim_fops_group_read, hwsim_fops_group_write,
801 "%llx\n");
803 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
804 struct net_device *dev)
806 /* TODO: allow packet injection */
807 dev_kfree_skb(skb);
808 return NETDEV_TX_OK;
811 static inline u64 mac80211_hwsim_get_tsf_raw(void)
813 return ktime_to_us(ktime_get_real());
816 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
818 u64 now = mac80211_hwsim_get_tsf_raw();
819 return cpu_to_le64(now + data->tsf_offset);
822 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
823 struct ieee80211_vif *vif)
825 struct mac80211_hwsim_data *data = hw->priv;
826 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
829 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
830 struct ieee80211_vif *vif, u64 tsf)
832 struct mac80211_hwsim_data *data = hw->priv;
833 u64 now = mac80211_hwsim_get_tsf(hw, vif);
834 u32 bcn_int = data->beacon_int;
835 u64 delta = abs(tsf - now);
837 /* adjust after beaconing with new timestamp at old TBTT */
838 if (tsf > now) {
839 data->tsf_offset += delta;
840 data->bcn_delta = do_div(delta, bcn_int);
841 } else {
842 data->tsf_offset -= delta;
843 data->bcn_delta = -(s64)do_div(delta, bcn_int);
847 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
848 struct sk_buff *tx_skb,
849 struct ieee80211_channel *chan)
851 struct mac80211_hwsim_data *data = hw->priv;
852 struct sk_buff *skb;
853 struct hwsim_radiotap_hdr *hdr;
854 u16 flags;
855 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
856 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
858 if (WARN_ON(!txrate))
859 return;
861 if (!netif_running(hwsim_mon))
862 return;
864 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
865 if (skb == NULL)
866 return;
868 hdr = skb_push(skb, sizeof(*hdr));
869 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
870 hdr->hdr.it_pad = 0;
871 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
872 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
873 (1 << IEEE80211_RADIOTAP_RATE) |
874 (1 << IEEE80211_RADIOTAP_TSFT) |
875 (1 << IEEE80211_RADIOTAP_CHANNEL));
876 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
877 hdr->rt_flags = 0;
878 hdr->rt_rate = txrate->bitrate / 5;
879 hdr->rt_channel = cpu_to_le16(chan->center_freq);
880 flags = IEEE80211_CHAN_2GHZ;
881 if (txrate->flags & IEEE80211_RATE_ERP_G)
882 flags |= IEEE80211_CHAN_OFDM;
883 else
884 flags |= IEEE80211_CHAN_CCK;
885 hdr->rt_chbitmask = cpu_to_le16(flags);
887 skb->dev = hwsim_mon;
888 skb_reset_mac_header(skb);
889 skb->ip_summed = CHECKSUM_UNNECESSARY;
890 skb->pkt_type = PACKET_OTHERHOST;
891 skb->protocol = htons(ETH_P_802_2);
892 memset(skb->cb, 0, sizeof(skb->cb));
893 netif_rx(skb);
897 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
898 const u8 *addr)
900 struct sk_buff *skb;
901 struct hwsim_radiotap_ack_hdr *hdr;
902 u16 flags;
903 struct ieee80211_hdr *hdr11;
905 if (!netif_running(hwsim_mon))
906 return;
908 skb = dev_alloc_skb(100);
909 if (skb == NULL)
910 return;
912 hdr = skb_put(skb, sizeof(*hdr));
913 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
914 hdr->hdr.it_pad = 0;
915 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
916 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
917 (1 << IEEE80211_RADIOTAP_CHANNEL));
918 hdr->rt_flags = 0;
919 hdr->pad = 0;
920 hdr->rt_channel = cpu_to_le16(chan->center_freq);
921 flags = IEEE80211_CHAN_2GHZ;
922 hdr->rt_chbitmask = cpu_to_le16(flags);
924 hdr11 = skb_put(skb, 10);
925 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
926 IEEE80211_STYPE_ACK);
927 hdr11->duration_id = cpu_to_le16(0);
928 memcpy(hdr11->addr1, addr, ETH_ALEN);
930 skb->dev = hwsim_mon;
931 skb_reset_mac_header(skb);
932 skb->ip_summed = CHECKSUM_UNNECESSARY;
933 skb->pkt_type = PACKET_OTHERHOST;
934 skb->protocol = htons(ETH_P_802_2);
935 memset(skb->cb, 0, sizeof(skb->cb));
936 netif_rx(skb);
939 struct mac80211_hwsim_addr_match_data {
940 u8 addr[ETH_ALEN];
941 bool ret;
944 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
945 struct ieee80211_vif *vif)
947 struct mac80211_hwsim_addr_match_data *md = data;
949 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
950 md->ret = true;
953 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
954 const u8 *addr)
956 struct mac80211_hwsim_addr_match_data md = {
957 .ret = false,
960 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
961 return true;
963 memcpy(md.addr, addr, ETH_ALEN);
965 ieee80211_iterate_active_interfaces_atomic(data->hw,
966 IEEE80211_IFACE_ITER_NORMAL,
967 mac80211_hwsim_addr_iter,
968 &md);
970 return md.ret;
973 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
974 struct sk_buff *skb)
976 switch (data->ps) {
977 case PS_DISABLED:
978 return true;
979 case PS_ENABLED:
980 return false;
981 case PS_AUTO_POLL:
982 /* TODO: accept (some) Beacons by default and other frames only
983 * if pending PS-Poll has been sent */
984 return true;
985 case PS_MANUAL_POLL:
986 /* Allow unicast frames to own address if there is a pending
987 * PS-Poll */
988 if (data->ps_poll_pending &&
989 mac80211_hwsim_addr_match(data, skb->data + 4)) {
990 data->ps_poll_pending = false;
991 return true;
993 return false;
996 return true;
999 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1000 struct sk_buff *skb, int portid)
1002 struct net *net;
1003 bool found = false;
1004 int res = -ENOENT;
1006 rcu_read_lock();
1007 for_each_net_rcu(net) {
1008 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1009 res = genlmsg_unicast(net, skb, portid);
1010 found = true;
1011 break;
1014 rcu_read_unlock();
1016 if (!found)
1017 nlmsg_free(skb);
1019 return res;
1022 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1024 u16 result = 0;
1026 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1027 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1028 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1029 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1030 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1031 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1032 if (rate->flags & IEEE80211_TX_RC_MCS)
1033 result |= MAC80211_HWSIM_TX_RC_MCS;
1034 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1035 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1036 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1037 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1038 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1039 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1040 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1041 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1042 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1043 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1044 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1045 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1046 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1047 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1049 return result;
1052 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1053 struct sk_buff *my_skb,
1054 int dst_portid)
1056 struct sk_buff *skb;
1057 struct mac80211_hwsim_data *data = hw->priv;
1058 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1059 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1060 void *msg_head;
1061 unsigned int hwsim_flags = 0;
1062 int i;
1063 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1064 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1065 uintptr_t cookie;
1067 if (data->ps != PS_DISABLED)
1068 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1069 /* If the queue contains MAX_QUEUE skb's drop some */
1070 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1071 /* Droping until WARN_QUEUE level */
1072 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1073 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1074 data->tx_dropped++;
1078 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1079 if (skb == NULL)
1080 goto nla_put_failure;
1082 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1083 HWSIM_CMD_FRAME);
1084 if (msg_head == NULL) {
1085 pr_debug("mac80211_hwsim: problem with msg_head\n");
1086 goto nla_put_failure;
1089 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1090 ETH_ALEN, data->addresses[1].addr))
1091 goto nla_put_failure;
1093 /* We get the skb->data */
1094 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1095 goto nla_put_failure;
1097 /* We get the flags for this transmission, and we translate them to
1098 wmediumd flags */
1100 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1101 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1103 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1104 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1106 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1107 goto nla_put_failure;
1109 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1110 goto nla_put_failure;
1112 /* We get the tx control (rate and retries) info*/
1114 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1115 tx_attempts[i].idx = info->status.rates[i].idx;
1116 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1117 tx_attempts[i].count = info->status.rates[i].count;
1118 tx_attempts_flags[i].flags =
1119 trans_tx_rate_flags_ieee2hwsim(
1120 &info->status.rates[i]);
1123 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1124 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1125 tx_attempts))
1126 goto nla_put_failure;
1128 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1129 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1130 tx_attempts_flags))
1131 goto nla_put_failure;
1133 /* We create a cookie to identify this skb */
1134 data->pending_cookie++;
1135 cookie = data->pending_cookie;
1136 info->rate_driver_data[0] = (void *)cookie;
1137 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1138 goto nla_put_failure;
1140 genlmsg_end(skb, msg_head);
1141 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1142 goto err_free_txskb;
1144 /* Enqueue the packet */
1145 skb_queue_tail(&data->pending, my_skb);
1146 data->tx_pkts++;
1147 data->tx_bytes += my_skb->len;
1148 return;
1150 nla_put_failure:
1151 nlmsg_free(skb);
1152 err_free_txskb:
1153 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1154 ieee80211_free_txskb(hw, my_skb);
1155 data->tx_failed++;
1158 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1159 struct ieee80211_channel *c2)
1161 if (!c1 || !c2)
1162 return false;
1164 return c1->center_freq == c2->center_freq;
1167 struct tx_iter_data {
1168 struct ieee80211_channel *channel;
1169 bool receive;
1172 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1173 struct ieee80211_vif *vif)
1175 struct tx_iter_data *data = _data;
1177 if (!vif->chanctx_conf)
1178 return;
1180 if (!hwsim_chans_compat(data->channel,
1181 rcu_dereference(vif->chanctx_conf)->def.chan))
1182 return;
1184 data->receive = true;
1187 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1190 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1191 * e.g. like this:
1192 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1193 * (but you should use a valid OUI, not that)
1195 * If anyone wants to 'donate' a radiotap OUI/subns code
1196 * please send a patch removing this #ifdef and changing
1197 * the values accordingly.
1199 #ifdef HWSIM_RADIOTAP_OUI
1200 struct ieee80211_vendor_radiotap *rtap;
1203 * Note that this code requires the headroom in the SKB
1204 * that was allocated earlier.
1206 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1207 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1208 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1209 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1210 rtap->subns = 127;
1213 * Radiotap vendor namespaces can (and should) also be
1214 * split into fields by using the standard radiotap
1215 * presence bitmap mechanism. Use just BIT(0) here for
1216 * the presence bitmap.
1218 rtap->present = BIT(0);
1219 /* We have 8 bytes of (dummy) data */
1220 rtap->len = 8;
1221 /* For testing, also require it to be aligned */
1222 rtap->align = 8;
1223 /* And also test that padding works, 4 bytes */
1224 rtap->pad = 4;
1225 /* push the data */
1226 memcpy(rtap->data, "ABCDEFGH", 8);
1227 /* make sure to clear padding, mac80211 doesn't */
1228 memset(rtap->data + 8, 0, 4);
1230 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1231 #endif
1234 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1235 struct sk_buff *skb,
1236 struct ieee80211_channel *chan)
1238 struct mac80211_hwsim_data *data = hw->priv, *data2;
1239 bool ack = false;
1240 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1241 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1242 struct ieee80211_rx_status rx_status;
1243 u64 now;
1245 memset(&rx_status, 0, sizeof(rx_status));
1246 rx_status.flag |= RX_FLAG_MACTIME_START;
1247 rx_status.freq = chan->center_freq;
1248 rx_status.band = chan->band;
1249 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1250 rx_status.rate_idx =
1251 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1252 rx_status.nss =
1253 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1254 rx_status.encoding = RX_ENC_VHT;
1255 } else {
1256 rx_status.rate_idx = info->control.rates[0].idx;
1257 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1258 rx_status.encoding = RX_ENC_HT;
1260 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1261 rx_status.bw = RATE_INFO_BW_40;
1262 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1263 rx_status.bw = RATE_INFO_BW_80;
1264 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1265 rx_status.bw = RATE_INFO_BW_160;
1266 else
1267 rx_status.bw = RATE_INFO_BW_20;
1268 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1269 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1270 /* TODO: simulate real signal strength (and optional packet loss) */
1271 rx_status.signal = -50;
1272 if (info->control.vif)
1273 rx_status.signal += info->control.vif->bss_conf.txpower;
1275 if (data->ps != PS_DISABLED)
1276 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1278 /* release the skb's source info */
1279 skb_orphan(skb);
1280 skb_dst_drop(skb);
1281 skb->mark = 0;
1282 secpath_reset(skb);
1283 nf_reset(skb);
1286 * Get absolute mactime here so all HWs RX at the "same time", and
1287 * absolute TX time for beacon mactime so the timestamp matches.
1288 * Giving beacons a different mactime than non-beacons looks messy, but
1289 * it helps the Toffset be exact and a ~10us mactime discrepancy
1290 * probably doesn't really matter.
1292 if (ieee80211_is_beacon(hdr->frame_control) ||
1293 ieee80211_is_probe_resp(hdr->frame_control))
1294 now = data->abs_bcn_ts;
1295 else
1296 now = mac80211_hwsim_get_tsf_raw();
1298 /* Copy skb to all enabled radios that are on the current frequency */
1299 spin_lock(&hwsim_radio_lock);
1300 list_for_each_entry(data2, &hwsim_radios, list) {
1301 struct sk_buff *nskb;
1302 struct tx_iter_data tx_iter_data = {
1303 .receive = false,
1304 .channel = chan,
1307 if (data == data2)
1308 continue;
1310 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1311 !hwsim_ps_rx_ok(data2, skb))
1312 continue;
1314 if (!(data->group & data2->group))
1315 continue;
1317 if (data->netgroup != data2->netgroup)
1318 continue;
1320 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1321 !hwsim_chans_compat(chan, data2->channel)) {
1322 ieee80211_iterate_active_interfaces_atomic(
1323 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1324 mac80211_hwsim_tx_iter, &tx_iter_data);
1325 if (!tx_iter_data.receive)
1326 continue;
1330 * reserve some space for our vendor and the normal
1331 * radiotap header, since we're copying anyway
1333 if (skb->len < PAGE_SIZE && paged_rx) {
1334 struct page *page = alloc_page(GFP_ATOMIC);
1336 if (!page)
1337 continue;
1339 nskb = dev_alloc_skb(128);
1340 if (!nskb) {
1341 __free_page(page);
1342 continue;
1345 memcpy(page_address(page), skb->data, skb->len);
1346 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1347 } else {
1348 nskb = skb_copy(skb, GFP_ATOMIC);
1349 if (!nskb)
1350 continue;
1353 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1354 ack = true;
1356 rx_status.mactime = now + data2->tsf_offset;
1358 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1360 mac80211_hwsim_add_vendor_rtap(nskb);
1362 data2->rx_pkts++;
1363 data2->rx_bytes += nskb->len;
1364 ieee80211_rx_irqsafe(data2->hw, nskb);
1366 spin_unlock(&hwsim_radio_lock);
1368 return ack;
1371 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1372 struct ieee80211_tx_control *control,
1373 struct sk_buff *skb)
1375 struct mac80211_hwsim_data *data = hw->priv;
1376 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1377 struct ieee80211_hdr *hdr = (void *)skb->data;
1378 struct ieee80211_chanctx_conf *chanctx_conf;
1379 struct ieee80211_channel *channel;
1380 bool ack;
1381 u32 _portid;
1383 if (WARN_ON(skb->len < 10)) {
1384 /* Should not happen; just a sanity check for addr1 use */
1385 ieee80211_free_txskb(hw, skb);
1386 return;
1389 if (!data->use_chanctx) {
1390 channel = data->channel;
1391 } else if (txi->hw_queue == 4) {
1392 channel = data->tmp_chan;
1393 } else {
1394 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1395 if (chanctx_conf)
1396 channel = chanctx_conf->def.chan;
1397 else
1398 channel = NULL;
1401 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1402 ieee80211_free_txskb(hw, skb);
1403 return;
1406 if (data->idle && !data->tmp_chan) {
1407 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1408 ieee80211_free_txskb(hw, skb);
1409 return;
1412 if (txi->control.vif)
1413 hwsim_check_magic(txi->control.vif);
1414 if (control->sta)
1415 hwsim_check_sta_magic(control->sta);
1417 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1418 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1419 txi->control.rates,
1420 ARRAY_SIZE(txi->control.rates));
1422 if (skb->len >= 24 + 8 &&
1423 ieee80211_is_probe_resp(hdr->frame_control)) {
1424 /* fake header transmission time */
1425 struct ieee80211_mgmt *mgmt;
1426 struct ieee80211_rate *txrate;
1427 u64 ts;
1429 mgmt = (struct ieee80211_mgmt *)skb->data;
1430 txrate = ieee80211_get_tx_rate(hw, txi);
1431 ts = mac80211_hwsim_get_tsf_raw();
1432 mgmt->u.probe_resp.timestamp =
1433 cpu_to_le64(ts + data->tsf_offset +
1434 24 * 8 * 10 / txrate->bitrate);
1437 mac80211_hwsim_monitor_rx(hw, skb, channel);
1439 /* wmediumd mode check */
1440 _portid = READ_ONCE(data->wmediumd);
1442 if (_portid)
1443 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1445 /* NO wmediumd detected, perfect medium simulation */
1446 data->tx_pkts++;
1447 data->tx_bytes += skb->len;
1448 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1450 if (ack && skb->len >= 16)
1451 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1453 ieee80211_tx_info_clear_status(txi);
1455 /* frame was transmitted at most favorable rate at first attempt */
1456 txi->control.rates[0].count = 1;
1457 txi->control.rates[1].idx = -1;
1459 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1460 txi->flags |= IEEE80211_TX_STAT_ACK;
1461 ieee80211_tx_status_irqsafe(hw, skb);
1465 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1467 struct mac80211_hwsim_data *data = hw->priv;
1468 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1469 data->started = true;
1470 return 0;
1474 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1476 struct mac80211_hwsim_data *data = hw->priv;
1477 data->started = false;
1478 tasklet_hrtimer_cancel(&data->beacon_timer);
1479 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1483 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1484 struct ieee80211_vif *vif)
1486 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1487 __func__, ieee80211_vif_type_p2p(vif),
1488 vif->addr);
1489 hwsim_set_magic(vif);
1491 vif->cab_queue = 0;
1492 vif->hw_queue[IEEE80211_AC_VO] = 0;
1493 vif->hw_queue[IEEE80211_AC_VI] = 1;
1494 vif->hw_queue[IEEE80211_AC_BE] = 2;
1495 vif->hw_queue[IEEE80211_AC_BK] = 3;
1497 return 0;
1501 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1502 struct ieee80211_vif *vif,
1503 enum nl80211_iftype newtype,
1504 bool newp2p)
1506 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1507 wiphy_dbg(hw->wiphy,
1508 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1509 __func__, ieee80211_vif_type_p2p(vif),
1510 newtype, vif->addr);
1511 hwsim_check_magic(vif);
1514 * interface may change from non-AP to AP in
1515 * which case this needs to be set up again
1517 vif->cab_queue = 0;
1519 return 0;
1522 static void mac80211_hwsim_remove_interface(
1523 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1525 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1526 __func__, ieee80211_vif_type_p2p(vif),
1527 vif->addr);
1528 hwsim_check_magic(vif);
1529 hwsim_clear_magic(vif);
1532 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1533 struct sk_buff *skb,
1534 struct ieee80211_channel *chan)
1536 struct mac80211_hwsim_data *data = hw->priv;
1537 u32 _pid = READ_ONCE(data->wmediumd);
1539 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1540 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1541 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1542 txi->control.rates,
1543 ARRAY_SIZE(txi->control.rates));
1546 mac80211_hwsim_monitor_rx(hw, skb, chan);
1548 if (_pid)
1549 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1551 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1552 dev_kfree_skb(skb);
1555 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1556 struct ieee80211_vif *vif)
1558 struct mac80211_hwsim_data *data = arg;
1559 struct ieee80211_hw *hw = data->hw;
1560 struct ieee80211_tx_info *info;
1561 struct ieee80211_rate *txrate;
1562 struct ieee80211_mgmt *mgmt;
1563 struct sk_buff *skb;
1565 hwsim_check_magic(vif);
1567 if (vif->type != NL80211_IFTYPE_AP &&
1568 vif->type != NL80211_IFTYPE_MESH_POINT &&
1569 vif->type != NL80211_IFTYPE_ADHOC)
1570 return;
1572 skb = ieee80211_beacon_get(hw, vif);
1573 if (skb == NULL)
1574 return;
1575 info = IEEE80211_SKB_CB(skb);
1576 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1577 ieee80211_get_tx_rates(vif, NULL, skb,
1578 info->control.rates,
1579 ARRAY_SIZE(info->control.rates));
1581 txrate = ieee80211_get_tx_rate(hw, info);
1583 mgmt = (struct ieee80211_mgmt *) skb->data;
1584 /* fake header transmission time */
1585 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1586 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1587 data->tsf_offset +
1588 24 * 8 * 10 / txrate->bitrate);
1590 mac80211_hwsim_tx_frame(hw, skb,
1591 rcu_dereference(vif->chanctx_conf)->def.chan);
1593 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1594 ieee80211_csa_finish(vif);
1597 static enum hrtimer_restart
1598 mac80211_hwsim_beacon(struct hrtimer *timer)
1600 struct mac80211_hwsim_data *data =
1601 container_of(timer, struct mac80211_hwsim_data,
1602 beacon_timer.timer);
1603 struct ieee80211_hw *hw = data->hw;
1604 u64 bcn_int = data->beacon_int;
1605 ktime_t next_bcn;
1607 if (!data->started)
1608 goto out;
1610 ieee80211_iterate_active_interfaces_atomic(
1611 hw, IEEE80211_IFACE_ITER_NORMAL,
1612 mac80211_hwsim_beacon_tx, data);
1614 /* beacon at new TBTT + beacon interval */
1615 if (data->bcn_delta) {
1616 bcn_int -= data->bcn_delta;
1617 data->bcn_delta = 0;
1620 next_bcn = ktime_add(hrtimer_get_expires(timer),
1621 ns_to_ktime(bcn_int * 1000));
1622 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1623 out:
1624 return HRTIMER_NORESTART;
1627 static const char * const hwsim_chanwidths[] = {
1628 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1629 [NL80211_CHAN_WIDTH_20] = "ht20",
1630 [NL80211_CHAN_WIDTH_40] = "ht40",
1631 [NL80211_CHAN_WIDTH_80] = "vht80",
1632 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1633 [NL80211_CHAN_WIDTH_160] = "vht160",
1636 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1638 struct mac80211_hwsim_data *data = hw->priv;
1639 struct ieee80211_conf *conf = &hw->conf;
1640 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1641 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1642 [IEEE80211_SMPS_OFF] = "off",
1643 [IEEE80211_SMPS_STATIC] = "static",
1644 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1646 int idx;
1648 if (conf->chandef.chan)
1649 wiphy_dbg(hw->wiphy,
1650 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1651 __func__,
1652 conf->chandef.chan->center_freq,
1653 conf->chandef.center_freq1,
1654 conf->chandef.center_freq2,
1655 hwsim_chanwidths[conf->chandef.width],
1656 !!(conf->flags & IEEE80211_CONF_IDLE),
1657 !!(conf->flags & IEEE80211_CONF_PS),
1658 smps_modes[conf->smps_mode]);
1659 else
1660 wiphy_dbg(hw->wiphy,
1661 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1662 __func__,
1663 !!(conf->flags & IEEE80211_CONF_IDLE),
1664 !!(conf->flags & IEEE80211_CONF_PS),
1665 smps_modes[conf->smps_mode]);
1667 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1669 WARN_ON(conf->chandef.chan && data->use_chanctx);
1671 mutex_lock(&data->mutex);
1672 if (data->scanning && conf->chandef.chan) {
1673 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1674 if (data->survey_data[idx].channel == data->channel) {
1675 data->survey_data[idx].start =
1676 data->survey_data[idx].next_start;
1677 data->survey_data[idx].end = jiffies;
1678 break;
1682 data->channel = conf->chandef.chan;
1684 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1685 if (data->survey_data[idx].channel &&
1686 data->survey_data[idx].channel != data->channel)
1687 continue;
1688 data->survey_data[idx].channel = data->channel;
1689 data->survey_data[idx].next_start = jiffies;
1690 break;
1692 } else {
1693 data->channel = conf->chandef.chan;
1695 mutex_unlock(&data->mutex);
1697 if (!data->started || !data->beacon_int)
1698 tasklet_hrtimer_cancel(&data->beacon_timer);
1699 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
1700 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1701 u32 bcn_int = data->beacon_int;
1702 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1704 tasklet_hrtimer_start(&data->beacon_timer,
1705 ns_to_ktime(until_tbtt * 1000),
1706 HRTIMER_MODE_REL);
1709 return 0;
1713 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1714 unsigned int changed_flags,
1715 unsigned int *total_flags,u64 multicast)
1717 struct mac80211_hwsim_data *data = hw->priv;
1719 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1721 data->rx_filter = 0;
1722 if (*total_flags & FIF_ALLMULTI)
1723 data->rx_filter |= FIF_ALLMULTI;
1725 *total_flags = data->rx_filter;
1728 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1729 struct ieee80211_vif *vif)
1731 unsigned int *count = data;
1732 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1734 if (vp->bcn_en)
1735 (*count)++;
1738 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1739 struct ieee80211_vif *vif,
1740 struct ieee80211_bss_conf *info,
1741 u32 changed)
1743 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1744 struct mac80211_hwsim_data *data = hw->priv;
1746 hwsim_check_magic(vif);
1748 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1749 __func__, changed, vif->addr);
1751 if (changed & BSS_CHANGED_BSSID) {
1752 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1753 __func__, info->bssid);
1754 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1757 if (changed & BSS_CHANGED_ASSOC) {
1758 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1759 info->assoc, info->aid);
1760 vp->assoc = info->assoc;
1761 vp->aid = info->aid;
1764 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1765 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1766 info->enable_beacon, info->beacon_int);
1767 vp->bcn_en = info->enable_beacon;
1768 if (data->started &&
1769 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1770 info->enable_beacon) {
1771 u64 tsf, until_tbtt;
1772 u32 bcn_int;
1773 data->beacon_int = info->beacon_int * 1024;
1774 tsf = mac80211_hwsim_get_tsf(hw, vif);
1775 bcn_int = data->beacon_int;
1776 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1777 tasklet_hrtimer_start(&data->beacon_timer,
1778 ns_to_ktime(until_tbtt * 1000),
1779 HRTIMER_MODE_REL);
1780 } else if (!info->enable_beacon) {
1781 unsigned int count = 0;
1782 ieee80211_iterate_active_interfaces_atomic(
1783 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1784 mac80211_hwsim_bcn_en_iter, &count);
1785 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1786 count);
1787 if (count == 0) {
1788 tasklet_hrtimer_cancel(&data->beacon_timer);
1789 data->beacon_int = 0;
1794 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1795 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1796 info->use_cts_prot);
1799 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1800 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1801 info->use_short_preamble);
1804 if (changed & BSS_CHANGED_ERP_SLOT) {
1805 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1808 if (changed & BSS_CHANGED_HT) {
1809 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1810 info->ht_operation_mode);
1813 if (changed & BSS_CHANGED_BASIC_RATES) {
1814 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1815 (unsigned long long) info->basic_rates);
1818 if (changed & BSS_CHANGED_TXPOWER)
1819 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1822 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1823 struct ieee80211_vif *vif,
1824 struct ieee80211_sta *sta)
1826 hwsim_check_magic(vif);
1827 hwsim_set_sta_magic(sta);
1829 return 0;
1832 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1833 struct ieee80211_vif *vif,
1834 struct ieee80211_sta *sta)
1836 hwsim_check_magic(vif);
1837 hwsim_clear_sta_magic(sta);
1839 return 0;
1842 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1843 struct ieee80211_vif *vif,
1844 enum sta_notify_cmd cmd,
1845 struct ieee80211_sta *sta)
1847 hwsim_check_magic(vif);
1849 switch (cmd) {
1850 case STA_NOTIFY_SLEEP:
1851 case STA_NOTIFY_AWAKE:
1852 /* TODO: make good use of these flags */
1853 break;
1854 default:
1855 WARN(1, "Invalid sta notify: %d\n", cmd);
1856 break;
1860 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1861 struct ieee80211_sta *sta,
1862 bool set)
1864 hwsim_check_sta_magic(sta);
1865 return 0;
1868 static int mac80211_hwsim_conf_tx(
1869 struct ieee80211_hw *hw,
1870 struct ieee80211_vif *vif, u16 queue,
1871 const struct ieee80211_tx_queue_params *params)
1873 wiphy_dbg(hw->wiphy,
1874 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1875 __func__, queue,
1876 params->txop, params->cw_min,
1877 params->cw_max, params->aifs);
1878 return 0;
1881 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1882 struct survey_info *survey)
1884 struct mac80211_hwsim_data *hwsim = hw->priv;
1886 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
1887 return -ENOENT;
1889 mutex_lock(&hwsim->mutex);
1890 survey->channel = hwsim->survey_data[idx].channel;
1891 if (!survey->channel) {
1892 mutex_unlock(&hwsim->mutex);
1893 return -ENOENT;
1897 * Magically conjured dummy values --- this is only ok for simulated hardware.
1899 * A real driver which cannot determine real values noise MUST NOT
1900 * report any, especially not a magically conjured ones :-)
1902 survey->filled = SURVEY_INFO_NOISE_DBM |
1903 SURVEY_INFO_TIME |
1904 SURVEY_INFO_TIME_BUSY;
1905 survey->noise = -92;
1906 survey->time =
1907 jiffies_to_msecs(hwsim->survey_data[idx].end -
1908 hwsim->survey_data[idx].start);
1909 /* report 12.5% of channel time is used */
1910 survey->time_busy = survey->time/8;
1911 mutex_unlock(&hwsim->mutex);
1913 return 0;
1916 #ifdef CONFIG_NL80211_TESTMODE
1918 * This section contains example code for using netlink
1919 * attributes with the testmode command in nl80211.
1922 /* These enums need to be kept in sync with userspace */
1923 enum hwsim_testmode_attr {
1924 __HWSIM_TM_ATTR_INVALID = 0,
1925 HWSIM_TM_ATTR_CMD = 1,
1926 HWSIM_TM_ATTR_PS = 2,
1928 /* keep last */
1929 __HWSIM_TM_ATTR_AFTER_LAST,
1930 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1933 enum hwsim_testmode_cmd {
1934 HWSIM_TM_CMD_SET_PS = 0,
1935 HWSIM_TM_CMD_GET_PS = 1,
1936 HWSIM_TM_CMD_STOP_QUEUES = 2,
1937 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1940 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1941 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1942 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1945 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1946 struct ieee80211_vif *vif,
1947 void *data, int len)
1949 struct mac80211_hwsim_data *hwsim = hw->priv;
1950 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1951 struct sk_buff *skb;
1952 int err, ps;
1954 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1955 hwsim_testmode_policy, NULL);
1956 if (err)
1957 return err;
1959 if (!tb[HWSIM_TM_ATTR_CMD])
1960 return -EINVAL;
1962 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1963 case HWSIM_TM_CMD_SET_PS:
1964 if (!tb[HWSIM_TM_ATTR_PS])
1965 return -EINVAL;
1966 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1967 return hwsim_fops_ps_write(hwsim, ps);
1968 case HWSIM_TM_CMD_GET_PS:
1969 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1970 nla_total_size(sizeof(u32)));
1971 if (!skb)
1972 return -ENOMEM;
1973 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1974 goto nla_put_failure;
1975 return cfg80211_testmode_reply(skb);
1976 case HWSIM_TM_CMD_STOP_QUEUES:
1977 ieee80211_stop_queues(hw);
1978 return 0;
1979 case HWSIM_TM_CMD_WAKE_QUEUES:
1980 ieee80211_wake_queues(hw);
1981 return 0;
1982 default:
1983 return -EOPNOTSUPP;
1986 nla_put_failure:
1987 kfree_skb(skb);
1988 return -ENOBUFS;
1990 #endif
1992 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1993 struct ieee80211_vif *vif,
1994 struct ieee80211_ampdu_params *params)
1996 struct ieee80211_sta *sta = params->sta;
1997 enum ieee80211_ampdu_mlme_action action = params->action;
1998 u16 tid = params->tid;
2000 switch (action) {
2001 case IEEE80211_AMPDU_TX_START:
2002 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2003 break;
2004 case IEEE80211_AMPDU_TX_STOP_CONT:
2005 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2006 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2007 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2008 break;
2009 case IEEE80211_AMPDU_TX_OPERATIONAL:
2010 break;
2011 case IEEE80211_AMPDU_RX_START:
2012 case IEEE80211_AMPDU_RX_STOP:
2013 break;
2014 default:
2015 return -EOPNOTSUPP;
2018 return 0;
2021 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2022 struct ieee80211_vif *vif,
2023 u32 queues, bool drop)
2025 /* Not implemented, queues only on kernel side */
2028 static void hw_scan_work(struct work_struct *work)
2030 struct mac80211_hwsim_data *hwsim =
2031 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2032 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2033 int dwell, i;
2035 mutex_lock(&hwsim->mutex);
2036 if (hwsim->scan_chan_idx >= req->n_channels) {
2037 struct cfg80211_scan_info info = {
2038 .aborted = false,
2041 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2042 ieee80211_scan_completed(hwsim->hw, &info);
2043 hwsim->hw_scan_request = NULL;
2044 hwsim->hw_scan_vif = NULL;
2045 hwsim->tmp_chan = NULL;
2046 mutex_unlock(&hwsim->mutex);
2047 return;
2050 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2051 req->channels[hwsim->scan_chan_idx]->center_freq);
2053 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2054 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2055 IEEE80211_CHAN_RADAR) ||
2056 !req->n_ssids) {
2057 dwell = 120;
2058 } else {
2059 dwell = 30;
2060 /* send probes */
2061 for (i = 0; i < req->n_ssids; i++) {
2062 struct sk_buff *probe;
2063 struct ieee80211_mgmt *mgmt;
2065 probe = ieee80211_probereq_get(hwsim->hw,
2066 hwsim->scan_addr,
2067 req->ssids[i].ssid,
2068 req->ssids[i].ssid_len,
2069 req->ie_len);
2070 if (!probe)
2071 continue;
2073 mgmt = (struct ieee80211_mgmt *) probe->data;
2074 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2075 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2077 if (req->ie_len)
2078 skb_put_data(probe, req->ie, req->ie_len);
2080 local_bh_disable();
2081 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2082 hwsim->tmp_chan);
2083 local_bh_enable();
2086 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2087 msecs_to_jiffies(dwell));
2088 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2089 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2090 hwsim->survey_data[hwsim->scan_chan_idx].end =
2091 jiffies + msecs_to_jiffies(dwell);
2092 hwsim->scan_chan_idx++;
2093 mutex_unlock(&hwsim->mutex);
2096 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2097 struct ieee80211_vif *vif,
2098 struct ieee80211_scan_request *hw_req)
2100 struct mac80211_hwsim_data *hwsim = hw->priv;
2101 struct cfg80211_scan_request *req = &hw_req->req;
2103 mutex_lock(&hwsim->mutex);
2104 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2105 mutex_unlock(&hwsim->mutex);
2106 return -EBUSY;
2108 hwsim->hw_scan_request = req;
2109 hwsim->hw_scan_vif = vif;
2110 hwsim->scan_chan_idx = 0;
2111 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2112 get_random_mask_addr(hwsim->scan_addr,
2113 hw_req->req.mac_addr,
2114 hw_req->req.mac_addr_mask);
2115 else
2116 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2117 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2118 mutex_unlock(&hwsim->mutex);
2120 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2122 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2124 return 0;
2127 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2128 struct ieee80211_vif *vif)
2130 struct mac80211_hwsim_data *hwsim = hw->priv;
2131 struct cfg80211_scan_info info = {
2132 .aborted = true,
2135 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2137 cancel_delayed_work_sync(&hwsim->hw_scan);
2139 mutex_lock(&hwsim->mutex);
2140 ieee80211_scan_completed(hwsim->hw, &info);
2141 hwsim->tmp_chan = NULL;
2142 hwsim->hw_scan_request = NULL;
2143 hwsim->hw_scan_vif = NULL;
2144 mutex_unlock(&hwsim->mutex);
2147 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2148 struct ieee80211_vif *vif,
2149 const u8 *mac_addr)
2151 struct mac80211_hwsim_data *hwsim = hw->priv;
2153 mutex_lock(&hwsim->mutex);
2155 if (hwsim->scanning) {
2156 pr_debug("two hwsim sw_scans detected!\n");
2157 goto out;
2160 pr_debug("hwsim sw_scan request, prepping stuff\n");
2162 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2163 hwsim->scanning = true;
2164 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2166 out:
2167 mutex_unlock(&hwsim->mutex);
2170 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2171 struct ieee80211_vif *vif)
2173 struct mac80211_hwsim_data *hwsim = hw->priv;
2175 mutex_lock(&hwsim->mutex);
2177 pr_debug("hwsim sw_scan_complete\n");
2178 hwsim->scanning = false;
2179 eth_zero_addr(hwsim->scan_addr);
2181 mutex_unlock(&hwsim->mutex);
2184 static void hw_roc_start(struct work_struct *work)
2186 struct mac80211_hwsim_data *hwsim =
2187 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2189 mutex_lock(&hwsim->mutex);
2191 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2192 hwsim->tmp_chan = hwsim->roc_chan;
2193 ieee80211_ready_on_channel(hwsim->hw);
2195 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2196 msecs_to_jiffies(hwsim->roc_duration));
2198 mutex_unlock(&hwsim->mutex);
2201 static void hw_roc_done(struct work_struct *work)
2203 struct mac80211_hwsim_data *hwsim =
2204 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2206 mutex_lock(&hwsim->mutex);
2207 ieee80211_remain_on_channel_expired(hwsim->hw);
2208 hwsim->tmp_chan = NULL;
2209 mutex_unlock(&hwsim->mutex);
2211 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2214 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2215 struct ieee80211_vif *vif,
2216 struct ieee80211_channel *chan,
2217 int duration,
2218 enum ieee80211_roc_type type)
2220 struct mac80211_hwsim_data *hwsim = hw->priv;
2222 mutex_lock(&hwsim->mutex);
2223 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2224 mutex_unlock(&hwsim->mutex);
2225 return -EBUSY;
2228 hwsim->roc_chan = chan;
2229 hwsim->roc_duration = duration;
2230 mutex_unlock(&hwsim->mutex);
2232 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2233 chan->center_freq, duration);
2234 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2236 return 0;
2239 static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2241 struct mac80211_hwsim_data *hwsim = hw->priv;
2243 cancel_delayed_work_sync(&hwsim->roc_start);
2244 cancel_delayed_work_sync(&hwsim->roc_done);
2246 mutex_lock(&hwsim->mutex);
2247 hwsim->tmp_chan = NULL;
2248 mutex_unlock(&hwsim->mutex);
2250 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2252 return 0;
2255 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2256 struct ieee80211_chanctx_conf *ctx)
2258 hwsim_set_chanctx_magic(ctx);
2259 wiphy_dbg(hw->wiphy,
2260 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2261 ctx->def.chan->center_freq, ctx->def.width,
2262 ctx->def.center_freq1, ctx->def.center_freq2);
2263 return 0;
2266 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2267 struct ieee80211_chanctx_conf *ctx)
2269 wiphy_dbg(hw->wiphy,
2270 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2271 ctx->def.chan->center_freq, ctx->def.width,
2272 ctx->def.center_freq1, ctx->def.center_freq2);
2273 hwsim_check_chanctx_magic(ctx);
2274 hwsim_clear_chanctx_magic(ctx);
2277 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2278 struct ieee80211_chanctx_conf *ctx,
2279 u32 changed)
2281 hwsim_check_chanctx_magic(ctx);
2282 wiphy_dbg(hw->wiphy,
2283 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2284 ctx->def.chan->center_freq, ctx->def.width,
2285 ctx->def.center_freq1, ctx->def.center_freq2);
2288 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2289 struct ieee80211_vif *vif,
2290 struct ieee80211_chanctx_conf *ctx)
2292 hwsim_check_magic(vif);
2293 hwsim_check_chanctx_magic(ctx);
2295 return 0;
2298 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2299 struct ieee80211_vif *vif,
2300 struct ieee80211_chanctx_conf *ctx)
2302 hwsim_check_magic(vif);
2303 hwsim_check_chanctx_magic(ctx);
2306 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2307 "tx_pkts_nic",
2308 "tx_bytes_nic",
2309 "rx_pkts_nic",
2310 "rx_bytes_nic",
2311 "d_tx_dropped",
2312 "d_tx_failed",
2313 "d_ps_mode",
2314 "d_group",
2317 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2319 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2320 struct ieee80211_vif *vif,
2321 u32 sset, u8 *data)
2323 if (sset == ETH_SS_STATS)
2324 memcpy(data, *mac80211_hwsim_gstrings_stats,
2325 sizeof(mac80211_hwsim_gstrings_stats));
2328 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2329 struct ieee80211_vif *vif, int sset)
2331 if (sset == ETH_SS_STATS)
2332 return MAC80211_HWSIM_SSTATS_LEN;
2333 return 0;
2336 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2337 struct ieee80211_vif *vif,
2338 struct ethtool_stats *stats, u64 *data)
2340 struct mac80211_hwsim_data *ar = hw->priv;
2341 int i = 0;
2343 data[i++] = ar->tx_pkts;
2344 data[i++] = ar->tx_bytes;
2345 data[i++] = ar->rx_pkts;
2346 data[i++] = ar->rx_bytes;
2347 data[i++] = ar->tx_dropped;
2348 data[i++] = ar->tx_failed;
2349 data[i++] = ar->ps;
2350 data[i++] = ar->group;
2352 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2355 #define HWSIM_COMMON_OPS \
2356 .tx = mac80211_hwsim_tx, \
2357 .start = mac80211_hwsim_start, \
2358 .stop = mac80211_hwsim_stop, \
2359 .add_interface = mac80211_hwsim_add_interface, \
2360 .change_interface = mac80211_hwsim_change_interface, \
2361 .remove_interface = mac80211_hwsim_remove_interface, \
2362 .config = mac80211_hwsim_config, \
2363 .configure_filter = mac80211_hwsim_configure_filter, \
2364 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2365 .sta_add = mac80211_hwsim_sta_add, \
2366 .sta_remove = mac80211_hwsim_sta_remove, \
2367 .sta_notify = mac80211_hwsim_sta_notify, \
2368 .set_tim = mac80211_hwsim_set_tim, \
2369 .conf_tx = mac80211_hwsim_conf_tx, \
2370 .get_survey = mac80211_hwsim_get_survey, \
2371 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2372 .ampdu_action = mac80211_hwsim_ampdu_action, \
2373 .flush = mac80211_hwsim_flush, \
2374 .get_tsf = mac80211_hwsim_get_tsf, \
2375 .set_tsf = mac80211_hwsim_set_tsf, \
2376 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2377 .get_et_stats = mac80211_hwsim_get_et_stats, \
2378 .get_et_strings = mac80211_hwsim_get_et_strings,
2380 static const struct ieee80211_ops mac80211_hwsim_ops = {
2381 HWSIM_COMMON_OPS
2382 .sw_scan_start = mac80211_hwsim_sw_scan,
2383 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2386 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2387 HWSIM_COMMON_OPS
2388 .hw_scan = mac80211_hwsim_hw_scan,
2389 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2390 .sw_scan_start = NULL,
2391 .sw_scan_complete = NULL,
2392 .remain_on_channel = mac80211_hwsim_roc,
2393 .cancel_remain_on_channel = mac80211_hwsim_croc,
2394 .add_chanctx = mac80211_hwsim_add_chanctx,
2395 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2396 .change_chanctx = mac80211_hwsim_change_chanctx,
2397 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2398 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2401 struct hwsim_new_radio_params {
2402 unsigned int channels;
2403 const char *reg_alpha2;
2404 const struct ieee80211_regdomain *regd;
2405 bool reg_strict;
2406 bool p2p_device;
2407 bool use_chanctx;
2408 bool destroy_on_close;
2409 const char *hwname;
2410 bool no_vif;
2413 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2414 struct genl_info *info)
2416 if (info)
2417 genl_notify(&hwsim_genl_family, mcast_skb, info,
2418 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2419 else
2420 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2421 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2424 static int append_radio_msg(struct sk_buff *skb, int id,
2425 struct hwsim_new_radio_params *param)
2427 int ret;
2429 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2430 if (ret < 0)
2431 return ret;
2433 if (param->channels) {
2434 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2435 if (ret < 0)
2436 return ret;
2439 if (param->reg_alpha2) {
2440 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2441 param->reg_alpha2);
2442 if (ret < 0)
2443 return ret;
2446 if (param->regd) {
2447 int i;
2449 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2450 if (hwsim_world_regdom_custom[i] != param->regd)
2451 continue;
2453 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2454 if (ret < 0)
2455 return ret;
2456 break;
2460 if (param->reg_strict) {
2461 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2462 if (ret < 0)
2463 return ret;
2466 if (param->p2p_device) {
2467 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2468 if (ret < 0)
2469 return ret;
2472 if (param->use_chanctx) {
2473 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2474 if (ret < 0)
2475 return ret;
2478 if (param->hwname) {
2479 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2480 strlen(param->hwname), param->hwname);
2481 if (ret < 0)
2482 return ret;
2485 return 0;
2488 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2489 struct hwsim_new_radio_params *param)
2491 struct sk_buff *mcast_skb;
2492 void *data;
2494 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2495 if (!mcast_skb)
2496 return;
2498 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2499 HWSIM_CMD_NEW_RADIO);
2500 if (!data)
2501 goto out_err;
2503 if (append_radio_msg(mcast_skb, id, param) < 0)
2504 goto out_err;
2506 genlmsg_end(mcast_skb, data);
2508 hwsim_mcast_config_msg(mcast_skb, info);
2509 return;
2511 out_err:
2512 genlmsg_cancel(mcast_skb, data);
2513 nlmsg_free(mcast_skb);
2516 static int mac80211_hwsim_new_radio(struct genl_info *info,
2517 struct hwsim_new_radio_params *param)
2519 int err;
2520 u8 addr[ETH_ALEN];
2521 struct mac80211_hwsim_data *data;
2522 struct ieee80211_hw *hw;
2523 enum nl80211_band band;
2524 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2525 struct net *net;
2526 int idx;
2528 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2529 return -EINVAL;
2531 spin_lock_bh(&hwsim_radio_lock);
2532 idx = hwsim_radio_idx++;
2533 spin_unlock_bh(&hwsim_radio_lock);
2535 if (param->use_chanctx)
2536 ops = &mac80211_hwsim_mchan_ops;
2537 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2538 if (!hw) {
2539 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2540 err = -ENOMEM;
2541 goto failed;
2544 /* ieee80211_alloc_hw_nm may have used a default name */
2545 param->hwname = wiphy_name(hw->wiphy);
2547 if (info)
2548 net = genl_info_net(info);
2549 else
2550 net = &init_net;
2551 wiphy_net_set(hw->wiphy, net);
2553 data = hw->priv;
2554 data->hw = hw;
2556 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2557 if (IS_ERR(data->dev)) {
2558 printk(KERN_DEBUG
2559 "mac80211_hwsim: device_create failed (%ld)\n",
2560 PTR_ERR(data->dev));
2561 err = -ENOMEM;
2562 goto failed_drvdata;
2564 data->dev->driver = &mac80211_hwsim_driver.driver;
2565 err = device_bind_driver(data->dev);
2566 if (err != 0) {
2567 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
2568 err);
2569 goto failed_bind;
2572 skb_queue_head_init(&data->pending);
2574 SET_IEEE80211_DEV(hw, data->dev);
2575 eth_zero_addr(addr);
2576 addr[0] = 0x02;
2577 addr[3] = idx >> 8;
2578 addr[4] = idx;
2579 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2580 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2581 data->addresses[1].addr[0] |= 0x40;
2582 hw->wiphy->n_addresses = 2;
2583 hw->wiphy->addresses = data->addresses;
2585 data->channels = param->channels;
2586 data->use_chanctx = param->use_chanctx;
2587 data->idx = idx;
2588 data->destroy_on_close = param->destroy_on_close;
2589 if (info)
2590 data->portid = info->snd_portid;
2592 if (data->use_chanctx) {
2593 hw->wiphy->max_scan_ssids = 255;
2594 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2595 hw->wiphy->max_remain_on_channel_duration = 1000;
2596 hw->wiphy->iface_combinations = &data->if_combination;
2597 if (param->p2p_device)
2598 data->if_combination = hwsim_if_comb_p2p_dev[0];
2599 else
2600 data->if_combination = hwsim_if_comb[0];
2601 hw->wiphy->n_iface_combinations = 1;
2602 /* For channels > 1 DFS is not allowed */
2603 data->if_combination.radar_detect_widths = 0;
2604 data->if_combination.num_different_channels = data->channels;
2605 } else if (param->p2p_device) {
2606 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2607 hw->wiphy->n_iface_combinations =
2608 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
2609 } else {
2610 hw->wiphy->iface_combinations = hwsim_if_comb;
2611 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2614 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
2615 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2616 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2618 hw->queues = 5;
2619 hw->offchannel_tx_hw_queue = 4;
2620 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2621 BIT(NL80211_IFTYPE_AP) |
2622 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2623 BIT(NL80211_IFTYPE_P2P_GO) |
2624 BIT(NL80211_IFTYPE_ADHOC) |
2625 BIT(NL80211_IFTYPE_MESH_POINT);
2627 if (param->p2p_device)
2628 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
2630 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2631 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2632 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2633 ieee80211_hw_set(hw, QUEUE_CONTROL);
2634 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2635 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2636 ieee80211_hw_set(hw, MFP_CAPABLE);
2637 ieee80211_hw_set(hw, SIGNAL_DBM);
2638 ieee80211_hw_set(hw, TDLS_WIDER_BW);
2639 if (rctbl)
2640 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
2642 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2643 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2644 WIPHY_FLAG_AP_UAPSD |
2645 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
2646 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2647 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2648 NL80211_FEATURE_STATIC_SMPS |
2649 NL80211_FEATURE_DYNAMIC_SMPS |
2650 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
2651 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
2653 /* ask mac80211 to reserve space for magic */
2654 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2655 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2656 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2658 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2659 sizeof(hwsim_channels_2ghz));
2660 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2661 sizeof(hwsim_channels_5ghz));
2662 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2664 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
2665 struct ieee80211_supported_band *sband = &data->bands[band];
2666 switch (band) {
2667 case NL80211_BAND_2GHZ:
2668 sband->channels = data->channels_2ghz;
2669 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2670 sband->bitrates = data->rates;
2671 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2672 break;
2673 case NL80211_BAND_5GHZ:
2674 sband->channels = data->channels_5ghz;
2675 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2676 sband->bitrates = data->rates + 4;
2677 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
2679 sband->vht_cap.vht_supported = true;
2680 sband->vht_cap.cap =
2681 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2682 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2683 IEEE80211_VHT_CAP_RXLDPC |
2684 IEEE80211_VHT_CAP_SHORT_GI_80 |
2685 IEEE80211_VHT_CAP_SHORT_GI_160 |
2686 IEEE80211_VHT_CAP_TXSTBC |
2687 IEEE80211_VHT_CAP_RXSTBC_1 |
2688 IEEE80211_VHT_CAP_RXSTBC_2 |
2689 IEEE80211_VHT_CAP_RXSTBC_3 |
2690 IEEE80211_VHT_CAP_RXSTBC_4 |
2691 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2692 sband->vht_cap.vht_mcs.rx_mcs_map =
2693 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2694 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2695 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2696 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2697 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
2698 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2699 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
2700 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
2701 sband->vht_cap.vht_mcs.tx_mcs_map =
2702 sband->vht_cap.vht_mcs.rx_mcs_map;
2703 break;
2704 default:
2705 continue;
2708 sband->ht_cap.ht_supported = true;
2709 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2710 IEEE80211_HT_CAP_GRN_FLD |
2711 IEEE80211_HT_CAP_SGI_20 |
2712 IEEE80211_HT_CAP_SGI_40 |
2713 IEEE80211_HT_CAP_DSSSCCK40;
2714 sband->ht_cap.ampdu_factor = 0x3;
2715 sband->ht_cap.ampdu_density = 0x6;
2716 memset(&sband->ht_cap.mcs, 0,
2717 sizeof(sband->ht_cap.mcs));
2718 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2719 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2720 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2722 hw->wiphy->bands[band] = sband;
2725 /* By default all radios belong to the first group */
2726 data->group = 1;
2727 mutex_init(&data->mutex);
2729 data->netgroup = hwsim_net_get_netgroup(net);
2730 data->wmediumd = hwsim_net_get_wmediumd(net);
2732 /* Enable frame retransmissions for lossy channels */
2733 hw->max_rates = 4;
2734 hw->max_rate_tries = 11;
2736 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2737 hw->wiphy->n_vendor_commands =
2738 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2739 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2740 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2742 if (param->reg_strict)
2743 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
2744 if (param->regd) {
2745 data->regd = param->regd;
2746 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2747 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
2748 /* give the regulatory workqueue a chance to run */
2749 schedule_timeout_interruptible(1);
2752 if (param->no_vif)
2753 ieee80211_hw_set(hw, NO_AUTO_VIF);
2755 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2757 err = ieee80211_register_hw(hw);
2758 if (err < 0) {
2759 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2760 err);
2761 goto failed_hw;
2764 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
2766 if (param->reg_alpha2) {
2767 data->alpha2[0] = param->reg_alpha2[0];
2768 data->alpha2[1] = param->reg_alpha2[1];
2769 regulatory_hint(hw->wiphy, param->reg_alpha2);
2772 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2773 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2774 debugfs_create_file("group", 0666, data->debugfs, data,
2775 &hwsim_fops_group);
2776 if (!data->use_chanctx)
2777 debugfs_create_file("dfs_simulate_radar", 0222,
2778 data->debugfs,
2779 data, &hwsim_simulate_radar);
2781 tasklet_hrtimer_init(&data->beacon_timer,
2782 mac80211_hwsim_beacon,
2783 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2785 spin_lock_bh(&hwsim_radio_lock);
2786 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2787 hwsim_rht_params);
2788 if (err < 0) {
2789 pr_debug("mac80211_hwsim: radio index %d already present\n",
2790 idx);
2791 spin_unlock_bh(&hwsim_radio_lock);
2792 goto failed_final_insert;
2795 list_add_tail(&data->list, &hwsim_radios);
2796 spin_unlock_bh(&hwsim_radio_lock);
2798 if (idx > 0)
2799 hwsim_mcast_new_radio(idx, info, param);
2801 return idx;
2803 failed_final_insert:
2804 debugfs_remove_recursive(data->debugfs);
2805 ieee80211_unregister_hw(data->hw);
2806 failed_hw:
2807 device_release_driver(data->dev);
2808 failed_bind:
2809 device_unregister(data->dev);
2810 failed_drvdata:
2811 ieee80211_free_hw(hw);
2812 failed:
2813 return err;
2816 static void hwsim_mcast_del_radio(int id, const char *hwname,
2817 struct genl_info *info)
2819 struct sk_buff *skb;
2820 void *data;
2821 int ret;
2823 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2824 if (!skb)
2825 return;
2827 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2828 HWSIM_CMD_DEL_RADIO);
2829 if (!data)
2830 goto error;
2832 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2833 if (ret < 0)
2834 goto error;
2836 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2837 hwname);
2838 if (ret < 0)
2839 goto error;
2841 genlmsg_end(skb, data);
2843 hwsim_mcast_config_msg(skb, info);
2845 return;
2847 error:
2848 nlmsg_free(skb);
2851 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2852 const char *hwname,
2853 struct genl_info *info)
2855 hwsim_mcast_del_radio(data->idx, hwname, info);
2856 debugfs_remove_recursive(data->debugfs);
2857 ieee80211_unregister_hw(data->hw);
2858 device_release_driver(data->dev);
2859 device_unregister(data->dev);
2860 ieee80211_free_hw(data->hw);
2863 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
2864 struct mac80211_hwsim_data *data,
2865 u32 portid, u32 seq,
2866 struct netlink_callback *cb, int flags)
2868 void *hdr;
2869 struct hwsim_new_radio_params param = { };
2870 int res = -EMSGSIZE;
2872 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
2873 HWSIM_CMD_GET_RADIO);
2874 if (!hdr)
2875 return -EMSGSIZE;
2877 if (cb)
2878 genl_dump_check_consistent(cb, hdr);
2880 if (data->alpha2[0] && data->alpha2[1])
2881 param.reg_alpha2 = data->alpha2;
2883 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
2884 REGULATORY_STRICT_REG);
2885 param.p2p_device = !!(data->hw->wiphy->interface_modes &
2886 BIT(NL80211_IFTYPE_P2P_DEVICE));
2887 param.use_chanctx = data->use_chanctx;
2888 param.regd = data->regd;
2889 param.channels = data->channels;
2890 param.hwname = wiphy_name(data->hw->wiphy);
2892 res = append_radio_msg(skb, data->idx, &param);
2893 if (res < 0)
2894 goto out_err;
2896 genlmsg_end(skb, hdr);
2897 return 0;
2899 out_err:
2900 genlmsg_cancel(skb, hdr);
2901 return res;
2904 static void mac80211_hwsim_free(void)
2906 struct mac80211_hwsim_data *data;
2908 spin_lock_bh(&hwsim_radio_lock);
2909 while ((data = list_first_entry_or_null(&hwsim_radios,
2910 struct mac80211_hwsim_data,
2911 list))) {
2912 list_del(&data->list);
2913 spin_unlock_bh(&hwsim_radio_lock);
2914 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
2915 NULL);
2916 spin_lock_bh(&hwsim_radio_lock);
2918 spin_unlock_bh(&hwsim_radio_lock);
2919 class_destroy(hwsim_class);
2922 static const struct net_device_ops hwsim_netdev_ops = {
2923 .ndo_start_xmit = hwsim_mon_xmit,
2924 .ndo_set_mac_address = eth_mac_addr,
2925 .ndo_validate_addr = eth_validate_addr,
2928 static void hwsim_mon_setup(struct net_device *dev)
2930 dev->netdev_ops = &hwsim_netdev_ops;
2931 dev->needs_free_netdev = true;
2932 ether_setup(dev);
2933 dev->priv_flags |= IFF_NO_QUEUE;
2934 dev->type = ARPHRD_IEEE80211_RADIOTAP;
2935 eth_zero_addr(dev->dev_addr);
2936 dev->dev_addr[0] = 0x12;
2939 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
2941 return rhashtable_lookup_fast(&hwsim_radios_rht,
2942 addr,
2943 hwsim_rht_params);
2946 static void hwsim_register_wmediumd(struct net *net, u32 portid)
2948 struct mac80211_hwsim_data *data;
2950 hwsim_net_set_wmediumd(net, portid);
2952 spin_lock_bh(&hwsim_radio_lock);
2953 list_for_each_entry(data, &hwsim_radios, list) {
2954 if (data->netgroup == hwsim_net_get_netgroup(net))
2955 data->wmediumd = portid;
2957 spin_unlock_bh(&hwsim_radio_lock);
2960 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2961 struct genl_info *info)
2964 struct ieee80211_hdr *hdr;
2965 struct mac80211_hwsim_data *data2;
2966 struct ieee80211_tx_info *txi;
2967 struct hwsim_tx_rate *tx_attempts;
2968 u64 ret_skb_cookie;
2969 struct sk_buff *skb, *tmp;
2970 const u8 *src;
2971 unsigned int hwsim_flags;
2972 int i;
2973 bool found = false;
2975 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
2976 !info->attrs[HWSIM_ATTR_FLAGS] ||
2977 !info->attrs[HWSIM_ATTR_COOKIE] ||
2978 !info->attrs[HWSIM_ATTR_SIGNAL] ||
2979 !info->attrs[HWSIM_ATTR_TX_INFO])
2980 goto out;
2982 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
2983 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
2984 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
2986 data2 = get_hwsim_data_ref_from_addr(src);
2987 if (!data2)
2988 goto out;
2990 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
2991 goto out;
2993 if (info->snd_portid != data2->wmediumd)
2994 goto out;
2996 /* look for the skb matching the cookie passed back from user */
2997 skb_queue_walk_safe(&data2->pending, skb, tmp) {
2998 u64 skb_cookie;
3000 txi = IEEE80211_SKB_CB(skb);
3001 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3003 if (skb_cookie == ret_skb_cookie) {
3004 skb_unlink(skb, &data2->pending);
3005 found = true;
3006 break;
3010 /* not found */
3011 if (!found)
3012 goto out;
3014 /* Tx info received because the frame was broadcasted on user space,
3015 so we get all the necessary info: tx attempts and skb control buff */
3017 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3018 info->attrs[HWSIM_ATTR_TX_INFO]);
3020 /* now send back TX status */
3021 txi = IEEE80211_SKB_CB(skb);
3023 ieee80211_tx_info_clear_status(txi);
3025 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3026 txi->status.rates[i].idx = tx_attempts[i].idx;
3027 txi->status.rates[i].count = tx_attempts[i].count;
3030 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3032 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3033 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3034 if (skb->len >= 16) {
3035 hdr = (struct ieee80211_hdr *) skb->data;
3036 mac80211_hwsim_monitor_ack(data2->channel,
3037 hdr->addr2);
3039 txi->flags |= IEEE80211_TX_STAT_ACK;
3041 ieee80211_tx_status_irqsafe(data2->hw, skb);
3042 return 0;
3043 out:
3044 return -EINVAL;
3048 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3049 struct genl_info *info)
3051 struct mac80211_hwsim_data *data2;
3052 struct ieee80211_rx_status rx_status;
3053 const u8 *dst;
3054 int frame_data_len;
3055 void *frame_data;
3056 struct sk_buff *skb = NULL;
3058 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3059 !info->attrs[HWSIM_ATTR_FRAME] ||
3060 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3061 !info->attrs[HWSIM_ATTR_SIGNAL])
3062 goto out;
3064 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3065 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3066 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3068 /* Allocate new skb here */
3069 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3070 if (skb == NULL)
3071 goto err;
3073 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3074 goto err;
3076 /* Copy the data */
3077 skb_put_data(skb, frame_data, frame_data_len);
3079 data2 = get_hwsim_data_ref_from_addr(dst);
3080 if (!data2)
3081 goto out;
3083 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3084 goto out;
3086 if (info->snd_portid != data2->wmediumd)
3087 goto out;
3089 /* check if radio is configured properly */
3091 if (data2->idle || !data2->started)
3092 goto out;
3094 /* A frame is received from user space */
3095 memset(&rx_status, 0, sizeof(rx_status));
3096 if (info->attrs[HWSIM_ATTR_FREQ]) {
3097 /* throw away off-channel packets, but allow both the temporary
3098 * ("hw" scan/remain-on-channel) and regular channel, since the
3099 * internal datapath also allows this
3101 mutex_lock(&data2->mutex);
3102 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3104 if (rx_status.freq != data2->channel->center_freq &&
3105 (!data2->tmp_chan ||
3106 rx_status.freq != data2->tmp_chan->center_freq)) {
3107 mutex_unlock(&data2->mutex);
3108 goto out;
3110 mutex_unlock(&data2->mutex);
3111 } else {
3112 rx_status.freq = data2->channel->center_freq;
3115 rx_status.band = data2->channel->band;
3116 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3117 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3119 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3120 data2->rx_pkts++;
3121 data2->rx_bytes += skb->len;
3122 ieee80211_rx_irqsafe(data2->hw, skb);
3124 return 0;
3125 err:
3126 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3127 out:
3128 dev_kfree_skb(skb);
3129 return -EINVAL;
3132 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3133 struct genl_info *info)
3135 struct net *net = genl_info_net(info);
3136 struct mac80211_hwsim_data *data;
3137 int chans = 1;
3139 spin_lock_bh(&hwsim_radio_lock);
3140 list_for_each_entry(data, &hwsim_radios, list)
3141 chans = max(chans, data->channels);
3142 spin_unlock_bh(&hwsim_radio_lock);
3144 /* In the future we should revise the userspace API and allow it
3145 * to set a flag that it does support multi-channel, then we can
3146 * let this pass conditionally on the flag.
3147 * For current userspace, prohibit it since it won't work right.
3149 if (chans > 1)
3150 return -EOPNOTSUPP;
3152 if (hwsim_net_get_wmediumd(net))
3153 return -EBUSY;
3155 hwsim_register_wmediumd(net, info->snd_portid);
3157 pr_debug("mac80211_hwsim: received a REGISTER, "
3158 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3160 return 0;
3163 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3165 struct hwsim_new_radio_params param = { 0 };
3166 const char *hwname = NULL;
3167 int ret;
3169 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3170 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3171 param.channels = channels;
3172 param.destroy_on_close =
3173 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3175 if (info->attrs[HWSIM_ATTR_CHANNELS])
3176 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3178 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3179 GENL_SET_ERR_MSG(info, "too many channels specified");
3180 return -EINVAL;
3183 if (info->attrs[HWSIM_ATTR_NO_VIF])
3184 param.no_vif = true;
3186 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3187 hwname = kasprintf(GFP_KERNEL, "%.*s",
3188 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3189 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3190 if (!hwname)
3191 return -ENOMEM;
3192 param.hwname = hwname;
3195 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3196 param.use_chanctx = true;
3197 else
3198 param.use_chanctx = (param.channels > 1);
3200 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3201 param.reg_alpha2 =
3202 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3204 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3205 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3207 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3208 kfree(hwname);
3209 return -EINVAL;
3211 param.regd = hwsim_world_regdom_custom[idx];
3214 ret = mac80211_hwsim_new_radio(info, &param);
3215 kfree(hwname);
3216 return ret;
3219 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3221 struct mac80211_hwsim_data *data;
3222 s64 idx = -1;
3223 const char *hwname = NULL;
3225 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3226 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3227 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3228 hwname = kasprintf(GFP_KERNEL, "%.*s",
3229 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3230 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3231 if (!hwname)
3232 return -ENOMEM;
3233 } else
3234 return -EINVAL;
3236 spin_lock_bh(&hwsim_radio_lock);
3237 list_for_each_entry(data, &hwsim_radios, list) {
3238 if (idx >= 0) {
3239 if (data->idx != idx)
3240 continue;
3241 } else {
3242 if (!hwname ||
3243 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3244 continue;
3247 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3248 continue;
3250 list_del(&data->list);
3251 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3252 hwsim_rht_params);
3253 spin_unlock_bh(&hwsim_radio_lock);
3254 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3255 info);
3256 kfree(hwname);
3257 return 0;
3259 spin_unlock_bh(&hwsim_radio_lock);
3261 kfree(hwname);
3262 return -ENODEV;
3265 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3267 struct mac80211_hwsim_data *data;
3268 struct sk_buff *skb;
3269 int idx, res = -ENODEV;
3271 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3272 return -EINVAL;
3273 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3275 spin_lock_bh(&hwsim_radio_lock);
3276 list_for_each_entry(data, &hwsim_radios, list) {
3277 if (data->idx != idx)
3278 continue;
3280 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3281 continue;
3283 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3284 if (!skb) {
3285 res = -ENOMEM;
3286 goto out_err;
3289 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3290 info->snd_seq, NULL, 0);
3291 if (res < 0) {
3292 nlmsg_free(skb);
3293 goto out_err;
3296 genlmsg_reply(skb, info);
3297 break;
3300 out_err:
3301 spin_unlock_bh(&hwsim_radio_lock);
3303 return res;
3306 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3307 struct netlink_callback *cb)
3309 int idx = cb->args[0];
3310 struct mac80211_hwsim_data *data = NULL;
3311 int res;
3313 spin_lock_bh(&hwsim_radio_lock);
3315 if (idx == hwsim_radio_idx)
3316 goto done;
3318 list_for_each_entry(data, &hwsim_radios, list) {
3319 if (data->idx < idx)
3320 continue;
3322 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3323 continue;
3325 res = mac80211_hwsim_get_radio(skb, data,
3326 NETLINK_CB(cb->skb).portid,
3327 cb->nlh->nlmsg_seq, cb,
3328 NLM_F_MULTI);
3329 if (res < 0)
3330 break;
3332 idx = data->idx + 1;
3335 cb->args[0] = idx;
3337 done:
3338 spin_unlock_bh(&hwsim_radio_lock);
3339 return skb->len;
3342 /* Generic Netlink operations array */
3343 static const struct genl_ops hwsim_ops[] = {
3345 .cmd = HWSIM_CMD_REGISTER,
3346 .policy = hwsim_genl_policy,
3347 .doit = hwsim_register_received_nl,
3348 .flags = GENL_UNS_ADMIN_PERM,
3351 .cmd = HWSIM_CMD_FRAME,
3352 .policy = hwsim_genl_policy,
3353 .doit = hwsim_cloned_frame_received_nl,
3356 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3357 .policy = hwsim_genl_policy,
3358 .doit = hwsim_tx_info_frame_received_nl,
3361 .cmd = HWSIM_CMD_NEW_RADIO,
3362 .policy = hwsim_genl_policy,
3363 .doit = hwsim_new_radio_nl,
3364 .flags = GENL_UNS_ADMIN_PERM,
3367 .cmd = HWSIM_CMD_DEL_RADIO,
3368 .policy = hwsim_genl_policy,
3369 .doit = hwsim_del_radio_nl,
3370 .flags = GENL_UNS_ADMIN_PERM,
3373 .cmd = HWSIM_CMD_GET_RADIO,
3374 .policy = hwsim_genl_policy,
3375 .doit = hwsim_get_radio_nl,
3376 .dumpit = hwsim_dump_radio_nl,
3380 static struct genl_family hwsim_genl_family __ro_after_init = {
3381 .name = "MAC80211_HWSIM",
3382 .version = 1,
3383 .maxattr = HWSIM_ATTR_MAX,
3384 .netnsok = true,
3385 .module = THIS_MODULE,
3386 .ops = hwsim_ops,
3387 .n_ops = ARRAY_SIZE(hwsim_ops),
3388 .mcgrps = hwsim_mcgrps,
3389 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3392 static void destroy_radio(struct work_struct *work)
3394 struct mac80211_hwsim_data *data =
3395 container_of(work, struct mac80211_hwsim_data, destroy_work);
3397 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
3400 static void remove_user_radios(u32 portid)
3402 struct mac80211_hwsim_data *entry, *tmp;
3404 spin_lock_bh(&hwsim_radio_lock);
3405 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3406 if (entry->destroy_on_close && entry->portid == portid) {
3407 list_del(&entry->list);
3408 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3409 hwsim_rht_params);
3410 INIT_WORK(&entry->destroy_work, destroy_radio);
3411 queue_work(hwsim_wq, &entry->destroy_work);
3414 spin_unlock_bh(&hwsim_radio_lock);
3417 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3418 unsigned long state,
3419 void *_notify)
3421 struct netlink_notify *notify = _notify;
3423 if (state != NETLINK_URELEASE)
3424 return NOTIFY_DONE;
3426 remove_user_radios(notify->portid);
3428 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
3429 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3430 " socket, switching to perfect channel medium\n");
3431 hwsim_register_wmediumd(notify->net, 0);
3433 return NOTIFY_DONE;
3437 static struct notifier_block hwsim_netlink_notifier = {
3438 .notifier_call = mac80211_hwsim_netlink_notify,
3441 static int __init hwsim_init_netlink(void)
3443 int rc;
3445 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3447 rc = genl_register_family(&hwsim_genl_family);
3448 if (rc)
3449 goto failure;
3451 rc = netlink_register_notifier(&hwsim_netlink_notifier);
3452 if (rc) {
3453 genl_unregister_family(&hwsim_genl_family);
3454 goto failure;
3457 return 0;
3459 failure:
3460 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3461 return -EINVAL;
3464 static __net_init int hwsim_init_net(struct net *net)
3466 hwsim_net_set_netgroup(net);
3468 return 0;
3471 static void __net_exit hwsim_exit_net(struct net *net)
3473 struct mac80211_hwsim_data *data, *tmp;
3475 spin_lock_bh(&hwsim_radio_lock);
3476 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3477 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3478 continue;
3480 /* Radios created in init_net are returned to init_net. */
3481 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3482 continue;
3484 list_del(&data->list);
3485 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3486 hwsim_rht_params);
3487 spin_unlock_bh(&hwsim_radio_lock);
3488 mac80211_hwsim_del_radio(data,
3489 wiphy_name(data->hw->wiphy),
3490 NULL);
3491 spin_lock_bh(&hwsim_radio_lock);
3493 spin_unlock_bh(&hwsim_radio_lock);
3496 static struct pernet_operations hwsim_net_ops = {
3497 .init = hwsim_init_net,
3498 .exit = hwsim_exit_net,
3499 .id = &hwsim_net_id,
3500 .size = sizeof(struct hwsim_net),
3503 static void hwsim_exit_netlink(void)
3505 /* unregister the notifier */
3506 netlink_unregister_notifier(&hwsim_netlink_notifier);
3507 /* unregister the family */
3508 genl_unregister_family(&hwsim_genl_family);
3511 static int __init init_mac80211_hwsim(void)
3513 int i, err;
3515 if (radios < 0 || radios > 100)
3516 return -EINVAL;
3518 if (channels < 1)
3519 return -EINVAL;
3521 spin_lock_init(&hwsim_radio_lock);
3523 hwsim_wq = alloc_workqueue("hwsim_wq", 0, 0);
3524 if (!hwsim_wq)
3525 return -ENOMEM;
3526 rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
3528 err = register_pernet_device(&hwsim_net_ops);
3529 if (err)
3530 return err;
3532 err = platform_driver_register(&mac80211_hwsim_driver);
3533 if (err)
3534 goto out_unregister_pernet;
3536 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
3537 if (IS_ERR(hwsim_class)) {
3538 err = PTR_ERR(hwsim_class);
3539 goto out_unregister_driver;
3542 err = hwsim_init_netlink();
3543 if (err < 0)
3544 goto out_unregister_driver;
3546 for (i = 0; i < radios; i++) {
3547 struct hwsim_new_radio_params param = { 0 };
3549 param.channels = channels;
3551 switch (regtest) {
3552 case HWSIM_REGTEST_DIFF_COUNTRY:
3553 if (i < ARRAY_SIZE(hwsim_alpha2s))
3554 param.reg_alpha2 = hwsim_alpha2s[i];
3555 break;
3556 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3557 if (!i)
3558 param.reg_alpha2 = hwsim_alpha2s[0];
3559 break;
3560 case HWSIM_REGTEST_STRICT_ALL:
3561 param.reg_strict = true;
3562 case HWSIM_REGTEST_DRIVER_REG_ALL:
3563 param.reg_alpha2 = hwsim_alpha2s[0];
3564 break;
3565 case HWSIM_REGTEST_WORLD_ROAM:
3566 if (i == 0)
3567 param.regd = &hwsim_world_regdom_custom_01;
3568 break;
3569 case HWSIM_REGTEST_CUSTOM_WORLD:
3570 param.regd = &hwsim_world_regdom_custom_01;
3571 break;
3572 case HWSIM_REGTEST_CUSTOM_WORLD_2:
3573 if (i == 0)
3574 param.regd = &hwsim_world_regdom_custom_01;
3575 else if (i == 1)
3576 param.regd = &hwsim_world_regdom_custom_02;
3577 break;
3578 case HWSIM_REGTEST_STRICT_FOLLOW:
3579 if (i == 0) {
3580 param.reg_strict = true;
3581 param.reg_alpha2 = hwsim_alpha2s[0];
3583 break;
3584 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
3585 if (i == 0) {
3586 param.reg_strict = true;
3587 param.reg_alpha2 = hwsim_alpha2s[0];
3588 } else if (i == 1) {
3589 param.reg_alpha2 = hwsim_alpha2s[1];
3591 break;
3592 case HWSIM_REGTEST_ALL:
3593 switch (i) {
3594 case 0:
3595 param.regd = &hwsim_world_regdom_custom_01;
3596 break;
3597 case 1:
3598 param.regd = &hwsim_world_regdom_custom_02;
3599 break;
3600 case 2:
3601 param.reg_alpha2 = hwsim_alpha2s[0];
3602 break;
3603 case 3:
3604 param.reg_alpha2 = hwsim_alpha2s[1];
3605 break;
3606 case 4:
3607 param.reg_strict = true;
3608 param.reg_alpha2 = hwsim_alpha2s[2];
3609 break;
3611 break;
3612 default:
3613 break;
3616 param.p2p_device = support_p2p_device;
3617 param.use_chanctx = channels > 1;
3619 err = mac80211_hwsim_new_radio(NULL, &param);
3620 if (err < 0)
3621 goto out_free_radios;
3624 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3625 hwsim_mon_setup);
3626 if (hwsim_mon == NULL) {
3627 err = -ENOMEM;
3628 goto out_free_radios;
3631 rtnl_lock();
3632 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
3633 if (err < 0) {
3634 rtnl_unlock();
3635 goto out_free_radios;
3638 err = register_netdevice(hwsim_mon);
3639 if (err < 0) {
3640 rtnl_unlock();
3641 goto out_free_mon;
3643 rtnl_unlock();
3645 return 0;
3647 out_free_mon:
3648 free_netdev(hwsim_mon);
3649 out_free_radios:
3650 mac80211_hwsim_free();
3651 out_unregister_driver:
3652 platform_driver_unregister(&mac80211_hwsim_driver);
3653 out_unregister_pernet:
3654 unregister_pernet_device(&hwsim_net_ops);
3655 return err;
3657 module_init(init_mac80211_hwsim);
3659 static void __exit exit_mac80211_hwsim(void)
3661 pr_debug("mac80211_hwsim: unregister radios\n");
3663 hwsim_exit_netlink();
3665 mac80211_hwsim_free();
3666 flush_workqueue(hwsim_wq);
3668 rhashtable_destroy(&hwsim_radios_rht);
3669 unregister_netdev(hwsim_mon);
3670 platform_driver_unregister(&mac80211_hwsim_driver);
3671 unregister_pernet_device(&hwsim_net_ops);
3672 destroy_workqueue(hwsim_wq);
3674 module_exit(exit_mac80211_hwsim);