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