]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/mac80211_hwsim.c
mac80211: expose API allowing station iteration
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / mac80211_hwsim.c
CommitLineData
acc1e7a3
JM
1/*
2 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
7882513b 4 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
acc1e7a3
JM
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/*
12 * TODO:
2b2d7795
JB
13 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
acc1e7a3
JM
15 * - RX filtering based on filter configuration (data->rx_filter)
16 */
17
0e057d73 18#include <linux/list.h>
5a0e3ad6 19#include <linux/slab.h>
0e057d73 20#include <linux/spinlock.h>
90e3012e
JB
21#include <net/dst.h>
22#include <net/xfrm.h>
acc1e7a3
JM
23#include <net/mac80211.h>
24#include <net/ieee80211_radiotap.h>
25#include <linux/if_arp.h>
26#include <linux/rtnetlink.h>
27#include <linux/etherdevice.h>
9ea92774 28#include <linux/platform_device.h>
fc6971d4 29#include <linux/debugfs.h>
9d9779e7 30#include <linux/module.h>
2f40b940 31#include <linux/ktime.h>
7882513b
JL
32#include <net/genetlink.h>
33#include "mac80211_hwsim.h"
34
35#define WARN_QUEUE 100
36#define MAX_QUEUE 200
acc1e7a3
JM
37
38MODULE_AUTHOR("Jouni Malinen");
39MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
40MODULE_LICENSE("GPL");
41
15e47304 42static u32 wmediumd_portid;
a1910f9c 43
acc1e7a3
JM
44static int radios = 2;
45module_param(radios, int, 0444);
46MODULE_PARM_DESC(radios, "Number of simulated radios");
47
e8261171
JB
48static int channels = 1;
49module_param(channels, int, 0444);
50MODULE_PARM_DESC(channels, "Number of concurrent channels");
69068036 51
a357d7f9
JB
52static bool paged_rx = false;
53module_param(paged_rx, bool, 0644);
54MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
55
1eb32179
KB
56static bool rctbl = false;
57module_param(rctbl, bool, 0444);
58MODULE_PARM_DESC(rctbl, "Handle rate control table");
59
8c66a3d9
JB
60static bool support_p2p_device = true;
61module_param(support_p2p_device, bool, 0444);
62MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
63
4a5af9c2
LR
64/**
65 * enum hwsim_regtest - the type of regulatory tests we offer
66 *
67 * These are the different values you can use for the regtest
68 * module parameter. This is useful to help test world roaming
69 * and the driver regulatory_hint() call and combinations of these.
70 * If you want to do specific alpha2 regulatory domain tests simply
71 * use the userspace regulatory request as that will be respected as
72 * well without the need of this module parameter. This is designed
73 * only for testing the driver regulatory request, world roaming
74 * and all possible combinations.
75 *
76 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
77 * this is the default value.
78 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
79 * hint, only one driver regulatory hint will be sent as such the
80 * secondary radios are expected to follow.
81 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
82 * request with all radios reporting the same regulatory domain.
83 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
84 * different regulatory domains requests. Expected behaviour is for
85 * an intersection to occur but each device will still use their
86 * respective regulatory requested domains. Subsequent radios will
87 * use the resulting intersection.
25985edc 88 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
4a5af9c2
LR
89 * this by using a custom beacon-capable regulatory domain for the first
90 * radio. All other device world roam.
91 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
92 * domain requests. All radios will adhere to this custom world regulatory
93 * domain.
94 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
95 * domain requests. The first radio will adhere to the first custom world
96 * regulatory domain, the second one to the second custom world regulatory
97 * domain. All other devices will world roam.
98 * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
99 * settings, only the first radio will send a regulatory domain request
100 * and use strict settings. The rest of the radios are expected to follow.
101 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
102 * settings. All radios will adhere to this.
103 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
104 * domain settings, combined with secondary driver regulatory domain
105 * settings. The first radio will get a strict regulatory domain setting
106 * using the first driver regulatory request and the second radio will use
107 * non-strict settings using the second driver regulatory request. All
108 * other devices should follow the intersection created between the
109 * first two.
110 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
111 * at least 6 radios for a complete test. We will test in this order:
112 * 1 - driver custom world regulatory domain
113 * 2 - second custom world regulatory domain
114 * 3 - first driver regulatory domain request
115 * 4 - second driver regulatory domain request
116 * 5 - strict regulatory domain settings using the third driver regulatory
117 * domain request
118 * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
119 * regulatory requests.
120 */
121enum hwsim_regtest {
122 HWSIM_REGTEST_DISABLED = 0,
123 HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
124 HWSIM_REGTEST_DRIVER_REG_ALL = 2,
125 HWSIM_REGTEST_DIFF_COUNTRY = 3,
126 HWSIM_REGTEST_WORLD_ROAM = 4,
127 HWSIM_REGTEST_CUSTOM_WORLD = 5,
128 HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
129 HWSIM_REGTEST_STRICT_FOLLOW = 7,
130 HWSIM_REGTEST_STRICT_ALL = 8,
131 HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
132 HWSIM_REGTEST_ALL = 10,
133};
134
135/* Set to one of the HWSIM_REGTEST_* values above */
136static int regtest = HWSIM_REGTEST_DISABLED;
137module_param(regtest, int, 0444);
138MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
139
140static const char *hwsim_alpha2s[] = {
141 "FI",
142 "AL",
143 "US",
144 "DE",
145 "JP",
146 "AL",
147};
148
149static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
150 .n_reg_rules = 4,
151 .alpha2 = "99",
152 .reg_rules = {
153 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
154 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
155 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
156 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
157 }
158};
159
160static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
161 .n_reg_rules = 2,
162 .alpha2 = "99",
163 .reg_rules = {
164 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
165 REG_RULE(5725-10, 5850+10, 40, 0, 30,
8fe02e16 166 NL80211_RRF_NO_IR),
4a5af9c2
LR
167 }
168};
169
26b0e411
JB
170static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
171 &hwsim_world_regdom_custom_01,
172 &hwsim_world_regdom_custom_02,
173};
174
8aa21e6f
JB
175struct hwsim_vif_priv {
176 u32 magic;
fc6971d4
JM
177 u8 bssid[ETH_ALEN];
178 bool assoc;
0bb861e6 179 bool bcn_en;
fc6971d4 180 u16 aid;
8aa21e6f
JB
181};
182
183#define HWSIM_VIF_MAGIC 0x69537748
184
185static inline void hwsim_check_magic(struct ieee80211_vif *vif)
186{
187 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
e8261171
JB
188 WARN(vp->magic != HWSIM_VIF_MAGIC,
189 "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
190 vif, vp->magic, vif->addr, vif->type, vif->p2p);
8aa21e6f
JB
191}
192
193static inline void hwsim_set_magic(struct ieee80211_vif *vif)
194{
195 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
196 vp->magic = HWSIM_VIF_MAGIC;
197}
198
199static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
200{
201 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
202 vp->magic = 0;
203}
acc1e7a3 204
81c06523
JB
205struct hwsim_sta_priv {
206 u32 magic;
207};
208
e8261171 209#define HWSIM_STA_MAGIC 0x6d537749
81c06523
JB
210
211static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
212{
213 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
5d6924ea 214 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
81c06523
JB
215}
216
217static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
218{
219 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
5d6924ea 220 sp->magic = HWSIM_STA_MAGIC;
81c06523
JB
221}
222
223static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
224{
225 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
226 sp->magic = 0;
227}
228
e8261171
JB
229struct hwsim_chanctx_priv {
230 u32 magic;
231};
232
233#define HWSIM_CHANCTX_MAGIC 0x6d53774a
234
235static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
236{
237 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
238 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
239}
240
241static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
242{
243 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
244 cp->magic = HWSIM_CHANCTX_MAGIC;
245}
246
247static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
248{
249 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
250 cp->magic = 0;
251}
252
acc1e7a3
JM
253static struct class *hwsim_class;
254
acc1e7a3
JM
255static struct net_device *hwsim_mon; /* global monitor netdev */
256
22cad735
LR
257#define CHAN2G(_freq) { \
258 .band = IEEE80211_BAND_2GHZ, \
259 .center_freq = (_freq), \
260 .hw_value = (_freq), \
261 .max_power = 20, \
262}
263
264#define CHAN5G(_freq) { \
265 .band = IEEE80211_BAND_5GHZ, \
266 .center_freq = (_freq), \
267 .hw_value = (_freq), \
268 .max_power = 20, \
269}
270
271static const struct ieee80211_channel hwsim_channels_2ghz[] = {
272 CHAN2G(2412), /* Channel 1 */
273 CHAN2G(2417), /* Channel 2 */
274 CHAN2G(2422), /* Channel 3 */
275 CHAN2G(2427), /* Channel 4 */
276 CHAN2G(2432), /* Channel 5 */
277 CHAN2G(2437), /* Channel 6 */
278 CHAN2G(2442), /* Channel 7 */
279 CHAN2G(2447), /* Channel 8 */
280 CHAN2G(2452), /* Channel 9 */
281 CHAN2G(2457), /* Channel 10 */
282 CHAN2G(2462), /* Channel 11 */
283 CHAN2G(2467), /* Channel 12 */
284 CHAN2G(2472), /* Channel 13 */
285 CHAN2G(2484), /* Channel 14 */
286};
acc1e7a3 287
22cad735
LR
288static const struct ieee80211_channel hwsim_channels_5ghz[] = {
289 CHAN5G(5180), /* Channel 36 */
290 CHAN5G(5200), /* Channel 40 */
291 CHAN5G(5220), /* Channel 44 */
292 CHAN5G(5240), /* Channel 48 */
293
294 CHAN5G(5260), /* Channel 52 */
295 CHAN5G(5280), /* Channel 56 */
296 CHAN5G(5300), /* Channel 60 */
297 CHAN5G(5320), /* Channel 64 */
298
299 CHAN5G(5500), /* Channel 100 */
300 CHAN5G(5520), /* Channel 104 */
301 CHAN5G(5540), /* Channel 108 */
302 CHAN5G(5560), /* Channel 112 */
303 CHAN5G(5580), /* Channel 116 */
304 CHAN5G(5600), /* Channel 120 */
305 CHAN5G(5620), /* Channel 124 */
306 CHAN5G(5640), /* Channel 128 */
307 CHAN5G(5660), /* Channel 132 */
308 CHAN5G(5680), /* Channel 136 */
309 CHAN5G(5700), /* Channel 140 */
310
311 CHAN5G(5745), /* Channel 149 */
312 CHAN5G(5765), /* Channel 153 */
313 CHAN5G(5785), /* Channel 157 */
314 CHAN5G(5805), /* Channel 161 */
315 CHAN5G(5825), /* Channel 165 */
acc1e7a3
JM
316};
317
318static const struct ieee80211_rate hwsim_rates[] = {
319 { .bitrate = 10 },
320 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
321 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
322 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
323 { .bitrate = 60 },
324 { .bitrate = 90 },
325 { .bitrate = 120 },
326 { .bitrate = 180 },
327 { .bitrate = 240 },
328 { .bitrate = 360 },
329 { .bitrate = 480 },
330 { .bitrate = 540 }
331};
332
de0421d5
JB
333static const struct ieee80211_iface_limit hwsim_if_limits[] = {
334 { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
335 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
336 BIT(NL80211_IFTYPE_P2P_CLIENT) |
337#ifdef CONFIG_MAC80211_MESH
338 BIT(NL80211_IFTYPE_MESH_POINT) |
339#endif
340 BIT(NL80211_IFTYPE_AP) |
341 BIT(NL80211_IFTYPE_P2P_GO) },
8c66a3d9
JB
342 /* must be last, see hwsim_if_comb */
343 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }
de0421d5
JB
344};
345
346static const struct ieee80211_iface_limit hwsim_if_dfs_limits[] = {
347 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
348};
349
350static const struct ieee80211_iface_combination hwsim_if_comb[] = {
8c66a3d9
JB
351 {
352 .limits = hwsim_if_limits,
353 /* remove the last entry which is P2P_DEVICE */
354 .n_limits = ARRAY_SIZE(hwsim_if_limits) - 1,
355 .max_interfaces = 2048,
356 .num_different_channels = 1,
357 },
358 {
359 .limits = hwsim_if_dfs_limits,
360 .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits),
361 .max_interfaces = 8,
362 .num_different_channels = 1,
363 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
364 BIT(NL80211_CHAN_WIDTH_20) |
365 BIT(NL80211_CHAN_WIDTH_40) |
366 BIT(NL80211_CHAN_WIDTH_80) |
367 BIT(NL80211_CHAN_WIDTH_160),
368 }
369};
370
371static const struct ieee80211_iface_combination hwsim_if_comb_p2p_dev[] = {
de0421d5
JB
372 {
373 .limits = hwsim_if_limits,
374 .n_limits = ARRAY_SIZE(hwsim_if_limits),
375 .max_interfaces = 2048,
376 .num_different_channels = 1,
377 },
378 {
379 .limits = hwsim_if_dfs_limits,
380 .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits),
381 .max_interfaces = 8,
382 .num_different_channels = 1,
383 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
384 BIT(NL80211_CHAN_WIDTH_20) |
385 BIT(NL80211_CHAN_WIDTH_40) |
386 BIT(NL80211_CHAN_WIDTH_80) |
387 BIT(NL80211_CHAN_WIDTH_160),
388 }
389};
390
0e057d73
JB
391static spinlock_t hwsim_radio_lock;
392static struct list_head hwsim_radios;
2d68992b 393static int hwsim_radio_idx;
0e057d73 394
de0421d5
JB
395static struct platform_driver mac80211_hwsim_driver = {
396 .driver = {
397 .name = "mac80211_hwsim",
398 .owner = THIS_MODULE,
399 },
400};
0e057d73 401
acc1e7a3 402struct mac80211_hwsim_data {
0e057d73
JB
403 struct list_head list;
404 struct ieee80211_hw *hw;
acc1e7a3 405 struct device *dev;
1b083ea4 406 struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
22cad735
LR
407 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
408 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
acc1e7a3 409 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
38ed5048 410 struct ieee80211_iface_combination if_combination;
acc1e7a3 411
ef15aac6 412 struct mac_address addresses[2];
bc791098 413 int channels, idx;
361c3e04 414 bool use_chanctx;
e9ed49bf
JR
415 bool destroy_on_close;
416 struct work_struct destroy_work;
417 u32 portid;
ef15aac6 418
e8261171
JB
419 struct ieee80211_channel *tmp_chan;
420 struct delayed_work roc_done;
421 struct delayed_work hw_scan;
422 struct cfg80211_scan_request *hw_scan_request;
423 struct ieee80211_vif *hw_scan_vif;
424 int scan_chan_idx;
425
acc1e7a3 426 struct ieee80211_channel *channel;
01e59e46 427 u64 beacon_int /* beacon interval in us */;
acc1e7a3 428 unsigned int rx_filter;
f74cb0f7
LR
429 bool started, idle, scanning;
430 struct mutex mutex;
01e59e46 431 struct tasklet_hrtimer beacon_timer;
fc6971d4
JM
432 enum ps_mode {
433 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
434 } ps;
435 bool ps_poll_pending;
436 struct dentry *debugfs;
73606d00 437
7882513b 438 struct sk_buff_head pending; /* packets pending */
73606d00
DW
439 /*
440 * Only radios in the same group can communicate together (the
441 * channel has to match too). Each bit represents a group. A
9ebac15f 442 * radio can be in more than one group.
73606d00
DW
443 */
444 u64 group;
bdd7bd16
BG
445
446 int power_level;
2f40b940
JC
447
448 /* difference between this hw's clock and the real clock, in usecs */
45034bfb 449 s64 tsf_offset;
c51f8783 450 s64 bcn_delta;
b66851c3
TP
451 /* absolute beacon transmission time. Used to cover up "tx" delay. */
452 u64 abs_bcn_ts;
acc1e7a3
JM
453};
454
455
456struct hwsim_radiotap_hdr {
457 struct ieee80211_radiotap_header hdr;
2f40b940 458 __le64 rt_tsft;
acc1e7a3
JM
459 u8 rt_flags;
460 u8 rt_rate;
461 __le16 rt_channel;
462 __le16 rt_chbitmask;
ba2d3587 463} __packed;
acc1e7a3 464
76a56eb3
JM
465struct hwsim_radiotap_ack_hdr {
466 struct ieee80211_radiotap_header hdr;
467 u8 rt_flags;
468 u8 pad;
469 __le16 rt_channel;
470 __le16 rt_chbitmask;
471} __packed;
472
7882513b
JL
473/* MAC80211_HWSIM netlinf family */
474static struct genl_family hwsim_genl_family = {
475 .id = GENL_ID_GENERATE,
476 .hdrsize = 0,
477 .name = "MAC80211_HWSIM",
478 .version = 1,
479 .maxattr = HWSIM_ATTR_MAX,
480};
481
482/* MAC80211_HWSIM netlink policy */
483
205d2429 484static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
9ddd12af
JB
485 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
486 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
7882513b
JL
487 [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
488 .len = IEEE80211_MAX_DATA_LEN },
489 [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
490 [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
491 [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
492 [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
9ddd12af
JB
493 .len = IEEE80211_TX_MAX_RATES *
494 sizeof(struct hwsim_tx_rate)},
7882513b 495 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
bc791098
JB
496 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
497 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
26b0e411
JB
498 [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
499 [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
500 [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
8c66a3d9 501 [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
e9ed49bf 502 [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
7882513b 503};
acc1e7a3 504
de0421d5
JB
505static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
506 struct sk_buff *skb,
507 struct ieee80211_channel *chan);
508
509/* sysfs attributes */
510static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
511{
512 struct mac80211_hwsim_data *data = dat;
513 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
514 struct sk_buff *skb;
515 struct ieee80211_pspoll *pspoll;
516
517 if (!vp->assoc)
518 return;
519
520 wiphy_debug(data->hw->wiphy,
521 "%s: send PS-Poll to %pM for aid %d\n",
522 __func__, vp->bssid, vp->aid);
523
524 skb = dev_alloc_skb(sizeof(*pspoll));
525 if (!skb)
526 return;
527 pspoll = (void *) skb_put(skb, sizeof(*pspoll));
528 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
529 IEEE80211_STYPE_PSPOLL |
530 IEEE80211_FCTL_PM);
531 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
532 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
533 memcpy(pspoll->ta, mac, ETH_ALEN);
534
535 rcu_read_lock();
536 mac80211_hwsim_tx_frame(data->hw, skb,
537 rcu_dereference(vif->chanctx_conf)->def.chan);
538 rcu_read_unlock();
539}
540
541static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
542 struct ieee80211_vif *vif, int ps)
543{
544 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
545 struct sk_buff *skb;
546 struct ieee80211_hdr *hdr;
547
548 if (!vp->assoc)
549 return;
550
551 wiphy_debug(data->hw->wiphy,
552 "%s: send data::nullfunc to %pM ps=%d\n",
553 __func__, vp->bssid, ps);
554
555 skb = dev_alloc_skb(sizeof(*hdr));
556 if (!skb)
557 return;
558 hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
559 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
560 IEEE80211_STYPE_NULLFUNC |
561 (ps ? IEEE80211_FCTL_PM : 0));
562 hdr->duration_id = cpu_to_le16(0);
563 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
564 memcpy(hdr->addr2, mac, ETH_ALEN);
565 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
566
567 rcu_read_lock();
568 mac80211_hwsim_tx_frame(data->hw, skb,
569 rcu_dereference(vif->chanctx_conf)->def.chan);
570 rcu_read_unlock();
571}
572
573
574static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
575 struct ieee80211_vif *vif)
576{
577 struct mac80211_hwsim_data *data = dat;
578 hwsim_send_nullfunc(data, mac, vif, 1);
579}
580
581static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
582 struct ieee80211_vif *vif)
583{
584 struct mac80211_hwsim_data *data = dat;
585 hwsim_send_nullfunc(data, mac, vif, 0);
586}
587
588static int hwsim_fops_ps_read(void *dat, u64 *val)
589{
590 struct mac80211_hwsim_data *data = dat;
591 *val = data->ps;
592 return 0;
593}
594
595static int hwsim_fops_ps_write(void *dat, u64 val)
596{
597 struct mac80211_hwsim_data *data = dat;
598 enum ps_mode old_ps;
599
600 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
601 val != PS_MANUAL_POLL)
602 return -EINVAL;
603
604 old_ps = data->ps;
605 data->ps = val;
606
607 if (val == PS_MANUAL_POLL) {
608 ieee80211_iterate_active_interfaces(data->hw,
609 IEEE80211_IFACE_ITER_NORMAL,
610 hwsim_send_ps_poll, data);
611 data->ps_poll_pending = true;
612 } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
613 ieee80211_iterate_active_interfaces(data->hw,
614 IEEE80211_IFACE_ITER_NORMAL,
615 hwsim_send_nullfunc_ps,
616 data);
617 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
618 ieee80211_iterate_active_interfaces(data->hw,
619 IEEE80211_IFACE_ITER_NORMAL,
620 hwsim_send_nullfunc_no_ps,
621 data);
622 }
623
624 return 0;
625}
626
627DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
628 "%llu\n");
629
630static int hwsim_write_simulate_radar(void *dat, u64 val)
631{
632 struct mac80211_hwsim_data *data = dat;
633
634 ieee80211_radar_detected(data->hw);
635
636 return 0;
637}
638
639DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
640 hwsim_write_simulate_radar, "%llu\n");
641
642static int hwsim_fops_group_read(void *dat, u64 *val)
643{
644 struct mac80211_hwsim_data *data = dat;
645 *val = data->group;
646 return 0;
647}
648
649static int hwsim_fops_group_write(void *dat, u64 val)
650{
651 struct mac80211_hwsim_data *data = dat;
652 data->group = val;
653 return 0;
654}
655
656DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
657 hwsim_fops_group_read, hwsim_fops_group_write,
658 "%llx\n");
659
d0cf9c0d
SH
660static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
661 struct net_device *dev)
acc1e7a3
JM
662{
663 /* TODO: allow packet injection */
664 dev_kfree_skb(skb);
6ed10654 665 return NETDEV_TX_OK;
acc1e7a3
JM
666}
667
b66851c3
TP
668static inline u64 mac80211_hwsim_get_tsf_raw(void)
669{
670 return ktime_to_us(ktime_get_real());
671}
672
2f40b940
JC
673static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
674{
b66851c3 675 u64 now = mac80211_hwsim_get_tsf_raw();
2f40b940
JC
676 return cpu_to_le64(now + data->tsf_offset);
677}
acc1e7a3 678
12ce8ba3 679static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
b66851c3 680 struct ieee80211_vif *vif)
12ce8ba3
JC
681{
682 struct mac80211_hwsim_data *data = hw->priv;
683 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
684}
685
686static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
687 struct ieee80211_vif *vif, u64 tsf)
688{
689 struct mac80211_hwsim_data *data = hw->priv;
45034bfb 690 u64 now = mac80211_hwsim_get_tsf(hw, vif);
c51f8783 691 u32 bcn_int = data->beacon_int;
5d26b508 692 u64 delta = abs64(tsf - now);
45034bfb 693
c51f8783 694 /* adjust after beaconing with new timestamp at old TBTT */
5d26b508
AB
695 if (tsf > now) {
696 data->tsf_offset += delta;
697 data->bcn_delta = do_div(delta, bcn_int);
698 } else {
699 data->tsf_offset -= delta;
700 data->bcn_delta = -do_div(delta, bcn_int);
701 }
12ce8ba3
JC
702}
703
acc1e7a3 704static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
e8261171
JB
705 struct sk_buff *tx_skb,
706 struct ieee80211_channel *chan)
acc1e7a3
JM
707{
708 struct mac80211_hwsim_data *data = hw->priv;
709 struct sk_buff *skb;
710 struct hwsim_radiotap_hdr *hdr;
711 u16 flags;
712 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
713 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
714
715 if (!netif_running(hwsim_mon))
716 return;
717
718 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
719 if (skb == NULL)
720 return;
721
722 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
723 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
724 hdr->hdr.it_pad = 0;
725 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
f248f105
JM
726 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
727 (1 << IEEE80211_RADIOTAP_RATE) |
2f40b940 728 (1 << IEEE80211_RADIOTAP_TSFT) |
f248f105 729 (1 << IEEE80211_RADIOTAP_CHANNEL));
2f40b940 730 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
acc1e7a3
JM
731 hdr->rt_flags = 0;
732 hdr->rt_rate = txrate->bitrate / 5;
e8261171 733 hdr->rt_channel = cpu_to_le16(chan->center_freq);
acc1e7a3
JM
734 flags = IEEE80211_CHAN_2GHZ;
735 if (txrate->flags & IEEE80211_RATE_ERP_G)
736 flags |= IEEE80211_CHAN_OFDM;
737 else
738 flags |= IEEE80211_CHAN_CCK;
739 hdr->rt_chbitmask = cpu_to_le16(flags);
740
741 skb->dev = hwsim_mon;
742 skb_set_mac_header(skb, 0);
743 skb->ip_summed = CHECKSUM_UNNECESSARY;
744 skb->pkt_type = PACKET_OTHERHOST;
f248f105 745 skb->protocol = htons(ETH_P_802_2);
acc1e7a3
JM
746 memset(skb->cb, 0, sizeof(skb->cb));
747 netif_rx(skb);
748}
749
750
e8261171
JB
751static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
752 const u8 *addr)
6c085227 753{
6c085227 754 struct sk_buff *skb;
76a56eb3 755 struct hwsim_radiotap_ack_hdr *hdr;
6c085227
JM
756 u16 flags;
757 struct ieee80211_hdr *hdr11;
758
759 if (!netif_running(hwsim_mon))
760 return;
761
762 skb = dev_alloc_skb(100);
763 if (skb == NULL)
764 return;
765
76a56eb3 766 hdr = (struct hwsim_radiotap_ack_hdr *) skb_put(skb, sizeof(*hdr));
6c085227
JM
767 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
768 hdr->hdr.it_pad = 0;
769 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
770 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
771 (1 << IEEE80211_RADIOTAP_CHANNEL));
772 hdr->rt_flags = 0;
76a56eb3 773 hdr->pad = 0;
e8261171 774 hdr->rt_channel = cpu_to_le16(chan->center_freq);
6c085227
JM
775 flags = IEEE80211_CHAN_2GHZ;
776 hdr->rt_chbitmask = cpu_to_le16(flags);
777
778 hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
779 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
780 IEEE80211_STYPE_ACK);
781 hdr11->duration_id = cpu_to_le16(0);
782 memcpy(hdr11->addr1, addr, ETH_ALEN);
783
784 skb->dev = hwsim_mon;
785 skb_set_mac_header(skb, 0);
786 skb->ip_summed = CHECKSUM_UNNECESSARY;
787 skb->pkt_type = PACKET_OTHERHOST;
788 skb->protocol = htons(ETH_P_802_2);
789 memset(skb->cb, 0, sizeof(skb->cb));
790 netif_rx(skb);
791}
792
3283e286
JB
793struct mac80211_hwsim_addr_match_data {
794 u8 addr[ETH_ALEN];
795 bool ret;
796};
797
798static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
799 struct ieee80211_vif *vif)
800{
801 struct mac80211_hwsim_addr_match_data *md = data;
802
803 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
804 md->ret = true;
805}
806
807static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
808 const u8 *addr)
809{
810 struct mac80211_hwsim_addr_match_data md = {
811 .ret = false,
812 };
813
814 memcpy(md.addr, addr, ETH_ALEN);
815
816 ieee80211_iterate_active_interfaces_atomic(data->hw,
817 IEEE80211_IFACE_ITER_NORMAL,
818 mac80211_hwsim_addr_iter,
819 &md);
820
821 return md.ret;
822}
6c085227 823
fc6971d4
JM
824static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
825 struct sk_buff *skb)
826{
827 switch (data->ps) {
828 case PS_DISABLED:
829 return true;
830 case PS_ENABLED:
831 return false;
832 case PS_AUTO_POLL:
833 /* TODO: accept (some) Beacons by default and other frames only
834 * if pending PS-Poll has been sent */
835 return true;
836 case PS_MANUAL_POLL:
837 /* Allow unicast frames to own address if there is a pending
838 * PS-Poll */
839 if (data->ps_poll_pending &&
3283e286 840 mac80211_hwsim_addr_match(data, skb->data + 4)) {
fc6971d4
JM
841 data->ps_poll_pending = false;
842 return true;
843 }
844 return false;
845 }
846
847 return true;
848}
849
7882513b
JL
850static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
851 struct sk_buff *my_skb,
15e47304 852 int dst_portid)
7882513b
JL
853{
854 struct sk_buff *skb;
855 struct mac80211_hwsim_data *data = hw->priv;
856 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
857 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
858 void *msg_head;
859 unsigned int hwsim_flags = 0;
860 int i;
861 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
862
7882513b
JL
863 if (data->ps != PS_DISABLED)
864 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
865 /* If the queue contains MAX_QUEUE skb's drop some */
866 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
867 /* Droping until WARN_QUEUE level */
868 while (skb_queue_len(&data->pending) >= WARN_QUEUE)
b7bc9679 869 ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
7882513b
JL
870 }
871
58050fce 872 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
7882513b
JL
873 if (skb == NULL)
874 goto nla_put_failure;
875
876 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
877 HWSIM_CMD_FRAME);
878 if (msg_head == NULL) {
879 printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
880 goto nla_put_failure;
881 }
882
633c9389 883 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
9ddd12af 884 ETH_ALEN, data->addresses[1].addr))
633c9389 885 goto nla_put_failure;
265dc7f0 886
7882513b 887 /* We get the skb->data */
633c9389
DM
888 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
889 goto nla_put_failure;
7882513b
JL
890
891 /* We get the flags for this transmission, and we translate them to
892 wmediumd flags */
893
894 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
895 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
896
897 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
898 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
899
633c9389
DM
900 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
901 goto nla_put_failure;
7882513b
JL
902
903 /* We get the tx control (rate and retries) info*/
904
905 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
906 tx_attempts[i].idx = info->status.rates[i].idx;
907 tx_attempts[i].count = info->status.rates[i].count;
908 }
909
633c9389
DM
910 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
911 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
912 tx_attempts))
913 goto nla_put_failure;
7882513b
JL
914
915 /* We create a cookie to identify this skb */
633c9389
DM
916 if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
917 goto nla_put_failure;
7882513b
JL
918
919 genlmsg_end(skb, msg_head);
15e47304 920 genlmsg_unicast(&init_net, skb, dst_portid);
7882513b
JL
921
922 /* Enqueue the packet */
923 skb_queue_tail(&data->pending, my_skb);
924 return;
925
926nla_put_failure:
c393862f 927 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
b7bc9679 928 ieee80211_free_txskb(hw, my_skb);
7882513b
JL
929}
930
e8261171
JB
931static bool hwsim_chans_compat(struct ieee80211_channel *c1,
932 struct ieee80211_channel *c2)
933{
934 if (!c1 || !c2)
935 return false;
936
937 return c1->center_freq == c2->center_freq;
938}
939
940struct tx_iter_data {
941 struct ieee80211_channel *channel;
942 bool receive;
943};
944
945static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
946 struct ieee80211_vif *vif)
947{
948 struct tx_iter_data *data = _data;
949
950 if (!vif->chanctx_conf)
951 return;
952
953 if (!hwsim_chans_compat(data->channel,
4bf88530 954 rcu_dereference(vif->chanctx_conf)->def.chan))
e8261171
JB
955 return;
956
957 data->receive = true;
958}
959
7882513b 960static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
e8261171
JB
961 struct sk_buff *skb,
962 struct ieee80211_channel *chan)
acc1e7a3 963{
0e057d73
JB
964 struct mac80211_hwsim_data *data = hw->priv, *data2;
965 bool ack = false;
e36cfdc9 966 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
acc1e7a3 967 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e36cfdc9 968 struct ieee80211_rx_status rx_status;
b66851c3 969 u64 now;
acc1e7a3
JM
970
971 memset(&rx_status, 0, sizeof(rx_status));
f4bda337 972 rx_status.flag |= RX_FLAG_MACTIME_START;
e8261171
JB
973 rx_status.freq = chan->center_freq;
974 rx_status.band = chan->band;
dad6330d
KB
975 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
976 rx_status.rate_idx =
977 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
978 rx_status.vht_nss =
979 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
980 rx_status.flag |= RX_FLAG_VHT;
981 } else {
982 rx_status.rate_idx = info->control.rates[0].idx;
983 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
984 rx_status.flag |= RX_FLAG_HT;
985 }
281ed297
JM
986 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
987 rx_status.flag |= RX_FLAG_40MHZ;
988 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
989 rx_status.flag |= RX_FLAG_SHORT_GI;
4b14c96d 990 /* TODO: simulate real signal strength (and optional packet loss) */
bdd7bd16 991 rx_status.signal = data->power_level - 50;
acc1e7a3 992
fc6971d4
JM
993 if (data->ps != PS_DISABLED)
994 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
995
90e3012e
JB
996 /* release the skb's source info */
997 skb_orphan(skb);
c0acf38e 998 skb_dst_drop(skb);
90e3012e
JB
999 skb->mark = 0;
1000 secpath_reset(skb);
1001 nf_reset(skb);
1002
b66851c3
TP
1003 /*
1004 * Get absolute mactime here so all HWs RX at the "same time", and
1005 * absolute TX time for beacon mactime so the timestamp matches.
1006 * Giving beacons a different mactime than non-beacons looks messy, but
1007 * it helps the Toffset be exact and a ~10us mactime discrepancy
1008 * probably doesn't really matter.
1009 */
1010 if (ieee80211_is_beacon(hdr->frame_control) ||
1011 ieee80211_is_probe_resp(hdr->frame_control))
1012 now = data->abs_bcn_ts;
1013 else
1014 now = mac80211_hwsim_get_tsf_raw();
1015
acc1e7a3 1016 /* Copy skb to all enabled radios that are on the current frequency */
0e057d73
JB
1017 spin_lock(&hwsim_radio_lock);
1018 list_for_each_entry(data2, &hwsim_radios, list) {
acc1e7a3 1019 struct sk_buff *nskb;
e8261171
JB
1020 struct tx_iter_data tx_iter_data = {
1021 .receive = false,
1022 .channel = chan,
1023 };
acc1e7a3 1024
0e057d73 1025 if (data == data2)
acc1e7a3 1026 continue;
0e057d73 1027
e8261171
JB
1028 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1029 !hwsim_ps_rx_ok(data2, skb))
acc1e7a3
JM
1030 continue;
1031
e8261171
JB
1032 if (!(data->group & data2->group))
1033 continue;
1034
1035 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1036 !hwsim_chans_compat(chan, data2->channel)) {
1037 ieee80211_iterate_active_interfaces_atomic(
8b2c9824
JB
1038 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1039 mac80211_hwsim_tx_iter, &tx_iter_data);
e8261171
JB
1040 if (!tx_iter_data.receive)
1041 continue;
1042 }
1043
90b9e446
JB
1044 /*
1045 * reserve some space for our vendor and the normal
1046 * radiotap header, since we're copying anyway
1047 */
a357d7f9
JB
1048 if (skb->len < PAGE_SIZE && paged_rx) {
1049 struct page *page = alloc_page(GFP_ATOMIC);
1050
1051 if (!page)
1052 continue;
1053
1054 nskb = dev_alloc_skb(128);
1055 if (!nskb) {
1056 __free_page(page);
1057 continue;
1058 }
1059
1060 memcpy(page_address(page), skb->data, skb->len);
1061 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1062 } else {
1063 nskb = skb_copy(skb, GFP_ATOMIC);
1064 if (!nskb)
1065 continue;
1066 }
acc1e7a3 1067
265dc7f0 1068 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
0e057d73 1069 ack = true;
f483ad25 1070
b66851c3 1071 rx_status.mactime = now + data2->tsf_offset;
90b9e446 1072
f1d58c25
JB
1073 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1074 ieee80211_rx_irqsafe(data2->hw, nskb);
acc1e7a3 1075 }
0e057d73 1076 spin_unlock(&hwsim_radio_lock);
acc1e7a3 1077
e36cfdc9
JM
1078 return ack;
1079}
1080
36323f81
TH
1081static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1082 struct ieee80211_tx_control *control,
1083 struct sk_buff *skb)
e36cfdc9 1084{
e8261171
JB
1085 struct mac80211_hwsim_data *data = hw->priv;
1086 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1087 struct ieee80211_chanctx_conf *chanctx_conf;
1088 struct ieee80211_channel *channel;
0e057d73 1089 bool ack;
15e47304 1090 u32 _portid;
e36cfdc9 1091
e8261171 1092 if (WARN_ON(skb->len < 10)) {
e36cfdc9 1093 /* Should not happen; just a sanity check for addr1 use */
fe0f3cd2 1094 ieee80211_free_txskb(hw, skb);
7bb45683 1095 return;
e36cfdc9
JM
1096 }
1097
361c3e04 1098 if (!data->use_chanctx) {
e8261171
JB
1099 channel = data->channel;
1100 } else if (txi->hw_queue == 4) {
1101 channel = data->tmp_chan;
1102 } else {
1103 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1104 if (chanctx_conf)
4bf88530 1105 channel = chanctx_conf->def.chan;
e8261171
JB
1106 else
1107 channel = NULL;
1108 }
1109
1110 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
fe0f3cd2 1111 ieee80211_free_txskb(hw, skb);
e8261171
JB
1112 return;
1113 }
1114
1115 if (data->idle && !data->tmp_chan) {
1116 wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
fe0f3cd2 1117 ieee80211_free_txskb(hw, skb);
e8261171
JB
1118 return;
1119 }
1120
1121 if (txi->control.vif)
1122 hwsim_check_magic(txi->control.vif);
1123 if (control->sta)
1124 hwsim_check_sta_magic(control->sta);
1125
81338410 1126 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
1eb32179
KB
1127 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1128 txi->control.rates,
1129 ARRAY_SIZE(txi->control.rates));
e8261171 1130
1eb32179 1131 txi->rate_driver_data[0] = channel;
e8261171
JB
1132 mac80211_hwsim_monitor_rx(hw, skb, channel);
1133
7882513b 1134 /* wmediumd mode check */
15e47304 1135 _portid = ACCESS_ONCE(wmediumd_portid);
7882513b 1136
15e47304
EB
1137 if (_portid)
1138 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
7882513b
JL
1139
1140 /* NO wmediumd detected, perfect medium simulation */
e8261171 1141 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
7882513b 1142
6c085227
JM
1143 if (ack && skb->len >= 16) {
1144 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
e8261171 1145 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
6c085227 1146 }
e36cfdc9 1147
e6a9854b 1148 ieee80211_tx_info_clear_status(txi);
2a4ffa4c
JC
1149
1150 /* frame was transmitted at most favorable rate at first attempt */
1151 txi->control.rates[0].count = 1;
1152 txi->control.rates[1].idx = -1;
1153
e6a9854b
JB
1154 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1155 txi->flags |= IEEE80211_TX_STAT_ACK;
acc1e7a3 1156 ieee80211_tx_status_irqsafe(hw, skb);
acc1e7a3
JM
1157}
1158
1159
1160static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1161{
1162 struct mac80211_hwsim_data *data = hw->priv;
c96c31e4 1163 wiphy_debug(hw->wiphy, "%s\n", __func__);
3db1cd5c 1164 data->started = true;
acc1e7a3
JM
1165 return 0;
1166}
1167
1168
1169static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1170{
1171 struct mac80211_hwsim_data *data = hw->priv;
3db1cd5c 1172 data->started = false;
01e59e46 1173 tasklet_hrtimer_cancel(&data->beacon_timer);
c96c31e4 1174 wiphy_debug(hw->wiphy, "%s\n", __func__);
acc1e7a3
JM
1175}
1176
1177
1178static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1ed32e4f 1179 struct ieee80211_vif *vif)
acc1e7a3 1180{
c96c31e4 1181 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
2ca27bcf
JB
1182 __func__, ieee80211_vif_type_p2p(vif),
1183 vif->addr);
1ed32e4f 1184 hwsim_set_magic(vif);
e8261171
JB
1185
1186 vif->cab_queue = 0;
1187 vif->hw_queue[IEEE80211_AC_VO] = 0;
1188 vif->hw_queue[IEEE80211_AC_VI] = 1;
1189 vif->hw_queue[IEEE80211_AC_BE] = 2;
1190 vif->hw_queue[IEEE80211_AC_BK] = 3;
1191
acc1e7a3
JM
1192 return 0;
1193}
1194
1195
c35d0270
JB
1196static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1197 struct ieee80211_vif *vif,
2ca27bcf
JB
1198 enum nl80211_iftype newtype,
1199 bool newp2p)
c35d0270 1200{
2ca27bcf 1201 newtype = ieee80211_iftype_p2p(newtype, newp2p);
c35d0270
JB
1202 wiphy_debug(hw->wiphy,
1203 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
2ca27bcf
JB
1204 __func__, ieee80211_vif_type_p2p(vif),
1205 newtype, vif->addr);
c35d0270
JB
1206 hwsim_check_magic(vif);
1207
3eb92f6a
JB
1208 /*
1209 * interface may change from non-AP to AP in
1210 * which case this needs to be set up again
1211 */
1212 vif->cab_queue = 0;
1213
c35d0270
JB
1214 return 0;
1215}
1216
acc1e7a3 1217static void mac80211_hwsim_remove_interface(
1ed32e4f 1218 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
acc1e7a3 1219{
c96c31e4 1220 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
2ca27bcf
JB
1221 __func__, ieee80211_vif_type_p2p(vif),
1222 vif->addr);
1ed32e4f
JB
1223 hwsim_check_magic(vif);
1224 hwsim_clear_magic(vif);
acc1e7a3
JM
1225}
1226
e8261171
JB
1227static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1228 struct sk_buff *skb,
1229 struct ieee80211_channel *chan)
1230{
1231 u32 _pid = ACCESS_ONCE(wmediumd_portid);
1232
81338410 1233 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE) {
1eb32179
KB
1234 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1235 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1236 txi->control.rates,
1237 ARRAY_SIZE(txi->control.rates));
1238 }
1239
e8261171
JB
1240 mac80211_hwsim_monitor_rx(hw, skb, chan);
1241
1242 if (_pid)
1243 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1244
1245 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1246 dev_kfree_skb(skb);
1247}
acc1e7a3
JM
1248
1249static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1250 struct ieee80211_vif *vif)
1251{
b66851c3
TP
1252 struct mac80211_hwsim_data *data = arg;
1253 struct ieee80211_hw *hw = data->hw;
1254 struct ieee80211_tx_info *info;
1255 struct ieee80211_rate *txrate;
1256 struct ieee80211_mgmt *mgmt;
acc1e7a3 1257 struct sk_buff *skb;
acc1e7a3 1258
8aa21e6f
JB
1259 hwsim_check_magic(vif);
1260
55b39619 1261 if (vif->type != NL80211_IFTYPE_AP &&
2b2d7795
JB
1262 vif->type != NL80211_IFTYPE_MESH_POINT &&
1263 vif->type != NL80211_IFTYPE_ADHOC)
acc1e7a3
JM
1264 return;
1265
1266 skb = ieee80211_beacon_get(hw, vif);
1267 if (skb == NULL)
1268 return;
b66851c3 1269 info = IEEE80211_SKB_CB(skb);
81338410 1270 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
1eb32179
KB
1271 ieee80211_get_tx_rates(vif, NULL, skb,
1272 info->control.rates,
1273 ARRAY_SIZE(info->control.rates));
1274
b66851c3
TP
1275 txrate = ieee80211_get_tx_rate(hw, info);
1276
1277 mgmt = (struct ieee80211_mgmt *) skb->data;
1278 /* fake header transmission time */
1279 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1280 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1281 data->tsf_offset +
1282 24 * 8 * 10 / txrate->bitrate);
7882513b 1283
e8261171 1284 mac80211_hwsim_tx_frame(hw, skb,
4bf88530 1285 rcu_dereference(vif->chanctx_conf)->def.chan);
0037db63
AO
1286
1287 if (vif->csa_active && ieee80211_csa_is_complete(vif))
1288 ieee80211_csa_finish(vif);
acc1e7a3
JM
1289}
1290
01e59e46
TP
1291static enum hrtimer_restart
1292mac80211_hwsim_beacon(struct hrtimer *timer)
acc1e7a3 1293{
01e59e46
TP
1294 struct mac80211_hwsim_data *data =
1295 container_of(timer, struct mac80211_hwsim_data,
1296 beacon_timer.timer);
1297 struct ieee80211_hw *hw = data->hw;
1298 u64 bcn_int = data->beacon_int;
1299 ktime_t next_bcn;
acc1e7a3 1300
4c4c671a 1301 if (!data->started)
01e59e46 1302 goto out;
acc1e7a3 1303
f248f105 1304 ieee80211_iterate_active_interfaces_atomic(
8b2c9824 1305 hw, IEEE80211_IFACE_ITER_NORMAL,
b66851c3 1306 mac80211_hwsim_beacon_tx, data);
acc1e7a3 1307
c51f8783
TP
1308 /* beacon at new TBTT + beacon interval */
1309 if (data->bcn_delta) {
1310 bcn_int -= data->bcn_delta;
1311 data->bcn_delta = 0;
1312 }
1313
01e59e46
TP
1314 next_bcn = ktime_add(hrtimer_get_expires(timer),
1315 ns_to_ktime(bcn_int * 1000));
1316 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1317out:
1318 return HRTIMER_NORESTART;
acc1e7a3
JM
1319}
1320
675a0b04
KB
1321static const char * const hwsim_chanwidths[] = {
1322 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1323 [NL80211_CHAN_WIDTH_20] = "ht20",
1324 [NL80211_CHAN_WIDTH_40] = "ht40",
1325 [NL80211_CHAN_WIDTH_80] = "vht80",
1326 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1327 [NL80211_CHAN_WIDTH_160] = "vht160",
0aaffa9b 1328};
acc1e7a3 1329
e8975581 1330static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
acc1e7a3
JM
1331{
1332 struct mac80211_hwsim_data *data = hw->priv;
e8975581 1333 struct ieee80211_conf *conf = &hw->conf;
0f78231b
JB
1334 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1335 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1336 [IEEE80211_SMPS_OFF] = "off",
1337 [IEEE80211_SMPS_STATIC] = "static",
1338 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1339 };
1340
675a0b04
KB
1341 if (conf->chandef.chan)
1342 wiphy_debug(hw->wiphy,
1343 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1344 __func__,
1345 conf->chandef.chan->center_freq,
1346 conf->chandef.center_freq1,
1347 conf->chandef.center_freq2,
1348 hwsim_chanwidths[conf->chandef.width],
1349 !!(conf->flags & IEEE80211_CONF_IDLE),
1350 !!(conf->flags & IEEE80211_CONF_PS),
1351 smps_modes[conf->smps_mode]);
1352 else
1353 wiphy_debug(hw->wiphy,
1354 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1355 __func__,
1356 !!(conf->flags & IEEE80211_CONF_IDLE),
1357 !!(conf->flags & IEEE80211_CONF_PS),
1358 smps_modes[conf->smps_mode]);
acc1e7a3 1359
70541839
JM
1360 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1361
675a0b04 1362 data->channel = conf->chandef.chan;
e8261171 1363
361c3e04 1364 WARN_ON(data->channel && data->use_chanctx);
e8261171 1365
bdd7bd16 1366 data->power_level = conf->power_level;
4c4c671a 1367 if (!data->started || !data->beacon_int)
01e59e46
TP
1368 tasklet_hrtimer_cancel(&data->beacon_timer);
1369 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
c51f8783
TP
1370 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1371 u32 bcn_int = data->beacon_int;
1372 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1373
01e59e46 1374 tasklet_hrtimer_start(&data->beacon_timer,
c51f8783 1375 ns_to_ktime(until_tbtt * 1000),
01e59e46
TP
1376 HRTIMER_MODE_REL);
1377 }
acc1e7a3
JM
1378
1379 return 0;
1380}
1381
1382
1383static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1384 unsigned int changed_flags,
3ac64bee 1385 unsigned int *total_flags,u64 multicast)
acc1e7a3
JM
1386{
1387 struct mac80211_hwsim_data *data = hw->priv;
1388
c96c31e4 1389 wiphy_debug(hw->wiphy, "%s\n", __func__);
acc1e7a3
JM
1390
1391 data->rx_filter = 0;
1392 if (*total_flags & FIF_PROMISC_IN_BSS)
1393 data->rx_filter |= FIF_PROMISC_IN_BSS;
1394 if (*total_flags & FIF_ALLMULTI)
1395 data->rx_filter |= FIF_ALLMULTI;
1396
1397 *total_flags = data->rx_filter;
1398}
1399
0bb861e6
JM
1400static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1401 struct ieee80211_vif *vif)
1402{
1403 unsigned int *count = data;
1404 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1405
1406 if (vp->bcn_en)
1407 (*count)++;
1408}
1409
8aa21e6f
JB
1410static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1411 struct ieee80211_vif *vif,
1412 struct ieee80211_bss_conf *info,
1413 u32 changed)
1414{
fc6971d4 1415 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
57c4d7b4 1416 struct mac80211_hwsim_data *data = hw->priv;
fc6971d4 1417
8aa21e6f 1418 hwsim_check_magic(vif);
fe63bfa3 1419
0bb861e6
JM
1420 wiphy_debug(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1421 __func__, changed, vif->addr);
fe63bfa3 1422
2d0ddec5 1423 if (changed & BSS_CHANGED_BSSID) {
c96c31e4
JP
1424 wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
1425 __func__, info->bssid);
2d0ddec5
JB
1426 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1427 }
1428
fe63bfa3 1429 if (changed & BSS_CHANGED_ASSOC) {
c96c31e4
JP
1430 wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1431 info->assoc, info->aid);
fc6971d4
JM
1432 vp->assoc = info->assoc;
1433 vp->aid = info->aid;
fe63bfa3
JM
1434 }
1435
57c4d7b4 1436 if (changed & BSS_CHANGED_BEACON_INT) {
c96c31e4 1437 wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
01e59e46
TP
1438 data->beacon_int = info->beacon_int * 1024;
1439 }
1440
1441 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1442 wiphy_debug(hw->wiphy, " BCN EN: %d\n", info->enable_beacon);
0bb861e6 1443 vp->bcn_en = info->enable_beacon;
01e59e46
TP
1444 if (data->started &&
1445 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1446 info->enable_beacon) {
c51f8783
TP
1447 u64 tsf, until_tbtt;
1448 u32 bcn_int;
01e59e46
TP
1449 if (WARN_ON(!data->beacon_int))
1450 data->beacon_int = 1000 * 1024;
c51f8783
TP
1451 tsf = mac80211_hwsim_get_tsf(hw, vif);
1452 bcn_int = data->beacon_int;
1453 until_tbtt = bcn_int - do_div(tsf, bcn_int);
01e59e46 1454 tasklet_hrtimer_start(&data->beacon_timer,
c51f8783 1455 ns_to_ktime(until_tbtt * 1000),
01e59e46 1456 HRTIMER_MODE_REL);
0bb861e6
JM
1457 } else if (!info->enable_beacon) {
1458 unsigned int count = 0;
cdb1b805 1459 ieee80211_iterate_active_interfaces_atomic(
0bb861e6
JM
1460 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1461 mac80211_hwsim_bcn_en_iter, &count);
1462 wiphy_debug(hw->wiphy, " beaconing vifs remaining: %u",
1463 count);
1464 if (count == 0)
1465 tasklet_hrtimer_cancel(&data->beacon_timer);
1466 }
57c4d7b4
JB
1467 }
1468
fe63bfa3 1469 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
c96c31e4
JP
1470 wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
1471 info->use_cts_prot);
fe63bfa3
JM
1472 }
1473
1474 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
c96c31e4
JP
1475 wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
1476 info->use_short_preamble);
fe63bfa3
JM
1477 }
1478
1479 if (changed & BSS_CHANGED_ERP_SLOT) {
c96c31e4 1480 wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
fe63bfa3
JM
1481 }
1482
1483 if (changed & BSS_CHANGED_HT) {
4bf88530
JB
1484 wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
1485 info->ht_operation_mode);
fe63bfa3
JM
1486 }
1487
1488 if (changed & BSS_CHANGED_BASIC_RATES) {
c96c31e4
JP
1489 wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1490 (unsigned long long) info->basic_rates);
fe63bfa3 1491 }
cbc668a7
JB
1492
1493 if (changed & BSS_CHANGED_TXPOWER)
1494 wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
8aa21e6f
JB
1495}
1496
1d669cbf
JB
1497static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1498 struct ieee80211_vif *vif,
1499 struct ieee80211_sta *sta)
1500{
1501 hwsim_check_magic(vif);
1502 hwsim_set_sta_magic(sta);
1503
1504 return 0;
1505}
1506
1507static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1508 struct ieee80211_vif *vif,
1509 struct ieee80211_sta *sta)
1510{
1511 hwsim_check_magic(vif);
1512 hwsim_clear_sta_magic(sta);
1513
1514 return 0;
1515}
1516
8aa21e6f
JB
1517static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1518 struct ieee80211_vif *vif,
17741cdc
JB
1519 enum sta_notify_cmd cmd,
1520 struct ieee80211_sta *sta)
8aa21e6f
JB
1521{
1522 hwsim_check_magic(vif);
1d669cbf 1523
81c06523 1524 switch (cmd) {
89fad578
CL
1525 case STA_NOTIFY_SLEEP:
1526 case STA_NOTIFY_AWAKE:
1527 /* TODO: make good use of these flags */
1528 break;
1d669cbf
JB
1529 default:
1530 WARN(1, "Invalid sta notify: %d\n", cmd);
1531 break;
81c06523
JB
1532 }
1533}
1534
1535static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1536 struct ieee80211_sta *sta,
1537 bool set)
1538{
1539 hwsim_check_sta_magic(sta);
1540 return 0;
8aa21e6f 1541}
acc1e7a3 1542
1e898ff8 1543static int mac80211_hwsim_conf_tx(
8a3a3c85
EP
1544 struct ieee80211_hw *hw,
1545 struct ieee80211_vif *vif, u16 queue,
1e898ff8
JM
1546 const struct ieee80211_tx_queue_params *params)
1547{
c96c31e4
JP
1548 wiphy_debug(hw->wiphy,
1549 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1550 __func__, queue,
1551 params->txop, params->cw_min,
1552 params->cw_max, params->aifs);
1e898ff8
JM
1553 return 0;
1554}
1555
1289723e
HS
1556static int mac80211_hwsim_get_survey(
1557 struct ieee80211_hw *hw, int idx,
1558 struct survey_info *survey)
1559{
1560 struct ieee80211_conf *conf = &hw->conf;
1561
c96c31e4 1562 wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
1289723e
HS
1563
1564 if (idx != 0)
1565 return -ENOENT;
1566
1567 /* Current channel */
675a0b04 1568 survey->channel = conf->chandef.chan;
1289723e
HS
1569
1570 /*
1571 * Magically conjured noise level --- this is only ok for simulated hardware.
1572 *
1573 * A real driver which cannot determine the real channel noise MUST NOT
1574 * report any noise, especially not a magically conjured one :-)
1575 */
1576 survey->filled = SURVEY_INFO_NOISE_DBM;
1577 survey->noise = -92;
1578
1579 return 0;
1580}
1581
aff89a9b
JB
1582#ifdef CONFIG_NL80211_TESTMODE
1583/*
1584 * This section contains example code for using netlink
1585 * attributes with the testmode command in nl80211.
1586 */
1587
1588/* These enums need to be kept in sync with userspace */
1589enum hwsim_testmode_attr {
1590 __HWSIM_TM_ATTR_INVALID = 0,
1591 HWSIM_TM_ATTR_CMD = 1,
1592 HWSIM_TM_ATTR_PS = 2,
1593
1594 /* keep last */
1595 __HWSIM_TM_ATTR_AFTER_LAST,
1596 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1597};
1598
1599enum hwsim_testmode_cmd {
1600 HWSIM_TM_CMD_SET_PS = 0,
1601 HWSIM_TM_CMD_GET_PS = 1,
4d6d0ae2
JB
1602 HWSIM_TM_CMD_STOP_QUEUES = 2,
1603 HWSIM_TM_CMD_WAKE_QUEUES = 3,
aff89a9b
JB
1604};
1605
1606static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1607 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1608 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1609};
1610
5bc38193 1611static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
52981cd7 1612 struct ieee80211_vif *vif,
5bc38193 1613 void *data, int len)
aff89a9b
JB
1614{
1615 struct mac80211_hwsim_data *hwsim = hw->priv;
1616 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1617 struct sk_buff *skb;
1618 int err, ps;
1619
1620 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1621 hwsim_testmode_policy);
1622 if (err)
1623 return err;
1624
1625 if (!tb[HWSIM_TM_ATTR_CMD])
1626 return -EINVAL;
1627
1628 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1629 case HWSIM_TM_CMD_SET_PS:
1630 if (!tb[HWSIM_TM_ATTR_PS])
1631 return -EINVAL;
1632 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1633 return hwsim_fops_ps_write(hwsim, ps);
1634 case HWSIM_TM_CMD_GET_PS:
1635 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1636 nla_total_size(sizeof(u32)));
1637 if (!skb)
1638 return -ENOMEM;
633c9389
DM
1639 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1640 goto nla_put_failure;
aff89a9b 1641 return cfg80211_testmode_reply(skb);
4d6d0ae2
JB
1642 case HWSIM_TM_CMD_STOP_QUEUES:
1643 ieee80211_stop_queues(hw);
1644 return 0;
1645 case HWSIM_TM_CMD_WAKE_QUEUES:
1646 ieee80211_wake_queues(hw);
1647 return 0;
aff89a9b
JB
1648 default:
1649 return -EOPNOTSUPP;
1650 }
1651
1652 nla_put_failure:
1653 kfree_skb(skb);
1654 return -ENOBUFS;
1655}
1656#endif
1657
8b73d13a
JB
1658static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1659 struct ieee80211_vif *vif,
1660 enum ieee80211_ampdu_mlme_action action,
0b01f030
JB
1661 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
1662 u8 buf_size)
8b73d13a
JB
1663{
1664 switch (action) {
1665 case IEEE80211_AMPDU_TX_START:
1666 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1667 break;
18b559d5
JB
1668 case IEEE80211_AMPDU_TX_STOP_CONT:
1669 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1670 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
8b73d13a
JB
1671 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1672 break;
1673 case IEEE80211_AMPDU_TX_OPERATIONAL:
1674 break;
1675 case IEEE80211_AMPDU_RX_START:
1676 case IEEE80211_AMPDU_RX_STOP:
1677 break;
1678 default:
1679 return -EOPNOTSUPP;
1680 }
1681
1682 return 0;
1683}
1684
77be2c54
EG
1685static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
1686 struct ieee80211_vif *vif,
1687 u32 queues, bool drop)
a80f7c0b 1688{
7882513b 1689 /* Not implemented, queues only on kernel side */
a80f7c0b
JB
1690}
1691
e8261171 1692static void hw_scan_work(struct work_struct *work)
69068036 1693{
e8261171
JB
1694 struct mac80211_hwsim_data *hwsim =
1695 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
1696 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
1697 int dwell, i;
69068036 1698
e8261171
JB
1699 mutex_lock(&hwsim->mutex);
1700 if (hwsim->scan_chan_idx >= req->n_channels) {
1701 wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
1702 ieee80211_scan_completed(hwsim->hw, false);
1703 hwsim->hw_scan_request = NULL;
1704 hwsim->hw_scan_vif = NULL;
1705 hwsim->tmp_chan = NULL;
1706 mutex_unlock(&hwsim->mutex);
1707 return;
1708 }
1709
1710 wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
1711 req->channels[hwsim->scan_chan_idx]->center_freq);
1712
1713 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
8fe02e16 1714 if (hwsim->tmp_chan->flags & IEEE80211_CHAN_NO_IR ||
e8261171
JB
1715 !req->n_ssids) {
1716 dwell = 120;
1717 } else {
1718 dwell = 30;
1719 /* send probes */
1720 for (i = 0; i < req->n_ssids; i++) {
1721 struct sk_buff *probe;
1722
1723 probe = ieee80211_probereq_get(hwsim->hw,
1724 hwsim->hw_scan_vif,
1725 req->ssids[i].ssid,
1726 req->ssids[i].ssid_len,
b9a9ada1 1727 req->ie_len);
e8261171
JB
1728 if (!probe)
1729 continue;
b9a9ada1
JB
1730
1731 if (req->ie_len)
1732 memcpy(skb_put(probe, req->ie_len), req->ie,
1733 req->ie_len);
1734
e8261171
JB
1735 local_bh_disable();
1736 mac80211_hwsim_tx_frame(hwsim->hw, probe,
1737 hwsim->tmp_chan);
1738 local_bh_enable();
1739 }
1740 }
1741 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
1742 msecs_to_jiffies(dwell));
1743 hwsim->scan_chan_idx++;
1744 mutex_unlock(&hwsim->mutex);
69068036
JB
1745}
1746
1747static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
a060bbfe 1748 struct ieee80211_vif *vif,
c56ef672 1749 struct ieee80211_scan_request *hw_req)
69068036 1750{
e8261171 1751 struct mac80211_hwsim_data *hwsim = hw->priv;
c56ef672 1752 struct cfg80211_scan_request *req = &hw_req->req;
69068036 1753
e8261171
JB
1754 mutex_lock(&hwsim->mutex);
1755 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
1756 mutex_unlock(&hwsim->mutex);
1757 return -EBUSY;
1758 }
1759 hwsim->hw_scan_request = req;
1760 hwsim->hw_scan_vif = vif;
1761 hwsim->scan_chan_idx = 0;
1762 mutex_unlock(&hwsim->mutex);
69068036 1763
e8261171 1764 wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
69068036 1765
e8261171 1766 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
69068036
JB
1767
1768 return 0;
1769}
1770
e8261171
JB
1771static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
1772 struct ieee80211_vif *vif)
1773{
1774 struct mac80211_hwsim_data *hwsim = hw->priv;
1775
1776 wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
1777
1778 cancel_delayed_work_sync(&hwsim->hw_scan);
1779
1780 mutex_lock(&hwsim->mutex);
1781 ieee80211_scan_completed(hwsim->hw, true);
1782 hwsim->tmp_chan = NULL;
1783 hwsim->hw_scan_request = NULL;
1784 hwsim->hw_scan_vif = NULL;
1785 mutex_unlock(&hwsim->mutex);
1786}
1787
f74cb0f7
LR
1788static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
1789{
1790 struct mac80211_hwsim_data *hwsim = hw->priv;
1791
1792 mutex_lock(&hwsim->mutex);
1793
1794 if (hwsim->scanning) {
1795 printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
1796 goto out;
1797 }
1798
1799 printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
1800 hwsim->scanning = true;
1801
1802out:
1803 mutex_unlock(&hwsim->mutex);
1804}
1805
1806static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
1807{
1808 struct mac80211_hwsim_data *hwsim = hw->priv;
1809
1810 mutex_lock(&hwsim->mutex);
1811
1812 printk(KERN_DEBUG "hwsim sw_scan_complete\n");
23ff98fc 1813 hwsim->scanning = false;
f74cb0f7
LR
1814
1815 mutex_unlock(&hwsim->mutex);
1816}
1817
e8261171
JB
1818static void hw_roc_done(struct work_struct *work)
1819{
1820 struct mac80211_hwsim_data *hwsim =
1821 container_of(work, struct mac80211_hwsim_data, roc_done.work);
1822
1823 mutex_lock(&hwsim->mutex);
1824 ieee80211_remain_on_channel_expired(hwsim->hw);
1825 hwsim->tmp_chan = NULL;
1826 mutex_unlock(&hwsim->mutex);
1827
1828 wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
1829}
1830
1831static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
49884568 1832 struct ieee80211_vif *vif,
e8261171 1833 struct ieee80211_channel *chan,
d339d5ca
IP
1834 int duration,
1835 enum ieee80211_roc_type type)
e8261171
JB
1836{
1837 struct mac80211_hwsim_data *hwsim = hw->priv;
1838
1839 mutex_lock(&hwsim->mutex);
1840 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
1841 mutex_unlock(&hwsim->mutex);
1842 return -EBUSY;
1843 }
1844
1845 hwsim->tmp_chan = chan;
1846 mutex_unlock(&hwsim->mutex);
1847
1848 wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
1849 chan->center_freq, duration);
1850
1851 ieee80211_ready_on_channel(hw);
1852
1853 ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
1854 msecs_to_jiffies(duration));
1855 return 0;
1856}
1857
1858static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
1859{
1860 struct mac80211_hwsim_data *hwsim = hw->priv;
1861
1862 cancel_delayed_work_sync(&hwsim->roc_done);
1863
1864 mutex_lock(&hwsim->mutex);
1865 hwsim->tmp_chan = NULL;
1866 mutex_unlock(&hwsim->mutex);
1867
1868 wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
1869
1870 return 0;
1871}
1872
1873static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
1874 struct ieee80211_chanctx_conf *ctx)
1875{
1876 hwsim_set_chanctx_magic(ctx);
4bf88530
JB
1877 wiphy_debug(hw->wiphy,
1878 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1879 ctx->def.chan->center_freq, ctx->def.width,
1880 ctx->def.center_freq1, ctx->def.center_freq2);
e8261171
JB
1881 return 0;
1882}
1883
1884static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
1885 struct ieee80211_chanctx_conf *ctx)
1886{
4bf88530
JB
1887 wiphy_debug(hw->wiphy,
1888 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1889 ctx->def.chan->center_freq, ctx->def.width,
1890 ctx->def.center_freq1, ctx->def.center_freq2);
e8261171
JB
1891 hwsim_check_chanctx_magic(ctx);
1892 hwsim_clear_chanctx_magic(ctx);
1893}
1894
1895static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
1896 struct ieee80211_chanctx_conf *ctx,
1897 u32 changed)
1898{
1899 hwsim_check_chanctx_magic(ctx);
4bf88530
JB
1900 wiphy_debug(hw->wiphy,
1901 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1902 ctx->def.chan->center_freq, ctx->def.width,
1903 ctx->def.center_freq1, ctx->def.center_freq2);
e8261171
JB
1904}
1905
1906static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
1907 struct ieee80211_vif *vif,
1908 struct ieee80211_chanctx_conf *ctx)
1909{
1910 hwsim_check_magic(vif);
1911 hwsim_check_chanctx_magic(ctx);
1912
1913 return 0;
1914}
1915
1916static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
1917 struct ieee80211_vif *vif,
1918 struct ieee80211_chanctx_conf *ctx)
1919{
1920 hwsim_check_magic(vif);
1921 hwsim_check_chanctx_magic(ctx);
1922}
1923
38ed5048 1924static const struct ieee80211_ops mac80211_hwsim_ops = {
acc1e7a3
JM
1925 .tx = mac80211_hwsim_tx,
1926 .start = mac80211_hwsim_start,
1927 .stop = mac80211_hwsim_stop,
1928 .add_interface = mac80211_hwsim_add_interface,
c35d0270 1929 .change_interface = mac80211_hwsim_change_interface,
acc1e7a3
JM
1930 .remove_interface = mac80211_hwsim_remove_interface,
1931 .config = mac80211_hwsim_config,
1932 .configure_filter = mac80211_hwsim_configure_filter,
8aa21e6f 1933 .bss_info_changed = mac80211_hwsim_bss_info_changed,
1d669cbf
JB
1934 .sta_add = mac80211_hwsim_sta_add,
1935 .sta_remove = mac80211_hwsim_sta_remove,
8aa21e6f 1936 .sta_notify = mac80211_hwsim_sta_notify,
81c06523 1937 .set_tim = mac80211_hwsim_set_tim,
1e898ff8 1938 .conf_tx = mac80211_hwsim_conf_tx,
1289723e 1939 .get_survey = mac80211_hwsim_get_survey,
aff89a9b 1940 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
8b73d13a 1941 .ampdu_action = mac80211_hwsim_ampdu_action,
f74cb0f7
LR
1942 .sw_scan_start = mac80211_hwsim_sw_scan,
1943 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
a80f7c0b 1944 .flush = mac80211_hwsim_flush,
12ce8ba3
JC
1945 .get_tsf = mac80211_hwsim_get_tsf,
1946 .set_tsf = mac80211_hwsim_set_tsf,
acc1e7a3
JM
1947};
1948
38ed5048 1949static struct ieee80211_ops mac80211_hwsim_mchan_ops;
acc1e7a3 1950
26b0e411
JB
1951static int mac80211_hwsim_create_radio(int channels, const char *reg_alpha2,
1952 const struct ieee80211_regdomain *regd,
361c3e04 1953 bool reg_strict, bool p2p_device,
e9ed49bf
JR
1954 bool use_chanctx, bool destroy_on_close,
1955 u32 portid)
acc1e7a3 1956{
de0421d5
JB
1957 int err;
1958 u8 addr[ETH_ALEN];
e4afb603 1959 struct mac80211_hwsim_data *data;
de0421d5
JB
1960 struct ieee80211_hw *hw;
1961 enum ieee80211_band band;
1962 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
1963 int idx;
0e057d73 1964
361c3e04
LC
1965 if (WARN_ON(channels > 1 && !use_chanctx))
1966 return -EINVAL;
1967
0e057d73 1968 spin_lock_bh(&hwsim_radio_lock);
de0421d5 1969 idx = hwsim_radio_idx++;
0e057d73
JB
1970 spin_unlock_bh(&hwsim_radio_lock);
1971
361c3e04 1972 if (use_chanctx)
de0421d5
JB
1973 ops = &mac80211_hwsim_mchan_ops;
1974 hw = ieee80211_alloc_hw(sizeof(*data), ops);
1975 if (!hw) {
1976 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw failed\n");
1977 err = -ENOMEM;
1978 goto failed;
1979 }
1980 data = hw->priv;
1981 data->hw = hw;
acc1e7a3 1982
de0421d5
JB
1983 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
1984 if (IS_ERR(data->dev)) {
1985 printk(KERN_DEBUG
1986 "mac80211_hwsim: device_create failed (%ld)\n",
1987 PTR_ERR(data->dev));
1988 err = -ENOMEM;
1989 goto failed_drvdata;
1990 }
1991 data->dev->driver = &mac80211_hwsim_driver.driver;
1992 err = device_bind_driver(data->dev);
1993 if (err != 0) {
1994 printk(KERN_DEBUG "mac80211_hwsim: device_bind_driver failed (%d)\n",
1995 err);
1996 goto failed_hw;
acc1e7a3 1997 }
acc1e7a3 1998
de0421d5 1999 skb_queue_head_init(&data->pending);
acc1e7a3 2000
de0421d5
JB
2001 SET_IEEE80211_DEV(hw, data->dev);
2002 memset(addr, 0, ETH_ALEN);
2003 addr[0] = 0x02;
2004 addr[3] = idx >> 8;
2005 addr[4] = idx;
2006 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
2007 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
2008 data->addresses[1].addr[0] |= 0x40;
2009 hw->wiphy->n_addresses = 2;
2010 hw->wiphy->addresses = data->addresses;
fc6971d4 2011
de0421d5 2012 data->channels = channels;
361c3e04 2013 data->use_chanctx = use_chanctx;
bc791098 2014 data->idx = idx;
e9ed49bf
JR
2015 data->destroy_on_close = destroy_on_close;
2016 data->portid = portid;
fc6971d4 2017
361c3e04 2018 if (data->use_chanctx) {
de0421d5
JB
2019 hw->wiphy->max_scan_ssids = 255;
2020 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
2021 hw->wiphy->max_remain_on_channel_duration = 1000;
2022 /* For channels > 1 DFS is not allowed */
2023 hw->wiphy->n_iface_combinations = 1;
2024 hw->wiphy->iface_combinations = &data->if_combination;
8c66a3d9
JB
2025 if (p2p_device)
2026 data->if_combination = hwsim_if_comb_p2p_dev[0];
2027 else
2028 data->if_combination = hwsim_if_comb[0];
b59ec8dd 2029 data->if_combination.num_different_channels = data->channels;
8c66a3d9
JB
2030 } else if (p2p_device) {
2031 hw->wiphy->iface_combinations = hwsim_if_comb_p2p_dev;
2032 hw->wiphy->n_iface_combinations =
2033 ARRAY_SIZE(hwsim_if_comb_p2p_dev);
de0421d5
JB
2034 } else {
2035 hw->wiphy->iface_combinations = hwsim_if_comb;
2036 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
2037 }
acc1e7a3 2038
de0421d5
JB
2039 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
2040 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
7882513b 2041
de0421d5
JB
2042 hw->queues = 5;
2043 hw->offchannel_tx_hw_queue = 4;
2044 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2045 BIT(NL80211_IFTYPE_AP) |
2046 BIT(NL80211_IFTYPE_P2P_CLIENT) |
2047 BIT(NL80211_IFTYPE_P2P_GO) |
2048 BIT(NL80211_IFTYPE_ADHOC) |
8c66a3d9
JB
2049 BIT(NL80211_IFTYPE_MESH_POINT);
2050
2051 if (p2p_device)
2052 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
fc6971d4 2053
de0421d5
JB
2054 hw->flags = IEEE80211_HW_MFP_CAPABLE |
2055 IEEE80211_HW_SIGNAL_DBM |
de0421d5
JB
2056 IEEE80211_HW_AMPDU_AGGREGATION |
2057 IEEE80211_HW_WANT_MONITOR_VIF |
2058 IEEE80211_HW_QUEUE_CONTROL |
0037db63
AO
2059 IEEE80211_HW_SUPPORTS_HT_CCK_RATES |
2060 IEEE80211_HW_CHANCTX_STA_CSA;
de0421d5
JB
2061 if (rctbl)
2062 hw->flags |= IEEE80211_HW_SUPPORTS_RC_TABLE;
fc6971d4 2063
de0421d5
JB
2064 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
2065 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
0037db63
AO
2066 WIPHY_FLAG_AP_UAPSD |
2067 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
0d8614b4
EP
2068 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
2069 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2070 NL80211_FEATURE_STATIC_SMPS |
2071 NL80211_FEATURE_DYNAMIC_SMPS;
fc6971d4 2072
de0421d5
JB
2073 /* ask mac80211 to reserve space for magic */
2074 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
2075 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
2076 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
fc6971d4 2077
de0421d5
JB
2078 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
2079 sizeof(hwsim_channels_2ghz));
2080 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
2081 sizeof(hwsim_channels_5ghz));
2082 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
7882513b 2083
de0421d5
JB
2084 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
2085 struct ieee80211_supported_band *sband = &data->bands[band];
2086 switch (band) {
2087 case IEEE80211_BAND_2GHZ:
2088 sband->channels = data->channels_2ghz;
2089 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
2090 sband->bitrates = data->rates;
2091 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
2092 break;
2093 case IEEE80211_BAND_5GHZ:
2094 sband->channels = data->channels_5ghz;
2095 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
2096 sband->bitrates = data->rates + 4;
2097 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
2098 break;
2099 default:
2100 continue;
2101 }
acc1e7a3 2102
de0421d5
JB
2103 sband->ht_cap.ht_supported = true;
2104 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2105 IEEE80211_HT_CAP_GRN_FLD |
2106 IEEE80211_HT_CAP_SGI_40 |
2107 IEEE80211_HT_CAP_DSSSCCK40;
2108 sband->ht_cap.ampdu_factor = 0x3;
2109 sband->ht_cap.ampdu_density = 0x6;
2110 memset(&sband->ht_cap.mcs, 0,
2111 sizeof(sband->ht_cap.mcs));
2112 sband->ht_cap.mcs.rx_mask[0] = 0xff;
2113 sband->ht_cap.mcs.rx_mask[1] = 0xff;
2114 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
fc6971d4 2115
de0421d5 2116 hw->wiphy->bands[band] = sband;
fc6971d4 2117
de0421d5
JB
2118 sband->vht_cap.vht_supported = true;
2119 sband->vht_cap.cap =
2120 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2121 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
2122 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2123 IEEE80211_VHT_CAP_RXLDPC |
2124 IEEE80211_VHT_CAP_SHORT_GI_80 |
2125 IEEE80211_VHT_CAP_SHORT_GI_160 |
2126 IEEE80211_VHT_CAP_TXSTBC |
2127 IEEE80211_VHT_CAP_RXSTBC_1 |
2128 IEEE80211_VHT_CAP_RXSTBC_2 |
2129 IEEE80211_VHT_CAP_RXSTBC_3 |
2130 IEEE80211_VHT_CAP_RXSTBC_4 |
2131 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
2132 sband->vht_cap.vht_mcs.rx_mcs_map =
2133 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
2134 IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
2135 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2136 IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
2137 IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
2138 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
2139 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
2140 IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
2141 sband->vht_cap.vht_mcs.tx_mcs_map =
2142 sband->vht_cap.vht_mcs.rx_mcs_map;
2143 }
acc1e7a3 2144
de0421d5
JB
2145 /* By default all radios belong to the first group */
2146 data->group = 1;
2147 mutex_init(&data->mutex);
fc6971d4 2148
de0421d5
JB
2149 /* Enable frame retransmissions for lossy channels */
2150 hw->max_rates = 4;
2151 hw->max_rate_tries = 11;
fc6971d4 2152
26b0e411
JB
2153 if (reg_strict)
2154 hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
2155 if (regd) {
2156 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2157 wiphy_apply_custom_regulatory(hw->wiphy, regd);
2158 /* give the regulatory workqueue a chance to run */
2159 schedule_timeout_interruptible(1);
2160 }
fc6971d4 2161
de0421d5
JB
2162 err = ieee80211_register_hw(hw);
2163 if (err < 0) {
2164 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2165 err);
2166 goto failed_hw;
2167 }
fc6971d4 2168
de0421d5 2169 wiphy_debug(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
fc6971d4 2170
26b0e411
JB
2171 if (reg_alpha2)
2172 regulatory_hint(hw->wiphy, reg_alpha2);
7882513b 2173
de0421d5
JB
2174 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2175 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2176 debugfs_create_file("group", 0666, data->debugfs, data,
2177 &hwsim_fops_group);
361c3e04 2178 if (!data->use_chanctx)
de0421d5
JB
2179 debugfs_create_file("dfs_simulate_radar", 0222,
2180 data->debugfs,
2181 data, &hwsim_simulate_radar);
fc6971d4 2182
de0421d5
JB
2183 tasklet_hrtimer_init(&data->beacon_timer,
2184 mac80211_hwsim_beacon,
2185 CLOCK_MONOTONIC_RAW, HRTIMER_MODE_ABS);
fc6971d4 2186
de0421d5
JB
2187 spin_lock_bh(&hwsim_radio_lock);
2188 list_add_tail(&data->list, &hwsim_radios);
2189 spin_unlock_bh(&hwsim_radio_lock);
fc6971d4 2190
bc791098 2191 return idx;
fc6971d4 2192
de0421d5
JB
2193failed_hw:
2194 device_unregister(data->dev);
2195failed_drvdata:
2196 ieee80211_free_hw(hw);
2197failed:
2198 return err;
fc6971d4
JM
2199}
2200
de0421d5 2201static void mac80211_hwsim_destroy_radio(struct mac80211_hwsim_data *data)
fc6971d4 2202{
de0421d5
JB
2203 debugfs_remove_recursive(data->debugfs);
2204 ieee80211_unregister_hw(data->hw);
2205 device_release_driver(data->dev);
2206 device_unregister(data->dev);
2207 ieee80211_free_hw(data->hw);
fc6971d4
JM
2208}
2209
de0421d5 2210static void mac80211_hwsim_free(void)
fc6971d4 2211{
de0421d5 2212 struct mac80211_hwsim_data *data;
fc6971d4 2213
de0421d5
JB
2214 spin_lock_bh(&hwsim_radio_lock);
2215 while ((data = list_first_entry_or_null(&hwsim_radios,
2216 struct mac80211_hwsim_data,
2217 list))) {
2218 list_del(&data->list);
2219 spin_unlock_bh(&hwsim_radio_lock);
2220 mac80211_hwsim_destroy_radio(data);
2221 spin_lock_bh(&hwsim_radio_lock);
fc6971d4 2222 }
de0421d5
JB
2223 spin_unlock_bh(&hwsim_radio_lock);
2224 class_destroy(hwsim_class);
bba05e3d
JD
2225}
2226
de0421d5
JB
2227static const struct net_device_ops hwsim_netdev_ops = {
2228 .ndo_start_xmit = hwsim_mon_xmit,
2229 .ndo_change_mtu = eth_change_mtu,
2230 .ndo_set_mac_address = eth_mac_addr,
2231 .ndo_validate_addr = eth_validate_addr,
2232};
73606d00 2233
de0421d5 2234static void hwsim_mon_setup(struct net_device *dev)
73606d00 2235{
de0421d5
JB
2236 dev->netdev_ops = &hwsim_netdev_ops;
2237 dev->destructor = free_netdev;
2238 ether_setup(dev);
2239 dev->tx_queue_len = 0;
2240 dev->type = ARPHRD_IEEE80211_RADIOTAP;
2241 memset(dev->dev_addr, 0, ETH_ALEN);
2242 dev->dev_addr[0] = 0x12;
73606d00
DW
2243}
2244
9ddd12af 2245static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
7882513b
JL
2246{
2247 struct mac80211_hwsim_data *data;
2248 bool _found = false;
2249
2250 spin_lock_bh(&hwsim_radio_lock);
2251 list_for_each_entry(data, &hwsim_radios, list) {
9ddd12af 2252 if (memcmp(data->addresses[1].addr, addr, ETH_ALEN) == 0) {
7882513b
JL
2253 _found = true;
2254 break;
2255 }
2256 }
2257 spin_unlock_bh(&hwsim_radio_lock);
2258
2259 if (!_found)
2260 return NULL;
2261
2262 return data;
2263}
2264
2265static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2266 struct genl_info *info)
2267{
2268
2269 struct ieee80211_hdr *hdr;
2270 struct mac80211_hwsim_data *data2;
2271 struct ieee80211_tx_info *txi;
2272 struct hwsim_tx_rate *tx_attempts;
d0f718c1 2273 unsigned long ret_skb_ptr;
7882513b 2274 struct sk_buff *skb, *tmp;
9ddd12af 2275 const u8 *src;
7882513b 2276 unsigned int hwsim_flags;
7882513b
JL
2277 int i;
2278 bool found = false;
2279
85ca8fc7
JB
2280 if (info->snd_portid != wmediumd_portid)
2281 return -EINVAL;
2282
7882513b 2283 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
9ddd12af
JB
2284 !info->attrs[HWSIM_ATTR_FLAGS] ||
2285 !info->attrs[HWSIM_ATTR_COOKIE] ||
2286 !info->attrs[HWSIM_ATTR_TX_INFO])
7882513b
JL
2287 goto out;
2288
9ddd12af 2289 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
7882513b 2290 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
d0f718c1 2291 ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
7882513b
JL
2292
2293 data2 = get_hwsim_data_ref_from_addr(src);
9ddd12af 2294 if (!data2)
7882513b
JL
2295 goto out;
2296
2297 /* look for the skb matching the cookie passed back from user */
2298 skb_queue_walk_safe(&data2->pending, skb, tmp) {
d0f718c1 2299 if ((unsigned long)skb == ret_skb_ptr) {
7882513b
JL
2300 skb_unlink(skb, &data2->pending);
2301 found = true;
2302 break;
2303 }
2304 }
2305
2306 /* not found */
2307 if (!found)
2308 goto out;
2309
2310 /* Tx info received because the frame was broadcasted on user space,
2311 so we get all the necessary info: tx attempts and skb control buff */
2312
2313 tx_attempts = (struct hwsim_tx_rate *)nla_data(
2314 info->attrs[HWSIM_ATTR_TX_INFO]);
2315
2316 /* now send back TX status */
2317 txi = IEEE80211_SKB_CB(skb);
2318
7882513b
JL
2319 ieee80211_tx_info_clear_status(txi);
2320
2321 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2322 txi->status.rates[i].idx = tx_attempts[i].idx;
2323 txi->status.rates[i].count = tx_attempts[i].count;
2324 /*txi->status.rates[i].flags = 0;*/
2325 }
2326
2327 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2328
2329 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
2330 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
2331 if (skb->len >= 16) {
2332 hdr = (struct ieee80211_hdr *) skb->data;
c0e30763 2333 mac80211_hwsim_monitor_ack(data2->channel,
e8261171 2334 hdr->addr2);
7882513b 2335 }
06cf5c4c 2336 txi->flags |= IEEE80211_TX_STAT_ACK;
7882513b
JL
2337 }
2338 ieee80211_tx_status_irqsafe(data2->hw, skb);
2339 return 0;
2340out:
2341 return -EINVAL;
2342
2343}
2344
2345static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
2346 struct genl_info *info)
2347{
2348
e8261171 2349 struct mac80211_hwsim_data *data2;
7882513b 2350 struct ieee80211_rx_status rx_status;
9ddd12af 2351 const u8 *dst;
7882513b 2352 int frame_data_len;
9ddd12af 2353 void *frame_data;
7882513b
JL
2354 struct sk_buff *skb = NULL;
2355
85ca8fc7
JB
2356 if (info->snd_portid != wmediumd_portid)
2357 return -EINVAL;
2358
7882513b 2359 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
e8261171
JB
2360 !info->attrs[HWSIM_ATTR_FRAME] ||
2361 !info->attrs[HWSIM_ATTR_RX_RATE] ||
2362 !info->attrs[HWSIM_ATTR_SIGNAL])
7882513b
JL
2363 goto out;
2364
9ddd12af 2365 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
7882513b 2366 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
9ddd12af 2367 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
7882513b
JL
2368
2369 /* Allocate new skb here */
2370 skb = alloc_skb(frame_data_len, GFP_KERNEL);
2371 if (skb == NULL)
2372 goto err;
2373
9ddd12af 2374 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
7882513b
JL
2375 goto err;
2376
9ddd12af
JB
2377 /* Copy the data */
2378 memcpy(skb_put(skb, frame_data_len), frame_data, frame_data_len);
7882513b 2379
9ddd12af
JB
2380 data2 = get_hwsim_data_ref_from_addr(dst);
2381 if (!data2)
7882513b
JL
2382 goto out;
2383
2384 /* check if radio is configured properly */
2385
e8261171 2386 if (data2->idle || !data2->started)
7882513b
JL
2387 goto out;
2388
9ddd12af 2389 /* A frame is received from user space */
7882513b
JL
2390 memset(&rx_status, 0, sizeof(rx_status));
2391 rx_status.freq = data2->channel->center_freq;
2392 rx_status.band = data2->channel->band;
2393 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
2394 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2395
2396 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
2397 ieee80211_rx_irqsafe(data2->hw, skb);
2398
2399 return 0;
2400err:
c393862f 2401 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
7882513b
JL
2402out:
2403 dev_kfree_skb(skb);
2404 return -EINVAL;
2405}
2406
2407static int hwsim_register_received_nl(struct sk_buff *skb_2,
2408 struct genl_info *info)
2409{
c5ac0854
JB
2410 struct mac80211_hwsim_data *data;
2411 int chans = 1;
2412
2413 spin_lock_bh(&hwsim_radio_lock);
2414 list_for_each_entry(data, &hwsim_radios, list)
2415 chans = max(chans, data->channels);
2416 spin_unlock_bh(&hwsim_radio_lock);
2417
2418 /* In the future we should revise the userspace API and allow it
2419 * to set a flag that it does support multi-channel, then we can
2420 * let this pass conditionally on the flag.
2421 * For current userspace, prohibit it since it won't work right.
2422 */
2423 if (chans > 1)
2424 return -EOPNOTSUPP;
2425
85ca8fc7
JB
2426 if (wmediumd_portid)
2427 return -EBUSY;
7882513b 2428
15e47304 2429 wmediumd_portid = info->snd_portid;
7882513b
JL
2430
2431 printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
15e47304 2432 "switching to wmediumd mode with pid %d\n", info->snd_portid);
7882513b
JL
2433
2434 return 0;
7882513b
JL
2435}
2436
bc791098
JB
2437static int hwsim_create_radio_nl(struct sk_buff *msg, struct genl_info *info)
2438{
2439 unsigned int chans = channels;
26b0e411
JB
2440 const char *alpha2 = NULL;
2441 const struct ieee80211_regdomain *regd = NULL;
2442 bool reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
8c66a3d9 2443 bool p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
e9ed49bf 2444 bool destroy_on_close = info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
361c3e04 2445 bool use_chanctx;
bc791098
JB
2446
2447 if (info->attrs[HWSIM_ATTR_CHANNELS])
2448 chans = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
2449
361c3e04
LC
2450 if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
2451 use_chanctx = true;
2452 else
2453 use_chanctx = (chans > 1);
2454
26b0e411
JB
2455 if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
2456 alpha2 = nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
2457
2458 if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
2459 u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
2460
2461 if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
2462 return -EINVAL;
2463 regd = hwsim_world_regdom_custom[idx];
2464 }
2465
8c66a3d9 2466 return mac80211_hwsim_create_radio(chans, alpha2, regd, reg_strict,
e9ed49bf
JR
2467 p2p_device, use_chanctx,
2468 destroy_on_close, info->snd_portid);
bc791098
JB
2469}
2470
2471static int hwsim_destroy_radio_nl(struct sk_buff *msg, struct genl_info *info)
2472{
2473 struct mac80211_hwsim_data *data;
2474 int idx;
2475
2476 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
2477 return -EINVAL;
2478 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
2479
2480 spin_lock_bh(&hwsim_radio_lock);
2481 list_for_each_entry(data, &hwsim_radios, list) {
2482 if (data->idx != idx)
2483 continue;
2484 list_del(&data->list);
2485 spin_unlock_bh(&hwsim_radio_lock);
2486 mac80211_hwsim_destroy_radio(data);
2487 return 0;
2488 }
2489 spin_unlock_bh(&hwsim_radio_lock);
2490
2491 return -ENODEV;
7882513b
JL
2492}
2493
2494/* Generic Netlink operations array */
4534de83 2495static const struct genl_ops hwsim_ops[] = {
7882513b
JL
2496 {
2497 .cmd = HWSIM_CMD_REGISTER,
2498 .policy = hwsim_genl_policy,
2499 .doit = hwsim_register_received_nl,
2500 .flags = GENL_ADMIN_PERM,
2501 },
2502 {
2503 .cmd = HWSIM_CMD_FRAME,
2504 .policy = hwsim_genl_policy,
2505 .doit = hwsim_cloned_frame_received_nl,
2506 },
2507 {
2508 .cmd = HWSIM_CMD_TX_INFO_FRAME,
2509 .policy = hwsim_genl_policy,
2510 .doit = hwsim_tx_info_frame_received_nl,
2511 },
bc791098
JB
2512 {
2513 .cmd = HWSIM_CMD_CREATE_RADIO,
2514 .policy = hwsim_genl_policy,
2515 .doit = hwsim_create_radio_nl,
2516 .flags = GENL_ADMIN_PERM,
2517 },
2518 {
2519 .cmd = HWSIM_CMD_DESTROY_RADIO,
2520 .policy = hwsim_genl_policy,
2521 .doit = hwsim_destroy_radio_nl,
2522 .flags = GENL_ADMIN_PERM,
2523 },
7882513b
JL
2524};
2525
e9ed49bf
JR
2526static void destroy_radio(struct work_struct *work)
2527{
2528 struct mac80211_hwsim_data *data =
2529 container_of(work, struct mac80211_hwsim_data, destroy_work);
2530
2531 mac80211_hwsim_destroy_radio(data);
2532}
2533
2534static void remove_user_radios(u32 portid)
2535{
2536 struct mac80211_hwsim_data *entry, *tmp;
2537
2538 spin_lock_bh(&hwsim_radio_lock);
2539 list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
2540 if (entry->destroy_on_close && entry->portid == portid) {
2541 list_del(&entry->list);
2542 INIT_WORK(&entry->destroy_work, destroy_radio);
2543 schedule_work(&entry->destroy_work);
2544 }
2545 }
2546 spin_unlock_bh(&hwsim_radio_lock);
2547}
2548
7882513b
JL
2549static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
2550 unsigned long state,
2551 void *_notify)
2552{
2553 struct netlink_notify *notify = _notify;
2554
2555 if (state != NETLINK_URELEASE)
2556 return NOTIFY_DONE;
2557
e9ed49bf
JR
2558 remove_user_radios(notify->portid);
2559
15e47304 2560 if (notify->portid == wmediumd_portid) {
7882513b
JL
2561 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
2562 " socket, switching to perfect channel medium\n");
15e47304 2563 wmediumd_portid = 0;
7882513b
JL
2564 }
2565 return NOTIFY_DONE;
2566
2567}
2568
2569static struct notifier_block hwsim_netlink_notifier = {
2570 .notifier_call = mac80211_hwsim_netlink_notify,
2571};
2572
2573static int hwsim_init_netlink(void)
2574{
2575 int rc;
e8261171 2576
7882513b
JL
2577 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
2578
c53ed742 2579 rc = genl_register_family_with_ops(&hwsim_genl_family, hwsim_ops);
7882513b
JL
2580 if (rc)
2581 goto failure;
2582
2583 rc = netlink_register_notifier(&hwsim_netlink_notifier);
2584 if (rc)
2585 goto failure;
2586
2587 return 0;
2588
2589failure:
c393862f 2590 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
7882513b
JL
2591 return -EINVAL;
2592}
2593
2594static void hwsim_exit_netlink(void)
2595{
7882513b
JL
2596 /* unregister the notifier */
2597 netlink_unregister_notifier(&hwsim_netlink_notifier);
2598 /* unregister the family */
cf7a6b2d 2599 genl_unregister_family(&hwsim_genl_family);
7882513b
JL
2600}
2601
acc1e7a3
JM
2602static int __init init_mac80211_hwsim(void)
2603{
f39c2bfa 2604 int i, err;
acc1e7a3 2605
bc791098 2606 if (radios < 0 || radios > 100)
acc1e7a3
JM
2607 return -EINVAL;
2608
e8261171
JB
2609 if (channels < 1)
2610 return -EINVAL;
2611
38ed5048
JB
2612 mac80211_hwsim_mchan_ops = mac80211_hwsim_ops;
2613 mac80211_hwsim_mchan_ops.hw_scan = mac80211_hwsim_hw_scan;
2614 mac80211_hwsim_mchan_ops.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan;
2615 mac80211_hwsim_mchan_ops.sw_scan_start = NULL;
2616 mac80211_hwsim_mchan_ops.sw_scan_complete = NULL;
2617 mac80211_hwsim_mchan_ops.remain_on_channel = mac80211_hwsim_roc;
2618 mac80211_hwsim_mchan_ops.cancel_remain_on_channel = mac80211_hwsim_croc;
2619 mac80211_hwsim_mchan_ops.add_chanctx = mac80211_hwsim_add_chanctx;
2620 mac80211_hwsim_mchan_ops.remove_chanctx = mac80211_hwsim_remove_chanctx;
2621 mac80211_hwsim_mchan_ops.change_chanctx = mac80211_hwsim_change_chanctx;
2622 mac80211_hwsim_mchan_ops.assign_vif_chanctx =
2623 mac80211_hwsim_assign_vif_chanctx;
2624 mac80211_hwsim_mchan_ops.unassign_vif_chanctx =
2625 mac80211_hwsim_unassign_vif_chanctx;
69068036 2626
0e057d73
JB
2627 spin_lock_init(&hwsim_radio_lock);
2628 INIT_LIST_HEAD(&hwsim_radios);
acc1e7a3 2629
c07fe5ae 2630 err = platform_driver_register(&mac80211_hwsim_driver);
9ea92774
MP
2631 if (err)
2632 return err;
2633
acc1e7a3 2634 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
9ea92774
MP
2635 if (IS_ERR(hwsim_class)) {
2636 err = PTR_ERR(hwsim_class);
f39c2bfa 2637 goto out_unregister_driver;
9ea92774 2638 }
acc1e7a3 2639
0e057d73 2640 for (i = 0; i < radios; i++) {
26b0e411
JB
2641 const char *reg_alpha2 = NULL;
2642 const struct ieee80211_regdomain *regd = NULL;
2643 bool reg_strict = false;
acc1e7a3 2644
4a5af9c2 2645 switch (regtest) {
4a5af9c2 2646 case HWSIM_REGTEST_DIFF_COUNTRY:
26b0e411
JB
2647 if (i < ARRAY_SIZE(hwsim_alpha2s))
2648 reg_alpha2 = hwsim_alpha2s[i];
4a5af9c2
LR
2649 break;
2650 case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
2651 if (!i)
26b0e411 2652 reg_alpha2 = hwsim_alpha2s[0];
4a5af9c2 2653 break;
4a5af9c2 2654 case HWSIM_REGTEST_STRICT_ALL:
26b0e411
JB
2655 reg_strict = true;
2656 case HWSIM_REGTEST_DRIVER_REG_ALL:
2657 reg_alpha2 = hwsim_alpha2s[0];
4a5af9c2 2658 break;
26b0e411
JB
2659 case HWSIM_REGTEST_WORLD_ROAM:
2660 if (i == 0)
2661 regd = &hwsim_world_regdom_custom_01;
4a5af9c2
LR
2662 break;
2663 case HWSIM_REGTEST_CUSTOM_WORLD:
26b0e411
JB
2664 regd = &hwsim_world_regdom_custom_01;
2665 break;
4a5af9c2 2666 case HWSIM_REGTEST_CUSTOM_WORLD_2:
26b0e411
JB
2667 if (i == 0)
2668 regd = &hwsim_world_regdom_custom_01;
2669 else if (i == 1)
2670 regd = &hwsim_world_regdom_custom_02;
4a5af9c2
LR
2671 break;
2672 case HWSIM_REGTEST_STRICT_FOLLOW:
26b0e411
JB
2673 if (i == 0) {
2674 reg_strict = true;
2675 reg_alpha2 = hwsim_alpha2s[0];
2676 }
4a5af9c2
LR
2677 break;
2678 case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
26b0e411
JB
2679 if (i == 0) {
2680 reg_strict = true;
2681 reg_alpha2 = hwsim_alpha2s[0];
2682 } else if (i == 1) {
2683 reg_alpha2 = hwsim_alpha2s[1];
2684 }
4a5af9c2
LR
2685 break;
2686 case HWSIM_REGTEST_ALL:
26b0e411
JB
2687 switch (i) {
2688 case 0:
2689 regd = &hwsim_world_regdom_custom_01;
2690 break;
2691 case 1:
2692 regd = &hwsim_world_regdom_custom_02;
2693 break;
2694 case 2:
2695 reg_alpha2 = hwsim_alpha2s[0];
2696 break;
2697 case 3:
2698 reg_alpha2 = hwsim_alpha2s[1];
2699 break;
2700 case 4:
2701 reg_strict = true;
2702 reg_alpha2 = hwsim_alpha2s[2];
2703 break;
2704 }
4a5af9c2
LR
2705 break;
2706 default:
2707 break;
2708 }
2709
26b0e411 2710 err = mac80211_hwsim_create_radio(channels, reg_alpha2,
8c66a3d9 2711 regd, reg_strict,
361c3e04 2712 support_p2p_device,
e9ed49bf 2713 channels > 1, false, 0);
bc791098 2714 if (err < 0)
f39c2bfa 2715 goto out_free_radios;
acc1e7a3
JM
2716 }
2717
c835a677
TG
2718 hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
2719 hwsim_mon_setup);
bcdd8220
WY
2720 if (hwsim_mon == NULL) {
2721 err = -ENOMEM;
f39c2bfa 2722 goto out_free_radios;
bcdd8220 2723 }
acc1e7a3 2724
7882513b 2725 rtnl_lock();
7882513b 2726 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
f39c2bfa
JB
2727 if (err < 0) {
2728 rtnl_unlock();
2729 goto out_free_radios;
2730 }
7882513b
JL
2731
2732 err = register_netdevice(hwsim_mon);
f39c2bfa
JB
2733 if (err < 0) {
2734 rtnl_unlock();
2735 goto out_free_mon;
2736 }
7882513b
JL
2737 rtnl_unlock();
2738
2739 err = hwsim_init_netlink();
2740 if (err < 0)
f39c2bfa 2741 goto out_free_mon;
7882513b 2742
acc1e7a3
JM
2743 return 0;
2744
f39c2bfa 2745out_free_mon:
acc1e7a3 2746 free_netdev(hwsim_mon);
f39c2bfa 2747out_free_radios:
3a33cc10 2748 mac80211_hwsim_free();
f39c2bfa 2749out_unregister_driver:
c07fe5ae 2750 platform_driver_unregister(&mac80211_hwsim_driver);
acc1e7a3
JM
2751 return err;
2752}
f8fffc7e 2753module_init(init_mac80211_hwsim);
acc1e7a3
JM
2754
2755static void __exit exit_mac80211_hwsim(void)
2756{
0e057d73 2757 printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
acc1e7a3 2758
7882513b
JL
2759 hwsim_exit_netlink();
2760
acc1e7a3 2761 mac80211_hwsim_free();
5d416351 2762 unregister_netdev(hwsim_mon);
c07fe5ae 2763 platform_driver_unregister(&mac80211_hwsim_driver);
acc1e7a3 2764}
acc1e7a3 2765module_exit(exit_mac80211_hwsim);