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