P2P: Add option for Provision Discovery before GO Negotiation
[mech_eap.git] / src / drivers / driver_test.c
index 9cd36ba..c18eaad 100644 (file)
@@ -2,14 +2,8 @@
  * Testing driver interface for a simulated network driver
  * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
  *
- * 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
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
@@ -34,6 +28,8 @@
 #include "common/ieee802_11_defs.h"
 #include "crypto/sha1.h"
 #include "l2_packet/l2_packet.h"
+#include "p2p/p2p.h"
+#include "wps/wps.h"
 #include "driver.h"
 
 
@@ -87,7 +83,6 @@ struct wpa_driver_test_data {
        int use_associnfo;
        u8 assoc_wpa_ie[80];
        size_t assoc_wpa_ie_len;
-       int use_mlme;
        int associated;
        u8 *probe_req_ie;
        size_t probe_req_ie_len;
@@ -107,6 +102,20 @@ struct wpa_driver_test_data {
        unsigned int remain_on_channel_duration;
 
        int current_freq;
+
+       struct p2p_data *p2p;
+       unsigned int off_channel_freq;
+       struct wpabuf *pending_action_tx;
+       u8 pending_action_src[ETH_ALEN];
+       u8 pending_action_dst[ETH_ALEN];
+       u8 pending_action_bssid[ETH_ALEN];
+       unsigned int pending_action_freq;
+       unsigned int pending_action_no_cck;
+       unsigned int pending_listen_freq;
+       unsigned int pending_listen_duration;
+       int pending_p2p_scan;
+       struct sockaddr *probe_from;
+       socklen_t probe_from_len;
 };
 
 
@@ -116,6 +125,7 @@ static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
 static void wpa_driver_test_close_test_socket(
        struct wpa_driver_test_data *drv);
 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx);
+static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv);
 
 
 static void test_driver_free_bss(struct test_driver_bss *bss)
@@ -159,7 +169,7 @@ test_driver_get_cli(struct wpa_driver_test_data *drv, struct sockaddr_un *from,
 
 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
                                  size_t data_len, int encrypt,
-                                 const u8 *own_addr)
+                                 const u8 *own_addr, u32 flags)
 {
        struct test_driver_bss *dbss = priv;
        struct wpa_driver_test_data *drv = dbss->drv;
@@ -293,7 +303,7 @@ static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
 
 
 static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
-                                    size_t data_len)
+                                    size_t data_len, int noack)
 {
        struct test_driver_bss *dbss = priv;
        struct wpa_driver_test_data *drv = dbss->drv;
@@ -469,6 +479,34 @@ static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
        event.tx_status.ack = ret >= 0;
        wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
 
+#ifdef CONFIG_P2P
+       if (drv->p2p &&
+           WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
+           WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
+               if (drv->pending_action_tx == NULL) {
+                       wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
+                                  "no pending operation");
+                       return ret;
+               }
+
+               if (os_memcmp(hdr->addr1, drv->pending_action_dst, ETH_ALEN) !=
+                   0) {
+                       wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
+                                  "unknown destination address");
+                       return ret;
+               }
+
+               wpabuf_free(drv->pending_action_tx);
+               drv->pending_action_tx = NULL;
+
+               p2p_send_action_cb(drv->p2p, drv->pending_action_freq,
+                                  drv->pending_action_dst,
+                                  drv->pending_action_src,
+                                  drv->pending_action_bssid,
+                                  ret >= 0);
+       }
+#endif /* CONFIG_P2P */
+
        return ret;
 }
 
@@ -515,6 +553,10 @@ static void test_driver_scan(struct wpa_driver_test_data *drv,
                event.rx_probe_req.ie = ie;
                event.rx_probe_req.ie_len = ielen;
                wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, &event);
+#ifdef CONFIG_P2P
+               if (drv->p2p)
+                       p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen);
+#endif /* CONFIG_P2P */
        }
 
        dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
@@ -624,7 +666,7 @@ static void test_driver_assoc(struct wpa_driver_test_data *drv,
        sendto(drv->test_socket, cmd, strlen(cmd), 0,
               (struct sockaddr *) from, fromlen);
 
-       drv_event_assoc(bss->bss_ctx, cli->addr, ie, ielen);
+       drv_event_assoc(bss->bss_ctx, cli->addr, ie, ielen, 0);
 }
 
 
@@ -840,9 +882,9 @@ static int test_driver_set_generic_elem(void *priv,
 }
 
 
