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