Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
[linux-2.6.git] / drivers / net / wireless / mac80211_hwsim.c
index bdce71a..6cf6d6d 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 
 /*
  * TODO:
- * - IBSS mode simulation (Beacon transmission with competition for "air time")
+ * - Add TSF sync and fix IBSS beacon transmission by adding
+ *   competition for "air time" at TBTT
  * - RX filtering based on filter configuration (data->rx_filter)
  */
 
 #include <linux/list.h>
+#include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <net/dst.h>
 #include <net/xfrm.h>
 #include <linux/rtnetlink.h>
 #include <linux/etherdevice.h>
 #include <linux/debugfs.h>
+#include <linux/module.h>
+#include <net/genetlink.h>
+#include "mac80211_hwsim.h"
+
+#define WARN_QUEUE 100
+#define MAX_QUEUE 200
 
 MODULE_AUTHOR("Jouni Malinen");
 MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
 MODULE_LICENSE("GPL");
 
+int wmediumd_pid;
 static int radios = 2;
 module_param(radios, int, 0444);
 MODULE_PARM_DESC(radios, "Number of simulated radios");
@@ -60,7 +70,7 @@ MODULE_PARM_DESC(fake_hw_scan, "Install fake (no-op) hw-scan handler");
  *     an intersection to occur but each device will still use their
  *     respective regulatory requested domains. Subsequent radios will
  *     use the resulting intersection.
- * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We acomplish
+ * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  *     this by using a custom beacon-capable regulatory domain for the first
  *     radio. All other device world roam.
  * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
@@ -300,6 +310,7 @@ struct mac80211_hwsim_data {
        struct dentry *debugfs;
        struct dentry *debugfs_ps;
 
+       struct sk_buff_head pending;    /* packets pending */
        /*
         * Only radios in the same group can communicate together (the
         * channel has to match too). Each bit represents a group. A
@@ -307,6 +318,8 @@ struct mac80211_hwsim_data {
         */
        u64 group;
        struct dentry *debugfs_group;
+
+       int power_level;
 };
 
 
@@ -316,8 +329,34 @@ struct hwsim_radiotap_hdr {
        u8 rt_rate;
        __le16 rt_channel;
        __le16 rt_chbitmask;
-} __attribute__ ((packed));
+} __packed;
+
+/* MAC80211_HWSIM netlinf family */
+static struct genl_family hwsim_genl_family = {
+       .id = GENL_ID_GENERATE,
+       .hdrsize = 0,
+       .name = "MAC80211_HWSIM",
+       .version = 1,
+       .maxattr = HWSIM_ATTR_MAX,
+};
 
+/* MAC80211_HWSIM netlink policy */
+
+static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
+       [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
+                                      .len = 6*sizeof(u8) },
+       [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
+                                         .len = 6*sizeof(u8) },
+       [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
+                              .len = IEEE80211_MAX_DATA_LEN },
+       [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
+       [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
+       [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
+       [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
+                                .len = IEEE80211_TX_MAX_RATES*sizeof(
+                                       struct hwsim_tx_rate)},
+       [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
+};
 
 static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
                                        struct net_device *dev)
@@ -474,9 +513,89 @@ static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
        return md.ret;
 }
 
+static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
+                                      struct sk_buff *my_skb,
+                                      int dst_pid)
+{
+       struct sk_buff *skb;
+       struct mac80211_hwsim_data *data = hw->priv;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
+       void *msg_head;
+       unsigned int hwsim_flags = 0;
+       int i;
+       struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
+
+       if (data->idle) {
+               wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
+               dev_kfree_skb(my_skb);
+               return;
+       }
+
+       if (data->ps != PS_DISABLED)
+               hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
+       /* If the queue contains MAX_QUEUE skb's drop some */
+       if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
+               /* Droping until WARN_QUEUE level */
+               while (skb_queue_len(&data->pending) >= WARN_QUEUE)
+                       skb_dequeue(&data->pending);
+       }
+
+       skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
+       if (skb == NULL)
+               goto nla_put_failure;
+
+       msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
+                              HWSIM_CMD_FRAME);
+       if (msg_head == NULL) {
+               printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
+               goto nla_put_failure;
+       }
+
+       NLA_PUT(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
+                    sizeof(struct mac_address), data->addresses[1].addr);
+
+       /* We get the skb->data */
+       NLA_PUT(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data);
 