-static int test_driver_set_ap_wps_ie(const char *ifname, void *priv,
-                                    const struct wpabuf *beacon,
-                                    const struct wpabuf *proberesp)
+static int test_driver_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
+                                    const struct wpabuf *proberesp,
+                                    const struct wpabuf *assocresp)
 {
        struct test_driver_bss *bss = priv;
 
@@ -1015,12 +1057,29 @@ static int test_driver_bss_remove(void *priv, const char *ifname)
 
 static int test_driver_if_add(void *priv, enum wpa_driver_if_type type,
                              const char *ifname, const u8 *addr,
-                             void *bss_ctx, void **drv_priv)
+                             void *bss_ctx, void **drv_priv,
+                             char *force_ifname, u8 *if_addr,
+                             const char *bridge)
 {
+       struct test_driver_bss *dbss = priv;
+       struct wpa_driver_test_data *drv = dbss->drv;
+
        wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s bss_ctx=%p)",
                   __func__, type, ifname, bss_ctx);
-       if (type == WPA_IF_AP_BSS)
-               return test_driver_bss_add(priv, ifname, addr, bss_ctx,
+       if (addr)
+               os_memcpy(if_addr, addr, ETH_ALEN);
+       else {
+               drv->alloc_iface_idx++;
+               if_addr[0] = 0x02; /* locally administered */
+               sha1_prf(drv->own_addr, ETH_ALEN,
+                        "hostapd test addr generation",
+                        (const u8 *) &drv->alloc_iface_idx,
+                        sizeof(drv->alloc_iface_idx),
+                        if_addr + 1, ETH_ALEN - 1);
+       }
+       if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
+           type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
+               return test_driver_bss_add(priv, ifname, if_addr, bss_ctx,
                                           drv_priv);
        return 0;
 }
@@ -1030,27 +1089,23 @@ static int test_driver_if_remove(void *priv, enum wpa_driver_if_type type,
                                 const char *ifname)
 {
        wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
-       if (type == WPA_IF_AP_BSS)
+       if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
+           type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
                return test_driver_bss_remove(priv, ifname);
        return 0;
 }
 
 
-static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
-                                     const u8 *mask)
-{
-       return 0;
-}
-
-
 static int test_driver_set_ssid(void *priv, const u8 *buf, int len)
 {
        struct test_driver_bss *bss = priv;
 
        wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, bss->ifname);
+       if (len < 0)
+               return -1;
        wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
 
-       if (len < 0 || (size_t) len > sizeof(bss->ssid))
+       if ((size_t) len > sizeof(bss->ssid))
                return -1;
 
        os_memcpy(bss->ssid, buf, len);
