blob: 60ca13e0f15b7b02aca1e425f8a971555ac172e3 [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>
Ilan Peer518ea3c2018-07-06 15:29:05 +03005 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
Shaul Triebitzadd74532018-09-05 08:06:08 +03006 * Copyright (C) 2018 Intel Corporation
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
14 * TODO:
Johannes Berg2b2d7792010-08-17 12:08:07 +020015 * - Add TSF sync and fix IBSS beacon transmission by adding
16 * competition for "air time" at TBTT
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030017 * - RX filtering based on filter configuration (data->rx_filter)
18 */
19
Johannes Berg0e057d732008-09-12 00:39:22 +020020#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Johannes Berg0e057d732008-09-12 00:39:22 +020022#include <linux/spinlock.h>
Johannes Berg90e30122009-06-18 14:51:12 +020023#include <net/dst.h>
24#include <net/xfrm.h>
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030025#include <net/mac80211.h>
26#include <net/ieee80211_radiotap.h>
27#include <linux/if_arp.h>
28#include <linux/rtnetlink.h>
29#include <linux/etherdevice.h>
Martin Pitt9ea927742013-04-08 11:30:01 +020030#include <linux/platform_device.h>
Jouni Malinenfc6971d2008-10-30 19:59:05 +020031#include <linux/debugfs.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040032#include <linux/module.h>
Javier Cardona2f40b942012-03-05 17:20:37 -080033#include <linux/ktime.h>
Javier Lopez78825132011-06-01 11:26:13 +020034#include <net/genetlink.h>
Martin Willi100cb9f2016-05-09 18:33:59 +020035#include <net/net_namespace.h>
36#include <net/netns/generic.h>
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +010037#include <linux/rhashtable.h>
Jinbum Park3a2af7c2018-07-31 23:10:40 +090038#include <linux/nospec.h>
Javier Lopez78825132011-06-01 11:26:13 +020039#include "mac80211_hwsim.h"
40
41#define WARN_QUEUE 100
42#define MAX_QUEUE 200
Jouni Malinenacc1e7a2008-06-11 10:42:31 +030043
44MODULE_AUTHOR("Jouni Malinen");
45MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
46MODULE_LICENSE("GPL");
47
48static int radios = 2;
49module_param(radios, int, 0444);
50MODULE_PARM_DESC(radios, "Number of simulated radios");
51
Johannes Berge8261172012-07-27 19:48:26 +020052static int channels = 1;
53module_param(channels, int, 0444);
54MODULE_PARM_DESC(channels, "Number of concurrent channels");
Johannes Berg69068032010-02-03 10:47:55 +010055
Johannes Berga357d7f2012-12-10 11:57:42 +010056static bool paged_rx = false;
57module_param(paged_rx, bool, 0644);
58MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
59
Karl Beldan1eb32172013-04-19 14:44:52 +020060static bool rctbl = false;
61module_param(rctbl, bool, 0444);
62MODULE_PARM_DESC(rctbl, "Handle rate control table");
63
Johannes Berg8c66a3d2014-01-10 20:07:49 +010064static bool support_p2p_device = true;
65module_param(support_p2p_device, bool, 0444);
66MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
67
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040068/**
69 * enum hwsim_regtest - the type of regulatory tests we offer
70 *
71 * These are the different values you can use for the regtest
72 * module parameter. This is useful to help test world roaming
73 * and the driver regulatory_hint() call and combinations of these.
74 * If you want to do specific alpha2 regulatory domain tests simply
75 * use the userspace regulatory request as that will be respected as
76 * well without the need of this module parameter. This is designed
77 * only for testing the driver regulatory request, world roaming
78 * and all possible combinations.
79 *
80 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
81 * this is the default value.
82 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
83 * hint, only one driver regulatory hint will be sent as such the
84 * secondary radios are expected to follow.
85 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
86 * request with all radios reporting the same regulatory domain.
87 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
88 * different regulatory domains requests. Expected behaviour is for
89 * an intersection to occur but each device will still use their
90 * respective regulatory requested domains. Subsequent radios will
91 * use the resulting intersection.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030092 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -040093 * this by using a custom beacon-capable regulatory domain for the first
94 * radio. All other device world roam.
95 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
96 * domain requests. All radios will adhere to this custom world regulatory
97 * domain.
98 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
99 * domain requests. The first radio will adhere to the first custom world
100 * regulatory domain, the second one to the second custom world regulatory
101 * domain. All other devices will world roam.
102 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
103 * settings, only the first radio will send a regulatory domain request
104 * and use strict settings. The rest of the radios are expected to follow.
105 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
106 * settings. All radios will adhere to this.
107 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
108 * domain settings, combined with secondary driver regulatory domain
109 * settings. The first radio will get a strict regulatory domain setting
110 * using the first driver regulatory request and the second radio will use
111 * non-strict settings using the second driver regulatory request. All
112 * other devices should follow the intersection created between the
113 * first two.
114 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
115 * at least 6 radios for a complete test. We will test in this order:
116 * 1 - driver custom world regulatory domain
117 * 2 - second custom world regulatory domain
118 * 3 - first driver regulatory domain request
119 * 4 - second driver regulatory domain request
120 * 5 - strict regulatory domain settings using the third driver regulatory
121 * domain request
122 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
123 * regulatory requests.
124 */
125enum hwsim_regtest {
126 HWSIM_REGTEST_DISABLED = 0,
127 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
128 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
129 HWSIM_REGTEST_DIFF_COUNTRY = 3,
130 HWSIM_REGTEST_WORLD_ROAM = 4,
131 HWSIM_REGTEST_CUSTOM_WORLD = 5,
132 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
133 HWSIM_REGTEST_STRICT_FOLLOW = 7,
134 HWSIM_REGTEST_STRICT_ALL = 8,
135 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
136 HWSIM_REGTEST_ALL = 10,
137};
138
139/* Set to one of the HWSIM_REGTEST_* values above */
140static int regtest = HWSIM_REGTEST_DISABLED;
141module_param(regtest, int, 0444);
142MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
143
144static const char *hwsim_alpha2s[] = {
145 "FI",
146 "AL",
147 "US",
148 "DE",
149 "JP",
150 "AL",
151};
152
153static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
154 .n_reg_rules = 4,
155 .alpha2 = "99",
156 .reg_rules = {
157 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
158 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
159 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
160 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
161 }
162};
163
164static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
165 .n_reg_rules = 2,
166 .alpha2 = "99",
167 .reg_rules = {
168 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
169 REG_RULE(5725-10, 5850+10, 40, 0, 30,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200170 NL80211_RRF_NO_IR),
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -0400171 }
172};
173
Johannes Berg26b0e412014-01-10 13:37:38 +0100174static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
175 &hwsim_world_regdom_custom_01,
176 &hwsim_world_regdom_custom_02,
177};
178
Johannes Berg8aa21e62008-09-11 02:16:36 +0200179struct hwsim_vif_priv {
180 u32 magic;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200181 u8 bssid[ETH_ALEN];
182 bool assoc;
Jouni Malinen0bb861e2013-10-22 14:11:17 +0300183 bool bcn_en;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200184 u16 aid;
Johannes Berg8aa21e62008-09-11 02:16:36 +0200185};
186
187#define HWSIM_VIF_MAGIC 0x69537748
188
189static inline void hwsim_check_magic(struct ieee80211_vif *vif)
190{
191 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berge8261172012-07-27 19:48:26 +0200192 WARN(vp->magic != HWSIM_VIF_MAGIC,
193 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
194 vif, vp->magic, vif->addr, vif->type, vif->p2p);
Johannes Berg8aa21e62008-09-11 02:16:36 +0200195}
196
197static inline void hwsim_set_magic(struct ieee80211_vif *vif)
198{
199 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
200 vp->magic = HWSIM_VIF_MAGIC;
201}
202
203static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
204{
205 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
206 vp->magic = 0;
207}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300208
Johannes Berg81c06522008-09-11 02:17:01 +0200209struct hwsim_sta_priv {
210 u32 magic;
211};
212
Johannes Berge8261172012-07-27 19:48:26 +0200213#define HWSIM_STA_MAGIC 0x6d537749
Johannes Berg81c06522008-09-11 02:17:01 +0200214
215static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
216{
217 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200218 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
Johannes Berg81c06522008-09-11 02:17:01 +0200219}
220
221static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
222{
223 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
Rami Rosen5d6924e2008-10-08 11:18:27 +0200224 sp->magic = HWSIM_STA_MAGIC;
Johannes Berg81c06522008-09-11 02:17:01 +0200225}
226
227static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
228{
229 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
230 sp->magic = 0;
231}
232
Johannes Berge8261172012-07-27 19:48:26 +0200233struct hwsim_chanctx_priv {
234 u32 magic;
235};
236
237#define HWSIM_CHANCTX_MAGIC 0x6d53774a
238
239static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
240{
241 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
242 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
243}
244
245static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
246{
247 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
248 cp->magic = HWSIM_CHANCTX_MAGIC;
249}
250
251static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
252{
253 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
254 cp->magic = 0;
255}
256
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300257static unsigned int hwsim_net_id;
Martin Willi100cb9f2016-05-09 18:33:59 +0200258
Matthew Wilcox60a05272018-04-10 16:36:33 -0700259static DEFINE_IDA(hwsim_netgroup_ida);
Martin Willi100cb9f2016-05-09 18:33:59 +0200260
261struct hwsim_net {
262 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200263 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200264};
265
266static inline int hwsim_net_get_netgroup(struct net *net)
267{
268 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
269
270 return hwsim_net->netgroup;
271}
272
Kirill Tkhai03695542018-03-01 14:30:09 +0300273static inline int hwsim_net_set_netgroup(struct net *net)
Martin Willi100cb9f2016-05-09 18:33:59 +0200274{
275 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
276
Kirill Tkhai03695542018-03-01 14:30:09 +0300277 hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
278 0, 0, GFP_KERNEL);
279 return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
Martin Willi100cb9f2016-05-09 18:33:59 +0200280}
281
Martin Willif21e4d82016-05-14 10:34:44 +0200282static inline u32 hwsim_net_get_wmediumd(struct net *net)
283{
284 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
285
286 return hwsim_net->wmediumd;
287}
288
289static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
290{
291 struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
292
293 hwsim_net->wmediumd = portid;
294}
295
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300296static struct class *hwsim_class;
297
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300298static struct net_device *hwsim_mon; /* global monitor netdev */
299
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800300#define CHAN2G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200301 .band = NL80211_BAND_2GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800302 .center_freq = (_freq), \
303 .hw_value = (_freq), \
304 .max_power = 20, \
305}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300306
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800307#define CHAN5G(_freq) { \
Johannes Berg57fbcce2016-04-12 15:56:15 +0200308 .band = NL80211_BAND_5GHZ, \
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800309 .center_freq = (_freq), \
310 .hw_value = (_freq), \
311 .max_power = 20, \
312}
313
314static const struct ieee80211_channel hwsim_channels_2ghz[] = {
315 CHAN2G(2412), /* Channel 1 */
316 CHAN2G(2417), /* Channel 2 */
317 CHAN2G(2422), /* Channel 3 */
318 CHAN2G(2427), /* Channel 4 */
319 CHAN2G(2432), /* Channel 5 */
320 CHAN2G(2437), /* Channel 6 */
321 CHAN2G(2442), /* Channel 7 */
322 CHAN2G(2447), /* Channel 8 */
323 CHAN2G(2452), /* Channel 9 */
324 CHAN2G(2457), /* Channel 10 */
325 CHAN2G(2462), /* Channel 11 */
326 CHAN2G(2467), /* Channel 12 */
327 CHAN2G(2472), /* Channel 13 */
328 CHAN2G(2484), /* Channel 14 */
329};
330
331static const struct ieee80211_channel hwsim_channels_5ghz[] = {
332 CHAN5G(5180), /* Channel 36 */
333 CHAN5G(5200), /* Channel 40 */
334 CHAN5G(5220), /* Channel 44 */
335 CHAN5G(5240), /* Channel 48 */
336
337 CHAN5G(5260), /* Channel 52 */
338 CHAN5G(5280), /* Channel 56 */
339 CHAN5G(5300), /* Channel 60 */
340 CHAN5G(5320), /* Channel 64 */
341
342 CHAN5G(5500), /* Channel 100 */
343 CHAN5G(5520), /* Channel 104 */
344 CHAN5G(5540), /* Channel 108 */
345 CHAN5G(5560), /* Channel 112 */
346 CHAN5G(5580), /* Channel 116 */
347 CHAN5G(5600), /* Channel 120 */
348 CHAN5G(5620), /* Channel 124 */
349 CHAN5G(5640), /* Channel 128 */
350 CHAN5G(5660), /* Channel 132 */
351 CHAN5G(5680), /* Channel 136 */
352 CHAN5G(5700), /* Channel 140 */
353
354 CHAN5G(5745), /* Channel 149 */
355 CHAN5G(5765), /* Channel 153 */
356 CHAN5G(5785), /* Channel 157 */
357 CHAN5G(5805), /* Channel 161 */
358 CHAN5G(5825), /* Channel 165 */
Jouni Malinen85bbd802017-03-06 11:56:06 +0200359 CHAN5G(5845), /* Channel 169 */
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300360};
361
362static const struct ieee80211_rate hwsim_rates[] = {
363 { .bitrate = 10 },
364 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
365 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
366 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
367 { .bitrate = 60 },
368 { .bitrate = 90 },
369 { .bitrate = 120 },
370 { .bitrate = 180 },
371 { .bitrate = 240 },
372 { .bitrate = 360 },
373 { .bitrate = 480 },
374 { .bitrate = 540 }
375};
376
James Prestwoodc0a01892018-10-17 14:16:24 -0700377static const u32 hwsim_ciphers[] = {
378 WLAN_CIPHER_SUITE_WEP40,
379 WLAN_CIPHER_SUITE_WEP104,
380 WLAN_CIPHER_SUITE_TKIP,
381 WLAN_CIPHER_SUITE_CCMP,
382 WLAN_CIPHER_SUITE_CCMP_256,
383 WLAN_CIPHER_SUITE_GCMP,
384 WLAN_CIPHER_SUITE_GCMP_256,
385 WLAN_CIPHER_SUITE_AES_CMAC,
386 WLAN_CIPHER_SUITE_BIP_CMAC_256,
387 WLAN_CIPHER_SUITE_BIP_GMAC_128,
388 WLAN_CIPHER_SUITE_BIP_GMAC_256,
389};
390
Jouni Malinend5d011b2015-02-26 15:26:54 +0200391#define OUI_QCA 0x001374
392#define QCA_NL80211_SUBCMD_TEST 1
393enum qca_nl80211_vendor_subcmds {
394 QCA_WLAN_VENDOR_ATTR_TEST = 8,
395 QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
396};
397
398static const struct nla_policy
399hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
400 [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
401};
402
403static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
404 struct wireless_dev *wdev,
405 const void *data, int data_len)
406{
407 struct sk_buff *skb;
408 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
409 int err;
410 u32 val;
411
Johannes Berg8cb08172019-04-26 14:07:28 +0200412 err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
413 data_len, hwsim_vendor_test_policy, NULL);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200414 if (err)
415 return err;
416 if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
417 return -EINVAL;
418 val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200419 wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
Jouni Malinend5d011b2015-02-26 15:26:54 +0200420
421 /* Send a vendor event as a test. Note that this would not normally be
422 * done within a command handler, but rather, based on some other
423 * trigger. For simplicity, this command is used to trigger the event
424 * here.
425 *
426 * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
427 */
428 skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
429 if (skb) {
430 /* skb_put() or nla_put() will fill up data within
431 * NL80211_ATTR_VENDOR_DATA.
432 */
433
434 /* Add vendor data */
435 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
436
437 /* Send the event - this will call nla_nest_end() */
438 cfg80211_vendor_event(skb, GFP_KERNEL);
439 }
440
441 /* Send a response to the command */
442 skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
443 if (!skb)
444 return -ENOMEM;
445
446 /* skb_put() or nla_put() will fill up data within
447 * NL80211_ATTR_VENDOR_DATA
448 */
449 nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
450
451 return cfg80211_vendor_cmd_reply(skb);
452}
453
454static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
455 {
456 .info = { .vendor_id = OUI_QCA,
457 .subcmd = QCA_NL80211_SUBCMD_TEST },
458 .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
459 .doit = mac80211_hwsim_vendor_cmd_test,
460 }
461};
462
463/* Advertise support vendor specific events */
464static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
465 { .vendor_id = OUI_QCA, .subcmd = 1 },
466};
467
Johannes Berg0e057d732008-09-12 00:39:22 +0200468static spinlock_t hwsim_radio_lock;
Johannes Bergb59abfb2016-09-15 16:30:03 +0300469static LIST_HEAD(hwsim_radios);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100470static struct rhashtable hwsim_radios_rht;
Johannes Berg2d689922014-01-06 22:56:30 +0100471static int hwsim_radio_idx;
Benjamin Beichler63356982018-01-22 18:04:37 +0100472static int hwsim_radios_generation = 1;
Johannes Berg0e057d732008-09-12 00:39:22 +0200473
Johannes Bergde0421d2014-01-06 23:12:12 +0100474static struct platform_driver mac80211_hwsim_driver = {
475 .driver = {
476 .name = "mac80211_hwsim",
Johannes Bergde0421d2014-01-06 23:12:12 +0100477 },
478};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300479
480struct mac80211_hwsim_data {
Johannes Berg0e057d732008-09-12 00:39:22 +0200481 struct list_head list;
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100482 struct rhash_head rht;
Johannes Berg0e057d732008-09-12 00:39:22 +0200483 struct ieee80211_hw *hw;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300484 struct device *dev;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200485 struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
Luis R. Rodriguez22cad732009-03-05 21:13:06 -0800486 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
487 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300488 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
Johannes Berg38ed5042014-01-06 22:11:06 +0100489 struct ieee80211_iface_combination if_combination;
James Prestwood99e3a44b2018-10-17 12:33:06 -0700490 struct ieee80211_iface_limit if_limits[3];
491 int n_if_limits;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300492
James Prestwoodc0a01892018-10-17 14:16:24 -0700493 u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300494
Johannes Bergef15aac2010-01-20 12:02:33 +0100495 struct mac_address addresses[2];
Johannes Bergbc7910982014-01-06 23:29:20 +0100496 int channels, idx;
Luciano Coelho361c3e02014-02-10 15:23:03 +0200497 bool use_chanctx;
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300498 bool destroy_on_close;
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300499 u32 portid;
Patrik Flykt93d638d2014-11-12 16:42:40 +0200500 char alpha2[2];
501 const struct ieee80211_regdomain *regd;
Johannes Bergef15aac2010-01-20 12:02:33 +0100502
Johannes Berge8261172012-07-27 19:48:26 +0200503 struct ieee80211_channel *tmp_chan;
Johannes Berg661ef472015-11-24 18:29:45 +0100504 struct ieee80211_channel *roc_chan;
505 u32 roc_duration;
506 struct delayed_work roc_start;
Johannes Berge8261172012-07-27 19:48:26 +0200507 struct delayed_work roc_done;
508 struct delayed_work hw_scan;
509 struct cfg80211_scan_request *hw_scan_request;
510 struct ieee80211_vif *hw_scan_vif;
511 int scan_chan_idx;
Johannes Berg339467b2014-06-12 22:41:00 +0200512 u8 scan_addr[ETH_ALEN];
Johannes Berg7f813ce2013-11-06 10:37:34 +0100513 struct {
514 struct ieee80211_channel *channel;
515 unsigned long next_start, start, end;
516 } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
517 ARRAY_SIZE(hwsim_channels_5ghz)];
Johannes Berge8261172012-07-27 19:48:26 +0200518
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300519 struct ieee80211_channel *channel;
Thomas Pedersen01e59e42013-01-02 14:55:17 -0800520 u64 beacon_int /* beacon interval in us */;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300521 unsigned int rx_filter;
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -0400522 bool started, idle, scanning;
523 struct mutex mutex;
Thomas Gleixner77dcc622019-03-01 23:48:19 +0100524 struct hrtimer beacon_timer;
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200525 enum ps_mode {
526 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
527 } ps;
528 bool ps_poll_pending;
529 struct dentry *debugfs;
Daniel Wagner73606d02009-05-18 19:49:25 +0200530
Johannes Berg959eb2f2015-11-06 11:57:23 +0100531 uintptr_t pending_cookie;
Javier Lopez78825132011-06-01 11:26:13 +0200532 struct sk_buff_head pending; /* packets pending */
Daniel Wagner73606d02009-05-18 19:49:25 +0200533 /*
534 * Only radios in the same group can communicate together (the
535 * channel has to match too). Each bit represents a group. A
Ben Greear9ebac152014-09-25 14:22:20 -0700536 * radio can be in more than one group.
Daniel Wagner73606d02009-05-18 19:49:25 +0200537 */
538 u64 group;
Blaise Gassendbdd7bd12010-10-28 02:01:24 -0700539
Martin Willi100cb9f2016-05-09 18:33:59 +0200540 /* group shared by radios created in the same netns */
541 int netgroup;
Martin Willif21e4d82016-05-14 10:34:44 +0200542 /* wmediumd portid responsible for netgroup of this radio */
543 u32 wmediumd;
Martin Willi100cb9f2016-05-09 18:33:59 +0200544
Javier Cardona2f40b942012-03-05 17:20:37 -0800545 /* difference between this hw's clock and the real clock, in usecs */
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800546 s64 tsf_offset;
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800547 s64 bcn_delta;
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800548 /* absolute beacon transmission time. Used to cover up "tx" delay. */
549 u64 abs_bcn_ts;
Ben Greear2155c3f2014-10-22 12:22:58 -0700550
551 /* Stats */
552 u64 tx_pkts;
553 u64 rx_pkts;
554 u64 tx_bytes;
555 u64 rx_bytes;
556 u64 tx_dropped;
557 u64 tx_failed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300558};
559
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +0100560static const struct rhashtable_params hwsim_rht_params = {
561 .nelem_hint = 2,
562 .automatic_shrinking = true,
563 .key_len = ETH_ALEN,
564 .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
565 .head_offset = offsetof(struct mac80211_hwsim_data, rht),
566};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300567
568struct hwsim_radiotap_hdr {
569 struct ieee80211_radiotap_header hdr;
Javier Cardona2f40b942012-03-05 17:20:37 -0800570 __le64 rt_tsft;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300571 u8 rt_flags;
572 u8 rt_rate;
573 __le16 rt_channel;
574 __le16 rt_chbitmask;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000575} __packed;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300576
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200577struct hwsim_radiotap_ack_hdr {
578 struct ieee80211_radiotap_header hdr;
579 u8 rt_flags;
580 u8 pad;
581 __le16 rt_channel;
582 __le16 rt_chbitmask;
583} __packed;
584
Johannes Berg489111e2016-10-24 14:40:03 +0200585/* MAC80211_HWSIM netlink family */
586static struct genl_family hwsim_genl_family;
Javier Lopez78825132011-06-01 11:26:13 +0200587
Jukka Rissanen62759362014-10-31 14:48:47 +0200588enum hwsim_multicast_groups {
589 HWSIM_MCGRP_CONFIG,
590};
591
592static const struct genl_multicast_group hwsim_mcgrps[] = {
593 [HWSIM_MCGRP_CONFIG] = { .name = "config", },
594};
595
Javier Lopez78825132011-06-01 11:26:13 +0200596/* MAC80211_HWSIM netlink policy */
597
Johannes Berg205d2422014-01-21 00:06:29 +0100598static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100599 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
600 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
Javier Lopez78825132011-06-01 11:26:13 +0200601 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
602 .len = IEEE80211_MAX_DATA_LEN },
603 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
604 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
605 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
606 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
Johannes Berg9ddd12a2014-01-06 23:03:02 +0100607 .len = IEEE80211_TX_MAX_RATES *
608 sizeof(struct hwsim_tx_rate)},
Javier Lopez78825132011-06-01 11:26:13 +0200609 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
Johannes Bergbc7910982014-01-06 23:29:20 +0100610 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
611 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
Johannes Berg26b0e412014-01-10 13:37:38 +0100612 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
613 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
614 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
Johannes Berg8c66a3d2014-01-10 20:07:49 +0100615 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
Jukka Rissanene9ed49b2014-10-09 15:27:51 +0300616 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
Johannes Bergde29eda2014-10-31 09:25:43 +0100617 [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
618 [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
619 [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +0100620 [HWSIM_ATTR_PERM_ADDR] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
James Prestwood99e3a44b2018-10-17 12:33:06 -0700621 [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
James Prestwoodc0a01892018-10-17 14:16:24 -0700622 [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
Javier Lopez78825132011-06-01 11:26:13 +0200623};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300624
Johannes Bergde0421d2014-01-06 23:12:12 +0100625static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
626 struct sk_buff *skb,
627 struct ieee80211_channel *chan);
628
629/* sysfs attributes */
630static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
631{
632 struct mac80211_hwsim_data *data = dat;
633 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
634 struct sk_buff *skb;
635 struct ieee80211_pspoll *pspoll;
636
637 if (!vp->assoc)
638 return;
639
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200640 wiphy_dbg(data->hw->wiphy,
641 "%s: send PS-Poll to %pM for aid %d\n",
642 __func__, vp->bssid, vp->aid);
Johannes Bergde0421d2014-01-06 23:12:12 +0100643
644 skb = dev_alloc_skb(sizeof(*pspoll));
645 if (!skb)
646 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200647 pspoll = skb_put(skb, sizeof(*pspoll));
Johannes Bergde0421d2014-01-06 23:12:12 +0100648 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
649 IEEE80211_STYPE_PSPOLL |
650 IEEE80211_FCTL_PM);
651 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
652 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
653 memcpy(pspoll->ta, mac, ETH_ALEN);
654
655 rcu_read_lock();
656 mac80211_hwsim_tx_frame(data->hw, skb,
657 rcu_dereference(vif->chanctx_conf)->def.chan);
658 rcu_read_unlock();
659}
660
661static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
662 struct ieee80211_vif *vif, int ps)
663{
664 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
665 struct sk_buff *skb;
666 struct ieee80211_hdr *hdr;
667
668 if (!vp->assoc)
669 return;
670
Lubomir Rintel62b093b2017-09-18 15:56:51 +0200671 wiphy_dbg(data->hw->wiphy,
672 "%s: send data::nullfunc to %pM ps=%d\n",
673 __func__, vp->bssid, ps);
Johannes Bergde0421d2014-01-06 23:12:12 +0100674
675 skb = dev_alloc_skb(sizeof(*hdr));
676 if (!skb)
677 return;
Johannes Berg4df864c2017-06-16 14:29:21 +0200678 hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
Johannes Bergde0421d2014-01-06 23:12:12 +0100679 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
680 IEEE80211_STYPE_NULLFUNC |
Adiel Alonib65c7b82017-12-18 12:14:04 +0200681 IEEE80211_FCTL_TODS |
Johannes Bergde0421d2014-01-06 23:12:12 +0100682 (ps ? IEEE80211_FCTL_PM : 0));
683 hdr->duration_id = cpu_to_le16(0);
684 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
685 memcpy(hdr->addr2, mac, ETH_ALEN);
686 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
687
688 rcu_read_lock();
689 mac80211_hwsim_tx_frame(data->hw, skb,
690 rcu_dereference(vif->chanctx_conf)->def.chan);
691 rcu_read_unlock();
692}
693
694
695static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
696 struct ieee80211_vif *vif)
697{
698 struct mac80211_hwsim_data *data = dat;
699 hwsim_send_nullfunc(data, mac, vif, 1);
700}
701
702static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
703 struct ieee80211_vif *vif)
704{
705 struct mac80211_hwsim_data *data = dat;
706 hwsim_send_nullfunc(data, mac, vif, 0);
707}
708
709static int hwsim_fops_ps_read(void *dat, u64 *val)
710{
711 struct mac80211_hwsim_data *data = dat;
712 *val = data->ps;
713 return 0;
714}
715
716static int hwsim_fops_ps_write(void *dat, u64 val)
717{
718 struct mac80211_hwsim_data *data = dat;
719 enum ps_mode old_ps;
720
721 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
722 val != PS_MANUAL_POLL)
723 return -EINVAL;
724
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200725 if (val == PS_MANUAL_POLL) {
726 if (data->ps != PS_ENABLED)
727 return -EINVAL;
728 local_bh_disable();
729 ieee80211_iterate_active_interfaces_atomic(
730 data->hw, IEEE80211_IFACE_ITER_NORMAL,
731 hwsim_send_ps_poll, data);
732 local_bh_enable();
733 return 0;
734 }
Johannes Bergde0421d2014-01-06 23:12:12 +0100735 old_ps = data->ps;
736 data->ps = val;
737
Johannes Berga809ca52015-01-09 11:29:50 +0100738 local_bh_disable();
Adiel Alonie16ea4b2017-12-01 13:50:53 +0200739 if (old_ps == PS_DISABLED && val != PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100740 ieee80211_iterate_active_interfaces_atomic(
741 data->hw, IEEE80211_IFACE_ITER_NORMAL,
742 hwsim_send_nullfunc_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100743 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
Johannes Berga809ca52015-01-09 11:29:50 +0100744 ieee80211_iterate_active_interfaces_atomic(
745 data->hw, IEEE80211_IFACE_ITER_NORMAL,
746 hwsim_send_nullfunc_no_ps, data);
Johannes Bergde0421d2014-01-06 23:12:12 +0100747 }
Johannes Berga809ca52015-01-09 11:29:50 +0100748 local_bh_enable();
Johannes Bergde0421d2014-01-06 23:12:12 +0100749
750 return 0;
751}
752
753DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
754 "%llu\n");
755
756static int hwsim_write_simulate_radar(void *dat, u64 val)
757{
758 struct mac80211_hwsim_data *data = dat;
759
760 ieee80211_radar_detected(data->hw);
761
762 return 0;
763}
764
765DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
766 hwsim_write_simulate_radar, "%llu\n");
767
768static int hwsim_fops_group_read(void *dat, u64 *val)
769{
770 struct mac80211_hwsim_data *data = dat;
771 *val = data->group;
772 return 0;
773}
774
775static int hwsim_fops_group_write(void *dat, u64 val)
776{
777 struct mac80211_hwsim_data *data = dat;
778 data->group = val;
779 return 0;
780}
781
782DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
783 hwsim_fops_group_read, hwsim_fops_group_write,
784 "%llx\n");
785
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000786static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
787 struct net_device *dev)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300788{
789 /* TODO: allow packet injection */
790 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000791 return NETDEV_TX_OK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300792}
793
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800794static inline u64 mac80211_hwsim_get_tsf_raw(void)
795{
796 return ktime_to_us(ktime_get_real());
797}
798
Javier Cardona2f40b942012-03-05 17:20:37 -0800799static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
800{
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800801 u64 now = mac80211_hwsim_get_tsf_raw();
Javier Cardona2f40b942012-03-05 17:20:37 -0800802 return cpu_to_le64(now + data->tsf_offset);
803}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300804
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800805static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
Thomas Pedersenb66851c2013-01-07 14:48:07 -0800806 struct ieee80211_vif *vif)
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800807{
808 struct mac80211_hwsim_data *data = hw->priv;
809 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
810}
811
812static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
813 struct ieee80211_vif *vif, u64 tsf)
814{
815 struct mac80211_hwsim_data *data = hw->priv;
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800816 u64 now = mac80211_hwsim_get_tsf(hw, vif);
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800817 u32 bcn_int = data->beacon_int;
Andrew Morton79211c82015-11-09 14:58:13 -0800818 u64 delta = abs(tsf - now);
Thomas Pedersen45034bf2013-01-02 14:55:16 -0800819
Thomas Pedersenc51f8782013-01-02 14:55:18 -0800820 /* adjust after beaconing with new timestamp at old TBTT */
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700821 if (tsf > now) {
822 data->tsf_offset += delta;
823 data->bcn_delta = do_div(delta, bcn_int);
824 } else {
825 data->tsf_offset -= delta;
Benjamin Beichler4fb7f8a2016-11-11 17:37:56 +0100826 data->bcn_delta = -(s64)do_div(delta, bcn_int);
Andrew Bresticker5d26b502014-07-22 14:43:51 -0700827 }
Javier Cardona12ce8ba2012-03-05 17:20:38 -0800828}
829
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300830static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +0200831 struct sk_buff *tx_skb,
832 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300833{
834 struct mac80211_hwsim_data *data = hw->priv;
835 struct sk_buff *skb;
836 struct hwsim_radiotap_hdr *hdr;
837 u16 flags;
838 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
839 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
840
Amit Khatri89f44d82015-11-30 12:46:52 +0530841 if (WARN_ON(!txrate))
842 return;
843
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300844 if (!netif_running(hwsim_mon))
845 return;
846
847 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
848 if (skb == NULL)
849 return;
850
Johannes Bergd58ff352017-06-16 14:29:23 +0200851 hdr = skb_push(skb, sizeof(*hdr));
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300852 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
853 hdr->hdr.it_pad = 0;
854 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
Jouni Malinenf248f102008-06-13 19:44:47 +0300855 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
856 (1 << IEEE80211_RADIOTAP_RATE) |
Javier Cardona2f40b942012-03-05 17:20:37 -0800857 (1 << IEEE80211_RADIOTAP_TSFT) |
Jouni Malinenf248f102008-06-13 19:44:47 +0300858 (1 << IEEE80211_RADIOTAP_CHANNEL));
Javier Cardona2f40b942012-03-05 17:20:37 -0800859 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300860 hdr->rt_flags = 0;
861 hdr->rt_rate = txrate->bitrate / 5;
Johannes Berge8261172012-07-27 19:48:26 +0200862 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300863 flags = IEEE80211_CHAN_2GHZ;
864 if (txrate->flags & IEEE80211_RATE_ERP_G)
865 flags |= IEEE80211_CHAN_OFDM;
866 else
867 flags |= IEEE80211_CHAN_CCK;
868 hdr->rt_chbitmask = cpu_to_le16(flags);
869
870 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000871 skb_reset_mac_header(skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300872 skb->ip_summed = CHECKSUM_UNNECESSARY;
873 skb->pkt_type = PACKET_OTHERHOST;
Jouni Malinenf248f102008-06-13 19:44:47 +0300874 skb->protocol = htons(ETH_P_802_2);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +0300875 memset(skb->cb, 0, sizeof(skb->cb));
876 netif_rx(skb);
877}
878
879
Johannes Berge8261172012-07-27 19:48:26 +0200880static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
881 const u8 *addr)
Jouni Malinen6c085222009-11-01 11:31:45 +0200882{
Jouni Malinen6c085222009-11-01 11:31:45 +0200883 struct sk_buff *skb;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200884 struct hwsim_radiotap_ack_hdr *hdr;
Jouni Malinen6c085222009-11-01 11:31:45 +0200885 u16 flags;
886 struct ieee80211_hdr *hdr11;
887
888 if (!netif_running(hwsim_mon))
889 return;
890
891 skb = dev_alloc_skb(100);
892 if (skb == NULL)
893 return;
894
Johannes Berg4df864c2017-06-16 14:29:21 +0200895 hdr = skb_put(skb, sizeof(*hdr));
Jouni Malinen6c085222009-11-01 11:31:45 +0200896 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
897 hdr->hdr.it_pad = 0;
898 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
899 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
900 (1 << IEEE80211_RADIOTAP_CHANNEL));
901 hdr->rt_flags = 0;
Jouni Malinen76a56eb2013-11-02 15:57:36 +0200902 hdr->pad = 0;
Johannes Berge8261172012-07-27 19:48:26 +0200903 hdr->rt_channel = cpu_to_le16(chan->center_freq);
Jouni Malinen6c085222009-11-01 11:31:45 +0200904 flags = IEEE80211_CHAN_2GHZ;
905 hdr->rt_chbitmask = cpu_to_le16(flags);
906
Johannes Berg4df864c2017-06-16 14:29:21 +0200907 hdr11 = skb_put(skb, 10);
Jouni Malinen6c085222009-11-01 11:31:45 +0200908 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
909 IEEE80211_STYPE_ACK);
910 hdr11->duration_id = cpu_to_le16(0);
911 memcpy(hdr11->addr1, addr, ETH_ALEN);
912
913 skb->dev = hwsim_mon;
Zhang Shengju6b163a82016-03-03 01:16:55 +0000914 skb_reset_mac_header(skb);
Jouni Malinen6c085222009-11-01 11:31:45 +0200915 skb->ip_summed = CHECKSUM_UNNECESSARY;
916 skb->pkt_type = PACKET_OTHERHOST;
917 skb->protocol = htons(ETH_P_802_2);
918 memset(skb->cb, 0, sizeof(skb->cb));
919 netif_rx(skb);
920}
921
Johannes Berg3283e2862014-06-12 22:34:49 +0200922struct mac80211_hwsim_addr_match_data {
923 u8 addr[ETH_ALEN];
924 bool ret;
925};
926
927static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
928 struct ieee80211_vif *vif)
929{
930 struct mac80211_hwsim_addr_match_data *md = data;
931
932 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
933 md->ret = true;
934}
935
936static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
937 const u8 *addr)
938{
939 struct mac80211_hwsim_addr_match_data md = {
940 .ret = false,
941 };
942
Johannes Berg339467b2014-06-12 22:41:00 +0200943 if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
944 return true;
945
Johannes Berg3283e2862014-06-12 22:34:49 +0200946 memcpy(md.addr, addr, ETH_ALEN);
947
948 ieee80211_iterate_active_interfaces_atomic(data->hw,
949 IEEE80211_IFACE_ITER_NORMAL,
950 mac80211_hwsim_addr_iter,
951 &md);
952
953 return md.ret;
954}
Jouni Malinen6c085222009-11-01 11:31:45 +0200955
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200956static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
957 struct sk_buff *skb)
958{
959 switch (data->ps) {
960 case PS_DISABLED:
961 return true;
962 case PS_ENABLED:
963 return false;
964 case PS_AUTO_POLL:
965 /* TODO: accept (some) Beacons by default and other frames only
966 * if pending PS-Poll has been sent */
967 return true;
968 case PS_MANUAL_POLL:
969 /* Allow unicast frames to own address if there is a pending
970 * PS-Poll */
971 if (data->ps_poll_pending &&
Johannes Berg3283e2862014-06-12 22:34:49 +0200972 mac80211_hwsim_addr_match(data, skb->data + 4)) {
Jouni Malinenfc6971d2008-10-30 19:59:05 +0200973 data->ps_poll_pending = false;
974 return true;
975 }
976 return false;
977 }
978
979 return true;
980}
981
Martin Willif21e4d82016-05-14 10:34:44 +0200982static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
983 struct sk_buff *skb, int portid)
984{
985 struct net *net;
986 bool found = false;
987 int res = -ENOENT;
988
989 rcu_read_lock();
990 for_each_net_rcu(net) {
991 if (data->netgroup == hwsim_net_get_netgroup(net)) {
992 res = genlmsg_unicast(net, skb, portid);
993 found = true;
994 break;
995 }
996 }
997 rcu_read_unlock();
998
999 if (!found)
1000 nlmsg_free(skb);
1001
1002 return res;
1003}
1004
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001005static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1006{
1007 u16 result = 0;
1008
1009 if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1010 result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1011 if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1012 result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1013 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1014 result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1015 if (rate->flags & IEEE80211_TX_RC_MCS)
1016 result |= MAC80211_HWSIM_TX_RC_MCS;
1017 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1018 result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1019 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1020 result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1021 if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1022 result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1023 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1024 result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1025 if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1026 result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1027 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1028 result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1029 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1030 result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1031
1032 return result;
1033}
1034
Javier Lopez78825132011-06-01 11:26:13 +02001035static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1036 struct sk_buff *my_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001037 int dst_portid)
Javier Lopez78825132011-06-01 11:26:13 +02001038{
1039 struct sk_buff *skb;
1040 struct mac80211_hwsim_data *data = hw->priv;
1041 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1042 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1043 void *msg_head;
1044 unsigned int hwsim_flags = 0;
1045 int i;
1046 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001047 struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
Johannes Berg959eb2f2015-11-06 11:57:23 +01001048 uintptr_t cookie;
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001049
Javier Lopez78825132011-06-01 11:26:13 +02001050 if (data->ps != PS_DISABLED)
1051 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1052 /* If the queue contains MAX_QUEUE skb's drop some */
1053 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1054 /* Droping until WARN_QUEUE level */
Ben Greear2155c3f2014-10-22 12:22:58 -07001055 while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
Ben Greearb7bc9672014-09-25 14:22:18 -07001056 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
Ben Greear2155c3f2014-10-22 12:22:58 -07001057 data->tx_dropped++;
1058 }
Javier Lopez78825132011-06-01 11:26:13 +02001059 }
1060
Thomas Graf58050fc2012-06-28 03:57:45 +00001061 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Javier Lopez78825132011-06-01 11:26:13 +02001062 if (skb == NULL)
1063 goto nla_put_failure;
1064
1065 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1066 HWSIM_CMD_FRAME);
1067 if (msg_head == NULL) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001068 pr_debug("mac80211_hwsim: problem with msg_head\n");
Javier Lopez78825132011-06-01 11:26:13 +02001069 goto nla_put_failure;
1070 }
1071
Bob Copeland354210f2015-12-19 09:59:37 -05001072 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1073 ETH_ALEN, data->addresses[1].addr))
David S. Miller633c9382012-04-01 21:03:30 -04001074 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001075
1076 /* We get the skb->data */
David S. Miller633c9382012-04-01 21:03:30 -04001077 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1078 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001079
1080 /* We get the flags for this transmission, and we translate them to
1081 wmediumd flags */
1082
1083 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1084 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1085
1086 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1087 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1088
David S. Miller633c9382012-04-01 21:03:30 -04001089 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1090 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001091
Ben Greear4f86ed82014-10-27 15:04:46 -07001092 if (nla_put_u32(skb, HWSIM_ATTR_FREQ, data->channel->center_freq))
1093 goto nla_put_failure;
1094
Javier Lopez78825132011-06-01 11:26:13 +02001095 /* We get the tx control (rate and retries) info*/
1096
1097 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1098 tx_attempts[i].idx = info->status.rates[i].idx;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001099 tx_attempts_flags[i].idx = info->status.rates[i].idx;
Javier Lopez78825132011-06-01 11:26:13 +02001100 tx_attempts[i].count = info->status.rates[i].count;
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001101 tx_attempts_flags[i].flags =
1102 trans_tx_rate_flags_ieee2hwsim(
1103 &info->status.rates[i]);
Javier Lopez78825132011-06-01 11:26:13 +02001104 }
1105
David S. Miller633c9382012-04-01 21:03:30 -04001106 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1107 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1108 tx_attempts))
1109 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001110
Benjamin Beichler6e3d6ca12018-01-10 17:42:55 +01001111 if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1112 sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1113 tx_attempts_flags))
1114 goto nla_put_failure;
1115
Javier Lopez78825132011-06-01 11:26:13 +02001116 /* We create a cookie to identify this skb */
Johannes Berg959eb2f2015-11-06 11:57:23 +01001117 data->pending_cookie++;
1118 cookie = data->pending_cookie;
1119 info->rate_driver_data[0] = (void *)cookie;
Nicolas Dichtel08f4cbb2016-04-26 10:06:12 +02001120 if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
David S. Miller633c9382012-04-01 21:03:30 -04001121 goto nla_put_failure;
Javier Lopez78825132011-06-01 11:26:13 +02001122
1123 genlmsg_end(skb, msg_head);
Martin Willif21e4d82016-05-14 10:34:44 +02001124 if (hwsim_unicast_netgroup(data, skb, dst_portid))
Bob Copelandf2831e22015-02-05 08:45:43 -05001125 goto err_free_txskb;
Javier Lopez78825132011-06-01 11:26:13 +02001126
1127 /* Enqueue the packet */
1128 skb_queue_tail(&data->pending, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001129 data->tx_pkts++;
1130 data->tx_bytes += my_skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02001131 return;
1132
1133nla_put_failure:
Bob Copelandf2831e22015-02-05 08:45:43 -05001134 nlmsg_free(skb);
1135err_free_txskb:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001136 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Ben Greearb7bc9672014-09-25 14:22:18 -07001137 ieee80211_free_txskb(hw, my_skb);
Ben Greear2155c3f2014-10-22 12:22:58 -07001138 data->tx_failed++;
Javier Lopez78825132011-06-01 11:26:13 +02001139}
1140
Johannes Berge8261172012-07-27 19:48:26 +02001141static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1142 struct ieee80211_channel *c2)
1143{
1144 if (!c1 || !c2)
1145 return false;
1146
1147 return c1->center_freq == c2->center_freq;
1148}
1149
1150struct tx_iter_data {
1151 struct ieee80211_channel *channel;
1152 bool receive;
1153};
1154
1155static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1156 struct ieee80211_vif *vif)
1157{
1158 struct tx_iter_data *data = _data;
1159
1160 if (!vif->chanctx_conf)
1161 return;
1162
1163 if (!hwsim_chans_compat(data->channel,
Johannes Berg4bf88532012-11-09 11:39:59 +01001164 rcu_dereference(vif->chanctx_conf)->def.chan))
Johannes Berge8261172012-07-27 19:48:26 +02001165 return;
1166
1167 data->receive = true;
1168}
1169
Johannes Berg1f7bba72014-11-06 22:56:36 +01001170static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1171{
1172 /*
1173 * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1174 * e.g. like this:
1175 * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1176 * (but you should use a valid OUI, not that)
1177 *
1178 * If anyone wants to 'donate' a radiotap OUI/subns code
1179 * please send a patch removing this #ifdef and changing
1180 * the values accordingly.
1181 */
1182#ifdef HWSIM_RADIOTAP_OUI
1183 struct ieee80211_vendor_radiotap *rtap;
1184
1185 /*
1186 * Note that this code requires the headroom in the SKB
1187 * that was allocated earlier.
1188 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001189 rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
Johannes Berg1f7bba72014-11-06 22:56:36 +01001190 rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1191 rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1192 rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1193 rtap->subns = 127;
1194
1195 /*
1196 * Radiotap vendor namespaces can (and should) also be
1197 * split into fields by using the standard radiotap
1198 * presence bitmap mechanism. Use just BIT(0) here for
1199 * the presence bitmap.
1200 */
1201 rtap->present = BIT(0);
1202 /* We have 8 bytes of (dummy) data */
1203 rtap->len = 8;
1204 /* For testing, also require it to be aligned */
1205 rtap->align = 8;
1206 /* And also test that padding works, 4 bytes */
1207 rtap->pad = 4;
1208 /* push the data */
1209 memcpy(rtap->data, "ABCDEFGH", 8);
1210 /* make sure to clear padding, mac80211 doesn't */
1211 memset(rtap->data + 8, 0, 4);
1212
1213 IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1214#endif
1215}
1216
Javier Lopez78825132011-06-01 11:26:13 +02001217static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
Johannes Berge8261172012-07-27 19:48:26 +02001218 struct sk_buff *skb,
1219 struct ieee80211_channel *chan)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001220{
Johannes Berg0e057d732008-09-12 00:39:22 +02001221 struct mac80211_hwsim_data *data = hw->priv, *data2;
1222 bool ack = false;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001223 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001224 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001225 struct ieee80211_rx_status rx_status;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001226 u64 now;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001227
1228 memset(&rx_status, 0, sizeof(rx_status));
Thomas Pedersenf4bda332012-11-13 10:46:27 -08001229 rx_status.flag |= RX_FLAG_MACTIME_START;
Johannes Berge8261172012-07-27 19:48:26 +02001230 rx_status.freq = chan->center_freq;
1231 rx_status.band = chan->band;
Karl Beldandad63302013-04-15 17:09:30 +02001232 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1233 rx_status.rate_idx =
1234 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
Johannes Berg8613c942017-04-26 13:51:41 +02001235 rx_status.nss =
Karl Beldandad63302013-04-15 17:09:30 +02001236 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
Johannes Bergda6a4352017-04-26 12:14:59 +02001237 rx_status.encoding = RX_ENC_VHT;
Karl Beldandad63302013-04-15 17:09:30 +02001238 } else {
1239 rx_status.rate_idx = info->control.rates[0].idx;
1240 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
Johannes Bergda6a4352017-04-26 12:14:59 +02001241 rx_status.encoding = RX_ENC_HT;
Karl Beldandad63302013-04-15 17:09:30 +02001242 }
Jouni Malinen281ed292011-08-06 23:07:00 +03001243 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
Johannes Berg2f242bf2017-05-05 11:53:19 +02001244 rx_status.bw = RATE_INFO_BW_40;
1245 else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1246 rx_status.bw = RATE_INFO_BW_80;
1247 else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1248 rx_status.bw = RATE_INFO_BW_160;
1249 else
1250 rx_status.bw = RATE_INFO_BW_20;
Jouni Malinen281ed292011-08-06 23:07:00 +03001251 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
Johannes Berg7fdd69c2017-04-26 11:13:00 +02001252 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
Johannes Berg4b14c962009-07-10 16:56:59 +02001253 /* TODO: simulate real signal strength (and optional packet loss) */
Johannes Berg1d5e9f82017-04-13 10:31:16 +02001254 rx_status.signal = -50;
1255 if (info->control.vif)
1256 rx_status.signal += info->control.vif->bss_conf.txpower;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001257
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001258 if (data->ps != PS_DISABLED)
1259 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1260
Johannes Berg90e30122009-06-18 14:51:12 +02001261 /* release the skb's source info */
1262 skb_orphan(skb);
John W. Linvillec0acf382009-06-30 16:55:52 -04001263 skb_dst_drop(skb);
Johannes Berg90e30122009-06-18 14:51:12 +02001264 skb->mark = 0;
1265 secpath_reset(skb);
1266 nf_reset(skb);
1267
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001268 /*
1269 * Get absolute mactime here so all HWs RX at the "same time", and
1270 * absolute TX time for beacon mactime so the timestamp matches.
1271 * Giving beacons a different mactime than non-beacons looks messy, but
1272 * it helps the Toffset be exact and a ~10us mactime discrepancy
1273 * probably doesn't really matter.
1274 */
1275 if (ieee80211_is_beacon(hdr->frame_control) ||
Jouni Malinen341203e2019-02-02 01:16:31 +02001276 ieee80211_is_probe_resp(hdr->frame_control)) {
1277 rx_status.boottime_ns = ktime_get_boot_ns();
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001278 now = data->abs_bcn_ts;
Jouni Malinen341203e2019-02-02 01:16:31 +02001279 } else {
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001280 now = mac80211_hwsim_get_tsf_raw();
Jouni Malinen341203e2019-02-02 01:16:31 +02001281 }
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001282
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001283 /* Copy skb to all enabled radios that are on the current frequency */
Johannes Berg0e057d732008-09-12 00:39:22 +02001284 spin_lock(&hwsim_radio_lock);
1285 list_for_each_entry(data2, &hwsim_radios, list) {
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001286 struct sk_buff *nskb;
Johannes Berge8261172012-07-27 19:48:26 +02001287 struct tx_iter_data tx_iter_data = {
1288 .receive = false,
1289 .channel = chan,
1290 };
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001291
Johannes Berg0e057d732008-09-12 00:39:22 +02001292 if (data == data2)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001293 continue;
Johannes Berg0e057d732008-09-12 00:39:22 +02001294
Johannes Berge8261172012-07-27 19:48:26 +02001295 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1296 !hwsim_ps_rx_ok(data2, skb))
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001297 continue;
1298
Johannes Berge8261172012-07-27 19:48:26 +02001299 if (!(data->group & data2->group))
1300 continue;
1301
Martin Willi100cb9f2016-05-09 18:33:59 +02001302 if (data->netgroup != data2->netgroup)
1303 continue;
1304
Johannes Berge8261172012-07-27 19:48:26 +02001305 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1306 !hwsim_chans_compat(chan, data2->channel)) {
1307 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001308 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1309 mac80211_hwsim_tx_iter, &tx_iter_data);
Johannes Berge8261172012-07-27 19:48:26 +02001310 if (!tx_iter_data.receive)
1311 continue;
1312 }
1313
Johannes Berg90b9e4462012-11-16 10:09:08 +01001314 /*
1315 * reserve some space for our vendor and the normal
1316 * radiotap header, since we're copying anyway
1317 */
Johannes Berga357d7f2012-12-10 11:57:42 +01001318 if (skb->len < PAGE_SIZE && paged_rx) {
1319 struct page *page = alloc_page(GFP_ATOMIC);
1320
1321 if (!page)
1322 continue;
1323
1324 nskb = dev_alloc_skb(128);
1325 if (!nskb) {
1326 __free_page(page);
1327 continue;
1328 }
1329
1330 memcpy(page_address(page), skb->data, skb->len);
1331 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1332 } else {
1333 nskb = skb_copy(skb, GFP_ATOMIC);
1334 if (!nskb)
1335 continue;
1336 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001337
Jouni Malinen265dc7f2009-12-04 19:10:34 +02001338 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
Johannes Berg0e057d732008-09-12 00:39:22 +02001339 ack = true;
Javier Cardonaf483ad22012-03-31 11:31:30 -07001340
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001341 rx_status.mactime = now + data2->tsf_offset;
Johannes Berg90b9e4462012-11-16 10:09:08 +01001342
Johannes Bergf1d58c22009-06-17 13:13:00 +02001343 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
Johannes Berg1f7bba72014-11-06 22:56:36 +01001344
1345 mac80211_hwsim_add_vendor_rtap(nskb);
1346
Ben Greear2155c3f2014-10-22 12:22:58 -07001347 data2->rx_pkts++;
1348 data2->rx_bytes += nskb->len;
Johannes Bergf1d58c22009-06-17 13:13:00 +02001349 ieee80211_rx_irqsafe(data2->hw, nskb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001350 }
Johannes Berg0e057d732008-09-12 00:39:22 +02001351 spin_unlock(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001352
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001353 return ack;
1354}
1355
Thomas Huehn36323f82012-07-23 21:33:42 +02001356static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1357 struct ieee80211_tx_control *control,
1358 struct sk_buff *skb)
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001359{
Johannes Berge8261172012-07-27 19:48:26 +02001360 struct mac80211_hwsim_data *data = hw->priv;
1361 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
Jouni Malinen732b5392015-11-26 20:50:12 +02001362 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Berge8261172012-07-27 19:48:26 +02001363 struct ieee80211_chanctx_conf *chanctx_conf;
1364 struct ieee80211_channel *channel;
Johannes Berg0e057d732008-09-12 00:39:22 +02001365 bool ack;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001366 u32 _portid;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001367
Johannes Berge8261172012-07-27 19:48:26 +02001368 if (WARN_ON(skb->len < 10)) {
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001369 /* Should not happen; just a sanity check for addr1 use */
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001370 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001371 return;
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001372 }
1373
Luciano Coelho361c3e02014-02-10 15:23:03 +02001374 if (!data->use_chanctx) {
Johannes Berge8261172012-07-27 19:48:26 +02001375 channel = data->channel;
1376 } else if (txi->hw_queue == 4) {
1377 channel = data->tmp_chan;
1378 } else {
1379 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1380 if (chanctx_conf)
Johannes Berg4bf88532012-11-09 11:39:59 +01001381 channel = chanctx_conf->def.chan;
Johannes Berge8261172012-07-27 19:48:26 +02001382 else
1383 channel = NULL;
1384 }
1385
1386 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001387 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001388 return;
1389 }
1390
1391 if (data->idle && !data->tmp_chan) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001392 wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
Johannes Bergfe0f3cd2013-07-14 23:37:14 +03001393 ieee80211_free_txskb(hw, skb);
Johannes Berge8261172012-07-27 19:48:26 +02001394 return;
1395 }
1396
1397 if (txi->control.vif)
1398 hwsim_check_magic(txi->control.vif);
1399 if (control->sta)
1400 hwsim_check_sta_magic(control->sta);
1401
Johannes Berg30686bf2015-06-02 21:39:54 +02001402 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001403 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1404 txi->control.rates,
1405 ARRAY_SIZE(txi->control.rates));
Johannes Berge8261172012-07-27 19:48:26 +02001406
Jouni Malinen732b5392015-11-26 20:50:12 +02001407 if (skb->len >= 24 + 8 &&
1408 ieee80211_is_probe_resp(hdr->frame_control)) {
1409 /* fake header transmission time */
1410 struct ieee80211_mgmt *mgmt;
1411 struct ieee80211_rate *txrate;
1412 u64 ts;
1413
1414 mgmt = (struct ieee80211_mgmt *)skb->data;
1415 txrate = ieee80211_get_tx_rate(hw, txi);
1416 ts = mac80211_hwsim_get_tsf_raw();
1417 mgmt->u.probe_resp.timestamp =
1418 cpu_to_le64(ts + data->tsf_offset +
1419 24 * 8 * 10 / txrate->bitrate);
1420 }
1421
Johannes Berge8261172012-07-27 19:48:26 +02001422 mac80211_hwsim_monitor_rx(hw, skb, channel);
1423
Javier Lopez78825132011-06-01 11:26:13 +02001424 /* wmediumd mode check */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001425 _portid = READ_ONCE(data->wmediumd);
Javier Lopez78825132011-06-01 11:26:13 +02001426
Eric W. Biederman15e47302012-09-07 20:12:54 +00001427 if (_portid)
1428 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
Javier Lopez78825132011-06-01 11:26:13 +02001429
1430 /* NO wmediumd detected, perfect medium simulation */
Ben Greear2155c3f2014-10-22 12:22:58 -07001431 data->tx_pkts++;
1432 data->tx_bytes += skb->len;
Johannes Berge8261172012-07-27 19:48:26 +02001433 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
Javier Lopez78825132011-06-01 11:26:13 +02001434
Johannes Bergf06b7ab2016-01-13 14:21:01 +01001435 if (ack && skb->len >= 16)
Johannes Berge8261172012-07-27 19:48:26 +02001436 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
Jouni Malinene36cfdc2008-06-13 19:44:48 +03001437
Johannes Berge6a98542008-10-21 12:40:02 +02001438 ieee80211_tx_info_clear_status(txi);
Javier Cardona2a4ffa42012-05-01 03:01:28 -07001439
1440 /* frame was transmitted at most favorable rate at first attempt */
1441 txi->control.rates[0].count = 1;
1442 txi->control.rates[1].idx = -1;
1443
Johannes Berge6a98542008-10-21 12:40:02 +02001444 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1445 txi->flags |= IEEE80211_TX_STAT_ACK;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001446 ieee80211_tx_status_irqsafe(hw, skb);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001447}
1448
1449
1450static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1451{
1452 struct mac80211_hwsim_data *data = hw->priv;
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001453 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001454 data->started = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001455 return 0;
1456}
1457
1458
1459static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1460{
1461 struct mac80211_hwsim_data *data = hw->priv;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001462 data->started = false;
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001463 hrtimer_cancel(&data->beacon_timer);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001464 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001465}
1466
1467
1468static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001469 struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001470{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001471 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1472 __func__, ieee80211_vif_type_p2p(vif),
1473 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001474 hwsim_set_magic(vif);
Johannes Berge8261172012-07-27 19:48:26 +02001475
1476 vif->cab_queue = 0;
1477 vif->hw_queue[IEEE80211_AC_VO] = 0;
1478 vif->hw_queue[IEEE80211_AC_VI] = 1;
1479 vif->hw_queue[IEEE80211_AC_BE] = 2;
1480 vif->hw_queue[IEEE80211_AC_BK] = 3;
1481
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001482 return 0;
1483}
1484
1485
Johannes Bergc35d0272010-08-27 12:35:59 +02001486static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1487 struct ieee80211_vif *vif,
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001488 enum nl80211_iftype newtype,
1489 bool newp2p)
Johannes Bergc35d0272010-08-27 12:35:59 +02001490{
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001491 newtype = ieee80211_iftype_p2p(newtype, newp2p);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001492 wiphy_dbg(hw->wiphy,
1493 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1494 __func__, ieee80211_vif_type_p2p(vif),
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001495 newtype, vif->addr);
Johannes Bergc35d0272010-08-27 12:35:59 +02001496 hwsim_check_magic(vif);
1497
Johannes Berg3eb92f62013-03-18 22:13:18 +01001498 /*
1499 * interface may change from non-AP to AP in
1500 * which case this needs to be set up again
1501 */
1502 vif->cab_queue = 0;
1503
Johannes Bergc35d0272010-08-27 12:35:59 +02001504 return 0;
1505}
1506
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001507static void mac80211_hwsim_remove_interface(
Johannes Berg1ed32e42009-12-23 13:15:45 +01001508 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001509{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001510 wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1511 __func__, ieee80211_vif_type_p2p(vif),
1512 vif->addr);
Johannes Berg1ed32e42009-12-23 13:15:45 +01001513 hwsim_check_magic(vif);
1514 hwsim_clear_magic(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001515}
1516
Johannes Berge8261172012-07-27 19:48:26 +02001517static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1518 struct sk_buff *skb,
1519 struct ieee80211_channel *chan)
1520{
Martin Willif21e4d82016-05-14 10:34:44 +02001521 struct mac80211_hwsim_data *data = hw->priv;
Mark Rutland6aa7de02017-10-23 14:07:29 -07001522 u32 _pid = READ_ONCE(data->wmediumd);
Johannes Berge8261172012-07-27 19:48:26 +02001523
Johannes Berg30686bf2015-06-02 21:39:54 +02001524 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
Karl Beldan1eb32172013-04-19 14:44:52 +02001525 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1526 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1527 txi->control.rates,
1528 ARRAY_SIZE(txi->control.rates));
1529 }
1530
Johannes Berge8261172012-07-27 19:48:26 +02001531 mac80211_hwsim_monitor_rx(hw, skb, chan);
1532
1533 if (_pid)
1534 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1535
1536 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1537 dev_kfree_skb(skb);
1538}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001539
1540static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1541 struct ieee80211_vif *vif)
1542{
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001543 struct mac80211_hwsim_data *data = arg;
1544 struct ieee80211_hw *hw = data->hw;
1545 struct ieee80211_tx_info *info;
1546 struct ieee80211_rate *txrate;
1547 struct ieee80211_mgmt *mgmt;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001548 struct sk_buff *skb;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001549
Johannes Berg8aa21e62008-09-11 02:16:36 +02001550 hwsim_check_magic(vif);
1551
Andrey Yurovsky55b39612008-10-31 23:23:35 -07001552 if (vif->type != NL80211_IFTYPE_AP &&
Johannes Berg2b2d7792010-08-17 12:08:07 +02001553 vif->type != NL80211_IFTYPE_MESH_POINT &&
1554 vif->type != NL80211_IFTYPE_ADHOC)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001555 return;
1556
1557 skb = ieee80211_beacon_get(hw, vif);
1558 if (skb == NULL)
1559 return;
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001560 info = IEEE80211_SKB_CB(skb);
Johannes Berg30686bf2015-06-02 21:39:54 +02001561 if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
Karl Beldan1eb32172013-04-19 14:44:52 +02001562 ieee80211_get_tx_rates(vif, NULL, skb,
1563 info->control.rates,
1564 ARRAY_SIZE(info->control.rates));
1565
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001566 txrate = ieee80211_get_tx_rate(hw, info);
1567
1568 mgmt = (struct ieee80211_mgmt *) skb->data;
1569 /* fake header transmission time */
1570 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1571 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1572 data->tsf_offset +
1573 24 * 8 * 10 / txrate->bitrate);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001574
Johannes Berge8261172012-07-27 19:48:26 +02001575 mac80211_hwsim_tx_frame(hw, skb,
Johannes Berg4bf88532012-11-09 11:39:59 +01001576 rcu_dereference(vif->chanctx_conf)->def.chan);
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02001577
1578 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1579 ieee80211_csa_finish(vif);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001580}
1581
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001582static enum hrtimer_restart
1583mac80211_hwsim_beacon(struct hrtimer *timer)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001584{
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001585 struct mac80211_hwsim_data *data =
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001586 container_of(timer, struct mac80211_hwsim_data, beacon_timer);
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001587 struct ieee80211_hw *hw = data->hw;
1588 u64 bcn_int = data->beacon_int;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001589
Johannes Berg4c4c6712009-06-01 14:29:52 +02001590 if (!data->started)
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001591 return HRTIMER_NORESTART;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001592
Jouni Malinenf248f102008-06-13 19:44:47 +03001593 ieee80211_iterate_active_interfaces_atomic(
Johannes Berg8b2c9822012-11-06 20:23:30 +01001594 hw, IEEE80211_IFACE_ITER_NORMAL,
Thomas Pedersenb66851c2013-01-07 14:48:07 -08001595 mac80211_hwsim_beacon_tx, data);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001596
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001597 /* beacon at new TBTT + beacon interval */
1598 if (data->bcn_delta) {
1599 bcn_int -= data->bcn_delta;
1600 data->bcn_delta = 0;
1601 }
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001602 hrtimer_forward(&data->beacon_timer, hrtimer_get_expires(timer),
1603 ns_to_ktime(bcn_int * NSEC_PER_USEC));
1604 return HRTIMER_RESTART;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001605}
1606
Karl Beldan675a0b02013-03-25 16:26:57 +01001607static const char * const hwsim_chanwidths[] = {
1608 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1609 [NL80211_CHAN_WIDTH_20] = "ht20",
1610 [NL80211_CHAN_WIDTH_40] = "ht40",
1611 [NL80211_CHAN_WIDTH_80] = "vht80",
1612 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1613 [NL80211_CHAN_WIDTH_160] = "vht160",
Johannes Berg0aaffa92010-05-05 15:28:27 +02001614};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001615
Johannes Berge8975582008-10-09 12:18:51 +02001616static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001617{
1618 struct mac80211_hwsim_data *data = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001619 struct ieee80211_conf *conf = &hw->conf;
Johannes Berg0f782312009-12-01 13:37:02 +01001620 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1621 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1622 [IEEE80211_SMPS_OFF] = "off",
1623 [IEEE80211_SMPS_STATIC] = "static",
1624 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1625 };
Johannes Berg7f813ce2013-11-06 10:37:34 +01001626 int idx;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001627
Karl Beldan675a0b02013-03-25 16:26:57 +01001628 if (conf->chandef.chan)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001629 wiphy_dbg(hw->wiphy,
1630 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1631 __func__,
1632 conf->chandef.chan->center_freq,
1633 conf->chandef.center_freq1,
1634 conf->chandef.center_freq2,
1635 hwsim_chanwidths[conf->chandef.width],
1636 !!(conf->flags & IEEE80211_CONF_IDLE),
1637 !!(conf->flags & IEEE80211_CONF_PS),
1638 smps_modes[conf->smps_mode]);
Karl Beldan675a0b02013-03-25 16:26:57 +01001639 else
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001640 wiphy_dbg(hw->wiphy,
1641 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1642 __func__,
1643 !!(conf->flags & IEEE80211_CONF_IDLE),
1644 !!(conf->flags & IEEE80211_CONF_PS),
1645 smps_modes[conf->smps_mode]);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001646
Jouni Malinen70541832009-11-01 11:30:48 +02001647 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1648
Johannes Berg7f813ce2013-11-06 10:37:34 +01001649 WARN_ON(conf->chandef.chan && data->use_chanctx);
Johannes Berge8261172012-07-27 19:48:26 +02001650
Johannes Berg7f813ce2013-11-06 10:37:34 +01001651 mutex_lock(&data->mutex);
1652 if (data->scanning && conf->chandef.chan) {
1653 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1654 if (data->survey_data[idx].channel == data->channel) {
1655 data->survey_data[idx].start =
1656 data->survey_data[idx].next_start;
1657 data->survey_data[idx].end = jiffies;
1658 break;
1659 }
1660 }
Johannes Berge8261172012-07-27 19:48:26 +02001661
Johannes Berg7f813ce2013-11-06 10:37:34 +01001662 data->channel = conf->chandef.chan;
1663
1664 for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
1665 if (data->survey_data[idx].channel &&
1666 data->survey_data[idx].channel != data->channel)
1667 continue;
1668 data->survey_data[idx].channel = data->channel;
1669 data->survey_data[idx].next_start = jiffies;
1670 break;
1671 }
1672 } else {
1673 data->channel = conf->chandef.chan;
1674 }
1675 mutex_unlock(&data->mutex);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001676
Johannes Berg4c4c6712009-06-01 14:29:52 +02001677 if (!data->started || !data->beacon_int)
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001678 hrtimer_cancel(&data->beacon_timer);
1679 else if (!hrtimer_is_queued(&data->beacon_timer)) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001680 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1681 u32 bcn_int = data->beacon_int;
1682 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1683
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001684 hrtimer_start(&data->beacon_timer,
1685 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1686 HRTIMER_MODE_REL_SOFT);
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001687 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001688
1689 return 0;
1690}
1691
1692
1693static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1694 unsigned int changed_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001695 unsigned int *total_flags,u64 multicast)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001696{
1697 struct mac80211_hwsim_data *data = hw->priv;
1698
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001699 wiphy_dbg(hw->wiphy, "%s\n", __func__);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001700
1701 data->rx_filter = 0;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001702 if (*total_flags & FIF_ALLMULTI)
1703 data->rx_filter |= FIF_ALLMULTI;
1704
1705 *total_flags = data->rx_filter;
1706}
1707
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001708static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1709 struct ieee80211_vif *vif)
1710{
1711 unsigned int *count = data;
1712 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1713
1714 if (vp->bcn_en)
1715 (*count)++;
1716}
1717
Johannes Berg8aa21e62008-09-11 02:16:36 +02001718static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1719 struct ieee80211_vif *vif,
1720 struct ieee80211_bss_conf *info,
1721 u32 changed)
1722{
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001723 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001724 struct mac80211_hwsim_data *data = hw->priv;
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001725
Johannes Berg8aa21e62008-09-11 02:16:36 +02001726 hwsim_check_magic(vif);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001727
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001728 wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1729 __func__, changed, vif->addr);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001730
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001731 if (changed & BSS_CHANGED_BSSID) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001732 wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
1733 __func__, info->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001734 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1735 }
1736
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001737 if (changed & BSS_CHANGED_ASSOC) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001738 wiphy_dbg(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1739 info->assoc, info->aid);
Jouni Malinenfc6971d2008-10-30 19:59:05 +02001740 vp->assoc = info->assoc;
1741 vp->aid = info->aid;
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001742 }
1743
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001744 if (changed & BSS_CHANGED_BEACON_ENABLED) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001745 wiphy_dbg(hw->wiphy, " BCN EN: %d (BI=%u)\n",
1746 info->enable_beacon, info->beacon_int);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001747 vp->bcn_en = info->enable_beacon;
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001748 if (data->started &&
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001749 !hrtimer_is_queued(&data->beacon_timer) &&
Thomas Pedersen01e59e42013-01-02 14:55:17 -08001750 info->enable_beacon) {
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001751 u64 tsf, until_tbtt;
1752 u32 bcn_int;
Johannes Berg56067622015-03-03 22:09:05 +01001753 data->beacon_int = info->beacon_int * 1024;
Thomas Pedersenc51f8782013-01-02 14:55:18 -08001754 tsf = mac80211_hwsim_get_tsf(hw, vif);
1755 bcn_int = data->beacon_int;
1756 until_tbtt = bcn_int - do_div(tsf, bcn_int);
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001757
1758 hrtimer_start(&data->beacon_timer,
1759 ns_to_ktime(until_tbtt * NSEC_PER_USEC),
1760 HRTIMER_MODE_REL_SOFT);
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001761 } else if (!info->enable_beacon) {
1762 unsigned int count = 0;
Jouni Malinencdb1b802013-11-06 12:06:30 +02001763 ieee80211_iterate_active_interfaces_atomic(
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001764 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1765 mac80211_hwsim_bcn_en_iter, &count);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001766 wiphy_dbg(hw->wiphy, " beaconing vifs remaining: %u",
1767 count);
Johannes Berg56067622015-03-03 22:09:05 +01001768 if (count == 0) {
Thomas Gleixner77dcc622019-03-01 23:48:19 +01001769 hrtimer_cancel(&data->beacon_timer);
Johannes Berg56067622015-03-03 22:09:05 +01001770 data->beacon_int = 0;
1771 }
Jouni Malinen0bb861e2013-10-22 14:11:17 +03001772 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001773 }
1774
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001775 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001776 wiphy_dbg(hw->wiphy, " ERP_CTS_PROT: %d\n",
1777 info->use_cts_prot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001778 }
1779
1780 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001781 wiphy_dbg(hw->wiphy, " ERP_PREAMBLE: %d\n",
1782 info->use_short_preamble);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001783 }
1784
1785 if (changed & BSS_CHANGED_ERP_SLOT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001786 wiphy_dbg(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001787 }
1788
1789 if (changed & BSS_CHANGED_HT) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001790 wiphy_dbg(hw->wiphy, " HT: op_mode=0x%x\n",
1791 info->ht_operation_mode);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001792 }
1793
1794 if (changed & BSS_CHANGED_BASIC_RATES) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001795 wiphy_dbg(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1796 (unsigned long long) info->basic_rates);
Jouni Malinenfe63bfa2008-10-30 16:59:21 +02001797 }
Johannes Bergcbc668a2012-10-24 11:54:31 +02001798
1799 if (changed & BSS_CHANGED_TXPOWER)
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001800 wiphy_dbg(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
Johannes Berg8aa21e62008-09-11 02:16:36 +02001801}
1802
Johannes Berg1d669cb2010-02-19 19:06:55 +01001803static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1804 struct ieee80211_vif *vif,
1805 struct ieee80211_sta *sta)
1806{
1807 hwsim_check_magic(vif);
1808 hwsim_set_sta_magic(sta);
1809
1810 return 0;
1811}
1812
1813static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1814 struct ieee80211_vif *vif,
1815 struct ieee80211_sta *sta)
1816{
1817 hwsim_check_magic(vif);
1818 hwsim_clear_sta_magic(sta);
1819
1820 return 0;
1821}
1822
Johannes Berg8aa21e62008-09-11 02:16:36 +02001823static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1824 struct ieee80211_vif *vif,
Johannes Berg17741cd2008-09-11 00:02:02 +02001825 enum sta_notify_cmd cmd,
1826 struct ieee80211_sta *sta)
Johannes Berg8aa21e62008-09-11 02:16:36 +02001827{
1828 hwsim_check_magic(vif);
Johannes Berg1d669cb2010-02-19 19:06:55 +01001829
Johannes Berg81c06522008-09-11 02:17:01 +02001830 switch (cmd) {
Christian Lamparter89fad572008-12-09 16:28:06 +01001831 case STA_NOTIFY_SLEEP:
1832 case STA_NOTIFY_AWAKE:
1833 /* TODO: make good use of these flags */
1834 break;
Johannes Berg1d669cb2010-02-19 19:06:55 +01001835 default:
1836 WARN(1, "Invalid sta notify: %d\n", cmd);
1837 break;
Johannes Berg81c06522008-09-11 02:17:01 +02001838 }
1839}
1840
1841static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1842 struct ieee80211_sta *sta,
1843 bool set)
1844{
1845 hwsim_check_sta_magic(sta);
1846 return 0;
Johannes Berg8aa21e62008-09-11 02:16:36 +02001847}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03001848
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001849static int mac80211_hwsim_conf_tx(
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001850 struct ieee80211_hw *hw,
1851 struct ieee80211_vif *vif, u16 queue,
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001852 const struct ieee80211_tx_queue_params *params)
1853{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02001854 wiphy_dbg(hw->wiphy,
1855 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1856 __func__, queue,
1857 params->txop, params->cw_min,
1858 params->cw_max, params->aifs);
Jouni Malinen1e898ff82008-10-30 16:59:23 +02001859 return 0;
1860}
1861
Johannes Berg7f813ce2013-11-06 10:37:34 +01001862static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
1863 struct survey_info *survey)
Holger Schurig12897232010-04-19 10:23:57 +02001864{
Johannes Berg7f813ce2013-11-06 10:37:34 +01001865 struct mac80211_hwsim_data *hwsim = hw->priv;
Holger Schurig12897232010-04-19 10:23:57 +02001866
Johannes Berg7f813ce2013-11-06 10:37:34 +01001867 if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
Holger Schurig12897232010-04-19 10:23:57 +02001868 return -ENOENT;
1869
Johannes Berg7f813ce2013-11-06 10:37:34 +01001870 mutex_lock(&hwsim->mutex);
1871 survey->channel = hwsim->survey_data[idx].channel;
1872 if (!survey->channel) {
1873 mutex_unlock(&hwsim->mutex);
1874 return -ENOENT;
1875 }
Holger Schurig12897232010-04-19 10:23:57 +02001876
1877 /*
Johannes Berg7f813ce2013-11-06 10:37:34 +01001878 * Magically conjured dummy values --- this is only ok for simulated hardware.
Holger Schurig12897232010-04-19 10:23:57 +02001879 *
Johannes Berg7f813ce2013-11-06 10:37:34 +01001880 * A real driver which cannot determine real values noise MUST NOT
1881 * report any, especially not a magically conjured ones :-)
Holger Schurig12897232010-04-19 10:23:57 +02001882 */
Johannes Berg7f813ce2013-11-06 10:37:34 +01001883 survey->filled = SURVEY_INFO_NOISE_DBM |
1884 SURVEY_INFO_TIME |
1885 SURVEY_INFO_TIME_BUSY;
Holger Schurig12897232010-04-19 10:23:57 +02001886 survey->noise = -92;
Johannes Berg7f813ce2013-11-06 10:37:34 +01001887 survey->time =
1888 jiffies_to_msecs(hwsim->survey_data[idx].end -
1889 hwsim->survey_data[idx].start);
1890 /* report 12.5% of channel time is used */
1891 survey->time_busy = survey->time/8;
1892 mutex_unlock(&hwsim->mutex);
Holger Schurig12897232010-04-19 10:23:57 +02001893
1894 return 0;
1895}
1896
Johannes Bergaff89a92009-07-01 21:26:51 +02001897#ifdef CONFIG_NL80211_TESTMODE
1898/*
1899 * This section contains example code for using netlink
1900 * attributes with the testmode command in nl80211.
1901 */
1902
1903/* These enums need to be kept in sync with userspace */
1904enum hwsim_testmode_attr {
1905 __HWSIM_TM_ATTR_INVALID = 0,
1906 HWSIM_TM_ATTR_CMD = 1,
1907 HWSIM_TM_ATTR_PS = 2,
1908
1909 /* keep last */
1910 __HWSIM_TM_ATTR_AFTER_LAST,
1911 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1912};
1913
1914enum hwsim_testmode_cmd {
1915 HWSIM_TM_CMD_SET_PS = 0,
1916 HWSIM_TM_CMD_GET_PS = 1,
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001917 HWSIM_TM_CMD_STOP_QUEUES = 2,
1918 HWSIM_TM_CMD_WAKE_QUEUES = 3,
Johannes Bergaff89a92009-07-01 21:26:51 +02001919};
1920
1921static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1922 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1923 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1924};
1925
Johannes Berg5bc38192009-07-07 11:00:55 +02001926static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
David Spinadel52981cd2013-07-31 18:06:22 +03001927 struct ieee80211_vif *vif,
Johannes Berg5bc38192009-07-07 11:00:55 +02001928 void *data, int len)
Johannes Bergaff89a92009-07-01 21:26:51 +02001929{
1930 struct mac80211_hwsim_data *hwsim = hw->priv;
1931 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1932 struct sk_buff *skb;
1933 int err, ps;
1934
Johannes Berg8cb08172019-04-26 14:07:28 +02001935 err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
1936 hwsim_testmode_policy, NULL);
Johannes Bergaff89a92009-07-01 21:26:51 +02001937 if (err)
1938 return err;
1939
1940 if (!tb[HWSIM_TM_ATTR_CMD])
1941 return -EINVAL;
1942
1943 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1944 case HWSIM_TM_CMD_SET_PS:
1945 if (!tb[HWSIM_TM_ATTR_PS])
1946 return -EINVAL;
1947 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1948 return hwsim_fops_ps_write(hwsim, ps);
1949 case HWSIM_TM_CMD_GET_PS:
1950 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1951 nla_total_size(sizeof(u32)));
1952 if (!skb)
1953 return -ENOMEM;
David S. Miller633c9382012-04-01 21:03:30 -04001954 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1955 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02001956 return cfg80211_testmode_reply(skb);
Johannes Berg4d6d0ae2012-07-04 12:58:40 +02001957 case HWSIM_TM_CMD_STOP_QUEUES:
1958 ieee80211_stop_queues(hw);
1959 return 0;
1960 case HWSIM_TM_CMD_WAKE_QUEUES:
1961 ieee80211_wake_queues(hw);
1962 return 0;
Johannes Bergaff89a92009-07-01 21:26:51 +02001963 default:
1964 return -EOPNOTSUPP;
1965 }
1966
1967 nla_put_failure:
1968 kfree_skb(skb);
1969 return -ENOBUFS;
1970}
1971#endif
1972
Johannes Berg8b73d132009-12-01 14:24:24 +01001973static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1974 struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02001975 struct ieee80211_ampdu_params *params)
Johannes Berg8b73d132009-12-01 14:24:24 +01001976{
Sara Sharon50ea05e2015-12-30 16:06:04 +02001977 struct ieee80211_sta *sta = params->sta;
1978 enum ieee80211_ampdu_mlme_action action = params->action;
1979 u16 tid = params->tid;
1980
Johannes Berg8b73d132009-12-01 14:24:24 +01001981 switch (action) {
1982 case IEEE80211_AMPDU_TX_START:
1983 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1984 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02001985 case IEEE80211_AMPDU_TX_STOP_CONT:
1986 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1987 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Johannes Berg8b73d132009-12-01 14:24:24 +01001988 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1989 break;
1990 case IEEE80211_AMPDU_TX_OPERATIONAL:
1991 break;
1992 case IEEE80211_AMPDU_RX_START:
1993 case IEEE80211_AMPDU_RX_STOP:
1994 break;
1995 default:
1996 return -EOPNOTSUPP;
1997 }
1998
1999 return 0;
2000}
2001
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02002002static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2003 struct ieee80211_vif *vif,
2004 u32 queues, bool drop)
Johannes Berga80f7c02009-12-23 13:15:32 +01002005{
Javier Lopez78825132011-06-01 11:26:13 +02002006 /* Not implemented, queues only on kernel side */
Johannes Berga80f7c02009-12-23 13:15:32 +01002007}
2008
Johannes Berge8261172012-07-27 19:48:26 +02002009static void hw_scan_work(struct work_struct *work)
Johannes Berg69068032010-02-03 10:47:55 +01002010{
Johannes Berge8261172012-07-27 19:48:26 +02002011 struct mac80211_hwsim_data *hwsim =
2012 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2013 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2014 int dwell, i;
Johannes Berg69068032010-02-03 10:47:55 +01002015
Johannes Berge8261172012-07-27 19:48:26 +02002016 mutex_lock(&hwsim->mutex);
2017 if (hwsim->scan_chan_idx >= req->n_channels) {
Avraham Stern7947d3e2016-07-05 15:23:12 +03002018 struct cfg80211_scan_info info = {
2019 .aborted = false,
2020 };
2021
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002022 wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
Avraham Stern7947d3e2016-07-05 15:23:12 +03002023 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002024 hwsim->hw_scan_request = NULL;
2025 hwsim->hw_scan_vif = NULL;
2026 hwsim->tmp_chan = NULL;
2027 mutex_unlock(&hwsim->mutex);
2028 return;
2029 }
2030
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002031 wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2032 req->channels[hwsim->scan_chan_idx]->center_freq);
Johannes Berge8261172012-07-27 19:48:26 +02002033
2034 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
Johannes Berga8e828d2015-11-26 17:59:27 +01002035 if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2036 IEEE80211_CHAN_RADAR) ||
Johannes Berge8261172012-07-27 19:48:26 +02002037 !req->n_ssids) {
2038 dwell = 120;
2039 } else {
2040 dwell = 30;
2041 /* send probes */
2042 for (i = 0; i < req->n_ssids; i++) {
2043 struct sk_buff *probe;
Jouni Malinen12880d12016-02-26 22:12:49 +02002044 struct ieee80211_mgmt *mgmt;
Johannes Berge8261172012-07-27 19:48:26 +02002045
2046 probe = ieee80211_probereq_get(hwsim->hw,
Johannes Berg339467b2014-06-12 22:41:00 +02002047 hwsim->scan_addr,
Johannes Berge8261172012-07-27 19:48:26 +02002048 req->ssids[i].ssid,
2049 req->ssids[i].ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002050 req->ie_len);
Johannes Berge8261172012-07-27 19:48:26 +02002051 if (!probe)
2052 continue;
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002053
Jouni Malinen12880d12016-02-26 22:12:49 +02002054 mgmt = (struct ieee80211_mgmt *) probe->data;
2055 memcpy(mgmt->da, req->bssid, ETH_ALEN);
2056 memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2057
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002058 if (req->ie_len)
Johannes Berg59ae1d12017-06-16 14:29:20 +02002059 skb_put_data(probe, req->ie, req->ie_len);
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002060
Johannes Berge8261172012-07-27 19:48:26 +02002061 local_bh_disable();
2062 mac80211_hwsim_tx_frame(hwsim->hw, probe,
2063 hwsim->tmp_chan);
2064 local_bh_enable();
2065 }
2066 }
2067 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2068 msecs_to_jiffies(dwell));
Johannes Berg7f813ce2013-11-06 10:37:34 +01002069 hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2070 hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2071 hwsim->survey_data[hwsim->scan_chan_idx].end =
2072 jiffies + msecs_to_jiffies(dwell);
Johannes Berge8261172012-07-27 19:48:26 +02002073 hwsim->scan_chan_idx++;
2074 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002075}
2076
2077static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +02002078 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02002079 struct ieee80211_scan_request *hw_req)
Johannes Berg69068032010-02-03 10:47:55 +01002080{
Johannes Berge8261172012-07-27 19:48:26 +02002081 struct mac80211_hwsim_data *hwsim = hw->priv;
David Spinadelc56ef672014-02-05 15:21:13 +02002082 struct cfg80211_scan_request *req = &hw_req->req;
Johannes Berg69068032010-02-03 10:47:55 +01002083
Johannes Berge8261172012-07-27 19:48:26 +02002084 mutex_lock(&hwsim->mutex);
2085 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2086 mutex_unlock(&hwsim->mutex);
2087 return -EBUSY;
2088 }
2089 hwsim->hw_scan_request = req;
2090 hwsim->hw_scan_vif = vif;
2091 hwsim->scan_chan_idx = 0;
Johannes Berg339467b2014-06-12 22:41:00 +02002092 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2093 get_random_mask_addr(hwsim->scan_addr,
2094 hw_req->req.mac_addr,
2095 hw_req->req.mac_addr_mask);
2096 else
2097 memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
Johannes Berg7f813ce2013-11-06 10:37:34 +01002098 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Johannes Berge8261172012-07-27 19:48:26 +02002099 mutex_unlock(&hwsim->mutex);
Johannes Berg69068032010-02-03 10:47:55 +01002100
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002101 wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
Johannes Berg69068032010-02-03 10:47:55 +01002102
Johannes Berge8261172012-07-27 19:48:26 +02002103 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
Johannes Berg69068032010-02-03 10:47:55 +01002104
2105 return 0;
2106}
2107
Johannes Berge8261172012-07-27 19:48:26 +02002108static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2109 struct ieee80211_vif *vif)
2110{
2111 struct mac80211_hwsim_data *hwsim = hw->priv;
Avraham Stern7947d3e2016-07-05 15:23:12 +03002112 struct cfg80211_scan_info info = {
2113 .aborted = true,
2114 };
Johannes Berge8261172012-07-27 19:48:26 +02002115
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002116 wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
Johannes Berge8261172012-07-27 19:48:26 +02002117
2118 cancel_delayed_work_sync(&hwsim->hw_scan);
2119
2120 mutex_lock(&hwsim->mutex);
Avraham Stern7947d3e2016-07-05 15:23:12 +03002121 ieee80211_scan_completed(hwsim->hw, &info);
Johannes Berge8261172012-07-27 19:48:26 +02002122 hwsim->tmp_chan = NULL;
2123 hwsim->hw_scan_request = NULL;
2124 hwsim->hw_scan_vif = NULL;
2125 mutex_unlock(&hwsim->mutex);
2126}
2127
Johannes Berga344d672014-06-12 22:24:31 +02002128static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2129 struct ieee80211_vif *vif,
2130 const u8 *mac_addr)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002131{
2132 struct mac80211_hwsim_data *hwsim = hw->priv;
2133
2134 mutex_lock(&hwsim->mutex);
2135
2136 if (hwsim->scanning) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002137 pr_debug("two hwsim sw_scans detected!\n");
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002138 goto out;
2139 }
2140
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002141 pr_debug("hwsim sw_scan request, prepping stuff\n");
Johannes Berg339467b2014-06-12 22:41:00 +02002142
2143 memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002144 hwsim->scanning = true;
Johannes Berg7f813ce2013-11-06 10:37:34 +01002145 memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002146
2147out:
2148 mutex_unlock(&hwsim->mutex);
2149}
2150
Johannes Berga344d672014-06-12 22:24:31 +02002151static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2152 struct ieee80211_vif *vif)
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002153{
2154 struct mac80211_hwsim_data *hwsim = hw->priv;
2155
2156 mutex_lock(&hwsim->mutex);
2157
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002158 pr_debug("hwsim sw_scan_complete\n");
Johannes Berg23ff98f2010-05-03 09:21:14 +02002159 hwsim->scanning = false;
Joe Perches93803b32015-03-02 19:54:49 -08002160 eth_zero_addr(hwsim->scan_addr);
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002161
2162 mutex_unlock(&hwsim->mutex);
2163}
2164
Johannes Berg661ef472015-11-24 18:29:45 +01002165static void hw_roc_start(struct work_struct *work)
2166{
2167 struct mac80211_hwsim_data *hwsim =
2168 container_of(work, struct mac80211_hwsim_data, roc_start.work);
2169
2170 mutex_lock(&hwsim->mutex);
2171
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002172 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
Johannes Berg661ef472015-11-24 18:29:45 +01002173 hwsim->tmp_chan = hwsim->roc_chan;
2174 ieee80211_ready_on_channel(hwsim->hw);
2175
2176 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2177 msecs_to_jiffies(hwsim->roc_duration));
2178
2179 mutex_unlock(&hwsim->mutex);
2180}
2181
Johannes Berge8261172012-07-27 19:48:26 +02002182static void hw_roc_done(struct work_struct *work)
2183{
2184 struct mac80211_hwsim_data *hwsim =
2185 container_of(work, struct mac80211_hwsim_data, roc_done.work);
2186
2187 mutex_lock(&hwsim->mutex);
2188 ieee80211_remain_on_channel_expired(hwsim->hw);
2189 hwsim->tmp_chan = NULL;
2190 mutex_unlock(&hwsim->mutex);
2191
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002192 wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
Johannes Berge8261172012-07-27 19:48:26 +02002193}
2194
2195static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
Eliad Peller49884562012-11-19 17:05:09 +02002196 struct ieee80211_vif *vif,
Johannes Berge8261172012-07-27 19:48:26 +02002197 struct ieee80211_channel *chan,
Ilan Peerd339d5c2013-02-12 09:34:13 +02002198 int duration,
2199 enum ieee80211_roc_type type)
Johannes Berge8261172012-07-27 19:48:26 +02002200{
2201 struct mac80211_hwsim_data *hwsim = hw->priv;
2202
2203 mutex_lock(&hwsim->mutex);
2204 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2205 mutex_unlock(&hwsim->mutex);
2206 return -EBUSY;
2207 }
2208
Johannes Berg661ef472015-11-24 18:29:45 +01002209 hwsim->roc_chan = chan;
2210 hwsim->roc_duration = duration;
Johannes Berge8261172012-07-27 19:48:26 +02002211 mutex_unlock(&hwsim->mutex);
2212
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002213 wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2214 chan->center_freq, duration);
Johannes Berg661ef472015-11-24 18:29:45 +01002215 ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
Johannes Berge8261172012-07-27 19:48:26 +02002216
Johannes Berge8261172012-07-27 19:48:26 +02002217 return 0;
2218}
2219
2220static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
2221{
2222 struct mac80211_hwsim_data *hwsim = hw->priv;
2223
Johannes Berg661ef472015-11-24 18:29:45 +01002224 cancel_delayed_work_sync(&hwsim->roc_start);
Johannes Berge8261172012-07-27 19:48:26 +02002225 cancel_delayed_work_sync(&hwsim->roc_done);
2226
2227 mutex_lock(&hwsim->mutex);
2228 hwsim->tmp_chan = NULL;
2229 mutex_unlock(&hwsim->mutex);
2230
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002231 wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
Johannes Berge8261172012-07-27 19:48:26 +02002232
2233 return 0;
2234}
2235
2236static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2237 struct ieee80211_chanctx_conf *ctx)
2238{
2239 hwsim_set_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002240 wiphy_dbg(hw->wiphy,
2241 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2242 ctx->def.chan->center_freq, ctx->def.width,
2243 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002244 return 0;
2245}
2246
2247static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2248 struct ieee80211_chanctx_conf *ctx)
2249{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002250 wiphy_dbg(hw->wiphy,
2251 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2252 ctx->def.chan->center_freq, ctx->def.width,
2253 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002254 hwsim_check_chanctx_magic(ctx);
2255 hwsim_clear_chanctx_magic(ctx);
2256}
2257
2258static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2259 struct ieee80211_chanctx_conf *ctx,
2260 u32 changed)
2261{
2262 hwsim_check_chanctx_magic(ctx);
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002263 wiphy_dbg(hw->wiphy,
2264 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2265 ctx->def.chan->center_freq, ctx->def.width,
2266 ctx->def.center_freq1, ctx->def.center_freq2);
Johannes Berge8261172012-07-27 19:48:26 +02002267}
2268
2269static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2270 struct ieee80211_vif *vif,
2271 struct ieee80211_chanctx_conf *ctx)
2272{
2273 hwsim_check_magic(vif);
2274 hwsim_check_chanctx_magic(ctx);
2275
2276 return 0;
2277}
2278
2279static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2280 struct ieee80211_vif *vif,
2281 struct ieee80211_chanctx_conf *ctx)
2282{
2283 hwsim_check_magic(vif);
2284 hwsim_check_chanctx_magic(ctx);
2285}
2286
Ben Greear2155c3f2014-10-22 12:22:58 -07002287static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2288 "tx_pkts_nic",
2289 "tx_bytes_nic",
2290 "rx_pkts_nic",
2291 "rx_bytes_nic",
2292 "d_tx_dropped",
2293 "d_tx_failed",
2294 "d_ps_mode",
2295 "d_group",
Ben Greear2155c3f2014-10-22 12:22:58 -07002296};
2297
2298#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2299
2300static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2301 struct ieee80211_vif *vif,
2302 u32 sset, u8 *data)
2303{
2304 if (sset == ETH_SS_STATS)
2305 memcpy(data, *mac80211_hwsim_gstrings_stats,
2306 sizeof(mac80211_hwsim_gstrings_stats));
2307}
2308
2309static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2310 struct ieee80211_vif *vif, int sset)
2311{
2312 if (sset == ETH_SS_STATS)
2313 return MAC80211_HWSIM_SSTATS_LEN;
2314 return 0;
2315}
2316
2317static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2318 struct ieee80211_vif *vif,
2319 struct ethtool_stats *stats, u64 *data)
2320{
2321 struct mac80211_hwsim_data *ar = hw->priv;
2322 int i = 0;
2323
2324 data[i++] = ar->tx_pkts;
2325 data[i++] = ar->tx_bytes;
2326 data[i++] = ar->rx_pkts;
2327 data[i++] = ar->rx_bytes;
2328 data[i++] = ar->tx_dropped;
2329 data[i++] = ar->tx_failed;
2330 data[i++] = ar->ps;
2331 data[i++] = ar->group;
Ben Greear2155c3f2014-10-22 12:22:58 -07002332
2333 WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2334}
2335
Johannes Berg246ad562016-10-10 12:09:52 +02002336#define HWSIM_COMMON_OPS \
2337 .tx = mac80211_hwsim_tx, \
2338 .start = mac80211_hwsim_start, \
2339 .stop = mac80211_hwsim_stop, \
2340 .add_interface = mac80211_hwsim_add_interface, \
2341 .change_interface = mac80211_hwsim_change_interface, \
2342 .remove_interface = mac80211_hwsim_remove_interface, \
2343 .config = mac80211_hwsim_config, \
2344 .configure_filter = mac80211_hwsim_configure_filter, \
2345 .bss_info_changed = mac80211_hwsim_bss_info_changed, \
2346 .sta_add = mac80211_hwsim_sta_add, \
2347 .sta_remove = mac80211_hwsim_sta_remove, \
2348 .sta_notify = mac80211_hwsim_sta_notify, \
2349 .set_tim = mac80211_hwsim_set_tim, \
2350 .conf_tx = mac80211_hwsim_conf_tx, \
2351 .get_survey = mac80211_hwsim_get_survey, \
2352 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) \
2353 .ampdu_action = mac80211_hwsim_ampdu_action, \
2354 .flush = mac80211_hwsim_flush, \
2355 .get_tsf = mac80211_hwsim_get_tsf, \
2356 .set_tsf = mac80211_hwsim_set_tsf, \
2357 .get_et_sset_count = mac80211_hwsim_get_et_sset_count, \
2358 .get_et_stats = mac80211_hwsim_get_et_stats, \
2359 .get_et_strings = mac80211_hwsim_get_et_strings,
2360
Johannes Berg38ed5042014-01-06 22:11:06 +01002361static const struct ieee80211_ops mac80211_hwsim_ops = {
Johannes Berg246ad562016-10-10 12:09:52 +02002362 HWSIM_COMMON_OPS
Luis R. Rodriguezf74cb0f2010-04-08 11:50:47 -04002363 .sw_scan_start = mac80211_hwsim_sw_scan,
2364 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002365};
2366
Johannes Berg246ad562016-10-10 12:09:52 +02002367static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2368 HWSIM_COMMON_OPS
2369 .hw_scan = mac80211_hwsim_hw_scan,
2370 .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2371 .sw_scan_start = NULL,
2372 .sw_scan_complete = NULL,
2373 .remain_on_channel = mac80211_hwsim_roc,
2374 .cancel_remain_on_channel = mac80211_hwsim_croc,
2375 .add_chanctx = mac80211_hwsim_add_chanctx,
2376 .remove_chanctx = mac80211_hwsim_remove_chanctx,
2377 .change_chanctx = mac80211_hwsim_change_chanctx,
2378 .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2379 .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2380};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03002381
Jukka Rissanen62759362014-10-31 14:48:47 +02002382struct hwsim_new_radio_params {
2383 unsigned int channels;
2384 const char *reg_alpha2;
2385 const struct ieee80211_regdomain *regd;
2386 bool reg_strict;
2387 bool p2p_device;
2388 bool use_chanctx;
2389 bool destroy_on_close;
2390 const char *hwname;
2391 bool no_vif;
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002392 const u8 *perm_addr;
James Prestwood99e3a44b2018-10-17 12:33:06 -07002393 u32 iftypes;
James Prestwoodc0a01892018-10-17 14:16:24 -07002394 u32 *ciphers;
2395 u8 n_ciphers;
Jukka Rissanen62759362014-10-31 14:48:47 +02002396};
2397
2398static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2399 struct genl_info *info)
2400{
2401 if (info)
Jiri Benc92c14d92015-09-22 18:56:43 +02002402 genl_notify(&hwsim_genl_family, mcast_skb, info,
2403 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002404 else
2405 genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2406 HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2407}
2408
Patrik Flyktc4499812014-11-12 16:42:39 +02002409static int append_radio_msg(struct sk_buff *skb, int id,
2410 struct hwsim_new_radio_params *param)
Jukka Rissanen62759362014-10-31 14:48:47 +02002411{
Jukka Rissanen62759362014-10-31 14:48:47 +02002412 int ret;
2413
Jukka Rissanen62759362014-10-31 14:48:47 +02002414 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2415 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002416 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002417
2418 if (param->channels) {
2419 ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2420 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002421 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002422 }
2423
2424 if (param->reg_alpha2) {
2425 ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2426 param->reg_alpha2);
2427 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002428 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002429 }
2430
2431 if (param->regd) {
2432 int i;
2433
Johannes Berg58757552014-12-22 12:51:25 +01002434 for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2435 if (hwsim_world_regdom_custom[i] != param->regd)
2436 continue;
Jukka Rissanen62759362014-10-31 14:48:47 +02002437
Jukka Rissanen62759362014-10-31 14:48:47 +02002438 ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2439 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002440 return ret;
Johannes Berg58757552014-12-22 12:51:25 +01002441 break;
Jukka Rissanen62759362014-10-31 14:48:47 +02002442 }
2443 }
2444
2445 if (param->reg_strict) {
2446 ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2447 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002448 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002449 }
2450
2451 if (param->p2p_device) {
2452 ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2453 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002454 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002455 }
2456
2457 if (param->use_chanctx) {
2458 ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2459 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002460 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002461 }
2462
2463 if (param->hwname) {
2464 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2465 strlen(param->hwname), param->hwname);
2466 if (ret < 0)
Patrik Flyktc4499812014-11-12 16:42:39 +02002467 return ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02002468 }
2469
Patrik Flyktc4499812014-11-12 16:42:39 +02002470 return 0;
Jukka Rissanen62759362014-10-31 14:48:47 +02002471}
2472
Patrik Flyktc4499812014-11-12 16:42:39 +02002473static void hwsim_mcast_new_radio(int id, struct genl_info *info,
Jukka Rissanen62759362014-10-31 14:48:47 +02002474 struct hwsim_new_radio_params *param)
2475{
2476 struct sk_buff *mcast_skb;
Patrik Flyktc4499812014-11-12 16:42:39 +02002477 void *data;
Jukka Rissanen62759362014-10-31 14:48:47 +02002478
Patrik Flyktc4499812014-11-12 16:42:39 +02002479 mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jukka Rissanen62759362014-10-31 14:48:47 +02002480 if (!mcast_skb)
2481 return;
2482
Patrik Flyktc4499812014-11-12 16:42:39 +02002483 data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2484 HWSIM_CMD_NEW_RADIO);
2485 if (!data)
2486 goto out_err;
2487
2488 if (append_radio_msg(mcast_skb, id, param) < 0)
2489 goto out_err;
2490
2491 genlmsg_end(mcast_skb, data);
2492
Jukka Rissanen62759362014-10-31 14:48:47 +02002493 hwsim_mcast_config_msg(mcast_skb, info);
Patrik Flyktc4499812014-11-12 16:42:39 +02002494 return;
2495
2496out_err:
Patrik Flyktc4499812014-11-12 16:42:39 +02002497 nlmsg_free(mcast_skb);
Jukka Rissanen62759362014-10-31 14:48:47 +02002498}
2499
Ilan Peer518ea3c2018-07-06 15:29:05 +03002500static const struct ieee80211_sband_iftype_data he_capa_2ghz = {
2501 /* TODO: should we support other types, e.g., P2P?*/
2502 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2503 .he_cap = {
2504 .has_he = true,
2505 .he_cap_elem = {
2506 .mac_cap_info[0] =
2507 IEEE80211_HE_MAC_CAP0_HTC_HE,
2508 .mac_cap_info[1] =
2509 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002510 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002511 .mac_cap_info[2] =
2512 IEEE80211_HE_MAC_CAP2_BSR |
2513 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2514 IEEE80211_HE_MAC_CAP2_ACK_EN,
2515 .mac_cap_info[3] =
Ilan Peer518ea3c2018-07-06 15:29:05 +03002516 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002517 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002518 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002519 .phy_cap_info[1] =
2520 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2521 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2522 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002523 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002524 .phy_cap_info[2] =
2525 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2526 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2527 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2528 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2529 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2530
2531 /* Leave all the other PHY capability bytes unset, as
2532 * DCM, beam forming, RU and PPE threshold information
2533 * are not supported
2534 */
2535 },
2536 .he_mcs_nss_supp = {
2537 .rx_mcs_80 = cpu_to_le16(0xfffa),
2538 .tx_mcs_80 = cpu_to_le16(0xfffa),
2539 .rx_mcs_160 = cpu_to_le16(0xffff),
2540 .tx_mcs_160 = cpu_to_le16(0xffff),
2541 .rx_mcs_80p80 = cpu_to_le16(0xffff),
2542 .tx_mcs_80p80 = cpu_to_le16(0xffff),
2543 },
2544 },
2545};
2546
2547static const struct ieee80211_sband_iftype_data he_capa_5ghz = {
2548 /* TODO: should we support other types, e.g., P2P?*/
2549 .types_mask = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
2550 .he_cap = {
2551 .has_he = true,
2552 .he_cap_elem = {
2553 .mac_cap_info[0] =
2554 IEEE80211_HE_MAC_CAP0_HTC_HE,
2555 .mac_cap_info[1] =
2556 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002557 IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002558 .mac_cap_info[2] =
2559 IEEE80211_HE_MAC_CAP2_BSR |
2560 IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2561 IEEE80211_HE_MAC_CAP2_ACK_EN,
2562 .mac_cap_info[3] =
Ilan Peer518ea3c2018-07-06 15:29:05 +03002563 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002564 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_VHT_2,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002565 .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU,
2566 .phy_cap_info[0] =
Ilan Peer518ea3c2018-07-06 15:29:05 +03002567 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2568 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2569 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
2570 .phy_cap_info[1] =
2571 IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2572 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2573 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
Shaul Triebitzadd74532018-09-05 08:06:08 +03002574 IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
Ilan Peer518ea3c2018-07-06 15:29:05 +03002575 .phy_cap_info[2] =
2576 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2577 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2578 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2579 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2580 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2581
2582 /* Leave all the other PHY capability bytes unset, as
2583 * DCM, beam forming, RU and PPE threshold information
2584 * are not supported
2585 */
2586 },
2587 .he_mcs_nss_supp = {
2588 .rx_mcs_80 = cpu_to_le16(0xfffa),
2589 .tx_mcs_80 = cpu_to_le16(0xfffa),
2590 .rx_mcs_160 = cpu_to_le16(0xfffa),
2591 .tx_mcs_160 = cpu_to_le16(0xfffa),
2592 .rx_mcs_80p80 = cpu_to_le16(0xfffa),
2593 .tx_mcs_80p80 = cpu_to_le16(0xfffa),
2594 },
2595 },
2596};
2597
2598static void mac80211_hswim_he_capab(struct ieee80211_supported_band *sband)
2599{
2600 if (sband->band == NL80211_BAND_2GHZ)
2601 sband->iftype_data =
2602 (struct ieee80211_sband_iftype_data *)&he_capa_2ghz;
2603 else if (sband->band == NL80211_BAND_5GHZ)
2604 sband->iftype_data =
2605 (struct ieee80211_sband_iftype_data *)&he_capa_5ghz;
2606 else
2607 return;
2608
2609 sband->n_iftype_data = 1;
2610}
2611
James Prestwood99e3a44b2018-10-17 12:33:06 -07002612#ifdef CONFIG_MAC80211_MESH
2613#define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
2614#else
2615#define HWSIM_MESH_BIT 0
2616#endif
2617
2618#define HWSIM_DEFAULT_IF_LIMIT \
2619 (BIT(NL80211_IFTYPE_STATION) | \
2620 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2621 BIT(NL80211_IFTYPE_AP) | \
2622 BIT(NL80211_IFTYPE_P2P_GO) | \
2623 HWSIM_MESH_BIT)
2624
2625#define HWSIM_IFTYPE_SUPPORT_MASK \
2626 (BIT(NL80211_IFTYPE_STATION) | \
2627 BIT(NL80211_IFTYPE_AP) | \
2628 BIT(NL80211_IFTYPE_P2P_CLIENT) | \
2629 BIT(NL80211_IFTYPE_P2P_GO) | \
2630 BIT(NL80211_IFTYPE_ADHOC) | \
2631 BIT(NL80211_IFTYPE_MESH_POINT))
2632
Jukka Rissanen62759362014-10-31 14:48:47 +02002633static int mac80211_hwsim_new_radio(struct genl_info *info,
2634 struct hwsim_new_radio_params *param)
Johannes Bergde0421d2014-01-06 23:12:12 +01002635{
2636 int err;
2637 u8 addr[ETH_ALEN];
2638 struct mac80211_hwsim_data *data;
2639 struct ieee80211_hw *hw;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002640 enum nl80211_band band;
Johannes Bergde0421d2014-01-06 23:12:12 +01002641 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
Martin Willi100cb9f2016-05-09 18:33:59 +02002642 struct net *net;
Johannes Berg45fcef82019-03-29 08:56:22 +01002643 int idx, i;
James Prestwood99e3a44b2018-10-17 12:33:06 -07002644 int n_limits = 0;
Johannes Bergde0421d2014-01-06 23:12:12 +01002645
Jukka Rissanen62759362014-10-31 14:48:47 +02002646 if (WARN_ON(param->channels > 1 && !param->use_chanctx))
Luciano Coelho361c3e02014-02-10 15:23:03 +02002647 return -EINVAL;
2648
Johannes Bergde0421d2014-01-06 23:12:12 +01002649 spin_lock_bh(&hwsim_radio_lock);
2650 idx = hwsim_radio_idx++;
2651 spin_unlock_bh(&hwsim_radio_lock);
2652
Jukka Rissanen62759362014-10-31 14:48:47 +02002653 if (param->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002654 ops = &mac80211_hwsim_mchan_ops;
Jukka Rissanen62759362014-10-31 14:48:47 +02002655 hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
Johannes Bergde0421d2014-01-06 23:12:12 +01002656 if (!hw) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002657 pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
Johannes Bergde0421d2014-01-06 23:12:12 +01002658 err = -ENOMEM;
2659 goto failed;
2660 }
Martin Willi100cb9f2016-05-09 18:33:59 +02002661
Andrew Zaborowskif7d3b4f2017-02-23 13:02:10 +01002662 /* ieee80211_alloc_hw_nm may have used a default name */
2663 param->hwname = wiphy_name(hw->wiphy);
2664
Martin Willi100cb9f2016-05-09 18:33:59 +02002665 if (info)
2666 net = genl_info_net(info);
2667 else
2668 net = &init_net;
2669 wiphy_net_set(hw->wiphy, net);
2670
Johannes Bergde0421d2014-01-06 23:12:12 +01002671 data = hw->priv;
2672 data->hw = hw;
2673
2674 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
2675 if (IS_ERR(data->dev)) {
2676 printk(KERN_DEBUG
2677 "mac80211_hwsim: device_create failed (%ld)\n",
2678 PTR_ERR(data->dev));
2679 err = -ENOMEM;
2680 goto failed_drvdata;
2681 }
2682 data->dev->driver = &mac80211_hwsim_driver.driver;
2683 err = device_bind_driver(data->dev);
2684 if (err != 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002685 pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002686 err);
Junjie Mao805dbe12014-10-28 09:31:47 +08002687 goto failed_bind;
Johannes Bergde0421d2014-01-06 23:12:12 +01002688 }
2689
2690 skb_queue_head_init(&data->pending);
2691
2692 SET_IEEE80211_DEV(hw, data->dev);
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002693 if (!param->perm_addr) {
2694 eth_zero_addr(addr);
2695 addr[0] = 0x02;
2696 addr[3] = idx >> 8;
2697 addr[4] = idx;
2698 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2699 /* Why need here second address ? */
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002700 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
Johannes Berg796e1112018-03-21 09:07:02 +01002701 data->addresses[1].addr[0] |= 0x40;
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01002702 hw->wiphy->n_addresses = 2;
2703 hw->wiphy->addresses = data->addresses;
2704 /* possible address clash is checked at hash table insertion */
2705 } else {
2706 memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
2707 /* compatibility with automatically generated mac addr */
2708 memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
2709 hw->wiphy->n_addresses = 2;
2710 hw->wiphy->addresses = data->addresses;
2711 }
Johannes Bergde0421d2014-01-06 23:12:12 +01002712
Jukka Rissanen62759362014-10-31 14:48:47 +02002713 data->channels = param->channels;
2714 data->use_chanctx = param->use_chanctx;
Johannes Bergbc7910982014-01-06 23:29:20 +01002715 data->idx = idx;
Jukka Rissanen62759362014-10-31 14:48:47 +02002716 data->destroy_on_close = param->destroy_on_close;
2717 if (info)
2718 data->portid = info->snd_portid;
Johannes Bergde0421d2014-01-06 23:12:12 +01002719
James Prestwood99e3a44b2018-10-17 12:33:06 -07002720 /* setup interface limits, only on interface types we support */
2721 if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
2722 data->if_limits[n_limits].max = 1;
2723 data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
2724 n_limits++;
2725 }
2726
2727 if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
2728 data->if_limits[n_limits].max = 2048;
2729 /*
2730 * For this case, we may only support a subset of
2731 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
2732 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
2733 */
2734 data->if_limits[n_limits].types =
2735 HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
2736 n_limits++;
2737 }
2738
2739 if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
2740 data->if_limits[n_limits].max = 1;
2741 data->if_limits[n_limits].types =
2742 BIT(NL80211_IFTYPE_P2P_DEVICE);
2743 n_limits++;
2744 }
2745
Luciano Coelho361c3e02014-02-10 15:23:03 +02002746 if (data->use_chanctx) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002747 hw->wiphy->max_scan_ssids = 255;
2748 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2749 hw->wiphy->max_remain_on_channel_duration = 1000;
Ilan Peerf7736f52016-07-05 15:23:09 +03002750 data->if_combination.radar_detect_widths = 0;
Johannes Bergb59ec8d2014-02-07 10:44:50 +01002751 data->if_combination.num_different_channels = data->channels;
Johannes Bergde0421d2014-01-06 23:12:12 +01002752 } else {
James Prestwood68406a92018-12-11 16:52:14 -08002753 data->if_combination.num_different_channels = 1;
2754 data->if_combination.radar_detect_widths =
James Prestwood99e3a44b2018-10-17 12:33:06 -07002755 BIT(NL80211_CHAN_WIDTH_20_NOHT) |
2756 BIT(NL80211_CHAN_WIDTH_20) |
2757 BIT(NL80211_CHAN_WIDTH_40) |
2758 BIT(NL80211_CHAN_WIDTH_80) |
2759 BIT(NL80211_CHAN_WIDTH_160);
James Prestwood68406a92018-12-11 16:52:14 -08002760 }
2761
Johannes Berg9c5d3af2019-01-15 14:24:23 +01002762 if (!n_limits) {
2763 err = -EINVAL;
2764 goto failed_hw;
2765 }
2766
Johannes Berg45fcef82019-03-29 08:56:22 +01002767 data->if_combination.max_interfaces = 0;
2768 for (i = 0; i < n_limits; i++)
2769 data->if_combination.max_interfaces +=
2770 data->if_limits[i].max;
2771
James Prestwood68406a92018-12-11 16:52:14 -08002772 data->if_combination.n_limits = n_limits;
James Prestwood99e3a44b2018-10-17 12:33:06 -07002773 data->if_combination.limits = data->if_limits;
2774
Johannes Berg45fcef82019-03-29 08:56:22 +01002775 /*
2776 * If we actually were asked to support combinations,
2777 * advertise them - if there's only a single thing like
2778 * only IBSS then don't advertise it as combinations.
2779 */
2780 if (data->if_combination.max_interfaces > 1) {
2781 hw->wiphy->iface_combinations = &data->if_combination;
2782 hw->wiphy->n_iface_combinations = 1;
2783 }
James Prestwood99e3a44b2018-10-17 12:33:06 -07002784
James Prestwoodc0a01892018-10-17 14:16:24 -07002785 if (param->ciphers) {
2786 memcpy(data->ciphers, param->ciphers,
2787 param->n_ciphers * sizeof(u32));
2788 hw->wiphy->cipher_suites = data->ciphers;
2789 hw->wiphy->n_cipher_suites = param->n_ciphers;
Johannes Bergde0421d2014-01-06 23:12:12 +01002790 }
2791
Johannes Berg661ef472015-11-24 18:29:45 +01002792 INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
Johannes Bergde0421d2014-01-06 23:12:12 +01002793 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2794 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
2795
2796 hw->queues = 5;
2797 hw->offchannel_tx_hw_queue = 4;
Johannes Bergde0421d2014-01-06 23:12:12 +01002798
Johannes Berg30686bf2015-06-02 21:39:54 +02002799 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
2800 ieee80211_hw_set(hw, CHANCTX_STA_CSA);
2801 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
2802 ieee80211_hw_set(hw, QUEUE_CONTROL);
2803 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2804 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2805 ieee80211_hw_set(hw, MFP_CAPABLE);
2806 ieee80211_hw_set(hw, SIGNAL_DBM);
Bjoern Johanssone4a2eb82018-04-11 11:41:56 -07002807 ieee80211_hw_set(hw, SUPPORTS_PS);
Johannes Berg33c2f532015-06-17 13:07:14 +02002808 ieee80211_hw_set(hw, TDLS_WIDER_BW);
Alexander Wetzelcfe70072019-04-23 22:47:11 +02002809
2810 /* We only have SW crypto and only implement the A-MPDU API
2811 * (but don't really build A-MPDUs) so can have extended key
2812 * support
2813 */
2814 ieee80211_hw_set(hw, EXT_KEY_ID_NATIVE);
Johannes Bergde0421d2014-01-06 23:12:12 +01002815 if (rctbl)
Johannes Berg30686bf2015-06-02 21:39:54 +02002816 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
Jouni Malinen040bda82018-12-07 14:10:50 +02002817 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
Johannes Bergde0421d2014-01-06 23:12:12 +01002818
2819 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2820 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
Andrei Otcheretianski0037db62013-11-18 09:46:24 +02002821 WIPHY_FLAG_AP_UAPSD |
2822 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Eliad Peller0d8614b2014-09-10 14:07:36 +03002823 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2824 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2825 NL80211_FEATURE_STATIC_SMPS |
Johannes Berg339467b2014-06-12 22:41:00 +02002826 NL80211_FEATURE_DYNAMIC_SMPS |
2827 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
Jouni Malinen00eeccc2015-11-26 20:51:04 +02002828 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
Johannes Bergde0421d2014-01-06 23:12:12 +01002829
James Prestwood99e3a44b2018-10-17 12:33:06 -07002830 hw->wiphy->interface_modes = param->iftypes;
2831
Johannes Bergde0421d2014-01-06 23:12:12 +01002832 /* ask mac80211 to reserve space for magic */
2833 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2834 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2835 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
2836
2837 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2838 sizeof(hwsim_channels_2ghz));
2839 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2840 sizeof(hwsim_channels_5ghz));
2841 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
2842
Johannes Berg57fbcce2016-04-12 15:56:15 +02002843 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
Johannes Bergde0421d2014-01-06 23:12:12 +01002844 struct ieee80211_supported_band *sband = &data->bands[band];
Ilan Peer518ea3c2018-07-06 15:29:05 +03002845
2846 sband->band = band;
2847
Johannes Bergde0421d2014-01-06 23:12:12 +01002848 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02002849 case NL80211_BAND_2GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002850 sband->channels = data->channels_2ghz;
2851 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2852 sband->bitrates = data->rates;
2853 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2854 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002855 case NL80211_BAND_5GHZ:
Johannes Bergde0421d2014-01-06 23:12:12 +01002856 sband->channels = data->channels_5ghz;
2857 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2858 sband->bitrates = data->rates + 4;
2859 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
Ilan Peerf33f8482015-04-07 19:05:21 +03002860
2861 sband->vht_cap.vht_supported = true;
2862 sband->vht_cap.cap =
2863 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2864 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2865 IEEE80211_VHT_CAP_RXLDPC |
2866 IEEE80211_VHT_CAP_SHORT_GI_80 |
2867 IEEE80211_VHT_CAP_SHORT_GI_160 |
2868 IEEE80211_VHT_CAP_TXSTBC |
Ilan Peerf33f8482015-04-07 19:05:21 +03002869 IEEE80211_VHT_CAP_RXSTBC_4 |
2870 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2871 sband->vht_cap.vht_mcs.rx_mcs_map =
Ilan Peer8aca9b22015-04-07 19:05:22 +03002872 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2873 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002874 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002875 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
2876 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
Ilan Peerf33f8482015-04-07 19:05:21 +03002877 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2878 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
Ilan Peer8aca9b22015-04-07 19:05:22 +03002879 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
Ilan Peerf33f8482015-04-07 19:05:21 +03002880 sband->vht_cap.vht_mcs.tx_mcs_map =
2881 sband->vht_cap.vht_mcs.rx_mcs_map;
Johannes Bergde0421d2014-01-06 23:12:12 +01002882 break;
2883 default:
2884 continue;
2885 }
2886
2887 sband->ht_cap.ht_supported = true;
2888 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2889 IEEE80211_HT_CAP_GRN_FLD |
Ben Greeare49786d2014-10-24 11:12:08 -07002890 IEEE80211_HT_CAP_SGI_20 |
Johannes Bergde0421d2014-01-06 23:12:12 +01002891 IEEE80211_HT_CAP_SGI_40 |
2892 IEEE80211_HT_CAP_DSSSCCK40;
2893 sband->ht_cap.ampdu_factor = 0x3;
2894 sband->ht_cap.ampdu_density = 0x6;
2895 memset(&sband->ht_cap.mcs, 0,
2896 sizeof(sband->ht_cap.mcs));
2897 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2898 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2899 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2900
Ilan Peer518ea3c2018-07-06 15:29:05 +03002901 mac80211_hswim_he_capab(sband);
2902
Johannes Bergde0421d2014-01-06 23:12:12 +01002903 hw->wiphy->bands[band] = sband;
Johannes Bergde0421d2014-01-06 23:12:12 +01002904 }
2905
2906 /* By default all radios belong to the first group */
2907 data->group = 1;
2908 mutex_init(&data->mutex);
2909
Martin Willi100cb9f2016-05-09 18:33:59 +02002910 data->netgroup = hwsim_net_get_netgroup(net);
Andrew Zaborowski4ea5aca2018-03-21 08:05:18 +01002911 data->wmediumd = hwsim_net_get_wmediumd(net);
Martin Willi100cb9f2016-05-09 18:33:59 +02002912
Johannes Bergde0421d2014-01-06 23:12:12 +01002913 /* Enable frame retransmissions for lossy channels */
2914 hw->max_rates = 4;
2915 hw->max_rate_tries = 11;
2916
Jouni Malinend5d011b2015-02-26 15:26:54 +02002917 hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
2918 hw->wiphy->n_vendor_commands =
2919 ARRAY_SIZE(mac80211_hwsim_vendor_commands);
2920 hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
2921 hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
2922
Jukka Rissanen62759362014-10-31 14:48:47 +02002923 if (param->reg_strict)
Johannes Berg26b0e412014-01-10 13:37:38 +01002924 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002925 if (param->regd) {
Patrik Flykt93d638d2014-11-12 16:42:40 +02002926 data->regd = param->regd;
Johannes Berg26b0e412014-01-10 13:37:38 +01002927 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Jukka Rissanen62759362014-10-31 14:48:47 +02002928 wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
Johannes Berg26b0e412014-01-10 13:37:38 +01002929 /* give the regulatory workqueue a chance to run */
2930 schedule_timeout_interruptible(1);
2931 }
2932
Jukka Rissanen62759362014-10-31 14:48:47 +02002933 if (param->no_vif)
Johannes Berg30686bf2015-06-02 21:39:54 +02002934 ieee80211_hw_set(hw, NO_AUTO_VIF);
Ben Greear9a0cb892014-10-22 12:23:04 -07002935
Andrew Zaborowskiae44b502017-02-10 04:50:23 +01002936 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2937
Thomas Gleixner77dcc622019-03-01 23:48:19 +01002938 hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
2939 HRTIMER_MODE_ABS_SOFT);
2940 data->beacon_timer.function = mac80211_hwsim_beacon;
Vasyl Vavrychuka1881c92018-10-18 01:02:12 +03002941
Johannes Bergde0421d2014-01-06 23:12:12 +01002942 err = ieee80211_register_hw(hw);
2943 if (err < 0) {
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002944 pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
Johannes Bergde0421d2014-01-06 23:12:12 +01002945 err);
2946 goto failed_hw;
2947 }
2948
Lubomir Rintel62b093b2017-09-18 15:56:51 +02002949 wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
Johannes Bergde0421d2014-01-06 23:12:12 +01002950
Patrik Flykt93d638d2014-11-12 16:42:40 +02002951 if (param->reg_alpha2) {
2952 data->alpha2[0] = param->reg_alpha2[0];
2953 data->alpha2[1] = param->reg_alpha2[1];
Jukka Rissanen62759362014-10-31 14:48:47 +02002954 regulatory_hint(hw->wiphy, param->reg_alpha2);
Patrik Flykt93d638d2014-11-12 16:42:40 +02002955 }
Johannes Berg26b0e412014-01-10 13:37:38 +01002956
Johannes Bergde0421d2014-01-06 23:12:12 +01002957 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2958 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2959 debugfs_create_file("group", 0666, data->debugfs, data,
2960 &hwsim_fops_group);
Luciano Coelho361c3e02014-02-10 15:23:03 +02002961 if (!data->use_chanctx)
Johannes Bergde0421d2014-01-06 23:12:12 +01002962 debugfs_create_file("dfs_simulate_radar", 0222,
2963 data->debugfs,
2964 data, &hwsim_simulate_radar);
2965
Johannes Bergde0421d2014-01-06 23:12:12 +01002966 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002967 err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
2968 hwsim_rht_params);
2969 if (err < 0) {
Benjamin Beichler301b0402018-01-22 18:04:36 +01002970 if (info) {
2971 GENL_SET_ERR_MSG(info, "perm addr already present");
2972 NL_SET_BAD_ATTR(info->extack,
2973 info->attrs[HWSIM_ATTR_PERM_ADDR]);
2974 }
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002975 spin_unlock_bh(&hwsim_radio_lock);
2976 goto failed_final_insert;
2977 }
2978
Johannes Bergde0421d2014-01-06 23:12:12 +01002979 list_add_tail(&data->list, &hwsim_radios);
Benjamin Beichler63356982018-01-22 18:04:37 +01002980 hwsim_radios_generation++;
Johannes Bergde0421d2014-01-06 23:12:12 +01002981 spin_unlock_bh(&hwsim_radio_lock);
2982
Martin Willi28ef8b42018-09-25 09:51:02 +02002983 hwsim_mcast_new_radio(idx, info, param);
Jukka Rissanen62759362014-10-31 14:48:47 +02002984
Johannes Bergbc7910982014-01-06 23:29:20 +01002985 return idx;
Johannes Bergde0421d2014-01-06 23:12:12 +01002986
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01002987failed_final_insert:
2988 debugfs_remove_recursive(data->debugfs);
2989 ieee80211_unregister_hw(data->hw);
Johannes Bergde0421d2014-01-06 23:12:12 +01002990failed_hw:
Junjie Mao805dbe12014-10-28 09:31:47 +08002991 device_release_driver(data->dev);
2992failed_bind:
Johannes Bergde0421d2014-01-06 23:12:12 +01002993 device_unregister(data->dev);
2994failed_drvdata:
2995 ieee80211_free_hw(hw);
2996failed:
2997 return err;
2998}
2999
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003000static void hwsim_mcast_del_radio(int id, const char *hwname,
3001 struct genl_info *info)
Johannes Berge4afb602014-01-06 22:43:11 +01003002{
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003003 struct sk_buff *skb;
3004 void *data;
3005 int ret;
3006
3007 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3008 if (!skb)
3009 return;
3010
3011 data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3012 HWSIM_CMD_DEL_RADIO);
3013 if (!data)
3014 goto error;
3015
3016 ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3017 if (ret < 0)
3018 goto error;
3019
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003020 ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3021 hwname);
3022 if (ret < 0)
3023 goto error;
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003024
3025 genlmsg_end(skb, data);
3026
3027 hwsim_mcast_config_msg(skb, info);
3028
3029 return;
3030
3031error:
3032 nlmsg_free(skb);
3033}
3034
3035static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3036 const char *hwname,
3037 struct genl_info *info)
3038{
3039 hwsim_mcast_del_radio(data->idx, hwname, info);
Johannes Berge4afb602014-01-06 22:43:11 +01003040 debugfs_remove_recursive(data->debugfs);
3041 ieee80211_unregister_hw(data->hw);
3042 device_release_driver(data->dev);
3043 device_unregister(data->dev);
3044 ieee80211_free_hw(data->hw);
3045}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003046
Patrik Flykt93d638d2014-11-12 16:42:40 +02003047static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3048 struct mac80211_hwsim_data *data,
3049 u32 portid, u32 seq,
3050 struct netlink_callback *cb, int flags)
3051{
3052 void *hdr;
3053 struct hwsim_new_radio_params param = { };
3054 int res = -EMSGSIZE;
3055
3056 hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3057 HWSIM_CMD_GET_RADIO);
3058 if (!hdr)
3059 return -EMSGSIZE;
3060
3061 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +01003062 genl_dump_check_consistent(cb, hdr);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003063
Patrik Flyktb0ad5e82014-11-21 15:08:02 +02003064 if (data->alpha2[0] && data->alpha2[1])
3065 param.reg_alpha2 = data->alpha2;
3066
Patrik Flykt93d638d2014-11-12 16:42:40 +02003067 param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3068 REGULATORY_STRICT_REG);
3069 param.p2p_device = !!(data->hw->wiphy->interface_modes &
3070 BIT(NL80211_IFTYPE_P2P_DEVICE));
3071 param.use_chanctx = data->use_chanctx;
3072 param.regd = data->regd;
3073 param.channels = data->channels;
3074 param.hwname = wiphy_name(data->hw->wiphy);
3075
3076 res = append_radio_msg(skb, data->idx, &param);
3077 if (res < 0)
3078 goto out_err;
3079
Johannes Berg053c0952015-01-16 22:09:00 +01003080 genlmsg_end(skb, hdr);
3081 return 0;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003082
3083out_err:
3084 genlmsg_cancel(skb, hdr);
3085 return res;
3086}
3087
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003088static void mac80211_hwsim_free(void)
3089{
Johannes Berge4afb602014-01-06 22:43:11 +01003090 struct mac80211_hwsim_data *data;
Johannes Berg0e057d732008-09-12 00:39:22 +02003091
3092 spin_lock_bh(&hwsim_radio_lock);
Johannes Berge4afb602014-01-06 22:43:11 +01003093 while ((data = list_first_entry_or_null(&hwsim_radios,
3094 struct mac80211_hwsim_data,
3095 list))) {
3096 list_del(&data->list);
3097 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003098 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3099 NULL);
Johannes Berge4afb602014-01-06 22:43:11 +01003100 spin_lock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003101 }
Johannes Berge4afb602014-01-06 22:43:11 +01003102 spin_unlock_bh(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003103 class_destroy(hwsim_class);
3104}
3105
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00003106static const struct net_device_ops hwsim_netdev_ops = {
3107 .ndo_start_xmit = hwsim_mon_xmit,
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00003108 .ndo_set_mac_address = eth_mac_addr,
3109 .ndo_validate_addr = eth_validate_addr,
3110};
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003111
3112static void hwsim_mon_setup(struct net_device *dev)
3113{
Stephen Hemminger98d2faa2009-03-20 19:36:33 +00003114 dev->netdev_ops = &hwsim_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04003115 dev->needs_free_netdev = true;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003116 ether_setup(dev);
Phil Sutter3db6da12015-08-18 10:30:43 +02003117 dev->priv_flags |= IFF_NO_QUEUE;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003118 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Joe Perches93803b32015-03-02 19:54:49 -08003119 eth_zero_addr(dev->dev_addr);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003120 dev->dev_addr[0] = 0x12;
3121}
3122
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003123static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
Javier Lopez78825132011-06-01 11:26:13 +02003124{
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003125 return rhashtable_lookup_fast(&hwsim_radios_rht,
3126 addr,
3127 hwsim_rht_params);
Javier Lopez78825132011-06-01 11:26:13 +02003128}
3129
Martin Willif21e4d82016-05-14 10:34:44 +02003130static void hwsim_register_wmediumd(struct net *net, u32 portid)
3131{
3132 struct mac80211_hwsim_data *data;
3133
3134 hwsim_net_set_wmediumd(net, portid);
3135
3136 spin_lock_bh(&hwsim_radio_lock);
3137 list_for_each_entry(data, &hwsim_radios, list) {
3138 if (data->netgroup == hwsim_net_get_netgroup(net))
3139 data->wmediumd = portid;
3140 }
3141 spin_unlock_bh(&hwsim_radio_lock);
3142}
3143
Javier Lopez78825132011-06-01 11:26:13 +02003144static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
3145 struct genl_info *info)
3146{
3147
3148 struct ieee80211_hdr *hdr;
3149 struct mac80211_hwsim_data *data2;
3150 struct ieee80211_tx_info *txi;
3151 struct hwsim_tx_rate *tx_attempts;
Johannes Berg959eb2f2015-11-06 11:57:23 +01003152 u64 ret_skb_cookie;
Javier Lopez78825132011-06-01 11:26:13 +02003153 struct sk_buff *skb, *tmp;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003154 const u8 *src;
Javier Lopez78825132011-06-01 11:26:13 +02003155 unsigned int hwsim_flags;
Javier Lopez78825132011-06-01 11:26:13 +02003156 int i;
3157 bool found = false;
3158
3159 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003160 !info->attrs[HWSIM_ATTR_FLAGS] ||
3161 !info->attrs[HWSIM_ATTR_COOKIE] ||
Martin Willi62397da2016-05-13 12:41:48 +02003162 !info->attrs[HWSIM_ATTR_SIGNAL] ||
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003163 !info->attrs[HWSIM_ATTR_TX_INFO])
Javier Lopez78825132011-06-01 11:26:13 +02003164 goto out;
3165
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003166 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
Javier Lopez78825132011-06-01 11:26:13 +02003167 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
Johannes Berg959eb2f2015-11-06 11:57:23 +01003168 ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
Javier Lopez78825132011-06-01 11:26:13 +02003169
3170 data2 = get_hwsim_data_ref_from_addr(src);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003171 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003172 goto out;
3173
Martin Willif21e4d82016-05-14 10:34:44 +02003174 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3175 goto out;
3176
3177 if (info->snd_portid != data2->wmediumd)
3178 goto out;
3179
Javier Lopez78825132011-06-01 11:26:13 +02003180 /* look for the skb matching the cookie passed back from user */
3181 skb_queue_walk_safe(&data2->pending, skb, tmp) {
Johannes Berg959eb2f2015-11-06 11:57:23 +01003182 u64 skb_cookie;
3183
3184 txi = IEEE80211_SKB_CB(skb);
3185 skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
3186
3187 if (skb_cookie == ret_skb_cookie) {
Javier Lopez78825132011-06-01 11:26:13 +02003188 skb_unlink(skb, &data2->pending);
3189 found = true;
3190 break;
3191 }
3192 }
3193
3194 /* not found */
3195 if (!found)
3196 goto out;
3197
3198 /* Tx info received because the frame was broadcasted on user space,
3199 so we get all the necessary info: tx attempts and skb control buff */
3200
3201 tx_attempts = (struct hwsim_tx_rate *)nla_data(
3202 info->attrs[HWSIM_ATTR_TX_INFO]);
3203
3204 /* now send back TX status */
3205 txi = IEEE80211_SKB_CB(skb);
3206
Javier Lopez78825132011-06-01 11:26:13 +02003207 ieee80211_tx_info_clear_status(txi);
3208
3209 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3210 txi->status.rates[i].idx = tx_attempts[i].idx;
3211 txi->status.rates[i].count = tx_attempts[i].count;
Javier Lopez78825132011-06-01 11:26:13 +02003212 }
3213
3214 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3215
3216 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
3217 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
3218 if (skb->len >= 16) {
3219 hdr = (struct ieee80211_hdr *) skb->data;
Javier Lopezc0e30762013-12-16 10:01:07 -08003220 mac80211_hwsim_monitor_ack(data2->channel,
Johannes Berge8261172012-07-27 19:48:26 +02003221 hdr->addr2);
Javier Lopez78825132011-06-01 11:26:13 +02003222 }
Qasim Javed06cf5c42012-06-05 01:25:44 -05003223 txi->flags |= IEEE80211_TX_STAT_ACK;
Javier Lopez78825132011-06-01 11:26:13 +02003224 }
3225 ieee80211_tx_status_irqsafe(data2->hw, skb);
3226 return 0;
3227out:
3228 return -EINVAL;
3229
3230}
3231
3232static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
3233 struct genl_info *info)
3234{
Johannes Berge8261172012-07-27 19:48:26 +02003235 struct mac80211_hwsim_data *data2;
Javier Lopez78825132011-06-01 11:26:13 +02003236 struct ieee80211_rx_status rx_status;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003237 const u8 *dst;
Javier Lopez78825132011-06-01 11:26:13 +02003238 int frame_data_len;
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003239 void *frame_data;
Javier Lopez78825132011-06-01 11:26:13 +02003240 struct sk_buff *skb = NULL;
3241
3242 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
Johannes Berge8261172012-07-27 19:48:26 +02003243 !info->attrs[HWSIM_ATTR_FRAME] ||
3244 !info->attrs[HWSIM_ATTR_RX_RATE] ||
3245 !info->attrs[HWSIM_ATTR_SIGNAL])
Javier Lopez78825132011-06-01 11:26:13 +02003246 goto out;
3247
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003248 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
Javier Lopez78825132011-06-01 11:26:13 +02003249 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003250 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
Javier Lopez78825132011-06-01 11:26:13 +02003251
3252 /* Allocate new skb here */
3253 skb = alloc_skb(frame_data_len, GFP_KERNEL);
3254 if (skb == NULL)
3255 goto err;
3256
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003257 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
Javier Lopez78825132011-06-01 11:26:13 +02003258 goto err;
3259
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003260 /* Copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02003261 skb_put_data(skb, frame_data, frame_data_len);
Javier Lopez78825132011-06-01 11:26:13 +02003262
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003263 data2 = get_hwsim_data_ref_from_addr(dst);
3264 if (!data2)
Javier Lopez78825132011-06-01 11:26:13 +02003265 goto out;
3266
Martin Willif21e4d82016-05-14 10:34:44 +02003267 if (hwsim_net_get_netgroup(genl_info_net(info)) != data2->netgroup)
3268 goto out;
3269
3270 if (info->snd_portid != data2->wmediumd)
3271 goto out;
3272
Javier Lopez78825132011-06-01 11:26:13 +02003273 /* check if radio is configured properly */
3274
Johannes Berge8261172012-07-27 19:48:26 +02003275 if (data2->idle || !data2->started)
Javier Lopez78825132011-06-01 11:26:13 +02003276 goto out;
3277
Johannes Berg9ddd12a2014-01-06 23:03:02 +01003278 /* A frame is received from user space */
Javier Lopez78825132011-06-01 11:26:13 +02003279 memset(&rx_status, 0, sizeof(rx_status));
Adam Welle641cf2a2015-12-01 17:13:52 -05003280 if (info->attrs[HWSIM_ATTR_FREQ]) {
3281 /* throw away off-channel packets, but allow both the temporary
3282 * ("hw" scan/remain-on-channel) and regular channel, since the
3283 * internal datapath also allows this
3284 */
3285 mutex_lock(&data2->mutex);
3286 rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
3287
3288 if (rx_status.freq != data2->channel->center_freq &&
3289 (!data2->tmp_chan ||
3290 rx_status.freq != data2->tmp_chan->center_freq)) {
3291 mutex_unlock(&data2->mutex);
3292 goto out;
3293 }
3294 mutex_unlock(&data2->mutex);
3295 } else {
3296 rx_status.freq = data2->channel->center_freq;
3297 }
3298
Javier Lopez78825132011-06-01 11:26:13 +02003299 rx_status.band = data2->channel->band;
3300 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
3301 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
3302
3303 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
Ben Greear2155c3f2014-10-22 12:22:58 -07003304 data2->rx_pkts++;
3305 data2->rx_bytes += skb->len;
Javier Lopez78825132011-06-01 11:26:13 +02003306 ieee80211_rx_irqsafe(data2->hw, skb);
3307
3308 return 0;
3309err:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003310 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003311out:
3312 dev_kfree_skb(skb);
3313 return -EINVAL;
3314}
3315
3316static int hwsim_register_received_nl(struct sk_buff *skb_2,
3317 struct genl_info *info)
3318{
Martin Willif21e4d82016-05-14 10:34:44 +02003319 struct net *net = genl_info_net(info);
Johannes Bergc5ac0852014-01-07 23:28:08 +01003320 struct mac80211_hwsim_data *data;
3321 int chans = 1;
3322
3323 spin_lock_bh(&hwsim_radio_lock);
3324 list_for_each_entry(data, &hwsim_radios, list)
3325 chans = max(chans, data->channels);
3326 spin_unlock_bh(&hwsim_radio_lock);
3327
3328 /* In the future we should revise the userspace API and allow it
3329 * to set a flag that it does support multi-channel, then we can
3330 * let this pass conditionally on the flag.
3331 * For current userspace, prohibit it since it won't work right.
3332 */
3333 if (chans > 1)
3334 return -EOPNOTSUPP;
3335
Martin Willif21e4d82016-05-14 10:34:44 +02003336 if (hwsim_net_get_wmediumd(net))
Johannes Berg85ca8fc2014-01-07 23:21:34 +01003337 return -EBUSY;
Javier Lopez78825132011-06-01 11:26:13 +02003338
Martin Willif21e4d82016-05-14 10:34:44 +02003339 hwsim_register_wmediumd(net, info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003340
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003341 pr_debug("mac80211_hwsim: received a REGISTER, "
Eric W. Biederman15e47302012-09-07 20:12:54 +00003342 "switching to wmediumd mode with pid %d\n", info->snd_portid);
Javier Lopez78825132011-06-01 11:26:13 +02003343
3344 return 0;
Javier Lopez78825132011-06-01 11:26:13 +02003345}
3346
James Prestwoodc0a01892018-10-17 14:16:24 -07003347/* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
3348static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
3349{
3350 int i;
3351
3352 for (i = 0; i < n_ciphers; i++) {
3353 int j;
3354 int found = 0;
3355
3356 for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
3357 if (ciphers[i] == hwsim_ciphers[j]) {
3358 found = 1;
3359 break;
3360 }
3361 }
3362
3363 if (!found)
3364 return false;
3365 }
3366
3367 return true;
3368}
3369
Jukka Rissanen62759362014-10-31 14:48:47 +02003370static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003371{
Jukka Rissanen62759362014-10-31 14:48:47 +02003372 struct hwsim_new_radio_params param = { 0 };
Johannes Bergff4dd732017-02-27 17:15:28 +01003373 const char *hwname = NULL;
Ben Hutchings67bd5232017-11-10 18:48:50 +00003374 int ret;
Jukka Rissanen62759362014-10-31 14:48:47 +02003375
3376 param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
3377 param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
3378 param.channels = channels;
3379 param.destroy_on_close =
3380 info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
Johannes Bergbc7910982014-01-06 23:29:20 +01003381
3382 if (info->attrs[HWSIM_ATTR_CHANNELS])
Jukka Rissanen62759362014-10-31 14:48:47 +02003383 param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
Johannes Bergbc7910982014-01-06 23:29:20 +01003384
Johannes Berg48400432018-08-15 18:17:03 +02003385 if (param.channels < 1) {
3386 GENL_SET_ERR_MSG(info, "must have at least one channel");
3387 return -EINVAL;
3388 }
3389
Johannes Berg51a1aaa2018-01-15 09:32:36 +01003390 if (param.channels > CFG80211_MAX_NUM_DIFFERENT_CHANNELS) {
3391 GENL_SET_ERR_MSG(info, "too many channels specified");
3392 return -EINVAL;
3393 }
3394
Ben Greear9a0cb892014-10-22 12:23:04 -07003395 if (info->attrs[HWSIM_ATTR_NO_VIF])
Jukka Rissanen62759362014-10-31 14:48:47 +02003396 param.no_vif = true;
Ben Greear9a0cb892014-10-22 12:23:04 -07003397
Luciano Coelho361c3e02014-02-10 15:23:03 +02003398 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
Jukka Rissanen62759362014-10-31 14:48:47 +02003399 param.use_chanctx = true;
Luciano Coelho361c3e02014-02-10 15:23:03 +02003400 else
Jukka Rissanen62759362014-10-31 14:48:47 +02003401 param.use_chanctx = (param.channels > 1);
Luciano Coelho361c3e02014-02-10 15:23:03 +02003402
Johannes Berg26b0e412014-01-10 13:37:38 +01003403 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
Jukka Rissanen62759362014-10-31 14:48:47 +02003404 param.reg_alpha2 =
3405 nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
Johannes Berg26b0e412014-01-10 13:37:38 +01003406
3407 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
3408 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
3409
Johannes Berg2f98abb2018-11-09 11:32:39 +01003410 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
Johannes Berg26b0e412014-01-10 13:37:38 +01003411 return -EINVAL;
Jinbum Park3a2af7c2018-07-31 23:10:40 +09003412
3413 idx = array_index_nospec(idx,
3414 ARRAY_SIZE(hwsim_world_regdom_custom));
Jukka Rissanen62759362014-10-31 14:48:47 +02003415 param.regd = hwsim_world_regdom_custom[idx];
Johannes Berg26b0e412014-01-10 13:37:38 +01003416 }
3417
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01003418 if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
3419 if (!is_valid_ether_addr(
3420 nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
3421 GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
3422 NL_SET_BAD_ATTR(info->extack,
3423 info->attrs[HWSIM_ATTR_PERM_ADDR]);
3424 return -EINVAL;
3425 }
3426
Benjamin Beichlercb1a5ba2018-01-22 18:04:35 +01003427 param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
3428 }
3429
James Prestwood99e3a44b2018-10-17 12:33:06 -07003430 if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
3431 param.iftypes =
3432 nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
3433
3434 if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
3435 NL_SET_ERR_MSG_ATTR(info->extack,
3436 info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
3437 "cannot support more iftypes than kernel");
3438 return -EINVAL;
3439 }
3440 } else {
3441 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
3442 }
3443
3444 /* ensure both flag and iftype support is honored */
3445 if (param.p2p_device ||
3446 param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3447 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
3448 param.p2p_device = true;
3449 }
3450
James Prestwoodc0a01892018-10-17 14:16:24 -07003451 if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
3452 u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3453
3454 param.ciphers =
3455 nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
3456
3457 if (len % sizeof(u32)) {
3458 NL_SET_ERR_MSG_ATTR(info->extack,
3459 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3460 "bad cipher list length");
3461 return -EINVAL;
3462 }
3463
3464 param.n_ciphers = len / sizeof(u32);
3465
3466 if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
3467 NL_SET_ERR_MSG_ATTR(info->extack,
3468 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3469 "too many ciphers specified");
3470 return -EINVAL;
3471 }
3472
3473 if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
3474 NL_SET_ERR_MSG_ATTR(info->extack,
3475 info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
3476 "unsupported ciphers specified");
3477 return -EINVAL;
3478 }
3479 }
3480
Johannes Berg2f98abb2018-11-09 11:32:39 +01003481 if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3482 hwname = kasprintf(GFP_KERNEL, "%.*s",
3483 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3484 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3485 if (!hwname)
3486 return -ENOMEM;
3487 param.hwname = hwname;
3488 }
3489
Ben Hutchings67bd5232017-11-10 18:48:50 +00003490 ret = mac80211_hwsim_new_radio(info, &param);
3491 kfree(hwname);
3492 return ret;
Johannes Bergbc7910982014-01-06 23:29:20 +01003493}
3494
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003495static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
Johannes Bergbc7910982014-01-06 23:29:20 +01003496{
3497 struct mac80211_hwsim_data *data;
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003498 s64 idx = -1;
3499 const char *hwname = NULL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003500
Johannes Bergff4dd732017-02-27 17:15:28 +01003501 if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003502 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
Johannes Bergff4dd732017-02-27 17:15:28 +01003503 } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
3504 hwname = kasprintf(GFP_KERNEL, "%.*s",
3505 nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
3506 (char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]));
3507 if (!hwname)
3508 return -ENOMEM;
3509 } else
Johannes Bergbc7910982014-01-06 23:29:20 +01003510 return -EINVAL;
Johannes Bergbc7910982014-01-06 23:29:20 +01003511
3512 spin_lock_bh(&hwsim_radio_lock);
3513 list_for_each_entry(data, &hwsim_radios, list) {
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003514 if (idx >= 0) {
3515 if (data->idx != idx)
3516 continue;
3517 } else {
Johannes Bergff4dd732017-02-27 17:15:28 +01003518 if (!hwname ||
3519 strcmp(hwname, wiphy_name(data->hw->wiphy)))
Ben Greear8cdd9e1c2014-10-22 12:22:59 -07003520 continue;
3521 }
3522
Martin Willi100cb9f2016-05-09 18:33:59 +02003523 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3524 continue;
3525
Johannes Bergbc7910982014-01-06 23:29:20 +01003526 list_del(&data->list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003527 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3528 hwsim_rht_params);
Benjamin Beichler63356982018-01-22 18:04:37 +01003529 hwsim_radios_generation++;
Johannes Bergbc7910982014-01-06 23:29:20 +01003530 spin_unlock_bh(&hwsim_radio_lock);
Jukka Rissanenb9995f82014-11-14 13:16:34 +02003531 mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
3532 info);
Johannes Bergff4dd732017-02-27 17:15:28 +01003533 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003534 return 0;
3535 }
3536 spin_unlock_bh(&hwsim_radio_lock);
3537
Johannes Bergff4dd732017-02-27 17:15:28 +01003538 kfree(hwname);
Johannes Bergbc7910982014-01-06 23:29:20 +01003539 return -ENODEV;
Javier Lopez78825132011-06-01 11:26:13 +02003540}
3541
Patrik Flykt93d638d2014-11-12 16:42:40 +02003542static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
3543{
3544 struct mac80211_hwsim_data *data;
3545 struct sk_buff *skb;
3546 int idx, res = -ENODEV;
3547
3548 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
3549 return -EINVAL;
3550 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
3551
3552 spin_lock_bh(&hwsim_radio_lock);
3553 list_for_each_entry(data, &hwsim_radios, list) {
3554 if (data->idx != idx)
3555 continue;
3556
Martin Willi100cb9f2016-05-09 18:33:59 +02003557 if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
3558 continue;
3559
Jia-Ju Bai162bd5e2017-12-12 17:26:36 +08003560 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003561 if (!skb) {
3562 res = -ENOMEM;
3563 goto out_err;
3564 }
3565
3566 res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
3567 info->snd_seq, NULL, 0);
3568 if (res < 0) {
3569 nlmsg_free(skb);
3570 goto out_err;
3571 }
3572
Li RongQing17407712019-02-19 13:12:40 +08003573 res = genlmsg_reply(skb, info);
Patrik Flykt93d638d2014-11-12 16:42:40 +02003574 break;
3575 }
3576
3577out_err:
3578 spin_unlock_bh(&hwsim_radio_lock);
3579
3580 return res;
3581}
3582
3583static int hwsim_dump_radio_nl(struct sk_buff *skb,
3584 struct netlink_callback *cb)
3585{
Andrew Zaborowskifed48252018-05-22 02:43:56 +02003586 int last_idx = cb->args[0] - 1;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003587 struct mac80211_hwsim_data *data = NULL;
Benjamin Beichler63356982018-01-22 18:04:37 +01003588 int res = 0;
3589 void *hdr;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003590
3591 spin_lock_bh(&hwsim_radio_lock);
Benjamin Beichler63356982018-01-22 18:04:37 +01003592 cb->seq = hwsim_radios_generation;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003593
Benjamin Beichler63356982018-01-22 18:04:37 +01003594 if (last_idx >= hwsim_radio_idx-1)
Patrik Flykt93d638d2014-11-12 16:42:40 +02003595 goto done;
3596
3597 list_for_each_entry(data, &hwsim_radios, list) {
Benjamin Beichler63356982018-01-22 18:04:37 +01003598 if (data->idx <= last_idx)
Patrik Flykt93d638d2014-11-12 16:42:40 +02003599 continue;
3600
Martin Willi100cb9f2016-05-09 18:33:59 +02003601 if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
3602 continue;
3603
Patrik Flykt93d638d2014-11-12 16:42:40 +02003604 res = mac80211_hwsim_get_radio(skb, data,
3605 NETLINK_CB(cb->skb).portid,
3606 cb->nlh->nlmsg_seq, cb,
3607 NLM_F_MULTI);
3608 if (res < 0)
3609 break;
3610
Benjamin Beichler63356982018-01-22 18:04:37 +01003611 last_idx = data->idx;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003612 }
3613
Andrew Zaborowskifed48252018-05-22 02:43:56 +02003614 cb->args[0] = last_idx + 1;
Benjamin Beichler63356982018-01-22 18:04:37 +01003615
3616 /* list changed, but no new element sent, set interrupted flag */
3617 if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
3618 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3619 cb->nlh->nlmsg_seq, &hwsim_genl_family,
3620 NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
3621 if (!hdr)
3622 res = -EMSGSIZE;
3623 genl_dump_check_consistent(cb, hdr);
3624 genlmsg_end(skb, hdr);
3625 }
Patrik Flykt93d638d2014-11-12 16:42:40 +02003626
3627done:
3628 spin_unlock_bh(&hwsim_radio_lock);
Benjamin Beichler63356982018-01-22 18:04:37 +01003629 return res ?: skb->len;
Patrik Flykt93d638d2014-11-12 16:42:40 +02003630}
3631
Javier Lopez78825132011-06-01 11:26:13 +02003632/* Generic Netlink operations array */
Johannes Berg4534de82013-11-14 17:14:46 +01003633static const struct genl_ops hwsim_ops[] = {
Javier Lopez78825132011-06-01 11:26:13 +02003634 {
3635 .cmd = HWSIM_CMD_REGISTER,
Johannes Bergef6243a2019-04-26 14:07:31 +02003636 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Javier Lopez78825132011-06-01 11:26:13 +02003637 .doit = hwsim_register_received_nl,
Martin Willif21e4d82016-05-14 10:34:44 +02003638 .flags = GENL_UNS_ADMIN_PERM,
Javier Lopez78825132011-06-01 11:26:13 +02003639 },
3640 {
3641 .cmd = HWSIM_CMD_FRAME,
Johannes Bergef6243a2019-04-26 14:07:31 +02003642 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Javier Lopez78825132011-06-01 11:26:13 +02003643 .doit = hwsim_cloned_frame_received_nl,
3644 },
3645 {
3646 .cmd = HWSIM_CMD_TX_INFO_FRAME,
Johannes Bergef6243a2019-04-26 14:07:31 +02003647 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Javier Lopez78825132011-06-01 11:26:13 +02003648 .doit = hwsim_tx_info_frame_received_nl,
3649 },
Johannes Bergbc7910982014-01-06 23:29:20 +01003650 {
Jukka Rissanen62759362014-10-31 14:48:47 +02003651 .cmd = HWSIM_CMD_NEW_RADIO,
Johannes Bergef6243a2019-04-26 14:07:31 +02003652 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jukka Rissanen62759362014-10-31 14:48:47 +02003653 .doit = hwsim_new_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003654 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003655 },
3656 {
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003657 .cmd = HWSIM_CMD_DEL_RADIO,
Johannes Bergef6243a2019-04-26 14:07:31 +02003658 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jukka Rissanen579a05f2014-10-31 14:48:48 +02003659 .doit = hwsim_del_radio_nl,
Martin Willi100cb9f2016-05-09 18:33:59 +02003660 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergbc7910982014-01-06 23:29:20 +01003661 },
Patrik Flykt93d638d2014-11-12 16:42:40 +02003662 {
3663 .cmd = HWSIM_CMD_GET_RADIO,
Johannes Bergef6243a2019-04-26 14:07:31 +02003664 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Patrik Flykt93d638d2014-11-12 16:42:40 +02003665 .doit = hwsim_get_radio_nl,
3666 .dumpit = hwsim_dump_radio_nl,
3667 },
Javier Lopez78825132011-06-01 11:26:13 +02003668};
3669
Johannes Berg56989f62016-10-24 14:40:05 +02003670static struct genl_family hwsim_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02003671 .name = "MAC80211_HWSIM",
3672 .version = 1,
3673 .maxattr = HWSIM_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01003674 .policy = hwsim_genl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02003675 .netnsok = true,
3676 .module = THIS_MODULE,
3677 .ops = hwsim_ops,
3678 .n_ops = ARRAY_SIZE(hwsim_ops),
3679 .mcgrps = hwsim_mcgrps,
3680 .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
3681};
3682
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003683static void remove_user_radios(u32 portid)
3684{
3685 struct mac80211_hwsim_data *entry, *tmp;
Martin Willif1c47eb2018-09-25 09:41:14 +02003686 LIST_HEAD(list);
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003687
3688 spin_lock_bh(&hwsim_radio_lock);
3689 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
3690 if (entry->destroy_on_close && entry->portid == portid) {
Martin Willif1c47eb2018-09-25 09:41:14 +02003691 list_move(&entry->list, &list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003692 rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
3693 hwsim_rht_params);
Martin Willif1c47eb2018-09-25 09:41:14 +02003694 hwsim_radios_generation++;
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003695 }
3696 }
3697 spin_unlock_bh(&hwsim_radio_lock);
Martin Willif1c47eb2018-09-25 09:41:14 +02003698
3699 list_for_each_entry_safe(entry, tmp, &list, list) {
3700 list_del(&entry->list);
3701 mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
3702 NULL);
3703 }
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003704}
3705
Javier Lopez78825132011-06-01 11:26:13 +02003706static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
3707 unsigned long state,
3708 void *_notify)
3709{
3710 struct netlink_notify *notify = _notify;
3711
3712 if (state != NETLINK_URELEASE)
3713 return NOTIFY_DONE;
3714
Jukka Rissanene9ed49b2014-10-09 15:27:51 +03003715 remove_user_radios(notify->portid);
3716
Martin Willif21e4d82016-05-14 10:34:44 +02003717 if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
Javier Lopez78825132011-06-01 11:26:13 +02003718 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
3719 " socket, switching to perfect channel medium\n");
Martin Willif21e4d82016-05-14 10:34:44 +02003720 hwsim_register_wmediumd(notify->net, 0);
Javier Lopez78825132011-06-01 11:26:13 +02003721 }
3722 return NOTIFY_DONE;
3723
3724}
3725
3726static struct notifier_block hwsim_netlink_notifier = {
3727 .notifier_call = mac80211_hwsim_netlink_notify,
3728};
3729
Johannes Berg56989f62016-10-24 14:40:05 +02003730static int __init hwsim_init_netlink(void)
Javier Lopez78825132011-06-01 11:26:13 +02003731{
3732 int rc;
Johannes Berge8261172012-07-27 19:48:26 +02003733
Javier Lopez78825132011-06-01 11:26:13 +02003734 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
3735
Johannes Berg489111e2016-10-24 14:40:03 +02003736 rc = genl_register_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003737 if (rc)
3738 goto failure;
3739
3740 rc = netlink_register_notifier(&hwsim_netlink_notifier);
Su Kang Yin2459cd82015-08-07 16:54:10 +08003741 if (rc) {
3742 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003743 goto failure;
Su Kang Yin2459cd82015-08-07 16:54:10 +08003744 }
Javier Lopez78825132011-06-01 11:26:13 +02003745
3746 return 0;
3747
3748failure:
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003749 pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
Javier Lopez78825132011-06-01 11:26:13 +02003750 return -EINVAL;
3751}
3752
Martin Willi100cb9f2016-05-09 18:33:59 +02003753static __net_init int hwsim_init_net(struct net *net)
3754{
Kirill Tkhai03695542018-03-01 14:30:09 +03003755 return hwsim_net_set_netgroup(net);
Martin Willi100cb9f2016-05-09 18:33:59 +02003756}
3757
3758static void __net_exit hwsim_exit_net(struct net *net)
3759{
3760 struct mac80211_hwsim_data *data, *tmp;
Martin Willi628980e2018-09-25 09:41:13 +02003761 LIST_HEAD(list);
Martin Willi100cb9f2016-05-09 18:33:59 +02003762
3763 spin_lock_bh(&hwsim_radio_lock);
3764 list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
3765 if (!net_eq(wiphy_net(data->hw->wiphy), net))
3766 continue;
3767
3768 /* Radios created in init_net are returned to init_net. */
3769 if (data->netgroup == hwsim_net_get_netgroup(&init_net))
3770 continue;
3771
Martin Willi628980e2018-09-25 09:41:13 +02003772 list_move(&data->list, &list);
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003773 rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
3774 hwsim_rht_params);
Benjamin Beichler8cfd36a2018-03-07 18:11:07 +01003775 hwsim_radios_generation++;
Martin Willi628980e2018-09-25 09:41:13 +02003776 }
3777 spin_unlock_bh(&hwsim_radio_lock);
3778
3779 list_for_each_entry_safe(data, tmp, &list, list) {
3780 list_del(&data->list);
Benjamin Beichler8cfd36a2018-03-07 18:11:07 +01003781 mac80211_hwsim_del_radio(data,
3782 wiphy_name(data->hw->wiphy),
3783 NULL);
Martin Willi100cb9f2016-05-09 18:33:59 +02003784 }
Kirill Tkhai03695542018-03-01 14:30:09 +03003785
3786 ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
Martin Willi100cb9f2016-05-09 18:33:59 +02003787}
3788
3789static struct pernet_operations hwsim_net_ops = {
3790 .init = hwsim_init_net,
3791 .exit = hwsim_exit_net,
3792 .id = &hwsim_net_id,
3793 .size = sizeof(struct hwsim_net),
3794};
3795
Javier Lopez78825132011-06-01 11:26:13 +02003796static void hwsim_exit_netlink(void)
3797{
Javier Lopez78825132011-06-01 11:26:13 +02003798 /* unregister the notifier */
3799 netlink_unregister_notifier(&hwsim_netlink_notifier);
3800 /* unregister the family */
Johannes Bergcf7a6b22014-01-06 21:55:14 +01003801 genl_unregister_family(&hwsim_genl_family);
Javier Lopez78825132011-06-01 11:26:13 +02003802}
3803
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003804static int __init init_mac80211_hwsim(void)
3805{
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003806 int i, err;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003807
Johannes Bergbc7910982014-01-06 23:29:20 +01003808 if (radios < 0 || radios > 100)
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003809 return -EINVAL;
3810
Johannes Berge8261172012-07-27 19:48:26 +02003811 if (channels < 1)
3812 return -EINVAL;
3813
Johannes Berg0e057d732008-09-12 00:39:22 +02003814 spin_lock_init(&hwsim_radio_lock);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003815
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003816 err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
3817 if (err)
Martin Willi1edcfc22018-09-25 09:41:15 +02003818 return err;
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003819
Martin Willi100cb9f2016-05-09 18:33:59 +02003820 err = register_pernet_device(&hwsim_net_ops);
Martin Pitt9ea927742013-04-08 11:30:01 +02003821 if (err)
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003822 goto out_free_rht;
Martin Pitt9ea927742013-04-08 11:30:01 +02003823
Martin Willi100cb9f2016-05-09 18:33:59 +02003824 err = platform_driver_register(&mac80211_hwsim_driver);
3825 if (err)
3826 goto out_unregister_pernet;
3827
Alexey Khoroshilov05cc09d2018-10-05 23:22:06 +03003828 err = hwsim_init_netlink();
3829 if (err)
3830 goto out_unregister_driver;
3831
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003832 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
Martin Pitt9ea927742013-04-08 11:30:01 +02003833 if (IS_ERR(hwsim_class)) {
3834 err = PTR_ERR(hwsim_class);
Alexey Khoroshilov05cc09d2018-10-05 23:22:06 +03003835 goto out_exit_netlink;
Martin Pitt9ea927742013-04-08 11:30:01 +02003836 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003837
Johannes Berg0e057d732008-09-12 00:39:22 +02003838 for (i = 0; i < radios; i++) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003839 struct hwsim_new_radio_params param = { 0 };
3840
3841 param.channels = channels;
Johannes Berg0e057d732008-09-12 00:39:22 +02003842
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003843 switch (regtest) {
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003844 case HWSIM_REGTEST_DIFF_COUNTRY:
Johannes Berg26b0e412014-01-10 13:37:38 +01003845 if (i < ARRAY_SIZE(hwsim_alpha2s))
Jukka Rissanen62759362014-10-31 14:48:47 +02003846 param.reg_alpha2 = hwsim_alpha2s[i];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003847 break;
3848 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
3849 if (!i)
Jukka Rissanen62759362014-10-31 14:48:47 +02003850 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003851 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003852 case HWSIM_REGTEST_STRICT_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003853 param.reg_strict = true;
Johannes Berg26b0e412014-01-10 13:37:38 +01003854 case HWSIM_REGTEST_DRIVER_REG_ALL:
Jukka Rissanen62759362014-10-31 14:48:47 +02003855 param.reg_alpha2 = hwsim_alpha2s[0];
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003856 break;
Johannes Berg26b0e412014-01-10 13:37:38 +01003857 case HWSIM_REGTEST_WORLD_ROAM:
3858 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003859 param.regd = &hwsim_world_regdom_custom_01;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003860 break;
3861 case HWSIM_REGTEST_CUSTOM_WORLD:
Jukka Rissanen62759362014-10-31 14:48:47 +02003862 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003863 break;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003864 case HWSIM_REGTEST_CUSTOM_WORLD_2:
Johannes Berg26b0e412014-01-10 13:37:38 +01003865 if (i == 0)
Jukka Rissanen62759362014-10-31 14:48:47 +02003866 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003867 else if (i == 1)
Jukka Rissanen62759362014-10-31 14:48:47 +02003868 param.regd = &hwsim_world_regdom_custom_02;
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003869 break;
3870 case HWSIM_REGTEST_STRICT_FOLLOW:
Johannes Berg26b0e412014-01-10 13:37:38 +01003871 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003872 param.reg_strict = true;
3873 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003874 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003875 break;
3876 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
Johannes Berg26b0e412014-01-10 13:37:38 +01003877 if (i == 0) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003878 param.reg_strict = true;
3879 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003880 } else if (i == 1) {
Jukka Rissanen62759362014-10-31 14:48:47 +02003881 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003882 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003883 break;
3884 case HWSIM_REGTEST_ALL:
Johannes Berg26b0e412014-01-10 13:37:38 +01003885 switch (i) {
3886 case 0:
Jukka Rissanen62759362014-10-31 14:48:47 +02003887 param.regd = &hwsim_world_regdom_custom_01;
Johannes Berg26b0e412014-01-10 13:37:38 +01003888 break;
3889 case 1:
Jukka Rissanen62759362014-10-31 14:48:47 +02003890 param.regd = &hwsim_world_regdom_custom_02;
Johannes Berg26b0e412014-01-10 13:37:38 +01003891 break;
3892 case 2:
Jukka Rissanen62759362014-10-31 14:48:47 +02003893 param.reg_alpha2 = hwsim_alpha2s[0];
Johannes Berg26b0e412014-01-10 13:37:38 +01003894 break;
3895 case 3:
Jukka Rissanen62759362014-10-31 14:48:47 +02003896 param.reg_alpha2 = hwsim_alpha2s[1];
Johannes Berg26b0e412014-01-10 13:37:38 +01003897 break;
3898 case 4:
Jukka Rissanen62759362014-10-31 14:48:47 +02003899 param.reg_strict = true;
3900 param.reg_alpha2 = hwsim_alpha2s[2];
Johannes Berg26b0e412014-01-10 13:37:38 +01003901 break;
3902 }
Luis R. Rodriguez4a5af9c2009-03-09 22:08:27 -04003903 break;
3904 default:
3905 break;
3906 }
3907
Jukka Rissanen62759362014-10-31 14:48:47 +02003908 param.p2p_device = support_p2p_device;
3909 param.use_chanctx = channels > 1;
James Prestwood082b12d2018-11-13 11:26:42 -08003910 param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
Avraham Stern05383952019-03-15 17:39:01 +02003911 if (param.p2p_device)
3912 param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
Jukka Rissanen62759362014-10-31 14:48:47 +02003913
3914 err = mac80211_hwsim_new_radio(NULL, &param);
Johannes Bergbc7910982014-01-06 23:29:20 +01003915 if (err < 0)
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003916 goto out_free_radios;
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003917 }
3918
Tom Gundersenc835a672014-07-14 16:37:24 +02003919 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
3920 hwsim_mon_setup);
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003921 if (hwsim_mon == NULL) {
3922 err = -ENOMEM;
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003923 goto out_free_radios;
Wei Yongjunbcdd8222013-08-26 15:32:58 +08003924 }
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003925
Javier Lopez78825132011-06-01 11:26:13 +02003926 rtnl_lock();
Javier Lopez78825132011-06-01 11:26:13 +02003927 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003928 if (err < 0) {
3929 rtnl_unlock();
3930 goto out_free_radios;
3931 }
Javier Lopez78825132011-06-01 11:26:13 +02003932
3933 err = register_netdevice(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003934 if (err < 0) {
3935 rtnl_unlock();
3936 goto out_free_mon;
3937 }
Javier Lopez78825132011-06-01 11:26:13 +02003938 rtnl_unlock();
3939
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003940 return 0;
3941
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003942out_free_mon:
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003943 free_netdev(hwsim_mon);
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003944out_free_radios:
Ian Schram3a33cc12008-07-21 13:19:35 -07003945 mac80211_hwsim_free();
Alexey Khoroshilov05cc09d2018-10-05 23:22:06 +03003946out_exit_netlink:
3947 hwsim_exit_netlink();
Johannes Bergf39c2bf2014-01-06 22:39:27 +01003948out_unregister_driver:
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003949 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003950out_unregister_pernet:
3951 unregister_pernet_device(&hwsim_net_ops);
Johannes Berg3f61b7a2018-05-29 12:04:51 +02003952out_free_rht:
3953 rhashtable_destroy(&hwsim_radios_rht);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003954 return err;
3955}
Johannes Bergf8fffc72012-07-27 15:16:02 +02003956module_init(init_mac80211_hwsim);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003957
3958static void __exit exit_mac80211_hwsim(void)
3959{
Lubomir Rintel62b093b2017-09-18 15:56:51 +02003960 pr_debug("mac80211_hwsim: unregister radios\n");
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003961
Javier Lopez78825132011-06-01 11:26:13 +02003962 hwsim_exit_netlink();
3963
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003964 mac80211_hwsim_free();
Benjamin Beichlerb71d8562018-01-10 17:42:51 +01003965
Benjamin Beichlerc6509cc2018-01-10 17:42:52 +01003966 rhashtable_destroy(&hwsim_radios_rht);
Johannes Berg5d416352009-07-13 13:04:30 +02003967 unregister_netdev(hwsim_mon);
Sasha Levinc07fe5a2013-04-24 00:40:05 -04003968 platform_driver_unregister(&mac80211_hwsim_driver);
Martin Willi100cb9f2016-05-09 18:33:59 +02003969 unregister_pernet_device(&hwsim_net_ops);
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003970}
Jouni Malinenacc1e7a2008-06-11 10:42:31 +03003971module_exit(exit_mac80211_hwsim);