-static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
-                                   struct sk_buff *skb)
+       /* We get the flags for this transmission, and we translate them to
+          wmediumd flags  */
+
+       if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
+               hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
+
+       if (info->flags & IEEE80211_TX_CTL_NO_ACK)
+               hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
+
+       NLA_PUT_U32(skb, HWSIM_ATTR_FLAGS, hwsim_flags);
+
+       /* We get the tx control (rate and retries) info*/
+
+       for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
+               tx_attempts[i].idx = info->status.rates[i].idx;
+               tx_attempts[i].count = info->status.rates[i].count;
+       }
+
+       NLA_PUT(skb, HWSIM_ATTR_TX_INFO,
+                    sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
+                    tx_attempts);
+
+       /* We create a cookie to identify this skb */
+       NLA_PUT_U64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb);
+
+       genlmsg_end(skb, msg_head);
+       genlmsg_unicast(&init_net, skb, dst_pid);
+
+       /* Enqueue the packet */
+       skb_queue_tail(&data->pending, my_skb);
+       return;
+
+nla_put_failure:
+       printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
+}
+
+static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
+                                         struct sk_buff *skb)
 {
        struct mac80211_hwsim_data *data = hw->priv, *data2;
        bool ack = false;
@@ -485,8 +604,7 @@ static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
        struct ieee80211_rx_status rx_status;
 
        if (data->idle) {
-               printk(KERN_DEBUG "%s: Trying to TX when idle - reject\n",
-                      wiphy_name(hw->wiphy));
+               wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
                return false;
        }
 
@@ -495,8 +613,14 @@ static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
        rx_status.freq = data->channel->center_freq;
        rx_status.band = data->channel->band;
        rx_status.rate_idx = info->control.rates[0].idx;
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
+               rx_status.flag |= RX_FLAG_HT;
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
+               rx_status.flag |= RX_FLAG_40MHZ;
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
+               rx_status.flag |= RX_FLAG_SHORT_GI;
        /* TODO: simulate real signal strength (and optional packet loss) */
-       rx_status.signal = -50;
+       rx_status.signal = data->power_level - 50;
 
        if (data->ps != PS_DISABLED)
                hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
@@ -537,21 +661,29 @@ static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
        return ack;
 }
 
-
-static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
+static void mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
 {
        bool ack;
        struct ieee80211_tx_info *txi;
+       int _pid;
 
        mac80211_hwsim_monitor_rx(hw, skb);
 
        if (skb->len < 10) {
                /* Should not happen; just a sanity check for addr1 use */
                dev_kfree_skb(skb);
-               return NETDEV_TX_OK;
+               return;
        }
 
-       ack = mac80211_hwsim_tx_frame(hw, skb);
+       /* wmediumd mode check */
+       _pid = wmediumd_pid;
+
+       if (_pid)
+               return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
+
+       /* NO wmediumd detected, perfect medium simulation */
+       ack = mac80211_hwsim_tx_frame_no_nl(hw, skb);
+
        if (ack && skb->len >= 16) {
                struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
                mac80211_hwsim_monitor_ack(hw, hdr->addr2);
@@ -568,14 +700,13 @@ static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
        if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
                txi->flags |= IEEE80211_TX_STAT_ACK;
        ieee80211_tx_status_irqsafe(hw, skb);
-       return NETDEV_TX_OK;
 }
 
 
 static int mac80211_hwsim_start(struct ieee80211_hw *hw)
 {
        struct mac80211_hwsim_data *data = hw->priv;
-       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+       wiphy_debug(hw->wiphy, "%s\n", __func__);
        data->started = 1;
        return 0;
 }
@@ -586,27 +717,42 @@ static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
        struct mac80211_hwsim_data *data = hw->priv;
        data->started = 0;
        del_timer(&data->beacon_timer);
-       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+       wiphy_debug(hw->wiphy, "%s\n", __func__);
 }
 
 
 static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
                                        struct ieee80211_vif *vif)
 {
-       printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
-              wiphy_name(hw->wiphy), __func__, vif->type,
-              vif->addr);
+       wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
+                   __func__, ieee80211_vif_type_p2p(vif),
+                   vif->addr);
        hwsim_set_magic(vif);
        return 0;
 }
 
 
