blob: 80e2c8595c7c8d63da25cd245357d15acce4635b [file] [log] [blame]
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001/*
2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
Javier Lopez78825132011-06-01 11:26:13 +02004 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/*
12 * TODO:
Johannes Berg2b2d7792010-08-17 12:08:07 +020013 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030015 * - RX filtering based on filter configuration (data->rx_filter)
16 */
17
Johannes Berg0e057d732008-09-12 00:39:22 +020018#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Johannes Berg0e057d732008-09-12 00:39:22 +020020#include <linux/spinlock.h>
Johannes Berg90e30122009-06-18 14:51:12 +020021#include <net/dst.h>
22#include <net/xfrm.h>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030023#include <net/mac80211.h>
24#include <net/ieee80211_radiotap.h>
25#include <linux/if_arp.h>
26#include <linux/rtnetlink.h>
27#include <linux/etherdevice.h>
Martin Pitt9ea927742013-04-08 11:30:01 +020028#include <linux/platform_device.h>
Jouni Malinenfc6971d2008-10-30 19:59:05 +020029#include <linux/debugfs.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040030#include <linux/module.h>
Javier Cardona2f40b942012-03-05 17:20:37 -080031#include <linux/ktime.h>
Javier Lopez78825132011-06-01 11:26:13 +020032#include <net/genetlink.h>
Martin Willi100cb9f2016-05-09 18:33:59 +020033#include <net/net_namespace.h>
34#include <net/netns/generic.h>
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +010035#include <linux/rhashtable.h>
Jinbum Park3a2af7c2018-07-31 23:10:40 +090036#include <linux/nospec.h>
Javier Lopez78825132011-06-01 11:26:13 +020037#include "mac80211_hwsim.h"
38
39#define WARN_QUEUE 100
40#define MAX_QUEUE 200
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030041
42MODULE_AUTHOR("Jouni Malinen");
43MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
44MODULE_LICENSE("GPL");
45
46static int radios = 2;
47module_param(radios, int, 0444);
48MODULE_PARM_DESC(radios, "Number of simulated radios");
49
Johannes Berge8261172012-07-27 19:48:26 +020050static int channels = 1;
51module_param(channels, int, 0444);
52MODULE_PARM_DESC(channels, "Number of concurrent channels");
Johannes Berg69068032010-02-03 10:47:55 +010053
Johannes Berga357d7f2012-12-10 11:57:42 +010054static bool paged_rx = false;
55module_param(paged_rx, bool, 0644);
56MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
57
Karl Beldan1eb32172013-04-19 14:44:52 +020058static bool rctbl = false;
59module_param(rctbl, bool, 0444);
60MODULE_PARM_DESC(rctbl, "Handle rate control table");
61
Johannes Berg8c66a3d2014-01-10 20:07:49 +010062static bool support_p2p_device = true;
63module_param(support_p2p_device, bool, 0444);
64MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
65
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040066/**
67 * enum hwsim_regtest - the type of regulatory tests we offer
68 *
69 * These are the different values you can use for the regtest
70 * module parameter. This is useful to help test world roaming
71 * and the driver regulatory_hint() call and combinations of these.
72 * If you want to do specific alpha2 regulatory domain tests simply
73 * use the userspace regulatory request as that will be respected as
74 * well without the need of this module parameter. This is designed
75 * only for testing the driver regulatory request, world roaming
76 * and all possible combinations.
77 *
78 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
79 * this is the default value.
80 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
81 * hint, only one driver regulatory hint will be sent as such the
82 * secondary radios are expected to follow.
83 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
84 * request with all radios reporting the same regulatory domain.
85 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
86 * different regulatory domains requests. Expected behaviour is for
87 * an intersection to occur but each device will still use their
88 * respective regulatory requested domains. Subsequent radios will
89 * use the resulting intersection.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030090 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040091 * this by using a custom beacon-capable regulatory domain for the first
92 * radio. All other device world roam.
93 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
94 * domain requests. All radios will adhere to this custom world regulatory
95 * domain.
96 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
97 * domain requests. The first radio will adhere to the first custom world
98 * regulatory domain, the second one to the second custom world regulatory
99 * domain. All other devices will world roam.
100 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
101 * settings, only the first radio will send a regulatory domain request
102 * and use strict settings. The rest of the radios are expected to follow.
103 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
104 * settings. All radios will adhere to this.
105 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
106 * domain settings, combined with secondary driver regulatory domain
107 * settings. The first radio will get a strict regulatory domain setting
108 * using the first driver regulatory request and the second radio will use
109 * non-strict settings using the second driver regulatory request. All
110 * other devices should follow the intersection created between the
111 * first two.
112 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
113 * at least 6 radios for a complete test. We will test in this order:
114 * 1 - driver custom world regulatory domain
115 * 2 - second custom world regulatory domain
116 * 3 - first driver regulatory domain request
117 * 4 - second driver regulatory domain request
118 * 5 - strict regulatory domain settings using the third driver regulatory
119 * domain request
120 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
121 * regulatory requests.
122 */
123enum hwsim_regtest {
124 HWSIM_REGTEST_DISABLED = 0,
125 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
126 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
127 HWSIM_REGTEST_DIFF_COUNTRY = 3,
128 HWSIM_REGTEST_WORLD_ROAM = 4,
129 HWSIM_REGTEST_CUSTOM_WORLD = 5,
130 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
131 HWSIM_REGTEST_STRICT_FOLLOW = 7,
132 HWSIM_REGTEST_STRICT_ALL = 8,
133 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
134 HWSIM_REGTEST_ALL = 10,
135};
136
137/* Set to one of the HWSIM_REGTEST_* values above */
138static int regtest = HWSIM_REGTEST_DISABLED;
139module_param(regtest, int, 0444);
140MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
141
142static const char *hwsim_alpha2s[] = {
143 "FI",
144 "AL",
145 "US",
146 "DE",
147 "JP",
148 "AL",
149};
150
151static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
152 .n_reg_rules = 4,
153 .alpha2 = "99",
154 .reg_rules = {
155 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
156 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
157 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
158 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
159 }
160};
161
162static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
163 .n_reg_rules = 2,
164 .alpha2 = "99",
165 .reg_rules = {
166 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
167 REG_RULE(5725-10, 5850+10, 40, 0, 30,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200168 NL80211_RRF_NO_IR),
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -0400169 }
170};
171
Johannes Berg26b0e412014-01-10 13:37:38 +0100172static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
173 &hwsim_world_regdom_custom_01,
174 &hwsim_world_regdom_custom_02,
175};
176
Johannes Berg8aa21e62008-09-11 02:16:36 +0200177struct hwsim_vif_priv {
178 u32 magic;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200179 u8 bssid[ETH_ALEN];
180 bool assoc;
Jouni Malinen0bb861e2013-10-22 14:11:17 +0300181 bool bcn_en;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200182 u16 aid;
Johannes Berg8aa21e62008-09-11 02:16:36 +0200183};
184
185#define HWSIM_VIF_MAGIC 0x69537748
186
187static inline void hwsim_check_magic(struct ieee80211_vif *vif)
188{
189 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berge8261172012-07-27 19:48:26 +0200190 WARN(vp->magic != HWSIM_VIF_MAGIC,
191 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
192 vif, vp->magic, vif->addr, vif->type, vif->p2p);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200193}
194
195static inline void hwsim_set_magic(struct ieee80211_vif *vif)
196{
197 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
198 vp->magic = HWSIM_VIF_MAGIC;
199}
200
201static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
202{
203 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
204 vp->magic = 0;
205}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300206
Johannes Berg81c06522008-09-11 02:17:01 +0200207struct hwsim_sta_priv {
208 u32 magic;
209};
210
Johannes Berge8261172012-07-27 19:48:26 +0200211#define HWSIM_STA_MAGIC 0x6d537749
Johannes Berg81c06522008-09-11 02:17:01 +0200212
213static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
214{
215 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200216 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
Johannes Berg81c06522008-09-11 02:17:01 +0200217}
218
219static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
220{
221 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200222 sp->magic = HWSIM_STA_MAGIC;
Johannes Berg81c06522008-09-11 02:17:01 +0200223}
224
225static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
226{
227 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
228 sp->magic = 0;
229}
230
Johannes Berge8261172012-07-27 19:48:26 +0200231struct hwsim_chanctx_priv {
232 u32 magic;
233};
234
235#define HWSIM_CHANCTX_MAGIC 0x6d53774a
236
237static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
238{
239 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
240 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
241}
242
243static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
244{
245 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
246 cp->magic = HWSIM_CHANCTX_MAGIC;
247}
248
249static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
250{
251 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
252 cp->magic = 0;
253}
254
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300255static unsigned int hwsim_net_id;
Martin Willi100cb9f2016-05-09 18:33:59 +0200256
Matthew Wilcox60a05272018-04-10 16:36:33 -0700257static DEFINE_IDA(hwsim_netgroup_ida);
Martin Willi100cb9f2016-05-09 18:33:59 +0200258
259struct hwsim_net {
260 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200261 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200262};
263
264static inline int hwsim_net_get_netgroup(struct net *net)
265{
266 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
267
268 return hwsim_net->netgroup;
269}
270
Kirill Tkhai03695542018-03-01 14:30:09 +0300271static inline int hwsim_net_set_netgroup(struct net *net)
Martin Willi100cb9f2016-05-09 18:33:59 +0200272{
273 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
274
Kirill Tkhai03695542018-03-01 14:30:09 +0300275 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
276 0, 0, GFP_KERNEL);
277 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
Martin Willi100cb9f2016-05-09 18:33:59 +0200278}
279
Martin Willif21e4d82016-05-14 10:34:44 +0200280static inline u32 hwsim_net_get_wmediumd(struct net *net)
281{
282 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
283
284 return hwsim_net->wmediumd;
285}
286
287static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
288{
289 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
290
291 hwsim_net->wmediumd = portid;
292}
293
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300294static struct class *hwsim_class;
295
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300296static struct net_device *hwsim_mon; /* global monitor netdev */
297
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800298#define CHAN2G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200299 .band = NL80211_BAND_2GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800300 .center_freq = (_freq), \
301 .hw_value = (_freq), \
302 .max_power = 20, \
303}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300304
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800305#define CHAN5G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200306 .band = NL80211_BAND_5GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800307 .center_freq = (_freq), \
308 .hw_value = (_freq), \
309 .max_power = 20, \
310}
311
312static const struct ieee80211_channel hwsim_channels_2ghz[] = {
313 CHAN2G(2412), /* Channel 1 */
314 CHAN2G(2417), /* Channel 2 */
315 CHAN2G(2422), /* Channel 3 */
316 CHAN2G(2427), /* Channel 4 */
317 CHAN2G(2432), /* Channel 5 */
318 CHAN2G(2437), /* Channel 6 */
319 CHAN2G(2442), /* Channel 7 */
320 CHAN2G(2447), /* Channel 8 */
321 CHAN2G(2452), /* Channel 9 */
322 CHAN2G(2457), /* Channel 10 */
323 CHAN2G(2462), /* Channel 11 */
324 CHAN2G(2467), /* Channel 12 */
325 CHAN2G(2472), /* Channel 13 */
326 CHAN2G(2484), /* Channel 14 */
327};
328
329static const struct ieee80211_channel hwsim_channels_5ghz[] = {
330 CHAN5G(5180), /* Channel 36 */
331 CHAN5G(5200), /* Channel 40 */
332 CHAN5G(5220), /* Channel 44 */
333 CHAN5G(5240), /* Channel 48 */
334
335 CHAN5G(5260), /* Channel 52 */
336 CHAN5G(5280), /* Channel 56 */
337 CHAN5G(5300), /* Channel 60 */
338 CHAN5G(5320), /* Channel 64 */
339
340 CHAN5G(5500), /* Channel 100 */
341 CHAN5G(5520), /* Channel 104 */
342 CHAN5G(5540), /* Channel 108 */
343 CHAN5G(5560), /* Channel 112 */
344 CHAN5G(5580), /* Channel 116 */
345 CHAN5G(5600), /* Channel 120 */
346 CHAN5G(5620), /* Channel 124 */
347 CHAN5G(5640), /* Channel 128 */
348 CHAN5G(5660), /* Channel 132 */
349 CHAN5G(5680), /* Channel 136 */
350 CHAN5G(5700), /* Channel 140 */
351
352 CHAN5G(5745), /* Channel 149 */
353 CHAN5G(5765), /* Channel 153 */
354 CHAN5G(5785), /* Channel 157 */
355 CHAN5G(5805), /* Channel 161 */
356 CHAN5G(5825), /* Channel 165 */
Jouni Malinen85bbd802017-03-06 11:56:06 +0200357 CHAN5G(5845), /* Channel 169 */
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300358};
359
360static const struct ieee80211_rate hwsim_rates[] = {
361 { .bitrate = 10 },
362 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
363 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
364 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
365 { .bitrate = 60 },
366 { .bitrate = 90 },
367 { .bitrate = 120 },
368 { .bitrate = 180 },
369 { .bitrate = 240 },
370 { .bitrate = 360 },
371 { .bitrate = 480 },
372 { .bitrate = 540 }
373};
374
Jouni Malinend5d011b2015-02-26 15:26:54 +0200375#define OUI_QCA 0x001374
376#define QCA_NL80211_SUBCMD_TEST 1
377enum qca_nl80211_vendor_subcmds {
378 QCA_WLAN_VENDOR_ATTR_TEST = 8,
379 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
380};
381
382static const struct nla_policy
383hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
384 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
385};
386
387static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
388 struct wireless_dev *wdev,
389 const void *data, int data_len)
390{
391 struct sk_buff *skb;
392 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
393 int err;
394 u32 val;
395
396 err = nla_parse(tb, QCA_WLAN_VENDOR_ATTR_MAX, data, data_len,
Johannes Bergfceb6432017-04-12 14:34:07 +0200397 hwsim_vendor_test_policy, NULL);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200398 if (err)
399 return err;
400 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
401 return -EINVAL;
402 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200403 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200404
405 /* Send a vendor event as a test. Note that this would not normally be
406 * done within a command handler, but rather, based on some other
407 * trigger. For simplicity, this command is used to trigger the event
408 * here.
409 *
410 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
411 */
412 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
413 if (skb) {
414 /* skb_put() or nla_put() will fill up data within
415 * NL80211_ATTR_VENDOR_DATA.
416 */
417
418 /* Add vendor data */
419 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
420
421 /* Send the event - this will call nla_nest_end() */
422 cfg80211_vendor_event(skb, GFP_KERNEL);
423 }
424
425 /* Send a response to the command */
426 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
427 if (!skb)
428 return -ENOMEM;
429
430 /* skb_put() or nla_put() will fill up data within
431 * NL80211_ATTR_VENDOR_DATA
432 */
433 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
434
435 return cfg80211_vendor_cmd_reply(skb);
436}
437
438static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
439 {
440 .info = { .vendor_id = OUI_QCA,
441 .subcmd = QCA_NL80211_SUBCMD_TEST },
442 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
443 .doit = mac80211_hwsim_vendor_cmd_test,
444 }
445};
446
447/* Advertise support vendor specific events */
448static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
449 { .vendor_id = OUI_QCA, .subcmd = 1 },
450};
451
Johannes Bergde0421d2014-01-06 23:12:12 +0100452static const struct ieee80211_iface_limit hwsim_if_limits[] = {
453 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
454 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
455 BIT(NL80211_IFTYPE_P2P_CLIENT) |
456#ifdef CONFIG_MAC80211_MESH
457 BIT(NL80211_IFTYPE_MESH_POINT) |
458#endif
459 BIT(NL80211_IFTYPE_AP) |
460 BIT(NL80211_IFTYPE_P2P_GO) },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100461 /* must be last, see hwsim_if_comb */
462 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
Johannes Bergde0421d2014-01-06 23:12:12 +0100463};
464
Johannes Bergde0421d2014-01-06 23:12:12 +0100465static const struct ieee80211_iface_combination hwsim_if_comb[] = {
466 {
467 .limits = hwsim_if_limits,
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100468 /* remove the last entry which is P2P_DEVICE */
469 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
470 .max_interfaces = 2048,
471 .num_different_channels = 1,
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100472 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
473 BIT(NL80211_CHAN_WIDTH_20) |
474 BIT(NL80211_CHAN_WIDTH_40) |
475 BIT(NL80211_CHAN_WIDTH_80) |
476 BIT(NL80211_CHAN_WIDTH_160),
Ilan Peerf7736f52016-07-05 15:23:09 +0300477 },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100478};
479
480static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
481 {
482 .limits = hwsim_if_limits,
Johannes Bergde0421d2014-01-06 23:12:12 +0100483 .n_limits = ARRAY_SIZE(hwsim_if_limits),
484 .max_interfaces = 2048,
485 .num_different_channels = 1,
Johannes Bergde0421d2014-01-06 23:12:12 +0100486 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
487 BIT(NL80211_CHAN_WIDTH_20) |
488 BIT(NL80211_CHAN_WIDTH_40) |
489 BIT(NL80211_CHAN_WIDTH_80) |
490 BIT(NL80211_CHAN_WIDTH_160),
Ilan Peerf7736f52016-07-05 15:23:09 +0300491 },
Johannes Bergde0421d2014-01-06 23:12:12 +0100492};
493
Johannes Berg0e057d732008-09-12 00:39:22 +0200494static spinlock_t hwsim_radio_lock;
Johannes Bergb59abfb2016-09-15 16:30:03 +0300495static LIST_HEAD(hwsim_radios);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +0100496static struct workqueue_struct *hwsim_wq;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100497static struct rhashtable hwsim_radios_rht;
Johannes Berg2d689922014-01-06 22:56:30 +0100498static int hwsim_radio_idx;
Benjamin Beichler63356982018-01-22 18:04:37 +0100499static int hwsim_radios_generation = 1;
Johannes Berg0e057d732008-09-12 00:39:22 +0200500
Johannes Bergde0421d2014-01-06 23:12:12 +0100501static struct platform_driver mac80211_hwsim_driver = {
502 .driver = {
503 .name = "mac80211_hwsim",
Johannes Bergde0421d2014-01-06 23:12:12 +0100504 },
505};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300506
507struct mac80211_hwsim_data {
Johannes Berg0e057d732008-09-12 00:39:22 +0200508 struct list_head list;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100509 struct rhash_head rht;
Johannes Berg0e057d732008-09-12 00:39:22 +0200510 struct ieee80211_hw *hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300511 struct device *dev;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200512 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800513 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
514 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300515 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
Johannes Berg38ed5042014-01-06 22:11:06 +0100516 struct ieee80211_iface_combination if_combination;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300517
Johannes Bergef15aac2010-01-20 12:02:33 +0100518 struct mac_address addresses[2];
Johannes Bergbc7910982014-01-06 23:29:20 +0100519 int channels, idx;
Luciano Coelho361c3e02014-02-10 15:23:03 +0200520 bool use_chanctx;
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300521 bool destroy_on_close;
522 struct work_struct destroy_work;
523 u32 portid;
Patrik Flykt93d638d2014-11-12 16:42:40 +0200524 char alpha2[2];
525 const struct ieee80211_regdomain *regd;
Johannes Bergef15aac2010-01-20 12:02:33 +0100526
Johannes Berge8261172012-07-27 19:48:26 +0200527 struct ieee80211_channel *tmp_chan;
Johannes Berg661ef472015-11-24 18:29:45 +0100528 struct ieee80211_channel *roc_chan;
529 u32 roc_duration;
530 struct delayed_work roc_start;
Johannes Berge8261172012-07-27 19:48:26 +0200531 struct delayed_work roc_done;
532 struct delayed_work hw_scan;
533 struct cfg80211_scan_request *hw_scan_request;
534 struct ieee80211_vif *hw_scan_vif;
535 int scan_chan_idx;
Johannes Berg339467b2014-06-12 22:41:00 +0200536 u8 scan_addr[ETH_ALEN];
Johannes Berg7f813ce2013-11-06 10:37:34 +0100537 struct {
538 struct ieee80211_channel *channel;
539 unsigned long next_start, start, end;
540 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
541 ARRAY_SIZE(hwsim_channels_5ghz)];
Johannes Berge8261172012-07-27 19:48:26 +0200542
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300543 struct ieee80211_channel *channel;
Thomas Pedersen01e59e42013-01-02 14:55:17 -0800544 u64 beacon_int /* beacon interval in us */;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300545 unsigned int rx_filter;
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -0400546 bool started, idle, scanning;
547 struct mutex mutex;
Thomas Pedersen01e59e42013-01-02 14:55:17 -0800548 struct tasklet_hrtimer beacon_timer;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200549 enum ps_mode {
550 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
551 } ps;
552 bool ps_poll_pending;
553 struct dentry *debugfs;
Daniel Wagner73606d02009-05-18 19:49:25 +0200554
Johannes Berg959eb2f2015-11-06 11:57:23 +0100555 uintptr_t pending_cookie;
Javier Lopez78825132011-06-01 11:26:13 +0200556 struct sk_buff_head pending; /* packets pending */
Daniel Wagner73606d02009-05-18 19:49:25 +0200557 /*
558 * Only radios in the same group can communicate together (the
559 * channel has to match too). Each bit represents a group. A
Ben Greear9ebac152014-09-25 14:22:20 -0700560 * radio can be in more than one group.
Daniel Wagner73606d02009-05-18 19:49:25 +0200561 */
562 u64 group;
Blaise Gassendbdd7bd12010-10-28 02:01:24 -0700563
Martin Willi100cb9f2016-05-09 18:33:59 +0200564 /* group shared by radios created in the same netns */
565 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200566 /* wmediumd portid responsible for netgroup of this radio */
567 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200568
Javier Cardona2f40b942012-03-05 17:20:37 -0800569 /* difference between this hw's clock and the real clock, in usecs */
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800570 s64 tsf_offset;
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800571 s64 bcn_delta;
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800572 /* absolute beacon transmission time. Used to cover up "tx" delay. */
573 u64 abs_bcn_ts;
Ben Greear2155c3f2014-10-22 12:22:58 -0700574
575 /* Stats */
576 u64 tx_pkts;
577 u64 rx_pkts;
578 u64 tx_bytes;
579 u64 rx_bytes;
580 u64 tx_dropped;
581 u64 tx_failed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300582};
583
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100584static const struct rhashtable_params hwsim_rht_params = {
585 .nelem_hint = 2,
586 .automatic_shrinking = true,
587 .key_len = ETH_ALEN,
588 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
589 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
590};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300591
592struct hwsim_radiotap_hdr {
593 struct ieee80211_radiotap_header hdr;
Javier Cardona2f40b942012-03-05 17:20:37 -0800594 __le64 rt_tsft;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300595 u8 rt_flags;
596 u8 rt_rate;
597 __le16 rt_channel;
598 __le16 rt_chbitmask;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000599} __packed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300600
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200601struct hwsim_radiotap_ack_hdr {
602 struct ieee80211_radiotap_header hdr;
603 u8 rt_flags;
604 u8 pad;
605 __le16 rt_channel;
606 __le16 rt_chbitmask;
607} __packed;
608
Johannes Berg489111e2016-10-24 14:40:03 +0200609/* MAC80211_HWSIM netlink family */
610static struct genl_family hwsim_genl_family;
Javier Lopez78825132011-06-01 11:26:13 +0200611
Jukka Rissanen62759362014-10-31 14:48:47 +0200612enum hwsim_multicast_groups {
613 HWSIM_MCGRP_CONFIG,
614};
615
616static const struct genl_multicast_group hwsim_mcgrps[] = {
617 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
618};
619
Javier Lopez78825132011-06-01 11:26:13 +0200620/* MAC80211_HWSIM netlink policy */
621
Johannes Berg205d2422014-01-21 00:06:29 +0100622static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100623 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
624 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
Javier Lopez78825132011-06-01 11:26:13 +0200625 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
626 .len = IEEE80211_MAX_DATA_LEN },
627 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
628 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
629 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
630 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100631 .len = IEEE80211_TX_MAX_RATES *
632 sizeof(struct hwsim_tx_rate)},
Javier Lopez78825132011-06-01 11:26:13 +0200633 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
Johannes Bergbc7910982014-01-06 23:29:20 +0100634 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
635 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
Johannes Berg26b0e412014-01-10 13:37:38 +0100636 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
637 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
638 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100639 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300640 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
Johannes Bergde29eda2014-10-31 09:25:43 +0100641 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
642 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
643 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +0100644 [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
Javier Lopez78825132011-06-01 11:26:13 +0200645};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300646
Johannes Bergde0421d2014-01-06 23:12:12 +0100647static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
648 struct sk_buff *skb,
649 struct ieee80211_channel *chan);
650
651/* sysfs attributes */
652static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
653{
654 struct mac80211_hwsim_data *data = dat;
655 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
656 struct sk_buff *skb;
657 struct ieee80211_pspoll *pspoll;
658
659 if (!vp->assoc)
660 return;
661
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200662 wiphy_dbg(data->hw->wiphy,
663 "%s: send PS-Poll to %pM for aid %d\n",
664 __func__, vp->bssid, vp->aid);
Johannes Bergde0421d2014-01-06 23:12:12 +0100665
666 skb = dev_alloc_skb(sizeof(*pspoll));
667 if (!skb)
668 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200669 pspoll = skb_put(skb, sizeof(*pspoll));
Johannes Bergde0421d2014-01-06 23:12:12 +0100670 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
671 IEEE80211_STYPE_PSPOLL |
672 IEEE80211_FCTL_PM);
673 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
674 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
675 memcpy(pspoll->ta, mac, ETH_ALEN);
676
677 rcu_read_lock();
678 mac80211_hwsim_tx_frame(data->hw, skb,
679 rcu_dereference(vif->chanctx_conf)->def.chan);
680 rcu_read_unlock();
681}
682
683static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
684 struct ieee80211_vif *vif, int ps)
685{
686 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
687 struct sk_buff *skb;
688 struct ieee80211_hdr *hdr;
689
690 if (!vp->assoc)
691 return;
692
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200693 wiphy_dbg(data->hw->wiphy,
694 "%s: send data::nullfunc to %pM ps=%d\n",
695 __func__, vp->bssid, ps);
Johannes Bergde0421d2014-01-06 23:12:12 +0100696
697 skb = dev_alloc_skb(sizeof(*hdr));
698 if (!skb)
699 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200700 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
Johannes Bergde0421d2014-01-06 23:12:12 +0100701 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
702 IEEE80211_STYPE_NULLFUNC |
Adiel Alonib65c7b82017-12-18 12:14:04 +0200703 IEEE80211_FCTL_TODS |
Johannes Bergde0421d2014-01-06 23:12:12 +0100704 (ps ? IEEE80211_FCTL_PM : 0));
705 hdr->duration_id = cpu_to_le16(0);
706 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
707 memcpy(hdr->addr2, mac, ETH_ALEN);
708 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
709
710 rcu_read_lock();
711 mac80211_hwsim_tx_frame(data->hw, skb,
712 rcu_dereference(vif->chanctx_conf)->def.chan);
713 rcu_read_unlock();
714}
715
716
717static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
718 struct ieee80211_vif *vif)
719{
720 struct mac80211_hwsim_data *data = dat;
721 hwsim_send_nullfunc(data, mac, vif, 1);
722}
723
724static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
725 struct ieee80211_vif *vif)
726{
727 struct mac80211_hwsim_data *data = dat;
728 hwsim_send_nullfunc(data, mac, vif, 0);
729}
730
731static int hwsim_fops_ps_read(void *dat, u64 *val)
732{
733 struct mac80211_hwsim_data *data = dat;
734 *val = data->ps;
735 return 0;
736}
737
738static int hwsim_fops_ps_write(void *dat, u64 val)
739{
740 struct mac80211_hwsim_data *data = dat;
741 enum ps_mode old_ps;
742
743 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
744 val != PS_MANUAL_POLL)
745 return -EINVAL;
746
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200747 if (val == PS_MANUAL_POLL) {
748 if (data->ps != PS_ENABLED)
749 return -EINVAL;
750 local_bh_disable();
751 ieee80211_iterate_active_interfaces_atomic(
752 data->hw, IEEE80211_IFACE_ITER_NORMAL,
753 hwsim_send_ps_poll, data);
754 local_bh_enable();
755 return 0;
756 }
Johannes Bergde0421d2014-01-06 23:12:12 +0100757 old_ps = data->ps;
758 data->ps = val;
759
Johannes Berga809ca52015-01-09 11:29:50 +0100760 local_bh_disable();
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200761 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100762 ieee80211_iterate_active_interfaces_atomic(
763 data->hw, IEEE80211_IFACE_ITER_NORMAL,
764 hwsim_send_nullfunc_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100765 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100766 ieee80211_iterate_active_interfaces_atomic(
767 data->hw, IEEE80211_IFACE_ITER_NORMAL,
768 hwsim_send_nullfunc_no_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100769 }
Johannes Berga809ca52015-01-09 11:29:50 +0100770 local_bh_enable();
Johannes Bergde0421d2014-01-06 23:12:12 +0100771
772 return 0;
773}
774
775DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
776 "%llu\n");
777
778static int hwsim_write_simulate_radar(void *dat, u64 val)
779{
780 struct mac80211_hwsim_data *data = dat;
781
782 ieee80211_radar_detected(data->hw);
783
784 return 0;
785}
786
787DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
788 hwsim_write_simulate_radar, "%llu\n");
789
790static int hwsim_fops_group_read(void *dat, u64 *val)
791{
792 struct mac80211_hwsim_data *data = dat;
793 *val = data->group;
794 return 0;
795}
796
797static int hwsim_fops_group_write(void *dat, u64 val)
798{
799 struct mac80211_hwsim_data *data = dat;
800 data->group = val;
801 return 0;
802}
803
804DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
805 hwsim_fops_group_read, hwsim_fops_group_write,
806 "%llx\n");
807
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000808static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
809 struct net_device *dev)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300810{
811 /* TODO: allow packet injection */
812 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000813 return NETDEV_TX_OK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300814}
815
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800816static inline u64 mac80211_hwsim_get_tsf_raw(void)
817{
818 return ktime_to_us(ktime_get_real());
819}
820
Javier Cardona2f40b942012-03-05 17:20:37 -0800821static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
822{
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800823 u64 now = mac80211_hwsim_get_tsf_raw();
Javier Cardona2f40b942012-03-05 17:20:37 -0800824 return cpu_to_le64(now + data->tsf_offset);
825}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300826
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800827static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800828 struct ieee80211_vif *vif)
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800829{
830 struct mac80211_hwsim_data *data = hw->priv;
831 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
832}
833
834static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
835 struct ieee80211_vif *vif, u64 tsf)
836{
837 struct mac80211_hwsim_data *data = hw->priv;
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800838 u64 now = mac80211_hwsim_get_tsf(hw, vif);
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800839 u32 bcn_int = data->beacon_int;
Andrew Morton79211c82015-11-09 14:58:13 -0800840 u64 delta = abs(tsf - now);
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800841
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800842 /* adjust after beaconing with new timestamp at old TBTT */
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700843 if (tsf > now) {
844 data->tsf_offset += delta;
845 data->bcn_delta = do_div(delta, bcn_int);
846 } else {
847 data->tsf_offset -= delta;
Benjamin Beichler4fb7f8a2016-11-11 17:37:56 +0100848 data->bcn_delta = -(s64)do_div(delta, bcn_int);
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700849 }
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800850}
851
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300852static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +0200853 struct sk_buff *tx_skb,
854 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300855{
856 struct mac80211_hwsim_data *data = hw->priv;
857 struct sk_buff *skb;
858 struct hwsim_radiotap_hdr *hdr;
859 u16 flags;
860 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
861 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
862
Amit Khatri89f44d82015-11-30 12:46:52 +0530863 if (WARN_ON(!txrate))
864 return;
865
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300866 if (!netif_running(hwsim_mon))
867 return;
868
869 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
870 if (skb == NULL)
871 return;
872
Johannes Bergd58ff352017-06-16 14:29:23 +0200873 hdr = skb_push(skb, sizeof(*hdr));
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300874 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
875 hdr->hdr.it_pad = 0;
876 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
Jouni Malinenf248f102008-06-13 19:44:47 +0300877 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
878 (1 << IEEE80211_RADIOTAP_RATE) |
Javier Cardona2f40b942012-03-05 17:20:37 -0800879 (1 << IEEE80211_RADIOTAP_TSFT) |
Jouni Malinenf248f102008-06-13 19:44:47 +0300880 (1 << IEEE80211_RADIOTAP_CHANNEL));
Javier Cardona2f40b942012-03-05 17:20:37 -0800881 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300882 hdr->rt_flags = 0;
883 hdr->rt_rate = txrate->bitrate / 5;
Johannes Berge8261172012-07-27 19:48:26 +0200884 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300885 flags = IEEE80211_CHAN_2GHZ;
886 if (txrate->flags & IEEE80211_RATE_ERP_G)
887 flags |= IEEE80211_CHAN_OFDM;
888 else
889 flags |= IEEE80211_CHAN_CCK;
890 hdr->rt_chbitmask = cpu_to_le16(flags);
891
892 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000893 skb_reset_mac_header(skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300894 skb->ip_summed = CHECKSUM_UNNECESSARY;
895 skb->pkt_type = PACKET_OTHERHOST;
Jouni Malinenf248f102008-06-13 19:44:47 +0300896 skb->protocol = htons(ETH_P_802_2);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300897 memset(skb->cb, 0, sizeof(skb->cb));
898 netif_rx(skb);
899}
900
901
Johannes Berge8261172012-07-27 19:48:26 +0200902static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
903 const u8 *addr)
Jouni Malinen6c085222009-11-01 11:31:45 +0200904{
Jouni Malinen6c085222009-11-01 11:31:45 +0200905 struct sk_buff *skb;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200906 struct hwsim_radiotap_ack_hdr *hdr;
Jouni Malinen6c085222009-11-01 11:31:45 +0200907 u16 flags;
908 struct ieee80211_hdr *hdr11;
909
910 if (!netif_running(hwsim_mon))
911 return;
912
913 skb = dev_alloc_skb(100);
914 if (skb == NULL)
915 return;
916
Johannes Berg4df864c2017-06-16 14:29:21 +0200917 hdr = skb_put(skb, sizeof(*hdr));
Jouni Malinen6c085222009-11-01 11:31:45 +0200918 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
919 hdr->hdr.it_pad = 0;
920 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
921 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
922 (1 << IEEE80211_RADIOTAP_CHANNEL));
923 hdr->rt_flags = 0;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200924 hdr->pad = 0;
Johannes Berge8261172012-07-27 19:48:26 +0200925 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinen6c085222009-11-01 11:31:45 +0200926 flags = IEEE80211_CHAN_2GHZ;
927 hdr->rt_chbitmask = cpu_to_le16(flags);
928
Johannes Berg4df864c2017-06-16 14:29:21 +0200929 hdr11 = skb_put(skb, 10);
Jouni Malinen6c085222009-11-01 11:31:45 +0200930 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
931 IEEE80211_STYPE_ACK);
932 hdr11->duration_id = cpu_to_le16(0);
933 memcpy(hdr11->addr1, addr, ETH_ALEN);
934
935 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000936 skb_reset_mac_header(skb);
Jouni Malinen6c085222009-11-01 11:31:45 +0200937 skb->ip_summed = CHECKSUM_UNNECESSARY;
938 skb->pkt_type = PACKET_OTHERHOST;
939 skb->protocol = htons(ETH_P_802_2);
940 memset(skb->cb, 0, sizeof(skb->cb));
941 netif_rx(skb);
942}
943
Johannes Berg3283e2862014-06-12 22:34:49 +0200944struct mac80211_hwsim_addr_match_data {
945 u8 addr[ETH_ALEN];
946 bool ret;
947};
948
949static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
950 struct ieee80211_vif *vif)
951{
952 struct mac80211_hwsim_addr_match_data *md = data;
953
954 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
955 md->ret = true;
956}
957
958static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
959 const u8 *addr)
960{
961 struct mac80211_hwsim_addr_match_data md = {
962 .ret = false,
963 };
964
Johannes Berg339467b2014-06-12 22:41:00 +0200965 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
966 return true;
967
Johannes Berg3283e2862014-06-12 22:34:49 +0200968 memcpy(md.addr, addr, ETH_ALEN);
969
970 ieee80211_iterate_active_interfaces_atomic(data->hw,
971 IEEE80211_IFACE_ITER_NORMAL,
972 mac80211_hwsim_addr_iter,
973 &md);
974
975 return md.ret;
976}
Jouni Malinen6c085222009-11-01 11:31:45 +0200977
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200978static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
979 struct sk_buff *skb)
980{
981 switch (data->ps) {
982 case PS_DISABLED:
983 return true;
984 case PS_ENABLED:
985 return false;
986 case PS_AUTO_POLL:
987 /* TODO: accept (some) Beacons by default and other frames only
988 * if pending PS-Poll has been sent */
989 return true;
990 case PS_MANUAL_POLL:
991 /* Allow unicast frames to own address if there is a pending
992 * PS-Poll */
993 if (data->ps_poll_pending &&
Johannes Berg3283e2862014-06-12 22:34:49 +0200994 mac80211_hwsim_addr_match(data, skb->data + 4)) {
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200995 data->ps_poll_pending = false;
996 return true;
997 }
998 return false;
999 }
1000
1001 return true;
1002}
1003
Martin Willif21e4d82016-05-14 10:34:44 +02001004static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1005 struct sk_buff *skb, int portid)
1006{
1007 struct net *net;
1008 bool found = false;
1009 int res = -ENOENT;
1010
1011 rcu_read_lock();
1012 for_each_net_rcu(net) {
1013 if (data->netgroup == hwsim_net_get_netgroup(net)) {
1014 res = genlmsg_unicast(net, skb, portid);
1015 found = true;
1016 break;
1017 }
1018 }
1019 rcu_read_unlock();
1020
1021 if (!found)
1022 nlmsg_free(skb);
1023
1024 return res;
1025}
1026
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001027static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1028{
1029 u16 result = 0;
1030
1031 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1032 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1033 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1034 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1035 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1036 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1037 if (rate->flags & IEEE80211_TX_RC_MCS)
1038 result |= MAC80211_HWSIM_TX_RC_MCS;
1039 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1040 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1041 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1042 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1043 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1044 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1045 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1046 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1047 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1048 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1049 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1050 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1051 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1052 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1053
1054 return result;
1055}
1056
Javier Lopez78825132011-06-01 11:26:13 +02001057static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1058 struct sk_buff *my_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001059 int dst_portid)
Javier Lopez78825132011-06-01 11:26:13 +02001060{
1061 struct sk_buff *skb;
1062 struct mac80211_hwsim_data *data = hw->priv;
1063 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1064 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1065 void *msg_head;
1066 unsigned int hwsim_flags = 0;
1067 int i;
1068 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001069 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
Johannes Berg959eb2f2015-11-06 11:57:23 +01001070 uintptr_t cookie;
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001071
Javier Lopez78825132011-06-01 11:26:13 +02001072 if (data->ps != PS_DISABLED)
1073 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1074 /* If the queue contains MAX_QUEUE skb's drop some */
1075 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1076 /* Droping until WARN_QUEUE level */
Ben Greear2155c3f2014-10-22 12:22:58 -07001077 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
Ben Greearb7bc9672014-09-25 14:22:18 -07001078 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
Ben Greear2155c3f2014-10-22 12:22:58 -07001079 data->tx_dropped++;
1080 }
Javier Lopez78825132011-06-01 11:26:13 +02001081 }
1082
Thomas Graf58050fc2012-06-28 03:57:45 +00001083 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Javier Lopez78825132011-06-01 11:26:13 +02001084 if (skb == NULL)
1085 goto nla_put_failure;
1086
1087 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1088 HWSIM_CMD_FRAME);
1089 if (msg_head == NULL) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001090 pr_debug("mac80211_hwsim: problem with msg_head\n");
Javier Lopez78825132011-06-01 11:26:13 +02001091 goto nla_put_failure;
1092 }
1093
Bob Copeland354210f2015-12-19 09:59:37 -05001094 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1095 ETH_ALEN, data->addresses[1].addr))
David S. Miller633c9382012-04-01 21:03:30 -04001096 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001097
1098 /* We get the skb->data */
David S. Miller633c9382012-04-01 21:03:30 -04001099 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1100 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001101
1102 /* We get the flags for this transmission, and we translate them to
1103 wmediumd flags */
1104
1105 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1106 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1107
1108 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1109 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1110
David S. Miller633c9382012-04-01 21:03:30 -04001111 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1112 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001113
Ben Greear4f86ed82014-10-27 15:04:46 -07001114 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1115 goto nla_put_failure;
1116
Javier Lopez78825132011-06-01 11:26:13 +02001117 /* We get the tx control (rate and retries) info*/
1118
1119 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1120 tx_attempts[i].idx = info->status.rates[i].idx;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001121 tx_attempts_flags[i].idx = info->status.rates[i].idx;
Javier Lopez78825132011-06-01 11:26:13 +02001122 tx_attempts[i].count = info->status.rates[i].count;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001123 tx_attempts_flags[i].flags =
1124 trans_tx_rate_flags_ieee2hwsim(
1125 &info->status.rates[i]);
Javier Lopez78825132011-06-01 11:26:13 +02001126 }
1127
David S. Miller633c9382012-04-01 21:03:30 -04001128 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1129 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1130 tx_attempts))
1131 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001132
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001133 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1134 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1135 tx_attempts_flags))
1136 goto nla_put_failure;
1137
Javier Lopez78825132011-06-01 11:26:13 +02001138 /* We create a cookie to identify this skb */
Johannes Berg959eb2f2015-11-06 11:57:23 +01001139 data->pending_cookie++;
1140 cookie = data->pending_cookie;
1141 info->rate_driver_data[0] = (void *)cookie;
Nicolas Dichtel08f4cbb2016-04-26 10:06:12 +02001142 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
David S. Miller633c9382012-04-01 21:03:30 -04001143 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001144
1145 genlmsg_end(skb, msg_head);
Martin Willif21e4d82016-05-14 10:34:44 +02001146 if (hwsim_unicast_netgroup(data, skb, dst_portid))
Bob Copelandf2831e22015-02-05 08:45:43 -05001147 goto err_free_txskb;
Javier Lopez78825132011-06-01 11:26:13 +02001148
1149 /* Enqueue the packet */
1150 skb_queue_tail(&data->pending, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001151 data->tx_pkts++;
1152 data->tx_bytes += my_skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02001153 return;
1154
1155nla_put_failure:
Bob Copelandf2831e22015-02-05 08:45:43 -05001156 nlmsg_free(skb);
1157err_free_txskb:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001158 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Ben Greearb7bc9672014-09-25 14:22:18 -07001159 ieee80211_free_txskb(hw, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001160 data->tx_failed++;
Javier Lopez78825132011-06-01 11:26:13 +02001161}
1162
Johannes Berge8261172012-07-27 19:48:26 +02001163static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1164 struct ieee80211_channel *c2)
1165{
1166 if (!c1 || !c2)
1167 return false;
1168
1169 return c1->center_freq == c2->center_freq;
1170}
1171
1172struct tx_iter_data {
1173 struct ieee80211_channel *channel;
1174 bool receive;
1175};
1176
1177static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1178 struct ieee80211_vif *vif)
1179{
1180 struct tx_iter_data *data = _data;
1181
1182 if (!vif->chanctx_conf)
1183 return;
1184
1185 if (!hwsim_chans_compat(data->channel,
Johannes Berg4bf88532012-11-09 11:39:59 +01001186 rcu_dereference(vif->chanctx_conf)->def.chan))
Johannes Berge8261172012-07-27 19:48:26 +02001187 return;
1188
1189 data->receive = true;
1190}
1191
Johannes Berg1f7bba72014-11-06 22:56:36 +01001192static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1193{
1194 /*
1195 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1196 * e.g. like this:
1197 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1198 * (but you should use a valid OUI, not that)
1199 *
1200 * If anyone wants to 'donate' a radiotap OUI/subns code
1201 * please send a patch removing this #ifdef and changing
1202 * the values accordingly.
1203 */
1204#ifdef HWSIM_RADIOTAP_OUI
1205 struct ieee80211_vendor_radiotap *rtap;
1206
1207 /*
1208 * Note that this code requires the headroom in the SKB
1209 * that was allocated earlier.
1210 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001211 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
Johannes Berg1f7bba72014-11-06 22:56:36 +01001212 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1213 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1214 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1215 rtap->subns = 127;
1216
1217 /*
1218 * Radiotap vendor namespaces can (and should) also be
1219 * split into fields by using the standard radiotap
1220 * presence bitmap mechanism. Use just BIT(0) here for
1221 * the presence bitmap.
1222 */
1223 rtap->present = BIT(0);
1224 /* We have 8 bytes of (dummy) data */
1225 rtap->len = 8;
1226 /* For testing, also require it to be aligned */
1227 rtap->align = 8;
1228 /* And also test that padding works, 4 bytes */
1229 rtap->pad = 4;
1230 /* push the data */
1231 memcpy(rtap->data, "ABCDEFGH", 8);
1232 /* make sure to clear padding, mac80211 doesn't */
1233 memset(rtap->data + 8, 0, 4);
1234
1235 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1236#endif
1237}
1238
Javier Lopez78825132011-06-01 11:26:13 +02001239static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +02001240 struct sk_buff *skb,
1241 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001242{
Johannes Berg0e057d732008-09-12 00:39:22 +02001243 struct mac80211_hwsim_data *data = hw->priv, *data2;
1244 bool ack = false;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001245 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001246 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001247 struct ieee80211_rx_status rx_status;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001248 u64 now;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001249
1250 memset(&rx_status, 0, sizeof(rx_status));
Thomas Pedersenf4bda332012-11-13 10:46:27 -08001251 rx_status.flag |= RX_FLAG_MACTIME_START;
Johannes Berge8261172012-07-27 19:48:26 +02001252 rx_status.freq = chan->center_freq;
1253 rx_status.band = chan->band;
Karl Beldandad63302013-04-15 17:09:30 +02001254 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1255 rx_status.rate_idx =
1256 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
Johannes Berg8613c942017-04-26 13:51:41 +02001257 rx_status.nss =
Karl Beldandad63302013-04-15 17:09:30 +02001258 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
Johannes Bergda6a4352017-04-26 12:14:59 +02001259 rx_status.encoding = RX_ENC_VHT;
Karl Beldandad63302013-04-15 17:09:30 +02001260 } else {
1261 rx_status.rate_idx = info->control.rates[0].idx;
1262 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
Johannes Bergda6a4352017-04-26 12:14:59 +02001263 rx_status.encoding = RX_ENC_HT;
Karl Beldandad63302013-04-15 17:09:30 +02001264 }
Jouni Malinen281ed292011-08-06 23:07:00 +03001265 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
Johannes Berg2f242bf2017-05-05 11:53:19 +02001266 rx_status.bw = RATE_INFO_BW_40;
1267 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1268 rx_status.bw = RATE_INFO_BW_80;
1269 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1270 rx_status.bw = RATE_INFO_BW_160;
1271 else
1272 rx_status.bw = RATE_INFO_BW_20;
Jouni Malinen281ed292011-08-06 23:07:00 +03001273 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
Johannes Berg7fdd69c2017-04-26 11:13:00 +02001274 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
Johannes Berg4b14c962009-07-10 16:56:59 +02001275 /* TODO: simulate real signal strength (and optional packet loss) */
Johannes Berg1d5e9f82017-04-13 10:31:16 +02001276 rx_status.signal = -50;
1277 if (info->control.vif)
1278 rx_status.signal += info->control.vif->bss_conf.txpower;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001279
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001280 if (data->ps != PS_DISABLED)
1281 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1282
Johannes Berg90e30122009-06-18 14:51:12 +02001283 /* release the skb's source info */
1284 skb_orphan(skb);
John W. Linvillec0acf382009-06-30 16:55:52 -04001285 skb_dst_drop(skb);
Johannes Berg90e30122009-06-18 14:51:12 +02001286 skb->mark = 0;
1287 secpath_reset(skb);
1288 nf_reset(skb);
1289
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001290 /*
1291 * Get absolute mactime here so all HWs RX at the "same time", and
1292 * absolute TX time for beacon mactime so the timestamp matches.
1293 * Giving beacons a different mactime than non-beacons looks messy, but
1294 * it helps the Toffset be exact and a ~10us mactime discrepancy
1295 * probably doesn't really matter.
1296 */
1297 if (ieee80211_is_beacon(hdr->frame_control) ||
1298 ieee80211_is_probe_resp(hdr->frame_control))
1299 now = data->abs_bcn_ts;
1300 else
1301 now = mac80211_hwsim_get_tsf_raw();
1302
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001303 /* Copy skb to all enabled radios that are on the current frequency */
Johannes Berg0e057d732008-09-12 00:39:22 +02001304 spin_lock(&hwsim_radio_lock);
1305 list_for_each_entry(data2, &hwsim_radios, list) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001306 struct sk_buff *nskb;
Johannes Berge8261172012-07-27 19:48:26 +02001307 struct tx_iter_data tx_iter_data = {
1308 .receive = false,
1309 .channel = chan,
1310 };
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001311
Johannes Berg0e057d732008-09-12 00:39:22 +02001312 if (data == data2)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001313 continue;
Johannes Berg0e057d732008-09-12 00:39:22 +02001314
Johannes Berge8261172012-07-27 19:48:26 +02001315 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1316 !hwsim_ps_rx_ok(data2, skb))
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001317 continue;
1318
Johannes Berge8261172012-07-27 19:48:26 +02001319 if (!(data->group & data2->group))
1320 continue;
1321
Martin Willi100cb9f2016-05-09 18:33:59 +02001322 if (data->netgroup != data2->netgroup)
1323 continue;
1324
Johannes Berge8261172012-07-27 19:48:26 +02001325 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1326 !hwsim_chans_compat(chan, data2->channel)) {
1327 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001328 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1329 mac80211_hwsim_tx_iter, &tx_iter_data);
Johannes Berge8261172012-07-27 19:48:26 +02001330 if (!tx_iter_data.receive)
1331 continue;
1332 }
1333
Johannes Berg90b9e4462012-11-16 10:09:08 +01001334 /*
1335 * reserve some space for our vendor and the normal
1336 * radiotap header, since we're copying anyway
1337 */
Johannes Berga357d7f2012-12-10 11:57:42 +01001338 if (skb->len < PAGE_SIZE && paged_rx) {
1339 struct page *page = alloc_page(GFP_ATOMIC);
1340
1341 if (!page)
1342 continue;
1343
1344 nskb = dev_alloc_skb(128);
1345 if (!nskb) {
1346 __free_page(page);
1347 continue;
1348 }
1349
1350 memcpy(page_address(page), skb->data, skb->len);
1351 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1352 } else {
1353 nskb = skb_copy(skb, GFP_ATOMIC);
1354 if (!nskb)
1355 continue;
1356 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001357
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001358 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
Johannes Berg0e057d732008-09-12 00:39:22 +02001359 ack = true;
Javier Cardonaf483ad22012-03-31 11:31:30 -07001360
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001361 rx_status.mactime = now + data2->tsf_offset;
Johannes Berg90b9e4462012-11-16 10:09:08 +01001362
Johannes Bergf1d58c22009-06-17 13:13:00 +02001363 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
Johannes Berg1f7bba72014-11-06 22:56:36 +01001364
1365 mac80211_hwsim_add_vendor_rtap(nskb);
1366
Ben Greear2155c3f2014-10-22 12:22:58 -07001367 data2->rx_pkts++;
1368 data2->rx_bytes += nskb->len;
Johannes Bergf1d58c22009-06-17 13:13:00 +02001369 ieee80211_rx_irqsafe(data2->hw, nskb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001370 }
Johannes Berg0e057d732008-09-12 00:39:22 +02001371 spin_unlock(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001372
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001373 return ack;
1374}
1375
Thomas Huehn36323f82012-07-23 21:33:42 +02001376static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1377 struct ieee80211_tx_control *control,
1378 struct sk_buff *skb)
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001379{
Johannes Berge8261172012-07-27 19:48:26 +02001380 struct mac80211_hwsim_data *data = hw->priv;
1381 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
Jouni Malinen732b5392015-11-26 20:50:12 +02001382 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Berge8261172012-07-27 19:48:26 +02001383 struct ieee80211_chanctx_conf *chanctx_conf;
1384 struct ieee80211_channel *channel;
Johannes Berg0e057d732008-09-12 00:39:22 +02001385 bool ack;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001386 u32 _portid;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001387
Johannes Berge8261172012-07-27 19:48:26 +02001388 if (WARN_ON(skb->len < 10)) {
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001389 /* Should not happen; just a sanity check for addr1 use */
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001390 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001391 return;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001392 }
1393
Luciano Coelho361c3e02014-02-10 15:23:03 +02001394 if (!data->use_chanctx) {
Johannes Berge8261172012-07-27 19:48:26 +02001395 channel = data->channel;
1396 } else if (txi->hw_queue == 4) {
1397 channel = data->tmp_chan;
1398 } else {
1399 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1400 if (chanctx_conf)
Johannes Berg4bf88532012-11-09 11:39:59 +01001401 channel = chanctx_conf->def.chan;
Johannes Berge8261172012-07-27 19:48:26 +02001402 else
1403 channel = NULL;
1404 }
1405
1406 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001407 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001408 return;
1409 }
1410
1411 if (data->idle && !data->tmp_chan) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001412 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001413 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001414 return;
1415 }
1416
1417 if (txi->control.vif)
1418 hwsim_check_magic(txi->control.vif);
1419 if (control->sta)
1420 hwsim_check_sta_magic(control->sta);
1421
Johannes Berg30686bf2015-06-02 21:39:54 +02001422 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001423 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1424 txi->control.rates,
1425 ARRAY_SIZE(txi->control.rates));
Johannes Berge8261172012-07-27 19:48:26 +02001426
Jouni Malinen732b5392015-11-26 20:50:12 +02001427 if (skb->len >= 24 + 8 &&
1428 ieee80211_is_probe_resp(hdr->frame_control)) {
1429 /* fake header transmission time */
1430 struct ieee80211_mgmt *mgmt;
1431 struct ieee80211_rate *txrate;
1432 u64 ts;
1433
1434 mgmt = (struct ieee80211_mgmt *)skb->data;
1435 txrate = ieee80211_get_tx_rate(hw, txi);
1436 ts = mac80211_hwsim_get_tsf_raw();
1437 mgmt->u.probe_resp.timestamp =
1438 cpu_to_le64(ts + data->tsf_offset +
1439 24 * 8 * 10 / txrate->bitrate);
1440 }
1441
Johannes Berge8261172012-07-27 19:48:26 +02001442 mac80211_hwsim_monitor_rx(hw, skb, channel);
1443
Javier Lopez78825132011-06-01 11:26:13 +02001444 /* wmediumd mode check */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001445 _portid = READ_ONCE(data->wmediumd);
Javier Lopez78825132011-06-01 11:26:13 +02001446
Eric W. Biederman15e47302012-09-07 20:12:54 +00001447 if (_portid)
1448 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
Javier Lopez78825132011-06-01 11:26:13 +02001449
1450 /* NO wmediumd detected, perfect medium simulation */
Ben Greear2155c3f2014-10-22 12:22:58 -07001451 data->tx_pkts++;
1452 data->tx_bytes += skb->len;
Johannes Berge8261172012-07-27 19:48:26 +02001453 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
Javier Lopez78825132011-06-01 11:26:13 +02001454
Johannes Bergf06b7ab2016-01-13 14:21:01 +01001455 if (ack && skb->len >= 16)
Johannes Berge8261172012-07-27 19:48:26 +02001456 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001457
Johannes Berge6a98542008-10-21 12:40:02 +02001458 ieee80211_tx_info_clear_status(txi);
Javier Cardona2a4ffa42012-05-01 03:01:28 -07001459
1460 /* frame was transmitted at most favorable rate at first attempt */
1461 txi->control.rates[0].count = 1;
1462 txi->control.rates[1].idx = -1;
1463
Johannes Berge6a98542008-10-21 12:40:02 +02001464 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1465 txi->flags |= IEEE80211_TX_STAT_ACK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001466 ieee80211_tx_status_irqsafe(hw, skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001467}
1468
1469
1470static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1471{
1472 struct mac80211_hwsim_data *data = hw->priv;
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001473 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001474 data->started = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001475 return 0;
1476}
1477
1478
1479static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1480{
1481 struct mac80211_hwsim_data *data = hw->priv;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001482 data->started = false;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001483 tasklet_hrtimer_cancel(&data->beacon_timer);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001484 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001485}
1486
1487
1488static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001489 struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001490{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001491 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1492 __func__, ieee80211_vif_type_p2p(vif),
1493 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001494 hwsim_set_magic(vif);
Johannes Berge8261172012-07-27 19:48:26 +02001495
1496 vif->cab_queue = 0;
1497 vif->hw_queue[IEEE80211_AC_VO] = 0;
1498 vif->hw_queue[IEEE80211_AC_VI] = 1;
1499 vif->hw_queue[IEEE80211_AC_BE] = 2;
1500 vif->hw_queue[IEEE80211_AC_BK] = 3;
1501
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001502 return 0;
1503}
1504
1505
Johannes Bergc35d0272010-08-27 12:35:59 +02001506static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1507 struct ieee80211_vif *vif,
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001508 enum nl80211_iftype newtype,
1509 bool newp2p)
Johannes Bergc35d0272010-08-27 12:35:59 +02001510{
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001511 newtype = ieee80211_iftype_p2p(newtype, newp2p);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001512 wiphy_dbg(hw->wiphy,
1513 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1514 __func__, ieee80211_vif_type_p2p(vif),
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001515 newtype, vif->addr);
Johannes Bergc35d0272010-08-27 12:35:59 +02001516 hwsim_check_magic(vif);
1517
Johannes Berg3eb92f62013-03-18 22:13:18 +01001518 /*
1519 * interface may change from non-AP to AP in
1520 * which case this needs to be set up again
1521 */
1522 vif->cab_queue = 0;
1523
Johannes Bergc35d0272010-08-27 12:35:59 +02001524 return 0;
1525}
1526
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001527static void mac80211_hwsim_remove_interface(
Johannes Berg1ed32e42009-12-23 13:15:45 +01001528 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001529{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001530 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1531 __func__, ieee80211_vif_type_p2p(vif),
1532 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001533 hwsim_check_magic(vif);
1534 hwsim_clear_magic(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001535}
1536
Johannes Berge8261172012-07-27 19:48:26 +02001537static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1538 struct sk_buff *skb,
1539 struct ieee80211_channel *chan)
1540{
Martin Willif21e4d82016-05-14 10:34:44 +02001541 struct mac80211_hwsim_data *data = hw->priv;
Mark Rutland6aa7de02017-10-23 14:07:29 -07001542 u32 _pid = READ_ONCE(data->wmediumd);
Johannes Berge8261172012-07-27 19:48:26 +02001543
Johannes Berg30686bf2015-06-02 21:39:54 +02001544 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
Karl Beldan1eb32172013-04-19 14:44:52 +02001545 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1546 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1547 txi->control.rates,
1548 ARRAY_SIZE(txi->control.rates));
1549 }
1550
Johannes Berge8261172012-07-27 19:48:26 +02001551 mac80211_hwsim_monitor_rx(hw, skb, chan);
1552
1553 if (_pid)
1554 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1555
1556 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1557 dev_kfree_skb(skb);
1558}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001559
1560static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1561 struct ieee80211_vif *vif)
1562{
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001563 struct mac80211_hwsim_data *data = arg;
1564 struct ieee80211_hw *hw = data->hw;
1565 struct ieee80211_tx_info *info;
1566 struct ieee80211_rate *txrate;
1567 struct ieee80211_mgmt *mgmt;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001568 struct sk_buff *skb;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001569
Johannes Berg8aa21e62008-09-11 02:16:36 +02001570 hwsim_check_magic(vif);
1571
Andrey Yurovsky55b39612008-10-31 23:23:35 -07001572 if (vif->type != NL80211_IFTYPE_AP &&
Johannes Berg2b2d7792010-08-17 12:08:07 +02001573 vif->type != NL80211_IFTYPE_MESH_POINT &&
1574 vif->type != NL80211_IFTYPE_ADHOC)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001575 return;
1576
1577 skb = ieee80211_beacon_get(hw, vif);
1578 if (skb == NULL)
1579 return;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001580 info = IEEE80211_SKB_CB(skb);
Johannes Berg30686bf2015-06-02 21:39:54 +02001581 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001582 ieee80211_get_tx_rates(vif, NULL, skb,
1583 info->control.rates,
1584 ARRAY_SIZE(info->control.rates));
1585
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001586 txrate = ieee80211_get_tx_rate(hw, info);
1587
1588 mgmt = (struct ieee80211_mgmt *) skb->data;
1589 /* fake header transmission time */
1590 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1591 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1592 data->tsf_offset +
1593 24 * 8 * 10 / txrate->bitrate);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001594
Johannes Berge8261172012-07-27 19:48:26 +02001595 mac80211_hwsim_tx_frame(hw, skb,
Johannes Berg4bf88532012-11-09 11:39:59 +01001596 rcu_dereference(vif->chanctx_conf)->def.chan);
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02001597
1598 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1599 ieee80211_csa_finish(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001600}
1601
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001602static enum hrtimer_restart
1603mac80211_hwsim_beacon(struct hrtimer *timer)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001604{
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001605 struct mac80211_hwsim_data *data =
1606 container_of(timer, struct mac80211_hwsim_data,
1607 beacon_timer.timer);
1608 struct ieee80211_hw *hw = data->hw;
1609 u64 bcn_int = data->beacon_int;
1610 ktime_t next_bcn;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001611
Johannes Berg4c4c6712009-06-01 14:29:52 +02001612 if (!data->started)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001613 goto out;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001614
Jouni Malinenf248f102008-06-13 19:44:47 +03001615 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001616 hw, IEEE80211_IFACE_ITER_NORMAL,
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001617 mac80211_hwsim_beacon_tx, data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001618
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001619 /* beacon at new TBTT + beacon interval */
1620 if (data->bcn_delta) {
1621 bcn_int -= data->bcn_delta;
1622 data->bcn_delta = 0;
1623 }
1624
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001625 next_bcn = ktime_add(hrtimer_get_expires(timer),
1626 ns_to_ktime(bcn_int * 1000));
1627 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1628out:
1629 return HRTIMER_NORESTART;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001630}
1631
Karl Beldan675a0b02013-03-25 16:26:57 +01001632static const char * const hwsim_chanwidths[] = {
1633 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1634 [NL80211_CHAN_WIDTH_20] = "ht20",
1635 [NL80211_CHAN_WIDTH_40] = "ht40",
1636 [NL80211_CHAN_WIDTH_80] = "vht80",
1637 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1638 [NL80211_CHAN_WIDTH_160] = "vht160",
Johannes Berg0aaffa92010-05-05 15:28:27 +02001639};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001640
Johannes Berge8975582008-10-09 12:18:51 +02001641static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001642{
1643 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001644 struct ieee80211_conf *conf = &hw->conf;
Johannes Berg0f782312009-12-01 13:37:02 +01001645 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1646 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1647 [IEEE80211_SMPS_OFF] = "off",
1648 [IEEE80211_SMPS_STATIC] = "static",
1649 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1650 };
Johannes Berg7f813ce2013-11-06 10:37:34 +01001651 int idx;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001652
Karl Beldan675a0b02013-03-25 16:26:57 +01001653 if (conf->chandef.chan)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001654 wiphy_dbg(hw->wiphy,
1655 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1656 __func__,
1657 conf->chandef.chan->center_freq,
1658 conf->chandef.center_freq1,
1659 conf->chandef.center_freq2,
1660 hwsim_chanwidths[conf->chandef.width],
1661 !!(conf->flags & IEEE80211_CONF_IDLE),
1662 !!(conf->flags & IEEE80211_CONF_PS),
1663 smps_modes[conf->smps_mode]);
Karl Beldan675a0b02013-03-25 16:26:57 +01001664 else
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001665 wiphy_dbg(hw->wiphy,
1666 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1667 __func__,
1668 !!(conf->flags & IEEE80211_CONF_IDLE),
1669 !!(conf->flags & IEEE80211_CONF_PS),
1670 smps_modes[conf->smps_mode]);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001671
Jouni Malinen70541832009-11-01 11:30:48 +02001672 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1673
Johannes Berg7f813ce2013-11-06 10:37:34 +01001674 WARN_ON(conf->chandef.chan && data->use_chanctx);
Johannes Berge8261172012-07-27 19:48:26 +02001675
Johannes Berg7f813ce2013-11-06 10:37:34 +01001676 mutex_lock(&data->mutex);
1677 if (data->scanning && conf->chandef.chan) {
1678 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1679 if (data->survey_data[idx].channel == data->channel) {
1680 data->survey_data[idx].start =
1681 data->survey_data[idx].next_start;
1682 data->survey_data[idx].end = jiffies;
1683 break;
1684 }
1685 }
Johannes Berge8261172012-07-27 19:48:26 +02001686
Johannes Berg7f813ce2013-11-06 10:37:34 +01001687 data->channel = conf->chandef.chan;
1688
1689 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1690 if (data->survey_data[idx].channel &&
1691 data->survey_data[idx].channel != data->channel)
1692 continue;
1693 data->survey_data[idx].channel = data->channel;
1694 data->survey_data[idx].next_start = jiffies;
1695 break;
1696 }
1697 } else {
1698 data->channel = conf->chandef.chan;
1699 }
1700 mutex_unlock(&data->mutex);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001701
Johannes Berg4c4c6712009-06-01 14:29:52 +02001702 if (!data->started || !data->beacon_int)
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001703 tasklet_hrtimer_cancel(&data->beacon_timer);
1704 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001705 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1706 u32 bcn_int = data->beacon_int;
1707 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1708
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001709 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001710 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001711 HRTIMER_MODE_REL);
1712 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001713
1714 return 0;
1715}
1716
1717
1718static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1719 unsigned int changed_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001720 unsigned int *total_flags,u64 multicast)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001721{
1722 struct mac80211_hwsim_data *data = hw->priv;
1723
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001724 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001725
1726 data->rx_filter = 0;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001727 if (*total_flags & FIF_ALLMULTI)
1728 data->rx_filter |= FIF_ALLMULTI;
1729
1730 *total_flags = data->rx_filter;
1731}
1732
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001733static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1734 struct ieee80211_vif *vif)
1735{
1736 unsigned int *count = data;
1737 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1738
1739 if (vp->bcn_en)
1740 (*count)++;
1741}
1742
Johannes Berg8aa21e62008-09-11 02:16:36 +02001743static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1744 struct ieee80211_vif *vif,
1745 struct ieee80211_bss_conf *info,
1746 u32 changed)
1747{
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001748 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001749 struct mac80211_hwsim_data *data = hw->priv;
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001750
Johannes Berg8aa21e62008-09-11 02:16:36 +02001751 hwsim_check_magic(vif);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001752
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001753 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1754 __func__, changed, vif->addr);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001755
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001756 if (changed & BSS_CHANGED_BSSID) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001757 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1758 __func__, info->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001759 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1760 }
1761
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001762 if (changed & BSS_CHANGED_ASSOC) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001763 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1764 info->assoc, info->aid);
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001765 vp->assoc = info->assoc;
1766 vp->aid = info->aid;
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001767 }
1768
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001769 if (changed & BSS_CHANGED_BEACON_ENABLED) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001770 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1771 info->enable_beacon, info->beacon_int);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001772 vp->bcn_en = info->enable_beacon;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001773 if (data->started &&
1774 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1775 info->enable_beacon) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001776 u64 tsf, until_tbtt;
1777 u32 bcn_int;
Johannes Berg56067622015-03-03 22:09:05 +01001778 data->beacon_int = info->beacon_int * 1024;
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001779 tsf = mac80211_hwsim_get_tsf(hw, vif);
1780 bcn_int = data->beacon_int;
1781 until_tbtt = bcn_int - do_div(tsf, bcn_int);
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001782 tasklet_hrtimer_start(&data->beacon_timer,
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001783 ns_to_ktime(until_tbtt * 1000),
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001784 HRTIMER_MODE_REL);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001785 } else if (!info->enable_beacon) {
1786 unsigned int count = 0;
Jouni Malinencdb1b802013-11-06 12:06:30 +02001787 ieee80211_iterate_active_interfaces_atomic(
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001788 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1789 mac80211_hwsim_bcn_en_iter, &count);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001790 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1791 count);
Johannes Berg56067622015-03-03 22:09:05 +01001792 if (count == 0) {
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001793 tasklet_hrtimer_cancel(&data->beacon_timer);
Johannes Berg56067622015-03-03 22:09:05 +01001794 data->beacon_int = 0;
1795 }
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001796 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001797 }
1798
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001799 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001800 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1801 info->use_cts_prot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001802 }
1803
1804 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001805 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1806 info->use_short_preamble);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001807 }
1808
1809 if (changed & BSS_CHANGED_ERP_SLOT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001810 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001811 }
1812
1813 if (changed & BSS_CHANGED_HT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001814 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1815 info->ht_operation_mode);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001816 }
1817
1818 if (changed & BSS_CHANGED_BASIC_RATES) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001819 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1820 (unsigned long long) info->basic_rates);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001821 }
Johannes Bergcbc668a2012-10-24 11:54:31 +02001822
1823 if (changed & BSS_CHANGED_TXPOWER)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001824 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
Johannes Berg8aa21e62008-09-11 02:16:36 +02001825}
1826
Johannes Berg1d669cb2010-02-19 19:06:55 +01001827static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1828 struct ieee80211_vif *vif,
1829 struct ieee80211_sta *sta)
1830{
1831 hwsim_check_magic(vif);
1832 hwsim_set_sta_magic(sta);
1833
1834 return 0;
1835}
1836
1837static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1838 struct ieee80211_vif *vif,
1839 struct ieee80211_sta *sta)
1840{
1841 hwsim_check_magic(vif);
1842 hwsim_clear_sta_magic(sta);
1843
1844 return 0;
1845}
1846
Johannes Berg8aa21e62008-09-11 02:16:36 +02001847static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1848 struct ieee80211_vif *vif,
Johannes Berg17741cd2008-09-11 00:02:02 +02001849 enum sta_notify_cmd cmd,
1850 struct ieee80211_sta *sta)
Johannes Berg8aa21e62008-09-11 02:16:36 +02001851{
1852 hwsim_check_magic(vif);
Johannes Berg1d669cb2010-02-19 19:06:55 +01001853
Johannes Berg81c06522008-09-11 02:17:01 +02001854 switch (cmd) {
Christian Lamparter89fad572008-12-09 16:28:06 +01001855 case STA_NOTIFY_SLEEP:
1856 case STA_NOTIFY_AWAKE:
1857 /* TODO: make good use of these flags */
1858 break;
Johannes Berg1d669cb2010-02-19 19:06:55 +01001859 default:
1860 WARN(1, "Invalid sta notify: %d\n", cmd);
1861 break;
Johannes Berg81c06522008-09-11 02:17:01 +02001862 }
1863}
1864
1865static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1866 struct ieee80211_sta *sta,
1867 bool set)
1868{
1869 hwsim_check_sta_magic(sta);
1870 return 0;
Johannes Berg8aa21e62008-09-11 02:16:36 +02001871}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001872
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001873static int mac80211_hwsim_conf_tx(
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001874 struct ieee80211_hw *hw,
1875 struct ieee80211_vif *vif, u16 queue,
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001876 const struct ieee80211_tx_queue_params *params)
1877{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001878 wiphy_dbg(hw->wiphy,
1879 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1880 __func__, queue,
1881 params->txop, params->cw_min,
1882 params->cw_max, params->aifs);
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001883 return 0;
1884}
1885
Johannes Berg7f813ce2013-11-06 10:37:34 +01001886static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1887 struct survey_info *survey)
Holger Schurig12897232010-04-19 10:23:57 +02001888{
Johannes Berg7f813ce2013-11-06 10:37:34 +01001889 struct mac80211_hwsim_data *hwsim = hw->priv;
Holger Schurig12897232010-04-19 10:23:57 +02001890
Johannes Berg7f813ce2013-11-06 10:37:34 +01001891 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
Holger Schurig12897232010-04-19 10:23:57 +02001892 return -ENOENT;
1893
Johannes Berg7f813ce2013-11-06 10:37:34 +01001894 mutex_lock(&hwsim->mutex);
1895 survey->channel = hwsim->survey_data[idx].channel;
1896 if (!survey->channel) {
1897 mutex_unlock(&hwsim->mutex);
1898 return -ENOENT;
1899 }
Holger Schurig12897232010-04-19 10:23:57 +02001900
1901 /*
Johannes Berg7f813ce2013-11-06 10:37:34 +01001902 * Magically conjured dummy values --- this is only ok for simulated hardware.
Holger Schurig12897232010-04-19 10:23:57 +02001903 *
Johannes Berg7f813ce2013-11-06 10:37:34 +01001904 * A real driver which cannot determine real values noise MUST NOT
1905 * report any, especially not a magically conjured ones :-)
Holger Schurig12897232010-04-19 10:23:57 +02001906 */
Johannes Berg7f813ce2013-11-06 10:37:34 +01001907 survey->filled = SURVEY_INFO_NOISE_DBM |
1908 SURVEY_INFO_TIME |
1909 SURVEY_INFO_TIME_BUSY;
Holger Schurig12897232010-04-19 10:23:57 +02001910 survey->noise = -92;
Johannes Berg7f813ce2013-11-06 10:37:34 +01001911 survey->time =
1912 jiffies_to_msecs(hwsim->survey_data[idx].end -
1913 hwsim->survey_data[idx].start);
1914 /* report 12.5% of channel time is used */
1915 survey->time_busy = survey->time/8;
1916 mutex_unlock(&hwsim->mutex);
Holger Schurig12897232010-04-19 10:23:57 +02001917
1918 return 0;
1919}
1920
Johannes Bergaff89a92009-07-01 21:26:51 +02001921#ifdef CONFIG_NL80211_TESTMODE
1922/*
1923 * This section contains example code for using netlink
1924 * attributes with the testmode command in nl80211.
1925 */
1926
1927/* These enums need to be kept in sync with userspace */
1928enum hwsim_testmode_attr {
1929 __HWSIM_TM_ATTR_INVALID = 0,
1930 HWSIM_TM_ATTR_CMD = 1,
1931 HWSIM_TM_ATTR_PS = 2,
1932
1933 /* keep last */
1934 __HWSIM_TM_ATTR_AFTER_LAST,
1935 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1936};
1937
1938enum hwsim_testmode_cmd {
1939 HWSIM_TM_CMD_SET_PS = 0,
1940 HWSIM_TM_CMD_GET_PS = 1,
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001941 HWSIM_TM_CMD_STOP_QUEUES = 2,
1942 HWSIM_TM_CMD_WAKE_QUEUES = 3,
Johannes Bergaff89a92009-07-01 21:26:51 +02001943};
1944
1945static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1946 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1947 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1948};
1949
Johannes Berg5bc38192009-07-07 11:00:55 +02001950static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
David Spinadel52981cd2013-07-31 18:06:22 +03001951 struct ieee80211_vif *vif,
Johannes Berg5bc38192009-07-07 11:00:55 +02001952 void *data, int len)
Johannes Bergaff89a92009-07-01 21:26:51 +02001953{
1954 struct mac80211_hwsim_data *hwsim = hw->priv;
1955 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1956 struct sk_buff *skb;
1957 int err, ps;
1958
1959 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
Johannes Bergfceb6432017-04-12 14:34:07 +02001960 hwsim_testmode_policy, NULL);
Johannes Bergaff89a92009-07-01 21:26:51 +02001961 if (err)
1962 return err;
1963
1964 if (!tb[HWSIM_TM_ATTR_CMD])
1965 return -EINVAL;
1966
1967 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1968 case HWSIM_TM_CMD_SET_PS:
1969 if (!tb[HWSIM_TM_ATTR_PS])
1970 return -EINVAL;
1971 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1972 return hwsim_fops_ps_write(hwsim, ps);
1973 case HWSIM_TM_CMD_GET_PS:
1974 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1975 nla_total_size(sizeof(u32)));
1976 if (!skb)
1977 return -ENOMEM;
David S. Miller633c9382012-04-01 21:03:30 -04001978 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1979 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02001980 return cfg80211_testmode_reply(skb);
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001981 case HWSIM_TM_CMD_STOP_QUEUES:
1982 ieee80211_stop_queues(hw);
1983 return 0;
1984 case HWSIM_TM_CMD_WAKE_QUEUES:
1985 ieee80211_wake_queues(hw);
1986 return 0;
Johannes Bergaff89a92009-07-01 21:26:51 +02001987 default:
1988 return -EOPNOTSUPP;
1989 }
1990
1991 nla_put_failure:
1992 kfree_skb(skb);
1993 return -ENOBUFS;
1994}
1995#endif
1996
Johannes Berg8b73d132009-12-01 14:24:24 +01001997static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1998 struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02001999 struct ieee80211_ampdu_params *params)
Johannes Berg8b73d132009-12-01 14:24:24 +01002000{
Sara Sharon50ea05e2015-12-30 16:06:04 +02002001 struct ieee80211_sta *sta = params->sta;
2002 enum ieee80211_ampdu_mlme_action action = params->action;
2003 u16 tid = params->tid;
2004
Johannes Berg8b73d132009-12-01 14:24:24 +01002005 switch (action) {
2006 case IEEE80211_AMPDU_TX_START:
2007 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2008 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02002009 case IEEE80211_AMPDU_TX_STOP_CONT:
2010 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2011 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Johannes Berg8b73d132009-12-01 14:24:24 +01002012 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2013 break;
2014 case IEEE80211_AMPDU_TX_OPERATIONAL:
2015 break;
2016 case IEEE80211_AMPDU_RX_START:
2017 case IEEE80211_AMPDU_RX_STOP:
2018 break;
2019 default:
2020 return -EOPNOTSUPP;
2021 }
2022
2023 return 0;
2024}
2025
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02002026static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2027 struct ieee80211_vif *vif,
2028 u32 queues, bool drop)
Johannes Berga80f7c02009-12-23 13:15:32 +01002029{
Javier Lopez78825132011-06-01 11:26:13 +02002030 /* Not implemented, queues only on kernel side */
Johannes Berga80f7c02009-12-23 13:15:32 +01002031}
2032
Johannes Berge8261172012-07-27 19:48:26 +02002033static void hw_scan_work(struct work_struct *work)
Johannes Berg69068032010-02-03 10:47:55 +01002034{
Johannes Berge8261172012-07-27 19:48:26 +02002035 struct mac80211_hwsim_data *hwsim =
2036 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2037 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2038 int dwell, i;
Johannes Berg69068032010-02-03 10:47:55 +01002039
Johannes Berge8261172012-07-27 19:48:26 +02002040 mutex_lock(&hwsim->mutex);
2041 if (hwsim->scan_chan_idx >= req->n_channels) {
Avraham Stern7947d3e2016-07-05 15:23:12 +03002042 struct cfg80211_scan_info info = {
2043 .aborted = false,
2044 };
2045
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002046 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
Avraham Stern7947d3e2016-07-05 15:23:12 +03002047 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002048 hwsim->hw_scan_request = NULL;
2049 hwsim->hw_scan_vif = NULL;
2050 hwsim->tmp_chan = NULL;
2051 mutex_unlock(&hwsim->mutex);
2052 return;
2053 }
2054
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002055 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2056 req->channels[hwsim->scan_chan_idx]->center_freq);
Johannes Berge8261172012-07-27 19:48:26 +02002057
2058 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
Johannes Berga8e828d2015-11-26 17:59:27 +01002059 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2060 IEEE80211_CHAN_RADAR) ||
Johannes Berge8261172012-07-27 19:48:26 +02002061 !req->n_ssids) {
2062 dwell = 120;
2063 } else {
2064 dwell = 30;
2065 /* send probes */
2066 for (i = 0; i < req->n_ssids; i++) {
2067 struct sk_buff *probe;
Jouni Malinen12880d12016-02-26 22:12:49 +02002068 struct ieee80211_mgmt *mgmt;
Johannes Berge8261172012-07-27 19:48:26 +02002069
2070 probe = ieee80211_probereq_get(hwsim->hw,
Johannes Berg339467b2014-06-12 22:41:00 +02002071 hwsim->scan_addr,
Johannes Berge8261172012-07-27 19:48:26 +02002072 req->ssids[i].ssid,
2073 req->ssids[i].ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002074 req->ie_len);
Johannes Berge8261172012-07-27 19:48:26 +02002075 if (!probe)
2076 continue;
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002077
Jouni Malinen12880d12016-02-26 22:12:49 +02002078 mgmt = (struct ieee80211_mgmt *) probe->data;
2079 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2080 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2081
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002082 if (req->ie_len)
Johannes Berg59ae1d12017-06-16 14:29:20 +02002083 skb_put_data(probe, req->ie, req->ie_len);
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002084
Johannes Berge8261172012-07-27 19:48:26 +02002085 local_bh_disable();
2086 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2087 hwsim->tmp_chan);
2088 local_bh_enable();
2089 }
2090 }
2091 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2092 msecs_to_jiffies(dwell));
Johannes Berg7f813ce2013-11-06 10:37:34 +01002093 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2094 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2095 hwsim->survey_data[hwsim->scan_chan_idx].end =
2096 jiffies + msecs_to_jiffies(dwell);
Johannes Berge8261172012-07-27 19:48:26 +02002097 hwsim->scan_chan_idx++;
2098 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002099}
2100
2101static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +02002102 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02002103 struct ieee80211_scan_request *hw_req)
Johannes Berg69068032010-02-03 10:47:55 +01002104{
Johannes Berge8261172012-07-27 19:48:26 +02002105 struct mac80211_hwsim_data *hwsim = hw->priv;
David Spinadelc56ef672014-02-05 15:21:13 +02002106 struct cfg80211_scan_request *req = &hw_req->req;
Johannes Berg69068032010-02-03 10:47:55 +01002107
Johannes Berge8261172012-07-27 19:48:26 +02002108 mutex_lock(&hwsim->mutex);
2109 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2110 mutex_unlock(&hwsim->mutex);
2111 return -EBUSY;
2112 }
2113 hwsim->hw_scan_request = req;
2114 hwsim->hw_scan_vif = vif;
2115 hwsim->scan_chan_idx = 0;
Johannes Berg339467b2014-06-12 22:41:00 +02002116 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2117 get_random_mask_addr(hwsim->scan_addr,
2118 hw_req->req.mac_addr,
2119 hw_req->req.mac_addr_mask);
2120 else
2121 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
Johannes Berg7f813ce2013-11-06 10:37:34 +01002122 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Johannes Berge8261172012-07-27 19:48:26 +02002123 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002124
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002125 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
Johannes Berg69068032010-02-03 10:47:55 +01002126
Johannes Berge8261172012-07-27 19:48:26 +02002127 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
Johannes Berg69068032010-02-03 10:47:55 +01002128
2129 return 0;
2130}
2131
Johannes Berge8261172012-07-27 19:48:26 +02002132static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2133 struct ieee80211_vif *vif)
2134{
2135 struct mac80211_hwsim_data *hwsim = hw->priv;
Avraham Stern7947d3e2016-07-05 15:23:12 +03002136 struct cfg80211_scan_info info = {
2137 .aborted = true,
2138 };
Johannes Berge8261172012-07-27 19:48:26 +02002139
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002140 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
Johannes Berge8261172012-07-27 19:48:26 +02002141
2142 cancel_delayed_work_sync(&hwsim->hw_scan);
2143
2144 mutex_lock(&hwsim->mutex);
Avraham Stern7947d3e2016-07-05 15:23:12 +03002145 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002146 hwsim->tmp_chan = NULL;
2147 hwsim->hw_scan_request = NULL;
2148 hwsim->hw_scan_vif = NULL;
2149 mutex_unlock(&hwsim->mutex);
2150}
2151
Johannes Berga344d672014-06-12 22:24:31 +02002152static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2153 struct ieee80211_vif *vif,
2154 const u8 *mac_addr)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002155{
2156 struct mac80211_hwsim_data *hwsim = hw->priv;
2157
2158 mutex_lock(&hwsim->mutex);
2159
2160 if (hwsim->scanning) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002161 pr_debug("two hwsim sw_scans detected!\n");
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002162 goto out;
2163 }
2164
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002165 pr_debug("hwsim sw_scan request, prepping stuff\n");
Johannes Berg339467b2014-06-12 22:41:00 +02002166
2167 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002168 hwsim->scanning = true;
Johannes Berg7f813ce2013-11-06 10:37:34 +01002169 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002170
2171out:
2172 mutex_unlock(&hwsim->mutex);
2173}
2174
Johannes Berga344d672014-06-12 22:24:31 +02002175static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2176 struct ieee80211_vif *vif)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002177{
2178 struct mac80211_hwsim_data *hwsim = hw->priv;
2179
2180 mutex_lock(&hwsim->mutex);
2181
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002182 pr_debug("hwsim sw_scan_complete\n");
Johannes Berg23ff98f2010-05-03 09:21:14 +02002183 hwsim->scanning = false;
Joe Perches93803b32015-03-02 19:54:49 -08002184 eth_zero_addr(hwsim->scan_addr);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002185
2186 mutex_unlock(&hwsim->mutex);
2187}
2188
Johannes Berg661ef472015-11-24 18:29:45 +01002189static void hw_roc_start(struct work_struct *work)
2190{
2191 struct mac80211_hwsim_data *hwsim =
2192 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2193
2194 mutex_lock(&hwsim->mutex);
2195
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002196 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
Johannes Berg661ef472015-11-24 18:29:45 +01002197 hwsim->tmp_chan = hwsim->roc_chan;
2198 ieee80211_ready_on_channel(hwsim->hw);
2199
2200 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2201 msecs_to_jiffies(hwsim->roc_duration));
2202
2203 mutex_unlock(&hwsim->mutex);
2204}
2205
Johannes Berge8261172012-07-27 19:48:26 +02002206static void hw_roc_done(struct work_struct *work)
2207{
2208 struct mac80211_hwsim_data *hwsim =
2209 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2210
2211 mutex_lock(&hwsim->mutex);
2212 ieee80211_remain_on_channel_expired(hwsim->hw);
2213 hwsim->tmp_chan = NULL;
2214 mutex_unlock(&hwsim->mutex);
2215
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002216 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
Johannes Berge8261172012-07-27 19:48:26 +02002217}
2218
2219static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
Eliad Peller49884562012-11-19 17:05:09 +02002220 struct ieee80211_vif *vif,
Johannes Berge8261172012-07-27 19:48:26 +02002221 struct ieee80211_channel *chan,
Ilan Peerd339d5c2013-02-12 09:34:13 +02002222 int duration,
2223 enum ieee80211_roc_type type)
Johannes Berge8261172012-07-27 19:48:26 +02002224{
2225 struct mac80211_hwsim_data *hwsim = hw->priv;
2226
2227 mutex_lock(&hwsim->mutex);
2228 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2229 mutex_unlock(&hwsim->mutex);
2230 return -EBUSY;
2231 }
2232
Johannes Berg661ef472015-11-24 18:29:45 +01002233 hwsim->roc_chan = chan;
2234 hwsim->roc_duration = duration;
Johannes Berge8261172012-07-27 19:48:26 +02002235 mutex_unlock(&hwsim->mutex);
2236
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002237 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2238 chan->center_freq, duration);
Johannes Berg661ef472015-11-24 18:29:45 +01002239 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
Johannes Berge8261172012-07-27 19:48:26 +02002240
Johannes Berge8261172012-07-27 19:48:26 +02002241 return 0;
2242}
2243
2244static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2245{
2246 struct mac80211_hwsim_data *hwsim = hw->priv;
2247
Johannes Berg661ef472015-11-24 18:29:45 +01002248 cancel_delayed_work_sync(&hwsim->roc_start);
Johannes Berge8261172012-07-27 19:48:26 +02002249 cancel_delayed_work_sync(&hwsim->roc_done);
2250
2251 mutex_lock(&hwsim->mutex);
2252 hwsim->tmp_chan = NULL;
2253 mutex_unlock(&hwsim->mutex);
2254
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002255 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
Johannes Berge8261172012-07-27 19:48:26 +02002256
2257 return 0;
2258}
2259
2260static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2261 struct ieee80211_chanctx_conf *ctx)
2262{
2263 hwsim_set_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002264 wiphy_dbg(hw->wiphy,
2265 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2266 ctx->def.chan->center_freq, ctx->def.width,
2267 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002268 return 0;
2269}
2270
2271static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2272 struct ieee80211_chanctx_conf *ctx)
2273{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002274 wiphy_dbg(hw->wiphy,
2275 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2276 ctx->def.chan->center_freq, ctx->def.width,
2277 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002278 hwsim_check_chanctx_magic(ctx);
2279 hwsim_clear_chanctx_magic(ctx);
2280}
2281
2282static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2283 struct ieee80211_chanctx_conf *ctx,
2284 u32 changed)
2285{
2286 hwsim_check_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002287 wiphy_dbg(hw->wiphy,
2288 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2289 ctx->def.chan->center_freq, ctx->def.width,
2290 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002291}
2292
2293static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2294 struct ieee80211_vif *vif,
2295 struct ieee80211_chanctx_conf *ctx)
2296{
2297 hwsim_check_magic(vif);
2298 hwsim_check_chanctx_magic(ctx);
2299
2300 return 0;
2301}
2302
2303static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2304 struct ieee80211_vif *vif,
2305 struct ieee80211_chanctx_conf *ctx)
2306{
2307 hwsim_check_magic(vif);
2308 hwsim_check_chanctx_magic(ctx);
2309}
2310
Ben Greear2155c3f2014-10-22 12:22:58 -07002311static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2312 "tx_pkts_nic",
2313 "tx_bytes_nic",
2314 "rx_pkts_nic",
2315 "rx_bytes_nic",
2316 "d_tx_dropped",
2317 "d_tx_failed",
2318 "d_ps_mode",
2319 "d_group",
Ben Greear2155c3f2014-10-22 12:22:58 -07002320};
2321
2322#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2323
2324static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2325 struct ieee80211_vif *vif,
2326 u32 sset, u8 *data)
2327{
2328 if (sset == ETH_SS_STATS)
2329 memcpy(data, *mac80211_hwsim_gstrings_stats,
2330 sizeof(mac80211_hwsim_gstrings_stats));
2331}
2332
2333static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2334 struct ieee80211_vif *vif, int sset)
2335{
2336 if (sset == ETH_SS_STATS)
2337 return MAC80211_HWSIM_SSTATS_LEN;
2338 return 0;
2339}
2340
2341static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2342 struct ieee80211_vif *vif,
2343 struct ethtool_stats *stats, u64 *data)
2344{
2345 struct mac80211_hwsim_data *ar = hw->priv;
2346 int i = 0;
2347
2348 data[i++] = ar->tx_pkts;
2349 data[i++] = ar->tx_bytes;
2350 data[i++] = ar->rx_pkts;
2351 data[i++] = ar->rx_bytes;
2352 data[i++] = ar->tx_dropped;
2353 data[i++] = ar->tx_failed;
2354 data[i++] = ar->ps;
2355 data[i++] = ar->group;
Ben Greear2155c3f2014-10-22 12:22:58 -07002356
2357 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2358}
2359
Johannes Berg246ad562016-10-10 12:09:52 +02002360#define HWSIM_COMMON_OPS \
2361 .tx = mac80211_hwsim_tx, \
2362 .start = mac80211_hwsim_start, \
2363 .stop = mac80211_hwsim_stop, \
2364 .add_interface = mac80211_hwsim_add_interface, \
2365 .change_interface = mac80211_hwsim_change_interface, \
2366 .remove_interface = mac80211_hwsim_remove_interface, \
2367 .config = mac80211_hwsim_config, \
2368 .configure_filter = mac80211_hwsim_configure_filter, \
2369 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2370 .sta_add = mac80211_hwsim_sta_add, \
2371 .sta_remove = mac80211_hwsim_sta_remove, \
2372 .sta_notify = mac80211_hwsim_sta_notify, \
2373 .set_tim = mac80211_hwsim_set_tim, \
2374 .conf_tx = mac80211_hwsim_conf_tx, \
2375 .get_survey = mac80211_hwsim_get_survey, \
2376 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2377 .ampdu_action = mac80211_hwsim_ampdu_action, \
2378 .flush = mac80211_hwsim_flush, \
2379 .get_tsf = mac80211_hwsim_get_tsf, \
2380 .set_tsf = mac80211_hwsim_set_tsf, \
2381 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2382 .get_et_stats = mac80211_hwsim_get_et_stats, \
2383 .get_et_strings = mac80211_hwsim_get_et_strings,
2384
Johannes Berg38ed5042014-01-06 22:11:06 +01002385static const struct ieee80211_ops mac80211_hwsim_ops = {
Johannes Berg246ad562016-10-10 12:09:52 +02002386 HWSIM_COMMON_OPS
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002387 .sw_scan_start = mac80211_hwsim_sw_scan,
2388 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002389};
2390
Johannes Berg246ad562016-10-10 12:09:52 +02002391static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2392 HWSIM_COMMON_OPS
2393 .hw_scan = mac80211_hwsim_hw_scan,
2394 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2395 .sw_scan_start = NULL,
2396 .sw_scan_complete = NULL,
2397 .remain_on_channel = mac80211_hwsim_roc,
2398 .cancel_remain_on_channel = mac80211_hwsim_croc,
2399 .add_chanctx = mac80211_hwsim_add_chanctx,
2400 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2401 .change_chanctx = mac80211_hwsim_change_chanctx,
2402 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2403 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2404};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002405
Jukka Rissanen62759362014-10-31 14:48:47 +02002406struct hwsim_new_radio_params {
2407 unsigned int channels;
2408 const char *reg_alpha2;
2409 const struct ieee80211_regdomain *regd;
2410 bool reg_strict;
2411 bool p2p_device;
2412 bool use_chanctx;
2413 bool destroy_on_close;
2414 const char *hwname;
2415 bool no_vif;
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002416 const u8 *perm_addr;
Jukka Rissanen62759362014-10-31 14:48:47 +02002417};
2418
2419static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2420 struct genl_info *info)
2421{
2422 if (info)
Jiri Benc92c14d92015-09-22 18:56:43 +02002423 genl_notify(&hwsim_genl_family, mcast_skb, info,
2424 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002425 else
2426 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2427 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2428}
2429
Patrik Flyktc4499812014-11-12 16:42:39 +02002430static int append_radio_msg(struct sk_buff *skb, int id,
2431 struct hwsim_new_radio_params *param)
Jukka Rissanen62759362014-10-31 14:48:47 +02002432{
Jukka Rissanen62759362014-10-31 14:48:47 +02002433 int ret;
2434
Jukka Rissanen62759362014-10-31 14:48:47 +02002435 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2436 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002437 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002438
2439 if (param->channels) {
2440 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2441 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002442 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002443 }
2444
2445 if (param->reg_alpha2) {
2446 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2447 param->reg_alpha2);
2448 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002449 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002450 }
2451
2452 if (param->regd) {
2453 int i;
2454
Johannes Berg58757552014-12-22 12:51:25 +01002455 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2456 if (hwsim_world_regdom_custom[i] != param->regd)
2457 continue;
Jukka Rissanen62759362014-10-31 14:48:47 +02002458
Jukka Rissanen62759362014-10-31 14:48:47 +02002459 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2460 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002461 return ret;
Johannes Berg58757552014-12-22 12:51:25 +01002462 break;
Jukka Rissanen62759362014-10-31 14:48:47 +02002463 }
2464 }
2465
2466 if (param->reg_strict) {
2467 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
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->p2p_device) {
2473 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
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->use_chanctx) {
2479 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2480 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002481 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002482 }
2483
2484 if (param->hwname) {
2485 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2486 strlen(param->hwname), param->hwname);
2487 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002488 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002489 }
2490
Patrik Flyktc4499812014-11-12 16:42:39 +02002491 return 0;
Jukka Rissanen62759362014-10-31 14:48:47 +02002492}
2493
Patrik Flyktc4499812014-11-12 16:42:39 +02002494static void hwsim_mcast_new_radio(int id, struct genl_info *info,
Jukka Rissanen62759362014-10-31 14:48:47 +02002495 struct hwsim_new_radio_params *param)
2496{
2497 struct sk_buff *mcast_skb;
Patrik Flyktc4499812014-11-12 16:42:39 +02002498 void *data;
Jukka Rissanen62759362014-10-31 14:48:47 +02002499
Patrik Flyktc4499812014-11-12 16:42:39 +02002500 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002501 if (!mcast_skb)
2502 return;
2503
Patrik Flyktc4499812014-11-12 16:42:39 +02002504 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2505 HWSIM_CMD_NEW_RADIO);
2506 if (!data)
2507 goto out_err;
2508
2509 if (append_radio_msg(mcast_skb, id, param) < 0)
2510 goto out_err;
2511
2512 genlmsg_end(mcast_skb, data);
2513
Jukka Rissanen62759362014-10-31 14:48:47 +02002514 hwsim_mcast_config_msg(mcast_skb, info);
Patrik Flyktc4499812014-11-12 16:42:39 +02002515 return;
2516
2517out_err:
Patrik Flyktc4499812014-11-12 16:42:39 +02002518 nlmsg_free(mcast_skb);
Jukka Rissanen62759362014-10-31 14:48:47 +02002519}
2520
2521static int mac80211_hwsim_new_radio(struct genl_info *info,
2522 struct hwsim_new_radio_params *param)
Johannes Bergde0421d2014-01-06 23:12:12 +01002523{
2524 int err;
2525 u8 addr[ETH_ALEN];
2526 struct mac80211_hwsim_data *data;
2527 struct ieee80211_hw *hw;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002528 enum nl80211_band band;
Johannes Bergde0421d2014-01-06 23:12:12 +01002529 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
Martin Willi100cb9f2016-05-09 18:33:59 +02002530 struct net *net;
Johannes Bergde0421d2014-01-06 23:12:12 +01002531 int idx;
2532
Jukka Rissanen62759362014-10-31 14:48:47 +02002533 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
Luciano Coelho361c3e02014-02-10 15:23:03 +02002534 return -EINVAL;
2535
Johannes Bergde0421d2014-01-06 23:12:12 +01002536 spin_lock_bh(&hwsim_radio_lock);
2537 idx = hwsim_radio_idx++;
2538 spin_unlock_bh(&hwsim_radio_lock);
2539
Jukka Rissanen62759362014-10-31 14:48:47 +02002540 if (param->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002541 ops = &mac80211_hwsim_mchan_ops;
Jukka Rissanen62759362014-10-31 14:48:47 +02002542 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
Johannes Bergde0421d2014-01-06 23:12:12 +01002543 if (!hw) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002544 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
Johannes Bergde0421d2014-01-06 23:12:12 +01002545 err = -ENOMEM;
2546 goto failed;
2547 }
Martin Willi100cb9f2016-05-09 18:33:59 +02002548
Andrew Zaborowskif7d3b4f2017-02-23 13:02:10 +01002549 /* ieee80211_alloc_hw_nm may have used a default name */
2550 param->hwname = wiphy_name(hw->wiphy);
2551
Martin Willi100cb9f2016-05-09 18:33:59 +02002552 if (info)
2553 net = genl_info_net(info);
2554 else
2555 net = &init_net;
2556 wiphy_net_set(hw->wiphy, net);
2557
Johannes Bergde0421d2014-01-06 23:12:12 +01002558 data = hw->priv;
2559 data->hw = hw;
2560
2561 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2562 if (IS_ERR(data->dev)) {
2563 printk(KERN_DEBUG
2564 "mac80211_hwsim: device_create failed (%ld)\n",
2565 PTR_ERR(data->dev));
2566 err = -ENOMEM;
2567 goto failed_drvdata;
2568 }
2569 data->dev->driver = &mac80211_hwsim_driver.driver;
2570 err = device_bind_driver(data->dev);
2571 if (err != 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002572 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002573 err);
Junjie Mao805dbe12014-10-28 09:31:47 +08002574 goto failed_bind;
Johannes Bergde0421d2014-01-06 23:12:12 +01002575 }
2576
2577 skb_queue_head_init(&data->pending);
2578
2579 SET_IEEE80211_DEV(hw, data->dev);
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002580 if (!param->perm_addr) {
2581 eth_zero_addr(addr);
2582 addr[0] = 0x02;
2583 addr[3] = idx >> 8;
2584 addr[4] = idx;
2585 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2586 /* Why need here second address ? */
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002587 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
Johannes Berg796e1112018-03-21 09:07:02 +01002588 data->addresses[1].addr[0] |= 0x40;
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002589 hw->wiphy->n_addresses = 2;
2590 hw->wiphy->addresses = data->addresses;
2591 /* possible address clash is checked at hash table insertion */
2592 } else {
2593 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2594 /* compatibility with automatically generated mac addr */
2595 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2596 hw->wiphy->n_addresses = 2;
2597 hw->wiphy->addresses = data->addresses;
2598 }
Johannes Bergde0421d2014-01-06 23:12:12 +01002599
Jukka Rissanen62759362014-10-31 14:48:47 +02002600 data->channels = param->channels;
2601 data->use_chanctx = param->use_chanctx;
Johannes Bergbc7910982014-01-06 23:29:20 +01002602 data->idx = idx;
Jukka Rissanen62759362014-10-31 14:48:47 +02002603 data->destroy_on_close = param->destroy_on_close;
2604 if (info)
2605 data->portid = info->snd_portid;
Johannes Bergde0421d2014-01-06 23:12:12 +01002606
Luciano Coelho361c3e02014-02-10 15:23:03 +02002607 if (data->use_chanctx) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002608 hw->wiphy->max_scan_ssids = 255;
2609 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2610 hw->wiphy->max_remain_on_channel_duration = 1000;
Johannes Bergde0421d2014-01-06 23:12:12 +01002611 hw->wiphy->iface_combinations = &data->if_combination;
Jukka Rissanen62759362014-10-31 14:48:47 +02002612 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002613 data->if_combination = hwsim_if_comb_p2p_dev[0];
2614 else
2615 data->if_combination = hwsim_if_comb[0];
Ilan Peerf7736f52016-07-05 15:23:09 +03002616 hw->wiphy->n_iface_combinations = 1;
2617 /* For channels > 1 DFS is not allowed */
2618 data->if_combination.radar_detect_widths = 0;
Johannes Bergb59ec8d2014-02-07 10:44:50 +01002619 data->if_combination.num_different_channels = data->channels;
Jukka Rissanen62759362014-10-31 14:48:47 +02002620 } else if (param->p2p_device) {
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002621 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2622 hw->wiphy->n_iface_combinations =
2623 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
Johannes Bergde0421d2014-01-06 23:12:12 +01002624 } else {
2625 hw->wiphy->iface_combinations = hwsim_if_comb;
2626 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2627 }
2628
Johannes Berg661ef472015-11-24 18:29:45 +01002629 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
Johannes Bergde0421d2014-01-06 23:12:12 +01002630 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2631 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2632
2633 hw->queues = 5;
2634 hw->offchannel_tx_hw_queue = 4;
2635 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2636 BIT(NL80211_IFTYPE_AP) |
2637 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2638 BIT(NL80211_IFTYPE_P2P_GO) |
2639 BIT(NL80211_IFTYPE_ADHOC) |
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002640 BIT(NL80211_IFTYPE_MESH_POINT);
2641
Jukka Rissanen62759362014-10-31 14:48:47 +02002642 if (param->p2p_device)
Johannes Berg8c66a3d2014-01-10 20:07:49 +01002643 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002644
Johannes Berg30686bf2015-06-02 21:39:54 +02002645 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2646 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2647 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2648 ieee80211_hw_set(hw, QUEUE_CONTROL);
2649 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2650 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2651 ieee80211_hw_set(hw, MFP_CAPABLE);
2652 ieee80211_hw_set(hw, SIGNAL_DBM);
Bjoern Johanssone4a2eb82018-04-11 11:41:56 -07002653 ieee80211_hw_set(hw, SUPPORTS_PS);
Johannes Berg33c2f532015-06-17 13:07:14 +02002654 ieee80211_hw_set(hw, TDLS_WIDER_BW);
Johannes Bergde0421d2014-01-06 23:12:12 +01002655 if (rctbl)
Johannes Berg30686bf2015-06-02 21:39:54 +02002656 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002657
2658 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2659 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02002660 WIPHY_FLAG_AP_UAPSD |
2661 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Eliad Peller0d8614b2014-09-10 14:07:36 +03002662 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2663 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2664 NL80211_FEATURE_STATIC_SMPS |
Johannes Berg339467b2014-06-12 22:41:00 +02002665 NL80211_FEATURE_DYNAMIC_SMPS |
2666 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
Jouni Malinen00eeccc2015-11-26 20:51:04 +02002667 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002668
2669 /* ask mac80211 to reserve space for magic */
2670 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2671 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2672 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2673
2674 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2675 sizeof(hwsim_channels_2ghz));
2676 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2677 sizeof(hwsim_channels_5ghz));
2678 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2679
Johannes Berg57fbcce2016-04-12 15:56:15 +02002680 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002681 struct ieee80211_supported_band *sband = &data->bands[band];
2682 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02002683 case NL80211_BAND_2GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002684 sband->channels = data->channels_2ghz;
2685 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2686 sband->bitrates = data->rates;
2687 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2688 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002689 case NL80211_BAND_5GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002690 sband->channels = data->channels_5ghz;
2691 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2692 sband->bitrates = data->rates + 4;
2693 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
Ilan Peerf33f8482015-04-07 19:05:21 +03002694
2695 sband->vht_cap.vht_supported = true;
2696 sband->vht_cap.cap =
2697 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2698 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2699 IEEE80211_VHT_CAP_RXLDPC |
2700 IEEE80211_VHT_CAP_SHORT_GI_80 |
2701 IEEE80211_VHT_CAP_SHORT_GI_160 |
2702 IEEE80211_VHT_CAP_TXSTBC |
Ilan Peerf33f8482015-04-07 19:05:21 +03002703 IEEE80211_VHT_CAP_RXSTBC_4 |
2704 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2705 sband->vht_cap.vht_mcs.rx_mcs_map =
Ilan Peer8aca9b22015-04-07 19:05:22 +03002706 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2707 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002708 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002709 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2710 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002711 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2712 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002713 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
Ilan Peerf33f8482015-04-07 19:05:21 +03002714 sband->vht_cap.vht_mcs.tx_mcs_map =
2715 sband->vht_cap.vht_mcs.rx_mcs_map;
Johannes Bergde0421d2014-01-06 23:12:12 +01002716 break;
2717 default:
2718 continue;
2719 }
2720
2721 sband->ht_cap.ht_supported = true;
2722 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2723 IEEE80211_HT_CAP_GRN_FLD |
Ben Greeare49786d2014-10-24 11:12:08 -07002724 IEEE80211_HT_CAP_SGI_20 |
Johannes Bergde0421d2014-01-06 23:12:12 +01002725 IEEE80211_HT_CAP_SGI_40 |
2726 IEEE80211_HT_CAP_DSSSCCK40;
2727 sband->ht_cap.ampdu_factor = 0x3;
2728 sband->ht_cap.ampdu_density = 0x6;
2729 memset(&sband->ht_cap.mcs, 0,
2730 sizeof(sband->ht_cap.mcs));
2731 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2732 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2733 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2734
2735 hw->wiphy->bands[band] = sband;
Johannes Bergde0421d2014-01-06 23:12:12 +01002736 }
2737
2738 /* By default all radios belong to the first group */
2739 data->group = 1;
2740 mutex_init(&data->mutex);
2741
Martin Willi100cb9f2016-05-09 18:33:59 +02002742 data->netgroup = hwsim_net_get_netgroup(net);
Andrew Zaborowski4ea5aca2018-03-21 08:05:18 +01002743 data->wmediumd = hwsim_net_get_wmediumd(net);
Martin Willi100cb9f2016-05-09 18:33:59 +02002744
Johannes Bergde0421d2014-01-06 23:12:12 +01002745 /* Enable frame retransmissions for lossy channels */
2746 hw->max_rates = 4;
2747 hw->max_rate_tries = 11;
2748
Jouni Malinend5d011b2015-02-26 15:26:54 +02002749 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2750 hw->wiphy->n_vendor_commands =
2751 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2752 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2753 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2754
Jukka Rissanen62759362014-10-31 14:48:47 +02002755 if (param->reg_strict)
Johannes Berg26b0e412014-01-10 13:37:38 +01002756 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002757 if (param->regd) {
Patrik Flykt93d638d2014-11-12 16:42:40 +02002758 data->regd = param->regd;
Johannes Berg26b0e412014-01-10 13:37:38 +01002759 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002760 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
Johannes Berg26b0e412014-01-10 13:37:38 +01002761 /* give the regulatory workqueue a chance to run */
2762 schedule_timeout_interruptible(1);
2763 }
2764
Jukka Rissanen62759362014-10-31 14:48:47 +02002765 if (param->no_vif)
Johannes Berg30686bf2015-06-02 21:39:54 +02002766 ieee80211_hw_set(hw, NO_AUTO_VIF);
Ben Greear9a0cb892014-10-22 12:23:04 -07002767
Andrew Zaborowskiae44b502017-02-10 04:50:23 +01002768 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2769
Johannes Bergde0421d2014-01-06 23:12:12 +01002770 err = ieee80211_register_hw(hw);
2771 if (err < 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002772 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002773 err);
2774 goto failed_hw;
2775 }
2776
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002777 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
Johannes Bergde0421d2014-01-06 23:12:12 +01002778
Patrik Flykt93d638d2014-11-12 16:42:40 +02002779 if (param->reg_alpha2) {
2780 data->alpha2[0] = param->reg_alpha2[0];
2781 data->alpha2[1] = param->reg_alpha2[1];
Jukka Rissanen62759362014-10-31 14:48:47 +02002782 regulatory_hint(hw->wiphy, param->reg_alpha2);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002783 }
Johannes Berg26b0e412014-01-10 13:37:38 +01002784
Johannes Bergde0421d2014-01-06 23:12:12 +01002785 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2786 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2787 debugfs_create_file("group", 0666, data->debugfs, data,
2788 &hwsim_fops_group);
Luciano Coelho361c3e02014-02-10 15:23:03 +02002789 if (!data->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002790 debugfs_create_file("dfs_simulate_radar", 0222,
2791 data->debugfs,
2792 data, &hwsim_simulate_radar);
2793
2794 tasklet_hrtimer_init(&data->beacon_timer,
2795 mac80211_hwsim_beacon,
Thomas Gleixner8fbcfeb2017-02-25 10:27:37 +00002796 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002797
2798 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002799 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2800 hwsim_rht_params);
2801 if (err < 0) {
Benjamin Beichler301b0402018-01-22 18:04:36 +01002802 if (info) {
2803 GENL_SET_ERR_MSG(info, "perm addr already present");
2804 NL_SET_BAD_ATTR(info->extack,
2805 info->attrs[HWSIM_ATTR_PERM_ADDR]);
2806 }
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002807 spin_unlock_bh(&hwsim_radio_lock);
2808 goto failed_final_insert;
2809 }
2810
Johannes Bergde0421d2014-01-06 23:12:12 +01002811 list_add_tail(&data->list, &hwsim_radios);
Benjamin Beichler63356982018-01-22 18:04:37 +01002812 hwsim_radios_generation++;
Johannes Bergde0421d2014-01-06 23:12:12 +01002813 spin_unlock_bh(&hwsim_radio_lock);
2814
Jukka Rissanen62759362014-10-31 14:48:47 +02002815 if (idx > 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002816 hwsim_mcast_new_radio(idx, info, param);
Jukka Rissanen62759362014-10-31 14:48:47 +02002817
Johannes Bergbc7910982014-01-06 23:29:20 +01002818 return idx;
Johannes Bergde0421d2014-01-06 23:12:12 +01002819
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002820failed_final_insert:
2821 debugfs_remove_recursive(data->debugfs);
2822 ieee80211_unregister_hw(data->hw);
Johannes Bergde0421d2014-01-06 23:12:12 +01002823failed_hw:
Junjie Mao805dbe12014-10-28 09:31:47 +08002824 device_release_driver(data->dev);
2825failed_bind:
Johannes Bergde0421d2014-01-06 23:12:12 +01002826 device_unregister(data->dev);
2827failed_drvdata:
2828 ieee80211_free_hw(hw);
2829failed:
2830 return err;
2831}
2832
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002833static void hwsim_mcast_del_radio(int id, const char *hwname,
2834 struct genl_info *info)
Johannes Berge4afb602014-01-06 22:43:11 +01002835{
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002836 struct sk_buff *skb;
2837 void *data;
2838 int ret;
2839
2840 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2841 if (!skb)
2842 return;
2843
2844 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
2845 HWSIM_CMD_DEL_RADIO);
2846 if (!data)
2847 goto error;
2848
2849 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2850 if (ret < 0)
2851 goto error;
2852
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002853 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
2854 hwname);
2855 if (ret < 0)
2856 goto error;
Jukka Rissanen579a05f2014-10-31 14:48:48 +02002857
2858 genlmsg_end(skb, data);
2859
2860 hwsim_mcast_config_msg(skb, info);
2861
2862 return;
2863
2864error:
2865 nlmsg_free(skb);
2866}
2867
2868static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
2869 const char *hwname,
2870 struct genl_info *info)
2871{
2872 hwsim_mcast_del_radio(data->idx, hwname, info);
Johannes Berge4afb602014-01-06 22:43:11 +01002873 debugfs_remove_recursive(data->debugfs);
2874 ieee80211_unregister_hw(data->hw);
2875 device_release_driver(data->dev);
2876 device_unregister(data->dev);
2877 ieee80211_free_hw(data->hw);
2878}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002879
Patrik Flykt93d638d2014-11-12 16:42:40 +02002880static int mac80211_hwsim_get_radio(struct sk_buff *skb,
2881 struct mac80211_hwsim_data *data,
2882 u32 portid, u32 seq,
2883 struct netlink_callback *cb, int flags)
2884{
2885 void *hdr;
2886 struct hwsim_new_radio_params param = { };
2887 int res = -EMSGSIZE;
2888
2889 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
2890 HWSIM_CMD_GET_RADIO);
2891 if (!hdr)
2892 return -EMSGSIZE;
2893
2894 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +01002895 genl_dump_check_consistent(cb, hdr);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002896
Patrik Flyktb0ad5e82014-11-21 15:08:02 +02002897 if (data->alpha2[0] && data->alpha2[1])
2898 param.reg_alpha2 = data->alpha2;
2899
Patrik Flykt93d638d2014-11-12 16:42:40 +02002900 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
2901 REGULATORY_STRICT_REG);
2902 param.p2p_device = !!(data->hw->wiphy->interface_modes &
2903 BIT(NL80211_IFTYPE_P2P_DEVICE));
2904 param.use_chanctx = data->use_chanctx;
2905 param.regd = data->regd;
2906 param.channels = data->channels;
2907 param.hwname = wiphy_name(data->hw->wiphy);
2908
2909 res = append_radio_msg(skb, data->idx, &param);
2910 if (res < 0)
2911 goto out_err;
2912
Johannes Berg053c0952015-01-16 22:09:00 +01002913 genlmsg_end(skb, hdr);
2914 return 0;
Patrik Flykt93d638d2014-11-12 16:42:40 +02002915
2916out_err:
2917 genlmsg_cancel(skb, hdr);
2918 return res;
2919}
2920
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002921static void mac80211_hwsim_free(void)
2922{
Johannes Berge4afb602014-01-06 22:43:11 +01002923 struct mac80211_hwsim_data *data;
Johannes Berg0e057d732008-09-12 00:39:22 +02002924
2925 spin_lock_bh(&hwsim_radio_lock);
Johannes Berge4afb602014-01-06 22:43:11 +01002926 while ((data = list_first_entry_or_null(&hwsim_radios,
2927 struct mac80211_hwsim_data,
2928 list))) {
2929 list_del(&data->list);
2930 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02002931 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
2932 NULL);
Johannes Berge4afb602014-01-06 22:43:11 +01002933 spin_lock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002934 }
Johannes Berge4afb602014-01-06 22:43:11 +01002935 spin_unlock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002936 class_destroy(hwsim_class);
2937}
2938
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002939static const struct net_device_ops hwsim_netdev_ops = {
2940 .ndo_start_xmit = hwsim_mon_xmit,
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002941 .ndo_set_mac_address = eth_mac_addr,
2942 .ndo_validate_addr = eth_validate_addr,
2943};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002944
2945static void hwsim_mon_setup(struct net_device *dev)
2946{
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00002947 dev->netdev_ops = &hwsim_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002948 dev->needs_free_netdev = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002949 ether_setup(dev);
Phil Sutter3db6da12015-08-18 10:30:43 +02002950 dev->priv_flags |= IFF_NO_QUEUE;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002951 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Joe Perches93803b32015-03-02 19:54:49 -08002952 eth_zero_addr(dev->dev_addr);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002953 dev->dev_addr[0] = 0x12;
2954}
2955
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002956static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
Javier Lopez78825132011-06-01 11:26:13 +02002957{
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002958 return rhashtable_lookup_fast(&hwsim_radios_rht,
2959 addr,
2960 hwsim_rht_params);
Javier Lopez78825132011-06-01 11:26:13 +02002961}
2962
Martin Willif21e4d82016-05-14 10:34:44 +02002963static void hwsim_register_wmediumd(struct net *net, u32 portid)
2964{
2965 struct mac80211_hwsim_data *data;
2966
2967 hwsim_net_set_wmediumd(net, portid);
2968
2969 spin_lock_bh(&hwsim_radio_lock);
2970 list_for_each_entry(data, &hwsim_radios, list) {
2971 if (data->netgroup == hwsim_net_get_netgroup(net))
2972 data->wmediumd = portid;
2973 }
2974 spin_unlock_bh(&hwsim_radio_lock);
2975}
2976
Javier Lopez78825132011-06-01 11:26:13 +02002977static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2978 struct genl_info *info)
2979{
2980
2981 struct ieee80211_hdr *hdr;
2982 struct mac80211_hwsim_data *data2;
2983 struct ieee80211_tx_info *txi;
2984 struct hwsim_tx_rate *tx_attempts;
Johannes Berg959eb2f2015-11-06 11:57:23 +01002985 u64 ret_skb_cookie;
Javier Lopez78825132011-06-01 11:26:13 +02002986 struct sk_buff *skb, *tmp;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002987 const u8 *src;
Javier Lopez78825132011-06-01 11:26:13 +02002988 unsigned int hwsim_flags;
Javier Lopez78825132011-06-01 11:26:13 +02002989 int i;
2990 bool found = false;
2991
2992 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002993 !info->attrs[HWSIM_ATTR_FLAGS] ||
2994 !info->attrs[HWSIM_ATTR_COOKIE] ||
Martin Willi62397da2016-05-13 12:41:48 +02002995 !info->attrs[HWSIM_ATTR_SIGNAL] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002996 !info->attrs[HWSIM_ATTR_TX_INFO])
Javier Lopez78825132011-06-01 11:26:13 +02002997 goto out;
2998
Johannes Berg9ddd12a2014-01-06 23:03:02 +01002999 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
Javier Lopez78825132011-06-01 11:26:13 +02003000 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
Johannes Berg959eb2f2015-11-06 11:57:23 +01003001 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
Javier Lopez78825132011-06-01 11:26:13 +02003002
3003 data2 = get_hwsim_data_ref_from_addr(src);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003004 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003005 goto out;
3006
Martin Willif21e4d82016-05-14 10:34:44 +02003007 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3008 goto out;
3009
3010 if (info->snd_portid != data2->wmediumd)
3011 goto out;
3012
Javier Lopez78825132011-06-01 11:26:13 +02003013 /* look for the skb matching the cookie passed back from user */
3014 skb_queue_walk_safe(&data2->pending, skb, tmp) {
Johannes Berg959eb2f2015-11-06 11:57:23 +01003015 u64 skb_cookie;
3016
3017 txi = IEEE80211_SKB_CB(skb);
3018 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3019
3020 if (skb_cookie == ret_skb_cookie) {
Javier Lopez78825132011-06-01 11:26:13 +02003021 skb_unlink(skb, &data2->pending);
3022 found = true;
3023 break;
3024 }
3025 }
3026
3027 /* not found */
3028 if (!found)
3029 goto out;
3030
3031 /* Tx info received because the frame was broadcasted on user space,
3032 so we get all the necessary info: tx attempts and skb control buff */
3033
3034 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3035 info->attrs[HWSIM_ATTR_TX_INFO]);
3036
3037 /* now send back TX status */
3038 txi = IEEE80211_SKB_CB(skb);
3039
Javier Lopez78825132011-06-01 11:26:13 +02003040 ieee80211_tx_info_clear_status(txi);
3041
3042 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3043 txi->status.rates[i].idx = tx_attempts[i].idx;
3044 txi->status.rates[i].count = tx_attempts[i].count;
Javier Lopez78825132011-06-01 11:26:13 +02003045 }
3046
3047 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3048
3049 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3050 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3051 if (skb->len >= 16) {
3052 hdr = (struct ieee80211_hdr *) skb->data;
Javier Lopezc0e30762013-12-16 10:01:07 -08003053 mac80211_hwsim_monitor_ack(data2->channel,
Johannes Berge8261172012-07-27 19:48:26 +02003054 hdr->addr2);
Javier Lopez78825132011-06-01 11:26:13 +02003055 }
Qasim Javed06cf5c42012-06-05 01:25:44 -05003056 txi->flags |= IEEE80211_TX_STAT_ACK;
Javier Lopez78825132011-06-01 11:26:13 +02003057 }
3058 ieee80211_tx_status_irqsafe(data2->hw, skb);
3059 return 0;
3060out:
3061 return -EINVAL;
3062
3063}
3064
3065static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3066 struct genl_info *info)
3067{
Johannes Berge8261172012-07-27 19:48:26 +02003068 struct mac80211_hwsim_data *data2;
Javier Lopez78825132011-06-01 11:26:13 +02003069 struct ieee80211_rx_status rx_status;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003070 const u8 *dst;
Javier Lopez78825132011-06-01 11:26:13 +02003071 int frame_data_len;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003072 void *frame_data;
Javier Lopez78825132011-06-01 11:26:13 +02003073 struct sk_buff *skb = NULL;
3074
3075 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
Johannes Berge8261172012-07-27 19:48:26 +02003076 !info->attrs[HWSIM_ATTR_FRAME] ||
3077 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3078 !info->attrs[HWSIM_ATTR_SIGNAL])
Javier Lopez78825132011-06-01 11:26:13 +02003079 goto out;
3080
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003081 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
Javier Lopez78825132011-06-01 11:26:13 +02003082 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003083 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
Javier Lopez78825132011-06-01 11:26:13 +02003084
3085 /* Allocate new skb here */
3086 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3087 if (skb == NULL)
3088 goto err;
3089
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003090 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
Javier Lopez78825132011-06-01 11:26:13 +02003091 goto err;
3092
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003093 /* Copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02003094 skb_put_data(skb, frame_data, frame_data_len);
Javier Lopez78825132011-06-01 11:26:13 +02003095
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003096 data2 = get_hwsim_data_ref_from_addr(dst);
3097 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003098 goto out;
3099
Martin Willif21e4d82016-05-14 10:34:44 +02003100 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3101 goto out;
3102
3103 if (info->snd_portid != data2->wmediumd)
3104 goto out;
3105
Javier Lopez78825132011-06-01 11:26:13 +02003106 /* check if radio is configured properly */
3107
Johannes Berge8261172012-07-27 19:48:26 +02003108 if (data2->idle || !data2->started)
Javier Lopez78825132011-06-01 11:26:13 +02003109 goto out;
3110
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003111 /* A frame is received from user space */
Javier Lopez78825132011-06-01 11:26:13 +02003112 memset(&rx_status, 0, sizeof(rx_status));
Adam Welle641cf2a2015-12-01 17:13:52 -05003113 if (info->attrs[HWSIM_ATTR_FREQ]) {
3114 /* throw away off-channel packets, but allow both the temporary
3115 * ("hw" scan/remain-on-channel) and regular channel, since the
3116 * internal datapath also allows this
3117 */
3118 mutex_lock(&data2->mutex);
3119 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3120
3121 if (rx_status.freq != data2->channel->center_freq &&
3122 (!data2->tmp_chan ||
3123 rx_status.freq != data2->tmp_chan->center_freq)) {
3124 mutex_unlock(&data2->mutex);
3125 goto out;
3126 }
3127 mutex_unlock(&data2->mutex);
3128 } else {
3129 rx_status.freq = data2->channel->center_freq;
3130 }
3131
Javier Lopez78825132011-06-01 11:26:13 +02003132 rx_status.band = data2->channel->band;
3133 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3134 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3135
3136 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
Ben Greear2155c3f2014-10-22 12:22:58 -07003137 data2->rx_pkts++;
3138 data2->rx_bytes += skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02003139 ieee80211_rx_irqsafe(data2->hw, skb);
3140
3141 return 0;
3142err:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003143 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003144out:
3145 dev_kfree_skb(skb);
3146 return -EINVAL;
3147}
3148
3149static int hwsim_register_received_nl(struct sk_buff *skb_2,
3150 struct genl_info *info)
3151{
Martin Willif21e4d82016-05-14 10:34:44 +02003152 struct net *net = genl_info_net(info);
Johannes Bergc5ac0852014-01-07 23:28:08 +01003153 struct mac80211_hwsim_data *data;
3154 int chans = 1;
3155
3156 spin_lock_bh(&hwsim_radio_lock);
3157 list_for_each_entry(data, &hwsim_radios, list)
3158 chans = max(chans, data->channels);
3159 spin_unlock_bh(&hwsim_radio_lock);
3160
3161 /* In the future we should revise the userspace API and allow it
3162 * to set a flag that it does support multi-channel, then we can
3163 * let this pass conditionally on the flag.
3164 * For current userspace, prohibit it since it won't work right.
3165 */
3166 if (chans > 1)
3167 return -EOPNOTSUPP;
3168
Martin Willif21e4d82016-05-14 10:34:44 +02003169 if (hwsim_net_get_wmediumd(net))
Johannes Berg85ca8fc2014-01-07 23:21:34 +01003170 return -EBUSY;
Javier Lopez78825132011-06-01 11:26:13 +02003171
Martin Willif21e4d82016-05-14 10:34:44 +02003172 hwsim_register_wmediumd(net, info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003173
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003174 pr_debug("mac80211_hwsim: received a REGISTER, "
Eric W. Biederman15e47302012-09-07 20:12:54 +00003175 "switching to wmediumd mode with pid %d\n", info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003176
3177 return 0;
Javier Lopez78825132011-06-01 11:26:13 +02003178}
3179
Jukka Rissanen62759362014-10-31 14:48:47 +02003180static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003181{
Jukka Rissanen62759362014-10-31 14:48:47 +02003182 struct hwsim_new_radio_params param = { 0 };
Johannes Bergff4dd732017-02-27 17:15:28 +01003183 const char *hwname = NULL;
Ben Hutchings67bd5232017-11-10 18:48:50 +00003184 int ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02003185
3186 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3187 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3188 param.channels = channels;
3189 param.destroy_on_close =
3190 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
Johannes Bergbc7910982014-01-06 23:29:20 +01003191
3192 if (info->attrs[HWSIM_ATTR_CHANNELS])
Jukka Rissanen62759362014-10-31 14:48:47 +02003193 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
Johannes Bergbc7910982014-01-06 23:29:20 +01003194
Johannes Berg48400432018-08-15 18:17:03 +02003195 if (param.channels < 1) {
3196 GENL_SET_ERR_MSG(info, "must have at least one channel");
3197 return -EINVAL;
3198 }
3199
Johannes Berg51a1aaa2018-01-15 09:32:36 +01003200 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3201 GENL_SET_ERR_MSG(info, "too many channels specified");
3202 return -EINVAL;
3203 }
3204
Ben Greear9a0cb892014-10-22 12:23:04 -07003205 if (info->attrs[HWSIM_ATTR_NO_VIF])
Jukka Rissanen62759362014-10-31 14:48:47 +02003206 param.no_vif = true;
Ben Greear9a0cb892014-10-22 12:23:04 -07003207
Johannes Bergff4dd732017-02-27 17:15:28 +01003208 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3209 hwname = kasprintf(GFP_KERNEL, "%.*s",
3210 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3211 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3212 if (!hwname)
3213 return -ENOMEM;
3214 param.hwname = hwname;
3215 }
Ben Greear5cd89262014-10-22 12:23:02 -07003216
Luciano Coelho361c3e02014-02-10 15:23:03 +02003217 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
Jukka Rissanen62759362014-10-31 14:48:47 +02003218 param.use_chanctx = true;
Luciano Coelho361c3e02014-02-10 15:23:03 +02003219 else
Jukka Rissanen62759362014-10-31 14:48:47 +02003220 param.use_chanctx = (param.channels > 1);
Luciano Coelho361c3e02014-02-10 15:23:03 +02003221
Johannes Berg26b0e412014-01-10 13:37:38 +01003222 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
Jukka Rissanen62759362014-10-31 14:48:47 +02003223 param.reg_alpha2 =
3224 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
Johannes Berg26b0e412014-01-10 13:37:38 +01003225
3226 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3227 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3228
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003229 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom)) {
3230 kfree(hwname);
Johannes Berg26b0e412014-01-10 13:37:38 +01003231 return -EINVAL;
weiyongjun (A)0ddcff42018-01-18 02:23:34 +00003232 }
Jinbum Park3a2af7c2018-07-31 23:10:40 +09003233
3234 idx = array_index_nospec(idx,
3235 ARRAY_SIZE(hwsim_world_regdom_custom));
Jukka Rissanen62759362014-10-31 14:48:47 +02003236 param.regd = hwsim_world_regdom_custom[idx];
Johannes Berg26b0e412014-01-10 13:37:38 +01003237 }
3238
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01003239 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3240 if (!is_valid_ether_addr(
3241 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3242 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3243 NL_SET_BAD_ATTR(info->extack,
3244 info->attrs[HWSIM_ATTR_PERM_ADDR]);
YueHaibinge8f90c72018-04-24 11:08:35 +08003245 kfree(hwname);
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01003246 return -EINVAL;
3247 }
3248
3249
3250 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3251 }
3252
Ben Hutchings67bd5232017-11-10 18:48:50 +00003253 ret = mac80211_hwsim_new_radio(info, &param);
3254 kfree(hwname);
3255 return ret;
Johannes Bergbc7910982014-01-06 23:29:20 +01003256}
3257
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003258static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003259{
3260 struct mac80211_hwsim_data *data;
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003261 s64 idx = -1;
3262 const char *hwname = NULL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003263
Johannes Bergff4dd732017-02-27 17:15:28 +01003264 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003265 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
Johannes Bergff4dd732017-02-27 17:15:28 +01003266 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3267 hwname = kasprintf(GFP_KERNEL, "%.*s",
3268 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3269 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3270 if (!hwname)
3271 return -ENOMEM;
3272 } else
Johannes Bergbc7910982014-01-06 23:29:20 +01003273 return -EINVAL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003274
3275 spin_lock_bh(&hwsim_radio_lock);
3276 list_for_each_entry(data, &hwsim_radios, list) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003277 if (idx >= 0) {
3278 if (data->idx != idx)
3279 continue;
3280 } else {
Johannes Bergff4dd732017-02-27 17:15:28 +01003281 if (!hwname ||
3282 strcmp(hwname, wiphy_name(data->hw->wiphy)))
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003283 continue;
3284 }
3285
Martin Willi100cb9f2016-05-09 18:33:59 +02003286 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3287 continue;
3288
Johannes Bergbc7910982014-01-06 23:29:20 +01003289 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003290 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3291 hwsim_rht_params);
Benjamin Beichler63356982018-01-22 18:04:37 +01003292 hwsim_radios_generation++;
Johannes Bergbc7910982014-01-06 23:29:20 +01003293 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003294 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3295 info);
Johannes Bergff4dd732017-02-27 17:15:28 +01003296 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003297 return 0;
3298 }
3299 spin_unlock_bh(&hwsim_radio_lock);
3300
Johannes Bergff4dd732017-02-27 17:15:28 +01003301 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003302 return -ENODEV;
Javier Lopez78825132011-06-01 11:26:13 +02003303}
3304
Patrik Flykt93d638d2014-11-12 16:42:40 +02003305static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3306{
3307 struct mac80211_hwsim_data *data;
3308 struct sk_buff *skb;
3309 int idx, res = -ENODEV;
3310
3311 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3312 return -EINVAL;
3313 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3314
3315 spin_lock_bh(&hwsim_radio_lock);
3316 list_for_each_entry(data, &hwsim_radios, list) {
3317 if (data->idx != idx)
3318 continue;
3319
Martin Willi100cb9f2016-05-09 18:33:59 +02003320 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3321 continue;
3322
Jia-Ju Bai162bd5e2017-12-12 17:26:36 +08003323 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003324 if (!skb) {
3325 res = -ENOMEM;
3326 goto out_err;
3327 }
3328
3329 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3330 info->snd_seq, NULL, 0);
3331 if (res < 0) {
3332 nlmsg_free(skb);
3333 goto out_err;
3334 }
3335
3336 genlmsg_reply(skb, info);
3337 break;
3338 }
3339
3340out_err:
3341 spin_unlock_bh(&hwsim_radio_lock);
3342
3343 return res;
3344}
3345
3346static int hwsim_dump_radio_nl(struct sk_buff *skb,
3347 struct netlink_callback *cb)
3348{
Andrew Zaborowskifed48252018-05-22 02:43:56 +02003349 int last_idx = cb->args[0] - 1;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003350 struct mac80211_hwsim_data *data = NULL;
Benjamin Beichler63356982018-01-22 18:04:37 +01003351 int res = 0;
3352 void *hdr;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003353
3354 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichler63356982018-01-22 18:04:37 +01003355 cb->seq = hwsim_radios_generation;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003356
Benjamin Beichler63356982018-01-22 18:04:37 +01003357 if (last_idx >= hwsim_radio_idx-1)
Patrik Flykt93d638d2014-11-12 16:42:40 +02003358 goto done;
3359
3360 list_for_each_entry(data, &hwsim_radios, list) {
Benjamin Beichler63356982018-01-22 18:04:37 +01003361 if (data->idx <= last_idx)
Patrik Flykt93d638d2014-11-12 16:42:40 +02003362 continue;
3363
Martin Willi100cb9f2016-05-09 18:33:59 +02003364 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3365 continue;
3366
Patrik Flykt93d638d2014-11-12 16:42:40 +02003367 res = mac80211_hwsim_get_radio(skb, data,
3368 NETLINK_CB(cb->skb).portid,
3369 cb->nlh->nlmsg_seq, cb,
3370 NLM_F_MULTI);
3371 if (res < 0)
3372 break;
3373
Benjamin Beichler63356982018-01-22 18:04:37 +01003374 last_idx = data->idx;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003375 }
3376
Andrew Zaborowskifed48252018-05-22 02:43:56 +02003377 cb->args[0] = last_idx + 1;
Benjamin Beichler63356982018-01-22 18:04:37 +01003378
3379 /* list changed, but no new element sent, set interrupted flag */
3380 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3381 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3382 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3383 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3384 if (!hdr)
3385 res = -EMSGSIZE;
3386 genl_dump_check_consistent(cb, hdr);
3387 genlmsg_end(skb, hdr);
3388 }
Patrik Flykt93d638d2014-11-12 16:42:40 +02003389
3390done:
3391 spin_unlock_bh(&hwsim_radio_lock);
Benjamin Beichler63356982018-01-22 18:04:37 +01003392 return res ?: skb->len;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003393}
3394
Javier Lopez78825132011-06-01 11:26:13 +02003395/* Generic Netlink operations array */
Johannes Berg4534de82013-11-14 17:14:46 +01003396static const struct genl_ops hwsim_ops[] = {
Javier Lopez78825132011-06-01 11:26:13 +02003397 {
3398 .cmd = HWSIM_CMD_REGISTER,
3399 .policy = hwsim_genl_policy,
3400 .doit = hwsim_register_received_nl,
Martin Willif21e4d82016-05-14 10:34:44 +02003401 .flags = GENL_UNS_ADMIN_PERM,
Javier Lopez78825132011-06-01 11:26:13 +02003402 },
3403 {
3404 .cmd = HWSIM_CMD_FRAME,
3405 .policy = hwsim_genl_policy,
3406 .doit = hwsim_cloned_frame_received_nl,
3407 },
3408 {
3409 .cmd = HWSIM_CMD_TX_INFO_FRAME,
3410 .policy = hwsim_genl_policy,
3411 .doit = hwsim_tx_info_frame_received_nl,
3412 },
Johannes Bergbc7910982014-01-06 23:29:20 +01003413 {
Jukka Rissanen62759362014-10-31 14:48:47 +02003414 .cmd = HWSIM_CMD_NEW_RADIO,
Johannes Bergbc7910982014-01-06 23:29:20 +01003415 .policy = hwsim_genl_policy,
Jukka Rissanen62759362014-10-31 14:48:47 +02003416 .doit = hwsim_new_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003417 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003418 },
3419 {
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003420 .cmd = HWSIM_CMD_DEL_RADIO,
Johannes Bergbc7910982014-01-06 23:29:20 +01003421 .policy = hwsim_genl_policy,
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003422 .doit = hwsim_del_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003423 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003424 },
Patrik Flykt93d638d2014-11-12 16:42:40 +02003425 {
3426 .cmd = HWSIM_CMD_GET_RADIO,
3427 .policy = hwsim_genl_policy,
3428 .doit = hwsim_get_radio_nl,
3429 .dumpit = hwsim_dump_radio_nl,
3430 },
Javier Lopez78825132011-06-01 11:26:13 +02003431};
3432
Johannes Berg56989f62016-10-24 14:40:05 +02003433static struct genl_family hwsim_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003434 .name = "MAC80211_HWSIM",
3435 .version = 1,
3436 .maxattr = HWSIM_ATTR_MAX,
3437 .netnsok = true,
3438 .module = THIS_MODULE,
3439 .ops = hwsim_ops,
3440 .n_ops = ARRAY_SIZE(hwsim_ops),
3441 .mcgrps = hwsim_mcgrps,
3442 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3443};
3444
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003445static void destroy_radio(struct work_struct *work)
3446{
3447 struct mac80211_hwsim_data *data =
3448 container_of(work, struct mac80211_hwsim_data, destroy_work);
3449
Benjamin Beichler63356982018-01-22 18:04:37 +01003450 hwsim_radios_generation++;
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003451 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy), NULL);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003452}
3453
3454static void remove_user_radios(u32 portid)
3455{
3456 struct mac80211_hwsim_data *entry, *tmp;
3457
3458 spin_lock_bh(&hwsim_radio_lock);
3459 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3460 if (entry->destroy_on_close && entry->portid == portid) {
3461 list_del(&entry->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003462 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3463 hwsim_rht_params);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003464 INIT_WORK(&entry->destroy_work, destroy_radio);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003465 queue_work(hwsim_wq, &entry->destroy_work);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003466 }
3467 }
3468 spin_unlock_bh(&hwsim_radio_lock);
3469}
3470
Javier Lopez78825132011-06-01 11:26:13 +02003471static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3472 unsigned long state,
3473 void *_notify)
3474{
3475 struct netlink_notify *notify = _notify;
3476
3477 if (state != NETLINK_URELEASE)
3478 return NOTIFY_DONE;
3479
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003480 remove_user_radios(notify->portid);
3481
Martin Willif21e4d82016-05-14 10:34:44 +02003482 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
Javier Lopez78825132011-06-01 11:26:13 +02003483 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3484 " socket, switching to perfect channel medium\n");
Martin Willif21e4d82016-05-14 10:34:44 +02003485 hwsim_register_wmediumd(notify->net, 0);
Javier Lopez78825132011-06-01 11:26:13 +02003486 }
3487 return NOTIFY_DONE;
3488
3489}
3490
3491static struct notifier_block hwsim_netlink_notifier = {
3492 .notifier_call = mac80211_hwsim_netlink_notify,
3493};
3494
Johannes Berg56989f62016-10-24 14:40:05 +02003495static int __init hwsim_init_netlink(void)
Javier Lopez78825132011-06-01 11:26:13 +02003496{
3497 int rc;
Johannes Berge8261172012-07-27 19:48:26 +02003498
Javier Lopez78825132011-06-01 11:26:13 +02003499 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3500
Johannes Berg489111e2016-10-24 14:40:03 +02003501 rc = genl_register_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003502 if (rc)
3503 goto failure;
3504
3505 rc = netlink_register_notifier(&hwsim_netlink_notifier);
Su Kang Yin2459cd82015-08-07 16:54:10 +08003506 if (rc) {
3507 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003508 goto failure;
Su Kang Yin2459cd82015-08-07 16:54:10 +08003509 }
Javier Lopez78825132011-06-01 11:26:13 +02003510
3511 return 0;
3512
3513failure:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003514 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003515 return -EINVAL;
3516}
3517
Martin Willi100cb9f2016-05-09 18:33:59 +02003518static __net_init int hwsim_init_net(struct net *net)
3519{
Kirill Tkhai03695542018-03-01 14:30:09 +03003520 return hwsim_net_set_netgroup(net);
Martin Willi100cb9f2016-05-09 18:33:59 +02003521}
3522
3523static void __net_exit hwsim_exit_net(struct net *net)
3524{
3525 struct mac80211_hwsim_data *data, *tmp;
3526
3527 spin_lock_bh(&hwsim_radio_lock);
3528 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3529 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3530 continue;
3531
3532 /* Radios created in init_net are returned to init_net. */
3533 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3534 continue;
3535
3536 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003537 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3538 hwsim_rht_params);
Benjamin Beichler8cfd36a2018-03-07 18:11:07 +01003539 hwsim_radios_generation++;
3540 spin_unlock_bh(&hwsim_radio_lock);
3541 mac80211_hwsim_del_radio(data,
3542 wiphy_name(data->hw->wiphy),
3543 NULL);
3544 spin_lock_bh(&hwsim_radio_lock);
Martin Willi100cb9f2016-05-09 18:33:59 +02003545 }
3546 spin_unlock_bh(&hwsim_radio_lock);
Kirill Tkhai03695542018-03-01 14:30:09 +03003547
3548 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
Martin Willi100cb9f2016-05-09 18:33:59 +02003549}
3550
3551static struct pernet_operations hwsim_net_ops = {
3552 .init = hwsim_init_net,
3553 .exit = hwsim_exit_net,
3554 .id = &hwsim_net_id,
3555 .size = sizeof(struct hwsim_net),
3556};
3557
Javier Lopez78825132011-06-01 11:26:13 +02003558static void hwsim_exit_netlink(void)
3559{
Javier Lopez78825132011-06-01 11:26:13 +02003560 /* unregister the notifier */
3561 netlink_unregister_notifier(&hwsim_netlink_notifier);
3562 /* unregister the family */
Johannes Bergcf7a6b22014-01-06 21:55:14 +01003563 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003564}
3565
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003566static int __init init_mac80211_hwsim(void)
3567{
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003568 int i, err;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003569
Johannes Bergbc7910982014-01-06 23:29:20 +01003570 if (radios < 0 || radios > 100)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003571 return -EINVAL;
3572
Johannes Berge8261172012-07-27 19:48:26 +02003573 if (channels < 1)
3574 return -EINVAL;
3575
Johannes Berg0e057d732008-09-12 00:39:22 +02003576 spin_lock_init(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003577
Johannes Bergce162bf2018-01-24 08:40:51 +01003578 hwsim_wq = alloc_workqueue("hwsim_wq", 0, 0);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003579 if (!hwsim_wq)
3580 return -ENOMEM;
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003581
3582 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
3583 if (err)
3584 goto out_free_wq;
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003585
Martin Willi100cb9f2016-05-09 18:33:59 +02003586 err = register_pernet_device(&hwsim_net_ops);
Martin Pitt9ea927742013-04-08 11:30:01 +02003587 if (err)
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003588 goto out_free_rht;
Martin Pitt9ea927742013-04-08 11:30:01 +02003589
Martin Willi100cb9f2016-05-09 18:33:59 +02003590 err = platform_driver_register(&mac80211_hwsim_driver);
3591 if (err)
3592 goto out_unregister_pernet;
3593
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003594 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
Martin Pitt9ea927742013-04-08 11:30:01 +02003595 if (IS_ERR(hwsim_class)) {
3596 err = PTR_ERR(hwsim_class);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003597 goto out_unregister_driver;
Martin Pitt9ea927742013-04-08 11:30:01 +02003598 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003599
Jukka Rissanen62759362014-10-31 14:48:47 +02003600 err = hwsim_init_netlink();
3601 if (err < 0)
3602 goto out_unregister_driver;
3603
Johannes Berg0e057d732008-09-12 00:39:22 +02003604 for (i = 0; i < radios; i++) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003605 struct hwsim_new_radio_params param = { 0 };
3606
3607 param.channels = channels;
Johannes Berg0e057d732008-09-12 00:39:22 +02003608
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003609 switch (regtest) {
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003610 case HWSIM_REGTEST_DIFF_COUNTRY:
Johannes Berg26b0e412014-01-10 13:37:38 +01003611 if (i < ARRAY_SIZE(hwsim_alpha2s))
Jukka Rissanen62759362014-10-31 14:48:47 +02003612 param.reg_alpha2 = hwsim_alpha2s[i];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003613 break;
3614 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3615 if (!i)
Jukka Rissanen62759362014-10-31 14:48:47 +02003616 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003617 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003618 case HWSIM_REGTEST_STRICT_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003619 param.reg_strict = true;
Johannes Berg26b0e412014-01-10 13:37:38 +01003620 case HWSIM_REGTEST_DRIVER_REG_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003621 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003622 break;
Johannes Berg26b0e412014-01-10 13:37:38 +01003623 case HWSIM_REGTEST_WORLD_ROAM:
3624 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003625 param.regd = &hwsim_world_regdom_custom_01;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003626 break;
3627 case HWSIM_REGTEST_CUSTOM_WORLD:
Jukka Rissanen62759362014-10-31 14:48:47 +02003628 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003629 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003630 case HWSIM_REGTEST_CUSTOM_WORLD_2:
Johannes Berg26b0e412014-01-10 13:37:38 +01003631 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003632 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003633 else if (i == 1)
Jukka Rissanen62759362014-10-31 14:48:47 +02003634 param.regd = &hwsim_world_regdom_custom_02;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003635 break;
3636 case HWSIM_REGTEST_STRICT_FOLLOW:
Johannes Berg26b0e412014-01-10 13:37:38 +01003637 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003638 param.reg_strict = true;
3639 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003640 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003641 break;
3642 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
Johannes Berg26b0e412014-01-10 13:37:38 +01003643 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003644 param.reg_strict = true;
3645 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003646 } else if (i == 1) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003647 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003648 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003649 break;
3650 case HWSIM_REGTEST_ALL:
Johannes Berg26b0e412014-01-10 13:37:38 +01003651 switch (i) {
3652 case 0:
Jukka Rissanen62759362014-10-31 14:48:47 +02003653 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003654 break;
3655 case 1:
Jukka Rissanen62759362014-10-31 14:48:47 +02003656 param.regd = &hwsim_world_regdom_custom_02;
Johannes Berg26b0e412014-01-10 13:37:38 +01003657 break;
3658 case 2:
Jukka Rissanen62759362014-10-31 14:48:47 +02003659 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003660 break;
3661 case 3:
Jukka Rissanen62759362014-10-31 14:48:47 +02003662 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003663 break;
3664 case 4:
Jukka Rissanen62759362014-10-31 14:48:47 +02003665 param.reg_strict = true;
3666 param.reg_alpha2 = hwsim_alpha2s[2];
Johannes Berg26b0e412014-01-10 13:37:38 +01003667 break;
3668 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003669 break;
3670 default:
3671 break;
3672 }
3673
Jukka Rissanen62759362014-10-31 14:48:47 +02003674 param.p2p_device = support_p2p_device;
3675 param.use_chanctx = channels > 1;
3676
3677 err = mac80211_hwsim_new_radio(NULL, &param);
Johannes Bergbc7910982014-01-06 23:29:20 +01003678 if (err < 0)
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003679 goto out_free_radios;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003680 }
3681
Tom Gundersenc835a672014-07-14 16:37:24 +02003682 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3683 hwsim_mon_setup);
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003684 if (hwsim_mon == NULL) {
3685 err = -ENOMEM;
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003686 goto out_free_radios;
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003687 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003688
Javier Lopez78825132011-06-01 11:26:13 +02003689 rtnl_lock();
Javier Lopez78825132011-06-01 11:26:13 +02003690 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003691 if (err < 0) {
3692 rtnl_unlock();
3693 goto out_free_radios;
3694 }
Javier Lopez78825132011-06-01 11:26:13 +02003695
3696 err = register_netdevice(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003697 if (err < 0) {
3698 rtnl_unlock();
3699 goto out_free_mon;
3700 }
Javier Lopez78825132011-06-01 11:26:13 +02003701 rtnl_unlock();
3702
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003703 return 0;
3704
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003705out_free_mon:
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003706 free_netdev(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003707out_free_radios:
Ian Schram3a33cc12008-07-21 13:19:35 -07003708 mac80211_hwsim_free();
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003709out_unregister_driver:
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003710 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003711out_unregister_pernet:
3712 unregister_pernet_device(&hwsim_net_ops);
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003713out_free_rht:
3714 rhashtable_destroy(&hwsim_radios_rht);
3715out_free_wq:
3716 destroy_workqueue(hwsim_wq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003717 return err;
3718}
Johannes Bergf8fffc72012-07-27 15:16:02 +02003719module_init(init_mac80211_hwsim);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003720
3721static void __exit exit_mac80211_hwsim(void)
3722{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003723 pr_debug("mac80211_hwsim: unregister radios\n");
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003724
Javier Lopez78825132011-06-01 11:26:13 +02003725 hwsim_exit_netlink();
3726
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003727 mac80211_hwsim_free();
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003728 flush_workqueue(hwsim_wq);
3729
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003730 rhashtable_destroy(&hwsim_radios_rht);
Johannes Berg5d416352009-07-13 13:04:30 +02003731 unregister_netdev(hwsim_mon);
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003732 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003733 unregister_pernet_device(&hwsim_net_ops);
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003734 destroy_workqueue(hwsim_wq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003735}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003736module_exit(exit_mac80211_hwsim);