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