blob: 1cf22e62e3dddafcbf3136f8ba07aa397b9bb088 [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 <[email protected]>
Javier Lopez78825132011-06-01 11:26:13 +02004 * Copyright (c) 2011, Javier Lopez <[email protected]>
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 Cardona2f40b9402012-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 Malinen85bbd80372017-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 Bergbc7910982014-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 Cardona2f40b9402012-03-05 17:20:37 -0800565 /* difference between this hw's clock and the real clock, in usecs */
Thomas Pedersen45034bfb2013-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 Cardona2f40b9402012-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 Bergbc7910982014-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 },
Javier Lopez78825132011-06-01 11:26:13 +0200640};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300641
Johannes Bergde0421d2014-01-06 23:12:12 +0100642static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
643 struct sk_buff *skb,
644 struct ieee80211_channel *chan);
645
646/* sysfs attributes */
647static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
648{
649 struct mac80211_hwsim_data *data = dat;
650 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
651 struct sk_buff *skb;
652 struct ieee80211_pspoll *pspoll;
653
654 if (!vp->assoc)
655 return;
656
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200657 wiphy_dbg(data->hw->wiphy,
658 "%s: send PS-Poll to %pM for aid %d\n",
659 __func__, vp->bssid, vp->aid);
Johannes Bergde0421d2014-01-06 23:12:12 +0100660
661 skb = dev_alloc_skb(sizeof(*pspoll));
662 if (!skb)
663 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200664 pspoll = skb_put(skb, sizeof(*pspoll));
Johannes Bergde0421d2014-01-06 23:12:12 +0100665 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
666 IEEE80211_STYPE_PSPOLL |
667 IEEE80211_FCTL_PM);
668 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
669 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
670 memcpy(pspoll->ta, mac, ETH_ALEN);
671
672 rcu_read_lock();
673 mac80211_hwsim_tx_frame(data->hw, skb,
674 rcu_dereference(vif->chanctx_conf)->def.chan);
675 rcu_read_unlock();
676}
677
678static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
679 struct ieee80211_vif *vif, int ps)
680{
681 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
682 struct sk_buff *skb;
683 struct ieee80211_hdr *hdr;
684
685 if (!vp->assoc)
686 return;
687
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200688 wiphy_dbg(data->hw->wiphy,
689 "%s: send data::nullfunc to %pM ps=%d\n",
690 __func__, vp->bssid, ps);
Johannes Bergde0421d2014-01-06 23:12:12 +0100691
692 skb = dev_alloc_skb(sizeof(*hdr));
693 if (!skb)
694 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200695 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
Johannes Bergde0421d2014-01-06 23:12:12 +0100696 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
697 IEEE80211_STYPE_NULLFUNC |
Adiel Alonib65c7b82017-12-18 12:14:04 +0200698 IEEE80211_FCTL_TODS |
Johannes Bergde0421d2014-01-06 23:12:12 +0100699 (ps ? IEEE80211_FCTL_PM : 0));
700 hdr->duration_id = cpu_to_le16(0);
701 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
702 memcpy(hdr->addr2, mac, ETH_ALEN);
703 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
704
705 rcu_read_lock();
706 mac80211_hwsim_tx_frame(data->hw, skb,
707 rcu_dereference(vif->chanctx_conf)->def.chan);
708 rcu_read_unlock();
709}
710
711
712static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
713 struct ieee80211_vif *vif)
714{
715 struct mac80211_hwsim_data *data = dat;
716 hwsim_send_nullfunc(data, mac, vif, 1);
717}
718
719static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
720 struct ieee80211_vif *vif)
721{
722 struct mac80211_hwsim_data *data = dat;
723 hwsim_send_nullfunc(data, mac, vif, 0);
724}
725
726static int hwsim_fops_ps_read(void *dat, u64 *val)
727{
728 struct mac80211_hwsim_data *data = dat;
729 *val = data->ps;
730 return 0;
731}
732
733static int hwsim_fops_ps_write(void *dat, u64 val)
734{
735 struct mac80211_hwsim_data *data = dat;
736 enum ps_mode old_ps;
737
738 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
739 val != PS_MANUAL_POLL)
740 return -EINVAL;
741
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200742 if (val == PS_MANUAL_POLL) {
743 if (data->ps != PS_ENABLED)
744 return -EINVAL;
745 local_bh_disable();
746 ieee80211_iterate_active_interfaces_atomic(
747 data->hw, IEEE80211_IFACE_ITER_NORMAL,
748 hwsim_send_ps_poll, data);
749 local_bh_enable();
750 return 0;
751 }
Johannes Bergde0421d2014-01-06 23:12:12 +0100752 old_ps = data->ps;
753 data->ps = val;
754
Johannes Berga809ca52015-01-09 11:29:50 +0100755 local_bh_disable();
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200756 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100757 ieee80211_iterate_active_interfaces_atomic(
758 data->hw, IEEE80211_IFACE_ITER_NORMAL,
759 hwsim_send_nullfunc_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100760 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100761 ieee80211_iterate_active_interfaces_atomic(
762 data->hw, IEEE80211_IFACE_ITER_NORMAL,
763 hwsim_send_nullfunc_no_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100764 }
Johannes Berga809ca52015-01-09 11:29:50 +0100765 local_bh_enable();
Johannes Bergde0421d2014-01-06 23:12:12 +0100766
767 return 0;
768}
769
770DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
771 "%llu\n");
772
773static int hwsim_write_simulate_radar(void *dat, u64 val)
774{
775 struct mac80211_hwsim_data *data = dat;
776
777 ieee80211_radar_detected(data->hw);
778
779 return 0;
780}
781
782DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
783 hwsim_write_simulate_radar, "%llu\n");
784
785static int hwsim_fops_group_read(void *dat, u64 *val)
786{
787 struct mac80211_hwsim_data *data = dat;
788 *val = data->group;
789 return 0;
790}
791
792static int hwsim_fops_group_write(void *dat, u64 val)
793{
794 struct mac80211_hwsim_data *data = dat;
795 data->group = val;
796 return 0;
797}
798
799DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
800 hwsim_fops_group_read, hwsim_fops_group_write,
801 "%llx\n");
802
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000803static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
804 struct net_device *dev)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300805{
806 /* TODO: allow packet injection */
807 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000808 return NETDEV_TX_OK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300809}
810
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800811static inline u64 mac80211_hwsim_get_tsf_raw(void)
812{
813 return ktime_to_us(ktime_get_real());
814}
815
Javier Cardona2f40b9402012-03-05 17:20:37 -0800816static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
817{
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800818 u64 now = mac80211_hwsim_get_tsf_raw();
Javier Cardona2f40b9402012-03-05 17:20:37 -0800819 return cpu_to_le64(now + data->tsf_offset);
820}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300821
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800822static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800823 struct ieee80211_vif *vif)
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800824{
825 struct mac80211_hwsim_data *data = hw->priv;
826 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
827}
828
829static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
830 struct ieee80211_vif *vif, u64 tsf)
831{
832 struct mac80211_hwsim_data *data = hw->priv;
Thomas Pedersen45034bfb2013-01-02 14:55:16 -0800833 u64 now = mac80211_hwsim_get_tsf(hw, vif);
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800834 u32 bcn_int = data->beacon_int;
Andrew Morton79211c82015-11-09 14:58:13 -0800835 u64 delta = abs(tsf - now);
Thomas Pedersen45034bfb2013-01-02 14:55:16 -0800836
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800837 /* adjust after beaconing with new timestamp at old TBTT */
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700838 if (tsf > now) {
839 data->tsf_offset += delta;
840 data->bcn_delta = do_div(delta, bcn_int);
841 } else {
842 data->tsf_offset -= delta;
Benjamin Beichler4fb7f8a2016-11-11 17:37:56 +0100843 data->bcn_delta = -(s64)do_div(delta, bcn_int);
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700844 }
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800845}
846
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300847static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +0200848 struct sk_buff *tx_skb,
849 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300850{
851 struct mac80211_hwsim_data *data = hw->priv;
852 struct sk_buff *skb;
853 struct hwsim_radiotap_hdr *hdr;
854 u16 flags;
855 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
856 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
857
Amit Khatri89f44d82015-11-30 12:46:52 +0530858 if (WARN_ON(!txrate))
859 return;
860
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300861 if (!netif_running(hwsim_mon))
862 return;
863
864 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
865 if (skb == NULL)
866 return;
867
Johannes Bergd58ff352017-06-16 14:29:23 +0200868 hdr = skb_push(skb, sizeof(*hdr));
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300869 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
870 hdr->hdr.it_pad = 0;
871 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
Jouni Malinenf248f102008-06-13 19:44:47 +0300872 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
873 (1 << IEEE80211_RADIOTAP_RATE) |
Javier Cardona2f40b9402012-03-05 17:20:37 -0800874 (1 << IEEE80211_RADIOTAP_TSFT) |
Jouni Malinenf248f102008-06-13 19:44:47 +0300875 (1 << IEEE80211_RADIOTAP_CHANNEL));
Javier Cardona2f40b9402012-03-05 17:20:37 -0800876 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300877 hdr->rt_flags = 0;
878 hdr->rt_rate = txrate->bitrate / 5;
Johannes Berge8261172012-07-27 19:48:26 +0200879 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300880 flags = IEEE80211_CHAN_2GHZ;
881 if (txrate->flags & IEEE80211_RATE_ERP_G)
882 flags |= IEEE80211_CHAN_OFDM;
883 else
884 flags |= IEEE80211_CHAN_CCK;
885 hdr->rt_chbitmask = cpu_to_le16(flags);
886
887 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000888 skb_reset_mac_header(skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300889 skb->ip_summed = CHECKSUM_UNNECESSARY;
890 skb->pkt_type = PACKET_OTHERHOST;
Jouni Malinenf248f102008-06-13 19:44:47 +0300891 skb->protocol = htons(ETH_P_802_2);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300892 memset(skb->cb, 0, sizeof(skb->cb));
893 netif_rx(skb);
894}
895
896
Johannes Berge8261172012-07-27 19:48:26 +0200897static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
898 const u8 *addr)
Jouni Malinen6c0852272009-11-01 11:31:45 +0200899{
Jouni Malinen6c0852272009-11-01 11:31:45 +0200900 struct sk_buff *skb;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200901 struct hwsim_radiotap_ack_hdr *hdr;
Jouni Malinen6c0852272009-11-01 11:31:45 +0200902 u16 flags;
903 struct ieee80211_hdr *hdr11;
904
905 if (!netif_running(hwsim_mon))
906 return;
907
908 skb = dev_alloc_skb(100);
909 if (skb == NULL)
910 return;
911
Johannes Berg4df864c2017-06-16 14:29:21 +0200912 hdr = skb_put(skb, sizeof(*hdr));
Jouni Malinen6c0852272009-11-01 11:31:45 +0200913 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
914 hdr->hdr.it_pad = 0;
915 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
916 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
917 (1 << IEEE80211_RADIOTAP_CHANNEL));
918 hdr->rt_flags = 0;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200919 hdr->pad = 0;
Johannes Berge8261172012-07-27 19:48:26 +0200920 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinen6c0852272009-11-01 11:31:45 +0200921 flags = IEEE80211_CHAN_2GHZ;
922 hdr->rt_chbitmask = cpu_to_le16(flags);
923
Johannes Berg4df864c2017-06-16 14:29:21 +0200924 hdr11 = skb_put(skb, 10);
Jouni Malinen6c0852272009-11-01 11:31:45 +0200925 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
926 IEEE80211_STYPE_ACK);
927 hdr11->duration_id = cpu_to_le16(0);
928 memcpy(hdr11->addr1, addr, ETH_ALEN);
929
930 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000931 skb_reset_mac_header(skb);
Jouni Malinen6c0852272009-11-01 11:31:45 +0200932 skb->ip_summed = CHECKSUM_UNNECESSARY;
933 skb->pkt_type = PACKET_OTHERHOST;
934 skb->protocol = htons(ETH_P_802_2);
935 memset(skb->cb, 0, sizeof(skb->cb));
936 netif_rx(skb);
937}
938
Johannes Berg3283e2862014-06-12 22:34:49 +0200939struct mac80211_hwsim_addr_match_data {
940 u8 addr[ETH_ALEN];
941 bool ret;
942};
943
944static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
945 struct ieee80211_vif *vif)
946{
947 struct mac80211_hwsim_addr_match_data *md = data;
948
949 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
950 md->ret = true;
951}
952
953static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
954 const u8 *addr)
955{
956 struct mac80211_hwsim_addr_match_data md = {
957 .ret = false,
958 };
959
Johannes Berg339467b2014-06-12 22:41:00 +0200960 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
961 return true;
962
Johannes Berg3283e2862014-06-12 22:34:49 +0200963 memcpy(md.addr, addr, ETH_ALEN);
964
965 ieee80211_iterate_active_interfaces_atomic(data->hw,
966 IEEE80211_IFACE_ITER_NORMAL,
967 mac80211_hwsim_addr_iter,
968 &md);
969
970 return md.ret;
971}
Jouni Malinen6c0852272009-11-01 11:31:45 +0200972
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200973static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
974 struct sk_buff *skb)
975{
976 switch (data->ps) {
977 case PS_DISABLED:
978 return true;
979 case PS_ENABLED:
980 return false;
981 case PS_AUTO_POLL:
982 /* TODO: accept (some) Beacons by default and other frames only
983 * if pending PS-Poll has been sent */
984 return true;
985 case PS_MANUAL_POLL:
986 /* Allow unicast frames to own address if there is a pending
987 * PS-Poll */
988 if (data->ps_poll_pending &&
Johannes Berg3283e2862014-06-12 22:34:49 +0200989 mac80211_hwsim_addr_match(data, skb->data + 4)) {
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200990 data->ps_poll_pending = false;
991 return true;
992 }
993 return false;
994 }
995
996 return true;
997}
998
Martin Willif21e4d82016-05-14 10:34:44 +0200999static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1000 struct sk_buff *skb, int portid)
1001{
1002 struct net *net;
1003 bool found = false;
1004 int res = -ENOENT;
1005
1006 rcu_read_lock();
1007 for_each_net_rcu(net) {
1008 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1009 res = genlmsg_unicast(net, skb, portid);
1010 found = true;
1011 break;
1012 }
1013 }
1014 rcu_read_unlock();
1015
1016 if (!found)
1017 nlmsg_free(skb);
1018
1019 return res;
1020}
1021
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001022static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1023{
1024 u16 result = 0;
1025
1026 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1027 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1028 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1029 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1030 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1031 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1032 if (rate->flags & IEEE80211_TX_RC_MCS)
1033 result |= MAC80211_HWSIM_TX_RC_MCS;
1034 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1035 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1036 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1037 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1038 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1039 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1040 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1041 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1042 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1043 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1044 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1045 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1046 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1047 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1048
1049 return result;
1050}
1051
Javier Lopez78825132011-06-01 11:26:13 +02001052static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1053 struct sk_buff *my_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001054 int dst_portid)
Javier Lopez78825132011-06-01 11:26:13 +02001055{
1056 struct sk_buff *skb;
1057 struct mac80211_hwsim_data *data = hw->priv;
1058 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1059 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1060 void *msg_head;
1061 unsigned int hwsim_flags = 0;
1062 int i;
1063 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001064 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
Johannes Berg959eb2f2015-11-06 11:57:23 +01001065 uintptr_t cookie;
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001066
Javier Lopez78825132011-06-01 11:26:13 +02001067 if (data->ps != PS_DISABLED)
1068 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1069 /* If the queue contains MAX_QUEUE skb's drop some */
1070 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1071 /* Droping until WARN_QUEUE level */
Ben Greear2155c3f2014-10-22 12:22:58 -07001072 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
Ben Greearb7bc9672014-09-25 14:22:18 -07001073 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
Ben Greear2155c3f2014-10-22 12:22:58 -07001074 data->tx_dropped++;
1075 }
Javier Lopez78825132011-06-01 11:26:13 +02001076 }
1077
Thomas Graf58050fc2012-06-28 03:57:45 +00001078 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Javier Lopez78825132011-06-01 11:26:13 +02001079 if (skb == NULL)
1080 goto nla_put_failure;
1081
1082 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1083 HWSIM_CMD_FRAME);
1084 if (msg_head == NULL) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001085 pr_debug("mac80211_hwsim: problem with msg_head\n");
Javier Lopez78825132011-06-01 11:26:13 +02001086 goto nla_put_failure;
1087 }
1088
Bob Copeland354210f2015-12-19 09:59:37 -05001089 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1090 ETH_ALEN, data->addresses[1].addr))
David S. Miller633c9382012-04-01 21:03:30 -04001091 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001092
1093 /* We get the skb->data */
David S. Miller633c9382012-04-01 21:03:30 -04001094 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1095 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001096
1097 /* We get the flags for this transmission, and we translate them to
1098 wmediumd flags */
1099
1100 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1101 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1102
1103 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1104 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1105
David S. Miller633c9382012-04-01 21:03:30 -04001106 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1107 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001108
Ben Greear4f86ed82014-10-27 15:04:46 -07001109 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1110 goto nla_put_failure;
1111
Javier Lopez78825132011-06-01 11:26:13 +02001112 /* We get the tx control (rate and retries) info*/
1113
1114 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1115 tx_attempts[i].idx = info->status.rates[i].idx;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001116 tx_attempts_flags[i].idx = info->status.rates[i].idx;
Javier Lopez78825132011-06-01 11:26:13 +02001117 tx_attempts[i].count = info->status.rates[i].count;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001118 tx_attempts_flags[i].flags =
1119 trans_tx_rate_flags_ieee2hwsim(
1120 &info->status.rates[i]);
Javier Lopez78825132011-06-01 11:26:13 +02001121 }
1122
David S. Miller633c9382012-04-01 21:03:30 -04001123 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1124 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1125 tx_attempts))
1126 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001127
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001128 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1129 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1130 tx_attempts_flags))
1131 goto nla_put_failure;
1132
Javier Lopez78825132011-06-01 11:26:13 +02001133 /* We create a cookie to identify this skb */
Johannes Berg959eb2f2015-11-06 11:57:23 +01001134 data->pending_cookie++;
1135 cookie = data->pending_cookie;
1136 info->rate_driver_data[0] = (void *)cookie;
Nicolas Dichtel08f4cbb2016-04-26 10:06:12 +02001137 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
David S. Miller633c9382012-04-01 21:03:30 -04001138 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001139
1140 genlmsg_end(skb, msg_head);
Martin Willif21e4d82016-05-14 10:34:44 +02001141 if (hwsim_unicast_netgroup(data, skb, dst_portid))
Bob Copelandf2831e22015-02-05 08:45:43 -05001142 goto err_free_txskb;
Javier Lopez78825132011-06-01 11:26:13 +02001143
1144 /* Enqueue the packet */
1145 skb_queue_tail(&data->pending, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001146 data->tx_pkts++;
1147 data->tx_bytes += my_skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02001148 return;
1149
1150nla_put_failure:
Bob Copelandf2831e22015-02-05 08:45:43 -05001151 nlmsg_free(skb);
1152err_free_txskb:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001153 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Ben Greearb7bc9672014-09-25 14:22:18 -07001154 ieee80211_free_txskb(hw, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001155 data->tx_failed++;
Javier Lopez78825132011-06-01 11:26:13 +02001156}
1157
Johannes Berge8261172012-07-27 19:48:26 +02001158static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1159 struct ieee80211_channel *c2)
1160{
1161 if (!c1 || !c2)
1162 return false;
1163
1164 return c1->center_freq == c2->center_freq;
1165}
1166
1167struct tx_iter_data {
1168 struct ieee80211_channel *channel;
1169 bool receive;
1170};
1171
1172static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1173 struct ieee80211_vif *vif)
1174{
1175 struct tx_iter_data *data = _data;
1176
1177 if (!vif->chanctx_conf)
1178 return;
1179
1180 if (!hwsim_chans_compat(data->channel,
Johannes Berg4bf88532012-11-09 11:39:59 +01001181 rcu_dereference(vif->chanctx_conf)->def.chan))
Johannes Berge8261172012-07-27 19:48:26 +02001182 return;
1183
1184 data->receive = true;
1185}
1186
Johannes Berg1f7bba72014-11-06 22:56:36 +01001187static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1188{
1189 /*
1190 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1191 * e.g. like this:
1192 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1193 * (but you should use a valid OUI, not that)
1194 *
1195 * If anyone wants to 'donate' a radiotap OUI/subns code
1196 * please send a patch removing this #ifdef and changing
1197 * the values accordingly.
1198 */
1199#ifdef HWSIM_RADIOTAP_OUI
1200 struct ieee80211_vendor_radiotap *rtap;
1201
1202 /*
1203 * Note that this code requires the headroom in the SKB
1204 * that was allocated earlier.
1205 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001206 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
Johannes Berg1f7bba72014-11-06 22:56:36 +01001207 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1208 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1209 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1210 rtap->subns = 127;
1211
1212 /*
1213 * Radiotap vendor namespaces can (and should) also be
1214 * split into fields by using the standard radiotap
1215 * presence bitmap mechanism. Use just BIT(0) here for
1216 * the presence bitmap.
1217 */
1218 rtap->present = BIT(0);
1219 /* We have 8 bytes of (dummy) data */
1220 rtap->len = 8;
1221 /* For testing, also require it to be aligned */
1222 rtap->align = 8;
1223 /* And also test that padding works, 4 bytes */
1224 rtap->pad = 4;
1225 /* push the data */
1226 memcpy(rtap->data, "ABCDEFGH", 8);
1227 /* make sure to clear padding, mac80211 doesn't */
1228 memset(rtap->data + 8, 0, 4);
1229
1230 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1231#endif
1232}
1233
Javier Lopez78825132011-06-01 11:26:13 +02001234static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +02001235 struct sk_buff *skb,
1236 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001237{
Johannes Berg0e057d732008-09-12 00:39:22 +02001238 struct mac80211_hwsim_data *data = hw->priv, *data2;
1239 bool ack = false;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001240 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001241 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001242 struct ieee80211_rx_status rx_status;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001243 u64 now;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001244
1245 memset(&rx_status, 0, sizeof(rx_status));
Thomas Pedersenf4bda332012-11-13 10:46:27 -08001246 rx_status.flag |= RX_FLAG_MACTIME_START;
Johannes Berge8261172012-07-27 19:48:26 +02001247 rx_status.freq = chan->center_freq;
1248 rx_status.band = chan->band;
Karl Beldandad63302013-04-15 17:09:30 +02001249 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1250 rx_status.rate_idx =
1251 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
Johannes Berg8613c942017-04-26 13:51:41 +02001252 rx_status.nss =
Karl Beldandad63302013-04-15 17:09:30 +02001253 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
Johannes Bergda6a4352017-04-26 12:14:59 +02001254 rx_status.encoding = RX_ENC_VHT;
Karl Beldandad63302013-04-15 17:09:30 +02001255 } else {
1256 rx_status.rate_idx = info->control.rates[0].idx;
1257 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
Johannes Bergda6a4352017-04-26 12:14:59 +02001258 rx_status.encoding = RX_ENC_HT;
Karl Beldandad63302013-04-15 17:09:30 +02001259 }
Jouni Malinen281ed292011-08-06 23:07:00 +03001260 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
Johannes Berg2f242bf2017-05-05 11:53:19 +02001261 rx_status.bw = RATE_INFO_BW_40;
1262 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1263 rx_status.bw = RATE_INFO_BW_80;
1264 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1265 rx_status.bw = RATE_INFO_BW_160;
1266 else
1267 rx_status.bw = RATE_INFO_BW_20;
Jouni Malinen281ed292011-08-06 23:07:00 +03001268 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
Johannes Berg7fdd69c2017-04-26 11:13:00 +02001269 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
Johannes Berg4b14c962009-07-10 16:56:59 +02001270 /* TODO: simulate real signal strength (and optional packet loss) */
Johannes Berg1d5e9f82017-04-13 10:31:16 +02001271 rx_status.signal = -50;
1272 if (info->control.vif)
1273 rx_status.signal += info->control.vif->bss_conf.txpower;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001274
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001275 if (data->ps != PS_DISABLED)
1276 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1277
Johannes Berg90e30122009-06-18 14:51:12 +02001278 /* release the skb's source info */
1279 skb_orphan(skb);
John W. Linvillec0acf382009-06-30 16:55:52 -04001280 skb_dst_drop(skb);
Johannes Berg90e30122009-06-18 14:51:12 +02001281 skb->mark = 0;
1282 secpath_reset(skb);
1283 nf_reset(skb);
1284
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001285 /*
1286 * Get absolute mactime here so all HWs RX at the "same time", and
1287 * absolute TX time for beacon mactime so the timestamp matches.
1288 * Giving beacons a different mactime than non-beacons looks messy, but
1289 * it helps the Toffset be exact and a ~10us mactime discrepancy
1290 * probably doesn't really matter.
1291 */
1292 if (ieee80211_is_beacon(hdr->frame_control) ||
1293 ieee80211_is_probe_resp(hdr->frame_control))
1294 now = data->abs_bcn_ts;
1295 else
1296 now = mac80211_hwsim_get_tsf_raw();
1297
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001298 /* Copy skb to all enabled radios that are on the current frequency */
Johannes Berg0e057d732008-09-12 00:39:22 +02001299 spin_lock(&hwsim_radio_lock);
1300 list_for_each_entry(data2, &hwsim_radios, list) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001301 struct sk_buff *nskb;
Johannes Berge8261172012-07-27 19:48:26 +02001302 struct tx_iter_data tx_iter_data = {
1303 .receive = false,
1304 .channel = chan,
1305 };
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001306
Johannes Berg0e057d732008-09-12 00:39:22 +02001307 if (data == data2)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001308 continue;
Johannes Berg0e057d732008-09-12 00:39:22 +02001309
Johannes Berge8261172012-07-27 19:48:26 +02001310 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1311 !hwsim_ps_rx_ok(data2, skb))
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001312 continue;
1313
Johannes Berge8261172012-07-27 19:48:26 +02001314 if (!(data->group & data2->group))
1315 continue;
1316
Martin Willi100cb9f2016-05-09 18:33:59 +02001317 if (data->netgroup != data2->netgroup)
1318 continue;
1319
Johannes Berge8261172012-07-27 19:48:26 +02001320 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1321 !hwsim_chans_compat(chan, data2->channel)) {
1322 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001323 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1324 mac80211_hwsim_tx_iter, &tx_iter_data);
Johannes Berge8261172012-07-27 19:48:26 +02001325 if (!tx_iter_data.receive)
1326 continue;
1327 }
1328
Johannes Berg90b9e4462012-11-16 10:09:08 +01001329 /*
1330 * reserve some space for our vendor and the normal
1331 * radiotap header, since we're copying anyway
1332 */
Johannes Berga357d7f2012-12-10 11:57:42 +01001333 if (skb->len < PAGE_SIZE && paged_rx) {
1334 struct page *page = alloc_page(GFP_ATOMIC);
1335
1336 if (!page)
1337 continue;
1338
1339 nskb = dev_alloc_skb(128);
1340 if (!nskb) {
1341 __free_page(page);
1342 continue;
1343 }
1344
1345 memcpy(page_address(page), skb->data, skb->len);
1346 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1347 } else {
1348 nskb = skb_copy(skb, GFP_ATOMIC);
1349 if (!nskb)
1350 continue;
1351 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001352
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001353 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
Johannes Berg0e057d732008-09-12 00:39:22 +02001354 ack = true;
Javier Cardonaf483ad22012-03-31 11:31:30 -07001355
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001356 rx_status.mactime = now + data2->tsf_offset;
Johannes Berg90b9e4462012-11-16 10:09:08 +01001357
Johannes Bergf1d58c22009-06-17 13:13:00 +02001358 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
Johannes Berg1f7bba72014-11-06 22:56:36 +01001359
1360 mac80211_hwsim_add_vendor_rtap(nskb);
1361
Ben Greear2155c3f2014-10-22 12:22:58 -07001362 data2->rx_pkts++;
1363 data2->rx_bytes += nskb->len;
Johannes Bergf1d58c22009-06-17 13:13:00 +02001364 ieee80211_rx_irqsafe(data2->hw, nskb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001365 }
Johannes Berg0e057d732008-09-12 00:39:22 +02001366 spin_unlock(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001367
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001368 return ack;
1369}
1370
Thomas Huehn36323f82012-07-23 21:33:42 +02001371static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1372 struct ieee80211_tx_control *control,
1373 struct sk_buff *skb)
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001374{
Johannes Berge8261172012-07-27 19:48:26 +02001375 struct mac80211_hwsim_data *data = hw->priv;
1376 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
Jouni Malinen732b5392015-11-26 20:50:12 +02001377 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Berge8261172012-07-27 19:48:26 +02001378 struct ieee80211_chanctx_conf *chanctx_conf;
1379 struct ieee80211_channel *channel;
Johannes Berg0e057d732008-09-12 00:39:22 +02001380 bool ack;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001381 u32 _portid;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001382
Johannes Berge8261172012-07-27 19:48:26 +02001383 if (WARN_ON(skb->len < 10)) {
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001384 /* Should not happen; just a sanity check for addr1 use */
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001385 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001386 return;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001387 }
1388
Luciano Coelho361c3e02014-02-10 15:23:03 +02001389 if (!data->use_chanctx) {
Johannes Berge8261172012-07-27 19:48:26 +02001390 channel = data->channel;
1391 } else if (txi->hw_queue == 4) {
1392 channel = data->tmp_chan;
1393 } else {
1394 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1395 if (chanctx_conf)
Johannes Berg4bf88532012-11-09 11:39:59 +01001396 channel = chanctx_conf->def.chan;
Johannes Berge8261172012-07-27 19:48:26 +02001397 else
1398 channel = NULL;
1399 }
1400
1401 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001402 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001403 return;
1404 }
1405
1406 if (data->idle && !data->tmp_chan) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001407 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001408 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001409 return;
1410 }
1411
1412 if (txi->control.vif)
1413 hwsim_check_magic(txi->control.vif);
1414 if (control->sta)
1415 hwsim_check_sta_magic(control->sta);
1416
Johannes Berg30686bf2015-06-02 21:39:54 +02001417 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001418 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1419 txi->control.rates,
1420 ARRAY_SIZE(txi->control.rates));
Johannes Berge8261172012-07-27 19:48:26 +02001421
Jouni Malinen732b5392015-11-26 20:50:12 +02001422 if (skb->len >= 24 + 8 &&
1423 ieee80211_is_probe_resp(hdr->frame_control)) {
1424 /* fake header transmission time */
1425 struct ieee80211_mgmt *mgmt;
1426 struct ieee80211_rate *txrate;
1427 u64 ts;
1428
1429 mgmt = (struct ieee80211_mgmt *)skb->data;
1430 txrate = ieee80211_get_tx_rate(hw, txi);
1431 ts = mac80211_hwsim_get_tsf_raw();
1432 mgmt->u.probe_resp.timestamp =
1433 cpu_to_le64(ts + data->tsf_offset +
1434 24 * 8 * 10 / txrate->bitrate);
1435 }
1436
Johannes Berge8261172012-07-27 19:48:26 +02001437 mac80211_hwsim_monitor_rx(hw, skb, channel);
1438
Javier Lopez78825132011-06-01 11:26:13 +02001439 /* wmediumd mode check */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001440 _portid = READ_ONCE(data->wmediumd);
Javier Lopez78825132011-06-01 11:26:13 +02001441
Eric W. Biederman15e47302012-09-07 20:12:54 +00001442 if (_portid)
1443 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
Javier Lopez78825132011-06-01 11:26:13 +02001444
1445 /* NO wmediumd detected, perfect medium simulation */
Ben Greear2155c3f2014-10-22 12:22:58 -07001446 data->tx_pkts++;
1447 data->tx_bytes += skb->len;
Johannes Berge8261172012-07-27 19:48:26 +02001448 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
Javier Lopez78825132011-06-01 11:26:13 +02001449
Johannes Bergf06b7ab82016-01-13 14:21:01 +01001450 if (ack && skb->len >= 16)
Johannes Berge8261172012-07-27 19:48:26 +02001451 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001452
Johannes Berge6a98542008-10-21 12:40:02 +02001453 ieee80211_tx_info_clear_status(txi);
Javier Cardona2a4ffa42012-05-01 03:01:28 -07001454
1455 /* frame was transmitted at most favorable rate at first attempt */
1456 txi->control.rates[0].count = 1;
1457 txi->control.rates[1].idx = -1;
1458
Johannes Berge6a98542008-10-21 12:40:02 +02001459 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1460 txi->flags |= IEEE80211_TX_STAT_ACK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001461 ieee80211_tx_status_irqsafe(hw, skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001462}
1463
1464
1465static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1466{
1467 struct mac80211_hwsim_data *data = hw->priv;
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001468 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001469 data->started = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001470 return 0;
1471}
1472
1473
1474static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1475{
1476 struct mac80211_hwsim_data *data = hw->priv;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001477 data->started = false;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001478 tasklet_hrtimer_cancel(&data->beacon_timer);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001479 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001480}
1481
1482
1483static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001484 struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001485{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001486 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1487 __func__, ieee80211_vif_type_p2p(vif),
1488 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001489 hwsim_set_magic(vif);
Johannes Berge8261172012-07-27 19:48:26 +02001490
1491 vif->cab_queue = 0;
1492 vif->hw_queue[IEEE80211_AC_VO] = 0;
1493 vif->hw_queue[IEEE80211_AC_VI] = 1;
1494 vif->hw_queue[IEEE80211_AC_BE] = 2;
1495 vif->hw_queue[IEEE80211_AC_BK] = 3;
1496
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001497 return 0;
1498}
1499
1500
Johannes Bergc35d0272010-08-27 12:35:59 +02001501static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1502 struct ieee80211_vif *vif,
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001503 enum nl80211_iftype newtype,
1504 bool newp2p)
Johannes Bergc35d0272010-08-27 12:35:59 +02001505{
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001506 newtype = ieee80211_iftype_p2p(newtype, newp2p);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001507 wiphy_dbg(hw->wiphy,
1508 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1509 __func__, ieee80211_vif_type_p2p(vif),
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001510 newtype, vif->addr);
Johannes Bergc35d0272010-08-27 12:35:59 +02001511 hwsim_check_magic(vif);
1512
Johannes Berg3eb92f62013-03-18 22:13:18 +01001513 /*
1514 * interface may change from non-AP to AP in
1515 * which case this needs to be set up again
1516 */
1517 vif->cab_queue = 0;
1518
Johannes Bergc35d0272010-08-27 12:35:59 +02001519 return 0;
1520}
1521
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001522static void mac80211_hwsim_remove_interface(
Johannes Berg1ed32e42009-12-23 13:15:45 +01001523 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001524{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001525 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1526 __func__, ieee80211_vif_type_p2p(vif),
1527 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001528 hwsim_check_magic(vif);
1529 hwsim_clear_magic(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001530}
1531
Johannes Berge8261172012-07-27 19:48:26 +02001532static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1533 struct sk_buff *skb,
1534 struct ieee80211_channel *chan)
1535{
Martin Willif21e4d82016-05-14 10:34:44 +02001536 struct mac80211_hwsim_data *data = hw->priv;
Mark Rutland6aa7de02017-10-23 14:07:29 -07001537 u32 _pid = READ_ONCE(data->wmediumd);
Johannes Berge8261172012-07-27 19:48:26 +02001538
Johannes Berg30686bf2015-06-02 21:39:54 +02001539 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
Karl Beldan1eb32172013-04-19 14:44:52 +02001540 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1541 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1542 txi->control.rates,
1543 ARRAY_SIZE(txi->control.rates));
1544 }
1545
Johannes Berge8261172012-07-27 19:48:26 +02001546 mac80211_hwsim_monitor_rx(hw, skb, chan);
1547
1548 if (_pid)
1549 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1550
1551 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1552 dev_kfree_skb(skb);
1553}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001554
1555static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1556 struct ieee80211_vif *vif)
1557{
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001558 struct mac80211_hwsim_data *data = arg;
1559 struct ieee80211_hw *hw = data->hw;
1560 struct ieee80211_tx_info *info;
1561 struct ieee80211_rate *txrate;
1562 struct ieee80211_mgmt *mgmt;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001563 struct sk_buff *skb;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001564
Johannes Berg8aa21e62008-09-11 02:16:36 +02001565 hwsim_check_magic(vif);
1566
Andrey Yurovsky55b39612008-10-31 23:23:35 -07001567 if (vif->type != NL80211_IFTYPE_AP &&
Johannes Berg2b2d7792010-08-17 12:08:07 +02001568 vif->type != NL80211_IFTYPE_MESH_POINT &&
1569 vif->type != NL80211_IFTYPE_ADHOC)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001570 return;
1571
1572 skb = ieee80211_beacon_get(hw, vif);
1573 if (skb == NULL)
1574 return;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001575 info = IEEE80211_SKB_CB(skb);
Johannes Berg30686bf2015-06-02 21:39:54 +02001576 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001577 ieee80211_get_tx_rates(vif, NULL, skb,
1578 info->control.rates,
1579 ARRAY_SIZE(info->control.rates));
1580
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001581 txrate = ieee80211_get_tx_rate(hw, info);
1582
1583 mgmt = (struct ieee80211_mgmt *) skb->data;
1584 /* fake header transmission time */
1585 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1586 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1587 data->tsf_offset +
1588 24 * 8 * 10 / txrate->bitrate);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001589
Johannes Berge8261172012-07-27 19:48:26 +02001590 mac80211_hwsim_tx_frame(hw, skb,
Johannes Berg4bf88532012-11-09 11:39:59 +01001591 rcu_dereference(vif->chanctx_conf)->def.chan);
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02001592
1593 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1594 ieee80211_csa_finish(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001595}
1596
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001597static enum hrtimer_restart
1598mac80211_hwsim_beacon(struct hrtimer *timer)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001599{
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001600 struct mac80211_hwsim_data *data =
1601 container_of(timer, struct mac80211_hwsim_data,
1602 beacon_timer.timer);
1603 struct ieee80211_hw *hw = data->hw;
1604 u64 bcn_int = data->beacon_int;
1605 ktime_t next_bcn;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001606
Johannes Berg4c4c6712009-06-01 14:29:52 +02001607 if (!data->started)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001608 goto out;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001609
Jouni Malinenf248f102008-06-13 19:44:47 +03001610 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001611 hw, IEEE80211_IFACE_ITER_NORMAL,
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001612 mac80211_hwsim_beacon_tx, data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001613
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001614 /* beacon at new TBTT + beacon interval */
1615 if (data->bcn_delta) {
1616 bcn_int -= data->bcn_delta;
1617 data->bcn_delta = 0;
1618 }
1619
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001620 next_bcn = ktime_add(hrtimer_get_expires(timer),
1621 ns_to_ktime(bcn_int * 1000));
1622 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1623out:
1624 return HRTIMER_NORESTART;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001625}
1626
Karl Beldan675a0b02013-03-25 16:26:57 +01001627static const char * const hwsim_chanwidths[] = {
1628 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1629 [NL80211_CHAN_WIDTH_20] = "ht20",
1630 [NL80211_CHAN_WIDTH_40] = "ht40",
1631 [NL80211_CHAN_WIDTH_80] = "vht80",
1632 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1633 [NL80211_CHAN_WIDTH_160] = "vht160",
Johannes Berg0aaffa92010-05-05 15:28:27 +02001634};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001635
Johannes Berge8975582008-10-09 12:18:51 +02001636static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001637{
1638 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001639 struct ieee80211_conf *conf = &hw->conf;
Johannes Berg0f782312009-12-01 13:37:02 +01001640 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1641 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1642 [IEEE80211_SMPS_OFF] = "off",
1643 [IEEE80211_SMPS_STATIC] = "static",
1644 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1645 };
Johannes Berg7f813ce2013-11-06 10:37:34 +01001646 int idx;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001647
Karl Beldan675a0b02013-03-25 16:26:57 +01001648 if (conf->chandef.chan)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001649 wiphy_dbg(hw->wiphy,
1650 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1651 __func__,
1652 conf->chandef.chan->center_freq,
1653 conf->chandef.center_freq1,
1654 conf->chandef.center_freq2,
1655 hwsim_chanwidths[conf->chandef.width],
1656 !!(conf->flags & IEEE80211_CONF_IDLE),
1657 !!(conf->flags & IEEE80211_CONF_PS),
1658 smps_modes[conf->smps_mode]);
Karl Beldan675a0b02013-03-25 16:26:57 +01001659 else
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001660 wiphy_dbg(hw->wiphy,
1661 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1662 __func__,
1663 !!(conf->flags & IEEE80211_CONF_IDLE),
1664 !!(conf->flags & IEEE80211_CONF_PS),
1665 smps_modes[conf->smps_mode]);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001666
Jouni Malinen70541832009-11-01 11:30:48 +02001667 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1668
Johannes Berg7f813ce2013-11-06 10:37:34 +01001669 WARN_ON(conf->chandef.chan && data->use_chanctx);
Johannes Berge8261172012-07-27 19:48:26 +02001670
Johannes Berg7f813ce2013-11-06 10:37:34 +01001671 mutex_lock(&data->mutex);
1672 if (data->scanning && conf->chandef.chan) {
1673 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1674 if (data->survey_data[idx].channel == data->channel) {
1675 data->survey_data[idx].start =
1676 data->survey_data[idx].next_start;
1677 data->survey_data[idx].end = jiffies;
1678 break;
1679 }
1680 }
Johannes Berge8261172012-07-27 19:48:26 +02001681
Johannes Berg7f813ce2013-11-06 10:37:34 +01001682 data->channel = conf->chandef.chan;
1683
1684 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1685 if (data->survey_data[idx].channel &&
1686 data->survey_data[idx].channel != data->channel)
1687 continue;
1688 data->survey_data[idx].channel = data->channel;
1689 data->survey_data[idx].next_start = jiffies;
1690 break;
1691 }
1692 } else {
1693 data->channel = conf->chandef.chan;
1694 }
1695 mutex_unlock(&data->mutex);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001696
Johannes Berg4c4c6712009-06-01 14:29:52 +02001697 if (!data->started || !data->beacon_int)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001698 tasklet_hrtimer_cancel(&data->beacon_timer);
1699 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001700 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1701 u32 bcn_int = data->beacon_int;
1702 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1703
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001704 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001705 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001706 HRTIMER_MODE_REL);
1707 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001708
1709 return 0;
1710}
1711
1712
1713static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1714 unsigned int changed_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001715 unsigned int *total_flags,u64 multicast)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001716{
1717 struct mac80211_hwsim_data *data = hw->priv;
1718
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001719 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001720
1721 data->rx_filter = 0;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001722 if (*total_flags & FIF_ALLMULTI)
1723 data->rx_filter |= FIF_ALLMULTI;
1724
1725 *total_flags = data->rx_filter;
1726}
1727
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001728static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1729 struct ieee80211_vif *vif)
1730{
1731 unsigned int *count = data;
1732 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1733
1734 if (vp->bcn_en)
1735 (*count)++;
1736}
1737
Johannes Berg8aa21e62008-09-11 02:16:36 +02001738static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1739 struct ieee80211_vif *vif,
1740 struct ieee80211_bss_conf *info,
1741 u32 changed)
1742{
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001743 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001744 struct mac80211_hwsim_data *data = hw->priv;
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001745
Johannes Berg8aa21e62008-09-11 02:16:36 +02001746 hwsim_check_magic(vif);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001747
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001748 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1749 __func__, changed, vif->addr);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001750
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001751 if (changed & BSS_CHANGED_BSSID) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001752 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1753 __func__, info->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001754 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1755 }
1756
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001757 if (changed & BSS_CHANGED_ASSOC) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001758 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1759 info->assoc, info->aid);
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001760 vp->assoc = info->assoc;
1761 vp->aid = info->aid;
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001762 }
1763
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001764 if (changed & BSS_CHANGED_BEACON_ENABLED) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001765 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1766 info->enable_beacon, info->beacon_int);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001767 vp->bcn_en = info->enable_beacon;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001768 if (data->started &&
1769 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1770 info->enable_beacon) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001771 u64 tsf, until_tbtt;
1772 u32 bcn_int;
Johannes Berg56067622015-03-03 22:09:05 +01001773 data->beacon_int = info->beacon_int * 1024;
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001774 tsf = mac80211_hwsim_get_tsf(hw, vif);
1775 bcn_int = data->beacon_int;
1776 until_tbtt = bcn_int - do_div(tsf, bcn_int);
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001777 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001778 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001779 HRTIMER_MODE_REL);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001780 } else if (!info->enable_beacon) {
1781 unsigned int count = 0;
Jouni Malinencdb1b802013-11-06 12:06:30 +02001782 ieee80211_iterate_active_interfaces_atomic(
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001783 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1784 mac80211_hwsim_bcn_en_iter, &count);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001785 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1786 count);
Johannes Berg56067622015-03-03 22:09:05 +01001787 if (count == 0) {
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001788 tasklet_hrtimer_cancel(&data->beacon_timer);
Johannes Berg56067622015-03-03 22:09:05 +01001789 data->beacon_int = 0;
1790 }
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001791 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001792 }
1793
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001794 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001795 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1796 info->use_cts_prot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001797 }
1798
1799 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001800 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1801 info->use_short_preamble);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001802 }
1803
1804 if (changed & BSS_CHANGED_ERP_SLOT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001805 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001806 }
1807
1808 if (changed & BSS_CHANGED_HT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001809 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1810 info->ht_operation_mode);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001811 }
1812
1813 if (changed & BSS_CHANGED_BASIC_RATES) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001814 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1815 (unsigned long long) info->basic_rates);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001816 }
Johannes Bergcbc668a2012-10-24 11:54:31 +02001817
1818 if (changed & BSS_CHANGED_TXPOWER)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001819 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
Johannes Berg8aa21e62008-09-11 02:16:36 +02001820}
1821
Johannes Berg1d669cb2010-02-19 19:06:55 +01001822static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1823 struct ieee80211_vif *vif,
1824 struct ieee80211_sta *sta)
1825{
1826 hwsim_check_magic(vif);
1827 hwsim_set_sta_magic(sta);
1828
1829 return 0;
1830}
1831
1832static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1833 struct ieee80211_vif *vif,
1834 struct ieee80211_sta *sta)
1835{
1836 hwsim_check_magic(vif);
1837 hwsim_clear_sta_magic(sta);
1838
1839 return 0;
1840}
1841
Johannes Berg8aa21e62008-09-11 02:16:36 +02001842static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1843 struct ieee80211_vif *vif,
Johannes Berg17741cd2008-09-11 00:02:02 +02001844 enum sta_notify_cmd cmd,
1845 struct ieee80211_sta *sta)
Johannes Berg8aa21e62008-09-11 02:16:36 +02001846{
1847 hwsim_check_magic(vif);
Johannes Berg1d669cb2010-02-19 19:06:55 +01001848
Johannes Berg81c06522008-09-11 02:17:01 +02001849 switch (cmd) {
Christian Lamparter89fad572008-12-09 16:28:06 +01001850 case STA_NOTIFY_SLEEP:
1851 case STA_NOTIFY_AWAKE:
1852 /* TODO: make good use of these flags */
1853 break;
Johannes Berg1d669cb2010-02-19 19:06:55 +01001854 default:
1855 WARN(1, "Invalid sta notify: %d\n", cmd);
1856 break;
Johannes Berg81c06522008-09-11 02:17:01 +02001857 }
1858}
1859
1860static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1861 struct ieee80211_sta *sta,
1862 bool set)
1863{
1864 hwsim_check_sta_magic(sta);
1865 return 0;
Johannes Berg8aa21e62008-09-11 02:16:36 +02001866}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001867
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001868static int mac80211_hwsim_conf_tx(
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001869 struct ieee80211_hw *hw,
1870 struct ieee80211_vif *vif, u16 queue,
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001871 const struct ieee80211_tx_queue_params *params)
1872{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001873 wiphy_dbg(hw->wiphy,
1874 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1875 __func__, queue,
1876 params->txop, params->cw_min,
1877 params->cw_max, params->aifs);
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001878 return 0;
1879}
1880
Johannes Berg7f813ce2013-11-06 10:37:34 +01001881static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1882 struct survey_info *survey)
Holger Schurig12897232010-04-19 10:23:57 +02001883{
Johannes Berg7f813ce2013-11-06 10:37:34 +01001884 struct mac80211_hwsim_data *hwsim = hw->priv;
Holger Schurig12897232010-04-19 10:23:57 +02001885
Johannes Berg7f813ce2013-11-06 10:37:34 +01001886 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
Holger Schurig12897232010-04-19 10:23:57 +02001887 return -ENOENT;
1888
Johannes Berg7f813ce2013-11-06 10:37:34 +01001889 mutex_lock(&hwsim->mutex);
1890 survey->channel = hwsim->survey_data[idx].channel;
1891 if (!survey->channel) {
1892 mutex_unlock(&hwsim->mutex);
1893 return -ENOENT;
1894 }
Holger Schurig12897232010-04-19 10:23:57 +02001895
1896 /*
Johannes Berg7f813ce2013-11-06 10:37:34 +01001897 * Magically conjured dummy values --- this is only ok for simulated hardware.
Holger Schurig12897232010-04-19 10:23:57 +02001898 *
Johannes Berg7f813ce2013-11-06 10:37:34 +01001899 * A real driver which cannot determine real values noise MUST NOT
1900 * report any, especially not a magically conjured ones :-)
Holger Schurig12897232010-04-19 10:23:57 +02001901 */
Johannes Berg7f813ce2013-11-06 10:37:34 +01001902 survey->filled = SURVEY_INFO_NOISE_DBM |
1903 SURVEY_INFO_TIME |
1904 SURVEY_INFO_TIME_BUSY;
Holger Schurig12897232010-04-19 10:23:57 +02001905 survey->noise = -92;
Johannes Berg7f813ce2013-11-06 10:37:34 +01001906 survey->time =
1907 jiffies_to_msecs(hwsim->survey_data[idx].end -
1908 hwsim->survey_data[idx].start);
1909 /* report 12.5% of channel time is used */
1910 survey->time_busy = survey->time/8;
1911 mutex_unlock(&hwsim->mutex);
Holger Schurig12897232010-04-19 10:23:57 +02001912
1913 return 0;
1914}
1915
Johannes Bergaff89a92009-07-01 21:26:51 +02001916#ifdef CONFIG_NL80211_TESTMODE
1917/*
1918 * This section contains example code for using netlink
1919 * attributes with the testmode command in nl80211.
1920 */
1921
1922/* These enums need to be kept in sync with userspace */
1923enum hwsim_testmode_attr {
1924 __HWSIM_TM_ATTR_INVALID = 0,
1925 HWSIM_TM_ATTR_CMD = 1,
1926 HWSIM_TM_ATTR_PS = 2,
1927
1928 /* keep last */
1929 __HWSIM_TM_ATTR_AFTER_LAST,
1930 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1931};
1932
1933enum hwsim_testmode_cmd {
1934 HWSIM_TM_CMD_SET_PS = 0,
1935 HWSIM_TM_CMD_GET_PS = 1,
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001936 HWSIM_TM_CMD_STOP_QUEUES = 2,
1937 HWSIM_TM_CMD_WAKE_QUEUES = 3,
Johannes Bergaff89a92009-07-01 21:26:51 +02001938};
1939
1940static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1941 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1942 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1943};
1944
Johannes Berg5bc38192009-07-07 11:00:55 +02001945static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
David Spinadel52981cd2013-07-31 18:06:22 +03001946 struct ieee80211_vif *vif,
Johannes Berg5bc38192009-07-07 11:00:55 +02001947 void *data, int len)
Johannes Bergaff89a92009-07-01 21:26:51 +02001948{
1949 struct mac80211_hwsim_data *hwsim = hw->priv;
1950 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1951 struct sk_buff *skb;
1952 int err, ps;
1953
1954 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
Johannes Bergfceb6432017-04-12 14:34:07 +02001955 hwsim_testmode_policy, NULL);
Johannes Bergaff89a92009-07-01 21:26:51 +02001956 if (err)
1957 return err;
1958
1959 if (!tb[HWSIM_TM_ATTR_CMD])
1960 return -EINVAL;
1961
1962 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1963 case HWSIM_TM_CMD_SET_PS:
1964 if (!tb[HWSIM_TM_ATTR_PS])
1965 return -EINVAL;
1966 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1967 return hwsim_fops_ps_write(hwsim, ps);
1968 case HWSIM_TM_CMD_GET_PS:
1969 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1970 nla_total_size(sizeof(u32)));
1971 if (!skb)
1972 return -ENOMEM;
David S. Miller633c9382012-04-01 21:03:30 -04001973 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1974 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02001975 return cfg80211_testmode_reply(skb);
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001976 case HWSIM_TM_CMD_STOP_QUEUES:
1977 ieee80211_stop_queues(hw);
1978 return 0;
1979 case HWSIM_TM_CMD_WAKE_QUEUES:
1980 ieee80211_wake_queues(hw);
1981 return 0;
Johannes Bergaff89a92009-07-01 21:26:51 +02001982 default:
1983 return -EOPNOTSUPP;
1984 }
1985
1986 nla_put_failure:
1987 kfree_skb(skb);
1988 return -ENOBUFS;
1989}
1990#endif
1991
Johannes Berg8b73d132009-12-01 14:24:24 +01001992static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1993 struct ieee80211_vif *vif,
Sara Sharon50ea05efa2015-12-30 16:06:04 +02001994 struct ieee80211_ampdu_params *params)
Johannes Berg8b73d132009-12-01 14:24:24 +01001995{
Sara Sharon50ea05efa2015-12-30 16:06:04 +02001996 struct ieee80211_sta *sta = params->sta;
1997 enum ieee80211_ampdu_mlme_action action = params->action;
1998 u16 tid = params->tid;
1999
Johannes Berg8b73d132009-12-01 14:24:24 +01002000 switch (action) {
2001 case IEEE80211_AMPDU_TX_START:
2002 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2003 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02002004 case IEEE80211_AMPDU_TX_STOP_CONT:
2005 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2006 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Johannes Berg8b73d132009-12-01 14:24:24 +01002007 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2008 break;
2009 case IEEE80211_AMPDU_TX_OPERATIONAL:
2010 break;
2011 case IEEE80211_AMPDU_RX_START:
2012 case IEEE80211_AMPDU_RX_STOP:
2013 break;
2014 default:
2015 return -EOPNOTSUPP;
2016 }
2017
2018 return 0;
2019}
2020
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02002021static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2022 struct ieee80211_vif *vif,
2023 u32 queues, bool drop)
Johannes Berga80f7c02009-12-23 13:15:32 +01002024{
Javier Lopez78825132011-06-01 11:26:13 +02002025 /* Not implemented, queues only on kernel side */
Johannes Berga80f7c02009-12-23 13:15:32 +01002026}
2027
Johannes Berge8261172012-07-27 19:48:26 +02002028static void hw_scan_work(struct work_struct *work)
Johannes Berg69068032010-02-03 10:47:55 +01002029{
Johannes Berge8261172012-07-27 19:48:26 +02002030 struct mac80211_hwsim_data *hwsim =
2031 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2032 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2033 int dwell, i;
Johannes Berg69068032010-02-03 10:47:55 +01002034
Johannes Berge8261172012-07-27 19:48:26 +02002035 mutex_lock(&hwsim->mutex);
2036 if (hwsim->scan_chan_idx >= req->n_channels) {
Avraham Stern7947d3e2016-07-05 15:23:12 +03002037 struct cfg80211_scan_info info = {
2038 .aborted = false,
2039 };
2040
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002041 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
Avraham Stern7947d3e2016-07-05 15:23:12 +03002042 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002043 hwsim->hw_scan_request = NULL;
2044 hwsim->hw_scan_vif = NULL;
2045 hwsim->tmp_chan = NULL;
2046 mutex_unlock(&hwsim->mutex);
2047 return;
2048 }
2049
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002050 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2051 req->channels[hwsim->scan_chan_idx]->center_freq);
Johannes Berge8261172012-07-27 19:48:26 +02002052
2053 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
Johannes Berga8e828d2015-11-26 17:59:27 +01002054 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2055 IEEE80211_CHAN_RADAR) ||
Johannes Berge8261172012-07-27 19:48:26 +02002056 !req->n_ssids) {
2057 dwell = 120;
2058 } else {
2059 dwell = 30;
2060 /* send probes */
2061 for (i = 0; i < req->n_ssids; i++) {
2062 struct sk_buff *probe;
Jouni Malinen12880d12016-02-26 22:12:49 +02002063 struct ieee80211_mgmt *mgmt;
Johannes Berge8261172012-07-27 19:48:26 +02002064
2065 probe = ieee80211_probereq_get(hwsim->hw,
Johannes Berg339467b2014-06-12 22:41:00 +02002066 hwsim->scan_addr,
Johannes Berge8261172012-07-27 19:48:26 +02002067 req->ssids[i].ssid,
2068 req->ssids[i].ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002069 req->ie_len);
Johannes Berge8261172012-07-27 19:48:26 +02002070 if (!probe)
2071 continue;
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002072
Jouni Malinen12880d12016-02-26 22:12:49 +02002073 mgmt = (struct ieee80211_mgmt *) probe->data;
2074 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2075 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2076
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002077 if (req->ie_len)
Johannes Berg59ae1d12017-06-16 14:29:20 +02002078 skb_put_data(probe, req->ie, req->ie_len);
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002079
Johannes Berge8261172012-07-27 19:48:26 +02002080 local_bh_disable();
2081 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2082 hwsim->tmp_chan);
2083 local_bh_enable();
2084 }
2085 }
2086 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2087 msecs_to_jiffies(dwell));
Johannes Berg7f813ce2013-11-06 10:37:34 +01002088 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2089 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2090 hwsim->survey_data[hwsim->scan_chan_idx].end =
2091 jiffies + msecs_to_jiffies(dwell);
Johannes Berge8261172012-07-27 19:48:26 +02002092 hwsim->scan_chan_idx++;
2093 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002094}
2095
2096static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +02002097 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02002098 struct ieee80211_scan_request *hw_req)
Johannes Berg69068032010-02-03 10:47:55 +01002099{
Johannes Berge8261172012-07-27 19:48:26 +02002100 struct mac80211_hwsim_data *hwsim = hw->priv;
David Spinadelc56ef672014-02-05 15:21:13 +02002101 struct cfg80211_scan_request *req = &hw_req->req;
Johannes Berg69068032010-02-03 10:47:55 +01002102
Johannes Berge8261172012-07-27 19:48:26 +02002103 mutex_lock(&hwsim->mutex);
2104 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2105 mutex_unlock(&hwsim->mutex);
2106 return -EBUSY;
2107 }
2108 hwsim->hw_scan_request = req;
2109 hwsim->hw_scan_vif = vif;
2110 hwsim->scan_chan_idx = 0;
Johannes Berg339467b2014-06-12 22:41:00 +02002111 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2112 get_random_mask_addr(hwsim->scan_addr,
2113 hw_req->req.mac_addr,
2114 hw_req->req.mac_addr_mask);
2115 else
2116 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
Johannes Berg7f813ce2013-11-06 10:37:34 +01002117 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Johannes Berge8261172012-07-27 19:48:26 +02002118 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002119
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002120 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
Johannes Berg69068032010-02-03 10:47:55 +01002121
Johannes Berge8261172012-07-27 19:48:26 +02002122 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
Johannes Berg69068032010-02-03 10:47:55 +01002123
2124 return 0;
2125}
2126
Johannes Berge8261172012-07-27 19:48:26 +02002127static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2128 struct ieee80211_vif *vif)
2129{
2130 struct mac80211_hwsim_data *hwsim = hw->priv;
Avraham Stern7947d3e2016-07-05 15:23:12 +03002131 struct cfg80211_scan_info info = {
2132 .aborted = true,
2133 };
Johannes Berge8261172012-07-27 19:48:26 +02002134
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002135 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
Johannes Berge8261172012-07-27 19:48:26 +02002136
2137 cancel_delayed_work_sync(&hwsim->hw_scan);
2138
2139 mutex_lock(&hwsim->mutex);
Avraham Stern7947d3e2016-07-05 15:23:12 +03002140 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002141 hwsim->tmp_chan = NULL;
2142 hwsim->hw_scan_request = NULL;
2143 hwsim->hw_scan_vif = NULL;
2144 mutex_unlock(&hwsim->mutex);
2145}
2146
Johannes Berga344d672014-06-12 22:24:31 +02002147static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2148 struct ieee80211_vif *vif,
2149 const u8 *mac_addr)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002150{
2151 struct mac80211_hwsim_data *hwsim = hw->priv;
2152
2153 mutex_lock(&hwsim->mutex);
2154
2155 if (hwsim->scanning) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002156 pr_debug("two hwsim sw_scans detected!\n");
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002157 goto out;
2158 }
2159
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002160 pr_debug("hwsim sw_scan request, prepping stuff\n");
Johannes Berg339467b2014-06-12 22:41:00 +02002161
2162 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002163 hwsim->scanning = true;
Johannes Berg7f813ce2013-11-06 10:37:34 +01002164 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002165
2166out:
2167 mutex_unlock(&hwsim->mutex);
2168}
2169
Johannes Berga344d672014-06-12 22:24:31 +02002170static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2171 struct ieee80211_vif *vif)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002172{
2173 struct mac80211_hwsim_data *hwsim = hw->priv;
2174
2175 mutex_lock(&hwsim->mutex);
2176
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002177 pr_debug("hwsim sw_scan_complete\n");
Johannes Berg23ff98f2010-05-03 09:21:14 +02002178 hwsim->scanning = false;
Joe Perches93803b32015-03-02 19:54:49 -08002179 eth_zero_addr(hwsim->scan_addr);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002180
2181 mutex_unlock(&hwsim->mutex);
2182}
2183
Johannes Berg661ef472015-11-24 18:29:45 +01002184static void hw_roc_start(struct work_struct *work)
2185{
2186 struct mac80211_hwsim_data *hwsim =
2187 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2188
2189 mutex_lock(&hwsim->mutex);
2190
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002191 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
Johannes Berg661ef472015-11-24 18:29:45 +01002192 hwsim->tmp_chan = hwsim->roc_chan;
2193 ieee80211_ready_on_channel(hwsim->hw);
2194
2195 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2196 msecs_to_jiffies(hwsim->roc_duration));
2197
2198 mutex_unlock(&hwsim->mutex);
2199}
2200
Johannes Berge8261172012-07-27 19:48:26 +02002201static void hw_roc_done(struct work_struct *work)
2202{
2203 struct mac80211_hwsim_data *hwsim =
2204 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2205
2206 mutex_lock(&hwsim->mutex);
2207 ieee80211_remain_on_channel_expired(hwsim->hw);
2208 hwsim->tmp_chan = NULL;
2209 mutex_unlock(&hwsim->mutex);
2210
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002211 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
Johannes Berge8261172012-07-27 19:48:26 +02002212}
2213
2214static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
Eliad Peller49884562012-11-19 17:05:09 +02002215 struct ieee80211_vif *vif,
Johannes Berge8261172012-07-27 19:48:26 +02002216 struct ieee80211_channel *chan,
Ilan Peerd339d5c2013-02-12 09:34:13 +02002217 int duration,
2218 enum ieee80211_roc_type type)
Johannes Berge8261172012-07-27 19:48:26 +02002219{
2220 struct mac80211_hwsim_data *hwsim = hw->priv;
2221
2222 mutex_lock(&hwsim->mutex);
2223 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2224 mutex_unlock(&hwsim->mutex);
2225 return -EBUSY;
2226 }
2227
Johannes Berg661ef472015-11-24 18:29:45 +01002228 hwsim->roc_chan = chan;
2229 hwsim->roc_duration = duration;
Johannes Berge8261172012-07-27 19:48:26 +02002230 mutex_unlock(&hwsim->mutex);
2231
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002232 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2233 chan->center_freq, duration);
Johannes Berg661ef472015-11-24 18:29:45 +01002234 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
Johannes Berge8261172012-07-27 19:48:26 +02002235
Johannes Berge8261172012-07-27 19:48:26 +02002236 return 0;
2237}
2238
2239static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2240{
2241 struct mac80211_hwsim_data *hwsim = hw->priv;
2242
Johannes Berg661ef472015-11-24 18:29:45 +01002243 cancel_delayed_work_sync(&hwsim->roc_start);
Johannes Berge8261172012-07-27 19:48:26 +02002244 cancel_delayed_work_sync(&hwsim->roc_done);
2245
2246 mutex_lock(&hwsim->mutex);
2247 hwsim->tmp_chan = NULL;
2248 mutex_unlock(&hwsim->mutex);
2249
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002250 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
Johannes Berge8261172012-07-27 19:48:26 +02002251
2252 return 0;
2253}
2254
2255static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2256 struct ieee80211_chanctx_conf *ctx)
2257{
2258 hwsim_set_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002259 wiphy_dbg(hw->wiphy,
2260 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2261 ctx->def.chan->center_freq, ctx->def.width,
2262 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002263 return 0;
2264}
2265
2266static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2267 struct ieee80211_chanctx_conf *ctx)
2268{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002269 wiphy_dbg(hw->wiphy,
2270 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2271 ctx->def.chan->center_freq, ctx->def.width,
2272 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002273 hwsim_check_chanctx_magic(ctx);
2274 hwsim_clear_chanctx_magic(ctx);
2275}
2276
2277static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2278 struct ieee80211_chanctx_conf *ctx,
2279 u32 changed)
2280{
2281 hwsim_check_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002282 wiphy_dbg(hw->wiphy,
2283 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2284 ctx->def.chan->center_freq, ctx->def.width,
2285 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002286}
2287
2288static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2289 struct ieee80211_vif *vif,
2290 struct ieee80211_chanctx_conf *ctx)
2291{
2292 hwsim_check_magic(vif);
2293 hwsim_check_chanctx_magic(ctx);
2294
2295 return 0;
2296}
2297
2298static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2299 struct ieee80211_vif *vif,
2300 struct ieee80211_chanctx_conf *ctx)
2301{
2302 hwsim_check_magic(vif);
2303 hwsim_check_chanctx_magic(ctx);
2304}
2305
Ben Greear2155c3f2014-10-22 12:22:58 -07002306static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2307 "tx_pkts_nic",
2308 "tx_bytes_nic",
2309 "rx_pkts_nic",
2310 "rx_bytes_nic",
2311 "d_tx_dropped",
2312 "d_tx_failed",
2313 "d_ps_mode",
2314 "d_group",
Ben Greear2155c3f2014-10-22 12:22:58 -07002315};
2316
2317#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2318
2319static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2320 struct ieee80211_vif *vif,
2321 u32 sset, u8 *data)
2322{
2323 if (sset == ETH_SS_STATS)
2324 memcpy(data, *mac80211_hwsim_gstrings_stats,
2325 sizeof(mac80211_hwsim_gstrings_stats));
2326}
2327
2328static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2329 struct ieee80211_vif *vif, int sset)
2330{
2331 if (sset == ETH_SS_STATS)
2332 return MAC80211_HWSIM_SSTATS_LEN;
2333 return 0;
2334}
2335
2336static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2337 struct ieee80211_vif *vif,
2338 struct ethtool_stats *stats, u64 *data)
2339{
2340 struct mac80211_hwsim_data *ar = hw->priv;
2341 int i = 0;
2342
2343 data[i++] = ar->tx_pkts;
2344 data[i++] = ar->tx_bytes;
2345 data[i++] = ar->rx_pkts;
2346 data[i++] = ar->rx_bytes;
2347 data[i++] = ar->tx_dropped;
2348 data[i++] = ar->tx_failed;
2349 data[i++] = ar->ps;
2350 data[i++] = ar->group;
Ben Greear2155c3f2014-10-22 12:22:58 -07002351
2352 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2353}
2354
Johannes Berg246ad562016-10-10 12:09:52 +02002355#define HWSIM_COMMON_OPS \
2356 .tx = mac80211_hwsim_tx, \
2357 .start = mac80211_hwsim_start, \
2358 .stop = mac80211_hwsim_stop, \
2359 .add_interface = mac80211_hwsim_add_interface, \
2360 .change_interface = mac80211_hwsim_change_interface, \
2361 .remove_interface = mac80211_hwsim_remove_interface, \
2362 .config = mac80211_hwsim_config, \
2363 .configure_filter = mac80211_hwsim_configure_filter, \
2364 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2365 .sta_add = mac80211_hwsim_sta_add, \
2366 .sta_remove = mac80211_hwsim_sta_remove, \
2367 .sta_notify = mac80211_hwsim_sta_notify, \
2368 .set_tim = mac80211_hwsim_set_tim, \
2369 .conf_tx = mac80211_hwsim_conf_tx, \
2370 .get_survey = mac80211_hwsim_get_survey, \
2371 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2372 .ampdu_action = mac80211_hwsim_ampdu_action, \
2373 .flush = mac80211_hwsim_flush, \
2374 .get_tsf = mac80211_hwsim_get_tsf, \
2375 .set_tsf = mac80211_hwsim_set_tsf, \
2376 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2377 .get_et_stats = mac80211_hwsim_get_et_stats, \
2378 .get_et_strings = mac80211_hwsim_get_et_strings,
2379
Johannes Berg38ed5042014-01-06 22:11:06 +01002380static const struct ieee80211_ops mac80211_hwsim_ops = {
Johannes Berg246ad562016-10-10 12:09:52 +02002381 HWSIM_COMMON_OPS
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002382 .sw_scan_start = mac80211_hwsim_sw_scan,
2383 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002384};
2385
Johannes Berg246ad562016-10-10 12:09:52 +02002386static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2387 HWSIM_COMMON_OPS
2388 .hw_scan = mac80211_hwsim_hw_scan,
2389 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2390 .sw_scan_start = NULL,
2391 .sw_scan_complete = NULL,
2392 .remain_on_channel = mac80211_hwsim_roc,
2393 .cancel_remain_on_channel = mac80211_hwsim_croc,
2394 .add_chanctx = mac80211_hwsim_add_chanctx,
2395 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2396 .change_chanctx = mac80211_hwsim_change_chanctx,
2397 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2398 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2399};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002400
Jukka Rissanen62759362014-10-31 14:48:47 +02002401struct hwsim_new_radio_params {
2402 unsigned int channels;
2403 const char *reg_alpha2;
2404 const struct ieee80211_regdomain *regd;
2405 bool reg_strict;
2406 bool p2p_device;
2407 bool use_chanctx;
2408 bool destroy_on_close;
2409 const char *hwname;
2410 bool no_vif;
2411};
2412
2413static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2414 struct genl_info *info)
2415{
2416 if (info)
Jiri Benc92c14d92015-09-22 18:56:43 +02002417 genl_notify(&hwsim_genl_family, mcast_skb, info,
2418 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002419 else
2420 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2421 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2422}
2423
Patrik Flyktc4499812014-11-12 16:42:39 +02002424static int append_radio_msg(struct sk_buff *skb, int id,
2425 struct hwsim_new_radio_params *param)
Jukka Rissanen62759362014-10-31 14:48:47 +02002426{
Jukka Rissanen62759362014-10-31 14:48:47 +02002427 int ret;
2428
Jukka Rissanen62759362014-10-31 14:48:47 +02002429 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2430 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002431 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002432
2433 if (param->channels) {
2434 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2435 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002436 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002437 }
2438
2439 if (param->reg_alpha2) {
2440 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2441 param->reg_alpha2);
2442 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002443 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002444 }
2445
2446 if (param->regd) {
2447 int i;
2448
Johannes Berg58757552014-12-22 12:51:25 +01002449 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2450 if (hwsim_world_regdom_custom[i] != param->regd)
2451 continue;
Jukka Rissanen62759362014-10-31 14:48:47 +02002452
Jukka Rissanen62759362014-10-31 14:48:47 +02002453 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2454 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002455 return ret;
Johannes Berg58757552014-12-22 12:51:25 +01002456 break;
Jukka Rissanen62759362014-10-31 14:48:47 +02002457 }
2458 }
2459
2460 if (param->reg_strict) {
2461 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2462 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002463 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002464 }
2465
2466 if (param->p2p_device) {
2467 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2468 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002469 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002470 }
2471
2472 if (param->use_chanctx) {
2473 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2474 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002475 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002476 }
2477
2478 if (param->hwname) {
2479 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2480 strlen(param->hwname), param->hwname);
2481 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002482 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002483 }
2484
Patrik Flyktc4499812014-11-12 16:42:39 +02002485 return 0;
Jukka Rissanen62759362014-10-31 14:48:47 +02002486}
2487
Patrik Flyktc4499812014-11-12 16:42:39 +02002488static void hwsim_mcast_new_radio(int id, struct genl_info *info,
Jukka Rissanen62759362014-10-31 14:48:47 +02002489 struct hwsim_new_radio_params *param)
2490{
2491 struct sk_buff *mcast_skb;
Patrik Flyktc4499812014-11-12 16:42:39 +02002492 void *data;
Jukka Rissanen62759362014-10-31 14:48:47 +02002493
Patrik Flyktc4499812014-11-12 16:42:39 +02002494 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002495 if (!mcast_skb)
2496 return;
2497
Patrik Flyktc4499812014-11-12 16:42:39 +02002498 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2499 HWSIM_CMD_NEW_RADIO);
2500 if (!data)
2501 goto out_err;
2502
2503 if (append_radio_msg(mcast_skb, id, param) < 0)
2504 goto out_err;
2505
2506 genlmsg_end(mcast_skb, data);
2507
Jukka Rissanen62759362014-10-31 14:48:47 +02002508 hwsim_mcast_config_msg(mcast_skb, info);
Patrik Flyktc4499812014-11-12 16:42:39 +02002509 return;
2510
2511out_err:
2512 genlmsg_cancel(mcast_skb, data);
2513 nlmsg_free(mcast_skb);
Jukka Rissanen62759362014-10-31 14:48:47 +02002514}
2515
2516static int mac80211_hwsim_new_radio(struct genl_info *info,
2517 struct hwsim_new_radio_params *param)
Johannes Bergde0421d2014-01-06 23:12:12 +01002518{
2519 int err;
2520 u8 addr[ETH_ALEN];
2521 struct mac80211_hwsim_data *data;
2522 struct ieee80211_hw *hw;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002523 enum nl80211_band band;
Johannes Bergde0421d2014-01-06 23:12:12 +01002524 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
Martin Willi100cb9f2016-05-09 18:33:59 +02002525 struct net *net;
Johannes Bergde0421d2014-01-06 23:12:12 +01002526 int idx;
2527
Jukka Rissanen62759362014-10-31 14:48:47 +02002528 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
Luciano Coelho361c3e02014-02-10 15:23:03 +02002529 return -EINVAL;
2530
Johannes Bergde0421d2014-01-06 23:12:12 +01002531 spin_lock_bh(&hwsim_radio_lock);
2532 idx = hwsim_radio_idx++;
2533 spin_unlock_bh(&hwsim_radio_lock);
2534
Jukka Rissanen62759362014-10-31 14:48:47 +02002535 if (param->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002536 ops = &mac80211_hwsim_mchan_ops;
Jukka Rissanen62759362014-10-31 14:48:47 +02002537 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
Johannes Bergde0421d2014-01-06 23:12:12 +01002538 if (!hw) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002539 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
Johannes Bergde0421d2014-01-06 23:12:12 +01002540 err = -ENOMEM;
2541 goto failed;
2542 }
Martin Willi100cb9f2016-05-09 18:33:59 +02002543
Andrew Zaborowskif7d3b4f2017-02-23 13:02:10 +01002544 /* ieee80211_alloc_hw_nm may have used a default name */
2545 param->hwname = wiphy_name(hw->wiphy);
2546
Martin Willi100cb9f2016-05-09 18:33:59 +02002547 if (info)
2548 net = genl_info_net(info);
2549 else
2550 net = &init_net;
2551 wiphy_net_set(hw->wiphy, net);
2552
Johannes Bergde0421d2014-01-06 23:12:12 +01002553 data = hw->priv;
2554 data->hw = hw;
2555
2556 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2557 if (IS_ERR(data->dev)) {
2558 printk(KERN_DEBUG
2559 "mac80211_hwsim: device_create failed (%ld)\n",
2560 PTR_ERR(data->dev));
2561 err = -ENOMEM;
2562 goto failed_drvdata;
2563 }
2564 data->dev->driver = &mac80211_hwsim_driver.driver;
2565 err = device_bind_driver(data->dev);
2566 if (err != 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002567 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002568 err);
Junjie Mao805dbe12014-10-28 09:31:47 +08002569 goto failed_bind;
Johannes Bergde0421d2014-01-06 23:12:12 +01002570 }
2571
2572 skb_queue_head_init(&data->pending);
2573
2574 SET_IEEE80211_DEV(hw, data->dev);
Joe Perches93803b32015-03-02 19:54:49 -08002575 eth_zero_addr(addr);
Johannes Bergde0421d2014-01-06 23:12:12 +01002576 addr[0] = 0x02;
2577 addr[3] = idx >> 8;
2578 addr[4] = idx;
2579 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2580 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2581 data->addresses[1].addr[0] |= 0x40;
2582 hw->wiphy->n_addresses = 2;
2583 hw->wiphy->addresses = data->addresses;
2584
Jukka Rissanen62759362014-10-31 14:48:47 +02002585 data->channels = param->channels;
2586 data->use_chanctx = param->use_chanctx;
Johannes Bergbc7910982014-01-06 23:29:20 +01002587 data->idx = idx;
Jukka Rissanen62759362014-10-31 14:48:47 +02002588 data->destroy_on_close = param->destroy_on_close;
2589 if (info)
2590 data->portid = info->snd_portid;
Johannes Bergde0421d2014-01-06 23:12:12 +01002591
Luciano Coelho361c3e02014-02-10 15:23:03 +02002592 if (data->use_chanctx) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002593 hw->wiphy->max_scan_ssids = 255;
2594 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2595 hw->wiphy->max_remain_on_channel_duration = 1000;
Johannes Bergde0421d2014-01-06 23:12:12 +01002596 hw->wiphy->iface_combinations = &data->if_combination;
Jukka Rissanen62759362014-10-31 14:48:47 +02002597 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002598 data->if_combination = hwsim_if_comb_p2p_dev[0];
2599 else
2600 data->if_combination = hwsim_if_comb[0];
Ilan Peerf7736f52016-07-05 15:23:09 +03002601 hw->wiphy->n_iface_combinations = 1;
2602 /* For channels > 1 DFS is not allowed */
2603 data->if_combination.radar_detect_widths = 0;
Johannes Bergb59ec8d2014-02-07 10:44:50 +01002604 data->if_combination.num_different_channels = data->channels;
Jukka Rissanen62759362014-10-31 14:48:47 +02002605 } else if (param->p2p_device) {
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002606 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2607 hw->wiphy->n_iface_combinations =
2608 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
Johannes Bergde0421d2014-01-06 23:12:12 +01002609 } else {
2610 hw->wiphy->iface_combinations = hwsim_if_comb;
2611 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2612 }
2613
Johannes Berg661ef472015-11-24 18:29:45 +01002614 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
Johannes Bergde0421d2014-01-06 23:12:12 +01002615 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2616 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2617
2618 hw->queues = 5;
2619 hw->offchannel_tx_hw_queue = 4;
2620 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2621 BIT(NL80211_IFTYPE_AP) |
2622 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2623 BIT(NL80211_IFTYPE_P2P_GO) |
2624 BIT(NL80211_IFTYPE_ADHOC) |
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002625 BIT(NL80211_IFTYPE_MESH_POINT);
2626
Jukka Rissanen62759362014-10-31 14:48:47 +02002627 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002628 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002629
Johannes Berg30686bf2015-06-02 21:39:54 +02002630 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2631 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2632 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2633 ieee80211_hw_set(hw, QUEUE_CONTROL);
2634 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2635 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2636 ieee80211_hw_set(hw, MFP_CAPABLE);
2637 ieee80211_hw_set(hw, SIGNAL_DBM);
Johannes Berg33c2f532015-06-17 13:07:14 +02002638 ieee80211_hw_set(hw, TDLS_WIDER_BW);
Johannes Bergde0421d2014-01-06 23:12:12 +01002639 if (rctbl)
Johannes Berg30686bf2015-06-02 21:39:54 +02002640 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002641
2642 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2643 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02002644 WIPHY_FLAG_AP_UAPSD |
2645 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Eliad Peller0d8614b2014-09-10 14:07:36 +03002646 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2647 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2648 NL80211_FEATURE_STATIC_SMPS |
Johannes Berg339467b2014-06-12 22:41:00 +02002649 NL80211_FEATURE_DYNAMIC_SMPS |
2650 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
Jouni Malinen00eeccc2015-11-26 20:51:04 +02002651 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002652
2653 /* ask mac80211 to reserve space for magic */
2654 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2655 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2656 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2657
2658 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2659 sizeof(hwsim_channels_2ghz));
2660 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2661 sizeof(hwsim_channels_5ghz));
2662 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2663
Johannes Berg57fbcce2016-04-12 15:56:15 +02002664 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002665 struct ieee80211_supported_band *sband = &data->bands[band];
2666 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02002667 case NL80211_BAND_2GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002668 sband->channels = data->channels_2ghz;
2669 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2670 sband->bitrates = data->rates;
2671 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2672 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002673 case NL80211_BAND_5GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002674 sband->channels = data->channels_5ghz;
2675 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2676 sband->bitrates = data->rates + 4;
2677 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
Ilan Peerf33f8482015-04-07 19:05:21 +03002678
2679 sband->vht_cap.vht_supported = true;
2680 sband->vht_cap.cap =
2681 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2682 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2683 IEEE80211_VHT_CAP_RXLDPC |
2684 IEEE80211_VHT_CAP_SHORT_GI_80 |
2685 IEEE80211_VHT_CAP_SHORT_GI_160 |
2686 IEEE80211_VHT_CAP_TXSTBC |
2687 IEEE80211_VHT_CAP_RXSTBC_1 |
2688 IEEE80211_VHT_CAP_RXSTBC_2 |
2689 IEEE80211_VHT_CAP_RXSTBC_3 |
2690 IEEE80211_VHT_CAP_RXSTBC_4 |
2691 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2692 sband->vht_cap.vht_mcs.rx_mcs_map =
Ilan Peer8aca9b22015-04-07 19:05:22 +03002693 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2694 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002695 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002696 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2697 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002698 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2699 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002700 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
Ilan Peerf33f8482015-04-07 19:05:21 +03002701 sband->vht_cap.vht_mcs.tx_mcs_map =
2702 sband->vht_cap.vht_mcs.rx_mcs_map;
Johannes Bergde0421d2014-01-06 23:12:12 +01002703 break;
2704 default:
2705 continue;
2706 }
2707
2708 sband->ht_cap.ht_supported = true;
2709 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2710 IEEE80211_HT_CAP_GRN_FLD |
Ben Greeare49786d2014-10-24 11:12:08 -07002711 IEEE80211_HT_CAP_SGI_20 |
Johannes Bergde0421d2014-01-06 23:12:12 +01002712 IEEE80211_HT_CAP_SGI_40 |
2713 IEEE80211_HT_CAP_DSSSCCK40;
2714 sband->ht_cap.ampdu_factor = 0x3;
2715 sband->ht_cap.ampdu_density = 0x6;
2716 memset(&sband->ht_cap.mcs, 0,
2717 sizeof(sband->ht_cap.mcs));
2718 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2719 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2720 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2721
2722 hw->wiphy->bands[band] = sband;
Johannes Bergde0421d2014-01-06 23:12:12 +01002723 }
2724
2725 /* By default all radios belong to the first group */
2726 data->group = 1;
2727 mutex_init(&data->mutex);
2728
Martin Willi100cb9f2016-05-09 18:33:59 +02002729 data->netgroup = hwsim_net_get_netgroup(net);
2730
Johannes Bergde0421d2014-01-06 23:12:12 +01002731 /* Enable frame retransmissions for lossy channels */
2732 hw->max_rates = 4;
2733 hw->max_rate_tries = 11;
2734
Jouni Malinend5d011b2015-02-26 15:26:54 +02002735 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2736 hw->wiphy->n_vendor_commands =
2737 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2738 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2739 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2740
Jukka Rissanen62759362014-10-31 14:48:47 +02002741 if (param->reg_strict)
Johannes Berg26b0e412014-01-10 13:37:38 +01002742 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002743 if (param->regd) {
Patrik Flykt93d638d2014-11-12 16:42:40 +02002744 data->regd = param->regd;
Johannes Berg26b0e412014-01-10 13:37:38 +01002745 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002746 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
Johannes Berg26b0e412014-01-10 13:37:38 +01002747 /* give the regulatory workqueue a chance to run */
2748 schedule_timeout_interruptible(1);
2749 }
2750
Jukka Rissanen62759362014-10-31 14:48:47 +02002751 if (param->no_vif)
Johannes Berg30686bf2015-06-02 21:39:54 +02002752 ieee80211_hw_set(hw, NO_AUTO_VIF);
Ben Greear9a0cb892014-10-22 12:23:04 -07002753
Andrew Zaborowskiae44b502017-02-10 04:50:23 +01002754 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2755
Johannes Bergde0421d2014-01-06 23:12:12 +01002756 err = ieee80211_register_hw(hw);
2757 if (err < 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002758 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002759 err);
2760 goto failed_hw;
2761 }
2762
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002763 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
Johannes Bergde0421d2014-01-06 23:12:12 +01002764
Patrik Flykt93d638d2014-11-12 16:42:40 +02002765 if (param->reg_alpha2) {
2766 data->alpha2[0] = param->reg_alpha2[0];
2767 data->alpha2[1] = param->reg_alpha2[1];
Jukka Rissanen62759362014-10-31 14:48:47 +02002768 regulatory_hint(hw->wiphy, param->reg_alpha2);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002769 }
Johannes Berg26b0e412014-01-10 13:37:38 +01002770
Johannes Bergde0421d2014-01-06 23:12:12 +01002771 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2772 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2773 debugfs_create_file("group", 0666, data->debugfs, data,
2774 &hwsim_fops_group);
Luciano Coelho361c3e02014-02-10 15:23:03 +02002775 if (!data->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002776 debugfs_create_file("dfs_simulate_radar", 0222,
2777 data->debugfs,
2778 data, &hwsim_simulate_radar);
2779
2780 tasklet_hrtimer_init(&data->beacon_timer,
2781 mac80211_hwsim_beacon,
Thomas Gleixner8fbcfeb2017-02-25 10:27:37 +00002782 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002783
2784 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002785 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2786 hwsim_rht_params);
2787 if (err < 0) {
2788 pr_debug("mac80211_hwsim: radio index %d already present\n",
2789 idx);
2790 spin_unlock_bh(&hwsim_radio_lock);
2791 goto failed_final_insert;
2792 }
2793
Johannes Bergde0421d2014-01-06 23:12:12 +01002794 list_add_tail(&data->list, &hwsim_radios);
2795 spin_unlock_bh(&hwsim_radio_lock);
2796
Jukka Rissanen62759362014-10-31 14:48:47 +02002797 if (idx > 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002798 hwsim_mcast_new_radio(idx, info, param);
Jukka Rissanen62759362014-10-31 14:48:47 +02002799
Johannes Bergbc7910982014-01-06 23:29:20 +01002800 return idx;
Johannes Bergde0421d2014-01-06 23:12:12 +01002801
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002802failed_final_insert:
2803 debugfs_remove_recursive(data->debugfs);
2804 ieee80211_unregister_hw(data->hw);
Johannes Bergde0421d2014-01-06 23:12:12 +01002805failed_hw:
Junjie Mao805dbe12014-10-28 09:31:47 +08002806 device_release_driver(data->dev);
2807failed_bind:
Johannes Bergde0421d2014-01-06 23:12:12 +01002808 device_unregister(data->dev);
2809failed_drvdata:
2810 ieee80211_free_hw(hw);
2811failed:
2812 return err;
2813}
2814
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002815static void hwsim_mcast_del_radio(int id, const char *hwname,
2816 struct genl_info *info)
Johannes Berge4afb602014-01-06 22:43:11 +01002817{
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002818 struct sk_buff *skb;
2819 void *data;
2820 int ret;
2821
2822 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2823 if (!skb)
2824 return;
2825
2826 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2827 HWSIM_CMD_DEL_RADIO);
2828 if (!data)
2829 goto error;
2830
2831 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2832 if (ret < 0)
2833 goto error;
2834
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002835 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2836 hwname);
2837 if (ret < 0)
2838 goto error;
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002839
2840 genlmsg_end(skb, data);
2841
2842 hwsim_mcast_config_msg(skb, info);
2843
2844 return;
2845
2846error:
2847 nlmsg_free(skb);
2848}
2849
2850static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2851 const char *hwname,
2852 struct genl_info *info)
2853{
2854 hwsim_mcast_del_radio(data->idx, hwname, info);
Johannes Berge4afb602014-01-06 22:43:11 +01002855 debugfs_remove_recursive(data->debugfs);
2856 ieee80211_unregister_hw(data->hw);
2857 device_release_driver(data->dev);
2858 device_unregister(data->dev);
2859 ieee80211_free_hw(data->hw);
2860}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002861
Patrik Flykt93d638d2014-11-12 16:42:40 +02002862static int mac80211_hwsim_get_radio(struct sk_buff *skb,
2863 struct mac80211_hwsim_data *data,
2864 u32 portid, u32 seq,
2865 struct netlink_callback *cb, int flags)
2866{
2867 void *hdr;
2868 struct hwsim_new_radio_params param = { };
2869 int res = -EMSGSIZE;
2870
2871 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
2872 HWSIM_CMD_GET_RADIO);
2873 if (!hdr)
2874 return -EMSGSIZE;
2875
2876 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +01002877 genl_dump_check_consistent(cb, hdr);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002878
Patrik Flyktb0ad5e82014-11-21 15:08:02 +02002879 if (data->alpha2[0] && data->alpha2[1])
2880 param.reg_alpha2 = data->alpha2;
2881
Patrik Flykt93d638d2014-11-12 16:42:40 +02002882 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
2883 REGULATORY_STRICT_REG);
2884 param.p2p_device = !!(data->hw->wiphy->interface_modes &
2885 BIT(NL80211_IFTYPE_P2P_DEVICE));
2886 param.use_chanctx = data->use_chanctx;
2887 param.regd = data->regd;
2888 param.channels = data->channels;
2889 param.hwname = wiphy_name(data->hw->wiphy);
2890
2891 res = append_radio_msg(skb, data->idx, &param);
2892 if (res < 0)
2893 goto out_err;
2894
Johannes Berg053c0952015-01-16 22:09:00 +01002895 genlmsg_end(skb, hdr);
2896 return 0;
Patrik Flykt93d638d2014-11-12 16:42:40 +02002897
2898out_err:
2899 genlmsg_cancel(skb, hdr);
2900 return res;
2901}
2902
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002903static void mac80211_hwsim_free(void)
2904{
Johannes Berge4afb602014-01-06 22:43:11 +01002905 struct mac80211_hwsim_data *data;
Johannes Berg0e057d732008-09-12 00:39:22 +02002906
2907 spin_lock_bh(&hwsim_radio_lock);
Johannes Berge4afb602014-01-06 22:43:11 +01002908 while ((data = list_first_entry_or_null(&hwsim_radios,
2909 struct mac80211_hwsim_data,
2910 list))) {
2911 list_del(&data->list);
2912 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002913 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
2914 NULL);
Johannes Berge4afb602014-01-06 22:43:11 +01002915 spin_lock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002916 }
Johannes Berge4afb602014-01-06 22:43:11 +01002917 spin_unlock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002918 class_destroy(hwsim_class);
2919}
2920
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002921static const struct net_device_ops hwsim_netdev_ops = {
2922 .ndo_start_xmit = hwsim_mon_xmit,
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002923 .ndo_set_mac_address = eth_mac_addr,
2924 .ndo_validate_addr = eth_validate_addr,
2925};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002926
2927static void hwsim_mon_setup(struct net_device *dev)
2928{
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002929 dev->netdev_ops = &hwsim_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002930 dev->needs_free_netdev = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002931 ether_setup(dev);
Phil Sutter3db6da12015-08-18 10:30:43 +02002932 dev->priv_flags |= IFF_NO_QUEUE;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002933 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Joe Perches93803b32015-03-02 19:54:49 -08002934 eth_zero_addr(dev->dev_addr);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002935 dev->dev_addr[0] = 0x12;
2936}
2937
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002938static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
Javier Lopez78825132011-06-01 11:26:13 +02002939{
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002940 return rhashtable_lookup_fast(&hwsim_radios_rht,
2941 addr,
2942 hwsim_rht_params);
Javier Lopez78825132011-06-01 11:26:13 +02002943}
2944
Martin Willif21e4d82016-05-14 10:34:44 +02002945static void hwsim_register_wmediumd(struct net *net, u32 portid)
2946{
2947 struct mac80211_hwsim_data *data;
2948
2949 hwsim_net_set_wmediumd(net, portid);
2950
2951 spin_lock_bh(&hwsim_radio_lock);
2952 list_for_each_entry(data, &hwsim_radios, list) {
2953 if (data->netgroup == hwsim_net_get_netgroup(net))
2954 data->wmediumd = portid;
2955 }
2956 spin_unlock_bh(&hwsim_radio_lock);
2957}
2958
Javier Lopez78825132011-06-01 11:26:13 +02002959static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2960 struct genl_info *info)
2961{
2962
2963 struct ieee80211_hdr *hdr;
2964 struct mac80211_hwsim_data *data2;
2965 struct ieee80211_tx_info *txi;
2966 struct hwsim_tx_rate *tx_attempts;
Johannes Berg959eb2f2015-11-06 11:57:23 +01002967 u64 ret_skb_cookie;
Javier Lopez78825132011-06-01 11:26:13 +02002968 struct sk_buff *skb, *tmp;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002969 const u8 *src;
Javier Lopez78825132011-06-01 11:26:13 +02002970 unsigned int hwsim_flags;
Javier Lopez78825132011-06-01 11:26:13 +02002971 int i;
2972 bool found = false;
2973
2974 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002975 !info->attrs[HWSIM_ATTR_FLAGS] ||
2976 !info->attrs[HWSIM_ATTR_COOKIE] ||
Martin Willi62397da2016-05-13 12:41:48 +02002977 !info->attrs[HWSIM_ATTR_SIGNAL] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002978 !info->attrs[HWSIM_ATTR_TX_INFO])
Javier Lopez78825132011-06-01 11:26:13 +02002979 goto out;
2980
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002981 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
Javier Lopez78825132011-06-01 11:26:13 +02002982 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
Johannes Berg959eb2f2015-11-06 11:57:23 +01002983 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
Javier Lopez78825132011-06-01 11:26:13 +02002984
2985 data2 = get_hwsim_data_ref_from_addr(src);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002986 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02002987 goto out;
2988
Martin Willif21e4d82016-05-14 10:34:44 +02002989 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
2990 goto out;
2991
2992 if (info->snd_portid != data2->wmediumd)
2993 goto out;
2994
Javier Lopez78825132011-06-01 11:26:13 +02002995 /* look for the skb matching the cookie passed back from user */
2996 skb_queue_walk_safe(&data2->pending, skb, tmp) {
Johannes Berg959eb2f2015-11-06 11:57:23 +01002997 u64 skb_cookie;
2998
2999 txi = IEEE80211_SKB_CB(skb);
3000 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3001
3002 if (skb_cookie == ret_skb_cookie) {
Javier Lopez78825132011-06-01 11:26:13 +02003003 skb_unlink(skb, &data2->pending);
3004 found = true;
3005 break;
3006 }
3007 }
3008
3009 /* not found */
3010 if (!found)
3011 goto out;
3012
3013 /* Tx info received because the frame was broadcasted on user space,
3014 so we get all the necessary info: tx attempts and skb control buff */
3015
3016 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3017 info->attrs[HWSIM_ATTR_TX_INFO]);
3018
3019 /* now send back TX status */
3020 txi = IEEE80211_SKB_CB(skb);
3021
Javier Lopez78825132011-06-01 11:26:13 +02003022 ieee80211_tx_info_clear_status(txi);
3023
3024 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3025 txi->status.rates[i].idx = tx_attempts[i].idx;
3026 txi->status.rates[i].count = tx_attempts[i].count;
Javier Lopez78825132011-06-01 11:26:13 +02003027 }
3028
3029 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3030
3031 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3032 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3033 if (skb->len >= 16) {
3034 hdr = (struct ieee80211_hdr *) skb->data;
Javier Lopezc0e30762013-12-16 10:01:07 -08003035 mac80211_hwsim_monitor_ack(data2->channel,
Johannes Berge8261172012-07-27 19:48:26 +02003036 hdr->addr2);
Javier Lopez78825132011-06-01 11:26:13 +02003037 }
Qasim Javed06cf5c42012-06-05 01:25:44 -05003038 txi->flags |= IEEE80211_TX_STAT_ACK;
Javier Lopez78825132011-06-01 11:26:13 +02003039 }
3040 ieee80211_tx_status_irqsafe(data2->hw, skb);
3041 return 0;
3042out:
3043 return -EINVAL;
3044
3045}
3046
3047static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3048 struct genl_info *info)
3049{
Johannes Berge8261172012-07-27 19:48:26 +02003050 struct mac80211_hwsim_data *data2;
Javier Lopez78825132011-06-01 11:26:13 +02003051 struct ieee80211_rx_status rx_status;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003052 const u8 *dst;
Javier Lopez78825132011-06-01 11:26:13 +02003053 int frame_data_len;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003054 void *frame_data;
Javier Lopez78825132011-06-01 11:26:13 +02003055 struct sk_buff *skb = NULL;
3056
3057 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
Johannes Berge8261172012-07-27 19:48:26 +02003058 !info->attrs[HWSIM_ATTR_FRAME] ||
3059 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3060 !info->attrs[HWSIM_ATTR_SIGNAL])
Javier Lopez78825132011-06-01 11:26:13 +02003061 goto out;
3062
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003063 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
Javier Lopez78825132011-06-01 11:26:13 +02003064 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003065 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
Javier Lopez78825132011-06-01 11:26:13 +02003066
3067 /* Allocate new skb here */
3068 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3069 if (skb == NULL)
3070 goto err;
3071
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003072 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
Javier Lopez78825132011-06-01 11:26:13 +02003073 goto err;
3074
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003075 /* Copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02003076 skb_put_data(skb, frame_data, frame_data_len);
Javier Lopez78825132011-06-01 11:26:13 +02003077
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003078 data2 = get_hwsim_data_ref_from_addr(dst);
3079 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003080 goto out;
3081
Martin Willif21e4d82016-05-14 10:34:44 +02003082 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3083 goto out;
3084
3085 if (info->snd_portid != data2->wmediumd)
3086 goto out;
3087
Javier Lopez78825132011-06-01 11:26:13 +02003088 /* check if radio is configured properly */
3089
Johannes Berge8261172012-07-27 19:48:26 +02003090 if (data2->idle || !data2->started)
Javier Lopez78825132011-06-01 11:26:13 +02003091 goto out;
3092
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003093 /* A frame is received from user space */
Javier Lopez78825132011-06-01 11:26:13 +02003094 memset(&rx_status, 0, sizeof(rx_status));
Adam Welle641cf2a2015-12-01 17:13:52 -05003095 if (info->attrs[HWSIM_ATTR_FREQ]) {
3096 /* throw away off-channel packets, but allow both the temporary
3097 * ("hw" scan/remain-on-channel) and regular channel, since the
3098 * internal datapath also allows this
3099 */
3100 mutex_lock(&data2->mutex);
3101 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3102
3103 if (rx_status.freq != data2->channel->center_freq &&
3104 (!data2->tmp_chan ||
3105 rx_status.freq != data2->tmp_chan->center_freq)) {
3106 mutex_unlock(&data2->mutex);
3107 goto out;
3108 }
3109 mutex_unlock(&data2->mutex);
3110 } else {
3111 rx_status.freq = data2->channel->center_freq;
3112 }
3113
Javier Lopez78825132011-06-01 11:26:13 +02003114 rx_status.band = data2->channel->band;
3115 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3116 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3117
3118 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
Ben Greear2155c3f2014-10-22 12:22:58 -07003119 data2->rx_pkts++;
3120 data2->rx_bytes += skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02003121 ieee80211_rx_irqsafe(data2->hw, skb);
3122
3123 return 0;
3124err:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003125 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003126out:
3127 dev_kfree_skb(skb);
3128 return -EINVAL;
3129}
3130
3131static int hwsim_register_received_nl(struct sk_buff *skb_2,
3132 struct genl_info *info)
3133{
Martin Willif21e4d82016-05-14 10:34:44 +02003134 struct net *net = genl_info_net(info);
Johannes Bergc5ac0852014-01-07 23:28:08 +01003135 struct mac80211_hwsim_data *data;
3136 int chans = 1;
3137
3138 spin_lock_bh(&hwsim_radio_lock);
3139 list_for_each_entry(data, &hwsim_radios, list)
3140 chans = max(chans, data->channels);
3141 spin_unlock_bh(&hwsim_radio_lock);
3142
3143 /* In the future we should revise the userspace API and allow it
3144 * to set a flag that it does support multi-channel, then we can
3145 * let this pass conditionally on the flag.
3146 * For current userspace, prohibit it since it won't work right.
3147 */
3148 if (chans > 1)
3149 return -EOPNOTSUPP;
3150
Martin Willif21e4d82016-05-14 10:34:44 +02003151 if (hwsim_net_get_wmediumd(net))
Johannes Berg85ca8fc2014-01-07 23:21:34 +01003152 return -EBUSY;
Javier Lopez78825132011-06-01 11:26:13 +02003153
Martin Willif21e4d82016-05-14 10:34:44 +02003154 hwsim_register_wmediumd(net, info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003155
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003156 pr_debug("mac80211_hwsim: received a REGISTER, "
Eric W. Biederman15e47302012-09-07 20:12:54 +00003157 "switching to wmediumd mode with pid %d\n", info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003158
3159 return 0;
Javier Lopez78825132011-06-01 11:26:13 +02003160}
3161
Jukka Rissanen62759362014-10-31 14:48:47 +02003162static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003163{
Jukka Rissanen62759362014-10-31 14:48:47 +02003164 struct hwsim_new_radio_params param = { 0 };
Johannes Bergff4dd732017-02-27 17:15:28 +01003165 const char *hwname = NULL;
Ben Hutchings67bd5232017-11-10 18:48:50 +00003166 int ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02003167
3168 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3169 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3170 param.channels = channels;
3171 param.destroy_on_close =
3172 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
Johannes Bergbc7910982014-01-06 23:29:20 +01003173
3174 if (info->attrs[HWSIM_ATTR_CHANNELS])
Jukka Rissanen62759362014-10-31 14:48:47 +02003175 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
Johannes Bergbc7910982014-01-06 23:29:20 +01003176
Johannes Berg51a1aaa2018-01-15 09:32:36 +01003177 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3178 GENL_SET_ERR_MSG(info, "too many channels specified");
3179 return -EINVAL;
3180 }
3181
Ben Greear9a0cb892014-10-22 12:23:04 -07003182 if (info->attrs[HWSIM_ATTR_NO_VIF])
Jukka Rissanen62759362014-10-31 14:48:47 +02003183 param.no_vif = true;
Ben Greear9a0cb892014-10-22 12:23:04 -07003184
Johannes Bergff4dd732017-02-27 17:15:28 +01003185 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3186 hwname = kasprintf(GFP_KERNEL, "%.*s",
3187 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3188 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3189 if (!hwname)
3190 return -ENOMEM;
3191 param.hwname = hwname;
3192 }
Ben Greear5cd89262014-10-22 12:23:02 -07003193
Luciano Coelho361c3e02014-02-10 15:23:03 +02003194 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
Jukka Rissanen62759362014-10-31 14:48:47 +02003195 param.use_chanctx = true;
Luciano Coelho361c3e02014-02-10 15:23:03 +02003196 else
Jukka Rissanen62759362014-10-31 14:48:47 +02003197 param.use_chanctx = (param.channels > 1);
Luciano Coelho361c3e02014-02-10 15:23:03 +02003198
Johannes Berg26b0e412014-01-10 13:37:38 +01003199 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
Jukka Rissanen62759362014-10-31 14:48:47 +02003200 param.reg_alpha2 =
3201 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
Johannes Berg26b0e412014-01-10 13:37:38 +01003202
3203 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3204 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3205
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003206 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3207 kfree(hwname);
Johannes Berg26b0e412014-01-10 13:37:38 +01003208 return -EINVAL;
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003209 }
Jukka Rissanen62759362014-10-31 14:48:47 +02003210 param.regd = hwsim_world_regdom_custom[idx];
Johannes Berg26b0e412014-01-10 13:37:38 +01003211 }
3212
Ben Hutchings67bd5232017-11-10 18:48:50 +00003213 ret = mac80211_hwsim_new_radio(info, &param);
3214 kfree(hwname);
3215 return ret;
Johannes Bergbc7910982014-01-06 23:29:20 +01003216}
3217
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003218static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003219{
3220 struct mac80211_hwsim_data *data;
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003221 s64 idx = -1;
3222 const char *hwname = NULL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003223
Johannes Bergff4dd732017-02-27 17:15:28 +01003224 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003225 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
Johannes Bergff4dd732017-02-27 17:15:28 +01003226 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3227 hwname = kasprintf(GFP_KERNEL, "%.*s",
3228 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3229 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3230 if (!hwname)
3231 return -ENOMEM;
3232 } else
Johannes Bergbc7910982014-01-06 23:29:20 +01003233 return -EINVAL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003234
3235 spin_lock_bh(&hwsim_radio_lock);
3236 list_for_each_entry(data, &hwsim_radios, list) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003237 if (idx >= 0) {
3238 if (data->idx != idx)
3239 continue;
3240 } else {
Johannes Bergff4dd732017-02-27 17:15:28 +01003241 if (!hwname ||
3242 strcmp(hwname, wiphy_name(data->hw->wiphy)))
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003243 continue;
3244 }
3245
Martin Willi100cb9f2016-05-09 18:33:59 +02003246 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3247 continue;
3248
Johannes Bergbc7910982014-01-06 23:29:20 +01003249 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003250 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3251 hwsim_rht_params);
Johannes Bergbc7910982014-01-06 23:29:20 +01003252 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003253 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3254 info);
Johannes Bergff4dd732017-02-27 17:15:28 +01003255 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003256 return 0;
3257 }
3258 spin_unlock_bh(&hwsim_radio_lock);
3259
Johannes Bergff4dd732017-02-27 17:15:28 +01003260 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003261 return -ENODEV;
Javier Lopez78825132011-06-01 11:26:13 +02003262}
3263
Patrik Flykt93d638d2014-11-12 16:42:40 +02003264static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3265{
3266 struct mac80211_hwsim_data *data;
3267 struct sk_buff *skb;
3268 int idx, res = -ENODEV;
3269
3270 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3271 return -EINVAL;
3272 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3273
3274 spin_lock_bh(&hwsim_radio_lock);
3275 list_for_each_entry(data, &hwsim_radios, list) {
3276 if (data->idx != idx)
3277 continue;
3278
Martin Willi100cb9f2016-05-09 18:33:59 +02003279 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3280 continue;
3281
Jia-Ju Bai162bd5e2017-12-12 17:26:36 +08003282 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003283 if (!skb) {
3284 res = -ENOMEM;
3285 goto out_err;
3286 }
3287
3288 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3289 info->snd_seq, NULL, 0);
3290 if (res < 0) {
3291 nlmsg_free(skb);
3292 goto out_err;
3293 }
3294
3295 genlmsg_reply(skb, info);
3296 break;
3297 }
3298
3299out_err:
3300 spin_unlock_bh(&hwsim_radio_lock);
3301
3302 return res;
3303}
3304
3305static int hwsim_dump_radio_nl(struct sk_buff *skb,
3306 struct netlink_callback *cb)
3307{
3308 int idx = cb->args[0];
3309 struct mac80211_hwsim_data *data = NULL;
3310 int res;
3311
3312 spin_lock_bh(&hwsim_radio_lock);
3313
3314 if (idx == hwsim_radio_idx)
3315 goto done;
3316
3317 list_for_each_entry(data, &hwsim_radios, list) {
3318 if (data->idx < idx)
3319 continue;
3320
Martin Willi100cb9f2016-05-09 18:33:59 +02003321 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3322 continue;
3323
Patrik Flykt93d638d2014-11-12 16:42:40 +02003324 res = mac80211_hwsim_get_radio(skb, data,
3325 NETLINK_CB(cb->skb).portid,
3326 cb->nlh->nlmsg_seq, cb,
3327 NLM_F_MULTI);
3328 if (res < 0)
3329 break;
3330
3331 idx = data->idx + 1;
3332 }
3333
3334 cb->args[0] = idx;
3335
3336done:
3337 spin_unlock_bh(&hwsim_radio_lock);
3338 return skb->len;
3339}
3340
Javier Lopez78825132011-06-01 11:26:13 +02003341/* Generic Netlink operations array */
Johannes Berg4534de82013-11-14 17:14:46 +01003342static const struct genl_ops hwsim_ops[] = {
Javier Lopez78825132011-06-01 11:26:13 +02003343 {
3344 .cmd = HWSIM_CMD_REGISTER,
3345 .policy = hwsim_genl_policy,
3346 .doit = hwsim_register_received_nl,
Martin Willif21e4d82016-05-14 10:34:44 +02003347 .flags = GENL_UNS_ADMIN_PERM,
Javier Lopez78825132011-06-01 11:26:13 +02003348 },
3349 {
3350 .cmd = HWSIM_CMD_FRAME,
3351 .policy = hwsim_genl_policy,
3352 .doit = hwsim_cloned_frame_received_nl,
3353 },
3354 {
3355 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3356 .policy = hwsim_genl_policy,
3357 .doit = hwsim_tx_info_frame_received_nl,
3358 },
Johannes Bergbc7910982014-01-06 23:29:20 +01003359 {
Jukka Rissanen62759362014-10-31 14:48:47 +02003360 .cmd = HWSIM_CMD_NEW_RADIO,
Johannes Bergbc7910982014-01-06 23:29:20 +01003361 .policy = hwsim_genl_policy,
Jukka Rissanen62759362014-10-31 14:48:47 +02003362 .doit = hwsim_new_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003363 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003364 },
3365 {
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003366 .cmd = HWSIM_CMD_DEL_RADIO,
Johannes Bergbc7910982014-01-06 23:29:20 +01003367 .policy = hwsim_genl_policy,
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003368 .doit = hwsim_del_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003369 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003370 },
Patrik Flykt93d638d2014-11-12 16:42:40 +02003371 {
3372 .cmd = HWSIM_CMD_GET_RADIO,
3373 .policy = hwsim_genl_policy,
3374 .doit = hwsim_get_radio_nl,
3375 .dumpit = hwsim_dump_radio_nl,
3376 },
Javier Lopez78825132011-06-01 11:26:13 +02003377};
3378
Johannes Berg56989f62016-10-24 14:40:05 +02003379static struct genl_family hwsim_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003380 .name = "MAC80211_HWSIM",
3381 .version = 1,
3382 .maxattr = HWSIM_ATTR_MAX,
3383 .netnsok = true,
3384 .module = THIS_MODULE,
3385 .ops = hwsim_ops,
3386 .n_ops = ARRAY_SIZE(hwsim_ops),
3387 .mcgrps = hwsim_mcgrps,
3388 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3389};
3390
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003391static void destroy_radio(struct work_struct *work)
3392{
3393 struct mac80211_hwsim_data *data =
3394 container_of(work, struct mac80211_hwsim_data, destroy_work);
3395
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003396 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003397}
3398
3399static void remove_user_radios(u32 portid)
3400{
3401 struct mac80211_hwsim_data *entry, *tmp;
3402
3403 spin_lock_bh(&hwsim_radio_lock);
3404 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3405 if (entry->destroy_on_close && entry->portid == portid) {
3406 list_del(&entry->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003407 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3408 hwsim_rht_params);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003409 INIT_WORK(&entry->destroy_work, destroy_radio);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003410 queue_work(hwsim_wq, &entry->destroy_work);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003411 }
3412 }
3413 spin_unlock_bh(&hwsim_radio_lock);
3414}
3415
Javier Lopez78825132011-06-01 11:26:13 +02003416static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3417 unsigned long state,
3418 void *_notify)
3419{
3420 struct netlink_notify *notify = _notify;
3421
3422 if (state != NETLINK_URELEASE)
3423 return NOTIFY_DONE;
3424
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003425 remove_user_radios(notify->portid);
3426
Martin Willif21e4d82016-05-14 10:34:44 +02003427 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
Javier Lopez78825132011-06-01 11:26:13 +02003428 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3429 " socket, switching to perfect channel medium\n");
Martin Willif21e4d82016-05-14 10:34:44 +02003430 hwsim_register_wmediumd(notify->net, 0);
Javier Lopez78825132011-06-01 11:26:13 +02003431 }
3432 return NOTIFY_DONE;
3433
3434}
3435
3436static struct notifier_block hwsim_netlink_notifier = {
3437 .notifier_call = mac80211_hwsim_netlink_notify,
3438};
3439
Johannes Berg56989f62016-10-24 14:40:05 +02003440static int __init hwsim_init_netlink(void)
Javier Lopez78825132011-06-01 11:26:13 +02003441{
3442 int rc;
Johannes Berge8261172012-07-27 19:48:26 +02003443
Javier Lopez78825132011-06-01 11:26:13 +02003444 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3445
Johannes Berg489111e2016-10-24 14:40:03 +02003446 rc = genl_register_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003447 if (rc)
3448 goto failure;
3449
3450 rc = netlink_register_notifier(&hwsim_netlink_notifier);
Su Kang Yin2459cd82015-08-07 16:54:10 +08003451 if (rc) {
3452 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003453 goto failure;
Su Kang Yin2459cd82015-08-07 16:54:10 +08003454 }
Javier Lopez78825132011-06-01 11:26:13 +02003455
3456 return 0;
3457
3458failure:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003459 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003460 return -EINVAL;
3461}
3462
Martin Willi100cb9f2016-05-09 18:33:59 +02003463static __net_init int hwsim_init_net(struct net *net)
3464{
3465 hwsim_net_set_netgroup(net);
3466
3467 return 0;
3468}
3469
3470static void __net_exit hwsim_exit_net(struct net *net)
3471{
3472 struct mac80211_hwsim_data *data, *tmp;
3473
3474 spin_lock_bh(&hwsim_radio_lock);
3475 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3476 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3477 continue;
3478
3479 /* Radios created in init_net are returned to init_net. */
3480 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3481 continue;
3482
3483 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003484 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3485 hwsim_rht_params);
Martin Willi100cb9f2016-05-09 18:33:59 +02003486 INIT_WORK(&data->destroy_work, destroy_radio);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003487 queue_work(hwsim_wq, &data->destroy_work);
Martin Willi100cb9f2016-05-09 18:33:59 +02003488 }
3489 spin_unlock_bh(&hwsim_radio_lock);
3490}
3491
3492static struct pernet_operations hwsim_net_ops = {
3493 .init = hwsim_init_net,
3494 .exit = hwsim_exit_net,
3495 .id = &hwsim_net_id,
3496 .size = sizeof(struct hwsim_net),
3497};
3498
Javier Lopez78825132011-06-01 11:26:13 +02003499static void hwsim_exit_netlink(void)
3500{
Javier Lopez78825132011-06-01 11:26:13 +02003501 /* unregister the notifier */
3502 netlink_unregister_notifier(&hwsim_netlink_notifier);
3503 /* unregister the family */
Johannes Bergcf7a6b2d2014-01-06 21:55:14 +01003504 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003505}
3506
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003507static int __init init_mac80211_hwsim(void)
3508{
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003509 int i, err;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003510
Johannes Bergbc7910982014-01-06 23:29:20 +01003511 if (radios < 0 || radios > 100)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003512 return -EINVAL;
3513
Johannes Berge8261172012-07-27 19:48:26 +02003514 if (channels < 1)
3515 return -EINVAL;
3516
Johannes Berg0e057d732008-09-12 00:39:22 +02003517 spin_lock_init(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003518
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003519 hwsim_wq = alloc_workqueue("hwsim_wq",WQ_MEM_RECLAIM,0);
3520 if (!hwsim_wq)
3521 return -ENOMEM;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003522 rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003523
Martin Willi100cb9f2016-05-09 18:33:59 +02003524 err = register_pernet_device(&hwsim_net_ops);
Martin Pitt9ea927742013-04-08 11:30:01 +02003525 if (err)
3526 return err;
3527
Martin Willi100cb9f2016-05-09 18:33:59 +02003528 err = platform_driver_register(&mac80211_hwsim_driver);
3529 if (err)
3530 goto out_unregister_pernet;
3531
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003532 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
Martin Pitt9ea927742013-04-08 11:30:01 +02003533 if (IS_ERR(hwsim_class)) {
3534 err = PTR_ERR(hwsim_class);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003535 goto out_unregister_driver;
Martin Pitt9ea927742013-04-08 11:30:01 +02003536 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003537
Jukka Rissanen62759362014-10-31 14:48:47 +02003538 err = hwsim_init_netlink();
3539 if (err < 0)
3540 goto out_unregister_driver;
3541
Johannes Berg0e057d732008-09-12 00:39:22 +02003542 for (i = 0; i < radios; i++) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003543 struct hwsim_new_radio_params param = { 0 };
3544
3545 param.channels = channels;
Johannes Berg0e057d732008-09-12 00:39:22 +02003546
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003547 switch (regtest) {
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003548 case HWSIM_REGTEST_DIFF_COUNTRY:
Johannes Berg26b0e412014-01-10 13:37:38 +01003549 if (i < ARRAY_SIZE(hwsim_alpha2s))
Jukka Rissanen62759362014-10-31 14:48:47 +02003550 param.reg_alpha2 = hwsim_alpha2s[i];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003551 break;
3552 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3553 if (!i)
Jukka Rissanen62759362014-10-31 14:48:47 +02003554 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003555 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003556 case HWSIM_REGTEST_STRICT_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003557 param.reg_strict = true;
Johannes Berg26b0e412014-01-10 13:37:38 +01003558 case HWSIM_REGTEST_DRIVER_REG_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003559 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003560 break;
Johannes Berg26b0e412014-01-10 13:37:38 +01003561 case HWSIM_REGTEST_WORLD_ROAM:
3562 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003563 param.regd = &hwsim_world_regdom_custom_01;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003564 break;
3565 case HWSIM_REGTEST_CUSTOM_WORLD:
Jukka Rissanen62759362014-10-31 14:48:47 +02003566 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003567 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003568 case HWSIM_REGTEST_CUSTOM_WORLD_2:
Johannes Berg26b0e412014-01-10 13:37:38 +01003569 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003570 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003571 else if (i == 1)
Jukka Rissanen62759362014-10-31 14:48:47 +02003572 param.regd = &hwsim_world_regdom_custom_02;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003573 break;
3574 case HWSIM_REGTEST_STRICT_FOLLOW:
Johannes Berg26b0e412014-01-10 13:37:38 +01003575 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003576 param.reg_strict = true;
3577 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003578 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003579 break;
3580 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
Johannes Berg26b0e412014-01-10 13:37:38 +01003581 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003582 param.reg_strict = true;
3583 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003584 } else if (i == 1) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003585 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003586 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003587 break;
3588 case HWSIM_REGTEST_ALL:
Johannes Berg26b0e412014-01-10 13:37:38 +01003589 switch (i) {
3590 case 0:
Jukka Rissanen62759362014-10-31 14:48:47 +02003591 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003592 break;
3593 case 1:
Jukka Rissanen62759362014-10-31 14:48:47 +02003594 param.regd = &hwsim_world_regdom_custom_02;
Johannes Berg26b0e412014-01-10 13:37:38 +01003595 break;
3596 case 2:
Jukka Rissanen62759362014-10-31 14:48:47 +02003597 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003598 break;
3599 case 3:
Jukka Rissanen62759362014-10-31 14:48:47 +02003600 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003601 break;
3602 case 4:
Jukka Rissanen62759362014-10-31 14:48:47 +02003603 param.reg_strict = true;
3604 param.reg_alpha2 = hwsim_alpha2s[2];
Johannes Berg26b0e412014-01-10 13:37:38 +01003605 break;
3606 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003607 break;
3608 default:
3609 break;
3610 }
3611
Jukka Rissanen62759362014-10-31 14:48:47 +02003612 param.p2p_device = support_p2p_device;
3613 param.use_chanctx = channels > 1;
3614
3615 err = mac80211_hwsim_new_radio(NULL, &param);
Johannes Bergbc7910982014-01-06 23:29:20 +01003616 if (err < 0)
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003617 goto out_free_radios;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003618 }
3619
Tom Gundersenc835a672014-07-14 16:37:24 +02003620 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3621 hwsim_mon_setup);
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003622 if (hwsim_mon == NULL) {
3623 err = -ENOMEM;
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003624 goto out_free_radios;
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003625 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003626
Javier Lopez78825132011-06-01 11:26:13 +02003627 rtnl_lock();
Javier Lopez78825132011-06-01 11:26:13 +02003628 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003629 if (err < 0) {
3630 rtnl_unlock();
3631 goto out_free_radios;
3632 }
Javier Lopez78825132011-06-01 11:26:13 +02003633
3634 err = register_netdevice(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003635 if (err < 0) {
3636 rtnl_unlock();
3637 goto out_free_mon;
3638 }
Javier Lopez78825132011-06-01 11:26:13 +02003639 rtnl_unlock();
3640
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003641 return 0;
3642
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003643out_free_mon:
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003644 free_netdev(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003645out_free_radios:
Ian Schram3a33cc12008-07-21 13:19:35 -07003646 mac80211_hwsim_free();
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003647out_unregister_driver:
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003648 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003649out_unregister_pernet:
3650 unregister_pernet_device(&hwsim_net_ops);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003651 return err;
3652}
Johannes Bergf8fffc72012-07-27 15:16:02 +02003653module_init(init_mac80211_hwsim);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003654
3655static void __exit exit_mac80211_hwsim(void)
3656{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003657 pr_debug("mac80211_hwsim: unregister radios\n");
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003658
Javier Lopez78825132011-06-01 11:26:13 +02003659 hwsim_exit_netlink();
3660
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003661 mac80211_hwsim_free();
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003662 flush_workqueue(hwsim_wq);
3663
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003664 rhashtable_destroy(&hwsim_radios_rht);
Johannes Berg5d416352009-07-13 13:04:30 +02003665 unregister_netdev(hwsim_mon);
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003666 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003667 unregister_pernet_device(&hwsim_net_ops);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003668 destroy_workqueue(hwsim_wq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003669}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003670module_exit(exit_mac80211_hwsim);