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