@@ -1164,6 +1219,7 @@ static void * test_driver_init(struct hostapd_data *hapd,
                return NULL;
        drv->ap = 1;
        bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
+       drv->global = params->global_priv;
 
        bss->bss_ctx = hapd;
        os_memcpy(bss->bssid, drv->own_addr, ETH_ALEN);
@@ -1219,7 +1275,7 @@ static void * test_driver_init(struct hostapd_data *hapd,
                        alen = sizeof(addr_un);
                }
                if (bind(drv->test_socket, addr, alen) < 0) {
-                       perror("bind(PF_UNIX)");
+                       perror("test-driver-init: bind(PF_UNIX)");
                        close(drv->test_socket);
                        if (drv->own_socket_path)
                                unlink(drv->own_socket_path);
@@ -1257,7 +1313,23 @@ static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
 
 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
 {
+       struct wpa_driver_test_data *drv = eloop_ctx;
        wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
+       if (drv->pending_p2p_scan && drv->p2p) {
+#ifdef CONFIG_P2P
+               size_t i;
+               for (i = 0; i < drv->num_scanres; i++) {
+                       struct wpa_scan_res *bss = drv->scanres[i];
+                       if (p2p_scan_res_handler(drv->p2p, bss->bssid,
+                                                bss->freq, bss->level,
+                                                (const u8 *) (bss + 1),
+                                                bss->ie_len) > 0)
+                               return;
+               }
+               p2p_scan_res_handled(drv->p2p);
+#endif /* CONFIG_P2P */
+               return;
+       }
        wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
 }
 
@@ -1473,6 +1545,7 @@ static int wpa_driver_test_associate(
                   __func__, priv, params->freq, params->pairwise_suite,
                   params->group_suite, params->key_mgmt_suite,
                   params->auth_alg, params->mode);
+       wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
        if (params->bssid) {
                wpa_printf(MSG_DEBUG, "   bssid=" MACSTR,
                           MAC2STR(params->bssid));
@@ -1834,6 +1907,8 @@ static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
 {
        int freq = 0, own_freq;
        union wpa_event_data event;
+       const struct ieee80211_mgmt *mgmt;
+       u16 fc;
        struct test_driver_bss *bss;
 
        bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
@@ -1871,23 +1946,45 @@ static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
        event.mlme_rx.freq = freq;
        wpa_supplicant_event(drv->ctx, EVENT_MLME_RX, &event);
 
-       if (drv->probe_req_report && data_len >= 24) {
-               const struct ieee80211_mgmt *mgmt;
-               u16 fc;
+       mgmt = (const struct ieee80211_mgmt *) data;
+       fc = le_to_host16(mgmt->frame_control);
 
-               mgmt = (const struct ieee80211_mgmt *) data;
-               fc = le_to_host16(mgmt->frame_control);
+       if (drv->probe_req_report && data_len >= 24) {
                if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
                    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ) {
                        os_memset(&event, 0, sizeof(event));
                        event.rx_probe_req.sa = mgmt->sa;
+                       event.rx_probe_req.da = mgmt->da;
+                       event.rx_probe_req.bssid = mgmt->bssid;
                        event.rx_probe_req.ie = mgmt->u.probe_req.variable;
                        event.rx_probe_req.ie_len =
                                data_len - (mgmt->u.probe_req.variable - data);
                        wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ,
                                             &event);
+#ifdef CONFIG_P2P
+                       if (drv->p2p)
+                               p2p_probe_req_rx(drv->p2p, mgmt->sa,
+                                                mgmt->da, mgmt->bssid,
+                                                event.rx_probe_req.ie,
+                                                event.rx_probe_req.ie_len);
+#endif /* CONFIG_P2P */
                }
        }
+
+#ifdef CONFIG_P2P
+       if (drv->p2p &&
+           WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
+           WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
+               size_t hdr_len;
+               hdr_len = (const u8 *)
+                       &mgmt->u.action.u.vs_public_action.action - data;
+               p2p_rx_action(drv->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
+                             mgmt->u.action.category,
+                             &mgmt->u.action.u.vs_public_action.action,
+                             data_len - hdr_len, freq);
+       }
+#endif /* CONFIG_P2P */
+
 }
 
 
@@ -1903,6 +2000,29 @@ static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
        bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
 
        /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
+#ifdef CONFIG_P2P
+       if (drv->probe_req_report && drv->p2p && data_len) {
+               const char *d = (const char *) data;
+               u8 sa[ETH_ALEN];
+               u8 ie[512];
+               size_t ielen;
+
+               if (hwaddr_aton(d, sa))
+                       return;
+               d += 18;
+               while (*d == ' ')
+                       d++;
+               ielen = os_strlen(d) / 2;
+               if (ielen > sizeof(ie))
+                       ielen = sizeof(ie);
+               if (hexstr2bin(d, ie, ielen) < 0)
+                       ielen = 0;
+               drv->probe_from = from;
+               drv->probe_from_len = fromlen;
+               p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen);
+               drv->probe_from = NULL;
+       }
+#endif /* CONFIG_P2P */
 
        if (!drv->ibss)
                return;
@@ -2059,6 +2179,12 @@ static void wpa_driver_test_deinit(void *priv)
        struct test_client_socket *cli, *prev;
        int i;
 
+#ifdef CONFIG_P2P
+       if (drv->p2p)
+               p2p_deinit(drv->p2p);
+       wpabuf_free(drv->pending_action_tx);
+#endif /* CONFIG_P2P */
+
        cli = drv->cli;
        while (cli) {
                prev = cli;
@@ -2126,7 +2252,7 @@ static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
        os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
        if (bind(drv->test_socket, (struct sockaddr *) &addr,
                 sizeof(addr)) < 0) {
-               perror("bind(PF_UNIX)");
+               perror("test-driver-attach: bind(PF_UNIX)");
                close(drv->test_socket);
                unlink(drv->own_socket_path);
                os_free(drv->own_socket_path);
@@ -2255,12 +2381,12 @@ static int wpa_driver_test_set_param(void *priv, const char *param)
                drv->use_associnfo = 1;
        }
 
-#ifdef CONFIG_CLIENT_MLME
-       if (os_strstr(param, "use_mlme=1")) {
-               wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
-               drv->use_mlme = 1;
+       if (os_strstr(param, "p2p_mgmt=1")) {
+               wpa_printf(MSG_DEBUG, "test_driver: Use internal P2P "
+                          "management");
+               if (wpa_driver_test_init_p2p(drv) < 0)
+                       return -1;
        }
-#endif /* CONFIG_CLIENT_MLME */
 
        return 0;
 }
@@ -2368,10 +2494,14 @@ static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
        capa->auth = WPA_DRIVER_AUTH_OPEN |
                WPA_DRIVER_AUTH_SHARED |
                WPA_DRIVER_AUTH_LEAP;
-       if (drv->use_mlme)
-               capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
+       if (drv->p2p)
+               capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT;
        capa->flags |= WPA_DRIVER_FLAGS_AP;
+       capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
+       capa->flags |= WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE;
+       capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
        capa->max_scan_ssids = 2;
+       capa->max_remain_on_chan = 60000;
 
        return 0;
 }
@@ -2393,50 +2523,6 @@ static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
 }
 
 
