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