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>
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.
13 * - Add TSF sync and fix IBSS beacon transmission by adding
14 * competition for "air time" at TBTT
15 * - RX filtering based on filter configuration (data->rx_filter)
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
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>
28 #include <linux/platform_device.h>
29 #include <linux/debugfs.h>
30 #include <linux/module.h>
31 #include <linux/ktime.h>
32 #include <net/genetlink.h>
33 #include "mac80211_hwsim.h"
35 #define WARN_QUEUE 100
38 MODULE_AUTHOR("Jouni Malinen");
39 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
40 MODULE_LICENSE("GPL");
42 static u32 wmediumd_portid;
44 static int radios = 2;
45 module_param(radios, int, 0444);
46 MODULE_PARM_DESC(radios, "Number of simulated radios");
48 static int channels = 1;
49 module_param(channels, int, 0444);
50 MODULE_PARM_DESC(channels, "Number of concurrent channels");
52 static bool paged_rx = false;
53 module_param(paged_rx, bool, 0644);
54 MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
56 static bool rctbl = false;
57 module_param(rctbl, bool, 0444);
58 MODULE_PARM_DESC(rctbl, "Handle rate control table");
60 struct hwsim_vif_priv {
68 #define HWSIM_VIF_MAGIC 0x69537748
70 static inline void hwsim_check_magic(struct ieee80211_vif *vif)
72 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
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);
78 static inline void hwsim_set_magic(struct ieee80211_vif *vif)
80 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
81 vp->magic = HWSIM_VIF_MAGIC;
84 static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
86 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
90 struct hwsim_sta_priv {
94 #define HWSIM_STA_MAGIC 0x6d537749
96 static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
98 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
99 WARN_ON(sp->magic != HWSIM_STA_MAGIC);
102 static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
104 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
105 sp->magic = HWSIM_STA_MAGIC;
108 static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
110 struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
114 struct hwsim_chanctx_priv {
118 #define HWSIM_CHANCTX_MAGIC 0x6d53774a
120 static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
122 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
123 WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
126 static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
128 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
129 cp->magic = HWSIM_CHANCTX_MAGIC;
132 static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
134 struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
138 static struct class *hwsim_class;
140 static struct net_device *hwsim_mon; /* global monitor netdev */
142 #define CHAN2G(_freq) { \
143 .band = IEEE80211_BAND_2GHZ, \
144 .center_freq = (_freq), \
145 .hw_value = (_freq), \
149 #define CHAN5G(_freq) { \
150 .band = IEEE80211_BAND_5GHZ, \
151 .center_freq = (_freq), \
152 .hw_value = (_freq), \
156 static 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 */
173 static 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 */
179 CHAN5G(5260), /* Channel 52 */
180 CHAN5G(5280), /* Channel 56 */
181 CHAN5G(5300), /* Channel 60 */
182 CHAN5G(5320), /* Channel 64 */
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 */
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 */
203 static const struct ieee80211_rate hwsim_rates[] = {
205 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
206 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
207 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
218 static 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) |
225 BIT(NL80211_IFTYPE_AP) |
226 BIT(NL80211_IFTYPE_P2P_GO) },
227 { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
230 static const struct ieee80211_iface_limit hwsim_if_dfs_limits[] = {
231 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
234 static const struct ieee80211_iface_combination hwsim_if_comb[] = {
236 .limits = hwsim_if_limits,
237 .n_limits = ARRAY_SIZE(hwsim_if_limits),
238 .max_interfaces = 2048,
239 .num_different_channels = 1,
242 .limits = hwsim_if_dfs_limits,
243 .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits),
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),
254 static spinlock_t hwsim_radio_lock;
255 static struct list_head hwsim_radios;
256 static int hwsim_radio_idx;
258 static struct platform_driver mac80211_hwsim_driver = {
260 .name = "mac80211_hwsim",
261 .owner = THIS_MODULE,
265 struct mac80211_hwsim_data {
266 struct list_head list;
267 struct ieee80211_hw *hw;
269 struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
270 struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
271 struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
272 struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
273 struct ieee80211_iface_combination if_combination;
275 struct mac_address addresses[2];
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;
285 struct ieee80211_channel *channel;
286 u64 beacon_int /* beacon interval in us */;
287 unsigned int rx_filter;
288 bool started, idle, scanning;
290 struct tasklet_hrtimer beacon_timer;
292 PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
294 bool ps_poll_pending;
295 struct dentry *debugfs;
297 struct sk_buff_head pending; /* packets pending */
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.
307 /* difference between this hw's clock and the real clock, in usecs */
310 /* absolute beacon transmission time. Used to cover up "tx" delay. */
315 struct hwsim_radiotap_hdr {
316 struct ieee80211_radiotap_header hdr;
324 struct hwsim_radiotap_ack_hdr {
325 struct ieee80211_radiotap_header hdr;
332 /* MAC80211_HWSIM netlinf family */
333 static struct genl_family hwsim_genl_family = {
334 .id = GENL_ID_GENERATE,
336 .name = "MAC80211_HWSIM",
338 .maxattr = HWSIM_ATTR_MAX,
341 /* MAC80211_HWSIM netlink policy */
343 static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
344 [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
345 [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN },
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,
352 .len = IEEE80211_TX_MAX_RATES *
353 sizeof(struct hwsim_tx_rate)},
354 [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
355 [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
356 [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
359 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
361 struct ieee80211_channel *chan);
363 /* sysfs attributes */
364 static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
366 struct mac80211_hwsim_data *data = dat;
367 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
369 struct ieee80211_pspoll *pspoll;
374 wiphy_debug(data->hw->wiphy,
375 "%s: send PS-Poll to %pM for aid %d\n",
376 __func__, vp->bssid, vp->aid);
378 skb = dev_alloc_skb(sizeof(*pspoll));
381 pspoll = (void *) skb_put(skb, sizeof(*pspoll));
382 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
383 IEEE80211_STYPE_PSPOLL |
385 pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
386 memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
387 memcpy(pspoll->ta, mac, ETH_ALEN);
390 mac80211_hwsim_tx_frame(data->hw, skb,
391 rcu_dereference(vif->chanctx_conf)->def.chan);
395 static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
396 struct ieee80211_vif *vif, int ps)
398 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
400 struct ieee80211_hdr *hdr;
405 wiphy_debug(data->hw->wiphy,
406 "%s: send data::nullfunc to %pM ps=%d\n",
407 __func__, vp->bssid, ps);
409 skb = dev_alloc_skb(sizeof(*hdr));
412 hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
413 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
414 IEEE80211_STYPE_NULLFUNC |
415 (ps ? IEEE80211_FCTL_PM : 0));
416 hdr->duration_id = cpu_to_le16(0);
417 memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
418 memcpy(hdr->addr2, mac, ETH_ALEN);
419 memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
422 mac80211_hwsim_tx_frame(data->hw, skb,
423 rcu_dereference(vif->chanctx_conf)->def.chan);
428 static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
429 struct ieee80211_vif *vif)
431 struct mac80211_hwsim_data *data = dat;
432 hwsim_send_nullfunc(data, mac, vif, 1);
435 static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
436 struct ieee80211_vif *vif)
438 struct mac80211_hwsim_data *data = dat;
439 hwsim_send_nullfunc(data, mac, vif, 0);
442 static int hwsim_fops_ps_read(void *dat, u64 *val)
444 struct mac80211_hwsim_data *data = dat;
449 static int hwsim_fops_ps_write(void *dat, u64 val)
451 struct mac80211_hwsim_data *data = dat;
454 if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
455 val != PS_MANUAL_POLL)
461 if (val == PS_MANUAL_POLL) {
462 ieee80211_iterate_active_interfaces(data->hw,
463 IEEE80211_IFACE_ITER_NORMAL,
464 hwsim_send_ps_poll, data);
465 data->ps_poll_pending = true;
466 } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
467 ieee80211_iterate_active_interfaces(data->hw,
468 IEEE80211_IFACE_ITER_NORMAL,
469 hwsim_send_nullfunc_ps,
471 } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
472 ieee80211_iterate_active_interfaces(data->hw,
473 IEEE80211_IFACE_ITER_NORMAL,
474 hwsim_send_nullfunc_no_ps,
481 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
484 static int hwsim_write_simulate_radar(void *dat, u64 val)
486 struct mac80211_hwsim_data *data = dat;
488 ieee80211_radar_detected(data->hw);
493 DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL,
494 hwsim_write_simulate_radar, "%llu\n");
496 static int hwsim_fops_group_read(void *dat, u64 *val)
498 struct mac80211_hwsim_data *data = dat;
503 static int hwsim_fops_group_write(void *dat, u64 val)
505 struct mac80211_hwsim_data *data = dat;
510 DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
511 hwsim_fops_group_read, hwsim_fops_group_write,
514 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
515 struct net_device *dev)
517 /* TODO: allow packet injection */
522 static inline u64 mac80211_hwsim_get_tsf_raw(void)
524 return ktime_to_us(ktime_get_real());
527 static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
529 u64 now = mac80211_hwsim_get_tsf_raw();
530 return cpu_to_le64(now + data->tsf_offset);
533 static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
534 struct ieee80211_vif *vif)
536 struct mac80211_hwsim_data *data = hw->priv;
537 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
540 static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
541 struct ieee80211_vif *vif, u64 tsf)
543 struct mac80211_hwsim_data *data = hw->priv;
544 u64 now = mac80211_hwsim_get_tsf(hw, vif);
545 u32 bcn_int = data->beacon_int;
546 s64 delta = tsf - now;
548 data->tsf_offset += delta;
549 /* adjust after beaconing with new timestamp at old TBTT */
550 data->bcn_delta = do_div(delta, bcn_int);
553 static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
554 struct sk_buff *tx_skb,
555 struct ieee80211_channel *chan)
557 struct mac80211_hwsim_data *data = hw->priv;
559 struct hwsim_radiotap_hdr *hdr;
561 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
562 struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
564 if (!netif_running(hwsim_mon))
567 skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
571 hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
572 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
574 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
575 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
576 (1 << IEEE80211_RADIOTAP_RATE) |
577 (1 << IEEE80211_RADIOTAP_TSFT) |
578 (1 << IEEE80211_RADIOTAP_CHANNEL));
579 hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
581 hdr->rt_rate = txrate->bitrate / 5;
582 hdr->rt_channel = cpu_to_le16(chan->center_freq);
583 flags = IEEE80211_CHAN_2GHZ;
584 if (txrate->flags & IEEE80211_RATE_ERP_G)
585 flags |= IEEE80211_CHAN_OFDM;
587 flags |= IEEE80211_CHAN_CCK;
588 hdr->rt_chbitmask = cpu_to_le16(flags);
590 skb->dev = hwsim_mon;
591 skb_set_mac_header(skb, 0);
592 skb->ip_summed = CHECKSUM_UNNECESSARY;
593 skb->pkt_type = PACKET_OTHERHOST;
594 skb->protocol = htons(ETH_P_802_2);
595 memset(skb->cb, 0, sizeof(skb->cb));
600 static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
604 struct hwsim_radiotap_ack_hdr *hdr;
606 struct ieee80211_hdr *hdr11;
608 if (!netif_running(hwsim_mon))
611 skb = dev_alloc_skb(100);
615 hdr = (struct hwsim_radiotap_ack_hdr *) skb_put(skb, sizeof(*hdr));
616 hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
618 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
619 hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
620 (1 << IEEE80211_RADIOTAP_CHANNEL));
623 hdr->rt_channel = cpu_to_le16(chan->center_freq);
624 flags = IEEE80211_CHAN_2GHZ;
625 hdr->rt_chbitmask = cpu_to_le16(flags);
627 hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
628 hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
629 IEEE80211_STYPE_ACK);
630 hdr11->duration_id = cpu_to_le16(0);
631 memcpy(hdr11->addr1, addr, ETH_ALEN);
633 skb->dev = hwsim_mon;
634 skb_set_mac_header(skb, 0);
635 skb->ip_summed = CHECKSUM_UNNECESSARY;
636 skb->pkt_type = PACKET_OTHERHOST;
637 skb->protocol = htons(ETH_P_802_2);
638 memset(skb->cb, 0, sizeof(skb->cb));
643 static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
652 /* TODO: accept (some) Beacons by default and other frames only
653 * if pending PS-Poll has been sent */
656 /* Allow unicast frames to own address if there is a pending
658 if (data->ps_poll_pending &&
659 memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
661 data->ps_poll_pending = false;
671 struct mac80211_hwsim_addr_match_data {
676 static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
677 struct ieee80211_vif *vif)
679 struct mac80211_hwsim_addr_match_data *md = data;
680 if (memcmp(mac, md->addr, ETH_ALEN) == 0)
685 static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
688 struct mac80211_hwsim_addr_match_data md;
690 if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
695 ieee80211_iterate_active_interfaces_atomic(data->hw,
696 IEEE80211_IFACE_ITER_NORMAL,
697 mac80211_hwsim_addr_iter,
703 static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
704 struct sk_buff *my_skb,
708 struct mac80211_hwsim_data *data = hw->priv;
709 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
710 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
712 unsigned int hwsim_flags = 0;
714 struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
716 if (data->ps != PS_DISABLED)
717 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
718 /* If the queue contains MAX_QUEUE skb's drop some */
719 if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
720 /* Droping until WARN_QUEUE level */
721 while (skb_queue_len(&data->pending) >= WARN_QUEUE)
722 skb_dequeue(&data->pending);
725 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
727 goto nla_put_failure;
729 msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
731 if (msg_head == NULL) {
732 printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
733 goto nla_put_failure;
736 if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
737 ETH_ALEN, data->addresses[1].addr))
738 goto nla_put_failure;
740 /* We get the skb->data */
741 if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
742 goto nla_put_failure;
744 /* We get the flags for this transmission, and we translate them to
747 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
748 hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
750 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
751 hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
753 if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
754 goto nla_put_failure;
756 /* We get the tx control (rate and retries) info*/
758 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
759 tx_attempts[i].idx = info->status.rates[i].idx;
760 tx_attempts[i].count = info->status.rates[i].count;
763 if (nla_put(skb, HWSIM_ATTR_TX_INFO,
764 sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
766 goto nla_put_failure;
768 /* We create a cookie to identify this skb */
769 if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
770 goto nla_put_failure;
772 genlmsg_end(skb, msg_head);
773 genlmsg_unicast(&init_net, skb, dst_portid);
775 /* Enqueue the packet */
776 skb_queue_tail(&data->pending, my_skb);
780 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
783 static bool hwsim_chans_compat(struct ieee80211_channel *c1,
784 struct ieee80211_channel *c2)
789 return c1->center_freq == c2->center_freq;
792 struct tx_iter_data {
793 struct ieee80211_channel *channel;
797 static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
798 struct ieee80211_vif *vif)
800 struct tx_iter_data *data = _data;
802 if (!vif->chanctx_conf)
805 if (!hwsim_chans_compat(data->channel,
806 rcu_dereference(vif->chanctx_conf)->def.chan))
809 data->receive = true;
812 static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
814 struct ieee80211_channel *chan)
816 struct mac80211_hwsim_data *data = hw->priv, *data2;
818 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
819 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
820 struct ieee80211_rx_status rx_status;
823 memset(&rx_status, 0, sizeof(rx_status));
824 rx_status.flag |= RX_FLAG_MACTIME_START;
825 rx_status.freq = chan->center_freq;
826 rx_status.band = chan->band;
827 if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
829 ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
831 ieee80211_rate_get_vht_nss(&info->control.rates[0]);
832 rx_status.flag |= RX_FLAG_VHT;
834 rx_status.rate_idx = info->control.rates[0].idx;
835 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
836 rx_status.flag |= RX_FLAG_HT;
838 if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
839 rx_status.flag |= RX_FLAG_40MHZ;
840 if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
841 rx_status.flag |= RX_FLAG_SHORT_GI;
842 /* TODO: simulate real signal strength (and optional packet loss) */
843 rx_status.signal = data->power_level - 50;
845 if (data->ps != PS_DISABLED)
846 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
848 /* release the skb's source info */
856 * Get absolute mactime here so all HWs RX at the "same time", and
857 * absolute TX time for beacon mactime so the timestamp matches.
858 * Giving beacons a different mactime than non-beacons looks messy, but
859 * it helps the Toffset be exact and a ~10us mactime discrepancy
860 * probably doesn't really matter.
862 if (ieee80211_is_beacon(hdr->frame_control) ||
863 ieee80211_is_probe_resp(hdr->frame_control))
864 now = data->abs_bcn_ts;
866 now = mac80211_hwsim_get_tsf_raw();
868 /* Copy skb to all enabled radios that are on the current frequency */
869 spin_lock(&hwsim_radio_lock);
870 list_for_each_entry(data2, &hwsim_radios, list) {
871 struct sk_buff *nskb;
872 struct tx_iter_data tx_iter_data = {
880 if (!data2->started || (data2->idle && !data2->tmp_chan) ||
881 !hwsim_ps_rx_ok(data2, skb))
884 if (!(data->group & data2->group))
887 if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
888 !hwsim_chans_compat(chan, data2->channel)) {
889 ieee80211_iterate_active_interfaces_atomic(
890 data2->hw, IEEE80211_IFACE_ITER_NORMAL,
891 mac80211_hwsim_tx_iter, &tx_iter_data);
892 if (!tx_iter_data.receive)
897 * reserve some space for our vendor and the normal
898 * radiotap header, since we're copying anyway
900 if (skb->len < PAGE_SIZE && paged_rx) {
901 struct page *page = alloc_page(GFP_ATOMIC);
906 nskb = dev_alloc_skb(128);
912 memcpy(page_address(page), skb->data, skb->len);
913 skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
915 nskb = skb_copy(skb, GFP_ATOMIC);
920 if (mac80211_hwsim_addr_match(data2, hdr->addr1))
923 rx_status.mactime = now + data2->tsf_offset;
926 * Don't enable this code by default as the OUI 00:00:00
927 * is registered to Xerox so we shouldn't use it here, it
928 * might find its way into pcap files.
929 * Note that this code requires the headroom in the SKB
930 * that was allocated earlier.
932 rx_status.vendor_radiotap_oui[0] = 0x00;
933 rx_status.vendor_radiotap_oui[1] = 0x00;
934 rx_status.vendor_radiotap_oui[2] = 0x00;
935 rx_status.vendor_radiotap_subns = 127;
937 * Radiotap vendor namespaces can (and should) also be
938 * split into fields by using the standard radiotap
939 * presence bitmap mechanism. Use just BIT(0) here for
940 * the presence bitmap.
942 rx_status.vendor_radiotap_bitmap = BIT(0);
943 /* We have 8 bytes of (dummy) data */
944 rx_status.vendor_radiotap_len = 8;
945 /* For testing, also require it to be aligned */
946 rx_status.vendor_radiotap_align = 8;
948 memcpy(skb_push(nskb, 8), "ABCDEFGH", 8);
951 memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
952 ieee80211_rx_irqsafe(data2->hw, nskb);
954 spin_unlock(&hwsim_radio_lock);
959 static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
960 struct ieee80211_tx_control *control,
963 struct mac80211_hwsim_data *data = hw->priv;
964 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
965 struct ieee80211_chanctx_conf *chanctx_conf;
966 struct ieee80211_channel *channel;
970 if (WARN_ON(skb->len < 10)) {
971 /* Should not happen; just a sanity check for addr1 use */
972 ieee80211_free_txskb(hw, skb);
976 if (data->channels == 1) {
977 channel = data->channel;
978 } else if (txi->hw_queue == 4) {
979 channel = data->tmp_chan;
981 chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
983 channel = chanctx_conf->def.chan;
988 if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
989 ieee80211_free_txskb(hw, skb);
993 if (data->idle && !data->tmp_chan) {
994 wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
995 ieee80211_free_txskb(hw, skb);
999 if (txi->control.vif)
1000 hwsim_check_magic(txi->control.vif);
1002 hwsim_check_sta_magic(control->sta);
1004 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
1005 ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1007 ARRAY_SIZE(txi->control.rates));
1009 txi->rate_driver_data[0] = channel;
1010 mac80211_hwsim_monitor_rx(hw, skb, channel);
1012 /* wmediumd mode check */
1013 _portid = ACCESS_ONCE(wmediumd_portid);
1016 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
1018 /* NO wmediumd detected, perfect medium simulation */
1019 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1021 if (ack && skb->len >= 16) {
1022 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1023 mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1026 ieee80211_tx_info_clear_status(txi);
1028 /* frame was transmitted at most favorable rate at first attempt */
1029 txi->control.rates[0].count = 1;
1030 txi->control.rates[1].idx = -1;
1032 if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1033 txi->flags |= IEEE80211_TX_STAT_ACK;
1034 ieee80211_tx_status_irqsafe(hw, skb);
1038 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1040 struct mac80211_hwsim_data *data = hw->priv;
1041 wiphy_debug(hw->wiphy, "%s\n", __func__);
1042 data->started = true;
1047 static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1049 struct mac80211_hwsim_data *data = hw->priv;
1050 data->started = false;
1051 tasklet_hrtimer_cancel(&data->beacon_timer);
1052 wiphy_debug(hw->wiphy, "%s\n", __func__);
1056 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1057 struct ieee80211_vif *vif)
1059 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1060 __func__, ieee80211_vif_type_p2p(vif),
1062 hwsim_set_magic(vif);
1065 vif->hw_queue[IEEE80211_AC_VO] = 0;
1066 vif->hw_queue[IEEE80211_AC_VI] = 1;
1067 vif->hw_queue[IEEE80211_AC_BE] = 2;
1068 vif->hw_queue[IEEE80211_AC_BK] = 3;
1074 static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1075 struct ieee80211_vif *vif,
1076 enum nl80211_iftype newtype,
1079 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1080 wiphy_debug(hw->wiphy,
1081 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1082 __func__, ieee80211_vif_type_p2p(vif),
1083 newtype, vif->addr);
1084 hwsim_check_magic(vif);
1087 * interface may change from non-AP to AP in
1088 * which case this needs to be set up again
1095 static void mac80211_hwsim_remove_interface(
1096 struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1098 wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1099 __func__, ieee80211_vif_type_p2p(vif),
1101 hwsim_check_magic(vif);
1102 hwsim_clear_magic(vif);
1105 static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1106 struct sk_buff *skb,
1107 struct ieee80211_channel *chan)
1109 u32 _pid = ACCESS_ONCE(wmediumd_portid);
1111 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE) {
1112 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1113 ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1115 ARRAY_SIZE(txi->control.rates));
1118 mac80211_hwsim_monitor_rx(hw, skb, chan);
1121 return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
1123 mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1127 static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1128 struct ieee80211_vif *vif)
1130 struct mac80211_hwsim_data *data = arg;
1131 struct ieee80211_hw *hw = data->hw;
1132 struct ieee80211_tx_info *info;
1133 struct ieee80211_rate *txrate;
1134 struct ieee80211_mgmt *mgmt;
1135 struct sk_buff *skb;
1137 hwsim_check_magic(vif);
1139 if (vif->type != NL80211_IFTYPE_AP &&
1140 vif->type != NL80211_IFTYPE_MESH_POINT &&
1141 vif->type != NL80211_IFTYPE_ADHOC)
1144 skb = ieee80211_beacon_get(hw, vif);
1147 info = IEEE80211_SKB_CB(skb);
1148 if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
1149 ieee80211_get_tx_rates(vif, NULL, skb,
1150 info->control.rates,
1151 ARRAY_SIZE(info->control.rates));
1153 txrate = ieee80211_get_tx_rate(hw, info);
1155 mgmt = (struct ieee80211_mgmt *) skb->data;
1156 /* fake header transmission time */
1157 data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1158 mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1160 24 * 8 * 10 / txrate->bitrate);
1162 mac80211_hwsim_tx_frame(hw, skb,
1163 rcu_dereference(vif->chanctx_conf)->def.chan);
1166 static enum hrtimer_restart
1167 mac80211_hwsim_beacon(struct hrtimer *timer)
1169 struct mac80211_hwsim_data *data =
1170 container_of(timer, struct mac80211_hwsim_data,
1171 beacon_timer.timer);
1172 struct ieee80211_hw *hw = data->hw;
1173 u64 bcn_int = data->beacon_int;
1179 ieee80211_iterate_active_interfaces_atomic(
1180 hw, IEEE80211_IFACE_ITER_NORMAL,
1181 mac80211_hwsim_beacon_tx, data);
1183 /* beacon at new TBTT + beacon interval */
1184 if (data->bcn_delta) {
1185 bcn_int -= data->bcn_delta;
1186 data->bcn_delta = 0;
1189 next_bcn = ktime_add(hrtimer_get_expires(timer),
1190 ns_to_ktime(bcn_int * 1000));
1191 tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
1193 return HRTIMER_NORESTART;
1196 static const char * const hwsim_chanwidths[] = {
1197 [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1198 [NL80211_CHAN_WIDTH_20] = "ht20",
1199 [NL80211_CHAN_WIDTH_40] = "ht40",
1200 [NL80211_CHAN_WIDTH_80] = "vht80",
1201 [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1202 [NL80211_CHAN_WIDTH_160] = "vht160",
1205 static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1207 struct mac80211_hwsim_data *data = hw->priv;
1208 struct ieee80211_conf *conf = &hw->conf;
1209 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1210 [IEEE80211_SMPS_AUTOMATIC] = "auto",
1211 [IEEE80211_SMPS_OFF] = "off",
1212 [IEEE80211_SMPS_STATIC] = "static",
1213 [IEEE80211_SMPS_DYNAMIC] = "dynamic",
1216 if (conf->chandef.chan)
1217 wiphy_debug(hw->wiphy,
1218 "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
1220 conf->chandef.chan->center_freq,
1221 conf->chandef.center_freq1,
1222 conf->chandef.center_freq2,
1223 hwsim_chanwidths[conf->chandef.width],
1224 !!(conf->flags & IEEE80211_CONF_IDLE),
1225 !!(conf->flags & IEEE80211_CONF_PS),
1226 smps_modes[conf->smps_mode]);
1228 wiphy_debug(hw->wiphy,
1229 "%s (freq=0 idle=%d ps=%d smps=%s)\n",
1231 !!(conf->flags & IEEE80211_CONF_IDLE),
1232 !!(conf->flags & IEEE80211_CONF_PS),
1233 smps_modes[conf->smps_mode]);
1235 data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1237 data->channel = conf->chandef.chan;
1239 WARN_ON(data->channel && data->channels > 1);
1241 data->power_level = conf->power_level;
1242 if (!data->started || !data->beacon_int)
1243 tasklet_hrtimer_cancel(&data->beacon_timer);
1244 else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
1245 u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
1246 u32 bcn_int = data->beacon_int;
1247 u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1249 tasklet_hrtimer_start(&data->beacon_timer,
1250 ns_to_ktime(until_tbtt * 1000),
1258 static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
1259 unsigned int changed_flags,
1260 unsigned int *total_flags,u64 multicast)
1262 struct mac80211_hwsim_data *data = hw->priv;
1264 wiphy_debug(hw->wiphy, "%s\n", __func__);
1266 data->rx_filter = 0;
1267 if (*total_flags & FIF_PROMISC_IN_BSS)
1268 data->rx_filter |= FIF_PROMISC_IN_BSS;
1269 if (*total_flags & FIF_ALLMULTI)
1270 data->rx_filter |= FIF_ALLMULTI;
1272 *total_flags = data->rx_filter;
1275 static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
1276 struct ieee80211_vif *vif)
1278 unsigned int *count = data;
1279 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1285 static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
1286 struct ieee80211_vif *vif,
1287 struct ieee80211_bss_conf *info,
1290 struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
1291 struct mac80211_hwsim_data *data = hw->priv;
1293 hwsim_check_magic(vif);
1295 wiphy_debug(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
1296 __func__, changed, vif->addr);
1298 if (changed & BSS_CHANGED_BSSID) {
1299 wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
1300 __func__, info->bssid);
1301 memcpy(vp->bssid, info->bssid, ETH_ALEN);
1304 if (changed & BSS_CHANGED_ASSOC) {
1305 wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
1306 info->assoc, info->aid);
1307 vp->assoc = info->assoc;
1308 vp->aid = info->aid;
1311 if (changed & BSS_CHANGED_BEACON_INT) {
1312 wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
1313 data->beacon_int = info->beacon_int * 1024;
1316 if (changed & BSS_CHANGED_BEACON_ENABLED) {
1317 wiphy_debug(hw->wiphy, " BCN EN: %d\n", info->enable_beacon);
1318 vp->bcn_en = info->enable_beacon;
1319 if (data->started &&
1320 !hrtimer_is_queued(&data->beacon_timer.timer) &&
1321 info->enable_beacon) {
1322 u64 tsf, until_tbtt;
1324 if (WARN_ON(!data->beacon_int))
1325 data->beacon_int = 1000 * 1024;
1326 tsf = mac80211_hwsim_get_tsf(hw, vif);
1327 bcn_int = data->beacon_int;
1328 until_tbtt = bcn_int - do_div(tsf, bcn_int);
1329 tasklet_hrtimer_start(&data->beacon_timer,
1330 ns_to_ktime(until_tbtt * 1000),
1332 } else if (!info->enable_beacon) {
1333 unsigned int count = 0;
1334 ieee80211_iterate_active_interfaces_atomic(
1335 data->hw, IEEE80211_IFACE_ITER_NORMAL,
1336 mac80211_hwsim_bcn_en_iter, &count);
1337 wiphy_debug(hw->wiphy, " beaconing vifs remaining: %u",
1340 tasklet_hrtimer_cancel(&data->beacon_timer);
1344 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1345 wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
1346 info->use_cts_prot);
1349 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1350 wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
1351 info->use_short_preamble);
1354 if (changed & BSS_CHANGED_ERP_SLOT) {
1355 wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
1358 if (changed & BSS_CHANGED_HT) {
1359 wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
1360 info->ht_operation_mode);
1363 if (changed & BSS_CHANGED_BASIC_RATES) {
1364 wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
1365 (unsigned long long) info->basic_rates);
1368 if (changed & BSS_CHANGED_TXPOWER)
1369 wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
1372 static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
1373 struct ieee80211_vif *vif,
1374 struct ieee80211_sta *sta)
1376 hwsim_check_magic(vif);
1377 hwsim_set_sta_magic(sta);
1382 static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
1383 struct ieee80211_vif *vif,
1384 struct ieee80211_sta *sta)
1386 hwsim_check_magic(vif);
1387 hwsim_clear_sta_magic(sta);
1392 static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
1393 struct ieee80211_vif *vif,
1394 enum sta_notify_cmd cmd,
1395 struct ieee80211_sta *sta)
1397 hwsim_check_magic(vif);
1400 case STA_NOTIFY_SLEEP:
1401 case STA_NOTIFY_AWAKE:
1402 /* TODO: make good use of these flags */
1405 WARN(1, "Invalid sta notify: %d\n", cmd);
1410 static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
1411 struct ieee80211_sta *sta,
1414 hwsim_check_sta_magic(sta);
1418 static int mac80211_hwsim_conf_tx(
1419 struct ieee80211_hw *hw,
1420 struct ieee80211_vif *vif, u16 queue,
1421 const struct ieee80211_tx_queue_params *params)
1423 wiphy_debug(hw->wiphy,
1424 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1426 params->txop, params->cw_min,
1427 params->cw_max, params->aifs);
1431 static int mac80211_hwsim_get_survey(
1432 struct ieee80211_hw *hw, int idx,
1433 struct survey_info *survey)
1435 struct ieee80211_conf *conf = &hw->conf;
1437 wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
1442 /* Current channel */
1443 survey->channel = conf->chandef.chan;
1446 * Magically conjured noise level --- this is only ok for simulated hardware.
1448 * A real driver which cannot determine the real channel noise MUST NOT
1449 * report any noise, especially not a magically conjured one :-)
1451 survey->filled = SURVEY_INFO_NOISE_DBM;
1452 survey->noise = -92;
1457 #ifdef CONFIG_NL80211_TESTMODE
1459 * This section contains example code for using netlink
1460 * attributes with the testmode command in nl80211.
1463 /* These enums need to be kept in sync with userspace */
1464 enum hwsim_testmode_attr {
1465 __HWSIM_TM_ATTR_INVALID = 0,
1466 HWSIM_TM_ATTR_CMD = 1,
1467 HWSIM_TM_ATTR_PS = 2,
1470 __HWSIM_TM_ATTR_AFTER_LAST,
1471 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1474 enum hwsim_testmode_cmd {
1475 HWSIM_TM_CMD_SET_PS = 0,
1476 HWSIM_TM_CMD_GET_PS = 1,
1477 HWSIM_TM_CMD_STOP_QUEUES = 2,
1478 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1481 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1482 [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
1483 [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
1486 static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
1487 struct ieee80211_vif *vif,
1488 void *data, int len)
1490 struct mac80211_hwsim_data *hwsim = hw->priv;
1491 struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
1492 struct sk_buff *skb;
1495 err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1496 hwsim_testmode_policy);
1500 if (!tb[HWSIM_TM_ATTR_CMD])
1503 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1504 case HWSIM_TM_CMD_SET_PS:
1505 if (!tb[HWSIM_TM_ATTR_PS])
1507 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1508 return hwsim_fops_ps_write(hwsim, ps);
1509 case HWSIM_TM_CMD_GET_PS:
1510 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
1511 nla_total_size(sizeof(u32)));
1514 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
1515 goto nla_put_failure;
1516 return cfg80211_testmode_reply(skb);
1517 case HWSIM_TM_CMD_STOP_QUEUES:
1518 ieee80211_stop_queues(hw);
1520 case HWSIM_TM_CMD_WAKE_QUEUES:
1521 ieee80211_wake_queues(hw);
1533 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
1534 struct ieee80211_vif *vif,
1535 enum ieee80211_ampdu_mlme_action action,
1536 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
1540 case IEEE80211_AMPDU_TX_START:
1541 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1543 case IEEE80211_AMPDU_TX_STOP_CONT:
1544 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1545 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1546 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1548 case IEEE80211_AMPDU_TX_OPERATIONAL:
1550 case IEEE80211_AMPDU_RX_START:
1551 case IEEE80211_AMPDU_RX_STOP:
1560 static void mac80211_hwsim_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1562 /* Not implemented, queues only on kernel side */
1565 static void hw_scan_work(struct work_struct *work)
1567 struct mac80211_hwsim_data *hwsim =
1568 container_of(work, struct mac80211_hwsim_data, hw_scan.work);
1569 struct cfg80211_scan_request *req = hwsim->hw_scan_request;
1572 mutex_lock(&hwsim->mutex);
1573 if (hwsim->scan_chan_idx >= req->n_channels) {
1574 wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
1575 ieee80211_scan_completed(hwsim->hw, false);
1576 hwsim->hw_scan_request = NULL;
1577 hwsim->hw_scan_vif = NULL;
1578 hwsim->tmp_chan = NULL;
1579 mutex_unlock(&hwsim->mutex);
1583 wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
1584 req->channels[hwsim->scan_chan_idx]->center_freq);
1586 hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
1587 if (hwsim->tmp_chan->flags & IEEE80211_CHAN_NO_IR ||
1593 for (i = 0; i < req->n_ssids; i++) {
1594 struct sk_buff *probe;
1596 probe = ieee80211_probereq_get(hwsim->hw,
1599 req->ssids[i].ssid_len,
1605 memcpy(skb_put(probe, req->ie_len), req->ie,
1609 mac80211_hwsim_tx_frame(hwsim->hw, probe,
1614 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
1615 msecs_to_jiffies(dwell));
1616 hwsim->scan_chan_idx++;
1617 mutex_unlock(&hwsim->mutex);
1620 static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
1621 struct ieee80211_vif *vif,
1622 struct cfg80211_scan_request *req)
1624 struct mac80211_hwsim_data *hwsim = hw->priv;
1626 mutex_lock(&hwsim->mutex);
1627 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
1628 mutex_unlock(&hwsim->mutex);
1631 hwsim->hw_scan_request = req;
1632 hwsim->hw_scan_vif = vif;
1633 hwsim->scan_chan_idx = 0;
1634 mutex_unlock(&hwsim->mutex);
1636 wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
1638 ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
1643 static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
1644 struct ieee80211_vif *vif)
1646 struct mac80211_hwsim_data *hwsim = hw->priv;
1648 wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
1650 cancel_delayed_work_sync(&hwsim->hw_scan);
1652 mutex_lock(&hwsim->mutex);
1653 ieee80211_scan_completed(hwsim->hw, true);
1654 hwsim->tmp_chan = NULL;
1655 hwsim->hw_scan_request = NULL;
1656 hwsim->hw_scan_vif = NULL;
1657 mutex_unlock(&hwsim->mutex);
1660 static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
1662 struct mac80211_hwsim_data *hwsim = hw->priv;
1664 mutex_lock(&hwsim->mutex);
1666 if (hwsim->scanning) {
1667 printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
1671 printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
1672 hwsim->scanning = true;
1675 mutex_unlock(&hwsim->mutex);
1678 static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
1680 struct mac80211_hwsim_data *hwsim = hw->priv;
1682 mutex_lock(&hwsim->mutex);
1684 printk(KERN_DEBUG "hwsim sw_scan_complete\n");
1685 hwsim->scanning = false;
1687 mutex_unlock(&hwsim->mutex);
1690 static void hw_roc_done(struct work_struct *work)
1692 struct mac80211_hwsim_data *hwsim =
1693 container_of(work, struct mac80211_hwsim_data, roc_done.work);
1695 mutex_lock(&hwsim->mutex);
1696 ieee80211_remain_on_channel_expired(hwsim->hw);
1697 hwsim->tmp_chan = NULL;
1698 mutex_unlock(&hwsim->mutex);
1700 wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
1703 static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
1704 struct ieee80211_vif *vif,
1705 struct ieee80211_channel *chan,
1707 enum ieee80211_roc_type type)
1709 struct mac80211_hwsim_data *hwsim = hw->priv;
1711 mutex_lock(&hwsim->mutex);
1712 if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
1713 mutex_unlock(&hwsim->mutex);
1717 hwsim->tmp_chan = chan;
1718 mutex_unlock(&hwsim->mutex);
1720 wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
1721 chan->center_freq, duration);
1723 ieee80211_ready_on_channel(hw);
1725 ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
1726 msecs_to_jiffies(duration));
1730 static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
1732 struct mac80211_hwsim_data *hwsim = hw->priv;
1734 cancel_delayed_work_sync(&hwsim->roc_done);
1736 mutex_lock(&hwsim->mutex);
1737 hwsim->tmp_chan = NULL;
1738 mutex_unlock(&hwsim->mutex);
1740 wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
1745 static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
1746 struct ieee80211_chanctx_conf *ctx)
1748 hwsim_set_chanctx_magic(ctx);
1749 wiphy_debug(hw->wiphy,
1750 "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1751 ctx->def.chan->center_freq, ctx->def.width,
1752 ctx->def.center_freq1, ctx->def.center_freq2);
1756 static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
1757 struct ieee80211_chanctx_conf *ctx)
1759 wiphy_debug(hw->wiphy,
1760 "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1761 ctx->def.chan->center_freq, ctx->def.width,
1762 ctx->def.center_freq1, ctx->def.center_freq2);
1763 hwsim_check_chanctx_magic(ctx);
1764 hwsim_clear_chanctx_magic(ctx);
1767 static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
1768 struct ieee80211_chanctx_conf *ctx,
1771 hwsim_check_chanctx_magic(ctx);
1772 wiphy_debug(hw->wiphy,
1773 "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
1774 ctx->def.chan->center_freq, ctx->def.width,
1775 ctx->def.center_freq1, ctx->def.center_freq2);
1778 static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
1779 struct ieee80211_vif *vif,
1780 struct ieee80211_chanctx_conf *ctx)
1782 hwsim_check_magic(vif);
1783 hwsim_check_chanctx_magic(ctx);
1788 static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
1789 struct ieee80211_vif *vif,
1790 struct ieee80211_chanctx_conf *ctx)
1792 hwsim_check_magic(vif);
1793 hwsim_check_chanctx_magic(ctx);
1796 static const struct ieee80211_ops mac80211_hwsim_ops = {
1797 .tx = mac80211_hwsim_tx,
1798 .start = mac80211_hwsim_start,
1799 .stop = mac80211_hwsim_stop,
1800 .add_interface = mac80211_hwsim_add_interface,
1801 .change_interface = mac80211_hwsim_change_interface,
1802 .remove_interface = mac80211_hwsim_remove_interface,
1803 .config = mac80211_hwsim_config,
1804 .configure_filter = mac80211_hwsim_configure_filter,
1805 .bss_info_changed = mac80211_hwsim_bss_info_changed,
1806 .sta_add = mac80211_hwsim_sta_add,
1807 .sta_remove = mac80211_hwsim_sta_remove,
1808 .sta_notify = mac80211_hwsim_sta_notify,
1809 .set_tim = mac80211_hwsim_set_tim,
1810 .conf_tx = mac80211_hwsim_conf_tx,
1811 .get_survey = mac80211_hwsim_get_survey,
1812 CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
1813 .ampdu_action = mac80211_hwsim_ampdu_action,
1814 .sw_scan_start = mac80211_hwsim_sw_scan,
1815 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
1816 .flush = mac80211_hwsim_flush,
1817 .get_tsf = mac80211_hwsim_get_tsf,
1818 .set_tsf = mac80211_hwsim_set_tsf,
1821 static struct ieee80211_ops mac80211_hwsim_mchan_ops;
1823 static int mac80211_hwsim_create_radio(int channels)
1827 struct mac80211_hwsim_data *data;
1828 struct ieee80211_hw *hw;
1829 enum ieee80211_band band;
1830 const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
1833 spin_lock_bh(&hwsim_radio_lock);
1834 idx = hwsim_radio_idx++;
1835 spin_unlock_bh(&hwsim_radio_lock);
1838 ops = &mac80211_hwsim_mchan_ops;
1839 hw = ieee80211_alloc_hw(sizeof(*data), ops);
1841 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw failed\n");
1848 data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
1849 if (IS_ERR(data->dev)) {
1851 "mac80211_hwsim: device_create failed (%ld)\n",
1852 PTR_ERR(data->dev));
1854 goto failed_drvdata;
1856 data->dev->driver = &mac80211_hwsim_driver.driver;
1857 err = device_bind_driver(data->dev);
1859 printk(KERN_DEBUG "mac80211_hwsim: device_bind_driver failed (%d)\n",
1864 skb_queue_head_init(&data->pending);
1866 SET_IEEE80211_DEV(hw, data->dev);
1867 memset(addr, 0, ETH_ALEN);
1871 memcpy(data->addresses[0].addr, addr, ETH_ALEN);
1872 memcpy(data->addresses[1].addr, addr, ETH_ALEN);
1873 data->addresses[1].addr[0] |= 0x40;
1874 hw->wiphy->n_addresses = 2;
1875 hw->wiphy->addresses = data->addresses;
1877 data->channels = channels;
1880 if (data->channels > 1) {
1881 hw->wiphy->max_scan_ssids = 255;
1882 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
1883 hw->wiphy->max_remain_on_channel_duration = 1000;
1884 /* For channels > 1 DFS is not allowed */
1885 hw->wiphy->n_iface_combinations = 1;
1886 hw->wiphy->iface_combinations = &data->if_combination;
1887 data->if_combination = hwsim_if_comb[0];
1888 data->if_combination.num_different_channels = data->channels;
1890 hw->wiphy->iface_combinations = hwsim_if_comb;
1891 hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb);
1894 INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
1895 INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
1898 hw->offchannel_tx_hw_queue = 4;
1899 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1900 BIT(NL80211_IFTYPE_AP) |
1901 BIT(NL80211_IFTYPE_P2P_CLIENT) |
1902 BIT(NL80211_IFTYPE_P2P_GO) |
1903 BIT(NL80211_IFTYPE_ADHOC) |
1904 BIT(NL80211_IFTYPE_MESH_POINT) |
1905 BIT(NL80211_IFTYPE_P2P_DEVICE);
1907 hw->flags = IEEE80211_HW_MFP_CAPABLE |
1908 IEEE80211_HW_SIGNAL_DBM |
1909 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
1910 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
1911 IEEE80211_HW_AMPDU_AGGREGATION |
1912 IEEE80211_HW_WANT_MONITOR_VIF |
1913 IEEE80211_HW_QUEUE_CONTROL |
1914 IEEE80211_HW_SUPPORTS_HT_CCK_RATES;
1916 hw->flags |= IEEE80211_HW_SUPPORTS_RC_TABLE;
1918 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
1919 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
1920 WIPHY_FLAG_AP_UAPSD;
1921 hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
1923 /* ask mac80211 to reserve space for magic */
1924 hw->vif_data_size = sizeof(struct hwsim_vif_priv);
1925 hw->sta_data_size = sizeof(struct hwsim_sta_priv);
1926 hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
1928 memcpy(data->channels_2ghz, hwsim_channels_2ghz,
1929 sizeof(hwsim_channels_2ghz));
1930 memcpy(data->channels_5ghz, hwsim_channels_5ghz,
1931 sizeof(hwsim_channels_5ghz));
1932 memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
1934 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
1935 struct ieee80211_supported_band *sband = &data->bands[band];
1937 case IEEE80211_BAND_2GHZ:
1938 sband->channels = data->channels_2ghz;
1939 sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
1940 sband->bitrates = data->rates;
1941 sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
1943 case IEEE80211_BAND_5GHZ:
1944 sband->channels = data->channels_5ghz;
1945 sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
1946 sband->bitrates = data->rates + 4;
1947 sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
1953 sband->ht_cap.ht_supported = true;
1954 sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
1955 IEEE80211_HT_CAP_GRN_FLD |
1956 IEEE80211_HT_CAP_SGI_40 |
1957 IEEE80211_HT_CAP_DSSSCCK40;
1958 sband->ht_cap.ampdu_factor = 0x3;
1959 sband->ht_cap.ampdu_density = 0x6;
1960 memset(&sband->ht_cap.mcs, 0,
1961 sizeof(sband->ht_cap.mcs));
1962 sband->ht_cap.mcs.rx_mask[0] = 0xff;
1963 sband->ht_cap.mcs.rx_mask[1] = 0xff;
1964 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
1966 hw->wiphy->bands[band] = sband;
1968 sband->vht_cap.vht_supported = true;
1969 sband->vht_cap.cap =
1970 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
1971 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
1972 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
1973 IEEE80211_VHT_CAP_RXLDPC |
1974 IEEE80211_VHT_CAP_SHORT_GI_80 |
1975 IEEE80211_VHT_CAP_SHORT_GI_160 |
1976 IEEE80211_VHT_CAP_TXSTBC |
1977 IEEE80211_VHT_CAP_RXSTBC_1 |
1978 IEEE80211_VHT_CAP_RXSTBC_2 |
1979 IEEE80211_VHT_CAP_RXSTBC_3 |
1980 IEEE80211_VHT_CAP_RXSTBC_4 |
1981 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
1982 sband->vht_cap.vht_mcs.rx_mcs_map =
1983 cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
1984 IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
1985 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
1986 IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
1987 IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
1988 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
1989 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
1990 IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
1991 sband->vht_cap.vht_mcs.tx_mcs_map =
1992 sband->vht_cap.vht_mcs.rx_mcs_map;
1995 /* By default all radios belong to the first group */
1997 mutex_init(&data->mutex);
1999 /* Enable frame retransmissions for lossy channels */
2001 hw->max_rate_tries = 11;
2003 err = ieee80211_register_hw(hw);
2005 printk(KERN_DEBUG "mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
2010 wiphy_debug(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
2012 data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
2013 debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
2014 debugfs_create_file("group", 0666, data->debugfs, data,
2016 if (data->channels == 1)
2017 debugfs_create_file("dfs_simulate_radar", 0222,
2019 data, &hwsim_simulate_radar);
2021 tasklet_hrtimer_init(&data->beacon_timer,
2022 mac80211_hwsim_beacon,
2023 CLOCK_MONOTONIC_RAW, HRTIMER_MODE_ABS);
2025 spin_lock_bh(&hwsim_radio_lock);
2026 list_add_tail(&data->list, &hwsim_radios);
2027 spin_unlock_bh(&hwsim_radio_lock);
2032 device_unregister(data->dev);
2034 ieee80211_free_hw(hw);
2039 static void mac80211_hwsim_destroy_radio(struct mac80211_hwsim_data *data)
2041 debugfs_remove_recursive(data->debugfs);
2042 ieee80211_unregister_hw(data->hw);
2043 device_release_driver(data->dev);
2044 device_unregister(data->dev);
2045 ieee80211_free_hw(data->hw);
2048 static void mac80211_hwsim_free(void)
2050 struct mac80211_hwsim_data *data;
2052 spin_lock_bh(&hwsim_radio_lock);
2053 while ((data = list_first_entry_or_null(&hwsim_radios,
2054 struct mac80211_hwsim_data,
2056 list_del(&data->list);
2057 spin_unlock_bh(&hwsim_radio_lock);
2058 mac80211_hwsim_destroy_radio(data);
2059 spin_lock_bh(&hwsim_radio_lock);
2061 spin_unlock_bh(&hwsim_radio_lock);
2062 class_destroy(hwsim_class);
2065 static const struct net_device_ops hwsim_netdev_ops = {
2066 .ndo_start_xmit = hwsim_mon_xmit,
2067 .ndo_change_mtu = eth_change_mtu,
2068 .ndo_set_mac_address = eth_mac_addr,
2069 .ndo_validate_addr = eth_validate_addr,
2072 static void hwsim_mon_setup(struct net_device *dev)
2074 dev->netdev_ops = &hwsim_netdev_ops;
2075 dev->destructor = free_netdev;
2077 dev->tx_queue_len = 0;
2078 dev->type = ARPHRD_IEEE80211_RADIOTAP;
2079 memset(dev->dev_addr, 0, ETH_ALEN);
2080 dev->dev_addr[0] = 0x12;
2083 static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
2085 struct mac80211_hwsim_data *data;
2086 bool _found = false;
2088 spin_lock_bh(&hwsim_radio_lock);
2089 list_for_each_entry(data, &hwsim_radios, list) {
2090 if (memcmp(data->addresses[1].addr, addr, ETH_ALEN) == 0) {
2095 spin_unlock_bh(&hwsim_radio_lock);
2103 static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
2104 struct genl_info *info)
2107 struct ieee80211_hdr *hdr;
2108 struct mac80211_hwsim_data *data2;
2109 struct ieee80211_tx_info *txi;
2110 struct hwsim_tx_rate *tx_attempts;
2111 unsigned long ret_skb_ptr;
2112 struct sk_buff *skb, *tmp;
2114 unsigned int hwsim_flags;
2118 if (info->snd_portid != wmediumd_portid)
2121 if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
2122 !info->attrs[HWSIM_ATTR_FLAGS] ||
2123 !info->attrs[HWSIM_ATTR_COOKIE] ||
2124 !info->attrs[HWSIM_ATTR_TX_INFO])
2127 src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
2128 hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
2129 ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
2131 data2 = get_hwsim_data_ref_from_addr(src);
2135 /* look for the skb matching the cookie passed back from user */
2136 skb_queue_walk_safe(&data2->pending, skb, tmp) {
2137 if ((unsigned long)skb == ret_skb_ptr) {
2138 skb_unlink(skb, &data2->pending);
2148 /* Tx info received because the frame was broadcasted on user space,
2149 so we get all the necessary info: tx attempts and skb control buff */
2151 tx_attempts = (struct hwsim_tx_rate *)nla_data(
2152 info->attrs[HWSIM_ATTR_TX_INFO]);
2154 /* now send back TX status */
2155 txi = IEEE80211_SKB_CB(skb);
2157 ieee80211_tx_info_clear_status(txi);
2159 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2160 txi->status.rates[i].idx = tx_attempts[i].idx;
2161 txi->status.rates[i].count = tx_attempts[i].count;
2162 /*txi->status.rates[i].flags = 0;*/
2165 txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2167 if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
2168 (hwsim_flags & HWSIM_TX_STAT_ACK)) {
2169 if (skb->len >= 16) {
2170 hdr = (struct ieee80211_hdr *) skb->data;
2171 mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
2174 txi->flags |= IEEE80211_TX_STAT_ACK;
2176 ieee80211_tx_status_irqsafe(data2->hw, skb);
2183 static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
2184 struct genl_info *info)
2187 struct mac80211_hwsim_data *data2;
2188 struct ieee80211_rx_status rx_status;
2192 struct sk_buff *skb = NULL;
2194 if (info->snd_portid != wmediumd_portid)
2197 if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
2198 !info->attrs[HWSIM_ATTR_FRAME] ||
2199 !info->attrs[HWSIM_ATTR_RX_RATE] ||
2200 !info->attrs[HWSIM_ATTR_SIGNAL])
2203 dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
2204 frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
2205 frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
2207 /* Allocate new skb here */
2208 skb = alloc_skb(frame_data_len, GFP_KERNEL);
2212 if (frame_data_len > IEEE80211_MAX_DATA_LEN)
2216 memcpy(skb_put(skb, frame_data_len), frame_data, frame_data_len);
2218 data2 = get_hwsim_data_ref_from_addr(dst);
2222 /* check if radio is configured properly */
2224 if (data2->idle || !data2->started)
2227 /* A frame is received from user space */
2228 memset(&rx_status, 0, sizeof(rx_status));
2229 rx_status.freq = data2->channel->center_freq;
2230 rx_status.band = data2->channel->band;
2231 rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
2232 rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
2234 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
2235 ieee80211_rx_irqsafe(data2->hw, skb);
2239 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
2246 static int hwsim_register_received_nl(struct sk_buff *skb_2,
2247 struct genl_info *info)
2249 struct mac80211_hwsim_data *data;
2252 spin_lock_bh(&hwsim_radio_lock);
2253 list_for_each_entry(data, &hwsim_radios, list)
2254 chans = max(chans, data->channels);
2255 spin_unlock_bh(&hwsim_radio_lock);
2257 /* In the future we should revise the userspace API and allow it
2258 * to set a flag that it does support multi-channel, then we can
2259 * let this pass conditionally on the flag.
2260 * For current userspace, prohibit it since it won't work right.
2265 if (wmediumd_portid)
2268 wmediumd_portid = info->snd_portid;
2270 printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
2271 "switching to wmediumd mode with pid %d\n", info->snd_portid);
2276 static int hwsim_create_radio_nl(struct sk_buff *msg, struct genl_info *info)
2278 unsigned int chans = channels;
2280 if (info->attrs[HWSIM_ATTR_CHANNELS])
2281 chans = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
2283 return mac80211_hwsim_create_radio(chans);
2286 static int hwsim_destroy_radio_nl(struct sk_buff *msg, struct genl_info *info)
2288 struct mac80211_hwsim_data *data;
2291 if (!info->attrs[HWSIM_ATTR_RADIO_ID])
2293 idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
2295 spin_lock_bh(&hwsim_radio_lock);
2296 list_for_each_entry(data, &hwsim_radios, list) {
2297 if (data->idx != idx)
2299 list_del(&data->list);
2300 spin_unlock_bh(&hwsim_radio_lock);
2301 mac80211_hwsim_destroy_radio(data);
2304 spin_unlock_bh(&hwsim_radio_lock);
2309 /* Generic Netlink operations array */
2310 static const struct genl_ops hwsim_ops[] = {
2312 .cmd = HWSIM_CMD_REGISTER,
2313 .policy = hwsim_genl_policy,
2314 .doit = hwsim_register_received_nl,
2315 .flags = GENL_ADMIN_PERM,
2318 .cmd = HWSIM_CMD_FRAME,
2319 .policy = hwsim_genl_policy,
2320 .doit = hwsim_cloned_frame_received_nl,
2323 .cmd = HWSIM_CMD_TX_INFO_FRAME,
2324 .policy = hwsim_genl_policy,
2325 .doit = hwsim_tx_info_frame_received_nl,
2328 .cmd = HWSIM_CMD_CREATE_RADIO,
2329 .policy = hwsim_genl_policy,
2330 .doit = hwsim_create_radio_nl,
2331 .flags = GENL_ADMIN_PERM,
2334 .cmd = HWSIM_CMD_DESTROY_RADIO,
2335 .policy = hwsim_genl_policy,
2336 .doit = hwsim_destroy_radio_nl,
2337 .flags = GENL_ADMIN_PERM,
2341 static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
2342 unsigned long state,
2345 struct netlink_notify *notify = _notify;
2347 if (state != NETLINK_URELEASE)
2350 if (notify->portid == wmediumd_portid) {
2351 printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
2352 " socket, switching to perfect channel medium\n");
2353 wmediumd_portid = 0;
2359 static struct notifier_block hwsim_netlink_notifier = {
2360 .notifier_call = mac80211_hwsim_netlink_notify,
2363 static int hwsim_init_netlink(void)
2367 printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
2369 rc = genl_register_family_with_ops(&hwsim_genl_family, hwsim_ops);
2373 rc = netlink_register_notifier(&hwsim_netlink_notifier);
2380 printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
2384 static void hwsim_exit_netlink(void)
2386 /* unregister the notifier */
2387 netlink_unregister_notifier(&hwsim_netlink_notifier);
2388 /* unregister the family */
2389 genl_unregister_family(&hwsim_genl_family);
2392 static int __init init_mac80211_hwsim(void)
2396 if (radios < 0 || radios > 100)
2402 mac80211_hwsim_mchan_ops = mac80211_hwsim_ops;
2403 mac80211_hwsim_mchan_ops.hw_scan = mac80211_hwsim_hw_scan;
2404 mac80211_hwsim_mchan_ops.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan;
2405 mac80211_hwsim_mchan_ops.sw_scan_start = NULL;
2406 mac80211_hwsim_mchan_ops.sw_scan_complete = NULL;
2407 mac80211_hwsim_mchan_ops.remain_on_channel = mac80211_hwsim_roc;
2408 mac80211_hwsim_mchan_ops.cancel_remain_on_channel = mac80211_hwsim_croc;
2409 mac80211_hwsim_mchan_ops.add_chanctx = mac80211_hwsim_add_chanctx;
2410 mac80211_hwsim_mchan_ops.remove_chanctx = mac80211_hwsim_remove_chanctx;
2411 mac80211_hwsim_mchan_ops.change_chanctx = mac80211_hwsim_change_chanctx;
2412 mac80211_hwsim_mchan_ops.assign_vif_chanctx =
2413 mac80211_hwsim_assign_vif_chanctx;
2414 mac80211_hwsim_mchan_ops.unassign_vif_chanctx =
2415 mac80211_hwsim_unassign_vif_chanctx;
2417 spin_lock_init(&hwsim_radio_lock);
2418 INIT_LIST_HEAD(&hwsim_radios);
2420 err = platform_driver_register(&mac80211_hwsim_driver);
2424 hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
2425 if (IS_ERR(hwsim_class)) {
2426 err = PTR_ERR(hwsim_class);
2427 goto out_unregister_driver;
2430 for (i = 0; i < radios; i++) {
2431 err = mac80211_hwsim_create_radio(channels);
2433 goto out_free_radios;
2436 hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
2437 if (hwsim_mon == NULL) {
2439 goto out_free_radios;
2443 err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
2446 goto out_free_radios;
2449 err = register_netdevice(hwsim_mon);
2456 err = hwsim_init_netlink();
2463 free_netdev(hwsim_mon);
2465 mac80211_hwsim_free();
2466 out_unregister_driver:
2467 platform_driver_unregister(&mac80211_hwsim_driver);
2470 module_init(init_mac80211_hwsim);
2472 static void __exit exit_mac80211_hwsim(void)
2474 printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
2476 hwsim_exit_netlink();
2478 mac80211_hwsim_free();
2479 unregister_netdev(hwsim_mon);
2480 platform_driver_unregister(&mac80211_hwsim_driver);
2482 module_exit(exit_mac80211_hwsim);