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