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