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