Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 1 | /* |
| 2 | * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211 |
| 3 | * Copyright (c) 2008, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * TODO: |
| 12 | * - IBSS mode simulation (Beacon transmission with competition for "air time") |
| 13 | * - IEEE 802.11a and 802.11n modes |
| 14 | * - RX filtering based on filter configuration (data->rx_filter) |
| 15 | */ |
| 16 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 17 | #include <linux/list.h> |
| 18 | #include <linux/spinlock.h> |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 19 | #include <net/mac80211.h> |
| 20 | #include <net/ieee80211_radiotap.h> |
| 21 | #include <linux/if_arp.h> |
| 22 | #include <linux/rtnetlink.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | |
| 25 | MODULE_AUTHOR("Jouni Malinen"); |
| 26 | MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
| 29 | static int radios = 2; |
| 30 | module_param(radios, int, 0444); |
| 31 | MODULE_PARM_DESC(radios, "Number of simulated radios"); |
| 32 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 33 | struct hwsim_vif_priv { |
| 34 | u32 magic; |
| 35 | }; |
| 36 | |
| 37 | #define HWSIM_VIF_MAGIC 0x69537748 |
| 38 | |
| 39 | static inline void hwsim_check_magic(struct ieee80211_vif *vif) |
| 40 | { |
| 41 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 42 | WARN_ON(vp->magic != HWSIM_VIF_MAGIC); |
| 43 | } |
| 44 | |
| 45 | static inline void hwsim_set_magic(struct ieee80211_vif *vif) |
| 46 | { |
| 47 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 48 | vp->magic = HWSIM_VIF_MAGIC; |
| 49 | } |
| 50 | |
| 51 | static inline void hwsim_clear_magic(struct ieee80211_vif *vif) |
| 52 | { |
| 53 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 54 | vp->magic = 0; |
| 55 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 56 | |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 57 | struct hwsim_sta_priv { |
| 58 | u32 magic; |
| 59 | }; |
| 60 | |
| 61 | #define HWSIM_STA_MAGIC 0x6d537748 |
| 62 | |
| 63 | static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta) |
| 64 | { |
| 65 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
Rami Rosen | 5d6924e | 2008-10-08 11:18:27 +0200 | [diff] [blame] | 66 | WARN_ON(sp->magic != HWSIM_STA_MAGIC); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta) |
| 70 | { |
| 71 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
Rami Rosen | 5d6924e | 2008-10-08 11:18:27 +0200 | [diff] [blame] | 72 | sp->magic = HWSIM_STA_MAGIC; |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta) |
| 76 | { |
| 77 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
| 78 | sp->magic = 0; |
| 79 | } |
| 80 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 81 | static struct class *hwsim_class; |
| 82 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 83 | static struct net_device *hwsim_mon; /* global monitor netdev */ |
| 84 | |
| 85 | |
| 86 | static const struct ieee80211_channel hwsim_channels[] = { |
| 87 | { .center_freq = 2412 }, |
| 88 | { .center_freq = 2417 }, |
| 89 | { .center_freq = 2422 }, |
| 90 | { .center_freq = 2427 }, |
| 91 | { .center_freq = 2432 }, |
| 92 | { .center_freq = 2437 }, |
| 93 | { .center_freq = 2442 }, |
| 94 | { .center_freq = 2447 }, |
| 95 | { .center_freq = 2452 }, |
| 96 | { .center_freq = 2457 }, |
| 97 | { .center_freq = 2462 }, |
| 98 | { .center_freq = 2467 }, |
| 99 | { .center_freq = 2472 }, |
| 100 | { .center_freq = 2484 }, |
| 101 | }; |
| 102 | |
| 103 | static const struct ieee80211_rate hwsim_rates[] = { |
| 104 | { .bitrate = 10 }, |
| 105 | { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 106 | { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 107 | { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 108 | { .bitrate = 60 }, |
| 109 | { .bitrate = 90 }, |
| 110 | { .bitrate = 120 }, |
| 111 | { .bitrate = 180 }, |
| 112 | { .bitrate = 240 }, |
| 113 | { .bitrate = 360 }, |
| 114 | { .bitrate = 480 }, |
| 115 | { .bitrate = 540 } |
| 116 | }; |
| 117 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 118 | static spinlock_t hwsim_radio_lock; |
| 119 | static struct list_head hwsim_radios; |
| 120 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 121 | struct mac80211_hwsim_data { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 122 | struct list_head list; |
| 123 | struct ieee80211_hw *hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 124 | struct device *dev; |
| 125 | struct ieee80211_supported_band band; |
| 126 | struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)]; |
| 127 | struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)]; |
| 128 | |
| 129 | struct ieee80211_channel *channel; |
| 130 | int radio_enabled; |
| 131 | unsigned long beacon_int; /* in jiffies unit */ |
| 132 | unsigned int rx_filter; |
| 133 | int started; |
| 134 | struct timer_list beacon_timer; |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | struct hwsim_radiotap_hdr { |
| 139 | struct ieee80211_radiotap_header hdr; |
| 140 | u8 rt_flags; |
| 141 | u8 rt_rate; |
| 142 | __le16 rt_channel; |
| 143 | __le16 rt_chbitmask; |
| 144 | } __attribute__ ((packed)); |
| 145 | |
| 146 | |
| 147 | static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev) |
| 148 | { |
| 149 | /* TODO: allow packet injection */ |
| 150 | dev_kfree_skb(skb); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw, |
| 156 | struct sk_buff *tx_skb) |
| 157 | { |
| 158 | struct mac80211_hwsim_data *data = hw->priv; |
| 159 | struct sk_buff *skb; |
| 160 | struct hwsim_radiotap_hdr *hdr; |
| 161 | u16 flags; |
| 162 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb); |
| 163 | struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info); |
| 164 | |
| 165 | if (!netif_running(hwsim_mon)) |
| 166 | return; |
| 167 | |
| 168 | skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC); |
| 169 | if (skb == NULL) |
| 170 | return; |
| 171 | |
| 172 | hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr)); |
| 173 | hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION; |
| 174 | hdr->hdr.it_pad = 0; |
| 175 | hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr)); |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 176 | hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) | |
| 177 | (1 << IEEE80211_RADIOTAP_RATE) | |
| 178 | (1 << IEEE80211_RADIOTAP_CHANNEL)); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 179 | hdr->rt_flags = 0; |
| 180 | hdr->rt_rate = txrate->bitrate / 5; |
Johannes Berg | df70b4a | 2008-07-10 11:56:33 +0200 | [diff] [blame] | 181 | hdr->rt_channel = cpu_to_le16(data->channel->center_freq); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 182 | flags = IEEE80211_CHAN_2GHZ; |
| 183 | if (txrate->flags & IEEE80211_RATE_ERP_G) |
| 184 | flags |= IEEE80211_CHAN_OFDM; |
| 185 | else |
| 186 | flags |= IEEE80211_CHAN_CCK; |
| 187 | hdr->rt_chbitmask = cpu_to_le16(flags); |
| 188 | |
| 189 | skb->dev = hwsim_mon; |
| 190 | skb_set_mac_header(skb, 0); |
| 191 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 192 | skb->pkt_type = PACKET_OTHERHOST; |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 193 | skb->protocol = htons(ETH_P_802_2); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 194 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 195 | netif_rx(skb); |
| 196 | } |
| 197 | |
| 198 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 199 | static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw, |
| 200 | struct sk_buff *skb) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 201 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 202 | struct mac80211_hwsim_data *data = hw->priv, *data2; |
| 203 | bool ack = false; |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 204 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 205 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 206 | struct ieee80211_rx_status rx_status; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 207 | |
| 208 | memset(&rx_status, 0, sizeof(rx_status)); |
| 209 | /* TODO: set mactime */ |
| 210 | rx_status.freq = data->channel->center_freq; |
| 211 | rx_status.band = data->channel->band; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 212 | rx_status.rate_idx = info->control.rates[0].idx; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 213 | /* TODO: simulate signal strength (and optional packet drop) */ |
| 214 | |
| 215 | /* Copy skb to all enabled radios that are on the current frequency */ |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 216 | spin_lock(&hwsim_radio_lock); |
| 217 | list_for_each_entry(data2, &hwsim_radios, list) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 218 | struct sk_buff *nskb; |
| 219 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 220 | if (data == data2) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 221 | continue; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 222 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 223 | if (!data2->started || !data2->radio_enabled || |
| 224 | data->channel->center_freq != data2->channel->center_freq) |
| 225 | continue; |
| 226 | |
| 227 | nskb = skb_copy(skb, GFP_ATOMIC); |
| 228 | if (nskb == NULL) |
| 229 | continue; |
| 230 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 231 | if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr, |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 232 | ETH_ALEN) == 0) |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 233 | ack = true; |
| 234 | ieee80211_rx_irqsafe(data2->hw, nskb, &rx_status); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 235 | } |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 236 | spin_unlock(&hwsim_radio_lock); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 237 | |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 238 | return ack; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb) |
| 243 | { |
| 244 | struct mac80211_hwsim_data *data = hw->priv; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 245 | bool ack; |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 246 | struct ieee80211_tx_info *txi; |
| 247 | |
| 248 | mac80211_hwsim_monitor_rx(hw, skb); |
| 249 | |
| 250 | if (skb->len < 10) { |
| 251 | /* Should not happen; just a sanity check for addr1 use */ |
| 252 | dev_kfree_skb(skb); |
| 253 | return NETDEV_TX_OK; |
| 254 | } |
| 255 | |
| 256 | if (!data->radio_enabled) { |
| 257 | printk(KERN_DEBUG "%s: dropped TX frame since radio " |
| 258 | "disabled\n", wiphy_name(hw->wiphy)); |
| 259 | dev_kfree_skb(skb); |
| 260 | return NETDEV_TX_OK; |
| 261 | } |
| 262 | |
| 263 | ack = mac80211_hwsim_tx_frame(hw, skb); |
| 264 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 265 | txi = IEEE80211_SKB_CB(skb); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 266 | |
Johannes Berg | 25d834e | 2008-09-12 22:52:47 +0200 | [diff] [blame] | 267 | if (txi->control.vif) |
| 268 | hwsim_check_magic(txi->control.vif); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 269 | if (txi->control.sta) |
| 270 | hwsim_check_sta_magic(txi->control.sta); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 271 | |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 272 | ieee80211_tx_info_clear_status(txi); |
| 273 | if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack) |
| 274 | txi->flags |= IEEE80211_TX_STAT_ACK; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 275 | ieee80211_tx_status_irqsafe(hw, skb); |
| 276 | return NETDEV_TX_OK; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static int mac80211_hwsim_start(struct ieee80211_hw *hw) |
| 281 | { |
| 282 | struct mac80211_hwsim_data *data = hw->priv; |
| 283 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 284 | data->started = 1; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | static void mac80211_hwsim_stop(struct ieee80211_hw *hw) |
| 290 | { |
| 291 | struct mac80211_hwsim_data *data = hw->priv; |
| 292 | data->started = 0; |
| 293 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw, |
| 298 | struct ieee80211_if_init_conf *conf) |
| 299 | { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 300 | printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 301 | wiphy_name(hw->wiphy), __func__, conf->type, |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 302 | conf->mac_addr); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 303 | hwsim_set_magic(conf->vif); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | static void mac80211_hwsim_remove_interface( |
| 309 | struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf) |
| 310 | { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 311 | printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 312 | wiphy_name(hw->wiphy), __func__, conf->type, |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 313 | conf->mac_addr); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 314 | hwsim_check_magic(conf->vif); |
| 315 | hwsim_clear_magic(conf->vif); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac, |
| 320 | struct ieee80211_vif *vif) |
| 321 | { |
| 322 | struct ieee80211_hw *hw = arg; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 323 | struct sk_buff *skb; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 324 | struct ieee80211_tx_info *info; |
| 325 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 326 | hwsim_check_magic(vif); |
| 327 | |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 328 | if (vif->type != NL80211_IFTYPE_AP) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 329 | return; |
| 330 | |
| 331 | skb = ieee80211_beacon_get(hw, vif); |
| 332 | if (skb == NULL) |
| 333 | return; |
| 334 | info = IEEE80211_SKB_CB(skb); |
| 335 | |
| 336 | mac80211_hwsim_monitor_rx(hw, skb); |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 337 | mac80211_hwsim_tx_frame(hw, skb); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 338 | dev_kfree_skb(skb); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static void mac80211_hwsim_beacon(unsigned long arg) |
| 343 | { |
| 344 | struct ieee80211_hw *hw = (struct ieee80211_hw *) arg; |
| 345 | struct mac80211_hwsim_data *data = hw->priv; |
| 346 | |
| 347 | if (!data->started || !data->radio_enabled) |
| 348 | return; |
| 349 | |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 350 | ieee80211_iterate_active_interfaces_atomic( |
| 351 | hw, mac80211_hwsim_beacon_tx, hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 352 | |
| 353 | data->beacon_timer.expires = jiffies + data->beacon_int; |
| 354 | add_timer(&data->beacon_timer); |
| 355 | } |
| 356 | |
| 357 | |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 358 | static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 359 | { |
| 360 | struct mac80211_hwsim_data *data = hw->priv; |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 361 | struct ieee80211_conf *conf = &hw->conf; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 362 | |
| 363 | printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n", |
| 364 | wiphy_name(hw->wiphy), __func__, |
| 365 | conf->channel->center_freq, conf->radio_enabled, |
| 366 | conf->beacon_int); |
| 367 | |
| 368 | data->channel = conf->channel; |
| 369 | data->radio_enabled = conf->radio_enabled; |
| 370 | data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000; |
| 371 | if (data->beacon_int < 1) |
| 372 | data->beacon_int = 1; |
| 373 | |
| 374 | if (!data->started || !data->radio_enabled) |
| 375 | del_timer(&data->beacon_timer); |
| 376 | else |
| 377 | mod_timer(&data->beacon_timer, jiffies + data->beacon_int); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw, |
| 384 | unsigned int changed_flags, |
| 385 | unsigned int *total_flags, |
| 386 | int mc_count, |
| 387 | struct dev_addr_list *mc_list) |
| 388 | { |
| 389 | struct mac80211_hwsim_data *data = hw->priv; |
| 390 | |
| 391 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 392 | |
| 393 | data->rx_filter = 0; |
| 394 | if (*total_flags & FIF_PROMISC_IN_BSS) |
| 395 | data->rx_filter |= FIF_PROMISC_IN_BSS; |
| 396 | if (*total_flags & FIF_ALLMULTI) |
| 397 | data->rx_filter |= FIF_ALLMULTI; |
| 398 | |
| 399 | *total_flags = data->rx_filter; |
| 400 | } |
| 401 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 402 | static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw, |
| 403 | struct ieee80211_vif *vif, |
| 404 | struct ieee80211_if_conf *conf) |
| 405 | { |
| 406 | hwsim_check_magic(vif); |
| 407 | return 0; |
| 408 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 409 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 410 | static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw, |
| 411 | struct ieee80211_vif *vif, |
| 412 | struct ieee80211_bss_conf *info, |
| 413 | u32 changed) |
| 414 | { |
| 415 | hwsim_check_magic(vif); |
Jouni Malinen | fe63bfa | 2008-10-30 16:59:21 +0200 | [diff] [blame] | 416 | |
| 417 | printk(KERN_DEBUG "%s:%s(changed=0x%x)\n", |
| 418 | wiphy_name(hw->wiphy), __func__, changed); |
| 419 | |
| 420 | if (changed & BSS_CHANGED_ASSOC) { |
| 421 | printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n", |
| 422 | wiphy_name(hw->wiphy), info->assoc, info->aid); |
| 423 | } |
| 424 | |
| 425 | if (changed & BSS_CHANGED_ERP_CTS_PROT) { |
| 426 | printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n", |
| 427 | wiphy_name(hw->wiphy), info->use_cts_prot); |
| 428 | } |
| 429 | |
| 430 | if (changed & BSS_CHANGED_ERP_PREAMBLE) { |
| 431 | printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n", |
| 432 | wiphy_name(hw->wiphy), info->use_short_preamble); |
| 433 | } |
| 434 | |
| 435 | if (changed & BSS_CHANGED_ERP_SLOT) { |
| 436 | printk(KERN_DEBUG " %s: ERP_SLOT: %d\n", |
| 437 | wiphy_name(hw->wiphy), info->use_short_slot); |
| 438 | } |
| 439 | |
| 440 | if (changed & BSS_CHANGED_HT) { |
| 441 | printk(KERN_DEBUG " %s: HT: sec_ch_offs=%d width_40_ok=%d " |
| 442 | "op_mode=%d\n", |
| 443 | wiphy_name(hw->wiphy), |
| 444 | info->ht.secondary_channel_offset, |
| 445 | info->ht.width_40_ok, info->ht.operation_mode); |
| 446 | } |
| 447 | |
| 448 | if (changed & BSS_CHANGED_BASIC_RATES) { |
| 449 | printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n", |
| 450 | wiphy_name(hw->wiphy), |
| 451 | (unsigned long long) info->basic_rates); |
| 452 | } |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw, |
| 456 | struct ieee80211_vif *vif, |
Johannes Berg | 17741cd | 2008-09-11 00:02:02 +0200 | [diff] [blame] | 457 | enum sta_notify_cmd cmd, |
| 458 | struct ieee80211_sta *sta) |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 459 | { |
| 460 | hwsim_check_magic(vif); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 461 | switch (cmd) { |
| 462 | case STA_NOTIFY_ADD: |
| 463 | hwsim_set_sta_magic(sta); |
| 464 | break; |
| 465 | case STA_NOTIFY_REMOVE: |
| 466 | hwsim_clear_sta_magic(sta); |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw, |
| 472 | struct ieee80211_sta *sta, |
| 473 | bool set) |
| 474 | { |
| 475 | hwsim_check_sta_magic(sta); |
| 476 | return 0; |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 477 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 478 | |
Jouni Malinen | 1e898ff8 | 2008-10-30 16:59:23 +0200 | [diff] [blame] | 479 | static int mac80211_hwsim_conf_tx( |
| 480 | struct ieee80211_hw *hw, u16 queue, |
| 481 | const struct ieee80211_tx_queue_params *params) |
| 482 | { |
| 483 | printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d " |
| 484 | "aifs=%d)\n", |
| 485 | wiphy_name(hw->wiphy), __func__, queue, |
| 486 | params->txop, params->cw_min, params->cw_max, params->aifs); |
| 487 | return 0; |
| 488 | } |
| 489 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 490 | static const struct ieee80211_ops mac80211_hwsim_ops = |
| 491 | { |
| 492 | .tx = mac80211_hwsim_tx, |
| 493 | .start = mac80211_hwsim_start, |
| 494 | .stop = mac80211_hwsim_stop, |
| 495 | .add_interface = mac80211_hwsim_add_interface, |
| 496 | .remove_interface = mac80211_hwsim_remove_interface, |
| 497 | .config = mac80211_hwsim_config, |
| 498 | .configure_filter = mac80211_hwsim_configure_filter, |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 499 | .config_interface = mac80211_hwsim_config_interface, |
| 500 | .bss_info_changed = mac80211_hwsim_bss_info_changed, |
| 501 | .sta_notify = mac80211_hwsim_sta_notify, |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 502 | .set_tim = mac80211_hwsim_set_tim, |
Jouni Malinen | 1e898ff8 | 2008-10-30 16:59:23 +0200 | [diff] [blame] | 503 | .conf_tx = mac80211_hwsim_conf_tx, |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | |
| 507 | static void mac80211_hwsim_free(void) |
| 508 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 509 | struct list_head tmplist, *i, *tmp; |
| 510 | struct mac80211_hwsim_data *data; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 511 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 512 | INIT_LIST_HEAD(&tmplist); |
| 513 | |
| 514 | spin_lock_bh(&hwsim_radio_lock); |
| 515 | list_for_each_safe(i, tmp, &hwsim_radios) |
| 516 | list_move(i, &tmplist); |
| 517 | spin_unlock_bh(&hwsim_radio_lock); |
| 518 | |
| 519 | list_for_each_entry(data, &tmplist, list) { |
| 520 | ieee80211_unregister_hw(data->hw); |
| 521 | device_unregister(data->dev); |
| 522 | ieee80211_free_hw(data->hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 523 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 524 | class_destroy(hwsim_class); |
| 525 | } |
| 526 | |
| 527 | |
| 528 | static struct device_driver mac80211_hwsim_driver = { |
| 529 | .name = "mac80211_hwsim" |
| 530 | }; |
| 531 | |
| 532 | |
| 533 | static void hwsim_mon_setup(struct net_device *dev) |
| 534 | { |
| 535 | dev->hard_start_xmit = hwsim_mon_xmit; |
| 536 | dev->destructor = free_netdev; |
| 537 | ether_setup(dev); |
| 538 | dev->tx_queue_len = 0; |
| 539 | dev->type = ARPHRD_IEEE80211_RADIOTAP; |
| 540 | memset(dev->dev_addr, 0, ETH_ALEN); |
| 541 | dev->dev_addr[0] = 0x12; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | static int __init init_mac80211_hwsim(void) |
| 546 | { |
| 547 | int i, err = 0; |
| 548 | u8 addr[ETH_ALEN]; |
| 549 | struct mac80211_hwsim_data *data; |
| 550 | struct ieee80211_hw *hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 551 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 552 | if (radios < 1 || radios > 100) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 553 | return -EINVAL; |
| 554 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 555 | spin_lock_init(&hwsim_radio_lock); |
| 556 | INIT_LIST_HEAD(&hwsim_radios); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 557 | |
| 558 | hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim"); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 559 | if (IS_ERR(hwsim_class)) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 560 | return PTR_ERR(hwsim_class); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 561 | |
| 562 | memset(addr, 0, ETH_ALEN); |
| 563 | addr[0] = 0x02; |
| 564 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 565 | for (i = 0; i < radios; i++) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 566 | printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n", |
| 567 | i); |
| 568 | hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 569 | if (!hw) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 570 | printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw " |
| 571 | "failed\n"); |
| 572 | err = -ENOMEM; |
| 573 | goto failed; |
| 574 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 575 | data = hw->priv; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 576 | data->hw = hw; |
| 577 | |
Greg Kroah-Hartman | 6e05d6c | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 578 | data->dev = device_create(hwsim_class, NULL, 0, hw, |
| 579 | "hwsim%d", i); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 580 | if (IS_ERR(data->dev)) { |
Stephen Rothwell | e800f17 | 2008-06-16 15:35:10 +1000 | [diff] [blame] | 581 | printk(KERN_DEBUG |
Greg Kroah-Hartman | 6e05d6c | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 582 | "mac80211_hwsim: device_create " |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 583 | "failed (%ld)\n", PTR_ERR(data->dev)); |
| 584 | err = -ENOMEM; |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 585 | goto failed_drvdata; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 586 | } |
| 587 | data->dev->driver = &mac80211_hwsim_driver; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 588 | |
| 589 | SET_IEEE80211_DEV(hw, data->dev); |
| 590 | addr[3] = i >> 8; |
| 591 | addr[4] = i; |
| 592 | SET_IEEE80211_PERM_ADDR(hw, addr); |
| 593 | |
| 594 | hw->channel_change_time = 1; |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 595 | hw->queues = 4; |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 596 | hw->wiphy->interface_modes = |
| 597 | BIT(NL80211_IFTYPE_STATION) | |
| 598 | BIT(NL80211_IFTYPE_AP); |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 599 | hw->ampdu_queues = 1; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 600 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 601 | /* ask mac80211 to reserve space for magic */ |
| 602 | hw->vif_data_size = sizeof(struct hwsim_vif_priv); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 603 | hw->sta_data_size = sizeof(struct hwsim_sta_priv); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 604 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 605 | memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels)); |
| 606 | memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates)); |
| 607 | data->band.channels = data->channels; |
| 608 | data->band.n_channels = ARRAY_SIZE(hwsim_channels); |
| 609 | data->band.bitrates = data->rates; |
| 610 | data->band.n_bitrates = ARRAY_SIZE(hwsim_rates); |
Johannes Berg | d9fe60d | 2008-10-09 12:13:49 +0200 | [diff] [blame] | 611 | data->band.ht_cap.ht_supported = true; |
| 612 | data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 | |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 613 | IEEE80211_HT_CAP_GRN_FLD | |
| 614 | IEEE80211_HT_CAP_SGI_40 | |
| 615 | IEEE80211_HT_CAP_DSSSCCK40; |
Johannes Berg | d9fe60d | 2008-10-09 12:13:49 +0200 | [diff] [blame] | 616 | data->band.ht_cap.ampdu_factor = 0x3; |
| 617 | data->band.ht_cap.ampdu_density = 0x6; |
| 618 | memset(&data->band.ht_cap.mcs, 0, |
| 619 | sizeof(data->band.ht_cap.mcs)); |
| 620 | data->band.ht_cap.mcs.rx_mask[0] = 0xff; |
| 621 | data->band.ht_cap.mcs.rx_mask[1] = 0xff; |
| 622 | data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 623 | hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band; |
| 624 | |
| 625 | err = ieee80211_register_hw(hw); |
| 626 | if (err < 0) { |
| 627 | printk(KERN_DEBUG "mac80211_hwsim: " |
| 628 | "ieee80211_register_hw failed (%d)\n", err); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 629 | goto failed_hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 630 | } |
| 631 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 632 | printk(KERN_DEBUG "%s: hwaddr %pM registered\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 633 | wiphy_name(hw->wiphy), |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 634 | hw->wiphy->perm_addr); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 635 | |
| 636 | setup_timer(&data->beacon_timer, mac80211_hwsim_beacon, |
| 637 | (unsigned long) hw); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 638 | |
| 639 | list_add_tail(&data->list, &hwsim_radios); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup); |
| 643 | if (hwsim_mon == NULL) |
| 644 | goto failed; |
| 645 | |
| 646 | rtnl_lock(); |
| 647 | |
| 648 | err = dev_alloc_name(hwsim_mon, hwsim_mon->name); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 649 | if (err < 0) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 650 | goto failed_mon; |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 651 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 652 | |
| 653 | err = register_netdevice(hwsim_mon); |
| 654 | if (err < 0) |
| 655 | goto failed_mon; |
| 656 | |
| 657 | rtnl_unlock(); |
| 658 | |
| 659 | return 0; |
| 660 | |
| 661 | failed_mon: |
| 662 | rtnl_unlock(); |
| 663 | free_netdev(hwsim_mon); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 664 | mac80211_hwsim_free(); |
| 665 | return err; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 666 | |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 667 | failed_hw: |
| 668 | device_unregister(data->dev); |
| 669 | failed_drvdata: |
| 670 | ieee80211_free_hw(hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 671 | failed: |
| 672 | mac80211_hwsim_free(); |
| 673 | return err; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | static void __exit exit_mac80211_hwsim(void) |
| 678 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 679 | printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n"); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 680 | |
| 681 | unregister_netdev(hwsim_mon); |
| 682 | mac80211_hwsim_free(); |
| 683 | } |
| 684 | |
| 685 | |
| 686 | module_init(init_mac80211_hwsim); |
| 687 | module_exit(exit_mac80211_hwsim); |