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