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> |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 24 | #include <linux/debugfs.h> |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 25 | |
| 26 | MODULE_AUTHOR("Jouni Malinen"); |
| 27 | MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | static int radios = 2; |
| 31 | module_param(radios, int, 0444); |
| 32 | MODULE_PARM_DESC(radios, "Number of simulated radios"); |
| 33 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 34 | struct hwsim_vif_priv { |
| 35 | u32 magic; |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 36 | u8 bssid[ETH_ALEN]; |
| 37 | bool assoc; |
| 38 | u16 aid; |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define HWSIM_VIF_MAGIC 0x69537748 |
| 42 | |
| 43 | static inline void hwsim_check_magic(struct ieee80211_vif *vif) |
| 44 | { |
| 45 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 46 | WARN_ON(vp->magic != HWSIM_VIF_MAGIC); |
| 47 | } |
| 48 | |
| 49 | static inline void hwsim_set_magic(struct ieee80211_vif *vif) |
| 50 | { |
| 51 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 52 | vp->magic = HWSIM_VIF_MAGIC; |
| 53 | } |
| 54 | |
| 55 | static inline void hwsim_clear_magic(struct ieee80211_vif *vif) |
| 56 | { |
| 57 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 58 | vp->magic = 0; |
| 59 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 60 | |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 61 | struct hwsim_sta_priv { |
| 62 | u32 magic; |
| 63 | }; |
| 64 | |
| 65 | #define HWSIM_STA_MAGIC 0x6d537748 |
| 66 | |
| 67 | static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta) |
| 68 | { |
| 69 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
Rami Rosen | 5d6924e | 2008-10-08 11:18:27 +0200 | [diff] [blame] | 70 | WARN_ON(sp->magic != HWSIM_STA_MAGIC); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta) |
| 74 | { |
| 75 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
Rami Rosen | 5d6924e | 2008-10-08 11:18:27 +0200 | [diff] [blame] | 76 | sp->magic = HWSIM_STA_MAGIC; |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta) |
| 80 | { |
| 81 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
| 82 | sp->magic = 0; |
| 83 | } |
| 84 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 85 | static struct class *hwsim_class; |
| 86 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 87 | static struct net_device *hwsim_mon; /* global monitor netdev */ |
| 88 | |
| 89 | |
| 90 | static const struct ieee80211_channel hwsim_channels[] = { |
| 91 | { .center_freq = 2412 }, |
| 92 | { .center_freq = 2417 }, |
| 93 | { .center_freq = 2422 }, |
| 94 | { .center_freq = 2427 }, |
| 95 | { .center_freq = 2432 }, |
| 96 | { .center_freq = 2437 }, |
| 97 | { .center_freq = 2442 }, |
| 98 | { .center_freq = 2447 }, |
| 99 | { .center_freq = 2452 }, |
| 100 | { .center_freq = 2457 }, |
| 101 | { .center_freq = 2462 }, |
| 102 | { .center_freq = 2467 }, |
| 103 | { .center_freq = 2472 }, |
| 104 | { .center_freq = 2484 }, |
| 105 | }; |
| 106 | |
| 107 | static const struct ieee80211_rate hwsim_rates[] = { |
| 108 | { .bitrate = 10 }, |
| 109 | { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 110 | { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 111 | { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 112 | { .bitrate = 60 }, |
| 113 | { .bitrate = 90 }, |
| 114 | { .bitrate = 120 }, |
| 115 | { .bitrate = 180 }, |
| 116 | { .bitrate = 240 }, |
| 117 | { .bitrate = 360 }, |
| 118 | { .bitrate = 480 }, |
| 119 | { .bitrate = 540 } |
| 120 | }; |
| 121 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 122 | static spinlock_t hwsim_radio_lock; |
| 123 | static struct list_head hwsim_radios; |
| 124 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 125 | struct mac80211_hwsim_data { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 126 | struct list_head list; |
| 127 | struct ieee80211_hw *hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 128 | struct device *dev; |
| 129 | struct ieee80211_supported_band band; |
| 130 | struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)]; |
| 131 | struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)]; |
| 132 | |
| 133 | struct ieee80211_channel *channel; |
| 134 | int radio_enabled; |
| 135 | unsigned long beacon_int; /* in jiffies unit */ |
| 136 | unsigned int rx_filter; |
| 137 | int started; |
| 138 | struct timer_list beacon_timer; |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 139 | enum ps_mode { |
| 140 | PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL |
| 141 | } ps; |
| 142 | bool ps_poll_pending; |
| 143 | struct dentry *debugfs; |
| 144 | struct dentry *debugfs_ps; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | |
| 148 | struct hwsim_radiotap_hdr { |
| 149 | struct ieee80211_radiotap_header hdr; |
| 150 | u8 rt_flags; |
| 151 | u8 rt_rate; |
| 152 | __le16 rt_channel; |
| 153 | __le16 rt_chbitmask; |
| 154 | } __attribute__ ((packed)); |
| 155 | |
| 156 | |
| 157 | static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev) |
| 158 | { |
| 159 | /* TODO: allow packet injection */ |
| 160 | dev_kfree_skb(skb); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw, |
| 166 | struct sk_buff *tx_skb) |
| 167 | { |
| 168 | struct mac80211_hwsim_data *data = hw->priv; |
| 169 | struct sk_buff *skb; |
| 170 | struct hwsim_radiotap_hdr *hdr; |
| 171 | u16 flags; |
| 172 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb); |
| 173 | struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info); |
| 174 | |
| 175 | if (!netif_running(hwsim_mon)) |
| 176 | return; |
| 177 | |
| 178 | skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC); |
| 179 | if (skb == NULL) |
| 180 | return; |
| 181 | |
| 182 | hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr)); |
| 183 | hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION; |
| 184 | hdr->hdr.it_pad = 0; |
| 185 | hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr)); |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 186 | hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) | |
| 187 | (1 << IEEE80211_RADIOTAP_RATE) | |
| 188 | (1 << IEEE80211_RADIOTAP_CHANNEL)); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 189 | hdr->rt_flags = 0; |
| 190 | hdr->rt_rate = txrate->bitrate / 5; |
Johannes Berg | df70b4a | 2008-07-10 11:56:33 +0200 | [diff] [blame] | 191 | hdr->rt_channel = cpu_to_le16(data->channel->center_freq); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 192 | flags = IEEE80211_CHAN_2GHZ; |
| 193 | if (txrate->flags & IEEE80211_RATE_ERP_G) |
| 194 | flags |= IEEE80211_CHAN_OFDM; |
| 195 | else |
| 196 | flags |= IEEE80211_CHAN_CCK; |
| 197 | hdr->rt_chbitmask = cpu_to_le16(flags); |
| 198 | |
| 199 | skb->dev = hwsim_mon; |
| 200 | skb_set_mac_header(skb, 0); |
| 201 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 202 | skb->pkt_type = PACKET_OTHERHOST; |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 203 | skb->protocol = htons(ETH_P_802_2); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 204 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 205 | netif_rx(skb); |
| 206 | } |
| 207 | |
| 208 | |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 209 | static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data, |
| 210 | struct sk_buff *skb) |
| 211 | { |
| 212 | switch (data->ps) { |
| 213 | case PS_DISABLED: |
| 214 | return true; |
| 215 | case PS_ENABLED: |
| 216 | return false; |
| 217 | case PS_AUTO_POLL: |
| 218 | /* TODO: accept (some) Beacons by default and other frames only |
| 219 | * if pending PS-Poll has been sent */ |
| 220 | return true; |
| 221 | case PS_MANUAL_POLL: |
| 222 | /* Allow unicast frames to own address if there is a pending |
| 223 | * PS-Poll */ |
| 224 | if (data->ps_poll_pending && |
| 225 | memcmp(data->hw->wiphy->perm_addr, skb->data + 4, |
| 226 | ETH_ALEN) == 0) { |
| 227 | data->ps_poll_pending = false; |
| 228 | return true; |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 237 | static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw, |
| 238 | struct sk_buff *skb) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 239 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 240 | struct mac80211_hwsim_data *data = hw->priv, *data2; |
| 241 | bool ack = false; |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 242 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 243 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 244 | struct ieee80211_rx_status rx_status; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 245 | |
| 246 | memset(&rx_status, 0, sizeof(rx_status)); |
| 247 | /* TODO: set mactime */ |
| 248 | rx_status.freq = data->channel->center_freq; |
| 249 | rx_status.band = data->channel->band; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 250 | rx_status.rate_idx = info->control.rates[0].idx; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 251 | /* TODO: simulate signal strength (and optional packet drop) */ |
| 252 | |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 253 | if (data->ps != PS_DISABLED) |
| 254 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); |
| 255 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 256 | /* Copy skb to all enabled radios that are on the current frequency */ |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 257 | spin_lock(&hwsim_radio_lock); |
| 258 | list_for_each_entry(data2, &hwsim_radios, list) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 259 | struct sk_buff *nskb; |
| 260 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 261 | if (data == data2) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 262 | continue; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 263 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 264 | if (!data2->started || !data2->radio_enabled || |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 265 | !hwsim_ps_rx_ok(data2, skb) || |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 266 | data->channel->center_freq != data2->channel->center_freq) |
| 267 | continue; |
| 268 | |
| 269 | nskb = skb_copy(skb, GFP_ATOMIC); |
| 270 | if (nskb == NULL) |
| 271 | continue; |
| 272 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 273 | if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr, |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 274 | ETH_ALEN) == 0) |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 275 | ack = true; |
| 276 | ieee80211_rx_irqsafe(data2->hw, nskb, &rx_status); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 277 | } |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 278 | spin_unlock(&hwsim_radio_lock); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 279 | |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 280 | return ack; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb) |
| 285 | { |
| 286 | struct mac80211_hwsim_data *data = hw->priv; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 287 | bool ack; |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 288 | struct ieee80211_tx_info *txi; |
| 289 | |
| 290 | mac80211_hwsim_monitor_rx(hw, skb); |
| 291 | |
| 292 | if (skb->len < 10) { |
| 293 | /* Should not happen; just a sanity check for addr1 use */ |
| 294 | dev_kfree_skb(skb); |
| 295 | return NETDEV_TX_OK; |
| 296 | } |
| 297 | |
| 298 | if (!data->radio_enabled) { |
| 299 | printk(KERN_DEBUG "%s: dropped TX frame since radio " |
| 300 | "disabled\n", wiphy_name(hw->wiphy)); |
| 301 | dev_kfree_skb(skb); |
| 302 | return NETDEV_TX_OK; |
| 303 | } |
| 304 | |
| 305 | ack = mac80211_hwsim_tx_frame(hw, skb); |
| 306 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 307 | txi = IEEE80211_SKB_CB(skb); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 308 | |
Johannes Berg | 25d834e | 2008-09-12 22:52:47 +0200 | [diff] [blame] | 309 | if (txi->control.vif) |
| 310 | hwsim_check_magic(txi->control.vif); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 311 | if (txi->control.sta) |
| 312 | hwsim_check_sta_magic(txi->control.sta); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 313 | |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 314 | ieee80211_tx_info_clear_status(txi); |
| 315 | if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack) |
| 316 | txi->flags |= IEEE80211_TX_STAT_ACK; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 317 | ieee80211_tx_status_irqsafe(hw, skb); |
| 318 | return NETDEV_TX_OK; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | static int mac80211_hwsim_start(struct ieee80211_hw *hw) |
| 323 | { |
| 324 | struct mac80211_hwsim_data *data = hw->priv; |
| 325 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 326 | data->started = 1; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | static void mac80211_hwsim_stop(struct ieee80211_hw *hw) |
| 332 | { |
| 333 | struct mac80211_hwsim_data *data = hw->priv; |
| 334 | data->started = 0; |
Jouni Malinen | ab1ef98 | 2008-10-30 16:59:25 +0200 | [diff] [blame] | 335 | del_timer(&data->beacon_timer); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 336 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw, |
| 341 | struct ieee80211_if_init_conf *conf) |
| 342 | { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 343 | printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 344 | wiphy_name(hw->wiphy), __func__, conf->type, |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 345 | conf->mac_addr); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 346 | hwsim_set_magic(conf->vif); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | static void mac80211_hwsim_remove_interface( |
| 352 | struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf) |
| 353 | { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 354 | printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 355 | wiphy_name(hw->wiphy), __func__, conf->type, |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 356 | conf->mac_addr); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 357 | hwsim_check_magic(conf->vif); |
| 358 | hwsim_clear_magic(conf->vif); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
| 362 | static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac, |
| 363 | struct ieee80211_vif *vif) |
| 364 | { |
| 365 | struct ieee80211_hw *hw = arg; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 366 | struct sk_buff *skb; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 367 | struct ieee80211_tx_info *info; |
| 368 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 369 | hwsim_check_magic(vif); |
| 370 | |
Andrey Yurovsky | 55b3961 | 2008-10-31 23:23:35 -0700 | [diff] [blame] | 371 | if (vif->type != NL80211_IFTYPE_AP && |
| 372 | vif->type != NL80211_IFTYPE_MESH_POINT) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 373 | return; |
| 374 | |
| 375 | skb = ieee80211_beacon_get(hw, vif); |
| 376 | if (skb == NULL) |
| 377 | return; |
| 378 | info = IEEE80211_SKB_CB(skb); |
| 379 | |
| 380 | mac80211_hwsim_monitor_rx(hw, skb); |
Jouni Malinen | e36cfdc | 2008-06-13 19:44:48 +0300 | [diff] [blame] | 381 | mac80211_hwsim_tx_frame(hw, skb); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 382 | dev_kfree_skb(skb); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | static void mac80211_hwsim_beacon(unsigned long arg) |
| 387 | { |
| 388 | struct ieee80211_hw *hw = (struct ieee80211_hw *) arg; |
| 389 | struct mac80211_hwsim_data *data = hw->priv; |
| 390 | |
| 391 | if (!data->started || !data->radio_enabled) |
| 392 | return; |
| 393 | |
Jouni Malinen | f248f10 | 2008-06-13 19:44:47 +0300 | [diff] [blame] | 394 | ieee80211_iterate_active_interfaces_atomic( |
| 395 | hw, mac80211_hwsim_beacon_tx, hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 396 | |
| 397 | data->beacon_timer.expires = jiffies + data->beacon_int; |
| 398 | add_timer(&data->beacon_timer); |
| 399 | } |
| 400 | |
| 401 | |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 402 | static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 403 | { |
| 404 | struct mac80211_hwsim_data *data = hw->priv; |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 405 | struct ieee80211_conf *conf = &hw->conf; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 406 | |
| 407 | printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n", |
| 408 | wiphy_name(hw->wiphy), __func__, |
| 409 | conf->channel->center_freq, conf->radio_enabled, |
| 410 | conf->beacon_int); |
| 411 | |
| 412 | data->channel = conf->channel; |
| 413 | data->radio_enabled = conf->radio_enabled; |
| 414 | data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000; |
| 415 | if (data->beacon_int < 1) |
| 416 | data->beacon_int = 1; |
| 417 | |
| 418 | if (!data->started || !data->radio_enabled) |
| 419 | del_timer(&data->beacon_timer); |
| 420 | else |
| 421 | mod_timer(&data->beacon_timer, jiffies + data->beacon_int); |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw, |
| 428 | unsigned int changed_flags, |
| 429 | unsigned int *total_flags, |
| 430 | int mc_count, |
| 431 | struct dev_addr_list *mc_list) |
| 432 | { |
| 433 | struct mac80211_hwsim_data *data = hw->priv; |
| 434 | |
| 435 | printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__); |
| 436 | |
| 437 | data->rx_filter = 0; |
| 438 | if (*total_flags & FIF_PROMISC_IN_BSS) |
| 439 | data->rx_filter |= FIF_PROMISC_IN_BSS; |
| 440 | if (*total_flags & FIF_ALLMULTI) |
| 441 | data->rx_filter |= FIF_ALLMULTI; |
| 442 | |
| 443 | *total_flags = data->rx_filter; |
| 444 | } |
| 445 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 446 | static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw, |
| 447 | struct ieee80211_vif *vif, |
| 448 | struct ieee80211_if_conf *conf) |
| 449 | { |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 450 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 451 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 452 | hwsim_check_magic(vif); |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 453 | if (conf->changed & IEEE80211_IFCC_BSSID) { |
| 454 | DECLARE_MAC_BUF(mac); |
John W. Linville | b235507 | 2008-11-25 16:47:36 -0500 | [diff] [blame] | 455 | printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n", |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 456 | wiphy_name(hw->wiphy), __func__, |
John W. Linville | b235507 | 2008-11-25 16:47:36 -0500 | [diff] [blame] | 457 | conf->bssid); |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 458 | memcpy(vp->bssid, conf->bssid, ETH_ALEN); |
| 459 | } |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 460 | return 0; |
| 461 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 462 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 463 | static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw, |
| 464 | struct ieee80211_vif *vif, |
| 465 | struct ieee80211_bss_conf *info, |
| 466 | u32 changed) |
| 467 | { |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 468 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 469 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 470 | hwsim_check_magic(vif); |
Jouni Malinen | fe63bfa | 2008-10-30 16:59:21 +0200 | [diff] [blame] | 471 | |
| 472 | printk(KERN_DEBUG "%s:%s(changed=0x%x)\n", |
| 473 | wiphy_name(hw->wiphy), __func__, changed); |
| 474 | |
| 475 | if (changed & BSS_CHANGED_ASSOC) { |
| 476 | printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n", |
| 477 | wiphy_name(hw->wiphy), info->assoc, info->aid); |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 478 | vp->assoc = info->assoc; |
| 479 | vp->aid = info->aid; |
Jouni Malinen | fe63bfa | 2008-10-30 16:59:21 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | if (changed & BSS_CHANGED_ERP_CTS_PROT) { |
| 483 | printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n", |
| 484 | wiphy_name(hw->wiphy), info->use_cts_prot); |
| 485 | } |
| 486 | |
| 487 | if (changed & BSS_CHANGED_ERP_PREAMBLE) { |
| 488 | printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n", |
| 489 | wiphy_name(hw->wiphy), info->use_short_preamble); |
| 490 | } |
| 491 | |
| 492 | if (changed & BSS_CHANGED_ERP_SLOT) { |
| 493 | printk(KERN_DEBUG " %s: ERP_SLOT: %d\n", |
| 494 | wiphy_name(hw->wiphy), info->use_short_slot); |
| 495 | } |
| 496 | |
| 497 | if (changed & BSS_CHANGED_HT) { |
Sujith | 094d05d | 2008-12-12 11:57:43 +0530 | [diff] [blame] | 498 | printk(KERN_DEBUG " %s: HT: op_mode=0x%x\n", |
Jouni Malinen | fe63bfa | 2008-10-30 16:59:21 +0200 | [diff] [blame] | 499 | wiphy_name(hw->wiphy), |
Sujith | 094d05d | 2008-12-12 11:57:43 +0530 | [diff] [blame] | 500 | info->ht.operation_mode); |
Jouni Malinen | fe63bfa | 2008-10-30 16:59:21 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (changed & BSS_CHANGED_BASIC_RATES) { |
| 504 | printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n", |
| 505 | wiphy_name(hw->wiphy), |
| 506 | (unsigned long long) info->basic_rates); |
| 507 | } |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw, |
| 511 | struct ieee80211_vif *vif, |
Johannes Berg | 17741cd | 2008-09-11 00:02:02 +0200 | [diff] [blame] | 512 | enum sta_notify_cmd cmd, |
| 513 | struct ieee80211_sta *sta) |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 514 | { |
| 515 | hwsim_check_magic(vif); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 516 | switch (cmd) { |
| 517 | case STA_NOTIFY_ADD: |
| 518 | hwsim_set_sta_magic(sta); |
| 519 | break; |
| 520 | case STA_NOTIFY_REMOVE: |
| 521 | hwsim_clear_sta_magic(sta); |
| 522 | break; |
Christian Lamparter | 89fad57 | 2008-12-09 16:28:06 +0100 | [diff] [blame] | 523 | case STA_NOTIFY_SLEEP: |
| 524 | case STA_NOTIFY_AWAKE: |
| 525 | /* TODO: make good use of these flags */ |
| 526 | break; |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw, |
| 531 | struct ieee80211_sta *sta, |
| 532 | bool set) |
| 533 | { |
| 534 | hwsim_check_sta_magic(sta); |
| 535 | return 0; |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 536 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 537 | |
Jouni Malinen | 1e898ff8 | 2008-10-30 16:59:23 +0200 | [diff] [blame] | 538 | static int mac80211_hwsim_conf_tx( |
| 539 | struct ieee80211_hw *hw, u16 queue, |
| 540 | const struct ieee80211_tx_queue_params *params) |
| 541 | { |
| 542 | printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d " |
| 543 | "aifs=%d)\n", |
| 544 | wiphy_name(hw->wiphy), __func__, queue, |
| 545 | params->txop, params->cw_min, params->cw_max, params->aifs); |
| 546 | return 0; |
| 547 | } |
| 548 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 549 | static const struct ieee80211_ops mac80211_hwsim_ops = |
| 550 | { |
| 551 | .tx = mac80211_hwsim_tx, |
| 552 | .start = mac80211_hwsim_start, |
| 553 | .stop = mac80211_hwsim_stop, |
| 554 | .add_interface = mac80211_hwsim_add_interface, |
| 555 | .remove_interface = mac80211_hwsim_remove_interface, |
| 556 | .config = mac80211_hwsim_config, |
| 557 | .configure_filter = mac80211_hwsim_configure_filter, |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 558 | .config_interface = mac80211_hwsim_config_interface, |
| 559 | .bss_info_changed = mac80211_hwsim_bss_info_changed, |
| 560 | .sta_notify = mac80211_hwsim_sta_notify, |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 561 | .set_tim = mac80211_hwsim_set_tim, |
Jouni Malinen | 1e898ff8 | 2008-10-30 16:59:23 +0200 | [diff] [blame] | 562 | .conf_tx = mac80211_hwsim_conf_tx, |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 563 | }; |
| 564 | |
| 565 | |
| 566 | static void mac80211_hwsim_free(void) |
| 567 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 568 | struct list_head tmplist, *i, *tmp; |
| 569 | struct mac80211_hwsim_data *data; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 570 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 571 | INIT_LIST_HEAD(&tmplist); |
| 572 | |
| 573 | spin_lock_bh(&hwsim_radio_lock); |
| 574 | list_for_each_safe(i, tmp, &hwsim_radios) |
| 575 | list_move(i, &tmplist); |
| 576 | spin_unlock_bh(&hwsim_radio_lock); |
| 577 | |
| 578 | list_for_each_entry(data, &tmplist, list) { |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 579 | debugfs_remove(data->debugfs_ps); |
| 580 | debugfs_remove(data->debugfs); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 581 | ieee80211_unregister_hw(data->hw); |
| 582 | device_unregister(data->dev); |
| 583 | ieee80211_free_hw(data->hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 584 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 585 | class_destroy(hwsim_class); |
| 586 | } |
| 587 | |
| 588 | |
| 589 | static struct device_driver mac80211_hwsim_driver = { |
| 590 | .name = "mac80211_hwsim" |
| 591 | }; |
| 592 | |
| 593 | |
| 594 | static void hwsim_mon_setup(struct net_device *dev) |
| 595 | { |
| 596 | dev->hard_start_xmit = hwsim_mon_xmit; |
| 597 | dev->destructor = free_netdev; |
| 598 | ether_setup(dev); |
| 599 | dev->tx_queue_len = 0; |
| 600 | dev->type = ARPHRD_IEEE80211_RADIOTAP; |
| 601 | memset(dev->dev_addr, 0, ETH_ALEN); |
| 602 | dev->dev_addr[0] = 0x12; |
| 603 | } |
| 604 | |
| 605 | |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 606 | static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif) |
| 607 | { |
| 608 | struct mac80211_hwsim_data *data = dat; |
| 609 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 610 | DECLARE_MAC_BUF(buf); |
| 611 | struct sk_buff *skb; |
| 612 | struct ieee80211_pspoll *pspoll; |
| 613 | |
| 614 | if (!vp->assoc) |
| 615 | return; |
| 616 | |
John W. Linville | b235507 | 2008-11-25 16:47:36 -0500 | [diff] [blame] | 617 | printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n", |
| 618 | wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid); |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 619 | |
| 620 | skb = dev_alloc_skb(sizeof(*pspoll)); |
| 621 | if (!skb) |
| 622 | return; |
| 623 | pspoll = (void *) skb_put(skb, sizeof(*pspoll)); |
| 624 | pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | |
| 625 | IEEE80211_STYPE_PSPOLL | |
| 626 | IEEE80211_FCTL_PM); |
| 627 | pspoll->aid = cpu_to_le16(0xc000 | vp->aid); |
| 628 | memcpy(pspoll->bssid, vp->bssid, ETH_ALEN); |
| 629 | memcpy(pspoll->ta, mac, ETH_ALEN); |
| 630 | if (data->radio_enabled && |
| 631 | !mac80211_hwsim_tx_frame(data->hw, skb)) |
| 632 | printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__); |
| 633 | dev_kfree_skb(skb); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac, |
| 638 | struct ieee80211_vif *vif, int ps) |
| 639 | { |
| 640 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 641 | DECLARE_MAC_BUF(buf); |
| 642 | struct sk_buff *skb; |
| 643 | struct ieee80211_hdr *hdr; |
| 644 | |
| 645 | if (!vp->assoc) |
| 646 | return; |
| 647 | |
John W. Linville | b235507 | 2008-11-25 16:47:36 -0500 | [diff] [blame] | 648 | printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n", |
| 649 | wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps); |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 650 | |
| 651 | skb = dev_alloc_skb(sizeof(*hdr)); |
| 652 | if (!skb) |
| 653 | return; |
| 654 | hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN); |
| 655 | hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA | |
| 656 | IEEE80211_STYPE_NULLFUNC | |
| 657 | (ps ? IEEE80211_FCTL_PM : 0)); |
| 658 | hdr->duration_id = cpu_to_le16(0); |
| 659 | memcpy(hdr->addr1, vp->bssid, ETH_ALEN); |
| 660 | memcpy(hdr->addr2, mac, ETH_ALEN); |
| 661 | memcpy(hdr->addr3, vp->bssid, ETH_ALEN); |
| 662 | if (data->radio_enabled && |
| 663 | !mac80211_hwsim_tx_frame(data->hw, skb)) |
| 664 | printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__); |
| 665 | dev_kfree_skb(skb); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | static void hwsim_send_nullfunc_ps(void *dat, u8 *mac, |
| 670 | struct ieee80211_vif *vif) |
| 671 | { |
| 672 | struct mac80211_hwsim_data *data = dat; |
| 673 | hwsim_send_nullfunc(data, mac, vif, 1); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac, |
| 678 | struct ieee80211_vif *vif) |
| 679 | { |
| 680 | struct mac80211_hwsim_data *data = dat; |
| 681 | hwsim_send_nullfunc(data, mac, vif, 0); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | static int hwsim_fops_ps_read(void *dat, u64 *val) |
| 686 | { |
| 687 | struct mac80211_hwsim_data *data = dat; |
| 688 | *val = data->ps; |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int hwsim_fops_ps_write(void *dat, u64 val) |
| 693 | { |
| 694 | struct mac80211_hwsim_data *data = dat; |
| 695 | enum ps_mode old_ps; |
| 696 | |
| 697 | if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL && |
| 698 | val != PS_MANUAL_POLL) |
| 699 | return -EINVAL; |
| 700 | |
| 701 | old_ps = data->ps; |
| 702 | data->ps = val; |
| 703 | |
| 704 | if (val == PS_MANUAL_POLL) { |
| 705 | ieee80211_iterate_active_interfaces(data->hw, |
| 706 | hwsim_send_ps_poll, data); |
| 707 | data->ps_poll_pending = true; |
| 708 | } else if (old_ps == PS_DISABLED && val != PS_DISABLED) { |
| 709 | ieee80211_iterate_active_interfaces(data->hw, |
| 710 | hwsim_send_nullfunc_ps, |
| 711 | data); |
| 712 | } else if (old_ps != PS_DISABLED && val == PS_DISABLED) { |
| 713 | ieee80211_iterate_active_interfaces(data->hw, |
| 714 | hwsim_send_nullfunc_no_ps, |
| 715 | data); |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write, |
| 722 | "%llu\n"); |
| 723 | |
| 724 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 725 | static int __init init_mac80211_hwsim(void) |
| 726 | { |
| 727 | int i, err = 0; |
| 728 | u8 addr[ETH_ALEN]; |
| 729 | struct mac80211_hwsim_data *data; |
| 730 | struct ieee80211_hw *hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 731 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 732 | if (radios < 1 || radios > 100) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 733 | return -EINVAL; |
| 734 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 735 | spin_lock_init(&hwsim_radio_lock); |
| 736 | INIT_LIST_HEAD(&hwsim_radios); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 737 | |
| 738 | hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim"); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 739 | if (IS_ERR(hwsim_class)) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 740 | return PTR_ERR(hwsim_class); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 741 | |
| 742 | memset(addr, 0, ETH_ALEN); |
| 743 | addr[0] = 0x02; |
| 744 | |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 745 | for (i = 0; i < radios; i++) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 746 | printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n", |
| 747 | i); |
| 748 | hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 749 | if (!hw) { |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 750 | printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw " |
| 751 | "failed\n"); |
| 752 | err = -ENOMEM; |
| 753 | goto failed; |
| 754 | } |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 755 | data = hw->priv; |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 756 | data->hw = hw; |
| 757 | |
Greg Kroah-Hartman | 6e05d6c | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 758 | data->dev = device_create(hwsim_class, NULL, 0, hw, |
| 759 | "hwsim%d", i); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 760 | if (IS_ERR(data->dev)) { |
Stephen Rothwell | e800f17 | 2008-06-16 15:35:10 +1000 | [diff] [blame] | 761 | printk(KERN_DEBUG |
Greg Kroah-Hartman | 6e05d6c | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 762 | "mac80211_hwsim: device_create " |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 763 | "failed (%ld)\n", PTR_ERR(data->dev)); |
| 764 | err = -ENOMEM; |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 765 | goto failed_drvdata; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 766 | } |
| 767 | data->dev->driver = &mac80211_hwsim_driver; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 768 | |
| 769 | SET_IEEE80211_DEV(hw, data->dev); |
| 770 | addr[3] = i >> 8; |
| 771 | addr[4] = i; |
| 772 | SET_IEEE80211_PERM_ADDR(hw, addr); |
| 773 | |
| 774 | hw->channel_change_time = 1; |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 775 | hw->queues = 4; |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 776 | hw->wiphy->interface_modes = |
| 777 | BIT(NL80211_IFTYPE_STATION) | |
Andrey Yurovsky | 55b3961 | 2008-10-31 23:23:35 -0700 | [diff] [blame] | 778 | BIT(NL80211_IFTYPE_AP) | |
| 779 | BIT(NL80211_IFTYPE_MESH_POINT); |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 780 | hw->ampdu_queues = 1; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 781 | |
Jouni Malinen | fa77533 | 2009-01-08 13:32:14 +0200 | [diff] [blame^] | 782 | hw->flags = IEEE80211_HW_MFP_CAPABLE; |
| 783 | |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 784 | /* ask mac80211 to reserve space for magic */ |
| 785 | hw->vif_data_size = sizeof(struct hwsim_vif_priv); |
Johannes Berg | 81c0652 | 2008-09-11 02:17:01 +0200 | [diff] [blame] | 786 | hw->sta_data_size = sizeof(struct hwsim_sta_priv); |
Johannes Berg | 8aa21e6 | 2008-09-11 02:16:36 +0200 | [diff] [blame] | 787 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 788 | memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels)); |
| 789 | memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates)); |
| 790 | data->band.channels = data->channels; |
| 791 | data->band.n_channels = ARRAY_SIZE(hwsim_channels); |
| 792 | data->band.bitrates = data->rates; |
| 793 | data->band.n_bitrates = ARRAY_SIZE(hwsim_rates); |
Johannes Berg | d9fe60d | 2008-10-09 12:13:49 +0200 | [diff] [blame] | 794 | data->band.ht_cap.ht_supported = true; |
| 795 | data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 | |
Jouni Malinen | 87e8b64 | 2008-08-21 21:45:56 +0300 | [diff] [blame] | 796 | IEEE80211_HT_CAP_GRN_FLD | |
| 797 | IEEE80211_HT_CAP_SGI_40 | |
| 798 | IEEE80211_HT_CAP_DSSSCCK40; |
Johannes Berg | d9fe60d | 2008-10-09 12:13:49 +0200 | [diff] [blame] | 799 | data->band.ht_cap.ampdu_factor = 0x3; |
| 800 | data->band.ht_cap.ampdu_density = 0x6; |
| 801 | memset(&data->band.ht_cap.mcs, 0, |
| 802 | sizeof(data->band.ht_cap.mcs)); |
| 803 | data->band.ht_cap.mcs.rx_mask[0] = 0xff; |
| 804 | data->band.ht_cap.mcs.rx_mask[1] = 0xff; |
| 805 | data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 806 | hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band; |
| 807 | |
| 808 | err = ieee80211_register_hw(hw); |
| 809 | if (err < 0) { |
| 810 | printk(KERN_DEBUG "mac80211_hwsim: " |
| 811 | "ieee80211_register_hw failed (%d)\n", err); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 812 | goto failed_hw; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 813 | } |
| 814 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 815 | printk(KERN_DEBUG "%s: hwaddr %pM registered\n", |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 816 | wiphy_name(hw->wiphy), |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 817 | hw->wiphy->perm_addr); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 818 | |
Jouni Malinen | fc6971d | 2008-10-30 19:59:05 +0200 | [diff] [blame] | 819 | data->debugfs = debugfs_create_dir("hwsim", |
| 820 | hw->wiphy->debugfsdir); |
| 821 | data->debugfs_ps = debugfs_create_file("ps", 0666, |
| 822 | data->debugfs, data, |
| 823 | &hwsim_fops_ps); |
| 824 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 825 | setup_timer(&data->beacon_timer, mac80211_hwsim_beacon, |
| 826 | (unsigned long) hw); |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 827 | |
| 828 | list_add_tail(&data->list, &hwsim_radios); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup); |
| 832 | if (hwsim_mon == NULL) |
| 833 | goto failed; |
| 834 | |
| 835 | rtnl_lock(); |
| 836 | |
| 837 | err = dev_alloc_name(hwsim_mon, hwsim_mon->name); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 838 | if (err < 0) |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 839 | goto failed_mon; |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 840 | |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 841 | |
| 842 | err = register_netdevice(hwsim_mon); |
| 843 | if (err < 0) |
| 844 | goto failed_mon; |
| 845 | |
| 846 | rtnl_unlock(); |
| 847 | |
| 848 | return 0; |
| 849 | |
| 850 | failed_mon: |
| 851 | rtnl_unlock(); |
| 852 | free_netdev(hwsim_mon); |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 853 | mac80211_hwsim_free(); |
| 854 | return err; |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 855 | |
Ian Schram | 3a33cc1 | 2008-07-21 13:19:35 -0700 | [diff] [blame] | 856 | failed_hw: |
| 857 | device_unregister(data->dev); |
| 858 | failed_drvdata: |
| 859 | ieee80211_free_hw(hw); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 860 | failed: |
| 861 | mac80211_hwsim_free(); |
| 862 | return err; |
| 863 | } |
| 864 | |
| 865 | |
| 866 | static void __exit exit_mac80211_hwsim(void) |
| 867 | { |
Johannes Berg | 0e057d73 | 2008-09-12 00:39:22 +0200 | [diff] [blame] | 868 | printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n"); |
Jouni Malinen | acc1e7a | 2008-06-11 10:42:31 +0300 | [diff] [blame] | 869 | |
| 870 | unregister_netdev(hwsim_mon); |
| 871 | mac80211_hwsim_free(); |
| 872 | } |
| 873 | |
| 874 | |
| 875 | module_init(init_mac80211_hwsim); |
| 876 | module_exit(exit_mac80211_hwsim); |