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