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