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