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