+static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
+                                          struct ieee80211_vif *vif,
+                                          enum nl80211_iftype newtype,
+                                          bool newp2p)
+{
+       newtype = ieee80211_iftype_p2p(newtype, newp2p);
+       wiphy_debug(hw->wiphy,
+                   "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
+                   __func__, ieee80211_vif_type_p2p(vif),
+                   newtype, vif->addr);
+       hwsim_check_magic(vif);
+
+       return 0;
+}
+
 static void mac80211_hwsim_remove_interface(
        struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 {
-       printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
-              wiphy_name(hw->wiphy), __func__, vif->type,
-              vif->addr);
+       wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
+                   __func__, ieee80211_vif_type_p2p(vif),
+                   vif->addr);
        hwsim_check_magic(vif);
        hwsim_clear_magic(vif);
 }
@@ -618,11 +764,13 @@ static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
        struct ieee80211_hw *hw = arg;
        struct sk_buff *skb;
        struct ieee80211_tx_info *info;
+       int _pid;
 
        hwsim_check_magic(vif);
 
        if (vif->type != NL80211_IFTYPE_AP &&
-           vif->type != NL80211_IFTYPE_MESH_POINT)
+           vif->type != NL80211_IFTYPE_MESH_POINT &&
+           vif->type != NL80211_IFTYPE_ADHOC)
                return;
 
        skb = ieee80211_beacon_get(hw, vif);
@@ -631,7 +779,14 @@ static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
        info = IEEE80211_SKB_CB(skb);
 
        mac80211_hwsim_monitor_rx(hw, skb);
-       mac80211_hwsim_tx_frame(hw, skb);
+
+       /* wmediumd mode check */
+       _pid = wmediumd_pid;
+
+       if (_pid)
+               return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
+
+       mac80211_hwsim_tx_frame_no_nl(hw, skb);
        dev_kfree_skb(skb);
 }
 
@@ -669,17 +824,19 @@ static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
                [IEEE80211_SMPS_DYNAMIC] = "dynamic",
        };
 
-       printk(KERN_DEBUG "%s:%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
-              wiphy_name(hw->wiphy), __func__,
-              conf->channel->center_freq,
-              hwsim_chantypes[conf->channel_type],
-              !!(conf->flags & IEEE80211_CONF_IDLE),
-              !!(conf->flags & IEEE80211_CONF_PS),
-              smps_modes[conf->smps_mode]);
+       wiphy_debug(hw->wiphy,
+                   "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
+                   __func__,
+                   conf->channel->center_freq,
+                   hwsim_chantypes[conf->channel_type],
+                   !!(conf->flags & IEEE80211_CONF_IDLE),
+                   !!(conf->flags & IEEE80211_CONF_PS),
+                   smps_modes[conf->smps_mode]);
 
        data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
 
        data->channel = conf->channel;
+       data->power_level = conf->power_level;
        if (!data->started || !data->beacon_int)
                del_timer(&data->beacon_timer);
        else
