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