xref: /linux/drivers/net/wireless/virtual/mac80211_hwsim.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 - 2025 Intel Corporation
8  */
9 
10 /*
11  * TODO:
12  * - Add TSF sync and fix IBSS beacon transmission by adding
13  *   competition for "air time" at TBTT
14  * - RX filtering based on filter configuration (data->rx_filter)
15  */
16 
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <net/dst.h>
21 #include <net/xfrm.h>
22 #include <net/mac80211.h>
23 #include <net/ieee80211_radiotap.h>
24 #include <linux/if_arp.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/etherdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/debugfs.h>
29 #include <linux/module.h>
30 #include <linux/ktime.h>
31 #include <net/genetlink.h>
32 #include <net/net_namespace.h>
33 #include <net/netns/generic.h>
34 #include <linux/rhashtable.h>
35 #include <linux/nospec.h>
36 #include <linux/virtio.h>
37 #include <linux/virtio_ids.h>
38 #include <linux/virtio_config.h>
39 #include "mac80211_hwsim.h"
40 
41 #define WARN_QUEUE 100
42 #define MAX_QUEUE 200
43 
44 MODULE_AUTHOR("Jouni Malinen");
45 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46 MODULE_LICENSE("GPL");
47 
48 static int radios = 2;
49 module_param(radios, int, 0444);
50 MODULE_PARM_DESC(radios, "Number of simulated radios");
51 
52 static int channels = 1;
53 module_param(channels, int, 0444);
54 MODULE_PARM_DESC(channels, "Number of concurrent channels");
55 
56 static bool paged_rx = false;
57 module_param(paged_rx, bool, 0644);
58 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
59 
60 static bool rctbl = false;
61 module_param(rctbl, bool, 0444);
62 MODULE_PARM_DESC(rctbl, "Handle rate control table");
63 
64 static bool support_p2p_device = true;
65 module_param(support_p2p_device, bool, 0444);
66 MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
67 
68 static bool mlo;
69 module_param(mlo, bool, 0444);
70 MODULE_PARM_DESC(mlo, "Support MLO");
71 
72 static bool multi_radio;
73 module_param(multi_radio, bool, 0444);
74 MODULE_PARM_DESC(multi_radio, "Support Multiple Radios per wiphy");
75 
76 /**
77  * enum hwsim_regtest - the type of regulatory tests we offer
78  *
79  * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
80  * 	this is the default value.
81  * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
82  *	hint, only one driver regulatory hint will be sent as such the
83  * 	secondary radios are expected to follow.
84  * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
85  * 	request with all radios reporting the same regulatory domain.
86  * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
87  * 	different regulatory domains requests. Expected behaviour is for
88  * 	an intersection to occur but each device will still use their
89  * 	respective regulatory requested domains. Subsequent radios will
90  * 	use the resulting intersection.
91  * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
92  *	this by using a custom beacon-capable regulatory domain for the first
93  *	radio. All other device world roam.
94  * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
95  * 	domain requests. All radios will adhere to this custom world regulatory
96  * 	domain.
97  * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
98  * 	domain requests. The first radio will adhere to the first custom world
99  * 	regulatory domain, the second one to the second custom world regulatory
100  * 	domain. All other devices will world roam.
101  * @HWSIM_REGTEST_STRICT_FOLLOW: Used for testing strict regulatory domain
102  *	settings, only the first radio will send a regulatory domain request
103  *	and use strict settings. The rest of the radios are expected to follow.
104  * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
105  *	settings. All radios will adhere to this.
106  * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
107  *	domain settings, combined with secondary driver regulatory domain
108  *	settings. The first radio will get a strict regulatory domain setting
109  *	using the first driver regulatory request and the second radio will use
110  *	non-strict settings using the second driver regulatory request. All
111  *	other devices should follow the intersection created between the
112  *	first two.
113  * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
114  * 	at least 6 radios for a complete test. We will test in this order:
115  * 	1 - driver custom world regulatory domain
116  * 	2 - second custom world regulatory domain
117  * 	3 - first driver regulatory domain request
118  * 	4 - second driver regulatory domain request
119  * 	5 - strict regulatory domain settings using the third driver regulatory
120  * 	    domain request
121  * 	6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
122  * 	           regulatory requests.
123  *
124  * These are the different values you can use for the regtest
125  * module parameter. This is useful to help test world roaming
126  * and the driver regulatory_hint() call and combinations of these.
127  * If you want to do specific alpha2 regulatory domain tests simply
128  * use the userspace regulatory request as that will be respected as
129  * well without the need of this module parameter. This is designed
130  * only for testing the driver regulatory request, world roaming
131  * and all possible combinations.
132  */
133 enum hwsim_regtest {
134 	HWSIM_REGTEST_DISABLED = 0,
135 	HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
136 	HWSIM_REGTEST_DRIVER_REG_ALL = 2,
137 	HWSIM_REGTEST_DIFF_COUNTRY = 3,
138 	HWSIM_REGTEST_WORLD_ROAM = 4,
139 	HWSIM_REGTEST_CUSTOM_WORLD = 5,
140 	HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
141 	HWSIM_REGTEST_STRICT_FOLLOW = 7,
142 	HWSIM_REGTEST_STRICT_ALL = 8,
143 	HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
144 	HWSIM_REGTEST_ALL = 10,
145 };
146 
147 /* Set to one of the HWSIM_REGTEST_* values above */
148 static int regtest = HWSIM_REGTEST_DISABLED;
149 module_param(regtest, int, 0444);
150 MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
151 
152 static const char *hwsim_alpha2s[] = {
153 	"FI",
154 	"AL",
155 	"US",
156 	"DE",
157 	"JP",
158 	"AL",
159 };
160 
161 static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
162 	.n_reg_rules = 5,
163 	.alpha2 =  "99",
164 	.reg_rules = {
165 		REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
166 		REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
167 		REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
168 		REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
169 		REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
170 	}
171 };
172 
173 static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
174 	.n_reg_rules = 3,
175 	.alpha2 =  "99",
176 	.reg_rules = {
177 		REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
178 		REG_RULE(5725-10, 5850+10, 40, 0, 30,
179 			 NL80211_RRF_NO_IR),
180 		REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
181 	}
182 };
183 
184 static const struct ieee80211_regdomain hwsim_world_regdom_custom_03 = {
185 	.n_reg_rules = 6,
186 	.alpha2 =  "99",
187 	.reg_rules = {
188 		REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0),
189 		REG_RULE(2484 - 10, 2484 + 10, 40, 0, 20, 0),
190 		REG_RULE(5150 - 10, 5240 + 10, 40, 0, 30, 0),
191 		REG_RULE(5745 - 10, 5825 + 10, 40, 0, 30, 0),
192 		REG_RULE(5855 - 10, 5925 + 10, 40, 0, 33, 0),
193 		REG_RULE(5955 - 10, 7125 + 10, 320, 0, 33, 0),
194 	}
195 };
196 
197 static const struct ieee80211_regdomain hwsim_world_regdom_custom_04 = {
198 	.n_reg_rules = 6,
199 	.alpha2 =  "99",
200 	.reg_rules = {
201 		REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0),
202 		REG_RULE(2484 - 10, 2484 + 10, 40, 0, 20, 0),
203 		REG_RULE(5150 - 10, 5240 + 10, 80, 0, 30, NL80211_RRF_AUTO_BW),
204 		REG_RULE(5260 - 10, 5320 + 10, 80, 0, 30,
205 			 NL80211_RRF_DFS_CONCURRENT | NL80211_RRF_DFS |
206 			 NL80211_RRF_AUTO_BW),
207 		REG_RULE(5500 - 10, 5720 + 10, 160, 0, 30,
208 			 NL80211_RRF_DFS_CONCURRENT | NL80211_RRF_DFS),
209 		REG_RULE(5745 - 10, 5825 + 10, 80, 0, 30, 0),
210 		REG_RULE(5855 - 10, 5925 + 10, 80, 0, 33, 0),
211 	}
212 };
213 
214 static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
215 	&hwsim_world_regdom_custom_01,
216 	&hwsim_world_regdom_custom_02,
217 	&hwsim_world_regdom_custom_03,
218 	&hwsim_world_regdom_custom_04,
219 };
220 
221 struct hwsim_vif_priv {
222 	u32 magic;
223 	u32 skip_beacons[IEEE80211_MLD_MAX_NUM_LINKS];
224 	u8 bssid[ETH_ALEN];
225 	bool assoc;
226 	bool bcn_en;
227 	u16 aid;
228 };
229 
230 #define HWSIM_VIF_MAGIC	0x69537748
231 
232 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
233 {
234 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
235 	WARN(vp->magic != HWSIM_VIF_MAGIC,
236 	     "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
237 	     vif, vp->magic, vif->addr, vif->type, vif->p2p);
238 }
239 
240 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
241 {
242 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
243 	vp->magic = HWSIM_VIF_MAGIC;
244 }
245 
246 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
247 {
248 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
249 	vp->magic = 0;
250 }
251 
252 struct hwsim_sta_priv {
253 	u32 magic;
254 	unsigned int last_link;
255 	u16 active_links_rx;
256 };
257 
258 #define HWSIM_STA_MAGIC	0x6d537749
259 
260 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
261 {
262 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
263 	WARN_ON(sp->magic != HWSIM_STA_MAGIC);
264 }
265 
266 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
267 {
268 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
269 	sp->magic = HWSIM_STA_MAGIC;
270 }
271 
272 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
273 {
274 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
275 	sp->magic = 0;
276 }
277 
278 struct hwsim_chanctx_priv {
279 	u32 magic;
280 };
281 
282 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
283 
284 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
285 {
286 	struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
287 	WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
288 }
289 
290 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
291 {
292 	struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
293 	cp->magic = HWSIM_CHANCTX_MAGIC;
294 }
295 
296 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
297 {
298 	struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
299 	cp->magic = 0;
300 }
301 
302 static unsigned int hwsim_net_id;
303 
304 static DEFINE_IDA(hwsim_netgroup_ida);
305 
306 struct hwsim_net {
307 	int netgroup;
308 	u32 wmediumd;
309 };
310 
311 static inline int hwsim_net_get_netgroup(struct net *net)
312 {
313 	struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
314 
315 	return hwsim_net->netgroup;
316 }
317 
318 static inline int hwsim_net_set_netgroup(struct net *net)
319 {
320 	struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
321 
322 	hwsim_net->netgroup = ida_alloc(&hwsim_netgroup_ida, GFP_KERNEL);
323 	return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
324 }
325 
326 static inline u32 hwsim_net_get_wmediumd(struct net *net)
327 {
328 	struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
329 
330 	return hwsim_net->wmediumd;
331 }
332 
333 static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
334 {
335 	struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
336 
337 	hwsim_net->wmediumd = portid;
338 }
339 
340 static struct class *hwsim_class;
341 
342 static struct net_device *hwsim_mon; /* global monitor netdev */
343 
344 #define CHAN2G(_freq)  { \
345 	.band = NL80211_BAND_2GHZ, \
346 	.center_freq = (_freq), \
347 	.hw_value = (_freq), \
348 }
349 
350 #define CHAN5G(_freq) { \
351 	.band = NL80211_BAND_5GHZ, \
352 	.center_freq = (_freq), \
353 	.hw_value = (_freq), \
354 }
355 
356 #define CHAN6G(_freq) { \
357 	.band = NL80211_BAND_6GHZ, \
358 	.center_freq = (_freq), \
359 	.hw_value = (_freq), \
360 }
361 
362 static const struct ieee80211_channel hwsim_channels_2ghz[] = {
363 	CHAN2G(2412), /* Channel 1 */
364 	CHAN2G(2417), /* Channel 2 */
365 	CHAN2G(2422), /* Channel 3 */
366 	CHAN2G(2427), /* Channel 4 */
367 	CHAN2G(2432), /* Channel 5 */
368 	CHAN2G(2437), /* Channel 6 */
369 	CHAN2G(2442), /* Channel 7 */
370 	CHAN2G(2447), /* Channel 8 */
371 	CHAN2G(2452), /* Channel 9 */
372 	CHAN2G(2457), /* Channel 10 */
373 	CHAN2G(2462), /* Channel 11 */
374 	CHAN2G(2467), /* Channel 12 */
375 	CHAN2G(2472), /* Channel 13 */
376 	CHAN2G(2484), /* Channel 14 */
377 };
378 
379 static const struct ieee80211_channel hwsim_channels_5ghz[] = {
380 	CHAN5G(5180), /* Channel 36 */
381 	CHAN5G(5200), /* Channel 40 */
382 	CHAN5G(5220), /* Channel 44 */
383 	CHAN5G(5240), /* Channel 48 */
384 
385 	CHAN5G(5260), /* Channel 52 */
386 	CHAN5G(5280), /* Channel 56 */
387 	CHAN5G(5300), /* Channel 60 */
388 	CHAN5G(5320), /* Channel 64 */
389 
390 	CHAN5G(5500), /* Channel 100 */
391 	CHAN5G(5520), /* Channel 104 */
392 	CHAN5G(5540), /* Channel 108 */
393 	CHAN5G(5560), /* Channel 112 */
394 	CHAN5G(5580), /* Channel 116 */
395 	CHAN5G(5600), /* Channel 120 */
396 	CHAN5G(5620), /* Channel 124 */
397 	CHAN5G(5640), /* Channel 128 */
398 	CHAN5G(5660), /* Channel 132 */
399 	CHAN5G(5680), /* Channel 136 */
400 	CHAN5G(5700), /* Channel 140 */
401 
402 	CHAN5G(5745), /* Channel 149 */
403 	CHAN5G(5765), /* Channel 153 */
404 	CHAN5G(5785), /* Channel 157 */
405 	CHAN5G(5805), /* Channel 161 */
406 	CHAN5G(5825), /* Channel 165 */
407 	CHAN5G(5845), /* Channel 169 */
408 
409 	CHAN5G(5855), /* Channel 171 */
410 	CHAN5G(5860), /* Channel 172 */
411 	CHAN5G(5865), /* Channel 173 */
412 	CHAN5G(5870), /* Channel 174 */
413 
414 	CHAN5G(5875), /* Channel 175 */
415 	CHAN5G(5880), /* Channel 176 */
416 	CHAN5G(5885), /* Channel 177 */
417 	CHAN5G(5890), /* Channel 178 */
418 	CHAN5G(5895), /* Channel 179 */
419 	CHAN5G(5900), /* Channel 180 */
420 	CHAN5G(5905), /* Channel 181 */
421 
422 	CHAN5G(5910), /* Channel 182 */
423 	CHAN5G(5915), /* Channel 183 */
424 	CHAN5G(5920), /* Channel 184 */
425 	CHAN5G(5925), /* Channel 185 */
426 };
427 
428 static const struct ieee80211_channel hwsim_channels_6ghz[] = {
429 	CHAN6G(5955), /* Channel 1 */
430 	CHAN6G(5975), /* Channel 5 */
431 	CHAN6G(5995), /* Channel 9 */
432 	CHAN6G(6015), /* Channel 13 */
433 	CHAN6G(6035), /* Channel 17 */
434 	CHAN6G(6055), /* Channel 21 */
435 	CHAN6G(6075), /* Channel 25 */
436 	CHAN6G(6095), /* Channel 29 */
437 	CHAN6G(6115), /* Channel 33 */
438 	CHAN6G(6135), /* Channel 37 */
439 	CHAN6G(6155), /* Channel 41 */
440 	CHAN6G(6175), /* Channel 45 */
441 	CHAN6G(6195), /* Channel 49 */
442 	CHAN6G(6215), /* Channel 53 */
443 	CHAN6G(6235), /* Channel 57 */
444 	CHAN6G(6255), /* Channel 61 */
445 	CHAN6G(6275), /* Channel 65 */
446 	CHAN6G(6295), /* Channel 69 */
447 	CHAN6G(6315), /* Channel 73 */
448 	CHAN6G(6335), /* Channel 77 */
449 	CHAN6G(6355), /* Channel 81 */
450 	CHAN6G(6375), /* Channel 85 */
451 	CHAN6G(6395), /* Channel 89 */
452 	CHAN6G(6415), /* Channel 93 */
453 	CHAN6G(6435), /* Channel 97 */
454 	CHAN6G(6455), /* Channel 181 */
455 	CHAN6G(6475), /* Channel 105 */
456 	CHAN6G(6495), /* Channel 109 */
457 	CHAN6G(6515), /* Channel 113 */
458 	CHAN6G(6535), /* Channel 117 */
459 	CHAN6G(6555), /* Channel 121 */
460 	CHAN6G(6575), /* Channel 125 */
461 	CHAN6G(6595), /* Channel 129 */
462 	CHAN6G(6615), /* Channel 133 */
463 	CHAN6G(6635), /* Channel 137 */
464 	CHAN6G(6655), /* Channel 141 */
465 	CHAN6G(6675), /* Channel 145 */
466 	CHAN6G(6695), /* Channel 149 */
467 	CHAN6G(6715), /* Channel 153 */
468 	CHAN6G(6735), /* Channel 157 */
469 	CHAN6G(6755), /* Channel 161 */
470 	CHAN6G(6775), /* Channel 165 */
471 	CHAN6G(6795), /* Channel 169 */
472 	CHAN6G(6815), /* Channel 173 */
473 	CHAN6G(6835), /* Channel 177 */
474 	CHAN6G(6855), /* Channel 181 */
475 	CHAN6G(6875), /* Channel 185 */
476 	CHAN6G(6895), /* Channel 189 */
477 	CHAN6G(6915), /* Channel 193 */
478 	CHAN6G(6935), /* Channel 197 */
479 	CHAN6G(6955), /* Channel 201 */
480 	CHAN6G(6975), /* Channel 205 */
481 	CHAN6G(6995), /* Channel 209 */
482 	CHAN6G(7015), /* Channel 213 */
483 	CHAN6G(7035), /* Channel 217 */
484 	CHAN6G(7055), /* Channel 221 */
485 	CHAN6G(7075), /* Channel 225 */
486 	CHAN6G(7095), /* Channel 229 */
487 	CHAN6G(7115), /* Channel 233 */
488 };
489 
490 #define NUM_S1G_CHANS_US 51
491 static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
492 
493 static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
494 	.s1g = true,
495 	.cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
496 		 0,
497 		 0,
498 		 S1G_CAP3_MAX_MPDU_LEN,
499 		 0,
500 		 S1G_CAP5_AMPDU,
501 		 0,
502 		 S1G_CAP7_DUP_1MHZ,
503 		 S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
504 		 0},
505 	.nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
506 	/* RX Highest Supported Long GI Data Rate 0:7 */
507 		     0,
508 	/* RX Highest Supported Long GI Data Rate 0:7 */
509 	/* TX S1G MCS Map 0:6 */
510 		     0xfa,
511 	/* TX S1G MCS Map :7 */
512 	/* TX Highest Supported Long GI Data Rate 0:6 */
513 		     0x80,
514 	/* TX Highest Supported Long GI Data Rate 7:8 */
515 	/* Rx Single spatial stream and S1G-MCS Map for 1MHz */
516 	/* Tx Single spatial stream and S1G-MCS Map for 1MHz */
517 		     0 },
518 };
519 
520 static void hwsim_init_s1g_channels(struct ieee80211_channel *chans)
521 {
522 	int ch, freq;
523 
524 	for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
525 		freq = 902000 + (ch + 1) * 500;
526 		chans[ch].band = NL80211_BAND_S1GHZ;
527 		chans[ch].center_freq = KHZ_TO_MHZ(freq);
528 		chans[ch].freq_offset = freq % 1000;
529 		chans[ch].hw_value = ch + 1;
530 	}
531 }
532 
533 static const struct ieee80211_rate hwsim_rates[] = {
534 	{ .bitrate = 10 },
535 	{ .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
536 	{ .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
537 	{ .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
538 	{ .bitrate = 60 },
539 	{ .bitrate = 90 },
540 	{ .bitrate = 120 },
541 	{ .bitrate = 180 },
542 	{ .bitrate = 240 },
543 	{ .bitrate = 360 },
544 	{ .bitrate = 480 },
545 	{ .bitrate = 540 }
546 };
547 
548 #define DEFAULT_RX_RSSI -50
549 
550 static const u32 hwsim_ciphers[] = {
551 	WLAN_CIPHER_SUITE_WEP40,
552 	WLAN_CIPHER_SUITE_WEP104,
553 	WLAN_CIPHER_SUITE_TKIP,
554 	WLAN_CIPHER_SUITE_CCMP,
555 	WLAN_CIPHER_SUITE_CCMP_256,
556 	WLAN_CIPHER_SUITE_GCMP,
557 	WLAN_CIPHER_SUITE_GCMP_256,
558 	WLAN_CIPHER_SUITE_AES_CMAC,
559 	WLAN_CIPHER_SUITE_BIP_CMAC_256,
560 	WLAN_CIPHER_SUITE_BIP_GMAC_128,
561 	WLAN_CIPHER_SUITE_BIP_GMAC_256,
562 };
563 
564 #define OUI_QCA 0x001374
565 #define QCA_NL80211_SUBCMD_TEST 1
566 enum qca_nl80211_vendor_subcmds {
567 	QCA_WLAN_VENDOR_ATTR_TEST = 8,
568 	QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
569 };
570 
571 static const struct nla_policy
572 hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
573 	[QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
574 };
575 
576 static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
577 					  struct wireless_dev *wdev,
578 					  const void *data, int data_len)
579 {
580 	struct sk_buff *skb;
581 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
582 	int err;
583 	u32 val;
584 
585 	err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
586 				   data_len, hwsim_vendor_test_policy, NULL);
587 	if (err)
588 		return err;
589 	if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
590 		return -EINVAL;
591 	val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
592 	wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
593 
594 	/* Send a vendor event as a test. Note that this would not normally be
595 	 * done within a command handler, but rather, based on some other
596 	 * trigger. For simplicity, this command is used to trigger the event
597 	 * here.
598 	 *
599 	 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
600 	 */
601 	skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
602 	if (skb) {
603 		/* skb_put() or nla_put() will fill up data within
604 		 * NL80211_ATTR_VENDOR_DATA.
605 		 */
606 
607 		/* Add vendor data */
608 		nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
609 
610 		/* Send the event - this will call nla_nest_end() */
611 		cfg80211_vendor_event(skb, GFP_KERNEL);
612 	}
613 
614 	/* Send a response to the command */
615 	skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
616 	if (!skb)
617 		return -ENOMEM;
618 
619 	/* skb_put() or nla_put() will fill up data within
620 	 * NL80211_ATTR_VENDOR_DATA
621 	 */
622 	nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
623 
624 	return cfg80211_vendor_cmd_reply(skb);
625 }
626 
627 static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
628 	{
629 		.info = { .vendor_id = OUI_QCA,
630 			  .subcmd = QCA_NL80211_SUBCMD_TEST },
631 		.flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
632 		.doit = mac80211_hwsim_vendor_cmd_test,
633 		.policy = hwsim_vendor_test_policy,
634 		.maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
635 	}
636 };
637 
638 /* Advertise support vendor specific events */
639 static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
640 	{ .vendor_id = OUI_QCA, .subcmd = 1 },
641 };
642 
643 static DEFINE_SPINLOCK(hwsim_radio_lock);
644 static LIST_HEAD(hwsim_radios);
645 static struct rhashtable hwsim_radios_rht;
646 static int hwsim_radio_idx;
647 static int hwsim_radios_generation = 1;
648 static u8 hwsim_nan_cluster_id[ETH_ALEN];
649 
650 static struct platform_driver mac80211_hwsim_driver = {
651 	.driver = {
652 		.name = "mac80211_hwsim",
653 	},
654 };
655 
656 struct mac80211_hwsim_link_data {
657 	u32 link_id;
658 	u64 beacon_int	/* beacon interval in us */;
659 	struct hrtimer beacon_timer;
660 };
661 
662 struct mac80211_hwsim_data {
663 	struct list_head list;
664 	struct rhash_head rht;
665 	struct ieee80211_hw *hw;
666 	struct device *dev;
667 	struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
668 	struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
669 	struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
670 	struct ieee80211_channel channels_6ghz[ARRAY_SIZE(hwsim_channels_6ghz)];
671 	struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
672 	struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
673 	struct ieee80211_iface_combination if_combination;
674 	struct ieee80211_iface_limit if_limits[4];
675 	int n_if_limits;
676 
677 	struct ieee80211_iface_combination if_combination_radio;
678 	struct wiphy_radio_freq_range radio_range[NUM_NL80211_BANDS];
679 	struct wiphy_radio radio[NUM_NL80211_BANDS];
680 
681 	u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
682 
683 	struct mac_address addresses[3];
684 	int channels, idx;
685 	bool use_chanctx;
686 	bool destroy_on_close;
687 	u32 portid;
688 	char alpha2[2];
689 	const struct ieee80211_regdomain *regd;
690 
691 	struct ieee80211_channel *tmp_chan;
692 	struct ieee80211_channel *roc_chan;
693 	u32 roc_duration;
694 	struct delayed_work roc_start;
695 	struct delayed_work roc_done;
696 	struct delayed_work hw_scan;
697 	struct cfg80211_scan_request *hw_scan_request;
698 	struct ieee80211_vif *hw_scan_vif;
699 	int scan_chan_idx;
700 	u8 scan_addr[ETH_ALEN];
701 	struct {
702 		struct ieee80211_channel *channel;
703 		unsigned long next_start, start, end;
704 	} survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
705 		      ARRAY_SIZE(hwsim_channels_5ghz) +
706 		      ARRAY_SIZE(hwsim_channels_6ghz)];
707 
708 	struct ieee80211_channel *channel;
709 	enum nl80211_chan_width bw;
710 	unsigned int rx_filter;
711 	bool started, idle, scanning;
712 	struct mutex mutex;
713 	enum ps_mode {
714 		PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
715 	} ps;
716 	bool ps_poll_pending;
717 	struct dentry *debugfs;
718 
719 	atomic_t pending_cookie;
720 	struct sk_buff_head pending;	/* packets pending */
721 	/*
722 	 * Only radios in the same group can communicate together (the
723 	 * channel has to match too). Each bit represents a group. A
724 	 * radio can be in more than one group.
725 	 */
726 	u64 group;
727 
728 	/* group shared by radios created in the same netns */
729 	int netgroup;
730 	/* wmediumd portid responsible for netgroup of this radio */
731 	u32 wmediumd;
732 
733 	/* difference between this hw's clock and the real clock, in usecs */
734 	s64 tsf_offset;
735 	s64 bcn_delta;
736 	/* absolute beacon transmission time. Used to cover up "tx" delay. */
737 	u64 abs_bcn_ts;
738 
739 	/* Stats */
740 	u64 tx_pkts;
741 	u64 rx_pkts;
742 	u64 tx_bytes;
743 	u64 rx_bytes;
744 	u64 tx_dropped;
745 	u64 tx_failed;
746 
747 	/* RSSI in rx status of the receiver */
748 	int rx_rssi;
749 
750 	/* only used when pmsr capability is supplied */
751 	struct cfg80211_pmsr_capabilities pmsr_capa;
752 	struct cfg80211_pmsr_request *pmsr_request;
753 	struct wireless_dev *pmsr_request_wdev;
754 
755 	struct mac80211_hwsim_link_data link_data[IEEE80211_MLD_MAX_NUM_LINKS];
756 
757 	struct ieee80211_vif *nan_device_vif;
758 	u8 nan_bands;
759 
760 	enum nl80211_band nan_curr_dw_band;
761 	struct hrtimer nan_timer;
762 	bool notify_dw;
763 	struct ieee80211_vif *nan_vif;
764 };
765 
766 static const struct rhashtable_params hwsim_rht_params = {
767 	.nelem_hint = 2,
768 	.automatic_shrinking = true,
769 	.key_len = ETH_ALEN,
770 	.key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
771 	.head_offset = offsetof(struct mac80211_hwsim_data, rht),
772 };
773 
774 struct hwsim_radiotap_hdr {
775 	struct ieee80211_radiotap_header_fixed hdr;
776 	__le64 rt_tsft;
777 	u8 rt_flags;
778 	u8 rt_rate;
779 	__le16 rt_channel;
780 	__le16 rt_chbitmask;
781 } __packed;
782 
783 struct hwsim_radiotap_ack_hdr {
784 	struct ieee80211_radiotap_header_fixed hdr;
785 	u8 rt_flags;
786 	u8 pad;
787 	__le16 rt_channel;
788 	__le16 rt_chbitmask;
789 } __packed;
790 
791 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
792 {
793 	return rhashtable_lookup_fast(&hwsim_radios_rht, addr, hwsim_rht_params);
794 }
795 
796 /* MAC80211_HWSIM netlink family */
797 static struct genl_family hwsim_genl_family;
798 
799 enum hwsim_multicast_groups {
800 	HWSIM_MCGRP_CONFIG,
801 };
802 
803 static const struct genl_multicast_group hwsim_mcgrps[] = {
804 	[HWSIM_MCGRP_CONFIG] = { .name = "config", },
805 };
806 
807 /* MAC80211_HWSIM netlink policy */
808 
809 static const struct nla_policy
810 hwsim_rate_info_policy[HWSIM_RATE_INFO_ATTR_MAX + 1] = {
811 	[HWSIM_RATE_INFO_ATTR_FLAGS] = { .type = NLA_U8 },
812 	[HWSIM_RATE_INFO_ATTR_MCS] = { .type = NLA_U8 },
813 	[HWSIM_RATE_INFO_ATTR_LEGACY] = { .type = NLA_U16 },
814 	[HWSIM_RATE_INFO_ATTR_NSS] = { .type = NLA_U8 },
815 	[HWSIM_RATE_INFO_ATTR_BW] = { .type = NLA_U8 },
816 	[HWSIM_RATE_INFO_ATTR_HE_GI] = { .type = NLA_U8 },
817 	[HWSIM_RATE_INFO_ATTR_HE_DCM] = { .type = NLA_U8 },
818 	[HWSIM_RATE_INFO_ATTR_HE_RU_ALLOC] = { .type = NLA_U8 },
819 	[HWSIM_RATE_INFO_ATTR_N_BOUNDED_CH] = { .type = NLA_U8 },
820 	[HWSIM_RATE_INFO_ATTR_EHT_GI] = { .type = NLA_U8 },
821 	[HWSIM_RATE_INFO_ATTR_EHT_RU_ALLOC] = { .type = NLA_U8 },
822 };
823 
824 static const struct nla_policy
825 hwsim_ftm_result_policy[NL80211_PMSR_FTM_RESP_ATTR_MAX + 1] = {
826 	[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON] = { .type = NLA_U32 },
827 	[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX] = { .type = NLA_U16 },
828 	[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS] = { .type = NLA_U32 },
829 	[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES] = { .type = NLA_U32 },
830 	[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME] = { .type = NLA_U8 },
831 	[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP] = { .type = NLA_U8 },
832 	[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION] = { .type = NLA_U8 },
833 	[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST] = { .type = NLA_U8 },
834 	[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG] = { .type = NLA_U32 },
835 	[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD] = { .type = NLA_U32 },
836 	[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE] = NLA_POLICY_NESTED(hwsim_rate_info_policy),
837 	[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE] = NLA_POLICY_NESTED(hwsim_rate_info_policy),
838 	[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG] = { .type = NLA_U64 },
839 	[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE] = { .type = NLA_U64 },
840 	[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD] = { .type = NLA_U64 },
841 	[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG] = { .type = NLA_U64 },
842 	[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE] = { .type = NLA_U64 },
843 	[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD] = { .type = NLA_U64 },
844 	[NL80211_PMSR_FTM_RESP_ATTR_LCI] = { .type = NLA_STRING },
845 	[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_STRING },
846 };
847 
848 static const struct nla_policy
849 hwsim_pmsr_resp_type_policy[NL80211_PMSR_TYPE_MAX + 1] = {
850 	[NL80211_PMSR_TYPE_FTM] = NLA_POLICY_NESTED(hwsim_ftm_result_policy),
851 };
852 
853 static const struct nla_policy
854 hwsim_pmsr_resp_policy[NL80211_PMSR_RESP_ATTR_MAX + 1] = {
855 	[NL80211_PMSR_RESP_ATTR_STATUS] = { .type = NLA_U32 },
856 	[NL80211_PMSR_RESP_ATTR_HOST_TIME] = { .type = NLA_U64 },
857 	[NL80211_PMSR_RESP_ATTR_AP_TSF] = { .type = NLA_U64 },
858 	[NL80211_PMSR_RESP_ATTR_FINAL] = { .type = NLA_FLAG },
859 	[NL80211_PMSR_RESP_ATTR_DATA] = NLA_POLICY_NESTED(hwsim_pmsr_resp_type_policy),
860 };
861 
862 static const struct nla_policy
863 hwsim_pmsr_peer_result_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
864 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
865 	[NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_REJECT },
866 	[NL80211_PMSR_PEER_ATTR_REQ] = { .type = NLA_REJECT },
867 	[NL80211_PMSR_PEER_ATTR_RESP] = NLA_POLICY_NESTED(hwsim_pmsr_resp_policy),
868 };
869 
870 static const struct nla_policy
871 hwsim_pmsr_peers_result_policy[NL80211_PMSR_ATTR_MAX + 1] = {
872 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
873 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
874 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
875 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
876 	[NL80211_PMSR_ATTR_PEERS] = NLA_POLICY_NESTED_ARRAY(hwsim_pmsr_peer_result_policy),
877 };
878 
879 static const struct nla_policy
880 hwsim_ftm_capa_policy[NL80211_PMSR_FTM_CAPA_ATTR_MAX + 1] = {
881 	[NL80211_PMSR_FTM_CAPA_ATTR_ASAP] = { .type = NLA_FLAG },
882 	[NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP] = { .type = NLA_FLAG },
883 	[NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI] = { .type = NLA_FLAG },
884 	[NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC] = { .type = NLA_FLAG },
885 	[NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES] = { .type = NLA_U32 },
886 	[NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS] = { .type = NLA_U32 },
887 	[NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT] = NLA_POLICY_MAX(NLA_U8, 15),
888 	[NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST] = NLA_POLICY_MAX(NLA_U8, 31),
889 	[NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
890 	[NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
891 };
892 
893 static const struct nla_policy
894 hwsim_pmsr_capa_type_policy[NL80211_PMSR_TYPE_MAX + 1] = {
895 	[NL80211_PMSR_TYPE_FTM] = NLA_POLICY_NESTED(hwsim_ftm_capa_policy),
896 };
897 
898 static const struct nla_policy
899 hwsim_pmsr_capa_policy[NL80211_PMSR_ATTR_MAX + 1] = {
900 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_U32 },
901 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_FLAG },
902 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_FLAG },
903 	[NL80211_PMSR_ATTR_TYPE_CAPA] = NLA_POLICY_NESTED(hwsim_pmsr_capa_type_policy),
904 	[NL80211_PMSR_ATTR_PEERS] = { .type = NLA_REJECT }, // only for request.
905 };
906 
907 static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
908 	[HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
909 	[HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
910 	[HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
911 			       .len = IEEE80211_MAX_DATA_LEN },
912 	[HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
913 	[HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
914 	[HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
915 	[HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
916 				 .len = IEEE80211_TX_MAX_RATES *
917 					sizeof(struct hwsim_tx_rate)},
918 	[HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
919 	[HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
920 	[HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
921 	[HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
922 	[HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
923 	[HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
924 	[HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
925 	[HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
926 	[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
927 	[HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
928 	[HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
929 	[HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
930 	[HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
931 	[HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
932 	[HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
933 	[HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
934 	[HWSIM_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
935 	[HWSIM_ATTR_PMSR_SUPPORT] = NLA_POLICY_NESTED(hwsim_pmsr_capa_policy),
936 	[HWSIM_ATTR_PMSR_RESULT] = NLA_POLICY_NESTED(hwsim_pmsr_peers_result_policy),
937 	[HWSIM_ATTR_MULTI_RADIO] = { .type = NLA_FLAG },
938 	[HWSIM_ATTR_SUPPORT_NAN_DEVICE] = { .type = NLA_FLAG },
939 };
940 
941 #if IS_REACHABLE(CONFIG_VIRTIO)
942 
943 /* MAC80211_HWSIM virtio queues */
944 static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
945 static bool hwsim_virtio_enabled;
946 static DEFINE_SPINLOCK(hwsim_virtio_lock);
947 
948 static void hwsim_virtio_rx_work(struct work_struct *work);
949 static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
950 
951 static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
952 			   struct sk_buff *skb)
953 {
954 	struct scatterlist sg[1];
955 	unsigned long flags;
956 	int err;
957 
958 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
959 	if (!hwsim_virtio_enabled) {
960 		err = -ENODEV;
961 		goto out_free;
962 	}
963 
964 	sg_init_one(sg, skb->head, skb_end_offset(skb));
965 	err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
966 				   GFP_ATOMIC);
967 	if (err)
968 		goto out_free;
969 	virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
970 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
971 	return 0;
972 
973 out_free:
974 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
975 	nlmsg_free(skb);
976 	return err;
977 }
978 #else
979 /* cause a linker error if this ends up being needed */
980 extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
981 			   struct sk_buff *skb);
982 #define hwsim_virtio_enabled false
983 #endif
984 
985 static int hwsim_get_chanwidth(enum nl80211_chan_width bw)
986 {
987 	switch (bw) {
988 	case NL80211_CHAN_WIDTH_20_NOHT:
989 	case NL80211_CHAN_WIDTH_20:
990 		return 20;
991 	case NL80211_CHAN_WIDTH_40:
992 		return 40;
993 	case NL80211_CHAN_WIDTH_80:
994 		return 80;
995 	case NL80211_CHAN_WIDTH_80P80:
996 	case NL80211_CHAN_WIDTH_160:
997 		return 160;
998 	case NL80211_CHAN_WIDTH_320:
999 		return 320;
1000 	case NL80211_CHAN_WIDTH_5:
1001 		return 5;
1002 	case NL80211_CHAN_WIDTH_10:
1003 		return 10;
1004 	case NL80211_CHAN_WIDTH_1:
1005 		return 1;
1006 	case NL80211_CHAN_WIDTH_2:
1007 		return 2;
1008 	case NL80211_CHAN_WIDTH_4:
1009 		return 4;
1010 	case NL80211_CHAN_WIDTH_8:
1011 		return 8;
1012 	case NL80211_CHAN_WIDTH_16:
1013 		return 16;
1014 	}
1015 
1016 	return INT_MAX;
1017 }
1018 
1019 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1020 				    struct sk_buff *skb,
1021 				    struct ieee80211_channel *chan);
1022 
1023 /* sysfs attributes */
1024 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
1025 {
1026 	struct mac80211_hwsim_data *data = dat;
1027 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1028 	struct sk_buff *skb;
1029 	struct ieee80211_pspoll *pspoll;
1030 
1031 	if (!vp->assoc)
1032 		return;
1033 
1034 	wiphy_dbg(data->hw->wiphy,
1035 		  "%s: send PS-Poll to %pM for aid %d\n",
1036 		  __func__, vp->bssid, vp->aid);
1037 
1038 	skb = dev_alloc_skb(sizeof(*pspoll));
1039 	if (!skb)
1040 		return;
1041 	pspoll = skb_put(skb, sizeof(*pspoll));
1042 	pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1043 					    IEEE80211_STYPE_PSPOLL |
1044 					    IEEE80211_FCTL_PM);
1045 	pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
1046 	memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
1047 	memcpy(pspoll->ta, mac, ETH_ALEN);
1048 
1049 	rcu_read_lock();
1050 	mac80211_hwsim_tx_frame(data->hw, skb,
1051 				rcu_dereference(vif->bss_conf.chanctx_conf)->def.chan);
1052 	rcu_read_unlock();
1053 }
1054 
1055 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
1056 				struct ieee80211_vif *vif, int ps)
1057 {
1058 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1059 	struct sk_buff *skb;
1060 	struct ieee80211_hdr *hdr;
1061 	struct ieee80211_tx_info *cb;
1062 
1063 	if (!vp->assoc)
1064 		return;
1065 
1066 	wiphy_dbg(data->hw->wiphy,
1067 		  "%s: send data::nullfunc to %pM ps=%d\n",
1068 		  __func__, vp->bssid, ps);
1069 
1070 	skb = dev_alloc_skb(sizeof(*hdr));
1071 	if (!skb)
1072 		return;
1073 	hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
1074 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1075 					 IEEE80211_STYPE_NULLFUNC |
1076 					 IEEE80211_FCTL_TODS |
1077 					 (ps ? IEEE80211_FCTL_PM : 0));
1078 	hdr->duration_id = cpu_to_le16(0);
1079 	memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
1080 	memcpy(hdr->addr2, mac, ETH_ALEN);
1081 	memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
1082 
1083 	cb = IEEE80211_SKB_CB(skb);
1084 	cb->control.rates[0].count = 1;
1085 	cb->control.rates[1].idx = -1;
1086 
1087 	rcu_read_lock();
1088 	mac80211_hwsim_tx_frame(data->hw, skb,
1089 				rcu_dereference(vif->bss_conf.chanctx_conf)->def.chan);
1090 	rcu_read_unlock();
1091 }
1092 
1093 
1094 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
1095 				   struct ieee80211_vif *vif)
1096 {
1097 	struct mac80211_hwsim_data *data = dat;
1098 	hwsim_send_nullfunc(data, mac, vif, 1);
1099 }
1100 
1101 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
1102 				      struct ieee80211_vif *vif)
1103 {
1104 	struct mac80211_hwsim_data *data = dat;
1105 	hwsim_send_nullfunc(data, mac, vif, 0);
1106 }
1107 
1108 static int hwsim_fops_ps_read(void *dat, u64 *val)
1109 {
1110 	struct mac80211_hwsim_data *data = dat;
1111 	*val = data->ps;
1112 	return 0;
1113 }
1114 
1115 static int hwsim_fops_ps_write(void *dat, u64 val)
1116 {
1117 	struct mac80211_hwsim_data *data = dat;
1118 	enum ps_mode old_ps;
1119 
1120 	if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
1121 	    val != PS_MANUAL_POLL)
1122 		return -EINVAL;
1123 
1124 	if (val == PS_MANUAL_POLL) {
1125 		if (data->ps != PS_ENABLED)
1126 			return -EINVAL;
1127 		local_bh_disable();
1128 		ieee80211_iterate_active_interfaces_atomic(
1129 			data->hw, IEEE80211_IFACE_ITER_NORMAL,
1130 			hwsim_send_ps_poll, data);
1131 		local_bh_enable();
1132 		return 0;
1133 	}
1134 	old_ps = data->ps;
1135 	data->ps = val;
1136 
1137 	local_bh_disable();
1138 	if (old_ps == PS_DISABLED && val != PS_DISABLED) {
1139 		ieee80211_iterate_active_interfaces_atomic(
1140 			data->hw, IEEE80211_IFACE_ITER_NORMAL,
1141 			hwsim_send_nullfunc_ps, data);
1142 	} else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
1143 		ieee80211_iterate_active_interfaces_atomic(
1144 			data->hw, IEEE80211_IFACE_ITER_NORMAL,
1145 			hwsim_send_nullfunc_no_ps, data);
1146 	}
1147 	local_bh_enable();
1148 
1149 	return 0;
1150 }
1151 
1152 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
1153 			 "%llu\n");
1154 
1155 static int hwsim_write_simulate_radar(void *dat, u64 val)
1156 {
1157 	struct mac80211_hwsim_data *data = dat;
1158 
1159 	ieee80211_radar_detected(data->hw, NULL);
1160 
1161 	return 0;
1162 }
1163 
1164 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
1165 			 hwsim_write_simulate_radar, "%llu\n");
1166 
1167 static int hwsim_fops_group_read(void *dat, u64 *val)
1168 {
1169 	struct mac80211_hwsim_data *data = dat;
1170 	*val = data->group;
1171 	return 0;
1172 }
1173 
1174 static int hwsim_fops_group_write(void *dat, u64 val)
1175 {
1176 	struct mac80211_hwsim_data *data = dat;
1177 	data->group = val;
1178 	return 0;
1179 }
1180 
1181 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
1182 			 hwsim_fops_group_read, hwsim_fops_group_write,
1183 			 "%llx\n");
1184 
1185 static int hwsim_fops_rx_rssi_read(void *dat, u64 *val)
1186 {
1187 	struct mac80211_hwsim_data *data = dat;
1188 	*val = data->rx_rssi;
1189 	return 0;
1190 }
1191 
1192 static int hwsim_fops_rx_rssi_write(void *dat, u64 val)
1193 {
1194 	struct mac80211_hwsim_data *data = dat;
1195 	int rssi = (int)val;
1196 
1197 	if (rssi >= 0 || rssi < -100)
1198 		return -EINVAL;
1199 
1200 	data->rx_rssi = rssi;
1201 	return 0;
1202 }
1203 
1204 DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_rx_rssi,
1205 			 hwsim_fops_rx_rssi_read, hwsim_fops_rx_rssi_write,
1206 			 "%lld\n");
1207 
1208 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
1209 					struct net_device *dev)
1210 {
1211 	/* TODO: allow packet injection */
1212 	dev_kfree_skb(skb);
1213 	return NETDEV_TX_OK;
1214 }
1215 
1216 static inline u64 mac80211_hwsim_get_tsf_raw(void)
1217 {
1218 	return ktime_to_us(ktime_get_real());
1219 }
1220 
1221 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
1222 {
1223 	u64 now = mac80211_hwsim_get_tsf_raw();
1224 	return cpu_to_le64(now + data->tsf_offset);
1225 }
1226 
1227 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
1228 				  struct ieee80211_vif *vif)
1229 {
1230 	struct mac80211_hwsim_data *data = hw->priv;
1231 	return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
1232 }
1233 
1234 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
1235 		struct ieee80211_vif *vif, u64 tsf)
1236 {
1237 	struct mac80211_hwsim_data *data = hw->priv;
1238 	u64 now = mac80211_hwsim_get_tsf(hw, vif);
1239 	/* MLD not supported here */
1240 	u32 bcn_int = data->link_data[0].beacon_int;
1241 	u64 delta = abs(tsf - now);
1242 	struct ieee80211_bss_conf *conf;
1243 
1244 	conf = link_conf_dereference_protected(vif, data->link_data[0].link_id);
1245 	if (conf && !conf->enable_beacon)
1246 		return;
1247 
1248 	/* adjust after beaconing with new timestamp at old TBTT */
1249 	if (tsf > now) {
1250 		data->tsf_offset += delta;
1251 		data->bcn_delta = do_div(delta, bcn_int);
1252 	} else {
1253 		data->tsf_offset -= delta;
1254 		data->bcn_delta = -(s64)do_div(delta, bcn_int);
1255 	}
1256 }
1257 
1258 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
1259 				      struct sk_buff *tx_skb,
1260 				      struct ieee80211_channel *chan)
1261 {
1262 	struct mac80211_hwsim_data *data = hw->priv;
1263 	struct sk_buff *skb;
1264 	struct hwsim_radiotap_hdr *hdr;
1265 	u16 flags, bitrate;
1266 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
1267 	struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
1268 
1269 	if (!txrate)
1270 		bitrate = 0;
1271 	else
1272 		bitrate = txrate->bitrate;
1273 
1274 	if (!netif_running(hwsim_mon))
1275 		return;
1276 
1277 	skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
1278 	if (skb == NULL)
1279 		return;
1280 
1281 	hdr = skb_push(skb, sizeof(*hdr));
1282 	hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1283 	hdr->hdr.it_pad = 0;
1284 	hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1285 	hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1286 					  (1 << IEEE80211_RADIOTAP_RATE) |
1287 					  (1 << IEEE80211_RADIOTAP_TSFT) |
1288 					  (1 << IEEE80211_RADIOTAP_CHANNEL));
1289 	hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
1290 	hdr->rt_flags = 0;
1291 	hdr->rt_rate = bitrate / 5;
1292 	hdr->rt_channel = cpu_to_le16(chan->center_freq);
1293 	flags = IEEE80211_CHAN_2GHZ;
1294 	if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
1295 		flags |= IEEE80211_CHAN_OFDM;
1296 	else
1297 		flags |= IEEE80211_CHAN_CCK;
1298 	hdr->rt_chbitmask = cpu_to_le16(flags);
1299 
1300 	skb->dev = hwsim_mon;
1301 	skb_reset_mac_header(skb);
1302 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1303 	skb->pkt_type = PACKET_OTHERHOST;
1304 	skb->protocol = htons(ETH_P_802_2);
1305 	memset(skb->cb, 0, sizeof(skb->cb));
1306 	netif_rx(skb);
1307 }
1308 
1309 
1310 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
1311 				       const u8 *addr)
1312 {
1313 	struct sk_buff *skb;
1314 	struct hwsim_radiotap_ack_hdr *hdr;
1315 	u16 flags;
1316 	struct ieee80211_hdr *hdr11;
1317 
1318 	if (!netif_running(hwsim_mon))
1319 		return;
1320 
1321 	skb = dev_alloc_skb(100);
1322 	if (skb == NULL)
1323 		return;
1324 
1325 	hdr = skb_put(skb, sizeof(*hdr));
1326 	hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1327 	hdr->hdr.it_pad = 0;
1328 	hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1329 	hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1330 					  (1 << IEEE80211_RADIOTAP_CHANNEL));
1331 	hdr->rt_flags = 0;
1332 	hdr->pad = 0;
1333 	hdr->rt_channel = cpu_to_le16(chan->center_freq);
1334 	flags = IEEE80211_CHAN_2GHZ;
1335 	hdr->rt_chbitmask = cpu_to_le16(flags);
1336 
1337 	hdr11 = skb_put(skb, 10);
1338 	hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1339 					   IEEE80211_STYPE_ACK);
1340 	hdr11->duration_id = cpu_to_le16(0);
1341 	memcpy(hdr11->addr1, addr, ETH_ALEN);
1342 
1343 	skb->dev = hwsim_mon;
1344 	skb_reset_mac_header(skb);
1345 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1346 	skb->pkt_type = PACKET_OTHERHOST;
1347 	skb->protocol = htons(ETH_P_802_2);
1348 	memset(skb->cb, 0, sizeof(skb->cb));
1349 	netif_rx(skb);
1350 }
1351 
1352 struct mac80211_hwsim_addr_match_data {
1353 	u8 addr[ETH_ALEN];
1354 	bool ret;
1355 };
1356 
1357 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1358 				     struct ieee80211_vif *vif)
1359 {
1360 	int i;
1361 	struct mac80211_hwsim_addr_match_data *md = data;
1362 
1363 	if (memcmp(mac, md->addr, ETH_ALEN) == 0) {
1364 		md->ret = true;
1365 		return;
1366 	}
1367 
1368 	/* Match the link address */
1369 	for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
1370 		struct ieee80211_bss_conf *conf;
1371 
1372 		conf = rcu_dereference(vif->link_conf[i]);
1373 		if (!conf)
1374 			continue;
1375 
1376 		if (memcmp(conf->addr, md->addr, ETH_ALEN) == 0) {
1377 			md->ret = true;
1378 			return;
1379 		}
1380 	}
1381 }
1382 
1383 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1384 				      const u8 *addr)
1385 {
1386 	struct mac80211_hwsim_addr_match_data md = {
1387 		.ret = false,
1388 	};
1389 
1390 	if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1391 		return true;
1392 
1393 	memcpy(md.addr, addr, ETH_ALEN);
1394 
1395 	ieee80211_iterate_active_interfaces_atomic(data->hw,
1396 						   IEEE80211_IFACE_ITER_NORMAL,
1397 						   mac80211_hwsim_addr_iter,
1398 						   &md);
1399 
1400 	return md.ret;
1401 }
1402 
1403 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1404 			   struct sk_buff *skb)
1405 {
1406 	switch (data->ps) {
1407 	case PS_DISABLED:
1408 		return true;
1409 	case PS_ENABLED:
1410 		return false;
1411 	case PS_AUTO_POLL:
1412 		/* TODO: accept (some) Beacons by default and other frames only
1413 		 * if pending PS-Poll has been sent */
1414 		return true;
1415 	case PS_MANUAL_POLL:
1416 		/* Allow unicast frames to own address if there is a pending
1417 		 * PS-Poll */
1418 		if (data->ps_poll_pending &&
1419 		    mac80211_hwsim_addr_match(data, skb->data + 4)) {
1420 			data->ps_poll_pending = false;
1421 			return true;
1422 		}
1423 		return false;
1424 	}
1425 
1426 	return true;
1427 }
1428 
1429 static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1430 				  struct sk_buff *skb, int portid)
1431 {
1432 	struct net *net;
1433 	bool found = false;
1434 	int res = -ENOENT;
1435 
1436 	rcu_read_lock();
1437 	for_each_net_rcu(net) {
1438 		if (data->netgroup == hwsim_net_get_netgroup(net)) {
1439 			res = genlmsg_unicast(net, skb, portid);
1440 			found = true;
1441 			break;
1442 		}
1443 	}
1444 	rcu_read_unlock();
1445 
1446 	if (!found)
1447 		nlmsg_free(skb);
1448 
1449 	return res;
1450 }
1451 
1452 static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1453 					 const u8 *addr, bool add)
1454 {
1455 	struct mac80211_hwsim_data *data = hw->priv;
1456 	u32 _portid = READ_ONCE(data->wmediumd);
1457 	struct sk_buff *skb;
1458 	void *msg_head;
1459 
1460 	WARN_ON(!is_valid_ether_addr(addr));
1461 
1462 	if (!_portid && !hwsim_virtio_enabled)
1463 		return;
1464 
1465 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1466 	if (!skb)
1467 		return;
1468 
1469 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1470 			       add ? HWSIM_CMD_ADD_MAC_ADDR :
1471 				     HWSIM_CMD_DEL_MAC_ADDR);
1472 	if (!msg_head) {
1473 		pr_debug("mac80211_hwsim: problem with msg_head\n");
1474 		goto nla_put_failure;
1475 	}
1476 
1477 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1478 		    ETH_ALEN, data->addresses[1].addr))
1479 		goto nla_put_failure;
1480 
1481 	if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1482 		goto nla_put_failure;
1483 
1484 	genlmsg_end(skb, msg_head);
1485 
1486 	if (hwsim_virtio_enabled)
1487 		hwsim_tx_virtio(data, skb);
1488 	else
1489 		hwsim_unicast_netgroup(data, skb, _portid);
1490 	return;
1491 nla_put_failure:
1492 	nlmsg_free(skb);
1493 }
1494 
1495 static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1496 {
1497 	u16 result = 0;
1498 
1499 	if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1500 		result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1501 	if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1502 		result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1503 	if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1504 		result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1505 	if (rate->flags & IEEE80211_TX_RC_MCS)
1506 		result |= MAC80211_HWSIM_TX_RC_MCS;
1507 	if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1508 		result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1509 	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1510 		result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1511 	if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1512 		result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1513 	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1514 		result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1515 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1516 		result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1517 	if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1518 		result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1519 	if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1520 		result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1521 
1522 	return result;
1523 }
1524 
1525 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1526 				       struct sk_buff *my_skb,
1527 				       int dst_portid,
1528 				       struct ieee80211_channel *channel)
1529 {
1530 	struct sk_buff *skb;
1531 	struct mac80211_hwsim_data *data = hw->priv;
1532 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1533 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1534 	void *msg_head;
1535 	unsigned int hwsim_flags = 0;
1536 	int i;
1537 	struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1538 	struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1539 	uintptr_t cookie;
1540 
1541 	if (data->ps != PS_DISABLED)
1542 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1543 	/* If the queue contains MAX_QUEUE skb's drop some */
1544 	if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1545 		/* Dropping until WARN_QUEUE level */
1546 		while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1547 			ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1548 			data->tx_dropped++;
1549 		}
1550 	}
1551 
1552 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1553 	if (skb == NULL)
1554 		goto nla_put_failure;
1555 
1556 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1557 			       HWSIM_CMD_FRAME);
1558 	if (msg_head == NULL) {
1559 		pr_debug("mac80211_hwsim: problem with msg_head\n");
1560 		goto nla_put_failure;
1561 	}
1562 
1563 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1564 		    ETH_ALEN, data->addresses[1].addr))
1565 		goto nla_put_failure;
1566 
1567 	/* We get the skb->data */
1568 	if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1569 		goto nla_put_failure;
1570 
1571 	/* We get the flags for this transmission, and we translate them to
1572 	   wmediumd flags  */
1573 
1574 	if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1575 		hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1576 
1577 	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1578 		hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1579 
1580 	if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1581 		goto nla_put_failure;
1582 
1583 	if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1584 		goto nla_put_failure;
1585 
1586 	/* We get the tx control (rate and retries) info*/
1587 
1588 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1589 		tx_attempts[i].idx = info->status.rates[i].idx;
1590 		tx_attempts_flags[i].idx = info->status.rates[i].idx;
1591 		tx_attempts[i].count = info->status.rates[i].count;
1592 		tx_attempts_flags[i].flags =
1593 				trans_tx_rate_flags_ieee2hwsim(
1594 						&info->status.rates[i]);
1595 	}
1596 
1597 	if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1598 		    sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1599 		    tx_attempts))
1600 		goto nla_put_failure;
1601 
1602 	if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1603 		    sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1604 		    tx_attempts_flags))
1605 		goto nla_put_failure;
1606 
1607 	/* We create a cookie to identify this skb */
1608 	cookie = atomic_inc_return(&data->pending_cookie);
1609 	info->rate_driver_data[0] = (void *)cookie;
1610 	if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1611 		goto nla_put_failure;
1612 
1613 	genlmsg_end(skb, msg_head);
1614 
1615 	if (hwsim_virtio_enabled) {
1616 		if (hwsim_tx_virtio(data, skb))
1617 			goto err_free_txskb;
1618 	} else {
1619 		if (hwsim_unicast_netgroup(data, skb, dst_portid))
1620 			goto err_free_txskb;
1621 	}
1622 
1623 	/* Enqueue the packet */
1624 	skb_queue_tail(&data->pending, my_skb);
1625 	data->tx_pkts++;
1626 	data->tx_bytes += my_skb->len;
1627 	return;
1628 
1629 nla_put_failure:
1630 	nlmsg_free(skb);
1631 err_free_txskb:
1632 	pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1633 	ieee80211_free_txskb(hw, my_skb);
1634 	data->tx_failed++;
1635 }
1636 
1637 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1638 			       struct ieee80211_channel *c2)
1639 {
1640 	if (!c1 || !c2)
1641 		return false;
1642 
1643 	return c1->center_freq == c2->center_freq;
1644 }
1645 
1646 struct tx_iter_data {
1647 	struct ieee80211_channel *channel;
1648 	bool receive;
1649 };
1650 
1651 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1652 				   struct ieee80211_vif *vif)
1653 {
1654 	struct tx_iter_data *data = _data;
1655 	int i;
1656 
1657 	/* For NAN Device simulation purposes, assume that NAN is always
1658 	 * on channel 6 or channel 149.
1659 	 */
1660 	if (vif->type == NL80211_IFTYPE_NAN) {
1661 		data->receive = (data->channel &&
1662 				 (data->channel->center_freq == 2437 ||
1663 				  data->channel->center_freq == 5745));
1664 		return;
1665 	}
1666 
1667 	for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
1668 		struct ieee80211_bss_conf *conf;
1669 		struct ieee80211_chanctx_conf *chanctx;
1670 
1671 		conf = rcu_dereference(vif->link_conf[i]);
1672 		if (!conf)
1673 			continue;
1674 
1675 		chanctx = rcu_dereference(conf->chanctx_conf);
1676 		if (!chanctx)
1677 			continue;
1678 
1679 		if (!hwsim_chans_compat(data->channel, chanctx->def.chan))
1680 			continue;
1681 
1682 		data->receive = true;
1683 		return;
1684 	}
1685 }
1686 
1687 static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1688 {
1689 	/*
1690 	 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1691 	 * e.g. like this:
1692 	 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1693 	 * (but you should use a valid OUI, not that)
1694 	 *
1695 	 * If anyone wants to 'donate' a radiotap OUI/subns code
1696 	 * please send a patch removing this #ifdef and changing
1697 	 * the values accordingly.
1698 	 */
1699 #ifdef HWSIM_RADIOTAP_OUI
1700 	struct ieee80211_radiotap_vendor_tlv *rtap;
1701 	static const char vendor_data[8] = "ABCDEFGH";
1702 
1703 	// Make sure no padding is needed
1704 	BUILD_BUG_ON(sizeof(vendor_data) % 4);
1705 	/* this is last radiotap info before the mac header, so
1706 	 * skb_reset_mac_header for mac8022 to know the end of
1707 	 * the radiotap TLV/beginning of the 802.11 header
1708 	 */
1709 	skb_reset_mac_header(skb);
1710 
1711 	/*
1712 	 * Note that this code requires the headroom in the SKB
1713 	 * that was allocated earlier.
1714 	 */
1715 	rtap = skb_push(skb, sizeof(*rtap) + sizeof(vendor_data));
1716 
1717 	rtap->len = cpu_to_le16(sizeof(*rtap) -
1718 				sizeof(struct ieee80211_radiotap_tlv) +
1719 				sizeof(vendor_data));
1720 	rtap->type = cpu_to_le16(IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
1721 
1722 	rtap->content.oui[0] = HWSIM_RADIOTAP_OUI[0];
1723 	rtap->content.oui[1] = HWSIM_RADIOTAP_OUI[1];
1724 	rtap->content.oui[2] = HWSIM_RADIOTAP_OUI[2];
1725 	rtap->content.oui_subtype = 127;
1726 	/* clear reserved field */
1727 	rtap->content.reserved = 0;
1728 	rtap->content.vendor_type = 0;
1729 	memcpy(rtap->content.data, vendor_data, sizeof(vendor_data));
1730 
1731 	IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
1732 #endif
1733 }
1734 
1735 static void mac80211_hwsim_rx(struct mac80211_hwsim_data *data,
1736 			      struct ieee80211_rx_status *rx_status,
1737 			      struct sk_buff *skb)
1738 {
1739 	struct ieee80211_hdr *hdr = (void *)skb->data;
1740 
1741 	if (!ieee80211_has_morefrags(hdr->frame_control) &&
1742 	    !is_multicast_ether_addr(hdr->addr1) &&
1743 	    (ieee80211_is_mgmt(hdr->frame_control) ||
1744 	     ieee80211_is_data(hdr->frame_control))) {
1745 		struct ieee80211_sta *sta;
1746 		unsigned int link_id;
1747 
1748 		rcu_read_lock();
1749 		sta = ieee80211_find_sta_by_link_addrs(data->hw, hdr->addr2,
1750 						       hdr->addr1, &link_id);
1751 		if (sta) {
1752 			struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
1753 
1754 			if (ieee80211_has_pm(hdr->frame_control))
1755 				sp->active_links_rx &= ~BIT(link_id);
1756 			else
1757 				sp->active_links_rx |= BIT(link_id);
1758 
1759 			rx_status->link_valid = true;
1760 			rx_status->link_id = link_id;
1761 		}
1762 		rcu_read_unlock();
1763 	}
1764 
1765 	memcpy(IEEE80211_SKB_RXCB(skb), rx_status, sizeof(*rx_status));
1766 
1767 	mac80211_hwsim_add_vendor_rtap(skb);
1768 
1769 	data->rx_pkts++;
1770 	data->rx_bytes += skb->len;
1771 	ieee80211_rx_irqsafe(data->hw, skb);
1772 }
1773 
1774 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1775 					  struct sk_buff *skb,
1776 					  struct ieee80211_channel *chan)
1777 {
1778 	struct mac80211_hwsim_data *data = hw->priv, *data2;
1779 	bool ack = false;
1780 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1781 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1782 	struct ieee80211_rx_status rx_status;
1783 	u64 now;
1784 
1785 	memset(&rx_status, 0, sizeof(rx_status));
1786 	rx_status.flag |= RX_FLAG_MACTIME_START;
1787 	rx_status.freq = chan->center_freq;
1788 	rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1789 	rx_status.band = chan->band;
1790 	if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1791 		rx_status.rate_idx =
1792 			ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1793 		rx_status.nss =
1794 			ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1795 		rx_status.encoding = RX_ENC_VHT;
1796 	} else {
1797 		rx_status.rate_idx = info->control.rates[0].idx;
1798 		if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1799 			rx_status.encoding = RX_ENC_HT;
1800 	}
1801 	if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1802 		rx_status.bw = RATE_INFO_BW_40;
1803 	else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1804 		rx_status.bw = RATE_INFO_BW_80;
1805 	else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1806 		rx_status.bw = RATE_INFO_BW_160;
1807 	else
1808 		rx_status.bw = RATE_INFO_BW_20;
1809 	if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1810 		rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1811 	/* TODO: simulate optional packet loss */
1812 	rx_status.signal = data->rx_rssi;
1813 	if (info->control.vif)
1814 		rx_status.signal += info->control.vif->bss_conf.txpower;
1815 
1816 	if (data->ps != PS_DISABLED)
1817 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1818 
1819 	/* release the skb's source info */
1820 	skb_orphan(skb);
1821 	skb_dst_drop(skb);
1822 	skb->mark = 0;
1823 	skb_ext_reset(skb);
1824 	nf_reset_ct(skb);
1825 
1826 	/*
1827 	 * Get absolute mactime here so all HWs RX at the "same time", and
1828 	 * absolute TX time for beacon mactime so the timestamp matches.
1829 	 * Giving beacons a different mactime than non-beacons looks messy, but
1830 	 * it helps the Toffset be exact and a ~10us mactime discrepancy
1831 	 * probably doesn't really matter.
1832 	 */
1833 	if (ieee80211_is_beacon(hdr->frame_control) ||
1834 	    ieee80211_is_probe_resp(hdr->frame_control)) {
1835 		rx_status.boottime_ns = ktime_get_boottime_ns();
1836 		now = data->abs_bcn_ts;
1837 	} else {
1838 		now = mac80211_hwsim_get_tsf_raw();
1839 	}
1840 
1841 	/* Copy skb to all enabled radios that are on the current frequency */
1842 	spin_lock(&hwsim_radio_lock);
1843 	list_for_each_entry(data2, &hwsim_radios, list) {
1844 		struct sk_buff *nskb;
1845 		struct tx_iter_data tx_iter_data = {
1846 			.receive = false,
1847 			.channel = chan,
1848 		};
1849 
1850 		if (data == data2)
1851 			continue;
1852 
1853 		if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1854 		    !hwsim_ps_rx_ok(data2, skb))
1855 			continue;
1856 
1857 		if (!(data->group & data2->group))
1858 			continue;
1859 
1860 		if (data->netgroup != data2->netgroup)
1861 			continue;
1862 
1863 		if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1864 		    !hwsim_chans_compat(chan, data2->channel)) {
1865 			ieee80211_iterate_active_interfaces_atomic(
1866 				data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1867 				mac80211_hwsim_tx_iter, &tx_iter_data);
1868 			if (!tx_iter_data.receive)
1869 				continue;
1870 		}
1871 
1872 		/*
1873 		 * reserve some space for our vendor and the normal
1874 		 * radiotap header, since we're copying anyway
1875 		 */
1876 		if (skb->len < PAGE_SIZE && paged_rx) {
1877 			struct page *page = alloc_page(GFP_ATOMIC);
1878 
1879 			if (!page)
1880 				continue;
1881 
1882 			nskb = dev_alloc_skb(128);
1883 			if (!nskb) {
1884 				__free_page(page);
1885 				continue;
1886 			}
1887 
1888 			memcpy(page_address(page), skb->data, skb->len);
1889 			skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1890 		} else {
1891 			nskb = skb_copy(skb, GFP_ATOMIC);
1892 			if (!nskb)
1893 				continue;
1894 		}
1895 
1896 		if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1897 			ack = true;
1898 
1899 		rx_status.mactime = now + data2->tsf_offset;
1900 
1901 		mac80211_hwsim_rx(data2, &rx_status, nskb);
1902 	}
1903 	spin_unlock(&hwsim_radio_lock);
1904 
1905 	return ack;
1906 }
1907 
1908 static struct ieee80211_bss_conf *
1909 mac80211_hwsim_select_tx_link(struct mac80211_hwsim_data *data,
1910 			      struct ieee80211_vif *vif,
1911 			      struct ieee80211_sta *sta,
1912 			      struct ieee80211_hdr *hdr,
1913 			      struct ieee80211_link_sta **link_sta)
1914 {
1915 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
1916 	int i;
1917 
1918 	if (!ieee80211_vif_is_mld(vif))
1919 		return &vif->bss_conf;
1920 
1921 	WARN_ON(is_multicast_ether_addr(hdr->addr1));
1922 
1923 	if (WARN_ON_ONCE(!sta || !sta->valid_links))
1924 		return &vif->bss_conf;
1925 
1926 	for (i = 0; i < ARRAY_SIZE(vif->link_conf); i++) {
1927 		struct ieee80211_bss_conf *bss_conf;
1928 		unsigned int link_id;
1929 
1930 		/* round-robin the available link IDs */
1931 		link_id = (sp->last_link + i + 1) % ARRAY_SIZE(vif->link_conf);
1932 
1933 		if (!(vif->active_links & BIT(link_id)))
1934 			continue;
1935 
1936 		if (!(sp->active_links_rx & BIT(link_id)))
1937 			continue;
1938 
1939 		*link_sta = rcu_dereference(sta->link[link_id]);
1940 		if (!*link_sta)
1941 			continue;
1942 
1943 		bss_conf = rcu_dereference(vif->link_conf[link_id]);
1944 		if (WARN_ON_ONCE(!bss_conf))
1945 			continue;
1946 
1947 		/* can happen while switching links */
1948 		if (!rcu_access_pointer(bss_conf->chanctx_conf))
1949 			continue;
1950 
1951 		sp->last_link = link_id;
1952 		return bss_conf;
1953 	}
1954 
1955 	return NULL;
1956 }
1957 
1958 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1959 			      struct ieee80211_tx_control *control,
1960 			      struct sk_buff *skb)
1961 {
1962 	struct mac80211_hwsim_data *data = hw->priv;
1963 	struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1964 	struct ieee80211_hdr *hdr = (void *)skb->data;
1965 	struct ieee80211_chanctx_conf *chanctx_conf;
1966 	struct ieee80211_channel *channel;
1967 	struct ieee80211_vif *vif = txi->control.vif;
1968 	bool ack;
1969 	enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
1970 	u32 _portid, i;
1971 
1972 	if (WARN_ON(skb->len < 10)) {
1973 		/* Should not happen; just a sanity check for addr1 use */
1974 		ieee80211_free_txskb(hw, skb);
1975 		return;
1976 	}
1977 
1978 	if (vif && vif->type == NL80211_IFTYPE_NAN && !data->tmp_chan) {
1979 		/* For NAN Device simulation purposes, assume that NAN is always
1980 		 * on channel 6 or channel 149, unless a ROC is in progress (for
1981 		 * USD use cases).
1982 		 */
1983 		if (data->nan_curr_dw_band == NL80211_BAND_2GHZ)
1984 			channel = ieee80211_get_channel(hw->wiphy, 2437);
1985 		else if (data->nan_curr_dw_band == NL80211_BAND_5GHZ)
1986 			channel = ieee80211_get_channel(hw->wiphy, 5745);
1987 		else
1988 			channel = NULL;
1989 
1990 		if (WARN_ON(!channel)) {
1991 			ieee80211_free_txskb(hw, skb);
1992 			return;
1993 		}
1994 	} else if (!data->use_chanctx) {
1995 		channel = data->channel;
1996 		confbw = data->bw;
1997 	} else if (txi->hw_queue == 4) {
1998 		channel = data->tmp_chan;
1999 	} else {
2000 		u8 link = u32_get_bits(IEEE80211_SKB_CB(skb)->control.flags,
2001 				       IEEE80211_TX_CTRL_MLO_LINK);
2002 		struct ieee80211_link_sta *link_sta = NULL;
2003 		struct ieee80211_sta *sta = control->sta;
2004 		struct ieee80211_bss_conf *bss_conf;
2005 
2006 		if (link != IEEE80211_LINK_UNSPECIFIED) {
2007 			bss_conf = rcu_dereference(txi->control.vif->link_conf[link]);
2008 			if (sta)
2009 				link_sta = rcu_dereference(sta->link[link]);
2010 		} else {
2011 			bss_conf = mac80211_hwsim_select_tx_link(data, vif, sta,
2012 								 hdr, &link_sta);
2013 		}
2014 
2015 		if (unlikely(!bss_conf)) {
2016 			/* if it's an MLO STA, it might have deactivated all
2017 			 * links temporarily - but we don't handle real PS in
2018 			 * this code yet, so just drop the frame in that case
2019 			 */
2020 			WARN(link != IEEE80211_LINK_UNSPECIFIED || !sta || !sta->mlo,
2021 			     "link:%d, sta:%pM, sta->mlo:%d\n",
2022 			     link, sta ? sta->addr : NULL, sta ? sta->mlo : -1);
2023 			ieee80211_free_txskb(hw, skb);
2024 			return;
2025 		}
2026 
2027 		/* Do address translations only between shared links. It is
2028 		 * possible that while an non-AP MLD station and an AP MLD
2029 		 * station have shared links, the frame is intended to be sent
2030 		 * on a link which is not shared (for example when sending a
2031 		 * probe response).
2032 		 */
2033 		if (sta && sta->mlo && link_sta) {
2034 			/* address translation to link addresses on TX */
2035 			ether_addr_copy(hdr->addr1, link_sta->addr);
2036 			ether_addr_copy(hdr->addr2, bss_conf->addr);
2037 			/* translate A3 only if it's the BSSID */
2038 			if (!ieee80211_has_tods(hdr->frame_control) &&
2039 			    !ieee80211_has_fromds(hdr->frame_control)) {
2040 				if (ether_addr_equal(hdr->addr3, sta->addr))
2041 					ether_addr_copy(hdr->addr3, link_sta->addr);
2042 				else if (ether_addr_equal(hdr->addr3, vif->addr))
2043 					ether_addr_copy(hdr->addr3, bss_conf->addr);
2044 			}
2045 			/* no need to look at A4, if present it's SA */
2046 		}
2047 
2048 		chanctx_conf = rcu_dereference(bss_conf->chanctx_conf);
2049 		if (chanctx_conf) {
2050 			channel = chanctx_conf->def.chan;
2051 			confbw = chanctx_conf->def.width;
2052 		} else {
2053 			channel = NULL;
2054 		}
2055 	}
2056 
2057 	if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
2058 		ieee80211_free_txskb(hw, skb);
2059 		return;
2060 	}
2061 
2062 	if (data->idle && !data->tmp_chan) {
2063 		wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
2064 		ieee80211_free_txskb(hw, skb);
2065 		return;
2066 	}
2067 
2068 	if (txi->control.vif)
2069 		hwsim_check_magic(txi->control.vif);
2070 	if (control->sta)
2071 		hwsim_check_sta_magic(control->sta);
2072 
2073 	if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
2074 		ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
2075 				       txi->control.rates,
2076 				       ARRAY_SIZE(txi->control.rates));
2077 
2078 	for (i = 0; i < ARRAY_SIZE(txi->control.rates); i++) {
2079 		u16 rflags = txi->control.rates[i].flags;
2080 		/* initialize to data->bw for 5/10 MHz handling */
2081 		enum nl80211_chan_width bw = data->bw;
2082 
2083 		if (txi->control.rates[i].idx == -1)
2084 			break;
2085 
2086 		if (rflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
2087 			bw = NL80211_CHAN_WIDTH_40;
2088 		else if (rflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
2089 			bw = NL80211_CHAN_WIDTH_80;
2090 		else if (rflags & IEEE80211_TX_RC_160_MHZ_WIDTH)
2091 			bw = NL80211_CHAN_WIDTH_160;
2092 
2093 		if (WARN_ON(hwsim_get_chanwidth(bw) > hwsim_get_chanwidth(confbw)))
2094 			return;
2095 	}
2096 
2097 	if (skb->len >= 24 + 8 &&
2098 	    ieee80211_is_probe_resp(hdr->frame_control)) {
2099 		/* fake header transmission time */
2100 		struct ieee80211_mgmt *mgmt;
2101 		struct ieee80211_rate *txrate;
2102 		/* TODO: get MCS */
2103 		int bitrate = 100;
2104 		u64 ts;
2105 
2106 		mgmt = (struct ieee80211_mgmt *)skb->data;
2107 		txrate = ieee80211_get_tx_rate(hw, txi);
2108 		if (txrate)
2109 			bitrate = txrate->bitrate;
2110 		ts = mac80211_hwsim_get_tsf_raw();
2111 		mgmt->u.probe_resp.timestamp =
2112 			cpu_to_le64(ts + data->tsf_offset +
2113 				    24 * 8 * 10 / bitrate);
2114 	}
2115 
2116 	mac80211_hwsim_monitor_rx(hw, skb, channel);
2117 
2118 	/* wmediumd mode check */
2119 	_portid = READ_ONCE(data->wmediumd);
2120 
2121 	if (_portid || hwsim_virtio_enabled)
2122 		return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
2123 
2124 	/* NO wmediumd detected, perfect medium simulation */
2125 	data->tx_pkts++;
2126 	data->tx_bytes += skb->len;
2127 	ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
2128 
2129 	if (ack && skb->len >= 16)
2130 		mac80211_hwsim_monitor_ack(channel, hdr->addr2);
2131 
2132 	ieee80211_tx_info_clear_status(txi);
2133 
2134 	/* frame was transmitted at most favorable rate at first attempt */
2135 	txi->control.rates[0].count = 1;
2136 	txi->control.rates[1].idx = -1;
2137 
2138 	if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
2139 		txi->flags |= IEEE80211_TX_STAT_ACK;
2140 	ieee80211_tx_status_irqsafe(hw, skb);
2141 }
2142 
2143 
2144 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
2145 {
2146 	struct mac80211_hwsim_data *data = hw->priv;
2147 	wiphy_dbg(hw->wiphy, "%s\n", __func__);
2148 	data->started = true;
2149 	return 0;
2150 }
2151 
2152 
2153 static void mac80211_hwsim_stop(struct ieee80211_hw *hw, bool suspend)
2154 {
2155 	struct mac80211_hwsim_data *data = hw->priv;
2156 	int i;
2157 
2158 	data->started = false;
2159 
2160 	for (i = 0; i < ARRAY_SIZE(data->link_data); i++)
2161 		hrtimer_cancel(&data->link_data[i].beacon_timer);
2162 
2163 	while (!skb_queue_empty(&data->pending))
2164 		ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
2165 
2166 	wiphy_dbg(hw->wiphy, "%s\n", __func__);
2167 }
2168 
2169 
2170 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
2171 					struct ieee80211_vif *vif)
2172 {
2173 	wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
2174 		  __func__, ieee80211_vif_type_p2p(vif),
2175 		  vif->addr);
2176 	hwsim_set_magic(vif);
2177 
2178 	if (vif->type != NL80211_IFTYPE_MONITOR)
2179 		mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
2180 
2181 	vif->cab_queue = 0;
2182 	vif->hw_queue[IEEE80211_AC_VO] = 0;
2183 	vif->hw_queue[IEEE80211_AC_VI] = 1;
2184 	vif->hw_queue[IEEE80211_AC_BE] = 2;
2185 	vif->hw_queue[IEEE80211_AC_BK] = 3;
2186 
2187 	return 0;
2188 }
2189 
2190 #ifdef CONFIG_MAC80211_DEBUGFS
2191 static void
2192 mac80211_hwsim_link_add_debugfs(struct ieee80211_hw *hw,
2193 				struct ieee80211_vif *vif,
2194 				struct ieee80211_bss_conf *link_conf,
2195 				struct dentry *dir)
2196 {
2197 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2198 
2199 	debugfs_create_u32("skip_beacons", 0600, dir,
2200 			   &vp->skip_beacons[link_conf->link_id]);
2201 }
2202 #endif
2203 
2204 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
2205 					   struct ieee80211_vif *vif,
2206 					   enum nl80211_iftype newtype,
2207 					   bool newp2p)
2208 {
2209 	newtype = ieee80211_iftype_p2p(newtype, newp2p);
2210 	wiphy_dbg(hw->wiphy,
2211 		  "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
2212 		  __func__, ieee80211_vif_type_p2p(vif),
2213 		    newtype, vif->addr);
2214 	hwsim_check_magic(vif);
2215 
2216 	/*
2217 	 * interface may change from non-AP to AP in
2218 	 * which case this needs to be set up again
2219 	 */
2220 	vif->cab_queue = 0;
2221 
2222 	return 0;
2223 }
2224 
2225 static void mac80211_hwsim_remove_interface(
2226 	struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2227 {
2228 	wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
2229 		  __func__, ieee80211_vif_type_p2p(vif),
2230 		  vif->addr);
2231 	hwsim_check_magic(vif);
2232 	hwsim_clear_magic(vif);
2233 	if (vif->type != NL80211_IFTYPE_MONITOR)
2234 		mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
2235 }
2236 
2237 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
2238 				    struct sk_buff *skb,
2239 				    struct ieee80211_channel *chan)
2240 {
2241 	struct mac80211_hwsim_data *data = hw->priv;
2242 	u32 _portid = READ_ONCE(data->wmediumd);
2243 
2244 	if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
2245 		struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
2246 		ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
2247 				       txi->control.rates,
2248 				       ARRAY_SIZE(txi->control.rates));
2249 	}
2250 
2251 	mac80211_hwsim_monitor_rx(hw, skb, chan);
2252 
2253 	if (_portid || hwsim_virtio_enabled)
2254 		return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, chan);
2255 
2256 	data->tx_pkts++;
2257 	data->tx_bytes += skb->len;
2258 	mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
2259 	dev_kfree_skb(skb);
2260 }
2261 
2262 static void __mac80211_hwsim_beacon_tx(struct ieee80211_bss_conf *link_conf,
2263 				       struct mac80211_hwsim_data *data,
2264 				       struct ieee80211_hw *hw,
2265 				       struct ieee80211_vif *vif,
2266 				       struct sk_buff *skb)
2267 {
2268 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2269 	struct ieee80211_tx_info *info;
2270 	struct ieee80211_rate *txrate;
2271 	struct ieee80211_mgmt *mgmt;
2272 	/* TODO: get MCS */
2273 	int bitrate = 100;
2274 
2275 	if (vp->skip_beacons[link_conf->link_id]) {
2276 		vp->skip_beacons[link_conf->link_id]--;
2277 		dev_kfree_skb(skb);
2278 		return;
2279 	}
2280 
2281 	info = IEEE80211_SKB_CB(skb);
2282 	if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
2283 		ieee80211_get_tx_rates(vif, NULL, skb,
2284 				       info->control.rates,
2285 				       ARRAY_SIZE(info->control.rates));
2286 
2287 	txrate = ieee80211_get_tx_rate(hw, info);
2288 	if (txrate)
2289 		bitrate = txrate->bitrate;
2290 
2291 	mgmt = (struct ieee80211_mgmt *) skb->data;
2292 	/* fake header transmission time */
2293 	data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
2294 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2295 		struct ieee80211_ext *ext = (void *) mgmt;
2296 
2297 		ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
2298 							  data->tsf_offset +
2299 							  10 * 8 * 10 /
2300 							  bitrate);
2301 	} else {
2302 		mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
2303 						       data->tsf_offset +
2304 						       24 * 8 * 10 /
2305 						       bitrate);
2306 	}
2307 
2308 	mac80211_hwsim_tx_frame(hw, skb,
2309 			rcu_dereference(link_conf->chanctx_conf)->def.chan);
2310 }
2311 
2312 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
2313 				     struct ieee80211_vif *vif)
2314 {
2315 	struct mac80211_hwsim_link_data *link_data = arg;
2316 	u32 link_id = link_data->link_id;
2317 	struct ieee80211_bss_conf *link_conf, *tx_bss_conf;
2318 	struct mac80211_hwsim_data *data =
2319 		container_of(link_data, struct mac80211_hwsim_data,
2320 			     link_data[link_id]);
2321 	struct ieee80211_hw *hw = data->hw;
2322 	struct sk_buff *skb;
2323 
2324 	hwsim_check_magic(vif);
2325 
2326 	link_conf = rcu_dereference(vif->link_conf[link_id]);
2327 	if (!link_conf)
2328 		return;
2329 
2330 	if (vif->type != NL80211_IFTYPE_AP &&
2331 	    vif->type != NL80211_IFTYPE_MESH_POINT &&
2332 	    vif->type != NL80211_IFTYPE_ADHOC &&
2333 	    vif->type != NL80211_IFTYPE_OCB)
2334 		return;
2335 
2336 	tx_bss_conf = rcu_access_pointer(link_conf->tx_bss_conf);
2337 	if (tx_bss_conf && tx_bss_conf != link_conf)
2338 		return;
2339 
2340 	if (link_conf->ema_ap) {
2341 		struct ieee80211_ema_beacons *ema;
2342 		u8 i = 0;
2343 
2344 		ema = ieee80211_beacon_get_template_ema_list(hw, vif, link_id);
2345 		if (!ema || !ema->cnt)
2346 			return;
2347 
2348 		for (i = 0; i < ema->cnt; i++) {
2349 			__mac80211_hwsim_beacon_tx(link_conf, data, hw, vif,
2350 						   ema->bcn[i].skb);
2351 			ema->bcn[i].skb = NULL; /* Already freed */
2352 		}
2353 		ieee80211_beacon_free_ema_list(ema);
2354 	} else {
2355 		skb = ieee80211_beacon_get(hw, vif, link_id);
2356 		if (!skb)
2357 			return;
2358 
2359 		__mac80211_hwsim_beacon_tx(link_conf, data, hw, vif, skb);
2360 	}
2361 
2362 	while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
2363 		mac80211_hwsim_tx_frame(hw, skb,
2364 			rcu_dereference(link_conf->chanctx_conf)->def.chan);
2365 	}
2366 
2367 	if (link_conf->csa_active && ieee80211_beacon_cntdwn_is_complete(vif, link_id))
2368 		ieee80211_csa_finish(vif, link_id);
2369 
2370 	if (link_conf->color_change_active &&
2371 	    ieee80211_beacon_cntdwn_is_complete(vif, link_id))
2372 		ieee80211_color_change_finish(vif, link_id);
2373 }
2374 
2375 static enum hrtimer_restart
2376 mac80211_hwsim_beacon(struct hrtimer *timer)
2377 {
2378 	struct mac80211_hwsim_link_data *link_data =
2379 		container_of(timer, struct mac80211_hwsim_link_data, beacon_timer);
2380 	struct mac80211_hwsim_data *data =
2381 		container_of(link_data, struct mac80211_hwsim_data,
2382 			     link_data[link_data->link_id]);
2383 	struct ieee80211_hw *hw = data->hw;
2384 	u64 bcn_int = link_data->beacon_int;
2385 
2386 	if (!data->started)
2387 		return HRTIMER_NORESTART;
2388 
2389 	ieee80211_iterate_active_interfaces_atomic(
2390 		hw, IEEE80211_IFACE_ITER_NORMAL,
2391 		mac80211_hwsim_beacon_tx, link_data);
2392 
2393 	/* beacon at new TBTT + beacon interval */
2394 	if (data->bcn_delta) {
2395 		bcn_int -= data->bcn_delta;
2396 		data->bcn_delta = 0;
2397 	}
2398 	hrtimer_forward_now(&link_data->beacon_timer,
2399 			    ns_to_ktime(bcn_int * NSEC_PER_USEC));
2400 	return HRTIMER_RESTART;
2401 }
2402 
2403 static const char * const hwsim_chanwidths[] = {
2404 	[NL80211_CHAN_WIDTH_5] = "ht5",
2405 	[NL80211_CHAN_WIDTH_10] = "ht10",
2406 	[NL80211_CHAN_WIDTH_20_NOHT] = "noht",
2407 	[NL80211_CHAN_WIDTH_20] = "ht20",
2408 	[NL80211_CHAN_WIDTH_40] = "ht40",
2409 	[NL80211_CHAN_WIDTH_80] = "vht80",
2410 	[NL80211_CHAN_WIDTH_80P80] = "vht80p80",
2411 	[NL80211_CHAN_WIDTH_160] = "vht160",
2412 	[NL80211_CHAN_WIDTH_1] = "1MHz",
2413 	[NL80211_CHAN_WIDTH_2] = "2MHz",
2414 	[NL80211_CHAN_WIDTH_4] = "4MHz",
2415 	[NL80211_CHAN_WIDTH_8] = "8MHz",
2416 	[NL80211_CHAN_WIDTH_16] = "16MHz",
2417 	[NL80211_CHAN_WIDTH_320] = "eht320",
2418 };
2419 
2420 static int mac80211_hwsim_config(struct ieee80211_hw *hw, int radio_idx,
2421 				 u32 changed)
2422 {
2423 	struct mac80211_hwsim_data *data = hw->priv;
2424 	struct ieee80211_conf *conf = &hw->conf;
2425 	static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
2426 		[IEEE80211_SMPS_AUTOMATIC] = "auto",
2427 		[IEEE80211_SMPS_OFF] = "off",
2428 		[IEEE80211_SMPS_STATIC] = "static",
2429 		[IEEE80211_SMPS_DYNAMIC] = "dynamic",
2430 	};
2431 	int idx;
2432 
2433 	if (conf->chandef.chan)
2434 		wiphy_dbg(hw->wiphy,
2435 			  "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
2436 			  __func__,
2437 			  conf->chandef.chan->center_freq,
2438 			  conf->chandef.center_freq1,
2439 			  conf->chandef.center_freq2,
2440 			  hwsim_chanwidths[conf->chandef.width],
2441 			  !!(conf->flags & IEEE80211_CONF_IDLE),
2442 			  !!(conf->flags & IEEE80211_CONF_PS),
2443 			  smps_modes[conf->smps_mode]);
2444 	else
2445 		wiphy_dbg(hw->wiphy,
2446 			  "%s (freq=0 idle=%d ps=%d smps=%s)\n",
2447 			  __func__,
2448 			  !!(conf->flags & IEEE80211_CONF_IDLE),
2449 			  !!(conf->flags & IEEE80211_CONF_PS),
2450 			  smps_modes[conf->smps_mode]);
2451 
2452 	data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
2453 
2454 	WARN_ON(conf->chandef.chan && data->use_chanctx);
2455 
2456 	mutex_lock(&data->mutex);
2457 	if (data->scanning && conf->chandef.chan) {
2458 		for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2459 			if (data->survey_data[idx].channel == data->channel) {
2460 				data->survey_data[idx].start =
2461 					data->survey_data[idx].next_start;
2462 				data->survey_data[idx].end = jiffies;
2463 				break;
2464 			}
2465 		}
2466 
2467 		data->channel = conf->chandef.chan;
2468 		data->bw = conf->chandef.width;
2469 
2470 		for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2471 			if (data->survey_data[idx].channel &&
2472 			    data->survey_data[idx].channel != data->channel)
2473 				continue;
2474 			data->survey_data[idx].channel = data->channel;
2475 			data->survey_data[idx].next_start = jiffies;
2476 			break;
2477 		}
2478 	} else {
2479 		data->channel = conf->chandef.chan;
2480 		data->bw = conf->chandef.width;
2481 	}
2482 	mutex_unlock(&data->mutex);
2483 
2484 	for (idx = 0; idx < ARRAY_SIZE(data->link_data); idx++) {
2485 		struct mac80211_hwsim_link_data *link_data =
2486 			&data->link_data[idx];
2487 
2488 		if (!data->started || !link_data->beacon_int) {
2489 			hrtimer_cancel(&link_data->beacon_timer);
2490 		} else if (!hrtimer_active(&link_data->beacon_timer)) {
2491 			u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
2492 			u32 bcn_int = link_data->beacon_int;
2493 			u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2494 
2495 			hrtimer_start(&link_data->beacon_timer,
2496 				      ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2497 				      HRTIMER_MODE_REL_SOFT);
2498 		}
2499 	}
2500 
2501 	return 0;
2502 }
2503 
2504 
2505 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
2506 					    unsigned int changed_flags,
2507 					    unsigned int *total_flags,u64 multicast)
2508 {
2509 	struct mac80211_hwsim_data *data = hw->priv;
2510 
2511 	wiphy_dbg(hw->wiphy, "%s\n", __func__);
2512 
2513 	data->rx_filter = 0;
2514 	if (*total_flags & FIF_ALLMULTI)
2515 		data->rx_filter |= FIF_ALLMULTI;
2516 	if (*total_flags & FIF_MCAST_ACTION)
2517 		data->rx_filter |= FIF_MCAST_ACTION;
2518 
2519 	*total_flags = data->rx_filter;
2520 }
2521 
2522 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
2523 				       struct ieee80211_vif *vif)
2524 {
2525 	unsigned int *count = data;
2526 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2527 
2528 	if (vp->bcn_en)
2529 		(*count)++;
2530 }
2531 
2532 static void mac80211_hwsim_vif_info_changed(struct ieee80211_hw *hw,
2533 					    struct ieee80211_vif *vif,
2534 					    u64 changed)
2535 {
2536 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2537 
2538 	hwsim_check_magic(vif);
2539 
2540 	wiphy_dbg(hw->wiphy, "%s(changed=0x%llx vif->addr=%pM)\n",
2541 		  __func__, changed, vif->addr);
2542 
2543 	if (changed & BSS_CHANGED_ASSOC) {
2544 		wiphy_dbg(hw->wiphy, "  ASSOC: assoc=%d aid=%d\n",
2545 			  vif->cfg.assoc, vif->cfg.aid);
2546 		vp->assoc = vif->cfg.assoc;
2547 		vp->aid = vif->cfg.aid;
2548 	}
2549 
2550 	if (vif->type == NL80211_IFTYPE_STATION &&
2551 	    changed & (BSS_CHANGED_MLD_VALID_LINKS | BSS_CHANGED_MLD_TTLM)) {
2552 		u16 usable_links = ieee80211_vif_usable_links(vif);
2553 
2554 		if (vif->active_links != usable_links)
2555 			ieee80211_set_active_links_async(vif, usable_links);
2556 	}
2557 }
2558 
2559 static void mac80211_hwsim_link_info_changed(struct ieee80211_hw *hw,
2560 					     struct ieee80211_vif *vif,
2561 					     struct ieee80211_bss_conf *info,
2562 					     u64 changed)
2563 {
2564 	struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2565 	struct mac80211_hwsim_data *data = hw->priv;
2566 	unsigned int link_id = info->link_id;
2567 	struct mac80211_hwsim_link_data *link_data = &data->link_data[link_id];
2568 
2569 	hwsim_check_magic(vif);
2570 
2571 	wiphy_dbg(hw->wiphy, "%s(changed=0x%llx vif->addr=%pM, link id %u)\n",
2572 		  __func__, (unsigned long long)changed, vif->addr, link_id);
2573 
2574 	if (changed & BSS_CHANGED_BSSID) {
2575 		wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
2576 			  __func__, info->bssid);
2577 		memcpy(vp->bssid, info->bssid, ETH_ALEN);
2578 	}
2579 
2580 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
2581 		wiphy_dbg(hw->wiphy, "  BCN EN: %d (BI=%u)\n",
2582 			  info->enable_beacon, info->beacon_int);
2583 		vp->bcn_en = info->enable_beacon;
2584 		if (data->started &&
2585 		    !hrtimer_active(&link_data->beacon_timer) &&
2586 		    info->enable_beacon) {
2587 			u64 tsf, until_tbtt;
2588 			u32 bcn_int;
2589 			link_data->beacon_int = info->beacon_int * 1024;
2590 			tsf = mac80211_hwsim_get_tsf(hw, vif);
2591 			bcn_int = link_data->beacon_int;
2592 			until_tbtt = bcn_int - do_div(tsf, bcn_int);
2593 
2594 			hrtimer_start(&link_data->beacon_timer,
2595 				      ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2596 				      HRTIMER_MODE_REL_SOFT);
2597 		} else if (!info->enable_beacon) {
2598 			unsigned int count = 0;
2599 			ieee80211_iterate_active_interfaces_atomic(
2600 				data->hw, IEEE80211_IFACE_ITER_NORMAL,
2601 				mac80211_hwsim_bcn_en_iter, &count);
2602 			wiphy_dbg(hw->wiphy, "  beaconing vifs remaining: %u",
2603 				  count);
2604 			if (count == 0) {
2605 				hrtimer_cancel(&link_data->beacon_timer);
2606 				link_data->beacon_int = 0;
2607 			}
2608 		}
2609 	}
2610 
2611 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2612 		wiphy_dbg(hw->wiphy, "  ERP_CTS_PROT: %d\n",
2613 			  info->use_cts_prot);
2614 	}
2615 
2616 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2617 		wiphy_dbg(hw->wiphy, "  ERP_PREAMBLE: %d\n",
2618 			  info->use_short_preamble);
2619 	}
2620 
2621 	if (changed & BSS_CHANGED_ERP_SLOT) {
2622 		wiphy_dbg(hw->wiphy, "  ERP_SLOT: %d\n", info->use_short_slot);
2623 	}
2624 
2625 	if (changed & BSS_CHANGED_HT) {
2626 		wiphy_dbg(hw->wiphy, "  HT: op_mode=0x%x\n",
2627 			  info->ht_operation_mode);
2628 	}
2629 
2630 	if (changed & BSS_CHANGED_BASIC_RATES) {
2631 		wiphy_dbg(hw->wiphy, "  BASIC_RATES: 0x%llx\n",
2632 			  (unsigned long long) info->basic_rates);
2633 	}
2634 
2635 	if (changed & BSS_CHANGED_TXPOWER)
2636 		wiphy_dbg(hw->wiphy, "  TX Power: %d dBm\n", info->txpower);
2637 }
2638 
2639 static void
2640 mac80211_hwsim_sta_rc_update(struct ieee80211_hw *hw,
2641 			     struct ieee80211_vif *vif,
2642 			     struct ieee80211_link_sta *link_sta,
2643 			     u32 changed)
2644 {
2645 	struct mac80211_hwsim_data *data = hw->priv;
2646 	struct ieee80211_sta *sta = link_sta->sta;
2647 	u32 bw = U32_MAX;
2648 	int link_id;
2649 
2650 	rcu_read_lock();
2651 	for (link_id = 0;
2652 	     link_id < ARRAY_SIZE(vif->link_conf);
2653 	     link_id++) {
2654 		enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
2655 		struct ieee80211_bss_conf *vif_conf;
2656 
2657 		link_sta = rcu_dereference(sta->link[link_id]);
2658 
2659 		if (!link_sta)
2660 			continue;
2661 
2662 		switch (link_sta->bandwidth) {
2663 #define C(_bw) case IEEE80211_STA_RX_BW_##_bw: bw = _bw; break
2664 		C(20);
2665 		C(40);
2666 		C(80);
2667 		C(160);
2668 		C(320);
2669 #undef C
2670 		}
2671 
2672 		if (!data->use_chanctx) {
2673 			confbw = data->bw;
2674 		} else {
2675 			struct ieee80211_chanctx_conf *chanctx_conf;
2676 
2677 			vif_conf = rcu_dereference(vif->link_conf[link_id]);
2678 			if (WARN_ON(!vif_conf))
2679 				continue;
2680 
2681 			chanctx_conf = rcu_dereference(vif_conf->chanctx_conf);
2682 
2683 			if (!WARN_ON(!chanctx_conf))
2684 				confbw = chanctx_conf->def.width;
2685 		}
2686 
2687 		WARN(bw > hwsim_get_chanwidth(confbw),
2688 		     "intf %pM [link=%d]: bad STA %pM bandwidth %d MHz (%d) > channel config %d MHz (%d)\n",
2689 		     vif->addr, link_id, sta->addr, bw, sta->deflink.bandwidth,
2690 		     hwsim_get_chanwidth(data->bw), data->bw);
2691 
2692 
2693 	}
2694 	rcu_read_unlock();
2695 
2696 
2697 }
2698 
2699 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2700 				  struct ieee80211_vif *vif,
2701 				  struct ieee80211_sta *sta)
2702 {
2703 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
2704 
2705 	hwsim_check_magic(vif);
2706 	hwsim_set_sta_magic(sta);
2707 	mac80211_hwsim_sta_rc_update(hw, vif, &sta->deflink, 0);
2708 
2709 	if (sta->valid_links) {
2710 		WARN(hweight16(sta->valid_links) > 1,
2711 		     "expect to add STA with single link, have 0x%x\n",
2712 		     sta->valid_links);
2713 		sp->active_links_rx = sta->valid_links;
2714 	}
2715 
2716 	return 0;
2717 }
2718 
2719 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2720 				     struct ieee80211_vif *vif,
2721 				     struct ieee80211_sta *sta)
2722 {
2723 	hwsim_check_magic(vif);
2724 	hwsim_clear_sta_magic(sta);
2725 
2726 	return 0;
2727 }
2728 
2729 static int mac80211_hwsim_sta_state(struct ieee80211_hw *hw,
2730 				    struct ieee80211_vif *vif,
2731 				    struct ieee80211_sta *sta,
2732 				    enum ieee80211_sta_state old_state,
2733 				    enum ieee80211_sta_state new_state)
2734 {
2735 	if (new_state == IEEE80211_STA_NOTEXIST)
2736 		return mac80211_hwsim_sta_remove(hw, vif, sta);
2737 
2738 	if (old_state == IEEE80211_STA_NOTEXIST)
2739 		return mac80211_hwsim_sta_add(hw, vif, sta);
2740 
2741 	/*
2742 	 * in an MLO connection, when client is authorized
2743 	 * (AP station marked as such), enable all links
2744 	 */
2745 	if (ieee80211_vif_is_mld(vif) &&
2746 	    vif->type == NL80211_IFTYPE_STATION &&
2747 	    new_state == IEEE80211_STA_AUTHORIZED && !sta->tdls)
2748 		ieee80211_set_active_links_async(vif,
2749 						 ieee80211_vif_usable_links(vif));
2750 
2751 	return 0;
2752 }
2753 
2754 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2755 				      struct ieee80211_vif *vif,
2756 				      enum sta_notify_cmd cmd,
2757 				      struct ieee80211_sta *sta)
2758 {
2759 	hwsim_check_magic(vif);
2760 
2761 	switch (cmd) {
2762 	case STA_NOTIFY_SLEEP:
2763 	case STA_NOTIFY_AWAKE:
2764 		/* TODO: make good use of these flags */
2765 		break;
2766 	default:
2767 		WARN(1, "Invalid sta notify: %d\n", cmd);
2768 		break;
2769 	}
2770 }
2771 
2772 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2773 				  struct ieee80211_sta *sta,
2774 				  bool set)
2775 {
2776 	hwsim_check_sta_magic(sta);
2777 	return 0;
2778 }
2779 
2780 static int mac80211_hwsim_conf_tx(struct ieee80211_hw *hw,
2781 				  struct ieee80211_vif *vif,
2782 				  unsigned int link_id, u16 queue,
2783 				  const struct ieee80211_tx_queue_params *params)
2784 {
2785 	wiphy_dbg(hw->wiphy,
2786 		  "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2787 		  __func__, queue,
2788 		  params->txop, params->cw_min,
2789 		  params->cw_max, params->aifs);
2790 	return 0;
2791 }
2792 
2793 static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2794 				     struct survey_info *survey)
2795 {
2796 	struct mac80211_hwsim_data *hwsim = hw->priv;
2797 
2798 	if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2799 		return -ENOENT;
2800 
2801 	mutex_lock(&hwsim->mutex);
2802 	survey->channel = hwsim->survey_data[idx].channel;
2803 	if (!survey->channel) {
2804 		mutex_unlock(&hwsim->mutex);
2805 		return -ENOENT;
2806 	}
2807 
2808 	/*
2809 	 * Magically conjured dummy values --- this is only ok for simulated hardware.
2810 	 *
2811 	 * A real driver which cannot determine real values noise MUST NOT
2812 	 * report any, especially not a magically conjured ones :-)
2813 	 */
2814 	survey->filled = SURVEY_INFO_NOISE_DBM |
2815 			 SURVEY_INFO_TIME |
2816 			 SURVEY_INFO_TIME_BUSY;
2817 	survey->noise = -92;
2818 	survey->time =
2819 		jiffies_to_msecs(hwsim->survey_data[idx].end -
2820 				 hwsim->survey_data[idx].start);
2821 	/* report 12.5% of channel time is used */
2822 	survey->time_busy = survey->time/8;
2823 	mutex_unlock(&hwsim->mutex);
2824 
2825 	return 0;
2826 }
2827 
2828 static enum ieee80211_neg_ttlm_res
2829 mac80211_hwsim_can_neg_ttlm(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2830 			    struct ieee80211_neg_ttlm *neg_ttlm)
2831 {
2832 	u32 i;
2833 
2834 	/* For testing purposes, accept if all TIDs are mapped to the same links
2835 	 * set, otherwise reject.
2836 	 */
2837 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
2838 		if (neg_ttlm->downlink[i] != neg_ttlm->uplink[i] ||
2839 		    neg_ttlm->downlink[i] != neg_ttlm->downlink[0])
2840 			return NEG_TTLM_RES_REJECT;
2841 	}
2842 
2843 	return NEG_TTLM_RES_ACCEPT;
2844 }
2845 
2846 #ifdef CONFIG_NL80211_TESTMODE
2847 /*
2848  * This section contains example code for using netlink
2849  * attributes with the testmode command in nl80211.
2850  */
2851 
2852 /* These enums need to be kept in sync with userspace */
2853 enum hwsim_testmode_attr {
2854 	__HWSIM_TM_ATTR_INVALID	= 0,
2855 	HWSIM_TM_ATTR_CMD	= 1,
2856 	HWSIM_TM_ATTR_PS	= 2,
2857 
2858 	/* keep last */
2859 	__HWSIM_TM_ATTR_AFTER_LAST,
2860 	HWSIM_TM_ATTR_MAX	= __HWSIM_TM_ATTR_AFTER_LAST - 1
2861 };
2862 
2863 enum hwsim_testmode_cmd {
2864 	HWSIM_TM_CMD_SET_PS		= 0,
2865 	HWSIM_TM_CMD_GET_PS		= 1,
2866 	HWSIM_TM_CMD_STOP_QUEUES	= 2,
2867 	HWSIM_TM_CMD_WAKE_QUEUES	= 3,
2868 };
2869 
2870 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2871 	[HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2872 	[HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2873 };
2874 
2875 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2876 				       struct ieee80211_vif *vif,
2877 				       void *data, int len)
2878 {
2879 	struct mac80211_hwsim_data *hwsim = hw->priv;
2880 	struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2881 	struct sk_buff *skb;
2882 	int err, ps;
2883 
2884 	err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2885 				   hwsim_testmode_policy, NULL);
2886 	if (err)
2887 		return err;
2888 
2889 	if (!tb[HWSIM_TM_ATTR_CMD])
2890 		return -EINVAL;
2891 
2892 	switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2893 	case HWSIM_TM_CMD_SET_PS:
2894 		if (!tb[HWSIM_TM_ATTR_PS])
2895 			return -EINVAL;
2896 		ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2897 		return hwsim_fops_ps_write(hwsim, ps);
2898 	case HWSIM_TM_CMD_GET_PS:
2899 		skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2900 						nla_total_size(sizeof(u32)));
2901 		if (!skb)
2902 			return -ENOMEM;
2903 		if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2904 			goto nla_put_failure;
2905 		return cfg80211_testmode_reply(skb);
2906 	case HWSIM_TM_CMD_STOP_QUEUES:
2907 		ieee80211_stop_queues(hw);
2908 		return 0;
2909 	case HWSIM_TM_CMD_WAKE_QUEUES:
2910 		ieee80211_wake_queues(hw);
2911 		return 0;
2912 	default:
2913 		return -EOPNOTSUPP;
2914 	}
2915 
2916  nla_put_failure:
2917 	kfree_skb(skb);
2918 	return -ENOBUFS;
2919 }
2920 #endif
2921 
2922 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2923 				       struct ieee80211_vif *vif,
2924 				       struct ieee80211_ampdu_params *params)
2925 {
2926 	struct ieee80211_sta *sta = params->sta;
2927 	enum ieee80211_ampdu_mlme_action action = params->action;
2928 	u16 tid = params->tid;
2929 
2930 	switch (action) {
2931 	case IEEE80211_AMPDU_TX_START:
2932 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2933 	case IEEE80211_AMPDU_TX_STOP_CONT:
2934 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
2935 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2936 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2937 		break;
2938 	case IEEE80211_AMPDU_TX_OPERATIONAL:
2939 		break;
2940 	case IEEE80211_AMPDU_RX_START:
2941 	case IEEE80211_AMPDU_RX_STOP:
2942 		break;
2943 	default:
2944 		return -EOPNOTSUPP;
2945 	}
2946 
2947 	return 0;
2948 }
2949 
2950 static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2951 				 struct ieee80211_vif *vif,
2952 				 u32 queues, bool drop)
2953 {
2954 	/* Not implemented, queues only on kernel side */
2955 }
2956 
2957 static void hw_scan_work(struct work_struct *work)
2958 {
2959 	struct mac80211_hwsim_data *hwsim =
2960 		container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2961 	struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2962 	int dwell, i;
2963 
2964 	mutex_lock(&hwsim->mutex);
2965 	if (hwsim->scan_chan_idx >= req->n_channels) {
2966 		struct cfg80211_scan_info info = {
2967 			.aborted = false,
2968 		};
2969 
2970 		wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2971 		ieee80211_scan_completed(hwsim->hw, &info);
2972 		hwsim->hw_scan_request = NULL;
2973 		hwsim->hw_scan_vif = NULL;
2974 		hwsim->tmp_chan = NULL;
2975 		mutex_unlock(&hwsim->mutex);
2976 		mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2977 					     false);
2978 		return;
2979 	}
2980 
2981 	wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2982 		  req->channels[hwsim->scan_chan_idx]->center_freq);
2983 
2984 	hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2985 	if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2986 				      IEEE80211_CHAN_RADAR) ||
2987 	    !req->n_ssids) {
2988 		dwell = 120;
2989 	} else {
2990 		dwell = 30;
2991 		/* send probes */
2992 		for (i = 0; i < req->n_ssids; i++) {
2993 			struct sk_buff *probe;
2994 			struct ieee80211_mgmt *mgmt;
2995 
2996 			probe = ieee80211_probereq_get(hwsim->hw,
2997 						       hwsim->scan_addr,
2998 						       req->ssids[i].ssid,
2999 						       req->ssids[i].ssid_len,
3000 						       req->ie_len);
3001 			if (!probe)
3002 				continue;
3003 
3004 			mgmt = (struct ieee80211_mgmt *) probe->data;
3005 			memcpy(mgmt->da, req->bssid, ETH_ALEN);
3006 			memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
3007 
3008 			if (req->ie_len)
3009 				skb_put_data(probe, req->ie, req->ie_len);
3010 
3011 			rcu_read_lock();
3012 			if (!ieee80211_tx_prepare_skb(hwsim->hw,
3013 						      hwsim->hw_scan_vif,
3014 						      probe,
3015 						      hwsim->tmp_chan->band,
3016 						      NULL)) {
3017 				rcu_read_unlock();
3018 				kfree_skb(probe);
3019 				continue;
3020 			}
3021 
3022 			local_bh_disable();
3023 			mac80211_hwsim_tx_frame(hwsim->hw, probe,
3024 						hwsim->tmp_chan);
3025 			rcu_read_unlock();
3026 			local_bh_enable();
3027 		}
3028 	}
3029 	ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
3030 				     msecs_to_jiffies(dwell));
3031 	hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
3032 	hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
3033 	hwsim->survey_data[hwsim->scan_chan_idx].end =
3034 		jiffies + msecs_to_jiffies(dwell);
3035 	hwsim->scan_chan_idx++;
3036 	mutex_unlock(&hwsim->mutex);
3037 }
3038 
3039 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
3040 				  struct ieee80211_vif *vif,
3041 				  struct ieee80211_scan_request *hw_req)
3042 {
3043 	struct mac80211_hwsim_data *hwsim = hw->priv;
3044 	struct cfg80211_scan_request *req = &hw_req->req;
3045 
3046 	mutex_lock(&hwsim->mutex);
3047 	if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
3048 		mutex_unlock(&hwsim->mutex);
3049 		return -EBUSY;
3050 	}
3051 	hwsim->hw_scan_request = req;
3052 	hwsim->hw_scan_vif = vif;
3053 	hwsim->scan_chan_idx = 0;
3054 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
3055 		get_random_mask_addr(hwsim->scan_addr,
3056 				     hw_req->req.mac_addr,
3057 				     hw_req->req.mac_addr_mask);
3058 	else
3059 		memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
3060 	memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
3061 	mutex_unlock(&hwsim->mutex);
3062 
3063 	mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
3064 	wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
3065 
3066 	ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
3067 
3068 	return 0;
3069 }
3070 
3071 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
3072 					  struct ieee80211_vif *vif)
3073 {
3074 	struct mac80211_hwsim_data *hwsim = hw->priv;
3075 	struct cfg80211_scan_info info = {
3076 		.aborted = true,
3077 	};
3078 
3079 	wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
3080 
3081 	cancel_delayed_work_sync(&hwsim->hw_scan);
3082 
3083 	mutex_lock(&hwsim->mutex);
3084 	ieee80211_scan_completed(hwsim->hw, &info);
3085 	hwsim->tmp_chan = NULL;
3086 	hwsim->hw_scan_request = NULL;
3087 	hwsim->hw_scan_vif = NULL;
3088 	mutex_unlock(&hwsim->mutex);
3089 }
3090 
3091 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
3092 				   struct ieee80211_vif *vif,
3093 				   const u8 *mac_addr)
3094 {
3095 	struct mac80211_hwsim_data *hwsim = hw->priv;
3096 
3097 	mutex_lock(&hwsim->mutex);
3098 
3099 	if (hwsim->scanning) {
3100 		pr_debug("two hwsim sw_scans detected!\n");
3101 		goto out;
3102 	}
3103 
3104 	pr_debug("hwsim sw_scan request, prepping stuff\n");
3105 
3106 	memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
3107 	mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
3108 	hwsim->scanning = true;
3109 	memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
3110 
3111 out:
3112 	mutex_unlock(&hwsim->mutex);
3113 }
3114 
3115 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
3116 					    struct ieee80211_vif *vif)
3117 {
3118 	struct mac80211_hwsim_data *hwsim = hw->priv;
3119 
3120 	mutex_lock(&hwsim->mutex);
3121 
3122 	pr_debug("hwsim sw_scan_complete\n");
3123 	hwsim->scanning = false;
3124 	mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
3125 	eth_zero_addr(hwsim->scan_addr);
3126 
3127 	mutex_unlock(&hwsim->mutex);
3128 }
3129 
3130 static void hw_roc_start(struct work_struct *work)
3131 {
3132 	struct mac80211_hwsim_data *hwsim =
3133 		container_of(work, struct mac80211_hwsim_data, roc_start.work);
3134 
3135 	mutex_lock(&hwsim->mutex);
3136 
3137 	wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
3138 	hwsim->tmp_chan = hwsim->roc_chan;
3139 	ieee80211_ready_on_channel(hwsim->hw);
3140 
3141 	ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
3142 				     msecs_to_jiffies(hwsim->roc_duration));
3143 
3144 	mutex_unlock(&hwsim->mutex);
3145 }
3146 
3147 static void hw_roc_done(struct work_struct *work)
3148 {
3149 	struct mac80211_hwsim_data *hwsim =
3150 		container_of(work, struct mac80211_hwsim_data, roc_done.work);
3151 
3152 	mutex_lock(&hwsim->mutex);
3153 	ieee80211_remain_on_channel_expired(hwsim->hw);
3154 	hwsim->tmp_chan = NULL;
3155 	mutex_unlock(&hwsim->mutex);
3156 
3157 	wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
3158 }
3159 
3160 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
3161 			      struct ieee80211_vif *vif,
3162 			      struct ieee80211_channel *chan,
3163 			      int duration,
3164 			      enum ieee80211_roc_type type)
3165 {
3166 	struct mac80211_hwsim_data *hwsim = hw->priv;
3167 
3168 	mutex_lock(&hwsim->mutex);
3169 	if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
3170 		mutex_unlock(&hwsim->mutex);
3171 		return -EBUSY;
3172 	}
3173 
3174 	hwsim->roc_chan = chan;
3175 	hwsim->roc_duration = duration;
3176 	mutex_unlock(&hwsim->mutex);
3177 
3178 	wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
3179 		  chan->center_freq, duration);
3180 	ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
3181 
3182 	return 0;
3183 }
3184 
3185 static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
3186 			       struct ieee80211_vif *vif)
3187 {
3188 	struct mac80211_hwsim_data *hwsim = hw->priv;
3189 
3190 	cancel_delayed_work_sync(&hwsim->roc_start);
3191 	cancel_delayed_work_sync(&hwsim->roc_done);
3192 
3193 	mutex_lock(&hwsim->mutex);
3194 	hwsim->tmp_chan = NULL;
3195 	mutex_unlock(&hwsim->mutex);
3196 
3197 	wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
3198 
3199 	return 0;
3200 }
3201 
3202 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
3203 				      struct ieee80211_chanctx_conf *ctx)
3204 {
3205 	hwsim_set_chanctx_magic(ctx);
3206 	wiphy_dbg(hw->wiphy,
3207 		  "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
3208 		  ctx->def.chan->center_freq, ctx->def.width,
3209 		  ctx->def.center_freq1, ctx->def.center_freq2);
3210 	return 0;
3211 }
3212 
3213 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
3214 					  struct ieee80211_chanctx_conf *ctx)
3215 {
3216 	wiphy_dbg(hw->wiphy,
3217 		  "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
3218 		  ctx->def.chan->center_freq, ctx->def.width,
3219 		  ctx->def.center_freq1, ctx->def.center_freq2);
3220 	hwsim_check_chanctx_magic(ctx);
3221 	hwsim_clear_chanctx_magic(ctx);
3222 }
3223 
3224 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
3225 					  struct ieee80211_chanctx_conf *ctx,
3226 					  u32 changed)
3227 {
3228 	hwsim_check_chanctx_magic(ctx);
3229 	wiphy_dbg(hw->wiphy,
3230 		  "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
3231 		  ctx->def.chan->center_freq, ctx->def.width,
3232 		  ctx->def.center_freq1, ctx->def.center_freq2);
3233 }
3234 
3235 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
3236 					     struct ieee80211_vif *vif,
3237 					     struct ieee80211_bss_conf *link_conf,
3238 					     struct ieee80211_chanctx_conf *ctx)
3239 {
3240 	hwsim_check_magic(vif);
3241 	hwsim_check_chanctx_magic(ctx);
3242 
3243 	/* if we activate a link while already associated wake it up */
3244 	if (vif->type == NL80211_IFTYPE_STATION && vif->cfg.assoc) {
3245 		struct sk_buff *skb;
3246 
3247 		skb = ieee80211_nullfunc_get(hw, vif, link_conf->link_id, true);
3248 		if (skb) {
3249 			local_bh_disable();
3250 			mac80211_hwsim_tx_frame(hw, skb, ctx->def.chan);
3251 			local_bh_enable();
3252 		}
3253 	}
3254 
3255 	return 0;
3256 }
3257 
3258 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
3259 						struct ieee80211_vif *vif,
3260 						struct ieee80211_bss_conf *link_conf,
3261 						struct ieee80211_chanctx_conf *ctx)
3262 {
3263 	hwsim_check_magic(vif);
3264 	hwsim_check_chanctx_magic(ctx);
3265 
3266 	/* if we deactivate a link while associated suspend it first */
3267 	if (vif->type == NL80211_IFTYPE_STATION && vif->cfg.assoc) {
3268 		struct sk_buff *skb;
3269 
3270 		skb = ieee80211_nullfunc_get(hw, vif, link_conf->link_id, true);
3271 		if (skb) {
3272 			struct ieee80211_hdr *hdr = (void *)skb->data;
3273 
3274 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
3275 
3276 			local_bh_disable();
3277 			mac80211_hwsim_tx_frame(hw, skb, ctx->def.chan);
3278 			local_bh_enable();
3279 		}
3280 	}
3281 }
3282 
3283 static int mac80211_hwsim_switch_vif_chanctx(struct ieee80211_hw *hw,
3284 					     struct ieee80211_vif_chanctx_switch *vifs,
3285 					     int n_vifs,
3286 					     enum ieee80211_chanctx_switch_mode mode)
3287 {
3288 	int i;
3289 
3290 	if (n_vifs <= 0)
3291 		return -EINVAL;
3292 
3293 	wiphy_dbg(hw->wiphy,
3294 		  "switch vif channel context mode: %u\n", mode);
3295 
3296 	for (i = 0; i < n_vifs; i++) {
3297 		hwsim_check_chanctx_magic(vifs[i].old_ctx);
3298 		wiphy_dbg(hw->wiphy,
3299 			  "switch vif channel context: %d MHz/width: %d/cfreqs:%d/%d MHz -> %d MHz/width: %d/cfreqs:%d/%d MHz\n",
3300 			  vifs[i].old_ctx->def.chan->center_freq,
3301 			  vifs[i].old_ctx->def.width,
3302 			  vifs[i].old_ctx->def.center_freq1,
3303 			  vifs[i].old_ctx->def.center_freq2,
3304 			  vifs[i].new_ctx->def.chan->center_freq,
3305 			  vifs[i].new_ctx->def.width,
3306 			  vifs[i].new_ctx->def.center_freq1,
3307 			  vifs[i].new_ctx->def.center_freq2);
3308 
3309 		switch (mode) {
3310 		case CHANCTX_SWMODE_REASSIGN_VIF:
3311 			hwsim_check_chanctx_magic(vifs[i].new_ctx);
3312 			break;
3313 		case CHANCTX_SWMODE_SWAP_CONTEXTS:
3314 			hwsim_set_chanctx_magic(vifs[i].new_ctx);
3315 			hwsim_clear_chanctx_magic(vifs[i].old_ctx);
3316 			break;
3317 		default:
3318 			WARN(1, "Invalid mode %d\n", mode);
3319 		}
3320 	}
3321 	return 0;
3322 }
3323 
3324 static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
3325 	"tx_pkts_nic",
3326 	"tx_bytes_nic",
3327 	"rx_pkts_nic",
3328 	"rx_bytes_nic",
3329 	"d_tx_dropped",
3330 	"d_tx_failed",
3331 	"d_ps_mode",
3332 	"d_group",
3333 };
3334 
3335 #define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
3336 
3337 static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
3338 					  struct ieee80211_vif *vif,
3339 					  u32 sset, u8 *data)
3340 {
3341 	if (sset == ETH_SS_STATS)
3342 		memcpy(data, mac80211_hwsim_gstrings_stats,
3343 		       sizeof(mac80211_hwsim_gstrings_stats));
3344 }
3345 
3346 static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
3347 					    struct ieee80211_vif *vif, int sset)
3348 {
3349 	if (sset == ETH_SS_STATS)
3350 		return MAC80211_HWSIM_SSTATS_LEN;
3351 	return 0;
3352 }
3353 
3354 static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
3355 					struct ieee80211_vif *vif,
3356 					struct ethtool_stats *stats, u64 *data)
3357 {
3358 	struct mac80211_hwsim_data *ar = hw->priv;
3359 	int i = 0;
3360 
3361 	data[i++] = ar->tx_pkts;
3362 	data[i++] = ar->tx_bytes;
3363 	data[i++] = ar->rx_pkts;
3364 	data[i++] = ar->rx_bytes;
3365 	data[i++] = ar->tx_dropped;
3366 	data[i++] = ar->tx_failed;
3367 	data[i++] = ar->ps;
3368 	data[i++] = ar->group;
3369 
3370 	WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
3371 }
3372 
3373 static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
3374 {
3375 	return 1;
3376 }
3377 
3378 static int mac80211_hwsim_set_rts_threshold(struct ieee80211_hw *hw,
3379 					    int radio_idx, u32 value)
3380 {
3381 	return -EOPNOTSUPP;
3382 }
3383 
3384 static int mac80211_hwsim_change_vif_links(struct ieee80211_hw *hw,
3385 					   struct ieee80211_vif *vif,
3386 					   u16 old_links, u16 new_links,
3387 					   struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
3388 {
3389 	unsigned long rem = old_links & ~new_links;
3390 	unsigned long add = new_links & ~old_links;
3391 	int i;
3392 
3393 	if (!old_links)
3394 		rem |= BIT(0);
3395 	if (!new_links)
3396 		add |= BIT(0);
3397 
3398 	for_each_set_bit(i, &rem, IEEE80211_MLD_MAX_NUM_LINKS)
3399 		mac80211_hwsim_config_mac_nl(hw, old[i]->addr, false);
3400 
3401 	for_each_set_bit(i, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
3402 		struct ieee80211_bss_conf *link_conf;
3403 
3404 		link_conf = link_conf_dereference_protected(vif, i);
3405 		if (WARN_ON(!link_conf))
3406 			continue;
3407 
3408 		mac80211_hwsim_config_mac_nl(hw, link_conf->addr, true);
3409 	}
3410 
3411 	return 0;
3412 }
3413 
3414 static int mac80211_hwsim_change_sta_links(struct ieee80211_hw *hw,
3415 					   struct ieee80211_vif *vif,
3416 					   struct ieee80211_sta *sta,
3417 					   u16 old_links, u16 new_links)
3418 {
3419 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
3420 
3421 	hwsim_check_sta_magic(sta);
3422 
3423 	if (vif->type == NL80211_IFTYPE_STATION)
3424 		sp->active_links_rx = new_links;
3425 
3426 	return 0;
3427 }
3428 
3429 static int mac80211_hwsim_send_pmsr_ftm_request_peer(struct sk_buff *msg,
3430 						     struct cfg80211_pmsr_ftm_request_peer *request)
3431 {
3432 	struct nlattr *ftm;
3433 
3434 	if (!request->requested)
3435 		return -EINVAL;
3436 
3437 	ftm = nla_nest_start(msg, NL80211_PMSR_TYPE_FTM);
3438 	if (!ftm)
3439 		return -ENOBUFS;
3440 
3441 	if (nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, request->preamble))
3442 		return -ENOBUFS;
3443 
3444 	if (nla_put_u16(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD, request->burst_period))
3445 		return -ENOBUFS;
3446 
3447 	if (request->asap && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_ASAP))
3448 		return -ENOBUFS;
3449 
3450 	if (request->request_lci && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI))
3451 		return -ENOBUFS;
3452 
3453 	if (request->request_civicloc &&
3454 	    nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC))
3455 		return -ENOBUFS;
3456 
3457 	if (request->trigger_based && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED))
3458 		return -ENOBUFS;
3459 
3460 	if (request->non_trigger_based &&
3461 	    nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED))
3462 		return -ENOBUFS;
3463 
3464 	if (request->lmr_feedback && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK))
3465 		return -ENOBUFS;
3466 
3467 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP, request->num_bursts_exp))
3468 		return -ENOBUFS;
3469 
3470 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION, request->burst_duration))
3471 		return -ENOBUFS;
3472 
3473 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST, request->ftms_per_burst))
3474 		return -ENOBUFS;
3475 
3476 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES, request->ftmr_retries))
3477 		return -ENOBUFS;
3478 
3479 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION, request->burst_duration))
3480 		return -ENOBUFS;
3481 
3482 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BSS_COLOR, request->bss_color))
3483 		return -ENOBUFS;
3484 
3485 	nla_nest_end(msg, ftm);
3486 
3487 	return 0;
3488 }
3489 
3490 static int mac80211_hwsim_send_pmsr_request_peer(struct sk_buff *msg,
3491 						 struct cfg80211_pmsr_request_peer *request)
3492 {
3493 	struct nlattr *peer, *chandef, *req, *data;
3494 	int err;
3495 
3496 	peer = nla_nest_start(msg, NL80211_PMSR_ATTR_PEERS);
3497 	if (!peer)
3498 		return -ENOBUFS;
3499 
3500 	if (nla_put(msg, NL80211_PMSR_PEER_ATTR_ADDR, ETH_ALEN,
3501 		    request->addr))
3502 		return -ENOBUFS;
3503 
3504 	chandef = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_CHAN);
3505 	if (!chandef)
3506 		return -ENOBUFS;
3507 
3508 	err = nl80211_send_chandef(msg, &request->chandef);
3509 	if (err)
3510 		return err;
3511 
3512 	nla_nest_end(msg, chandef);
3513 
3514 	req = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_REQ);
3515 	if (!req)
3516 		return -ENOBUFS;
3517 
3518 	if (request->report_ap_tsf && nla_put_flag(msg, NL80211_PMSR_REQ_ATTR_GET_AP_TSF))
3519 		return -ENOBUFS;
3520 
3521 	data = nla_nest_start(msg, NL80211_PMSR_REQ_ATTR_DATA);
3522 	if (!data)
3523 		return -ENOBUFS;
3524 
3525 	err = mac80211_hwsim_send_pmsr_ftm_request_peer(msg, &request->ftm);
3526 	if (err)
3527 		return err;
3528 
3529 	nla_nest_end(msg, data);
3530 	nla_nest_end(msg, req);
3531 	nla_nest_end(msg, peer);
3532 
3533 	return 0;
3534 }
3535 
3536 static int mac80211_hwsim_send_pmsr_request(struct sk_buff *msg,
3537 					    struct cfg80211_pmsr_request *request)
3538 {
3539 	struct nlattr *pmsr;
3540 	int err;
3541 
3542 	pmsr = nla_nest_start(msg, NL80211_ATTR_PEER_MEASUREMENTS);
3543 	if (!pmsr)
3544 		return -ENOBUFS;
3545 
3546 	if (nla_put_u32(msg, NL80211_ATTR_TIMEOUT, request->timeout))
3547 		return -ENOBUFS;
3548 
3549 	if (!is_zero_ether_addr(request->mac_addr)) {
3550 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, request->mac_addr))
3551 			return -ENOBUFS;
3552 		if (nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, request->mac_addr_mask))
3553 			return -ENOBUFS;
3554 	}
3555 
3556 	for (int i = 0; i < request->n_peers; i++) {
3557 		err = mac80211_hwsim_send_pmsr_request_peer(msg, &request->peers[i]);
3558 		if (err)
3559 			return err;
3560 	}
3561 
3562 	nla_nest_end(msg, pmsr);
3563 
3564 	return 0;
3565 }
3566 
3567 static int mac80211_hwsim_start_pmsr(struct ieee80211_hw *hw,
3568 				     struct ieee80211_vif *vif,
3569 				     struct cfg80211_pmsr_request *request)
3570 {
3571 	struct mac80211_hwsim_data *data;
3572 	struct sk_buff *skb = NULL;
3573 	struct nlattr *pmsr;
3574 	void *msg_head;
3575 	u32 _portid;
3576 	int err = 0;
3577 
3578 	data = hw->priv;
3579 	_portid = READ_ONCE(data->wmediumd);
3580 	if (!_portid && !hwsim_virtio_enabled)
3581 		return -EOPNOTSUPP;
3582 
3583 	mutex_lock(&data->mutex);
3584 
3585 	if (data->pmsr_request) {
3586 		err = -EBUSY;
3587 		goto out_free;
3588 	}
3589 
3590 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3591 
3592 	if (!skb) {
3593 		err = -ENOMEM;
3594 		goto out_free;
3595 	}
3596 
3597 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0, HWSIM_CMD_START_PMSR);
3598 
3599 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
3600 		    ETH_ALEN, data->addresses[1].addr)) {
3601 		err = -ENOMEM;
3602 		goto out_free;
3603 	}
3604 
3605 	pmsr = nla_nest_start(skb, HWSIM_ATTR_PMSR_REQUEST);
3606 	if (!pmsr) {
3607 		err = -ENOMEM;
3608 		goto out_free;
3609 	}
3610 
3611 	err = mac80211_hwsim_send_pmsr_request(skb, request);
3612 	if (err)
3613 		goto out_free;
3614 
3615 	nla_nest_end(skb, pmsr);
3616 
3617 	genlmsg_end(skb, msg_head);
3618 	if (hwsim_virtio_enabled)
3619 		hwsim_tx_virtio(data, skb);
3620 	else
3621 		hwsim_unicast_netgroup(data, skb, _portid);
3622 
3623 	data->pmsr_request = request;
3624 	data->pmsr_request_wdev = ieee80211_vif_to_wdev(vif);
3625 
3626 out_free:
3627 	if (err && skb)
3628 		nlmsg_free(skb);
3629 
3630 	mutex_unlock(&data->mutex);
3631 	return err;
3632 }
3633 
3634 static void mac80211_hwsim_abort_pmsr(struct ieee80211_hw *hw,
3635 				      struct ieee80211_vif *vif,
3636 				      struct cfg80211_pmsr_request *request)
3637 {
3638 	struct mac80211_hwsim_data *data;
3639 	struct sk_buff *skb = NULL;
3640 	struct nlattr *pmsr;
3641 	void *msg_head;
3642 	u32 _portid;
3643 	int err = 0;
3644 
3645 	data = hw->priv;
3646 	_portid = READ_ONCE(data->wmediumd);
3647 	if (!_portid && !hwsim_virtio_enabled)
3648 		return;
3649 
3650 	mutex_lock(&data->mutex);
3651 
3652 	if (data->pmsr_request != request) {
3653 		err = -EINVAL;
3654 		goto out;
3655 	}
3656 
3657 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3658 	if (!skb) {
3659 		err = -ENOMEM;
3660 		goto out;
3661 	}
3662 
3663 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0, HWSIM_CMD_ABORT_PMSR);
3664 
3665 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER, ETH_ALEN, data->addresses[1].addr))
3666 		goto out;
3667 
3668 	pmsr = nla_nest_start(skb, HWSIM_ATTR_PMSR_REQUEST);
3669 	if (!pmsr) {
3670 		err = -ENOMEM;
3671 		goto out;
3672 	}
3673 
3674 	err = mac80211_hwsim_send_pmsr_request(skb, request);
3675 	if (err)
3676 		goto out;
3677 
3678 	err = nla_nest_end(skb, pmsr);
3679 	if (err)
3680 		goto out;
3681 
3682 	genlmsg_end(skb, msg_head);
3683 	if (hwsim_virtio_enabled)
3684 		hwsim_tx_virtio(data, skb);
3685 	else
3686 		hwsim_unicast_netgroup(data, skb, _portid);
3687 
3688 out:
3689 	if (err && skb)
3690 		nlmsg_free(skb);
3691 
3692 	mutex_unlock(&data->mutex);
3693 }
3694 
3695 static int mac80211_hwsim_parse_rate_info(struct nlattr *rateattr,
3696 					  struct rate_info *rate_info,
3697 					  struct genl_info *info)
3698 {
3699 	struct nlattr *tb[HWSIM_RATE_INFO_ATTR_MAX + 1];
3700 	int ret;
3701 
3702 	ret = nla_parse_nested(tb, HWSIM_RATE_INFO_ATTR_MAX,
3703 			       rateattr, hwsim_rate_info_policy, info->extack);
3704 	if (ret)
3705 		return ret;
3706 
3707 	if (tb[HWSIM_RATE_INFO_ATTR_FLAGS])
3708 		rate_info->flags = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_FLAGS]);
3709 
3710 	if (tb[HWSIM_RATE_INFO_ATTR_MCS])
3711 		rate_info->mcs = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_MCS]);
3712 
3713 	if (tb[HWSIM_RATE_INFO_ATTR_LEGACY])
3714 		rate_info->legacy = nla_get_u16(tb[HWSIM_RATE_INFO_ATTR_LEGACY]);
3715 
3716 	if (tb[HWSIM_RATE_INFO_ATTR_NSS])
3717 		rate_info->nss = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_NSS]);
3718 
3719 	if (tb[HWSIM_RATE_INFO_ATTR_BW])
3720 		rate_info->bw = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_BW]);
3721 
3722 	if (tb[HWSIM_RATE_INFO_ATTR_HE_GI])
3723 		rate_info->he_gi = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_GI]);
3724 
3725 	if (tb[HWSIM_RATE_INFO_ATTR_HE_DCM])
3726 		rate_info->he_dcm = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_DCM]);
3727 
3728 	if (tb[HWSIM_RATE_INFO_ATTR_HE_RU_ALLOC])
3729 		rate_info->he_ru_alloc =
3730 			nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_RU_ALLOC]);
3731 
3732 	if (tb[HWSIM_RATE_INFO_ATTR_N_BOUNDED_CH])
3733 		rate_info->n_bonded_ch = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_N_BOUNDED_CH]);
3734 
3735 	if (tb[HWSIM_RATE_INFO_ATTR_EHT_GI])
3736 		rate_info->eht_gi = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_EHT_GI]);
3737 
3738 	if (tb[HWSIM_RATE_INFO_ATTR_EHT_RU_ALLOC])
3739 		rate_info->eht_ru_alloc = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_EHT_RU_ALLOC]);
3740 
3741 	return 0;
3742 }
3743 
3744 static int mac80211_hwsim_parse_ftm_result(struct nlattr *ftm,
3745 					   struct cfg80211_pmsr_ftm_result *result,
3746 					   struct genl_info *info)
3747 {
3748 	struct nlattr *tb[NL80211_PMSR_FTM_RESP_ATTR_MAX + 1];
3749 	int ret;
3750 
3751 	ret = nla_parse_nested(tb, NL80211_PMSR_FTM_RESP_ATTR_MAX,
3752 			       ftm, hwsim_ftm_result_policy, info->extack);
3753 	if (ret)
3754 		return ret;
3755 
3756 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON])
3757 		result->failure_reason = nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON]);
3758 
3759 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX])
3760 		result->burst_index = nla_get_u16(tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX]);
3761 
3762 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS]) {
3763 		result->num_ftmr_attempts_valid = 1;
3764 		result->num_ftmr_attempts =
3765 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS]);
3766 	}
3767 
3768 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES]) {
3769 		result->num_ftmr_successes_valid = 1;
3770 		result->num_ftmr_successes =
3771 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES]);
3772 	}
3773 
3774 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME])
3775 		result->busy_retry_time =
3776 			nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME]);
3777 
3778 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP])
3779 		result->num_bursts_exp = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP]);
3780 
3781 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION])
3782 		result->burst_duration = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION]);
3783 
3784 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST])
3785 		result->ftms_per_burst = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST]);
3786 
3787 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG]) {
3788 		result->rssi_avg_valid = 1;
3789 		result->rssi_avg = nla_get_s32(tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG]);
3790 	}
3791 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD]) {
3792 		result->rssi_spread_valid = 1;
3793 		result->rssi_spread =
3794 			nla_get_s32(tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD]);
3795 	}
3796 
3797 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE]) {
3798 		result->tx_rate_valid = 1;
3799 		ret = mac80211_hwsim_parse_rate_info(tb[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE],
3800 						     &result->tx_rate, info);
3801 		if (ret)
3802 			return ret;
3803 	}
3804 
3805 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE]) {
3806 		result->rx_rate_valid = 1;
3807 		ret = mac80211_hwsim_parse_rate_info(tb[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE],
3808 						     &result->rx_rate, info);
3809 		if (ret)
3810 			return ret;
3811 	}
3812 
3813 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]) {
3814 		result->rtt_avg_valid = 1;
3815 		result->rtt_avg =
3816 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]);
3817 	}
3818 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE]) {
3819 		result->rtt_variance_valid = 1;
3820 		result->rtt_variance =
3821 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE]);
3822 	}
3823 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD]) {
3824 		result->rtt_spread_valid = 1;
3825 		result->rtt_spread =
3826 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD]);
3827 	}
3828 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG]) {
3829 		result->dist_avg_valid = 1;
3830 		result->dist_avg =
3831 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG]);
3832 	}
3833 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE]) {
3834 		result->dist_variance_valid = 1;
3835 		result->dist_variance =
3836 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE]);
3837 	}
3838 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD]) {
3839 		result->dist_spread_valid = 1;
3840 		result->dist_spread =
3841 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD]);
3842 	}
3843 
3844 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]) {
3845 		result->lci = nla_data(tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]);
3846 		result->lci_len = nla_len(tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]);
3847 	}
3848 
3849 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]) {
3850 		result->civicloc = nla_data(tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]);
3851 		result->civicloc_len = nla_len(tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]);
3852 	}
3853 
3854 	return 0;
3855 }
3856 
3857 static int mac80211_hwsim_parse_pmsr_resp(struct nlattr *resp,
3858 					  struct cfg80211_pmsr_result *result,
3859 					  struct genl_info *info)
3860 {
3861 	struct nlattr *tb[NL80211_PMSR_RESP_ATTR_MAX + 1];
3862 	struct nlattr *pmsr;
3863 	int rem;
3864 	int ret;
3865 
3866 	ret = nla_parse_nested(tb, NL80211_PMSR_RESP_ATTR_MAX, resp, hwsim_pmsr_resp_policy,
3867 			       info->extack);
3868 	if (ret)
3869 		return ret;
3870 
3871 	if (tb[NL80211_PMSR_RESP_ATTR_STATUS])
3872 		result->status = nla_get_u32(tb[NL80211_PMSR_RESP_ATTR_STATUS]);
3873 
3874 	if (tb[NL80211_PMSR_RESP_ATTR_HOST_TIME])
3875 		result->host_time = nla_get_u64(tb[NL80211_PMSR_RESP_ATTR_HOST_TIME]);
3876 
3877 	if (tb[NL80211_PMSR_RESP_ATTR_AP_TSF]) {
3878 		result->ap_tsf_valid = 1;
3879 		result->ap_tsf = nla_get_u64(tb[NL80211_PMSR_RESP_ATTR_AP_TSF]);
3880 	}
3881 
3882 	result->final = !!tb[NL80211_PMSR_RESP_ATTR_FINAL];
3883 
3884 	if (!tb[NL80211_PMSR_RESP_ATTR_DATA])
3885 		return 0;
3886 
3887 	nla_for_each_nested(pmsr, tb[NL80211_PMSR_RESP_ATTR_DATA], rem) {
3888 		switch (nla_type(pmsr)) {
3889 		case NL80211_PMSR_TYPE_FTM:
3890 			result->type = NL80211_PMSR_TYPE_FTM;
3891 			ret = mac80211_hwsim_parse_ftm_result(pmsr, &result->ftm, info);
3892 			if (ret)
3893 				return ret;
3894 			break;
3895 		default:
3896 			NL_SET_ERR_MSG_ATTR(info->extack, pmsr, "Unknown pmsr resp type");
3897 			return -EINVAL;
3898 		}
3899 	}
3900 
3901 	return 0;
3902 }
3903 
3904 static int mac80211_hwsim_parse_pmsr_result(struct nlattr *peer,
3905 					    struct cfg80211_pmsr_result *result,
3906 					    struct genl_info *info)
3907 {
3908 	struct nlattr *tb[NL80211_PMSR_PEER_ATTR_MAX + 1];
3909 	int ret;
3910 
3911 	if (!peer)
3912 		return -EINVAL;
3913 
3914 	ret = nla_parse_nested(tb, NL80211_PMSR_PEER_ATTR_MAX, peer,
3915 			       hwsim_pmsr_peer_result_policy, info->extack);
3916 	if (ret)
3917 		return ret;
3918 
3919 	if (tb[NL80211_PMSR_PEER_ATTR_ADDR])
3920 		memcpy(result->addr, nla_data(tb[NL80211_PMSR_PEER_ATTR_ADDR]),
3921 		       ETH_ALEN);
3922 
3923 	if (tb[NL80211_PMSR_PEER_ATTR_RESP]) {
3924 		ret = mac80211_hwsim_parse_pmsr_resp(tb[NL80211_PMSR_PEER_ATTR_RESP], result, info);
3925 		if (ret)
3926 			return ret;
3927 	}
3928 
3929 	return 0;
3930 };
3931 
3932 static int hwsim_pmsr_report_nl(struct sk_buff *msg, struct genl_info *info)
3933 {
3934 	struct mac80211_hwsim_data *data;
3935 	struct nlattr *peers, *peer;
3936 	struct nlattr *reqattr;
3937 	const u8 *src;
3938 	int err;
3939 	int rem;
3940 
3941 	if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER])
3942 		return -EINVAL;
3943 
3944 	src = nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
3945 	data = get_hwsim_data_ref_from_addr(src);
3946 	if (!data)
3947 		return -EINVAL;
3948 
3949 	mutex_lock(&data->mutex);
3950 	if (!data->pmsr_request) {
3951 		err = -EINVAL;
3952 		goto out;
3953 	}
3954 
3955 	reqattr = info->attrs[HWSIM_ATTR_PMSR_RESULT];
3956 	if (!reqattr) {
3957 		err = -EINVAL;
3958 		goto out;
3959 	}
3960 
3961 	peers = nla_find_nested(reqattr, NL80211_PMSR_ATTR_PEERS);
3962 	if (!peers) {
3963 		err = -EINVAL;
3964 		goto out;
3965 	}
3966 
3967 	nla_for_each_nested(peer, peers, rem) {
3968 		struct cfg80211_pmsr_result result = {};
3969 
3970 		err = mac80211_hwsim_parse_pmsr_result(peer, &result, info);
3971 		if (err)
3972 			goto out;
3973 
3974 		cfg80211_pmsr_report(data->pmsr_request_wdev,
3975 				     data->pmsr_request, &result, GFP_KERNEL);
3976 	}
3977 
3978 	cfg80211_pmsr_complete(data->pmsr_request_wdev, data->pmsr_request, GFP_KERNEL);
3979 
3980 	err = 0;
3981 out:
3982 	data->pmsr_request = NULL;
3983 	data->pmsr_request_wdev = NULL;
3984 
3985 	mutex_unlock(&data->mutex);
3986 	return err;
3987 }
3988 
3989 static enum hrtimer_restart
3990 mac80211_hwsim_nan_dw_start(struct hrtimer *timer)
3991 {
3992 	struct mac80211_hwsim_data *data =
3993 		container_of(timer, struct mac80211_hwsim_data,
3994 			     nan_timer);
3995 	struct ieee80211_hw *hw = data->hw;
3996 	u64 orig_tsf = mac80211_hwsim_get_tsf(hw, NULL), tsf = orig_tsf;
3997 	u32 dw_int = 512 * 1024;
3998 	u64 until_dw;
3999 
4000 	if (!data->nan_device_vif)
4001 		return HRTIMER_NORESTART;
4002 
4003 	if (data->nan_bands & BIT(NL80211_BAND_5GHZ)) {
4004 		if (data->nan_curr_dw_band == NL80211_BAND_2GHZ) {
4005 			dw_int = 128 * 1024;
4006 			data->nan_curr_dw_band = NL80211_BAND_5GHZ;
4007 		} else if (data->nan_curr_dw_band == NL80211_BAND_5GHZ) {
4008 			data->nan_curr_dw_band = NL80211_BAND_2GHZ;
4009 		}
4010 	}
4011 
4012 	until_dw = dw_int - do_div(tsf, dw_int);
4013 
4014 	/* The timer might fire just before the actual DW, in which case
4015 	 * update the timeout to the actual next DW
4016 	 */
4017 	if (until_dw < dw_int / 2)
4018 		until_dw += dw_int;
4019 
4020 	/* The above do_div() call directly modifies the 'tsf' variable, thus,
4021 	 * use a copy so that the print below would show the original TSF.
4022 	 */
4023 	wiphy_debug(hw->wiphy,
4024 		    "%s: tsf=%llx, curr_dw_band=%u, next_dw=%llu\n",
4025 		    __func__, orig_tsf, data->nan_curr_dw_band,
4026 		    until_dw);
4027 
4028 	hrtimer_forward_now(&data->nan_timer,
4029 			    ns_to_ktime(until_dw * NSEC_PER_USEC));
4030 
4031 	if (data->notify_dw) {
4032 		struct ieee80211_channel *ch;
4033 		struct wireless_dev *wdev =
4034 			ieee80211_vif_to_wdev(data->nan_device_vif);
4035 
4036 		if (data->nan_curr_dw_band == NL80211_BAND_5GHZ)
4037 			ch = ieee80211_get_channel(hw->wiphy, 5475);
4038 		else
4039 			ch = ieee80211_get_channel(hw->wiphy, 2437);
4040 
4041 		cfg80211_next_nan_dw_notif(wdev, ch, GFP_ATOMIC);
4042 	}
4043 
4044 	return HRTIMER_RESTART;
4045 }
4046 
4047 static int mac80211_hwsim_start_nan(struct ieee80211_hw *hw,
4048 				    struct ieee80211_vif *vif,
4049 				    struct cfg80211_nan_conf *conf)
4050 {
4051 	struct mac80211_hwsim_data *data = hw->priv;
4052 	u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
4053 	u32 dw_int = 512 * 1000;
4054 	u64 until_dw = dw_int - do_div(tsf, dw_int);
4055 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
4056 
4057 	if (vif->type != NL80211_IFTYPE_NAN)
4058 		return -EINVAL;
4059 
4060 	if (data->nan_device_vif)
4061 		return -EALREADY;
4062 
4063 	/* set this before starting the timer, as preemption might occur */
4064 	data->nan_device_vif = vif;
4065 	data->nan_bands = conf->bands;
4066 	data->nan_curr_dw_band = NL80211_BAND_2GHZ;
4067 
4068 	wiphy_debug(hw->wiphy, "nan_started, next_dw=%llu\n",
4069 		    until_dw);
4070 
4071 	hrtimer_start(&data->nan_timer,
4072 		      ns_to_ktime(until_dw * NSEC_PER_USEC),
4073 		      HRTIMER_MODE_REL_SOFT);
4074 
4075 	if (conf->cluster_id && !is_zero_ether_addr(conf->cluster_id) &&
4076 	    is_zero_ether_addr(hwsim_nan_cluster_id)) {
4077 		memcpy(hwsim_nan_cluster_id, conf->cluster_id, ETH_ALEN);
4078 	} else if (is_zero_ether_addr(hwsim_nan_cluster_id)) {
4079 		hwsim_nan_cluster_id[0] = 0x50;
4080 		hwsim_nan_cluster_id[1] = 0x6f;
4081 		hwsim_nan_cluster_id[2] = 0x9a;
4082 		hwsim_nan_cluster_id[3] = 0x01;
4083 		hwsim_nan_cluster_id[4] = get_random_u8();
4084 		hwsim_nan_cluster_id[5] = get_random_u8();
4085 	}
4086 
4087 	data->notify_dw = conf->enable_dw_notification;
4088 
4089 	cfg80211_nan_cluster_joined(wdev, hwsim_nan_cluster_id, true,
4090 				    GFP_KERNEL);
4091 
4092 	return 0;
4093 }
4094 
4095 static int mac80211_hwsim_stop_nan(struct ieee80211_hw *hw,
4096 				   struct ieee80211_vif *vif)
4097 {
4098 	struct mac80211_hwsim_data *data = hw->priv;
4099 	struct mac80211_hwsim_data *data2;
4100 	bool nan_cluster_running = false;
4101 
4102 	if (vif->type != NL80211_IFTYPE_NAN || !data->nan_device_vif ||
4103 	    data->nan_device_vif != vif)
4104 		return -EINVAL;
4105 
4106 	hrtimer_cancel(&data->nan_timer);
4107 	data->nan_device_vif = NULL;
4108 
4109 	spin_lock(&hwsim_radio_lock);
4110 	list_for_each_entry(data2, &hwsim_radios, list) {
4111 		if (data2->nan_device_vif) {
4112 			nan_cluster_running = true;
4113 			break;
4114 		}
4115 	}
4116 	spin_unlock(&hwsim_radio_lock);
4117 
4118 	if (!nan_cluster_running)
4119 		memset(hwsim_nan_cluster_id, 0, ETH_ALEN);
4120 
4121 	return 0;
4122 }
4123 
4124 static int mac80211_hwsim_change_nan_config(struct ieee80211_hw *hw,
4125 					    struct ieee80211_vif *vif,
4126 					    struct cfg80211_nan_conf *conf,
4127 					    u32 changes)
4128 {
4129 	struct mac80211_hwsim_data *data = hw->priv;
4130 
4131 	if (vif->type != NL80211_IFTYPE_NAN)
4132 		return -EINVAL;
4133 
4134 	if (!data->nan_device_vif)
4135 		return -EINVAL;
4136 
4137 	wiphy_debug(hw->wiphy, "nan_config_changed: changes=0x%x\n", changes);
4138 
4139 	/* Handle only the changes we care about for simulation purposes */
4140 	if (changes & CFG80211_NAN_CONF_CHANGED_BANDS) {
4141 		data->nan_bands = conf->bands;
4142 		data->nan_curr_dw_band = NL80211_BAND_2GHZ;
4143 	}
4144 
4145 	if (changes & CFG80211_NAN_CONF_CHANGED_CONFIG)
4146 		data->notify_dw = conf->enable_dw_notification;
4147 
4148 	return 0;
4149 }
4150 
4151 #ifdef CONFIG_MAC80211_DEBUGFS
4152 #define HWSIM_DEBUGFS_OPS					\
4153 	.link_add_debugfs = mac80211_hwsim_link_add_debugfs,
4154 #else
4155 #define HWSIM_DEBUGFS_OPS
4156 #endif
4157 
4158 #define HWSIM_COMMON_OPS					\
4159 	.tx = mac80211_hwsim_tx,				\
4160 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,	\
4161 	.start = mac80211_hwsim_start,				\
4162 	.stop = mac80211_hwsim_stop,				\
4163 	.add_interface = mac80211_hwsim_add_interface,		\
4164 	.change_interface = mac80211_hwsim_change_interface,	\
4165 	.remove_interface = mac80211_hwsim_remove_interface,	\
4166 	.config = mac80211_hwsim_config,			\
4167 	.configure_filter = mac80211_hwsim_configure_filter,	\
4168 	.vif_cfg_changed = mac80211_hwsim_vif_info_changed,	\
4169 	.link_info_changed = mac80211_hwsim_link_info_changed,  \
4170 	.tx_last_beacon = mac80211_hwsim_tx_last_beacon,	\
4171 	.sta_notify = mac80211_hwsim_sta_notify,		\
4172 	.link_sta_rc_update = mac80211_hwsim_sta_rc_update,	\
4173 	.conf_tx = mac80211_hwsim_conf_tx,			\
4174 	.get_survey = mac80211_hwsim_get_survey,		\
4175 	CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)	\
4176 	.ampdu_action = mac80211_hwsim_ampdu_action,		\
4177 	.flush = mac80211_hwsim_flush,				\
4178 	.get_et_sset_count = mac80211_hwsim_get_et_sset_count,	\
4179 	.get_et_stats = mac80211_hwsim_get_et_stats,		\
4180 	.get_et_strings = mac80211_hwsim_get_et_strings,	\
4181 	.start_pmsr = mac80211_hwsim_start_pmsr,		\
4182 	.abort_pmsr = mac80211_hwsim_abort_pmsr,		\
4183 	.start_nan = mac80211_hwsim_start_nan,                  \
4184 	.stop_nan = mac80211_hwsim_stop_nan,                    \
4185 	.nan_change_conf = mac80211_hwsim_change_nan_config,    \
4186 	HWSIM_DEBUGFS_OPS
4187 
4188 #define HWSIM_NON_MLO_OPS					\
4189 	.sta_add = mac80211_hwsim_sta_add,			\
4190 	.sta_remove = mac80211_hwsim_sta_remove,		\
4191 	.set_tim = mac80211_hwsim_set_tim,			\
4192 	.get_tsf = mac80211_hwsim_get_tsf,			\
4193 	.set_tsf = mac80211_hwsim_set_tsf,
4194 
4195 static const struct ieee80211_ops mac80211_hwsim_ops = {
4196 	HWSIM_COMMON_OPS
4197 	HWSIM_NON_MLO_OPS
4198 	.sw_scan_start = mac80211_hwsim_sw_scan,
4199 	.sw_scan_complete = mac80211_hwsim_sw_scan_complete,
4200 	.add_chanctx = ieee80211_emulate_add_chanctx,
4201 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
4202 	.change_chanctx = ieee80211_emulate_change_chanctx,
4203 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
4204 };
4205 
4206 #define HWSIM_CHANCTX_OPS					\
4207 	.hw_scan = mac80211_hwsim_hw_scan,			\
4208 	.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,	\
4209 	.remain_on_channel = mac80211_hwsim_roc,		\
4210 	.cancel_remain_on_channel = mac80211_hwsim_croc,	\
4211 	.add_chanctx = mac80211_hwsim_add_chanctx,		\
4212 	.remove_chanctx = mac80211_hwsim_remove_chanctx,	\
4213 	.change_chanctx = mac80211_hwsim_change_chanctx,	\
4214 	.assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,\
4215 	.unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx, \
4216 	.switch_vif_chanctx = mac80211_hwsim_switch_vif_chanctx,
4217 
4218 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
4219 	HWSIM_COMMON_OPS
4220 	HWSIM_NON_MLO_OPS
4221 	HWSIM_CHANCTX_OPS
4222 };
4223 
4224 static const struct ieee80211_ops mac80211_hwsim_mlo_ops = {
4225 	HWSIM_COMMON_OPS
4226 	HWSIM_CHANCTX_OPS
4227 	.set_rts_threshold = mac80211_hwsim_set_rts_threshold,
4228 	.change_vif_links = mac80211_hwsim_change_vif_links,
4229 	.change_sta_links = mac80211_hwsim_change_sta_links,
4230 	.sta_state = mac80211_hwsim_sta_state,
4231 	.can_neg_ttlm = mac80211_hwsim_can_neg_ttlm,
4232 };
4233 
4234 struct hwsim_new_radio_params {
4235 	unsigned int channels;
4236 	const char *reg_alpha2;
4237 	const struct ieee80211_regdomain *regd;
4238 	bool reg_strict;
4239 	bool p2p_device;
4240 	bool use_chanctx;
4241 	bool multi_radio;
4242 	bool destroy_on_close;
4243 	const char *hwname;
4244 	bool no_vif;
4245 	const u8 *perm_addr;
4246 	u32 iftypes;
4247 	u32 *ciphers;
4248 	u8 n_ciphers;
4249 	bool mlo;
4250 	const struct cfg80211_pmsr_capabilities *pmsr_capa;
4251 	bool nan_device;
4252 };
4253 
4254 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
4255 				   struct genl_info *info)
4256 {
4257 	if (info)
4258 		genl_notify(&hwsim_genl_family, mcast_skb, info,
4259 			    HWSIM_MCGRP_CONFIG, GFP_KERNEL);
4260 	else
4261 		genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
4262 				  HWSIM_MCGRP_CONFIG, GFP_KERNEL);
4263 }
4264 
4265 static int append_radio_msg(struct sk_buff *skb, int id,
4266 			    struct hwsim_new_radio_params *param)
4267 {
4268 	int ret;
4269 
4270 	ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
4271 	if (ret < 0)
4272 		return ret;
4273 
4274 	if (param->channels) {
4275 		ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
4276 		if (ret < 0)
4277 			return ret;
4278 	}
4279 
4280 	if (param->reg_alpha2) {
4281 		ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
4282 			      param->reg_alpha2);
4283 		if (ret < 0)
4284 			return ret;
4285 	}
4286 
4287 	if (param->regd) {
4288 		int i;
4289 
4290 		for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
4291 			if (hwsim_world_regdom_custom[i] != param->regd)
4292 				continue;
4293 
4294 			ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
4295 			if (ret < 0)
4296 				return ret;
4297 			break;
4298 		}
4299 	}
4300 
4301 	if (param->reg_strict) {
4302 		ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
4303 		if (ret < 0)
4304 			return ret;
4305 	}
4306 
4307 	if (param->p2p_device) {
4308 		ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
4309 		if (ret < 0)
4310 			return ret;
4311 	}
4312 
4313 	if (param->use_chanctx) {
4314 		ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
4315 		if (ret < 0)
4316 			return ret;
4317 	}
4318 
4319 	if (param->multi_radio) {
4320 		ret = nla_put_flag(skb, HWSIM_ATTR_MULTI_RADIO);
4321 		if (ret < 0)
4322 			return ret;
4323 	}
4324 
4325 	if (param->hwname) {
4326 		ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
4327 			      strlen(param->hwname), param->hwname);
4328 		if (ret < 0)
4329 			return ret;
4330 	}
4331 
4332 	if (param->nan_device) {
4333 		ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_NAN_DEVICE);
4334 		if (ret < 0)
4335 			return ret;
4336 	}
4337 	return 0;
4338 }
4339 
4340 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
4341 				  struct hwsim_new_radio_params *param)
4342 {
4343 	struct sk_buff *mcast_skb;
4344 	void *data;
4345 
4346 	mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4347 	if (!mcast_skb)
4348 		return;
4349 
4350 	data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
4351 			   HWSIM_CMD_NEW_RADIO);
4352 	if (!data)
4353 		goto out_err;
4354 
4355 	if (append_radio_msg(mcast_skb, id, param) < 0)
4356 		goto out_err;
4357 
4358 	genlmsg_end(mcast_skb, data);
4359 
4360 	hwsim_mcast_config_msg(mcast_skb, info);
4361 	return;
4362 
4363 out_err:
4364 	nlmsg_free(mcast_skb);
4365 }
4366 
4367 static const struct ieee80211_sband_iftype_data sband_capa_2ghz[] = {
4368 	{
4369 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
4370 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
4371 		.he_cap = {
4372 			.has_he = true,
4373 			.he_cap_elem = {
4374 				.mac_cap_info[0] =
4375 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4376 				.mac_cap_info[1] =
4377 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4378 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4379 				.mac_cap_info[2] =
4380 					IEEE80211_HE_MAC_CAP2_BSR |
4381 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4382 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4383 				.mac_cap_info[3] =
4384 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4385 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4386 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4387 				.phy_cap_info[0] =
4388 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4389 				.phy_cap_info[1] =
4390 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4391 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4392 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4393 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4394 				.phy_cap_info[2] =
4395 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4396 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4397 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4398 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4399 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4400 
4401 				/* Leave all the other PHY capability bytes
4402 				 * unset, as DCM, beam forming, RU and PPE
4403 				 * threshold information are not supported
4404 				 */
4405 			},
4406 			.he_mcs_nss_supp = {
4407 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4408 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4409 				.rx_mcs_160 = cpu_to_le16(0xffff),
4410 				.tx_mcs_160 = cpu_to_le16(0xffff),
4411 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4412 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4413 			},
4414 		},
4415 		.eht_cap = {
4416 			.has_eht = true,
4417 			.eht_cap_elem = {
4418 				.mac_cap_info[0] =
4419 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4420 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4421 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4422 				.phy_cap_info[0] =
4423 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4424 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4425 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4426 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4427 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
4428 				.phy_cap_info[3] =
4429 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4430 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4431 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4432 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4433 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4434 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4435 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4436 				.phy_cap_info[4] =
4437 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4438 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4439 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4440 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4441 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4442 				.phy_cap_info[5] =
4443 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4444 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4445 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4446 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4447 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4448 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4449 				.phy_cap_info[6] =
4450 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4451 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4452 				.phy_cap_info[7] =
4453 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
4454 			},
4455 
4456 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4457 			 * Rx
4458 			 */
4459 			.eht_mcs_nss_supp = {
4460 				/*
4461 				 * Since B0, B1, B2 and B3 are not set in
4462 				 * the supported channel width set field in the
4463 				 * HE PHY capabilities information field the
4464 				 * device is a 20MHz only device on 2.4GHz band.
4465 				 */
4466 				.only_20mhz = {
4467 					.rx_tx_mcs7_max_nss = 0x88,
4468 					.rx_tx_mcs9_max_nss = 0x88,
4469 					.rx_tx_mcs11_max_nss = 0x88,
4470 					.rx_tx_mcs13_max_nss = 0x88,
4471 				},
4472 			},
4473 			/* PPE threshold information is not supported */
4474 		},
4475 	},
4476 	{
4477 		.types_mask = BIT(NL80211_IFTYPE_AP) |
4478 			      BIT(NL80211_IFTYPE_P2P_GO),
4479 		.he_cap = {
4480 			.has_he = true,
4481 			.he_cap_elem = {
4482 				.mac_cap_info[0] =
4483 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4484 				.mac_cap_info[1] =
4485 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4486 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4487 				.mac_cap_info[2] =
4488 					IEEE80211_HE_MAC_CAP2_BSR |
4489 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4490 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4491 				.mac_cap_info[3] =
4492 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4493 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4494 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4495 				.phy_cap_info[0] =
4496 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4497 				.phy_cap_info[1] =
4498 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4499 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4500 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4501 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4502 				.phy_cap_info[2] =
4503 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4504 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4505 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4506 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4507 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4508 
4509 				/* Leave all the other PHY capability bytes
4510 				 * unset, as DCM, beam forming, RU and PPE
4511 				 * threshold information are not supported
4512 				 */
4513 			},
4514 			.he_mcs_nss_supp = {
4515 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4516 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4517 				.rx_mcs_160 = cpu_to_le16(0xffff),
4518 				.tx_mcs_160 = cpu_to_le16(0xffff),
4519 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4520 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4521 			},
4522 		},
4523 		.eht_cap = {
4524 			.has_eht = true,
4525 			.eht_cap_elem = {
4526 				.mac_cap_info[0] =
4527 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4528 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4529 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4530 				.phy_cap_info[0] =
4531 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4532 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4533 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4534 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4535 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
4536 				.phy_cap_info[3] =
4537 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4538 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4539 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4540 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4541 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4542 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4543 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4544 				.phy_cap_info[4] =
4545 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4546 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4547 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4548 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4549 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4550 				.phy_cap_info[5] =
4551 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4552 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4553 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4554 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4555 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4556 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4557 				.phy_cap_info[6] =
4558 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4559 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4560 				.phy_cap_info[7] =
4561 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
4562 			},
4563 
4564 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4565 			 * Rx
4566 			 */
4567 			.eht_mcs_nss_supp = {
4568 				/*
4569 				 * Since B0, B1, B2 and B3 are not set in
4570 				 * the supported channel width set field in the
4571 				 * HE PHY capabilities information field the
4572 				 * device is a 20MHz only device on 2.4GHz band.
4573 				 */
4574 				.only_20mhz = {
4575 					.rx_tx_mcs7_max_nss = 0x88,
4576 					.rx_tx_mcs9_max_nss = 0x88,
4577 					.rx_tx_mcs11_max_nss = 0x88,
4578 					.rx_tx_mcs13_max_nss = 0x88,
4579 				},
4580 			},
4581 			/* PPE threshold information is not supported */
4582 		},
4583 	},
4584 #ifdef CONFIG_MAC80211_MESH
4585 	{
4586 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
4587 		.he_cap = {
4588 			.has_he = true,
4589 			.he_cap_elem = {
4590 				.mac_cap_info[0] =
4591 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4592 				.mac_cap_info[1] =
4593 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4594 				.mac_cap_info[2] =
4595 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4596 				.mac_cap_info[3] =
4597 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4598 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4599 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4600 				.phy_cap_info[0] =
4601 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4602 				.phy_cap_info[1] =
4603 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4604 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4605 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4606 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4607 				.phy_cap_info[2] = 0,
4608 
4609 				/* Leave all the other PHY capability bytes
4610 				 * unset, as DCM, beam forming, RU and PPE
4611 				 * threshold information are not supported
4612 				 */
4613 			},
4614 			.he_mcs_nss_supp = {
4615 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4616 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4617 				.rx_mcs_160 = cpu_to_le16(0xffff),
4618 				.tx_mcs_160 = cpu_to_le16(0xffff),
4619 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4620 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4621 			},
4622 		},
4623 	},
4624 #endif
4625 };
4626 
4627 static const struct ieee80211_sband_iftype_data sband_capa_5ghz[] = {
4628 	{
4629 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
4630 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
4631 		.he_cap = {
4632 			.has_he = true,
4633 			.he_cap_elem = {
4634 				.mac_cap_info[0] =
4635 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4636 				.mac_cap_info[1] =
4637 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4638 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4639 				.mac_cap_info[2] =
4640 					IEEE80211_HE_MAC_CAP2_BSR |
4641 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4642 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4643 				.mac_cap_info[3] =
4644 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4645 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4646 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4647 				.phy_cap_info[0] =
4648 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4649 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4650 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4651 				.phy_cap_info[1] =
4652 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4653 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4654 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4655 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4656 				.phy_cap_info[2] =
4657 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4658 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4659 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4660 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4661 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4662 
4663 				/* Leave all the other PHY capability bytes
4664 				 * unset, as DCM, beam forming, RU and PPE
4665 				 * threshold information are not supported
4666 				 */
4667 			},
4668 			.he_mcs_nss_supp = {
4669 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4670 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4671 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4672 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4673 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4674 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4675 			},
4676 		},
4677 		.eht_cap = {
4678 			.has_eht = true,
4679 			.eht_cap_elem = {
4680 				.mac_cap_info[0] =
4681 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4682 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4683 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4684 				.phy_cap_info[0] =
4685 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4686 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4687 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4688 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4689 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
4690 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
4691 				.phy_cap_info[1] =
4692 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
4693 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
4694 				.phy_cap_info[2] =
4695 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
4696 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
4697 				.phy_cap_info[3] =
4698 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4699 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4700 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4701 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4702 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4703 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4704 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4705 				.phy_cap_info[4] =
4706 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4707 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4708 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4709 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4710 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4711 				.phy_cap_info[5] =
4712 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4713 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4714 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4715 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4716 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4717 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4718 				.phy_cap_info[6] =
4719 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4720 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4721 				.phy_cap_info[7] =
4722 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
4723 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4724 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4725 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4726 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
4727 			},
4728 
4729 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4730 			 * Rx
4731 			 */
4732 			.eht_mcs_nss_supp = {
4733 				/*
4734 				 * As B1 and B2 are set in the supported
4735 				 * channel width set field in the HE PHY
4736 				 * capabilities information field include all
4737 				 * the following MCS/NSS.
4738 				 */
4739 				.bw._80 = {
4740 					.rx_tx_mcs9_max_nss = 0x88,
4741 					.rx_tx_mcs11_max_nss = 0x88,
4742 					.rx_tx_mcs13_max_nss = 0x88,
4743 				},
4744 				.bw._160 = {
4745 					.rx_tx_mcs9_max_nss = 0x88,
4746 					.rx_tx_mcs11_max_nss = 0x88,
4747 					.rx_tx_mcs13_max_nss = 0x88,
4748 				},
4749 			},
4750 			/* PPE threshold information is not supported */
4751 		},
4752 	},
4753 	{
4754 		.types_mask = BIT(NL80211_IFTYPE_AP) |
4755 			      BIT(NL80211_IFTYPE_P2P_GO),
4756 		.he_cap = {
4757 			.has_he = true,
4758 			.he_cap_elem = {
4759 				.mac_cap_info[0] =
4760 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4761 				.mac_cap_info[1] =
4762 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4763 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4764 				.mac_cap_info[2] =
4765 					IEEE80211_HE_MAC_CAP2_BSR |
4766 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4767 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4768 				.mac_cap_info[3] =
4769 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4770 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4771 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4772 				.phy_cap_info[0] =
4773 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4774 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4775 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4776 				.phy_cap_info[1] =
4777 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4778 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4779 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4780 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4781 				.phy_cap_info[2] =
4782 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4783 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4784 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4785 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4786 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4787 
4788 				/* Leave all the other PHY capability bytes
4789 				 * unset, as DCM, beam forming, RU and PPE
4790 				 * threshold information are not supported
4791 				 */
4792 			},
4793 			.he_mcs_nss_supp = {
4794 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4795 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4796 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4797 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4798 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4799 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4800 			},
4801 		},
4802 		.eht_cap = {
4803 			.has_eht = true,
4804 			.eht_cap_elem = {
4805 				.mac_cap_info[0] =
4806 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4807 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4808 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4809 				.phy_cap_info[0] =
4810 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4811 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4812 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4813 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4814 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
4815 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
4816 				.phy_cap_info[1] =
4817 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
4818 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
4819 				.phy_cap_info[2] =
4820 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
4821 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
4822 				.phy_cap_info[3] =
4823 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4824 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4825 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4826 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4827 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4828 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4829 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4830 				.phy_cap_info[4] =
4831 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4832 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4833 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4834 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4835 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4836 				.phy_cap_info[5] =
4837 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4838 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4839 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4840 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4841 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4842 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4843 				.phy_cap_info[6] =
4844 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4845 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4846 				.phy_cap_info[7] =
4847 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
4848 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4849 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4850 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4851 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
4852 			},
4853 
4854 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4855 			 * Rx
4856 			 */
4857 			.eht_mcs_nss_supp = {
4858 				/*
4859 				 * As B1 and B2 are set in the supported
4860 				 * channel width set field in the HE PHY
4861 				 * capabilities information field include all
4862 				 * the following MCS/NSS.
4863 				 */
4864 				.bw._80 = {
4865 					.rx_tx_mcs9_max_nss = 0x88,
4866 					.rx_tx_mcs11_max_nss = 0x88,
4867 					.rx_tx_mcs13_max_nss = 0x88,
4868 				},
4869 				.bw._160 = {
4870 					.rx_tx_mcs9_max_nss = 0x88,
4871 					.rx_tx_mcs11_max_nss = 0x88,
4872 					.rx_tx_mcs13_max_nss = 0x88,
4873 				},
4874 			},
4875 			/* PPE threshold information is not supported */
4876 		},
4877 	},
4878 #ifdef CONFIG_MAC80211_MESH
4879 	{
4880 		/* TODO: should we support other types, e.g., IBSS?*/
4881 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
4882 		.he_cap = {
4883 			.has_he = true,
4884 			.he_cap_elem = {
4885 				.mac_cap_info[0] =
4886 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4887 				.mac_cap_info[1] =
4888 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4889 				.mac_cap_info[2] =
4890 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4891 				.mac_cap_info[3] =
4892 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4893 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4894 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4895 				.phy_cap_info[0] =
4896 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4897 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4898 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4899 				.phy_cap_info[1] =
4900 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4901 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4902 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4903 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4904 				.phy_cap_info[2] = 0,
4905 
4906 				/* Leave all the other PHY capability bytes
4907 				 * unset, as DCM, beam forming, RU and PPE
4908 				 * threshold information are not supported
4909 				 */
4910 			},
4911 			.he_mcs_nss_supp = {
4912 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4913 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4914 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4915 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4916 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4917 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4918 			},
4919 		},
4920 	},
4921 #endif
4922 };
4923 
4924 static const struct ieee80211_sband_iftype_data sband_capa_6ghz[] = {
4925 	{
4926 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
4927 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
4928 		.he_6ghz_capa = {
4929 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
4930 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
4931 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
4932 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
4933 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
4934 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
4935 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
4936 		},
4937 		.he_cap = {
4938 			.has_he = true,
4939 			.he_cap_elem = {
4940 				.mac_cap_info[0] =
4941 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4942 				.mac_cap_info[1] =
4943 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4944 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4945 				.mac_cap_info[2] =
4946 					IEEE80211_HE_MAC_CAP2_BSR |
4947 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4948 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4949 				.mac_cap_info[3] =
4950 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4951 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4952 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4953 				.phy_cap_info[0] =
4954 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4955 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4956 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4957 				.phy_cap_info[1] =
4958 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4959 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4960 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4961 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4962 				.phy_cap_info[2] =
4963 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4964 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4965 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4966 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4967 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4968 
4969 				/* Leave all the other PHY capability bytes
4970 				 * unset, as DCM, beam forming, RU and PPE
4971 				 * threshold information are not supported
4972 				 */
4973 			},
4974 			.he_mcs_nss_supp = {
4975 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4976 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4977 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4978 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4979 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4980 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4981 			},
4982 		},
4983 		.eht_cap = {
4984 			.has_eht = true,
4985 			.eht_cap_elem = {
4986 				.mac_cap_info[0] =
4987 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4988 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4989 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4990 				.phy_cap_info[0] =
4991 					IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
4992 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4993 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4994 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4995 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4996 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
4997 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
4998 				.phy_cap_info[1] =
4999 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
5000 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
5001 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
5002 				.phy_cap_info[2] =
5003 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
5004 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
5005 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
5006 				.phy_cap_info[3] =
5007 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
5008 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
5009 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
5010 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
5011 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
5012 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
5013 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
5014 				.phy_cap_info[4] =
5015 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
5016 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
5017 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
5018 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
5019 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
5020 				.phy_cap_info[5] =
5021 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
5022 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
5023 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
5024 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
5025 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
5026 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
5027 				.phy_cap_info[6] =
5028 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
5029 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
5030 					IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
5031 				.phy_cap_info[7] =
5032 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
5033 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
5034 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
5035 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
5036 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
5037 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
5038 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
5039 			},
5040 
5041 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5042 			 * Rx
5043 			 */
5044 			.eht_mcs_nss_supp = {
5045 				/*
5046 				 * As B1 and B2 are set in the supported
5047 				 * channel width set field in the HE PHY
5048 				 * capabilities information field and 320MHz in
5049 				 * 6GHz is supported include all the following
5050 				 * MCS/NSS.
5051 				 */
5052 				.bw._80 = {
5053 					.rx_tx_mcs9_max_nss = 0x88,
5054 					.rx_tx_mcs11_max_nss = 0x88,
5055 					.rx_tx_mcs13_max_nss = 0x88,
5056 				},
5057 				.bw._160 = {
5058 					.rx_tx_mcs9_max_nss = 0x88,
5059 					.rx_tx_mcs11_max_nss = 0x88,
5060 					.rx_tx_mcs13_max_nss = 0x88,
5061 				},
5062 				.bw._320 = {
5063 					.rx_tx_mcs9_max_nss = 0x88,
5064 					.rx_tx_mcs11_max_nss = 0x88,
5065 					.rx_tx_mcs13_max_nss = 0x88,
5066 				},
5067 			},
5068 			/* PPE threshold information is not supported */
5069 		},
5070 	},
5071 	{
5072 		.types_mask = BIT(NL80211_IFTYPE_AP) |
5073 			      BIT(NL80211_IFTYPE_P2P_GO),
5074 		.he_6ghz_capa = {
5075 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
5076 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
5077 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
5078 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
5079 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
5080 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
5081 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
5082 		},
5083 		.he_cap = {
5084 			.has_he = true,
5085 			.he_cap_elem = {
5086 				.mac_cap_info[0] =
5087 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5088 				.mac_cap_info[1] =
5089 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
5090 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5091 				.mac_cap_info[2] =
5092 					IEEE80211_HE_MAC_CAP2_BSR |
5093 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
5094 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5095 				.mac_cap_info[3] =
5096 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5097 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5098 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5099 				.phy_cap_info[0] =
5100 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5101 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5102 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5103 				.phy_cap_info[1] =
5104 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5105 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5106 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5107 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5108 				.phy_cap_info[2] =
5109 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
5110 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
5111 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
5112 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5113 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
5114 
5115 				/* Leave all the other PHY capability bytes
5116 				 * unset, as DCM, beam forming, RU and PPE
5117 				 * threshold information are not supported
5118 				 */
5119 			},
5120 			.he_mcs_nss_supp = {
5121 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5122 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5123 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5124 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5125 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5126 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5127 			},
5128 		},
5129 		.eht_cap = {
5130 			.has_eht = true,
5131 			.eht_cap_elem = {
5132 				.mac_cap_info[0] =
5133 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
5134 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
5135 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
5136 				.phy_cap_info[0] =
5137 					IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
5138 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
5139 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
5140 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
5141 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
5142 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
5143 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
5144 				.phy_cap_info[1] =
5145 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
5146 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
5147 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
5148 				.phy_cap_info[2] =
5149 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
5150 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
5151 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
5152 				.phy_cap_info[3] =
5153 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
5154 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
5155 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
5156 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
5157 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
5158 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
5159 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
5160 				.phy_cap_info[4] =
5161 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
5162 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
5163 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
5164 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
5165 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
5166 				.phy_cap_info[5] =
5167 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
5168 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
5169 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
5170 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
5171 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
5172 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
5173 				.phy_cap_info[6] =
5174 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
5175 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
5176 					IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
5177 				.phy_cap_info[7] =
5178 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
5179 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
5180 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
5181 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
5182 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
5183 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
5184 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
5185 			},
5186 
5187 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5188 			 * Rx
5189 			 */
5190 			.eht_mcs_nss_supp = {
5191 				/*
5192 				 * As B1 and B2 are set in the supported
5193 				 * channel width set field in the HE PHY
5194 				 * capabilities information field and 320MHz in
5195 				 * 6GHz is supported include all the following
5196 				 * MCS/NSS.
5197 				 */
5198 				.bw._80 = {
5199 					.rx_tx_mcs9_max_nss = 0x88,
5200 					.rx_tx_mcs11_max_nss = 0x88,
5201 					.rx_tx_mcs13_max_nss = 0x88,
5202 				},
5203 				.bw._160 = {
5204 					.rx_tx_mcs9_max_nss = 0x88,
5205 					.rx_tx_mcs11_max_nss = 0x88,
5206 					.rx_tx_mcs13_max_nss = 0x88,
5207 				},
5208 				.bw._320 = {
5209 					.rx_tx_mcs9_max_nss = 0x88,
5210 					.rx_tx_mcs11_max_nss = 0x88,
5211 					.rx_tx_mcs13_max_nss = 0x88,
5212 				},
5213 			},
5214 			/* PPE threshold information is not supported */
5215 		},
5216 	},
5217 #ifdef CONFIG_MAC80211_MESH
5218 	{
5219 		/* TODO: should we support other types, e.g., IBSS?*/
5220 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
5221 		.he_6ghz_capa = {
5222 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
5223 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
5224 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
5225 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
5226 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
5227 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
5228 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
5229 		},
5230 		.he_cap = {
5231 			.has_he = true,
5232 			.he_cap_elem = {
5233 				.mac_cap_info[0] =
5234 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5235 				.mac_cap_info[1] =
5236 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5237 				.mac_cap_info[2] =
5238 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5239 				.mac_cap_info[3] =
5240 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5241 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5242 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5243 				.phy_cap_info[0] =
5244 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5245 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5246 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5247 				.phy_cap_info[1] =
5248 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5249 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5250 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5251 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5252 				.phy_cap_info[2] = 0,
5253 
5254 				/* Leave all the other PHY capability bytes
5255 				 * unset, as DCM, beam forming, RU and PPE
5256 				 * threshold information are not supported
5257 				 */
5258 			},
5259 			.he_mcs_nss_supp = {
5260 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5261 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5262 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5263 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5264 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5265 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5266 			},
5267 		},
5268 		.eht_cap = {
5269 			.has_eht = true,
5270 			.eht_cap_elem = {
5271 				.mac_cap_info[0] = IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
5272 						   IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
5273 				.phy_cap_info[0] = IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ,
5274 				/* Leave all the other PHY capability bytes
5275 				 * unset, as DCM, beam forming, RU and PPE
5276 				 * threshold information are not supported
5277 				 */
5278 			},
5279 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5280 			 * Rx
5281 			 */
5282 			.eht_mcs_nss_supp = {
5283 				/* As B1 and B2 are set in the supported
5284 				 * channel width set field in the HE PHY
5285 				 * capabilities information field and 320MHz in
5286 				 * 6GHz is supported include all the following
5287 				 * MCS/NSS.
5288 				 */
5289 				.bw._80 = {
5290 					.rx_tx_mcs9_max_nss = 0x88,
5291 					.rx_tx_mcs11_max_nss = 0x88,
5292 					.rx_tx_mcs13_max_nss = 0x88,
5293 				},
5294 				.bw._160 = {
5295 					.rx_tx_mcs9_max_nss = 0x88,
5296 					.rx_tx_mcs11_max_nss = 0x88,
5297 					.rx_tx_mcs13_max_nss = 0x88,
5298 				},
5299 				.bw._320 = {
5300 					.rx_tx_mcs9_max_nss = 0x88,
5301 					.rx_tx_mcs11_max_nss = 0x88,
5302 					.rx_tx_mcs13_max_nss = 0x88,
5303 				},
5304 			},
5305 			/* PPE threshold information is not supported */
5306 		},
5307 	},
5308 #endif
5309 };
5310 
5311 static void mac80211_hwsim_sband_capab(struct ieee80211_supported_band *sband)
5312 {
5313 	switch (sband->band) {
5314 	case NL80211_BAND_2GHZ:
5315 		ieee80211_set_sband_iftype_data(sband, sband_capa_2ghz);
5316 		break;
5317 	case NL80211_BAND_5GHZ:
5318 		ieee80211_set_sband_iftype_data(sband, sband_capa_5ghz);
5319 		break;
5320 	case NL80211_BAND_6GHZ:
5321 		ieee80211_set_sband_iftype_data(sband, sband_capa_6ghz);
5322 		break;
5323 	default:
5324 		break;
5325 	}
5326 }
5327 
5328 #ifdef CONFIG_MAC80211_MESH
5329 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
5330 #else
5331 #define HWSIM_MESH_BIT 0
5332 #endif
5333 
5334 #define HWSIM_DEFAULT_IF_LIMIT \
5335 	(BIT(NL80211_IFTYPE_STATION) | \
5336 	 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
5337 	 BIT(NL80211_IFTYPE_AP) | \
5338 	 BIT(NL80211_IFTYPE_P2P_GO) | \
5339 	 HWSIM_MESH_BIT)
5340 
5341 #define HWSIM_IFTYPE_SUPPORT_MASK \
5342 	(BIT(NL80211_IFTYPE_STATION) | \
5343 	 BIT(NL80211_IFTYPE_AP) | \
5344 	 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
5345 	 BIT(NL80211_IFTYPE_P2P_GO) | \
5346 	 BIT(NL80211_IFTYPE_ADHOC) | \
5347 	 BIT(NL80211_IFTYPE_MESH_POINT) | \
5348 	 BIT(NL80211_IFTYPE_OCB))
5349 
5350 static const u8 iftypes_ext_capa_ap[] = {
5351 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
5352 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
5353 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
5354 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
5355 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
5356 	 [9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
5357 };
5358 
5359 #define MAC80211_HWSIM_MLD_CAPA_OPS				\
5360 	FIELD_PREP_CONST(IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
5361 			 IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME) | \
5362 	FIELD_PREP_CONST(IEEE80211_MLD_CAP_OP_MAX_SIMUL_LINKS, \
5363 			 IEEE80211_MLD_MAX_NUM_LINKS - 1)
5364 
5365 static const struct wiphy_iftype_ext_capab mac80211_hwsim_iftypes_ext_capa[] = {
5366 	{
5367 		.iftype = NL80211_IFTYPE_AP,
5368 		.extended_capabilities = iftypes_ext_capa_ap,
5369 		.extended_capabilities_mask = iftypes_ext_capa_ap,
5370 		.extended_capabilities_len = sizeof(iftypes_ext_capa_ap),
5371 		.eml_capabilities = IEEE80211_EML_CAP_EMLSR_SUPP |
5372 				    IEEE80211_EML_CAP_EMLMR_SUPPORT,
5373 		.mld_capa_and_ops = MAC80211_HWSIM_MLD_CAPA_OPS,
5374 	},
5375 };
5376 
5377 static int mac80211_hwsim_new_radio(struct genl_info *info,
5378 				    struct hwsim_new_radio_params *param)
5379 {
5380 	int err;
5381 	u8 addr[ETH_ALEN];
5382 	struct mac80211_hwsim_data *data;
5383 	struct ieee80211_hw *hw;
5384 	enum nl80211_band band;
5385 	const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
5386 	struct net *net;
5387 	int idx, i;
5388 	int n_limits = 0;
5389 	int n_bands = 0;
5390 
5391 	if (WARN_ON(param->channels > 1 && !param->use_chanctx))
5392 		return -EINVAL;
5393 
5394 	spin_lock_bh(&hwsim_radio_lock);
5395 	idx = hwsim_radio_idx++;
5396 	spin_unlock_bh(&hwsim_radio_lock);
5397 
5398 	if (param->mlo)
5399 		ops = &mac80211_hwsim_mlo_ops;
5400 	else if (param->use_chanctx)
5401 		ops = &mac80211_hwsim_mchan_ops;
5402 	hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
5403 	if (!hw) {
5404 		pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
5405 		err = -ENOMEM;
5406 		goto failed;
5407 	}
5408 
5409 	/* ieee80211_alloc_hw_nm may have used a default name */
5410 	param->hwname = wiphy_name(hw->wiphy);
5411 
5412 	if (info)
5413 		net = genl_info_net(info);
5414 	else
5415 		net = &init_net;
5416 	wiphy_net_set(hw->wiphy, net);
5417 
5418 	data = hw->priv;
5419 	data->hw = hw;
5420 
5421 	data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
5422 	if (IS_ERR(data->dev)) {
5423 		printk(KERN_DEBUG
5424 		       "mac80211_hwsim: device_create failed (%ld)\n",
5425 		       PTR_ERR(data->dev));
5426 		err = -ENOMEM;
5427 		goto failed_drvdata;
5428 	}
5429 	data->dev->driver = &mac80211_hwsim_driver.driver;
5430 	err = device_bind_driver(data->dev);
5431 	if (err != 0) {
5432 		pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
5433 		       err);
5434 		goto failed_bind;
5435 	}
5436 
5437 	skb_queue_head_init(&data->pending);
5438 
5439 	SET_IEEE80211_DEV(hw, data->dev);
5440 	if (!param->perm_addr) {
5441 		eth_zero_addr(addr);
5442 		addr[0] = 0x02;
5443 		addr[3] = idx >> 8;
5444 		addr[4] = idx;
5445 		memcpy(data->addresses[0].addr, addr, ETH_ALEN);
5446 		/* Why need here second address ? */
5447 		memcpy(data->addresses[1].addr, addr, ETH_ALEN);
5448 		data->addresses[1].addr[0] |= 0x40;
5449 		memcpy(data->addresses[2].addr, addr, ETH_ALEN);
5450 		data->addresses[2].addr[0] |= 0x50;
5451 
5452 		hw->wiphy->n_addresses = 3;
5453 		hw->wiphy->addresses = data->addresses;
5454 		/* possible address clash is checked at hash table insertion */
5455 	} else {
5456 		memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
5457 		/* compatibility with automatically generated mac addr */
5458 		memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
5459 		memcpy(data->addresses[2].addr, param->perm_addr, ETH_ALEN);
5460 		hw->wiphy->n_addresses = 3;
5461 		hw->wiphy->addresses = data->addresses;
5462 	}
5463 
5464 	data->channels = param->channels;
5465 	data->use_chanctx = param->use_chanctx;
5466 	data->idx = idx;
5467 	data->destroy_on_close = param->destroy_on_close;
5468 	if (info)
5469 		data->portid = info->snd_portid;
5470 
5471 	/* setup interface limits, only on interface types we support */
5472 	if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
5473 		data->if_limits[n_limits].max = 1;
5474 		data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
5475 		n_limits++;
5476 	}
5477 
5478 	if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
5479 		data->if_limits[n_limits].max = 2048;
5480 		/*
5481 		 * For this case, we may only support a subset of
5482 		 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
5483 		 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
5484 		 */
5485 		data->if_limits[n_limits].types =
5486 					HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
5487 		n_limits++;
5488 	}
5489 
5490 	if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
5491 		data->if_limits[n_limits].max = 1;
5492 		data->if_limits[n_limits].types =
5493 						BIT(NL80211_IFTYPE_P2P_DEVICE);
5494 		n_limits++;
5495 	}
5496 
5497 	if (param->iftypes & BIT(NL80211_IFTYPE_NAN)) {
5498 		data->if_limits[n_limits].max = 1;
5499 		data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_NAN);
5500 		n_limits++;
5501 
5502 		hw->wiphy->nan_supported_bands = BIT(NL80211_BAND_2GHZ) |
5503 						 BIT(NL80211_BAND_5GHZ);
5504 
5505 		hw->wiphy->nan_capa.flags = WIPHY_NAN_FLAGS_CONFIGURABLE_SYNC |
5506 					    WIPHY_NAN_FLAGS_USERSPACE_DE;
5507 		hw->wiphy->nan_capa.op_mode = NAN_OP_MODE_PHY_MODE_MASK |
5508 					      NAN_OP_MODE_80P80MHZ |
5509 					      NAN_OP_MODE_160MHZ;
5510 
5511 		hw->wiphy->nan_capa.n_antennas = 0x22;
5512 		hw->wiphy->nan_capa.max_channel_switch_time = 0;
5513 		hw->wiphy->nan_capa.dev_capabilities =
5514 			NAN_DEV_CAPA_EXT_KEY_ID_SUPPORTED |
5515 			NAN_DEV_CAPA_NDPE_SUPPORTED;
5516 
5517 		hrtimer_setup(&data->nan_timer, mac80211_hwsim_nan_dw_start,
5518 			      CLOCK_MONOTONIC, HRTIMER_MODE_ABS_SOFT);
5519 	}
5520 
5521 	data->if_combination.radar_detect_widths =
5522 				BIT(NL80211_CHAN_WIDTH_5) |
5523 				BIT(NL80211_CHAN_WIDTH_10) |
5524 				BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5525 				BIT(NL80211_CHAN_WIDTH_20) |
5526 				BIT(NL80211_CHAN_WIDTH_40) |
5527 				BIT(NL80211_CHAN_WIDTH_80) |
5528 				BIT(NL80211_CHAN_WIDTH_160);
5529 
5530 	if (data->use_chanctx) {
5531 		hw->wiphy->max_scan_ssids = 255;
5532 		hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
5533 		hw->wiphy->max_remain_on_channel_duration = 1000;
5534 		data->if_combination.num_different_channels = data->channels;
5535 	} else {
5536 		data->if_combination.num_different_channels = 1;
5537 	}
5538 
5539 	if (!n_limits) {
5540 		err = -EINVAL;
5541 		goto failed_hw;
5542 	}
5543 
5544 	data->if_combination.max_interfaces = 0;
5545 	for (i = 0; i < n_limits; i++)
5546 		data->if_combination.max_interfaces +=
5547 			data->if_limits[i].max;
5548 
5549 	data->if_combination.n_limits = n_limits;
5550 	data->if_combination.limits = data->if_limits;
5551 
5552 	/*
5553 	 * If we actually were asked to support combinations,
5554 	 * advertise them - if there's only a single thing like
5555 	 * only IBSS then don't advertise it as combinations.
5556 	 */
5557 	if (data->if_combination.max_interfaces > 1) {
5558 		hw->wiphy->iface_combinations = &data->if_combination;
5559 		hw->wiphy->n_iface_combinations = 1;
5560 	}
5561 
5562 	if (param->ciphers) {
5563 		memcpy(data->ciphers, param->ciphers,
5564 		       param->n_ciphers * sizeof(u32));
5565 		hw->wiphy->cipher_suites = data->ciphers;
5566 		hw->wiphy->n_cipher_suites = param->n_ciphers;
5567 	}
5568 
5569 	hw->wiphy->mbssid_max_interfaces = 8;
5570 	hw->wiphy->ema_max_profile_periodicity = 3;
5571 
5572 	data->rx_rssi = DEFAULT_RX_RSSI;
5573 
5574 	INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
5575 	INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
5576 	INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
5577 
5578 	hw->queues = 5;
5579 	hw->offchannel_tx_hw_queue = 4;
5580 
5581 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
5582 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
5583 	ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
5584 	ieee80211_hw_set(hw, QUEUE_CONTROL);
5585 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
5586 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
5587 	ieee80211_hw_set(hw, MFP_CAPABLE);
5588 	ieee80211_hw_set(hw, SIGNAL_DBM);
5589 	ieee80211_hw_set(hw, SUPPORTS_PS);
5590 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
5591 	ieee80211_hw_set(hw, TDLS_WIDER_BW);
5592 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
5593 	ieee80211_hw_set(hw, STRICT);
5594 
5595 	if (param->mlo) {
5596 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
5597 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
5598 		ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
5599 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
5600 		ieee80211_hw_set(hw, AP_LINK_PS);
5601 
5602 		hw->wiphy->iftype_ext_capab = mac80211_hwsim_iftypes_ext_capa;
5603 		hw->wiphy->num_iftype_ext_capab =
5604 			ARRAY_SIZE(mac80211_hwsim_iftypes_ext_capa);
5605 	} else {
5606 		ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
5607 		ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
5608 		if (rctbl)
5609 			ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
5610 	}
5611 
5612 	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5613 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
5614 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
5615 			    WIPHY_FLAG_AP_UAPSD |
5616 			    WIPHY_FLAG_SUPPORTS_5_10_MHZ |
5617 			    WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5618 	hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
5619 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
5620 			       NL80211_FEATURE_STATIC_SMPS |
5621 			       NL80211_FEATURE_DYNAMIC_SMPS |
5622 			       NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
5623 			       NL80211_FEATURE_AP_SCAN;
5624 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
5625 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
5626 	wiphy_ext_feature_set(hw->wiphy,
5627 			      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
5628 	wiphy_ext_feature_set(hw->wiphy,
5629 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
5630 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
5631 
5632 	wiphy_ext_feature_set(hw->wiphy,
5633 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
5634 	wiphy_ext_feature_set(hw->wiphy,
5635 			      NL80211_EXT_FEATURE_BSS_COLOR);
5636 
5637 	hw->wiphy->interface_modes = param->iftypes;
5638 
5639 	/* ask mac80211 to reserve space for magic */
5640 	hw->vif_data_size = sizeof(struct hwsim_vif_priv);
5641 	hw->sta_data_size = sizeof(struct hwsim_sta_priv);
5642 	hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
5643 
5644 	memcpy(data->channels_2ghz, hwsim_channels_2ghz,
5645 		sizeof(hwsim_channels_2ghz));
5646 	memcpy(data->channels_5ghz, hwsim_channels_5ghz,
5647 		sizeof(hwsim_channels_5ghz));
5648 	memcpy(data->channels_6ghz, hwsim_channels_6ghz,
5649 		sizeof(hwsim_channels_6ghz));
5650 	memcpy(data->channels_s1g, hwsim_channels_s1g,
5651 	       sizeof(hwsim_channels_s1g));
5652 	memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
5653 
5654 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
5655 		struct ieee80211_supported_band *sband = &data->bands[band];
5656 		struct wiphy_radio_freq_range *radio_range;
5657 		const struct ieee80211_channel *c;
5658 		struct wiphy_radio *radio;
5659 
5660 		sband->band = band;
5661 
5662 		switch (band) {
5663 		case NL80211_BAND_2GHZ:
5664 			sband->channels = data->channels_2ghz;
5665 			sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
5666 			sband->bitrates = data->rates;
5667 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
5668 			break;
5669 		case NL80211_BAND_5GHZ:
5670 			sband->channels = data->channels_5ghz;
5671 			sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
5672 			sband->bitrates = data->rates + 4;
5673 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
5674 
5675 			sband->vht_cap.vht_supported = true;
5676 			sband->vht_cap.cap =
5677 				IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
5678 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
5679 				IEEE80211_VHT_CAP_RXLDPC |
5680 				IEEE80211_VHT_CAP_SHORT_GI_80 |
5681 				IEEE80211_VHT_CAP_SHORT_GI_160 |
5682 				IEEE80211_VHT_CAP_TXSTBC |
5683 				IEEE80211_VHT_CAP_RXSTBC_4 |
5684 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
5685 			sband->vht_cap.vht_mcs.rx_mcs_map =
5686 				cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
5687 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
5688 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
5689 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
5690 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
5691 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
5692 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
5693 					    IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
5694 			sband->vht_cap.vht_mcs.tx_mcs_map =
5695 				sband->vht_cap.vht_mcs.rx_mcs_map;
5696 			break;
5697 		case NL80211_BAND_6GHZ:
5698 			sband->channels = data->channels_6ghz;
5699 			sband->n_channels = ARRAY_SIZE(hwsim_channels_6ghz);
5700 			sband->bitrates = data->rates + 4;
5701 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
5702 			break;
5703 		case NL80211_BAND_S1GHZ:
5704 			memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
5705 			       sizeof(sband->s1g_cap));
5706 			sband->channels = data->channels_s1g;
5707 			sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
5708 			break;
5709 		default:
5710 			continue;
5711 		}
5712 
5713 		if (band != NL80211_BAND_6GHZ){
5714 			sband->ht_cap.ht_supported = true;
5715 			sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
5716 					    IEEE80211_HT_CAP_GRN_FLD |
5717 					    IEEE80211_HT_CAP_SGI_20 |
5718 					    IEEE80211_HT_CAP_SGI_40 |
5719 					    IEEE80211_HT_CAP_DSSSCCK40;
5720 			sband->ht_cap.ampdu_factor = 0x3;
5721 			sband->ht_cap.ampdu_density = 0x6;
5722 			memset(&sband->ht_cap.mcs, 0,
5723 			       sizeof(sband->ht_cap.mcs));
5724 			sband->ht_cap.mcs.rx_mask[0] = 0xff;
5725 			sband->ht_cap.mcs.rx_mask[1] = 0xff;
5726 			sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
5727 		}
5728 
5729 		mac80211_hwsim_sband_capab(sband);
5730 
5731 		hw->wiphy->bands[band] = sband;
5732 
5733 		if (!param->multi_radio)
5734 			continue;
5735 
5736 		c = sband->channels;
5737 		radio_range = &data->radio_range[n_bands];
5738 		radio_range->start_freq = ieee80211_channel_to_khz(c) - 10000;
5739 
5740 		c += sband->n_channels - 1;
5741 		radio_range->end_freq = ieee80211_channel_to_khz(c) + 10000;
5742 
5743 		radio = &data->radio[n_bands++];
5744 		radio->freq_range = radio_range;
5745 		radio->n_freq_range = 1;
5746 		radio->iface_combinations = &data->if_combination_radio;
5747 		radio->n_iface_combinations = 1;
5748 	}
5749 
5750 	if (param->multi_radio) {
5751 		hw->wiphy->radio = data->radio;
5752 		hw->wiphy->n_radio = n_bands;
5753 
5754 		memcpy(&data->if_combination_radio, &data->if_combination,
5755 		       sizeof(data->if_combination));
5756 		data->if_combination.num_different_channels *= n_bands;
5757 	}
5758 
5759 	if (data->use_chanctx)
5760 		data->if_combination.radar_detect_widths = 0;
5761 
5762 	/* By default all radios belong to the first group */
5763 	data->group = 1;
5764 	mutex_init(&data->mutex);
5765 
5766 	data->netgroup = hwsim_net_get_netgroup(net);
5767 	data->wmediumd = hwsim_net_get_wmediumd(net);
5768 
5769 	/* Enable frame retransmissions for lossy channels */
5770 	hw->max_rates = 4;
5771 	hw->max_rate_tries = 11;
5772 
5773 	hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
5774 	hw->wiphy->n_vendor_commands =
5775 		ARRAY_SIZE(mac80211_hwsim_vendor_commands);
5776 	hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
5777 	hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
5778 
5779 	if (param->reg_strict)
5780 		hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
5781 	if (param->regd) {
5782 		data->regd = param->regd;
5783 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
5784 		wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
5785 		/* give the regulatory workqueue a chance to run */
5786 		schedule_timeout_interruptible(1);
5787 	}
5788 
5789 	wiphy_ext_feature_set(hw->wiphy,
5790 			      NL80211_EXT_FEATURE_DFS_CONCURRENT);
5791 
5792 	if (param->no_vif)
5793 		ieee80211_hw_set(hw, NO_AUTO_VIF);
5794 
5795 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
5796 
5797 	for (i = 0; i < ARRAY_SIZE(data->link_data); i++) {
5798 		hrtimer_setup(&data->link_data[i].beacon_timer, mac80211_hwsim_beacon,
5799 			      CLOCK_MONOTONIC, HRTIMER_MODE_ABS_SOFT);
5800 		data->link_data[i].link_id = i;
5801 	}
5802 
5803 	err = ieee80211_register_hw(hw);
5804 	if (err < 0) {
5805 		pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
5806 		       err);
5807 		goto failed_hw;
5808 	}
5809 
5810 	wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
5811 
5812 	if (param->reg_alpha2) {
5813 		data->alpha2[0] = param->reg_alpha2[0];
5814 		data->alpha2[1] = param->reg_alpha2[1];
5815 		regulatory_hint(hw->wiphy, param->reg_alpha2);
5816 	}
5817 
5818 	data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
5819 	debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
5820 	debugfs_create_file("group", 0666, data->debugfs, data,
5821 			    &hwsim_fops_group);
5822 	debugfs_create_file("rx_rssi", 0666, data->debugfs, data,
5823 			    &hwsim_fops_rx_rssi);
5824 	if (!data->use_chanctx)
5825 		debugfs_create_file("dfs_simulate_radar", 0222,
5826 				    data->debugfs,
5827 				    data, &hwsim_simulate_radar);
5828 
5829 	if (param->pmsr_capa) {
5830 		data->pmsr_capa = *param->pmsr_capa;
5831 		hw->wiphy->pmsr_capa = &data->pmsr_capa;
5832 	}
5833 
5834 	spin_lock_bh(&hwsim_radio_lock);
5835 	err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
5836 				     hwsim_rht_params);
5837 	if (err < 0) {
5838 		if (info) {
5839 			GENL_SET_ERR_MSG(info, "perm addr already present");
5840 			NL_SET_BAD_ATTR(info->extack,
5841 					info->attrs[HWSIM_ATTR_PERM_ADDR]);
5842 		}
5843 		spin_unlock_bh(&hwsim_radio_lock);
5844 		goto failed_final_insert;
5845 	}
5846 
5847 	list_add_tail(&data->list, &hwsim_radios);
5848 	hwsim_radios_generation++;
5849 	spin_unlock_bh(&hwsim_radio_lock);
5850 
5851 	hwsim_mcast_new_radio(idx, info, param);
5852 
5853 	return idx;
5854 
5855 failed_final_insert:
5856 	debugfs_remove_recursive(data->debugfs);
5857 	ieee80211_unregister_hw(data->hw);
5858 failed_hw:
5859 	device_release_driver(data->dev);
5860 failed_bind:
5861 	device_unregister(data->dev);
5862 failed_drvdata:
5863 	ieee80211_free_hw(hw);
5864 failed:
5865 	return err;
5866 }
5867 
5868 static void hwsim_mcast_del_radio(int id, const char *hwname,
5869 				  struct genl_info *info)
5870 {
5871 	struct sk_buff *skb;
5872 	void *data;
5873 	int ret;
5874 
5875 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5876 	if (!skb)
5877 		return;
5878 
5879 	data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
5880 			   HWSIM_CMD_DEL_RADIO);
5881 	if (!data)
5882 		goto error;
5883 
5884 	ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
5885 	if (ret < 0)
5886 		goto error;
5887 
5888 	ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
5889 		      hwname);
5890 	if (ret < 0)
5891 		goto error;
5892 
5893 	genlmsg_end(skb, data);
5894 
5895 	hwsim_mcast_config_msg(skb, info);
5896 
5897 	return;
5898 
5899 error:
5900 	nlmsg_free(skb);
5901 }
5902 
5903 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
5904 				     const char *hwname,
5905 				     struct genl_info *info)
5906 {
5907 	hwsim_mcast_del_radio(data->idx, hwname, info);
5908 	debugfs_remove_recursive(data->debugfs);
5909 	ieee80211_unregister_hw(data->hw);
5910 	device_release_driver(data->dev);
5911 	device_unregister(data->dev);
5912 	ieee80211_free_hw(data->hw);
5913 }
5914 
5915 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
5916 				    struct mac80211_hwsim_data *data,
5917 				    u32 portid, u32 seq,
5918 				    struct netlink_callback *cb, int flags)
5919 {
5920 	void *hdr;
5921 	struct hwsim_new_radio_params param = { };
5922 	int res = -EMSGSIZE;
5923 
5924 	hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
5925 			  HWSIM_CMD_GET_RADIO);
5926 	if (!hdr)
5927 		return -EMSGSIZE;
5928 
5929 	if (cb)
5930 		genl_dump_check_consistent(cb, hdr);
5931 
5932 	if (data->alpha2[0] && data->alpha2[1])
5933 		param.reg_alpha2 = data->alpha2;
5934 
5935 	param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
5936 					REGULATORY_STRICT_REG);
5937 	param.p2p_device = !!(data->hw->wiphy->interface_modes &
5938 					BIT(NL80211_IFTYPE_P2P_DEVICE));
5939 	param.nan_device = !!(data->hw->wiphy->interface_modes &
5940 					BIT(NL80211_IFTYPE_NAN));
5941 	param.use_chanctx = data->use_chanctx;
5942 	param.regd = data->regd;
5943 	param.channels = data->channels;
5944 	param.hwname = wiphy_name(data->hw->wiphy);
5945 	param.pmsr_capa = &data->pmsr_capa;
5946 
5947 	res = append_radio_msg(skb, data->idx, &param);
5948 	if (res < 0)
5949 		goto out_err;
5950 
5951 	genlmsg_end(skb, hdr);
5952 	return 0;
5953 
5954 out_err:
5955 	genlmsg_cancel(skb, hdr);
5956 	return res;
5957 }
5958 
5959 static void mac80211_hwsim_free(void)
5960 {
5961 	struct mac80211_hwsim_data *data;
5962 
5963 	spin_lock_bh(&hwsim_radio_lock);
5964 	while ((data = list_first_entry_or_null(&hwsim_radios,
5965 						struct mac80211_hwsim_data,
5966 						list))) {
5967 		list_del(&data->list);
5968 		spin_unlock_bh(&hwsim_radio_lock);
5969 		mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
5970 					 NULL);
5971 		spin_lock_bh(&hwsim_radio_lock);
5972 	}
5973 	spin_unlock_bh(&hwsim_radio_lock);
5974 	class_destroy(hwsim_class);
5975 }
5976 
5977 static const struct net_device_ops hwsim_netdev_ops = {
5978 	.ndo_start_xmit 	= hwsim_mon_xmit,
5979 	.ndo_set_mac_address 	= eth_mac_addr,
5980 	.ndo_validate_addr	= eth_validate_addr,
5981 };
5982 
5983 static void hwsim_mon_setup(struct net_device *dev)
5984 {
5985 	u8 addr[ETH_ALEN];
5986 
5987 	dev->netdev_ops = &hwsim_netdev_ops;
5988 	dev->needs_free_netdev = true;
5989 	ether_setup(dev);
5990 	dev->priv_flags |= IFF_NO_QUEUE;
5991 	dev->type = ARPHRD_IEEE80211_RADIOTAP;
5992 	eth_zero_addr(addr);
5993 	addr[0] = 0x12;
5994 	eth_hw_addr_set(dev, addr);
5995 }
5996 
5997 static void hwsim_register_wmediumd(struct net *net, u32 portid)
5998 {
5999 	struct mac80211_hwsim_data *data;
6000 
6001 	hwsim_net_set_wmediumd(net, portid);
6002 
6003 	spin_lock_bh(&hwsim_radio_lock);
6004 	list_for_each_entry(data, &hwsim_radios, list) {
6005 		if (data->netgroup == hwsim_net_get_netgroup(net))
6006 			data->wmediumd = portid;
6007 	}
6008 	spin_unlock_bh(&hwsim_radio_lock);
6009 }
6010 
6011 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
6012 					   struct genl_info *info)
6013 {
6014 
6015 	struct ieee80211_hdr *hdr;
6016 	struct mac80211_hwsim_data *data2;
6017 	struct ieee80211_tx_info *txi;
6018 	struct hwsim_tx_rate *tx_attempts;
6019 	u64 ret_skb_cookie;
6020 	struct sk_buff *skb, *tmp;
6021 	const u8 *src;
6022 	unsigned int hwsim_flags;
6023 	int i;
6024 	unsigned long flags;
6025 	bool found = false;
6026 
6027 	if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
6028 	    !info->attrs[HWSIM_ATTR_FLAGS] ||
6029 	    !info->attrs[HWSIM_ATTR_COOKIE] ||
6030 	    !info->attrs[HWSIM_ATTR_SIGNAL] ||
6031 	    !info->attrs[HWSIM_ATTR_TX_INFO])
6032 		goto out;
6033 
6034 	src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
6035 	hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
6036 	ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
6037 
6038 	data2 = get_hwsim_data_ref_from_addr(src);
6039 	if (!data2)
6040 		goto out;
6041 
6042 	if (!hwsim_virtio_enabled) {
6043 		if (hwsim_net_get_netgroup(genl_info_net(info)) !=
6044 		    data2->netgroup)
6045 			goto out;
6046 
6047 		if (info->snd_portid != data2->wmediumd)
6048 			goto out;
6049 	}
6050 
6051 	/* look for the skb matching the cookie passed back from user */
6052 	spin_lock_irqsave(&data2->pending.lock, flags);
6053 	skb_queue_walk_safe(&data2->pending, skb, tmp) {
6054 		uintptr_t skb_cookie;
6055 
6056 		txi = IEEE80211_SKB_CB(skb);
6057 		skb_cookie = (uintptr_t)txi->rate_driver_data[0];
6058 
6059 		if (skb_cookie == ret_skb_cookie) {
6060 			__skb_unlink(skb, &data2->pending);
6061 			found = true;
6062 			break;
6063 		}
6064 	}
6065 	spin_unlock_irqrestore(&data2->pending.lock, flags);
6066 
6067 	/* not found */
6068 	if (!found)
6069 		goto out;
6070 
6071 	/* Tx info received because the frame was broadcasted on user space,
6072 	 so we get all the necessary info: tx attempts and skb control buff */
6073 
6074 	tx_attempts = (struct hwsim_tx_rate *)nla_data(
6075 		       info->attrs[HWSIM_ATTR_TX_INFO]);
6076 
6077 	/* now send back TX status */
6078 	txi = IEEE80211_SKB_CB(skb);
6079 
6080 	ieee80211_tx_info_clear_status(txi);
6081 
6082 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
6083 		txi->status.rates[i].idx = tx_attempts[i].idx;
6084 		txi->status.rates[i].count = tx_attempts[i].count;
6085 	}
6086 
6087 	txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
6088 
6089 	if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
6090 	   (hwsim_flags & HWSIM_TX_STAT_ACK)) {
6091 		if (skb->len >= 16) {
6092 			hdr = (struct ieee80211_hdr *) skb->data;
6093 			mac80211_hwsim_monitor_ack(data2->channel,
6094 						   hdr->addr2);
6095 		}
6096 		txi->flags |= IEEE80211_TX_STAT_ACK;
6097 	}
6098 
6099 	if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
6100 		txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
6101 
6102 	ieee80211_tx_status_irqsafe(data2->hw, skb);
6103 	return 0;
6104 out:
6105 	return -EINVAL;
6106 
6107 }
6108 
6109 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
6110 					  struct genl_info *info)
6111 {
6112 	struct mac80211_hwsim_data *data2;
6113 	struct ieee80211_rx_status rx_status;
6114 	struct ieee80211_hdr *hdr;
6115 	const u8 *dst;
6116 	int frame_data_len;
6117 	void *frame_data;
6118 	struct sk_buff *skb = NULL;
6119 	struct ieee80211_channel *channel = NULL;
6120 
6121 	if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
6122 	    !info->attrs[HWSIM_ATTR_FRAME] ||
6123 	    !info->attrs[HWSIM_ATTR_RX_RATE] ||
6124 	    !info->attrs[HWSIM_ATTR_SIGNAL])
6125 		goto out;
6126 
6127 	dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
6128 	frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
6129 	frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
6130 
6131 	if (frame_data_len < sizeof(struct ieee80211_hdr_3addr) ||
6132 	    frame_data_len > IEEE80211_MAX_DATA_LEN)
6133 		goto err;
6134 
6135 	/* Allocate new skb here */
6136 	skb = alloc_skb(frame_data_len, GFP_KERNEL);
6137 	if (skb == NULL)
6138 		goto err;
6139 
6140 	/* Copy the data */
6141 	skb_put_data(skb, frame_data, frame_data_len);
6142 
6143 	data2 = get_hwsim_data_ref_from_addr(dst);
6144 	if (!data2)
6145 		goto out;
6146 
6147 	if (data2->use_chanctx) {
6148 		if (data2->tmp_chan)
6149 			channel = data2->tmp_chan;
6150 	} else {
6151 		channel = data2->channel;
6152 	}
6153 
6154 	if (!hwsim_virtio_enabled) {
6155 		if (hwsim_net_get_netgroup(genl_info_net(info)) !=
6156 		    data2->netgroup)
6157 			goto out;
6158 
6159 		if (info->snd_portid != data2->wmediumd)
6160 			goto out;
6161 	}
6162 
6163 	/* check if radio is configured properly */
6164 
6165 	if ((data2->idle && !data2->tmp_chan) || !data2->started)
6166 		goto out;
6167 
6168 	/* A frame is received from user space */
6169 	memset(&rx_status, 0, sizeof(rx_status));
6170 	if (info->attrs[HWSIM_ATTR_FREQ]) {
6171 		struct tx_iter_data iter_data = {};
6172 
6173 		/* throw away off-channel packets, but allow both the temporary
6174 		 * ("hw" scan/remain-on-channel), regular channels and links,
6175 		 * since the internal datapath also allows this
6176 		 */
6177 		rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
6178 
6179 		iter_data.channel = ieee80211_get_channel(data2->hw->wiphy,
6180 							  rx_status.freq);
6181 		if (!iter_data.channel)
6182 			goto out;
6183 		rx_status.band = iter_data.channel->band;
6184 
6185 		mutex_lock(&data2->mutex);
6186 		if (!hwsim_chans_compat(iter_data.channel, channel)) {
6187 			ieee80211_iterate_active_interfaces_atomic(
6188 				data2->hw, IEEE80211_IFACE_ITER_NORMAL,
6189 				mac80211_hwsim_tx_iter, &iter_data);
6190 			if (!iter_data.receive) {
6191 				mutex_unlock(&data2->mutex);
6192 				goto out;
6193 			}
6194 		}
6195 		mutex_unlock(&data2->mutex);
6196 	} else if (!channel) {
6197 		goto out;
6198 	} else {
6199 		rx_status.freq = channel->center_freq;
6200 		rx_status.band = channel->band;
6201 	}
6202 
6203 	rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
6204 	if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
6205 		goto out;
6206 	rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
6207 
6208 	hdr = (void *)skb->data;
6209 
6210 	if (ieee80211_is_beacon(hdr->frame_control) ||
6211 	    ieee80211_is_probe_resp(hdr->frame_control))
6212 		rx_status.boottime_ns = ktime_get_boottime_ns();
6213 
6214 	mac80211_hwsim_rx(data2, &rx_status, skb);
6215 
6216 	return 0;
6217 err:
6218 	pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
6219 out:
6220 	dev_kfree_skb(skb);
6221 	return -EINVAL;
6222 }
6223 
6224 static int hwsim_register_received_nl(struct sk_buff *skb_2,
6225 				      struct genl_info *info)
6226 {
6227 	struct net *net = genl_info_net(info);
6228 	struct mac80211_hwsim_data *data;
6229 	int chans = 1;
6230 
6231 	spin_lock_bh(&hwsim_radio_lock);
6232 	list_for_each_entry(data, &hwsim_radios, list)
6233 		chans = max(chans, data->channels);
6234 	spin_unlock_bh(&hwsim_radio_lock);
6235 
6236 	/* In the future we should revise the userspace API and allow it
6237 	 * to set a flag that it does support multi-channel, then we can
6238 	 * let this pass conditionally on the flag.
6239 	 * For current userspace, prohibit it since it won't work right.
6240 	 */
6241 	if (chans > 1)
6242 		return -EOPNOTSUPP;
6243 
6244 	if (hwsim_net_get_wmediumd(net))
6245 		return -EBUSY;
6246 
6247 	hwsim_register_wmediumd(net, info->snd_portid);
6248 
6249 	pr_debug("mac80211_hwsim: received a REGISTER, "
6250 	       "switching to wmediumd mode with pid %d\n", info->snd_portid);
6251 
6252 	return 0;
6253 }
6254 
6255 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
6256 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
6257 {
6258 	int i;
6259 
6260 	for (i = 0; i < n_ciphers; i++) {
6261 		int j;
6262 		int found = 0;
6263 
6264 		for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
6265 			if (ciphers[i] == hwsim_ciphers[j]) {
6266 				found = 1;
6267 				break;
6268 			}
6269 		}
6270 
6271 		if (!found)
6272 			return false;
6273 	}
6274 
6275 	return true;
6276 }
6277 
6278 static int parse_ftm_capa(const struct nlattr *ftm_capa, struct cfg80211_pmsr_capabilities *out,
6279 			  struct genl_info *info)
6280 {
6281 	struct nlattr *tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX + 1];
6282 	int ret;
6283 
6284 	ret = nla_parse_nested(tb, NL80211_PMSR_FTM_CAPA_ATTR_MAX, ftm_capa, hwsim_ftm_capa_policy,
6285 			       NULL);
6286 	if (ret) {
6287 		NL_SET_ERR_MSG_ATTR(info->extack, ftm_capa, "malformed FTM capability");
6288 		return -EINVAL;
6289 	}
6290 
6291 	out->ftm.supported = 1;
6292 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES])
6293 		out->ftm.preambles = nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES]);
6294 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS])
6295 		out->ftm.bandwidths = nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS]);
6296 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT])
6297 		out->ftm.max_bursts_exponent =
6298 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT]);
6299 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST])
6300 		out->ftm.max_ftms_per_burst =
6301 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST]);
6302 	out->ftm.asap = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_ASAP];
6303 	out->ftm.non_asap = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP];
6304 	out->ftm.request_lci = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI];
6305 	out->ftm.request_civicloc = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC];
6306 	out->ftm.trigger_based = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED];
6307 	out->ftm.non_trigger_based = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED];
6308 
6309 	return 0;
6310 }
6311 
6312 static int parse_pmsr_capa(const struct nlattr *pmsr_capa, struct cfg80211_pmsr_capabilities *out,
6313 			   struct genl_info *info)
6314 {
6315 	struct nlattr *tb[NL80211_PMSR_ATTR_MAX + 1];
6316 	struct nlattr *nla;
6317 	int size;
6318 	int ret;
6319 
6320 	ret = nla_parse_nested(tb, NL80211_PMSR_ATTR_MAX, pmsr_capa, hwsim_pmsr_capa_policy, NULL);
6321 	if (ret) {
6322 		NL_SET_ERR_MSG_ATTR(info->extack, pmsr_capa, "malformed PMSR capability");
6323 		return -EINVAL;
6324 	}
6325 
6326 	if (tb[NL80211_PMSR_ATTR_MAX_PEERS])
6327 		out->max_peers = nla_get_u32(tb[NL80211_PMSR_ATTR_MAX_PEERS]);
6328 	out->report_ap_tsf = !!tb[NL80211_PMSR_ATTR_REPORT_AP_TSF];
6329 	out->randomize_mac_addr = !!tb[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR];
6330 
6331 	if (!tb[NL80211_PMSR_ATTR_TYPE_CAPA]) {
6332 		NL_SET_ERR_MSG_ATTR(info->extack, tb[NL80211_PMSR_ATTR_TYPE_CAPA],
6333 				    "malformed PMSR type");
6334 		return -EINVAL;
6335 	}
6336 
6337 	nla_for_each_nested(nla, tb[NL80211_PMSR_ATTR_TYPE_CAPA], size) {
6338 		switch (nla_type(nla)) {
6339 		case NL80211_PMSR_TYPE_FTM:
6340 			parse_ftm_capa(nla, out, info);
6341 			break;
6342 		default:
6343 			NL_SET_ERR_MSG_ATTR(info->extack, nla, "unsupported measurement type");
6344 			return -EINVAL;
6345 		}
6346 	}
6347 
6348 	return 0;
6349 }
6350 
6351 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
6352 {
6353 	struct hwsim_new_radio_params param = { 0 };
6354 	const char *hwname = NULL;
6355 	int ret;
6356 
6357 	param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
6358 	param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
6359 	param.nan_device = info->attrs[HWSIM_ATTR_SUPPORT_NAN_DEVICE];
6360 	param.channels = channels;
6361 	param.destroy_on_close =
6362 		info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
6363 
6364 	if (info->attrs[HWSIM_ATTR_CHANNELS])
6365 		param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
6366 
6367 	if (param.channels < 1) {
6368 		GENL_SET_ERR_MSG(info, "must have at least one channel");
6369 		return -EINVAL;
6370 	}
6371 
6372 	if (info->attrs[HWSIM_ATTR_NO_VIF])
6373 		param.no_vif = true;
6374 
6375 	if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
6376 		param.use_chanctx = true;
6377 	else
6378 		param.use_chanctx = (param.channels > 1);
6379 
6380 	if (info->attrs[HWSIM_ATTR_MULTI_RADIO])
6381 		param.multi_radio = true;
6382 
6383 	if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
6384 		param.reg_alpha2 =
6385 			nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
6386 
6387 	if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
6388 		u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
6389 
6390 		if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
6391 			return -EINVAL;
6392 
6393 		idx = array_index_nospec(idx,
6394 					 ARRAY_SIZE(hwsim_world_regdom_custom));
6395 		param.regd = hwsim_world_regdom_custom[idx];
6396 	}
6397 
6398 	if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
6399 		if (!is_valid_ether_addr(
6400 				nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
6401 			GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
6402 			NL_SET_BAD_ATTR(info->extack,
6403 					info->attrs[HWSIM_ATTR_PERM_ADDR]);
6404 			return -EINVAL;
6405 		}
6406 
6407 		param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
6408 	}
6409 
6410 	if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
6411 		param.iftypes =
6412 			nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
6413 
6414 		if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
6415 			NL_SET_ERR_MSG_ATTR(info->extack,
6416 					    info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
6417 					    "cannot support more iftypes than kernel");
6418 			return -EINVAL;
6419 		}
6420 	} else {
6421 		param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
6422 	}
6423 
6424 	/* ensure both flag and iftype support is honored */
6425 	if (param.p2p_device ||
6426 	    param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
6427 		param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
6428 		param.p2p_device = true;
6429 	}
6430 
6431 	/* ensure both flag and iftype support is honored */
6432 	if (param.nan_device ||
6433 	    param.iftypes & BIT(NL80211_IFTYPE_NAN)) {
6434 		param.iftypes |= BIT(NL80211_IFTYPE_NAN);
6435 		param.nan_device = true;
6436 	}
6437 
6438 	if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
6439 		u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
6440 
6441 		param.ciphers =
6442 			nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
6443 
6444 		if (len % sizeof(u32)) {
6445 			NL_SET_ERR_MSG_ATTR(info->extack,
6446 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6447 					    "bad cipher list length");
6448 			return -EINVAL;
6449 		}
6450 
6451 		param.n_ciphers = len / sizeof(u32);
6452 
6453 		if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
6454 			NL_SET_ERR_MSG_ATTR(info->extack,
6455 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6456 					    "too many ciphers specified");
6457 			return -EINVAL;
6458 		}
6459 
6460 		if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
6461 			NL_SET_ERR_MSG_ATTR(info->extack,
6462 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6463 					    "unsupported ciphers specified");
6464 			return -EINVAL;
6465 		}
6466 	}
6467 
6468 	param.mlo = info->attrs[HWSIM_ATTR_MLO_SUPPORT];
6469 
6470 	if (param.mlo || param.multi_radio)
6471 		param.use_chanctx = true;
6472 
6473 	if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
6474 		hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6475 				  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6476 				  GFP_KERNEL);
6477 		if (!hwname)
6478 			return -ENOMEM;
6479 		param.hwname = hwname;
6480 	}
6481 
6482 	if (info->attrs[HWSIM_ATTR_PMSR_SUPPORT]) {
6483 		struct cfg80211_pmsr_capabilities *pmsr_capa;
6484 
6485 		pmsr_capa = kmalloc(sizeof(*pmsr_capa), GFP_KERNEL);
6486 		if (!pmsr_capa) {
6487 			ret = -ENOMEM;
6488 			goto out_free;
6489 		}
6490 		param.pmsr_capa = pmsr_capa;
6491 
6492 		ret = parse_pmsr_capa(info->attrs[HWSIM_ATTR_PMSR_SUPPORT], pmsr_capa, info);
6493 		if (ret)
6494 			goto out_free;
6495 	}
6496 
6497 	ret = mac80211_hwsim_new_radio(info, &param);
6498 
6499 out_free:
6500 	kfree(hwname);
6501 	kfree(param.pmsr_capa);
6502 	return ret;
6503 }
6504 
6505 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
6506 {
6507 	struct mac80211_hwsim_data *data;
6508 	s64 idx = -1;
6509 	const char *hwname = NULL;
6510 
6511 	if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
6512 		idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
6513 	} else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
6514 		hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6515 				  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6516 				  GFP_KERNEL);
6517 		if (!hwname)
6518 			return -ENOMEM;
6519 	} else
6520 		return -EINVAL;
6521 
6522 	spin_lock_bh(&hwsim_radio_lock);
6523 	list_for_each_entry(data, &hwsim_radios, list) {
6524 		if (idx >= 0) {
6525 			if (data->idx != idx)
6526 				continue;
6527 		} else {
6528 			if (!hwname ||
6529 			    strcmp(hwname, wiphy_name(data->hw->wiphy)))
6530 				continue;
6531 		}
6532 
6533 		if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
6534 			continue;
6535 
6536 		list_del(&data->list);
6537 		rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
6538 				       hwsim_rht_params);
6539 		hwsim_radios_generation++;
6540 		spin_unlock_bh(&hwsim_radio_lock);
6541 		mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
6542 					 info);
6543 		kfree(hwname);
6544 		return 0;
6545 	}
6546 	spin_unlock_bh(&hwsim_radio_lock);
6547 
6548 	kfree(hwname);
6549 	return -ENODEV;
6550 }
6551 
6552 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
6553 {
6554 	struct mac80211_hwsim_data *data;
6555 	struct sk_buff *skb;
6556 	int idx, res = -ENODEV;
6557 
6558 	if (!info->attrs[HWSIM_ATTR_RADIO_ID])
6559 		return -EINVAL;
6560 	idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
6561 
6562 	spin_lock_bh(&hwsim_radio_lock);
6563 	list_for_each_entry(data, &hwsim_radios, list) {
6564 		if (data->idx != idx)
6565 			continue;
6566 
6567 		if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
6568 			continue;
6569 
6570 		skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
6571 		if (!skb) {
6572 			res = -ENOMEM;
6573 			goto out_err;
6574 		}
6575 
6576 		res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
6577 					       info->snd_seq, NULL, 0);
6578 		if (res < 0) {
6579 			nlmsg_free(skb);
6580 			goto out_err;
6581 		}
6582 
6583 		res = genlmsg_reply(skb, info);
6584 		break;
6585 	}
6586 
6587 out_err:
6588 	spin_unlock_bh(&hwsim_radio_lock);
6589 
6590 	return res;
6591 }
6592 
6593 static int hwsim_dump_radio_nl(struct sk_buff *skb,
6594 			       struct netlink_callback *cb)
6595 {
6596 	int last_idx = cb->args[0] - 1;
6597 	struct mac80211_hwsim_data *data = NULL;
6598 	int res = 0;
6599 	void *hdr;
6600 
6601 	spin_lock_bh(&hwsim_radio_lock);
6602 	cb->seq = hwsim_radios_generation;
6603 
6604 	if (last_idx >= hwsim_radio_idx-1)
6605 		goto done;
6606 
6607 	list_for_each_entry(data, &hwsim_radios, list) {
6608 		if (data->idx <= last_idx)
6609 			continue;
6610 
6611 		if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
6612 			continue;
6613 
6614 		res = mac80211_hwsim_get_radio(skb, data,
6615 					       NETLINK_CB(cb->skb).portid,
6616 					       cb->nlh->nlmsg_seq, cb,
6617 					       NLM_F_MULTI);
6618 		if (res < 0)
6619 			break;
6620 
6621 		last_idx = data->idx;
6622 	}
6623 
6624 	cb->args[0] = last_idx + 1;
6625 
6626 	/* list changed, but no new element sent, set interrupted flag */
6627 	if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
6628 		hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
6629 				  cb->nlh->nlmsg_seq, &hwsim_genl_family,
6630 				  NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
6631 		if (hdr) {
6632 			genl_dump_check_consistent(cb, hdr);
6633 			genlmsg_end(skb, hdr);
6634 		} else {
6635 			res = -EMSGSIZE;
6636 		}
6637 	}
6638 
6639 done:
6640 	spin_unlock_bh(&hwsim_radio_lock);
6641 	return res ?: skb->len;
6642 }
6643 
6644 /* Generic Netlink operations array */
6645 static const struct genl_small_ops hwsim_ops[] = {
6646 	{
6647 		.cmd = HWSIM_CMD_REGISTER,
6648 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6649 		.doit = hwsim_register_received_nl,
6650 		.flags = GENL_UNS_ADMIN_PERM,
6651 	},
6652 	{
6653 		.cmd = HWSIM_CMD_FRAME,
6654 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6655 		.doit = hwsim_cloned_frame_received_nl,
6656 	},
6657 	{
6658 		.cmd = HWSIM_CMD_TX_INFO_FRAME,
6659 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6660 		.doit = hwsim_tx_info_frame_received_nl,
6661 	},
6662 	{
6663 		.cmd = HWSIM_CMD_NEW_RADIO,
6664 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6665 		.doit = hwsim_new_radio_nl,
6666 		.flags = GENL_UNS_ADMIN_PERM,
6667 	},
6668 	{
6669 		.cmd = HWSIM_CMD_DEL_RADIO,
6670 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6671 		.doit = hwsim_del_radio_nl,
6672 		.flags = GENL_UNS_ADMIN_PERM,
6673 	},
6674 	{
6675 		.cmd = HWSIM_CMD_GET_RADIO,
6676 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6677 		.doit = hwsim_get_radio_nl,
6678 		.dumpit = hwsim_dump_radio_nl,
6679 	},
6680 	{
6681 		.cmd = HWSIM_CMD_REPORT_PMSR,
6682 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6683 		.doit = hwsim_pmsr_report_nl,
6684 	},
6685 };
6686 
6687 static struct genl_family hwsim_genl_family __ro_after_init = {
6688 	.name = "MAC80211_HWSIM",
6689 	.version = 1,
6690 	.maxattr = HWSIM_ATTR_MAX,
6691 	.policy = hwsim_genl_policy,
6692 	.netnsok = true,
6693 	.module = THIS_MODULE,
6694 	.small_ops = hwsim_ops,
6695 	.n_small_ops = ARRAY_SIZE(hwsim_ops),
6696 	.resv_start_op = HWSIM_CMD_REPORT_PMSR + 1, // match with __HWSIM_CMD_MAX
6697 	.mcgrps = hwsim_mcgrps,
6698 	.n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
6699 };
6700 
6701 static void remove_user_radios(u32 portid)
6702 {
6703 	struct mac80211_hwsim_data *entry, *tmp;
6704 	LIST_HEAD(list);
6705 
6706 	spin_lock_bh(&hwsim_radio_lock);
6707 	list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
6708 		if (entry->destroy_on_close && entry->portid == portid) {
6709 			list_move(&entry->list, &list);
6710 			rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
6711 					       hwsim_rht_params);
6712 			hwsim_radios_generation++;
6713 		}
6714 	}
6715 	spin_unlock_bh(&hwsim_radio_lock);
6716 
6717 	list_for_each_entry_safe(entry, tmp, &list, list) {
6718 		list_del(&entry->list);
6719 		mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
6720 					 NULL);
6721 	}
6722 }
6723 
6724 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
6725 					 unsigned long state,
6726 					 void *_notify)
6727 {
6728 	struct netlink_notify *notify = _notify;
6729 
6730 	if (state != NETLINK_URELEASE)
6731 		return NOTIFY_DONE;
6732 
6733 	remove_user_radios(notify->portid);
6734 
6735 	if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
6736 		printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
6737 		       " socket, switching to perfect channel medium\n");
6738 		hwsim_register_wmediumd(notify->net, 0);
6739 	}
6740 	return NOTIFY_DONE;
6741 
6742 }
6743 
6744 static struct notifier_block hwsim_netlink_notifier = {
6745 	.notifier_call = mac80211_hwsim_netlink_notify,
6746 };
6747 
6748 static int __init hwsim_init_netlink(void)
6749 {
6750 	int rc;
6751 
6752 	printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
6753 
6754 	rc = genl_register_family(&hwsim_genl_family);
6755 	if (rc)
6756 		goto failure;
6757 
6758 	rc = netlink_register_notifier(&hwsim_netlink_notifier);
6759 	if (rc) {
6760 		genl_unregister_family(&hwsim_genl_family);
6761 		goto failure;
6762 	}
6763 
6764 	return 0;
6765 
6766 failure:
6767 	pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
6768 	return -EINVAL;
6769 }
6770 
6771 static __net_init int hwsim_init_net(struct net *net)
6772 {
6773 	return hwsim_net_set_netgroup(net);
6774 }
6775 
6776 static void __net_exit hwsim_exit_net(struct net *net)
6777 {
6778 	struct mac80211_hwsim_data *data, *tmp;
6779 	LIST_HEAD(list);
6780 
6781 	spin_lock_bh(&hwsim_radio_lock);
6782 	list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
6783 		if (!net_eq(wiphy_net(data->hw->wiphy), net))
6784 			continue;
6785 
6786 		/* Radios created in init_net are returned to init_net. */
6787 		if (data->netgroup == hwsim_net_get_netgroup(&init_net))
6788 			continue;
6789 
6790 		list_move(&data->list, &list);
6791 		rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
6792 				       hwsim_rht_params);
6793 		hwsim_radios_generation++;
6794 	}
6795 	spin_unlock_bh(&hwsim_radio_lock);
6796 
6797 	list_for_each_entry_safe(data, tmp, &list, list) {
6798 		list_del(&data->list);
6799 		mac80211_hwsim_del_radio(data,
6800 					 wiphy_name(data->hw->wiphy),
6801 					 NULL);
6802 	}
6803 
6804 	ida_free(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
6805 }
6806 
6807 static struct pernet_operations hwsim_net_ops = {
6808 	.init = hwsim_init_net,
6809 	.exit = hwsim_exit_net,
6810 	.id   = &hwsim_net_id,
6811 	.size = sizeof(struct hwsim_net),
6812 };
6813 
6814 static void hwsim_exit_netlink(void)
6815 {
6816 	/* unregister the notifier */
6817 	netlink_unregister_notifier(&hwsim_netlink_notifier);
6818 	/* unregister the family */
6819 	genl_unregister_family(&hwsim_genl_family);
6820 }
6821 
6822 #if IS_REACHABLE(CONFIG_VIRTIO)
6823 static void hwsim_virtio_tx_done(struct virtqueue *vq)
6824 {
6825 	unsigned int len;
6826 	struct sk_buff *skb;
6827 	unsigned long flags;
6828 
6829 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
6830 	while ((skb = virtqueue_get_buf(vq, &len)))
6831 		dev_kfree_skb_irq(skb);
6832 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6833 }
6834 
6835 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
6836 {
6837 	struct nlmsghdr *nlh;
6838 	struct genlmsghdr *gnlh;
6839 	struct nlattr *tb[HWSIM_ATTR_MAX + 1];
6840 	struct genl_info info = {};
6841 	int err;
6842 
6843 	nlh = nlmsg_hdr(skb);
6844 	gnlh = nlmsg_data(nlh);
6845 
6846 	if (skb->len < nlh->nlmsg_len)
6847 		return -EINVAL;
6848 
6849 	err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
6850 			    hwsim_genl_policy, NULL);
6851 	if (err) {
6852 		pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
6853 		return err;
6854 	}
6855 
6856 	info.attrs = tb;
6857 
6858 	switch (gnlh->cmd) {
6859 	case HWSIM_CMD_FRAME:
6860 		hwsim_cloned_frame_received_nl(skb, &info);
6861 		break;
6862 	case HWSIM_CMD_TX_INFO_FRAME:
6863 		hwsim_tx_info_frame_received_nl(skb, &info);
6864 		break;
6865 	case HWSIM_CMD_REPORT_PMSR:
6866 		hwsim_pmsr_report_nl(skb, &info);
6867 		break;
6868 	default:
6869 		pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
6870 		return -EPROTO;
6871 	}
6872 	return 0;
6873 }
6874 
6875 static void hwsim_virtio_rx_work(struct work_struct *work)
6876 {
6877 	struct virtqueue *vq;
6878 	unsigned int len;
6879 	struct sk_buff *skb;
6880 	struct scatterlist sg[1];
6881 	int err;
6882 	unsigned long flags;
6883 
6884 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
6885 	if (!hwsim_virtio_enabled)
6886 		goto out_unlock;
6887 
6888 	skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
6889 	if (!skb)
6890 		goto out_unlock;
6891 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6892 
6893 	skb->data = skb->head;
6894 	skb_reset_tail_pointer(skb);
6895 	skb_put(skb, len);
6896 	hwsim_virtio_handle_cmd(skb);
6897 
6898 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
6899 	if (!hwsim_virtio_enabled) {
6900 		dev_kfree_skb_irq(skb);
6901 		goto out_unlock;
6902 	}
6903 	vq = hwsim_vqs[HWSIM_VQ_RX];
6904 	sg_init_one(sg, skb->head, skb_end_offset(skb));
6905 	err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
6906 	if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
6907 		dev_kfree_skb_irq(skb);
6908 	else
6909 		virtqueue_kick(vq);
6910 	schedule_work(&hwsim_virtio_rx);
6911 
6912 out_unlock:
6913 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6914 }
6915 
6916 static void hwsim_virtio_rx_done(struct virtqueue *vq)
6917 {
6918 	schedule_work(&hwsim_virtio_rx);
6919 }
6920 
6921 static int init_vqs(struct virtio_device *vdev)
6922 {
6923 	struct virtqueue_info vqs_info[HWSIM_NUM_VQS] = {
6924 		[HWSIM_VQ_TX] = { "tx", hwsim_virtio_tx_done },
6925 		[HWSIM_VQ_RX] = { "rx", hwsim_virtio_rx_done },
6926 	};
6927 
6928 	return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
6929 			       hwsim_vqs, vqs_info, NULL);
6930 }
6931 
6932 static int fill_vq(struct virtqueue *vq)
6933 {
6934 	int i, err;
6935 	struct sk_buff *skb;
6936 	struct scatterlist sg[1];
6937 
6938 	for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
6939 		skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6940 		if (!skb)
6941 			return -ENOMEM;
6942 
6943 		sg_init_one(sg, skb->head, skb_end_offset(skb));
6944 		err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
6945 		if (err) {
6946 			nlmsg_free(skb);
6947 			return err;
6948 		}
6949 	}
6950 	virtqueue_kick(vq);
6951 	return 0;
6952 }
6953 
6954 static void remove_vqs(struct virtio_device *vdev)
6955 {
6956 	int i;
6957 
6958 	virtio_reset_device(vdev);
6959 
6960 	for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
6961 		struct virtqueue *vq = hwsim_vqs[i];
6962 		struct sk_buff *skb;
6963 
6964 		while ((skb = virtqueue_detach_unused_buf(vq)))
6965 			nlmsg_free(skb);
6966 	}
6967 
6968 	vdev->config->del_vqs(vdev);
6969 }
6970 
6971 static int hwsim_virtio_probe(struct virtio_device *vdev)
6972 {
6973 	int err;
6974 	unsigned long flags;
6975 
6976 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
6977 	if (hwsim_virtio_enabled) {
6978 		spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6979 		return -EEXIST;
6980 	}
6981 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6982 
6983 	err = init_vqs(vdev);
6984 	if (err)
6985 		return err;
6986 
6987 	virtio_device_ready(vdev);
6988 
6989 	err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
6990 	if (err)
6991 		goto out_remove;
6992 
6993 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
6994 	hwsim_virtio_enabled = true;
6995 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
6996 
6997 	schedule_work(&hwsim_virtio_rx);
6998 	return 0;
6999 
7000 out_remove:
7001 	remove_vqs(vdev);
7002 	return err;
7003 }
7004 
7005 static void hwsim_virtio_remove(struct virtio_device *vdev)
7006 {
7007 	hwsim_virtio_enabled = false;
7008 
7009 	cancel_work_sync(&hwsim_virtio_rx);
7010 
7011 	remove_vqs(vdev);
7012 }
7013 
7014 /* MAC80211_HWSIM virtio device id table */
7015 static const struct virtio_device_id id_table[] = {
7016 	{ VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
7017 	{ 0 }
7018 };
7019 MODULE_DEVICE_TABLE(virtio, id_table);
7020 
7021 static struct virtio_driver virtio_hwsim = {
7022 	.driver.name = KBUILD_MODNAME,
7023 	.id_table = id_table,
7024 	.probe = hwsim_virtio_probe,
7025 	.remove = hwsim_virtio_remove,
7026 };
7027 
7028 static int hwsim_register_virtio_driver(void)
7029 {
7030 	return register_virtio_driver(&virtio_hwsim);
7031 }
7032 
7033 static void hwsim_unregister_virtio_driver(void)
7034 {
7035 	unregister_virtio_driver(&virtio_hwsim);
7036 }
7037 #else
7038 static inline int hwsim_register_virtio_driver(void)
7039 {
7040 	return 0;
7041 }
7042 
7043 static inline void hwsim_unregister_virtio_driver(void)
7044 {
7045 }
7046 #endif
7047 
7048 static int __init init_mac80211_hwsim(void)
7049 {
7050 	int i, err;
7051 
7052 	if (radios < 0 || radios > 100)
7053 		return -EINVAL;
7054 
7055 	if (channels < 1)
7056 		return -EINVAL;
7057 
7058 	err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
7059 	if (err)
7060 		return err;
7061 
7062 	err = register_pernet_device(&hwsim_net_ops);
7063 	if (err)
7064 		goto out_free_rht;
7065 
7066 	err = platform_driver_register(&mac80211_hwsim_driver);
7067 	if (err)
7068 		goto out_unregister_pernet;
7069 
7070 	err = hwsim_init_netlink();
7071 	if (err)
7072 		goto out_unregister_driver;
7073 
7074 	err = hwsim_register_virtio_driver();
7075 	if (err)
7076 		goto out_exit_netlink;
7077 
7078 	hwsim_class = class_create("mac80211_hwsim");
7079 	if (IS_ERR(hwsim_class)) {
7080 		err = PTR_ERR(hwsim_class);
7081 		goto out_exit_virtio;
7082 	}
7083 
7084 	hwsim_init_s1g_channels(hwsim_channels_s1g);
7085 
7086 	for (i = 0; i < radios; i++) {
7087 		struct hwsim_new_radio_params param = { 0 };
7088 
7089 		param.channels = channels;
7090 
7091 		switch (regtest) {
7092 		case HWSIM_REGTEST_DIFF_COUNTRY:
7093 			if (i < ARRAY_SIZE(hwsim_alpha2s))
7094 				param.reg_alpha2 = hwsim_alpha2s[i];
7095 			break;
7096 		case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
7097 			if (!i)
7098 				param.reg_alpha2 = hwsim_alpha2s[0];
7099 			break;
7100 		case HWSIM_REGTEST_STRICT_ALL:
7101 			param.reg_strict = true;
7102 			fallthrough;
7103 		case HWSIM_REGTEST_DRIVER_REG_ALL:
7104 			param.reg_alpha2 = hwsim_alpha2s[0];
7105 			break;
7106 		case HWSIM_REGTEST_WORLD_ROAM:
7107 			if (i == 0)
7108 				param.regd = &hwsim_world_regdom_custom_01;
7109 			break;
7110 		case HWSIM_REGTEST_CUSTOM_WORLD:
7111 			param.regd = &hwsim_world_regdom_custom_03;
7112 			break;
7113 		case HWSIM_REGTEST_CUSTOM_WORLD_2:
7114 			if (i == 0)
7115 				param.regd = &hwsim_world_regdom_custom_03;
7116 			else if (i == 1)
7117 				param.regd = &hwsim_world_regdom_custom_02;
7118 			break;
7119 		case HWSIM_REGTEST_STRICT_FOLLOW:
7120 			if (i == 0) {
7121 				param.reg_strict = true;
7122 				param.reg_alpha2 = hwsim_alpha2s[0];
7123 			}
7124 			break;
7125 		case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
7126 			if (i == 0) {
7127 				param.reg_strict = true;
7128 				param.reg_alpha2 = hwsim_alpha2s[0];
7129 			} else if (i == 1) {
7130 				param.reg_alpha2 = hwsim_alpha2s[1];
7131 			}
7132 			break;
7133 		case HWSIM_REGTEST_ALL:
7134 			switch (i) {
7135 			case 0:
7136 				param.regd = &hwsim_world_regdom_custom_01;
7137 				break;
7138 			case 1:
7139 				param.regd = &hwsim_world_regdom_custom_02;
7140 				break;
7141 			case 2:
7142 				param.reg_alpha2 = hwsim_alpha2s[0];
7143 				break;
7144 			case 3:
7145 				param.reg_alpha2 = hwsim_alpha2s[1];
7146 				break;
7147 			case 4:
7148 				param.reg_strict = true;
7149 				param.reg_alpha2 = hwsim_alpha2s[2];
7150 				break;
7151 			}
7152 			break;
7153 		default:
7154 			break;
7155 		}
7156 
7157 		param.p2p_device = support_p2p_device;
7158 		param.nan_device = true;
7159 		param.mlo = mlo;
7160 		param.multi_radio = multi_radio;
7161 		param.use_chanctx = channels > 1 || mlo || multi_radio;
7162 		param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
7163 		if (param.p2p_device)
7164 			param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
7165 		param.iftypes |= BIT(NL80211_IFTYPE_NAN);
7166 
7167 		err = mac80211_hwsim_new_radio(NULL, &param);
7168 		if (err < 0)
7169 			goto out_free_radios;
7170 	}
7171 
7172 	hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
7173 				 hwsim_mon_setup);
7174 	if (hwsim_mon == NULL) {
7175 		err = -ENOMEM;
7176 		goto out_free_radios;
7177 	}
7178 
7179 	rtnl_lock();
7180 	err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
7181 	if (err < 0) {
7182 		rtnl_unlock();
7183 		goto out_free_mon;
7184 	}
7185 
7186 	err = register_netdevice(hwsim_mon);
7187 	if (err < 0) {
7188 		rtnl_unlock();
7189 		goto out_free_mon;
7190 	}
7191 	rtnl_unlock();
7192 
7193 	return 0;
7194 
7195 out_free_mon:
7196 	free_netdev(hwsim_mon);
7197 out_free_radios:
7198 	mac80211_hwsim_free();
7199 out_exit_virtio:
7200 	hwsim_unregister_virtio_driver();
7201 out_exit_netlink:
7202 	hwsim_exit_netlink();
7203 out_unregister_driver:
7204 	platform_driver_unregister(&mac80211_hwsim_driver);
7205 out_unregister_pernet:
7206 	unregister_pernet_device(&hwsim_net_ops);
7207 out_free_rht:
7208 	rhashtable_destroy(&hwsim_radios_rht);
7209 	return err;
7210 }
7211 module_init(init_mac80211_hwsim);
7212 
7213 static void __exit exit_mac80211_hwsim(void)
7214 {
7215 	pr_debug("mac80211_hwsim: unregister radios\n");
7216 
7217 	hwsim_unregister_virtio_driver();
7218 	hwsim_exit_netlink();
7219 
7220 	mac80211_hwsim_free();
7221 
7222 	rhashtable_destroy(&hwsim_radios_rht);
7223 	unregister_netdev(hwsim_mon);
7224 	platform_driver_unregister(&mac80211_hwsim_driver);
7225 	unregister_pernet_device(&hwsim_net_ops);
7226 }
7227 module_exit(exit_mac80211_hwsim);
7228