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