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