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