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