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