@@ -695,7 +852,7 @@ static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
 {
        struct mac80211_hwsim_data *data = hw->priv;
 
-       printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
+       wiphy_debug(hw->wiphy, "%s\n", __func__);
 
        data->rx_filter = 0;
        if (*total_flags & FIF_PROMISC_IN_BSS)
@@ -716,26 +873,23 @@ static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
 
        hwsim_check_magic(vif);
 
-       printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
-              wiphy_name(hw->wiphy), __func__, changed);
+       wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
 
        if (changed & BSS_CHANGED_BSSID) {
-               printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n",
-                      wiphy_name(hw->wiphy), __func__,
-                      info->bssid);
+               wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
+                           __func__, info->bssid);
                memcpy(vp->bssid, info->bssid, ETH_ALEN);
        }
 
        if (changed & BSS_CHANGED_ASSOC) {
-               printk(KERN_DEBUG "  %s: ASSOC: assoc=%d aid=%d\n",
-                      wiphy_name(hw->wiphy), info->assoc, info->aid);
+               wiphy_debug(hw->wiphy, "  ASSOC: assoc=%d aid=%d\n",
+                           info->assoc, info->aid);
                vp->assoc = info->assoc;
                vp->aid = info->aid;
        }
 
        if (changed & BSS_CHANGED_BEACON_INT) {
-               printk(KERN_DEBUG "  %s: BCNINT: %d\n",
-                      wiphy_name(hw->wiphy), info->beacon_int);
+               wiphy_debug(hw->wiphy, "  BCNINT: %d\n", info->beacon_int);
                data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
                if (WARN_ON(!data->beacon_int))
                        data->beacon_int = 1;
@@ -745,31 +899,28 @@ static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
        }
 
        if (changed & BSS_CHANGED_ERP_CTS_PROT) {
-               printk(KERN_DEBUG "  %s: ERP_CTS_PROT: %d\n",
-                      wiphy_name(hw->wiphy), info->use_cts_prot);
+               wiphy_debug(hw->wiphy, "  ERP_CTS_PROT: %d\n",
+                           info->use_cts_prot);
        }
 
        if (changed & BSS_CHANGED_ERP_PREAMBLE) {
-               printk(KERN_DEBUG "  %s: ERP_PREAMBLE: %d\n",
-                      wiphy_name(hw->wiphy), info->use_short_preamble);
+               wiphy_debug(hw->wiphy, "  ERP_PREAMBLE: %d\n",
+                           info->use_short_preamble);
        }
 
        if (changed & BSS_CHANGED_ERP_SLOT) {
-               printk(KERN_DEBUG "  %s: ERP_SLOT: %d\n",
-                      wiphy_name(hw->wiphy), info->use_short_slot);
+               wiphy_debug(hw->wiphy, "  ERP_SLOT: %d\n", info->use_short_slot);
        }
 
        if (changed & BSS_CHANGED_HT) {
-               printk(KERN_DEBUG "  %s: HT: op_mode=0x%x, chantype=%s\n",
-                      wiphy_name(hw->wiphy),
-                      info->ht_operation_mode,
-                      hwsim_chantypes[info->channel_type]);
+               wiphy_debug(hw->wiphy, "  HT: op_mode=0x%x, chantype=%s\n",
+                           info->ht_operation_mode,
+                           hwsim_chantypes[info->channel_type]);
        }
 
        if (changed & BSS_CHANGED_BASIC_RATES) {
-               printk(KERN_DEBUG "  %s: BASIC_RATES: 0x%llx\n",
-                      wiphy_name(hw->wiphy),
-                      (unsigned long long) info->basic_rates);
+               wiphy_debug(hw->wiphy, "  BASIC_RATES: 0x%llx\n",
+                           (unsigned long long) info->basic_rates);
        }
 }
 
@@ -820,13 +971,15 @@ static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
 }
 
 static int mac80211_hwsim_conf_tx(
-       struct ieee80211_hw *hw, u16 queue,
+       struct ieee80211_hw *hw,
+       struct ieee80211_vif *vif, u16 queue,
        const struct ieee80211_tx_queue_params *params)
 {
-       printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
-              "aifs=%d)\n",
-              wiphy_name(hw->wiphy), __func__, queue,
-              params->txop, params->cw_min, params->cw_max, params->aifs);
+       wiphy_debug(hw->wiphy,
+                   "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
+                   __func__, queue,
+                   params->txop, params->cw_min,
+                   params->cw_max, params->aifs);
        return 0;
 }
 
