1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
4 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
6 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018 - 2020 Intel Corporation
8 */
9
10 /*
11 * TODO:
12 * - Add TSF sync and fix IBSS beacon transmission by adding
13 * competition for "air time" at TBTT
14 * - RX filtering based on filter configuration (data->rx_filter)
15 */
16
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <net/dst.h>
21 #include <net/xfrm.h>
22 #include <net/mac80211.h>
23 #include <net/ieee80211_radiotap.h>
24 #include <linux/if_arp.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/etherdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/debugfs.h>
29 #include <linux/module.h>
30 #include <linux/ktime.h>
31 #include <net/genetlink.h>
32 #include <net/net_namespace.h>
33 #include <net/netns/generic.h>
34 #include <linux/rhashtable.h>
35 #include <linux/nospec.h>
36 #include <linux/virtio.h>
37 #include <linux/virtio_ids.h>
38 #include <linux/virtio_config.h>
39 #include "mac80211_hwsim.h"
40
41 #define WARN_QUEUE 100
42 #define MAX_QUEUE 200
43
44 MODULE_AUTHOR("Jouni Malinen");
45 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46 MODULE_LICENSE("GPL");
47
48 static int radios = 2;
49 module_param(radios, int, 0444);
50 MODULE_PARM_DESC(radios, "Number of simulated radios");
51
52 static int channels = 1;
53 module_param(channels, int, 0444);
54 MODULE_PARM_DESC(channels, "Number of concurrent channels");
55
56 static bool paged_rx = false;
57 module_param(paged_rx, bool, 0644);
58 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
59
60 static bool rctbl = false;
61 module_param(rctbl, bool, 0444);
62 MODULE_PARM_DESC(rctbl, "Handle rate control table");
63
64 static bool support_p2p_device = true;
65 module_param(support_p2p_device, bool, 0444);
66 MODULE_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.
102 * @HWSIM_REGTEST_STRICT_FOLLOW: Used for testing strict regulatory domain
103 * 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 */
125 enum 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 */
140 static int regtest = HWSIM_REGTEST_DISABLED;
141 module_param(regtest, int, 0444);
142 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
143
144 static const char *hwsim_alpha2s[] = {
145 "FI",
146 "AL",
147 "US",
148 "DE",
149 "JP",
150 "AL",
151 };
152
153 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
154 .n_reg_rules = 5,
155 .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),
161 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
162 }
163 };
164
165 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
166 .n_reg_rules = 3,
167 .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),
172 REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
173 }
174 };
175
176 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
177 &hwsim_world_regdom_custom_01,
178 &hwsim_world_regdom_custom_02,
179 };
180
181 struct 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
hwsim_check_magic(struct ieee80211_vif * vif)191 static 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
hwsim_set_magic(struct ieee80211_vif * vif)199 static 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
hwsim_clear_magic(struct ieee80211_vif * vif)205 static 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
211 struct hwsim_sta_priv {
212 u32 magic;
213 };
214
215 #define HWSIM_STA_MAGIC 0x6d537749
216
hwsim_check_sta_magic(struct ieee80211_sta * sta)217 static 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
hwsim_set_sta_magic(struct ieee80211_sta * sta)223 static 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
hwsim_clear_sta_magic(struct ieee80211_sta * sta)229 static 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
235 struct hwsim_chanctx_priv {
236 u32 magic;
237 };
238
239 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
240
hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf * c)241 static 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
hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf * c)247 static 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
hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf * c)253 static 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
259 static unsigned int hwsim_net_id;
260
261 static DEFINE_IDA(hwsim_netgroup_ida);
262
263 struct hwsim_net {
264 int netgroup;
265 u32 wmediumd;
266 };
267
hwsim_net_get_netgroup(struct net * net)268 static 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
hwsim_net_set_netgroup(struct net * net)275 static 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
hwsim_net_get_wmediumd(struct net * net)284 static 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
hwsim_net_set_wmediumd(struct net * net,u32 portid)291 static 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
298 static struct class *hwsim_class;
299
300 static 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), \
306 }
307
308 #define CHAN5G(_freq) { \
309 .band = NL80211_BAND_5GHZ, \
310 .center_freq = (_freq), \
311 .hw_value = (_freq), \
312 }
313
314 static 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
331 static 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 */
360
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 */
378 };
379
380 #define NUM_S1G_CHANS_US 51
381 static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
382
383 static 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
hwsim_init_s1g_channels(struct ieee80211_channel * channels)410 static 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
423 static 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
438 static 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
452 #define OUI_QCA 0x001374
453 #define QCA_NL80211_SUBCMD_TEST 1
454 enum qca_nl80211_vendor_subcmds {
455 QCA_WLAN_VENDOR_ATTR_TEST = 8,
456 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
457 };
458
459 static const struct nla_policy
460 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
461 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
462 };
463
mac80211_hwsim_vendor_cmd_test(struct wiphy * wiphy,struct wireless_dev * wdev,const void * data,int data_len)464 static 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
473 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
474 data_len, hwsim_vendor_test_policy, NULL);
475 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
515 static 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,
521 .policy = hwsim_vendor_test_policy,
522 .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
523 }
524 };
525
526 /* Advertise support vendor specific events */
527 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
528 { .vendor_id = OUI_QCA, .subcmd = 1 },
529 };
530
531 static spinlock_t hwsim_radio_lock;
532 static LIST_HEAD(hwsim_radios);
533 static struct rhashtable hwsim_radios_rht;
534 static int hwsim_radio_idx;
535 static int hwsim_radios_generation = 1;
536
537 static struct platform_driver mac80211_hwsim_driver = {
538 .driver = {
539 .name = "mac80211_hwsim",
540 },
541 };
542
543 struct 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)];
551 struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
552 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
553 struct ieee80211_iface_combination if_combination;
554 struct ieee80211_iface_limit if_limits[3];
555 int n_if_limits;
556
557 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
558
559 struct mac_address addresses[2];
560 struct ieee80211_chanctx_conf *chanctx;
561 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;
589 struct hrtimer beacon_timer;
590 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
625 static 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
633 struct 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
642 struct 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 */
651 static struct genl_family hwsim_genl_family;
652
653 enum hwsim_multicast_groups {
654 HWSIM_MCGRP_CONFIG,
655 };
656
657 static const struct genl_multicast_group hwsim_mcgrps[] = {
658 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
659 };
660
661 /* MAC80211_HWSIM netlink policy */
662
663 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
664 [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
665 [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
666 [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 },
671 [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
672 .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 },
681 [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
682 [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 },
686 [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
687 [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
688 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
689 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
690 };
691
692 #if IS_REACHABLE(CONFIG_VIRTIO)
693
694 /* MAC80211_HWSIM virtio queues */
695 static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
696 static bool hwsim_virtio_enabled;
697 static spinlock_t hwsim_virtio_lock;
698
699 static void hwsim_virtio_rx_work(struct work_struct *work);
700 static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
701
hwsim_tx_virtio(struct mac80211_hwsim_data * data,struct sk_buff * skb)702 static 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
724 out_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 */
731 extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
732 struct sk_buff *skb);
733 #define hwsim_virtio_enabled false
734 #endif
735
736 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
737 struct sk_buff *skb,
738 struct ieee80211_channel *chan);
739
740 /* sysfs attributes */
hwsim_send_ps_poll(void * dat,u8 * mac,struct ieee80211_vif * vif)741 static 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
hwsim_send_nullfunc(struct mac80211_hwsim_data * data,u8 * mac,struct ieee80211_vif * vif,int ps)772 static 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
hwsim_send_nullfunc_ps(void * dat,u8 * mac,struct ieee80211_vif * vif)806 static 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
hwsim_send_nullfunc_no_ps(void * dat,u8 * mac,struct ieee80211_vif * vif)813 static 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
hwsim_fops_ps_read(void * dat,u64 * val)820 static 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
hwsim_fops_ps_write(void * dat,u64 val)827 static 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
864 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
865 "%llu\n");
866
hwsim_write_simulate_radar(void * dat,u64 val)867 static 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
876 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
877 hwsim_write_simulate_radar, "%llu\n");
878
hwsim_fops_group_read(void * dat,u64 * val)879 static 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
hwsim_fops_group_write(void * dat,u64 val)886 static 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
893 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
894 hwsim_fops_group_read, hwsim_fops_group_write,
895 "%llx\n");
896
hwsim_mon_xmit(struct sk_buff * skb,struct net_device * dev)897 static 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
mac80211_hwsim_get_tsf_raw(void)905 static inline u64 mac80211_hwsim_get_tsf_raw(void)
906 {
907 return ktime_to_us(ktime_get_real());
908 }
909
__mac80211_hwsim_get_tsf(struct mac80211_hwsim_data * data)910 static __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
mac80211_hwsim_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)916 static 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
mac80211_hwsim_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 tsf)923 static 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
mac80211_hwsim_monitor_rx(struct ieee80211_hw * hw,struct sk_buff * tx_skb,struct ieee80211_channel * chan)941 static 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;
948 u16 flags, bitrate;
949 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
950 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
951
952 if (!txrate)
953 bitrate = 0;
954 else
955 bitrate = txrate->bitrate;
956
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;
974 hdr->rt_rate = bitrate / 5;
975 hdr->rt_channel = cpu_to_le16(chan->center_freq);
976 flags = IEEE80211_CHAN_2GHZ;
977 if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
978 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
mac80211_hwsim_monitor_ack(struct ieee80211_channel * chan,const u8 * addr)993 static 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
1035 struct mac80211_hwsim_addr_match_data {
1036 u8 addr[ETH_ALEN];
1037 bool ret;
1038 };
1039
mac80211_hwsim_addr_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1040 static 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
mac80211_hwsim_addr_match(struct mac80211_hwsim_data * data,const u8 * addr)1049 static 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
hwsim_ps_rx_ok(struct mac80211_hwsim_data * data,struct sk_buff * skb)1069 static 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
hwsim_unicast_netgroup(struct mac80211_hwsim_data * data,struct sk_buff * skb,int portid)1095 static 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
mac80211_hwsim_config_mac_nl(struct ieee80211_hw * hw,const u8 * addr,bool add)1118 static 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;
1155 nla_put_failure:
1156 nlmsg_free(skb);
1157 }
1158
trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate * rate)1159 static 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
mac80211_hwsim_tx_frame_nl(struct ieee80211_hw * hw,struct sk_buff * my_skb,int dst_portid,struct ieee80211_channel * channel)1189 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1190 struct sk_buff *my_skb,
1191 int dst_portid,
1192 struct ieee80211_channel *channel)
1193 {
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
1247 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1248 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);
1279
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 }
1287
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
1294 nla_put_failure:
1295 nlmsg_free(skb);
1296 err_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
hwsim_chans_compat(struct ieee80211_channel * c1,struct ieee80211_channel * c2)1302 static 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
1311 struct tx_iter_data {
1312 struct ieee80211_channel *channel;
1313 bool receive;
1314 };
1315
mac80211_hwsim_tx_iter(void * _data,u8 * addr,struct ieee80211_vif * vif)1316 static 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
mac80211_hwsim_add_vendor_rtap(struct sk_buff * skb)1331 static 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
mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw * hw,struct sk_buff * skb,struct ieee80211_channel * chan)1378 static 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;
1392 rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1393 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;
1427 skb_ext_reset(skb);
1428 nf_reset_ct(skb);
1429
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) ||
1438 ieee80211_is_probe_resp(hdr->frame_control)) {
1439 rx_status.boottime_ns = ktime_get_boottime_ns();
1440 now = data->abs_bcn_ts;
1441 } else {
1442 now = mac80211_hwsim_get_tsf_raw();
1443 }
1444
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
mac80211_hwsim_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)1518 static 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;
1574 /* TODO: get MCS */
1575 int bitrate = 100;
1576 u64 ts;
1577
1578 mgmt = (struct ieee80211_mgmt *)skb->data;
1579 txrate = ieee80211_get_tx_rate(hw, txi);
1580 if (txrate)
1581 bitrate = txrate->bitrate;
1582 ts = mac80211_hwsim_get_tsf_raw();
1583 mgmt->u.probe_resp.timestamp =
1584 cpu_to_le64(ts + data->tsf_offset +
1585 24 * 8 * 10 / bitrate);
1586 }
1587
1588 mac80211_hwsim_monitor_rx(hw, skb, channel);
1589
1590 /* wmediumd mode check */
1591 _portid = READ_ONCE(data->wmediumd);
1592
1593 if (_portid || hwsim_virtio_enabled)
1594 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1595
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
mac80211_hwsim_start(struct ieee80211_hw * hw)1616 static 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
mac80211_hwsim_stop(struct ieee80211_hw * hw)1625 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1626 {
1627 struct mac80211_hwsim_data *data = hw->priv;
1628
1629 data->started = false;
1630 hrtimer_cancel(&data->beacon_timer);
1631
1632 while (!skb_queue_empty(&data->pending))
1633 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1634
1635 wiphy_dbg(hw->wiphy, "%s\n", __func__);
1636 }
1637
1638
mac80211_hwsim_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1639 static 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
1647 if (vif->type != NL80211_IFTYPE_MONITOR)
1648 mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1649
1650 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
mac80211_hwsim_change_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum nl80211_iftype newtype,bool newp2p)1660 static 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
mac80211_hwsim_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1681 static 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);
1689 if (vif->type != NL80211_IFTYPE_MONITOR)
1690 mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1691 }
1692
mac80211_hwsim_tx_frame(struct ieee80211_hw * hw,struct sk_buff * skb,struct ieee80211_channel * chan)1693 static 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
1709 if (_pid || hwsim_virtio_enabled)
1710 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1711
1712 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1713 dev_kfree_skb(skb);
1714 }
1715
mac80211_hwsim_beacon_tx(void * arg,u8 * mac,struct ieee80211_vif * vif)1716 static 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;
1725 /* TODO: get MCS */
1726 int bitrate = 100;
1727
1728 hwsim_check_magic(vif);
1729
1730 if (vif->type != NL80211_IFTYPE_AP &&
1731 vif->type != NL80211_IFTYPE_MESH_POINT &&
1732 vif->type != NL80211_IFTYPE_ADHOC &&
1733 vif->type != NL80211_IFTYPE_OCB)
1734 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);
1746 if (txrate)
1747 bitrate = txrate->bitrate;
1748
1749 mgmt = (struct ieee80211_mgmt *) skb->data;
1750 /* fake header transmission time */
1751 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1752 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 }
1765
1766 mac80211_hwsim_tx_frame(hw, skb,
1767 rcu_dereference(vif->chanctx_conf)->def.chan);
1768
1769 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))
1775 ieee80211_csa_finish(vif);
1776 }
1777
1778 static enum hrtimer_restart
mac80211_hwsim_beacon(struct hrtimer * timer)1779 mac80211_hwsim_beacon(struct hrtimer *timer)
1780 {
1781 struct mac80211_hwsim_data *data =
1782 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1783 struct ieee80211_hw *hw = data->hw;
1784 u64 bcn_int = data->beacon_int;
1785
1786 if (!data->started)
1787 return HRTIMER_NORESTART;
1788
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 }
1798 hrtimer_forward_now(&data->beacon_timer,
1799 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1800 return HRTIMER_RESTART;
1801 }
1802
1803 static const char * const hwsim_chanwidths[] = {
1804 [NL80211_CHAN_WIDTH_5] = "ht5",
1805 [NL80211_CHAN_WIDTH_10] = "ht10",
1806 [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",
1812 [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",
1817 };
1818
mac80211_hwsim_config(struct ieee80211_hw * hw,u32 changed)1819 static 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)
1881 hrtimer_cancel(&data->beacon_timer);
1882 else if (!hrtimer_is_queued(&data->beacon_timer)) {
1883 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
1887 hrtimer_start(&data->beacon_timer,
1888 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1889 HRTIMER_MODE_REL_SOFT);
1890 }
1891
1892 return 0;
1893 }
1894
1895
mac80211_hwsim_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)1896 static 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;
1907 if (*total_flags & FIF_MCAST_ACTION)
1908 data->rx_filter |= FIF_MCAST_ACTION;
1909
1910 *total_flags = data->rx_filter;
1911 }
1912
mac80211_hwsim_bcn_en_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1913 static 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
mac80211_hwsim_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)1923 static 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 &&
1954 !hrtimer_is_queued(&data->beacon_timer) &&
1955 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);
1962
1963 hrtimer_start(&data->beacon_timer,
1964 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1965 HRTIMER_MODE_REL_SOFT);
1966 } 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) {
1974 hrtimer_cancel(&data->beacon_timer);
1975 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
mac80211_hwsim_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)2008 static 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
mac80211_hwsim_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)2018 static 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
mac80211_hwsim_sta_notify(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)2028 static 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
mac80211_hwsim_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)2046 static 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
mac80211_hwsim_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)2054 static 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
mac80211_hwsim_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)2067 static 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 */
2109 enum 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
2119 enum 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
2126 static 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
mac80211_hwsim_testmode_cmd(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void * data,int len)2131 static 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
2140 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2141 hwsim_testmode_policy, NULL);
2142 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
mac80211_hwsim_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)2178 static 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:
2188 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2189 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
mac80211_hwsim_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)2206 static 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
hw_scan_work(struct work_struct * work)2213 static 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);
2232 mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2233 false);
2234 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
2267 local_bh_disable();
2268 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2269 hwsim->tmp_chan);
2270 local_bh_enable();
2271 }
2272 }
2273 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2274 msecs_to_jiffies(dwell));
2275 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2276 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2277 hwsim->survey_data[hwsim->scan_chan_idx].end =
2278 jiffies + msecs_to_jiffies(dwell);
2279 hwsim->scan_chan_idx++;
2280 mutex_unlock(&hwsim->mutex);
2281 }
2282
mac80211_hwsim_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)2283 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2284 struct ieee80211_vif *vif,
2285 struct ieee80211_scan_request *hw_req)
2286 {
2287 struct mac80211_hwsim_data *hwsim = hw->priv;
2288 struct cfg80211_scan_request *req = &hw_req->req;
2289
2290 mutex_lock(&hwsim->mutex);
2291 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2292 mutex_unlock(&hwsim->mutex);
2293 return -EBUSY;
2294 }
2295 hwsim->hw_scan_request = req;
2296 hwsim->hw_scan_vif = vif;
2297 hwsim->scan_chan_idx = 0;
2298 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2299 get_random_mask_addr(hwsim->scan_addr,
2300 hw_req->req.mac_addr,
2301 hw_req->req.mac_addr_mask);
2302 else
2303 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2304 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2305 mutex_unlock(&hwsim->mutex);
2306
2307 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2308 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2309
2310 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2311
2312 return 0;
2313 }
2314
mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2315 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2316 struct ieee80211_vif *vif)
2317 {
2318 struct mac80211_hwsim_data *hwsim = hw->priv;
2319 struct cfg80211_scan_info info = {
2320 .aborted = true,
2321 };
2322
2323 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2324
2325 cancel_delayed_work_sync(&hwsim->hw_scan);
2326
2327 mutex_lock(&hwsim->mutex);
2328 ieee80211_scan_completed(hwsim->hw, &info);
2329 hwsim->tmp_chan = NULL;
2330 hwsim->hw_scan_request = NULL;
2331 hwsim->hw_scan_vif = NULL;
2332 mutex_unlock(&hwsim->mutex);
2333 }
2334
mac80211_hwsim_sw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac_addr)2335 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2336 struct ieee80211_vif *vif,
2337 const u8 *mac_addr)
2338 {
2339 struct mac80211_hwsim_data *hwsim = hw->priv;
2340
2341 mutex_lock(&hwsim->mutex);
2342
2343 if (hwsim->scanning) {
2344 pr_debug("two hwsim sw_scans detected!\n");
2345 goto out;
2346 }
2347
2348 pr_debug("hwsim sw_scan request, prepping stuff\n");
2349
2350 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2351 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2352 hwsim->scanning = true;
2353 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2354
2355 out:
2356 mutex_unlock(&hwsim->mutex);
2357 }
2358
mac80211_hwsim_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2359 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2360 struct ieee80211_vif *vif)
2361 {
2362 struct mac80211_hwsim_data *hwsim = hw->priv;
2363
2364 mutex_lock(&hwsim->mutex);
2365
2366 pr_debug("hwsim sw_scan_complete\n");
2367 hwsim->scanning = false;
2368 mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2369 eth_zero_addr(hwsim->scan_addr);
2370
2371 mutex_unlock(&hwsim->mutex);
2372 }
2373
hw_roc_start(struct work_struct * work)2374 static void hw_roc_start(struct work_struct *work)
2375 {
2376 struct mac80211_hwsim_data *hwsim =
2377 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2378
2379 mutex_lock(&hwsim->mutex);
2380
2381 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2382 hwsim->tmp_chan = hwsim->roc_chan;
2383 ieee80211_ready_on_channel(hwsim->hw);
2384
2385 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2386 msecs_to_jiffies(hwsim->roc_duration));
2387
2388 mutex_unlock(&hwsim->mutex);
2389 }
2390
hw_roc_done(struct work_struct * work)2391 static void hw_roc_done(struct work_struct *work)
2392 {
2393 struct mac80211_hwsim_data *hwsim =
2394 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2395
2396 mutex_lock(&hwsim->mutex);
2397 ieee80211_remain_on_channel_expired(hwsim->hw);
2398 hwsim->tmp_chan = NULL;
2399 mutex_unlock(&hwsim->mutex);
2400
2401 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2402 }
2403
mac80211_hwsim_roc(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)2404 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2405 struct ieee80211_vif *vif,
2406 struct ieee80211_channel *chan,
2407 int duration,
2408 enum ieee80211_roc_type type)
2409 {
2410 struct mac80211_hwsim_data *hwsim = hw->priv;
2411
2412 mutex_lock(&hwsim->mutex);
2413 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2414 mutex_unlock(&hwsim->mutex);
2415 return -EBUSY;
2416 }
2417
2418 hwsim->roc_chan = chan;
2419 hwsim->roc_duration = duration;
2420 mutex_unlock(&hwsim->mutex);
2421
2422 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2423 chan->center_freq, duration);
2424 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2425
2426 return 0;
2427 }
2428
mac80211_hwsim_croc(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2429 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2430 struct ieee80211_vif *vif)
2431 {
2432 struct mac80211_hwsim_data *hwsim = hw->priv;
2433
2434 cancel_delayed_work_sync(&hwsim->roc_start);
2435 cancel_delayed_work_sync(&hwsim->roc_done);
2436
2437 mutex_lock(&hwsim->mutex);
2438 hwsim->tmp_chan = NULL;
2439 mutex_unlock(&hwsim->mutex);
2440
2441 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2442
2443 return 0;
2444 }
2445
mac80211_hwsim_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)2446 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2447 struct ieee80211_chanctx_conf *ctx)
2448 {
2449 struct mac80211_hwsim_data *hwsim = hw->priv;
2450
2451 mutex_lock(&hwsim->mutex);
2452 hwsim->chanctx = ctx;
2453 mutex_unlock(&hwsim->mutex);
2454 hwsim_set_chanctx_magic(ctx);
2455 wiphy_dbg(hw->wiphy,
2456 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2457 ctx->def.chan->center_freq, ctx->def.width,
2458 ctx->def.center_freq1, ctx->def.center_freq2);
2459 return 0;
2460 }
2461
mac80211_hwsim_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)2462 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2463 struct ieee80211_chanctx_conf *ctx)
2464 {
2465 struct mac80211_hwsim_data *hwsim = hw->priv;
2466
2467 mutex_lock(&hwsim->mutex);
2468 hwsim->chanctx = NULL;
2469 mutex_unlock(&hwsim->mutex);
2470 wiphy_dbg(hw->wiphy,
2471 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2472 ctx->def.chan->center_freq, ctx->def.width,
2473 ctx->def.center_freq1, ctx->def.center_freq2);
2474 hwsim_check_chanctx_magic(ctx);
2475 hwsim_clear_chanctx_magic(ctx);
2476 }
2477
mac80211_hwsim_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)2478 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2479 struct ieee80211_chanctx_conf *ctx,
2480 u32 changed)
2481 {
2482 struct mac80211_hwsim_data *hwsim = hw->priv;
2483
2484 mutex_lock(&hwsim->mutex);
2485 hwsim->chanctx = ctx;
2486 mutex_unlock(&hwsim->mutex);
2487 hwsim_check_chanctx_magic(ctx);
2488 wiphy_dbg(hw->wiphy,
2489 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2490 ctx->def.chan->center_freq, ctx->def.width,
2491 ctx->def.center_freq1, ctx->def.center_freq2);
2492 }
2493
mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)2494 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2495 struct ieee80211_vif *vif,
2496 struct ieee80211_chanctx_conf *ctx)
2497 {
2498 hwsim_check_magic(vif);
2499 hwsim_check_chanctx_magic(ctx);
2500
2501 return 0;
2502 }
2503
mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)2504 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2505 struct ieee80211_vif *vif,
2506 struct ieee80211_chanctx_conf *ctx)
2507 {
2508 hwsim_check_magic(vif);
2509 hwsim_check_chanctx_magic(ctx);
2510 }
2511
2512 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2513 "tx_pkts_nic",
2514 "tx_bytes_nic",
2515 "rx_pkts_nic",
2516 "rx_bytes_nic",
2517 "d_tx_dropped",
2518 "d_tx_failed",
2519 "d_ps_mode",
2520 "d_group",
2521 };
2522
2523 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2524
mac80211_hwsim_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)2525 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2526 struct ieee80211_vif *vif,
2527 u32 sset, u8 *data)
2528 {
2529 if (sset == ETH_SS_STATS)
2530 memcpy(data, *mac80211_hwsim_gstrings_stats,
2531 sizeof(mac80211_hwsim_gstrings_stats));
2532 }
2533
mac80211_hwsim_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)2534 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2535 struct ieee80211_vif *vif, int sset)
2536 {
2537 if (sset == ETH_SS_STATS)
2538 return MAC80211_HWSIM_SSTATS_LEN;
2539 return 0;
2540 }
2541
mac80211_hwsim_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)2542 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2543 struct ieee80211_vif *vif,
2544 struct ethtool_stats *stats, u64 *data)
2545 {
2546 struct mac80211_hwsim_data *ar = hw->priv;
2547 int i = 0;
2548
2549 data[i++] = ar->tx_pkts;
2550 data[i++] = ar->tx_bytes;
2551 data[i++] = ar->rx_pkts;
2552 data[i++] = ar->rx_bytes;
2553 data[i++] = ar->tx_dropped;
2554 data[i++] = ar->tx_failed;
2555 data[i++] = ar->ps;
2556 data[i++] = ar->group;
2557
2558 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2559 }
2560
mac80211_hwsim_tx_last_beacon(struct ieee80211_hw * hw)2561 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2562 {
2563 return 1;
2564 }
2565
2566 #define HWSIM_COMMON_OPS \
2567 .tx = mac80211_hwsim_tx, \
2568 .start = mac80211_hwsim_start, \
2569 .stop = mac80211_hwsim_stop, \
2570 .add_interface = mac80211_hwsim_add_interface, \
2571 .change_interface = mac80211_hwsim_change_interface, \
2572 .remove_interface = mac80211_hwsim_remove_interface, \
2573 .config = mac80211_hwsim_config, \
2574 .configure_filter = mac80211_hwsim_configure_filter, \
2575 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2576 .tx_last_beacon = mac80211_hwsim_tx_last_beacon, \
2577 .sta_add = mac80211_hwsim_sta_add, \
2578 .sta_remove = mac80211_hwsim_sta_remove, \
2579 .sta_notify = mac80211_hwsim_sta_notify, \
2580 .set_tim = mac80211_hwsim_set_tim, \
2581 .conf_tx = mac80211_hwsim_conf_tx, \
2582 .get_survey = mac80211_hwsim_get_survey, \
2583 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2584 .ampdu_action = mac80211_hwsim_ampdu_action, \
2585 .flush = mac80211_hwsim_flush, \
2586 .get_tsf = mac80211_hwsim_get_tsf, \
2587 .set_tsf = mac80211_hwsim_set_tsf, \
2588 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2589 .get_et_stats = mac80211_hwsim_get_et_stats, \
2590 .get_et_strings = mac80211_hwsim_get_et_strings,
2591
2592 static const struct ieee80211_ops mac80211_hwsim_ops = {
2593 HWSIM_COMMON_OPS
2594 .sw_scan_start = mac80211_hwsim_sw_scan,
2595 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2596 };
2597
2598 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2599 HWSIM_COMMON_OPS
2600 .hw_scan = mac80211_hwsim_hw_scan,
2601 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2602 .sw_scan_start = NULL,
2603 .sw_scan_complete = NULL,
2604 .remain_on_channel = mac80211_hwsim_roc,
2605 .cancel_remain_on_channel = mac80211_hwsim_croc,
2606 .add_chanctx = mac80211_hwsim_add_chanctx,
2607 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2608 .change_chanctx = mac80211_hwsim_change_chanctx,
2609 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2610 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2611 };
2612
2613 struct hwsim_new_radio_params {
2614 unsigned int channels;
2615 const char *reg_alpha2;
2616 const struct ieee80211_regdomain *regd;
2617 bool reg_strict;
2618 bool p2p_device;
2619 bool use_chanctx;
2620 bool destroy_on_close;
2621 const char *hwname;
2622 bool no_vif;
2623 const u8 *perm_addr;
2624 u32 iftypes;
2625 u32 *ciphers;
2626 u8 n_ciphers;
2627 };
2628
hwsim_mcast_config_msg(struct sk_buff * mcast_skb,struct genl_info * info)2629 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2630 struct genl_info *info)
2631 {
2632 if (info)
2633 genl_notify(&hwsim_genl_family, mcast_skb, info,
2634 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2635 else
2636 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2637 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2638 }
2639
append_radio_msg(struct sk_buff * skb,int id,struct hwsim_new_radio_params * param)2640 static int append_radio_msg(struct sk_buff *skb, int id,
2641 struct hwsim_new_radio_params *param)
2642 {
2643 int ret;
2644
2645 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2646 if (ret < 0)
2647 return ret;
2648
2649 if (param->channels) {
2650 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2651 if (ret < 0)
2652 return ret;
2653 }
2654
2655 if (param->reg_alpha2) {
2656 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2657 param->reg_alpha2);
2658 if (ret < 0)
2659 return ret;
2660 }
2661
2662 if (param->regd) {
2663 int i;
2664
2665 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2666 if (hwsim_world_regdom_custom[i] != param->regd)
2667 continue;
2668
2669 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2670 if (ret < 0)
2671 return ret;
2672 break;
2673 }
2674 }
2675
2676 if (param->reg_strict) {
2677 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2678 if (ret < 0)
2679 return ret;
2680 }
2681
2682 if (param->p2p_device) {
2683 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2684 if (ret < 0)
2685 return ret;
2686 }
2687
2688 if (param->use_chanctx) {
2689 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2690 if (ret < 0)
2691 return ret;
2692 }
2693
2694 if (param->hwname) {
2695 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2696 strlen(param->hwname), param->hwname);
2697 if (ret < 0)
2698 return ret;
2699 }
2700
2701 return 0;
2702 }
2703
hwsim_mcast_new_radio(int id,struct genl_info * info,struct hwsim_new_radio_params * param)2704 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2705 struct hwsim_new_radio_params *param)
2706 {
2707 struct sk_buff *mcast_skb;
2708 void *data;
2709
2710 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2711 if (!mcast_skb)
2712 return;
2713
2714 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2715 HWSIM_CMD_NEW_RADIO);
2716 if (!data)
2717 goto out_err;
2718
2719 if (append_radio_msg(mcast_skb, id, param) < 0)
2720 goto out_err;
2721
2722 genlmsg_end(mcast_skb, data);
2723
2724 hwsim_mcast_config_msg(mcast_skb, info);
2725 return;
2726
2727 out_err:
2728 nlmsg_free(mcast_skb);
2729 }
2730
2731 static const struct ieee80211_sband_iftype_data he_capa_2ghz[] = {
2732 {
2733 /* TODO: should we support other types, e.g., P2P?*/
2734 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2735 BIT(NL80211_IFTYPE_AP),
2736 .he_cap = {
2737 .has_he = true,
2738 .he_cap_elem = {
2739 .mac_cap_info[0] =
2740 IEEE80211_HE_MAC_CAP0_HTC_HE,
2741 .mac_cap_info[1] =
2742 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2743 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2744 .mac_cap_info[2] =
2745 IEEE80211_HE_MAC_CAP2_BSR |
2746 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2747 IEEE80211_HE_MAC_CAP2_ACK_EN,
2748 .mac_cap_info[3] =
2749 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2750 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2751 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2752 .phy_cap_info[1] =
2753 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2754 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2755 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2756 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2757 .phy_cap_info[2] =
2758 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2759 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2760 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2761 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2762 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2763
2764 /* Leave all the other PHY capability bytes
2765 * unset, as DCM, beam forming, RU and PPE
2766 * threshold information are not supported
2767 */
2768 },
2769 .he_mcs_nss_supp = {
2770 .rx_mcs_80 = cpu_to_le16(0xfffa),
2771 .tx_mcs_80 = cpu_to_le16(0xfffa),
2772 .rx_mcs_160 = cpu_to_le16(0xffff),
2773 .tx_mcs_160 = cpu_to_le16(0xffff),
2774 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2775 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2776 },
2777 },
2778 },
2779 #ifdef CONFIG_MAC80211_MESH
2780 {
2781 /* TODO: should we support other types, e.g., IBSS?*/
2782 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2783 .he_cap = {
2784 .has_he = true,
2785 .he_cap_elem = {
2786 .mac_cap_info[0] =
2787 IEEE80211_HE_MAC_CAP0_HTC_HE,
2788 .mac_cap_info[1] =
2789 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2790 .mac_cap_info[2] =
2791 IEEE80211_HE_MAC_CAP2_ACK_EN,
2792 .mac_cap_info[3] =
2793 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2794 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2795 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2796 .phy_cap_info[1] =
2797 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2798 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2799 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2800 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2801 .phy_cap_info[2] = 0,
2802
2803 /* Leave all the other PHY capability bytes
2804 * unset, as DCM, beam forming, RU and PPE
2805 * threshold information are not supported
2806 */
2807 },
2808 .he_mcs_nss_supp = {
2809 .rx_mcs_80 = cpu_to_le16(0xfffa),
2810 .tx_mcs_80 = cpu_to_le16(0xfffa),
2811 .rx_mcs_160 = cpu_to_le16(0xffff),
2812 .tx_mcs_160 = cpu_to_le16(0xffff),
2813 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2814 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2815 },
2816 },
2817 },
2818 #endif
2819 };
2820
2821 static const struct ieee80211_sband_iftype_data he_capa_5ghz[] = {
2822 {
2823 /* TODO: should we support other types, e.g., P2P?*/
2824 .types_mask = BIT(NL80211_IFTYPE_STATION) |
2825 BIT(NL80211_IFTYPE_AP),
2826 .he_cap = {
2827 .has_he = true,
2828 .he_cap_elem = {
2829 .mac_cap_info[0] =
2830 IEEE80211_HE_MAC_CAP0_HTC_HE,
2831 .mac_cap_info[1] =
2832 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2833 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2834 .mac_cap_info[2] =
2835 IEEE80211_HE_MAC_CAP2_BSR |
2836 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2837 IEEE80211_HE_MAC_CAP2_ACK_EN,
2838 .mac_cap_info[3] =
2839 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2840 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2841 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2842 .phy_cap_info[0] =
2843 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2844 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2845 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2846 .phy_cap_info[1] =
2847 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2848 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2849 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2850 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2851 .phy_cap_info[2] =
2852 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2853 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2854 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2855 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2856 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2857
2858 /* Leave all the other PHY capability bytes
2859 * unset, as DCM, beam forming, RU and PPE
2860 * threshold information are not supported
2861 */
2862 },
2863 .he_mcs_nss_supp = {
2864 .rx_mcs_80 = cpu_to_le16(0xfffa),
2865 .tx_mcs_80 = cpu_to_le16(0xfffa),
2866 .rx_mcs_160 = cpu_to_le16(0xfffa),
2867 .tx_mcs_160 = cpu_to_le16(0xfffa),
2868 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2869 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2870 },
2871 },
2872 },
2873 #ifdef CONFIG_MAC80211_MESH
2874 {
2875 /* TODO: should we support other types, e.g., IBSS?*/
2876 .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
2877 .he_cap = {
2878 .has_he = true,
2879 .he_cap_elem = {
2880 .mac_cap_info[0] =
2881 IEEE80211_HE_MAC_CAP0_HTC_HE,
2882 .mac_cap_info[1] =
2883 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2884 .mac_cap_info[2] =
2885 IEEE80211_HE_MAC_CAP2_ACK_EN,
2886 .mac_cap_info[3] =
2887 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2888 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
2889 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2890 .phy_cap_info[0] =
2891 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2892 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2893 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2894 .phy_cap_info[1] =
2895 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2896 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2897 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2898 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2899 .phy_cap_info[2] = 0,
2900
2901 /* Leave all the other PHY capability bytes
2902 * unset, as DCM, beam forming, RU and PPE
2903 * threshold information are not supported
2904 */
2905 },
2906 .he_mcs_nss_supp = {
2907 .rx_mcs_80 = cpu_to_le16(0xfffa),
2908 .tx_mcs_80 = cpu_to_le16(0xfffa),
2909 .rx_mcs_160 = cpu_to_le16(0xfffa),
2910 .tx_mcs_160 = cpu_to_le16(0xfffa),
2911 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2912 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2913 },
2914 },
2915 },
2916 #endif
2917 };
2918
mac80211_hwsim_he_capab(struct ieee80211_supported_band * sband)2919 static void mac80211_hwsim_he_capab(struct ieee80211_supported_band *sband)
2920 {
2921 u16 n_iftype_data;
2922
2923 if (sband->band == NL80211_BAND_2GHZ) {
2924 n_iftype_data = ARRAY_SIZE(he_capa_2ghz);
2925 sband->iftype_data =
2926 (struct ieee80211_sband_iftype_data *)he_capa_2ghz;
2927 } else if (sband->band == NL80211_BAND_5GHZ) {
2928 n_iftype_data = ARRAY_SIZE(he_capa_5ghz);
2929 sband->iftype_data =
2930 (struct ieee80211_sband_iftype_data *)he_capa_5ghz;
2931 } else {
2932 return;
2933 }
2934
2935 sband->n_iftype_data = n_iftype_data;
2936 }
2937
2938 #ifdef CONFIG_MAC80211_MESH
2939 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2940 #else
2941 #define HWSIM_MESH_BIT 0
2942 #endif
2943
2944 #define HWSIM_DEFAULT_IF_LIMIT \
2945 (BIT(NL80211_IFTYPE_STATION) | \
2946 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2947 BIT(NL80211_IFTYPE_AP) | \
2948 BIT(NL80211_IFTYPE_P2P_GO) | \
2949 HWSIM_MESH_BIT)
2950
2951 #define HWSIM_IFTYPE_SUPPORT_MASK \
2952 (BIT(NL80211_IFTYPE_STATION) | \
2953 BIT(NL80211_IFTYPE_AP) | \
2954 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2955 BIT(NL80211_IFTYPE_P2P_GO) | \
2956 BIT(NL80211_IFTYPE_ADHOC) | \
2957 BIT(NL80211_IFTYPE_MESH_POINT) | \
2958 BIT(NL80211_IFTYPE_OCB))
2959
mac80211_hwsim_new_radio(struct genl_info * info,struct hwsim_new_radio_params * param)2960 static int mac80211_hwsim_new_radio(struct genl_info *info,
2961 struct hwsim_new_radio_params *param)
2962 {
2963 int err;
2964 u8 addr[ETH_ALEN];
2965 struct mac80211_hwsim_data *data;
2966 struct ieee80211_hw *hw;
2967 enum nl80211_band band;
2968 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
2969 struct net *net;
2970 int idx, i;
2971 int n_limits = 0;
2972
2973 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
2974 return -EINVAL;
2975
2976 spin_lock_bh(&hwsim_radio_lock);
2977 idx = hwsim_radio_idx++;
2978 spin_unlock_bh(&hwsim_radio_lock);
2979
2980 if (param->use_chanctx)
2981 ops = &mac80211_hwsim_mchan_ops;
2982 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
2983 if (!hw) {
2984 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
2985 err = -ENOMEM;
2986 goto failed;
2987 }
2988
2989 /* ieee80211_alloc_hw_nm may have used a default name */
2990 param->hwname = wiphy_name(hw->wiphy);
2991
2992 if (info)
2993 net = genl_info_net(info);
2994 else
2995 net = &init_net;
2996 wiphy_net_set(hw->wiphy, net);
2997
2998 data = hw->priv;
2999 data->hw = hw;
3000
3001 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3002 if (IS_ERR(data->dev)) {
3003 printk(KERN_DEBUG
3004 "mac80211_hwsim: device_create failed (%ld)\n",
3005 PTR_ERR(data->dev));
3006 err = -ENOMEM;
3007 goto failed_drvdata;
3008 }
3009 data->dev->driver = &mac80211_hwsim_driver.driver;
3010 err = device_bind_driver(data->dev);
3011 if (err != 0) {
3012 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3013 err);
3014 goto failed_bind;
3015 }
3016
3017 skb_queue_head_init(&data->pending);
3018
3019 SET_IEEE80211_DEV(hw, data->dev);
3020 if (!param->perm_addr) {
3021 eth_zero_addr(addr);
3022 addr[0] = 0x02;
3023 addr[3] = idx >> 8;
3024 addr[4] = idx;
3025 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3026 /* Why need here second address ? */
3027 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3028 data->addresses[1].addr[0] |= 0x40;
3029 hw->wiphy->n_addresses = 2;
3030 hw->wiphy->addresses = data->addresses;
3031 /* possible address clash is checked at hash table insertion */
3032 } else {
3033 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3034 /* compatibility with automatically generated mac addr */
3035 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3036 hw->wiphy->n_addresses = 2;
3037 hw->wiphy->addresses = data->addresses;
3038 }
3039
3040 data->channels = param->channels;
3041 data->use_chanctx = param->use_chanctx;
3042 data->idx = idx;
3043 data->destroy_on_close = param->destroy_on_close;
3044 if (info)
3045 data->portid = info->snd_portid;
3046
3047 /* setup interface limits, only on interface types we support */
3048 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3049 data->if_limits[n_limits].max = 1;
3050 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3051 n_limits++;
3052 }
3053
3054 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3055 data->if_limits[n_limits].max = 2048;
3056 /*
3057 * For this case, we may only support a subset of
3058 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3059 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3060 */
3061 data->if_limits[n_limits].types =
3062 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3063 n_limits++;
3064 }
3065
3066 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3067 data->if_limits[n_limits].max = 1;
3068 data->if_limits[n_limits].types =
3069 BIT(NL80211_IFTYPE_P2P_DEVICE);
3070 n_limits++;
3071 }
3072
3073 if (data->use_chanctx) {
3074 hw->wiphy->max_scan_ssids = 255;
3075 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3076 hw->wiphy->max_remain_on_channel_duration = 1000;
3077 data->if_combination.radar_detect_widths = 0;
3078 data->if_combination.num_different_channels = data->channels;
3079 data->chanctx = NULL;
3080 } else {
3081 data->if_combination.num_different_channels = 1;
3082 data->if_combination.radar_detect_widths =
3083 BIT(NL80211_CHAN_WIDTH_5) |
3084 BIT(NL80211_CHAN_WIDTH_10) |
3085 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3086 BIT(NL80211_CHAN_WIDTH_20) |
3087 BIT(NL80211_CHAN_WIDTH_40) |
3088 BIT(NL80211_CHAN_WIDTH_80) |
3089 BIT(NL80211_CHAN_WIDTH_160);
3090 }
3091
3092 if (!n_limits) {
3093 err = -EINVAL;
3094 goto failed_hw;
3095 }
3096
3097 data->if_combination.max_interfaces = 0;
3098 for (i = 0; i < n_limits; i++)
3099 data->if_combination.max_interfaces +=
3100 data->if_limits[i].max;
3101
3102 data->if_combination.n_limits = n_limits;
3103 data->if_combination.limits = data->if_limits;
3104
3105 /*
3106 * If we actually were asked to support combinations,
3107 * advertise them - if there's only a single thing like
3108 * only IBSS then don't advertise it as combinations.
3109 */
3110 if (data->if_combination.max_interfaces > 1) {
3111 hw->wiphy->iface_combinations = &data->if_combination;
3112 hw->wiphy->n_iface_combinations = 1;
3113 }
3114
3115 if (param->ciphers) {
3116 memcpy(data->ciphers, param->ciphers,
3117 param->n_ciphers * sizeof(u32));
3118 hw->wiphy->cipher_suites = data->ciphers;
3119 hw->wiphy->n_cipher_suites = param->n_ciphers;
3120 }
3121
3122 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3123 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3124 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3125
3126 hw->queues = 5;
3127 hw->offchannel_tx_hw_queue = 4;
3128
3129 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3130 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3131 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3132 ieee80211_hw_set(hw, QUEUE_CONTROL);
3133 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3134 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3135 ieee80211_hw_set(hw, MFP_CAPABLE);
3136 ieee80211_hw_set(hw, SIGNAL_DBM);
3137 ieee80211_hw_set(hw, SUPPORTS_PS);
3138 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3139 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3140 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3141 ieee80211_hw_set(hw, TDLS_WIDER_BW);
3142 if (rctbl)
3143 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3144 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3145
3146 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3147 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3148 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3149 WIPHY_FLAG_AP_UAPSD |
3150 WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3151 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3152 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3153 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3154 NL80211_FEATURE_STATIC_SMPS |
3155 NL80211_FEATURE_DYNAMIC_SMPS |
3156 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3157 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3158 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3159 wiphy_ext_feature_set(hw->wiphy,
3160 NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3161 wiphy_ext_feature_set(hw->wiphy,
3162 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3163
3164 hw->wiphy->interface_modes = param->iftypes;
3165
3166 /* ask mac80211 to reserve space for magic */
3167 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3168 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3169 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3170
3171 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3172 sizeof(hwsim_channels_2ghz));
3173 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3174 sizeof(hwsim_channels_5ghz));
3175 memcpy(data->channels_s1g, hwsim_channels_s1g,
3176 sizeof(hwsim_channels_s1g));
3177 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3178
3179 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3180 struct ieee80211_supported_band *sband = &data->bands[band];
3181
3182 sband->band = band;
3183
3184 switch (band) {
3185 case NL80211_BAND_2GHZ:
3186 sband->channels = data->channels_2ghz;
3187 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3188 sband->bitrates = data->rates;
3189 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3190 break;
3191 case NL80211_BAND_5GHZ:
3192 sband->channels = data->channels_5ghz;
3193 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3194 sband->bitrates = data->rates + 4;
3195 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3196
3197 sband->vht_cap.vht_supported = true;
3198 sband->vht_cap.cap =
3199 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3200 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3201 IEEE80211_VHT_CAP_RXLDPC |
3202 IEEE80211_VHT_CAP_SHORT_GI_80 |
3203 IEEE80211_VHT_CAP_SHORT_GI_160 |
3204 IEEE80211_VHT_CAP_TXSTBC |
3205 IEEE80211_VHT_CAP_RXSTBC_4 |
3206 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3207 sband->vht_cap.vht_mcs.rx_mcs_map =
3208 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3209 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3210 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3211 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3212 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3213 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3214 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3215 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3216 sband->vht_cap.vht_mcs.tx_mcs_map =
3217 sband->vht_cap.vht_mcs.rx_mcs_map;
3218 break;
3219 case NL80211_BAND_S1GHZ:
3220 memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3221 sizeof(sband->s1g_cap));
3222 sband->channels = data->channels_s1g;
3223 sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3224 break;
3225 default:
3226 continue;
3227 }
3228
3229 sband->ht_cap.ht_supported = true;
3230 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3231 IEEE80211_HT_CAP_GRN_FLD |
3232 IEEE80211_HT_CAP_SGI_20 |
3233 IEEE80211_HT_CAP_SGI_40 |
3234 IEEE80211_HT_CAP_DSSSCCK40;
3235 sband->ht_cap.ampdu_factor = 0x3;
3236 sband->ht_cap.ampdu_density = 0x6;
3237 memset(&sband->ht_cap.mcs, 0,
3238 sizeof(sband->ht_cap.mcs));
3239 sband->ht_cap.mcs.rx_mask[0] = 0xff;
3240 sband->ht_cap.mcs.rx_mask[1] = 0xff;
3241 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3242
3243 mac80211_hwsim_he_capab(sband);
3244
3245 hw->wiphy->bands[band] = sband;
3246 }
3247
3248 /* By default all radios belong to the first group */
3249 data->group = 1;
3250 mutex_init(&data->mutex);
3251
3252 data->netgroup = hwsim_net_get_netgroup(net);
3253 data->wmediumd = hwsim_net_get_wmediumd(net);
3254
3255 /* Enable frame retransmissions for lossy channels */
3256 hw->max_rates = 4;
3257 hw->max_rate_tries = 11;
3258
3259 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3260 hw->wiphy->n_vendor_commands =
3261 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3262 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3263 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3264
3265 if (param->reg_strict)
3266 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3267 if (param->regd) {
3268 data->regd = param->regd;
3269 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3270 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3271 /* give the regulatory workqueue a chance to run */
3272 schedule_timeout_interruptible(1);
3273 }
3274
3275 if (param->no_vif)
3276 ieee80211_hw_set(hw, NO_AUTO_VIF);
3277
3278 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3279
3280 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3281 HRTIMER_MODE_ABS_SOFT);
3282 data->beacon_timer.function = mac80211_hwsim_beacon;
3283
3284 err = ieee80211_register_hw(hw);
3285 if (err < 0) {
3286 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3287 err);
3288 goto failed_hw;
3289 }
3290
3291 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3292
3293 if (param->reg_alpha2) {
3294 data->alpha2[0] = param->reg_alpha2[0];
3295 data->alpha2[1] = param->reg_alpha2[1];
3296 regulatory_hint(hw->wiphy, param->reg_alpha2);
3297 }
3298
3299 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3300 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3301 debugfs_create_file("group", 0666, data->debugfs, data,
3302 &hwsim_fops_group);
3303 if (!data->use_chanctx)
3304 debugfs_create_file("dfs_simulate_radar", 0222,
3305 data->debugfs,
3306 data, &hwsim_simulate_radar);
3307
3308 spin_lock_bh(&hwsim_radio_lock);
3309 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3310 hwsim_rht_params);
3311 if (err < 0) {
3312 if (info) {
3313 GENL_SET_ERR_MSG(info, "perm addr already present");
3314 NL_SET_BAD_ATTR(info->extack,
3315 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3316 }
3317 spin_unlock_bh(&hwsim_radio_lock);
3318 goto failed_final_insert;
3319 }
3320
3321 list_add_tail(&data->list, &hwsim_radios);
3322 hwsim_radios_generation++;
3323 spin_unlock_bh(&hwsim_radio_lock);
3324
3325 hwsim_mcast_new_radio(idx, info, param);
3326
3327 return idx;
3328
3329 failed_final_insert:
3330 debugfs_remove_recursive(data->debugfs);
3331 ieee80211_unregister_hw(data->hw);
3332 failed_hw:
3333 device_release_driver(data->dev);
3334 failed_bind:
3335 device_unregister(data->dev);
3336 failed_drvdata:
3337 ieee80211_free_hw(hw);
3338 failed:
3339 return err;
3340 }
3341
hwsim_mcast_del_radio(int id,const char * hwname,struct genl_info * info)3342 static void hwsim_mcast_del_radio(int id, const char *hwname,
3343 struct genl_info *info)
3344 {
3345 struct sk_buff *skb;
3346 void *data;
3347 int ret;
3348
3349 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3350 if (!skb)
3351 return;
3352
3353 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3354 HWSIM_CMD_DEL_RADIO);
3355 if (!data)
3356 goto error;
3357
3358 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3359 if (ret < 0)
3360 goto error;
3361
3362 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3363 hwname);
3364 if (ret < 0)
3365 goto error;
3366
3367 genlmsg_end(skb, data);
3368
3369 hwsim_mcast_config_msg(skb, info);
3370
3371 return;
3372
3373 error:
3374 nlmsg_free(skb);
3375 }
3376
mac80211_hwsim_del_radio(struct mac80211_hwsim_data * data,const char * hwname,struct genl_info * info)3377 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3378 const char *hwname,
3379 struct genl_info *info)
3380 {
3381 hwsim_mcast_del_radio(data->idx, hwname, info);
3382 debugfs_remove_recursive(data->debugfs);
3383 ieee80211_unregister_hw(data->hw);
3384 device_release_driver(data->dev);
3385 device_unregister(data->dev);
3386 ieee80211_free_hw(data->hw);
3387 }
3388
mac80211_hwsim_get_radio(struct sk_buff * skb,struct mac80211_hwsim_data * data,u32 portid,u32 seq,struct netlink_callback * cb,int flags)3389 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3390 struct mac80211_hwsim_data *data,
3391 u32 portid, u32 seq,
3392 struct netlink_callback *cb, int flags)
3393 {
3394 void *hdr;
3395 struct hwsim_new_radio_params param = { };
3396 int res = -EMSGSIZE;
3397
3398 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3399 HWSIM_CMD_GET_RADIO);
3400 if (!hdr)
3401 return -EMSGSIZE;
3402
3403 if (cb)
3404 genl_dump_check_consistent(cb, hdr);
3405
3406 if (data->alpha2[0] && data->alpha2[1])
3407 param.reg_alpha2 = data->alpha2;
3408
3409 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3410 REGULATORY_STRICT_REG);
3411 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3412 BIT(NL80211_IFTYPE_P2P_DEVICE));
3413 param.use_chanctx = data->use_chanctx;
3414 param.regd = data->regd;
3415 param.channels = data->channels;
3416 param.hwname = wiphy_name(data->hw->wiphy);
3417
3418 res = append_radio_msg(skb, data->idx, ¶m);
3419 if (res < 0)
3420 goto out_err;
3421
3422 genlmsg_end(skb, hdr);
3423 return 0;
3424
3425 out_err:
3426 genlmsg_cancel(skb, hdr);
3427 return res;
3428 }
3429
mac80211_hwsim_free(void)3430 static void mac80211_hwsim_free(void)
3431 {
3432 struct mac80211_hwsim_data *data;
3433
3434 spin_lock_bh(&hwsim_radio_lock);
3435 while ((data = list_first_entry_or_null(&hwsim_radios,
3436 struct mac80211_hwsim_data,
3437 list))) {
3438 list_del(&data->list);
3439 spin_unlock_bh(&hwsim_radio_lock);
3440 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3441 NULL);
3442 spin_lock_bh(&hwsim_radio_lock);
3443 }
3444 spin_unlock_bh(&hwsim_radio_lock);
3445 class_destroy(hwsim_class);
3446 }
3447
3448 static const struct net_device_ops hwsim_netdev_ops = {
3449 .ndo_start_xmit = hwsim_mon_xmit,
3450 .ndo_set_mac_address = eth_mac_addr,
3451 .ndo_validate_addr = eth_validate_addr,
3452 };
3453
hwsim_mon_setup(struct net_device * dev)3454 static void hwsim_mon_setup(struct net_device *dev)
3455 {
3456 dev->netdev_ops = &hwsim_netdev_ops;
3457 dev->needs_free_netdev = true;
3458 ether_setup(dev);
3459 dev->priv_flags |= IFF_NO_QUEUE;
3460 dev->type = ARPHRD_IEEE80211_RADIOTAP;
3461 eth_zero_addr(dev->dev_addr);
3462 dev->dev_addr[0] = 0x12;
3463 }
3464
get_hwsim_data_ref_from_addr(const u8 * addr)3465 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
3466 {
3467 return rhashtable_lookup_fast(&hwsim_radios_rht,
3468 addr,
3469 hwsim_rht_params);
3470 }
3471
hwsim_register_wmediumd(struct net * net,u32 portid)3472 static void hwsim_register_wmediumd(struct net *net, u32 portid)
3473 {
3474 struct mac80211_hwsim_data *data;
3475
3476 hwsim_net_set_wmediumd(net, portid);
3477
3478 spin_lock_bh(&hwsim_radio_lock);
3479 list_for_each_entry(data, &hwsim_radios, list) {
3480 if (data->netgroup == hwsim_net_get_netgroup(net))
3481 data->wmediumd = portid;
3482 }
3483 spin_unlock_bh(&hwsim_radio_lock);
3484 }
3485
hwsim_tx_info_frame_received_nl(struct sk_buff * skb_2,struct genl_info * info)3486 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3487 struct genl_info *info)
3488 {
3489
3490 struct ieee80211_hdr *hdr;
3491 struct mac80211_hwsim_data *data2;
3492 struct ieee80211_tx_info *txi;
3493 struct hwsim_tx_rate *tx_attempts;
3494 u64 ret_skb_cookie;
3495 struct sk_buff *skb, *tmp;
3496 const u8 *src;
3497 unsigned int hwsim_flags;
3498 int i;
3499 bool found = false;
3500
3501 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
3502 !info->attrs[HWSIM_ATTR_FLAGS] ||
3503 !info->attrs[HWSIM_ATTR_COOKIE] ||
3504 !info->attrs[HWSIM_ATTR_SIGNAL] ||
3505 !info->attrs[HWSIM_ATTR_TX_INFO])
3506 goto out;
3507
3508 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3509 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
3510 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
3511
3512 data2 = get_hwsim_data_ref_from_addr(src);
3513 if (!data2)
3514 goto out;
3515
3516 if (!hwsim_virtio_enabled) {
3517 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3518 data2->netgroup)
3519 goto out;
3520
3521 if (info->snd_portid != data2->wmediumd)
3522 goto out;
3523 }
3524
3525 /* look for the skb matching the cookie passed back from user */
3526 skb_queue_walk_safe(&data2->pending, skb, tmp) {
3527 u64 skb_cookie;
3528
3529 txi = IEEE80211_SKB_CB(skb);
3530 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3531
3532 if (skb_cookie == ret_skb_cookie) {
3533 skb_unlink(skb, &data2->pending);
3534 found = true;
3535 break;
3536 }
3537 }
3538
3539 /* not found */
3540 if (!found)
3541 goto out;
3542
3543 /* Tx info received because the frame was broadcasted on user space,
3544 so we get all the necessary info: tx attempts and skb control buff */
3545
3546 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3547 info->attrs[HWSIM_ATTR_TX_INFO]);
3548
3549 /* now send back TX status */
3550 txi = IEEE80211_SKB_CB(skb);
3551
3552 ieee80211_tx_info_clear_status(txi);
3553
3554 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3555 txi->status.rates[i].idx = tx_attempts[i].idx;
3556 txi->status.rates[i].count = tx_attempts[i].count;
3557 }
3558
3559 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3560
3561 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3562 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3563 if (skb->len >= 16) {
3564 hdr = (struct ieee80211_hdr *) skb->data;
3565 mac80211_hwsim_monitor_ack(data2->channel,
3566 hdr->addr2);
3567 }
3568 txi->flags |= IEEE80211_TX_STAT_ACK;
3569 }
3570 ieee80211_tx_status_irqsafe(data2->hw, skb);
3571 return 0;
3572 out:
3573 return -EINVAL;
3574
3575 }
3576
hwsim_cloned_frame_received_nl(struct sk_buff * skb_2,struct genl_info * info)3577 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3578 struct genl_info *info)
3579 {
3580 struct mac80211_hwsim_data *data2;
3581 struct ieee80211_rx_status rx_status;
3582 struct ieee80211_hdr *hdr;
3583 const u8 *dst;
3584 int frame_data_len;
3585 void *frame_data;
3586 struct sk_buff *skb = NULL;
3587 struct ieee80211_channel *channel = NULL;
3588
3589 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
3590 !info->attrs[HWSIM_ATTR_FRAME] ||
3591 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3592 !info->attrs[HWSIM_ATTR_SIGNAL])
3593 goto out;
3594
3595 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
3596 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
3597 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
3598
3599 /* Allocate new skb here */
3600 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3601 if (skb == NULL)
3602 goto err;
3603
3604 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
3605 goto err;
3606
3607 /* Copy the data */
3608 skb_put_data(skb, frame_data, frame_data_len);
3609
3610 data2 = get_hwsim_data_ref_from_addr(dst);
3611 if (!data2)
3612 goto out;
3613
3614 if (data2->use_chanctx) {
3615 if (data2->tmp_chan)
3616 channel = data2->tmp_chan;
3617 else if (data2->chanctx)
3618 channel = data2->chanctx->def.chan;
3619 } else {
3620 channel = data2->channel;
3621 }
3622 if (!channel)
3623 goto out;
3624
3625 if (!hwsim_virtio_enabled) {
3626 if (hwsim_net_get_netgroup(genl_info_net(info)) !=
3627 data2->netgroup)
3628 goto out;
3629
3630 if (info->snd_portid != data2->wmediumd)
3631 goto out;
3632 }
3633
3634 /* check if radio is configured properly */
3635
3636 if ((data2->idle && !data2->tmp_chan) || !data2->started)
3637 goto out;
3638
3639 /* A frame is received from user space */
3640 memset(&rx_status, 0, sizeof(rx_status));
3641 if (info->attrs[HWSIM_ATTR_FREQ]) {
3642 /* throw away off-channel packets, but allow both the temporary
3643 * ("hw" scan/remain-on-channel) and regular channel, since the
3644 * internal datapath also allows this
3645 */
3646 mutex_lock(&data2->mutex);
3647 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3648
3649 if (rx_status.freq != channel->center_freq) {
3650 mutex_unlock(&data2->mutex);
3651 goto out;
3652 }
3653 mutex_unlock(&data2->mutex);
3654 } else {
3655 rx_status.freq = channel->center_freq;
3656 }
3657
3658 rx_status.band = channel->band;
3659 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3660 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3661
3662 hdr = (void *)skb->data;
3663
3664 if (ieee80211_is_beacon(hdr->frame_control) ||
3665 ieee80211_is_probe_resp(hdr->frame_control))
3666 rx_status.boottime_ns = ktime_get_boottime_ns();
3667
3668 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
3669 data2->rx_pkts++;
3670 data2->rx_bytes += skb->len;
3671 ieee80211_rx_irqsafe(data2->hw, skb);
3672
3673 return 0;
3674 err:
3675 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
3676 out:
3677 dev_kfree_skb(skb);
3678 return -EINVAL;
3679 }
3680
hwsim_register_received_nl(struct sk_buff * skb_2,struct genl_info * info)3681 static int hwsim_register_received_nl(struct sk_buff *skb_2,
3682 struct genl_info *info)
3683 {
3684 struct net *net = genl_info_net(info);
3685 struct mac80211_hwsim_data *data;
3686 int chans = 1;
3687
3688 spin_lock_bh(&hwsim_radio_lock);
3689 list_for_each_entry(data, &hwsim_radios, list)
3690 chans = max(chans, data->channels);
3691 spin_unlock_bh(&hwsim_radio_lock);
3692
3693 /* In the future we should revise the userspace API and allow it
3694 * to set a flag that it does support multi-channel, then we can
3695 * let this pass conditionally on the flag.
3696 * For current userspace, prohibit it since it won't work right.
3697 */
3698 if (chans > 1)
3699 return -EOPNOTSUPP;
3700
3701 if (hwsim_net_get_wmediumd(net))
3702 return -EBUSY;
3703
3704 hwsim_register_wmediumd(net, info->snd_portid);
3705
3706 pr_debug("mac80211_hwsim: received a REGISTER, "
3707 "switching to wmediumd mode with pid %d\n", info->snd_portid);
3708
3709 return 0;
3710 }
3711
3712 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
hwsim_known_ciphers(const u32 * ciphers,int n_ciphers)3713 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3714 {
3715 int i;
3716
3717 for (i = 0; i < n_ciphers; i++) {
3718 int j;
3719 int found = 0;
3720
3721 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3722 if (ciphers[i] == hwsim_ciphers[j]) {
3723 found = 1;
3724 break;
3725 }
3726 }
3727
3728 if (!found)
3729 return false;
3730 }
3731
3732 return true;
3733 }
3734
hwsim_new_radio_nl(struct sk_buff * msg,struct genl_info * info)3735 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
3736 {
3737 struct hwsim_new_radio_params param = { 0 };
3738 const char *hwname = NULL;
3739 int ret;
3740
3741 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3742 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3743 param.channels = channels;
3744 param.destroy_on_close =
3745 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
3746
3747 if (info->attrs[HWSIM_ATTR_CHANNELS])
3748 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
3749
3750 if (param.channels < 1) {
3751 GENL_SET_ERR_MSG(info, "must have at least one channel");
3752 return -EINVAL;
3753 }
3754
3755 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3756 GENL_SET_ERR_MSG(info, "too many channels specified");
3757 return -EINVAL;
3758 }
3759
3760 if (info->attrs[HWSIM_ATTR_NO_VIF])
3761 param.no_vif = true;
3762
3763 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
3764 param.use_chanctx = true;
3765 else
3766 param.use_chanctx = (param.channels > 1);
3767
3768 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
3769 param.reg_alpha2 =
3770 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
3771
3772 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3773 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3774
3775 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
3776 return -EINVAL;
3777
3778 idx = array_index_nospec(idx,
3779 ARRAY_SIZE(hwsim_world_regdom_custom));
3780 param.regd = hwsim_world_regdom_custom[idx];
3781 }
3782
3783 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3784 if (!is_valid_ether_addr(
3785 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3786 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3787 NL_SET_BAD_ATTR(info->extack,
3788 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3789 return -EINVAL;
3790 }
3791
3792 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3793 }
3794
3795 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3796 param.iftypes =
3797 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3798
3799 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3800 NL_SET_ERR_MSG_ATTR(info->extack,
3801 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3802 "cannot support more iftypes than kernel");
3803 return -EINVAL;
3804 }
3805 } else {
3806 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3807 }
3808
3809 /* ensure both flag and iftype support is honored */
3810 if (param.p2p_device ||
3811 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3812 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3813 param.p2p_device = true;
3814 }
3815
3816 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3817 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3818
3819 param.ciphers =
3820 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3821
3822 if (len % sizeof(u32)) {
3823 NL_SET_ERR_MSG_ATTR(info->extack,
3824 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3825 "bad cipher list length");
3826 return -EINVAL;
3827 }
3828
3829 param.n_ciphers = len / sizeof(u32);
3830
3831 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3832 NL_SET_ERR_MSG_ATTR(info->extack,
3833 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3834 "too many ciphers specified");
3835 return -EINVAL;
3836 }
3837
3838 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3839 NL_SET_ERR_MSG_ATTR(info->extack,
3840 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3841 "unsupported ciphers specified");
3842 return -EINVAL;
3843 }
3844 }
3845
3846 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3847 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3848 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3849 GFP_KERNEL);
3850 if (!hwname)
3851 return -ENOMEM;
3852 param.hwname = hwname;
3853 }
3854
3855 ret = mac80211_hwsim_new_radio(info, ¶m);
3856 kfree(hwname);
3857 return ret;
3858 }
3859
hwsim_del_radio_nl(struct sk_buff * msg,struct genl_info * info)3860 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
3861 {
3862 struct mac80211_hwsim_data *data;
3863 s64 idx = -1;
3864 const char *hwname = NULL;
3865
3866 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
3867 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3868 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3869 hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3870 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3871 GFP_KERNEL);
3872 if (!hwname)
3873 return -ENOMEM;
3874 } else
3875 return -EINVAL;
3876
3877 spin_lock_bh(&hwsim_radio_lock);
3878 list_for_each_entry(data, &hwsim_radios, list) {
3879 if (idx >= 0) {
3880 if (data->idx != idx)
3881 continue;
3882 } else {
3883 if (!hwname ||
3884 strcmp(hwname, wiphy_name(data->hw->wiphy)))
3885 continue;
3886 }
3887
3888 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3889 continue;
3890
3891 list_del(&data->list);
3892 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3893 hwsim_rht_params);
3894 hwsim_radios_generation++;
3895 spin_unlock_bh(&hwsim_radio_lock);
3896 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3897 info);
3898 kfree(hwname);
3899 return 0;
3900 }
3901 spin_unlock_bh(&hwsim_radio_lock);
3902
3903 kfree(hwname);
3904 return -ENODEV;
3905 }
3906
hwsim_get_radio_nl(struct sk_buff * msg,struct genl_info * info)3907 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3908 {
3909 struct mac80211_hwsim_data *data;
3910 struct sk_buff *skb;
3911 int idx, res = -ENODEV;
3912
3913 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3914 return -EINVAL;
3915 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3916
3917 spin_lock_bh(&hwsim_radio_lock);
3918 list_for_each_entry(data, &hwsim_radios, list) {
3919 if (data->idx != idx)
3920 continue;
3921
3922 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3923 continue;
3924
3925 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3926 if (!skb) {
3927 res = -ENOMEM;
3928 goto out_err;
3929 }
3930
3931 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3932 info->snd_seq, NULL, 0);
3933 if (res < 0) {
3934 nlmsg_free(skb);
3935 goto out_err;
3936 }
3937
3938 res = genlmsg_reply(skb, info);
3939 break;
3940 }
3941
3942 out_err:
3943 spin_unlock_bh(&hwsim_radio_lock);
3944
3945 return res;
3946 }
3947
hwsim_dump_radio_nl(struct sk_buff * skb,struct netlink_callback * cb)3948 static int hwsim_dump_radio_nl(struct sk_buff *skb,
3949 struct netlink_callback *cb)
3950 {
3951 int last_idx = cb->args[0] - 1;
3952 struct mac80211_hwsim_data *data = NULL;
3953 int res = 0;
3954 void *hdr;
3955
3956 spin_lock_bh(&hwsim_radio_lock);
3957 cb->seq = hwsim_radios_generation;
3958
3959 if (last_idx >= hwsim_radio_idx-1)
3960 goto done;
3961
3962 list_for_each_entry(data, &hwsim_radios, list) {
3963 if (data->idx <= last_idx)
3964 continue;
3965
3966 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3967 continue;
3968
3969 res = mac80211_hwsim_get_radio(skb, data,
3970 NETLINK_CB(cb->skb).portid,
3971 cb->nlh->nlmsg_seq, cb,
3972 NLM_F_MULTI);
3973 if (res < 0)
3974 break;
3975
3976 last_idx = data->idx;
3977 }
3978
3979 cb->args[0] = last_idx + 1;
3980
3981 /* list changed, but no new element sent, set interrupted flag */
3982 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3983 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3984 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3985 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3986 if (hdr) {
3987 genl_dump_check_consistent(cb, hdr);
3988 genlmsg_end(skb, hdr);
3989 } else {
3990 res = -EMSGSIZE;
3991 }
3992 }
3993
3994 done:
3995 spin_unlock_bh(&hwsim_radio_lock);
3996 return res ?: skb->len;
3997 }
3998
3999 /* Generic Netlink operations array */
4000 static const struct genl_small_ops hwsim_ops[] = {
4001 {
4002 .cmd = HWSIM_CMD_REGISTER,
4003 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4004 .doit = hwsim_register_received_nl,
4005 .flags = GENL_UNS_ADMIN_PERM,
4006 },
4007 {
4008 .cmd = HWSIM_CMD_FRAME,
4009 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4010 .doit = hwsim_cloned_frame_received_nl,
4011 },
4012 {
4013 .cmd = HWSIM_CMD_TX_INFO_FRAME,
4014 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4015 .doit = hwsim_tx_info_frame_received_nl,
4016 },
4017 {
4018 .cmd = HWSIM_CMD_NEW_RADIO,
4019 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4020 .doit = hwsim_new_radio_nl,
4021 .flags = GENL_UNS_ADMIN_PERM,
4022 },
4023 {
4024 .cmd = HWSIM_CMD_DEL_RADIO,
4025 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4026 .doit = hwsim_del_radio_nl,
4027 .flags = GENL_UNS_ADMIN_PERM,
4028 },
4029 {
4030 .cmd = HWSIM_CMD_GET_RADIO,
4031 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4032 .doit = hwsim_get_radio_nl,
4033 .dumpit = hwsim_dump_radio_nl,
4034 },
4035 };
4036
4037 static struct genl_family hwsim_genl_family __ro_after_init = {
4038 .name = "MAC80211_HWSIM",
4039 .version = 1,
4040 .maxattr = HWSIM_ATTR_MAX,
4041 .policy = hwsim_genl_policy,
4042 .netnsok = true,
4043 .module = THIS_MODULE,
4044 .small_ops = hwsim_ops,
4045 .n_small_ops = ARRAY_SIZE(hwsim_ops),
4046 .mcgrps = hwsim_mcgrps,
4047 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4048 };
4049
remove_user_radios(u32 portid)4050 static void remove_user_radios(u32 portid)
4051 {
4052 struct mac80211_hwsim_data *entry, *tmp;
4053 LIST_HEAD(list);
4054
4055 spin_lock_bh(&hwsim_radio_lock);
4056 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4057 if (entry->destroy_on_close && entry->portid == portid) {
4058 list_move(&entry->list, &list);
4059 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4060 hwsim_rht_params);
4061 hwsim_radios_generation++;
4062 }
4063 }
4064 spin_unlock_bh(&hwsim_radio_lock);
4065
4066 list_for_each_entry_safe(entry, tmp, &list, list) {
4067 list_del(&entry->list);
4068 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4069 NULL);
4070 }
4071 }
4072
mac80211_hwsim_netlink_notify(struct notifier_block * nb,unsigned long state,void * _notify)4073 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4074 unsigned long state,
4075 void *_notify)
4076 {
4077 struct netlink_notify *notify = _notify;
4078
4079 if (state != NETLINK_URELEASE)
4080 return NOTIFY_DONE;
4081
4082 remove_user_radios(notify->portid);
4083
4084 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4085 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4086 " socket, switching to perfect channel medium\n");
4087 hwsim_register_wmediumd(notify->net, 0);
4088 }
4089 return NOTIFY_DONE;
4090
4091 }
4092
4093 static struct notifier_block hwsim_netlink_notifier = {
4094 .notifier_call = mac80211_hwsim_netlink_notify,
4095 };
4096
hwsim_init_netlink(void)4097 static int __init hwsim_init_netlink(void)
4098 {
4099 int rc;
4100
4101 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4102
4103 rc = genl_register_family(&hwsim_genl_family);
4104 if (rc)
4105 goto failure;
4106
4107 rc = netlink_register_notifier(&hwsim_netlink_notifier);
4108 if (rc) {
4109 genl_unregister_family(&hwsim_genl_family);
4110 goto failure;
4111 }
4112
4113 return 0;
4114
4115 failure:
4116 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4117 return -EINVAL;
4118 }
4119
hwsim_init_net(struct net * net)4120 static __net_init int hwsim_init_net(struct net *net)
4121 {
4122 return hwsim_net_set_netgroup(net);
4123 }
4124
hwsim_exit_net(struct net * net)4125 static void __net_exit hwsim_exit_net(struct net *net)
4126 {
4127 struct mac80211_hwsim_data *data, *tmp;
4128 LIST_HEAD(list);
4129
4130 spin_lock_bh(&hwsim_radio_lock);
4131 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4132 if (!net_eq(wiphy_net(data->hw->wiphy), net))
4133 continue;
4134
4135 /* Radios created in init_net are returned to init_net. */
4136 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4137 continue;
4138
4139 list_move(&data->list, &list);
4140 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4141 hwsim_rht_params);
4142 hwsim_radios_generation++;
4143 }
4144 spin_unlock_bh(&hwsim_radio_lock);
4145
4146 list_for_each_entry_safe(data, tmp, &list, list) {
4147 list_del(&data->list);
4148 mac80211_hwsim_del_radio(data,
4149 wiphy_name(data->hw->wiphy),
4150 NULL);
4151 }
4152
4153 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4154 }
4155
4156 static struct pernet_operations hwsim_net_ops = {
4157 .init = hwsim_init_net,
4158 .exit = hwsim_exit_net,
4159 .id = &hwsim_net_id,
4160 .size = sizeof(struct hwsim_net),
4161 };
4162
hwsim_exit_netlink(void)4163 static void hwsim_exit_netlink(void)
4164 {
4165 /* unregister the notifier */
4166 netlink_unregister_notifier(&hwsim_netlink_notifier);
4167 /* unregister the family */
4168 genl_unregister_family(&hwsim_genl_family);
4169 }
4170
4171 #if IS_REACHABLE(CONFIG_VIRTIO)
hwsim_virtio_tx_done(struct virtqueue * vq)4172 static void hwsim_virtio_tx_done(struct virtqueue *vq)
4173 {
4174 unsigned int len;
4175 struct sk_buff *skb;
4176 unsigned long flags;
4177
4178 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4179 while ((skb = virtqueue_get_buf(vq, &len)))
4180 nlmsg_free(skb);
4181 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4182 }
4183
hwsim_virtio_handle_cmd(struct sk_buff * skb)4184 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4185 {
4186 struct nlmsghdr *nlh;
4187 struct genlmsghdr *gnlh;
4188 struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4189 struct genl_info info = {};
4190 int err;
4191
4192 nlh = nlmsg_hdr(skb);
4193 gnlh = nlmsg_data(nlh);
4194 err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4195 hwsim_genl_policy, NULL);
4196 if (err) {
4197 pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4198 return err;
4199 }
4200
4201 info.attrs = tb;
4202
4203 switch (gnlh->cmd) {
4204 case HWSIM_CMD_FRAME:
4205 hwsim_cloned_frame_received_nl(skb, &info);
4206 break;
4207 case HWSIM_CMD_TX_INFO_FRAME:
4208 hwsim_tx_info_frame_received_nl(skb, &info);
4209 break;
4210 default:
4211 pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4212 return -EPROTO;
4213 }
4214 return 0;
4215 }
4216
hwsim_virtio_rx_work(struct work_struct * work)4217 static void hwsim_virtio_rx_work(struct work_struct *work)
4218 {
4219 struct virtqueue *vq;
4220 unsigned int len;
4221 struct sk_buff *skb;
4222 struct scatterlist sg[1];
4223 int err;
4224 unsigned long flags;
4225
4226 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4227 if (!hwsim_virtio_enabled)
4228 goto out_unlock;
4229
4230 skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4231 if (!skb)
4232 goto out_unlock;
4233 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4234
4235 skb->data = skb->head;
4236 skb_set_tail_pointer(skb, len);
4237 hwsim_virtio_handle_cmd(skb);
4238
4239 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4240 if (!hwsim_virtio_enabled) {
4241 nlmsg_free(skb);
4242 goto out_unlock;
4243 }
4244 vq = hwsim_vqs[HWSIM_VQ_RX];
4245 sg_init_one(sg, skb->head, skb_end_offset(skb));
4246 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4247 if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4248 nlmsg_free(skb);
4249 else
4250 virtqueue_kick(vq);
4251 schedule_work(&hwsim_virtio_rx);
4252
4253 out_unlock:
4254 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4255 }
4256
hwsim_virtio_rx_done(struct virtqueue * vq)4257 static void hwsim_virtio_rx_done(struct virtqueue *vq)
4258 {
4259 schedule_work(&hwsim_virtio_rx);
4260 }
4261
init_vqs(struct virtio_device * vdev)4262 static int init_vqs(struct virtio_device *vdev)
4263 {
4264 vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4265 [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4266 [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4267 };
4268 const char *names[HWSIM_NUM_VQS] = {
4269 [HWSIM_VQ_TX] = "tx",
4270 [HWSIM_VQ_RX] = "rx",
4271 };
4272
4273 return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4274 hwsim_vqs, callbacks, names, NULL);
4275 }
4276
fill_vq(struct virtqueue * vq)4277 static int fill_vq(struct virtqueue *vq)
4278 {
4279 int i, err;
4280 struct sk_buff *skb;
4281 struct scatterlist sg[1];
4282
4283 for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4284 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4285 if (!skb)
4286 return -ENOMEM;
4287
4288 sg_init_one(sg, skb->head, skb_end_offset(skb));
4289 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4290 if (err) {
4291 nlmsg_free(skb);
4292 return err;
4293 }
4294 }
4295 virtqueue_kick(vq);
4296 return 0;
4297 }
4298
remove_vqs(struct virtio_device * vdev)4299 static void remove_vqs(struct virtio_device *vdev)
4300 {
4301 int i;
4302
4303 vdev->config->reset(vdev);
4304
4305 for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4306 struct virtqueue *vq = hwsim_vqs[i];
4307 struct sk_buff *skb;
4308
4309 while ((skb = virtqueue_detach_unused_buf(vq)))
4310 nlmsg_free(skb);
4311 }
4312
4313 vdev->config->del_vqs(vdev);
4314 }
4315
hwsim_virtio_probe(struct virtio_device * vdev)4316 static int hwsim_virtio_probe(struct virtio_device *vdev)
4317 {
4318 int err;
4319 unsigned long flags;
4320
4321 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4322 if (hwsim_virtio_enabled) {
4323 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4324 return -EEXIST;
4325 }
4326 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4327
4328 err = init_vqs(vdev);
4329 if (err)
4330 return err;
4331
4332 err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4333 if (err)
4334 goto out_remove;
4335
4336 spin_lock_irqsave(&hwsim_virtio_lock, flags);
4337 hwsim_virtio_enabled = true;
4338 spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4339
4340 schedule_work(&hwsim_virtio_rx);
4341 return 0;
4342
4343 out_remove:
4344 remove_vqs(vdev);
4345 return err;
4346 }
4347
hwsim_virtio_remove(struct virtio_device * vdev)4348 static void hwsim_virtio_remove(struct virtio_device *vdev)
4349 {
4350 hwsim_virtio_enabled = false;
4351
4352 cancel_work_sync(&hwsim_virtio_rx);
4353
4354 remove_vqs(vdev);
4355 }
4356
4357 /* MAC80211_HWSIM virtio device id table */
4358 static const struct virtio_device_id id_table[] = {
4359 { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4360 { 0 }
4361 };
4362 MODULE_DEVICE_TABLE(virtio, id_table);
4363
4364 static struct virtio_driver virtio_hwsim = {
4365 .driver.name = KBUILD_MODNAME,
4366 .driver.owner = THIS_MODULE,
4367 .id_table = id_table,
4368 .probe = hwsim_virtio_probe,
4369 .remove = hwsim_virtio_remove,
4370 };
4371
hwsim_register_virtio_driver(void)4372 static int hwsim_register_virtio_driver(void)
4373 {
4374 spin_lock_init(&hwsim_virtio_lock);
4375
4376 return register_virtio_driver(&virtio_hwsim);
4377 }
4378
hwsim_unregister_virtio_driver(void)4379 static void hwsim_unregister_virtio_driver(void)
4380 {
4381 unregister_virtio_driver(&virtio_hwsim);
4382 }
4383 #else
hwsim_register_virtio_driver(void)4384 static inline int hwsim_register_virtio_driver(void)
4385 {
4386 return 0;
4387 }
4388
hwsim_unregister_virtio_driver(void)4389 static inline void hwsim_unregister_virtio_driver(void)
4390 {
4391 }
4392 #endif
4393
init_mac80211_hwsim(void)4394 static int __init init_mac80211_hwsim(void)
4395 {
4396 int i, err;
4397
4398 if (radios < 0 || radios > 100)
4399 return -EINVAL;
4400
4401 if (channels < 1)
4402 return -EINVAL;
4403
4404 spin_lock_init(&hwsim_radio_lock);
4405
4406 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4407 if (err)
4408 return err;
4409
4410 err = register_pernet_device(&hwsim_net_ops);
4411 if (err)
4412 goto out_free_rht;
4413
4414 err = platform_driver_register(&mac80211_hwsim_driver);
4415 if (err)
4416 goto out_unregister_pernet;
4417
4418 err = hwsim_init_netlink();
4419 if (err)
4420 goto out_unregister_driver;
4421
4422 err = hwsim_register_virtio_driver();
4423 if (err)
4424 goto out_exit_netlink;
4425
4426 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
4427 if (IS_ERR(hwsim_class)) {
4428 err = PTR_ERR(hwsim_class);
4429 goto out_exit_virtio;
4430 }
4431
4432 hwsim_init_s1g_channels(hwsim_channels_s1g);
4433
4434 for (i = 0; i < radios; i++) {
4435 struct hwsim_new_radio_params param = { 0 };
4436
4437 param.channels = channels;
4438
4439 switch (regtest) {
4440 case HWSIM_REGTEST_DIFF_COUNTRY:
4441 if (i < ARRAY_SIZE(hwsim_alpha2s))
4442 param.reg_alpha2 = hwsim_alpha2s[i];
4443 break;
4444 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
4445 if (!i)
4446 param.reg_alpha2 = hwsim_alpha2s[0];
4447 break;
4448 case HWSIM_REGTEST_STRICT_ALL:
4449 param.reg_strict = true;
4450 fallthrough;
4451 case HWSIM_REGTEST_DRIVER_REG_ALL:
4452 param.reg_alpha2 = hwsim_alpha2s[0];
4453 break;
4454 case HWSIM_REGTEST_WORLD_ROAM:
4455 if (i == 0)
4456 param.regd = &hwsim_world_regdom_custom_01;
4457 break;
4458 case HWSIM_REGTEST_CUSTOM_WORLD:
4459 param.regd = &hwsim_world_regdom_custom_01;
4460 break;
4461 case HWSIM_REGTEST_CUSTOM_WORLD_2:
4462 if (i == 0)
4463 param.regd = &hwsim_world_regdom_custom_01;
4464 else if (i == 1)
4465 param.regd = &hwsim_world_regdom_custom_02;
4466 break;
4467 case HWSIM_REGTEST_STRICT_FOLLOW:
4468 if (i == 0) {
4469 param.reg_strict = true;
4470 param.reg_alpha2 = hwsim_alpha2s[0];
4471 }
4472 break;
4473 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
4474 if (i == 0) {
4475 param.reg_strict = true;
4476 param.reg_alpha2 = hwsim_alpha2s[0];
4477 } else if (i == 1) {
4478 param.reg_alpha2 = hwsim_alpha2s[1];
4479 }
4480 break;
4481 case HWSIM_REGTEST_ALL:
4482 switch (i) {
4483 case 0:
4484 param.regd = &hwsim_world_regdom_custom_01;
4485 break;
4486 case 1:
4487 param.regd = &hwsim_world_regdom_custom_02;
4488 break;
4489 case 2:
4490 param.reg_alpha2 = hwsim_alpha2s[0];
4491 break;
4492 case 3:
4493 param.reg_alpha2 = hwsim_alpha2s[1];
4494 break;
4495 case 4:
4496 param.reg_strict = true;
4497 param.reg_alpha2 = hwsim_alpha2s[2];
4498 break;
4499 }
4500 break;
4501 default:
4502 break;
4503 }
4504
4505 param.p2p_device = support_p2p_device;
4506 param.use_chanctx = channels > 1;
4507 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4508 if (param.p2p_device)
4509 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4510
4511 err = mac80211_hwsim_new_radio(NULL, ¶m);
4512 if (err < 0)
4513 goto out_free_radios;
4514 }
4515
4516 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
4517 hwsim_mon_setup);
4518 if (hwsim_mon == NULL) {
4519 err = -ENOMEM;
4520 goto out_free_radios;
4521 }
4522
4523 rtnl_lock();
4524 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
4525 if (err < 0) {
4526 rtnl_unlock();
4527 goto out_free_mon;
4528 }
4529
4530 err = register_netdevice(hwsim_mon);
4531 if (err < 0) {
4532 rtnl_unlock();
4533 goto out_free_mon;
4534 }
4535 rtnl_unlock();
4536
4537 return 0;
4538
4539 out_free_mon:
4540 free_netdev(hwsim_mon);
4541 out_free_radios:
4542 mac80211_hwsim_free();
4543 out_exit_virtio:
4544 hwsim_unregister_virtio_driver();
4545 out_exit_netlink:
4546 hwsim_exit_netlink();
4547 out_unregister_driver:
4548 platform_driver_unregister(&mac80211_hwsim_driver);
4549 out_unregister_pernet:
4550 unregister_pernet_device(&hwsim_net_ops);
4551 out_free_rht:
4552 rhashtable_destroy(&hwsim_radios_rht);
4553 return err;
4554 }
4555 module_init(init_mac80211_hwsim);
4556
exit_mac80211_hwsim(void)4557 static void __exit exit_mac80211_hwsim(void)
4558 {
4559 pr_debug("mac80211_hwsim: unregister radios\n");
4560
4561 hwsim_unregister_virtio_driver();
4562 hwsim_exit_netlink();
4563
4564 mac80211_hwsim_free();
4565
4566 rhashtable_destroy(&hwsim_radios_rht);
4567 unregister_netdev(hwsim_mon);
4568 platform_driver_unregister(&mac80211_hwsim_driver);
4569 unregister_pernet_device(&hwsim_net_ops);
4570 }
4571 module_exit(exit_mac80211_hwsim);
4572