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