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