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