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