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