18 #include <linux/list.h>
19 #include <linux/slab.h>
25 #include <linux/if_arp.h>
26 #include <linux/rtnetlink.h>
29 #include <linux/module.h>
34 #define WARN_QUEUE 100
41 static u32 wmediumd_portid;
43 static int radios = 2;
47 static bool fake_hw_scan;
127 static const char *hwsim_alpha2s[] = {
140 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
141 REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
142 REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
143 REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
151 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
152 REG_RULE(5725-10, 5850+10, 40, 0, 30,
164 #define HWSIM_VIF_MAGIC 0x69537748
166 static inline void hwsim_check_magic(
struct ieee80211_vif *vif)
172 static inline void hwsim_set_magic(
struct ieee80211_vif *vif)
178 static inline void hwsim_clear_magic(
struct ieee80211_vif *vif)
188 #define HWSIM_STA_MAGIC 0x6d537748
190 static inline void hwsim_check_sta_magic(
struct ieee80211_sta *sta)
196 static inline void hwsim_set_sta_magic(
struct ieee80211_sta *sta)
202 static inline void hwsim_clear_sta_magic(
struct ieee80211_sta *sta)
208 static struct class *hwsim_class;
212 #define CHAN2G(_freq) { \
213 .band = IEEE80211_BAND_2GHZ, \
214 .center_freq = (_freq), \
215 .hw_value = (_freq), \
219 #define CHAN5G(_freq) { \
220 .band = IEEE80211_BAND_5GHZ, \
221 .center_freq = (_freq), \
222 .hw_value = (_freq), \
344 .name =
"MAC80211_HWSIM",
353 .len = 6*
sizeof(
u8) },
355 .len = 6*
sizeof(
u8) },
386 return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
389 static void mac80211_hwsim_set_tsf(
struct ieee80211_hw *hw,
398 static void mac80211_hwsim_monitor_rx(
struct ieee80211_hw *hw,
408 if (!netif_running(hwsim_mon))
423 hdr->
rt_tsft = __mac80211_hwsim_get_tsf(data);
434 skb->
dev = hwsim_mon;
435 skb_set_mac_header(skb, 0);
439 memset(skb->cb, 0,
sizeof(skb->cb));
452 if (!netif_running(hwsim_mon))
455 skb = dev_alloc_skb(100);
477 skb->
dev = hwsim_mon;
478 skb_set_mac_header(skb, 0);
482 memset(skb->cb, 0,
sizeof(skb->cb));
520 static void mac80211_hwsim_addr_iter(
void *data,
u8 *
mac,
540 mac80211_hwsim_addr_iter,
546 static void mac80211_hwsim_tx_frame_nl(
struct ieee80211_hw *hw,
555 unsigned int hwsim_flags = 0;
561 dev_kfree_skb(my_skb);
565 if (data->
ps != PS_DISABLED)
576 goto nla_put_failure;
578 msg_head =
genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
580 if (msg_head ==
NULL) {
582 goto nla_put_failure;
587 goto nla_put_failure;
591 goto nla_put_failure;
603 goto nla_put_failure;
608 tx_attempts[
i].idx = info->
status.rates[
i].idx;
609 tx_attempts[
i].count = info->
status.rates[
i].count;
615 goto nla_put_failure;
619 goto nla_put_failure;
621 genlmsg_end(skb, msg_head);
622 genlmsg_unicast(&
init_net, skb, dst_portid);
632 static bool mac80211_hwsim_tx_frame_no_nl(
struct ieee80211_hw *hw,
661 if (data->
ps != PS_DISABLED)
672 spin_lock(&hwsim_radio_lock);
681 !hwsim_ps_rx_ok(
data2, skb) || !
data2->channel ||
682 data->
channel->center_freq !=
data2->channel->center_freq ||
690 if (mac80211_hwsim_addr_match(
data2, hdr->
addr1))
702 24 * 8 * 10 / txrate->
bitrate);
707 spin_unlock(&hwsim_radio_lock);
720 mac80211_hwsim_monitor_rx(hw, skb);
732 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
735 ack = mac80211_hwsim_tx_frame_no_nl(hw, skb);
737 if (ack && skb->
len >= 16) {
739 mac80211_hwsim_monitor_ack(hw, hdr->
addr2);
742 txi = IEEE80211_SKB_CB(skb);
744 ieee80211_tx_info_clear_status(txi);
747 txi->
control.rates[0].count = 1;
748 txi->
control.rates[1].idx = -1;
756 static int mac80211_hwsim_start(
struct ieee80211_hw *hw)
765 static void mac80211_hwsim_stop(
struct ieee80211_hw *hw)
774 static int mac80211_hwsim_add_interface(
struct ieee80211_hw *hw,
778 __func__, ieee80211_vif_type_p2p(vif),
780 hwsim_set_magic(vif);
785 static int mac80211_hwsim_change_interface(
struct ieee80211_hw *hw,
790 newtype = ieee80211_iftype_p2p(newtype, newp2p);
792 "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
793 __func__, ieee80211_vif_type_p2p(vif),
795 hwsim_check_magic(vif);
800 static void mac80211_hwsim_remove_interface(
804 __func__, ieee80211_vif_type_p2p(vif),
806 hwsim_check_magic(vif);
807 hwsim_clear_magic(vif);
811 static void mac80211_hwsim_beacon_tx(
void *
arg,
u8 *
mac,
819 hwsim_check_magic(vif);
826 skb = ieee80211_beacon_get(hw, vif);
829 info = IEEE80211_SKB_CB(skb);
831 mac80211_hwsim_monitor_rx(hw, skb);
837 return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
839 mac80211_hwsim_tx_frame_no_nl(hw, skb);
844 static void mac80211_hwsim_beacon(
unsigned long arg)
853 hw, mac80211_hwsim_beacon_tx, hw);
859 static const char *hwsim_chantypes[] = {
878 "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
899 static void mac80211_hwsim_configure_filter(
struct ieee80211_hw *hw,
900 unsigned int changed_flags,
901 unsigned int *total_flags,
u64 multicast)
916 static void mac80211_hwsim_bss_info_changed(
struct ieee80211_hw *hw,
924 hwsim_check_magic(vif);
930 __func__, info->
bssid);
977 static int mac80211_hwsim_sta_add(
struct ieee80211_hw *hw,
981 hwsim_check_magic(vif);
982 hwsim_set_sta_magic(sta);
987 static int mac80211_hwsim_sta_remove(
struct ieee80211_hw *hw,
991 hwsim_check_magic(vif);
992 hwsim_clear_sta_magic(sta);
997 static void mac80211_hwsim_sta_notify(
struct ieee80211_hw *hw,
1002 hwsim_check_magic(vif);
1010 WARN(1,
"Invalid sta notify: %d\n", cmd);
1015 static int mac80211_hwsim_set_tim(
struct ieee80211_hw *hw,
1019 hwsim_check_sta_magic(sta);
1023 static int mac80211_hwsim_conf_tx(
1029 "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
1036 static int mac80211_hwsim_get_survey(
1057 survey->
noise = -92;
1062 #ifdef CONFIG_NL80211_TESTMODE
1069 enum hwsim_testmode_attr {
1070 __HWSIM_TM_ATTR_INVALID = 0,
1071 HWSIM_TM_ATTR_CMD = 1,
1072 HWSIM_TM_ATTR_PS = 2,
1075 __HWSIM_TM_ATTR_AFTER_LAST,
1076 HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
1079 enum hwsim_testmode_cmd {
1080 HWSIM_TM_CMD_SET_PS = 0,
1081 HWSIM_TM_CMD_GET_PS = 1,
1082 HWSIM_TM_CMD_STOP_QUEUES = 2,
1083 HWSIM_TM_CMD_WAKE_QUEUES = 3,
1086 static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
1088 [HWSIM_TM_ATTR_PS] = { .type =
NLA_U32 },
1091 static int hwsim_fops_ps_write(
void *
dat,
u64 val);
1093 static int mac80211_hwsim_testmode_cmd(
struct ieee80211_hw *hw,
1094 void *data,
int len)
1097 struct nlattr *
tb[HWSIM_TM_ATTR_MAX + 1];
1101 err =
nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
1102 hwsim_testmode_policy);
1106 if (!tb[HWSIM_TM_ATTR_CMD])
1109 switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
1110 case HWSIM_TM_CMD_SET_PS:
1111 if (!tb[HWSIM_TM_ATTR_PS])
1113 ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
1114 return hwsim_fops_ps_write(hwsim, ps);
1115 case HWSIM_TM_CMD_GET_PS:
1116 skb = cfg80211_testmode_alloc_reply_skb(hw->
wiphy,
1117 nla_total_size(
sizeof(
u32)));
1120 if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->
ps))
1121 goto nla_put_failure;
1122 return cfg80211_testmode_reply(skb);
1123 case HWSIM_TM_CMD_STOP_QUEUES:
1126 case HWSIM_TM_CMD_WAKE_QUEUES:
1139 static int mac80211_hwsim_ampdu_action(
struct ieee80211_hw *hw,
1164 static void mac80211_hwsim_flush(
struct ieee80211_hw *hw,
bool drop)
1183 static int mac80211_hwsim_hw_scan(
struct ieee80211_hw *hw,
1208 static void mac80211_hwsim_sw_scan(
struct ieee80211_hw *hw)
1226 static void mac80211_hwsim_sw_scan_complete(
struct ieee80211_hw *hw)
1240 .tx = mac80211_hwsim_tx,
1241 .start = mac80211_hwsim_start,
1242 .stop = mac80211_hwsim_stop,
1243 .add_interface = mac80211_hwsim_add_interface,
1244 .change_interface = mac80211_hwsim_change_interface,
1245 .remove_interface = mac80211_hwsim_remove_interface,
1246 .config = mac80211_hwsim_config,
1247 .configure_filter = mac80211_hwsim_configure_filter,
1248 .bss_info_changed = mac80211_hwsim_bss_info_changed,
1249 .sta_add = mac80211_hwsim_sta_add,
1250 .sta_remove = mac80211_hwsim_sta_remove,
1251 .sta_notify = mac80211_hwsim_sta_notify,
1252 .set_tim = mac80211_hwsim_set_tim,
1253 .conf_tx = mac80211_hwsim_conf_tx,
1254 .get_survey = mac80211_hwsim_get_survey,
1256 .ampdu_action = mac80211_hwsim_ampdu_action,
1257 .sw_scan_start = mac80211_hwsim_sw_scan,
1258 .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
1259 .flush = mac80211_hwsim_flush,
1260 .get_tsf = mac80211_hwsim_get_tsf,
1261 .set_tsf = mac80211_hwsim_set_tsf,
1265 static void mac80211_hwsim_free(
void)
1270 INIT_LIST_HEAD(&tmplist);
1272 spin_lock_bh(&hwsim_radio_lock);
1274 list_move(i, &tmplist);
1275 spin_unlock_bh(&hwsim_radio_lock);
1290 .name =
"mac80211_hwsim"
1294 .ndo_start_xmit = hwsim_mon_xmit,
1324 "%s: send PS-Poll to %pM for aid %d\n",
1327 skb = dev_alloc_skb(
sizeof(*pspoll));
1330 pspoll = (
void *)
skb_put(skb,
sizeof(*pspoll));
1342 return mac80211_hwsim_tx_frame_nl(data->
hw, skb, _portid);
1344 if (!mac80211_hwsim_tx_frame_no_nl(data->
hw, skb))
1362 "%s: send data::nullfunc to %pM ps=%d\n",
1363 __func__, vp->
bssid, ps);
1365 skb = dev_alloc_skb(
sizeof(*hdr));
1381 return mac80211_hwsim_tx_frame_nl(data->
hw, skb, _portid);
1383 if (!mac80211_hwsim_tx_frame_no_nl(data->
hw, skb))
1389 static void hwsim_send_nullfunc_ps(
void *dat,
u8 *mac,
1393 hwsim_send_nullfunc(data, mac, vif, 1);
1397 static void hwsim_send_nullfunc_no_ps(
void *dat,
u8 *mac,
1401 hwsim_send_nullfunc(data, mac, vif, 0);
1405 static int hwsim_fops_ps_read(
void *dat,
u64 *
val)
1412 static int hwsim_fops_ps_write(
void *dat,
u64 val)
1426 hwsim_send_ps_poll, data);
1430 hwsim_send_nullfunc_ps,
1434 hwsim_send_nullfunc_no_ps,
1445 static int hwsim_fops_group_read(
void *dat,
u64 *val)
1452 static int hwsim_fops_group_write(
void *dat,
u64 val)
1460 hwsim_fops_group_read, hwsim_fops_group_write,
1467 bool _found =
false;
1469 spin_lock_bh(&hwsim_radio_lock);
1477 spin_unlock_bh(&hwsim_radio_lock);
1485 static int hwsim_tx_info_frame_received_nl(
struct sk_buff *skb_2,
1493 unsigned long ret_skb_ptr;
1496 unsigned int hwsim_flags;
1513 data2 = get_hwsim_data_ref_from_addr(src);
1519 skb_queue_walk_safe(&data2->
pending, skb, tmp) {
1520 if ((
unsigned long)skb == ret_skb_ptr) {
1538 txi = IEEE80211_SKB_CB(skb);
1540 ieee80211_tx_info_clear_status(txi);
1552 if (skb->
len >= 16) {
1554 mac80211_hwsim_monitor_ack(data2->
hw, hdr->
addr2);
1565 static int hwsim_cloned_frame_received_nl(
struct sk_buff *skb_2,
1600 data2 = get_hwsim_data_ref_from_addr(dst);
1629 static int hwsim_register_received_nl(
struct sk_buff *skb_2,
1638 "switching to wmediumd mode with pid %d\n", info->
snd_portid);
1647 static struct genl_ops hwsim_ops[] = {
1650 .policy = hwsim_genl_policy,
1651 .doit = hwsim_register_received_nl,
1656 .policy = hwsim_genl_policy,
1657 .doit = hwsim_cloned_frame_received_nl,
1661 .policy = hwsim_genl_policy,
1662 .doit = hwsim_tx_info_frame_received_nl,
1666 static int mac80211_hwsim_netlink_notify(
struct notifier_block *nb,
1667 unsigned long state,
1672 if (state != NETLINK_URELEASE)
1675 if (notify->
portid == wmediumd_portid) {
1677 " socket, switching to perfect channel medium\n");
1678 wmediumd_portid = 0;
1685 .notifier_call = mac80211_hwsim_netlink_notify,
1688 static int hwsim_init_netlink(
void)
1709 static void hwsim_exit_netlink(
void)
1720 "unregister family %i\n", ret);
1727 #ifdef CONFIG_MAC80211_MESH
1736 .limits = hwsim_if_limits,
1738 .max_interfaces = 2048,
1739 .num_different_channels = 1,
1742 static int __init init_mac80211_hwsim(
void)
1750 if (radios < 1 || radios > 100)
1754 mac80211_hwsim_ops.
hw_scan = mac80211_hwsim_hw_scan;
1760 INIT_LIST_HEAD(&hwsim_radios);
1763 if (IS_ERR(hwsim_class))
1764 return PTR_ERR(hwsim_class);
1769 for (i = 0; i < radios; i++) {
1784 if (IS_ERR(data->
dev)) {
1786 "mac80211_hwsim: device_create "
1787 "failed (%ld)\n", PTR_ERR(data->
dev));
1789 goto failed_drvdata;
1791 data->
dev->driver = &mac80211_hwsim_driver;
1792 skb_queue_head_init(&data->
pending);
1794 SET_IEEE80211_DEV(hw, data->
dev);
1800 hw->
wiphy->n_addresses = 2;
1803 hw->
wiphy->iface_combinations = &hwsim_if_comb;
1804 hw->
wiphy->n_iface_combinations = 1;
1807 hw->
wiphy->max_scan_ssids = 255;
1813 hw->
wiphy->interface_modes =
1837 sizeof(hwsim_channels_2ghz));
1839 sizeof(hwsim_channels_5ghz));
1840 memcpy(data->
rates, hwsim_rates,
sizeof(hwsim_rates));
1863 sband->
ht_cap.ht_supported =
true;
1868 sband->
ht_cap.ampdu_factor = 0x3;
1869 sband->
ht_cap.ampdu_density = 0x6;
1871 sizeof(sband->
ht_cap.mcs));
1872 sband->
ht_cap.mcs.rx_mask[0] = 0xff;
1873 sband->
ht_cap.mcs.rx_mask[1] = 0xff;
1901 &hwsim_world_regdom_custom_01);
1907 &hwsim_world_regdom_custom_01);
1913 &hwsim_world_regdom_custom_01);
1914 }
else if (i == 1) {
1917 &hwsim_world_regdom_custom_02);
1932 &hwsim_world_regdom_custom_01);
1933 }
else if (i == 1) {
1936 &hwsim_world_regdom_custom_02);
1950 "ieee80211_register_hw failed (%d)\n", err);
2001 hw->
wiphy->perm_addr);
2004 hw->
wiphy->debugfsdir);
2013 (
unsigned long) hw);
2018 hwsim_mon =
alloc_netdev(0,
"hwsim%d", hwsim_mon_setup);
2019 if (hwsim_mon ==
NULL)
2035 err = hwsim_init_netlink();
2048 mac80211_hwsim_free();
2056 mac80211_hwsim_free();
2061 static void __exit exit_mac80211_hwsim(
void)
2065 hwsim_exit_netlink();
2067 mac80211_hwsim_free();