blob: 12b727ba9dec6b973298743404d00e1da0f7aa4a [file] [log] [blame]
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001/*
2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
Javier Lopez78825132011-06-01 11:26:13 +02004 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03005 *
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:
Johannes Berg2b2d7792010-08-17 12:08:07 +020013 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030015 * - RX filtering based on filter configuration (data->rx_filter)
16 */
17
Johannes Berg0e057d732008-09-12 00:39:22 +020018#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Johannes Berg0e057d732008-09-12 00:39:22 +020020#include <linux/spinlock.h>
Johannes Berg90e30122009-06-18 14:51:12 +020021#include <net/dst.h>
22#include <net/xfrm.h>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030023#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>
Martin Pitt9ea927742013-04-08 11:30:01 +020028#include <linux/platform_device.h>
Jouni Malinenfc6971d2008-10-30 19:59:05 +020029#include <linux/debugfs.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040030#include <linux/module.h>
Javier Cardona2f40b942012-03-05 17:20:37 -080031#include <linux/ktime.h>
Javier Lopez78825132011-06-01 11:26:13 +020032#include <net/genetlink.h>
Martin Willi100cb9f2016-05-09 18:33:59 +020033#include <net/net_namespace.h>
34#include <net/netns/generic.h>
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +010035#include <linux/rhashtable.h>
Javier Lopez78825132011-06-01 11:26:13 +020036#include "mac80211_hwsim.h"
37
38#define WARN_QUEUE 100
39#define MAX_QUEUE 200
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030040
41MODULE_AUTHOR("Jouni Malinen");
42MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
43MODULE_LICENSE("GPL");
44
45static int radios = 2;
46module_param(radios, int, 0444);
47MODULE_PARM_DESC(radios, "Number of simulated radios");
48
Johannes Berge8261172012-07-27 19:48:26 +020049static int channels = 1;
50module_param(channels, int, 0444);
51MODULE_PARM_DESC(channels, "Number of concurrent channels");
Johannes Berg69068032010-02-03 10:47:55 +010052
Johannes Berga357d7f2012-12-10 11:57:42 +010053static bool paged_rx = false;
54module_param(paged_rx, bool, 0644);
55MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
56
Karl Beldan1eb32172013-04-19 14:44:52 +020057static bool rctbl = false;
58module_param(rctbl, bool, 0444);
59MODULE_PARM_DESC(rctbl, "Handle rate control table");
60
Johannes Berg8c66a3d2014-01-10 20:07:49 +010061static bool support_p2p_device = true;
62module_param(support_p2p_device, bool, 0444);
63MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
64
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040065/**
66 * enum hwsim_regtest - the type of regulatory tests we offer
67 *
68 * These are the different values you can use for the regtest
69 * module parameter. This is useful to help test world roaming
70 * and the driver regulatory_hint() call and combinations of these.
71 * If you want to do specific alpha2 regulatory domain tests simply
72 * use the userspace regulatory request as that will be respected as
73 * well without the need of this module parameter. This is designed
74 * only for testing the driver regulatory request, world roaming
75 * and all possible combinations.
76 *
77 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
78 * this is the default value.
79 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
80 * hint, only one driver regulatory hint will be sent as such the
81 * secondary radios are expected to follow.
82 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
83 * request with all radios reporting the same regulatory domain.
84 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
85 * different regulatory domains requests. Expected behaviour is for
86 * an intersection to occur but each device will still use their
87 * respective regulatory requested domains. Subsequent radios will
88 * use the resulting intersection.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030089 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040090 * this by using a custom beacon-capable regulatory domain for the first
91 * radio. All other device world roam.
92 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
93 * domain requests. All radios will adhere to this custom world regulatory
94 * domain.
95 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
96 * domain requests. The first radio will adhere to the first custom world
97 * regulatory domain, the second one to the second custom world regulatory
98 * domain. All other devices will world roam.
99 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
100 * settings, only the first radio will send a regulatory domain request
101 * and use strict settings. The rest of the radios are expected to follow.
102 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
103 * settings. All radios will adhere to this.
104 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
105 * domain settings, combined with secondary driver regulatory domain
106 * settings. The first radio will get a strict regulatory domain setting
107 * using the first driver regulatory request and the second radio will use
108 * non-strict settings using the second driver regulatory request. All
109 * other devices should follow the intersection created between the
110 * first two.
111 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
112 * at least 6 radios for a complete test. We will test in this order:
113 * 1 - driver custom world regulatory domain
114 * 2 - second custom world regulatory domain
115 * 3 - first driver regulatory domain request
116 * 4 - second driver regulatory domain request
117 * 5 - strict regulatory domain settings using the third driver regulatory
118 * domain request
119 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
120 * regulatory requests.
121 */
122enum hwsim_regtest {
123 HWSIM_REGTEST_DISABLED = 0,
124 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
125 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
126 HWSIM_REGTEST_DIFF_COUNTRY = 3,
127 HWSIM_REGTEST_WORLD_ROAM = 4,
128 HWSIM_REGTEST_CUSTOM_WORLD = 5,
129 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
130 HWSIM_REGTEST_STRICT_FOLLOW = 7,
131 HWSIM_REGTEST_STRICT_ALL = 8,
132 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
133 HWSIM_REGTEST_ALL = 10,
134};
135
136/* Set to one of the HWSIM_REGTEST_* values above */
137static int regtest = HWSIM_REGTEST_DISABLED;
138module_param(regtest, int, 0444);
139MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
140
141static const char *hwsim_alpha2s[] = {
142 "FI",
143 "AL",
144 "US",
145 "DE",
146 "JP",
147 "AL",
148};
149
150static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
151 .n_reg_rules = 4,
152 .alpha2 = "99",
153 .reg_rules = {
154 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
155 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
156 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
157 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
158 }
159};
160
161static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
162 .n_reg_rules = 2,
163 .alpha2 = "99",
164 .reg_rules = {
165 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
166 REG_RULE(5725-10, 5850+10, 40, 0, 30,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200167 NL80211_RRF_NO_IR),
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -0400168 }
169};
170
Johannes Berg26b0e412014-01-10 13:37:38 +0100171static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
172 &hwsim_world_regdom_custom_01,
173 &hwsim_world_regdom_custom_02,
174};
175
Johannes Berg8aa21e62008-09-11 02:16:36 +0200176struct hwsim_vif_priv {
177 u32 magic;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200178 u8 bssid[ETH_ALEN];
179 bool assoc;
Jouni Malinen0bb861e2013-10-22 14:11:17 +0300180 bool bcn_en;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200181 u16 aid;
Johannes Berg8aa21e62008-09-11 02:16:36 +0200182};
183
184#define HWSIM_VIF_MAGIC 0x69537748
185
186static inline void hwsim_check_magic(struct ieee80211_vif *vif)
187{
188 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berge8261172012-07-27 19:48:26 +0200189 WARN(vp->magic != HWSIM_VIF_MAGIC,
190 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
191 vif, vp->magic, vif->addr, vif->type, vif->p2p);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200192}
193
194static inline void hwsim_set_magic(struct ieee80211_vif *vif)
195{
196 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
197 vp->magic = HWSIM_VIF_MAGIC;
198}
199
200static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
201{
202 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
203 vp->magic = 0;
204}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300205
Johannes Berg81c06522008-09-11 02:17:01 +0200206struct hwsim_sta_priv {
207 u32 magic;
208};
209
Johannes Berge8261172012-07-27 19:48:26 +0200210#define HWSIM_STA_MAGIC 0x6d537749
Johannes Berg81c06522008-09-11 02:17:01 +0200211
212static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
213{
214 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200215 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
Johannes Berg81c06522008-09-11 02:17:01 +0200216}
217
218static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
219{
220 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200221 sp->magic = HWSIM_STA_MAGIC;
Johannes Berg81c06522008-09-11 02:17:01 +0200222}
223
224static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
225{
226 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
227 sp->magic = 0;
228}
229
Johannes Berge8261172012-07-27 19:48:26 +0200230struct hwsim_chanctx_priv {
231 u32 magic;
232};
233
234#define HWSIM_CHANCTX_MAGIC 0x6d53774a
235
236static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
237{
238 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
239 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
240}
241
242static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
243{
244 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
245 cp->magic = HWSIM_CHANCTX_MAGIC;
246}
247
248static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
249{
250 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
251 cp->magic = 0;
252}
253
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300254static unsigned int hwsim_net_id;
Martin Willi100cb9f2016-05-09 18:33:59 +0200255
256static int hwsim_netgroup;
257
258struct hwsim_net {
259 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200260 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200261};
262
263static 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
270static 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
Martin Willif21e4d82016-05-14 10:34:44 +0200277static inline u32 hwsim_net_get_wmediumd(struct net *net)
278{
279 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
280
281 return hwsim_net->wmediumd;
282}
283
284static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
285{
286 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
287
288 hwsim_net->wmediumd = portid;
289}
290
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300291static struct class *hwsim_class;
292
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300293static struct net_device *hwsim_mon; /* global monitor netdev */
294
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800295#define CHAN2G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200296 .band = NL80211_BAND_2GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800297 .center_freq = (_freq), \
298 .hw_value = (_freq), \
299 .max_power = 20, \
300}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300301
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800302#define CHAN5G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200303 .band = NL80211_BAND_5GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800304 .center_freq = (_freq), \
305 .hw_value = (_freq), \
306 .max_power = 20, \
307}
308
309static const struct ieee80211_channel hwsim_channels_2ghz[] = {
310 CHAN2G(2412), /* Channel 1 */
311 CHAN2G(2417), /* Channel 2 */
312 CHAN2G(2422), /* Channel 3 */
313 CHAN2G(2427), /* Channel 4 */
314 CHAN2G(2432), /* Channel 5 */
315 CHAN2G(2437), /* Channel 6 */
316 CHAN2G(2442), /* Channel 7 */
317 CHAN2G(2447), /* Channel 8 */
318 CHAN2G(2452), /* Channel 9 */
319 CHAN2G(2457), /* Channel 10 */
320 CHAN2G(2462), /* Channel 11 */
321 CHAN2G(2467), /* Channel 12 */
322 CHAN2G(2472), /* Channel 13 */
323 CHAN2G(2484), /* Channel 14 */
324};
325
326static const struct ieee80211_channel hwsim_channels_5ghz[] = {
327 CHAN5G(5180), /* Channel 36 */
328 CHAN5G(5200), /* Channel 40 */
329 CHAN5G(5220), /* Channel 44 */
330 CHAN5G(5240), /* Channel 48 */
331
332 CHAN5G(5260), /* Channel 52 */
333 CHAN5G(5280), /* Channel 56 */
334 CHAN5G(5300), /* Channel 60 */
335 CHAN5G(5320), /* Channel 64 */
336
337 CHAN5G(5500), /* Channel 100 */
338 CHAN5G(5520), /* Channel 104 */
339 CHAN5G(5540), /* Channel 108 */
340 CHAN5G(5560), /* Channel 112 */
341 CHAN5G(5580), /* Channel 116 */
342 CHAN5G(5600), /* Channel 120 */
343 CHAN5G(5620), /* Channel 124 */
344 CHAN5G(5640), /* Channel 128 */
345 CHAN5G(5660), /* Channel 132 */
346 CHAN5G(5680), /* Channel 136 */
347 CHAN5G(5700), /* Channel 140 */
348
349 CHAN5G(5745), /* Channel 149 */
350 CHAN5G(5765), /* Channel 153 */
351 CHAN5G(5785), /* Channel 157 */
352 CHAN5G(5805), /* Channel 161 */
353 CHAN5G(5825), /* Channel 165 */
Jouni Malinen85bbd802017-03-06 11:56:06 +0200354 CHAN5G(5845), /* Channel 169 */
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300355};
356
357static const struct ieee80211_rate hwsim_rates[] = {
358 { .bitrate = 10 },
359 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
360 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
361 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
362 { .bitrate = 60 },
363 { .bitrate = 90 },
364 { .bitrate = 120 },
365 { .bitrate = 180 },
366 { .bitrate = 240 },
367 { .bitrate = 360 },
368 { .bitrate = 480 },
369 { .bitrate = 540 }
370};
371
Jouni Malinend5d011b2015-02-26 15:26:54 +0200372#define OUI_QCA 0x001374
373#define QCA_NL80211_SUBCMD_TEST 1
374enum qca_nl80211_vendor_subcmds {
375 QCA_WLAN_VENDOR_ATTR_TEST = 8,
376 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
377};
378
379static const struct nla_policy
380hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
381 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
382};
383
384static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
385 struct wireless_dev *wdev,
386 const void *data, int data_len)
387{
388 struct sk_buff *skb;
389 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
390 int err;
391 u32 val;
392
393 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
Johannes Bergfceb6432017-04-12 14:34:07 +0200394 hwsim_vendor_test_policy, NULL);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200395 if (err)
396 return err;
397 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
398 return -EINVAL;
399 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200400 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200401
402 /* Send a vendor event as a test. Note that this would not normally be
403 * done within a command handler, but rather, based on some other
404 * trigger. For simplicity, this command is used to trigger the event
405 * here.
406 *
407 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
408 */
409 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
410 if (skb) {
411 /* skb_put() or nla_put() will fill up data within
412 * NL80211_ATTR_VENDOR_DATA.
413 */
414
415 /* Add vendor data */
416 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
417
418 /* Send the event - this will call nla_nest_end() */
419 cfg80211_vendor_event(skb, GFP_KERNEL);
420 }
421
422 /* Send a response to the command */
423 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
424 if (!skb)
425 return -ENOMEM;
426
427 /* skb_put() or nla_put() will fill up data within
428 * NL80211_ATTR_VENDOR_DATA
429 */
430 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
431
432 return cfg80211_vendor_cmd_reply(skb);
433}
434
435static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
436 {
437 .info = { .vendor_id = OUI_QCA,
438 .subcmd = QCA_NL80211_SUBCMD_TEST },
439 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
440 .doit = mac80211_hwsim_vendor_cmd_test,
441 }
442};
443
444/* Advertise support vendor specific events */
445static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
446 { .vendor_id = OUI_QCA, .subcmd = 1 },
447};
448
Johannes Bergde0421d2014-01-06 23:12:12 +0100449static const struct ieee80211_iface_limit hwsim_if_limits[] = {
450 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
451 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
452 BIT(NL80211_IFTYPE_P2P_CLIENT) |
453#ifdef CONFIG_MAC80211_MESH
454 BIT(NL80211_IFTYPE_MESH_POINT) |
455#endif
456 BIT(NL80211_IFTYPE_AP) |
457 BIT(NL80211_IFTYPE_P2P_GO) },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100458 /* must be last, see hwsim_if_comb */
459 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
Johannes Bergde0421d2014-01-06 23:12:12 +0100460};
461
Johannes Bergde0421d2014-01-06 23:12:12 +0100462static const struct ieee80211_iface_combination hwsim_if_comb[] = {
463 {
464 .limits = hwsim_if_limits,
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100465 /* remove the last entry which is P2P_DEVICE */
466 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
467 .max_interfaces = 2048,
468 .num_different_channels = 1,
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100469 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
470 BIT(NL80211_CHAN_WIDTH_20) |
471 BIT(NL80211_CHAN_WIDTH_40) |
472 BIT(NL80211_CHAN_WIDTH_80) |
473 BIT(NL80211_CHAN_WIDTH_160),
Ilan Peerf7736f52016-07-05 15:23:09 +0300474 },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100475};
476
477static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
478 {
479 .limits = hwsim_if_limits,
Johannes Bergde0421d2014-01-06 23:12:12 +0100480 .n_limits = ARRAY_SIZE(hwsim_if_limits),
481 .max_interfaces = 2048,
482 .num_different_channels = 1,
Johannes Bergde0421d2014-01-06 23:12:12 +0100483 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
484 BIT(NL80211_CHAN_WIDTH_20) |
485 BIT(NL80211_CHAN_WIDTH_40) |
486 BIT(NL80211_CHAN_WIDTH_80) |
487 BIT(NL80211_CHAN_WIDTH_160),
Ilan Peerf7736f52016-07-05 15:23:09 +0300488 },
Johannes Bergde0421d2014-01-06 23:12:12 +0100489};
490
Johannes Berg0e057d732008-09-12 00:39:22 +0200491static spinlock_t hwsim_radio_lock;
Johannes Bergb59abfb2016-09-15 16:30:03 +0300492static LIST_HEAD(hwsim_radios);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +0100493static struct workqueue_struct *hwsim_wq;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100494static struct rhashtable hwsim_radios_rht;
Johannes Berg2d689922014-01-06 22:56:30 +0100495static int hwsim_radio_idx;
Johannes Berg0e057d732008-09-12 00:39:22 +0200496
Johannes Bergde0421d2014-01-06 23:12:12 +0100497static struct platform_driver mac80211_hwsim_driver = {
498 .driver = {
499 .name = "mac80211_hwsim",
Johannes Bergde0421d2014-01-06 23:12:12 +0100500 },
501};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300502
503struct mac80211_hwsim_data {
Johannes Berg0e057d732008-09-12 00:39:22 +0200504 struct list_head list;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100505 struct rhash_head rht;
Johannes Berg0e057d732008-09-12 00:39:22 +0200506 struct ieee80211_hw *hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300507 struct device *dev;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200508 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800509 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
510 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300511 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
Johannes Berg38ed5042014-01-06 22:11:06 +0100512 struct ieee80211_iface_combination if_combination;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300513
Johannes Bergef15aac2010-01-20 12:02:33 +0100514 struct mac_address addresses[2];
Johannes Bergbc791092014-01-06 23:29:20 +0100515 int channels, idx;
Luciano Coelho361c3e02014-02-10 15:23:03 +0200516 bool use_chanctx;
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300517 bool destroy_on_close;
518 struct work_struct destroy_work;
519 u32 portid;
Patrik Flykt93d638d2014-11-12 16:42:40 +0200520 char alpha2[2];
521 const struct ieee80211_regdomain *regd;
Johannes Bergef15aac2010-01-20 12:02:33 +0100522
Johannes Berge8261172012-07-27 19:48:26 +0200523 struct ieee80211_channel *tmp_chan;
Johannes Berg661ef472015-11-24 18:29:45 +0100524 struct ieee80211_channel *roc_chan;
525 u32 roc_duration;
526 struct delayed_work roc_start;
Johannes Berge8261172012-07-27 19:48:26 +0200527 struct delayed_work roc_done;
528 struct delayed_work hw_scan;
529 struct cfg80211_scan_request *hw_scan_request;
530 struct ieee80211_vif *hw_scan_vif;
531 int scan_chan_idx;
Johannes Berg339467b2014-06-12 22:41:00 +0200532 u8 scan_addr[ETH_ALEN];
Johannes Berg7f813ce2013-11-06 10:37:34 +0100533 struct {
534 struct ieee80211_channel *channel;
535 unsigned long next_start, start, end;
536 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
537 ARRAY_SIZE(hwsim_channels_5ghz)];
Johannes Berge8261172012-07-27 19:48:26 +0200538
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300539 struct ieee80211_channel *channel;
Thomas Pedersen01e59e42013-01-02 14:55:17 -0800540 u64 beacon_int /* beacon interval in us */;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300541 unsigned int rx_filter;
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -0400542 bool started, idle, scanning;
543 struct mutex mutex;
Thomas Pedersen01e59e42013-01-02 14:55:17 -0800544 struct tasklet_hrtimer beacon_timer;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200545 enum ps_mode {
546 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
547 } ps;
548 bool ps_poll_pending;
549 struct dentry *debugfs;
Daniel Wagner73606d02009-05-18 19:49:25 +0200550
Johannes Berg959eb2f2015-11-06 11:57:23 +0100551 uintptr_t pending_cookie;
Javier Lopez78825132011-06-01 11:26:13 +0200552 struct sk_buff_head pending; /* packets pending */
Daniel Wagner73606d02009-05-18 19:49:25 +0200553 /*
554 * Only radios in the same group can communicate together (the
555 * channel has to match too). Each bit represents a group. A
Ben Greear9ebac152014-09-25 14:22:20 -0700556 * radio can be in more than one group.
Daniel Wagner73606d02009-05-18 19:49:25 +0200557 */
558 u64 group;
Blaise Gassendbdd7bd12010-10-28 02:01:24 -0700559
Martin Willi100cb9f2016-05-09 18:33:59 +0200560 /* group shared by radios created in the same netns */
561 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200562 /* wmediumd portid responsible for netgroup of this radio */
563 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200564
Javier Cardona2f40b942012-03-05 17:20:37 -0800565 /* difference between this hw's clock and the real clock, in usecs */
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800566 s64 tsf_offset;
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800567 s64 bcn_delta;
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800568 /* absolute beacon transmission time. Used to cover up "tx" delay. */
569 u64 abs_bcn_ts;
Ben Greear2155c3f2014-10-22 12:22:58 -0700570
571 /* Stats */
572 u64 tx_pkts;
573 u64 rx_pkts;
574 u64 tx_bytes;
575 u64 rx_bytes;
576 u64 tx_dropped;
577 u64 tx_failed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300578};
579
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100580static const struct rhashtable_params hwsim_rht_params = {
581 .nelem_hint = 2,
582 .automatic_shrinking = true,
583 .key_len = ETH_ALEN,
584 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
585 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
586};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300587
588struct hwsim_radiotap_hdr {
589 struct ieee80211_radiotap_header hdr;
Javier Cardona2f40b942012-03-05 17:20:37 -0800590 __le64 rt_tsft;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300591 u8 rt_flags;
592 u8 rt_rate;
593 __le16 rt_channel;
594 __le16 rt_chbitmask;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000595} __packed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300596
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200597struct hwsim_radiotap_ack_hdr {
598 struct ieee80211_radiotap_header hdr;
599 u8 rt_flags;
600 u8 pad;
601 __le16 rt_channel;
602 __le16 rt_chbitmask;
603} __packed;
604
Johannes Berg489111e2016-10-24 14:40:03 +0200605/* MAC80211_HWSIM netlink family */
606static struct genl_family hwsim_genl_family;
Javier Lopez78825132011-06-01 11:26:13 +0200607
Jukka Rissanen62759362014-10-31 14:48:47 +0200608enum hwsim_multicast_groups {
609 HWSIM_MCGRP_CONFIG,
610};
611
612static const struct genl_multicast_group hwsim_mcgrps[] = {
613 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
614};
615
Javier Lopez78825132011-06-01 11:26:13 +0200616/* MAC80211_HWSIM netlink policy */
617
Johannes Berg205d2422014-01-21 00:06:29 +0100618static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100619 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
620 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
Javier Lopez78825132011-06-01 11:26:13 +0200621 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
622 .len = IEEE80211_MAX_DATA_LEN },
623 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
624 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
625 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
626 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100627 .len = IEEE80211_TX_MAX_RATES *
628 sizeof(struct hwsim_tx_rate)},
Javier Lopez78825132011-06-01 11:26:13 +0200629 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
Johannes Bergbc791092014-01-06 23:29:20 +0100630 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
631 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
Johannes Berg26b0e412014-01-10 13:37:38 +0100632 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
633 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
634 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100635 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300636 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
Johannes Bergde29eda2014-10-31 09:25:43 +0100637 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
638 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
639 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +0100640 [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
Javier Lopez78825132011-06-01 11:26:13 +0200641};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300642
Johannes Bergde0421d2014-01-06 23:12:12 +0100643static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
644 struct sk_buff *skb,
645 struct ieee80211_channel *chan);
646
647/* sysfs attributes */
648static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
649{
650 struct mac80211_hwsim_data *data = dat;
651 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
652 struct sk_buff *skb;
653 struct ieee80211_pspoll *pspoll;
654
655 if (!vp->assoc)
656 return;
657
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200658 wiphy_dbg(data->hw->wiphy,
659 "%s: send PS-Poll to %pM for aid %d\n",
660 __func__, vp->bssid, vp->aid);
Johannes Bergde0421d2014-01-06 23:12:12 +0100661
662 skb = dev_alloc_skb(sizeof(*pspoll));
663 if (!skb)
664 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200665 pspoll = skb_put(skb, sizeof(*pspoll));
Johannes Bergde0421d2014-01-06 23:12:12 +0100666 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
667 IEEE80211_STYPE_PSPOLL |
668 IEEE80211_FCTL_PM);
669 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
670 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
671 memcpy(pspoll->ta, mac, ETH_ALEN);
672
673 rcu_read_lock();
674 mac80211_hwsim_tx_frame(data->hw, skb,
675 rcu_dereference(vif->chanctx_conf)->def.chan);
676 rcu_read_unlock();
677}
678
679static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
680 struct ieee80211_vif *vif, int ps)
681{
682 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
683 struct sk_buff *skb;
684 struct ieee80211_hdr *hdr;
685
686 if (!vp->assoc)
687 return;
688
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200689 wiphy_dbg(data->hw->wiphy,
690 "%s: send data::nullfunc to %pM ps=%d\n",
691 __func__, vp->bssid, ps);
Johannes Bergde0421d2014-01-06 23:12:12 +0100692
693 skb = dev_alloc_skb(sizeof(*hdr));
694 if (!skb)
695 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200696 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
Johannes Bergde0421d2014-01-06 23:12:12 +0100697 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
698 IEEE80211_STYPE_NULLFUNC |
Adiel Alonib65c7b82017-12-18 12:14:04 +0200699 IEEE80211_FCTL_TODS |
Johannes Bergde0421d2014-01-06 23:12:12 +0100700 (ps ? IEEE80211_FCTL_PM : 0));
701 hdr->duration_id = cpu_to_le16(0);
702 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
703 memcpy(hdr->addr2, mac, ETH_ALEN);
704 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
705
706 rcu_read_lock();
707 mac80211_hwsim_tx_frame(data->hw, skb,
708 rcu_dereference(vif->chanctx_conf)->def.chan);
709 rcu_read_unlock();
710}
711
712
713static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
714 struct ieee80211_vif *vif)
715{
716 struct mac80211_hwsim_data *data = dat;
717 hwsim_send_nullfunc(data, mac, vif, 1);
718}
719
720static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
721 struct ieee80211_vif *vif)
722{
723 struct mac80211_hwsim_data *data = dat;
724 hwsim_send_nullfunc(data, mac, vif, 0);
725}
726
727static int hwsim_fops_ps_read(void *dat, u64 *val)
728{
729 struct mac80211_hwsim_data *data = dat;
730 *val = data->ps;
731 return 0;
732}
733
734static int hwsim_fops_ps_write(void *dat, u64 val)
735{
736 struct mac80211_hwsim_data *data = dat;
737 enum ps_mode old_ps;
738
739 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
740 val != PS_MANUAL_POLL)
741 return -EINVAL;
742
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200743 if (val == PS_MANUAL_POLL) {
744 if (data->ps != PS_ENABLED)
745 return -EINVAL;
746 local_bh_disable();
747 ieee80211_iterate_active_interfaces_atomic(
748 data->hw, IEEE80211_IFACE_ITER_NORMAL,
749 hwsim_send_ps_poll, data);
750 local_bh_enable();
751 return 0;
752 }
Johannes Bergde0421d2014-01-06 23:12:12 +0100753 old_ps = data->ps;
754 data->ps = val;
755
Johannes Berga809ca52015-01-09 11:29:50 +0100756 local_bh_disable();
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200757 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100758 ieee80211_iterate_active_interfaces_atomic(
759 data->hw, IEEE80211_IFACE_ITER_NORMAL,
760 hwsim_send_nullfunc_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100761 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100762 ieee80211_iterate_active_interfaces_atomic(
763 data->hw, IEEE80211_IFACE_ITER_NORMAL,
764 hwsim_send_nullfunc_no_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100765 }
Johannes Berga809ca52015-01-09 11:29:50 +0100766 local_bh_enable();
Johannes Bergde0421d2014-01-06 23:12:12 +0100767
768 return 0;
769}
770
771DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
772 "%llu\n");
773
774static int hwsim_write_simulate_radar(void *dat, u64 val)
775{
776 struct mac80211_hwsim_data *data = dat;
777
778 ieee80211_radar_detected(data->hw);
779
780 return 0;
781}
782
783DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
784 hwsim_write_simulate_radar, "%llu\n");
785
786static int hwsim_fops_group_read(void *dat, u64 *val)
787{
788 struct mac80211_hwsim_data *data = dat;
789 *val = data->group;
790 return 0;
791}
792
793static int hwsim_fops_group_write(void *dat, u64 val)
794{
795 struct mac80211_hwsim_data *data = dat;
796 data->group = val;
797 return 0;
798}
799
800DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
801 hwsim_fops_group_read, hwsim_fops_group_write,
802 "%llx\n");
803
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000804static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
805 struct net_device *dev)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300806{
807 /* TODO: allow packet injection */
808 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000809 return NETDEV_TX_OK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300810}
811
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800812static inline u64 mac80211_hwsim_get_tsf_raw(void)
813{
814 return ktime_to_us(ktime_get_real());
815}
816
Javier Cardona2f40b942012-03-05 17:20:37 -0800817static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
818{
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800819 u64 now = mac80211_hwsim_get_tsf_raw();
Javier Cardona2f40b942012-03-05 17:20:37 -0800820 return cpu_to_le64(now + data->tsf_offset);
821}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300822
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800823static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800824 struct ieee80211_vif *vif)
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800825{
826 struct mac80211_hwsim_data *data = hw->priv;
827 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
828}
829
830static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
831 struct ieee80211_vif *vif, u64 tsf)
832{
833 struct mac80211_hwsim_data *data = hw->priv;
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800834 u64 now = mac80211_hwsim_get_tsf(hw, vif);
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800835 u32 bcn_int = data->beacon_int;
Andrew Morton79211c82015-11-09 14:58:13 -0800836 u64 delta = abs(tsf - now);
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800837
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800838 /* adjust after beaconing with new timestamp at old TBTT */
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700839 if (tsf > now) {
840 data->tsf_offset += delta;
841 data->bcn_delta = do_div(delta, bcn_int);
842 } else {
843 data->tsf_offset -= delta;
Benjamin Beichler4fb7f8a2016-11-11 17:37:56 +0100844 data->bcn_delta = -(s64)do_div(delta, bcn_int);
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700845 }
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800846}
847
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300848static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +0200849 struct sk_buff *tx_skb,
850 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300851{
852 struct mac80211_hwsim_data *data = hw->priv;
853 struct sk_buff *skb;
854 struct hwsim_radiotap_hdr *hdr;
855 u16 flags;
856 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
857 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
858
Amit Khatri89f44d82015-11-30 12:46:52 +0530859 if (WARN_ON(!txrate))
860 return;
861
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300862 if (!netif_running(hwsim_mon))
863 return;
864
865 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
866 if (skb == NULL)
867 return;
868
Johannes Bergd58ff352017-06-16 14:29:23 +0200869 hdr = skb_push(skb, sizeof(*hdr));
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300870 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
871 hdr->hdr.it_pad = 0;
872 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
Jouni Malinenf248f102008-06-13 19:44:47 +0300873 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
874 (1 << IEEE80211_RADIOTAP_RATE) |
Javier Cardona2f40b942012-03-05 17:20:37 -0800875 (1 << IEEE80211_RADIOTAP_TSFT) |
Jouni Malinenf248f102008-06-13 19:44:47 +0300876 (1 << IEEE80211_RADIOTAP_CHANNEL));
Javier Cardona2f40b942012-03-05 17:20:37 -0800877 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300878 hdr->rt_flags = 0;
879 hdr->rt_rate = txrate->bitrate / 5;
Johannes Berge8261172012-07-27 19:48:26 +0200880 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300881 flags = IEEE80211_CHAN_2GHZ;
882 if (txrate->flags & IEEE80211_RATE_ERP_G)
883 flags |= IEEE80211_CHAN_OFDM;
884 else
885 flags |= IEEE80211_CHAN_CCK;
886 hdr->rt_chbitmask = cpu_to_le16(flags);
887
888 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000889 skb_reset_mac_header(skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300890 skb->ip_summed = CHECKSUM_UNNECESSARY;
891 skb->pkt_type = PACKET_OTHERHOST;
Jouni Malinenf248f102008-06-13 19:44:47 +0300892 skb->protocol = htons(ETH_P_802_2);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300893 memset(skb->cb, 0, sizeof(skb->cb));
894 netif_rx(skb);
895}
896
897
Johannes Berge8261172012-07-27 19:48:26 +0200898static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
899 const u8 *addr)
Jouni Malinen6c085222009-11-01 11:31:45 +0200900{
Jouni Malinen6c085222009-11-01 11:31:45 +0200901 struct sk_buff *skb;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200902 struct hwsim_radiotap_ack_hdr *hdr;
Jouni Malinen6c085222009-11-01 11:31:45 +0200903 u16 flags;
904 struct ieee80211_hdr *hdr11;
905
906 if (!netif_running(hwsim_mon))
907 return;
908
909 skb = dev_alloc_skb(100);
910 if (skb == NULL)
911 return;
912
Johannes Berg4df864c2017-06-16 14:29:21 +0200913 hdr = skb_put(skb, sizeof(*hdr));
Jouni Malinen6c085222009-11-01 11:31:45 +0200914 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
915 hdr->hdr.it_pad = 0;
916 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
917 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
918 (1 << IEEE80211_RADIOTAP_CHANNEL));
919 hdr->rt_flags = 0;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200920 hdr->pad = 0;
Johannes Berge8261172012-07-27 19:48:26 +0200921 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinen6c085222009-11-01 11:31:45 +0200922 flags = IEEE80211_CHAN_2GHZ;
923 hdr->rt_chbitmask = cpu_to_le16(flags);
924
Johannes Berg4df864c2017-06-16 14:29:21 +0200925 hdr11 = skb_put(skb, 10);
Jouni Malinen6c085222009-11-01 11:31:45 +0200926 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
927 IEEE80211_STYPE_ACK);
928 hdr11->duration_id = cpu_to_le16(0);
929 memcpy(hdr11->addr1, addr, ETH_ALEN);
930
931 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000932 skb_reset_mac_header(skb);
Jouni Malinen6c085222009-11-01 11:31:45 +0200933 skb->ip_summed = CHECKSUM_UNNECESSARY;
934 skb->pkt_type = PACKET_OTHERHOST;
935 skb->protocol = htons(ETH_P_802_2);
936 memset(skb->cb, 0, sizeof(skb->cb));
937 netif_rx(skb);
938}
939
Johannes Berg3283e2862014-06-12 22:34:49 +0200940struct mac80211_hwsim_addr_match_data {
941 u8 addr[ETH_ALEN];
942 bool ret;
943};
944
945static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
946 struct ieee80211_vif *vif)
947{
948 struct mac80211_hwsim_addr_match_data *md = data;
949
950 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
951 md->ret = true;
952}
953
954static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
955 const u8 *addr)
956{
957 struct mac80211_hwsim_addr_match_data md = {
958 .ret = false,
959 };
960
Johannes Berg339467b2014-06-12 22:41:00 +0200961 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
962 return true;
963
Johannes Berg3283e2862014-06-12 22:34:49 +0200964 memcpy(md.addr, addr, ETH_ALEN);
965
966 ieee80211_iterate_active_interfaces_atomic(data->hw,
967 IEEE80211_IFACE_ITER_NORMAL,
968 mac80211_hwsim_addr_iter,
969 &md);
970
971 return md.ret;
972}
Jouni Malinen6c085222009-11-01 11:31:45 +0200973
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200974static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
975 struct sk_buff *skb)
976{
977 switch (data->ps) {
978 case PS_DISABLED:
979 return true;
980 case PS_ENABLED:
981 return false;
982 case PS_AUTO_POLL:
983 /* TODO: accept (some) Beacons by default and other frames only
984 * if pending PS-Poll has been sent */
985 return true;
986 case PS_MANUAL_POLL:
987 /* Allow unicast frames to own address if there is a pending
988 * PS-Poll */
989 if (data->ps_poll_pending &&
Johannes Berg3283e2862014-06-12 22:34:49 +0200990 mac80211_hwsim_addr_match(data, skb->data + 4)) {
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200991 data->ps_poll_pending = false;
992 return true;
993 }
994 return false;
995 }
996
997 return true;
998}
999
Martin Willif21e4d82016-05-14 10:34:44 +02001000static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1001 struct sk_buff *skb, int portid)
1002{
1003 struct net *net;
1004 bool found = false;
1005 int res = -ENOENT;
1006
1007 rcu_read_lock();
1008 for_each_net_rcu(net) {
1009 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1010 res = genlmsg_unicast(net, skb, portid);
1011 found = true;
1012 break;
1013 }
1014 }
1015 rcu_read_unlock();
1016
1017 if (!found)
1018 nlmsg_free(skb);
1019
1020 return res;
1021}
1022
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001023static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1024{
1025 u16 result = 0;
1026
1027 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1028 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1029 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1030 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1031 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1032 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1033 if (rate->flags & IEEE80211_TX_RC_MCS)
1034 result |= MAC80211_HWSIM_TX_RC_MCS;
1035 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1036 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1037 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1038 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1039 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1040 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1041 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1042 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1043 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1044 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1045 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1046 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1047 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1048 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1049
1050 return result;
1051}
1052
Javier Lopez78825132011-06-01 11:26:13 +02001053static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1054 struct sk_buff *my_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001055 int dst_portid)
Javier Lopez78825132011-06-01 11:26:13 +02001056{
1057 struct sk_buff *skb;
1058 struct mac80211_hwsim_data *data = hw->priv;
1059 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1060 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1061 void *msg_head;
1062 unsigned int hwsim_flags = 0;
1063 int i;
1064 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001065 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
Johannes Berg959eb2f2015-11-06 11:57:23 +01001066 uintptr_t cookie;
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001067
Javier Lopez78825132011-06-01 11:26:13 +02001068 if (data->ps != PS_DISABLED)
1069 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1070 /* If the queue contains MAX_QUEUE skb's drop some */
1071 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1072 /* Droping until WARN_QUEUE level */
Ben Greear2155c3f2014-10-22 12:22:58 -07001073 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
Ben Greearb7bc9672014-09-25 14:22:18 -07001074 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
Ben Greear2155c3f2014-10-22 12:22:58 -07001075 data->tx_dropped++;
1076 }
Javier Lopez78825132011-06-01 11:26:13 +02001077 }
1078
Thomas Graf58050fc2012-06-28 03:57:45 +00001079 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Javier Lopez78825132011-06-01 11:26:13 +02001080 if (skb == NULL)
1081 goto nla_put_failure;
1082
1083 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1084 HWSIM_CMD_FRAME);
1085 if (msg_head == NULL) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001086 pr_debug("mac80211_hwsim: problem with msg_head\n");
Javier Lopez78825132011-06-01 11:26:13 +02001087 goto nla_put_failure;
1088 }
1089
Bob Copeland354210f2015-12-19 09:59:37 -05001090 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1091 ETH_ALEN, data->addresses[1].addr))
David S. Miller633c9382012-04-01 21:03:30 -04001092 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001093
1094 /* We get the skb->data */
David S. Miller633c9382012-04-01 21:03:30 -04001095 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1096 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001097
1098 /* We get the flags for this transmission, and we translate them to
1099 wmediumd flags */
1100
1101 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1102 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1103
1104 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1105 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1106
David S. Miller633c9382012-04-01 21:03:30 -04001107 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1108 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001109
Ben Greear4f86ed82014-10-27 15:04:46 -07001110 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1111 goto nla_put_failure;
1112
Javier Lopez78825132011-06-01 11:26:13 +02001113 /* We get the tx control (rate and retries) info*/
1114
1115 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1116 tx_attempts[i].idx = info->status.rates[i].idx;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001117 tx_attempts_flags[i].idx = info->status.rates[i].idx;
Javier Lopez78825132011-06-01 11:26:13 +02001118 tx_attempts[i].count = info->status.rates[i].count;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001119 tx_attempts_flags[i].flags =
1120 trans_tx_rate_flags_ieee2hwsim(
1121 &info->status.rates[i]);
Javier Lopez78825132011-06-01 11:26:13 +02001122 }
1123
David S. Miller633c9382012-04-01 21:03:30 -04001124 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1125 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1126 tx_attempts))
1127 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001128
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001129 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1130 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1131 tx_attempts_flags))
1132 goto nla_put_failure;
1133
Javier Lopez78825132011-06-01 11:26:13 +02001134 /* We create a cookie to identify this skb */
Johannes Berg959eb2f2015-11-06 11:57:23 +01001135 data->pending_cookie++;
1136 cookie = data->pending_cookie;
1137 info->rate_driver_data[0] = (void *)cookie;
Nicolas Dichtel08f4cbb2016-04-26 10:06:12 +02001138 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
David S. Miller633c9382012-04-01 21:03:30 -04001139 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001140
1141 genlmsg_end(skb, msg_head);
Martin Willif21e4d82016-05-14 10:34:44 +02001142 if (hwsim_unicast_netgroup(data, skb, dst_portid))
Bob Copelandf2831e22015-02-05 08:45:43 -05001143 goto err_free_txskb;
Javier Lopez78825132011-06-01 11:26:13 +02001144
1145 /* Enqueue the packet */
1146 skb_queue_tail(&data->pending, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001147 data->tx_pkts++;
1148 data->tx_bytes += my_skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02001149 return;
1150
1151nla_put_failure:
Bob Copelandf2831e22015-02-05 08:45:43 -05001152 nlmsg_free(skb);
1153err_free_txskb:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001154 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Ben Greearb7bc9672014-09-25 14:22:18 -07001155 ieee80211_free_txskb(hw, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001156 data->tx_failed++;
Javier Lopez78825132011-06-01 11:26:13 +02001157}
1158
Johannes Berge8261172012-07-27 19:48:26 +02001159static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1160 struct ieee80211_channel *c2)
1161{
1162 if (!c1 || !c2)
1163 return false;
1164
1165 return c1->center_freq == c2->center_freq;
1166}
1167
1168struct tx_iter_data {
1169 struct ieee80211_channel *channel;
1170 bool receive;
1171};
1172
1173static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1174 struct ieee80211_vif *vif)
1175{
1176 struct tx_iter_data *data = _data;
1177
1178 if (!vif->chanctx_conf)
1179 return;
1180
1181 if (!hwsim_chans_compat(data->channel,
Johannes Berg4bf88532012-11-09 11:39:59 +01001182 rcu_dereference(vif->chanctx_conf)->def.chan))
Johannes Berge8261172012-07-27 19:48:26 +02001183 return;
1184
1185 data->receive = true;
1186}
1187
Johannes Berg1f7bba72014-11-06 22:56:36 +01001188static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1189{
1190 /*
1191 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1192 * e.g. like this:
1193 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1194 * (but you should use a valid OUI, not that)
1195 *
1196 * If anyone wants to 'donate' a radiotap OUI/subns code
1197 * please send a patch removing this #ifdef and changing
1198 * the values accordingly.
1199 */
1200#ifdef HWSIM_RADIOTAP_OUI
1201 struct ieee80211_vendor_radiotap *rtap;
1202
1203 /*
1204 * Note that this code requires the headroom in the SKB
1205 * that was allocated earlier.
1206 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001207 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
Johannes Berg1f7bba72014-11-06 22:56:36 +01001208 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1209 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1210 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1211 rtap->subns = 127;
1212
1213 /*
1214 * Radiotap vendor namespaces can (and should) also be
1215 * split into fields by using the standard radiotap
1216 * presence bitmap mechanism. Use just BIT(0) here for
1217 * the presence bitmap.
1218 */
1219 rtap->present = BIT(0);
1220 /* We have 8 bytes of (dummy) data */
1221 rtap->len = 8;
1222 /* For testing, also require it to be aligned */
1223 rtap->align = 8;
1224 /* And also test that padding works, 4 bytes */
1225 rtap->pad = 4;
1226 /* push the data */
1227 memcpy(rtap->data, "ABCDEFGH", 8);
1228 /* make sure to clear padding, mac80211 doesn't */
1229 memset(rtap->data + 8, 0, 4);
1230
1231 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1232#endif
1233}
1234
Javier Lopez78825132011-06-01 11:26:13 +02001235static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +02001236 struct sk_buff *skb,
1237 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001238{
Johannes Berg0e057d732008-09-12 00:39:22 +02001239 struct mac80211_hwsim_data *data = hw->priv, *data2;
1240 bool ack = false;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001241 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001242 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001243 struct ieee80211_rx_status rx_status;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001244 u64 now;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001245
1246 memset(&rx_status, 0, sizeof(rx_status));
Thomas Pedersenf4bda332012-11-13 10:46:27 -08001247 rx_status.flag |= RX_FLAG_MACTIME_START;
Johannes Berge8261172012-07-27 19:48:26 +02001248 rx_status.freq = chan->center_freq;
1249 rx_status.band = chan->band;
Karl Beldandad63302013-04-15 17:09:30 +02001250 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1251 rx_status.rate_idx =
1252 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
Johannes Berg8613c942017-04-26 13:51:41 +02001253 rx_status.nss =
Karl Beldandad63302013-04-15 17:09:30 +02001254 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
Johannes Bergda6a4352017-04-26 12:14:59 +02001255 rx_status.encoding = RX_ENC_VHT;
Karl Beldandad63302013-04-15 17:09:30 +02001256 } else {
1257 rx_status.rate_idx = info->control.rates[0].idx;
1258 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
Johannes Bergda6a4352017-04-26 12:14:59 +02001259 rx_status.encoding = RX_ENC_HT;
Karl Beldandad63302013-04-15 17:09:30 +02001260 }
Jouni Malinen281ed292011-08-06 23:07:00 +03001261 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
Johannes Berg2f242bf2017-05-05 11:53:19 +02001262 rx_status.bw = RATE_INFO_BW_40;
1263 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1264 rx_status.bw = RATE_INFO_BW_80;
1265 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1266 rx_status.bw = RATE_INFO_BW_160;
1267 else
1268 rx_status.bw = RATE_INFO_BW_20;
Jouni Malinen281ed292011-08-06 23:07:00 +03001269 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
Johannes Berg7fdd69c2017-04-26 11:13:00 +02001270 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
Johannes Berg4b14c962009-07-10 16:56:59 +02001271 /* TODO: simulate real signal strength (and optional packet loss) */
Johannes Berg1d5e9f82017-04-13 10:31:16 +02001272 rx_status.signal = -50;
1273 if (info->control.vif)
1274 rx_status.signal += info->control.vif->bss_conf.txpower;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001275
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001276 if (data->ps != PS_DISABLED)
1277 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1278
Johannes Berg90e30122009-06-18 14:51:12 +02001279 /* release the skb's source info */
1280 skb_orphan(skb);
John W. Linvillec0acf382009-06-30 16:55:52 -04001281 skb_dst_drop(skb);
Johannes Berg90e30122009-06-18 14:51:12 +02001282 skb->mark = 0;
1283 secpath_reset(skb);
1284 nf_reset(skb);
1285
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001286 /*
1287 * Get absolute mactime here so all HWs RX at the "same time", and
1288 * absolute TX time for beacon mactime so the timestamp matches.
1289 * Giving beacons a different mactime than non-beacons looks messy, but
1290 * it helps the Toffset be exact and a ~10us mactime discrepancy
1291 * probably doesn't really matter.
1292 */
1293 if (ieee80211_is_beacon(hdr->frame_control) ||
1294 ieee80211_is_probe_resp(hdr->frame_control))
1295 now = data->abs_bcn_ts;
1296 else
1297 now = mac80211_hwsim_get_tsf_raw();
1298
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001299 /* Copy skb to all enabled radios that are on the current frequency */
Johannes Berg0e057d732008-09-12 00:39:22 +02001300 spin_lock(&hwsim_radio_lock);
1301 list_for_each_entry(data2, &hwsim_radios, list) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001302 struct sk_buff *nskb;
Johannes Berge8261172012-07-27 19:48:26 +02001303 struct tx_iter_data tx_iter_data = {
1304 .receive = false,
1305 .channel = chan,
1306 };
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001307
Johannes Berg0e057d732008-09-12 00:39:22 +02001308 if (data == data2)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001309 continue;
Johannes Berg0e057d732008-09-12 00:39:22 +02001310
Johannes Berge8261172012-07-27 19:48:26 +02001311 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1312 !hwsim_ps_rx_ok(data2, skb))
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001313 continue;
1314
Johannes Berge8261172012-07-27 19:48:26 +02001315 if (!(data->group & data2->group))
1316 continue;
1317
Martin Willi100cb9f2016-05-09 18:33:59 +02001318 if (data->netgroup != data2->netgroup)
1319 continue;
1320
Johannes Berge8261172012-07-27 19:48:26 +02001321 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1322 !hwsim_chans_compat(chan, data2->channel)) {
1323 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001324 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1325 mac80211_hwsim_tx_iter, &tx_iter_data);
Johannes Berge8261172012-07-27 19:48:26 +02001326 if (!tx_iter_data.receive)
1327 continue;
1328 }
1329
Johannes Berg90b9e4462012-11-16 10:09:08 +01001330 /*
1331 * reserve some space for our vendor and the normal
1332 * radiotap header, since we're copying anyway
1333 */
Johannes Berga357d7f2012-12-10 11:57:42 +01001334 if (skb->len < PAGE_SIZE && paged_rx) {
1335 struct page *page = alloc_page(GFP_ATOMIC);
1336
1337 if (!page)
1338 continue;
1339
1340 nskb = dev_alloc_skb(128);
1341 if (!nskb) {
1342 __free_page(page);
1343 continue;
1344 }
1345
1346 memcpy(page_address(page), skb->data, skb->len);
1347 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1348 } else {
1349 nskb = skb_copy(skb, GFP_ATOMIC);
1350 if (!nskb)
1351 continue;
1352 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001353
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001354 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
Johannes Berg0e057d732008-09-12 00:39:22 +02001355 ack = true;
Javier Cardonaf483ad22012-03-31 11:31:30 -07001356
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001357 rx_status.mactime = now + data2->tsf_offset;
Johannes Berg90b9e4462012-11-16 10:09:08 +01001358
Johannes Bergf1d58c22009-06-17 13:13:00 +02001359 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
Johannes Berg1f7bba72014-11-06 22:56:36 +01001360
1361 mac80211_hwsim_add_vendor_rtap(nskb);
1362
Ben Greear2155c3f2014-10-22 12:22:58 -07001363 data2->rx_pkts++;
1364 data2->rx_bytes += nskb->len;
Johannes Bergf1d58c22009-06-17 13:13:00 +02001365 ieee80211_rx_irqsafe(data2->hw, nskb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001366 }
Johannes Berg0e057d732008-09-12 00:39:22 +02001367 spin_unlock(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001368
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001369 return ack;
1370}
1371
Thomas Huehn36323f82012-07-23 21:33:42 +02001372static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1373 struct ieee80211_tx_control *control,
1374 struct sk_buff *skb)
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001375{
Johannes Berge8261172012-07-27 19:48:26 +02001376 struct mac80211_hwsim_data *data = hw->priv;
1377 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
Jouni Malinen732b5392015-11-26 20:50:12 +02001378 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Berge8261172012-07-27 19:48:26 +02001379 struct ieee80211_chanctx_conf *chanctx_conf;
1380 struct ieee80211_channel *channel;
Johannes Berg0e057d732008-09-12 00:39:22 +02001381 bool ack;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001382 u32 _portid;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001383
Johannes Berge8261172012-07-27 19:48:26 +02001384 if (WARN_ON(skb->len < 10)) {
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001385 /* Should not happen; just a sanity check for addr1 use */
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001386 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001387 return;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001388 }
1389
Luciano Coelho361c3e02014-02-10 15:23:03 +02001390 if (!data->use_chanctx) {
Johannes Berge8261172012-07-27 19:48:26 +02001391 channel = data->channel;
1392 } else if (txi->hw_queue == 4) {
1393 channel = data->tmp_chan;
1394 } else {
1395 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1396 if (chanctx_conf)
Johannes Berg4bf88532012-11-09 11:39:59 +01001397 channel = chanctx_conf->def.chan;
Johannes Berge8261172012-07-27 19:48:26 +02001398 else
1399 channel = NULL;
1400 }
1401
1402 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001403 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001404 return;
1405 }
1406
1407 if (data->idle && !data->tmp_chan) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001408 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001409 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001410 return;
1411 }
1412
1413 if (txi->control.vif)
1414 hwsim_check_magic(txi->control.vif);
1415 if (control->sta)
1416 hwsim_check_sta_magic(control->sta);
1417
Johannes Berg30686bf2015-06-02 21:39:54 +02001418 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001419 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1420 txi->control.rates,
1421 ARRAY_SIZE(txi->control.rates));
Johannes Berge8261172012-07-27 19:48:26 +02001422
Jouni Malinen732b5392015-11-26 20:50:12 +02001423 if (skb->len >= 24 + 8 &&
1424 ieee80211_is_probe_resp(hdr->frame_control)) {
1425 /* fake header transmission time */
1426 struct ieee80211_mgmt *mgmt;
1427 struct ieee80211_rate *txrate;
1428 u64 ts;
1429
1430 mgmt = (struct ieee80211_mgmt *)skb->data;
1431 txrate = ieee80211_get_tx_rate(hw, txi);
1432 ts = mac80211_hwsim_get_tsf_raw();
1433 mgmt->u.probe_resp.timestamp =
1434 cpu_to_le64(ts + data->tsf_offset +
1435 24 * 8 * 10 / txrate->bitrate);
1436 }
1437
Johannes Berge8261172012-07-27 19:48:26 +02001438 mac80211_hwsim_monitor_rx(hw, skb, channel);
1439
Javier Lopez78825132011-06-01 11:26:13 +02001440 /* wmediumd mode check */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001441 _portid = READ_ONCE(data->wmediumd);
Javier Lopez78825132011-06-01 11:26:13 +02001442
Eric W. Biederman15e47302012-09-07 20:12:54 +00001443 if (_portid)
1444 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
Javier Lopez78825132011-06-01 11:26:13 +02001445
1446 /* NO wmediumd detected, perfect medium simulation */
Ben Greear2155c3f2014-10-22 12:22:58 -07001447 data->tx_pkts++;
1448 data->tx_bytes += skb->len;
Johannes Berge8261172012-07-27 19:48:26 +02001449 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
Javier Lopez78825132011-06-01 11:26:13 +02001450
Johannes Bergf06b7ab2016-01-13 14:21:01 +01001451 if (ack && skb->len >= 16)
Johannes Berge8261172012-07-27 19:48:26 +02001452 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001453
Johannes Berge6a98542008-10-21 12:40:02 +02001454 ieee80211_tx_info_clear_status(txi);
Javier Cardona2a4ffa42012-05-01 03:01:28 -07001455
1456 /* frame was transmitted at most favorable rate at first attempt */
1457 txi->control.rates[0].count = 1;
1458 txi->control.rates[1].idx = -1;
1459
Johannes Berge6a98542008-10-21 12:40:02 +02001460 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1461 txi->flags |= IEEE80211_TX_STAT_ACK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001462 ieee80211_tx_status_irqsafe(hw, skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001463}
1464
1465
1466static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1467{
1468 struct mac80211_hwsim_data *data = hw->priv;
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001469 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001470 data->started = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001471 return 0;
1472}
1473
1474
1475static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1476{
1477 struct mac80211_hwsim_data *data = hw->priv;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001478 data->started = false;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001479 tasklet_hrtimer_cancel(&data->beacon_timer);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001480 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001481}
1482
1483
1484static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001485 struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001486{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001487 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1488 __func__, ieee80211_vif_type_p2p(vif),
1489 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001490 hwsim_set_magic(vif);
Johannes Berge8261172012-07-27 19:48:26 +02001491
1492 vif->cab_queue = 0;
1493 vif->hw_queue[IEEE80211_AC_VO] = 0;
1494 vif->hw_queue[IEEE80211_AC_VI] = 1;
1495 vif->hw_queue[IEEE80211_AC_BE] = 2;
1496 vif->hw_queue[IEEE80211_AC_BK] = 3;
1497
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001498 return 0;
1499}
1500
1501
Johannes Bergc35d0272010-08-27 12:35:59 +02001502static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1503 struct ieee80211_vif *vif,
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001504 enum nl80211_iftype newtype,
1505 bool newp2p)
Johannes Bergc35d0272010-08-27 12:35:59 +02001506{
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001507 newtype = ieee80211_iftype_p2p(newtype, newp2p);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001508 wiphy_dbg(hw->wiphy,
1509 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1510 __func__, ieee80211_vif_type_p2p(vif),
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001511 newtype, vif->addr);
Johannes Bergc35d0272010-08-27 12:35:59 +02001512 hwsim_check_magic(vif);
1513
Johannes Berg3eb92f62013-03-18 22:13:18 +01001514 /*
1515 * interface may change from non-AP to AP in
1516 * which case this needs to be set up again
1517 */
1518 vif->cab_queue = 0;
1519
Johannes Bergc35d0272010-08-27 12:35:59 +02001520 return 0;
1521}
1522
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001523static void mac80211_hwsim_remove_interface(
Johannes Berg1ed32e42009-12-23 13:15:45 +01001524 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001525{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001526 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1527 __func__, ieee80211_vif_type_p2p(vif),
1528 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001529 hwsim_check_magic(vif);
1530 hwsim_clear_magic(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001531}
1532
Johannes Berge8261172012-07-27 19:48:26 +02001533static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1534 struct sk_buff *skb,
1535 struct ieee80211_channel *chan)
1536{
Martin Willif21e4d82016-05-14 10:34:44 +02001537 struct mac80211_hwsim_data *data = hw->priv;
Mark Rutland6aa7de02017-10-23 14:07:29 -07001538 u32 _pid = READ_ONCE(data->wmediumd);
Johannes Berge8261172012-07-27 19:48:26 +02001539
Johannes Berg30686bf2015-06-02 21:39:54 +02001540 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
Karl Beldan1eb32172013-04-19 14:44:52 +02001541 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1542 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1543 txi->control.rates,
1544 ARRAY_SIZE(txi->control.rates));
1545 }
1546
Johannes Berge8261172012-07-27 19:48:26 +02001547 mac80211_hwsim_monitor_rx(hw, skb, chan);
1548
1549 if (_pid)
1550 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1551
1552 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1553 dev_kfree_skb(skb);
1554}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001555
1556static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1557 struct ieee80211_vif *vif)
1558{
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001559 struct mac80211_hwsim_data *data = arg;
1560 struct ieee80211_hw *hw = data->hw;
1561 struct ieee80211_tx_info *info;
1562 struct ieee80211_rate *txrate;
1563 struct ieee80211_mgmt *mgmt;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001564 struct sk_buff *skb;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001565
Johannes Berg8aa21e62008-09-11 02:16:36 +02001566 hwsim_check_magic(vif);
1567
Andrey Yurovsky55b39612008-10-31 23:23:35 -07001568 if (vif->type != NL80211_IFTYPE_AP &&
Johannes Berg2b2d7792010-08-17 12:08:07 +02001569 vif->type != NL80211_IFTYPE_MESH_POINT &&
1570 vif->type != NL80211_IFTYPE_ADHOC)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001571 return;
1572
1573 skb = ieee80211_beacon_get(hw, vif);
1574 if (skb == NULL)
1575 return;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001576 info = IEEE80211_SKB_CB(skb);
Johannes Berg30686bf2015-06-02 21:39:54 +02001577 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001578 ieee80211_get_tx_rates(vif, NULL, skb,
1579 info->control.rates,
1580 ARRAY_SIZE(info->control.rates));
1581
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001582 txrate = ieee80211_get_tx_rate(hw, info);
1583
1584 mgmt = (struct ieee80211_mgmt *) skb->data;
1585 /* fake header transmission time */
1586 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1587 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1588 data->tsf_offset +
1589 24 * 8 * 10 / txrate->bitrate);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001590
Johannes Berge8261172012-07-27 19:48:26 +02001591 mac80211_hwsim_tx_frame(hw, skb,
Johannes Berg4bf88532012-11-09 11:39:59 +01001592 rcu_dereference(vif->chanctx_conf)->def.chan);
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02001593
1594 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1595 ieee80211_csa_finish(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001596}
1597
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001598static enum hrtimer_restart
1599mac80211_hwsim_beacon(struct hrtimer *timer)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001600{
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001601 struct mac80211_hwsim_data *data =
1602 container_of(timer, struct mac80211_hwsim_data,
1603 beacon_timer.timer);
1604 struct ieee80211_hw *hw = data->hw;
1605 u64 bcn_int = data->beacon_int;
1606 ktime_t next_bcn;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001607
Johannes Berg4c4c6712009-06-01 14:29:52 +02001608 if (!data->started)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001609 goto out;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001610
Jouni Malinenf248f102008-06-13 19:44:47 +03001611 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001612 hw, IEEE80211_IFACE_ITER_NORMAL,
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001613 mac80211_hwsim_beacon_tx, data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001614
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001615 /* beacon at new TBTT + beacon interval */
1616 if (data->bcn_delta) {
1617 bcn_int -= data->bcn_delta;
1618 data->bcn_delta = 0;
1619 }
1620
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001621 next_bcn = ktime_add(hrtimer_get_expires(timer),
1622 ns_to_ktime(bcn_int * 1000));
1623 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1624out:
1625 return HRTIMER_NORESTART;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001626}
1627
Karl Beldan675a0b02013-03-25 16:26:57 +01001628static const char * const hwsim_chanwidths[] = {
1629 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1630 [NL80211_CHAN_WIDTH_20] = "ht20",
1631 [NL80211_CHAN_WIDTH_40] = "ht40",
1632 [NL80211_CHAN_WIDTH_80] = "vht80",
1633 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1634 [NL80211_CHAN_WIDTH_160] = "vht160",
Johannes Berg0aaffa92010-05-05 15:28:27 +02001635};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001636
Johannes Berge8975582008-10-09 12:18:51 +02001637static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001638{
1639 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001640 struct ieee80211_conf *conf = &hw->conf;
Johannes Berg0f782312009-12-01 13:37:02 +01001641 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1642 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1643 [IEEE80211_SMPS_OFF] = "off",
1644 [IEEE80211_SMPS_STATIC] = "static",
1645 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1646 };
Johannes Berg7f813ce2013-11-06 10:37:34 +01001647 int idx;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001648
Karl Beldan675a0b02013-03-25 16:26:57 +01001649 if (conf->chandef.chan)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001650 wiphy_dbg(hw->wiphy,
1651 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1652 __func__,
1653 conf->chandef.chan->center_freq,
1654 conf->chandef.center_freq1,
1655 conf->chandef.center_freq2,
1656 hwsim_chanwidths[conf->chandef.width],
1657 !!(conf->flags & IEEE80211_CONF_IDLE),
1658 !!(conf->flags & IEEE80211_CONF_PS),
1659 smps_modes[conf->smps_mode]);
Karl Beldan675a0b02013-03-25 16:26:57 +01001660 else
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001661 wiphy_dbg(hw->wiphy,
1662 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1663 __func__,
1664 !!(conf->flags & IEEE80211_CONF_IDLE),
1665 !!(conf->flags & IEEE80211_CONF_PS),
1666 smps_modes[conf->smps_mode]);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001667
Jouni Malinen70541832009-11-01 11:30:48 +02001668 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1669
Johannes Berg7f813ce2013-11-06 10:37:34 +01001670 WARN_ON(conf->chandef.chan && data->use_chanctx);
Johannes Berge8261172012-07-27 19:48:26 +02001671
Johannes Berg7f813ce2013-11-06 10:37:34 +01001672 mutex_lock(&data->mutex);
1673 if (data->scanning && conf->chandef.chan) {
1674 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1675 if (data->survey_data[idx].channel == data->channel) {
1676 data->survey_data[idx].start =
1677 data->survey_data[idx].next_start;
1678 data->survey_data[idx].end = jiffies;
1679 break;
1680 }
1681 }
Johannes Berge8261172012-07-27 19:48:26 +02001682
Johannes Berg7f813ce2013-11-06 10:37:34 +01001683 data->channel = conf->chandef.chan;
1684
1685 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1686 if (data->survey_data[idx].channel &&
1687 data->survey_data[idx].channel != data->channel)
1688 continue;
1689 data->survey_data[idx].channel = data->channel;
1690 data->survey_data[idx].next_start = jiffies;
1691 break;
1692 }
1693 } else {
1694 data->channel = conf->chandef.chan;
1695 }
1696 mutex_unlock(&data->mutex);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001697
Johannes Berg4c4c6712009-06-01 14:29:52 +02001698 if (!data->started || !data->beacon_int)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001699 tasklet_hrtimer_cancel(&data->beacon_timer);
1700 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001701 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1702 u32 bcn_int = data->beacon_int;
1703 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1704
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001705 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001706 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001707 HRTIMER_MODE_REL);
1708 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001709
1710 return 0;
1711}
1712
1713
1714static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1715 unsigned int changed_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001716 unsigned int *total_flags,u64 multicast)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001717{
1718 struct mac80211_hwsim_data *data = hw->priv;
1719
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001720 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001721
1722 data->rx_filter = 0;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001723 if (*total_flags & FIF_ALLMULTI)
1724 data->rx_filter |= FIF_ALLMULTI;
1725
1726 *total_flags = data->rx_filter;
1727}
1728
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001729static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1730 struct ieee80211_vif *vif)
1731{
1732 unsigned int *count = data;
1733 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1734
1735 if (vp->bcn_en)
1736 (*count)++;
1737}
1738
Johannes Berg8aa21e62008-09-11 02:16:36 +02001739static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1740 struct ieee80211_vif *vif,
1741 struct ieee80211_bss_conf *info,
1742 u32 changed)
1743{
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001744 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001745 struct mac80211_hwsim_data *data = hw->priv;
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001746
Johannes Berg8aa21e62008-09-11 02:16:36 +02001747 hwsim_check_magic(vif);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001748
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001749 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1750 __func__, changed, vif->addr);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001751
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001752 if (changed & BSS_CHANGED_BSSID) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001753 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1754 __func__, info->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001755 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1756 }
1757
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001758 if (changed & BSS_CHANGED_ASSOC) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001759 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1760 info->assoc, info->aid);
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001761 vp->assoc = info->assoc;
1762 vp->aid = info->aid;
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001763 }
1764
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001765 if (changed & BSS_CHANGED_BEACON_ENABLED) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001766 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1767 info->enable_beacon, info->beacon_int);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001768 vp->bcn_en = info->enable_beacon;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001769 if (data->started &&
1770 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1771 info->enable_beacon) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001772 u64 tsf, until_tbtt;
1773 u32 bcn_int;
Johannes Berg56067622015-03-03 22:09:05 +01001774 data->beacon_int = info->beacon_int * 1024;
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001775 tsf = mac80211_hwsim_get_tsf(hw, vif);
1776 bcn_int = data->beacon_int;
1777 until_tbtt = bcn_int - do_div(tsf, bcn_int);
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001778 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001779 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001780 HRTIMER_MODE_REL);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001781 } else if (!info->enable_beacon) {
1782 unsigned int count = 0;
Jouni Malinencdb1b802013-11-06 12:06:30 +02001783 ieee80211_iterate_active_interfaces_atomic(
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001784 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1785 mac80211_hwsim_bcn_en_iter, &count);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001786 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1787 count);
Johannes Berg56067622015-03-03 22:09:05 +01001788 if (count == 0) {
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001789 tasklet_hrtimer_cancel(&data->beacon_timer);
Johannes Berg56067622015-03-03 22:09:05 +01001790 data->beacon_int = 0;
1791 }
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001792 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001793 }
1794
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001795 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001796 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1797 info->use_cts_prot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001798 }
1799
1800 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001801 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1802 info->use_short_preamble);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001803 }
1804
1805 if (changed & BSS_CHANGED_ERP_SLOT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001806 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001807 }
1808
1809 if (changed & BSS_CHANGED_HT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001810 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1811 info->ht_operation_mode);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001812 }
1813
1814 if (changed & BSS_CHANGED_BASIC_RATES) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001815 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1816 (unsigned long long) info->basic_rates);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001817 }
Johannes Bergcbc668a2012-10-24 11:54:31 +02001818
1819 if (changed & BSS_CHANGED_TXPOWER)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001820 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
Johannes Berg8aa21e62008-09-11 02:16:36 +02001821}
1822
Johannes Berg1d669cb2010-02-19 19:06:55 +01001823static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1824 struct ieee80211_vif *vif,
1825 struct ieee80211_sta *sta)
1826{
1827 hwsim_check_magic(vif);
1828 hwsim_set_sta_magic(sta);
1829
1830 return 0;
1831}
1832
1833static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1834 struct ieee80211_vif *vif,
1835 struct ieee80211_sta *sta)
1836{
1837 hwsim_check_magic(vif);
1838 hwsim_clear_sta_magic(sta);
1839
1840 return 0;
1841}
1842
Johannes Berg8aa21e62008-09-11 02:16:36 +02001843static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1844 struct ieee80211_vif *vif,
Johannes Berg17741cd2008-09-11 00:02:02 +02001845 enum sta_notify_cmd cmd,
1846 struct ieee80211_sta *sta)
Johannes Berg8aa21e62008-09-11 02:16:36 +02001847{
1848 hwsim_check_magic(vif);
Johannes Berg1d669cb2010-02-19 19:06:55 +01001849
Johannes Berg81c06522008-09-11 02:17:01 +02001850 switch (cmd) {
Christian Lamparter89fad572008-12-09 16:28:06 +01001851 case STA_NOTIFY_SLEEP:
1852 case STA_NOTIFY_AWAKE:
1853 /* TODO: make good use of these flags */
1854 break;
Johannes Berg1d669cb2010-02-19 19:06:55 +01001855 default:
1856 WARN(1, "Invalid sta notify: %d\n", cmd);
1857 break;
Johannes Berg81c06522008-09-11 02:17:01 +02001858 }
1859}
1860
1861static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1862 struct ieee80211_sta *sta,
1863 bool set)
1864{
1865 hwsim_check_sta_magic(sta);
1866 return 0;
Johannes Berg8aa21e62008-09-11 02:16:36 +02001867}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001868
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001869static int mac80211_hwsim_conf_tx(
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001870 struct ieee80211_hw *hw,
1871 struct ieee80211_vif *vif, u16 queue,
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001872 const struct ieee80211_tx_queue_params *params)
1873{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001874 wiphy_dbg(hw->wiphy,
1875 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1876 __func__, queue,
1877 params->txop, params->cw_min,
1878 params->cw_max, params->aifs);
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001879 return 0;
1880}
1881
Johannes Berg7f813ce2013-11-06 10:37:34 +01001882static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1883 struct survey_info *survey)
Holger Schurig12897232010-04-19 10:23:57 +02001884{
Johannes Berg7f813ce2013-11-06 10:37:34 +01001885 struct mac80211_hwsim_data *hwsim = hw->priv;
Holger Schurig12897232010-04-19 10:23:57 +02001886
Johannes Berg7f813ce2013-11-06 10:37:34 +01001887 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
Holger Schurig12897232010-04-19 10:23:57 +02001888 return -ENOENT;
1889
Johannes Berg7f813ce2013-11-06 10:37:34 +01001890 mutex_lock(&hwsim->mutex);
1891 survey->channel = hwsim->survey_data[idx].channel;
1892 if (!survey->channel) {
1893 mutex_unlock(&hwsim->mutex);
1894 return -ENOENT;
1895 }
Holger Schurig12897232010-04-19 10:23:57 +02001896
1897 /*
Johannes Berg7f813ce2013-11-06 10:37:34 +01001898 * Magically conjured dummy values --- this is only ok for simulated hardware.
Holger Schurig12897232010-04-19 10:23:57 +02001899 *
Johannes Berg7f813ce2013-11-06 10:37:34 +01001900 * A real driver which cannot determine real values noise MUST NOT
1901 * report any, especially not a magically conjured ones :-)
Holger Schurig12897232010-04-19 10:23:57 +02001902 */
Johannes Berg7f813ce2013-11-06 10:37:34 +01001903 survey->filled = SURVEY_INFO_NOISE_DBM |
1904 SURVEY_INFO_TIME |
1905 SURVEY_INFO_TIME_BUSY;
Holger Schurig12897232010-04-19 10:23:57 +02001906 survey->noise = -92;
Johannes Berg7f813ce2013-11-06 10:37:34 +01001907 survey->time =
1908 jiffies_to_msecs(hwsim->survey_data[idx].end -
1909 hwsim->survey_data[idx].start);
1910 /* report 12.5% of channel time is used */
1911 survey->time_busy = survey->time/8;
1912 mutex_unlock(&hwsim->mutex);
Holger Schurig12897232010-04-19 10:23:57 +02001913
1914 return 0;
1915}
1916
Johannes Bergaff89a92009-07-01 21:26:51 +02001917#ifdef CONFIG_NL80211_TESTMODE
1918/*
1919 * This section contains example code for using netlink
1920 * attributes with the testmode command in nl80211.
1921 */
1922
1923/* These enums need to be kept in sync with userspace */
1924enum hwsim_testmode_attr {
1925 __HWSIM_TM_ATTR_INVALID = 0,
1926 HWSIM_TM_ATTR_CMD = 1,
1927 HWSIM_TM_ATTR_PS = 2,
1928
1929 /* keep last */
1930 __HWSIM_TM_ATTR_AFTER_LAST,
1931 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1932};
1933
1934enum hwsim_testmode_cmd {
1935 HWSIM_TM_CMD_SET_PS = 0,
1936 HWSIM_TM_CMD_GET_PS = 1,
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001937 HWSIM_TM_CMD_STOP_QUEUES = 2,
1938 HWSIM_TM_CMD_WAKE_QUEUES = 3,
Johannes Bergaff89a92009-07-01 21:26:51 +02001939};
1940
1941static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1942 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1943 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1944};
1945
Johannes Berg5bc38192009-07-07 11:00:55 +02001946static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
David Spinadel52981cd2013-07-31 18:06:22 +03001947 struct ieee80211_vif *vif,
Johannes Berg5bc38192009-07-07 11:00:55 +02001948 void *data, int len)
Johannes Bergaff89a92009-07-01 21:26:51 +02001949{
1950 struct mac80211_hwsim_data *hwsim = hw->priv;
1951 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1952 struct sk_buff *skb;
1953 int err, ps;
1954
1955 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
Johannes Bergfceb6432017-04-12 14:34:07 +02001956 hwsim_testmode_policy, NULL);
Johannes Bergaff89a92009-07-01 21:26:51 +02001957 if (err)
1958 return err;
1959
1960 if (!tb[HWSIM_TM_ATTR_CMD])
1961 return -EINVAL;
1962
1963 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1964 case HWSIM_TM_CMD_SET_PS:
1965 if (!tb[HWSIM_TM_ATTR_PS])
1966 return -EINVAL;
1967 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1968 return hwsim_fops_ps_write(hwsim, ps);
1969 case HWSIM_TM_CMD_GET_PS:
1970 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1971 nla_total_size(sizeof(u32)));
1972 if (!skb)
1973 return -ENOMEM;
David S. Miller633c9382012-04-01 21:03:30 -04001974 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1975 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02001976 return cfg80211_testmode_reply(skb);
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001977 case HWSIM_TM_CMD_STOP_QUEUES:
1978 ieee80211_stop_queues(hw);
1979 return 0;
1980 case HWSIM_TM_CMD_WAKE_QUEUES:
1981 ieee80211_wake_queues(hw);
1982 return 0;
Johannes Bergaff89a92009-07-01 21:26:51 +02001983 default:
1984 return -EOPNOTSUPP;
1985 }
1986
1987 nla_put_failure:
1988 kfree_skb(skb);
1989 return -ENOBUFS;
1990}
1991#endif
1992
Johannes Berg8b73d132009-12-01 14:24:24 +01001993static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1994 struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02001995 struct ieee80211_ampdu_params *params)
Johannes Berg8b73d132009-12-01 14:24:24 +01001996{
Sara Sharon50ea05e2015-12-30 16:06:04 +02001997 struct ieee80211_sta *sta = params->sta;
1998 enum ieee80211_ampdu_mlme_action action = params->action;
1999 u16 tid = params->tid;
2000
Johannes Berg8b73d132009-12-01 14:24:24 +01002001 switch (action) {
2002 case IEEE80211_AMPDU_TX_START:
2003 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2004 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02002005 case IEEE80211_AMPDU_TX_STOP_CONT:
2006 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2007 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Johannes Berg8b73d132009-12-01 14:24:24 +01002008 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2009 break;
2010 case IEEE80211_AMPDU_TX_OPERATIONAL:
2011 break;
2012 case IEEE80211_AMPDU_RX_START:
2013 case IEEE80211_AMPDU_RX_STOP:
2014 break;
2015 default:
2016 return -EOPNOTSUPP;
2017 }
2018
2019 return 0;
2020}
2021
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02002022static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2023 struct ieee80211_vif *vif,
2024 u32 queues, bool drop)
Johannes Berga80f7c02009-12-23 13:15:32 +01002025{
Javier Lopez78825132011-06-01 11:26:13 +02002026 /* Not implemented, queues only on kernel side */
Johannes Berga80f7c02009-12-23 13:15:32 +01002027}
2028
Johannes Berge8261172012-07-27 19:48:26 +02002029static void hw_scan_work(struct work_struct *work)
Johannes Berg69068032010-02-03 10:47:55 +01002030{
Johannes Berge8261172012-07-27 19:48:26 +02002031 struct mac80211_hwsim_data *hwsim =
2032 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2033 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2034 int dwell, i;
Johannes Berg69068032010-02-03 10:47:55 +01002035
Johannes Berge8261172012-07-27 19:48:26 +02002036 mutex_lock(&hwsim->mutex);
2037 if (hwsim->scan_chan_idx >= req->n_channels) {
Avraham Stern7947d3e2016-07-05 15:23:12 +03002038 struct cfg80211_scan_info info = {
2039 .aborted = false,
2040 };
2041
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002042 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
Avraham Stern7947d3e2016-07-05 15:23:12 +03002043 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002044 hwsim->hw_scan_request = NULL;
2045 hwsim->hw_scan_vif = NULL;
2046 hwsim->tmp_chan = NULL;
2047 mutex_unlock(&hwsim->mutex);
2048 return;
2049 }
2050
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002051 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2052 req->channels[hwsim->scan_chan_idx]->center_freq);
Johannes Berge8261172012-07-27 19:48:26 +02002053
2054 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
Johannes Berga8e828d2015-11-26 17:59:27 +01002055 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2056 IEEE80211_CHAN_RADAR) ||
Johannes Berge8261172012-07-27 19:48:26 +02002057 !req->n_ssids) {
2058 dwell = 120;
2059 } else {
2060 dwell = 30;
2061 /* send probes */
2062 for (i = 0; i < req->n_ssids; i++) {
2063 struct sk_buff *probe;
Jouni Malinen12880d12016-02-26 22:12:49 +02002064 struct ieee80211_mgmt *mgmt;
Johannes Berge8261172012-07-27 19:48:26 +02002065
2066 probe = ieee80211_probereq_get(hwsim->hw,
Johannes Berg339467b2014-06-12 22:41:00 +02002067 hwsim->scan_addr,
Johannes Berge8261172012-07-27 19:48:26 +02002068 req->ssids[i].ssid,
2069 req->ssids[i].ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002070 req->ie_len);
Johannes Berge8261172012-07-27 19:48:26 +02002071 if (!probe)
2072 continue;
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002073
Jouni Malinen12880d12016-02-26 22:12:49 +02002074 mgmt = (struct ieee80211_mgmt *) probe->data;
2075 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2076 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2077
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002078 if (req->ie_len)
Johannes Berg59ae1d12017-06-16 14:29:20 +02002079 skb_put_data(probe, req->ie, req->ie_len);
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002080
Johannes Berge8261172012-07-27 19:48:26 +02002081 local_bh_disable();
2082 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2083 hwsim->tmp_chan);
2084 local_bh_enable();
2085 }
2086 }
2087 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2088 msecs_to_jiffies(dwell));
Johannes Berg7f813ce2013-11-06 10:37:34 +01002089 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2090 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2091 hwsim->survey_data[hwsim->scan_chan_idx].end =
2092 jiffies + msecs_to_jiffies(dwell);
Johannes Berge8261172012-07-27 19:48:26 +02002093 hwsim->scan_chan_idx++;
2094 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002095}
2096
2097static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +02002098 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02002099 struct ieee80211_scan_request *hw_req)
Johannes Berg69068032010-02-03 10:47:55 +01002100{
Johannes Berge8261172012-07-27 19:48:26 +02002101 struct mac80211_hwsim_data *hwsim = hw->priv;
David Spinadelc56ef672014-02-05 15:21:13 +02002102 struct cfg80211_scan_request *req = &hw_req->req;
Johannes Berg69068032010-02-03 10:47:55 +01002103
Johannes Berge8261172012-07-27 19:48:26 +02002104 mutex_lock(&hwsim->mutex);
2105 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2106 mutex_unlock(&hwsim->mutex);
2107 return -EBUSY;
2108 }
2109 hwsim->hw_scan_request = req;
2110 hwsim->hw_scan_vif = vif;
2111 hwsim->scan_chan_idx = 0;
Johannes Berg339467b2014-06-12 22:41:00 +02002112 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2113 get_random_mask_addr(hwsim->scan_addr,
2114 hw_req->req.mac_addr,
2115 hw_req->req.mac_addr_mask);
2116 else
2117 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
Johannes Berg7f813ce2013-11-06 10:37:34 +01002118 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Johannes Berge8261172012-07-27 19:48:26 +02002119 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002120
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002121 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
Johannes Berg69068032010-02-03 10:47:55 +01002122
Johannes Berge8261172012-07-27 19:48:26 +02002123 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
Johannes Berg69068032010-02-03 10:47:55 +01002124
2125 return 0;
2126}
2127
Johannes Berge8261172012-07-27 19:48:26 +02002128static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2129 struct ieee80211_vif *vif)
2130{
2131 struct mac80211_hwsim_data *hwsim = hw->priv;
Avraham Stern7947d3e2016-07-05 15:23:12 +03002132 struct cfg80211_scan_info info = {
2133 .aborted = true,
2134 };
Johannes Berge8261172012-07-27 19:48:26 +02002135
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002136 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
Johannes Berge8261172012-07-27 19:48:26 +02002137
2138 cancel_delayed_work_sync(&hwsim->hw_scan);
2139
2140 mutex_lock(&hwsim->mutex);
Avraham Stern7947d3e2016-07-05 15:23:12 +03002141 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002142 hwsim->tmp_chan = NULL;
2143 hwsim->hw_scan_request = NULL;
2144 hwsim->hw_scan_vif = NULL;
2145 mutex_unlock(&hwsim->mutex);
2146}
2147
Johannes Berga344d672014-06-12 22:24:31 +02002148static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2149 struct ieee80211_vif *vif,
2150 const u8 *mac_addr)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002151{
2152 struct mac80211_hwsim_data *hwsim = hw->priv;
2153
2154 mutex_lock(&hwsim->mutex);
2155
2156 if (hwsim->scanning) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002157 pr_debug("two hwsim sw_scans detected!\n");
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002158 goto out;
2159 }
2160
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002161 pr_debug("hwsim sw_scan request, prepping stuff\n");
Johannes Berg339467b2014-06-12 22:41:00 +02002162
2163 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002164 hwsim->scanning = true;
Johannes Berg7f813ce2013-11-06 10:37:34 +01002165 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002166
2167out:
2168 mutex_unlock(&hwsim->mutex);
2169}
2170
Johannes Berga344d672014-06-12 22:24:31 +02002171static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2172 struct ieee80211_vif *vif)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002173{
2174 struct mac80211_hwsim_data *hwsim = hw->priv;
2175
2176 mutex_lock(&hwsim->mutex);
2177
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002178 pr_debug("hwsim sw_scan_complete\n");
Johannes Berg23ff98f2010-05-03 09:21:14 +02002179 hwsim->scanning = false;
Joe Perches93803b32015-03-02 19:54:49 -08002180 eth_zero_addr(hwsim->scan_addr);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002181
2182 mutex_unlock(&hwsim->mutex);
2183}
2184
Johannes Berg661ef472015-11-24 18:29:45 +01002185static void hw_roc_start(struct work_struct *work)
2186{
2187 struct mac80211_hwsim_data *hwsim =
2188 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2189
2190 mutex_lock(&hwsim->mutex);
2191
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002192 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
Johannes Berg661ef472015-11-24 18:29:45 +01002193 hwsim->tmp_chan = hwsim->roc_chan;
2194 ieee80211_ready_on_channel(hwsim->hw);
2195
2196 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2197 msecs_to_jiffies(hwsim->roc_duration));
2198
2199 mutex_unlock(&hwsim->mutex);
2200}
2201
Johannes Berge8261172012-07-27 19:48:26 +02002202static void hw_roc_done(struct work_struct *work)
2203{
2204 struct mac80211_hwsim_data *hwsim =
2205 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2206
2207 mutex_lock(&hwsim->mutex);
2208 ieee80211_remain_on_channel_expired(hwsim->hw);
2209 hwsim->tmp_chan = NULL;
2210 mutex_unlock(&hwsim->mutex);
2211
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002212 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
Johannes Berge8261172012-07-27 19:48:26 +02002213}
2214
2215static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
Eliad Peller49884562012-11-19 17:05:09 +02002216 struct ieee80211_vif *vif,
Johannes Berge8261172012-07-27 19:48:26 +02002217 struct ieee80211_channel *chan,
Ilan Peerd339d5c2013-02-12 09:34:13 +02002218 int duration,
2219 enum ieee80211_roc_type type)
Johannes Berge8261172012-07-27 19:48:26 +02002220{
2221 struct mac80211_hwsim_data *hwsim = hw->priv;
2222
2223 mutex_lock(&hwsim->mutex);
2224 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2225 mutex_unlock(&hwsim->mutex);
2226 return -EBUSY;
2227 }
2228
Johannes Berg661ef472015-11-24 18:29:45 +01002229 hwsim->roc_chan = chan;
2230 hwsim->roc_duration = duration;
Johannes Berge8261172012-07-27 19:48:26 +02002231 mutex_unlock(&hwsim->mutex);
2232
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002233 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2234 chan->center_freq, duration);
Johannes Berg661ef472015-11-24 18:29:45 +01002235 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
Johannes Berge8261172012-07-27 19:48:26 +02002236
Johannes Berge8261172012-07-27 19:48:26 +02002237 return 0;
2238}
2239
2240static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2241{
2242 struct mac80211_hwsim_data *hwsim = hw->priv;
2243
Johannes Berg661ef472015-11-24 18:29:45 +01002244 cancel_delayed_work_sync(&hwsim->roc_start);
Johannes Berge8261172012-07-27 19:48:26 +02002245 cancel_delayed_work_sync(&hwsim->roc_done);
2246
2247 mutex_lock(&hwsim->mutex);
2248 hwsim->tmp_chan = NULL;
2249 mutex_unlock(&hwsim->mutex);
2250
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002251 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
Johannes Berge8261172012-07-27 19:48:26 +02002252
2253 return 0;
2254}
2255
2256static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2257 struct ieee80211_chanctx_conf *ctx)
2258{
2259 hwsim_set_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002260 wiphy_dbg(hw->wiphy,
2261 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2262 ctx->def.chan->center_freq, ctx->def.width,
2263 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002264 return 0;
2265}
2266
2267static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2268 struct ieee80211_chanctx_conf *ctx)
2269{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002270 wiphy_dbg(hw->wiphy,
2271 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2272 ctx->def.chan->center_freq, ctx->def.width,
2273 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002274 hwsim_check_chanctx_magic(ctx);
2275 hwsim_clear_chanctx_magic(ctx);
2276}
2277
2278static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2279 struct ieee80211_chanctx_conf *ctx,
2280 u32 changed)
2281{
2282 hwsim_check_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002283 wiphy_dbg(hw->wiphy,
2284 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2285 ctx->def.chan->center_freq, ctx->def.width,
2286 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002287}
2288
2289static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2290 struct ieee80211_vif *vif,
2291 struct ieee80211_chanctx_conf *ctx)
2292{
2293 hwsim_check_magic(vif);
2294 hwsim_check_chanctx_magic(ctx);
2295
2296 return 0;
2297}
2298
2299static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2300 struct ieee80211_vif *vif,
2301 struct ieee80211_chanctx_conf *ctx)
2302{
2303 hwsim_check_magic(vif);
2304 hwsim_check_chanctx_magic(ctx);
2305}
2306
Ben Greear2155c3f2014-10-22 12:22:58 -07002307static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2308 "tx_pkts_nic",
2309 "tx_bytes_nic",
2310 "rx_pkts_nic",
2311 "rx_bytes_nic",
2312 "d_tx_dropped",
2313 "d_tx_failed",
2314 "d_ps_mode",
2315 "d_group",
Ben Greear2155c3f2014-10-22 12:22:58 -07002316};
2317
2318#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2319
2320static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2321 struct ieee80211_vif *vif,
2322 u32 sset, u8 *data)
2323{
2324 if (sset == ETH_SS_STATS)
2325 memcpy(data, *mac80211_hwsim_gstrings_stats,
2326 sizeof(mac80211_hwsim_gstrings_stats));
2327}
2328
2329static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2330 struct ieee80211_vif *vif, int sset)
2331{
2332 if (sset == ETH_SS_STATS)
2333 return MAC80211_HWSIM_SSTATS_LEN;
2334 return 0;
2335}
2336
2337static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2338 struct ieee80211_vif *vif,
2339 struct ethtool_stats *stats, u64 *data)
2340{
2341 struct mac80211_hwsim_data *ar = hw->priv;
2342 int i = 0;
2343
2344 data[i++] = ar->tx_pkts;
2345 data[i++] = ar->tx_bytes;
2346 data[i++] = ar->rx_pkts;
2347 data[i++] = ar->rx_bytes;
2348 data[i++] = ar->tx_dropped;
2349 data[i++] = ar->tx_failed;
2350 data[i++] = ar->ps;
2351 data[i++] = ar->group;
Ben Greear2155c3f2014-10-22 12:22:58 -07002352
2353 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2354}
2355
Johannes Berg246ad562016-10-10 12:09:52 +02002356#define HWSIM_COMMON_OPS \
2357 .tx = mac80211_hwsim_tx, \
2358 .start = mac80211_hwsim_start, \
2359 .stop = mac80211_hwsim_stop, \
2360 .add_interface = mac80211_hwsim_add_interface, \
2361 .change_interface = mac80211_hwsim_change_interface, \
2362 .remove_interface = mac80211_hwsim_remove_interface, \
2363 .config = mac80211_hwsim_config, \
2364 .configure_filter = mac80211_hwsim_configure_filter, \
2365 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2366 .sta_add = mac80211_hwsim_sta_add, \
2367 .sta_remove = mac80211_hwsim_sta_remove, \
2368 .sta_notify = mac80211_hwsim_sta_notify, \
2369 .set_tim = mac80211_hwsim_set_tim, \
2370 .conf_tx = mac80211_hwsim_conf_tx, \
2371 .get_survey = mac80211_hwsim_get_survey, \
2372 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2373 .ampdu_action = mac80211_hwsim_ampdu_action, \
2374 .flush = mac80211_hwsim_flush, \
2375 .get_tsf = mac80211_hwsim_get_tsf, \
2376 .set_tsf = mac80211_hwsim_set_tsf, \
2377 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2378 .get_et_stats = mac80211_hwsim_get_et_stats, \
2379 .get_et_strings = mac80211_hwsim_get_et_strings,
2380
Johannes Berg38ed5042014-01-06 22:11:06 +01002381static const struct ieee80211_ops mac80211_hwsim_ops = {
Johannes Berg246ad562016-10-10 12:09:52 +02002382 HWSIM_COMMON_OPS
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002383 .sw_scan_start = mac80211_hwsim_sw_scan,
2384 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002385};
2386
Johannes Berg246ad562016-10-10 12:09:52 +02002387static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2388 HWSIM_COMMON_OPS
2389 .hw_scan = mac80211_hwsim_hw_scan,
2390 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2391 .sw_scan_start = NULL,
2392 .sw_scan_complete = NULL,
2393 .remain_on_channel = mac80211_hwsim_roc,
2394 .cancel_remain_on_channel = mac80211_hwsim_croc,
2395 .add_chanctx = mac80211_hwsim_add_chanctx,
2396 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2397 .change_chanctx = mac80211_hwsim_change_chanctx,
2398 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2399 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2400};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002401
Jukka Rissanen62759362014-10-31 14:48:47 +02002402struct hwsim_new_radio_params {
2403 unsigned int channels;
2404 const char *reg_alpha2;
2405 const struct ieee80211_regdomain *regd;
2406 bool reg_strict;
2407 bool p2p_device;
2408 bool use_chanctx;
2409 bool destroy_on_close;
2410 const char *hwname;
2411 bool no_vif;
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002412 const u8 *perm_addr;
Jukka Rissanen62759362014-10-31 14:48:47 +02002413};
2414
2415static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2416 struct genl_info *info)
2417{
2418 if (info)
Jiri Benc92c14d92015-09-22 18:56:43 +02002419 genl_notify(&hwsim_genl_family, mcast_skb, info,
2420 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002421 else
2422 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2423 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2424}
2425
Patrik Flyktc4499812014-11-12 16:42:39 +02002426static int append_radio_msg(struct sk_buff *skb, int id,
2427 struct hwsim_new_radio_params *param)
Jukka Rissanen62759362014-10-31 14:48:47 +02002428{
Jukka Rissanen62759362014-10-31 14:48:47 +02002429 int ret;
2430
Jukka Rissanen62759362014-10-31 14:48:47 +02002431 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2432 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002433 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002434
2435 if (param->channels) {
2436 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2437 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002438 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002439 }
2440
2441 if (param->reg_alpha2) {
2442 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2443 param->reg_alpha2);
2444 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002445 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002446 }
2447
2448 if (param->regd) {
2449 int i;
2450
Johannes Berg58757552014-12-22 12:51:25 +01002451 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2452 if (hwsim_world_regdom_custom[i] != param->regd)
2453 continue;
Jukka Rissanen62759362014-10-31 14:48:47 +02002454
Jukka Rissanen62759362014-10-31 14:48:47 +02002455 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2456 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002457 return ret;
Johannes Berg58757552014-12-22 12:51:25 +01002458 break;
Jukka Rissanen62759362014-10-31 14:48:47 +02002459 }
2460 }
2461
2462 if (param->reg_strict) {
2463 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2464 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002465 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002466 }
2467
2468 if (param->p2p_device) {
2469 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2470 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002471 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002472 }
2473
2474 if (param->use_chanctx) {
2475 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2476 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002477 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002478 }
2479
2480 if (param->hwname) {
2481 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2482 strlen(param->hwname), param->hwname);
2483 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002484 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002485 }
2486
Patrik Flyktc4499812014-11-12 16:42:39 +02002487 return 0;
Jukka Rissanen62759362014-10-31 14:48:47 +02002488}
2489
Patrik Flyktc4499812014-11-12 16:42:39 +02002490static void hwsim_mcast_new_radio(int id, struct genl_info *info,
Jukka Rissanen62759362014-10-31 14:48:47 +02002491 struct hwsim_new_radio_params *param)
2492{
2493 struct sk_buff *mcast_skb;
Patrik Flyktc4499812014-11-12 16:42:39 +02002494 void *data;
Jukka Rissanen62759362014-10-31 14:48:47 +02002495
Patrik Flyktc4499812014-11-12 16:42:39 +02002496 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002497 if (!mcast_skb)
2498 return;
2499
Patrik Flyktc4499812014-11-12 16:42:39 +02002500 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2501 HWSIM_CMD_NEW_RADIO);
2502 if (!data)
2503 goto out_err;
2504
2505 if (append_radio_msg(mcast_skb, id, param) < 0)
2506 goto out_err;
2507
2508 genlmsg_end(mcast_skb, data);
2509
Jukka Rissanen62759362014-10-31 14:48:47 +02002510 hwsim_mcast_config_msg(mcast_skb, info);
Patrik Flyktc4499812014-11-12 16:42:39 +02002511 return;
2512
2513out_err:
2514 genlmsg_cancel(mcast_skb, data);
2515 nlmsg_free(mcast_skb);
Jukka Rissanen62759362014-10-31 14:48:47 +02002516}
2517
2518static int mac80211_hwsim_new_radio(struct genl_info *info,
2519 struct hwsim_new_radio_params *param)
Johannes Bergde0421d2014-01-06 23:12:12 +01002520{
2521 int err;
2522 u8 addr[ETH_ALEN];
2523 struct mac80211_hwsim_data *data;
2524 struct ieee80211_hw *hw;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002525 enum nl80211_band band;
Johannes Bergde0421d2014-01-06 23:12:12 +01002526 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
Martin Willi100cb9f2016-05-09 18:33:59 +02002527 struct net *net;
Johannes Bergde0421d2014-01-06 23:12:12 +01002528 int idx;
2529
Jukka Rissanen62759362014-10-31 14:48:47 +02002530 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
Luciano Coelho361c3e02014-02-10 15:23:03 +02002531 return -EINVAL;
2532
Johannes Bergde0421d2014-01-06 23:12:12 +01002533 spin_lock_bh(&hwsim_radio_lock);
2534 idx = hwsim_radio_idx++;
2535 spin_unlock_bh(&hwsim_radio_lock);
2536
Jukka Rissanen62759362014-10-31 14:48:47 +02002537 if (param->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002538 ops = &mac80211_hwsim_mchan_ops;
Jukka Rissanen62759362014-10-31 14:48:47 +02002539 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
Johannes Bergde0421d2014-01-06 23:12:12 +01002540 if (!hw) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002541 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
Johannes Bergde0421d2014-01-06 23:12:12 +01002542 err = -ENOMEM;
2543 goto failed;
2544 }
Martin Willi100cb9f2016-05-09 18:33:59 +02002545
Andrew Zaborowskif7d3b4f2017-02-23 13:02:10 +01002546 /* ieee80211_alloc_hw_nm may have used a default name */
2547 param->hwname = wiphy_name(hw->wiphy);
2548
Martin Willi100cb9f2016-05-09 18:33:59 +02002549 if (info)
2550 net = genl_info_net(info);
2551 else
2552 net = &init_net;
2553 wiphy_net_set(hw->wiphy, net);
2554
Johannes Bergde0421d2014-01-06 23:12:12 +01002555 data = hw->priv;
2556 data->hw = hw;
2557
2558 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2559 if (IS_ERR(data->dev)) {
2560 printk(KERN_DEBUG
2561 "mac80211_hwsim: device_create failed (%ld)\n",
2562 PTR_ERR(data->dev));
2563 err = -ENOMEM;
2564 goto failed_drvdata;
2565 }
2566 data->dev->driver = &mac80211_hwsim_driver.driver;
2567 err = device_bind_driver(data->dev);
2568 if (err != 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002569 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002570 err);
Junjie Mao805dbe12014-10-28 09:31:47 +08002571 goto failed_bind;
Johannes Bergde0421d2014-01-06 23:12:12 +01002572 }
2573
2574 skb_queue_head_init(&data->pending);
2575
2576 SET_IEEE80211_DEV(hw, data->dev);
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002577 if (!param->perm_addr) {
2578 eth_zero_addr(addr);
2579 addr[0] = 0x02;
2580 addr[3] = idx >> 8;
2581 addr[4] = idx;
2582 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2583 /* Why need here second address ? */
2584 data->addresses[1].addr[0] |= 0x40;
2585 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2586 hw->wiphy->n_addresses = 2;
2587 hw->wiphy->addresses = data->addresses;
2588 /* possible address clash is checked at hash table insertion */
2589 } else {
2590 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2591 /* compatibility with automatically generated mac addr */
2592 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2593 hw->wiphy->n_addresses = 2;
2594 hw->wiphy->addresses = data->addresses;
2595 }
Johannes Bergde0421d2014-01-06 23:12:12 +01002596
Jukka Rissanen62759362014-10-31 14:48:47 +02002597 data->channels = param->channels;
2598 data->use_chanctx = param->use_chanctx;
Johannes Bergbc791092014-01-06 23:29:20 +01002599 data->idx = idx;
Jukka Rissanen62759362014-10-31 14:48:47 +02002600 data->destroy_on_close = param->destroy_on_close;
2601 if (info)
2602 data->portid = info->snd_portid;
Johannes Bergde0421d2014-01-06 23:12:12 +01002603
Luciano Coelho361c3e02014-02-10 15:23:03 +02002604 if (data->use_chanctx) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002605 hw->wiphy->max_scan_ssids = 255;
2606 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2607 hw->wiphy->max_remain_on_channel_duration = 1000;
Johannes Bergde0421d2014-01-06 23:12:12 +01002608 hw->wiphy->iface_combinations = &data->if_combination;
Jukka Rissanen62759362014-10-31 14:48:47 +02002609 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002610 data->if_combination = hwsim_if_comb_p2p_dev[0];
2611 else
2612 data->if_combination = hwsim_if_comb[0];
Ilan Peerf7736f52016-07-05 15:23:09 +03002613 hw->wiphy->n_iface_combinations = 1;
2614 /* For channels > 1 DFS is not allowed */
2615 data->if_combination.radar_detect_widths = 0;
Johannes Bergb59ec8d2014-02-07 10:44:50 +01002616 data->if_combination.num_different_channels = data->channels;
Jukka Rissanen62759362014-10-31 14:48:47 +02002617 } else if (param->p2p_device) {
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002618 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2619 hw->wiphy->n_iface_combinations =
2620 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
Johannes Bergde0421d2014-01-06 23:12:12 +01002621 } else {
2622 hw->wiphy->iface_combinations = hwsim_if_comb;
2623 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2624 }
2625
Johannes Berg661ef472015-11-24 18:29:45 +01002626 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
Johannes Bergde0421d2014-01-06 23:12:12 +01002627 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2628 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2629
2630 hw->queues = 5;
2631 hw->offchannel_tx_hw_queue = 4;
2632 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2633 BIT(NL80211_IFTYPE_AP) |
2634 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2635 BIT(NL80211_IFTYPE_P2P_GO) |
2636 BIT(NL80211_IFTYPE_ADHOC) |
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002637 BIT(NL80211_IFTYPE_MESH_POINT);
2638
Jukka Rissanen62759362014-10-31 14:48:47 +02002639 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002640 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002641
Johannes Berg30686bf2015-06-02 21:39:54 +02002642 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2643 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2644 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2645 ieee80211_hw_set(hw, QUEUE_CONTROL);
2646 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2647 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2648 ieee80211_hw_set(hw, MFP_CAPABLE);
2649 ieee80211_hw_set(hw, SIGNAL_DBM);
Johannes Berg33c2f532015-06-17 13:07:14 +02002650 ieee80211_hw_set(hw, TDLS_WIDER_BW);
Johannes Bergde0421d2014-01-06 23:12:12 +01002651 if (rctbl)
Johannes Berg30686bf2015-06-02 21:39:54 +02002652 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002653
2654 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2655 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02002656 WIPHY_FLAG_AP_UAPSD |
2657 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Eliad Peller0d8614b2014-09-10 14:07:36 +03002658 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2659 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2660 NL80211_FEATURE_STATIC_SMPS |
Johannes Berg339467b2014-06-12 22:41:00 +02002661 NL80211_FEATURE_DYNAMIC_SMPS |
2662 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
Jouni Malinen00eeccc2015-11-26 20:51:04 +02002663 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002664
2665 /* ask mac80211 to reserve space for magic */
2666 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2667 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2668 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2669
2670 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2671 sizeof(hwsim_channels_2ghz));
2672 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2673 sizeof(hwsim_channels_5ghz));
2674 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2675
Johannes Berg57fbcce2016-04-12 15:56:15 +02002676 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002677 struct ieee80211_supported_band *sband = &data->bands[band];
2678 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02002679 case NL80211_BAND_2GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002680 sband->channels = data->channels_2ghz;
2681 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2682 sband->bitrates = data->rates;
2683 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2684 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002685 case NL80211_BAND_5GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002686 sband->channels = data->channels_5ghz;
2687 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2688 sband->bitrates = data->rates + 4;
2689 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
Ilan Peerf33f8482015-04-07 19:05:21 +03002690
2691 sband->vht_cap.vht_supported = true;
2692 sband->vht_cap.cap =
2693 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2694 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2695 IEEE80211_VHT_CAP_RXLDPC |
2696 IEEE80211_VHT_CAP_SHORT_GI_80 |
2697 IEEE80211_VHT_CAP_SHORT_GI_160 |
2698 IEEE80211_VHT_CAP_TXSTBC |
2699 IEEE80211_VHT_CAP_RXSTBC_1 |
2700 IEEE80211_VHT_CAP_RXSTBC_2 |
2701 IEEE80211_VHT_CAP_RXSTBC_3 |
2702 IEEE80211_VHT_CAP_RXSTBC_4 |
2703 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2704 sband->vht_cap.vht_mcs.rx_mcs_map =
Ilan Peer8aca9b22015-04-07 19:05:22 +03002705 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2706 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002707 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002708 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2709 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002710 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2711 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002712 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
Ilan Peerf33f8482015-04-07 19:05:21 +03002713 sband->vht_cap.vht_mcs.tx_mcs_map =
2714 sband->vht_cap.vht_mcs.rx_mcs_map;
Johannes Bergde0421d2014-01-06 23:12:12 +01002715 break;
2716 default:
2717 continue;
2718 }
2719
2720 sband->ht_cap.ht_supported = true;
2721 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2722 IEEE80211_HT_CAP_GRN_FLD |
Ben Greeare49786d2014-10-24 11:12:08 -07002723 IEEE80211_HT_CAP_SGI_20 |
Johannes Bergde0421d2014-01-06 23:12:12 +01002724 IEEE80211_HT_CAP_SGI_40 |
2725 IEEE80211_HT_CAP_DSSSCCK40;
2726 sband->ht_cap.ampdu_factor = 0x3;
2727 sband->ht_cap.ampdu_density = 0x6;
2728 memset(&sband->ht_cap.mcs, 0,
2729 sizeof(sband->ht_cap.mcs));
2730 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2731 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2732 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2733
2734 hw->wiphy->bands[band] = sband;
Johannes Bergde0421d2014-01-06 23:12:12 +01002735 }
2736
2737 /* By default all radios belong to the first group */
2738 data->group = 1;
2739 mutex_init(&data->mutex);
2740
Martin Willi100cb9f2016-05-09 18:33:59 +02002741 data->netgroup = hwsim_net_get_netgroup(net);
2742
Johannes Bergde0421d2014-01-06 23:12:12 +01002743 /* Enable frame retransmissions for lossy channels */
2744 hw->max_rates = 4;
2745 hw->max_rate_tries = 11;
2746
Jouni Malinend5d011b2015-02-26 15:26:54 +02002747 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2748 hw->wiphy->n_vendor_commands =
2749 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2750 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2751 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2752
Jukka Rissanen62759362014-10-31 14:48:47 +02002753 if (param->reg_strict)
Johannes Berg26b0e412014-01-10 13:37:38 +01002754 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002755 if (param->regd) {
Patrik Flykt93d638d2014-11-12 16:42:40 +02002756 data->regd = param->regd;
Johannes Berg26b0e412014-01-10 13:37:38 +01002757 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002758 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
Johannes Berg26b0e412014-01-10 13:37:38 +01002759 /* give the regulatory workqueue a chance to run */
2760 schedule_timeout_interruptible(1);
2761 }
2762
Jukka Rissanen62759362014-10-31 14:48:47 +02002763 if (param->no_vif)
Johannes Berg30686bf2015-06-02 21:39:54 +02002764 ieee80211_hw_set(hw, NO_AUTO_VIF);
Ben Greear9a0cb892014-10-22 12:23:04 -07002765
Andrew Zaborowskiae44b502017-02-10 04:50:23 +01002766 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2767
Johannes Bergde0421d2014-01-06 23:12:12 +01002768 err = ieee80211_register_hw(hw);
2769 if (err < 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002770 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002771 err);
2772 goto failed_hw;
2773 }
2774
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002775 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
Johannes Bergde0421d2014-01-06 23:12:12 +01002776
Patrik Flykt93d638d2014-11-12 16:42:40 +02002777 if (param->reg_alpha2) {
2778 data->alpha2[0] = param->reg_alpha2[0];
2779 data->alpha2[1] = param->reg_alpha2[1];
Jukka Rissanen62759362014-10-31 14:48:47 +02002780 regulatory_hint(hw->wiphy, param->reg_alpha2);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002781 }
Johannes Berg26b0e412014-01-10 13:37:38 +01002782
Johannes Bergde0421d2014-01-06 23:12:12 +01002783 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2784 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2785 debugfs_create_file("group", 0666, data->debugfs, data,
2786 &hwsim_fops_group);
Luciano Coelho361c3e02014-02-10 15:23:03 +02002787 if (!data->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002788 debugfs_create_file("dfs_simulate_radar", 0222,
2789 data->debugfs,
2790 data, &hwsim_simulate_radar);
2791
2792 tasklet_hrtimer_init(&data->beacon_timer,
2793 mac80211_hwsim_beacon,
Thomas Gleixner8fbcfeb2017-02-25 10:27:37 +00002794 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002795
2796 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002797 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2798 hwsim_rht_params);
2799 if (err < 0) {
2800 pr_debug("mac80211_hwsim: radio index %d already present\n",
2801 idx);
2802 spin_unlock_bh(&hwsim_radio_lock);
2803 goto failed_final_insert;
2804 }
2805
Johannes Bergde0421d2014-01-06 23:12:12 +01002806 list_add_tail(&data->list, &hwsim_radios);
2807 spin_unlock_bh(&hwsim_radio_lock);
2808
Jukka Rissanen62759362014-10-31 14:48:47 +02002809 if (idx > 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002810 hwsim_mcast_new_radio(idx, info, param);
Jukka Rissanen62759362014-10-31 14:48:47 +02002811
Johannes Bergbc791092014-01-06 23:29:20 +01002812 return idx;
Johannes Bergde0421d2014-01-06 23:12:12 +01002813
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002814failed_final_insert:
2815 debugfs_remove_recursive(data->debugfs);
2816 ieee80211_unregister_hw(data->hw);
Johannes Bergde0421d2014-01-06 23:12:12 +01002817failed_hw:
Junjie Mao805dbe12014-10-28 09:31:47 +08002818 device_release_driver(data->dev);
2819failed_bind:
Johannes Bergde0421d2014-01-06 23:12:12 +01002820 device_unregister(data->dev);
2821failed_drvdata:
2822 ieee80211_free_hw(hw);
2823failed:
2824 return err;
2825}
2826
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002827static void hwsim_mcast_del_radio(int id, const char *hwname,
2828 struct genl_info *info)
Johannes Berge4afb602014-01-06 22:43:11 +01002829{
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002830 struct sk_buff *skb;
2831 void *data;
2832 int ret;
2833
2834 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2835 if (!skb)
2836 return;
2837
2838 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2839 HWSIM_CMD_DEL_RADIO);
2840 if (!data)
2841 goto error;
2842
2843 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2844 if (ret < 0)
2845 goto error;
2846
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002847 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2848 hwname);
2849 if (ret < 0)
2850 goto error;
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002851
2852 genlmsg_end(skb, data);
2853
2854 hwsim_mcast_config_msg(skb, info);
2855
2856 return;
2857
2858error:
2859 nlmsg_free(skb);
2860}
2861
2862static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2863 const char *hwname,
2864 struct genl_info *info)
2865{
2866 hwsim_mcast_del_radio(data->idx, hwname, info);
Johannes Berge4afb602014-01-06 22:43:11 +01002867 debugfs_remove_recursive(data->debugfs);
2868 ieee80211_unregister_hw(data->hw);
2869 device_release_driver(data->dev);
2870 device_unregister(data->dev);
2871 ieee80211_free_hw(data->hw);
2872}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002873
Patrik Flykt93d638d2014-11-12 16:42:40 +02002874static int mac80211_hwsim_get_radio(struct sk_buff *skb,
2875 struct mac80211_hwsim_data *data,
2876 u32 portid, u32 seq,
2877 struct netlink_callback *cb, int flags)
2878{
2879 void *hdr;
2880 struct hwsim_new_radio_params param = { };
2881 int res = -EMSGSIZE;
2882
2883 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
2884 HWSIM_CMD_GET_RADIO);
2885 if (!hdr)
2886 return -EMSGSIZE;
2887
2888 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +01002889 genl_dump_check_consistent(cb, hdr);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002890
Patrik Flyktb0ad5e82014-11-21 15:08:02 +02002891 if (data->alpha2[0] && data->alpha2[1])
2892 param.reg_alpha2 = data->alpha2;
2893
Patrik Flykt93d638d2014-11-12 16:42:40 +02002894 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
2895 REGULATORY_STRICT_REG);
2896 param.p2p_device = !!(data->hw->wiphy->interface_modes &
2897 BIT(NL80211_IFTYPE_P2P_DEVICE));
2898 param.use_chanctx = data->use_chanctx;
2899 param.regd = data->regd;
2900 param.channels = data->channels;
2901 param.hwname = wiphy_name(data->hw->wiphy);
2902
2903 res = append_radio_msg(skb, data->idx, &param);
2904 if (res < 0)
2905 goto out_err;
2906
Johannes Berg053c0952015-01-16 22:09:00 +01002907 genlmsg_end(skb, hdr);
2908 return 0;
Patrik Flykt93d638d2014-11-12 16:42:40 +02002909
2910out_err:
2911 genlmsg_cancel(skb, hdr);
2912 return res;
2913}
2914
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002915static void mac80211_hwsim_free(void)
2916{
Johannes Berge4afb602014-01-06 22:43:11 +01002917 struct mac80211_hwsim_data *data;
Johannes Berg0e057d732008-09-12 00:39:22 +02002918
2919 spin_lock_bh(&hwsim_radio_lock);
Johannes Berge4afb602014-01-06 22:43:11 +01002920 while ((data = list_first_entry_or_null(&hwsim_radios,
2921 struct mac80211_hwsim_data,
2922 list))) {
2923 list_del(&data->list);
2924 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002925 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
2926 NULL);
Johannes Berge4afb602014-01-06 22:43:11 +01002927 spin_lock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002928 }
Johannes Berge4afb602014-01-06 22:43:11 +01002929 spin_unlock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002930 class_destroy(hwsim_class);
2931}
2932
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002933static const struct net_device_ops hwsim_netdev_ops = {
2934 .ndo_start_xmit = hwsim_mon_xmit,
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002935 .ndo_set_mac_address = eth_mac_addr,
2936 .ndo_validate_addr = eth_validate_addr,
2937};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002938
2939static void hwsim_mon_setup(struct net_device *dev)
2940{
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002941 dev->netdev_ops = &hwsim_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002942 dev->needs_free_netdev = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002943 ether_setup(dev);
Phil Sutter3db6da12015-08-18 10:30:43 +02002944 dev->priv_flags |= IFF_NO_QUEUE;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002945 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Joe Perches93803b32015-03-02 19:54:49 -08002946 eth_zero_addr(dev->dev_addr);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002947 dev->dev_addr[0] = 0x12;
2948}
2949
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002950static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
Javier Lopez78825132011-06-01 11:26:13 +02002951{
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002952 return rhashtable_lookup_fast(&hwsim_radios_rht,
2953 addr,
2954 hwsim_rht_params);
Javier Lopez78825132011-06-01 11:26:13 +02002955}
2956
Martin Willif21e4d82016-05-14 10:34:44 +02002957static void hwsim_register_wmediumd(struct net *net, u32 portid)
2958{
2959 struct mac80211_hwsim_data *data;
2960
2961 hwsim_net_set_wmediumd(net, portid);
2962
2963 spin_lock_bh(&hwsim_radio_lock);
2964 list_for_each_entry(data, &hwsim_radios, list) {
2965 if (data->netgroup == hwsim_net_get_netgroup(net))
2966 data->wmediumd = portid;
2967 }
2968 spin_unlock_bh(&hwsim_radio_lock);
2969}
2970
Javier Lopez78825132011-06-01 11:26:13 +02002971static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2972 struct genl_info *info)
2973{
2974
2975 struct ieee80211_hdr *hdr;
2976 struct mac80211_hwsim_data *data2;
2977 struct ieee80211_tx_info *txi;
2978 struct hwsim_tx_rate *tx_attempts;
Johannes Berg959eb2f2015-11-06 11:57:23 +01002979 u64 ret_skb_cookie;
Javier Lopez78825132011-06-01 11:26:13 +02002980 struct sk_buff *skb, *tmp;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002981 const u8 *src;
Javier Lopez78825132011-06-01 11:26:13 +02002982 unsigned int hwsim_flags;
Javier Lopez78825132011-06-01 11:26:13 +02002983 int i;
2984 bool found = false;
2985
2986 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002987 !info->attrs[HWSIM_ATTR_FLAGS] ||
2988 !info->attrs[HWSIM_ATTR_COOKIE] ||
Martin Willi62397da2016-05-13 12:41:48 +02002989 !info->attrs[HWSIM_ATTR_SIGNAL] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002990 !info->attrs[HWSIM_ATTR_TX_INFO])
Javier Lopez78825132011-06-01 11:26:13 +02002991 goto out;
2992
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002993 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
Javier Lopez78825132011-06-01 11:26:13 +02002994 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
Johannes Berg959eb2f2015-11-06 11:57:23 +01002995 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
Javier Lopez78825132011-06-01 11:26:13 +02002996
2997 data2 = get_hwsim_data_ref_from_addr(src);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002998 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02002999 goto out;
3000
Martin Willif21e4d82016-05-14 10:34:44 +02003001 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3002 goto out;
3003
3004 if (info->snd_portid != data2->wmediumd)
3005 goto out;
3006
Javier Lopez78825132011-06-01 11:26:13 +02003007 /* look for the skb matching the cookie passed back from user */
3008 skb_queue_walk_safe(&data2->pending, skb, tmp) {
Johannes Berg959eb2f2015-11-06 11:57:23 +01003009 u64 skb_cookie;
3010
3011 txi = IEEE80211_SKB_CB(skb);
3012 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3013
3014 if (skb_cookie == ret_skb_cookie) {
Javier Lopez78825132011-06-01 11:26:13 +02003015 skb_unlink(skb, &data2->pending);
3016 found = true;
3017 break;
3018 }
3019 }
3020
3021 /* not found */
3022 if (!found)
3023 goto out;
3024
3025 /* Tx info received because the frame was broadcasted on user space,
3026 so we get all the necessary info: tx attempts and skb control buff */
3027
3028 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3029 info->attrs[HWSIM_ATTR_TX_INFO]);
3030
3031 /* now send back TX status */
3032 txi = IEEE80211_SKB_CB(skb);
3033
Javier Lopez78825132011-06-01 11:26:13 +02003034 ieee80211_tx_info_clear_status(txi);
3035
3036 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3037 txi->status.rates[i].idx = tx_attempts[i].idx;
3038 txi->status.rates[i].count = tx_attempts[i].count;
Javier Lopez78825132011-06-01 11:26:13 +02003039 }
3040
3041 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3042
3043 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3044 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3045 if (skb->len >= 16) {
3046 hdr = (struct ieee80211_hdr *) skb->data;
Javier Lopezc0e30762013-12-16 10:01:07 -08003047 mac80211_hwsim_monitor_ack(data2->channel,
Johannes Berge8261172012-07-27 19:48:26 +02003048 hdr->addr2);
Javier Lopez78825132011-06-01 11:26:13 +02003049 }
Qasim Javed06cf5c42012-06-05 01:25:44 -05003050 txi->flags |= IEEE80211_TX_STAT_ACK;
Javier Lopez78825132011-06-01 11:26:13 +02003051 }
3052 ieee80211_tx_status_irqsafe(data2->hw, skb);
3053 return 0;
3054out:
3055 return -EINVAL;
3056
3057}
3058
3059static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3060 struct genl_info *info)
3061{
Johannes Berge8261172012-07-27 19:48:26 +02003062 struct mac80211_hwsim_data *data2;
Javier Lopez78825132011-06-01 11:26:13 +02003063 struct ieee80211_rx_status rx_status;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003064 const u8 *dst;
Javier Lopez78825132011-06-01 11:26:13 +02003065 int frame_data_len;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003066 void *frame_data;
Javier Lopez78825132011-06-01 11:26:13 +02003067 struct sk_buff *skb = NULL;
3068
3069 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
Johannes Berge8261172012-07-27 19:48:26 +02003070 !info->attrs[HWSIM_ATTR_FRAME] ||
3071 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3072 !info->attrs[HWSIM_ATTR_SIGNAL])
Javier Lopez78825132011-06-01 11:26:13 +02003073 goto out;
3074
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003075 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
Javier Lopez78825132011-06-01 11:26:13 +02003076 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003077 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
Javier Lopez78825132011-06-01 11:26:13 +02003078
3079 /* Allocate new skb here */
3080 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3081 if (skb == NULL)
3082 goto err;
3083
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003084 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
Javier Lopez78825132011-06-01 11:26:13 +02003085 goto err;
3086
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003087 /* Copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02003088 skb_put_data(skb, frame_data, frame_data_len);
Javier Lopez78825132011-06-01 11:26:13 +02003089
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003090 data2 = get_hwsim_data_ref_from_addr(dst);
3091 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003092 goto out;
3093
Martin Willif21e4d82016-05-14 10:34:44 +02003094 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3095 goto out;
3096
3097 if (info->snd_portid != data2->wmediumd)
3098 goto out;
3099
Javier Lopez78825132011-06-01 11:26:13 +02003100 /* check if radio is configured properly */
3101
Johannes Berge8261172012-07-27 19:48:26 +02003102 if (data2->idle || !data2->started)
Javier Lopez78825132011-06-01 11:26:13 +02003103 goto out;
3104
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003105 /* A frame is received from user space */
Javier Lopez78825132011-06-01 11:26:13 +02003106 memset(&rx_status, 0, sizeof(rx_status));
Adam Welle641cf2a2015-12-01 17:13:52 -05003107 if (info->attrs[HWSIM_ATTR_FREQ]) {
3108 /* throw away off-channel packets, but allow both the temporary
3109 * ("hw" scan/remain-on-channel) and regular channel, since the
3110 * internal datapath also allows this
3111 */
3112 mutex_lock(&data2->mutex);
3113 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3114
3115 if (rx_status.freq != data2->channel->center_freq &&
3116 (!data2->tmp_chan ||
3117 rx_status.freq != data2->tmp_chan->center_freq)) {
3118 mutex_unlock(&data2->mutex);
3119 goto out;
3120 }
3121 mutex_unlock(&data2->mutex);
3122 } else {
3123 rx_status.freq = data2->channel->center_freq;
3124 }
3125
Javier Lopez78825132011-06-01 11:26:13 +02003126 rx_status.band = data2->channel->band;
3127 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3128 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3129
3130 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
Ben Greear2155c3f2014-10-22 12:22:58 -07003131 data2->rx_pkts++;
3132 data2->rx_bytes += skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02003133 ieee80211_rx_irqsafe(data2->hw, skb);
3134
3135 return 0;
3136err:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003137 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003138out:
3139 dev_kfree_skb(skb);
3140 return -EINVAL;
3141}
3142
3143static int hwsim_register_received_nl(struct sk_buff *skb_2,
3144 struct genl_info *info)
3145{
Martin Willif21e4d82016-05-14 10:34:44 +02003146 struct net *net = genl_info_net(info);
Johannes Bergc5ac0852014-01-07 23:28:08 +01003147 struct mac80211_hwsim_data *data;
3148 int chans = 1;
3149
3150 spin_lock_bh(&hwsim_radio_lock);
3151 list_for_each_entry(data, &hwsim_radios, list)
3152 chans = max(chans, data->channels);
3153 spin_unlock_bh(&hwsim_radio_lock);
3154
3155 /* In the future we should revise the userspace API and allow it
3156 * to set a flag that it does support multi-channel, then we can
3157 * let this pass conditionally on the flag.
3158 * For current userspace, prohibit it since it won't work right.
3159 */
3160 if (chans > 1)
3161 return -EOPNOTSUPP;
3162
Martin Willif21e4d82016-05-14 10:34:44 +02003163 if (hwsim_net_get_wmediumd(net))
Johannes Berg85ca8fc2014-01-07 23:21:34 +01003164 return -EBUSY;
Javier Lopez78825132011-06-01 11:26:13 +02003165
Martin Willif21e4d82016-05-14 10:34:44 +02003166 hwsim_register_wmediumd(net, info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003167
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003168 pr_debug("mac80211_hwsim: received a REGISTER, "
Eric W. Biederman15e47302012-09-07 20:12:54 +00003169 "switching to wmediumd mode with pid %d\n", info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003170
3171 return 0;
Javier Lopez78825132011-06-01 11:26:13 +02003172}
3173
Jukka Rissanen62759362014-10-31 14:48:47 +02003174static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc791092014-01-06 23:29:20 +01003175{
Jukka Rissanen62759362014-10-31 14:48:47 +02003176 struct hwsim_new_radio_params param = { 0 };
Johannes Bergff4dd732017-02-27 17:15:28 +01003177 const char *hwname = NULL;
Ben Hutchings67bd5232017-11-10 18:48:50 +00003178 int ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02003179
3180 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3181 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3182 param.channels = channels;
3183 param.destroy_on_close =
3184 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
Johannes Bergbc791092014-01-06 23:29:20 +01003185
3186 if (info->attrs[HWSIM_ATTR_CHANNELS])
Jukka Rissanen62759362014-10-31 14:48:47 +02003187 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
Johannes Bergbc791092014-01-06 23:29:20 +01003188
Johannes Berg51a1aaa2018-01-15 09:32:36 +01003189 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3190 GENL_SET_ERR_MSG(info, "too many channels specified");
3191 return -EINVAL;
3192 }
3193
Ben Greear9a0cb892014-10-22 12:23:04 -07003194 if (info->attrs[HWSIM_ATTR_NO_VIF])
Jukka Rissanen62759362014-10-31 14:48:47 +02003195 param.no_vif = true;
Ben Greear9a0cb892014-10-22 12:23:04 -07003196
Johannes Bergff4dd732017-02-27 17:15:28 +01003197 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3198 hwname = kasprintf(GFP_KERNEL, "%.*s",
3199 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3200 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3201 if (!hwname)
3202 return -ENOMEM;
3203 param.hwname = hwname;
3204 }
Ben Greear5cd89262014-10-22 12:23:02 -07003205
Luciano Coelho361c3e02014-02-10 15:23:03 +02003206 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
Jukka Rissanen62759362014-10-31 14:48:47 +02003207 param.use_chanctx = true;
Luciano Coelho361c3e02014-02-10 15:23:03 +02003208 else
Jukka Rissanen62759362014-10-31 14:48:47 +02003209 param.use_chanctx = (param.channels > 1);
Luciano Coelho361c3e02014-02-10 15:23:03 +02003210
Johannes Berg26b0e412014-01-10 13:37:38 +01003211 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
Jukka Rissanen62759362014-10-31 14:48:47 +02003212 param.reg_alpha2 =
3213 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
Johannes Berg26b0e412014-01-10 13:37:38 +01003214
3215 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3216 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3217
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003218 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3219 kfree(hwname);
Johannes Berg26b0e412014-01-10 13:37:38 +01003220 return -EINVAL;
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003221 }
Jukka Rissanen62759362014-10-31 14:48:47 +02003222 param.regd = hwsim_world_regdom_custom[idx];
Johannes Berg26b0e412014-01-10 13:37:38 +01003223 }
3224
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01003225 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3226 if (!is_valid_ether_addr(
3227 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3228 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3229 NL_SET_BAD_ATTR(info->extack,
3230 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3231 return -EINVAL;
3232 }
3233
3234
3235 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3236 }
3237
Ben Hutchings67bd5232017-11-10 18:48:50 +00003238 ret = mac80211_hwsim_new_radio(info, &param);
3239 kfree(hwname);
3240 return ret;
Johannes Bergbc791092014-01-06 23:29:20 +01003241}
3242
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003243static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc791092014-01-06 23:29:20 +01003244{
3245 struct mac80211_hwsim_data *data;
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003246 s64 idx = -1;
3247 const char *hwname = NULL;
Johannes Bergbc791092014-01-06 23:29:20 +01003248
Johannes Bergff4dd732017-02-27 17:15:28 +01003249 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003250 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
Johannes Bergff4dd732017-02-27 17:15:28 +01003251 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3252 hwname = kasprintf(GFP_KERNEL, "%.*s",
3253 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3254 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3255 if (!hwname)
3256 return -ENOMEM;
3257 } else
Johannes Bergbc791092014-01-06 23:29:20 +01003258 return -EINVAL;
Johannes Bergbc791092014-01-06 23:29:20 +01003259
3260 spin_lock_bh(&hwsim_radio_lock);
3261 list_for_each_entry(data, &hwsim_radios, list) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003262 if (idx >= 0) {
3263 if (data->idx != idx)
3264 continue;
3265 } else {
Johannes Bergff4dd732017-02-27 17:15:28 +01003266 if (!hwname ||
3267 strcmp(hwname, wiphy_name(data->hw->wiphy)))
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003268 continue;
3269 }
3270
Martin Willi100cb9f2016-05-09 18:33:59 +02003271 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3272 continue;
3273
Johannes Bergbc791092014-01-06 23:29:20 +01003274 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003275 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3276 hwsim_rht_params);
Johannes Bergbc791092014-01-06 23:29:20 +01003277 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003278 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3279 info);
Johannes Bergff4dd732017-02-27 17:15:28 +01003280 kfree(hwname);
Johannes Bergbc791092014-01-06 23:29:20 +01003281 return 0;
3282 }
3283 spin_unlock_bh(&hwsim_radio_lock);
3284
Johannes Bergff4dd732017-02-27 17:15:28 +01003285 kfree(hwname);
Johannes Bergbc791092014-01-06 23:29:20 +01003286 return -ENODEV;
Javier Lopez78825132011-06-01 11:26:13 +02003287}
3288
Patrik Flykt93d638d2014-11-12 16:42:40 +02003289static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3290{
3291 struct mac80211_hwsim_data *data;
3292 struct sk_buff *skb;
3293 int idx, res = -ENODEV;
3294
3295 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3296 return -EINVAL;
3297 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3298
3299 spin_lock_bh(&hwsim_radio_lock);
3300 list_for_each_entry(data, &hwsim_radios, list) {
3301 if (data->idx != idx)
3302 continue;
3303
Martin Willi100cb9f2016-05-09 18:33:59 +02003304 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3305 continue;
3306
Jia-Ju Bai162bd5e2017-12-12 17:26:36 +08003307 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003308 if (!skb) {
3309 res = -ENOMEM;
3310 goto out_err;
3311 }
3312
3313 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3314 info->snd_seq, NULL, 0);
3315 if (res < 0) {
3316 nlmsg_free(skb);
3317 goto out_err;
3318 }
3319
3320 genlmsg_reply(skb, info);
3321 break;
3322 }
3323
3324out_err:
3325 spin_unlock_bh(&hwsim_radio_lock);
3326
3327 return res;
3328}
3329
3330static int hwsim_dump_radio_nl(struct sk_buff *skb,
3331 struct netlink_callback *cb)
3332{
3333 int idx = cb->args[0];
3334 struct mac80211_hwsim_data *data = NULL;
3335 int res;
3336
3337 spin_lock_bh(&hwsim_radio_lock);
3338
3339 if (idx == hwsim_radio_idx)
3340 goto done;
3341
3342 list_for_each_entry(data, &hwsim_radios, list) {
3343 if (data->idx < idx)
3344 continue;
3345
Martin Willi100cb9f2016-05-09 18:33:59 +02003346 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3347 continue;
3348
Patrik Flykt93d638d2014-11-12 16:42:40 +02003349 res = mac80211_hwsim_get_radio(skb, data,
3350 NETLINK_CB(cb->skb).portid,
3351 cb->nlh->nlmsg_seq, cb,
3352 NLM_F_MULTI);
3353 if (res < 0)
3354 break;
3355
3356 idx = data->idx + 1;
3357 }
3358
3359 cb->args[0] = idx;
3360
3361done:
3362 spin_unlock_bh(&hwsim_radio_lock);
3363 return skb->len;
3364}
3365
Javier Lopez78825132011-06-01 11:26:13 +02003366/* Generic Netlink operations array */
Johannes Berg4534de82013-11-14 17:14:46 +01003367static const struct genl_ops hwsim_ops[] = {
Javier Lopez78825132011-06-01 11:26:13 +02003368 {
3369 .cmd = HWSIM_CMD_REGISTER,
3370 .policy = hwsim_genl_policy,
3371 .doit = hwsim_register_received_nl,
Martin Willif21e4d82016-05-14 10:34:44 +02003372 .flags = GENL_UNS_ADMIN_PERM,
Javier Lopez78825132011-06-01 11:26:13 +02003373 },
3374 {
3375 .cmd = HWSIM_CMD_FRAME,
3376 .policy = hwsim_genl_policy,
3377 .doit = hwsim_cloned_frame_received_nl,
3378 },
3379 {
3380 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3381 .policy = hwsim_genl_policy,
3382 .doit = hwsim_tx_info_frame_received_nl,
3383 },
Johannes Bergbc791092014-01-06 23:29:20 +01003384 {
Jukka Rissanen62759362014-10-31 14:48:47 +02003385 .cmd = HWSIM_CMD_NEW_RADIO,
Johannes Bergbc791092014-01-06 23:29:20 +01003386 .policy = hwsim_genl_policy,
Jukka Rissanen62759362014-10-31 14:48:47 +02003387 .doit = hwsim_new_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003388 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc791092014-01-06 23:29:20 +01003389 },
3390 {
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003391 .cmd = HWSIM_CMD_DEL_RADIO,
Johannes Bergbc791092014-01-06 23:29:20 +01003392 .policy = hwsim_genl_policy,
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003393 .doit = hwsim_del_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003394 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc791092014-01-06 23:29:20 +01003395 },
Patrik Flykt93d638d2014-11-12 16:42:40 +02003396 {
3397 .cmd = HWSIM_CMD_GET_RADIO,
3398 .policy = hwsim_genl_policy,
3399 .doit = hwsim_get_radio_nl,
3400 .dumpit = hwsim_dump_radio_nl,
3401 },
Javier Lopez78825132011-06-01 11:26:13 +02003402};
3403
Johannes Berg56989f62016-10-24 14:40:05 +02003404static struct genl_family hwsim_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003405 .name = "MAC80211_HWSIM",
3406 .version = 1,
3407 .maxattr = HWSIM_ATTR_MAX,
3408 .netnsok = true,
3409 .module = THIS_MODULE,
3410 .ops = hwsim_ops,
3411 .n_ops = ARRAY_SIZE(hwsim_ops),
3412 .mcgrps = hwsim_mcgrps,
3413 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3414};
3415
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003416static void destroy_radio(struct work_struct *work)
3417{
3418 struct mac80211_hwsim_data *data =
3419 container_of(work, struct mac80211_hwsim_data, destroy_work);
3420
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003421 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003422}
3423
3424static void remove_user_radios(u32 portid)
3425{
3426 struct mac80211_hwsim_data *entry, *tmp;
3427
3428 spin_lock_bh(&hwsim_radio_lock);
3429 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3430 if (entry->destroy_on_close && entry->portid == portid) {
3431 list_del(&entry->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003432 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3433 hwsim_rht_params);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003434 INIT_WORK(&entry->destroy_work, destroy_radio);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003435 queue_work(hwsim_wq, &entry->destroy_work);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003436 }
3437 }
3438 spin_unlock_bh(&hwsim_radio_lock);
3439}
3440
Javier Lopez78825132011-06-01 11:26:13 +02003441static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3442 unsigned long state,
3443 void *_notify)
3444{
3445 struct netlink_notify *notify = _notify;
3446
3447 if (state != NETLINK_URELEASE)
3448 return NOTIFY_DONE;
3449
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003450 remove_user_radios(notify->portid);
3451
Martin Willif21e4d82016-05-14 10:34:44 +02003452 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
Javier Lopez78825132011-06-01 11:26:13 +02003453 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3454 " socket, switching to perfect channel medium\n");
Martin Willif21e4d82016-05-14 10:34:44 +02003455 hwsim_register_wmediumd(notify->net, 0);
Javier Lopez78825132011-06-01 11:26:13 +02003456 }
3457 return NOTIFY_DONE;
3458
3459}
3460
3461static struct notifier_block hwsim_netlink_notifier = {
3462 .notifier_call = mac80211_hwsim_netlink_notify,
3463};
3464
Johannes Berg56989f62016-10-24 14:40:05 +02003465static int __init hwsim_init_netlink(void)
Javier Lopez78825132011-06-01 11:26:13 +02003466{
3467 int rc;
Johannes Berge8261172012-07-27 19:48:26 +02003468
Javier Lopez78825132011-06-01 11:26:13 +02003469 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3470
Johannes Berg489111e2016-10-24 14:40:03 +02003471 rc = genl_register_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003472 if (rc)
3473 goto failure;
3474
3475 rc = netlink_register_notifier(&hwsim_netlink_notifier);
Su Kang Yin2459cd82015-08-07 16:54:10 +08003476 if (rc) {
3477 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003478 goto failure;
Su Kang Yin2459cd82015-08-07 16:54:10 +08003479 }
Javier Lopez78825132011-06-01 11:26:13 +02003480
3481 return 0;
3482
3483failure:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003484 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003485 return -EINVAL;
3486}
3487
Martin Willi100cb9f2016-05-09 18:33:59 +02003488static __net_init int hwsim_init_net(struct net *net)
3489{
3490 hwsim_net_set_netgroup(net);
3491
3492 return 0;
3493}
3494
3495static void __net_exit hwsim_exit_net(struct net *net)
3496{
3497 struct mac80211_hwsim_data *data, *tmp;
3498
3499 spin_lock_bh(&hwsim_radio_lock);
3500 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3501 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3502 continue;
3503
3504 /* Radios created in init_net are returned to init_net. */
3505 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3506 continue;
3507
3508 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003509 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3510 hwsim_rht_params);
Martin Willi100cb9f2016-05-09 18:33:59 +02003511 INIT_WORK(&data->destroy_work, destroy_radio);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003512 queue_work(hwsim_wq, &data->destroy_work);
Martin Willi100cb9f2016-05-09 18:33:59 +02003513 }
3514 spin_unlock_bh(&hwsim_radio_lock);
3515}
3516
3517static struct pernet_operations hwsim_net_ops = {
3518 .init = hwsim_init_net,
3519 .exit = hwsim_exit_net,
3520 .id = &hwsim_net_id,
3521 .size = sizeof(struct hwsim_net),
3522};
3523
Javier Lopez78825132011-06-01 11:26:13 +02003524static void hwsim_exit_netlink(void)
3525{
Javier Lopez78825132011-06-01 11:26:13 +02003526 /* unregister the notifier */
3527 netlink_unregister_notifier(&hwsim_netlink_notifier);
3528 /* unregister the family */
Johannes Bergcf7a6b22014-01-06 21:55:14 +01003529 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003530}
3531
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003532static int __init init_mac80211_hwsim(void)
3533{
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003534 int i, err;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003535
Johannes Bergbc791092014-01-06 23:29:20 +01003536 if (radios < 0 || radios > 100)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003537 return -EINVAL;
3538
Johannes Berge8261172012-07-27 19:48:26 +02003539 if (channels < 1)
3540 return -EINVAL;
3541
Johannes Berg0e057d732008-09-12 00:39:22 +02003542 spin_lock_init(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003543
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003544 hwsim_wq = alloc_workqueue("hwsim_wq",WQ_MEM_RECLAIM,0);
3545 if (!hwsim_wq)
3546 return -ENOMEM;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003547 rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003548
Martin Willi100cb9f2016-05-09 18:33:59 +02003549 err = register_pernet_device(&hwsim_net_ops);
Martin Pitt9ea927742013-04-08 11:30:01 +02003550 if (err)
3551 return err;
3552
Martin Willi100cb9f2016-05-09 18:33:59 +02003553 err = platform_driver_register(&mac80211_hwsim_driver);
3554 if (err)
3555 goto out_unregister_pernet;
3556
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003557 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
Martin Pitt9ea927742013-04-08 11:30:01 +02003558 if (IS_ERR(hwsim_class)) {
3559 err = PTR_ERR(hwsim_class);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003560 goto out_unregister_driver;
Martin Pitt9ea927742013-04-08 11:30:01 +02003561 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003562
Jukka Rissanen62759362014-10-31 14:48:47 +02003563 err = hwsim_init_netlink();
3564 if (err < 0)
3565 goto out_unregister_driver;
3566
Johannes Berg0e057d732008-09-12 00:39:22 +02003567 for (i = 0; i < radios; i++) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003568 struct hwsim_new_radio_params param = { 0 };
3569
3570 param.channels = channels;
Johannes Berg0e057d732008-09-12 00:39:22 +02003571
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003572 switch (regtest) {
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003573 case HWSIM_REGTEST_DIFF_COUNTRY:
Johannes Berg26b0e412014-01-10 13:37:38 +01003574 if (i < ARRAY_SIZE(hwsim_alpha2s))
Jukka Rissanen62759362014-10-31 14:48:47 +02003575 param.reg_alpha2 = hwsim_alpha2s[i];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003576 break;
3577 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3578 if (!i)
Jukka Rissanen62759362014-10-31 14:48:47 +02003579 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003580 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003581 case HWSIM_REGTEST_STRICT_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003582 param.reg_strict = true;
Johannes Berg26b0e412014-01-10 13:37:38 +01003583 case HWSIM_REGTEST_DRIVER_REG_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003584 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003585 break;
Johannes Berg26b0e412014-01-10 13:37:38 +01003586 case HWSIM_REGTEST_WORLD_ROAM:
3587 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003588 param.regd = &hwsim_world_regdom_custom_01;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003589 break;
3590 case HWSIM_REGTEST_CUSTOM_WORLD:
Jukka Rissanen62759362014-10-31 14:48:47 +02003591 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003592 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003593 case HWSIM_REGTEST_CUSTOM_WORLD_2:
Johannes Berg26b0e412014-01-10 13:37:38 +01003594 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003595 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003596 else if (i == 1)
Jukka Rissanen62759362014-10-31 14:48:47 +02003597 param.regd = &hwsim_world_regdom_custom_02;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003598 break;
3599 case HWSIM_REGTEST_STRICT_FOLLOW:
Johannes Berg26b0e412014-01-10 13:37:38 +01003600 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003601 param.reg_strict = true;
3602 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003603 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003604 break;
3605 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
Johannes Berg26b0e412014-01-10 13:37:38 +01003606 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003607 param.reg_strict = true;
3608 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003609 } else if (i == 1) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003610 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003611 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003612 break;
3613 case HWSIM_REGTEST_ALL:
Johannes Berg26b0e412014-01-10 13:37:38 +01003614 switch (i) {
3615 case 0:
Jukka Rissanen62759362014-10-31 14:48:47 +02003616 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003617 break;
3618 case 1:
Jukka Rissanen62759362014-10-31 14:48:47 +02003619 param.regd = &hwsim_world_regdom_custom_02;
Johannes Berg26b0e412014-01-10 13:37:38 +01003620 break;
3621 case 2:
Jukka Rissanen62759362014-10-31 14:48:47 +02003622 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003623 break;
3624 case 3:
Jukka Rissanen62759362014-10-31 14:48:47 +02003625 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003626 break;
3627 case 4:
Jukka Rissanen62759362014-10-31 14:48:47 +02003628 param.reg_strict = true;
3629 param.reg_alpha2 = hwsim_alpha2s[2];
Johannes Berg26b0e412014-01-10 13:37:38 +01003630 break;
3631 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003632 break;
3633 default:
3634 break;
3635 }
3636
Jukka Rissanen62759362014-10-31 14:48:47 +02003637 param.p2p_device = support_p2p_device;
3638 param.use_chanctx = channels > 1;
3639
3640 err = mac80211_hwsim_new_radio(NULL, &param);
Johannes Bergbc791092014-01-06 23:29:20 +01003641 if (err < 0)
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003642 goto out_free_radios;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003643 }
3644
Tom Gundersenc835a672014-07-14 16:37:24 +02003645 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3646 hwsim_mon_setup);
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003647 if (hwsim_mon == NULL) {
3648 err = -ENOMEM;
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003649 goto out_free_radios;
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003650 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003651
Javier Lopez78825132011-06-01 11:26:13 +02003652 rtnl_lock();
Javier Lopez78825132011-06-01 11:26:13 +02003653 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003654 if (err < 0) {
3655 rtnl_unlock();
3656 goto out_free_radios;
3657 }
Javier Lopez78825132011-06-01 11:26:13 +02003658
3659 err = register_netdevice(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003660 if (err < 0) {
3661 rtnl_unlock();
3662 goto out_free_mon;
3663 }
Javier Lopez78825132011-06-01 11:26:13 +02003664 rtnl_unlock();
3665
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003666 return 0;
3667
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003668out_free_mon:
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003669 free_netdev(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003670out_free_radios:
Ian Schram3a33cc12008-07-21 13:19:35 -07003671 mac80211_hwsim_free();
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003672out_unregister_driver:
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003673 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003674out_unregister_pernet:
3675 unregister_pernet_device(&hwsim_net_ops);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003676 return err;
3677}
Johannes Bergf8fffc72012-07-27 15:16:02 +02003678module_init(init_mac80211_hwsim);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003679
3680static void __exit exit_mac80211_hwsim(void)
3681{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003682 pr_debug("mac80211_hwsim: unregister radios\n");
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003683
Javier Lopez78825132011-06-01 11:26:13 +02003684 hwsim_exit_netlink();
3685
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003686 mac80211_hwsim_free();
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003687 flush_workqueue(hwsim_wq);
3688
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003689 rhashtable_destroy(&hwsim_radios_rht);
Johannes Berg5d416352009-07-13 13:04:30 +02003690 unregister_netdev(hwsim_mon);
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003691 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003692 unregister_pernet_device(&hwsim_net_ops);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003693 destroy_workqueue(hwsim_wq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003694}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003695module_exit(exit_mac80211_hwsim);