| 1 | /* |
| 2 | * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211 |
| 3 | * Copyright (c) 2008, Jouni Malinen <j@w1.fi> |
| 4 | * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * TODO: |
| 13 | * - Add TSF sync and fix IBSS beacon transmission by adding |
| 14 | * competition for "air time" at TBTT |
| 15 | * - RX filtering based on filter configuration (data->rx_filter) |
| 16 | */ |
| 17 | |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <net/dst.h> |
| 22 | #include <net/xfrm.h> |
| 23 | #include <net/mac80211.h> |
| 24 | #include <net/ieee80211_radiotap.h> |
| 25 | #include <linux/if_arp.h> |
| 26 | #include <linux/rtnetlink.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/debugfs.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/ktime.h> |
| 32 | #include <net/genetlink.h> |
| 33 | #include "mac80211_hwsim.h" |
| 34 | |
| 35 | #define WARN_QUEUE 100 |
| 36 | #define MAX_QUEUE 200 |
| 37 | |
| 38 | MODULE_AUTHOR("Jouni Malinen"); |
| 39 | MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
| 42 | static u32 wmediumd_portid; |
| 43 | |
| 44 | static int radios = 2; |
| 45 | module_param(radios, int, 0444); |
| 46 | MODULE_PARM_DESC(radios, "Number of simulated radios"); |
| 47 | |
| 48 | static int channels = 1; |
| 49 | module_param(channels, int, 0444); |
| 50 | MODULE_PARM_DESC(channels, "Number of concurrent channels"); |
| 51 | |
| 52 | static bool paged_rx = false; |
| 53 | module_param(paged_rx, bool, 0644); |
| 54 | MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones"); |
| 55 | |
| 56 | static bool rctbl = false; |
| 57 | module_param(rctbl, bool, 0444); |
| 58 | MODULE_PARM_DESC(rctbl, "Handle rate control table"); |
| 59 | |
| 60 | struct hwsim_vif_priv { |
| 61 | u32 magic; |
| 62 | u8 bssid[ETH_ALEN]; |
| 63 | bool assoc; |
| 64 | bool bcn_en; |
| 65 | u16 aid; |
| 66 | }; |
| 67 | |
| 68 | #define HWSIM_VIF_MAGIC 0x69537748 |
| 69 | |
| 70 | static inline void hwsim_check_magic(struct ieee80211_vif *vif) |
| 71 | { |
| 72 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 73 | WARN(vp->magic != HWSIM_VIF_MAGIC, |
| 74 | "Invalid VIF (%p) magic %#x, %pM, %d/%d\n", |
| 75 | vif, vp->magic, vif->addr, vif->type, vif->p2p); |
| 76 | } |
| 77 | |
| 78 | static inline void hwsim_set_magic(struct ieee80211_vif *vif) |
| 79 | { |
| 80 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 81 | vp->magic = HWSIM_VIF_MAGIC; |
| 82 | } |
| 83 | |
| 84 | static inline void hwsim_clear_magic(struct ieee80211_vif *vif) |
| 85 | { |
| 86 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 87 | vp->magic = 0; |
| 88 | } |
| 89 | |
| 90 | struct hwsim_sta_priv { |
| 91 | u32 magic; |
| 92 | }; |
| 93 | |
| 94 | #define HWSIM_STA_MAGIC 0x6d537749 |
| 95 | |
| 96 | static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta) |
| 97 | { |
| 98 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
| 99 | WARN_ON(sp->magic != HWSIM_STA_MAGIC); |
| 100 | } |
| 101 | |
| 102 | static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta) |
| 103 | { |
| 104 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
| 105 | sp->magic = HWSIM_STA_MAGIC; |
| 106 | } |
| 107 | |
| 108 | static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta) |
| 109 | { |
| 110 | struct hwsim_sta_priv *sp = (void *)sta->drv_priv; |
| 111 | sp->magic = 0; |
| 112 | } |
| 113 | |
| 114 | struct hwsim_chanctx_priv { |
| 115 | u32 magic; |
| 116 | }; |
| 117 | |
| 118 | #define HWSIM_CHANCTX_MAGIC 0x6d53774a |
| 119 | |
| 120 | static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c) |
| 121 | { |
| 122 | struct hwsim_chanctx_priv *cp = (void *)c->drv_priv; |
| 123 | WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC); |
| 124 | } |
| 125 | |
| 126 | static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c) |
| 127 | { |
| 128 | struct hwsim_chanctx_priv *cp = (void *)c->drv_priv; |
| 129 | cp->magic = HWSIM_CHANCTX_MAGIC; |
| 130 | } |
| 131 | |
| 132 | static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c) |
| 133 | { |
| 134 | struct hwsim_chanctx_priv *cp = (void *)c->drv_priv; |
| 135 | cp->magic = 0; |
| 136 | } |
| 137 | |
| 138 | static struct class *hwsim_class; |
| 139 | |
| 140 | static struct net_device *hwsim_mon; /* global monitor netdev */ |
| 141 | |
| 142 | #define CHAN2G(_freq) { \ |
| 143 | .band = IEEE80211_BAND_2GHZ, \ |
| 144 | .center_freq = (_freq), \ |
| 145 | .hw_value = (_freq), \ |
| 146 | .max_power = 20, \ |
| 147 | } |
| 148 | |
| 149 | #define CHAN5G(_freq) { \ |
| 150 | .band = IEEE80211_BAND_5GHZ, \ |
| 151 | .center_freq = (_freq), \ |
| 152 | .hw_value = (_freq), \ |
| 153 | .max_power = 20, \ |
| 154 | } |
| 155 | |
| 156 | static const struct ieee80211_channel hwsim_channels_2ghz[] = { |
| 157 | CHAN2G(2412), /* Channel 1 */ |
| 158 | CHAN2G(2417), /* Channel 2 */ |
| 159 | CHAN2G(2422), /* Channel 3 */ |
| 160 | CHAN2G(2427), /* Channel 4 */ |
| 161 | CHAN2G(2432), /* Channel 5 */ |
| 162 | CHAN2G(2437), /* Channel 6 */ |
| 163 | CHAN2G(2442), /* Channel 7 */ |
| 164 | CHAN2G(2447), /* Channel 8 */ |
| 165 | CHAN2G(2452), /* Channel 9 */ |
| 166 | CHAN2G(2457), /* Channel 10 */ |
| 167 | CHAN2G(2462), /* Channel 11 */ |
| 168 | CHAN2G(2467), /* Channel 12 */ |
| 169 | CHAN2G(2472), /* Channel 13 */ |
| 170 | CHAN2G(2484), /* Channel 14 */ |
| 171 | }; |
| 172 | |
| 173 | static const struct ieee80211_channel hwsim_channels_5ghz[] = { |
| 174 | CHAN5G(5180), /* Channel 36 */ |
| 175 | CHAN5G(5200), /* Channel 40 */ |
| 176 | CHAN5G(5220), /* Channel 44 */ |
| 177 | CHAN5G(5240), /* Channel 48 */ |
| 178 | |
| 179 | CHAN5G(5260), /* Channel 52 */ |
| 180 | CHAN5G(5280), /* Channel 56 */ |
| 181 | CHAN5G(5300), /* Channel 60 */ |
| 182 | CHAN5G(5320), /* Channel 64 */ |
| 183 | |
| 184 | CHAN5G(5500), /* Channel 100 */ |
| 185 | CHAN5G(5520), /* Channel 104 */ |
| 186 | CHAN5G(5540), /* Channel 108 */ |
| 187 | CHAN5G(5560), /* Channel 112 */ |
| 188 | CHAN5G(5580), /* Channel 116 */ |
| 189 | CHAN5G(5600), /* Channel 120 */ |
| 190 | CHAN5G(5620), /* Channel 124 */ |
| 191 | CHAN5G(5640), /* Channel 128 */ |
| 192 | CHAN5G(5660), /* Channel 132 */ |
| 193 | CHAN5G(5680), /* Channel 136 */ |
| 194 | CHAN5G(5700), /* Channel 140 */ |
| 195 | |
| 196 | CHAN5G(5745), /* Channel 149 */ |
| 197 | CHAN5G(5765), /* Channel 153 */ |
| 198 | CHAN5G(5785), /* Channel 157 */ |
| 199 | CHAN5G(5805), /* Channel 161 */ |
| 200 | CHAN5G(5825), /* Channel 165 */ |
| 201 | }; |
| 202 | |
| 203 | static const struct ieee80211_rate hwsim_rates[] = { |
| 204 | { .bitrate = 10 }, |
| 205 | { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 206 | { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 207 | { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE }, |
| 208 | { .bitrate = 60 }, |
| 209 | { .bitrate = 90 }, |
| 210 | { .bitrate = 120 }, |
| 211 | { .bitrate = 180 }, |
| 212 | { .bitrate = 240 }, |
| 213 | { .bitrate = 360 }, |
| 214 | { .bitrate = 480 }, |
| 215 | { .bitrate = 540 } |
| 216 | }; |
| 217 | |
| 218 | static const struct ieee80211_iface_limit hwsim_if_limits[] = { |
| 219 | { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) }, |
| 220 | { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) | |
| 221 | BIT(NL80211_IFTYPE_P2P_CLIENT) | |
| 222 | #ifdef CONFIG_MAC80211_MESH |
| 223 | BIT(NL80211_IFTYPE_MESH_POINT) | |
| 224 | #endif |
| 225 | BIT(NL80211_IFTYPE_AP) | |
| 226 | BIT(NL80211_IFTYPE_P2P_GO) }, |
| 227 | { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) }, |
| 228 | }; |
| 229 | |
| 230 | static const struct ieee80211_iface_limit hwsim_if_dfs_limits[] = { |
| 231 | { .max = 8, .types = BIT(NL80211_IFTYPE_AP) }, |
| 232 | }; |
| 233 | |
| 234 | static const struct ieee80211_iface_combination hwsim_if_comb[] = { |
| 235 | { |
| 236 | .limits = hwsim_if_limits, |
| 237 | .n_limits = ARRAY_SIZE(hwsim_if_limits), |
| 238 | .max_interfaces = 2048, |
| 239 | .num_different_channels = 1, |
| 240 | }, |
| 241 | { |
| 242 | .limits = hwsim_if_dfs_limits, |
| 243 | .n_limits = ARRAY_SIZE(hwsim_if_dfs_limits), |
| 244 | .max_interfaces = 8, |
| 245 | .num_different_channels = 1, |
| 246 | .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) | |
| 247 | BIT(NL80211_CHAN_WIDTH_20) | |
| 248 | BIT(NL80211_CHAN_WIDTH_40) | |
| 249 | BIT(NL80211_CHAN_WIDTH_80) | |
| 250 | BIT(NL80211_CHAN_WIDTH_160), |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | static spinlock_t hwsim_radio_lock; |
| 255 | static struct list_head hwsim_radios; |
| 256 | static int hwsim_radio_idx; |
| 257 | |
| 258 | static struct platform_driver mac80211_hwsim_driver = { |
| 259 | .driver = { |
| 260 | .name = "mac80211_hwsim", |
| 261 | .owner = THIS_MODULE, |
| 262 | }, |
| 263 | }; |
| 264 | |
| 265 | struct mac80211_hwsim_data { |
| 266 | struct list_head list; |
| 267 | struct ieee80211_hw *hw; |
| 268 | struct device *dev; |
| 269 | struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS]; |
| 270 | struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)]; |
| 271 | struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)]; |
| 272 | struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)]; |
| 273 | struct ieee80211_iface_combination if_combination; |
| 274 | |
| 275 | struct mac_address addresses[2]; |
| 276 | int channels; |
| 277 | |
| 278 | struct ieee80211_channel *tmp_chan; |
| 279 | struct delayed_work roc_done; |
| 280 | struct delayed_work hw_scan; |
| 281 | struct cfg80211_scan_request *hw_scan_request; |
| 282 | struct ieee80211_vif *hw_scan_vif; |
| 283 | int scan_chan_idx; |
| 284 | |
| 285 | struct ieee80211_channel *channel; |
| 286 | u64 beacon_int /* beacon interval in us */; |
| 287 | unsigned int rx_filter; |
| 288 | bool started, idle, scanning; |
| 289 | struct mutex mutex; |
| 290 | struct tasklet_hrtimer beacon_timer; |
| 291 | enum ps_mode { |
| 292 | PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL |
| 293 | } ps; |
| 294 | bool ps_poll_pending; |
| 295 | struct dentry *debugfs; |
| 296 | |
| 297 | struct sk_buff_head pending; /* packets pending */ |
| 298 | /* |
| 299 | * Only radios in the same group can communicate together (the |
| 300 | * channel has to match too). Each bit represents a group. A |
| 301 | * radio can be in more then one group. |
| 302 | */ |
| 303 | u64 group; |
| 304 | |
| 305 | int power_level; |
| 306 | |
| 307 | /* difference between this hw's clock and the real clock, in usecs */ |
| 308 | s64 tsf_offset; |
| 309 | s64 bcn_delta; |
| 310 | /* absolute beacon transmission time. Used to cover up "tx" delay. */ |
| 311 | u64 abs_bcn_ts; |
| 312 | }; |
| 313 | |
| 314 | |
| 315 | struct hwsim_radiotap_hdr { |
| 316 | struct ieee80211_radiotap_header hdr; |
| 317 | __le64 rt_tsft; |
| 318 | u8 rt_flags; |
| 319 | u8 rt_rate; |
| 320 | __le16 rt_channel; |
| 321 | __le16 rt_chbitmask; |
| 322 | } __packed; |
| 323 | |
| 324 | struct hwsim_radiotap_ack_hdr { |
| 325 | struct ieee80211_radiotap_header hdr; |
| 326 | u8 rt_flags; |
| 327 | u8 pad; |
| 328 | __le16 rt_channel; |
| 329 | __le16 rt_chbitmask; |
| 330 | } __packed; |
| 331 | |
| 332 | /* MAC80211_HWSIM netlinf family */ |
| 333 | static struct genl_family hwsim_genl_family = { |
| 334 | .id = GENL_ID_GENERATE, |
| 335 | .hdrsize = 0, |
| 336 | .name = "MAC80211_HWSIM", |
| 337 | .version = 1, |
| 338 | .maxattr = HWSIM_ATTR_MAX, |
| 339 | }; |
| 340 | |
| 341 | /* MAC80211_HWSIM netlink policy */ |
| 342 | |
| 343 | static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = { |
| 344 | [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC, .len = ETH_ALEN }, |
| 345 | [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC, .len = ETH_ALEN }, |
| 346 | [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY, |
| 347 | .len = IEEE80211_MAX_DATA_LEN }, |
| 348 | [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 }, |
| 349 | [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 }, |
| 350 | [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 }, |
| 351 | [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC, |
| 352 | .len = IEEE80211_TX_MAX_RATES * |
| 353 | sizeof(struct hwsim_tx_rate)}, |
| 354 | [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 }, |
| 355 | }; |
| 356 | |
| 357 | static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw, |
| 358 | struct sk_buff *skb, |
| 359 | struct ieee80211_channel *chan); |
| 360 | |
| 361 | /* sysfs attributes */ |
| 362 | static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif) |
| 363 | { |
| 364 | struct mac80211_hwsim_data *data = dat; |
| 365 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 366 | struct sk_buff *skb; |
| 367 | struct ieee80211_pspoll *pspoll; |
| 368 | |
| 369 | if (!vp->assoc) |
| 370 | return; |
| 371 | |
| 372 | wiphy_debug(data->hw->wiphy, |
| 373 | "%s: send PS-Poll to %pM for aid %d\n", |
| 374 | __func__, vp->bssid, vp->aid); |
| 375 | |
| 376 | skb = dev_alloc_skb(sizeof(*pspoll)); |
| 377 | if (!skb) |
| 378 | return; |
| 379 | pspoll = (void *) skb_put(skb, sizeof(*pspoll)); |
| 380 | pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | |
| 381 | IEEE80211_STYPE_PSPOLL | |
| 382 | IEEE80211_FCTL_PM); |
| 383 | pspoll->aid = cpu_to_le16(0xc000 | vp->aid); |
| 384 | memcpy(pspoll->bssid, vp->bssid, ETH_ALEN); |
| 385 | memcpy(pspoll->ta, mac, ETH_ALEN); |
| 386 | |
| 387 | rcu_read_lock(); |
| 388 | mac80211_hwsim_tx_frame(data->hw, skb, |
| 389 | rcu_dereference(vif->chanctx_conf)->def.chan); |
| 390 | rcu_read_unlock(); |
| 391 | } |
| 392 | |
| 393 | static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac, |
| 394 | struct ieee80211_vif *vif, int ps) |
| 395 | { |
| 396 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 397 | struct sk_buff *skb; |
| 398 | struct ieee80211_hdr *hdr; |
| 399 | |
| 400 | if (!vp->assoc) |
| 401 | return; |
| 402 | |
| 403 | wiphy_debug(data->hw->wiphy, |
| 404 | "%s: send data::nullfunc to %pM ps=%d\n", |
| 405 | __func__, vp->bssid, ps); |
| 406 | |
| 407 | skb = dev_alloc_skb(sizeof(*hdr)); |
| 408 | if (!skb) |
| 409 | return; |
| 410 | hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN); |
| 411 | hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA | |
| 412 | IEEE80211_STYPE_NULLFUNC | |
| 413 | (ps ? IEEE80211_FCTL_PM : 0)); |
| 414 | hdr->duration_id = cpu_to_le16(0); |
| 415 | memcpy(hdr->addr1, vp->bssid, ETH_ALEN); |
| 416 | memcpy(hdr->addr2, mac, ETH_ALEN); |
| 417 | memcpy(hdr->addr3, vp->bssid, ETH_ALEN); |
| 418 | |
| 419 | rcu_read_lock(); |
| 420 | mac80211_hwsim_tx_frame(data->hw, skb, |
| 421 | rcu_dereference(vif->chanctx_conf)->def.chan); |
| 422 | rcu_read_unlock(); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | static void hwsim_send_nullfunc_ps(void *dat, u8 *mac, |
| 427 | struct ieee80211_vif *vif) |
| 428 | { |
| 429 | struct mac80211_hwsim_data *data = dat; |
| 430 | hwsim_send_nullfunc(data, mac, vif, 1); |
| 431 | } |
| 432 | |
| 433 | static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac, |
| 434 | struct ieee80211_vif *vif) |
| 435 | { |
| 436 | struct mac80211_hwsim_data *data = dat; |
| 437 | hwsim_send_nullfunc(data, mac, vif, 0); |
| 438 | } |
| 439 | |
| 440 | static int hwsim_fops_ps_read(void *dat, u64 *val) |
| 441 | { |
| 442 | struct mac80211_hwsim_data *data = dat; |
| 443 | *val = data->ps; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int hwsim_fops_ps_write(void *dat, u64 val) |
| 448 | { |
| 449 | struct mac80211_hwsim_data *data = dat; |
| 450 | enum ps_mode old_ps; |
| 451 | |
| 452 | if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL && |
| 453 | val != PS_MANUAL_POLL) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | old_ps = data->ps; |
| 457 | data->ps = val; |
| 458 | |
| 459 | if (val == PS_MANUAL_POLL) { |
| 460 | ieee80211_iterate_active_interfaces(data->hw, |
| 461 | IEEE80211_IFACE_ITER_NORMAL, |
| 462 | hwsim_send_ps_poll, data); |
| 463 | data->ps_poll_pending = true; |
| 464 | } else if (old_ps == PS_DISABLED && val != PS_DISABLED) { |
| 465 | ieee80211_iterate_active_interfaces(data->hw, |
| 466 | IEEE80211_IFACE_ITER_NORMAL, |
| 467 | hwsim_send_nullfunc_ps, |
| 468 | data); |
| 469 | } else if (old_ps != PS_DISABLED && val == PS_DISABLED) { |
| 470 | ieee80211_iterate_active_interfaces(data->hw, |
| 471 | IEEE80211_IFACE_ITER_NORMAL, |
| 472 | hwsim_send_nullfunc_no_ps, |
| 473 | data); |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write, |
| 480 | "%llu\n"); |
| 481 | |
| 482 | static int hwsim_write_simulate_radar(void *dat, u64 val) |
| 483 | { |
| 484 | struct mac80211_hwsim_data *data = dat; |
| 485 | |
| 486 | ieee80211_radar_detected(data->hw); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | DEFINE_SIMPLE_ATTRIBUTE(hwsim_simulate_radar, NULL, |
| 492 | hwsim_write_simulate_radar, "%llu\n"); |
| 493 | |
| 494 | static int hwsim_fops_group_read(void *dat, u64 *val) |
| 495 | { |
| 496 | struct mac80211_hwsim_data *data = dat; |
| 497 | *val = data->group; |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int hwsim_fops_group_write(void *dat, u64 val) |
| 502 | { |
| 503 | struct mac80211_hwsim_data *data = dat; |
| 504 | data->group = val; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group, |
| 509 | hwsim_fops_group_read, hwsim_fops_group_write, |
| 510 | "%llx\n"); |
| 511 | |
| 512 | static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb, |
| 513 | struct net_device *dev) |
| 514 | { |
| 515 | /* TODO: allow packet injection */ |
| 516 | dev_kfree_skb(skb); |
| 517 | return NETDEV_TX_OK; |
| 518 | } |
| 519 | |
| 520 | static inline u64 mac80211_hwsim_get_tsf_raw(void) |
| 521 | { |
| 522 | return ktime_to_us(ktime_get_real()); |
| 523 | } |
| 524 | |
| 525 | static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data) |
| 526 | { |
| 527 | u64 now = mac80211_hwsim_get_tsf_raw(); |
| 528 | return cpu_to_le64(now + data->tsf_offset); |
| 529 | } |
| 530 | |
| 531 | static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw, |
| 532 | struct ieee80211_vif *vif) |
| 533 | { |
| 534 | struct mac80211_hwsim_data *data = hw->priv; |
| 535 | return le64_to_cpu(__mac80211_hwsim_get_tsf(data)); |
| 536 | } |
| 537 | |
| 538 | static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw, |
| 539 | struct ieee80211_vif *vif, u64 tsf) |
| 540 | { |
| 541 | struct mac80211_hwsim_data *data = hw->priv; |
| 542 | u64 now = mac80211_hwsim_get_tsf(hw, vif); |
| 543 | u32 bcn_int = data->beacon_int; |
| 544 | s64 delta = tsf - now; |
| 545 | |
| 546 | data->tsf_offset += delta; |
| 547 | /* adjust after beaconing with new timestamp at old TBTT */ |
| 548 | data->bcn_delta = do_div(delta, bcn_int); |
| 549 | } |
| 550 | |
| 551 | static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw, |
| 552 | struct sk_buff *tx_skb, |
| 553 | struct ieee80211_channel *chan) |
| 554 | { |
| 555 | struct mac80211_hwsim_data *data = hw->priv; |
| 556 | struct sk_buff *skb; |
| 557 | struct hwsim_radiotap_hdr *hdr; |
| 558 | u16 flags; |
| 559 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb); |
| 560 | struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info); |
| 561 | |
| 562 | if (!netif_running(hwsim_mon)) |
| 563 | return; |
| 564 | |
| 565 | skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC); |
| 566 | if (skb == NULL) |
| 567 | return; |
| 568 | |
| 569 | hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr)); |
| 570 | hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION; |
| 571 | hdr->hdr.it_pad = 0; |
| 572 | hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr)); |
| 573 | hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) | |
| 574 | (1 << IEEE80211_RADIOTAP_RATE) | |
| 575 | (1 << IEEE80211_RADIOTAP_TSFT) | |
| 576 | (1 << IEEE80211_RADIOTAP_CHANNEL)); |
| 577 | hdr->rt_tsft = __mac80211_hwsim_get_tsf(data); |
| 578 | hdr->rt_flags = 0; |
| 579 | hdr->rt_rate = txrate->bitrate / 5; |
| 580 | hdr->rt_channel = cpu_to_le16(chan->center_freq); |
| 581 | flags = IEEE80211_CHAN_2GHZ; |
| 582 | if (txrate->flags & IEEE80211_RATE_ERP_G) |
| 583 | flags |= IEEE80211_CHAN_OFDM; |
| 584 | else |
| 585 | flags |= IEEE80211_CHAN_CCK; |
| 586 | hdr->rt_chbitmask = cpu_to_le16(flags); |
| 587 | |
| 588 | skb->dev = hwsim_mon; |
| 589 | skb_set_mac_header(skb, 0); |
| 590 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 591 | skb->pkt_type = PACKET_OTHERHOST; |
| 592 | skb->protocol = htons(ETH_P_802_2); |
| 593 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 594 | netif_rx(skb); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan, |
| 599 | const u8 *addr) |
| 600 | { |
| 601 | struct sk_buff *skb; |
| 602 | struct hwsim_radiotap_ack_hdr *hdr; |
| 603 | u16 flags; |
| 604 | struct ieee80211_hdr *hdr11; |
| 605 | |
| 606 | if (!netif_running(hwsim_mon)) |
| 607 | return; |
| 608 | |
| 609 | skb = dev_alloc_skb(100); |
| 610 | if (skb == NULL) |
| 611 | return; |
| 612 | |
| 613 | hdr = (struct hwsim_radiotap_ack_hdr *) skb_put(skb, sizeof(*hdr)); |
| 614 | hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION; |
| 615 | hdr->hdr.it_pad = 0; |
| 616 | hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr)); |
| 617 | hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) | |
| 618 | (1 << IEEE80211_RADIOTAP_CHANNEL)); |
| 619 | hdr->rt_flags = 0; |
| 620 | hdr->pad = 0; |
| 621 | hdr->rt_channel = cpu_to_le16(chan->center_freq); |
| 622 | flags = IEEE80211_CHAN_2GHZ; |
| 623 | hdr->rt_chbitmask = cpu_to_le16(flags); |
| 624 | |
| 625 | hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10); |
| 626 | hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | |
| 627 | IEEE80211_STYPE_ACK); |
| 628 | hdr11->duration_id = cpu_to_le16(0); |
| 629 | memcpy(hdr11->addr1, addr, ETH_ALEN); |
| 630 | |
| 631 | skb->dev = hwsim_mon; |
| 632 | skb_set_mac_header(skb, 0); |
| 633 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 634 | skb->pkt_type = PACKET_OTHERHOST; |
| 635 | skb->protocol = htons(ETH_P_802_2); |
| 636 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 637 | netif_rx(skb); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data, |
| 642 | struct sk_buff *skb) |
| 643 | { |
| 644 | switch (data->ps) { |
| 645 | case PS_DISABLED: |
| 646 | return true; |
| 647 | case PS_ENABLED: |
| 648 | return false; |
| 649 | case PS_AUTO_POLL: |
| 650 | /* TODO: accept (some) Beacons by default and other frames only |
| 651 | * if pending PS-Poll has been sent */ |
| 652 | return true; |
| 653 | case PS_MANUAL_POLL: |
| 654 | /* Allow unicast frames to own address if there is a pending |
| 655 | * PS-Poll */ |
| 656 | if (data->ps_poll_pending && |
| 657 | memcmp(data->hw->wiphy->perm_addr, skb->data + 4, |
| 658 | ETH_ALEN) == 0) { |
| 659 | data->ps_poll_pending = false; |
| 660 | return true; |
| 661 | } |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | |
| 669 | struct mac80211_hwsim_addr_match_data { |
| 670 | bool ret; |
| 671 | const u8 *addr; |
| 672 | }; |
| 673 | |
| 674 | static void mac80211_hwsim_addr_iter(void *data, u8 *mac, |
| 675 | struct ieee80211_vif *vif) |
| 676 | { |
| 677 | struct mac80211_hwsim_addr_match_data *md = data; |
| 678 | if (memcmp(mac, md->addr, ETH_ALEN) == 0) |
| 679 | md->ret = true; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data, |
| 684 | const u8 *addr) |
| 685 | { |
| 686 | struct mac80211_hwsim_addr_match_data md; |
| 687 | |
| 688 | if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0) |
| 689 | return true; |
| 690 | |
| 691 | md.ret = false; |
| 692 | md.addr = addr; |
| 693 | ieee80211_iterate_active_interfaces_atomic(data->hw, |
| 694 | IEEE80211_IFACE_ITER_NORMAL, |
| 695 | mac80211_hwsim_addr_iter, |
| 696 | &md); |
| 697 | |
| 698 | return md.ret; |
| 699 | } |
| 700 | |
| 701 | static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw, |
| 702 | struct sk_buff *my_skb, |
| 703 | int dst_portid) |
| 704 | { |
| 705 | struct sk_buff *skb; |
| 706 | struct mac80211_hwsim_data *data = hw->priv; |
| 707 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data; |
| 708 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb); |
| 709 | void *msg_head; |
| 710 | unsigned int hwsim_flags = 0; |
| 711 | int i; |
| 712 | struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES]; |
| 713 | |
| 714 | if (data->ps != PS_DISABLED) |
| 715 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); |
| 716 | /* If the queue contains MAX_QUEUE skb's drop some */ |
| 717 | if (skb_queue_len(&data->pending) >= MAX_QUEUE) { |
| 718 | /* Droping until WARN_QUEUE level */ |
| 719 | while (skb_queue_len(&data->pending) >= WARN_QUEUE) |
| 720 | skb_dequeue(&data->pending); |
| 721 | } |
| 722 | |
| 723 | skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 724 | if (skb == NULL) |
| 725 | goto nla_put_failure; |
| 726 | |
| 727 | msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0, |
| 728 | HWSIM_CMD_FRAME); |
| 729 | if (msg_head == NULL) { |
| 730 | printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n"); |
| 731 | goto nla_put_failure; |
| 732 | } |
| 733 | |
| 734 | if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER, |
| 735 | ETH_ALEN, data->addresses[1].addr)) |
| 736 | goto nla_put_failure; |
| 737 | |
| 738 | /* We get the skb->data */ |
| 739 | if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data)) |
| 740 | goto nla_put_failure; |
| 741 | |
| 742 | /* We get the flags for this transmission, and we translate them to |
| 743 | wmediumd flags */ |
| 744 | |
| 745 | if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) |
| 746 | hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS; |
| 747 | |
| 748 | if (info->flags & IEEE80211_TX_CTL_NO_ACK) |
| 749 | hwsim_flags |= HWSIM_TX_CTL_NO_ACK; |
| 750 | |
| 751 | if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags)) |
| 752 | goto nla_put_failure; |
| 753 | |
| 754 | /* We get the tx control (rate and retries) info*/ |
| 755 | |
| 756 | for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { |
| 757 | tx_attempts[i].idx = info->status.rates[i].idx; |
| 758 | tx_attempts[i].count = info->status.rates[i].count; |
| 759 | } |
| 760 | |
| 761 | if (nla_put(skb, HWSIM_ATTR_TX_INFO, |
| 762 | sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES, |
| 763 | tx_attempts)) |
| 764 | goto nla_put_failure; |
| 765 | |
| 766 | /* We create a cookie to identify this skb */ |
| 767 | if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb)) |
| 768 | goto nla_put_failure; |
| 769 | |
| 770 | genlmsg_end(skb, msg_head); |
| 771 | genlmsg_unicast(&init_net, skb, dst_portid); |
| 772 | |
| 773 | /* Enqueue the packet */ |
| 774 | skb_queue_tail(&data->pending, my_skb); |
| 775 | return; |
| 776 | |
| 777 | nla_put_failure: |
| 778 | printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__); |
| 779 | } |
| 780 | |
| 781 | static bool hwsim_chans_compat(struct ieee80211_channel *c1, |
| 782 | struct ieee80211_channel *c2) |
| 783 | { |
| 784 | if (!c1 || !c2) |
| 785 | return false; |
| 786 | |
| 787 | return c1->center_freq == c2->center_freq; |
| 788 | } |
| 789 | |
| 790 | struct tx_iter_data { |
| 791 | struct ieee80211_channel *channel; |
| 792 | bool receive; |
| 793 | }; |
| 794 | |
| 795 | static void mac80211_hwsim_tx_iter(void *_data, u8 *addr, |
| 796 | struct ieee80211_vif *vif) |
| 797 | { |
| 798 | struct tx_iter_data *data = _data; |
| 799 | |
| 800 | if (!vif->chanctx_conf) |
| 801 | return; |
| 802 | |
| 803 | if (!hwsim_chans_compat(data->channel, |
| 804 | rcu_dereference(vif->chanctx_conf)->def.chan)) |
| 805 | return; |
| 806 | |
| 807 | data->receive = true; |
| 808 | } |
| 809 | |
| 810 | static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw, |
| 811 | struct sk_buff *skb, |
| 812 | struct ieee80211_channel *chan) |
| 813 | { |
| 814 | struct mac80211_hwsim_data *data = hw->priv, *data2; |
| 815 | bool ack = false; |
| 816 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 817 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 818 | struct ieee80211_rx_status rx_status; |
| 819 | u64 now; |
| 820 | |
| 821 | memset(&rx_status, 0, sizeof(rx_status)); |
| 822 | rx_status.flag |= RX_FLAG_MACTIME_START; |
| 823 | rx_status.freq = chan->center_freq; |
| 824 | rx_status.band = chan->band; |
| 825 | if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) { |
| 826 | rx_status.rate_idx = |
| 827 | ieee80211_rate_get_vht_mcs(&info->control.rates[0]); |
| 828 | rx_status.vht_nss = |
| 829 | ieee80211_rate_get_vht_nss(&info->control.rates[0]); |
| 830 | rx_status.flag |= RX_FLAG_VHT; |
| 831 | } else { |
| 832 | rx_status.rate_idx = info->control.rates[0].idx; |
| 833 | if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS) |
| 834 | rx_status.flag |= RX_FLAG_HT; |
| 835 | } |
| 836 | if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) |
| 837 | rx_status.flag |= RX_FLAG_40MHZ; |
| 838 | if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI) |
| 839 | rx_status.flag |= RX_FLAG_SHORT_GI; |
| 840 | /* TODO: simulate real signal strength (and optional packet loss) */ |
| 841 | rx_status.signal = data->power_level - 50; |
| 842 | |
| 843 | if (data->ps != PS_DISABLED) |
| 844 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); |
| 845 | |
| 846 | /* release the skb's source info */ |
| 847 | skb_orphan(skb); |
| 848 | skb_dst_drop(skb); |
| 849 | skb->mark = 0; |
| 850 | secpath_reset(skb); |
| 851 | nf_reset(skb); |
| 852 | |
| 853 | /* |
| 854 | * Get absolute mactime here so all HWs RX at the "same time", and |
| 855 | * absolute TX time for beacon mactime so the timestamp matches. |
| 856 | * Giving beacons a different mactime than non-beacons looks messy, but |
| 857 | * it helps the Toffset be exact and a ~10us mactime discrepancy |
| 858 | * probably doesn't really matter. |
| 859 | */ |
| 860 | if (ieee80211_is_beacon(hdr->frame_control) || |
| 861 | ieee80211_is_probe_resp(hdr->frame_control)) |
| 862 | now = data->abs_bcn_ts; |
| 863 | else |
| 864 | now = mac80211_hwsim_get_tsf_raw(); |
| 865 | |
| 866 | /* Copy skb to all enabled radios that are on the current frequency */ |
| 867 | spin_lock(&hwsim_radio_lock); |
| 868 | list_for_each_entry(data2, &hwsim_radios, list) { |
| 869 | struct sk_buff *nskb; |
| 870 | struct tx_iter_data tx_iter_data = { |
| 871 | .receive = false, |
| 872 | .channel = chan, |
| 873 | }; |
| 874 | |
| 875 | if (data == data2) |
| 876 | continue; |
| 877 | |
| 878 | if (!data2->started || (data2->idle && !data2->tmp_chan) || |
| 879 | !hwsim_ps_rx_ok(data2, skb)) |
| 880 | continue; |
| 881 | |
| 882 | if (!(data->group & data2->group)) |
| 883 | continue; |
| 884 | |
| 885 | if (!hwsim_chans_compat(chan, data2->tmp_chan) && |
| 886 | !hwsim_chans_compat(chan, data2->channel)) { |
| 887 | ieee80211_iterate_active_interfaces_atomic( |
| 888 | data2->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 889 | mac80211_hwsim_tx_iter, &tx_iter_data); |
| 890 | if (!tx_iter_data.receive) |
| 891 | continue; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * reserve some space for our vendor and the normal |
| 896 | * radiotap header, since we're copying anyway |
| 897 | */ |
| 898 | if (skb->len < PAGE_SIZE && paged_rx) { |
| 899 | struct page *page = alloc_page(GFP_ATOMIC); |
| 900 | |
| 901 | if (!page) |
| 902 | continue; |
| 903 | |
| 904 | nskb = dev_alloc_skb(128); |
| 905 | if (!nskb) { |
| 906 | __free_page(page); |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | memcpy(page_address(page), skb->data, skb->len); |
| 911 | skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len); |
| 912 | } else { |
| 913 | nskb = skb_copy(skb, GFP_ATOMIC); |
| 914 | if (!nskb) |
| 915 | continue; |
| 916 | } |
| 917 | |
| 918 | if (mac80211_hwsim_addr_match(data2, hdr->addr1)) |
| 919 | ack = true; |
| 920 | |
| 921 | rx_status.mactime = now + data2->tsf_offset; |
| 922 | #if 0 |
| 923 | /* |
| 924 | * Don't enable this code by default as the OUI 00:00:00 |
| 925 | * is registered to Xerox so we shouldn't use it here, it |
| 926 | * might find its way into pcap files. |
| 927 | * Note that this code requires the headroom in the SKB |
| 928 | * that was allocated earlier. |
| 929 | */ |
| 930 | rx_status.vendor_radiotap_oui[0] = 0x00; |
| 931 | rx_status.vendor_radiotap_oui[1] = 0x00; |
| 932 | rx_status.vendor_radiotap_oui[2] = 0x00; |
| 933 | rx_status.vendor_radiotap_subns = 127; |
| 934 | /* |
| 935 | * Radiotap vendor namespaces can (and should) also be |
| 936 | * split into fields by using the standard radiotap |
| 937 | * presence bitmap mechanism. Use just BIT(0) here for |
| 938 | * the presence bitmap. |
| 939 | */ |
| 940 | rx_status.vendor_radiotap_bitmap = BIT(0); |
| 941 | /* We have 8 bytes of (dummy) data */ |
| 942 | rx_status.vendor_radiotap_len = 8; |
| 943 | /* For testing, also require it to be aligned */ |
| 944 | rx_status.vendor_radiotap_align = 8; |
| 945 | /* push the data */ |
| 946 | memcpy(skb_push(nskb, 8), "ABCDEFGH", 8); |
| 947 | #endif |
| 948 | |
| 949 | memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status)); |
| 950 | ieee80211_rx_irqsafe(data2->hw, nskb); |
| 951 | } |
| 952 | spin_unlock(&hwsim_radio_lock); |
| 953 | |
| 954 | return ack; |
| 955 | } |
| 956 | |
| 957 | static void mac80211_hwsim_tx(struct ieee80211_hw *hw, |
| 958 | struct ieee80211_tx_control *control, |
| 959 | struct sk_buff *skb) |
| 960 | { |
| 961 | struct mac80211_hwsim_data *data = hw->priv; |
| 962 | struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb); |
| 963 | struct ieee80211_chanctx_conf *chanctx_conf; |
| 964 | struct ieee80211_channel *channel; |
| 965 | bool ack; |
| 966 | u32 _portid; |
| 967 | |
| 968 | if (WARN_ON(skb->len < 10)) { |
| 969 | /* Should not happen; just a sanity check for addr1 use */ |
| 970 | ieee80211_free_txskb(hw, skb); |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | if (data->channels == 1) { |
| 975 | channel = data->channel; |
| 976 | } else if (txi->hw_queue == 4) { |
| 977 | channel = data->tmp_chan; |
| 978 | } else { |
| 979 | chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf); |
| 980 | if (chanctx_conf) |
| 981 | channel = chanctx_conf->def.chan; |
| 982 | else |
| 983 | channel = NULL; |
| 984 | } |
| 985 | |
| 986 | if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) { |
| 987 | ieee80211_free_txskb(hw, skb); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | if (data->idle && !data->tmp_chan) { |
| 992 | wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n"); |
| 993 | ieee80211_free_txskb(hw, skb); |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | if (txi->control.vif) |
| 998 | hwsim_check_magic(txi->control.vif); |
| 999 | if (control->sta) |
| 1000 | hwsim_check_sta_magic(control->sta); |
| 1001 | |
| 1002 | if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE) |
| 1003 | ieee80211_get_tx_rates(txi->control.vif, control->sta, skb, |
| 1004 | txi->control.rates, |
| 1005 | ARRAY_SIZE(txi->control.rates)); |
| 1006 | |
| 1007 | txi->rate_driver_data[0] = channel; |
| 1008 | mac80211_hwsim_monitor_rx(hw, skb, channel); |
| 1009 | |
| 1010 | /* wmediumd mode check */ |
| 1011 | _portid = ACCESS_ONCE(wmediumd_portid); |
| 1012 | |
| 1013 | if (_portid) |
| 1014 | return mac80211_hwsim_tx_frame_nl(hw, skb, _portid); |
| 1015 | |
| 1016 | /* NO wmediumd detected, perfect medium simulation */ |
| 1017 | ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel); |
| 1018 | |
| 1019 | if (ack && skb->len >= 16) { |
| 1020 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 1021 | mac80211_hwsim_monitor_ack(channel, hdr->addr2); |
| 1022 | } |
| 1023 | |
| 1024 | ieee80211_tx_info_clear_status(txi); |
| 1025 | |
| 1026 | /* frame was transmitted at most favorable rate at first attempt */ |
| 1027 | txi->control.rates[0].count = 1; |
| 1028 | txi->control.rates[1].idx = -1; |
| 1029 | |
| 1030 | if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack) |
| 1031 | txi->flags |= IEEE80211_TX_STAT_ACK; |
| 1032 | ieee80211_tx_status_irqsafe(hw, skb); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | static int mac80211_hwsim_start(struct ieee80211_hw *hw) |
| 1037 | { |
| 1038 | struct mac80211_hwsim_data *data = hw->priv; |
| 1039 | wiphy_debug(hw->wiphy, "%s\n", __func__); |
| 1040 | data->started = true; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | static void mac80211_hwsim_stop(struct ieee80211_hw *hw) |
| 1046 | { |
| 1047 | struct mac80211_hwsim_data *data = hw->priv; |
| 1048 | data->started = false; |
| 1049 | tasklet_hrtimer_cancel(&data->beacon_timer); |
| 1050 | wiphy_debug(hw->wiphy, "%s\n", __func__); |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw, |
| 1055 | struct ieee80211_vif *vif) |
| 1056 | { |
| 1057 | wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n", |
| 1058 | __func__, ieee80211_vif_type_p2p(vif), |
| 1059 | vif->addr); |
| 1060 | hwsim_set_magic(vif); |
| 1061 | |
| 1062 | vif->cab_queue = 0; |
| 1063 | vif->hw_queue[IEEE80211_AC_VO] = 0; |
| 1064 | vif->hw_queue[IEEE80211_AC_VI] = 1; |
| 1065 | vif->hw_queue[IEEE80211_AC_BE] = 2; |
| 1066 | vif->hw_queue[IEEE80211_AC_BK] = 3; |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw, |
| 1073 | struct ieee80211_vif *vif, |
| 1074 | enum nl80211_iftype newtype, |
| 1075 | bool newp2p) |
| 1076 | { |
| 1077 | newtype = ieee80211_iftype_p2p(newtype, newp2p); |
| 1078 | wiphy_debug(hw->wiphy, |
| 1079 | "%s (old type=%d, new type=%d, mac_addr=%pM)\n", |
| 1080 | __func__, ieee80211_vif_type_p2p(vif), |
| 1081 | newtype, vif->addr); |
| 1082 | hwsim_check_magic(vif); |
| 1083 | |
| 1084 | /* |
| 1085 | * interface may change from non-AP to AP in |
| 1086 | * which case this needs to be set up again |
| 1087 | */ |
| 1088 | vif->cab_queue = 0; |
| 1089 | |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | static void mac80211_hwsim_remove_interface( |
| 1094 | struct ieee80211_hw *hw, struct ieee80211_vif *vif) |
| 1095 | { |
| 1096 | wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n", |
| 1097 | __func__, ieee80211_vif_type_p2p(vif), |
| 1098 | vif->addr); |
| 1099 | hwsim_check_magic(vif); |
| 1100 | hwsim_clear_magic(vif); |
| 1101 | } |
| 1102 | |
| 1103 | static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw, |
| 1104 | struct sk_buff *skb, |
| 1105 | struct ieee80211_channel *chan) |
| 1106 | { |
| 1107 | u32 _pid = ACCESS_ONCE(wmediumd_portid); |
| 1108 | |
| 1109 | if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE) { |
| 1110 | struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb); |
| 1111 | ieee80211_get_tx_rates(txi->control.vif, NULL, skb, |
| 1112 | txi->control.rates, |
| 1113 | ARRAY_SIZE(txi->control.rates)); |
| 1114 | } |
| 1115 | |
| 1116 | mac80211_hwsim_monitor_rx(hw, skb, chan); |
| 1117 | |
| 1118 | if (_pid) |
| 1119 | return mac80211_hwsim_tx_frame_nl(hw, skb, _pid); |
| 1120 | |
| 1121 | mac80211_hwsim_tx_frame_no_nl(hw, skb, chan); |
| 1122 | dev_kfree_skb(skb); |
| 1123 | } |
| 1124 | |
| 1125 | static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac, |
| 1126 | struct ieee80211_vif *vif) |
| 1127 | { |
| 1128 | struct mac80211_hwsim_data *data = arg; |
| 1129 | struct ieee80211_hw *hw = data->hw; |
| 1130 | struct ieee80211_tx_info *info; |
| 1131 | struct ieee80211_rate *txrate; |
| 1132 | struct ieee80211_mgmt *mgmt; |
| 1133 | struct sk_buff *skb; |
| 1134 | |
| 1135 | hwsim_check_magic(vif); |
| 1136 | |
| 1137 | if (vif->type != NL80211_IFTYPE_AP && |
| 1138 | vif->type != NL80211_IFTYPE_MESH_POINT && |
| 1139 | vif->type != NL80211_IFTYPE_ADHOC) |
| 1140 | return; |
| 1141 | |
| 1142 | skb = ieee80211_beacon_get(hw, vif); |
| 1143 | if (skb == NULL) |
| 1144 | return; |
| 1145 | info = IEEE80211_SKB_CB(skb); |
| 1146 | if (hw->flags & IEEE80211_HW_SUPPORTS_RC_TABLE) |
| 1147 | ieee80211_get_tx_rates(vif, NULL, skb, |
| 1148 | info->control.rates, |
| 1149 | ARRAY_SIZE(info->control.rates)); |
| 1150 | |
| 1151 | txrate = ieee80211_get_tx_rate(hw, info); |
| 1152 | |
| 1153 | mgmt = (struct ieee80211_mgmt *) skb->data; |
| 1154 | /* fake header transmission time */ |
| 1155 | data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw(); |
| 1156 | mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts + |
| 1157 | data->tsf_offset + |
| 1158 | 24 * 8 * 10 / txrate->bitrate); |
| 1159 | |
| 1160 | mac80211_hwsim_tx_frame(hw, skb, |
| 1161 | rcu_dereference(vif->chanctx_conf)->def.chan); |
| 1162 | } |
| 1163 | |
| 1164 | static enum hrtimer_restart |
| 1165 | mac80211_hwsim_beacon(struct hrtimer *timer) |
| 1166 | { |
| 1167 | struct mac80211_hwsim_data *data = |
| 1168 | container_of(timer, struct mac80211_hwsim_data, |
| 1169 | beacon_timer.timer); |
| 1170 | struct ieee80211_hw *hw = data->hw; |
| 1171 | u64 bcn_int = data->beacon_int; |
| 1172 | ktime_t next_bcn; |
| 1173 | |
| 1174 | if (!data->started) |
| 1175 | goto out; |
| 1176 | |
| 1177 | ieee80211_iterate_active_interfaces_atomic( |
| 1178 | hw, IEEE80211_IFACE_ITER_NORMAL, |
| 1179 | mac80211_hwsim_beacon_tx, data); |
| 1180 | |
| 1181 | /* beacon at new TBTT + beacon interval */ |
| 1182 | if (data->bcn_delta) { |
| 1183 | bcn_int -= data->bcn_delta; |
| 1184 | data->bcn_delta = 0; |
| 1185 | } |
| 1186 | |
| 1187 | next_bcn = ktime_add(hrtimer_get_expires(timer), |
| 1188 | ns_to_ktime(bcn_int * 1000)); |
| 1189 | tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS); |
| 1190 | out: |
| 1191 | return HRTIMER_NORESTART; |
| 1192 | } |
| 1193 | |
| 1194 | static const char * const hwsim_chanwidths[] = { |
| 1195 | [NL80211_CHAN_WIDTH_20_NOHT] = "noht", |
| 1196 | [NL80211_CHAN_WIDTH_20] = "ht20", |
| 1197 | [NL80211_CHAN_WIDTH_40] = "ht40", |
| 1198 | [NL80211_CHAN_WIDTH_80] = "vht80", |
| 1199 | [NL80211_CHAN_WIDTH_80P80] = "vht80p80", |
| 1200 | [NL80211_CHAN_WIDTH_160] = "vht160", |
| 1201 | }; |
| 1202 | |
| 1203 | static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed) |
| 1204 | { |
| 1205 | struct mac80211_hwsim_data *data = hw->priv; |
| 1206 | struct ieee80211_conf *conf = &hw->conf; |
| 1207 | static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = { |
| 1208 | [IEEE80211_SMPS_AUTOMATIC] = "auto", |
| 1209 | [IEEE80211_SMPS_OFF] = "off", |
| 1210 | [IEEE80211_SMPS_STATIC] = "static", |
| 1211 | [IEEE80211_SMPS_DYNAMIC] = "dynamic", |
| 1212 | }; |
| 1213 | |
| 1214 | if (conf->chandef.chan) |
| 1215 | wiphy_debug(hw->wiphy, |
| 1216 | "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n", |
| 1217 | __func__, |
| 1218 | conf->chandef.chan->center_freq, |
| 1219 | conf->chandef.center_freq1, |
| 1220 | conf->chandef.center_freq2, |
| 1221 | hwsim_chanwidths[conf->chandef.width], |
| 1222 | !!(conf->flags & IEEE80211_CONF_IDLE), |
| 1223 | !!(conf->flags & IEEE80211_CONF_PS), |
| 1224 | smps_modes[conf->smps_mode]); |
| 1225 | else |
| 1226 | wiphy_debug(hw->wiphy, |
| 1227 | "%s (freq=0 idle=%d ps=%d smps=%s)\n", |
| 1228 | __func__, |
| 1229 | !!(conf->flags & IEEE80211_CONF_IDLE), |
| 1230 | !!(conf->flags & IEEE80211_CONF_PS), |
| 1231 | smps_modes[conf->smps_mode]); |
| 1232 | |
| 1233 | data->idle = !!(conf->flags & IEEE80211_CONF_IDLE); |
| 1234 | |
| 1235 | data->channel = conf->chandef.chan; |
| 1236 | |
| 1237 | WARN_ON(data->channel && data->channels > 1); |
| 1238 | |
| 1239 | data->power_level = conf->power_level; |
| 1240 | if (!data->started || !data->beacon_int) |
| 1241 | tasklet_hrtimer_cancel(&data->beacon_timer); |
| 1242 | else if (!hrtimer_is_queued(&data->beacon_timer.timer)) { |
| 1243 | u64 tsf = mac80211_hwsim_get_tsf(hw, NULL); |
| 1244 | u32 bcn_int = data->beacon_int; |
| 1245 | u64 until_tbtt = bcn_int - do_div(tsf, bcn_int); |
| 1246 | |
| 1247 | tasklet_hrtimer_start(&data->beacon_timer, |
| 1248 | ns_to_ktime(until_tbtt * 1000), |
| 1249 | HRTIMER_MODE_REL); |
| 1250 | } |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw, |
| 1257 | unsigned int changed_flags, |
| 1258 | unsigned int *total_flags,u64 multicast) |
| 1259 | { |
| 1260 | struct mac80211_hwsim_data *data = hw->priv; |
| 1261 | |
| 1262 | wiphy_debug(hw->wiphy, "%s\n", __func__); |
| 1263 | |
| 1264 | data->rx_filter = 0; |
| 1265 | if (*total_flags & FIF_PROMISC_IN_BSS) |
| 1266 | data->rx_filter |= FIF_PROMISC_IN_BSS; |
| 1267 | if (*total_flags & FIF_ALLMULTI) |
| 1268 | data->rx_filter |= FIF_ALLMULTI; |
| 1269 | |
| 1270 | *total_flags = data->rx_filter; |
| 1271 | } |
| 1272 | |
| 1273 | static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac, |
| 1274 | struct ieee80211_vif *vif) |
| 1275 | { |
| 1276 | unsigned int *count = data; |
| 1277 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 1278 | |
| 1279 | if (vp->bcn_en) |
| 1280 | (*count)++; |
| 1281 | } |
| 1282 | |
| 1283 | static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw, |
| 1284 | struct ieee80211_vif *vif, |
| 1285 | struct ieee80211_bss_conf *info, |
| 1286 | u32 changed) |
| 1287 | { |
| 1288 | struct hwsim_vif_priv *vp = (void *)vif->drv_priv; |
| 1289 | struct mac80211_hwsim_data *data = hw->priv; |
| 1290 | |
| 1291 | hwsim_check_magic(vif); |
| 1292 | |
| 1293 | wiphy_debug(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n", |
| 1294 | __func__, changed, vif->addr); |
| 1295 | |
| 1296 | if (changed & BSS_CHANGED_BSSID) { |
| 1297 | wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n", |
| 1298 | __func__, info->bssid); |
| 1299 | memcpy(vp->bssid, info->bssid, ETH_ALEN); |
| 1300 | } |
| 1301 | |
| 1302 | if (changed & BSS_CHANGED_ASSOC) { |
| 1303 | wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n", |
| 1304 | info->assoc, info->aid); |
| 1305 | vp->assoc = info->assoc; |
| 1306 | vp->aid = info->aid; |
| 1307 | } |
| 1308 | |
| 1309 | if (changed & BSS_CHANGED_BEACON_INT) { |
| 1310 | wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int); |
| 1311 | data->beacon_int = info->beacon_int * 1024; |
| 1312 | } |
| 1313 | |
| 1314 | if (changed & BSS_CHANGED_BEACON_ENABLED) { |
| 1315 | wiphy_debug(hw->wiphy, " BCN EN: %d\n", info->enable_beacon); |
| 1316 | vp->bcn_en = info->enable_beacon; |
| 1317 | if (data->started && |
| 1318 | !hrtimer_is_queued(&data->beacon_timer.timer) && |
| 1319 | info->enable_beacon) { |
| 1320 | u64 tsf, until_tbtt; |
| 1321 | u32 bcn_int; |
| 1322 | if (WARN_ON(!data->beacon_int)) |
| 1323 | data->beacon_int = 1000 * 1024; |
| 1324 | tsf = mac80211_hwsim_get_tsf(hw, vif); |
| 1325 | bcn_int = data->beacon_int; |
| 1326 | until_tbtt = bcn_int - do_div(tsf, bcn_int); |
| 1327 | tasklet_hrtimer_start(&data->beacon_timer, |
| 1328 | ns_to_ktime(until_tbtt * 1000), |
| 1329 | HRTIMER_MODE_REL); |
| 1330 | } else if (!info->enable_beacon) { |
| 1331 | unsigned int count = 0; |
| 1332 | ieee80211_iterate_active_interfaces_atomic( |
| 1333 | data->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 1334 | mac80211_hwsim_bcn_en_iter, &count); |
| 1335 | wiphy_debug(hw->wiphy, " beaconing vifs remaining: %u", |
| 1336 | count); |
| 1337 | if (count == 0) |
| 1338 | tasklet_hrtimer_cancel(&data->beacon_timer); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if (changed & BSS_CHANGED_ERP_CTS_PROT) { |
| 1343 | wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n", |
| 1344 | info->use_cts_prot); |
| 1345 | } |
| 1346 | |
| 1347 | if (changed & BSS_CHANGED_ERP_PREAMBLE) { |
| 1348 | wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n", |
| 1349 | info->use_short_preamble); |
| 1350 | } |
| 1351 | |
| 1352 | if (changed & BSS_CHANGED_ERP_SLOT) { |
| 1353 | wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot); |
| 1354 | } |
| 1355 | |
| 1356 | if (changed & BSS_CHANGED_HT) { |
| 1357 | wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n", |
| 1358 | info->ht_operation_mode); |
| 1359 | } |
| 1360 | |
| 1361 | if (changed & BSS_CHANGED_BASIC_RATES) { |
| 1362 | wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n", |
| 1363 | (unsigned long long) info->basic_rates); |
| 1364 | } |
| 1365 | |
| 1366 | if (changed & BSS_CHANGED_TXPOWER) |
| 1367 | wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower); |
| 1368 | } |
| 1369 | |
| 1370 | static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw, |
| 1371 | struct ieee80211_vif *vif, |
| 1372 | struct ieee80211_sta *sta) |
| 1373 | { |
| 1374 | hwsim_check_magic(vif); |
| 1375 | hwsim_set_sta_magic(sta); |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw, |
| 1381 | struct ieee80211_vif *vif, |
| 1382 | struct ieee80211_sta *sta) |
| 1383 | { |
| 1384 | hwsim_check_magic(vif); |
| 1385 | hwsim_clear_sta_magic(sta); |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw, |
| 1391 | struct ieee80211_vif *vif, |
| 1392 | enum sta_notify_cmd cmd, |
| 1393 | struct ieee80211_sta *sta) |
| 1394 | { |
| 1395 | hwsim_check_magic(vif); |
| 1396 | |
| 1397 | switch (cmd) { |
| 1398 | case STA_NOTIFY_SLEEP: |
| 1399 | case STA_NOTIFY_AWAKE: |
| 1400 | /* TODO: make good use of these flags */ |
| 1401 | break; |
| 1402 | default: |
| 1403 | WARN(1, "Invalid sta notify: %d\n", cmd); |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw, |
| 1409 | struct ieee80211_sta *sta, |
| 1410 | bool set) |
| 1411 | { |
| 1412 | hwsim_check_sta_magic(sta); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static int mac80211_hwsim_conf_tx( |
| 1417 | struct ieee80211_hw *hw, |
| 1418 | struct ieee80211_vif *vif, u16 queue, |
| 1419 | const struct ieee80211_tx_queue_params *params) |
| 1420 | { |
| 1421 | wiphy_debug(hw->wiphy, |
| 1422 | "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n", |
| 1423 | __func__, queue, |
| 1424 | params->txop, params->cw_min, |
| 1425 | params->cw_max, params->aifs); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | static int mac80211_hwsim_get_survey( |
| 1430 | struct ieee80211_hw *hw, int idx, |
| 1431 | struct survey_info *survey) |
| 1432 | { |
| 1433 | struct ieee80211_conf *conf = &hw->conf; |
| 1434 | |
| 1435 | wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx); |
| 1436 | |
| 1437 | if (idx != 0) |
| 1438 | return -ENOENT; |
| 1439 | |
| 1440 | /* Current channel */ |
| 1441 | survey->channel = conf->chandef.chan; |
| 1442 | |
| 1443 | /* |
| 1444 | * Magically conjured noise level --- this is only ok for simulated hardware. |
| 1445 | * |
| 1446 | * A real driver which cannot determine the real channel noise MUST NOT |
| 1447 | * report any noise, especially not a magically conjured one :-) |
| 1448 | */ |
| 1449 | survey->filled = SURVEY_INFO_NOISE_DBM; |
| 1450 | survey->noise = -92; |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | #ifdef CONFIG_NL80211_TESTMODE |
| 1456 | /* |
| 1457 | * This section contains example code for using netlink |
| 1458 | * attributes with the testmode command in nl80211. |
| 1459 | */ |
| 1460 | |
| 1461 | /* These enums need to be kept in sync with userspace */ |
| 1462 | enum hwsim_testmode_attr { |
| 1463 | __HWSIM_TM_ATTR_INVALID = 0, |
| 1464 | HWSIM_TM_ATTR_CMD = 1, |
| 1465 | HWSIM_TM_ATTR_PS = 2, |
| 1466 | |
| 1467 | /* keep last */ |
| 1468 | __HWSIM_TM_ATTR_AFTER_LAST, |
| 1469 | HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1 |
| 1470 | }; |
| 1471 | |
| 1472 | enum hwsim_testmode_cmd { |
| 1473 | HWSIM_TM_CMD_SET_PS = 0, |
| 1474 | HWSIM_TM_CMD_GET_PS = 1, |
| 1475 | HWSIM_TM_CMD_STOP_QUEUES = 2, |
| 1476 | HWSIM_TM_CMD_WAKE_QUEUES = 3, |
| 1477 | }; |
| 1478 | |
| 1479 | static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = { |
| 1480 | [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 }, |
| 1481 | [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 }, |
| 1482 | }; |
| 1483 | |
| 1484 | static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw, |
| 1485 | struct ieee80211_vif *vif, |
| 1486 | void *data, int len) |
| 1487 | { |
| 1488 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1489 | struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1]; |
| 1490 | struct sk_buff *skb; |
| 1491 | int err, ps; |
| 1492 | |
| 1493 | err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len, |
| 1494 | hwsim_testmode_policy); |
| 1495 | if (err) |
| 1496 | return err; |
| 1497 | |
| 1498 | if (!tb[HWSIM_TM_ATTR_CMD]) |
| 1499 | return -EINVAL; |
| 1500 | |
| 1501 | switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) { |
| 1502 | case HWSIM_TM_CMD_SET_PS: |
| 1503 | if (!tb[HWSIM_TM_ATTR_PS]) |
| 1504 | return -EINVAL; |
| 1505 | ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]); |
| 1506 | return hwsim_fops_ps_write(hwsim, ps); |
| 1507 | case HWSIM_TM_CMD_GET_PS: |
| 1508 | skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, |
| 1509 | nla_total_size(sizeof(u32))); |
| 1510 | if (!skb) |
| 1511 | return -ENOMEM; |
| 1512 | if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps)) |
| 1513 | goto nla_put_failure; |
| 1514 | return cfg80211_testmode_reply(skb); |
| 1515 | case HWSIM_TM_CMD_STOP_QUEUES: |
| 1516 | ieee80211_stop_queues(hw); |
| 1517 | return 0; |
| 1518 | case HWSIM_TM_CMD_WAKE_QUEUES: |
| 1519 | ieee80211_wake_queues(hw); |
| 1520 | return 0; |
| 1521 | default: |
| 1522 | return -EOPNOTSUPP; |
| 1523 | } |
| 1524 | |
| 1525 | nla_put_failure: |
| 1526 | kfree_skb(skb); |
| 1527 | return -ENOBUFS; |
| 1528 | } |
| 1529 | #endif |
| 1530 | |
| 1531 | static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw, |
| 1532 | struct ieee80211_vif *vif, |
| 1533 | enum ieee80211_ampdu_mlme_action action, |
| 1534 | struct ieee80211_sta *sta, u16 tid, u16 *ssn, |
| 1535 | u8 buf_size) |
| 1536 | { |
| 1537 | switch (action) { |
| 1538 | case IEEE80211_AMPDU_TX_START: |
| 1539 | ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid); |
| 1540 | break; |
| 1541 | case IEEE80211_AMPDU_TX_STOP_CONT: |
| 1542 | case IEEE80211_AMPDU_TX_STOP_FLUSH: |
| 1543 | case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: |
| 1544 | ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); |
| 1545 | break; |
| 1546 | case IEEE80211_AMPDU_TX_OPERATIONAL: |
| 1547 | break; |
| 1548 | case IEEE80211_AMPDU_RX_START: |
| 1549 | case IEEE80211_AMPDU_RX_STOP: |
| 1550 | break; |
| 1551 | default: |
| 1552 | return -EOPNOTSUPP; |
| 1553 | } |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static void mac80211_hwsim_flush(struct ieee80211_hw *hw, u32 queues, bool drop) |
| 1559 | { |
| 1560 | /* Not implemented, queues only on kernel side */ |
| 1561 | } |
| 1562 | |
| 1563 | static void hw_scan_work(struct work_struct *work) |
| 1564 | { |
| 1565 | struct mac80211_hwsim_data *hwsim = |
| 1566 | container_of(work, struct mac80211_hwsim_data, hw_scan.work); |
| 1567 | struct cfg80211_scan_request *req = hwsim->hw_scan_request; |
| 1568 | int dwell, i; |
| 1569 | |
| 1570 | mutex_lock(&hwsim->mutex); |
| 1571 | if (hwsim->scan_chan_idx >= req->n_channels) { |
| 1572 | wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n"); |
| 1573 | ieee80211_scan_completed(hwsim->hw, false); |
| 1574 | hwsim->hw_scan_request = NULL; |
| 1575 | hwsim->hw_scan_vif = NULL; |
| 1576 | hwsim->tmp_chan = NULL; |
| 1577 | mutex_unlock(&hwsim->mutex); |
| 1578 | return; |
| 1579 | } |
| 1580 | |
| 1581 | wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n", |
| 1582 | req->channels[hwsim->scan_chan_idx]->center_freq); |
| 1583 | |
| 1584 | hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx]; |
| 1585 | if (hwsim->tmp_chan->flags & IEEE80211_CHAN_NO_IR || |
| 1586 | !req->n_ssids) { |
| 1587 | dwell = 120; |
| 1588 | } else { |
| 1589 | dwell = 30; |
| 1590 | /* send probes */ |
| 1591 | for (i = 0; i < req->n_ssids; i++) { |
| 1592 | struct sk_buff *probe; |
| 1593 | |
| 1594 | probe = ieee80211_probereq_get(hwsim->hw, |
| 1595 | hwsim->hw_scan_vif, |
| 1596 | req->ssids[i].ssid, |
| 1597 | req->ssids[i].ssid_len, |
| 1598 | req->ie_len); |
| 1599 | if (!probe) |
| 1600 | continue; |
| 1601 | |
| 1602 | if (req->ie_len) |
| 1603 | memcpy(skb_put(probe, req->ie_len), req->ie, |
| 1604 | req->ie_len); |
| 1605 | |
| 1606 | local_bh_disable(); |
| 1607 | mac80211_hwsim_tx_frame(hwsim->hw, probe, |
| 1608 | hwsim->tmp_chan); |
| 1609 | local_bh_enable(); |
| 1610 | } |
| 1611 | } |
| 1612 | ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, |
| 1613 | msecs_to_jiffies(dwell)); |
| 1614 | hwsim->scan_chan_idx++; |
| 1615 | mutex_unlock(&hwsim->mutex); |
| 1616 | } |
| 1617 | |
| 1618 | static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw, |
| 1619 | struct ieee80211_vif *vif, |
| 1620 | struct cfg80211_scan_request *req) |
| 1621 | { |
| 1622 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1623 | |
| 1624 | mutex_lock(&hwsim->mutex); |
| 1625 | if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) { |
| 1626 | mutex_unlock(&hwsim->mutex); |
| 1627 | return -EBUSY; |
| 1628 | } |
| 1629 | hwsim->hw_scan_request = req; |
| 1630 | hwsim->hw_scan_vif = vif; |
| 1631 | hwsim->scan_chan_idx = 0; |
| 1632 | mutex_unlock(&hwsim->mutex); |
| 1633 | |
| 1634 | wiphy_debug(hw->wiphy, "hwsim hw_scan request\n"); |
| 1635 | |
| 1636 | ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0); |
| 1637 | |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
| 1641 | static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw, |
| 1642 | struct ieee80211_vif *vif) |
| 1643 | { |
| 1644 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1645 | |
| 1646 | wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n"); |
| 1647 | |
| 1648 | cancel_delayed_work_sync(&hwsim->hw_scan); |
| 1649 | |
| 1650 | mutex_lock(&hwsim->mutex); |
| 1651 | ieee80211_scan_completed(hwsim->hw, true); |
| 1652 | hwsim->tmp_chan = NULL; |
| 1653 | hwsim->hw_scan_request = NULL; |
| 1654 | hwsim->hw_scan_vif = NULL; |
| 1655 | mutex_unlock(&hwsim->mutex); |
| 1656 | } |
| 1657 | |
| 1658 | static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw) |
| 1659 | { |
| 1660 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1661 | |
| 1662 | mutex_lock(&hwsim->mutex); |
| 1663 | |
| 1664 | if (hwsim->scanning) { |
| 1665 | printk(KERN_DEBUG "two hwsim sw_scans detected!\n"); |
| 1666 | goto out; |
| 1667 | } |
| 1668 | |
| 1669 | printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n"); |
| 1670 | hwsim->scanning = true; |
| 1671 | |
| 1672 | out: |
| 1673 | mutex_unlock(&hwsim->mutex); |
| 1674 | } |
| 1675 | |
| 1676 | static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw) |
| 1677 | { |
| 1678 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1679 | |
| 1680 | mutex_lock(&hwsim->mutex); |
| 1681 | |
| 1682 | printk(KERN_DEBUG "hwsim sw_scan_complete\n"); |
| 1683 | hwsim->scanning = false; |
| 1684 | |
| 1685 | mutex_unlock(&hwsim->mutex); |
| 1686 | } |
| 1687 | |
| 1688 | static void hw_roc_done(struct work_struct *work) |
| 1689 | { |
| 1690 | struct mac80211_hwsim_data *hwsim = |
| 1691 | container_of(work, struct mac80211_hwsim_data, roc_done.work); |
| 1692 | |
| 1693 | mutex_lock(&hwsim->mutex); |
| 1694 | ieee80211_remain_on_channel_expired(hwsim->hw); |
| 1695 | hwsim->tmp_chan = NULL; |
| 1696 | mutex_unlock(&hwsim->mutex); |
| 1697 | |
| 1698 | wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n"); |
| 1699 | } |
| 1700 | |
| 1701 | static int mac80211_hwsim_roc(struct ieee80211_hw *hw, |
| 1702 | struct ieee80211_vif *vif, |
| 1703 | struct ieee80211_channel *chan, |
| 1704 | int duration, |
| 1705 | enum ieee80211_roc_type type) |
| 1706 | { |
| 1707 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1708 | |
| 1709 | mutex_lock(&hwsim->mutex); |
| 1710 | if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) { |
| 1711 | mutex_unlock(&hwsim->mutex); |
| 1712 | return -EBUSY; |
| 1713 | } |
| 1714 | |
| 1715 | hwsim->tmp_chan = chan; |
| 1716 | mutex_unlock(&hwsim->mutex); |
| 1717 | |
| 1718 | wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n", |
| 1719 | chan->center_freq, duration); |
| 1720 | |
| 1721 | ieee80211_ready_on_channel(hw); |
| 1722 | |
| 1723 | ieee80211_queue_delayed_work(hw, &hwsim->roc_done, |
| 1724 | msecs_to_jiffies(duration)); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static int mac80211_hwsim_croc(struct ieee80211_hw *hw) |
| 1729 | { |
| 1730 | struct mac80211_hwsim_data *hwsim = hw->priv; |
| 1731 | |
| 1732 | cancel_delayed_work_sync(&hwsim->roc_done); |
| 1733 | |
| 1734 | mutex_lock(&hwsim->mutex); |
| 1735 | hwsim->tmp_chan = NULL; |
| 1736 | mutex_unlock(&hwsim->mutex); |
| 1737 | |
| 1738 | wiphy_debug(hw->wiphy, "hwsim ROC canceled\n"); |
| 1739 | |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw, |
| 1744 | struct ieee80211_chanctx_conf *ctx) |
| 1745 | { |
| 1746 | hwsim_set_chanctx_magic(ctx); |
| 1747 | wiphy_debug(hw->wiphy, |
| 1748 | "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n", |
| 1749 | ctx->def.chan->center_freq, ctx->def.width, |
| 1750 | ctx->def.center_freq1, ctx->def.center_freq2); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw, |
| 1755 | struct ieee80211_chanctx_conf *ctx) |
| 1756 | { |
| 1757 | wiphy_debug(hw->wiphy, |
| 1758 | "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n", |
| 1759 | ctx->def.chan->center_freq, ctx->def.width, |
| 1760 | ctx->def.center_freq1, ctx->def.center_freq2); |
| 1761 | hwsim_check_chanctx_magic(ctx); |
| 1762 | hwsim_clear_chanctx_magic(ctx); |
| 1763 | } |
| 1764 | |
| 1765 | static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw, |
| 1766 | struct ieee80211_chanctx_conf *ctx, |
| 1767 | u32 changed) |
| 1768 | { |
| 1769 | hwsim_check_chanctx_magic(ctx); |
| 1770 | wiphy_debug(hw->wiphy, |
| 1771 | "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n", |
| 1772 | ctx->def.chan->center_freq, ctx->def.width, |
| 1773 | ctx->def.center_freq1, ctx->def.center_freq2); |
| 1774 | } |
| 1775 | |
| 1776 | static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw, |
| 1777 | struct ieee80211_vif *vif, |
| 1778 | struct ieee80211_chanctx_conf *ctx) |
| 1779 | { |
| 1780 | hwsim_check_magic(vif); |
| 1781 | hwsim_check_chanctx_magic(ctx); |
| 1782 | |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw, |
| 1787 | struct ieee80211_vif *vif, |
| 1788 | struct ieee80211_chanctx_conf *ctx) |
| 1789 | { |
| 1790 | hwsim_check_magic(vif); |
| 1791 | hwsim_check_chanctx_magic(ctx); |
| 1792 | } |
| 1793 | |
| 1794 | static const struct ieee80211_ops mac80211_hwsim_ops = { |
| 1795 | .tx = mac80211_hwsim_tx, |
| 1796 | .start = mac80211_hwsim_start, |
| 1797 | .stop = mac80211_hwsim_stop, |
| 1798 | .add_interface = mac80211_hwsim_add_interface, |
| 1799 | .change_interface = mac80211_hwsim_change_interface, |
| 1800 | .remove_interface = mac80211_hwsim_remove_interface, |
| 1801 | .config = mac80211_hwsim_config, |
| 1802 | .configure_filter = mac80211_hwsim_configure_filter, |
| 1803 | .bss_info_changed = mac80211_hwsim_bss_info_changed, |
| 1804 | .sta_add = mac80211_hwsim_sta_add, |
| 1805 | .sta_remove = mac80211_hwsim_sta_remove, |
| 1806 | .sta_notify = mac80211_hwsim_sta_notify, |
| 1807 | .set_tim = mac80211_hwsim_set_tim, |
| 1808 | .conf_tx = mac80211_hwsim_conf_tx, |
| 1809 | .get_survey = mac80211_hwsim_get_survey, |
| 1810 | CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd) |
| 1811 | .ampdu_action = mac80211_hwsim_ampdu_action, |
| 1812 | .sw_scan_start = mac80211_hwsim_sw_scan, |
| 1813 | .sw_scan_complete = mac80211_hwsim_sw_scan_complete, |
| 1814 | .flush = mac80211_hwsim_flush, |
| 1815 | .get_tsf = mac80211_hwsim_get_tsf, |
| 1816 | .set_tsf = mac80211_hwsim_set_tsf, |
| 1817 | }; |
| 1818 | |
| 1819 | static struct ieee80211_ops mac80211_hwsim_mchan_ops; |
| 1820 | |
| 1821 | static int __init mac80211_hwsim_create_radio(void) |
| 1822 | { |
| 1823 | int err; |
| 1824 | u8 addr[ETH_ALEN]; |
| 1825 | struct mac80211_hwsim_data *data; |
| 1826 | struct ieee80211_hw *hw; |
| 1827 | enum ieee80211_band band; |
| 1828 | const struct ieee80211_ops *ops = &mac80211_hwsim_ops; |
| 1829 | int idx; |
| 1830 | |
| 1831 | spin_lock_bh(&hwsim_radio_lock); |
| 1832 | idx = hwsim_radio_idx++; |
| 1833 | spin_unlock_bh(&hwsim_radio_lock); |
| 1834 | |
| 1835 | if (channels > 1) |
| 1836 | ops = &mac80211_hwsim_mchan_ops; |
| 1837 | hw = ieee80211_alloc_hw(sizeof(*data), ops); |
| 1838 | if (!hw) { |
| 1839 | printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw failed\n"); |
| 1840 | err = -ENOMEM; |
| 1841 | goto failed; |
| 1842 | } |
| 1843 | data = hw->priv; |
| 1844 | data->hw = hw; |
| 1845 | |
| 1846 | data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx); |
| 1847 | if (IS_ERR(data->dev)) { |
| 1848 | printk(KERN_DEBUG |
| 1849 | "mac80211_hwsim: device_create failed (%ld)\n", |
| 1850 | PTR_ERR(data->dev)); |
| 1851 | err = -ENOMEM; |
| 1852 | goto failed_drvdata; |
| 1853 | } |
| 1854 | data->dev->driver = &mac80211_hwsim_driver.driver; |
| 1855 | err = device_bind_driver(data->dev); |
| 1856 | if (err != 0) { |
| 1857 | printk(KERN_DEBUG "mac80211_hwsim: device_bind_driver failed (%d)\n", |
| 1858 | err); |
| 1859 | goto failed_hw; |
| 1860 | } |
| 1861 | |
| 1862 | skb_queue_head_init(&data->pending); |
| 1863 | |
| 1864 | SET_IEEE80211_DEV(hw, data->dev); |
| 1865 | memset(addr, 0, ETH_ALEN); |
| 1866 | addr[0] = 0x02; |
| 1867 | addr[3] = idx >> 8; |
| 1868 | addr[4] = idx; |
| 1869 | memcpy(data->addresses[0].addr, addr, ETH_ALEN); |
| 1870 | memcpy(data->addresses[1].addr, addr, ETH_ALEN); |
| 1871 | data->addresses[1].addr[0] |= 0x40; |
| 1872 | hw->wiphy->n_addresses = 2; |
| 1873 | hw->wiphy->addresses = data->addresses; |
| 1874 | |
| 1875 | data->channels = channels; |
| 1876 | |
| 1877 | if (data->channels > 1) { |
| 1878 | hw->wiphy->max_scan_ssids = 255; |
| 1879 | hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN; |
| 1880 | hw->wiphy->max_remain_on_channel_duration = 1000; |
| 1881 | /* For channels > 1 DFS is not allowed */ |
| 1882 | hw->wiphy->n_iface_combinations = 1; |
| 1883 | hw->wiphy->iface_combinations = &data->if_combination; |
| 1884 | data->if_combination = hwsim_if_comb[0]; |
| 1885 | data->if_combination.num_different_channels = data->channels; |
| 1886 | } else { |
| 1887 | hw->wiphy->iface_combinations = hwsim_if_comb; |
| 1888 | hw->wiphy->n_iface_combinations = ARRAY_SIZE(hwsim_if_comb); |
| 1889 | } |
| 1890 | |
| 1891 | INIT_DELAYED_WORK(&data->roc_done, hw_roc_done); |
| 1892 | INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work); |
| 1893 | |
| 1894 | hw->queues = 5; |
| 1895 | hw->offchannel_tx_hw_queue = 4; |
| 1896 | hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | |
| 1897 | BIT(NL80211_IFTYPE_AP) | |
| 1898 | BIT(NL80211_IFTYPE_P2P_CLIENT) | |
| 1899 | BIT(NL80211_IFTYPE_P2P_GO) | |
| 1900 | BIT(NL80211_IFTYPE_ADHOC) | |
| 1901 | BIT(NL80211_IFTYPE_MESH_POINT) | |
| 1902 | BIT(NL80211_IFTYPE_P2P_DEVICE); |
| 1903 | |
| 1904 | hw->flags = IEEE80211_HW_MFP_CAPABLE | |
| 1905 | IEEE80211_HW_SIGNAL_DBM | |
| 1906 | IEEE80211_HW_SUPPORTS_STATIC_SMPS | |
| 1907 | IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS | |
| 1908 | IEEE80211_HW_AMPDU_AGGREGATION | |
| 1909 | IEEE80211_HW_WANT_MONITOR_VIF | |
| 1910 | IEEE80211_HW_QUEUE_CONTROL | |
| 1911 | IEEE80211_HW_SUPPORTS_HT_CCK_RATES; |
| 1912 | if (rctbl) |
| 1913 | hw->flags |= IEEE80211_HW_SUPPORTS_RC_TABLE; |
| 1914 | |
| 1915 | hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS | |
| 1916 | WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL | |
| 1917 | WIPHY_FLAG_AP_UAPSD; |
| 1918 | hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR; |
| 1919 | |
| 1920 | /* ask mac80211 to reserve space for magic */ |
| 1921 | hw->vif_data_size = sizeof(struct hwsim_vif_priv); |
| 1922 | hw->sta_data_size = sizeof(struct hwsim_sta_priv); |
| 1923 | hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv); |
| 1924 | |
| 1925 | memcpy(data->channels_2ghz, hwsim_channels_2ghz, |
| 1926 | sizeof(hwsim_channels_2ghz)); |
| 1927 | memcpy(data->channels_5ghz, hwsim_channels_5ghz, |
| 1928 | sizeof(hwsim_channels_5ghz)); |
| 1929 | memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates)); |
| 1930 | |
| 1931 | for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) { |
| 1932 | struct ieee80211_supported_band *sband = &data->bands[band]; |
| 1933 | switch (band) { |
| 1934 | case IEEE80211_BAND_2GHZ: |
| 1935 | sband->channels = data->channels_2ghz; |
| 1936 | sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz); |
| 1937 | sband->bitrates = data->rates; |
| 1938 | sband->n_bitrates = ARRAY_SIZE(hwsim_rates); |
| 1939 | break; |
| 1940 | case IEEE80211_BAND_5GHZ: |
| 1941 | sband->channels = data->channels_5ghz; |
| 1942 | sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz); |
| 1943 | sband->bitrates = data->rates + 4; |
| 1944 | sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4; |
| 1945 | break; |
| 1946 | default: |
| 1947 | continue; |
| 1948 | } |
| 1949 | |
| 1950 | sband->ht_cap.ht_supported = true; |
| 1951 | sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 | |
| 1952 | IEEE80211_HT_CAP_GRN_FLD | |
| 1953 | IEEE80211_HT_CAP_SGI_40 | |
| 1954 | IEEE80211_HT_CAP_DSSSCCK40; |
| 1955 | sband->ht_cap.ampdu_factor = 0x3; |
| 1956 | sband->ht_cap.ampdu_density = 0x6; |
| 1957 | memset(&sband->ht_cap.mcs, 0, |
| 1958 | sizeof(sband->ht_cap.mcs)); |
| 1959 | sband->ht_cap.mcs.rx_mask[0] = 0xff; |
| 1960 | sband->ht_cap.mcs.rx_mask[1] = 0xff; |
| 1961 | sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; |
| 1962 | |
| 1963 | hw->wiphy->bands[band] = sband; |
| 1964 | |
| 1965 | sband->vht_cap.vht_supported = true; |
| 1966 | sband->vht_cap.cap = |
| 1967 | IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | |
| 1968 | IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ | |
| 1969 | IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | |
| 1970 | IEEE80211_VHT_CAP_RXLDPC | |
| 1971 | IEEE80211_VHT_CAP_SHORT_GI_80 | |
| 1972 | IEEE80211_VHT_CAP_SHORT_GI_160 | |
| 1973 | IEEE80211_VHT_CAP_TXSTBC | |
| 1974 | IEEE80211_VHT_CAP_RXSTBC_1 | |
| 1975 | IEEE80211_VHT_CAP_RXSTBC_2 | |
| 1976 | IEEE80211_VHT_CAP_RXSTBC_3 | |
| 1977 | IEEE80211_VHT_CAP_RXSTBC_4 | |
| 1978 | IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK; |
| 1979 | sband->vht_cap.vht_mcs.rx_mcs_map = |
| 1980 | cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 | |
| 1981 | IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 | |
| 1982 | IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 | |
| 1983 | IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 | |
| 1984 | IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 | |
| 1985 | IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 | |
| 1986 | IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 | |
| 1987 | IEEE80211_VHT_MCS_SUPPORT_0_8 << 14); |
| 1988 | sband->vht_cap.vht_mcs.tx_mcs_map = |
| 1989 | sband->vht_cap.vht_mcs.rx_mcs_map; |
| 1990 | } |
| 1991 | |
| 1992 | /* By default all radios belong to the first group */ |
| 1993 | data->group = 1; |
| 1994 | mutex_init(&data->mutex); |
| 1995 | |
| 1996 | /* Enable frame retransmissions for lossy channels */ |
| 1997 | hw->max_rates = 4; |
| 1998 | hw->max_rate_tries = 11; |
| 1999 | |
| 2000 | err = ieee80211_register_hw(hw); |
| 2001 | if (err < 0) { |
| 2002 | printk(KERN_DEBUG "mac80211_hwsim: ieee80211_register_hw failed (%d)\n", |
| 2003 | err); |
| 2004 | goto failed_hw; |
| 2005 | } |
| 2006 | |
| 2007 | wiphy_debug(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr); |
| 2008 | |
| 2009 | data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir); |
| 2010 | debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps); |
| 2011 | debugfs_create_file("group", 0666, data->debugfs, data, |
| 2012 | &hwsim_fops_group); |
| 2013 | if (data->channels == 1) |
| 2014 | debugfs_create_file("dfs_simulate_radar", 0222, |
| 2015 | data->debugfs, |
| 2016 | data, &hwsim_simulate_radar); |
| 2017 | |
| 2018 | tasklet_hrtimer_init(&data->beacon_timer, |
| 2019 | mac80211_hwsim_beacon, |
| 2020 | CLOCK_MONOTONIC_RAW, HRTIMER_MODE_ABS); |
| 2021 | |
| 2022 | spin_lock_bh(&hwsim_radio_lock); |
| 2023 | list_add_tail(&data->list, &hwsim_radios); |
| 2024 | spin_unlock_bh(&hwsim_radio_lock); |
| 2025 | |
| 2026 | return 0; |
| 2027 | |
| 2028 | failed_hw: |
| 2029 | device_unregister(data->dev); |
| 2030 | failed_drvdata: |
| 2031 | ieee80211_free_hw(hw); |
| 2032 | failed: |
| 2033 | return err; |
| 2034 | } |
| 2035 | |
| 2036 | static void mac80211_hwsim_destroy_radio(struct mac80211_hwsim_data *data) |
| 2037 | { |
| 2038 | debugfs_remove_recursive(data->debugfs); |
| 2039 | ieee80211_unregister_hw(data->hw); |
| 2040 | device_release_driver(data->dev); |
| 2041 | device_unregister(data->dev); |
| 2042 | ieee80211_free_hw(data->hw); |
| 2043 | } |
| 2044 | |
| 2045 | static void mac80211_hwsim_free(void) |
| 2046 | { |
| 2047 | struct mac80211_hwsim_data *data; |
| 2048 | |
| 2049 | spin_lock_bh(&hwsim_radio_lock); |
| 2050 | while ((data = list_first_entry_or_null(&hwsim_radios, |
| 2051 | struct mac80211_hwsim_data, |
| 2052 | list))) { |
| 2053 | list_del(&data->list); |
| 2054 | spin_unlock_bh(&hwsim_radio_lock); |
| 2055 | mac80211_hwsim_destroy_radio(data); |
| 2056 | spin_lock_bh(&hwsim_radio_lock); |
| 2057 | } |
| 2058 | spin_unlock_bh(&hwsim_radio_lock); |
| 2059 | class_destroy(hwsim_class); |
| 2060 | } |
| 2061 | |
| 2062 | static const struct net_device_ops hwsim_netdev_ops = { |
| 2063 | .ndo_start_xmit = hwsim_mon_xmit, |
| 2064 | .ndo_change_mtu = eth_change_mtu, |
| 2065 | .ndo_set_mac_address = eth_mac_addr, |
| 2066 | .ndo_validate_addr = eth_validate_addr, |
| 2067 | }; |
| 2068 | |
| 2069 | static void hwsim_mon_setup(struct net_device *dev) |
| 2070 | { |
| 2071 | dev->netdev_ops = &hwsim_netdev_ops; |
| 2072 | dev->destructor = free_netdev; |
| 2073 | ether_setup(dev); |
| 2074 | dev->tx_queue_len = 0; |
| 2075 | dev->type = ARPHRD_IEEE80211_RADIOTAP; |
| 2076 | memset(dev->dev_addr, 0, ETH_ALEN); |
| 2077 | dev->dev_addr[0] = 0x12; |
| 2078 | } |
| 2079 | |
| 2080 | static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr) |
| 2081 | { |
| 2082 | struct mac80211_hwsim_data *data; |
| 2083 | bool _found = false; |
| 2084 | |
| 2085 | spin_lock_bh(&hwsim_radio_lock); |
| 2086 | list_for_each_entry(data, &hwsim_radios, list) { |
| 2087 | if (memcmp(data->addresses[1].addr, addr, ETH_ALEN) == 0) { |
| 2088 | _found = true; |
| 2089 | break; |
| 2090 | } |
| 2091 | } |
| 2092 | spin_unlock_bh(&hwsim_radio_lock); |
| 2093 | |
| 2094 | if (!_found) |
| 2095 | return NULL; |
| 2096 | |
| 2097 | return data; |
| 2098 | } |
| 2099 | |
| 2100 | static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2, |
| 2101 | struct genl_info *info) |
| 2102 | { |
| 2103 | |
| 2104 | struct ieee80211_hdr *hdr; |
| 2105 | struct mac80211_hwsim_data *data2; |
| 2106 | struct ieee80211_tx_info *txi; |
| 2107 | struct hwsim_tx_rate *tx_attempts; |
| 2108 | unsigned long ret_skb_ptr; |
| 2109 | struct sk_buff *skb, *tmp; |
| 2110 | const u8 *src; |
| 2111 | unsigned int hwsim_flags; |
| 2112 | int i; |
| 2113 | bool found = false; |
| 2114 | |
| 2115 | if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] || |
| 2116 | !info->attrs[HWSIM_ATTR_FLAGS] || |
| 2117 | !info->attrs[HWSIM_ATTR_COOKIE] || |
| 2118 | !info->attrs[HWSIM_ATTR_TX_INFO]) |
| 2119 | goto out; |
| 2120 | |
| 2121 | src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]); |
| 2122 | hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]); |
| 2123 | ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]); |
| 2124 | |
| 2125 | data2 = get_hwsim_data_ref_from_addr(src); |
| 2126 | if (!data2) |
| 2127 | goto out; |
| 2128 | |
| 2129 | /* look for the skb matching the cookie passed back from user */ |
| 2130 | skb_queue_walk_safe(&data2->pending, skb, tmp) { |
| 2131 | if ((unsigned long)skb == ret_skb_ptr) { |
| 2132 | skb_unlink(skb, &data2->pending); |
| 2133 | found = true; |
| 2134 | break; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | /* not found */ |
| 2139 | if (!found) |
| 2140 | goto out; |
| 2141 | |
| 2142 | /* Tx info received because the frame was broadcasted on user space, |
| 2143 | so we get all the necessary info: tx attempts and skb control buff */ |
| 2144 | |
| 2145 | tx_attempts = (struct hwsim_tx_rate *)nla_data( |
| 2146 | info->attrs[HWSIM_ATTR_TX_INFO]); |
| 2147 | |
| 2148 | /* now send back TX status */ |
| 2149 | txi = IEEE80211_SKB_CB(skb); |
| 2150 | |
| 2151 | ieee80211_tx_info_clear_status(txi); |
| 2152 | |
| 2153 | for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { |
| 2154 | txi->status.rates[i].idx = tx_attempts[i].idx; |
| 2155 | txi->status.rates[i].count = tx_attempts[i].count; |
| 2156 | /*txi->status.rates[i].flags = 0;*/ |
| 2157 | } |
| 2158 | |
| 2159 | txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]); |
| 2160 | |
| 2161 | if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) && |
| 2162 | (hwsim_flags & HWSIM_TX_STAT_ACK)) { |
| 2163 | if (skb->len >= 16) { |
| 2164 | hdr = (struct ieee80211_hdr *) skb->data; |
| 2165 | mac80211_hwsim_monitor_ack(txi->rate_driver_data[0], |
| 2166 | hdr->addr2); |
| 2167 | } |
| 2168 | txi->flags |= IEEE80211_TX_STAT_ACK; |
| 2169 | } |
| 2170 | ieee80211_tx_status_irqsafe(data2->hw, skb); |
| 2171 | return 0; |
| 2172 | out: |
| 2173 | return -EINVAL; |
| 2174 | |
| 2175 | } |
| 2176 | |
| 2177 | static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2, |
| 2178 | struct genl_info *info) |
| 2179 | { |
| 2180 | |
| 2181 | struct mac80211_hwsim_data *data2; |
| 2182 | struct ieee80211_rx_status rx_status; |
| 2183 | const u8 *dst; |
| 2184 | int frame_data_len; |
| 2185 | void *frame_data; |
| 2186 | struct sk_buff *skb = NULL; |
| 2187 | |
| 2188 | if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] || |
| 2189 | !info->attrs[HWSIM_ATTR_FRAME] || |
| 2190 | !info->attrs[HWSIM_ATTR_RX_RATE] || |
| 2191 | !info->attrs[HWSIM_ATTR_SIGNAL]) |
| 2192 | goto out; |
| 2193 | |
| 2194 | dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]); |
| 2195 | frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]); |
| 2196 | frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]); |
| 2197 | |
| 2198 | /* Allocate new skb here */ |
| 2199 | skb = alloc_skb(frame_data_len, GFP_KERNEL); |
| 2200 | if (skb == NULL) |
| 2201 | goto err; |
| 2202 | |
| 2203 | if (frame_data_len > IEEE80211_MAX_DATA_LEN) |
| 2204 | goto err; |
| 2205 | |
| 2206 | /* Copy the data */ |
| 2207 | memcpy(skb_put(skb, frame_data_len), frame_data, frame_data_len); |
| 2208 | |
| 2209 | data2 = get_hwsim_data_ref_from_addr(dst); |
| 2210 | if (!data2) |
| 2211 | goto out; |
| 2212 | |
| 2213 | /* check if radio is configured properly */ |
| 2214 | |
| 2215 | if (data2->idle || !data2->started) |
| 2216 | goto out; |
| 2217 | |
| 2218 | /* A frame is received from user space */ |
| 2219 | memset(&rx_status, 0, sizeof(rx_status)); |
| 2220 | rx_status.freq = data2->channel->center_freq; |
| 2221 | rx_status.band = data2->channel->band; |
| 2222 | rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]); |
| 2223 | rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]); |
| 2224 | |
| 2225 | memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); |
| 2226 | ieee80211_rx_irqsafe(data2->hw, skb); |
| 2227 | |
| 2228 | return 0; |
| 2229 | err: |
| 2230 | printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__); |
| 2231 | goto out; |
| 2232 | out: |
| 2233 | dev_kfree_skb(skb); |
| 2234 | return -EINVAL; |
| 2235 | } |
| 2236 | |
| 2237 | static int hwsim_register_received_nl(struct sk_buff *skb_2, |
| 2238 | struct genl_info *info) |
| 2239 | { |
| 2240 | wmediumd_portid = info->snd_portid; |
| 2241 | |
| 2242 | printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, " |
| 2243 | "switching to wmediumd mode with pid %d\n", info->snd_portid); |
| 2244 | |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | /* Generic Netlink operations array */ |
| 2249 | static const struct genl_ops hwsim_ops[] = { |
| 2250 | { |
| 2251 | .cmd = HWSIM_CMD_REGISTER, |
| 2252 | .policy = hwsim_genl_policy, |
| 2253 | .doit = hwsim_register_received_nl, |
| 2254 | .flags = GENL_ADMIN_PERM, |
| 2255 | }, |
| 2256 | { |
| 2257 | .cmd = HWSIM_CMD_FRAME, |
| 2258 | .policy = hwsim_genl_policy, |
| 2259 | .doit = hwsim_cloned_frame_received_nl, |
| 2260 | }, |
| 2261 | { |
| 2262 | .cmd = HWSIM_CMD_TX_INFO_FRAME, |
| 2263 | .policy = hwsim_genl_policy, |
| 2264 | .doit = hwsim_tx_info_frame_received_nl, |
| 2265 | }, |
| 2266 | }; |
| 2267 | |
| 2268 | static int mac80211_hwsim_netlink_notify(struct notifier_block *nb, |
| 2269 | unsigned long state, |
| 2270 | void *_notify) |
| 2271 | { |
| 2272 | struct netlink_notify *notify = _notify; |
| 2273 | |
| 2274 | if (state != NETLINK_URELEASE) |
| 2275 | return NOTIFY_DONE; |
| 2276 | |
| 2277 | if (notify->portid == wmediumd_portid) { |
| 2278 | printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink" |
| 2279 | " socket, switching to perfect channel medium\n"); |
| 2280 | wmediumd_portid = 0; |
| 2281 | } |
| 2282 | return NOTIFY_DONE; |
| 2283 | |
| 2284 | } |
| 2285 | |
| 2286 | static struct notifier_block hwsim_netlink_notifier = { |
| 2287 | .notifier_call = mac80211_hwsim_netlink_notify, |
| 2288 | }; |
| 2289 | |
| 2290 | static int hwsim_init_netlink(void) |
| 2291 | { |
| 2292 | int rc; |
| 2293 | |
| 2294 | /* userspace test API hasn't been adjusted for multi-channel */ |
| 2295 | if (channels > 1) |
| 2296 | return 0; |
| 2297 | |
| 2298 | printk(KERN_INFO "mac80211_hwsim: initializing netlink\n"); |
| 2299 | |
| 2300 | rc = genl_register_family_with_ops(&hwsim_genl_family, hwsim_ops); |
| 2301 | if (rc) |
| 2302 | goto failure; |
| 2303 | |
| 2304 | rc = netlink_register_notifier(&hwsim_netlink_notifier); |
| 2305 | if (rc) |
| 2306 | goto failure; |
| 2307 | |
| 2308 | return 0; |
| 2309 | |
| 2310 | failure: |
| 2311 | printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__); |
| 2312 | return -EINVAL; |
| 2313 | } |
| 2314 | |
| 2315 | static void hwsim_exit_netlink(void) |
| 2316 | { |
| 2317 | /* userspace test API hasn't been adjusted for multi-channel */ |
| 2318 | if (channels > 1) |
| 2319 | return; |
| 2320 | |
| 2321 | /* unregister the notifier */ |
| 2322 | netlink_unregister_notifier(&hwsim_netlink_notifier); |
| 2323 | /* unregister the family */ |
| 2324 | genl_unregister_family(&hwsim_genl_family); |
| 2325 | } |
| 2326 | |
| 2327 | static int __init init_mac80211_hwsim(void) |
| 2328 | { |
| 2329 | int i, err; |
| 2330 | |
| 2331 | if (radios < 1 || radios > 100) |
| 2332 | return -EINVAL; |
| 2333 | |
| 2334 | if (channels < 1) |
| 2335 | return -EINVAL; |
| 2336 | |
| 2337 | mac80211_hwsim_mchan_ops = mac80211_hwsim_ops; |
| 2338 | mac80211_hwsim_mchan_ops.hw_scan = mac80211_hwsim_hw_scan; |
| 2339 | mac80211_hwsim_mchan_ops.cancel_hw_scan = mac80211_hwsim_cancel_hw_scan; |
| 2340 | mac80211_hwsim_mchan_ops.sw_scan_start = NULL; |
| 2341 | mac80211_hwsim_mchan_ops.sw_scan_complete = NULL; |
| 2342 | mac80211_hwsim_mchan_ops.remain_on_channel = mac80211_hwsim_roc; |
| 2343 | mac80211_hwsim_mchan_ops.cancel_remain_on_channel = mac80211_hwsim_croc; |
| 2344 | mac80211_hwsim_mchan_ops.add_chanctx = mac80211_hwsim_add_chanctx; |
| 2345 | mac80211_hwsim_mchan_ops.remove_chanctx = mac80211_hwsim_remove_chanctx; |
| 2346 | mac80211_hwsim_mchan_ops.change_chanctx = mac80211_hwsim_change_chanctx; |
| 2347 | mac80211_hwsim_mchan_ops.assign_vif_chanctx = |
| 2348 | mac80211_hwsim_assign_vif_chanctx; |
| 2349 | mac80211_hwsim_mchan_ops.unassign_vif_chanctx = |
| 2350 | mac80211_hwsim_unassign_vif_chanctx; |
| 2351 | |
| 2352 | spin_lock_init(&hwsim_radio_lock); |
| 2353 | INIT_LIST_HEAD(&hwsim_radios); |
| 2354 | |
| 2355 | err = platform_driver_register(&mac80211_hwsim_driver); |
| 2356 | if (err) |
| 2357 | return err; |
| 2358 | |
| 2359 | hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim"); |
| 2360 | if (IS_ERR(hwsim_class)) { |
| 2361 | err = PTR_ERR(hwsim_class); |
| 2362 | goto out_unregister_driver; |
| 2363 | } |
| 2364 | |
| 2365 | for (i = 0; i < radios; i++) { |
| 2366 | err = mac80211_hwsim_create_radio(); |
| 2367 | if (err) |
| 2368 | goto out_free_radios; |
| 2369 | } |
| 2370 | |
| 2371 | hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup); |
| 2372 | if (hwsim_mon == NULL) { |
| 2373 | err = -ENOMEM; |
| 2374 | goto out_free_radios; |
| 2375 | } |
| 2376 | |
| 2377 | rtnl_lock(); |
| 2378 | err = dev_alloc_name(hwsim_mon, hwsim_mon->name); |
| 2379 | if (err < 0) { |
| 2380 | rtnl_unlock(); |
| 2381 | goto out_free_radios; |
| 2382 | } |
| 2383 | |
| 2384 | err = register_netdevice(hwsim_mon); |
| 2385 | if (err < 0) { |
| 2386 | rtnl_unlock(); |
| 2387 | goto out_free_mon; |
| 2388 | } |
| 2389 | rtnl_unlock(); |
| 2390 | |
| 2391 | err = hwsim_init_netlink(); |
| 2392 | if (err < 0) |
| 2393 | goto out_free_mon; |
| 2394 | |
| 2395 | return 0; |
| 2396 | |
| 2397 | out_free_mon: |
| 2398 | free_netdev(hwsim_mon); |
| 2399 | out_free_radios: |
| 2400 | mac80211_hwsim_free(); |
| 2401 | out_unregister_driver: |
| 2402 | platform_driver_unregister(&mac80211_hwsim_driver); |
| 2403 | return err; |
| 2404 | } |
| 2405 | module_init(init_mac80211_hwsim); |
| 2406 | |
| 2407 | static void __exit exit_mac80211_hwsim(void) |
| 2408 | { |
| 2409 | printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n"); |
| 2410 | |
| 2411 | hwsim_exit_netlink(); |
| 2412 | |
| 2413 | mac80211_hwsim_free(); |
| 2414 | unregister_netdev(hwsim_mon); |
| 2415 | platform_driver_unregister(&mac80211_hwsim_driver); |
| 2416 | } |
| 2417 | module_exit(exit_mac80211_hwsim); |