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