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