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