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