blob: 9ba74531baa1a543d5780d7f4e6bacc53fd5a2c4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
4 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
6 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018 - 2020 Intel Corporation
8 */
9
10/*
11 * TODO:
12 * - Add TSF sync and fix IBSS beacon transmission by adding
13 * competition for "air time" at TBTT
14 * - RX filtering based on filter configuration (data->rx_filter)
15 */
16
17#include <linux/list.h>
18#include <linux/slab.h>
19#include <linux/spinlock.h>
20#include <net/dst.h>
21#include <net/xfrm.h>
22#include <net/mac80211.h>
23#include <net/ieee80211_radiotap.h>
24#include <linux/if_arp.h>
25#include <linux/rtnetlink.h>
26#include <linux/etherdevice.h>
27#include <linux/platform_device.h>
28#include <linux/debugfs.h>
29#include <linux/module.h>
30#include <linux/ktime.h>
31#include <net/genetlink.h>
32#include <net/net_namespace.h>
33#include <net/netns/generic.h>
34#include <linux/rhashtable.h>
35#include <linux/nospec.h>
36#include <linux/virtio.h>
37#include <linux/virtio_ids.h>
38#include <linux/virtio_config.h>
39#include "mac80211_hwsim.h"
40
41#define WARN_QUEUE 100
42#define MAX_QUEUE 200
43
44MODULE_AUTHOR("Jouni Malinen");
45MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46MODULE_LICENSE("GPL");
47
48static int radios = 2;
49module_param(radios, int, 0444);
50MODULE_PARM_DESC(radios, "Number of simulated radios");
51
52static int channels = 1;
53module_param(channels, int, 0444);
54MODULE_PARM_DESC(channels, "Number of concurrent channels");
55
56static bool paged_rx = false;
57module_param(paged_rx, bool, 0644);
58MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
59
60static bool rctbl = false;
61module_param(rctbl, bool, 0444);
62MODULE_PARM_DESC(rctbl, "Handle rate control table");
63
64static bool support_p2p_device = true;
65module_param(support_p2p_device, bool, 0444);
66MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
67
68static ushort mac_prefix;
69module_param(mac_prefix, ushort, 0444);
70MODULE_PARM_DESC(mac_prefix, "Second and third most significant octets in MAC");
71
72/**
73 * enum hwsim_regtest - the type of regulatory tests we offer
74 *
75 * These are the different values you can use for the regtest
76 * module parameter. This is useful to help test world roaming
77 * and the driver regulatory_hint() call and combinations of these.
78 * If you want to do specific alpha2 regulatory domain tests simply
79 * use the userspace regulatory request as that will be respected as
80 * well without the need of this module parameter. This is designed
81 * only for testing the driver regulatory request, world roaming
82 * and all possible combinations.
83 *
84 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
85 * this is the default value.
86 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
87 * hint, only one driver regulatory hint will be sent as such the
88 * secondary radios are expected to follow.
89 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
90 * request with all radios reporting the same regulatory domain.
91 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
92 * different regulatory domains requests. Expected behaviour is for
93 * an intersection to occur but each device will still use their
94 * respective regulatory requested domains. Subsequent radios will
95 * use the resulting intersection.
96 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
97 * this by using a custom beacon-capable regulatory domain for the first
98 * radio. All other device world roam.
99 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
100 * domain requests. All radios will adhere to this custom world regulatory
101 * domain.
102 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
103 * domain requests. The first radio will adhere to the first custom world
104 * regulatory domain, the second one to the second custom world regulatory
105 * domain. All other devices will world roam.
106 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
107 * settings, only the first radio will send a regulatory domain request
108 * and use strict settings. The rest of the radios are expected to follow.
109 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
110 * settings. All radios will adhere to this.
111 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
112 * domain settings, combined with secondary driver regulatory domain
113 * settings. The first radio will get a strict regulatory domain setting
114 * using the first driver regulatory request and the second radio will use
115 * non-strict settings using the second driver regulatory request. All
116 * other devices should follow the intersection created between the
117 * first two.
118 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
119 * at least 6 radios for a complete test. We will test in this order:
120 * 1 - driver custom world regulatory domain
121 * 2 - second custom world regulatory domain
122 * 3 - first driver regulatory domain request
123 * 4 - second driver regulatory domain request
124 * 5 - strict regulatory domain settings using the third driver regulatory
125 * domain request
126 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
127 * regulatory requests.
128 */
129enum hwsim_regtest {
130 HWSIM_REGTEST_DISABLED = 0,
131 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
132 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
133 HWSIM_REGTEST_DIFF_COUNTRY = 3,
134 HWSIM_REGTEST_WORLD_ROAM = 4,
135 HWSIM_REGTEST_CUSTOM_WORLD = 5,
136 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
137 HWSIM_REGTEST_STRICT_FOLLOW = 7,
138 HWSIM_REGTEST_STRICT_ALL = 8,
139 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
140 HWSIM_REGTEST_ALL = 10,
141};
142
143/* Set to one of the HWSIM_REGTEST_* values above */
144static int regtest = HWSIM_REGTEST_DISABLED;
145module_param(regtest, int, 0444);
146MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
147
148static const char *hwsim_alpha2s[] = {
149 "FI",
150 "AL",
151 "US",
152 "DE",
153 "JP",
154 "AL",
155};
156
157static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
158 .n_reg_rules = 4,
159 .alpha2 = "99",
160 .reg_rules = {
161 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
162 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
163 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
164 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
165 }
166};
167
168static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
169 .n_reg_rules = 2,
170 .alpha2 = "99",
171 .reg_rules = {
172 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
173 REG_RULE(5725-10, 5850+10, 40, 0, 30,
174 NL80211_RRF_NO_IR),
175 }
176};
177
178static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
179 &hwsim_world_regdom_custom_01,
180 &hwsim_world_regdom_custom_02,
181};
182
183struct hwsim_vif_priv {
184 u32 magic;
185 u8 bssid[ETH_ALEN];
186 bool assoc;
187 bool bcn_en;
188 u16 aid;
189};
190
191#define HWSIM_VIF_MAGIC 0x69537748
192
193static inline void hwsim_check_magic(struct ieee80211_vif *vif)
194{
195 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
196 WARN(vp->magic != HWSIM_VIF_MAGIC,
197 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
198 vif, vp->magic, vif->addr, vif->type, vif->p2p);
199}
200
201static inline void hwsim_set_magic(struct ieee80211_vif *vif)
202{
203 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
204 vp->magic = HWSIM_VIF_MAGIC;
205}
206
207static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
208{
209 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
210 vp->magic = 0;
211}
212
213struct hwsim_sta_priv {
214 u32 magic;
215};
216
217#define HWSIM_STA_MAGIC 0x6d537749
218
219static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
220{
221 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
222 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
223}
224
225static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
226{
227 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
228 sp->magic = HWSIM_STA_MAGIC;
229}
230
231static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
232{
233 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
234 sp->magic = 0;
235}
236
237struct hwsim_chanctx_priv {
238 u32 magic;
239};
240
241#define HWSIM_CHANCTX_MAGIC 0x6d53774a
242
243static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
244{
245 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
246 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
247}
248
249static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
250{
251 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
252 cp->magic = HWSIM_CHANCTX_MAGIC;
253}
254
255static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
256{
257 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
258 cp->magic = 0;
259}
260
261static unsigned int hwsim_net_id;
262
263static DEFINE_IDA(hwsim_netgroup_ida);
264
265struct hwsim_net {
266 int netgroup;
267 u32 wmediumd;
268};
269
270static inline int hwsim_net_get_netgroup(struct net *net)
271{
272 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
273
274 return hwsim_net->netgroup;
275}
276
277static inline int hwsim_net_set_netgroup(struct net *net)
278{
279 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
280
281 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
282 0, 0, GFP_KERNEL);
283 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
284}
285
286static inline u32 hwsim_net_get_wmediumd(struct net *net)
287{
288 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
289
290 return hwsim_net->wmediumd;
291}
292
293static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
294{
295 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
296
297 hwsim_net->wmediumd = portid;
298}
299
300static struct class *hwsim_class;
301
302static struct net_device *hwsim_mon; /* global monitor netdev */
303
304#define CHAN2G(_freq) { \
305 .band = NL80211_BAND_2GHZ, \
306 .center_freq = (_freq), \
307 .hw_value = (_freq), \
308 .max_power = 20, \
309}
310
311#define CHAN5G(_freq) { \
312 .band = NL80211_BAND_5GHZ, \
313 .center_freq = (_freq), \
314 .hw_value = (_freq), \
315 .max_power = 20, \
316}
317
318static const struct ieee80211_channel hwsim_channels_2ghz[] = {
319 CHAN2G(2412), /* Channel 1 */
320 CHAN2G(2417), /* Channel 2 */
321 CHAN2G(2422), /* Channel 3 */
322 CHAN2G(2427), /* Channel 4 */
323 CHAN2G(2432), /* Channel 5 */
324 CHAN2G(2437), /* Channel 6 */
325 CHAN2G(2442), /* Channel 7 */
326 CHAN2G(2447), /* Channel 8 */
327 CHAN2G(2452), /* Channel 9 */
328 CHAN2G(2457), /* Channel 10 */
329 CHAN2G(2462), /* Channel 11 */
330 CHAN2G(2467), /* Channel 12 */
331 CHAN2G(2472), /* Channel 13 */
332 CHAN2G(2484), /* Channel 14 */
333};
334
335static const struct ieee80211_channel hwsim_channels_5ghz[] = {
336 CHAN5G(5180), /* Channel 36 */
337 CHAN5G(5200), /* Channel 40 */
338 CHAN5G(5220), /* Channel 44 */
339 CHAN5G(5240), /* Channel 48 */
340
341 CHAN5G(5260), /* Channel 52 */
342 CHAN5G(5280), /* Channel 56 */
343 CHAN5G(5300), /* Channel 60 */
344 CHAN5G(5320), /* Channel 64 */
345
346 CHAN5G(5500), /* Channel 100 */
347 CHAN5G(5520), /* Channel 104 */
348 CHAN5G(5540), /* Channel 108 */
349 CHAN5G(5560), /* Channel 112 */
350 CHAN5G(5580), /* Channel 116 */
351 CHAN5G(5600), /* Channel 120 */
352 CHAN5G(5620), /* Channel 124 */
353 CHAN5G(5640), /* Channel 128 */
354 CHAN5G(5660), /* Channel 132 */
355 CHAN5G(5680), /* Channel 136 */
356 CHAN5G(5700), /* Channel 140 */
357
358 CHAN5G(5745), /* Channel 149 */
359 CHAN5G(5765), /* Channel 153 */
360 CHAN5G(5785), /* Channel 157 */
361 CHAN5G(5805), /* Channel 161 */
362 CHAN5G(5825), /* Channel 165 */
363 CHAN5G(5845), /* Channel 169 */
364};
365
366static const struct ieee80211_rate hwsim_rates[] = {
367 { .bitrate = 10 },
368 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
369 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
370 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
371 { .bitrate = 60 },
372 { .bitrate = 90 },
373 { .bitrate = 120 },
374 { .bitrate = 180 },
375 { .bitrate = 240 },
376 { .bitrate = 360 },
377 { .bitrate = 480 },
378 { .bitrate = 540 }
379};
380
381static const u32 hwsim_ciphers[] = {
382 WLAN_CIPHER_SUITE_WEP40,
383 WLAN_CIPHER_SUITE_WEP104,
384 WLAN_CIPHER_SUITE_TKIP,
385 WLAN_CIPHER_SUITE_CCMP,
386 WLAN_CIPHER_SUITE_CCMP_256,
387 WLAN_CIPHER_SUITE_GCMP,
388 WLAN_CIPHER_SUITE_GCMP_256,
389 WLAN_CIPHER_SUITE_AES_CMAC,
390 WLAN_CIPHER_SUITE_BIP_CMAC_256,
391 WLAN_CIPHER_SUITE_BIP_GMAC_128,
392 WLAN_CIPHER_SUITE_BIP_GMAC_256,
393};
394
395#define OUI_QCA 0x001374
396#define QCA_NL80211_SUBCMD_TEST 1
397enum qca_nl80211_vendor_subcmds {
398 QCA_WLAN_VENDOR_ATTR_TEST = 8,
399 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
400};
401
402static const struct nla_policy
403hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
404 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
405};
406
407static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
408 struct wireless_dev *wdev,
409 const void *data, int data_len)
410{
411 struct sk_buff *skb;
412 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
413 int err;
414 u32 val;
415
416 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
417 data_len, hwsim_vendor_test_policy, NULL);
418 if (err)
419 return err;
420 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
421 return -EINVAL;
422 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
423 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
424
425 /* Send a vendor event as a test. Note that this would not normally be
426 * done within a command handler, but rather, based on some other
427 * trigger. For simplicity, this command is used to trigger the event
428 * here.
429 *
430 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
431 */
432 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
433 if (skb) {
434 /* skb_put() or nla_put() will fill up data within
435 * NL80211_ATTR_VENDOR_DATA.
436 */
437
438 /* Add vendor data */
439 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
440
441 /* Send the event - this will call nla_nest_end() */
442 cfg80211_vendor_event(skb, GFP_KERNEL);
443 }
444
445 /* Send a response to the command */
446 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
447 if (!skb)
448 return -ENOMEM;
449
450 /* skb_put() or nla_put() will fill up data within
451 * NL80211_ATTR_VENDOR_DATA
452 */
453 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
454
455 return cfg80211_vendor_cmd_reply(skb);
456}
457
458static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
459 {
460 .info = { .vendor_id = OUI_QCA,
461 .subcmd = QCA_NL80211_SUBCMD_TEST },
462 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
463 .doit = mac80211_hwsim_vendor_cmd_test,
464 .policy = hwsim_vendor_test_policy,
465 .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
466 }
467};
468
469/* Advertise support vendor specific events */
470static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
471 { .vendor_id = OUI_QCA, .subcmd = 1 },
472};
473
474static spinlock_t hwsim_radio_lock;
475static LIST_HEAD(hwsim_radios);
476static struct rhashtable hwsim_radios_rht;
477static int hwsim_radio_idx;
478static int hwsim_radios_generation = 1;
479
480static struct platform_driver mac80211_hwsim_driver = {
481 .driver = {
482 .name = "mac80211_hwsim",
483 },
484};
485
486struct mac80211_hwsim_data {
487 struct list_head list;
488 struct rhash_head rht;
489 struct ieee80211_hw *hw;
490 struct device *dev;
491 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
492 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
493 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
494 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
495 struct ieee80211_iface_combination if_combination;
496 struct ieee80211_iface_limit if_limits[3];
497 int n_if_limits;
498
499 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
500
501 struct mac_address addresses[2];
502 struct ieee80211_chanctx_conf *chanctx;
503 int channels, idx;
504 bool use_chanctx;
505 bool destroy_on_close;
506 u32 portid;
507 char alpha2[2];
508 const struct ieee80211_regdomain *regd;
509
510 struct ieee80211_channel *tmp_chan;
511 struct ieee80211_channel *roc_chan;
512 u32 roc_duration;
513 struct delayed_work roc_start;
514 struct delayed_work roc_done;
515 struct delayed_work hw_scan;
516 struct cfg80211_scan_request *hw_scan_request;
517 struct ieee80211_vif *hw_scan_vif;
518 int scan_chan_idx;
519 u8 scan_addr[ETH_ALEN];
520 struct {
521 struct ieee80211_channel *channel;
522 unsigned long next_start, start, end;
523 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
524 ARRAY_SIZE(hwsim_channels_5ghz)];
525
526 struct ieee80211_channel *channel;
527 u64 beacon_int /* beacon interval in us */;
528 unsigned int rx_filter;
529 bool started, idle, scanning;
530 struct mutex mutex;
531 struct hrtimer beacon_timer;
532 enum ps_mode {
533 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
534 } ps;
535 bool ps_poll_pending;
536 struct dentry *debugfs;
537
538 atomic_t pending_cookie;
539 struct sk_buff_head pending; /* packets pending */
540 /*
541 * Only radios in the same group can communicate together (the
542 * channel has to match too). Each bit represents a group. A
543 * radio can be in more than one group.
544 */
545 u64 group;
546
547 /* group shared by radios created in the same netns */
548 int netgroup;
549 /* wmediumd portid responsible for netgroup of this radio */
550 u32 wmediumd;
551
552 /* difference between this hw's clock and the real clock, in usecs */
553 s64 tsf_offset;
554 s64 bcn_delta;
555 /* absolute beacon transmission time. Used to cover up "tx" delay. */
556 u64 abs_bcn_ts;
557
558 /* Stats */
559 u64 tx_pkts;
560 u64 rx_pkts;
561 u64 tx_bytes;
562 u64 rx_bytes;
563 u64 tx_dropped;
564 u64 tx_failed;
565};
566
567static const struct rhashtable_params hwsim_rht_params = {
568 .nelem_hint = 2,
569 .automatic_shrinking = true,
570 .key_len = ETH_ALEN,
571 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
572 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
573};
574
575struct hwsim_radiotap_hdr {
576 struct ieee80211_radiotap_header hdr;
577 __le64 rt_tsft;
578 u8 rt_flags;
579 u8 rt_rate;
580 __le16 rt_channel;
581 __le16 rt_chbitmask;
582} __packed;
583
584struct hwsim_radiotap_ack_hdr {
585 struct ieee80211_radiotap_header hdr;
586 u8 rt_flags;
587 u8 pad;
588 __le16 rt_channel;
589 __le16 rt_chbitmask;
590} __packed;
591
592/* MAC80211_HWSIM netlink family */
593static struct genl_family hwsim_genl_family;
594
595enum hwsim_multicast_groups {
596 HWSIM_MCGRP_CONFIG,
597};
598
599static const struct genl_multicast_group hwsim_mcgrps[] = {
600 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
601};
602
603/* MAC80211_HWSIM netlink policy */
604
605static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
606 [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
607 [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
608 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
609 .len = IEEE80211_MAX_DATA_LEN },
610 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
611 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
612 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
613 [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
614 .len = IEEE80211_TX_MAX_RATES *
615 sizeof(struct hwsim_tx_rate)},
616 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
617 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
618 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
619 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
620 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
621 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
622 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
623 [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
624 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
625 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
626 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
627 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
628 [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
629 [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
630 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
631 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
632};
633
634#if IS_REACHABLE(CONFIG_VIRTIO)
635
636/* MAC80211_HWSIM virtio queues */
637static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
638static bool hwsim_virtio_enabled;
639static spinlock_t hwsim_virtio_lock;
640
641static void hwsim_virtio_rx_work(struct work_struct *work);
642static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
643
644static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
645 struct sk_buff *skb)
646{
647 struct scatterlist sg[1];
648 unsigned long flags;
649 int err;
650
651 spin_lock_irqsave(&hwsim_virtio_lock, flags);
652 if (!hwsim_virtio_enabled) {
653 err = -ENODEV;
654 goto out_free;
655 }
656
657 sg_init_one(sg, skb->head, skb_end_offset(skb));
658 err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
659 GFP_ATOMIC);
660 if (err)
661 goto out_free;
662 virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
663 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
664 return 0;
665
666out_free:
667 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
668 nlmsg_free(skb);
669 return err;
670}
671#else
672/* cause a linker error if this ends up being needed */
673extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
674 struct sk_buff *skb);
675#define hwsim_virtio_enabled false
676#endif
677
678static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
679 struct sk_buff *skb,
680 struct ieee80211_channel *chan);
681
682/* sysfs attributes */
683static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
684{
685 struct mac80211_hwsim_data *data = dat;
686 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
687 struct sk_buff *skb;
688 struct ieee80211_pspoll *pspoll;
689
690 if (!vp->assoc)
691 return;
692
693 wiphy_dbg(data->hw->wiphy,
694 "%s: send PS-Poll to %pM for aid %d\n",
695 __func__, vp->bssid, vp->aid);
696
697 skb = dev_alloc_skb(sizeof(*pspoll));
698 if (!skb)
699 return;
700 pspoll = skb_put(skb, sizeof(*pspoll));
701 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
702 IEEE80211_STYPE_PSPOLL |
703 IEEE80211_FCTL_PM);
704 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
705 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
706 memcpy(pspoll->ta, mac, ETH_ALEN);
707
708 rcu_read_lock();
709 mac80211_hwsim_tx_frame(data->hw, skb,
710 rcu_dereference(vif->chanctx_conf)->def.chan);
711 rcu_read_unlock();
712}
713
714static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
715 struct ieee80211_vif *vif, int ps)
716{
717 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
718 struct sk_buff *skb;
719 struct ieee80211_hdr *hdr;
720 struct ieee80211_tx_info *cb;
721
722 if (!vp->assoc)
723 return;
724
725 wiphy_dbg(data->hw->wiphy,
726 "%s: send data::nullfunc to %pM ps=%d\n",
727 __func__, vp->bssid, ps);
728
729 skb = dev_alloc_skb(sizeof(*hdr));
730 if (!skb)
731 return;
732 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
733 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
734 IEEE80211_STYPE_NULLFUNC |
735 IEEE80211_FCTL_TODS |
736 (ps ? IEEE80211_FCTL_PM : 0));
737 hdr->duration_id = cpu_to_le16(0);
738 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
739 memcpy(hdr->addr2, mac, ETH_ALEN);
740 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
741
742 cb = IEEE80211_SKB_CB(skb);
743 cb->control.rates[0].count = 1;
744 cb->control.rates[1].idx = -1;
745
746 rcu_read_lock();
747 mac80211_hwsim_tx_frame(data->hw, skb,
748 rcu_dereference(vif->chanctx_conf)->def.chan);
749 rcu_read_unlock();
750}
751
752
753static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
754 struct ieee80211_vif *vif)
755{
756 struct mac80211_hwsim_data *data = dat;
757 hwsim_send_nullfunc(data, mac, vif, 1);
758}
759
760static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
761 struct ieee80211_vif *vif)
762{
763 struct mac80211_hwsim_data *data = dat;
764 hwsim_send_nullfunc(data, mac, vif, 0);
765}
766
767static int hwsim_fops_ps_read(void *dat, u64 *val)
768{
769 struct mac80211_hwsim_data *data = dat;
770 *val = data->ps;
771 return 0;
772}
773
774static int hwsim_fops_ps_write(void *dat, u64 val)
775{
776 struct mac80211_hwsim_data *data = dat;
777 enum ps_mode old_ps;
778
779 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
780 val != PS_MANUAL_POLL)
781 return -EINVAL;
782
783 if (val == PS_MANUAL_POLL) {
784 if (data->ps != PS_ENABLED)
785 return -EINVAL;
786 local_bh_disable();
787 ieee80211_iterate_active_interfaces_atomic(
788 data->hw, IEEE80211_IFACE_ITER_NORMAL,
789 hwsim_send_ps_poll, data);
790 local_bh_enable();
791 return 0;
792 }
793 old_ps = data->ps;
794 data->ps = val;
795
796 local_bh_disable();
797 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
798 ieee80211_iterate_active_interfaces_atomic(
799 data->hw, IEEE80211_IFACE_ITER_NORMAL,
800 hwsim_send_nullfunc_ps, data);
801 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
802 ieee80211_iterate_active_interfaces_atomic(
803 data->hw, IEEE80211_IFACE_ITER_NORMAL,
804 hwsim_send_nullfunc_no_ps, data);
805 }
806 local_bh_enable();
807
808 return 0;
809}
810
811DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
812 "%llu\n");
813
814static int hwsim_write_simulate_radar(void *dat, u64 val)
815{
816 struct mac80211_hwsim_data *data = dat;
817
818 ieee80211_radar_detected(data->hw);
819
820 return 0;
821}
822
823DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
824 hwsim_write_simulate_radar, "%llu\n");
825
826static int hwsim_fops_group_read(void *dat, u64 *val)
827{
828 struct mac80211_hwsim_data *data = dat;
829 *val = data->group;
830 return 0;
831}
832
833static int hwsim_fops_group_write(void *dat, u64 val)
834{
835 struct mac80211_hwsim_data *data = dat;
836 data->group = val;
837 return 0;
838}
839
840DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
841 hwsim_fops_group_read, hwsim_fops_group_write,
842 "%llx\n");
843
844static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
845 struct net_device *dev)
846{
847 /* TODO: allow packet injection */
848 dev_kfree_skb(skb);
849 return NETDEV_TX_OK;
850}
851
852static inline u64 mac80211_hwsim_get_tsf_raw(void)
853{
854 return ktime_to_us(ktime_get_real());
855}
856
857static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
858{
859 u64 now = mac80211_hwsim_get_tsf_raw();
860 return cpu_to_le64(now + data->tsf_offset);
861}
862
863static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
864 struct ieee80211_vif *vif)
865{
866 struct mac80211_hwsim_data *data = hw->priv;
867 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
868}
869
870static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
871 struct ieee80211_vif *vif, u64 tsf)
872{
873 struct mac80211_hwsim_data *data = hw->priv;
874 u64 now = mac80211_hwsim_get_tsf(hw, vif);
875 u32 bcn_int = data->beacon_int;
876 u64 delta = abs(tsf - now);
877
878 /* adjust after beaconing with new timestamp at old TBTT */
879 if (tsf > now) {
880 data->tsf_offset += delta;
881 data->bcn_delta = do_div(delta, bcn_int);
882 } else {
883 data->tsf_offset -= delta;
884 data->bcn_delta = -(s64)do_div(delta, bcn_int);
885 }
886}
887
888static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
889 struct sk_buff *tx_skb,
890 struct ieee80211_channel *chan)
891{
892 struct mac80211_hwsim_data *data = hw->priv;
893 struct sk_buff *skb;
894 struct hwsim_radiotap_hdr *hdr;
895 u16 flags;
896 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
897 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
898
899 if (WARN_ON(!txrate))
900 return;
901
902 if (!netif_running(hwsim_mon))
903 return;
904
905 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
906 if (skb == NULL)
907 return;
908
909 hdr = skb_push(skb, sizeof(*hdr));
910 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
911 hdr->hdr.it_pad = 0;
912 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
913 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
914 (1 << IEEE80211_RADIOTAP_RATE) |
915 (1 << IEEE80211_RADIOTAP_TSFT) |
916 (1 << IEEE80211_RADIOTAP_CHANNEL));
917 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
918 hdr->rt_flags = 0;
919 hdr->rt_rate = txrate->bitrate / 5;
920 hdr->rt_channel = cpu_to_le16(chan->center_freq);
921 flags = IEEE80211_CHAN_2GHZ;
922 if (txrate->flags & IEEE80211_RATE_ERP_G)
923 flags |= IEEE80211_CHAN_OFDM;
924 else
925 flags |= IEEE80211_CHAN_CCK;
926 hdr->rt_chbitmask = cpu_to_le16(flags);
927
928 skb->dev = hwsim_mon;
929 skb_reset_mac_header(skb);
930 skb->ip_summed = CHECKSUM_UNNECESSARY;
931 skb->pkt_type = PACKET_OTHERHOST;
932 skb->protocol = htons(ETH_P_802_2);
933 memset(skb->cb, 0, sizeof(skb->cb));
934 netif_rx(skb);
935}
936
937
938static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
939 const u8 *addr)
940{
941 struct sk_buff *skb;
942 struct hwsim_radiotap_ack_hdr *hdr;
943 u16 flags;
944 struct ieee80211_hdr *hdr11;
945
946 if (!netif_running(hwsim_mon))
947 return;
948
949 skb = dev_alloc_skb(100);
950 if (skb == NULL)
951 return;
952
953 hdr = skb_put(skb, sizeof(*hdr));
954 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
955 hdr->hdr.it_pad = 0;
956 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
957 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
958 (1 << IEEE80211_RADIOTAP_CHANNEL));
959 hdr->rt_flags = 0;
960 hdr->pad = 0;
961 hdr->rt_channel = cpu_to_le16(chan->center_freq);
962 flags = IEEE80211_CHAN_2GHZ;
963 hdr->rt_chbitmask = cpu_to_le16(flags);
964
965 hdr11 = skb_put(skb, 10);
966 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
967 IEEE80211_STYPE_ACK);
968 hdr11->duration_id = cpu_to_le16(0);
969 memcpy(hdr11->addr1, addr, ETH_ALEN);
970
971 skb->dev = hwsim_mon;
972 skb_reset_mac_header(skb);
973 skb->ip_summed = CHECKSUM_UNNECESSARY;
974 skb->pkt_type = PACKET_OTHERHOST;
975 skb->protocol = htons(ETH_P_802_2);
976 memset(skb->cb, 0, sizeof(skb->cb));
977 netif_rx(skb);
978}
979
980struct mac80211_hwsim_addr_match_data {
981 u8 addr[ETH_ALEN];
982 bool ret;
983};
984
985static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
986 struct ieee80211_vif *vif)
987{
988 struct mac80211_hwsim_addr_match_data *md = data;
989
990 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
991 md->ret = true;
992}
993
994static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
995 const u8 *addr)
996{
997 struct mac80211_hwsim_addr_match_data md = {
998 .ret = false,
999 };
1000
1001 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1002 return true;
1003
1004 memcpy(md.addr, addr, ETH_ALEN);
1005
1006 ieee80211_iterate_active_interfaces_atomic(data->hw,
1007 IEEE80211_IFACE_ITER_NORMAL,
1008 mac80211_hwsim_addr_iter,
1009 &md);
1010
1011 return md.ret;
1012}
1013
1014static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1015 struct sk_buff *skb)
1016{
1017 switch (data->ps) {
1018 case PS_DISABLED:
1019 return true;
1020 case PS_ENABLED:
1021 return false;
1022 case PS_AUTO_POLL:
1023 /* TODO: accept (some) Beacons by default and other frames only
1024 * if pending PS-Poll has been sent */
1025 return true;
1026 case PS_MANUAL_POLL:
1027 /* Allow unicast frames to own address if there is a pending
1028 * PS-Poll */
1029 if (data->ps_poll_pending &&
1030 mac80211_hwsim_addr_match(data, skb->data + 4)) {
1031 data->ps_poll_pending = false;
1032 return true;
1033 }
1034 return false;
1035 }
1036
1037 return true;
1038}
1039
1040static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1041 struct sk_buff *skb, int portid)
1042{
1043 struct net *net;
1044 bool found = false;
1045 int res = -ENOENT;
1046
1047 rcu_read_lock();
1048 for_each_net_rcu(net) {
1049 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1050 res = genlmsg_unicast(net, skb, portid);
1051 found = true;
1052 break;
1053 }
1054 }
1055 rcu_read_unlock();
1056
1057 if (!found)
1058 nlmsg_free(skb);
1059
1060 return res;
1061}
1062
1063static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1064{
1065 u16 result = 0;
1066
1067 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1068 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1069 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1070 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1071 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1072 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1073 if (rate->flags & IEEE80211_TX_RC_MCS)
1074 result |= MAC80211_HWSIM_TX_RC_MCS;
1075 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1076 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1077 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1078 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1079 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1080 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1081 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1082 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1083 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1084 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1085 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1086 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1087 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1088 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1089
1090 return result;
1091}
1092
1093static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1094 struct sk_buff *my_skb,
1095 int dst_portid,
1096 struct ieee80211_channel *channel)
1097{
1098 struct sk_buff *skb;
1099 struct mac80211_hwsim_data *data = hw->priv;
1100 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1101 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1102 void *msg_head;
1103 unsigned int hwsim_flags = 0;
1104 int i;
1105 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1106 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1107 uintptr_t cookie;
1108
1109 if (data->ps != PS_DISABLED)
1110 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1111 /* If the queue contains MAX_QUEUE skb's drop some */
1112 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1113 /* Droping until WARN_QUEUE level */
1114 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1115 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1116 data->tx_dropped++;
1117 }
1118 }
1119
1120 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1121 if (skb == NULL)
1122 goto nla_put_failure;
1123
1124 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1125 HWSIM_CMD_FRAME);
1126 if (msg_head == NULL) {
1127 pr_debug("mac80211_hwsim: problem with msg_head\n");
1128 goto nla_put_failure;
1129 }
1130
1131 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1132 ETH_ALEN, data->addresses[1].addr))
1133 goto nla_put_failure;
1134
1135 /* We get the skb->data */
1136 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1137 goto nla_put_failure;
1138
1139 /* We get the flags for this transmission, and we translate them to
1140 wmediumd flags */
1141
1142 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1143 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1144
1145 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1146 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1147
1148 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1149 goto nla_put_failure;
1150
1151 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1152 goto nla_put_failure;
1153
1154 /* We get the tx control (rate and retries) info*/
1155
1156 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1157 tx_attempts[i].idx = info->status.rates[i].idx;
1158 tx_attempts_flags[i].idx = info->status.rates[i].idx;
1159 tx_attempts[i].count = info->status.rates[i].count;
1160 tx_attempts_flags[i].flags =
1161 trans_tx_rate_flags_ieee2hwsim(
1162 &info->status.rates[i]);
1163 }
1164
1165 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1166 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1167 tx_attempts))
1168 goto nla_put_failure;
1169
1170 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1171 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1172 tx_attempts_flags))
1173 goto nla_put_failure;
1174
1175 /* We create a cookie to identify this skb */
1176 cookie = atomic_inc_return(&data->pending_cookie);
1177 info->rate_driver_data[0] = (void *)cookie;
1178 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1179 goto nla_put_failure;
1180
1181 genlmsg_end(skb, msg_head);
1182
1183 if (hwsim_virtio_enabled) {
1184 if (hwsim_tx_virtio(data, skb))
1185 goto err_free_txskb;
1186 } else {
1187 if (hwsim_unicast_netgroup(data, skb, dst_portid))
1188 goto err_free_txskb;
1189 }
1190
1191 /* Enqueue the packet */
1192 skb_queue_tail(&data->pending, my_skb);
1193 data->tx_pkts++;
1194 data->tx_bytes += my_skb->len;
1195 return;
1196
1197nla_put_failure:
1198 nlmsg_free(skb);
1199err_free_txskb:
1200 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1201 ieee80211_free_txskb(hw, my_skb);
1202 data->tx_failed++;
1203}
1204
1205static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1206 struct ieee80211_channel *c2)
1207{
1208 if (!c1 || !c2)
1209 return false;
1210
1211 return c1->center_freq == c2->center_freq;
1212}
1213
1214struct tx_iter_data {
1215 struct ieee80211_channel *channel;
1216 bool receive;
1217};
1218
1219static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1220 struct ieee80211_vif *vif)
1221{
1222 struct tx_iter_data *data = _data;
1223
1224 if (!vif->chanctx_conf)
1225 return;
1226
1227 if (!hwsim_chans_compat(data->channel,
1228 rcu_dereference(vif->chanctx_conf)->def.chan))
1229 return;
1230
1231 data->receive = true;
1232}
1233
1234static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1235{
1236 /*
1237 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1238 * e.g. like this:
1239 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1240 * (but you should use a valid OUI, not that)
1241 *
1242 * If anyone wants to 'donate' a radiotap OUI/subns code
1243 * please send a patch removing this #ifdef and changing
1244 * the values accordingly.
1245 */
1246#ifdef HWSIM_RADIOTAP_OUI
1247 struct ieee80211_vendor_radiotap *rtap;
1248
1249 /*
1250 * Note that this code requires the headroom in the SKB
1251 * that was allocated earlier.
1252 */
1253 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1254 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1255 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1256 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1257 rtap->subns = 127;
1258
1259 /*
1260 * Radiotap vendor namespaces can (and should) also be
1261 * split into fields by using the standard radiotap
1262 * presence bitmap mechanism. Use just BIT(0) here for
1263 * the presence bitmap.
1264 */
1265 rtap->present = BIT(0);
1266 /* We have 8 bytes of (dummy) data */
1267 rtap->len = 8;
1268 /* For testing, also require it to be aligned */
1269 rtap->align = 8;
1270 /* And also test that padding works, 4 bytes */
1271 rtap->pad = 4;
1272 /* push the data */
1273 memcpy(rtap->data, "ABCDEFGH", 8);
1274 /* make sure to clear padding, mac80211 doesn't */
1275 memset(rtap->data + 8, 0, 4);
1276
1277 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1278#endif
1279}
1280
1281static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1282 struct sk_buff *skb,
1283 struct ieee80211_channel *chan)
1284{
1285 struct mac80211_hwsim_data *data = hw->priv, *data2;
1286 bool ack = false;
1287 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1288 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1289 struct ieee80211_rx_status rx_status;
1290 u64 now;
1291
1292 memset(&rx_status, 0, sizeof(rx_status));
1293 rx_status.flag |= RX_FLAG_MACTIME_START;
1294 rx_status.freq = chan->center_freq;
1295 rx_status.band = chan->band;
1296 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1297 rx_status.rate_idx =
1298 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1299 rx_status.nss =
1300 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1301 rx_status.encoding = RX_ENC_VHT;
1302 } else {
1303 rx_status.rate_idx = info->control.rates[0].idx;
1304 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1305 rx_status.encoding = RX_ENC_HT;
1306 }
1307 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1308 rx_status.bw = RATE_INFO_BW_40;
1309 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1310 rx_status.bw = RATE_INFO_BW_80;
1311 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1312 rx_status.bw = RATE_INFO_BW_160;
1313 else
1314 rx_status.bw = RATE_INFO_BW_20;
1315 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1316 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1317 /* TODO: simulate real signal strength (and optional packet loss) */
1318 rx_status.signal = -50;
1319 if (info->control.vif)
1320 rx_status.signal += info->control.vif->bss_conf.txpower;
1321
1322 if (data->ps != PS_DISABLED)
1323 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1324
1325 /* release the skb's source info */
1326 skb_orphan(skb);
1327 skb_dst_drop(skb);
1328 skb->mark = 0;
1329 skb_ext_reset(skb);
1330 nf_reset_ct(skb);
1331
1332 /*
1333 * Get absolute mactime here so all HWs RX at the "same time", and
1334 * absolute TX time for beacon mactime so the timestamp matches.
1335 * Giving beacons a different mactime than non-beacons looks messy, but
1336 * it helps the Toffset be exact and a ~10us mactime discrepancy
1337 * probably doesn't really matter.
1338 */
1339 if (ieee80211_is_beacon(hdr->frame_control) ||
1340 ieee80211_is_probe_resp(hdr->frame_control)) {
1341 rx_status.boottime_ns = ktime_get_boottime_ns();
1342 now = data->abs_bcn_ts;
1343 } else {
1344 now = mac80211_hwsim_get_tsf_raw();
1345 }
1346
1347 /* Copy skb to all enabled radios that are on the current frequency */
1348 spin_lock(&hwsim_radio_lock);
1349 list_for_each_entry(data2, &hwsim_radios, list) {
1350 struct sk_buff *nskb;
1351 struct tx_iter_data tx_iter_data = {
1352 .receive = false,
1353 .channel = chan,
1354 };
1355
1356 if (data == data2)
1357 continue;
1358
1359 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1360 !hwsim_ps_rx_ok(data2, skb))
1361 continue;
1362
1363 if (!(data->group & data2->group))
1364 continue;
1365
1366 if (data->netgroup != data2->netgroup)
1367 continue;
1368
1369 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1370 !hwsim_chans_compat(chan, data2->channel)) {
1371 ieee80211_iterate_active_interfaces_atomic(
1372 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1373 mac80211_hwsim_tx_iter, &tx_iter_data);
1374 if (!tx_iter_data.receive)
1375 continue;
1376 }
1377
1378 /*
1379 * reserve some space for our vendor and the normal
1380 * radiotap header, since we're copying anyway
1381 */
1382 if (skb->len < PAGE_SIZE && paged_rx) {
1383 struct page *page = alloc_page(GFP_ATOMIC);
1384
1385 if (!page)
1386 continue;
1387
1388 nskb = dev_alloc_skb(128);
1389 if (!nskb) {
1390 __free_page(page);
1391 continue;
1392 }
1393
1394 memcpy(page_address(page), skb->data, skb->len);
1395 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1396 } else {
1397 nskb = skb_copy(skb, GFP_ATOMIC);
1398 if (!nskb)
1399 continue;
1400 }
1401
1402 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1403 ack = true;
1404
1405 rx_status.mactime = now + data2->tsf_offset;
1406
1407 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1408
1409 mac80211_hwsim_add_vendor_rtap(nskb);
1410
1411 data2->rx_pkts++;
1412 data2->rx_bytes += nskb->len;
1413 ieee80211_rx_irqsafe(data2->hw, nskb);
1414 }
1415 spin_unlock(&hwsim_radio_lock);
1416
1417 return ack;
1418}
1419
1420static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1421 struct ieee80211_tx_control *control,
1422 struct sk_buff *skb)
1423{
1424 struct mac80211_hwsim_data *data = hw->priv;
1425 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1426 struct ieee80211_hdr *hdr = (void *)skb->data;
1427 struct ieee80211_chanctx_conf *chanctx_conf;
1428 struct ieee80211_channel *channel;
1429 bool ack;
1430 u32 _portid;
1431
1432 if (WARN_ON(skb->len < 10)) {
1433 /* Should not happen; just a sanity check for addr1 use */
1434 ieee80211_free_txskb(hw, skb);
1435 return;
1436 }
1437
1438 if (!data->use_chanctx) {
1439 channel = data->channel;
1440 } else if (txi->hw_queue == 4) {
1441 channel = data->tmp_chan;
1442 } else {
1443 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1444 if (chanctx_conf)
1445 channel = chanctx_conf->def.chan;
1446 else
1447 channel = NULL;
1448 }
1449
1450 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1451 ieee80211_free_txskb(hw, skb);
1452 return;
1453 }
1454
1455 if (data->idle && !data->tmp_chan) {
1456 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1457 ieee80211_free_txskb(hw, skb);
1458 return;
1459 }
1460
1461 if (txi->control.vif)
1462 hwsim_check_magic(txi->control.vif);
1463 if (control->sta)
1464 hwsim_check_sta_magic(control->sta);
1465
1466 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1467 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1468 txi->control.rates,
1469 ARRAY_SIZE(txi->control.rates));
1470
1471 if (skb->len >= 24 + 8 &&
1472 ieee80211_is_probe_resp(hdr->frame_control)) {
1473 /* fake header transmission time */
1474 struct ieee80211_mgmt *mgmt;
1475 struct ieee80211_rate *txrate;
1476 u64 ts;
1477
1478 mgmt = (struct ieee80211_mgmt *)skb->data;
1479 txrate = ieee80211_get_tx_rate(hw, txi);
1480 ts = mac80211_hwsim_get_tsf_raw();
1481 mgmt->u.probe_resp.timestamp =
1482 cpu_to_le64(ts + data->tsf_offset +
1483 24 * 8 * 10 / txrate->bitrate);
1484 }
1485
1486 mac80211_hwsim_monitor_rx(hw, skb, channel);
1487
1488 /* wmediumd mode check */
1489 _portid = READ_ONCE(data->wmediumd);
1490
1491 if (_portid || hwsim_virtio_enabled)
1492 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1493
1494 /* NO wmediumd detected, perfect medium simulation */
1495 data->tx_pkts++;
1496 data->tx_bytes += skb->len;
1497 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1498
1499 if (ack && skb->len >= 16)
1500 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1501
1502 ieee80211_tx_info_clear_status(txi);
1503
1504 /* frame was transmitted at most favorable rate at first attempt */
1505 txi->control.rates[0].count = 1;
1506 txi->control.rates[1].idx = -1;
1507
1508 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1509 txi->flags |= IEEE80211_TX_STAT_ACK;
1510 ieee80211_tx_status_irqsafe(hw, skb);
1511}
1512
1513
1514static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1515{
1516 struct mac80211_hwsim_data *data = hw->priv;
1517 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1518 data->started = true;
1519 return 0;
1520}
1521
1522
1523static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1524{
1525 struct mac80211_hwsim_data *data = hw->priv;
1526
1527 data->started = false;
1528 hrtimer_cancel(&data->beacon_timer);
1529
1530 while (!skb_queue_empty(&data->pending))
1531 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1532
1533 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1534}
1535
1536
1537static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1538 struct ieee80211_vif *vif)
1539{
1540 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1541 __func__, ieee80211_vif_type_p2p(vif),
1542 vif->addr);
1543 hwsim_set_magic(vif);
1544
1545 vif->cab_queue = 0;
1546 vif->hw_queue[IEEE80211_AC_VO] = 0;
1547 vif->hw_queue[IEEE80211_AC_VI] = 1;
1548 vif->hw_queue[IEEE80211_AC_BE] = 2;
1549 vif->hw_queue[IEEE80211_AC_BK] = 3;
1550
1551 return 0;
1552}
1553
1554
1555static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1556 struct ieee80211_vif *vif,
1557 enum nl80211_iftype newtype,
1558 bool newp2p)
1559{
1560 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1561 wiphy_dbg(hw->wiphy,
1562 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1563 __func__, ieee80211_vif_type_p2p(vif),
1564 newtype, vif->addr);
1565 hwsim_check_magic(vif);
1566
1567 /*
1568 * interface may change from non-AP to AP in
1569 * which case this needs to be set up again
1570 */
1571 vif->cab_queue = 0;
1572
1573 return 0;
1574}
1575
1576static void mac80211_hwsim_remove_interface(
1577 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1578{
1579 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1580 __func__, ieee80211_vif_type_p2p(vif),
1581 vif->addr);
1582 hwsim_check_magic(vif);
1583 hwsim_clear_magic(vif);
1584}
1585
1586static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1587 struct sk_buff *skb,
1588 struct ieee80211_channel *chan)
1589{
1590 struct mac80211_hwsim_data *data = hw->priv;
1591 u32 _pid = READ_ONCE(data->wmediumd);
1592
1593 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1594 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1595 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1596 txi->control.rates,
1597 ARRAY_SIZE(txi->control.rates));
1598 }
1599
1600 mac80211_hwsim_monitor_rx(hw, skb, chan);
1601
1602 if (_pid || hwsim_virtio_enabled)
1603 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1604
1605 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1606 dev_kfree_skb(skb);
1607}
1608
1609static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1610 struct ieee80211_vif *vif)
1611{
1612 struct mac80211_hwsim_data *data = arg;
1613 struct ieee80211_hw *hw = data->hw;
1614 struct ieee80211_tx_info *info;
1615 struct ieee80211_rate *txrate;
1616 struct ieee80211_mgmt *mgmt;
1617 struct sk_buff *skb;
1618
1619 hwsim_check_magic(vif);
1620
1621 if (vif->type != NL80211_IFTYPE_AP &&
1622 vif->type != NL80211_IFTYPE_MESH_POINT &&
1623 vif->type != NL80211_IFTYPE_ADHOC)
1624 return;
1625
1626 skb = ieee80211_beacon_get(hw, vif);
1627 if (skb == NULL)
1628 return;
1629 info = IEEE80211_SKB_CB(skb);
1630 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1631 ieee80211_get_tx_rates(vif, NULL, skb,
1632 info->control.rates,
1633 ARRAY_SIZE(info->control.rates));
1634
1635 txrate = ieee80211_get_tx_rate(hw, info);
1636
1637 mgmt = (struct ieee80211_mgmt *) skb->data;
1638 /* fake header transmission time */
1639 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1640 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1641 data->tsf_offset +
1642 24 * 8 * 10 / txrate->bitrate);
1643
1644 mac80211_hwsim_tx_frame(hw, skb,
1645 rcu_dereference(vif->chanctx_conf)->def.chan);
1646
1647 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1648 ieee80211_csa_finish(vif);
1649}
1650
1651static enum hrtimer_restart
1652mac80211_hwsim_beacon(struct hrtimer *timer)
1653{
1654 struct mac80211_hwsim_data *data =
1655 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1656 struct ieee80211_hw *hw = data->hw;
1657 u64 bcn_int = data->beacon_int;
1658
1659 if (!data->started)
1660 return HRTIMER_NORESTART;
1661
1662 ieee80211_iterate_active_interfaces_atomic(
1663 hw, IEEE80211_IFACE_ITER_NORMAL,
1664 mac80211_hwsim_beacon_tx, data);
1665
1666 /* beacon at new TBTT + beacon interval */
1667 if (data->bcn_delta) {
1668 bcn_int -= data->bcn_delta;
1669 data->bcn_delta = 0;
1670 }
1671 hrtimer_forward_now(&data->beacon_timer,
1672 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1673 return HRTIMER_RESTART;
1674}
1675
1676static const char * const hwsim_chanwidths[] = {
1677 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1678 [NL80211_CHAN_WIDTH_20] = "ht20",
1679 [NL80211_CHAN_WIDTH_40] = "ht40",
1680 [NL80211_CHAN_WIDTH_80] = "vht80",
1681 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1682 [NL80211_CHAN_WIDTH_160] = "vht160",
1683};
1684
1685static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1686{
1687 struct mac80211_hwsim_data *data = hw->priv;
1688 struct ieee80211_conf *conf = &hw->conf;
1689 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1690 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1691 [IEEE80211_SMPS_OFF] = "off",
1692 [IEEE80211_SMPS_STATIC] = "static",
1693 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1694 };
1695 int idx;
1696
1697 if (conf->chandef.chan)
1698 wiphy_dbg(hw->wiphy,
1699 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1700 __func__,
1701 conf->chandef.chan->center_freq,
1702 conf->chandef.center_freq1,
1703 conf->chandef.center_freq2,
1704 hwsim_chanwidths[conf->chandef.width],
1705 !!(conf->flags & IEEE80211_CONF_IDLE),
1706 !!(conf->flags & IEEE80211_CONF_PS),
1707 smps_modes[conf->smps_mode]);
1708 else
1709 wiphy_dbg(hw->wiphy,
1710 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1711 __func__,
1712 !!(conf->flags & IEEE80211_CONF_IDLE),
1713 !!(conf->flags & IEEE80211_CONF_PS),
1714 smps_modes[conf->smps_mode]);
1715
1716 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1717
1718 WARN_ON(conf->chandef.chan && data->use_chanctx);
1719
1720 mutex_lock(&data->mutex);
1721 if (data->scanning && conf->chandef.chan) {
1722 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1723 if (data->survey_data[idx].channel == data->channel) {
1724 data->survey_data[idx].start =
1725 data->survey_data[idx].next_start;
1726 data->survey_data[idx].end = jiffies;
1727 break;
1728 }
1729 }
1730
1731 data->channel = conf->chandef.chan;
1732
1733 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1734 if (data->survey_data[idx].channel &&
1735 data->survey_data[idx].channel != data->channel)
1736 continue;
1737 data->survey_data[idx].channel = data->channel;
1738 data->survey_data[idx].next_start = jiffies;
1739 break;
1740 }
1741 } else {
1742 data->channel = conf->chandef.chan;
1743 }
1744 mutex_unlock(&data->mutex);
1745
1746 if (!data->started || !data->beacon_int)
1747 hrtimer_cancel(&data->beacon_timer);
1748 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1749 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1750 u32 bcn_int = data->beacon_int;
1751 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1752
1753 hrtimer_start(&data->beacon_timer,
1754 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1755 HRTIMER_MODE_REL_SOFT);
1756 }
1757
1758 return 0;
1759}
1760
1761
1762static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1763 unsigned int changed_flags,
1764 unsigned int *total_flags,u64 multicast)
1765{
1766 struct mac80211_hwsim_data *data = hw->priv;
1767
1768 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1769
1770 data->rx_filter = 0;
1771 if (*total_flags & FIF_ALLMULTI)
1772 data->rx_filter |= FIF_ALLMULTI;
1773
1774 *total_flags = data->rx_filter;
1775}
1776
1777static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1778 struct ieee80211_vif *vif)
1779{
1780 unsigned int *count = data;
1781 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1782
1783 if (vp->bcn_en)
1784 (*count)++;
1785}
1786
1787static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1788 struct ieee80211_vif *vif,
1789 struct ieee80211_bss_conf *info,
1790 u32 changed)
1791{
1792 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1793 struct mac80211_hwsim_data *data = hw->priv;
1794
1795 hwsim_check_magic(vif);
1796
1797 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1798 __func__, changed, vif->addr);
1799
1800 if (changed & BSS_CHANGED_BSSID) {
1801 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1802 __func__, info->bssid);
1803 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1804 }
1805
1806 if (changed & BSS_CHANGED_ASSOC) {
1807 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1808 info->assoc, info->aid);
1809 vp->assoc = info->assoc;
1810 vp->aid = info->aid;
1811 }
1812
1813 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1814 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1815 info->enable_beacon, info->beacon_int);
1816 vp->bcn_en = info->enable_beacon;
1817 if (data->started &&
1818 !hrtimer_is_queued(&data->beacon_timer) &&
1819 info->enable_beacon) {
1820 u64 tsf, until_tbtt;
1821 u32 bcn_int;
1822 data->beacon_int = info->beacon_int * 1024;
1823 tsf = mac80211_hwsim_get_tsf(hw, vif);
1824 bcn_int = data->beacon_int;
1825 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1826
1827 hrtimer_start(&data->beacon_timer,
1828 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1829 HRTIMER_MODE_REL_SOFT);
1830 } else if (!info->enable_beacon) {
1831 unsigned int count = 0;
1832 ieee80211_iterate_active_interfaces_atomic(
1833 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1834 mac80211_hwsim_bcn_en_iter, &count);
1835 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1836 count);
1837 if (count == 0) {
1838 hrtimer_cancel(&data->beacon_timer);
1839 data->beacon_int = 0;
1840 }
1841 }
1842 }
1843
1844 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1845 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1846 info->use_cts_prot);
1847 }
1848
1849 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1850 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1851 info->use_short_preamble);
1852 }
1853
1854 if (changed & BSS_CHANGED_ERP_SLOT) {
1855 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1856 }
1857
1858 if (changed & BSS_CHANGED_HT) {
1859 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1860 info->ht_operation_mode);
1861 }
1862
1863 if (changed & BSS_CHANGED_BASIC_RATES) {
1864 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1865 (unsigned long long) info->basic_rates);
1866 }
1867
1868 if (changed & BSS_CHANGED_TXPOWER)
1869 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1870}
1871
1872static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1873 struct ieee80211_vif *vif,
1874 struct ieee80211_sta *sta)
1875{
1876 hwsim_check_magic(vif);
1877 hwsim_set_sta_magic(sta);
1878
1879 return 0;
1880}
1881
1882static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1883 struct ieee80211_vif *vif,
1884 struct ieee80211_sta *sta)
1885{
1886 hwsim_check_magic(vif);
1887 hwsim_clear_sta_magic(sta);
1888
1889 return 0;
1890}
1891
1892static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1893 struct ieee80211_vif *vif,
1894 enum sta_notify_cmd cmd,
1895 struct ieee80211_sta *sta)
1896{
1897 hwsim_check_magic(vif);
1898
1899 switch (cmd) {
1900 case STA_NOTIFY_SLEEP:
1901 case STA_NOTIFY_AWAKE:
1902 /* TODO: make good use of these flags */
1903 break;
1904 default:
1905 WARN(1, "Invalid sta notify: %d\n", cmd);
1906 break;
1907 }
1908}
1909
1910static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1911 struct ieee80211_sta *sta,
1912 bool set)
1913{
1914 hwsim_check_sta_magic(sta);
1915 return 0;
1916}
1917
1918static int mac80211_hwsim_conf_tx(
1919 struct ieee80211_hw *hw,
1920 struct ieee80211_vif *vif, u16 queue,
1921 const struct ieee80211_tx_queue_params *params)
1922{
1923 wiphy_dbg(hw->wiphy,
1924 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1925 __func__, queue,
1926 params->txop, params->cw_min,
1927 params->cw_max, params->aifs);
1928 return 0;
1929}
1930
1931static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1932 struct survey_info *survey)
1933{
1934 struct mac80211_hwsim_data *hwsim = hw->priv;
1935
1936 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
1937 return -ENOENT;
1938
1939 mutex_lock(&hwsim->mutex);
1940 survey->channel = hwsim->survey_data[idx].channel;
1941 if (!survey->channel) {
1942 mutex_unlock(&hwsim->mutex);
1943 return -ENOENT;
1944 }
1945
1946 /*
1947 * Magically conjured dummy values --- this is only ok for simulated hardware.
1948 *
1949 * A real driver which cannot determine real values noise MUST NOT
1950 * report any, especially not a magically conjured ones :-)
1951 */
1952 survey->filled = SURVEY_INFO_NOISE_DBM |
1953 SURVEY_INFO_TIME |
1954 SURVEY_INFO_TIME_BUSY;
1955 survey->noise = -92;
1956 survey->time =
1957 jiffies_to_msecs(hwsim->survey_data[idx].end -
1958 hwsim->survey_data[idx].start);
1959 /* report 12.5% of channel time is used */
1960 survey->time_busy = survey->time/8;
1961 mutex_unlock(&hwsim->mutex);
1962
1963 return 0;
1964}
1965
1966#ifdef CONFIG_NL80211_TESTMODE
1967/*
1968 * This section contains example code for using netlink
1969 * attributes with the testmode command in nl80211.
1970 */
1971
1972/* These enums need to be kept in sync with userspace */
1973enum hwsim_testmode_attr {
1974 __HWSIM_TM_ATTR_INVALID = 0,
1975 HWSIM_TM_ATTR_CMD = 1,
1976 HWSIM_TM_ATTR_PS = 2,
1977
1978 /* keep last */
1979 __HWSIM_TM_ATTR_AFTER_LAST,
1980 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1981};
1982
1983enum hwsim_testmode_cmd {
1984 HWSIM_TM_CMD_SET_PS = 0,
1985 HWSIM_TM_CMD_GET_PS = 1,
1986 HWSIM_TM_CMD_STOP_QUEUES = 2,
1987 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1988};
1989
1990static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1991 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1992 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1993};
1994
1995static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1996 struct ieee80211_vif *vif,
1997 void *data, int len)
1998{
1999 struct mac80211_hwsim_data *hwsim = hw->priv;
2000 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2001 struct sk_buff *skb;
2002 int err, ps;
2003
2004 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2005 hwsim_testmode_policy, NULL);
2006 if (err)
2007 return err;
2008
2009 if (!tb[HWSIM_TM_ATTR_CMD])
2010 return -EINVAL;
2011
2012 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2013 case HWSIM_TM_CMD_SET_PS:
2014 if (!tb[HWSIM_TM_ATTR_PS])
2015 return -EINVAL;
2016 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2017 return hwsim_fops_ps_write(hwsim, ps);
2018 case HWSIM_TM_CMD_GET_PS:
2019 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2020 nla_total_size(sizeof(u32)));
2021 if (!skb)
2022 return -ENOMEM;
2023 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2024 goto nla_put_failure;
2025 return cfg80211_testmode_reply(skb);
2026 case HWSIM_TM_CMD_STOP_QUEUES:
2027 ieee80211_stop_queues(hw);
2028 return 0;
2029 case HWSIM_TM_CMD_WAKE_QUEUES:
2030 ieee80211_wake_queues(hw);
2031 return 0;
2032 default:
2033 return -EOPNOTSUPP;
2034 }
2035
2036 nla_put_failure:
2037 kfree_skb(skb);
2038 return -ENOBUFS;
2039}
2040#endif
2041
2042static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2043 struct ieee80211_vif *vif,
2044 struct ieee80211_ampdu_params *params)
2045{
2046 struct ieee80211_sta *sta = params->sta;
2047 enum ieee80211_ampdu_mlme_action action = params->action;
2048 u16 tid = params->tid;
2049
2050 switch (action) {
2051 case IEEE80211_AMPDU_TX_START:
2052 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2053 break;
2054 case IEEE80211_AMPDU_TX_STOP_CONT:
2055 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2056 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2057 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2058 break;
2059 case IEEE80211_AMPDU_TX_OPERATIONAL:
2060 break;
2061 case IEEE80211_AMPDU_RX_START:
2062 case IEEE80211_AMPDU_RX_STOP:
2063 break;
2064 default:
2065 return -EOPNOTSUPP;
2066 }
2067
2068 return 0;
2069}
2070
2071static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2072 struct ieee80211_vif *vif,
2073 u32 queues, bool drop)
2074{
2075 /* Not implemented, queues only on kernel side */
2076}
2077
2078static void hw_scan_work(struct work_struct *work)
2079{
2080 struct mac80211_hwsim_data *hwsim =
2081 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2082 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2083 int dwell, i;
2084
2085 mutex_lock(&hwsim->mutex);
2086 if (hwsim->scan_chan_idx >= req->n_channels) {
2087 struct cfg80211_scan_info info = {
2088 .aborted = false,
2089 };
2090
2091 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2092 ieee80211_scan_completed(hwsim->hw, &info);
2093 hwsim->hw_scan_request = NULL;
2094 hwsim->hw_scan_vif = NULL;
2095 hwsim->tmp_chan = NULL;
2096 mutex_unlock(&hwsim->mutex);
2097 return;
2098 }
2099
2100 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2101 req->channels[hwsim->scan_chan_idx]->center_freq);
2102
2103 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2104 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2105 IEEE80211_CHAN_RADAR) ||
2106 !req->n_ssids) {
2107 dwell = 120;
2108 } else {
2109 dwell = 30;
2110 /* send probes */
2111 for (i = 0; i < req->n_ssids; i++) {
2112 struct sk_buff *probe;
2113 struct ieee80211_mgmt *mgmt;
2114
2115 probe = ieee80211_probereq_get(hwsim->hw,
2116 hwsim->scan_addr,
2117 req->ssids[i].ssid,
2118 req->ssids[i].ssid_len,
2119 req->ie_len);
2120 if (!probe)
2121 continue;
2122
2123 mgmt = (struct ieee80211_mgmt *) probe->data;
2124 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2125 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2126
2127 if (req->ie_len)
2128 skb_put_data(probe, req->ie, req->ie_len);
2129
2130 rcu_read_lock();
2131 if (!ieee80211_tx_prepare_skb(hwsim->hw,
2132 hwsim->hw_scan_vif,
2133 probe,
2134 hwsim->tmp_chan->band,
2135 NULL)) {
2136 rcu_read_unlock();
2137 kfree_skb(probe);
2138 continue;
2139 }
2140
2141 local_bh_disable();
2142 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2143 hwsim->tmp_chan);
2144 rcu_read_unlock();
2145 local_bh_enable();
2146 }
2147 }
2148 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2149 msecs_to_jiffies(dwell));
2150 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2151 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2152 hwsim->survey_data[hwsim->scan_chan_idx].end =
2153 jiffies + msecs_to_jiffies(dwell);
2154 hwsim->scan_chan_idx++;
2155 mutex_unlock(&hwsim->mutex);
2156}
2157
2158static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2159 struct ieee80211_vif *vif,
2160 struct ieee80211_scan_request *hw_req)
2161{
2162 struct mac80211_hwsim_data *hwsim = hw->priv;
2163 struct cfg80211_scan_request *req = &hw_req->req;
2164
2165 mutex_lock(&hwsim->mutex);
2166 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2167 mutex_unlock(&hwsim->mutex);
2168 return -EBUSY;
2169 }
2170 hwsim->hw_scan_request = req;
2171 hwsim->hw_scan_vif = vif;
2172 hwsim->scan_chan_idx = 0;
2173 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2174 get_random_mask_addr(hwsim->scan_addr,
2175 hw_req->req.mac_addr,
2176 hw_req->req.mac_addr_mask);
2177 else
2178 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2179 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2180 mutex_unlock(&hwsim->mutex);
2181
2182 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2183
2184 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2185
2186 return 0;
2187}
2188
2189static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2190 struct ieee80211_vif *vif)
2191{
2192 struct mac80211_hwsim_data *hwsim = hw->priv;
2193 struct cfg80211_scan_info info = {
2194 .aborted = true,
2195 };
2196
2197 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2198
2199 cancel_delayed_work_sync(&hwsim->hw_scan);
2200
2201 mutex_lock(&hwsim->mutex);
2202 ieee80211_scan_completed(hwsim->hw, &info);
2203 hwsim->tmp_chan = NULL;
2204 hwsim->hw_scan_request = NULL;
2205 hwsim->hw_scan_vif = NULL;
2206 mutex_unlock(&hwsim->mutex);
2207}
2208
2209static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2210 struct ieee80211_vif *vif,
2211 const u8 *mac_addr)
2212{
2213 struct mac80211_hwsim_data *hwsim = hw->priv;
2214
2215 mutex_lock(&hwsim->mutex);
2216
2217 if (hwsim->scanning) {
2218 pr_debug("two hwsim sw_scans detected!\n");
2219 goto out;
2220 }
2221
2222 pr_debug("hwsim sw_scan request, prepping stuff\n");
2223
2224 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2225 hwsim->scanning = true;
2226 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2227
2228out:
2229 mutex_unlock(&hwsim->mutex);
2230}
2231
2232static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2233 struct ieee80211_vif *vif)
2234{
2235 struct mac80211_hwsim_data *hwsim = hw->priv;
2236
2237 mutex_lock(&hwsim->mutex);
2238
2239 pr_debug("hwsim sw_scan_complete\n");
2240 hwsim->scanning = false;
2241 eth_zero_addr(hwsim->scan_addr);
2242
2243 mutex_unlock(&hwsim->mutex);
2244}
2245
2246static void hw_roc_start(struct work_struct *work)
2247{
2248 struct mac80211_hwsim_data *hwsim =
2249 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2250
2251 mutex_lock(&hwsim->mutex);
2252
2253 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2254 hwsim->tmp_chan = hwsim->roc_chan;
2255 ieee80211_ready_on_channel(hwsim->hw);
2256
2257 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2258 msecs_to_jiffies(hwsim->roc_duration));
2259
2260 mutex_unlock(&hwsim->mutex);
2261}
2262
2263static void hw_roc_done(struct work_struct *work)
2264{
2265 struct mac80211_hwsim_data *hwsim =
2266 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2267
2268 mutex_lock(&hwsim->mutex);
2269 ieee80211_remain_on_channel_expired(hwsim->hw);
2270 hwsim->tmp_chan = NULL;
2271 mutex_unlock(&hwsim->mutex);
2272
2273 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2274}
2275
2276static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2277 struct ieee80211_vif *vif,
2278 struct ieee80211_channel *chan,
2279 int duration,
2280 enum ieee80211_roc_type type)
2281{
2282 struct mac80211_hwsim_data *hwsim = hw->priv;
2283
2284 mutex_lock(&hwsim->mutex);
2285 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2286 mutex_unlock(&hwsim->mutex);
2287 return -EBUSY;
2288 }
2289
2290 hwsim->roc_chan = chan;
2291 hwsim->roc_duration = duration;
2292 mutex_unlock(&hwsim->mutex);
2293
2294 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2295 chan->center_freq, duration);
2296 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2297
2298 return 0;
2299}
2300
2301static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2302 struct ieee80211_vif *vif)
2303{
2304 struct mac80211_hwsim_data *hwsim = hw->priv;
2305
2306 cancel_delayed_work_sync(&hwsim->roc_start);
2307 cancel_delayed_work_sync(&hwsim->roc_done);
2308
2309 mutex_lock(&hwsim->mutex);
2310 hwsim->tmp_chan = NULL;
2311 mutex_unlock(&hwsim->mutex);
2312
2313 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2314
2315 return 0;
2316}
2317
2318static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2319 struct ieee80211_chanctx_conf *ctx)
2320{
2321 struct mac80211_hwsim_data *hwsim = hw->priv;
2322
2323 mutex_lock(&hwsim->mutex);
2324 hwsim->chanctx = ctx;
2325 mutex_unlock(&hwsim->mutex);
2326 hwsim_set_chanctx_magic(ctx);
2327 wiphy_dbg(hw->wiphy,
2328 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2329 ctx->def.chan->center_freq, ctx->def.width,
2330 ctx->def.center_freq1, ctx->def.center_freq2);
2331 return 0;
2332}
2333
2334static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2335 struct ieee80211_chanctx_conf *ctx)
2336{
2337 struct mac80211_hwsim_data *hwsim = hw->priv;
2338
2339 mutex_lock(&hwsim->mutex);
2340 hwsim->chanctx = NULL;
2341 mutex_unlock(&hwsim->mutex);
2342 wiphy_dbg(hw->wiphy,
2343 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2344 ctx->def.chan->center_freq, ctx->def.width,
2345 ctx->def.center_freq1, ctx->def.center_freq2);
2346 hwsim_check_chanctx_magic(ctx);
2347 hwsim_clear_chanctx_magic(ctx);
2348}
2349
2350static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2351 struct ieee80211_chanctx_conf *ctx,
2352 u32 changed)
2353{
2354 struct mac80211_hwsim_data *hwsim = hw->priv;
2355
2356 mutex_lock(&hwsim->mutex);
2357 hwsim->chanctx = ctx;
2358 mutex_unlock(&hwsim->mutex);
2359 hwsim_check_chanctx_magic(ctx);
2360 wiphy_dbg(hw->wiphy,
2361 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2362 ctx->def.chan->center_freq, ctx->def.width,
2363 ctx->def.center_freq1, ctx->def.center_freq2);
2364}
2365
2366static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2367 struct ieee80211_vif *vif,
2368 struct ieee80211_chanctx_conf *ctx)
2369{
2370 hwsim_check_magic(vif);
2371 hwsim_check_chanctx_magic(ctx);
2372
2373 return 0;
2374}
2375
2376static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2377 struct ieee80211_vif *vif,
2378 struct ieee80211_chanctx_conf *ctx)
2379{
2380 hwsim_check_magic(vif);
2381 hwsim_check_chanctx_magic(ctx);
2382}
2383
2384static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2385 "tx_pkts_nic",
2386 "tx_bytes_nic",
2387 "rx_pkts_nic",
2388 "rx_bytes_nic",
2389 "d_tx_dropped",
2390 "d_tx_failed",
2391 "d_ps_mode",
2392 "d_group",
2393};
2394
2395#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2396
2397static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2398 struct ieee80211_vif *vif,
2399 u32 sset, u8 *data)
2400{
2401 if (sset == ETH_SS_STATS)
2402 memcpy(data, mac80211_hwsim_gstrings_stats,
2403 sizeof(mac80211_hwsim_gstrings_stats));
2404}
2405
2406static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2407 struct ieee80211_vif *vif, int sset)
2408{
2409 if (sset == ETH_SS_STATS)
2410 return MAC80211_HWSIM_SSTATS_LEN;
2411 return 0;
2412}
2413
2414static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2415 struct ieee80211_vif *vif,
2416 struct ethtool_stats *stats, u64 *data)
2417{
2418 struct mac80211_hwsim_data *ar = hw->priv;
2419 int i = 0;
2420
2421 data[i++] = ar->tx_pkts;
2422 data[i++] = ar->tx_bytes;
2423 data[i++] = ar->rx_pkts;
2424 data[i++] = ar->rx_bytes;
2425 data[i++] = ar->tx_dropped;
2426 data[i++] = ar->tx_failed;
2427 data[i++] = ar->ps;
2428 data[i++] = ar->group;
2429
2430 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2431}
2432
2433#define HWSIM_COMMON_OPS \
2434 .tx = mac80211_hwsim_tx, \
2435 .start = mac80211_hwsim_start, \
2436 .stop = mac80211_hwsim_stop, \
2437 .add_interface = mac80211_hwsim_add_interface, \
2438 .change_interface = mac80211_hwsim_change_interface, \
2439 .remove_interface = mac80211_hwsim_remove_interface, \
2440 .config = mac80211_hwsim_config, \
2441 .configure_filter = mac80211_hwsim_configure_filter, \
2442 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2443 .sta_add = mac80211_hwsim_sta_add, \
2444 .sta_remove = mac80211_hwsim_sta_remove, \
2445 .sta_notify = mac80211_hwsim_sta_notify, \
2446 .set_tim = mac80211_hwsim_set_tim, \
2447 .conf_tx = mac80211_hwsim_conf_tx, \
2448 .get_survey = mac80211_hwsim_get_survey, \
2449 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2450 .ampdu_action = mac80211_hwsim_ampdu_action, \
2451 .flush = mac80211_hwsim_flush, \
2452 .get_tsf = mac80211_hwsim_get_tsf, \
2453 .set_tsf = mac80211_hwsim_set_tsf, \
2454 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2455 .get_et_stats = mac80211_hwsim_get_et_stats, \
2456 .get_et_strings = mac80211_hwsim_get_et_strings,
2457
2458static const struct ieee80211_ops mac80211_hwsim_ops = {
2459 HWSIM_COMMON_OPS
2460 .sw_scan_start = mac80211_hwsim_sw_scan,
2461 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2462};
2463
2464static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2465 HWSIM_COMMON_OPS
2466 .hw_scan = mac80211_hwsim_hw_scan,
2467 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2468 .sw_scan_start = NULL,
2469 .sw_scan_complete = NULL,
2470 .remain_on_channel = mac80211_hwsim_roc,
2471 .cancel_remain_on_channel = mac80211_hwsim_croc,
2472 .add_chanctx = mac80211_hwsim_add_chanctx,
2473 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2474 .change_chanctx = mac80211_hwsim_change_chanctx,
2475 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2476 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2477};
2478
2479struct hwsim_new_radio_params {
2480 unsigned int channels;
2481 const char *reg_alpha2;
2482 const struct ieee80211_regdomain *regd;
2483 bool reg_strict;
2484 bool p2p_device;
2485 bool use_chanctx;
2486 bool destroy_on_close;
2487 const char *hwname;
2488 bool no_vif;
2489 const u8 *perm_addr;
2490 u32 iftypes;
2491 u32 *ciphers;
2492 u8 n_ciphers;
2493};
2494
2495static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2496 struct genl_info *info)
2497{
2498 if (info)
2499 genl_notify(&hwsim_genl_family, mcast_skb, info,
2500 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2501 else
2502 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2503 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2504}
2505
2506static int append_radio_msg(struct sk_buff *skb, int id,
2507 struct hwsim_new_radio_params *param)
2508{
2509 int ret;
2510
2511 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2512 if (ret < 0)
2513 return ret;
2514
2515 if (param->channels) {
2516 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2517 if (ret < 0)
2518 return ret;
2519 }
2520
2521 if (param->reg_alpha2) {
2522 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2523 param->reg_alpha2);
2524 if (ret < 0)
2525 return ret;
2526 }
2527
2528 if (param->regd) {
2529 int i;
2530
2531 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2532 if (hwsim_world_regdom_custom[i] != param->regd)
2533 continue;
2534
2535 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2536 if (ret < 0)
2537 return ret;
2538 break;
2539 }
2540 }
2541
2542 if (param->reg_strict) {
2543 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2544 if (ret < 0)
2545 return ret;
2546 }
2547
2548 if (param->p2p_device) {
2549 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2550 if (ret < 0)
2551 return ret;
2552 }
2553
2554 if (param->use_chanctx) {
2555 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2556 if (ret < 0)
2557 return ret;
2558 }
2559
2560 if (param->hwname) {
2561 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2562 strlen(param->hwname), param->hwname);
2563 if (ret < 0)
2564 return ret;
2565 }
2566
2567 return 0;
2568}
2569
2570static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2571 struct hwsim_new_radio_params *param)
2572{
2573 struct sk_buff *mcast_skb;
2574 void *data;
2575
2576 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2577 if (!mcast_skb)
2578 return;
2579
2580 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2581 HWSIM_CMD_NEW_RADIO);
2582 if (!data)
2583 goto out_err;
2584
2585 if (append_radio_msg(mcast_skb, id, param) < 0)
2586 goto out_err;
2587
2588 genlmsg_end(mcast_skb, data);
2589
2590 hwsim_mcast_config_msg(mcast_skb, info);
2591 return;
2592
2593out_err:
2594 nlmsg_free(mcast_skb);
2595}
2596
2597static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2598 {
2599 /* TODO: should we support other types, e.g., P2P?*/
2600 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2601 BIT(NL80211_IFTYPE_AP),
2602 .he_cap = {
2603 .has_he = true,
2604 .he_cap_elem = {
2605 .mac_cap_info[0] =
2606 IEEE80211_HE_MAC_CAP0_HTC_HE,
2607 .mac_cap_info[1] =
2608 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2609 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2610 .mac_cap_info[2] =
2611 IEEE80211_HE_MAC_CAP2_BSR |
2612 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2613 IEEE80211_HE_MAC_CAP2_ACK_EN,
2614 .mac_cap_info[3] =
2615 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2616 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2617 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2618 .phy_cap_info[1] =
2619 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2620 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2621 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2622 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2623 .phy_cap_info[2] =
2624 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2625 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2626 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2627 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2628 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2629
2630 /* Leave all the other PHY capability bytes
2631 * unset, as DCM, beam forming, RU and PPE
2632 * threshold information are not supported
2633 */
2634 },
2635 .he_mcs_nss_supp = {
2636 .rx_mcs_80 = cpu_to_le16(0xfffa),
2637 .tx_mcs_80 = cpu_to_le16(0xfffa),
2638 .rx_mcs_160 = cpu_to_le16(0xffff),
2639 .tx_mcs_160 = cpu_to_le16(0xffff),
2640 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2641 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2642 },
2643 },
2644 },
2645#ifdef CONFIG_MAC80211_MESH
2646 {
2647 /* TODO: should we support other types, e.g., IBSS?*/
2648 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2649 .he_cap = {
2650 .has_he = true,
2651 .he_cap_elem = {
2652 .mac_cap_info[0] =
2653 IEEE80211_HE_MAC_CAP0_HTC_HE,
2654 .mac_cap_info[1] =
2655 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2656 .mac_cap_info[2] =
2657 IEEE80211_HE_MAC_CAP2_ACK_EN,
2658 .mac_cap_info[3] =
2659 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2660 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2661 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2662 .phy_cap_info[1] =
2663 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2664 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2665 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2666 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2667 .phy_cap_info[2] = 0,
2668
2669 /* Leave all the other PHY capability bytes
2670 * unset, as DCM, beam forming, RU and PPE
2671 * threshold information are not supported
2672 */
2673 },
2674 .he_mcs_nss_supp = {
2675 .rx_mcs_80 = cpu_to_le16(0xfffa),
2676 .tx_mcs_80 = cpu_to_le16(0xfffa),
2677 .rx_mcs_160 = cpu_to_le16(0xffff),
2678 .tx_mcs_160 = cpu_to_le16(0xffff),
2679 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2680 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2681 },
2682 },
2683 },
2684#endif
2685};
2686
2687static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2688 {
2689 /* TODO: should we support other types, e.g., P2P?*/
2690 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2691 BIT(NL80211_IFTYPE_AP),
2692 .he_cap = {
2693 .has_he = true,
2694 .he_cap_elem = {
2695 .mac_cap_info[0] =
2696 IEEE80211_HE_MAC_CAP0_HTC_HE,
2697 .mac_cap_info[1] =
2698 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2699 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2700 .mac_cap_info[2] =
2701 IEEE80211_HE_MAC_CAP2_BSR |
2702 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2703 IEEE80211_HE_MAC_CAP2_ACK_EN,
2704 .mac_cap_info[3] =
2705 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2706 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2707 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2708 .phy_cap_info[0] =
2709 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2710 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2711 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2712 .phy_cap_info[1] =
2713 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2714 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2715 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2716 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2717 .phy_cap_info[2] =
2718 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2719 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2720 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2721 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2722 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2723
2724 /* Leave all the other PHY capability bytes
2725 * unset, as DCM, beam forming, RU and PPE
2726 * threshold information are not supported
2727 */
2728 },
2729 .he_mcs_nss_supp = {
2730 .rx_mcs_80 = cpu_to_le16(0xfffa),
2731 .tx_mcs_80 = cpu_to_le16(0xfffa),
2732 .rx_mcs_160 = cpu_to_le16(0xfffa),
2733 .tx_mcs_160 = cpu_to_le16(0xfffa),
2734 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2735 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2736 },
2737 },
2738 },
2739#ifdef CONFIG_MAC80211_MESH
2740 {
2741 /* TODO: should we support other types, e.g., IBSS?*/
2742 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2743 .he_cap = {
2744 .has_he = true,
2745 .he_cap_elem = {
2746 .mac_cap_info[0] =
2747 IEEE80211_HE_MAC_CAP0_HTC_HE,
2748 .mac_cap_info[1] =
2749 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2750 .mac_cap_info[2] =
2751 IEEE80211_HE_MAC_CAP2_ACK_EN,
2752 .mac_cap_info[3] =
2753 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2754 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2755 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2756 .phy_cap_info[0] =
2757 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2758 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2759 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2760 .phy_cap_info[1] =
2761 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2762 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2763 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2764 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2765 .phy_cap_info[2] = 0,
2766
2767 /* Leave all the other PHY capability bytes
2768 * unset, as DCM, beam forming, RU and PPE
2769 * threshold information are not supported
2770 */
2771 },
2772 .he_mcs_nss_supp = {
2773 .rx_mcs_80 = cpu_to_le16(0xfffa),
2774 .tx_mcs_80 = cpu_to_le16(0xfffa),
2775 .rx_mcs_160 = cpu_to_le16(0xfffa),
2776 .tx_mcs_160 = cpu_to_le16(0xfffa),
2777 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2778 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2779 },
2780 },
2781 },
2782#endif
2783};
2784
2785static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2786{
2787 u16 n_iftype_data;
2788
2789 if (sband->band == NL80211_BAND_2GHZ) {
2790 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2791 sband->iftype_data =
2792 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2793 } else if (sband->band == NL80211_BAND_5GHZ) {
2794 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2795 sband->iftype_data =
2796 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2797 } else {
2798 return;
2799 }
2800
2801 sband->n_iftype_data = n_iftype_data;
2802}
2803
2804#ifdef CONFIG_MAC80211_MESH
2805#define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2806#else
2807#define HWSIM_MESH_BIT 0
2808#endif
2809
2810#define HWSIM_DEFAULT_IF_LIMIT \
2811 (BIT(NL80211_IFTYPE_STATION) | \
2812 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2813 BIT(NL80211_IFTYPE_AP) | \
2814 BIT(NL80211_IFTYPE_P2P_GO) | \
2815 HWSIM_MESH_BIT)
2816
2817#define HWSIM_IFTYPE_SUPPORT_MASK \
2818 (BIT(NL80211_IFTYPE_STATION) | \
2819 BIT(NL80211_IFTYPE_AP) | \
2820 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2821 BIT(NL80211_IFTYPE_P2P_GO) | \
2822 BIT(NL80211_IFTYPE_ADHOC) | \
2823 BIT(NL80211_IFTYPE_MESH_POINT))
2824
2825static int mac80211_hwsim_new_radio(struct genl_info *info,
2826 struct hwsim_new_radio_params *param)
2827{
2828 int err;
2829 u8 addr[ETH_ALEN];
2830 struct mac80211_hwsim_data *data;
2831 struct ieee80211_hw *hw;
2832 enum nl80211_band band;
2833 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2834 struct net *net;
2835 int idx, i;
2836 int n_limits = 0;
2837
2838 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2839 return -EINVAL;
2840
2841 spin_lock_bh(&hwsim_radio_lock);
2842 idx = hwsim_radio_idx++;
2843 spin_unlock_bh(&hwsim_radio_lock);
2844
2845 if (param->use_chanctx)
2846 ops = &mac80211_hwsim_mchan_ops;
2847 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2848 if (!hw) {
2849 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2850 err = -ENOMEM;
2851 goto failed;
2852 }
2853
2854 /* ieee80211_alloc_hw_nm may have used a default name */
2855 param->hwname = wiphy_name(hw->wiphy);
2856
2857 if (info)
2858 net = genl_info_net(info);
2859 else
2860 net = &init_net;
2861 wiphy_net_set(hw->wiphy, net);
2862
2863 data = hw->priv;
2864 data->hw = hw;
2865
2866 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2867 if (IS_ERR(data->dev)) {
2868 printk(KERN_DEBUG
2869 "mac80211_hwsim: device_create failed (%ld)\n",
2870 PTR_ERR(data->dev));
2871 err = -ENOMEM;
2872 goto failed_drvdata;
2873 }
2874 data->dev->driver = &mac80211_hwsim_driver.driver;
2875 err = device_bind_driver(data->dev);
2876 if (err != 0) {
2877 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
2878 err);
2879 goto failed_bind;
2880 }
2881
2882 skb_queue_head_init(&data->pending);
2883
2884 SET_IEEE80211_DEV(hw, data->dev);
2885 if (!param->perm_addr) {
2886 eth_zero_addr(addr);
2887 addr[0] = 0x02;
2888 addr[1] = (mac_prefix >> 8) & 0xFF;
2889 addr[2] = mac_prefix & 0xFF;
2890 addr[3] = idx >> 8;
2891 addr[4] = idx;
2892 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2893 /* Why need here second address ? */
2894 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2895 data->addresses[1].addr[0] |= 0x40;
2896 hw->wiphy->n_addresses = 2;
2897 hw->wiphy->addresses = data->addresses;
2898 /* possible address clash is checked at hash table insertion */
2899 } else {
2900 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2901 /* compatibility with automatically generated mac addr */
2902 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2903 hw->wiphy->n_addresses = 2;
2904 hw->wiphy->addresses = data->addresses;
2905 }
2906
2907 data->channels = param->channels;
2908 data->use_chanctx = param->use_chanctx;
2909 data->idx = idx;
2910 data->destroy_on_close = param->destroy_on_close;
2911 if (info)
2912 data->portid = info->snd_portid;
2913
2914 /* setup interface limits, only on interface types we support */
2915 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
2916 data->if_limits[n_limits].max = 1;
2917 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
2918 n_limits++;
2919 }
2920
2921 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
2922 data->if_limits[n_limits].max = 2048;
2923 /*
2924 * For this case, we may only support a subset of
2925 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
2926 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
2927 */
2928 data->if_limits[n_limits].types =
2929 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
2930 n_limits++;
2931 }
2932
2933 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
2934 data->if_limits[n_limits].max = 1;
2935 data->if_limits[n_limits].types =
2936 BIT(NL80211_IFTYPE_P2P_DEVICE);
2937 n_limits++;
2938 }
2939
2940 if (data->use_chanctx) {
2941 hw->wiphy->max_scan_ssids = 255;
2942 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2943 hw->wiphy->max_remain_on_channel_duration = 1000;
2944 data->if_combination.radar_detect_widths = 0;
2945 data->if_combination.num_different_channels = data->channels;
2946 data->chanctx = NULL;
2947 } else {
2948 data->if_combination.num_different_channels = 1;
2949 data->if_combination.radar_detect_widths =
2950 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
2951 BIT(NL80211_CHAN_WIDTH_20) |
2952 BIT(NL80211_CHAN_WIDTH_40) |
2953 BIT(NL80211_CHAN_WIDTH_80) |
2954 BIT(NL80211_CHAN_WIDTH_160);
2955 }
2956
2957 if (!n_limits) {
2958 err = -EINVAL;
2959 goto failed_hw;
2960 }
2961
2962 data->if_combination.max_interfaces = 0;
2963 for (i = 0; i < n_limits; i++)
2964 data->if_combination.max_interfaces +=
2965 data->if_limits[i].max;
2966
2967 data->if_combination.n_limits = n_limits;
2968 data->if_combination.limits = data->if_limits;
2969
2970 /*
2971 * If we actually were asked to support combinations,
2972 * advertise them - if there's only a single thing like
2973 * only IBSS then don't advertise it as combinations.
2974 */
2975 if (data->if_combination.max_interfaces > 1) {
2976 hw->wiphy->iface_combinations = &data->if_combination;
2977 hw->wiphy->n_iface_combinations = 1;
2978 }
2979
2980 if (param->ciphers) {
2981 memcpy(data->ciphers, param->ciphers,
2982 param->n_ciphers * sizeof(u32));
2983 hw->wiphy->cipher_suites = data->ciphers;
2984 hw->wiphy->n_cipher_suites = param->n_ciphers;
2985 }
2986
2987 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
2988 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2989 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2990
2991 hw->queues = 5;
2992 hw->offchannel_tx_hw_queue = 4;
2993
2994 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2995 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2996 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2997 ieee80211_hw_set(hw, QUEUE_CONTROL);
2998 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2999 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3000 ieee80211_hw_set(hw, MFP_CAPABLE);
3001 ieee80211_hw_set(hw, SIGNAL_DBM);
3002 ieee80211_hw_set(hw, SUPPORTS_PS);
3003 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3004 if (rctbl)
3005 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3006 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3007
3008 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3009 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3010 WIPHY_FLAG_AP_UAPSD |
3011 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3012 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3013 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3014 NL80211_FEATURE_STATIC_SMPS |
3015 NL80211_FEATURE_DYNAMIC_SMPS |
3016 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3017 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3018
3019 hw->wiphy->interface_modes = param->iftypes;
3020
3021 /* ask mac80211 to reserve space for magic */
3022 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3023 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3024 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3025
3026 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3027 sizeof(hwsim_channels_2ghz));
3028 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3029 sizeof(hwsim_channels_5ghz));
3030 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3031
3032 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3033 struct ieee80211_supported_band *sband = &data->bands[band];
3034
3035 sband->band = band;
3036
3037 switch (band) {
3038 case NL80211_BAND_2GHZ:
3039 sband->channels = data->channels_2ghz;
3040 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3041 sband->bitrates = data->rates;
3042 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3043 break;
3044 case NL80211_BAND_5GHZ:
3045 sband->channels = data->channels_5ghz;
3046 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3047 sband->bitrates = data->rates + 4;
3048 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3049
3050 sband->vht_cap.vht_supported = true;
3051 sband->vht_cap.cap =
3052 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3053 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3054 IEEE80211_VHT_CAP_RXLDPC |
3055 IEEE80211_VHT_CAP_SHORT_GI_80 |
3056 IEEE80211_VHT_CAP_SHORT_GI_160 |
3057 IEEE80211_VHT_CAP_TXSTBC |
3058 IEEE80211_VHT_CAP_RXSTBC_4 |
3059 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3060 sband->vht_cap.vht_mcs.rx_mcs_map =
3061 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3062 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3063 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3064 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3065 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3066 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3067 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3068 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3069 sband->vht_cap.vht_mcs.tx_mcs_map =
3070 sband->vht_cap.vht_mcs.rx_mcs_map;
3071 break;
3072 default:
3073 continue;
3074 }
3075
3076 sband->ht_cap.ht_supported = true;
3077 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3078 IEEE80211_HT_CAP_GRN_FLD |
3079 IEEE80211_HT_CAP_SGI_20 |
3080 IEEE80211_HT_CAP_SGI_40 |
3081 IEEE80211_HT_CAP_DSSSCCK40;
3082 sband->ht_cap.ampdu_factor = 0x3;
3083 sband->ht_cap.ampdu_density = 0x6;
3084 memset(&sband->ht_cap.mcs, 0,
3085 sizeof(sband->ht_cap.mcs));
3086 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3087 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3088 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3089
3090 mac80211_hwsim_he_capab(sband);
3091
3092 hw->wiphy->bands[band] = sband;
3093 }
3094
3095 /* By default all radios belong to the first group */
3096 data->group = 1;
3097 mutex_init(&data->mutex);
3098
3099 data->netgroup = hwsim_net_get_netgroup(net);
3100 data->wmediumd = hwsim_net_get_wmediumd(net);
3101
3102 /* Enable frame retransmissions for lossy channels */
3103 hw->max_rates = 4;
3104 hw->max_rate_tries = 11;
3105
3106 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3107 hw->wiphy->n_vendor_commands =
3108 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3109 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3110 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3111
3112 if (param->reg_strict)
3113 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3114 if (param->regd) {
3115 data->regd = param->regd;
3116 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3117 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3118 /* give the regulatory workqueue a chance to run */
3119 schedule_timeout_interruptible(1);
3120 }
3121
3122 if (param->no_vif)
3123 ieee80211_hw_set(hw, NO_AUTO_VIF);
3124
3125 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3126
3127 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3128 HRTIMER_MODE_ABS_SOFT);
3129 data->beacon_timer.function = mac80211_hwsim_beacon;
3130
3131 err = ieee80211_register_hw(hw);
3132 if (err < 0) {
3133 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3134 err);
3135 goto failed_hw;
3136 }
3137
3138 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3139
3140 if (param->reg_alpha2) {
3141 data->alpha2[0] = param->reg_alpha2[0];
3142 data->alpha2[1] = param->reg_alpha2[1];
3143 regulatory_hint(hw->wiphy, param->reg_alpha2);
3144 }
3145
3146 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3147 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3148 debugfs_create_file("group", 0666, data->debugfs, data,
3149 &hwsim_fops_group);
3150 if (!data->use_chanctx)
3151 debugfs_create_file("dfs_simulate_radar", 0222,
3152 data->debugfs,
3153 data, &hwsim_simulate_radar);
3154
3155 spin_lock_bh(&hwsim_radio_lock);
3156 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3157 hwsim_rht_params);
3158 if (err < 0) {
3159 if (info) {
3160 GENL_SET_ERR_MSG(info, "perm addr already present");
3161 NL_SET_BAD_ATTR(info->extack,
3162 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3163 }
3164 spin_unlock_bh(&hwsim_radio_lock);
3165 goto failed_final_insert;
3166 }
3167
3168 list_add_tail(&data->list, &hwsim_radios);
3169 hwsim_radios_generation++;
3170 spin_unlock_bh(&hwsim_radio_lock);
3171
3172 hwsim_mcast_new_radio(idx, info, param);
3173
3174 return idx;
3175
3176failed_final_insert:
3177 debugfs_remove_recursive(data->debugfs);
3178 ieee80211_unregister_hw(data->hw);
3179failed_hw:
3180 device_release_driver(data->dev);
3181failed_bind:
3182 device_unregister(data->dev);
3183failed_drvdata:
3184 ieee80211_free_hw(hw);
3185failed:
3186 return err;
3187}
3188
3189static void hwsim_mcast_del_radio(int id, const char *hwname,
3190 struct genl_info *info)
3191{
3192 struct sk_buff *skb;
3193 void *data;
3194 int ret;
3195
3196 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3197 if (!skb)
3198 return;
3199
3200 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3201 HWSIM_CMD_DEL_RADIO);
3202 if (!data)
3203 goto error;
3204
3205 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3206 if (ret < 0)
3207 goto error;
3208
3209 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3210 hwname);
3211 if (ret < 0)
3212 goto error;
3213
3214 genlmsg_end(skb, data);
3215
3216 hwsim_mcast_config_msg(skb, info);
3217
3218 return;
3219
3220error:
3221 nlmsg_free(skb);
3222}
3223
3224static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3225 const char *hwname,
3226 struct genl_info *info)
3227{
3228 hwsim_mcast_del_radio(data->idx, hwname, info);
3229 debugfs_remove_recursive(data->debugfs);
3230 ieee80211_unregister_hw(data->hw);
3231 device_release_driver(data->dev);
3232 device_unregister(data->dev);
3233 ieee80211_free_hw(data->hw);
3234}
3235
3236static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3237 struct mac80211_hwsim_data *data,
3238 u32 portid, u32 seq,
3239 struct netlink_callback *cb, int flags)
3240{
3241 void *hdr;
3242 struct hwsim_new_radio_params param = { };
3243 int res = -EMSGSIZE;
3244
3245 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3246 HWSIM_CMD_GET_RADIO);
3247 if (!hdr)
3248 return -EMSGSIZE;
3249
3250 if (cb)
3251 genl_dump_check_consistent(cb, hdr);
3252
3253 if (data->alpha2[0] && data->alpha2[1])
3254 param.reg_alpha2 = data->alpha2;
3255
3256 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3257 REGULATORY_STRICT_REG);
3258 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3259 BIT(NL80211_IFTYPE_P2P_DEVICE));
3260 param.use_chanctx = data->use_chanctx;
3261 param.regd = data->regd;
3262 param.channels = data->channels;
3263 param.hwname = wiphy_name(data->hw->wiphy);
3264
3265 res = append_radio_msg(skb, data->idx, &param);
3266 if (res < 0)
3267 goto out_err;
3268
3269 genlmsg_end(skb, hdr);
3270 return 0;
3271
3272out_err:
3273 genlmsg_cancel(skb, hdr);
3274 return res;
3275}
3276
3277static void mac80211_hwsim_free(void)
3278{
3279 struct mac80211_hwsim_data *data;
3280
3281 spin_lock_bh(&hwsim_radio_lock);
3282 while ((data = list_first_entry_or_null(&hwsim_radios,
3283 struct mac80211_hwsim_data,
3284 list))) {
3285 list_del(&data->list);
3286 spin_unlock_bh(&hwsim_radio_lock);
3287 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3288 NULL);
3289 spin_lock_bh(&hwsim_radio_lock);
3290 }
3291 spin_unlock_bh(&hwsim_radio_lock);
3292 class_destroy(hwsim_class);
3293}
3294
3295static const struct net_device_ops hwsim_netdev_ops = {
3296 .ndo_start_xmit = hwsim_mon_xmit,
3297 .ndo_set_mac_address = eth_mac_addr,
3298 .ndo_validate_addr = eth_validate_addr,
3299};
3300
3301static void hwsim_mon_setup(struct net_device *dev)
3302{
3303 dev->netdev_ops = &hwsim_netdev_ops;
3304 dev->needs_free_netdev = true;
3305 ether_setup(dev);
3306 dev->priv_flags |= IFF_NO_QUEUE;
3307 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3308 eth_zero_addr(dev->dev_addr);
3309 dev->dev_addr[0] = 0x12;
3310}
3311
3312static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3313{
3314 return rhashtable_lookup_fast(&hwsim_radios_rht,
3315 addr,
3316 hwsim_rht_params);
3317}
3318
3319static void hwsim_register_wmediumd(struct net *net, u32 portid)
3320{
3321 struct mac80211_hwsim_data *data;
3322
3323 hwsim_net_set_wmediumd(net, portid);
3324
3325 spin_lock_bh(&hwsim_radio_lock);
3326 list_for_each_entry(data, &hwsim_radios, list) {
3327 if (data->netgroup == hwsim_net_get_netgroup(net))
3328 data->wmediumd = portid;
3329 }
3330 spin_unlock_bh(&hwsim_radio_lock);
3331}
3332
3333static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3334 struct genl_info *info)
3335{
3336
3337 struct ieee80211_hdr *hdr;
3338 struct mac80211_hwsim_data *data2;
3339 struct ieee80211_tx_info *txi;
3340 struct hwsim_tx_rate *tx_attempts;
3341 u64 ret_skb_cookie;
3342 struct sk_buff *skb, *tmp;
3343 const u8 *src;
3344 unsigned int hwsim_flags;
3345 int i;
3346 unsigned long flags;
3347 bool found = false;
3348
3349 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3350 !info->attrs[HWSIM_ATTR_FLAGS] ||
3351 !info->attrs[HWSIM_ATTR_COOKIE] ||
3352 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3353 !info->attrs[HWSIM_ATTR_TX_INFO])
3354 goto out;
3355
3356 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3357 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3358 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3359
3360 data2 = get_hwsim_data_ref_from_addr(src);
3361 if (!data2)
3362 goto out;
3363
3364 if (!hwsim_virtio_enabled) {
3365 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3366 data2->netgroup)
3367 goto out;
3368
3369 if (info->snd_portid != data2->wmediumd)
3370 goto out;
3371 }
3372
3373 /* look for the skb matching the cookie passed back from user */
3374 spin_lock_irqsave(&data2->pending.lock, flags);
3375 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3376 uintptr_t skb_cookie;
3377
3378 txi = IEEE80211_SKB_CB(skb);
3379 skb_cookie = (uintptr_t)txi->rate_driver_data[0];
3380
3381 if (skb_cookie == ret_skb_cookie) {
3382 __skb_unlink(skb, &data2->pending);
3383 found = true;
3384 break;
3385 }
3386 }
3387 spin_unlock_irqrestore(&data2->pending.lock, flags);
3388
3389 /* not found */
3390 if (!found)
3391 goto out;
3392
3393 /* Tx info received because the frame was broadcasted on user space,
3394 so we get all the necessary info: tx attempts and skb control buff */
3395
3396 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3397 info->attrs[HWSIM_ATTR_TX_INFO]);
3398
3399 /* now send back TX status */
3400 txi = IEEE80211_SKB_CB(skb);
3401
3402 ieee80211_tx_info_clear_status(txi);
3403
3404 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3405 txi->status.rates[i].idx = tx_attempts[i].idx;
3406 txi->status.rates[i].count = tx_attempts[i].count;
3407 }
3408
3409 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3410
3411 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3412 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3413 if (skb->len >= 16) {
3414 hdr = (struct ieee80211_hdr *) skb->data;
3415 mac80211_hwsim_monitor_ack(data2->channel,
3416 hdr->addr2);
3417 }
3418 txi->flags |= IEEE80211_TX_STAT_ACK;
3419 }
3420
3421 if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
3422 txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
3423
3424 ieee80211_tx_status_irqsafe(data2->hw, skb);
3425 return 0;
3426out:
3427 return -EINVAL;
3428
3429}
3430
3431static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3432 struct genl_info *info)
3433{
3434 struct mac80211_hwsim_data *data2;
3435 struct ieee80211_rx_status rx_status;
3436 struct ieee80211_hdr *hdr;
3437 const u8 *dst;
3438 int frame_data_len;
3439 void *frame_data;
3440 struct sk_buff *skb = NULL;
3441 struct ieee80211_channel *channel = NULL;
3442
3443 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3444 !info->attrs[HWSIM_ATTR_FRAME] ||
3445 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3446 !info->attrs[HWSIM_ATTR_SIGNAL])
3447 goto out;
3448
3449 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3450 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3451 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3452
3453 if (frame_data_len < sizeof(struct ieee80211_hdr_3addr) ||
3454 frame_data_len > IEEE80211_MAX_DATA_LEN)
3455 goto err;
3456
3457 /* Allocate new skb here */
3458 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3459 if (skb == NULL)
3460 goto err;
3461
3462 /* Copy the data */
3463 skb_put_data(skb, frame_data, frame_data_len);
3464
3465 data2 = get_hwsim_data_ref_from_addr(dst);
3466 if (!data2)
3467 goto out;
3468
3469 if (data2->use_chanctx) {
3470 if (data2->tmp_chan)
3471 channel = data2->tmp_chan;
3472 else if (data2->chanctx)
3473 channel = data2->chanctx->def.chan;
3474 } else {
3475 channel = data2->channel;
3476 }
3477 if (!channel)
3478 goto out;
3479
3480 if (!hwsim_virtio_enabled) {
3481 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3482 data2->netgroup)
3483 goto out;
3484
3485 if (info->snd_portid != data2->wmediumd)
3486 goto out;
3487 }
3488
3489 /* check if radio is configured properly */
3490
3491 if ((data2->idle && !data2->tmp_chan) || !data2->started)
3492 goto out;
3493
3494 /* A frame is received from user space */
3495 memset(&rx_status, 0, sizeof(rx_status));
3496 if (info->attrs[HWSIM_ATTR_FREQ]) {
3497 /* throw away off-channel packets, but allow both the temporary
3498 * ("hw" scan/remain-on-channel) and regular channel, since the
3499 * internal datapath also allows this
3500 */
3501 mutex_lock(&data2->mutex);
3502 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3503
3504 if (rx_status.freq != channel->center_freq) {
3505 mutex_unlock(&data2->mutex);
3506 goto out;
3507 }
3508 mutex_unlock(&data2->mutex);
3509 } else {
3510 rx_status.freq = channel->center_freq;
3511 }
3512
3513 rx_status.band = channel->band;
3514 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3515 if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
3516 goto out;
3517 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3518
3519 hdr = (void *)skb->data;
3520
3521 if (ieee80211_is_beacon(hdr->frame_control) ||
3522 ieee80211_is_probe_resp(hdr->frame_control))
3523 rx_status.boottime_ns = ktime_get_boottime_ns();
3524
3525 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3526 data2->rx_pkts++;
3527 data2->rx_bytes += skb->len;
3528 ieee80211_rx_irqsafe(data2->hw, skb);
3529
3530 return 0;
3531err:
3532 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3533out:
3534 dev_kfree_skb(skb);
3535 return -EINVAL;
3536}
3537
3538static int hwsim_register_received_nl(struct sk_buff *skb_2,
3539 struct genl_info *info)
3540{
3541 struct net *net = genl_info_net(info);
3542 struct mac80211_hwsim_data *data;
3543 int chans = 1;
3544
3545 spin_lock_bh(&hwsim_radio_lock);
3546 list_for_each_entry(data, &hwsim_radios, list)
3547 chans = max(chans, data->channels);
3548 spin_unlock_bh(&hwsim_radio_lock);
3549
3550 /* In the future we should revise the userspace API and allow it
3551 * to set a flag that it does support multi-channel, then we can
3552 * let this pass conditionally on the flag.
3553 * For current userspace, prohibit it since it won't work right.
3554 */
3555 if (chans > 1)
3556 return -EOPNOTSUPP;
3557
3558 if (hwsim_net_get_wmediumd(net))
3559 return -EBUSY;
3560
3561 hwsim_register_wmediumd(net, info->snd_portid);
3562
3563 pr_debug("mac80211_hwsim: received a REGISTER, "
3564 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3565
3566 return 0;
3567}
3568
3569/* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3570static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3571{
3572 int i;
3573
3574 for (i = 0; i < n_ciphers; i++) {
3575 int j;
3576 int found = 0;
3577
3578 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3579 if (ciphers[i] == hwsim_ciphers[j]) {
3580 found = 1;
3581 break;
3582 }
3583 }
3584
3585 if (!found)
3586 return false;
3587 }
3588
3589 return true;
3590}
3591
3592static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3593{
3594 struct hwsim_new_radio_params param = { 0 };
3595 const char *hwname = NULL;
3596 int ret;
3597
3598 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3599 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3600 param.channels = channels;
3601 param.destroy_on_close =
3602 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3603
3604 if (info->attrs[HWSIM_ATTR_CHANNELS])
3605 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3606
3607 if (param.channels < 1) {
3608 GENL_SET_ERR_MSG(info, "must have at least one channel");
3609 return -EINVAL;
3610 }
3611
3612 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3613 GENL_SET_ERR_MSG(info, "too many channels specified");
3614 return -EINVAL;
3615 }
3616
3617 if (info->attrs[HWSIM_ATTR_NO_VIF])
3618 param.no_vif = true;
3619
3620 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3621 param.use_chanctx = true;
3622 else
3623 param.use_chanctx = (param.channels > 1);
3624
3625 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3626 param.reg_alpha2 =
3627 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3628
3629 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3630 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3631
3632 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3633 return -EINVAL;
3634
3635 idx = array_index_nospec(idx,
3636 ARRAY_SIZE(hwsim_world_regdom_custom));
3637 param.regd = hwsim_world_regdom_custom[idx];
3638 }
3639
3640 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3641 if (!is_valid_ether_addr(
3642 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3643 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3644 NL_SET_BAD_ATTR(info->extack,
3645 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3646 return -EINVAL;
3647 }
3648
3649 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3650 }
3651
3652 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3653 param.iftypes =
3654 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3655
3656 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3657 NL_SET_ERR_MSG_ATTR(info->extack,
3658 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3659 "cannot support more iftypes than kernel");
3660 return -EINVAL;
3661 }
3662 } else {
3663 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3664 }
3665
3666 /* ensure both flag and iftype support is honored */
3667 if (param.p2p_device ||
3668 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3669 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3670 param.p2p_device = true;
3671 }
3672
3673 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3674 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3675
3676 param.ciphers =
3677 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3678
3679 if (len % sizeof(u32)) {
3680 NL_SET_ERR_MSG_ATTR(info->extack,
3681 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3682 "bad cipher list length");
3683 return -EINVAL;
3684 }
3685
3686 param.n_ciphers = len / sizeof(u32);
3687
3688 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3689 NL_SET_ERR_MSG_ATTR(info->extack,
3690 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3691 "too many ciphers specified");
3692 return -EINVAL;
3693 }
3694
3695 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3696 NL_SET_ERR_MSG_ATTR(info->extack,
3697 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3698 "unsupported ciphers specified");
3699 return -EINVAL;
3700 }
3701 }
3702
3703 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3704 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3705 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3706 GFP_KERNEL);
3707 if (!hwname)
3708 return -ENOMEM;
3709 param.hwname = hwname;
3710 }
3711
3712 ret = mac80211_hwsim_new_radio(info, &param);
3713 kfree(hwname);
3714 return ret;
3715}
3716
3717static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3718{
3719 struct mac80211_hwsim_data *data;
3720 s64 idx = -1;
3721 const char *hwname = NULL;
3722
3723 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3724 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3725 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3726 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3727 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3728 GFP_KERNEL);
3729 if (!hwname)
3730 return -ENOMEM;
3731 } else
3732 return -EINVAL;
3733
3734 spin_lock_bh(&hwsim_radio_lock);
3735 list_for_each_entry(data, &hwsim_radios, list) {
3736 if (idx >= 0) {
3737 if (data->idx != idx)
3738 continue;
3739 } else {
3740 if (!hwname ||
3741 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3742 continue;
3743 }
3744
3745 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3746 continue;
3747
3748 list_del(&data->list);
3749 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3750 hwsim_rht_params);
3751 hwsim_radios_generation++;
3752 spin_unlock_bh(&hwsim_radio_lock);
3753 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3754 info);
3755 kfree(hwname);
3756 return 0;
3757 }
3758 spin_unlock_bh(&hwsim_radio_lock);
3759
3760 kfree(hwname);
3761 return -ENODEV;
3762}
3763
3764static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3765{
3766 struct mac80211_hwsim_data *data;
3767 struct sk_buff *skb;
3768 int idx, res = -ENODEV;
3769
3770 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3771 return -EINVAL;
3772 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3773
3774 spin_lock_bh(&hwsim_radio_lock);
3775 list_for_each_entry(data, &hwsim_radios, list) {
3776 if (data->idx != idx)
3777 continue;
3778
3779 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3780 continue;
3781
3782 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3783 if (!skb) {
3784 res = -ENOMEM;
3785 goto out_err;
3786 }
3787
3788 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3789 info->snd_seq, NULL, 0);
3790 if (res < 0) {
3791 nlmsg_free(skb);
3792 goto out_err;
3793 }
3794
3795 res = genlmsg_reply(skb, info);
3796 break;
3797 }
3798
3799out_err:
3800 spin_unlock_bh(&hwsim_radio_lock);
3801
3802 return res;
3803}
3804
3805static int hwsim_dump_radio_nl(struct sk_buff *skb,
3806 struct netlink_callback *cb)
3807{
3808 int last_idx = cb->args[0] - 1;
3809 struct mac80211_hwsim_data *data = NULL;
3810 int res = 0;
3811 void *hdr;
3812
3813 spin_lock_bh(&hwsim_radio_lock);
3814 cb->seq = hwsim_radios_generation;
3815
3816 if (last_idx >= hwsim_radio_idx-1)
3817 goto done;
3818
3819 list_for_each_entry(data, &hwsim_radios, list) {
3820 if (data->idx <= last_idx)
3821 continue;
3822
3823 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3824 continue;
3825
3826 res = mac80211_hwsim_get_radio(skb, data,
3827 NETLINK_CB(cb->skb).portid,
3828 cb->nlh->nlmsg_seq, cb,
3829 NLM_F_MULTI);
3830 if (res < 0)
3831 break;
3832
3833 last_idx = data->idx;
3834 }
3835
3836 cb->args[0] = last_idx + 1;
3837
3838 /* list changed, but no new element sent, set interrupted flag */
3839 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3840 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3841 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3842 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3843 if (hdr) {
3844 genl_dump_check_consistent(cb, hdr);
3845 genlmsg_end(skb, hdr);
3846 } else {
3847 res = -EMSGSIZE;
3848 }
3849 }
3850
3851done:
3852 spin_unlock_bh(&hwsim_radio_lock);
3853 return res ?: skb->len;
3854}
3855
3856/* Generic Netlink operations array */
3857static const struct genl_ops hwsim_ops[] = {
3858 {
3859 .cmd = HWSIM_CMD_REGISTER,
3860 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3861 .doit = hwsim_register_received_nl,
3862 .flags = GENL_UNS_ADMIN_PERM,
3863 },
3864 {
3865 .cmd = HWSIM_CMD_FRAME,
3866 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3867 .doit = hwsim_cloned_frame_received_nl,
3868 },
3869 {
3870 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3871 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3872 .doit = hwsim_tx_info_frame_received_nl,
3873 },
3874 {
3875 .cmd = HWSIM_CMD_NEW_RADIO,
3876 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3877 .doit = hwsim_new_radio_nl,
3878 .flags = GENL_UNS_ADMIN_PERM,
3879 },
3880 {
3881 .cmd = HWSIM_CMD_DEL_RADIO,
3882 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3883 .doit = hwsim_del_radio_nl,
3884 .flags = GENL_UNS_ADMIN_PERM,
3885 },
3886 {
3887 .cmd = HWSIM_CMD_GET_RADIO,
3888 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3889 .doit = hwsim_get_radio_nl,
3890 .dumpit = hwsim_dump_radio_nl,
3891 },
3892};
3893
3894static struct genl_family hwsim_genl_family __ro_after_init = {
3895 .name = "MAC80211_HWSIM",
3896 .version = 1,
3897 .maxattr = HWSIM_ATTR_MAX,
3898 .policy = hwsim_genl_policy,
3899 .netnsok = true,
3900 .module = THIS_MODULE,
3901 .ops = hwsim_ops,
3902 .n_ops = ARRAY_SIZE(hwsim_ops),
3903 .mcgrps = hwsim_mcgrps,
3904 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3905};
3906
3907static void remove_user_radios(u32 portid)
3908{
3909 struct mac80211_hwsim_data *entry, *tmp;
3910 LIST_HEAD(list);
3911
3912 spin_lock_bh(&hwsim_radio_lock);
3913 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3914 if (entry->destroy_on_close && entry->portid == portid) {
3915 list_move(&entry->list, &list);
3916 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3917 hwsim_rht_params);
3918 hwsim_radios_generation++;
3919 }
3920 }
3921 spin_unlock_bh(&hwsim_radio_lock);
3922
3923 list_for_each_entry_safe(entry, tmp, &list, list) {
3924 list_del(&entry->list);
3925 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
3926 NULL);
3927 }
3928}
3929
3930static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3931 unsigned long state,
3932 void *_notify)
3933{
3934 struct netlink_notify *notify = _notify;
3935
3936 if (state != NETLINK_URELEASE)
3937 return NOTIFY_DONE;
3938
3939 remove_user_radios(notify->portid);
3940
3941 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
3942 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3943 " socket, switching to perfect channel medium\n");
3944 hwsim_register_wmediumd(notify->net, 0);
3945 }
3946 return NOTIFY_DONE;
3947
3948}
3949
3950static struct notifier_block hwsim_netlink_notifier = {
3951 .notifier_call = mac80211_hwsim_netlink_notify,
3952};
3953
3954static int __init hwsim_init_netlink(void)
3955{
3956 int rc;
3957
3958 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3959
3960 rc = genl_register_family(&hwsim_genl_family);
3961 if (rc)
3962 goto failure;
3963
3964 rc = netlink_register_notifier(&hwsim_netlink_notifier);
3965 if (rc) {
3966 genl_unregister_family(&hwsim_genl_family);
3967 goto failure;
3968 }
3969
3970 return 0;
3971
3972failure:
3973 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3974 return -EINVAL;
3975}
3976
3977static __net_init int hwsim_init_net(struct net *net)
3978{
3979 return hwsim_net_set_netgroup(net);
3980}
3981
3982static void __net_exit hwsim_exit_net(struct net *net)
3983{
3984 struct mac80211_hwsim_data *data, *tmp;
3985 LIST_HEAD(list);
3986
3987 spin_lock_bh(&hwsim_radio_lock);
3988 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3989 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3990 continue;
3991
3992 /* Radios created in init_net are returned to init_net. */
3993 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3994 continue;
3995
3996 list_move(&data->list, &list);
3997 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3998 hwsim_rht_params);
3999 hwsim_radios_generation++;
4000 }
4001 spin_unlock_bh(&hwsim_radio_lock);
4002
4003 list_for_each_entry_safe(data, tmp, &list, list) {
4004 list_del(&data->list);
4005 mac80211_hwsim_del_radio(data,
4006 wiphy_name(data->hw->wiphy),
4007 NULL);
4008 }
4009
4010 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4011}
4012
4013static struct pernet_operations hwsim_net_ops = {
4014 .init = hwsim_init_net,
4015 .exit = hwsim_exit_net,
4016 .id = &hwsim_net_id,
4017 .size = sizeof(struct hwsim_net),
4018};
4019
4020static void hwsim_exit_netlink(void)
4021{
4022 /* unregister the notifier */
4023 netlink_unregister_notifier(&hwsim_netlink_notifier);
4024 /* unregister the family */
4025 genl_unregister_family(&hwsim_genl_family);
4026}
4027
4028#if IS_REACHABLE(CONFIG_VIRTIO)
4029static void hwsim_virtio_tx_done(struct virtqueue *vq)
4030{
4031 unsigned int len;
4032 struct sk_buff *skb;
4033 unsigned long flags;
4034
4035 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4036 while ((skb = virtqueue_get_buf(vq, &len)))
4037 nlmsg_free(skb);
4038 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4039}
4040
4041static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4042{
4043 struct nlmsghdr *nlh;
4044 struct genlmsghdr *gnlh;
4045 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4046 struct genl_info info = {};
4047 int err;
4048
4049 nlh = nlmsg_hdr(skb);
4050 gnlh = nlmsg_data(nlh);
4051 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4052 hwsim_genl_policy, NULL);
4053 if (err) {
4054 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4055 return err;
4056 }
4057
4058 info.attrs = tb;
4059
4060 switch (gnlh->cmd) {
4061 case HWSIM_CMD_FRAME:
4062 hwsim_cloned_frame_received_nl(skb, &info);
4063 break;
4064 case HWSIM_CMD_TX_INFO_FRAME:
4065 hwsim_tx_info_frame_received_nl(skb, &info);
4066 break;
4067 default:
4068 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4069 return -EPROTO;
4070 }
4071 return 0;
4072}
4073
4074static void hwsim_virtio_rx_work(struct work_struct *work)
4075{
4076 struct virtqueue *vq;
4077 unsigned int len;
4078 struct sk_buff *skb;
4079 struct scatterlist sg[1];
4080 int err;
4081 unsigned long flags;
4082
4083 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4084 if (!hwsim_virtio_enabled)
4085 goto out_unlock;
4086
4087 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4088 if (!skb)
4089 goto out_unlock;
4090 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4091
4092 skb->data = skb->head;
4093 skb_set_tail_pointer(skb, len);
4094 hwsim_virtio_handle_cmd(skb);
4095
4096 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4097 if (!hwsim_virtio_enabled) {
4098 nlmsg_free(skb);
4099 goto out_unlock;
4100 }
4101 vq = hwsim_vqs[HWSIM_VQ_RX];
4102 sg_init_one(sg, skb->head, skb_end_offset(skb));
4103 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4104 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4105 nlmsg_free(skb);
4106 else
4107 virtqueue_kick(vq);
4108 schedule_work(&hwsim_virtio_rx);
4109
4110out_unlock:
4111 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4112}
4113
4114static void hwsim_virtio_rx_done(struct virtqueue *vq)
4115{
4116 schedule_work(&hwsim_virtio_rx);
4117}
4118
4119static int init_vqs(struct virtio_device *vdev)
4120{
4121 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4122 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4123 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4124 };
4125 const char *names[HWSIM_NUM_VQS] = {
4126 [HWSIM_VQ_TX] = "tx",
4127 [HWSIM_VQ_RX] = "rx",
4128 };
4129
4130 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4131 hwsim_vqs, callbacks, names, NULL);
4132}
4133
4134static int fill_vq(struct virtqueue *vq)
4135{
4136 int i, err;
4137 struct sk_buff *skb;
4138 struct scatterlist sg[1];
4139
4140 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4141 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4142 if (!skb)
4143 return -ENOMEM;
4144
4145 sg_init_one(sg, skb->head, skb_end_offset(skb));
4146 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4147 if (err) {
4148 nlmsg_free(skb);
4149 return err;
4150 }
4151 }
4152 virtqueue_kick(vq);
4153 return 0;
4154}
4155
4156static void remove_vqs(struct virtio_device *vdev)
4157{
4158 int i;
4159
4160 vdev->config->reset(vdev);
4161
4162 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4163 struct virtqueue *vq = hwsim_vqs[i];
4164 struct sk_buff *skb;
4165
4166 while ((skb = virtqueue_detach_unused_buf(vq)))
4167 nlmsg_free(skb);
4168 }
4169
4170 vdev->config->del_vqs(vdev);
4171}
4172
4173static int hwsim_virtio_probe(struct virtio_device *vdev)
4174{
4175 int err;
4176 unsigned long flags;
4177
4178 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4179 if (hwsim_virtio_enabled) {
4180 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4181 return -EEXIST;
4182 }
4183 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4184
4185 err = init_vqs(vdev);
4186 if (err)
4187 return err;
4188
4189 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4190 if (err)
4191 goto out_remove;
4192
4193 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4194 hwsim_virtio_enabled = true;
4195 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4196
4197 schedule_work(&hwsim_virtio_rx);
4198 return 0;
4199
4200out_remove:
4201 remove_vqs(vdev);
4202 return err;
4203}
4204
4205static void hwsim_virtio_remove(struct virtio_device *vdev)
4206{
4207 hwsim_virtio_enabled = false;
4208
4209 cancel_work_sync(&hwsim_virtio_rx);
4210
4211 remove_vqs(vdev);
4212}
4213
4214/* MAC80211_HWSIM virtio device id table */
4215static const struct virtio_device_id id_table[] = {
4216 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4217 { 0 }
4218};
4219MODULE_DEVICE_TABLE(virtio, id_table);
4220
4221static struct virtio_driver virtio_hwsim = {
4222 .driver.name = KBUILD_MODNAME,
4223 .driver.owner = THIS_MODULE,
4224 .id_table = id_table,
4225 .probe = hwsim_virtio_probe,
4226 .remove = hwsim_virtio_remove,
4227};
4228
4229static int hwsim_register_virtio_driver(void)
4230{
4231 spin_lock_init(&hwsim_virtio_lock);
4232
4233 return register_virtio_driver(&virtio_hwsim);
4234}
4235
4236static void hwsim_unregister_virtio_driver(void)
4237{
4238 unregister_virtio_driver(&virtio_hwsim);
4239}
4240#else
4241static inline int hwsim_register_virtio_driver(void)
4242{
4243 return 0;
4244}
4245
4246static inline void hwsim_unregister_virtio_driver(void)
4247{
4248}
4249#endif
4250
4251static int __init init_mac80211_hwsim(void)
4252{
4253 int i, err;
4254
4255 if (radios < 0 || radios > 100)
4256 return -EINVAL;
4257
4258 if (channels < 1)
4259 return -EINVAL;
4260
4261 spin_lock_init(&hwsim_radio_lock);
4262
4263 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4264 if (err)
4265 return err;
4266
4267 err = register_pernet_device(&hwsim_net_ops);
4268 if (err)
4269 goto out_free_rht;
4270
4271 err = platform_driver_register(&mac80211_hwsim_driver);
4272 if (err)
4273 goto out_unregister_pernet;
4274
4275 err = hwsim_init_netlink();
4276 if (err)
4277 goto out_unregister_driver;
4278
4279 err = hwsim_register_virtio_driver();
4280 if (err)
4281 goto out_exit_netlink;
4282
4283 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4284 if (IS_ERR(hwsim_class)) {
4285 err = PTR_ERR(hwsim_class);
4286 goto out_exit_virtio;
4287 }
4288
4289 for (i = 0; i < radios; i++) {
4290 struct hwsim_new_radio_params param = { 0 };
4291
4292 param.channels = channels;
4293
4294 switch (regtest) {
4295 case HWSIM_REGTEST_DIFF_COUNTRY:
4296 if (i < ARRAY_SIZE(hwsim_alpha2s))
4297 param.reg_alpha2 = hwsim_alpha2s[i];
4298 break;
4299 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4300 if (!i)
4301 param.reg_alpha2 = hwsim_alpha2s[0];
4302 break;
4303 case HWSIM_REGTEST_STRICT_ALL:
4304 param.reg_strict = true;
4305 /* fall through */
4306 case HWSIM_REGTEST_DRIVER_REG_ALL:
4307 param.reg_alpha2 = hwsim_alpha2s[0];
4308 break;
4309 case HWSIM_REGTEST_WORLD_ROAM:
4310 if (i == 0)
4311 param.regd = &hwsim_world_regdom_custom_01;
4312 break;
4313 case HWSIM_REGTEST_CUSTOM_WORLD:
4314 param.regd = &hwsim_world_regdom_custom_01;
4315 break;
4316 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4317 if (i == 0)
4318 param.regd = &hwsim_world_regdom_custom_01;
4319 else if (i == 1)
4320 param.regd = &hwsim_world_regdom_custom_02;
4321 break;
4322 case HWSIM_REGTEST_STRICT_FOLLOW:
4323 if (i == 0) {
4324 param.reg_strict = true;
4325 param.reg_alpha2 = hwsim_alpha2s[0];
4326 }
4327 break;
4328 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4329 if (i == 0) {
4330 param.reg_strict = true;
4331 param.reg_alpha2 = hwsim_alpha2s[0];
4332 } else if (i == 1) {
4333 param.reg_alpha2 = hwsim_alpha2s[1];
4334 }
4335 break;
4336 case HWSIM_REGTEST_ALL:
4337 switch (i) {
4338 case 0:
4339 param.regd = &hwsim_world_regdom_custom_01;
4340 break;
4341 case 1:
4342 param.regd = &hwsim_world_regdom_custom_02;
4343 break;
4344 case 2:
4345 param.reg_alpha2 = hwsim_alpha2s[0];
4346 break;
4347 case 3:
4348 param.reg_alpha2 = hwsim_alpha2s[1];
4349 break;
4350 case 4:
4351 param.reg_strict = true;
4352 param.reg_alpha2 = hwsim_alpha2s[2];
4353 break;
4354 }
4355 break;
4356 default:
4357 break;
4358 }
4359
4360 param.p2p_device = support_p2p_device;
4361 param.use_chanctx = channels > 1;
4362 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4363 if (param.p2p_device)
4364 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4365
4366 err = mac80211_hwsim_new_radio(NULL, &param);
4367 if (err < 0)
4368 goto out_free_radios;
4369 }
4370
4371 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4372 hwsim_mon_setup);
4373 if (hwsim_mon == NULL) {
4374 err = -ENOMEM;
4375 goto out_free_radios;
4376 }
4377
4378 rtnl_lock();
4379 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4380 if (err < 0) {
4381 rtnl_unlock();
4382 goto out_free_mon;
4383 }
4384
4385 err = register_netdevice(hwsim_mon);
4386 if (err < 0) {
4387 rtnl_unlock();
4388 goto out_free_mon;
4389 }
4390 rtnl_unlock();
4391
4392 return 0;
4393
4394out_free_mon:
4395 free_netdev(hwsim_mon);
4396out_free_radios:
4397 mac80211_hwsim_free();
4398out_exit_virtio:
4399 hwsim_unregister_virtio_driver();
4400out_exit_netlink:
4401 hwsim_exit_netlink();
4402out_unregister_driver:
4403 platform_driver_unregister(&mac80211_hwsim_driver);
4404out_unregister_pernet:
4405 unregister_pernet_device(&hwsim_net_ops);
4406out_free_rht:
4407 rhashtable_destroy(&hwsim_radios_rht);
4408 return err;
4409}
4410module_init(init_mac80211_hwsim);
4411
4412static void __exit exit_mac80211_hwsim(void)
4413{
4414 pr_debug("mac80211_hwsim: unregister radios\n");
4415
4416 hwsim_unregister_virtio_driver();
4417 hwsim_exit_netlink();
4418
4419 mac80211_hwsim_free();
4420
4421 rhashtable_destroy(&hwsim_radios_rht);
4422 unregister_netdev(hwsim_mon);
4423 platform_driver_unregister(&mac80211_hwsim_driver);
4424 unregister_pernet_device(&hwsim_net_ops);
4425}
4426module_exit(exit_mac80211_hwsim);