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