-static int wpa_driver_test_set_channel(void *priv,
-                                      enum hostapd_hw_mode phymode,
-                                      int chan, int freq)
-{
-       struct test_driver_bss *dbss = priv;
-       struct wpa_driver_test_data *drv = dbss->drv;
-       wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
-                  __func__, phymode, chan, freq);
-       drv->current_freq = freq;
-       return 0;
-}
-
-
-static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
-                                       const u8 *supp_rates,
-                                       size_t supp_rates_len)
-{
-       wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
-       return 0;
-}
-
-
-static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
-{
-       wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
-       return 0;
-}
-
-
-static int wpa_driver_test_set_ssid(void *priv, const u8 *ssid,
-                                   size_t ssid_len)
-{
-       wpa_printf(MSG_DEBUG, "%s", __func__);
-       return 0;
-}
-
-
-static int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
-{
-       wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
-       return 0;
-}
-
-
 static void * wpa_driver_test_global_init(void)
 {
        struct wpa_driver_test_global *global;
@@ -2576,9 +2662,11 @@ static int wpa_driver_test_set_freq(void *priv,
 
 
 static int wpa_driver_test_send_action(void *priv, unsigned int freq,
+                                      unsigned int wait,
                                       const u8 *dst, const u8 *src,
                                       const u8 *bssid,
-                                      const u8 *data, size_t data_len)
+                                      const u8 *data, size_t data_len,
+                                      int no_cck)
 {
        struct test_driver_bss *dbss = priv;
        struct wpa_driver_test_data *drv = dbss->drv;
@@ -2611,34 +2699,37 @@ static int wpa_driver_test_send_action(void *priv, unsigned int freq,
        os_memcpy(hdr->addr2, src, ETH_ALEN);
        os_memcpy(hdr->addr3, bssid, ETH_ALEN);
 
-       ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len);
+       ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len, 0);
        os_free(buf);
        return ret;
 }
 
 
-static int wpa_driver_test_alloc_interface_addr(void *priv, u8 *addr,
-                                               char *ifname)
+#ifdef CONFIG_P2P
+static void test_send_action_cb(void *eloop_ctx, void *timeout_ctx)
 {
-       struct test_driver_bss *dbss = priv;
-       struct wpa_driver_test_data *drv = dbss->drv;
-
-       if (ifname)
-               ifname[0] = '\0';
-
-       drv->alloc_iface_idx++;
-       addr[0] = 0x02; /* locally administered */
-       sha1_prf(drv->own_addr, ETH_ALEN, "hostapd test addr generation",
-                (const u8 *) &drv->alloc_iface_idx,
-                sizeof(drv->alloc_iface_idx),
-                addr + 1, ETH_ALEN - 1);
-       return 0;
-}
+       struct wpa_driver_test_data *drv = eloop_ctx;
 
+       if (drv->pending_action_tx == NULL)
+               return;
 
-static void wpa_driver_test_release_interface_addr(void *priv, const u8 *addr)
-{
+       if (drv->off_channel_freq != drv->pending_action_freq) {
+               wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
+                          "waiting for another freq=%u",
+                          drv->pending_action_freq);
+               return;
+       }
+       wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
+                  MACSTR, MAC2STR(drv->pending_action_dst));
+       wpa_driver_test_send_action(drv, drv->pending_action_freq, 0,
+                                   drv->pending_action_dst,
+                                   drv->pending_action_src,
+                                   drv->pending_action_bssid,
+                                   wpabuf_head(drv->pending_action_tx),
+                                   wpabuf_len(drv->pending_action_tx),
+                                   drv->pending_action_no_cck);
 }
+#endif /* CONFIG_P2P */
 
 
 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx)
@@ -2651,9 +2742,13 @@ static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx)
        os_memset(&data, 0, sizeof(data));
        data.remain_on_channel.freq = drv->remain_on_channel_freq;
        data.remain_on_channel.duration = drv->remain_on_channel_duration;
-       wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data);
+
+       if (drv->p2p)
+               drv->off_channel_freq = 0;
 
        drv->remain_on_channel_freq = 0;
+
+       wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data);
 }
 
 
