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