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