xref: /linux/drivers/net/wireless/virtual/mac80211_hwsim_main.c (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
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 	wiphy_dbg(hw->wiphy, "%s:\n", __func__);
3538 
3539 	for_each_set_bit(i, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
3540 		mac80211_hwsim_config_mac_nl(hw, old[i]->addr, false);
3541 		wiphy_dbg(hw->wiphy,
3542 			  "  link [%d/%pM] removed\n", i, old[i]->addr);
3543 	}
3544 
3545 	for_each_set_bit(i, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
3546 		struct ieee80211_bss_conf *link_conf;
3547 
3548 		link_conf = link_conf_dereference_protected(vif, i);
3549 		if (WARN_ON(!link_conf))
3550 			continue;
3551 
3552 		mac80211_hwsim_config_mac_nl(hw, link_conf->addr, true);
3553 		wiphy_dbg(hw->wiphy,
3554 			  "  link [%d/%pM] added\n", i, link_conf->addr);
3555 	}
3556 
3557 	return 0;
3558 }
3559 
3560 static int mac80211_hwsim_change_sta_links(struct ieee80211_hw *hw,
3561 					   struct ieee80211_vif *vif,
3562 					   struct ieee80211_sta *sta,
3563 					   u16 old_links, u16 new_links)
3564 {
3565 	struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
3566 
3567 	hwsim_check_sta_magic(sta);
3568 
3569 	if (vif->type == NL80211_IFTYPE_STATION)
3570 		sp->active_links_rx = new_links;
3571 
3572 	return 0;
3573 }
3574 
3575 static int mac80211_hwsim_send_pmsr_ftm_request_peer(struct sk_buff *msg,
3576 						     struct cfg80211_pmsr_ftm_request_peer *request)
3577 {
3578 	struct nlattr *ftm;
3579 
3580 	if (!request->requested)
3581 		return -EINVAL;
3582 
3583 	ftm = nla_nest_start(msg, NL80211_PMSR_TYPE_FTM);
3584 	if (!ftm)
3585 		return -ENOBUFS;
3586 
3587 	if (nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE, request->preamble))
3588 		return -ENOBUFS;
3589 
3590 	if (nla_put_u16(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD, request->burst_period))
3591 		return -ENOBUFS;
3592 
3593 	if (request->asap && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_ASAP))
3594 		return -ENOBUFS;
3595 
3596 	if (request->request_lci && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI))
3597 		return -ENOBUFS;
3598 
3599 	if (request->request_civicloc &&
3600 	    nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC))
3601 		return -ENOBUFS;
3602 
3603 	if (request->trigger_based && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED))
3604 		return -ENOBUFS;
3605 
3606 	if (request->non_trigger_based &&
3607 	    nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED))
3608 		return -ENOBUFS;
3609 
3610 	if (request->lmr_feedback && nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK))
3611 		return -ENOBUFS;
3612 
3613 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP, request->num_bursts_exp))
3614 		return -ENOBUFS;
3615 
3616 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION, request->burst_duration))
3617 		return -ENOBUFS;
3618 
3619 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST, request->ftms_per_burst))
3620 		return -ENOBUFS;
3621 
3622 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES, request->ftmr_retries))
3623 		return -ENOBUFS;
3624 
3625 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION, request->burst_duration))
3626 		return -ENOBUFS;
3627 
3628 	if (nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_BSS_COLOR, request->bss_color))
3629 		return -ENOBUFS;
3630 
3631 	if (request->min_time_between_measurements &&
3632 	    nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_MIN_TIME_BETWEEN_MEASUREMENTS,
3633 			request->min_time_between_measurements))
3634 		return -ENOBUFS;
3635 
3636 	if (request->max_time_between_measurements &&
3637 	    nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_MAX_TIME_BETWEEN_MEASUREMENTS,
3638 			request->max_time_between_measurements))
3639 		return -ENOBUFS;
3640 
3641 	if (request->availability_window &&
3642 	    nla_put_u8(msg, NL80211_PMSR_FTM_REQ_ATTR_AW_DURATION,
3643 		       request->availability_window))
3644 		return -ENOBUFS;
3645 
3646 	if (request->nominal_time &&
3647 	    nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_NOMINAL_TIME,
3648 			request->nominal_time))
3649 		return -ENOBUFS;
3650 
3651 	if (request->num_measurements &&
3652 	    nla_put_u32(msg, NL80211_PMSR_FTM_REQ_ATTR_NUM_MEASUREMENTS,
3653 			request->num_measurements))
3654 		return -ENOBUFS;
3655 
3656 	if (request->ingress_distance &&
3657 	    nla_put_u64_64bit(msg, NL80211_PMSR_FTM_REQ_ATTR_INGRESS,
3658 			      request->ingress_distance,
3659 			      NL80211_PMSR_FTM_REQ_ATTR_PAD))
3660 		return -ENOBUFS;
3661 
3662 	if (request->egress_distance &&
3663 	    nla_put_u64_64bit(msg, NL80211_PMSR_FTM_REQ_ATTR_EGRESS,
3664 			      request->egress_distance,
3665 			      NL80211_PMSR_FTM_REQ_ATTR_PAD))
3666 		return -ENOBUFS;
3667 
3668 	if (request->pd_suppress_range_results &&
3669 	    nla_put_flag(msg, NL80211_PMSR_FTM_REQ_ATTR_PD_SUPPRESS_RESULTS))
3670 		return -ENOBUFS;
3671 
3672 	nla_nest_end(msg, ftm);
3673 
3674 	return 0;
3675 }
3676 
3677 static int mac80211_hwsim_send_pmsr_request_peer(struct sk_buff *msg,
3678 						 struct cfg80211_pmsr_request_peer *request)
3679 {
3680 	struct nlattr *peer, *chandef, *req, *data;
3681 	int err;
3682 
3683 	peer = nla_nest_start(msg, NL80211_PMSR_ATTR_PEERS);
3684 	if (!peer)
3685 		return -ENOBUFS;
3686 
3687 	if (nla_put(msg, NL80211_PMSR_PEER_ATTR_ADDR, ETH_ALEN,
3688 		    request->addr))
3689 		return -ENOBUFS;
3690 
3691 	chandef = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_CHAN);
3692 	if (!chandef)
3693 		return -ENOBUFS;
3694 
3695 	err = nl80211_send_chandef(msg, &request->chandef);
3696 	if (err)
3697 		return err;
3698 
3699 	nla_nest_end(msg, chandef);
3700 
3701 	req = nla_nest_start(msg, NL80211_PMSR_PEER_ATTR_REQ);
3702 	if (!req)
3703 		return -ENOBUFS;
3704 
3705 	if (request->report_ap_tsf && nla_put_flag(msg, NL80211_PMSR_REQ_ATTR_GET_AP_TSF))
3706 		return -ENOBUFS;
3707 
3708 	data = nla_nest_start(msg, NL80211_PMSR_REQ_ATTR_DATA);
3709 	if (!data)
3710 		return -ENOBUFS;
3711 
3712 	err = mac80211_hwsim_send_pmsr_ftm_request_peer(msg, &request->ftm);
3713 	if (err)
3714 		return err;
3715 
3716 	nla_nest_end(msg, data);
3717 	nla_nest_end(msg, req);
3718 	nla_nest_end(msg, peer);
3719 
3720 	return 0;
3721 }
3722 
3723 static int mac80211_hwsim_send_pmsr_request(struct sk_buff *msg,
3724 					    struct cfg80211_pmsr_request *request)
3725 {
3726 	struct nlattr *pmsr;
3727 	int err;
3728 
3729 	pmsr = nla_nest_start(msg, NL80211_ATTR_PEER_MEASUREMENTS);
3730 	if (!pmsr)
3731 		return -ENOBUFS;
3732 
3733 	if (nla_put_u32(msg, NL80211_ATTR_TIMEOUT, request->timeout))
3734 		return -ENOBUFS;
3735 
3736 	if (!is_zero_ether_addr(request->mac_addr)) {
3737 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, request->mac_addr))
3738 			return -ENOBUFS;
3739 		if (nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, request->mac_addr_mask))
3740 			return -ENOBUFS;
3741 	}
3742 
3743 	for (int i = 0; i < request->n_peers; i++) {
3744 		err = mac80211_hwsim_send_pmsr_request_peer(msg, &request->peers[i]);
3745 		if (err)
3746 			return err;
3747 	}
3748 
3749 	nla_nest_end(msg, pmsr);
3750 
3751 	return 0;
3752 }
3753 
3754 static int mac80211_hwsim_start_pmsr(struct ieee80211_hw *hw,
3755 				     struct ieee80211_vif *vif,
3756 				     struct cfg80211_pmsr_request *request)
3757 {
3758 	struct mac80211_hwsim_data *data;
3759 	struct sk_buff *skb = NULL;
3760 	struct nlattr *pmsr;
3761 	void *msg_head;
3762 	u32 _portid;
3763 	int err = 0;
3764 
3765 	data = hw->priv;
3766 	_portid = READ_ONCE(data->wmediumd);
3767 	if (!_portid && !hwsim_virtio_enabled)
3768 		return -EOPNOTSUPP;
3769 
3770 	mutex_lock(&data->mutex);
3771 
3772 	if (data->pmsr_request) {
3773 		err = -EBUSY;
3774 		goto out_free;
3775 	}
3776 
3777 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3778 
3779 	if (!skb) {
3780 		err = -ENOMEM;
3781 		goto out_free;
3782 	}
3783 
3784 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0, HWSIM_CMD_START_PMSR);
3785 
3786 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
3787 		    ETH_ALEN, data->addresses[1].addr)) {
3788 		err = -ENOMEM;
3789 		goto out_free;
3790 	}
3791 
3792 	pmsr = nla_nest_start(skb, HWSIM_ATTR_PMSR_REQUEST);
3793 	if (!pmsr) {
3794 		err = -ENOMEM;
3795 		goto out_free;
3796 	}
3797 
3798 	err = mac80211_hwsim_send_pmsr_request(skb, request);
3799 	if (err)
3800 		goto out_free;
3801 
3802 	nla_nest_end(skb, pmsr);
3803 
3804 	genlmsg_end(skb, msg_head);
3805 	if (hwsim_virtio_enabled)
3806 		hwsim_tx_virtio(data, skb);
3807 	else
3808 		hwsim_unicast_netgroup(data, skb, _portid);
3809 
3810 	data->pmsr_request = request;
3811 	data->pmsr_request_wdev = ieee80211_vif_to_wdev(vif);
3812 
3813 out_free:
3814 	if (err && skb)
3815 		nlmsg_free(skb);
3816 
3817 	mutex_unlock(&data->mutex);
3818 	return err;
3819 }
3820 
3821 static void mac80211_hwsim_abort_pmsr(struct ieee80211_hw *hw,
3822 				      struct ieee80211_vif *vif,
3823 				      struct cfg80211_pmsr_request *request)
3824 {
3825 	struct mac80211_hwsim_data *data;
3826 	struct sk_buff *skb = NULL;
3827 	struct nlattr *pmsr;
3828 	void *msg_head;
3829 	u32 _portid;
3830 	int err = 0;
3831 
3832 	data = hw->priv;
3833 	_portid = READ_ONCE(data->wmediumd);
3834 	if (!_portid && !hwsim_virtio_enabled)
3835 		return;
3836 
3837 	mutex_lock(&data->mutex);
3838 
3839 	if (data->pmsr_request != request) {
3840 		err = -EINVAL;
3841 		goto out;
3842 	}
3843 
3844 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3845 	if (!skb) {
3846 		err = -ENOMEM;
3847 		goto out;
3848 	}
3849 
3850 	msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0, HWSIM_CMD_ABORT_PMSR);
3851 
3852 	if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER, ETH_ALEN, data->addresses[1].addr))
3853 		goto out;
3854 
3855 	pmsr = nla_nest_start(skb, HWSIM_ATTR_PMSR_REQUEST);
3856 	if (!pmsr) {
3857 		err = -ENOMEM;
3858 		goto out;
3859 	}
3860 
3861 	err = mac80211_hwsim_send_pmsr_request(skb, request);
3862 	if (err)
3863 		goto out;
3864 
3865 	err = nla_nest_end(skb, pmsr);
3866 	if (err)
3867 		goto out;
3868 
3869 	genlmsg_end(skb, msg_head);
3870 	if (hwsim_virtio_enabled)
3871 		hwsim_tx_virtio(data, skb);
3872 	else
3873 		hwsim_unicast_netgroup(data, skb, _portid);
3874 
3875 out:
3876 	if (err && skb)
3877 		nlmsg_free(skb);
3878 
3879 	mutex_unlock(&data->mutex);
3880 }
3881 
3882 static int mac80211_hwsim_parse_rate_info(struct nlattr *rateattr,
3883 					  struct rate_info *rate_info,
3884 					  struct genl_info *info)
3885 {
3886 	struct nlattr *tb[HWSIM_RATE_INFO_ATTR_MAX + 1];
3887 	int ret;
3888 
3889 	ret = nla_parse_nested(tb, HWSIM_RATE_INFO_ATTR_MAX,
3890 			       rateattr, hwsim_rate_info_policy, info->extack);
3891 	if (ret)
3892 		return ret;
3893 
3894 	if (tb[HWSIM_RATE_INFO_ATTR_FLAGS])
3895 		rate_info->flags = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_FLAGS]);
3896 
3897 	if (tb[HWSIM_RATE_INFO_ATTR_MCS])
3898 		rate_info->mcs = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_MCS]);
3899 
3900 	if (tb[HWSIM_RATE_INFO_ATTR_LEGACY])
3901 		rate_info->legacy = nla_get_u16(tb[HWSIM_RATE_INFO_ATTR_LEGACY]);
3902 
3903 	if (tb[HWSIM_RATE_INFO_ATTR_NSS])
3904 		rate_info->nss = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_NSS]);
3905 
3906 	if (tb[HWSIM_RATE_INFO_ATTR_BW])
3907 		rate_info->bw = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_BW]);
3908 
3909 	if (tb[HWSIM_RATE_INFO_ATTR_HE_GI])
3910 		rate_info->he_gi = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_GI]);
3911 
3912 	if (tb[HWSIM_RATE_INFO_ATTR_HE_DCM])
3913 		rate_info->he_dcm = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_DCM]);
3914 
3915 	if (tb[HWSIM_RATE_INFO_ATTR_HE_RU_ALLOC])
3916 		rate_info->he_ru_alloc =
3917 			nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_HE_RU_ALLOC]);
3918 
3919 	if (tb[HWSIM_RATE_INFO_ATTR_N_BOUNDED_CH])
3920 		rate_info->n_bonded_ch = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_N_BOUNDED_CH]);
3921 
3922 	if (tb[HWSIM_RATE_INFO_ATTR_EHT_GI])
3923 		rate_info->eht_gi = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_EHT_GI]);
3924 
3925 	if (tb[HWSIM_RATE_INFO_ATTR_EHT_RU_ALLOC])
3926 		rate_info->eht_ru_alloc = nla_get_u8(tb[HWSIM_RATE_INFO_ATTR_EHT_RU_ALLOC]);
3927 
3928 	return 0;
3929 }
3930 
3931 static int mac80211_hwsim_parse_ftm_result(struct nlattr *ftm,
3932 					   struct cfg80211_pmsr_ftm_result *result,
3933 					   struct genl_info *info)
3934 {
3935 	struct nlattr *tb[NL80211_PMSR_FTM_RESP_ATTR_MAX + 1];
3936 	int ret;
3937 
3938 	ret = nla_parse_nested(tb, NL80211_PMSR_FTM_RESP_ATTR_MAX,
3939 			       ftm, hwsim_ftm_result_policy, info->extack);
3940 	if (ret)
3941 		return ret;
3942 
3943 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON])
3944 		result->failure_reason = nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON]);
3945 
3946 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX])
3947 		result->burst_index = nla_get_u16(tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_INDEX]);
3948 
3949 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS]) {
3950 		result->num_ftmr_attempts_valid = 1;
3951 		result->num_ftmr_attempts =
3952 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_ATTEMPTS]);
3953 	}
3954 
3955 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES]) {
3956 		result->num_ftmr_successes_valid = 1;
3957 		result->num_ftmr_successes =
3958 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_FTMR_SUCCESSES]);
3959 	}
3960 
3961 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME])
3962 		result->busy_retry_time =
3963 			nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME]);
3964 
3965 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP])
3966 		result->num_bursts_exp = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_BURSTS_EXP]);
3967 
3968 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION])
3969 		result->burst_duration = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_BURST_DURATION]);
3970 
3971 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST])
3972 		result->ftms_per_burst = nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_FTMS_PER_BURST]);
3973 
3974 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG]) {
3975 		result->rssi_avg_valid = 1;
3976 		result->rssi_avg = nla_get_s32(tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_AVG]);
3977 	}
3978 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD]) {
3979 		result->rssi_spread_valid = 1;
3980 		result->rssi_spread =
3981 			nla_get_s32(tb[NL80211_PMSR_FTM_RESP_ATTR_RSSI_SPREAD]);
3982 	}
3983 
3984 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE]) {
3985 		result->tx_rate_valid = 1;
3986 		ret = mac80211_hwsim_parse_rate_info(tb[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE],
3987 						     &result->tx_rate, info);
3988 		if (ret)
3989 			return ret;
3990 	}
3991 
3992 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE]) {
3993 		result->rx_rate_valid = 1;
3994 		ret = mac80211_hwsim_parse_rate_info(tb[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE],
3995 						     &result->rx_rate, info);
3996 		if (ret)
3997 			return ret;
3998 	}
3999 
4000 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]) {
4001 		result->rtt_avg_valid = 1;
4002 		result->rtt_avg =
4003 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_AVG]);
4004 	}
4005 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE]) {
4006 		result->rtt_variance_valid = 1;
4007 		result->rtt_variance =
4008 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_VARIANCE]);
4009 	}
4010 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD]) {
4011 		result->rtt_spread_valid = 1;
4012 		result->rtt_spread =
4013 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_RTT_SPREAD]);
4014 	}
4015 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG]) {
4016 		result->dist_avg_valid = 1;
4017 		result->dist_avg =
4018 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_AVG]);
4019 	}
4020 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE]) {
4021 		result->dist_variance_valid = 1;
4022 		result->dist_variance =
4023 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_VARIANCE]);
4024 	}
4025 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD]) {
4026 		result->dist_spread_valid = 1;
4027 		result->dist_spread =
4028 			nla_get_u64(tb[NL80211_PMSR_FTM_RESP_ATTR_DIST_SPREAD]);
4029 	}
4030 
4031 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]) {
4032 		result->lci = nla_data(tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]);
4033 		result->lci_len = nla_len(tb[NL80211_PMSR_FTM_RESP_ATTR_LCI]);
4034 	}
4035 
4036 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]) {
4037 		result->civicloc = nla_data(tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]);
4038 		result->civicloc_len = nla_len(tb[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]);
4039 	}
4040 
4041 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_TX_LTF_REPETITION_COUNT]) {
4042 		result->tx_ltf_repetition_count_valid = 1;
4043 		result->tx_ltf_repetition_count =
4044 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_TX_LTF_REPETITION_COUNT]);
4045 	}
4046 
4047 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_RX_LTF_REPETITION_COUNT]) {
4048 		result->rx_ltf_repetition_count_valid = 1;
4049 		result->rx_ltf_repetition_count =
4050 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_RX_LTF_REPETITION_COUNT]);
4051 	}
4052 
4053 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_MAX_TIME_BETWEEN_MEASUREMENTS]) {
4054 		result->max_time_between_measurements_valid = 1;
4055 		result->max_time_between_measurements =
4056 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_MAX_TIME_BETWEEN_MEASUREMENTS]);
4057 	}
4058 
4059 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_MIN_TIME_BETWEEN_MEASUREMENTS]) {
4060 		result->min_time_between_measurements_valid = 1;
4061 		result->min_time_between_measurements =
4062 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_MIN_TIME_BETWEEN_MEASUREMENTS]);
4063 	}
4064 
4065 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_TX_SPATIAL_STREAMS]) {
4066 		result->num_tx_spatial_streams_valid = 1;
4067 		result->num_tx_spatial_streams =
4068 			nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_TX_SPATIAL_STREAMS]);
4069 	}
4070 
4071 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_RX_SPATIAL_STREAMS]) {
4072 		result->num_rx_spatial_streams_valid = 1;
4073 		result->num_rx_spatial_streams =
4074 			nla_get_u8(tb[NL80211_PMSR_FTM_RESP_ATTR_NUM_RX_SPATIAL_STREAMS]);
4075 	}
4076 
4077 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_NOMINAL_TIME]) {
4078 		result->nominal_time_valid = 1;
4079 		result->nominal_time =
4080 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_NOMINAL_TIME]);
4081 	}
4082 
4083 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_AVAILABILITY_WINDOW]) {
4084 		result->availability_window_valid = 1;
4085 		result->availability_window =
4086 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_AVAILABILITY_WINDOW]);
4087 	}
4088 
4089 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_CHANNEL_WIDTH]) {
4090 		result->chan_width_valid = 1;
4091 		result->chan_width =
4092 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_CHANNEL_WIDTH]);
4093 	}
4094 
4095 	if (tb[NL80211_PMSR_FTM_RESP_ATTR_PREAMBLE]) {
4096 		result->preamble_valid = 1;
4097 		result->preamble =
4098 			nla_get_u32(tb[NL80211_PMSR_FTM_RESP_ATTR_PREAMBLE]);
4099 	}
4100 
4101 	result->is_delayed_lmr =
4102 		nla_get_flag(tb[NL80211_PMSR_FTM_RESP_ATTR_IS_DELAYED_LMR]);
4103 
4104 	return 0;
4105 }
4106 
4107 static int mac80211_hwsim_parse_pmsr_resp(struct nlattr *resp,
4108 					  struct cfg80211_pmsr_result *result,
4109 					  struct genl_info *info)
4110 {
4111 	struct nlattr *tb[NL80211_PMSR_RESP_ATTR_MAX + 1];
4112 	struct nlattr *pmsr;
4113 	int rem;
4114 	int ret;
4115 
4116 	ret = nla_parse_nested(tb, NL80211_PMSR_RESP_ATTR_MAX, resp, hwsim_pmsr_resp_policy,
4117 			       info->extack);
4118 	if (ret)
4119 		return ret;
4120 
4121 	if (tb[NL80211_PMSR_RESP_ATTR_STATUS])
4122 		result->status = nla_get_u32(tb[NL80211_PMSR_RESP_ATTR_STATUS]);
4123 
4124 	if (tb[NL80211_PMSR_RESP_ATTR_HOST_TIME])
4125 		result->host_time = nla_get_u64(tb[NL80211_PMSR_RESP_ATTR_HOST_TIME]);
4126 
4127 	if (tb[NL80211_PMSR_RESP_ATTR_AP_TSF]) {
4128 		result->ap_tsf_valid = 1;
4129 		result->ap_tsf = nla_get_u64(tb[NL80211_PMSR_RESP_ATTR_AP_TSF]);
4130 	}
4131 
4132 	result->final = !!tb[NL80211_PMSR_RESP_ATTR_FINAL];
4133 
4134 	if (!tb[NL80211_PMSR_RESP_ATTR_DATA])
4135 		return 0;
4136 
4137 	nla_for_each_nested(pmsr, tb[NL80211_PMSR_RESP_ATTR_DATA], rem) {
4138 		switch (nla_type(pmsr)) {
4139 		case NL80211_PMSR_TYPE_FTM:
4140 			result->type = NL80211_PMSR_TYPE_FTM;
4141 			ret = mac80211_hwsim_parse_ftm_result(pmsr, &result->ftm, info);
4142 			if (ret)
4143 				return ret;
4144 			break;
4145 		default:
4146 			NL_SET_ERR_MSG_ATTR(info->extack, pmsr, "Unknown pmsr resp type");
4147 			return -EINVAL;
4148 		}
4149 	}
4150 
4151 	return 0;
4152 }
4153 
4154 static int mac80211_hwsim_parse_pmsr_result(struct nlattr *peer,
4155 					    struct cfg80211_pmsr_result *result,
4156 					    struct genl_info *info)
4157 {
4158 	struct nlattr *tb[NL80211_PMSR_PEER_ATTR_MAX + 1];
4159 	int ret;
4160 
4161 	if (!peer)
4162 		return -EINVAL;
4163 
4164 	ret = nla_parse_nested(tb, NL80211_PMSR_PEER_ATTR_MAX, peer,
4165 			       hwsim_pmsr_peer_result_policy, info->extack);
4166 	if (ret)
4167 		return ret;
4168 
4169 	if (tb[NL80211_PMSR_PEER_ATTR_ADDR])
4170 		memcpy(result->addr, nla_data(tb[NL80211_PMSR_PEER_ATTR_ADDR]),
4171 		       ETH_ALEN);
4172 
4173 	if (tb[NL80211_PMSR_PEER_ATTR_RESP]) {
4174 		ret = mac80211_hwsim_parse_pmsr_resp(tb[NL80211_PMSR_PEER_ATTR_RESP], result, info);
4175 		if (ret)
4176 			return ret;
4177 	}
4178 
4179 	return 0;
4180 };
4181 
4182 static int hwsim_pmsr_report_nl(struct sk_buff *msg, struct genl_info *info)
4183 {
4184 	struct mac80211_hwsim_data *data;
4185 	struct nlattr *peers, *peer;
4186 	struct nlattr *reqattr;
4187 	const u8 *src;
4188 	int err;
4189 	int rem;
4190 
4191 	if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER])
4192 		return -EINVAL;
4193 
4194 	src = nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
4195 	data = get_hwsim_data_ref_from_addr(src);
4196 	if (!data)
4197 		return -EINVAL;
4198 
4199 	mutex_lock(&data->mutex);
4200 	if (!data->pmsr_request) {
4201 		err = -EINVAL;
4202 		goto out;
4203 	}
4204 
4205 	reqattr = info->attrs[HWSIM_ATTR_PMSR_RESULT];
4206 	if (!reqattr) {
4207 		err = -EINVAL;
4208 		goto out;
4209 	}
4210 
4211 	peers = nla_find_nested(reqattr, NL80211_PMSR_ATTR_PEERS);
4212 	if (!peers) {
4213 		err = -EINVAL;
4214 		goto out;
4215 	}
4216 
4217 	nla_for_each_nested(peer, peers, rem) {
4218 		struct cfg80211_pmsr_result result = {};
4219 
4220 		err = mac80211_hwsim_parse_pmsr_result(peer, &result, info);
4221 		if (err)
4222 			goto out;
4223 
4224 		cfg80211_pmsr_report(data->pmsr_request_wdev,
4225 				     data->pmsr_request, &result, GFP_KERNEL);
4226 	}
4227 
4228 	cfg80211_pmsr_complete(data->pmsr_request_wdev, data->pmsr_request, GFP_KERNEL);
4229 
4230 	err = 0;
4231 out:
4232 	data->pmsr_request = NULL;
4233 	data->pmsr_request_wdev = NULL;
4234 
4235 	mutex_unlock(&data->mutex);
4236 	return err;
4237 }
4238 
4239 static int mac80211_hwsim_set_radar_background(struct ieee80211_hw *hw,
4240 					       struct cfg80211_chan_def *chan)
4241 {
4242 	struct mac80211_hwsim_data *data = hw->priv;
4243 
4244 	if (!wiphy_ext_feature_isset(hw->wiphy,
4245 				     NL80211_EXT_FEATURE_RADAR_BACKGROUND))
4246 		return -EOPNOTSUPP;
4247 
4248 	if (chan)
4249 		data->radar_background_chandef = *chan;
4250 	else
4251 		memset(&data->radar_background_chandef, 0,
4252 		       sizeof(data->radar_background_chandef));
4253 
4254 	return 0;
4255 }
4256 
4257 #ifdef CONFIG_MAC80211_DEBUGFS
4258 #define HWSIM_DEBUGFS_OPS					\
4259 	.link_add_debugfs = mac80211_hwsim_link_add_debugfs,
4260 #else
4261 #define HWSIM_DEBUGFS_OPS
4262 #endif
4263 
4264 #define HWSIM_COMMON_OPS					\
4265 	.tx = mac80211_hwsim_tx,				\
4266 	.wake_tx_queue = ieee80211_hwsim_wake_tx_queue,		\
4267 	.start = mac80211_hwsim_start,				\
4268 	.stop = mac80211_hwsim_stop,				\
4269 	.add_interface = mac80211_hwsim_add_interface,		\
4270 	.change_interface = mac80211_hwsim_change_interface,	\
4271 	.remove_interface = mac80211_hwsim_remove_interface,	\
4272 	.config = mac80211_hwsim_config,			\
4273 	.configure_filter = mac80211_hwsim_configure_filter,	\
4274 	.vif_cfg_changed = mac80211_hwsim_vif_info_changed,	\
4275 	.link_info_changed = mac80211_hwsim_link_info_changed,  \
4276 	.tx_last_beacon = mac80211_hwsim_tx_last_beacon,	\
4277 	.sta_notify = mac80211_hwsim_sta_notify,		\
4278 	.link_sta_rc_update = mac80211_hwsim_sta_rc_update,	\
4279 	.conf_tx = mac80211_hwsim_conf_tx,			\
4280 	.get_survey = mac80211_hwsim_get_survey,		\
4281 	CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)	\
4282 	.ampdu_action = mac80211_hwsim_ampdu_action,		\
4283 	.flush = mac80211_hwsim_flush,				\
4284 	.get_et_sset_count = mac80211_hwsim_get_et_sset_count,	\
4285 	.get_et_stats = mac80211_hwsim_get_et_stats,		\
4286 	.get_et_strings = mac80211_hwsim_get_et_strings,	\
4287 	.start_pmsr = mac80211_hwsim_start_pmsr,		\
4288 	.abort_pmsr = mac80211_hwsim_abort_pmsr,		\
4289 	.set_radar_background = mac80211_hwsim_set_radar_background, \
4290 	.set_key = mac80211_hwsim_set_key,			\
4291 	.set_rts_threshold = mac80211_hwsim_set_rts_threshold,	\
4292 	.start_nan = mac80211_hwsim_nan_start,			\
4293 	.stop_nan = mac80211_hwsim_nan_stop,			\
4294 	.nan_change_conf = mac80211_hwsim_nan_change_config,	\
4295 	.nan_peer_sched_changed = mac80211_hwsim_nan_peer_sched_changed, \
4296 	HWSIM_DEBUGFS_OPS
4297 
4298 #define HWSIM_NON_MLO_OPS					\
4299 	.sta_add = mac80211_hwsim_sta_add,			\
4300 	.sta_remove = mac80211_hwsim_sta_remove,		\
4301 	.set_tim = mac80211_hwsim_set_tim,			\
4302 	.get_tsf = mac80211_hwsim_get_tsf,			\
4303 	.set_tsf = mac80211_hwsim_set_tsf,
4304 
4305 static const struct ieee80211_ops mac80211_hwsim_ops = {
4306 	HWSIM_COMMON_OPS
4307 	HWSIM_NON_MLO_OPS
4308 	.sw_scan_start = mac80211_hwsim_sw_scan,
4309 	.sw_scan_complete = mac80211_hwsim_sw_scan_complete,
4310 	.add_chanctx = ieee80211_emulate_add_chanctx,
4311 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
4312 	.change_chanctx = ieee80211_emulate_change_chanctx,
4313 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
4314 };
4315 
4316 #define HWSIM_CHANCTX_OPS					\
4317 	.hw_scan = mac80211_hwsim_hw_scan,			\
4318 	.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,	\
4319 	.remain_on_channel = mac80211_hwsim_roc,		\
4320 	.cancel_remain_on_channel = mac80211_hwsim_croc,	\
4321 	.add_chanctx = mac80211_hwsim_add_chanctx,		\
4322 	.remove_chanctx = mac80211_hwsim_remove_chanctx,	\
4323 	.change_chanctx = mac80211_hwsim_change_chanctx,	\
4324 	.assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,\
4325 	.unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx, \
4326 	.switch_vif_chanctx = mac80211_hwsim_switch_vif_chanctx,
4327 
4328 static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
4329 	HWSIM_COMMON_OPS
4330 	HWSIM_NON_MLO_OPS
4331 	HWSIM_CHANCTX_OPS
4332 };
4333 
4334 static const struct ieee80211_ops mac80211_hwsim_mlo_ops = {
4335 	HWSIM_COMMON_OPS
4336 	HWSIM_CHANCTX_OPS
4337 	.change_vif_links = mac80211_hwsim_change_vif_links,
4338 	.change_sta_links = mac80211_hwsim_change_sta_links,
4339 	.sta_state = mac80211_hwsim_sta_state,
4340 	.can_neg_ttlm = mac80211_hwsim_can_neg_ttlm,
4341 };
4342 
4343 struct hwsim_new_radio_params {
4344 	unsigned int channels;
4345 	const char *reg_alpha2;
4346 	const struct ieee80211_regdomain *regd;
4347 	bool reg_strict;
4348 	bool p2p_device;
4349 	bool use_chanctx;
4350 	bool multi_radio;
4351 	bool destroy_on_close;
4352 	const char *hwname;
4353 	bool no_vif;
4354 	const u8 *perm_addr;
4355 	u32 iftypes;
4356 	u32 *ciphers;
4357 	u8 n_ciphers;
4358 	bool mlo;
4359 	const struct cfg80211_pmsr_capabilities *pmsr_capa;
4360 	bool nan_device;
4361 	bool background_radar;
4362 };
4363 
4364 static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
4365 				   struct genl_info *info)
4366 {
4367 	if (info)
4368 		genl_notify(&hwsim_genl_family, mcast_skb, info,
4369 			    HWSIM_MCGRP_CONFIG, GFP_KERNEL);
4370 	else
4371 		genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
4372 				  HWSIM_MCGRP_CONFIG, GFP_KERNEL);
4373 }
4374 
4375 static int append_radio_msg(struct sk_buff *skb, int id,
4376 			    struct hwsim_new_radio_params *param)
4377 {
4378 	int ret;
4379 
4380 	ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
4381 	if (ret < 0)
4382 		return ret;
4383 
4384 	if (param->channels) {
4385 		ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
4386 		if (ret < 0)
4387 			return ret;
4388 	}
4389 
4390 	if (param->reg_alpha2) {
4391 		ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
4392 			      param->reg_alpha2);
4393 		if (ret < 0)
4394 			return ret;
4395 	}
4396 
4397 	if (param->regd) {
4398 		int i;
4399 
4400 		for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
4401 			if (hwsim_world_regdom_custom[i] != param->regd)
4402 				continue;
4403 
4404 			ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
4405 			if (ret < 0)
4406 				return ret;
4407 			break;
4408 		}
4409 	}
4410 
4411 	if (param->reg_strict) {
4412 		ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
4413 		if (ret < 0)
4414 			return ret;
4415 	}
4416 
4417 	if (param->p2p_device) {
4418 		ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
4419 		if (ret < 0)
4420 			return ret;
4421 	}
4422 
4423 	if (param->use_chanctx) {
4424 		ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
4425 		if (ret < 0)
4426 			return ret;
4427 	}
4428 
4429 	if (param->multi_radio) {
4430 		ret = nla_put_flag(skb, HWSIM_ATTR_MULTI_RADIO);
4431 		if (ret < 0)
4432 			return ret;
4433 	}
4434 
4435 	if (param->hwname) {
4436 		ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
4437 			      strlen(param->hwname), param->hwname);
4438 		if (ret < 0)
4439 			return ret;
4440 	}
4441 
4442 	if (param->nan_device) {
4443 		ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_NAN_DEVICE);
4444 		if (ret < 0)
4445 			return ret;
4446 	}
4447 
4448 	if (param->background_radar) {
4449 		ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_BACKGROUND_RADAR);
4450 		if (ret < 0)
4451 			return ret;
4452 	}
4453 	return 0;
4454 }
4455 
4456 static void hwsim_mcast_new_radio(int id, struct genl_info *info,
4457 				  struct hwsim_new_radio_params *param)
4458 {
4459 	struct sk_buff *mcast_skb;
4460 	void *data;
4461 
4462 	mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4463 	if (!mcast_skb)
4464 		return;
4465 
4466 	data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
4467 			   HWSIM_CMD_NEW_RADIO);
4468 	if (!data)
4469 		goto out_err;
4470 
4471 	if (append_radio_msg(mcast_skb, id, param) < 0)
4472 		goto out_err;
4473 
4474 	genlmsg_end(mcast_skb, data);
4475 
4476 	hwsim_mcast_config_msg(mcast_skb, info);
4477 	return;
4478 
4479 out_err:
4480 	nlmsg_free(mcast_skb);
4481 }
4482 
4483 static const struct ieee80211_sband_iftype_data sband_capa_2ghz[] = {
4484 	{
4485 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
4486 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
4487 		.he_cap = {
4488 			.has_he = true,
4489 			.he_cap_elem = {
4490 				.mac_cap_info[0] =
4491 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4492 				.mac_cap_info[1] =
4493 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4494 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4495 				.mac_cap_info[2] =
4496 					IEEE80211_HE_MAC_CAP2_BSR |
4497 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4498 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4499 				.mac_cap_info[3] =
4500 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4501 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4502 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4503 				.phy_cap_info[0] =
4504 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4505 				.phy_cap_info[1] =
4506 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4507 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4508 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4509 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4510 				.phy_cap_info[2] =
4511 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4512 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4513 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4514 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4515 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4516 
4517 				/* Leave all the other PHY capability bytes
4518 				 * unset, as DCM, beam forming, RU and PPE
4519 				 * threshold information are not supported
4520 				 */
4521 			},
4522 			.he_mcs_nss_supp = {
4523 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4524 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4525 				.rx_mcs_160 = cpu_to_le16(0xffff),
4526 				.tx_mcs_160 = cpu_to_le16(0xffff),
4527 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4528 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4529 			},
4530 		},
4531 		.eht_cap = {
4532 			.has_eht = true,
4533 			.eht_cap_elem = {
4534 				.mac_cap_info[0] =
4535 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4536 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4537 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4538 				.phy_cap_info[0] =
4539 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4540 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4541 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4542 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4543 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
4544 				.phy_cap_info[3] =
4545 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4546 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4547 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4548 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4549 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4550 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4551 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4552 				.phy_cap_info[4] =
4553 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4554 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4555 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4556 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4557 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4558 				.phy_cap_info[5] =
4559 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4560 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4561 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4562 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4563 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4564 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4565 				.phy_cap_info[6] =
4566 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4567 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4568 				.phy_cap_info[7] =
4569 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
4570 			},
4571 
4572 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4573 			 * Rx
4574 			 */
4575 			.eht_mcs_nss_supp = {
4576 				/*
4577 				 * Since B0, B1, B2 and B3 are not set in
4578 				 * the supported channel width set field in the
4579 				 * HE PHY capabilities information field the
4580 				 * device is a 20MHz only device on 2.4GHz band.
4581 				 */
4582 				.only_20mhz = {
4583 					.rx_tx_mcs7_max_nss = 0x88,
4584 					.rx_tx_mcs9_max_nss = 0x88,
4585 					.rx_tx_mcs11_max_nss = 0x88,
4586 					.rx_tx_mcs13_max_nss = 0x88,
4587 				},
4588 			},
4589 			/* PPE threshold information is not supported */
4590 		},
4591 		.uhr_cap = {
4592 			.has_uhr = true,
4593 			.mac.mac_cap = {
4594 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
4595 			},
4596 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_RX |
4597 					       IEEE80211_UHR_PHY_CAP_ELR_TX),
4598 		},
4599 	},
4600 	{
4601 		.types_mask = BIT(NL80211_IFTYPE_AP) |
4602 			      BIT(NL80211_IFTYPE_P2P_GO),
4603 		.he_cap = {
4604 			.has_he = true,
4605 			.he_cap_elem = {
4606 				.mac_cap_info[0] =
4607 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4608 				.mac_cap_info[1] =
4609 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4610 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4611 				.mac_cap_info[2] =
4612 					IEEE80211_HE_MAC_CAP2_BSR |
4613 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4614 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4615 				.mac_cap_info[3] =
4616 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4617 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4618 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4619 				.phy_cap_info[0] =
4620 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4621 				.phy_cap_info[1] =
4622 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4623 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4624 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4625 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4626 				.phy_cap_info[2] =
4627 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4628 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4629 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4630 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4631 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4632 
4633 				/* Leave all the other PHY capability bytes
4634 				 * unset, as DCM, beam forming, RU and PPE
4635 				 * threshold information are not supported
4636 				 */
4637 			},
4638 			.he_mcs_nss_supp = {
4639 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4640 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4641 				.rx_mcs_160 = cpu_to_le16(0xffff),
4642 				.tx_mcs_160 = cpu_to_le16(0xffff),
4643 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4644 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4645 			},
4646 		},
4647 		.eht_cap = {
4648 			.has_eht = true,
4649 			.eht_cap_elem = {
4650 				.mac_cap_info[0] =
4651 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4652 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4653 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4654 				.phy_cap_info[0] =
4655 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4656 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4657 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4658 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4659 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
4660 				.phy_cap_info[3] =
4661 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4662 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4663 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4664 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4665 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4666 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4667 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4668 				.phy_cap_info[4] =
4669 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4670 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4671 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4672 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4673 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4674 				.phy_cap_info[5] =
4675 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4676 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4677 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4678 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4679 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4680 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4681 				.phy_cap_info[6] =
4682 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4683 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4684 				.phy_cap_info[7] =
4685 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
4686 			},
4687 
4688 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4689 			 * Rx
4690 			 */
4691 			.eht_mcs_nss_supp = {
4692 				/*
4693 				 * Since B0, B1, B2 and B3 are not set in
4694 				 * the supported channel width set field in the
4695 				 * HE PHY capabilities information field the
4696 				 * device is a 20MHz only device on 2.4GHz band.
4697 				 */
4698 				.only_20mhz = {
4699 					.rx_tx_mcs7_max_nss = 0x88,
4700 					.rx_tx_mcs9_max_nss = 0x88,
4701 					.rx_tx_mcs11_max_nss = 0x88,
4702 					.rx_tx_mcs13_max_nss = 0x88,
4703 				},
4704 			},
4705 			/* PPE threshold information is not supported */
4706 		},
4707 		.uhr_cap = {
4708 			.has_uhr = true,
4709 			.mac.mac_cap = {
4710 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
4711 				[1] = IEEE80211_UHR_MAC_CAP1_DBE_SUPP,
4712 			},
4713 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_RX |
4714 					       IEEE80211_UHR_PHY_CAP_ELR_TX),
4715 		},
4716 	},
4717 #ifdef CONFIG_MAC80211_MESH
4718 	{
4719 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
4720 		.he_cap = {
4721 			.has_he = true,
4722 			.he_cap_elem = {
4723 				.mac_cap_info[0] =
4724 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4725 				.mac_cap_info[1] =
4726 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4727 				.mac_cap_info[2] =
4728 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4729 				.mac_cap_info[3] =
4730 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4731 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4732 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4733 				.phy_cap_info[0] =
4734 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G,
4735 				.phy_cap_info[1] =
4736 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4737 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4738 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4739 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4740 				.phy_cap_info[2] = 0,
4741 
4742 				/* Leave all the other PHY capability bytes
4743 				 * unset, as DCM, beam forming, RU and PPE
4744 				 * threshold information are not supported
4745 				 */
4746 			},
4747 			.he_mcs_nss_supp = {
4748 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4749 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4750 				.rx_mcs_160 = cpu_to_le16(0xffff),
4751 				.tx_mcs_160 = cpu_to_le16(0xffff),
4752 				.rx_mcs_80p80 = cpu_to_le16(0xffff),
4753 				.tx_mcs_80p80 = cpu_to_le16(0xffff),
4754 			},
4755 		},
4756 	},
4757 #endif
4758 };
4759 
4760 static const struct ieee80211_sband_iftype_data sband_capa_5ghz[] = {
4761 	{
4762 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
4763 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
4764 		.he_cap = {
4765 			.has_he = true,
4766 			.he_cap_elem = {
4767 				.mac_cap_info[0] =
4768 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4769 				.mac_cap_info[1] =
4770 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4771 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4772 				.mac_cap_info[2] =
4773 					IEEE80211_HE_MAC_CAP2_BSR |
4774 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4775 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4776 				.mac_cap_info[3] =
4777 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4778 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4779 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4780 				.phy_cap_info[0] =
4781 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4782 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4783 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4784 				.phy_cap_info[1] =
4785 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4786 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4787 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4788 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4789 				.phy_cap_info[2] =
4790 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4791 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4792 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4793 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4794 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4795 
4796 				/* Leave all the other PHY capability bytes
4797 				 * unset, as DCM, beam forming, RU and PPE
4798 				 * threshold information are not supported
4799 				 */
4800 			},
4801 			.he_mcs_nss_supp = {
4802 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4803 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4804 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4805 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4806 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4807 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4808 			},
4809 		},
4810 		.eht_cap = {
4811 			.has_eht = true,
4812 			.eht_cap_elem = {
4813 				.mac_cap_info[0] =
4814 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4815 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4816 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4817 				.phy_cap_info[0] =
4818 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4819 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4820 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4821 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4822 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
4823 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
4824 				.phy_cap_info[1] =
4825 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
4826 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
4827 				.phy_cap_info[2] =
4828 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
4829 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
4830 				.phy_cap_info[3] =
4831 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4832 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4833 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4834 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4835 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4836 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4837 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4838 				.phy_cap_info[4] =
4839 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4840 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4841 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4842 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4843 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4844 				.phy_cap_info[5] =
4845 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4846 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4847 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4848 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4849 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4850 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4851 				.phy_cap_info[6] =
4852 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4853 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4854 				.phy_cap_info[7] =
4855 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
4856 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4857 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4858 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4859 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
4860 			},
4861 
4862 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4863 			 * Rx
4864 			 */
4865 			.eht_mcs_nss_supp = {
4866 				/*
4867 				 * As B1 and B2 are set in the supported
4868 				 * channel width set field in the HE PHY
4869 				 * capabilities information field include all
4870 				 * the following MCS/NSS.
4871 				 */
4872 				.bw._80 = {
4873 					.rx_tx_mcs9_max_nss = 0x88,
4874 					.rx_tx_mcs11_max_nss = 0x88,
4875 					.rx_tx_mcs13_max_nss = 0x88,
4876 				},
4877 				.bw._160 = {
4878 					.rx_tx_mcs9_max_nss = 0x88,
4879 					.rx_tx_mcs11_max_nss = 0x88,
4880 					.rx_tx_mcs13_max_nss = 0x88,
4881 				},
4882 			},
4883 			/* PPE threshold information is not supported */
4884 		},
4885 		.uhr_cap = {
4886 			.has_uhr = true,
4887 			.mac.mac_cap = {
4888 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
4889 				[1] = IEEE80211_UHR_MAC_CAP1_DBE_SUPP,
4890 			},
4891 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_TX),
4892 		},
4893 	},
4894 	{
4895 		.types_mask = BIT(NL80211_IFTYPE_AP) |
4896 			      BIT(NL80211_IFTYPE_P2P_GO),
4897 		.he_cap = {
4898 			.has_he = true,
4899 			.he_cap_elem = {
4900 				.mac_cap_info[0] =
4901 					IEEE80211_HE_MAC_CAP0_HTC_HE,
4902 				.mac_cap_info[1] =
4903 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
4904 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
4905 				.mac_cap_info[2] =
4906 					IEEE80211_HE_MAC_CAP2_BSR |
4907 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
4908 					IEEE80211_HE_MAC_CAP2_ACK_EN,
4909 				.mac_cap_info[3] =
4910 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
4911 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
4912 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
4913 				.phy_cap_info[0] =
4914 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4915 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4916 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
4917 				.phy_cap_info[1] =
4918 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
4919 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
4920 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
4921 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
4922 				.phy_cap_info[2] =
4923 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
4924 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
4925 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
4926 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4927 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
4928 
4929 				/* Leave all the other PHY capability bytes
4930 				 * unset, as DCM, beam forming, RU and PPE
4931 				 * threshold information are not supported
4932 				 */
4933 			},
4934 			.he_mcs_nss_supp = {
4935 				.rx_mcs_80 = cpu_to_le16(0xfffa),
4936 				.tx_mcs_80 = cpu_to_le16(0xfffa),
4937 				.rx_mcs_160 = cpu_to_le16(0xfffa),
4938 				.tx_mcs_160 = cpu_to_le16(0xfffa),
4939 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
4940 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
4941 			},
4942 		},
4943 		.eht_cap = {
4944 			.has_eht = true,
4945 			.eht_cap_elem = {
4946 				.mac_cap_info[0] =
4947 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
4948 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
4949 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
4950 				.phy_cap_info[0] =
4951 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
4952 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
4953 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
4954 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
4955 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
4956 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
4957 				.phy_cap_info[1] =
4958 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
4959 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
4960 				.phy_cap_info[2] =
4961 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
4962 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
4963 				.phy_cap_info[3] =
4964 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
4965 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4966 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
4967 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4968 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
4969 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4970 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
4971 				.phy_cap_info[4] =
4972 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4973 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4974 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4975 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
4976 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
4977 				.phy_cap_info[5] =
4978 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4979 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4980 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4981 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
4982 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
4983 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
4984 				.phy_cap_info[6] =
4985 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
4986 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
4987 				.phy_cap_info[7] =
4988 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
4989 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4990 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4991 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4992 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
4993 			},
4994 
4995 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
4996 			 * Rx
4997 			 */
4998 			.eht_mcs_nss_supp = {
4999 				/*
5000 				 * As B1 and B2 are set in the supported
5001 				 * channel width set field in the HE PHY
5002 				 * capabilities information field include all
5003 				 * the following MCS/NSS.
5004 				 */
5005 				.bw._80 = {
5006 					.rx_tx_mcs9_max_nss = 0x88,
5007 					.rx_tx_mcs11_max_nss = 0x88,
5008 					.rx_tx_mcs13_max_nss = 0x88,
5009 				},
5010 				.bw._160 = {
5011 					.rx_tx_mcs9_max_nss = 0x88,
5012 					.rx_tx_mcs11_max_nss = 0x88,
5013 					.rx_tx_mcs13_max_nss = 0x88,
5014 				},
5015 			},
5016 			/* PPE threshold information is not supported */
5017 		},
5018 		.uhr_cap = {
5019 			.has_uhr = true,
5020 			.mac.mac_cap = {
5021 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
5022 				[1] = IEEE80211_UHR_MAC_CAP1_DBE_SUPP,
5023 			},
5024 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_RX),
5025 		},
5026 	},
5027 #ifdef CONFIG_MAC80211_MESH
5028 	{
5029 		/* TODO: should we support other types, e.g., IBSS?*/
5030 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
5031 		.he_cap = {
5032 			.has_he = true,
5033 			.he_cap_elem = {
5034 				.mac_cap_info[0] =
5035 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5036 				.mac_cap_info[1] =
5037 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5038 				.mac_cap_info[2] =
5039 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5040 				.mac_cap_info[3] =
5041 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5042 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5043 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5044 				.phy_cap_info[0] =
5045 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5046 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5047 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5048 				.phy_cap_info[1] =
5049 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5050 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5051 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5052 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5053 				.phy_cap_info[2] = 0,
5054 
5055 				/* Leave all the other PHY capability bytes
5056 				 * unset, as DCM, beam forming, RU and PPE
5057 				 * threshold information are not supported
5058 				 */
5059 			},
5060 			.he_mcs_nss_supp = {
5061 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5062 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5063 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5064 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5065 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5066 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5067 			},
5068 		},
5069 	},
5070 #endif
5071 };
5072 
5073 static const struct ieee80211_sband_iftype_data sband_capa_6ghz[] = {
5074 	{
5075 		.types_mask = BIT(NL80211_IFTYPE_STATION) |
5076 			      BIT(NL80211_IFTYPE_P2P_CLIENT),
5077 		.he_6ghz_capa = {
5078 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
5079 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
5080 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
5081 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
5082 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
5083 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
5084 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
5085 		},
5086 		.he_cap = {
5087 			.has_he = true,
5088 			.he_cap_elem = {
5089 				.mac_cap_info[0] =
5090 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5091 				.mac_cap_info[1] =
5092 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
5093 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5094 				.mac_cap_info[2] =
5095 					IEEE80211_HE_MAC_CAP2_BSR |
5096 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
5097 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5098 				.mac_cap_info[3] =
5099 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5100 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5101 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5102 				.phy_cap_info[0] =
5103 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5104 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5105 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5106 				.phy_cap_info[1] =
5107 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5108 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5109 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5110 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5111 				.phy_cap_info[2] =
5112 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
5113 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
5114 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
5115 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5116 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
5117 
5118 				/* Leave all the other PHY capability bytes
5119 				 * unset, as DCM, beam forming, RU and PPE
5120 				 * threshold information are not supported
5121 				 */
5122 			},
5123 			.he_mcs_nss_supp = {
5124 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5125 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5126 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5127 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5128 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5129 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5130 			},
5131 		},
5132 		.eht_cap = {
5133 			.has_eht = true,
5134 			.eht_cap_elem = {
5135 				.mac_cap_info[0] =
5136 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
5137 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
5138 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
5139 				.phy_cap_info[0] =
5140 					IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
5141 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
5142 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
5143 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
5144 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
5145 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
5146 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
5147 				.phy_cap_info[1] =
5148 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
5149 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
5150 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
5151 				.phy_cap_info[2] =
5152 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
5153 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
5154 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
5155 				.phy_cap_info[3] =
5156 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
5157 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
5158 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
5159 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
5160 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
5161 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
5162 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
5163 				.phy_cap_info[4] =
5164 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
5165 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
5166 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
5167 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
5168 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
5169 				.phy_cap_info[5] =
5170 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
5171 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
5172 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
5173 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
5174 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
5175 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
5176 				.phy_cap_info[6] =
5177 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
5178 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
5179 					IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
5180 				.phy_cap_info[7] =
5181 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
5182 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
5183 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
5184 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
5185 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
5186 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
5187 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
5188 			},
5189 
5190 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5191 			 * Rx
5192 			 */
5193 			.eht_mcs_nss_supp = {
5194 				/*
5195 				 * As B1 and B2 are set in the supported
5196 				 * channel width set field in the HE PHY
5197 				 * capabilities information field and 320MHz in
5198 				 * 6GHz is supported include all the following
5199 				 * MCS/NSS.
5200 				 */
5201 				.bw._80 = {
5202 					.rx_tx_mcs9_max_nss = 0x88,
5203 					.rx_tx_mcs11_max_nss = 0x88,
5204 					.rx_tx_mcs13_max_nss = 0x88,
5205 				},
5206 				.bw._160 = {
5207 					.rx_tx_mcs9_max_nss = 0x88,
5208 					.rx_tx_mcs11_max_nss = 0x88,
5209 					.rx_tx_mcs13_max_nss = 0x88,
5210 				},
5211 				.bw._320 = {
5212 					.rx_tx_mcs9_max_nss = 0x88,
5213 					.rx_tx_mcs11_max_nss = 0x88,
5214 					.rx_tx_mcs13_max_nss = 0x88,
5215 				},
5216 			},
5217 			/* PPE threshold information is not supported */
5218 		},
5219 		.uhr_cap = {
5220 			.has_uhr = true,
5221 			.mac.mac_cap = {
5222 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
5223 				[1] = IEEE80211_UHR_MAC_CAP1_DBE_SUPP,
5224 			},
5225 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_TX),
5226 		},
5227 	},
5228 	{
5229 		.types_mask = BIT(NL80211_IFTYPE_AP) |
5230 			      BIT(NL80211_IFTYPE_P2P_GO),
5231 		.he_6ghz_capa = {
5232 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
5233 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
5234 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
5235 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
5236 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
5237 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
5238 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
5239 		},
5240 		.he_cap = {
5241 			.has_he = true,
5242 			.he_cap_elem = {
5243 				.mac_cap_info[0] =
5244 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5245 				.mac_cap_info[1] =
5246 					IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
5247 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5248 				.mac_cap_info[2] =
5249 					IEEE80211_HE_MAC_CAP2_BSR |
5250 					IEEE80211_HE_MAC_CAP2_MU_CASCADING |
5251 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5252 				.mac_cap_info[3] =
5253 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5254 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5255 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5256 				.phy_cap_info[0] =
5257 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5258 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5259 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5260 				.phy_cap_info[1] =
5261 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5262 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5263 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5264 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5265 				.phy_cap_info[2] =
5266 					IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
5267 					IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
5268 					IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
5269 					IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5270 					IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
5271 
5272 				/* Leave all the other PHY capability bytes
5273 				 * unset, as DCM, beam forming, RU and PPE
5274 				 * threshold information are not supported
5275 				 */
5276 			},
5277 			.he_mcs_nss_supp = {
5278 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5279 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5280 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5281 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5282 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5283 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5284 			},
5285 		},
5286 		.eht_cap = {
5287 			.has_eht = true,
5288 			.eht_cap_elem = {
5289 				.mac_cap_info[0] =
5290 					IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
5291 					IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
5292 					IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
5293 				.phy_cap_info[0] =
5294 					IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
5295 					IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
5296 					IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
5297 					IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
5298 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
5299 					IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
5300 					IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
5301 				.phy_cap_info[1] =
5302 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
5303 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
5304 					IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
5305 				.phy_cap_info[2] =
5306 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
5307 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
5308 					IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
5309 				.phy_cap_info[3] =
5310 					IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
5311 					IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
5312 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
5313 					IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
5314 					IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
5315 					IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
5316 					IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
5317 				.phy_cap_info[4] =
5318 					IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
5319 					IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
5320 					IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
5321 					IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
5322 					IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
5323 				.phy_cap_info[5] =
5324 					IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
5325 					IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
5326 					IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
5327 					IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
5328 					IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
5329 					IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
5330 				.phy_cap_info[6] =
5331 					IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
5332 					IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
5333 					IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
5334 				.phy_cap_info[7] =
5335 					IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
5336 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
5337 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
5338 					IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
5339 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
5340 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
5341 					IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
5342 			},
5343 
5344 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5345 			 * Rx
5346 			 */
5347 			.eht_mcs_nss_supp = {
5348 				/*
5349 				 * As B1 and B2 are set in the supported
5350 				 * channel width set field in the HE PHY
5351 				 * capabilities information field and 320MHz in
5352 				 * 6GHz is supported include all the following
5353 				 * MCS/NSS.
5354 				 */
5355 				.bw._80 = {
5356 					.rx_tx_mcs9_max_nss = 0x88,
5357 					.rx_tx_mcs11_max_nss = 0x88,
5358 					.rx_tx_mcs13_max_nss = 0x88,
5359 				},
5360 				.bw._160 = {
5361 					.rx_tx_mcs9_max_nss = 0x88,
5362 					.rx_tx_mcs11_max_nss = 0x88,
5363 					.rx_tx_mcs13_max_nss = 0x88,
5364 				},
5365 				.bw._320 = {
5366 					.rx_tx_mcs9_max_nss = 0x88,
5367 					.rx_tx_mcs11_max_nss = 0x88,
5368 					.rx_tx_mcs13_max_nss = 0x88,
5369 				},
5370 			},
5371 			/* PPE threshold information is not supported */
5372 		},
5373 		.uhr_cap = {
5374 			.has_uhr = true,
5375 			.mac.mac_cap = {
5376 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
5377 				[1] = IEEE80211_UHR_MAC_CAP1_DBE_SUPP,
5378 			},
5379 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_RX),
5380 		},
5381 	},
5382 #ifdef CONFIG_MAC80211_MESH
5383 	{
5384 		/* TODO: should we support other types, e.g., IBSS?*/
5385 		.types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
5386 		.he_6ghz_capa = {
5387 			.capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
5388 					    IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
5389 					    IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
5390 					    IEEE80211_HE_6GHZ_CAP_SM_PS |
5391 					    IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
5392 					    IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
5393 					    IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
5394 		},
5395 		.he_cap = {
5396 			.has_he = true,
5397 			.he_cap_elem = {
5398 				.mac_cap_info[0] =
5399 					IEEE80211_HE_MAC_CAP0_HTC_HE,
5400 				.mac_cap_info[1] =
5401 					IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5402 				.mac_cap_info[2] =
5403 					IEEE80211_HE_MAC_CAP2_ACK_EN,
5404 				.mac_cap_info[3] =
5405 					IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5406 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5407 				.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5408 				.phy_cap_info[0] =
5409 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5410 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5411 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5412 				.phy_cap_info[1] =
5413 					IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5414 					IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5415 					IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5416 					IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5417 				.phy_cap_info[2] = 0,
5418 
5419 				/* Leave all the other PHY capability bytes
5420 				 * unset, as DCM, beam forming, RU and PPE
5421 				 * threshold information are not supported
5422 				 */
5423 			},
5424 			.he_mcs_nss_supp = {
5425 				.rx_mcs_80 = cpu_to_le16(0xfffa),
5426 				.tx_mcs_80 = cpu_to_le16(0xfffa),
5427 				.rx_mcs_160 = cpu_to_le16(0xfffa),
5428 				.tx_mcs_160 = cpu_to_le16(0xfffa),
5429 				.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5430 				.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5431 			},
5432 		},
5433 		.eht_cap = {
5434 			.has_eht = true,
5435 			.eht_cap_elem = {
5436 				.mac_cap_info[0] = IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
5437 						   IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
5438 				.phy_cap_info[0] = IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ,
5439 				/* Leave all the other PHY capability bytes
5440 				 * unset, as DCM, beam forming, RU and PPE
5441 				 * threshold information are not supported
5442 				 */
5443 			},
5444 			/* For all MCS and bandwidth, set 8 NSS for both Tx and
5445 			 * Rx
5446 			 */
5447 			.eht_mcs_nss_supp = {
5448 				/* As B1 and B2 are set in the supported
5449 				 * channel width set field in the HE PHY
5450 				 * capabilities information field and 320MHz in
5451 				 * 6GHz is supported include all the following
5452 				 * MCS/NSS.
5453 				 */
5454 				.bw._80 = {
5455 					.rx_tx_mcs9_max_nss = 0x88,
5456 					.rx_tx_mcs11_max_nss = 0x88,
5457 					.rx_tx_mcs13_max_nss = 0x88,
5458 				},
5459 				.bw._160 = {
5460 					.rx_tx_mcs9_max_nss = 0x88,
5461 					.rx_tx_mcs11_max_nss = 0x88,
5462 					.rx_tx_mcs13_max_nss = 0x88,
5463 				},
5464 				.bw._320 = {
5465 					.rx_tx_mcs9_max_nss = 0x88,
5466 					.rx_tx_mcs11_max_nss = 0x88,
5467 					.rx_tx_mcs13_max_nss = 0x88,
5468 				},
5469 			},
5470 			/* PPE threshold information is not supported */
5471 		},
5472 		.uhr_cap = {
5473 			.has_uhr = true,
5474 			.mac.mac_cap = {
5475 				[0] = IEEE80211_UHR_MAC_CAP0_NPCA_SUPP,
5476 			},
5477 			.phy.cap = cpu_to_le32(IEEE80211_UHR_PHY_CAP_ELR_RX |
5478 					       IEEE80211_UHR_PHY_CAP_ELR_TX),
5479 		},
5480 	},
5481 #endif
5482 };
5483 
5484 #define HWSIM_VHT_MCS_MAP				\
5485 	(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |		\
5486 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |		\
5487 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |		\
5488 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |		\
5489 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |		\
5490 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |		\
5491 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |		\
5492 	 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14)
5493 
5494 static const struct ieee80211_sta_ht_cap hwsim_nan_ht_cap = {
5495 	.ht_supported = true,
5496 	.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
5497 	       IEEE80211_HT_CAP_GRN_FLD |
5498 	       IEEE80211_HT_CAP_SGI_20 |
5499 	       IEEE80211_HT_CAP_SGI_40 |
5500 	       IEEE80211_HT_CAP_DSSSCCK40,
5501 	.ampdu_factor = 0x3,
5502 	.ampdu_density = 0x6,
5503 	.mcs = {
5504 		.rx_mask = { 0xff, 0xff },
5505 		.tx_params = IEEE80211_HT_MCS_TX_DEFINED,
5506 	},
5507 };
5508 
5509 static const struct ieee80211_sta_vht_cap hwsim_nan_vht_cap = {
5510 	.vht_supported = true,
5511 	.cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
5512 	       IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
5513 	       IEEE80211_VHT_CAP_RXLDPC |
5514 	       IEEE80211_VHT_CAP_SHORT_GI_80 |
5515 	       IEEE80211_VHT_CAP_SHORT_GI_160 |
5516 	       IEEE80211_VHT_CAP_TXSTBC |
5517 	       IEEE80211_VHT_CAP_RXSTBC_4 |
5518 	       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5519 	.vht_mcs = {
5520 		.rx_mcs_map = cpu_to_le16(HWSIM_VHT_MCS_MAP),
5521 		.tx_mcs_map = cpu_to_le16(HWSIM_VHT_MCS_MAP),
5522 	},
5523 };
5524 
5525 static const struct ieee80211_sta_he_cap hwsim_nan_he_cap = {
5526 	.has_he = true,
5527 	.he_cap_elem = {
5528 		.mac_cap_info[0] =
5529 			IEEE80211_HE_MAC_CAP0_HTC_HE,
5530 		.mac_cap_info[1] =
5531 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
5532 			IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
5533 		.mac_cap_info[2] =
5534 			IEEE80211_HE_MAC_CAP2_BSR |
5535 			IEEE80211_HE_MAC_CAP2_MU_CASCADING |
5536 			IEEE80211_HE_MAC_CAP2_ACK_EN,
5537 		.mac_cap_info[3] =
5538 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
5539 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
5540 		.mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
5541 		.phy_cap_info[0] =
5542 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
5543 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
5544 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
5545 		.phy_cap_info[1] =
5546 			IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
5547 			IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
5548 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
5549 			IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
5550 		.phy_cap_info[2] =
5551 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
5552 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
5553 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
5554 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5555 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
5556 
5557 		/*
5558 		 * Leave all the other PHY capability bytes
5559 		 * unset, as DCM, beam forming, RU and PPE
5560 		 * threshold information are not supported
5561 		 */
5562 	},
5563 	.he_mcs_nss_supp = {
5564 		.rx_mcs_80 = cpu_to_le16(0xfffa),
5565 		.tx_mcs_80 = cpu_to_le16(0xfffa),
5566 		.rx_mcs_160 = cpu_to_le16(0xfffa),
5567 		.tx_mcs_160 = cpu_to_le16(0xfffa),
5568 		.rx_mcs_80p80 = cpu_to_le16(0xfffa),
5569 		.tx_mcs_80p80 = cpu_to_le16(0xfffa),
5570 	},
5571 };
5572 
5573 static void mac80211_hwsim_sband_capab(struct ieee80211_supported_band *sband)
5574 {
5575 	switch (sband->band) {
5576 	case NL80211_BAND_2GHZ:
5577 		ieee80211_set_sband_iftype_data(sband, sband_capa_2ghz);
5578 		break;
5579 	case NL80211_BAND_5GHZ:
5580 		ieee80211_set_sband_iftype_data(sband, sband_capa_5ghz);
5581 		break;
5582 	case NL80211_BAND_6GHZ:
5583 		ieee80211_set_sband_iftype_data(sband, sband_capa_6ghz);
5584 		break;
5585 	default:
5586 		break;
5587 	}
5588 }
5589 
5590 #ifdef CONFIG_MAC80211_MESH
5591 #define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
5592 #else
5593 #define HWSIM_MESH_BIT 0
5594 #endif
5595 
5596 #define HWSIM_DEFAULT_IF_LIMIT \
5597 	(BIT(NL80211_IFTYPE_STATION) | \
5598 	 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
5599 	 BIT(NL80211_IFTYPE_AP) | \
5600 	 BIT(NL80211_IFTYPE_P2P_GO) | \
5601 	 HWSIM_MESH_BIT)
5602 
5603 #define HWSIM_IFTYPE_SUPPORT_MASK \
5604 	(BIT(NL80211_IFTYPE_STATION) | \
5605 	 BIT(NL80211_IFTYPE_AP) | \
5606 	 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
5607 	 BIT(NL80211_IFTYPE_P2P_GO) | \
5608 	 BIT(NL80211_IFTYPE_ADHOC) | \
5609 	 BIT(NL80211_IFTYPE_MESH_POINT) | \
5610 	 BIT(NL80211_IFTYPE_OCB))
5611 
5612 static const u8 iftypes_ext_capa_ap[] = {
5613 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
5614 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
5615 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
5616 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
5617 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
5618 	 [9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
5619 };
5620 
5621 #define MAC80211_HWSIM_MLD_CAPA_OPS				\
5622 	FIELD_PREP_CONST(IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
5623 			 IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME) | \
5624 	FIELD_PREP_CONST(IEEE80211_MLD_CAP_OP_MAX_SIMUL_LINKS, \
5625 			 IEEE80211_MLD_MAX_NUM_LINKS - 1)
5626 
5627 static const struct wiphy_iftype_ext_capab mac80211_hwsim_iftypes_ext_capa[] = {
5628 	{
5629 		.iftype = NL80211_IFTYPE_AP,
5630 		.extended_capabilities = iftypes_ext_capa_ap,
5631 		.extended_capabilities_mask = iftypes_ext_capa_ap,
5632 		.extended_capabilities_len = sizeof(iftypes_ext_capa_ap),
5633 		.eml_capabilities = IEEE80211_EML_CAP_EMLSR_SUPP |
5634 				    IEEE80211_EML_CAP_EMLMR_SUPPORT,
5635 		.mld_capa_and_ops = MAC80211_HWSIM_MLD_CAPA_OPS,
5636 	},
5637 };
5638 
5639 static int mac80211_hwsim_new_radio(struct genl_info *info,
5640 				    struct hwsim_new_radio_params *param)
5641 {
5642 	int err;
5643 	u8 addr[ETH_ALEN];
5644 	struct mac80211_hwsim_data *data;
5645 	struct ieee80211_hw *hw;
5646 	enum nl80211_band band;
5647 	const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
5648 	struct net *net;
5649 	int idx, i;
5650 	int n_limits = 0;
5651 	int n_bands = 0;
5652 
5653 	if (WARN_ON(param->channels > 1 && !param->use_chanctx))
5654 		return -EINVAL;
5655 
5656 	spin_lock_bh(&hwsim_radio_lock);
5657 	idx = hwsim_radio_idx++;
5658 	spin_unlock_bh(&hwsim_radio_lock);
5659 
5660 	if (param->mlo)
5661 		ops = &mac80211_hwsim_mlo_ops;
5662 	else if (param->use_chanctx)
5663 		ops = &mac80211_hwsim_mchan_ops;
5664 	hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
5665 	if (!hw) {
5666 		pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
5667 		err = -ENOMEM;
5668 		goto failed;
5669 	}
5670 
5671 	/* ieee80211_alloc_hw_nm may have used a default name */
5672 	param->hwname = wiphy_name(hw->wiphy);
5673 
5674 	if (info)
5675 		net = genl_info_net(info);
5676 	else
5677 		net = &init_net;
5678 	wiphy_net_set(hw->wiphy, net);
5679 
5680 	data = hw->priv;
5681 	data->hw = hw;
5682 
5683 	data->dev = device_create(&hwsim_class, NULL, 0, hw, "hwsim%d", idx);
5684 	if (IS_ERR(data->dev)) {
5685 		printk(KERN_DEBUG
5686 		       "mac80211_hwsim: device_create failed (%ld)\n",
5687 		       PTR_ERR(data->dev));
5688 		err = -ENOMEM;
5689 		goto failed_drvdata;
5690 	}
5691 	data->dev->driver = &mac80211_hwsim_driver.driver;
5692 	err = device_bind_driver(data->dev);
5693 	if (err != 0) {
5694 		pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
5695 		       err);
5696 		goto failed_bind;
5697 	}
5698 
5699 	skb_queue_head_init(&data->pending);
5700 
5701 	SET_IEEE80211_DEV(hw, data->dev);
5702 	if (!param->perm_addr) {
5703 		eth_zero_addr(addr);
5704 		addr[0] = 0x02;
5705 		addr[3] = idx >> 8;
5706 		addr[4] = idx;
5707 		memcpy(data->addresses[0].addr, addr, ETH_ALEN);
5708 		/* Why need here second address ? */
5709 		memcpy(data->addresses[1].addr, addr, ETH_ALEN);
5710 		data->addresses[1].addr[0] |= 0x40;
5711 		memcpy(data->addresses[2].addr, addr, ETH_ALEN);
5712 		data->addresses[2].addr[0] |= 0x50;
5713 
5714 		hw->wiphy->n_addresses = 3;
5715 		hw->wiphy->addresses = data->addresses;
5716 		/* possible address clash is checked at hash table insertion */
5717 	} else {
5718 		memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
5719 		/* compatibility with automatically generated mac addr */
5720 		memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
5721 		memcpy(data->addresses[2].addr, param->perm_addr, ETH_ALEN);
5722 		hw->wiphy->n_addresses = 3;
5723 		hw->wiphy->addresses = data->addresses;
5724 	}
5725 
5726 	data->channels = param->channels;
5727 	data->use_chanctx = param->use_chanctx;
5728 	data->idx = idx;
5729 	data->destroy_on_close = param->destroy_on_close;
5730 	if (info)
5731 		data->portid = info->snd_portid;
5732 
5733 	/* setup interface limits, only on interface types we support */
5734 	if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
5735 		data->if_limits[n_limits].max = 1;
5736 		data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
5737 		n_limits++;
5738 	}
5739 
5740 	if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
5741 		data->if_limits[n_limits].max = 2048;
5742 		/*
5743 		 * For this case, we may only support a subset of
5744 		 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
5745 		 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
5746 		 */
5747 		data->if_limits[n_limits].types =
5748 					HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
5749 		n_limits++;
5750 	}
5751 
5752 	if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
5753 		data->if_limits[n_limits].max = 1;
5754 		data->if_limits[n_limits].types =
5755 						BIT(NL80211_IFTYPE_P2P_DEVICE);
5756 		n_limits++;
5757 	}
5758 
5759 	if (param->iftypes & BIT(NL80211_IFTYPE_NAN)) {
5760 		data->if_limits[n_limits].max = 1;
5761 		data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_NAN);
5762 		n_limits++;
5763 
5764 		hw->wiphy->nan_supported_bands = BIT(NL80211_BAND_2GHZ) |
5765 						 BIT(NL80211_BAND_5GHZ);
5766 
5767 		hw->wiphy->nan_capa.flags = WIPHY_NAN_FLAGS_CONFIGURABLE_SYNC |
5768 					    WIPHY_NAN_FLAGS_USERSPACE_DE;
5769 		hw->wiphy->nan_capa.op_mode = NAN_OP_MODE_PHY_MODE_MASK |
5770 					      NAN_OP_MODE_80P80MHZ |
5771 					      NAN_OP_MODE_160MHZ;
5772 
5773 		hw->wiphy->nan_capa.n_antennas = 0x22;
5774 		hw->wiphy->nan_capa.max_channel_switch_time = 0;
5775 
5776 		wiphy_ext_feature_set(hw->wiphy,
5777 				      NL80211_EXT_FEATURE_SECURE_NAN);
5778 
5779 		hrtimer_setup(&data->nan.slot_timer,
5780 			      mac80211_hwsim_nan_slot_timer,
5781 			      CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
5782 		hrtimer_setup(&data->nan.resume_txqs_timer,
5783 			      mac80211_hwsim_nan_resume_txqs_timer,
5784 			      CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
5785 		hrtimer_setup(&data->nan.discovery_beacon_timer,
5786 			      mac80211_hwsim_nan_discovery_beacon_timer,
5787 			      CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
5788 
5789 		spin_lock_init(&data->nan.state_lock);
5790 	}
5791 
5792 	if (param->iftypes & BIT(NL80211_IFTYPE_NAN_DATA)) {
5793 		data->if_limits[n_limits].max = 2;
5794 		data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_NAN_DATA);
5795 		n_limits++;
5796 
5797 		hw->wiphy->nan_capa.phy.ht = hwsim_nan_ht_cap;
5798 		hw->wiphy->nan_capa.phy.vht = hwsim_nan_vht_cap;
5799 		hw->wiphy->nan_capa.phy.he = hwsim_nan_he_cap;
5800 
5801 		/*
5802 		 * NAN switches between bands/channels per its schedule,
5803 		 * so mac80211 rate control can't work here.
5804 		 */
5805 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
5806 	}
5807 
5808 	data->if_combination.radar_detect_widths =
5809 				BIT(NL80211_CHAN_WIDTH_5) |
5810 				BIT(NL80211_CHAN_WIDTH_10) |
5811 				BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5812 				BIT(NL80211_CHAN_WIDTH_20) |
5813 				BIT(NL80211_CHAN_WIDTH_40) |
5814 				BIT(NL80211_CHAN_WIDTH_80) |
5815 				BIT(NL80211_CHAN_WIDTH_160);
5816 
5817 	if (data->use_chanctx) {
5818 		hw->wiphy->max_scan_ssids = 255;
5819 		hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
5820 		hw->wiphy->max_remain_on_channel_duration = 1000;
5821 		data->if_combination.num_different_channels = data->channels;
5822 	} else {
5823 		data->if_combination.num_different_channels = 1;
5824 	}
5825 
5826 	if (!n_limits) {
5827 		err = -EINVAL;
5828 		goto failed_hw;
5829 	}
5830 
5831 	data->if_combination.max_interfaces = 0;
5832 	for (i = 0; i < n_limits; i++)
5833 		data->if_combination.max_interfaces +=
5834 			data->if_limits[i].max;
5835 
5836 	data->if_combination.n_limits = n_limits;
5837 	data->if_combination.limits = data->if_limits;
5838 
5839 	/*
5840 	 * If we actually were asked to support combinations,
5841 	 * advertise them - if there's only a single thing like
5842 	 * only IBSS then don't advertise it as combinations.
5843 	 */
5844 	if (data->if_combination.max_interfaces > 1) {
5845 		hw->wiphy->iface_combinations = &data->if_combination;
5846 		hw->wiphy->n_iface_combinations = 1;
5847 	}
5848 
5849 	if (param->ciphers) {
5850 		memcpy(data->ciphers, param->ciphers,
5851 		       param->n_ciphers * sizeof(u32));
5852 		hw->wiphy->cipher_suites = data->ciphers;
5853 		hw->wiphy->n_cipher_suites = param->n_ciphers;
5854 	}
5855 
5856 	hw->wiphy->mbssid_max_interfaces = 8;
5857 	hw->wiphy->ema_max_profile_periodicity = 3;
5858 
5859 	spin_lock_init(&data->tsf_offset_lock);
5860 
5861 	data->rx_rssi = DEFAULT_RX_RSSI;
5862 
5863 	INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
5864 	INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
5865 	INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
5866 
5867 	hw->queues = 5;
5868 	hw->offchannel_tx_hw_queue = 4;
5869 
5870 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
5871 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
5872 	ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
5873 	ieee80211_hw_set(hw, QUEUE_CONTROL);
5874 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
5875 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
5876 	ieee80211_hw_set(hw, MFP_CAPABLE);
5877 	ieee80211_hw_set(hw, SIGNAL_DBM);
5878 	ieee80211_hw_set(hw, SUPPORTS_PS);
5879 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
5880 	ieee80211_hw_set(hw, TDLS_WIDER_BW);
5881 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
5882 	ieee80211_hw_set(hw, STRICT);
5883 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
5884 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
5885 
5886 	if (param->mlo) {
5887 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
5888 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
5889 		ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
5890 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
5891 		ieee80211_hw_set(hw, AP_LINK_PS);
5892 
5893 		hw->wiphy->iftype_ext_capab = mac80211_hwsim_iftypes_ext_capa;
5894 		hw->wiphy->num_iftype_ext_capab =
5895 			ARRAY_SIZE(mac80211_hwsim_iftypes_ext_capa);
5896 	} else {
5897 		ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
5898 		ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
5899 		if (rctbl)
5900 			ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
5901 	}
5902 
5903 	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5904 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
5905 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
5906 			    WIPHY_FLAG_AP_UAPSD |
5907 			    WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5908 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5909 	hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
5910 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
5911 			       NL80211_FEATURE_STATIC_SMPS |
5912 			       NL80211_FEATURE_DYNAMIC_SMPS |
5913 			       NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
5914 			       NL80211_FEATURE_AP_SCAN;
5915 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
5916 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
5917 	wiphy_ext_feature_set(hw->wiphy,
5918 			      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
5919 	wiphy_ext_feature_set(hw->wiphy,
5920 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
5921 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
5922 
5923 	wiphy_ext_feature_set(hw->wiphy,
5924 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
5925 	wiphy_ext_feature_set(hw->wiphy,
5926 			      NL80211_EXT_FEATURE_BSS_COLOR);
5927 	wiphy_ext_feature_set(hw->wiphy,
5928 			      NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
5929 	wiphy_ext_feature_set(hw->wiphy,
5930 			      NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
5931 	wiphy_ext_feature_set(hw->wiphy,
5932 			      NL80211_EXT_FEATURE_EXT_KEY_ID);
5933 	wiphy_ext_feature_set(hw->wiphy,
5934 			      NL80211_EXT_FEATURE_ASSOC_FRAME_ENCRYPTION);
5935 
5936 	hw->wiphy->interface_modes = param->iftypes;
5937 
5938 	/* ask mac80211 to reserve space for magic */
5939 	hw->vif_data_size = sizeof(struct hwsim_vif_priv);
5940 	hw->sta_data_size = sizeof(struct hwsim_sta_priv);
5941 	hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
5942 	hw->txq_data_size = 0;
5943 
5944 	memcpy(data->channels_2ghz, hwsim_channels_2ghz,
5945 		sizeof(hwsim_channels_2ghz));
5946 	memcpy(data->channels_5ghz, hwsim_channels_5ghz,
5947 		sizeof(hwsim_channels_5ghz));
5948 	memcpy(data->channels_6ghz, hwsim_channels_6ghz,
5949 		sizeof(hwsim_channels_6ghz));
5950 	memcpy(data->channels_s1g, hwsim_channels_s1g,
5951 	       sizeof(hwsim_channels_s1g));
5952 	memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
5953 
5954 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
5955 		struct ieee80211_supported_band *sband = &data->bands[band];
5956 		struct wiphy_radio_freq_range *radio_range;
5957 		const struct ieee80211_channel *c;
5958 		struct wiphy_radio *radio;
5959 
5960 		sband->band = band;
5961 
5962 		switch (band) {
5963 		case NL80211_BAND_2GHZ:
5964 			sband->channels = data->channels_2ghz;
5965 			sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
5966 			sband->bitrates = data->rates;
5967 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
5968 			break;
5969 		case NL80211_BAND_5GHZ:
5970 			sband->channels = data->channels_5ghz;
5971 			sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
5972 			sband->bitrates = data->rates + 4;
5973 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
5974 
5975 			sband->vht_cap.vht_supported = true;
5976 			sband->vht_cap.cap =
5977 				IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
5978 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
5979 				IEEE80211_VHT_CAP_RXLDPC |
5980 				IEEE80211_VHT_CAP_SHORT_GI_80 |
5981 				IEEE80211_VHT_CAP_SHORT_GI_160 |
5982 				IEEE80211_VHT_CAP_TXSTBC |
5983 				IEEE80211_VHT_CAP_RXSTBC_4 |
5984 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
5985 			sband->vht_cap.vht_mcs.rx_mcs_map =
5986 				cpu_to_le16(HWSIM_VHT_MCS_MAP);
5987 			sband->vht_cap.vht_mcs.tx_mcs_map =
5988 				sband->vht_cap.vht_mcs.rx_mcs_map;
5989 			break;
5990 		case NL80211_BAND_6GHZ:
5991 			sband->channels = data->channels_6ghz;
5992 			sband->n_channels = ARRAY_SIZE(hwsim_channels_6ghz);
5993 			sband->bitrates = data->rates + 4;
5994 			sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
5995 			break;
5996 		case NL80211_BAND_S1GHZ:
5997 			memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
5998 			       sizeof(sband->s1g_cap));
5999 			sband->channels = data->channels_s1g;
6000 			sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
6001 			break;
6002 		default:
6003 			continue;
6004 		}
6005 
6006 		if (band != NL80211_BAND_6GHZ){
6007 			sband->ht_cap.ht_supported = true;
6008 			sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
6009 					    IEEE80211_HT_CAP_GRN_FLD |
6010 					    IEEE80211_HT_CAP_SGI_20 |
6011 					    IEEE80211_HT_CAP_SGI_40 |
6012 					    IEEE80211_HT_CAP_DSSSCCK40 |
6013 					    IEEE80211_HT_CAP_TX_STBC |
6014 					    IEEE80211_HT_CAP_RX_STBC;
6015 			sband->ht_cap.ampdu_factor = 0x3;
6016 			sband->ht_cap.ampdu_density = 0x6;
6017 			memset(&sband->ht_cap.mcs, 0,
6018 			       sizeof(sband->ht_cap.mcs));
6019 			sband->ht_cap.mcs.rx_mask[0] = 0xff;
6020 			sband->ht_cap.mcs.rx_mask[1] = 0xff;
6021 			sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
6022 		}
6023 
6024 		mac80211_hwsim_sband_capab(sband);
6025 
6026 		hw->wiphy->bands[band] = sband;
6027 
6028 		if (!param->multi_radio)
6029 			continue;
6030 
6031 		c = sband->channels;
6032 		radio_range = &data->radio_range[n_bands];
6033 		radio_range->start_freq = ieee80211_channel_to_khz(c) - 10000;
6034 
6035 		c += sband->n_channels - 1;
6036 		radio_range->end_freq = ieee80211_channel_to_khz(c) + 10000;
6037 
6038 		radio = &data->radio[n_bands++];
6039 		radio->freq_range = radio_range;
6040 		radio->n_freq_range = 1;
6041 		radio->iface_combinations = &data->if_combination_radio;
6042 		radio->n_iface_combinations = 1;
6043 	}
6044 
6045 	if (param->multi_radio) {
6046 		hw->wiphy->radio = data->radio;
6047 		hw->wiphy->n_radio = n_bands;
6048 
6049 		memcpy(&data->if_combination_radio, &data->if_combination,
6050 		       sizeof(data->if_combination));
6051 		data->if_combination.num_different_channels *= n_bands;
6052 	}
6053 
6054 	if (data->use_chanctx)
6055 		data->if_combination.radar_detect_widths = 0;
6056 
6057 	/* By default all radios belong to the first group */
6058 	data->group = 1;
6059 	mutex_init(&data->mutex);
6060 
6061 	data->netgroup = hwsim_net_get_netgroup(net);
6062 	data->wmediumd = hwsim_net_get_wmediumd(net);
6063 
6064 	/* Enable frame retransmissions for lossy channels */
6065 	hw->max_rates = 4;
6066 	hw->max_rate_tries = 11;
6067 
6068 	hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
6069 	hw->wiphy->n_vendor_commands =
6070 		ARRAY_SIZE(mac80211_hwsim_vendor_commands);
6071 	hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
6072 	hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
6073 
6074 	if (param->reg_strict)
6075 		hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
6076 	if (param->regd) {
6077 		data->regd = param->regd;
6078 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
6079 		wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
6080 		/* give the regulatory workqueue a chance to run */
6081 		schedule_timeout_interruptible(1);
6082 	}
6083 
6084 	wiphy_ext_feature_set(hw->wiphy,
6085 			      NL80211_EXT_FEATURE_DFS_CONCURRENT);
6086 	if (param->background_radar)
6087 		wiphy_ext_feature_set(hw->wiphy,
6088 				      NL80211_EXT_FEATURE_RADAR_BACKGROUND);
6089 
6090 	if (param->no_vif)
6091 		ieee80211_hw_set(hw, NO_AUTO_VIF);
6092 
6093 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6094 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_PUNCT);
6095 
6096 	for (i = 0; i < ARRAY_SIZE(data->link_data); i++) {
6097 		hrtimer_setup(&data->link_data[i].beacon_timer, mac80211_hwsim_beacon,
6098 			      CLOCK_MONOTONIC, HRTIMER_MODE_ABS_SOFT);
6099 		data->link_data[i].link_id = i;
6100 	}
6101 
6102 	err = ieee80211_register_hw(hw);
6103 	if (err < 0) {
6104 		pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
6105 		       err);
6106 		goto failed_hw;
6107 	}
6108 
6109 	wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
6110 
6111 	if (param->reg_alpha2) {
6112 		data->alpha2[0] = param->reg_alpha2[0];
6113 		data->alpha2[1] = param->reg_alpha2[1];
6114 		regulatory_hint(hw->wiphy, param->reg_alpha2);
6115 	}
6116 
6117 	data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
6118 	debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
6119 	debugfs_create_file("group", 0666, data->debugfs, data,
6120 			    &hwsim_fops_group);
6121 	debugfs_create_file("rx_rssi", 0666, data->debugfs, data,
6122 			    &hwsim_fops_rx_rssi);
6123 	if (!data->use_chanctx)
6124 		debugfs_create_file("dfs_simulate_radar", 0222,
6125 				    data->debugfs,
6126 				    data, &hwsim_simulate_radar);
6127 	if (param->background_radar)
6128 		debugfs_create_file("dfs_background_cac", 0200,
6129 				    data->debugfs,
6130 				    data, &hwsim_background_cac_ops);
6131 	debugfs_create_file("simulate_incumbent_signal_interference", 0200,
6132 			    data->debugfs,
6133 			    data, &hwsim_simulate_incumbent_signal_fops);
6134 
6135 	if (param->pmsr_capa) {
6136 		data->pmsr_capa = *param->pmsr_capa;
6137 		hw->wiphy->pmsr_capa = &data->pmsr_capa;
6138 	}
6139 
6140 	spin_lock_bh(&hwsim_radio_lock);
6141 	err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
6142 				     hwsim_rht_params);
6143 	if (err < 0) {
6144 		if (info) {
6145 			GENL_SET_ERR_MSG(info, "perm addr already present");
6146 			NL_SET_BAD_ATTR(info->extack,
6147 					info->attrs[HWSIM_ATTR_PERM_ADDR]);
6148 		}
6149 		spin_unlock_bh(&hwsim_radio_lock);
6150 		goto failed_final_insert;
6151 	}
6152 
6153 	list_add_tail(&data->list, &hwsim_radios);
6154 	hwsim_radios_generation++;
6155 	spin_unlock_bh(&hwsim_radio_lock);
6156 
6157 	hwsim_mcast_new_radio(idx, info, param);
6158 
6159 	return idx;
6160 
6161 failed_final_insert:
6162 	debugfs_remove_recursive(data->debugfs);
6163 	ieee80211_unregister_hw(data->hw);
6164 failed_hw:
6165 	device_release_driver(data->dev);
6166 failed_bind:
6167 	device_unregister(data->dev);
6168 failed_drvdata:
6169 	ieee80211_free_hw(hw);
6170 failed:
6171 	return err;
6172 }
6173 
6174 static void hwsim_mcast_del_radio(int id, const char *hwname,
6175 				  struct genl_info *info)
6176 {
6177 	struct sk_buff *skb;
6178 	void *data;
6179 	int ret;
6180 
6181 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6182 	if (!skb)
6183 		return;
6184 
6185 	data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
6186 			   HWSIM_CMD_DEL_RADIO);
6187 	if (!data)
6188 		goto error;
6189 
6190 	ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
6191 	if (ret < 0)
6192 		goto error;
6193 
6194 	ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
6195 		      hwname);
6196 	if (ret < 0)
6197 		goto error;
6198 
6199 	genlmsg_end(skb, data);
6200 
6201 	hwsim_mcast_config_msg(skb, info);
6202 
6203 	return;
6204 
6205 error:
6206 	nlmsg_free(skb);
6207 }
6208 
6209 static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
6210 				     const char *hwname,
6211 				     struct genl_info *info)
6212 {
6213 	hwsim_mcast_del_radio(data->idx, hwname, info);
6214 	debugfs_remove_recursive(data->debugfs);
6215 	ieee80211_unregister_hw(data->hw);
6216 	device_release_driver(data->dev);
6217 	device_unregister(data->dev);
6218 	ieee80211_free_hw(data->hw);
6219 }
6220 
6221 static int mac80211_hwsim_get_radio(struct sk_buff *skb,
6222 				    struct mac80211_hwsim_data *data,
6223 				    u32 portid, u32 seq,
6224 				    struct netlink_callback *cb, int flags)
6225 {
6226 	void *hdr;
6227 	struct hwsim_new_radio_params param = { };
6228 	int res = -EMSGSIZE;
6229 
6230 	hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
6231 			  HWSIM_CMD_GET_RADIO);
6232 	if (!hdr)
6233 		return -EMSGSIZE;
6234 
6235 	if (cb)
6236 		genl_dump_check_consistent(cb, hdr);
6237 
6238 	if (data->alpha2[0] && data->alpha2[1])
6239 		param.reg_alpha2 = data->alpha2;
6240 
6241 	param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
6242 					REGULATORY_STRICT_REG);
6243 	param.p2p_device = !!(data->hw->wiphy->interface_modes &
6244 					BIT(NL80211_IFTYPE_P2P_DEVICE));
6245 	param.nan_device = !!(data->hw->wiphy->interface_modes &
6246 					BIT(NL80211_IFTYPE_NAN));
6247 	param.use_chanctx = data->use_chanctx;
6248 	param.regd = data->regd;
6249 	param.channels = data->channels;
6250 	param.hwname = wiphy_name(data->hw->wiphy);
6251 	param.pmsr_capa = &data->pmsr_capa;
6252 	param.background_radar =
6253 		wiphy_ext_feature_isset(data->hw->wiphy,
6254 					NL80211_EXT_FEATURE_RADAR_BACKGROUND);
6255 
6256 	res = append_radio_msg(skb, data->idx, &param);
6257 	if (res < 0)
6258 		goto out_err;
6259 
6260 	genlmsg_end(skb, hdr);
6261 	return 0;
6262 
6263 out_err:
6264 	genlmsg_cancel(skb, hdr);
6265 	return res;
6266 }
6267 
6268 static void mac80211_hwsim_free(void)
6269 {
6270 	struct mac80211_hwsim_data *data;
6271 
6272 	spin_lock_bh(&hwsim_radio_lock);
6273 	while ((data = list_first_entry_or_null(&hwsim_radios,
6274 						struct mac80211_hwsim_data,
6275 						list))) {
6276 		list_del(&data->list);
6277 		spin_unlock_bh(&hwsim_radio_lock);
6278 		mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
6279 					 NULL);
6280 		spin_lock_bh(&hwsim_radio_lock);
6281 	}
6282 	spin_unlock_bh(&hwsim_radio_lock);
6283 	class_unregister(&hwsim_class);
6284 }
6285 
6286 static const struct net_device_ops hwsim_netdev_ops = {
6287 	.ndo_start_xmit 	= hwsim_mon_xmit,
6288 	.ndo_set_mac_address 	= eth_mac_addr,
6289 	.ndo_validate_addr	= eth_validate_addr,
6290 };
6291 
6292 static void hwsim_mon_setup(struct net_device *dev)
6293 {
6294 	u8 addr[ETH_ALEN];
6295 
6296 	dev->netdev_ops = &hwsim_netdev_ops;
6297 	dev->needs_free_netdev = true;
6298 	ether_setup(dev);
6299 	dev->priv_flags |= IFF_NO_QUEUE;
6300 	dev->type = ARPHRD_IEEE80211_RADIOTAP;
6301 	eth_zero_addr(addr);
6302 	addr[0] = 0x12;
6303 	eth_hw_addr_set(dev, addr);
6304 }
6305 
6306 static void hwsim_register_wmediumd(struct net *net, u32 portid)
6307 {
6308 	struct mac80211_hwsim_data *data;
6309 
6310 	hwsim_net_set_wmediumd(net, portid);
6311 
6312 	spin_lock_bh(&hwsim_radio_lock);
6313 	list_for_each_entry(data, &hwsim_radios, list) {
6314 		if (data->netgroup == hwsim_net_get_netgroup(net))
6315 			data->wmediumd = portid;
6316 	}
6317 	spin_unlock_bh(&hwsim_radio_lock);
6318 }
6319 
6320 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
6321 					   struct genl_info *info)
6322 {
6323 
6324 	struct ieee80211_hdr *hdr;
6325 	struct mac80211_hwsim_data *data2;
6326 	struct ieee80211_tx_info *txi;
6327 	struct hwsim_tx_rate *tx_attempts;
6328 	u64 ret_skb_cookie;
6329 	struct sk_buff *skb, *tmp;
6330 	const u8 *src;
6331 	unsigned int hwsim_flags;
6332 	int i;
6333 	unsigned long flags;
6334 	bool found = false;
6335 
6336 	if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
6337 	    !info->attrs[HWSIM_ATTR_FLAGS] ||
6338 	    !info->attrs[HWSIM_ATTR_COOKIE] ||
6339 	    !info->attrs[HWSIM_ATTR_SIGNAL] ||
6340 	    !info->attrs[HWSIM_ATTR_TX_INFO])
6341 		goto out;
6342 
6343 	src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
6344 	hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
6345 	ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
6346 
6347 	data2 = get_hwsim_data_ref_from_addr(src);
6348 	if (!data2)
6349 		goto out;
6350 
6351 	if (!hwsim_virtio_enabled) {
6352 		if (hwsim_net_get_netgroup(genl_info_net(info)) !=
6353 		    data2->netgroup)
6354 			goto out;
6355 
6356 		if (info->snd_portid != data2->wmediumd)
6357 			goto out;
6358 	}
6359 
6360 	/* look for the skb matching the cookie passed back from user */
6361 	spin_lock_irqsave(&data2->pending.lock, flags);
6362 	skb_queue_walk_safe(&data2->pending, skb, tmp) {
6363 		uintptr_t skb_cookie;
6364 
6365 		txi = IEEE80211_SKB_CB(skb);
6366 		skb_cookie = (uintptr_t)txi->rate_driver_data[0];
6367 
6368 		if (skb_cookie == ret_skb_cookie) {
6369 			__skb_unlink(skb, &data2->pending);
6370 			found = true;
6371 			break;
6372 		}
6373 	}
6374 	spin_unlock_irqrestore(&data2->pending.lock, flags);
6375 
6376 	/* not found */
6377 	if (!found)
6378 		goto out;
6379 
6380 	mac80211_hwsim_monitor_rx(data2->hw, skb, data2->channel);
6381 
6382 	/* Tx info received because the frame was broadcasted on user space,
6383 	 so we get all the necessary info: tx attempts and skb control buff */
6384 
6385 	tx_attempts = (struct hwsim_tx_rate *)nla_data(
6386 		       info->attrs[HWSIM_ATTR_TX_INFO]);
6387 
6388 	/* now send back TX status */
6389 	txi = IEEE80211_SKB_CB(skb);
6390 
6391 	ieee80211_tx_info_clear_status(txi);
6392 
6393 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
6394 		txi->status.rates[i].idx = tx_attempts[i].idx;
6395 		txi->status.rates[i].count = tx_attempts[i].count;
6396 	}
6397 
6398 	txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
6399 
6400 	if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
6401 	   (hwsim_flags & HWSIM_TX_STAT_ACK)) {
6402 		if (skb->len >= 16) {
6403 			hdr = (struct ieee80211_hdr *) skb->data;
6404 			mac80211_hwsim_monitor_ack(data2->channel,
6405 						   hdr->addr2);
6406 		}
6407 		txi->flags |= IEEE80211_TX_STAT_ACK;
6408 	}
6409 
6410 	if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
6411 		txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
6412 
6413 	ieee80211_tx_status_irqsafe(data2->hw, skb);
6414 	return 0;
6415 out:
6416 	return -EINVAL;
6417 
6418 }
6419 
6420 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
6421 					  struct genl_info *info)
6422 {
6423 	struct mac80211_hwsim_data *data2;
6424 	struct ieee80211_rx_status rx_status;
6425 	struct ieee80211_hdr *hdr;
6426 	const u8 *dst;
6427 	int frame_data_len;
6428 	void *frame_data;
6429 	struct sk_buff *skb = NULL;
6430 	struct ieee80211_channel *channel = NULL;
6431 
6432 	if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
6433 	    !info->attrs[HWSIM_ATTR_FRAME] ||
6434 	    !info->attrs[HWSIM_ATTR_RX_RATE] ||
6435 	    !info->attrs[HWSIM_ATTR_SIGNAL])
6436 		goto out;
6437 
6438 	dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
6439 	frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
6440 	frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
6441 
6442 	if (frame_data_len < sizeof(struct ieee80211_hdr_3addr) ||
6443 	    frame_data_len > IEEE80211_MAX_DATA_LEN)
6444 		goto err;
6445 
6446 	/* Allocate new skb here */
6447 	skb = alloc_skb(frame_data_len, GFP_KERNEL);
6448 	if (skb == NULL)
6449 		goto err;
6450 
6451 	/* Copy the data */
6452 	skb_put_data(skb, frame_data, frame_data_len);
6453 
6454 	data2 = get_hwsim_data_ref_from_addr(dst);
6455 	if (!data2)
6456 		goto out;
6457 
6458 	if (data2->use_chanctx) {
6459 		if (data2->tmp_chan)
6460 			channel = data2->tmp_chan;
6461 	} else {
6462 		channel = data2->channel;
6463 	}
6464 
6465 	if (!hwsim_virtio_enabled) {
6466 		if (hwsim_net_get_netgroup(genl_info_net(info)) !=
6467 		    data2->netgroup)
6468 			goto out;
6469 
6470 		if (info->snd_portid != data2->wmediumd)
6471 			goto out;
6472 	}
6473 
6474 	/* check if radio is configured properly */
6475 
6476 	if ((data2->idle && !data2->tmp_chan) || !data2->started)
6477 		goto out;
6478 
6479 	/* A frame is received from user space */
6480 	memset(&rx_status, 0, sizeof(rx_status));
6481 	if (info->attrs[HWSIM_ATTR_FREQ]) {
6482 		struct tx_iter_data iter_data = {
6483 			.hw = data2->hw,
6484 			.rx_status = &rx_status,
6485 		};
6486 
6487 		/* throw away off-channel packets, but allow both the temporary
6488 		 * ("hw" scan/remain-on-channel), regular channels and links,
6489 		 * since the internal datapath also allows this
6490 		 */
6491 		rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
6492 
6493 		iter_data.channel = ieee80211_get_channel(data2->hw->wiphy,
6494 							  rx_status.freq);
6495 		if (!iter_data.channel)
6496 			goto out;
6497 		rx_status.band = iter_data.channel->band;
6498 
6499 		mutex_lock(&data2->mutex);
6500 		if (!hwsim_chans_compat(iter_data.channel, channel)) {
6501 			ieee80211_iterate_active_interfaces_atomic(
6502 				data2->hw, IEEE80211_IFACE_ITER_NORMAL,
6503 				mac80211_hwsim_tx_iter, &iter_data);
6504 			if (!iter_data.receive) {
6505 				mutex_unlock(&data2->mutex);
6506 				goto out;
6507 			}
6508 		}
6509 		mutex_unlock(&data2->mutex);
6510 	} else if (!channel) {
6511 		goto out;
6512 	} else {
6513 		rx_status.freq = channel->center_freq;
6514 		rx_status.band = channel->band;
6515 	}
6516 
6517 	rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
6518 	if (rx_status.rate_idx >= data2->hw->wiphy->bands[rx_status.band]->n_bitrates)
6519 		goto out;
6520 	rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
6521 
6522 	hdr = (void *)skb->data;
6523 
6524 	if (ieee80211_is_beacon(hdr->frame_control) ||
6525 	    ieee80211_is_probe_resp(hdr->frame_control))
6526 		rx_status.boottime_ns = ktime_get_boottime_ns();
6527 
6528 	mac80211_hwsim_rx(data2, &rx_status, skb);
6529 
6530 	return 0;
6531 err:
6532 	pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
6533 out:
6534 	dev_kfree_skb(skb);
6535 	return -EINVAL;
6536 }
6537 
6538 static int hwsim_register_received_nl(struct sk_buff *skb_2,
6539 				      struct genl_info *info)
6540 {
6541 	struct net *net = genl_info_net(info);
6542 	struct mac80211_hwsim_data *data;
6543 	int chans = 1;
6544 
6545 	spin_lock_bh(&hwsim_radio_lock);
6546 	list_for_each_entry(data, &hwsim_radios, list)
6547 		chans = max(chans, data->channels);
6548 	spin_unlock_bh(&hwsim_radio_lock);
6549 
6550 	/* In the future we should revise the userspace API and allow it
6551 	 * to set a flag that it does support multi-channel, then we can
6552 	 * let this pass conditionally on the flag.
6553 	 * For current userspace, prohibit it since it won't work right.
6554 	 */
6555 	if (chans > 1)
6556 		return -EOPNOTSUPP;
6557 
6558 	if (hwsim_net_get_wmediumd(net))
6559 		return -EBUSY;
6560 
6561 	hwsim_register_wmediumd(net, info->snd_portid);
6562 
6563 	pr_debug("mac80211_hwsim: received a REGISTER, "
6564 	       "switching to wmediumd mode with pid %d\n", info->snd_portid);
6565 
6566 	return 0;
6567 }
6568 
6569 /* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
6570 static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
6571 {
6572 	int i;
6573 
6574 	for (i = 0; i < n_ciphers; i++) {
6575 		int j;
6576 		int found = 0;
6577 
6578 		for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
6579 			if (ciphers[i] == hwsim_ciphers[j]) {
6580 				found = 1;
6581 				break;
6582 			}
6583 		}
6584 
6585 		if (!found)
6586 			return false;
6587 	}
6588 
6589 	return true;
6590 }
6591 
6592 static int parse_ftm_capa(const struct nlattr *ftm_capa, struct cfg80211_pmsr_capabilities *out,
6593 			  struct genl_info *info)
6594 {
6595 	struct nlattr *tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX + 1];
6596 	int ret;
6597 
6598 	ret = nla_parse_nested(tb, NL80211_PMSR_FTM_CAPA_ATTR_MAX, ftm_capa, hwsim_ftm_capa_policy,
6599 			       NULL);
6600 	if (ret) {
6601 		NL_SET_ERR_MSG_ATTR(info->extack, ftm_capa, "malformed FTM capability");
6602 		return -EINVAL;
6603 	}
6604 
6605 	out->ftm.supported = 1;
6606 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES])
6607 		out->ftm.preambles = nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES]);
6608 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS])
6609 		out->ftm.bandwidths = nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS]);
6610 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT])
6611 		out->ftm.max_bursts_exponent =
6612 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT]);
6613 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST])
6614 		out->ftm.max_ftms_per_burst =
6615 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST]);
6616 	out->ftm.asap = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_ASAP];
6617 	out->ftm.non_asap = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP];
6618 	out->ftm.request_lci = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI];
6619 	out->ftm.request_civicloc = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC];
6620 	out->ftm.trigger_based = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED];
6621 	out->ftm.non_trigger_based = !!tb[NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED];
6622 
6623 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_NUM_TX_ANTENNAS])
6624 		out->ftm.max_no_of_tx_antennas =
6625 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_NUM_TX_ANTENNAS]);
6626 
6627 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_NUM_RX_ANTENNAS])
6628 		out->ftm.max_no_of_rx_antennas =
6629 			nla_get_u8(tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX_NUM_RX_ANTENNAS]);
6630 
6631 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MIN_INTERVAL_EDCA])
6632 		out->ftm.min_allowed_ranging_interval_edca =
6633 			nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_MIN_INTERVAL_EDCA]);
6634 
6635 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_MIN_INTERVAL_NTB])
6636 		out->ftm.min_allowed_ranging_interval_ntb =
6637 			nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_MIN_INTERVAL_NTB]);
6638 
6639 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_PD_PREAMBLES])
6640 		out->ftm.pd_preambles =
6641 			nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_PD_PREAMBLES]);
6642 
6643 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_PD_BANDWIDTHS])
6644 		out->ftm.pd_bandwidths =
6645 			nla_get_u32(tb[NL80211_PMSR_FTM_CAPA_ATTR_PD_BANDWIDTHS]);
6646 
6647 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_ISTA_CAPS]) {
6648 		struct nlattr *ista_tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX + 1];
6649 
6650 		if (!nla_parse_nested(ista_tb, NL80211_PMSR_FTM_CAPA_ATTR_MAX,
6651 				      tb[NL80211_PMSR_FTM_CAPA_ATTR_ISTA_CAPS],
6652 				      hwsim_ftm_role_capa_policy, NULL)) {
6653 			out->ftm.ista.support_ntb =
6654 				!!ista_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_NTB];
6655 			out->ftm.ista.support_tb =
6656 				!!ista_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_TB];
6657 			out->ftm.ista.support_edca =
6658 				!!ista_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_EDCA];
6659 			if (ista_tb[NL80211_PMSR_ATTR_MAX_PEER_ISTA_ROLE])
6660 				out->ftm.ista.max_peers =
6661 					nla_get_u32(ista_tb[NL80211_PMSR_ATTR_MAX_PEER_ISTA_ROLE]);
6662 		}
6663 	}
6664 
6665 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_RSTA_CAPS]) {
6666 		struct nlattr *rsta_tb[NL80211_PMSR_FTM_CAPA_ATTR_MAX + 1];
6667 
6668 		if (!nla_parse_nested(rsta_tb, NL80211_PMSR_FTM_CAPA_ATTR_MAX,
6669 				      tb[NL80211_PMSR_FTM_CAPA_ATTR_RSTA_CAPS],
6670 				      hwsim_ftm_role_capa_policy, NULL)) {
6671 			out->ftm.rsta.support_ntb =
6672 				!!rsta_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_NTB];
6673 			out->ftm.rsta.support_tb =
6674 				!!rsta_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_TB];
6675 			out->ftm.rsta.support_edca =
6676 				!!rsta_tb[NL80211_PMSR_FTM_CAPA_ATTR_SUPPORT_EDCA];
6677 			if (rsta_tb[NL80211_PMSR_ATTR_MAX_PEER_RSTA_ROLE])
6678 				out->ftm.rsta.max_peers =
6679 					nla_get_u32(rsta_tb[NL80211_PMSR_ATTR_MAX_PEER_RSTA_ROLE]);
6680 		}
6681 	}
6682 
6683 	if (tb[NL80211_PMSR_FTM_CAPA_ATTR_TYPE_CAPS]) {
6684 		struct nlattr *type_tb[NL80211_PMSR_FTM_TYPE_CAPA_ATTR_MAX + 1];
6685 
6686 		if (!nla_parse_nested(type_tb, NL80211_PMSR_FTM_TYPE_CAPA_ATTR_MAX,
6687 				      tb[NL80211_PMSR_FTM_CAPA_ATTR_TYPE_CAPS],
6688 				      hwsim_ftm_type_capa_policy, NULL)) {
6689 			out->ftm.type.infra_support =
6690 				!!type_tb[NL80211_PMSR_FTM_TYPE_CAPA_ATTR_INFRA_SUPPORT];
6691 			out->ftm.type.pd_support =
6692 				!!type_tb[NL80211_PMSR_FTM_TYPE_CAPA_ATTR_PD_SUPPORT];
6693 		}
6694 	}
6695 
6696 	out->ftm.concurrent_ista_rsta_support =
6697 		!!tb[NL80211_PMSR_FTM_CAPA_ATTR_CONCURRENT_ISTA_RSTA_SUPPORT];
6698 
6699 	return 0;
6700 }
6701 
6702 static int parse_pmsr_capa(const struct nlattr *pmsr_capa, struct cfg80211_pmsr_capabilities *out,
6703 			   struct genl_info *info)
6704 {
6705 	struct nlattr *tb[NL80211_PMSR_ATTR_MAX + 1];
6706 	struct nlattr *nla;
6707 	int size;
6708 	int ret;
6709 
6710 	ret = nla_parse_nested(tb, NL80211_PMSR_ATTR_MAX, pmsr_capa, hwsim_pmsr_capa_policy, NULL);
6711 	if (ret) {
6712 		NL_SET_ERR_MSG_ATTR(info->extack, pmsr_capa, "malformed PMSR capability");
6713 		return -EINVAL;
6714 	}
6715 
6716 	if (tb[NL80211_PMSR_ATTR_MAX_PEERS])
6717 		out->max_peers = nla_get_u32(tb[NL80211_PMSR_ATTR_MAX_PEERS]);
6718 	out->report_ap_tsf = !!tb[NL80211_PMSR_ATTR_REPORT_AP_TSF];
6719 	out->randomize_mac_addr = !!tb[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR];
6720 
6721 	if (!tb[NL80211_PMSR_ATTR_TYPE_CAPA]) {
6722 		NL_SET_ERR_MSG_ATTR(info->extack, tb[NL80211_PMSR_ATTR_TYPE_CAPA],
6723 				    "malformed PMSR type");
6724 		return -EINVAL;
6725 	}
6726 
6727 	nla_for_each_nested(nla, tb[NL80211_PMSR_ATTR_TYPE_CAPA], size) {
6728 		switch (nla_type(nla)) {
6729 		case NL80211_PMSR_TYPE_FTM:
6730 			parse_ftm_capa(nla, out, info);
6731 			break;
6732 		default:
6733 			NL_SET_ERR_MSG_ATTR(info->extack, nla, "unsupported measurement type");
6734 			return -EINVAL;
6735 		}
6736 	}
6737 
6738 	return 0;
6739 }
6740 
6741 static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
6742 {
6743 	struct hwsim_new_radio_params param = { 0 };
6744 	const char *hwname = NULL;
6745 	int ret;
6746 
6747 	param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
6748 	param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
6749 	param.nan_device = info->attrs[HWSIM_ATTR_SUPPORT_NAN_DEVICE];
6750 	param.channels = channels;
6751 	param.destroy_on_close =
6752 		info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
6753 
6754 	if (info->attrs[HWSIM_ATTR_CHANNELS])
6755 		param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
6756 
6757 	if (param.channels < 1) {
6758 		GENL_SET_ERR_MSG(info, "must have at least one channel");
6759 		return -EINVAL;
6760 	}
6761 
6762 	if (info->attrs[HWSIM_ATTR_NO_VIF])
6763 		param.no_vif = true;
6764 
6765 	if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
6766 		param.use_chanctx = true;
6767 	else
6768 		param.use_chanctx = (param.channels > 1);
6769 
6770 	if (info->attrs[HWSIM_ATTR_MULTI_RADIO])
6771 		param.multi_radio = true;
6772 
6773 	if (info->attrs[HWSIM_ATTR_SUPPORT_BACKGROUND_RADAR])
6774 		param.background_radar = true;
6775 
6776 	if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
6777 		param.reg_alpha2 =
6778 			nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
6779 
6780 	if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
6781 		u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
6782 
6783 		if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
6784 			return -EINVAL;
6785 
6786 		idx = array_index_nospec(idx,
6787 					 ARRAY_SIZE(hwsim_world_regdom_custom));
6788 		param.regd = hwsim_world_regdom_custom[idx];
6789 	}
6790 
6791 	if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
6792 		if (!is_valid_ether_addr(
6793 				nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
6794 			GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
6795 			NL_SET_BAD_ATTR(info->extack,
6796 					info->attrs[HWSIM_ATTR_PERM_ADDR]);
6797 			return -EINVAL;
6798 		}
6799 
6800 		param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
6801 	}
6802 
6803 	if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
6804 		param.iftypes =
6805 			nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
6806 
6807 		if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
6808 			NL_SET_ERR_MSG_ATTR(info->extack,
6809 					    info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
6810 					    "cannot support more iftypes than kernel");
6811 			return -EINVAL;
6812 		}
6813 	} else {
6814 		param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
6815 	}
6816 
6817 	/* ensure both flag and iftype support is honored */
6818 	if (param.p2p_device ||
6819 	    param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
6820 		param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
6821 		param.p2p_device = true;
6822 	}
6823 
6824 	if (param.nan_device) {
6825 		if (param.multi_radio) {
6826 			NL_SET_ERR_MSG(info->extack,
6827 				       "NAN is not supported on multi-radio wiphys");
6828 			return -EINVAL;
6829 		}
6830 		param.iftypes |= BIT(NL80211_IFTYPE_NAN) |
6831 				 BIT(NL80211_IFTYPE_NAN_DATA);
6832 	}
6833 
6834 	if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
6835 		u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
6836 
6837 		param.ciphers =
6838 			nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
6839 
6840 		if (len % sizeof(u32)) {
6841 			NL_SET_ERR_MSG_ATTR(info->extack,
6842 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6843 					    "bad cipher list length");
6844 			return -EINVAL;
6845 		}
6846 
6847 		param.n_ciphers = len / sizeof(u32);
6848 
6849 		if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
6850 			NL_SET_ERR_MSG_ATTR(info->extack,
6851 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6852 					    "too many ciphers specified");
6853 			return -EINVAL;
6854 		}
6855 
6856 		if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
6857 			NL_SET_ERR_MSG_ATTR(info->extack,
6858 					    info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
6859 					    "unsupported ciphers specified");
6860 			return -EINVAL;
6861 		}
6862 	}
6863 
6864 	param.mlo = info->attrs[HWSIM_ATTR_MLO_SUPPORT];
6865 
6866 	if (param.mlo || param.multi_radio)
6867 		param.use_chanctx = true;
6868 
6869 	if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
6870 		hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6871 				  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6872 				  GFP_KERNEL);
6873 		if (!hwname)
6874 			return -ENOMEM;
6875 		param.hwname = hwname;
6876 	}
6877 
6878 	if (info->attrs[HWSIM_ATTR_PMSR_SUPPORT]) {
6879 		struct cfg80211_pmsr_capabilities *pmsr_capa;
6880 
6881 		pmsr_capa = kzalloc_obj(*pmsr_capa);
6882 		if (!pmsr_capa) {
6883 			ret = -ENOMEM;
6884 			goto out_free;
6885 		}
6886 		param.pmsr_capa = pmsr_capa;
6887 
6888 		ret = parse_pmsr_capa(info->attrs[HWSIM_ATTR_PMSR_SUPPORT], pmsr_capa, info);
6889 		if (ret)
6890 			goto out_free;
6891 	}
6892 
6893 	ret = mac80211_hwsim_new_radio(info, &param);
6894 
6895 out_free:
6896 	kfree(hwname);
6897 	kfree(param.pmsr_capa);
6898 	return ret;
6899 }
6900 
6901 static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
6902 {
6903 	struct mac80211_hwsim_data *data;
6904 	s64 idx = -1;
6905 	const char *hwname = NULL;
6906 
6907 	if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
6908 		idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
6909 	} else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
6910 		hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6911 				  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
6912 				  GFP_KERNEL);
6913 		if (!hwname)
6914 			return -ENOMEM;
6915 	} else
6916 		return -EINVAL;
6917 
6918 	spin_lock_bh(&hwsim_radio_lock);
6919 	list_for_each_entry(data, &hwsim_radios, list) {
6920 		if (idx >= 0) {
6921 			if (data->idx != idx)
6922 				continue;
6923 		} else {
6924 			if (!hwname ||
6925 			    strcmp(hwname, wiphy_name(data->hw->wiphy)))
6926 				continue;
6927 		}
6928 
6929 		if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
6930 			continue;
6931 
6932 		list_del(&data->list);
6933 		rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
6934 				       hwsim_rht_params);
6935 		hwsim_radios_generation++;
6936 		spin_unlock_bh(&hwsim_radio_lock);
6937 		mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
6938 					 info);
6939 		kfree(hwname);
6940 		return 0;
6941 	}
6942 	spin_unlock_bh(&hwsim_radio_lock);
6943 
6944 	kfree(hwname);
6945 	return -ENODEV;
6946 }
6947 
6948 static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
6949 {
6950 	struct mac80211_hwsim_data *data;
6951 	struct sk_buff *skb;
6952 	int idx, res = -ENODEV;
6953 
6954 	if (!info->attrs[HWSIM_ATTR_RADIO_ID])
6955 		return -EINVAL;
6956 	idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
6957 
6958 	spin_lock_bh(&hwsim_radio_lock);
6959 	list_for_each_entry(data, &hwsim_radios, list) {
6960 		if (data->idx != idx)
6961 			continue;
6962 
6963 		if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
6964 			continue;
6965 
6966 		skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
6967 		if (!skb) {
6968 			res = -ENOMEM;
6969 			goto out_err;
6970 		}
6971 
6972 		res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
6973 					       info->snd_seq, NULL, 0);
6974 		if (res < 0) {
6975 			nlmsg_free(skb);
6976 			goto out_err;
6977 		}
6978 
6979 		res = genlmsg_reply(skb, info);
6980 		break;
6981 	}
6982 
6983 out_err:
6984 	spin_unlock_bh(&hwsim_radio_lock);
6985 
6986 	return res;
6987 }
6988 
6989 static int hwsim_dump_radio_nl(struct sk_buff *skb,
6990 			       struct netlink_callback *cb)
6991 {
6992 	int last_idx = cb->args[0] - 1;
6993 	struct mac80211_hwsim_data *data = NULL;
6994 	int res = 0;
6995 	void *hdr;
6996 
6997 	spin_lock_bh(&hwsim_radio_lock);
6998 	cb->seq = hwsim_radios_generation;
6999 
7000 	if (last_idx >= hwsim_radio_idx-1)
7001 		goto done;
7002 
7003 	list_for_each_entry(data, &hwsim_radios, list) {
7004 		if (data->idx <= last_idx)
7005 			continue;
7006 
7007 		if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
7008 			continue;
7009 
7010 		res = mac80211_hwsim_get_radio(skb, data,
7011 					       NETLINK_CB(cb->skb).portid,
7012 					       cb->nlh->nlmsg_seq, cb,
7013 					       NLM_F_MULTI);
7014 		if (res < 0)
7015 			break;
7016 
7017 		last_idx = data->idx;
7018 	}
7019 
7020 	cb->args[0] = last_idx + 1;
7021 
7022 	/* list changed, but no new element sent, set interrupted flag */
7023 	if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
7024 		hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
7025 				  cb->nlh->nlmsg_seq, &hwsim_genl_family,
7026 				  NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
7027 		if (hdr) {
7028 			genl_dump_check_consistent(cb, hdr);
7029 			genlmsg_end(skb, hdr);
7030 		} else {
7031 			res = -EMSGSIZE;
7032 		}
7033 	}
7034 
7035 done:
7036 	spin_unlock_bh(&hwsim_radio_lock);
7037 	return res ?: skb->len;
7038 }
7039 
7040 /* Generic Netlink operations array */
7041 static const struct genl_small_ops hwsim_ops[] = {
7042 	{
7043 		.cmd = HWSIM_CMD_REGISTER,
7044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7045 		.doit = hwsim_register_received_nl,
7046 		.flags = GENL_UNS_ADMIN_PERM,
7047 	},
7048 	{
7049 		.cmd = HWSIM_CMD_FRAME,
7050 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7051 		.doit = hwsim_cloned_frame_received_nl,
7052 	},
7053 	{
7054 		.cmd = HWSIM_CMD_TX_INFO_FRAME,
7055 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7056 		.doit = hwsim_tx_info_frame_received_nl,
7057 	},
7058 	{
7059 		.cmd = HWSIM_CMD_NEW_RADIO,
7060 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7061 		.doit = hwsim_new_radio_nl,
7062 		.flags = GENL_UNS_ADMIN_PERM,
7063 	},
7064 	{
7065 		.cmd = HWSIM_CMD_DEL_RADIO,
7066 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7067 		.doit = hwsim_del_radio_nl,
7068 		.flags = GENL_UNS_ADMIN_PERM,
7069 	},
7070 	{
7071 		.cmd = HWSIM_CMD_GET_RADIO,
7072 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7073 		.doit = hwsim_get_radio_nl,
7074 		.dumpit = hwsim_dump_radio_nl,
7075 	},
7076 	{
7077 		.cmd = HWSIM_CMD_REPORT_PMSR,
7078 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7079 		.doit = hwsim_pmsr_report_nl,
7080 	},
7081 };
7082 
7083 static struct genl_family hwsim_genl_family __ro_after_init = {
7084 	.name = "MAC80211_HWSIM",
7085 	.version = 1,
7086 	.maxattr = HWSIM_ATTR_MAX,
7087 	.policy = hwsim_genl_policy,
7088 	.netnsok = true,
7089 	.module = THIS_MODULE,
7090 	.small_ops = hwsim_ops,
7091 	.n_small_ops = ARRAY_SIZE(hwsim_ops),
7092 	.resv_start_op = HWSIM_CMD_REPORT_PMSR + 1, // match with __HWSIM_CMD_MAX
7093 	.mcgrps = hwsim_mcgrps,
7094 	.n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
7095 };
7096 
7097 static void remove_user_radios(u32 portid, int netgroup)
7098 {
7099 	struct mac80211_hwsim_data *entry, *tmp;
7100 	LIST_HEAD(list);
7101 
7102 	spin_lock_bh(&hwsim_radio_lock);
7103 	list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
7104 		if (entry->destroy_on_close && entry->portid == portid &&
7105 		    entry->netgroup == netgroup) {
7106 			list_move(&entry->list, &list);
7107 			rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
7108 					       hwsim_rht_params);
7109 			hwsim_radios_generation++;
7110 		}
7111 	}
7112 	spin_unlock_bh(&hwsim_radio_lock);
7113 
7114 	list_for_each_entry_safe(entry, tmp, &list, list) {
7115 		list_del(&entry->list);
7116 		mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
7117 					 NULL);
7118 	}
7119 }
7120 
7121 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
7122 					 unsigned long state,
7123 					 void *_notify)
7124 {
7125 	struct netlink_notify *notify = _notify;
7126 
7127 	if (state != NETLINK_URELEASE)
7128 		return NOTIFY_DONE;
7129 
7130 	remove_user_radios(notify->portid, hwsim_net_get_netgroup(notify->net));
7131 
7132 	if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
7133 		printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
7134 		       " socket, switching to perfect channel medium\n");
7135 		hwsim_register_wmediumd(notify->net, 0);
7136 	}
7137 	return NOTIFY_DONE;
7138 
7139 }
7140 
7141 static struct notifier_block hwsim_netlink_notifier = {
7142 	.notifier_call = mac80211_hwsim_netlink_notify,
7143 };
7144 
7145 static int __init hwsim_init_netlink(void)
7146 {
7147 	int rc;
7148 
7149 	printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
7150 
7151 	rc = genl_register_family(&hwsim_genl_family);
7152 	if (rc)
7153 		goto failure;
7154 
7155 	rc = netlink_register_notifier(&hwsim_netlink_notifier);
7156 	if (rc) {
7157 		genl_unregister_family(&hwsim_genl_family);
7158 		goto failure;
7159 	}
7160 
7161 	return 0;
7162 
7163 failure:
7164 	pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
7165 	return -EINVAL;
7166 }
7167 
7168 static __net_init int hwsim_init_net(struct net *net)
7169 {
7170 	return hwsim_net_set_netgroup(net);
7171 }
7172 
7173 static void __net_exit hwsim_exit_net(struct net *net)
7174 {
7175 	struct mac80211_hwsim_data *data, *tmp;
7176 	LIST_HEAD(list);
7177 
7178 	spin_lock_bh(&hwsim_radio_lock);
7179 	list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
7180 		if (!net_eq(wiphy_net(data->hw->wiphy), net))
7181 			continue;
7182 
7183 		/* Radios created in init_net are returned to init_net. */
7184 		if (data->netgroup == hwsim_net_get_netgroup(&init_net))
7185 			continue;
7186 
7187 		list_move(&data->list, &list);
7188 		rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
7189 				       hwsim_rht_params);
7190 		hwsim_radios_generation++;
7191 	}
7192 	spin_unlock_bh(&hwsim_radio_lock);
7193 
7194 	list_for_each_entry_safe(data, tmp, &list, list) {
7195 		list_del(&data->list);
7196 		mac80211_hwsim_del_radio(data,
7197 					 wiphy_name(data->hw->wiphy),
7198 					 NULL);
7199 	}
7200 
7201 	ida_free(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
7202 }
7203 
7204 static struct pernet_operations hwsim_net_ops = {
7205 	.init = hwsim_init_net,
7206 	.exit = hwsim_exit_net,
7207 	.id   = &hwsim_net_id,
7208 	.size = sizeof(struct hwsim_net),
7209 };
7210 
7211 static void hwsim_exit_netlink(void)
7212 {
7213 	/* unregister the notifier */
7214 	netlink_unregister_notifier(&hwsim_netlink_notifier);
7215 	/* unregister the family */
7216 	genl_unregister_family(&hwsim_genl_family);
7217 }
7218 
7219 #if IS_REACHABLE(CONFIG_VIRTIO)
7220 static void hwsim_virtio_tx_done(struct virtqueue *vq)
7221 {
7222 	unsigned int len;
7223 	struct sk_buff *skb;
7224 	unsigned long flags;
7225 
7226 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
7227 	while ((skb = virtqueue_get_buf(vq, &len)))
7228 		dev_kfree_skb_irq(skb);
7229 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7230 }
7231 
7232 static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
7233 {
7234 	struct nlmsghdr *nlh;
7235 	struct genlmsghdr *gnlh;
7236 	struct nlattr *tb[HWSIM_ATTR_MAX + 1];
7237 	struct genl_info info = {};
7238 	int err;
7239 
7240 	nlh = nlmsg_hdr(skb);
7241 	gnlh = nlmsg_data(nlh);
7242 
7243 	if (skb->len < nlh->nlmsg_len)
7244 		return -EINVAL;
7245 
7246 	err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
7247 			    hwsim_genl_policy, NULL);
7248 	if (err) {
7249 		pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
7250 		return err;
7251 	}
7252 
7253 	info.attrs = tb;
7254 
7255 	switch (gnlh->cmd) {
7256 	case HWSIM_CMD_FRAME:
7257 		hwsim_cloned_frame_received_nl(skb, &info);
7258 		break;
7259 	case HWSIM_CMD_TX_INFO_FRAME:
7260 		hwsim_tx_info_frame_received_nl(skb, &info);
7261 		break;
7262 	case HWSIM_CMD_REPORT_PMSR:
7263 		hwsim_pmsr_report_nl(skb, &info);
7264 		break;
7265 	default:
7266 		pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
7267 		return -EPROTO;
7268 	}
7269 	return 0;
7270 }
7271 
7272 static void hwsim_virtio_rx_work(struct work_struct *work)
7273 {
7274 	struct virtqueue *vq;
7275 	unsigned int len;
7276 	struct sk_buff *skb;
7277 	struct scatterlist sg[1];
7278 	int err;
7279 	unsigned long flags;
7280 
7281 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
7282 	if (!hwsim_virtio_enabled)
7283 		goto out_unlock;
7284 
7285 	skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
7286 	if (!skb)
7287 		goto out_unlock;
7288 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7289 
7290 	skb->data = skb->head;
7291 	skb_reset_tail_pointer(skb);
7292 	skb_put(skb, len);
7293 	hwsim_virtio_handle_cmd(skb);
7294 
7295 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
7296 	if (!hwsim_virtio_enabled) {
7297 		dev_kfree_skb_irq(skb);
7298 		goto out_unlock;
7299 	}
7300 	vq = hwsim_vqs[HWSIM_VQ_RX];
7301 	sg_init_one(sg, skb->head, skb_end_offset(skb));
7302 	err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
7303 	if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
7304 		dev_kfree_skb_irq(skb);
7305 	else
7306 		virtqueue_kick(vq);
7307 	schedule_work(&hwsim_virtio_rx);
7308 
7309 out_unlock:
7310 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7311 }
7312 
7313 static void hwsim_virtio_rx_done(struct virtqueue *vq)
7314 {
7315 	schedule_work(&hwsim_virtio_rx);
7316 }
7317 
7318 static int init_vqs(struct virtio_device *vdev)
7319 {
7320 	struct virtqueue_info vqs_info[HWSIM_NUM_VQS] = {
7321 		[HWSIM_VQ_TX] = { "tx", hwsim_virtio_tx_done },
7322 		[HWSIM_VQ_RX] = { "rx", hwsim_virtio_rx_done },
7323 	};
7324 
7325 	return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
7326 			       hwsim_vqs, vqs_info, NULL);
7327 }
7328 
7329 static int fill_vq(struct virtqueue *vq)
7330 {
7331 	int i, err;
7332 	struct sk_buff *skb;
7333 	struct scatterlist sg[1];
7334 
7335 	for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
7336 		skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7337 		if (!skb)
7338 			return -ENOMEM;
7339 
7340 		sg_init_one(sg, skb->head, skb_end_offset(skb));
7341 		err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
7342 		if (err) {
7343 			nlmsg_free(skb);
7344 			return err;
7345 		}
7346 	}
7347 	virtqueue_kick(vq);
7348 	return 0;
7349 }
7350 
7351 static void remove_vqs(struct virtio_device *vdev)
7352 {
7353 	int i;
7354 
7355 	virtio_reset_device(vdev);
7356 
7357 	for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
7358 		struct virtqueue *vq = hwsim_vqs[i];
7359 		struct sk_buff *skb;
7360 
7361 		while ((skb = virtqueue_detach_unused_buf(vq)))
7362 			nlmsg_free(skb);
7363 	}
7364 
7365 	vdev->config->del_vqs(vdev);
7366 }
7367 
7368 static int hwsim_virtio_probe(struct virtio_device *vdev)
7369 {
7370 	int err;
7371 	unsigned long flags;
7372 
7373 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
7374 	if (hwsim_virtio_enabled) {
7375 		spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7376 		return -EEXIST;
7377 	}
7378 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7379 
7380 	err = init_vqs(vdev);
7381 	if (err)
7382 		return err;
7383 
7384 	virtio_device_ready(vdev);
7385 
7386 	err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
7387 	if (err)
7388 		goto out_remove;
7389 
7390 	spin_lock_irqsave(&hwsim_virtio_lock, flags);
7391 	hwsim_virtio_enabled = true;
7392 	spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
7393 
7394 	schedule_work(&hwsim_virtio_rx);
7395 	return 0;
7396 
7397 out_remove:
7398 	remove_vqs(vdev);
7399 	return err;
7400 }
7401 
7402 static void hwsim_virtio_remove(struct virtio_device *vdev)
7403 {
7404 	hwsim_virtio_enabled = false;
7405 
7406 	cancel_work_sync(&hwsim_virtio_rx);
7407 
7408 	remove_vqs(vdev);
7409 }
7410 
7411 /* MAC80211_HWSIM virtio device id table */
7412 static const struct virtio_device_id id_table[] = {
7413 	{ VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
7414 	{ 0 }
7415 };
7416 MODULE_DEVICE_TABLE(virtio, id_table);
7417 
7418 static struct virtio_driver virtio_hwsim = {
7419 	.driver.name = KBUILD_MODNAME,
7420 	.id_table = id_table,
7421 	.probe = hwsim_virtio_probe,
7422 	.remove = hwsim_virtio_remove,
7423 };
7424 
7425 static int hwsim_register_virtio_driver(void)
7426 {
7427 	return register_virtio_driver(&virtio_hwsim);
7428 }
7429 
7430 static void hwsim_unregister_virtio_driver(void)
7431 {
7432 	unregister_virtio_driver(&virtio_hwsim);
7433 }
7434 #else
7435 static inline int hwsim_register_virtio_driver(void)
7436 {
7437 	return 0;
7438 }
7439 
7440 static inline void hwsim_unregister_virtio_driver(void)
7441 {
7442 }
7443 #endif
7444 
7445 static int __init init_mac80211_hwsim(void)
7446 {
7447 	int i, err;
7448 
7449 	if (radios < 0 || radios > 100)
7450 		return -EINVAL;
7451 
7452 	if (channels < 1)
7453 		return -EINVAL;
7454 
7455 	err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
7456 	if (err)
7457 		return err;
7458 
7459 	err = register_pernet_device(&hwsim_net_ops);
7460 	if (err)
7461 		goto out_free_rht;
7462 
7463 	err = platform_driver_register(&mac80211_hwsim_driver);
7464 	if (err)
7465 		goto out_unregister_pernet;
7466 
7467 	err = hwsim_init_netlink();
7468 	if (err)
7469 		goto out_unregister_driver;
7470 
7471 	err = hwsim_register_virtio_driver();
7472 	if (err)
7473 		goto out_exit_netlink;
7474 
7475 	err = class_register(&hwsim_class);
7476 	if (err)
7477 		goto out_exit_virtio;
7478 
7479 	for (i = 0; i < radios; i++) {
7480 		struct hwsim_new_radio_params param = { 0 };
7481 
7482 		param.channels = channels;
7483 
7484 		switch (regtest) {
7485 		case HWSIM_REGTEST_DIFF_COUNTRY:
7486 			if (i < ARRAY_SIZE(hwsim_alpha2s))
7487 				param.reg_alpha2 = hwsim_alpha2s[i];
7488 			break;
7489 		case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
7490 			if (!i)
7491 				param.reg_alpha2 = hwsim_alpha2s[0];
7492 			break;
7493 		case HWSIM_REGTEST_STRICT_ALL:
7494 			param.reg_strict = true;
7495 			fallthrough;
7496 		case HWSIM_REGTEST_DRIVER_REG_ALL:
7497 			param.reg_alpha2 = hwsim_alpha2s[0];
7498 			break;
7499 		case HWSIM_REGTEST_WORLD_ROAM:
7500 			if (i == 0)
7501 				param.regd = &hwsim_world_regdom_custom_01;
7502 			break;
7503 		case HWSIM_REGTEST_CUSTOM_WORLD:
7504 			param.regd = &hwsim_world_regdom_custom_03;
7505 			break;
7506 		case HWSIM_REGTEST_CUSTOM_WORLD_2:
7507 			if (i == 0)
7508 				param.regd = &hwsim_world_regdom_custom_03;
7509 			else if (i == 1)
7510 				param.regd = &hwsim_world_regdom_custom_02;
7511 			break;
7512 		case HWSIM_REGTEST_STRICT_FOLLOW:
7513 			if (i == 0) {
7514 				param.reg_strict = true;
7515 				param.reg_alpha2 = hwsim_alpha2s[0];
7516 			}
7517 			break;
7518 		case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
7519 			if (i == 0) {
7520 				param.reg_strict = true;
7521 				param.reg_alpha2 = hwsim_alpha2s[0];
7522 			} else if (i == 1) {
7523 				param.reg_alpha2 = hwsim_alpha2s[1];
7524 			}
7525 			break;
7526 		case HWSIM_REGTEST_ALL:
7527 			switch (i) {
7528 			case 0:
7529 				param.regd = &hwsim_world_regdom_custom_01;
7530 				break;
7531 			case 1:
7532 				param.regd = &hwsim_world_regdom_custom_02;
7533 				break;
7534 			case 2:
7535 				param.reg_alpha2 = hwsim_alpha2s[0];
7536 				break;
7537 			case 3:
7538 				param.reg_alpha2 = hwsim_alpha2s[1];
7539 				break;
7540 			case 4:
7541 				param.reg_strict = true;
7542 				param.reg_alpha2 = hwsim_alpha2s[2];
7543 				break;
7544 			}
7545 			break;
7546 		default:
7547 			break;
7548 		}
7549 
7550 		param.p2p_device = support_p2p_device;
7551 		param.mlo = mlo;
7552 		param.multi_radio = multi_radio;
7553 		param.background_radar = true;
7554 		param.use_chanctx = channels > 1 || mlo || multi_radio;
7555 		param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
7556 		if (param.p2p_device)
7557 			param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
7558 
7559 		err = mac80211_hwsim_new_radio(NULL, &param);
7560 		if (err < 0)
7561 			goto out_free_radios;
7562 	}
7563 
7564 	hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
7565 				 hwsim_mon_setup);
7566 	if (hwsim_mon == NULL) {
7567 		err = -ENOMEM;
7568 		goto out_free_radios;
7569 	}
7570 
7571 	rtnl_lock();
7572 	err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
7573 	if (err < 0) {
7574 		rtnl_unlock();
7575 		goto out_free_mon;
7576 	}
7577 
7578 	err = register_netdevice(hwsim_mon);
7579 	if (err < 0) {
7580 		rtnl_unlock();
7581 		goto out_free_mon;
7582 	}
7583 	rtnl_unlock();
7584 
7585 	return 0;
7586 
7587 out_free_mon:
7588 	free_netdev(hwsim_mon);
7589 out_free_radios:
7590 	mac80211_hwsim_free();
7591 out_exit_virtio:
7592 	hwsim_unregister_virtio_driver();
7593 out_exit_netlink:
7594 	hwsim_exit_netlink();
7595 out_unregister_driver:
7596 	platform_driver_unregister(&mac80211_hwsim_driver);
7597 out_unregister_pernet:
7598 	unregister_pernet_device(&hwsim_net_ops);
7599 out_free_rht:
7600 	rhashtable_destroy(&hwsim_radios_rht);
7601 	return err;
7602 }
7603 module_init(init_mac80211_hwsim);
7604 
7605 static void __exit exit_mac80211_hwsim(void)
7606 {
7607 	pr_debug("mac80211_hwsim: unregister radios\n");
7608 
7609 	hwsim_unregister_virtio_driver();
7610 	hwsim_exit_netlink();
7611 
7612 	mac80211_hwsim_free();
7613 
7614 	rhashtable_destroy(&hwsim_radios_rht);
7615 	unregister_netdev(hwsim_mon);
7616 	platform_driver_unregister(&mac80211_hwsim_driver);
7617 	unregister_pernet_device(&hwsim_net_ops);
7618 }
7619 module_exit(exit_mac80211_hwsim);
7620