@@ -836,8 +989,7 @@ static int mac80211_hwsim_get_survey(
 {
        struct ieee80211_conf *conf = &hw->conf;
 
-       printk(KERN_DEBUG "%s:%s (idx=%d)\n",
-              wiphy_name(hw->wiphy), __func__, idx);
+       wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
 
        if (idx != 0)
                return -ENOENT;
@@ -928,7 +1080,8 @@ static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
 static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
                                       struct ieee80211_vif *vif,
                                       enum ieee80211_ampdu_mlme_action action,
-                                      struct ieee80211_sta *sta, u16 tid, u16 *ssn)
+                                      struct ieee80211_sta *sta, u16 tid, u16 *ssn,
+                                      u8 buf_size)
 {
        switch (action) {
        case IEEE80211_AMPDU_TX_START:
@@ -951,12 +1104,7 @@ static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
 
 static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
 {
-       /*
-        * In this special case, there's nothing we need to
-        * do because hwsim does transmission synchronously.
-        * In the future, when it does transmissions via
-        * userspace, we may need to do something.
-        */
+       /* Not implemented, queues only on kernel side */
 }
 
 struct hw_scan_done {
@@ -990,6 +1138,8 @@ static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
        for (i = 0; i < req->n_channels; i++)
                printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
                        req->channels[i]->center_freq);
+       print_hex_dump(KERN_DEBUG, "scan IEs: ", DUMP_PREFIX_OFFSET,
+                       16, 1, req->ie, req->ie_len, 1);
 
        ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
 
@@ -1032,6 +1182,7 @@ static struct ieee80211_ops mac80211_hwsim_ops =
        .start = mac80211_hwsim_start,
        .stop = mac80211_hwsim_stop,
        .add_interface = mac80211_hwsim_add_interface,
+       .change_interface = mac80211_hwsim_change_interface,
        .remove_interface = mac80211_hwsim_remove_interface,
        .config = mac80211_hwsim_config,
        .configure_filter = mac80211_hwsim_configure_filter,
@@ -1103,12 +1254,14 @@ static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
        struct sk_buff *skb;
        struct ieee80211_pspoll *pspoll;
+       int _pid;
 
        if (!vp->assoc)
                return;
 
-       printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
-              wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
+       wiphy_debug(data->hw->wiphy,
+                   "%s: send PS-Poll to %pM for aid %d\n",
+                   __func__, vp->bssid, vp->aid);
 
        skb = dev_alloc_skb(sizeof(*pspoll));
        if (!skb)
@@ -1120,8 +1273,15 @@ static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
        pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
        memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
        memcpy(pspoll->ta, mac, ETH_ALEN);
-       if (!mac80211_hwsim_tx_frame(data->hw, skb))
-               printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
+
+       /* wmediumd mode check */
+       _pid = wmediumd_pid;
+
+       if (_pid)
+               return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
+
+       if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
+               printk(KERN_DEBUG "%s: PS-poll frame not ack'ed\n", __func__);
        dev_kfree_skb(skb);
 }
 
@@ -1132,12 +1292,14 @@ static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
        struct sk_buff *skb;
        struct ieee80211_hdr *hdr;
+       int _pid;
 
        if (!vp->assoc)
                return;
 
-       printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
-              wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
+       wiphy_debug(data->hw->wiphy,
+                   "%s: send data::nullfunc to %pM ps=%d\n",
+                   __func__, vp->bssid, ps);
 
        skb = dev_alloc_skb(sizeof(*hdr));
        if (!skb)
@@ -1150,7 +1312,14 @@ static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
        memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
        memcpy(hdr->addr2, mac, ETH_ALEN);
        memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
-       if (!mac80211_hwsim_tx_frame(data->hw, skb))
+
+       /* wmediumd mode check */
+       _pid = wmediumd_pid;
+
+       if (_pid)
+               return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
+
+       if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
                printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
        dev_kfree_skb(skb);
 }
@@ -1230,6 +1399,273 @@ DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
                        hwsim_fops_group_read, hwsim_fops_group_write,
                        "%llx\n");
 
+struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
+                            struct mac_address *addr)
+{
+       struct mac80211_hwsim_data *data;
+       bool _found = false;
+
+       spin_lock_bh(&hwsim_radio_lock);
+       list_for_each_entry(data, &hwsim_radios, list) {
+               if (memcmp(data->addresses[1].addr, addr,
+                         sizeof(struct mac_address)) == 0) {
+                       _found = true;
+                       break;
+               }
+       }
+       spin_unlock_bh(&hwsim_radio_lock);
+
+       if (!_found)
+               return NULL;
+
+       return data;
+}
+
+static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
+                                          struct genl_info *info)
+{
+
+       struct ieee80211_hdr *hdr;
+       struct mac80211_hwsim_data *data2;
+       struct ieee80211_tx_info *txi;
+       struct hwsim_tx_rate *tx_attempts;
+       struct sk_buff __user *ret_skb;
+       struct sk_buff *skb, *tmp;
+       struct mac_address *src;
+       unsigned int hwsim_flags;
+
+       int i;
+       bool found = false;
+
+       if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
+          !info->attrs[HWSIM_ATTR_FLAGS] ||
+          !info->attrs[HWSIM_ATTR_COOKIE] ||
+          !info->attrs[HWSIM_ATTR_TX_INFO])
+               goto out;
+
+       src = (struct mac_address *)nla_data(
+                                  info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
+       hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
+
+       ret_skb = (struct sk_buff __user *)
+                 (unsigned long) nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
+
+       data2 = get_hwsim_data_ref_from_addr(src);
+
+       if (data2 == NULL)
+               goto out;
+
+       /* look for the skb matching the cookie passed back from user */
+       skb_queue_walk_safe(&data2->pending, skb, tmp) {
+               if (skb == ret_skb) {
+                       skb_unlink(skb, &data2->pending);
+                       found = true;
+                       break;
+               }
+       }
+
+       /* not found */
+       if (!found)
+               goto out;
+
+       /* Tx info received because the frame was broadcasted on user space,
+        so we get all the necessary info: tx attempts and skb control buff */
+
+       tx_attempts = (struct hwsim_tx_rate *)nla_data(
+                      info->attrs[HWSIM_ATTR_TX_INFO]);
+
+       /* now send back TX status */
+       txi = IEEE80211_SKB_CB(skb);
+
+       if (txi->control.vif)
+               hwsim_check_magic(txi->control.vif);
+       if (txi->control.sta)
+               hwsim_check_sta_magic(txi->control.sta);
+
+       ieee80211_tx_info_clear_status(txi);
+
+       for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
+               txi->status.rates[i].idx = tx_attempts[i].idx;
+               txi->status.rates[i].count = tx_attempts[i].count;
+               /*txi->status.rates[i].flags = 0;*/
+       }
+
+       txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
+
+       if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
+          (hwsim_flags & HWSIM_TX_STAT_ACK)) {
+               if (skb->len >= 16) {
+                       hdr = (struct ieee80211_hdr *) skb->data;
+                       mac80211_hwsim_monitor_ack(data2->hw, hdr->addr2);
+               }
+       }
+       ieee80211_tx_status_irqsafe(data2->hw, skb);
+       return 0;
+out:
+       return -EINVAL;
+
+}
+
+static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
+                                         struct genl_info *info)
+{
+
+       struct mac80211_hwsim_data  *data2;
+       struct ieee80211_rx_status rx_status;
+       struct mac_address *dst;
+       int frame_data_len;
+       char *frame_data;
+       struct sk_buff *skb = NULL;
+
+       if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
+          !info->attrs[HWSIM_ATTR_FRAME] ||
+          !info->attrs[HWSIM_ATTR_RX_RATE] ||
+          !info->attrs[HWSIM_ATTR_SIGNAL])
+               goto out;
+
+       dst = (struct mac_address *)nla_data(
+                                  info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
+
+       frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
+       frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
+
+       /* Allocate new skb here */
+       skb = alloc_skb(frame_data_len, GFP_KERNEL);
+       if (skb == NULL)
+               goto err;
+
+       if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
+               /* Copy the data */
+               memcpy(skb_put(skb, frame_data_len), frame_data,
+                      frame_data_len);
+       } else
+               goto err;
+
+       data2 = get_hwsim_data_ref_from_addr(dst);
+
+       if (data2 == NULL)
+               goto out;
+
+       /* check if radio is configured properly */
+
+       if (data2->idle || !data2->started || !data2->channel)
+               goto out;
+
+       /*A frame is received from user space*/
+       memset(&rx_status, 0, sizeof(rx_status));
+       rx_status.freq = data2->channel->center_freq;
+       rx_status.band = data2->channel->band;
+       rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
+       rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
+
+       memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
+       ieee80211_rx_irqsafe(data2->hw, skb);
+
+       return 0;
+err:
+       printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
+       goto out;
+out:
+       dev_kfree_skb(skb);
+       return -EINVAL;
+}
+
+static int hwsim_register_received_nl(struct sk_buff *skb_2,
+                                     struct genl_info *info)
+{
+       if (info == NULL)
+               goto out;
+
+       wmediumd_pid = info->snd_pid;
+
+       printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
+       "switching to wmediumd mode with pid %d\n", info->snd_pid);
+
+       return 0;
+out:
+       printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
+       return -EINVAL;
+}
+
+/* Generic Netlink operations array */
+static struct genl_ops hwsim_ops[] = {
+       {
+               .cmd = HWSIM_CMD_REGISTER,
+               .policy = hwsim_genl_policy,
+               .doit = hwsim_register_received_nl,
+               .flags = GENL_ADMIN_PERM,
+       },
+       {
+               .cmd = HWSIM_CMD_FRAME,
+               .policy = hwsim_genl_policy,
+               .doit = hwsim_cloned_frame_received_nl,
+       },
+       {
+               .cmd = HWSIM_CMD_TX_INFO_FRAME,
+               .policy = hwsim_genl_policy,
+               .doit = hwsim_tx_info_frame_received_nl,
+       },
+};
+
+static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
+                                        unsigned long state,
+                                        void *_notify)
+{
+       struct netlink_notify *notify = _notify;
+
+       if (state != NETLINK_URELEASE)
+               return NOTIFY_DONE;
+
+       if (notify->pid == wmediumd_pid) {
+               printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
+                      " socket, switching to perfect channel medium\n");
+               wmediumd_pid = 0;
+       }
+       return NOTIFY_DONE;
+
+}
+
+static struct notifier_block hwsim_netlink_notifier = {
+       .notifier_call = mac80211_hwsim_netlink_notify,
+};
+
+static int hwsim_init_netlink(void)
+{
+       int rc;
+       printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
+
+       wmediumd_pid = 0;
+
+       rc = genl_register_family_with_ops(&hwsim_genl_family,
+               hwsim_ops, ARRAY_SIZE(hwsim_ops));
+       if (rc)
+               goto failure;
+
+       rc = netlink_register_notifier(&hwsim_netlink_notifier);
+       if (rc)
+               goto failure;
+
+       return 0;
+
+failure:
+       printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
+       return -EINVAL;
+}
+
+static void hwsim_exit_netlink(void)
+{
+       int ret;
+
+       printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
+       /* unregister the notifier */
+       netlink_unregister_notifier(&hwsim_netlink_notifier);
+       /* unregister the family */
+       ret = genl_unregister_family(&hwsim_genl_family);
+       if (ret)
+               printk(KERN_DEBUG "mac80211_hwsim: "
+                      "unregister family %i\n", ret);
+}
+
 static int __init init_mac80211_hwsim(void)
 {
        int i, err = 0;
@@ -1280,6 +1716,7 @@ static int __init init_mac80211_hwsim(void)
                        goto failed_drvdata;
                }
                data->dev->driver = &mac80211_hwsim_driver;
