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