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