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