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