+               skb_queue_head_init(&data->pending);
 
                SET_IEEE80211_DEV(hw, data->dev);
                addr[3] = i >> 8;
@@ -1290,11 +1727,19 @@ static int __init init_mac80211_hwsim(void)
                hw->wiphy->n_addresses = 2;
                hw->wiphy->addresses = data->addresses;
 
+               if (fake_hw_scan) {
+                       hw->wiphy->max_scan_ssids = 255;
+                       hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
+               }
+
                hw->channel_change_time = 1;
                hw->queues = 4;
                hw->wiphy->interface_modes =
                        BIT(NL80211_IFTYPE_STATION) |
                        BIT(NL80211_IFTYPE_AP) |
+                       BIT(NL80211_IFTYPE_P2P_CLIENT) |
+                       BIT(NL80211_IFTYPE_P2P_GO) |
+                       BIT(NL80211_IFTYPE_ADHOC) |
                        BIT(NL80211_IFTYPE_MESH_POINT);
 
                hw->flags = IEEE80211_HW_MFP_CAPABLE |
@@ -1303,6 +1748,8 @@ static int __init init_mac80211_hwsim(void)
                            IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
                            IEEE80211_HW_AMPDU_AGGREGATION;
 
+               hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
+
                /* ask mac80211 to reserve space for magic */
                hw->vif_data_size = sizeof(struct hwsim_vif_priv);
                hw->sta_data_size = sizeof(struct hwsim_sta_priv);
@@ -1353,6 +1800,10 @@ static int __init init_mac80211_hwsim(void)
                data->group = 1;
                mutex_init(&data->mutex);
 
+               /* Enable frame retransmissions for lossy channels */
+               hw->max_rates = 4;
+               hw->max_rate_tries = 11;
+
                /* Work to be done prior to ieee80211_register_hw() */
                switch (regtest) {
                case HWSIM_REGTEST_DISABLED:
@@ -1467,9 +1918,8 @@ static int __init init_mac80211_hwsim(void)
                        break;
                }
 
-               printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
-                      wiphy_name(hw->wiphy),
-                      hw->wiphy->perm_addr);
+               wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
+                           hw->wiphy->perm_addr);
 
                data->debugfs = debugfs_create_dir("hwsim",
                                                   hw->wiphy->debugfsdir);
@@ -1503,8 +1953,16 @@ static int __init init_mac80211_hwsim(void)
 
        rtnl_unlock();
 
+       err = hwsim_init_netlink();
+       if (err < 0)
+               goto failed_nl;
+
        return 0;
 
+failed_nl:
+       printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
+       return err;
+
 failed_mon:
        rtnl_unlock();
        free_netdev(hwsim_mon);
@@ -1525,6 +1983,8 @@ static void __exit exit_mac80211_hwsim(void)
 {
        printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
 
+       hwsim_exit_netlink();
+
        mac80211_hwsim_free();
        unregister_netdev(hwsim_mon);
 }