@@ -2684,6 +2779,18 @@ static int wpa_driver_test_remain_on_channel(void *priv, unsigned int freq,
        data.remain_on_channel.duration = duration;
        wpa_supplicant_event(drv->ctx, EVENT_REMAIN_ON_CHANNEL, &data);
 
+#ifdef CONFIG_P2P
+       if (drv->p2p) {
+               drv->off_channel_freq = drv->remain_on_channel_freq;
+               test_send_action_cb(drv, NULL);
+               if (drv->off_channel_freq == drv->pending_listen_freq) {
+                       p2p_listen_cb(drv->p2p, drv->pending_listen_freq,
+                                     drv->pending_listen_duration);
+                       drv->pending_listen_freq = 0;
+               }
+       }
+#endif /* CONFIG_P2P */
+
        return 0;
 }
 
@@ -2711,6 +2818,462 @@ static int wpa_driver_test_probe_req_report(void *priv, int report)
 }
 
 
+#ifdef CONFIG_P2P
+
+static int wpa_driver_test_p2p_find(void *priv, unsigned int timeout, int type)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
+       if (!drv->p2p)
+               return -1;
+       return p2p_find(drv->p2p, timeout, type, 0, NULL, NULL);
+}
+
+
+static int wpa_driver_test_p2p_stop_find(void *priv)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       if (!drv->p2p)
+               return -1;
+       p2p_stop_find(drv->p2p);
+       return 0;
+}
+
+
+static int wpa_driver_test_p2p_listen(void *priv, unsigned int timeout)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
+       if (!drv->p2p)
+               return -1;
+       return p2p_listen(drv->p2p, timeout);
+}
+
+
+static int wpa_driver_test_p2p_connect(void *priv, const u8 *peer_addr,
+                                      int wps_method, int go_intent,
+                                      const u8 *own_interface_addr,
+                                      unsigned int force_freq,
+                                      int persistent_group)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR " wps_method=%d "
+                  "go_intent=%d "
+                  "own_interface_addr=" MACSTR " force_freq=%u "
+                  "persistent_group=%d)",
+                  __func__, MAC2STR(peer_addr), wps_method, go_intent,
+                  MAC2STR(own_interface_addr), force_freq, persistent_group);
+       if (!drv->p2p)
+               return -1;
+       return p2p_connect(drv->p2p, peer_addr, wps_method, go_intent,
+                          own_interface_addr, force_freq, persistent_group,
+                          NULL, 0, 0);
+}
+
+
+static int wpa_driver_test_wps_success_cb(void *priv, const u8 *peer_addr)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR ")",
+                  __func__, MAC2STR(peer_addr));
+       if (!drv->p2p)
+               return -1;
+       p2p_wps_success_cb(drv->p2p, peer_addr);
+       return 0;
+}
+
+
+static int wpa_driver_test_p2p_group_formation_failed(void *priv)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       if (!drv->p2p)
+               return -1;
+       p2p_group_formation_failed(drv->p2p);
+       return 0;
+}
+
+
+static int wpa_driver_test_p2p_set_params(void *priv,
+                                         const struct p2p_params *params)
+{
+       struct wpa_driver_test_data *drv = priv;
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       if (!drv->p2p)
+               return -1;
+       if (p2p_set_dev_name(drv->p2p, params->dev_name) < 0 ||
+           p2p_set_pri_dev_type(drv->p2p, params->pri_dev_type) < 0 ||
+           p2p_set_sec_dev_types(drv->p2p, params->sec_dev_type,
+                                 params->num_sec_dev_types) < 0)
+               return -1;
+       return 0;
+}
+
+
+static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
+                        unsigned int num_req_dev_types,
+                        const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
+{
+       struct wpa_driver_test_data *drv = ctx;
+       struct wpa_driver_scan_params params;
+       int ret;
+       struct wpabuf *wps_ie, *ies;
+       int social_channels[] = { 2412, 2437, 2462, 0, 0 };
+       size_t ielen;
+
+       wpa_printf(MSG_DEBUG, "%s(type=%d freq=%d)",
+                  __func__, type, freq);
+
+       os_memset(&params, 0, sizeof(params));
+
+       /* P2P Wildcard SSID */
+       params.num_ssids = 1;
+       params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
+       params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
+
+#if 0 /* TODO: WPS IE */
+       wpa_s->wps->dev.p2p = 1;
+       wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
+                                       wpa_s->wps->uuid, WPS_REQ_ENROLLEE);
+#else
+       wps_ie = wpabuf_alloc(1);
+#endif
+       if (wps_ie == NULL)
+               return -1;
+
+       ielen = p2p_scan_ie_buf_len(drv->p2p);
+       ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
+       if (ies == NULL) {
+               wpabuf_free(wps_ie);
+               return -1;
+       }
+       wpabuf_put_buf(ies, wps_ie);
+       wpabuf_free(wps_ie);
+
+       p2p_scan_ie(drv->p2p, ies, dev_id);
+
+       params.extra_ies = wpabuf_head(ies);
+       params.extra_ies_len = wpabuf_len(ies);
+
+       switch (type) {
+       case P2P_SCAN_SOCIAL:
+               params.freqs = social_channels;
+               break;
+       case P2P_SCAN_FULL:
+               break;
+       case P2P_SCAN_SPECIFIC:
+               social_channels[0] = freq;
+               social_channels[1] = 0;
+               params.freqs = social_channels;
+               break;
+       case P2P_SCAN_SOCIAL_PLUS_ONE:
+               social_channels[3] = freq;
+               params.freqs = social_channels;
+               break;
+       }
+
+       drv->pending_p2p_scan = 1;
+       ret = wpa_driver_test_scan(drv, &params);
+
+       wpabuf_free(ies);
+
+       return ret;
+}
+
+
+static int test_send_action(void *ctx, unsigned int freq, const u8 *dst,
+                           const u8 *src, const u8 *bssid, const u8 *buf,
+                           size_t len, unsigned int wait_time)
+{
+       struct wpa_driver_test_data *drv = ctx;
+
+       wpa_printf(MSG_DEBUG, "%s(freq=%u dst=" MACSTR " src=" MACSTR
+                  " bssid=" MACSTR " len=%d",
+                  __func__, freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
+                  (int) len);
+       if (freq <= 0) {
+               wpa_printf(MSG_WARNING, "P2P: No frequency specified for "
+                          "action frame TX");
+               return -1;
+       }
+
+       if (drv->pending_action_tx) {
+               wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
+                          "to " MACSTR, MAC2STR(drv->pending_action_dst));
+               wpabuf_free(drv->pending_action_tx);
+       }
+       drv->pending_action_tx = wpabuf_alloc(len);
+       if (drv->pending_action_tx == NULL)
+               return -1;
+       wpabuf_put_data(drv->pending_action_tx, buf, len);
+       os_memcpy(drv->pending_action_src, src, ETH_ALEN);
+       os_memcpy(drv->pending_action_dst, dst, ETH_ALEN);
+       os_memcpy(drv->pending_action_bssid, bssid, ETH_ALEN);
+       drv->pending_action_freq = freq;
+       drv->pending_action_no_cck = 1;
+
+       if (drv->off_channel_freq == freq) {
+               /* Already on requested channel; send immediately */
+               /* TODO: Would there ever be need to extend the current
+                * duration on the channel? */
+               eloop_cancel_timeout(test_send_action_cb, drv, NULL);
+               eloop_register_timeout(0, 0, test_send_action_cb, drv, NULL);
+               return 0;
+       }
+
+       wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
+                  "once the driver gets to the requested channel");
+       if (wpa_driver_test_remain_on_channel(drv, freq, wait_time) < 0) {
+               wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
+                          "to remain on channel (%u MHz) for Action "
+                          "Frame TX", freq);
+               return -1;
+       }
+
+       return 0;
+}
+
+
+static void test_send_action_done(void *ctx)
+{
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       /* TODO */
+}
+
+
+static void test_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
+{
+       struct wpa_driver_test_data *drv = ctx;
+       union wpa_event_data event;
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       os_memset(&event, 0, sizeof(event));
+       event.p2p_go_neg_completed.res = res;
+       wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_COMPLETED, &event);
+}
+
+
+static void test_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
+{
+       struct wpa_driver_test_data *drv = ctx;
+       union wpa_event_data event;
+       wpa_printf(MSG_DEBUG, "%s(src=" MACSTR ")", __func__, MAC2STR(src));
+       os_memset(&event, 0, sizeof(event));
+       event.p2p_go_neg_req_rx.src = src;
+       event.p2p_go_neg_req_rx.dev_passwd_id = dev_passwd_id;
+       wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_REQ_RX, &event);
+}
+
+
+static void test_dev_found(void *ctx, const u8 *addr,
+                          const struct p2p_peer_info *info, int new_device)
+{
+       struct wpa_driver_test_data *drv = ctx;
+       union wpa_event_data event;
+       char devtype[WPS_DEV_TYPE_BUFSIZE];
+       wpa_printf(MSG_DEBUG, "%s(" MACSTR " p2p_dev_addr=" MACSTR
+                  " pri_dev_type=%s name='%s' config_methods=0x%x "
+                  "dev_capab=0x%x group_capab=0x%x)",
+                  __func__, MAC2STR(addr), MAC2STR(info->p2p_device_addr),
+                  wps_dev_type_bin2str(info->pri_dev_type, devtype,
+                                       sizeof(devtype)),
+                  info->device_name, info->config_methods, info->dev_capab,
+                  info->group_capab);
+
+       os_memset(&event, 0, sizeof(event));
+       event.p2p_dev_found.addr = addr;
+       event.p2p_dev_found.dev_addr = info->p2p_device_addr;
+       event.p2p_dev_found.pri_dev_type = info->pri_dev_type;
+       event.p2p_dev_found.dev_name = info->device_name;
+       event.p2p_dev_found.config_methods = info->config_methods;
+       event.p2p_dev_found.dev_capab = info->dev_capab;
+       event.p2p_dev_found.group_capab = info->group_capab;
+       wpa_supplicant_event(drv->ctx, EVENT_P2P_DEV_FOUND, &event);
+}
+
+
+static int test_start_listen(void *ctx, unsigned int freq,
+                            unsigned int duration,
+                            const struct wpabuf *probe_resp_ie)
+{
+       struct wpa_driver_test_data *drv = ctx;
+
+       wpa_printf(MSG_DEBUG, "%s(freq=%u duration=%u)",
+                  __func__, freq, duration);
+
+       if (wpa_driver_test_probe_req_report(drv, 1) < 0)
+               return -1;
+
+       drv->pending_listen_freq = freq;
+       drv->pending_listen_duration = duration;
+
+       if (wpa_driver_test_remain_on_channel(drv, freq, duration) < 0) {
+               drv->pending_listen_freq = 0;
+               return -1;
+       }
+
+       return 0;
+}
+
+
+static void test_stop_listen(void *ctx)
+{
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       /* TODO */
+}
+
+
+static int test_send_probe_resp(void *ctx, const struct wpabuf *buf)
+{
+       struct wpa_driver_test_data *drv = ctx;
+       char resp[512], *pos, *end;
+       int ret;
+       const struct ieee80211_mgmt *mgmt;
+       const u8 *ie, *ie_end;
+
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       wpa_hexdump_buf(MSG_MSGDUMP, "Probe Response", buf);
+       if (wpabuf_len(buf) < 24)
+               return -1;
+       if (!drv->probe_from) {
+               wpa_printf(MSG_DEBUG, "%s: probe_from not set", __func__);
+               return -1;
+       }
+
+       pos = resp;
+       end = resp + sizeof(resp);
+
+       mgmt = wpabuf_head(buf);
+
+       /* reply: SCANRESP BSSID SSID IEs */
+       ret = os_snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
+                         MAC2STR(mgmt->bssid));
+       if (ret < 0 || ret >= end - pos)
+               return -1;
+       pos += ret;
+
+       ie = mgmt->u.probe_resp.variable;
+       ie_end = wpabuf_head_u8(buf) + wpabuf_len(buf);
+       if (ie_end - ie < 2 || ie[0] != WLAN_EID_SSID ||
+           ie + 2 + ie[1] > ie_end)
+               return -1;
+       pos += wpa_snprintf_hex(pos, end - pos, ie + 2, ie[1]);
+
+       ret = os_snprintf(pos, end - pos, " ");
+       if (ret < 0 || ret >= end - pos)
+               return -1;
+       pos += ret;
+       pos += wpa_snprintf_hex(pos, end - pos, ie, ie_end - ie);
+
+       sendto(drv->test_socket, resp, pos - resp, 0,
+              drv->probe_from, drv->probe_from_len);
+
+       return 0;
+}
+
+
+static void test_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
+                           u16 update_indic, const u8 *tlvs, size_t tlvs_len)
+{
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       /* TODO */
+}
+
+
+static void test_sd_response(void *ctx, const u8 *sa, u16 update_indic,
+                            const u8 *tlvs, size_t tlvs_len)
+{
+       wpa_printf(MSG_DEBUG, "%s", __func__);
+       /* TODO */
+}
+
+
+static void test_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
+                              const u8 *dev_addr, const u8 *pri_dev_type,
+                              const char *dev_name, u16 supp_config_methods,
+                              u8 dev_capab, u8 group_capab,
+                              const u8 *group_id, size_t group_id_len)
+{
+       wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
+                  __func__, MAC2STR(peer), config_methods);
+       /* TODO */
+}
+
+
+static void test_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
+{
+       wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
+                  __func__, MAC2STR(peer), config_methods);
+       /* TODO */
+}
+
+#endif /* CONFIG_P2P */
+
+
+static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv)
+{
+#ifdef CONFIG_P2P
+       struct p2p_config p2p;
+       unsigned int r;
+       int i;
+
+       os_memset(&p2p, 0, sizeof(p2p));
+       p2p.msg_ctx = drv->ctx;
+       p2p.cb_ctx = drv;
+       p2p.p2p_scan = test_p2p_scan;
+       p2p.send_action = test_send_action;
+       p2p.send_action_done = test_send_action_done;
+       p2p.go_neg_completed = test_go_neg_completed;
+       p2p.go_neg_req_rx = test_go_neg_req_rx;
+       p2p.dev_found = test_dev_found;
+       p2p.start_listen = test_start_listen;
+       p2p.stop_listen = test_stop_listen;
+       p2p.send_probe_resp = test_send_probe_resp;
+       p2p.sd_request = test_sd_request;
+       p2p.sd_response = test_sd_response;
+       p2p.prov_disc_req = test_prov_disc_req;
+       p2p.prov_disc_resp = test_prov_disc_resp;
+
+       os_memcpy(p2p.dev_addr, drv->own_addr, ETH_ALEN);
+
+       p2p.reg_class = 12; /* TODO: change depending on location */
+       /*
+        * Pick one of the social channels randomly as the listen
+        * channel.
+        */
+       os_get_random((u8 *) &r, sizeof(r));
+       p2p.channel = 1 + (r % 3) * 5;
+
+       /* TODO: change depending on location */
+       p2p.op_reg_class = 12;
+       /*
+        * For initial tests, pick the operation channel randomly.
+        * TODO: Use scan results (etc.) to select the best channel.
+        */
+       p2p.op_channel = 1 + r % 11;
+
+       os_memcpy(p2p.country, "US ", 3);
+
+       /* FIX: fetch available channels from the driver */
+       p2p.channels.reg_classes = 1;
+       p2p.channels.reg_class[0].reg_class = 12; /* US/12 = 2.4 GHz band */
+       p2p.channels.reg_class[0].channels = 11;
+       for (i = 0; i < 11; i++)
+               p2p.channels.reg_class[0].channel[i] = i + 1;
+
+       p2p.max_peers = 100;
+
+       drv->p2p = p2p_init(&p2p);
+       if (drv->p2p == NULL)
+               return -1;
+       return 0;
+#else /* CONFIG_P2P */
+       wpa_printf(MSG_INFO, "driver_test: P2P support not included");
+       return -1;
+#endif /* CONFIG_P2P */
+}
+
+
 const struct wpa_driver_ops wpa_driver_test_ops = {
        "test",
        "wpa_supplicant test driver",
@@ -2724,7 +3287,6 @@ const struct wpa_driver_ops wpa_driver_test_ops = {
        .get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
        .if_add = test_driver_if_add,
        .if_remove = test_driver_if_remove,
-       .valid_bss_mask = test_driver_valid_bss_mask,
        .hapd_set_ssid = test_driver_set_ssid,
        .set_privacy = test_driver_set_privacy,
        .set_sta_vlan = test_driver_set_sta_vlan,
@@ -2743,11 +3305,6 @@ const struct wpa_driver_ops wpa_driver_test_ops = {
        .get_mac_addr = wpa_driver_test_get_mac_addr,
        .send_eapol = wpa_driver_test_send_eapol,
        .mlme_setprotection = wpa_driver_test_mlme_setprotection,
-       .set_channel = wpa_driver_test_set_channel,
-       .set_ssid = wpa_driver_test_set_ssid,
-       .set_bssid = wpa_driver_test_set_bssid,
-       .mlme_add_sta = wpa_driver_test_mlme_add_sta,
-       .mlme_remove_sta = wpa_driver_test_mlme_remove_sta,
        .get_scan_results2 = wpa_driver_test_get_scan_results2,
        .global_init = wpa_driver_test_global_init,
        .global_deinit = wpa_driver_test_global_deinit,
@@ -2756,9 +3313,17 @@ const struct wpa_driver_ops wpa_driver_test_ops = {
        .scan2 = wpa_driver_test_scan,
        .set_freq = wpa_driver_test_set_freq,
        .send_action = wpa_driver_test_send_action,
-       .alloc_interface_addr = wpa_driver_test_alloc_interface_addr,
-       .release_interface_addr = wpa_driver_test_release_interface_addr,
        .remain_on_channel = wpa_driver_test_remain_on_channel,
        .cancel_remain_on_channel = wpa_driver_test_cancel_remain_on_channel,
        .probe_req_report = wpa_driver_test_probe_req_report,
+#ifdef CONFIG_P2P
+       .p2p_find = wpa_driver_test_p2p_find,
+       .p2p_stop_find = wpa_driver_test_p2p_stop_find,
+       .p2p_listen = wpa_driver_test_p2p_listen,
+       .p2p_connect = wpa_driver_test_p2p_connect,
+       .wps_success_cb = wpa_driver_test_wps_success_cb,
+       .p2p_group_formation_failed =
+       wpa_driver_test_p2p_group_formation_failed,
+       .p2p_set_params = wpa_driver_test_p2p_set_params,
+#endif /* CONFIG_P2P */
 };