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