P2P NFC: Optimize join-a-group operation based on NFC information
[mech_eap.git] / src / p2p / p2p.c
index b196e6f..64dc8bd 100644 (file)
@@ -12,7 +12,6 @@
 #include "eloop.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
-#include "common/wpa_ctrl.h"
 #include "wps/wps_i.h"
 #include "p2p_i.h"
 #include "p2p.h"
@@ -42,21 +41,32 @@ static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
  * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
  * entries will be removed
  */
-#define P2P_PEER_EXPIRATION_AGE 300
+#ifndef P2P_PEER_EXPIRATION_AGE
+#define P2P_PEER_EXPIRATION_AGE 60
+#endif /* P2P_PEER_EXPIRATION_AGE */
 
 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
 
 static void p2p_expire_peers(struct p2p_data *p2p)
 {
        struct p2p_device *dev, *n;
-       struct os_time now;
+       struct os_reltime now;
        size_t i;
 
-       os_get_time(&now);
+       os_get_reltime(&now);
        dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
                if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
                        continue;
 
+               if (dev == p2p->go_neg_peer) {
+                       /*
+                        * GO Negotiation is in progress with the peer, so
+                        * don't expire the peer entry until GO Negotiation
+                        * fails or times out.
+                        */
+                       continue;
+               }
+
                if (p2p->cfg->go_connected &&
                    p2p->cfg->go_connected(p2p->cfg->cb_ctx,
                                           dev->info.p2p_device_addr)) {
@@ -64,7 +74,7 @@ static void p2p_expire_peers(struct p2p_data *p2p)
                         * We are connected as a client to a group in which the
                         * peer is the GO, so do not expire the peer entry.
                         */
-                       os_get_time(&dev->last_seen);
+                       os_get_reltime(&dev->last_seen);
                        continue;
                }
 
@@ -78,12 +88,12 @@ static void p2p_expire_peers(struct p2p_data *p2p)
                         * The peer is connected as a client in a group where
                         * we are the GO, so do not expire the peer entry.
                         */
-                       os_get_time(&dev->last_seen);
+                       os_get_reltime(&dev->last_seen);
                        continue;
                }
 
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
-                       "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
+               p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
+                       MAC2STR(dev->info.p2p_device_addr));
                dl_list_del(&dev->list);
                p2p_device_free(p2p, dev);
        }
@@ -128,14 +138,18 @@ static const char * p2p_state_txt(int state)
                return "INVITE";
        case P2P_INVITE_LISTEN:
                return "INVITE_LISTEN";
-       case P2P_SEARCH_WHEN_READY:
-               return "SEARCH_WHEN_READY";
        default:
                return "?";
        }
 }
 
 
+const char * p2p_get_state_txt(struct p2p_data *p2p)
+{
+       return p2p_state_txt(p2p->state);
+}
+
+
 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
 {
        struct p2p_device *dev = NULL;
@@ -151,14 +165,14 @@ u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
 }
 
 
-void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *iface_addr)
+void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
 {
        struct p2p_device *dev = NULL;
 
-       if (!iface_addr || !p2p)
+       if (!addr || !p2p)
                return;
 
-       dev = p2p_get_device_interface(p2p, iface_addr);
+       dev = p2p_get_device(p2p, addr);
        if (dev)
                dev->wps_prov_info = 0;
 }
@@ -166,7 +180,7 @@ void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *iface_addr)
 
 void p2p_set_state(struct p2p_data *p2p, int new_state)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
+       p2p_dbg(p2p, "State %s -> %s",
                p2p_state_txt(p2p->state), p2p_state_txt(new_state));
        p2p->state = new_state;
 }
@@ -174,8 +188,7 @@ void p2p_set_state(struct p2p_data *p2p, int new_state)
 
 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Set timeout (state=%s): %u.%06u sec",
+       p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
                p2p_state_txt(p2p->state), sec, usec);
        eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
        eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
@@ -184,8 +197,7 @@ void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
 
 void p2p_clear_timeout(struct p2p_data *p2p)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
-               p2p_state_txt(p2p->state));
+       p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
        eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
 }
 
@@ -196,8 +208,11 @@ void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
        struct p2p_go_neg_results res;
        p2p_clear_timeout(p2p);
        p2p_set_state(p2p, P2P_IDLE);
-       if (p2p->go_neg_peer)
+       if (p2p->go_neg_peer) {
+               p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
                p2p->go_neg_peer->wps_method = WPS_NOT_READY;
+               p2p->go_neg_peer->oob_pw_id = 0;
+       }
        p2p->go_neg_peer = NULL;
 
        os_memset(&res, 0, sizeof(res));
@@ -212,27 +227,36 @@ void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
 }
 
 
-static void p2p_listen_in_find(struct p2p_data *p2p)
+static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
 {
        unsigned int r, tu;
        int freq;
        struct wpabuf *ies;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Starting short listen state (state=%s)",
+       p2p_dbg(p2p, "Starting short listen state (state=%s)",
                p2p_state_txt(p2p->state));
 
-       freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
-                                  p2p->cfg->channel);
+       freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
        if (freq < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Unknown regulatory class/channel");
+               p2p_dbg(p2p, "Unknown regulatory class/channel");
                return;
        }
 
        os_get_random((u8 *) &r, sizeof(r));
        tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
              p2p->min_disc_int) * 100;
+       if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
+               tu = p2p->max_disc_tu;
+       if (!dev_disc && tu < 100)
+               tu = 100; /* Need to wait in non-device discovery use cases */
+       if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
+               tu = p2p->cfg->max_listen * 1000 / 1024;
+
+       if (tu == 0) {
+               p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
+               p2p_set_timeout(p2p, 0, 0);
+               return;
+       }
 
        p2p->pending_listen_freq = freq;
        p2p->pending_listen_sec = 0;
@@ -244,8 +268,7 @@ static void p2p_listen_in_find(struct p2p_data *p2p)
 
        if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
                    ies) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to start listen mode");
+               p2p_dbg(p2p, "Failed to start listen mode");
                p2p->pending_listen_freq = 0;
        }
        wpabuf_free(ies);
@@ -257,14 +280,11 @@ int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
        int freq;
        struct wpabuf *ies;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Going to listen(only) state");
+       p2p_dbg(p2p, "Going to listen(only) state");
 
-       freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
-                                  p2p->cfg->channel);
+       freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
        if (freq < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Unknown regulatory class/channel");
+               p2p_dbg(p2p, "Unknown regulatory class/channel");
                return -1;
        }
 
@@ -273,14 +293,11 @@ int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
        p2p->pending_listen_usec = (timeout % 1000) * 1000;
 
        if (p2p->p2p_scan_running) {
-               if (p2p->start_after_scan == P2P_AFTER_SCAN_NOTHING) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: p2p_scan running - connect is already "
-                               "pending - skip listen");
+               if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
+                       p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
                        return 0;
                }
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: p2p_scan running - delay start of listen state");
+               p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
                p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
                return 0;
        }
@@ -290,8 +307,7 @@ int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
                return -1;
 
        if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to start listen mode");
+               p2p_dbg(p2p, "Failed to start listen mode");
                p2p->pending_listen_freq = 0;
                wpabuf_free(ies);
                return -1;
@@ -369,13 +385,11 @@ static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
        dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
                count++;
                if (oldest == NULL ||
-                   os_time_before(&dev->last_seen, &oldest->last_seen))
+                   os_reltime_before(&dev->last_seen, &oldest->last_seen))
                        oldest = dev;
        }
        if (count + 1 > p2p->cfg->max_peers && oldest) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Remove oldest peer entry to make room for a new "
-                       "peer");
+               p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
                dl_list_del(&oldest->list);
                p2p_device_free(p2p, oldest);
        }
@@ -437,13 +451,25 @@ static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
                        continue; /* ignore our own entry */
                dev = p2p_get_device(p2p, cli->p2p_device_addr);
                if (dev) {
-                       /*
-                        * Update information only if we have not received this
-                        * directly from the client.
-                        */
                        if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
-                                         P2P_DEV_PROBE_REQ_ONLY))
+                                         P2P_DEV_PROBE_REQ_ONLY)) {
+                               /*
+                                * Update information since we have not
+                                * received this directly from the client.
+                                */
                                p2p_copy_client_info(dev, cli);
+                       } else {
+                               /*
+                                * Need to update P2P Client Discoverability
+                                * flag since it is valid only in P2P Group
+                                * Info attribute.
+                                */
+                               dev->info.dev_capab &=
+                                       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
+                               dev->info.dev_capab |=
+                                       cli->dev_capab &
+                                       P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
+                       }
                        if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
                                dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
                        }
@@ -462,7 +488,7 @@ static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
 
                os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
                          ETH_ALEN);
-               os_get_time(&dev->last_seen);
+               os_get_reltime(&dev->last_seen);
                os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
                os_memcpy(dev->member_in_go_iface, go_interface_addr,
                          ETH_ALEN);
@@ -472,8 +498,8 @@ static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
 }
 
 
-static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
-                             const struct p2p_message *msg)
+static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
+                             int probe_req, const struct p2p_message *msg)
 {
        os_memcpy(dev->info.device_name, msg->device_name,
                  sizeof(dev->info.device_name));
@@ -526,7 +552,13 @@ static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
        }
 
        if (msg->capability) {
-               dev->info.dev_capab = msg->capability[0];
+               /*
+                * P2P Client Discoverability bit is reserved in all frames
+                * that use this function, so do not change its value here.
+                */
+               dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
+               dev->info.dev_capab |= msg->capability[0] &
+                       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
                dev->info.group_capab = msg->capability[1];
        }
 
@@ -537,21 +569,33 @@ static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
        }
 
        if (!probe_req) {
-               dev->info.config_methods = msg->config_methods ?
+               u16 new_config_methods;
+               new_config_methods = msg->config_methods ?
                        msg->config_methods : msg->wps_config_methods;
+               if (new_config_methods &&
+                   dev->info.config_methods != new_config_methods) {
+                       p2p_dbg(p2p, "Update peer " MACSTR
+                               " config_methods 0x%x -> 0x%x",
+                               MAC2STR(dev->info.p2p_device_addr),
+                               dev->info.config_methods,
+                               new_config_methods);
+                       dev->info.config_methods = new_config_methods;
+               }
        }
 }
 
 
 /**
- * p2p_add_device - Add peer entries based on scan results
+ * p2p_add_device - Add peer entries based on scan results or P2P frames
  * @p2p: P2P module context from p2p_init()
  * @addr: Source address of Beacon or Probe Response frame (may be either
  *     P2P Device Address or P2P Interface Address)
  * @level: Signal level (signal strength of the received frame from the peer)
  * @freq: Frequency on which the Beacon or Probe Response frame was received
+ * @rx_time: Time when the result was received
  * @ies: IEs from the Beacon or Probe Response frame
  * @ies_len: Length of ies buffer in octets
+ * @scan_res: Whether this was based on scan results
  * Returns: 0 on success, -1 on failure
  *
  * If the scan result is for a GO, the clients in the group will also be added
@@ -559,18 +603,19 @@ static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
  * like Provision Discovery Request that contains P2P Capability and P2P Device
  * Info attributes.
  */
-int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
-                  const u8 *ies, size_t ies_len)
+int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
+                  struct os_reltime *rx_time, int level, const u8 *ies,
+                  size_t ies_len, int scan_res)
 {
        struct p2p_device *dev;
        struct p2p_message msg;
        const u8 *p2p_dev_addr;
        int i;
+       struct os_reltime time_now;
 
        os_memset(&msg, 0, sizeof(msg));
        if (p2p_parse_ies(ies, ies_len, &msg)) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to parse P2P IE for a device entry");
+               p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
                p2p_parse_free(&msg);
                return -1;
        }
@@ -580,18 +625,16 @@ int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
        else if (msg.device_id)
                p2p_dev_addr = msg.device_id;
        else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Ignore scan data without P2P Device Info or "
-                       "P2P Device Id");
+               p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
                p2p_parse_free(&msg);
                return -1;
        }
 
        if (!is_zero_ether_addr(p2p->peer_filter) &&
            os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
-                       "filter for " MACSTR " due to peer filter",
-                       MAC2STR(p2p_dev_addr));
+               p2p_dbg(p2p, "Do not add peer filter for " MACSTR
+                       " due to peer filter", MAC2STR(p2p_dev_addr));
+               p2p_parse_free(&msg);
                return 0;
        }
 
@@ -600,7 +643,29 @@ int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
                p2p_parse_free(&msg);
                return -1;
        }
-       os_get_time(&dev->last_seen);
+
+       if (rx_time == NULL) {
+               os_get_reltime(&time_now);
+               rx_time = &time_now;
+       }
+
+       /*
+        * Update the device entry only if the new peer
+        * entry is newer than the one previously stored.
+        */
+       if (dev->last_seen.sec > 0 &&
+           os_reltime_before(rx_time, &dev->last_seen)) {
+               p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
+                       (unsigned int) rx_time->sec,
+                       (unsigned int) rx_time->usec,
+                       (unsigned int) dev->last_seen.sec,
+                       (unsigned int) dev->last_seen.usec);
+               p2p_parse_free(&msg);
+               return -1;
+       }
+
+       os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
+
        dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
 
        if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
@@ -621,27 +686,26 @@ int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
                else
                        ds_freq = 2407 + *msg.ds_params * 5;
                if (freq != ds_freq) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Update Listen frequency based on DS "
-                               "Parameter Set IE: %d -> %d MHz",
+                       p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
                                freq, ds_freq);
                        freq = ds_freq;
                }
        }
 
-       if (dev->listen_freq && dev->listen_freq != freq) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Update Listen frequency based on scan "
-                       "results (" MACSTR " %d -> %d MHz (DS param %d)",
+       if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
+               p2p_dbg(p2p, "Update Listen frequency based on scan results ("
+                       MACSTR " %d -> %d MHz (DS param %d)",
                        MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
                        freq, msg.ds_params ? *msg.ds_params : -1);
        }
-       dev->listen_freq = freq;
-       if (msg.group_info)
-               dev->oper_freq = freq;
+       if (scan_res) {
+               dev->listen_freq = freq;
+               if (msg.group_info)
+                       dev->oper_freq = freq;
+       }
        dev->info.level = level;
 
-       p2p_copy_wps_info(dev, 0, &msg);
+       p2p_copy_wps_info(p2p, dev, 0, &msg);
 
        for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
                wpabuf_free(dev->info.wps_vendor_ext[i]);
@@ -657,8 +721,15 @@ int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
                        break;
        }
 
-       p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
-                             msg.group_info_len);
+       if (msg.wfd_subelems) {
+               wpabuf_free(dev->info.wfd_subelems);
+               dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
+       }
+
+       if (scan_res) {
+               p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
+                                     msg.group_info, msg.group_info_len);
+       }
 
        p2p_parse_free(&msg);
 
@@ -668,11 +739,32 @@ int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
        if (dev->flags & P2P_DEV_REPORTED)
                return 0;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Peer found with Listen frequency %d MHz", freq);
+       p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
+               freq, (unsigned int) rx_time->sec,
+               (unsigned int) rx_time->usec);
        if (dev->flags & P2P_DEV_USER_REJECTED) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Do not report rejected device");
+               p2p_dbg(p2p, "Do not report rejected device");
+               return 0;
+       }
+
+       if (dev->info.config_methods == 0 &&
+           (freq == 2412 || freq == 2437 || freq == 2462)) {
+               /*
+                * If we have only seen a Beacon frame from a GO, we do not yet
+                * know what WPS config methods it supports. Since some
+                * applications use config_methods value from P2P-DEVICE-FOUND
+                * events, postpone reporting this peer until we've fully
+                * discovered its capabilities.
+                *
+                * At least for now, do this only if the peer was detected on
+                * one of the social channels since that peer can be easily be
+                * found again and there are no limitations of having to use
+                * passive scan on this channels, so this can be done through
+                * Probe Response frame that includes the config_methods
+                * information.
+                */
+               p2p_dbg(p2p, "Do not report peer " MACSTR
+                       " with unknown config methods", MAC2STR(addr));
                return 0;
        }
 
@@ -712,6 +804,8 @@ static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
                dev->info.wps_vendor_ext[i] = NULL;
        }
 
+       wpabuf_free(dev->info.wfd_subelems);
+
        os_free(dev);
 }
 
@@ -758,9 +852,8 @@ static int p2p_get_next_prog_freq(struct p2p_data *p2p)
                channel = c->reg_class[cl].channel[ch];
        }
 
-       freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
-               "channel: reg_class %u channel %u -> %d MHz",
+       freq = p2p_channel_to_freq(reg_class, channel);
+       p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
                reg_class, channel, freq);
        p2p->last_prog_scan_class = reg_class;
        p2p->last_prog_scan_chan = channel;
@@ -775,55 +868,32 @@ static void p2p_search(struct p2p_data *p2p)
 {
        int freq = 0;
        enum p2p_scan_type type;
+       u16 pw_id = DEV_PW_DEFAULT;
+       int res;
 
        if (p2p->drv_in_listen) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
-                       "in Listen state - wait for it to end before "
-                       "continuing");
+               p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
                return;
        }
        p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
 
-       if (p2p->go_neg_peer) {
-               /*
-                * Only scan the known listen frequency of the peer
-                * during GO Negotiation start.
-                */
-               freq = p2p->go_neg_peer->listen_freq;
-               if (freq <= 0)
-                       freq = p2p->go_neg_peer->oper_freq;
-               type = P2P_SCAN_SPECIFIC;
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
-                       "for freq %u (GO Neg)", freq);
-       } else if (p2p->invite_peer) {
-               /*
-                * Only scan the known listen frequency of the peer
-                * during Invite start.
-                */
-               freq = p2p->invite_peer->listen_freq;
-               if (freq <= 0)
-                       freq = p2p->invite_peer->oper_freq;
-               type = P2P_SCAN_SPECIFIC;
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
-                       "for freq %u (Invite)", freq);
-       } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
-                  (freq = p2p_get_next_prog_freq(p2p)) > 0) {
+       if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
+           (freq = p2p_get_next_prog_freq(p2p)) > 0) {
                type = P2P_SCAN_SOCIAL_PLUS_ONE;
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
-                       "(+ freq %u)", freq);
+               p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
        } else {
                type = P2P_SCAN_SOCIAL;
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
+               p2p_dbg(p2p, "Starting search");
        }
 
-       if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
-                              p2p->num_req_dev_types, p2p->req_dev_types,
-                              p2p->find_dev_id)) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Scan request failed");
+       res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
+                                p2p->num_req_dev_types, p2p->req_dev_types,
+                                p2p->find_dev_id, pw_id);
+       if (res < 0) {
+               p2p_dbg(p2p, "Scan request failed");
                p2p_continue_find(p2p);
        } else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
+               p2p_dbg(p2p, "Running p2p_scan");
                p2p->p2p_scan_running = 1;
                eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
                eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
@@ -835,7 +905,7 @@ static void p2p_search(struct p2p_data *p2p)
 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
 {
        struct p2p_data *p2p = eloop_ctx;
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
+       p2p_dbg(p2p, "Find timeout -> stop");
        p2p_stop_find(p2p);
 }
 
@@ -846,10 +916,8 @@ static int p2p_run_after_scan(struct p2p_data *p2p)
        enum p2p_after_scan op;
 
        if (p2p->after_scan_tx) {
-               /* TODO: schedule p2p_run_after_scan to be called from TX
-                * status callback(?) */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
-                       "Action frame at p2p_scan completion");
+               p2p->after_scan_tx_in_progress = 1;
+               p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
                p2p->cfg->send_action(p2p->cfg->cb_ctx,
                                      p2p->after_scan_tx->freq,
                                      p2p->after_scan_tx->dst,
@@ -869,19 +937,16 @@ static int p2p_run_after_scan(struct p2p_data *p2p)
        case P2P_AFTER_SCAN_NOTHING:
                break;
        case P2P_AFTER_SCAN_LISTEN:
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
-                       "requested Listen state");
+               p2p_dbg(p2p, "Start previously requested Listen state");
                p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
                           p2p->pending_listen_usec / 1000);
                return 1;
        case P2P_AFTER_SCAN_CONNECT:
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
-                       "requested connect with " MACSTR,
+               p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
                        MAC2STR(p2p->after_scan_peer));
                dev = p2p_get_device(p2p, p2p->after_scan_peer);
                if (dev == NULL) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
-                               "known anymore");
+                       p2p_dbg(p2p, "Peer not known anymore");
                        break;
                }
                p2p_connect_send(p2p, dev);
@@ -896,8 +961,7 @@ static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
 {
        struct p2p_data *p2p = eloop_ctx;
        int running;
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
-               "(running=%d)", p2p->p2p_scan_running);
+       p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
        running = p2p->p2p_scan_running;
        /* Make sure we recover from missed scan results callback */
        p2p->p2p_scan_running = 0;
@@ -918,15 +982,14 @@ static void p2p_free_req_dev_types(struct p2p_data *p2p)
 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
             enum p2p_discovery_type type,
             unsigned int num_req_dev_types, const u8 *req_dev_types,
-            const u8 *dev_id)
+            const u8 *dev_id, unsigned int search_delay)
 {
        int res;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
-               type);
+       p2p_dbg(p2p, "Starting find (type=%d)", type);
+       os_get_reltime(&p2p->find_start);
        if (p2p->p2p_scan_running) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
-                       "already running");
+               p2p_dbg(p2p, "p2p_scan is already running");
        }
 
        p2p_free_req_dev_types(p2p);
@@ -952,6 +1015,8 @@ int p2p_find(struct p2p_data *p2p, unsigned int timeout,
        p2p->find_type = type;
        p2p_device_clear_reported(p2p);
        p2p_set_state(p2p, P2P_SEARCH);
+       p2p->search_delay = search_delay;
+       p2p->in_search_delay = 0;
        eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
        p2p->last_p2p_find_timeout = timeout;
        if (timeout)
@@ -962,33 +1027,30 @@ int p2p_find(struct p2p_data *p2p, unsigned int timeout,
        case P2P_FIND_PROGRESSIVE:
                res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
                                         p2p->num_req_dev_types,
-                                        p2p->req_dev_types, dev_id);
+                                        p2p->req_dev_types, dev_id,
+                                        DEV_PW_DEFAULT);
                break;
        case P2P_FIND_ONLY_SOCIAL:
                res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
                                         p2p->num_req_dev_types,
-                                        p2p->req_dev_types, dev_id);
+                                        p2p->req_dev_types, dev_id,
+                                        DEV_PW_DEFAULT);
                break;
        default:
                return -1;
        }
 
        if (res == 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
+               p2p_dbg(p2p, "Running p2p_scan");
                p2p->p2p_scan_running = 1;
                eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
                eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
                                       p2p, NULL);
-       } else if (res == 1) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
-                       "p2p_scan at this point - will try again after "
-                       "previous scan completes");
-               res = 0;
-               p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
-               eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
+       } else if (p2p->p2p_scan_running) {
+               p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
+               /* wait for the previous p2p_scan to complete */
        } else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
-                       "p2p_scan");
+               p2p_dbg(p2p, "Failed to start p2p_scan");
                p2p_set_state(p2p, P2P_IDLE);
                eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
        }
@@ -997,30 +1059,18 @@ int p2p_find(struct p2p_data *p2p, unsigned int timeout,
 }
 
 
-int p2p_other_scan_completed(struct p2p_data *p2p)
-{
-       if (p2p->state != P2P_SEARCH_WHEN_READY)
-               return 0;
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
-               "now that previous scan was completed");
-       if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
-                    p2p->num_req_dev_types, p2p->req_dev_types,
-                    p2p->find_dev_id) < 0)
-               return 0;
-       return 1;
-}
-
-
 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
+       p2p_dbg(p2p, "Stopping find");
        eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
        p2p_clear_timeout(p2p);
        if (p2p->state == P2P_SEARCH)
-               wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
+               p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
        p2p_set_state(p2p, P2P_IDLE);
        p2p_free_req_dev_types(p2p);
        p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
+       if (p2p->go_neg_peer)
+               p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
        p2p->go_neg_peer = NULL;
        p2p->sd_peer = NULL;
        p2p->invite_peer = NULL;
@@ -1031,8 +1081,7 @@ void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
 {
        if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
-                       "since we are on correct channel for response");
+               p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
                return;
        }
        if (p2p->in_listen) {
@@ -1045,95 +1094,176 @@ void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
                 * when the operation gets canceled, so clear the internal
                 * variable that is tracking driver state.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
-                       "drv_in_listen (%d)", p2p->drv_in_listen);
+               p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
                p2p->drv_in_listen = 0;
        }
        p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
 }
 
 
+void p2p_stop_listen(struct p2p_data *p2p)
+{
+       if (p2p->state != P2P_LISTEN_ONLY) {
+               p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
+               return;
+       }
+
+       p2p_stop_listen_for_freq(p2p, 0);
+       p2p_set_state(p2p, P2P_IDLE);
+}
+
+
 void p2p_stop_find(struct p2p_data *p2p)
 {
        p2p_stop_find_for_freq(p2p, 0);
 }
 
 
-static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
+static int p2p_prepare_channel_pref(struct p2p_data *p2p,
+                                   unsigned int force_freq,
+                                   unsigned int pref_freq, int go)
 {
+       u8 op_class, op_channel;
+       unsigned int freq = force_freq ? force_freq : pref_freq;
+
+       p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
+               force_freq, pref_freq, go);
+       if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
+               p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
+               return -1;
+       }
+
+       if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
+           (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
+                                         op_channel))) {
+               p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
+                       freq, op_class, op_channel);
+               return -1;
+       }
+
+       p2p->op_reg_class = op_class;
+       p2p->op_channel = op_channel;
+
        if (force_freq) {
-               u8 op_reg_class, op_channel;
-               if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
-                                       &op_reg_class, &op_channel) < 0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Unsupported frequency %u MHz",
-                               force_freq);
-                       return -1;
-               }
-               if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
-                                          op_channel)) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Frequency %u MHz (oper_class %u "
-                               "channel %u) not allowed for P2P",
-                               force_freq, op_reg_class, op_channel);
-                       return -1;
-               }
-               p2p->op_reg_class = op_reg_class;
-               p2p->op_channel = op_channel;
                p2p->channels.reg_classes = 1;
                p2p->channels.reg_class[0].channels = 1;
                p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
                p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
        } else {
-               u8 op_reg_class, op_channel;
-
-               if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
-                   p2p_supported_freq(p2p, p2p->best_freq_overall) &&
-                   p2p_freq_to_channel(p2p->cfg->country,
-                                       p2p->best_freq_overall,
-                                       &op_reg_class, &op_channel) == 0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Select best overall channel as "
-                               "operating channel preference");
-                       p2p->op_reg_class = op_reg_class;
-                       p2p->op_channel = op_channel;
-               } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
-                          p2p_supported_freq(p2p, p2p->best_freq_5) &&
-                          p2p_freq_to_channel(p2p->cfg->country,
-                                              p2p->best_freq_5,
-                                              &op_reg_class, &op_channel) ==
-                          0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Select best 5 GHz channel as "
-                               "operating channel preference");
-                       p2p->op_reg_class = op_reg_class;
-                       p2p->op_channel = op_channel;
-               } else if (!p2p->cfg->cfg_op_channel &&
-                          p2p->best_freq_24 > 0 &&
-                          p2p_supported_freq(p2p, p2p->best_freq_24) &&
-                          p2p_freq_to_channel(p2p->cfg->country,
-                                              p2p->best_freq_24,
-                                              &op_reg_class, &op_channel) ==
-                          0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Select best 2.4 GHz channel as "
-                               "operating channel preference");
-                       p2p->op_reg_class = op_reg_class;
-                       p2p->op_channel = op_channel;
-               } else {
-                       p2p->op_reg_class = p2p->cfg->op_reg_class;
-                       p2p->op_channel = p2p->cfg->op_channel;
-               }
-
                os_memcpy(&p2p->channels, &p2p->cfg->channels,
                          sizeof(struct p2p_channels));
        }
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Own preference for operation channel: "
-               "Operating Class %u Channel %u%s",
+
+       return 0;
+}
+
+
+static void p2p_prepare_channel_best(struct p2p_data *p2p)
+{
+       u8 op_class, op_channel;
+       const int op_classes_5ghz[] = { 124, 115, 0 };
+       const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
+       const int op_classes_vht[] = { 128, 0 };
+
+       p2p_dbg(p2p, "Prepare channel best");
+
+       if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
+           p2p_supported_freq(p2p, p2p->best_freq_overall) &&
+           p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
+           == 0) {
+               p2p_dbg(p2p, "Select best overall channel as operating channel preference");
+               p2p->op_reg_class = op_class;
+               p2p->op_channel = op_channel;
+       } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
+                  p2p_supported_freq(p2p, p2p->best_freq_5) &&
+                  p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
+                  == 0) {
+               p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
+               p2p->op_reg_class = op_class;
+               p2p->op_channel = op_channel;
+       } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
+                  p2p_supported_freq(p2p, p2p->best_freq_24) &&
+                  p2p_freq_to_channel(p2p->best_freq_24, &op_class,
+                                      &op_channel) == 0) {
+               p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
+               p2p->op_reg_class = op_class;
+               p2p->op_channel = op_channel;
+       } else if (p2p->cfg->num_pref_chan > 0 &&
+                  p2p_channels_includes(&p2p->cfg->channels,
+                                        p2p->cfg->pref_chan[0].op_class,
+                                        p2p->cfg->pref_chan[0].chan)) {
+               p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
+               p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
+               p2p->op_channel = p2p->cfg->pref_chan[0].chan;
+       } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
+                                     &p2p->op_reg_class, &p2p->op_channel) ==
+                  0) {
+               p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
+                       p2p->op_reg_class, p2p->op_channel);
+       } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
+                                     &p2p->op_reg_class, &p2p->op_channel) ==
+                  0) {
+               p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
+                       p2p->op_reg_class, p2p->op_channel);
+       } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
+                                     &p2p->op_reg_class, &p2p->op_channel) ==
+                  0) {
+               p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
+                       p2p->op_reg_class, p2p->op_channel);
+       } else {
+               p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
+               p2p->op_reg_class = p2p->cfg->op_reg_class;
+               p2p->op_channel = p2p->cfg->op_channel;
+       }
+
+       os_memcpy(&p2p->channels, &p2p->cfg->channels,
+                 sizeof(struct p2p_channels));
+}
+
+
+/**
+ * p2p_prepare_channel - Select operating channel for GO Negotiation
+ * @p2p: P2P module context from p2p_init()
+ * @dev: Selected peer device
+ * @force_freq: Forced frequency in MHz or 0 if not forced
+ * @pref_freq: Preferred frequency in MHz or 0 if no preference
+ * @go: Whether the local end will be forced to be GO
+ * Returns: 0 on success, -1 on failure (channel not supported for P2P)
+ *
+ * This function is used to do initial operating channel selection for GO
+ * Negotiation prior to having received peer information. The selected channel
+ * may be further optimized in p2p_reselect_channel() once the peer information
+ * is available.
+ */
+int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
+                       unsigned int force_freq, unsigned int pref_freq, int go)
+{
+       p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
+               force_freq, pref_freq, go);
+       if (force_freq || pref_freq) {
+               if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
+                   0)
+                       return -1;
+       } else {
+               p2p_prepare_channel_best(p2p);
+       }
+       p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
+       if (go)
+               p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
+       else if (!force_freq)
+               p2p_channels_union(&p2p->channels, &p2p->cfg->cli_channels,
+                                  &p2p->channels);
+       p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
+
+       p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
                p2p->op_reg_class, p2p->op_channel,
                force_freq ? " (forced)" : "");
 
+       if (force_freq)
+               dev->flags |= P2P_DEV_FORCE_FREQ;
+       else
+               dev->flags &= ~P2P_DEV_FORCE_FREQ;
+
        return 0;
 }
 
@@ -1161,41 +1291,40 @@ static void p2p_set_dev_persistent(struct p2p_device *dev,
 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
                enum p2p_wps_method wps_method,
                int go_intent, const u8 *own_interface_addr,
-               unsigned int force_freq, int persistent_group)
+               unsigned int force_freq, int persistent_group,
+               const u8 *force_ssid, size_t force_ssid_len,
+               int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
 {
        struct p2p_device *dev;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Request to start group negotiation - peer=" MACSTR
+       p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
                "  GO Intent=%d  Intended Interface Address=" MACSTR
-               " wps_method=%d persistent_group=%d",
+               " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
+               "oob_pw_id=%u",
                MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
-               wps_method, persistent_group);
+               wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
 
-       if (p2p_prepare_channel(p2p, force_freq) < 0)
-               return -1;
-
-       p2p->ssid_set = 0;
        dev = p2p_get_device(p2p, peer_addr);
        if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Cannot connect to unknown P2P Device " MACSTR,
+               p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
                        MAC2STR(peer_addr));
                return -1;
        }
 
+       if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
+                               go_intent == 15) < 0)
+               return -1;
+
        if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
                if (!(dev->info.dev_capab &
                      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Cannot connect to P2P Device " MACSTR
+                       p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
                                " that is in a group and is not discoverable",
                                MAC2STR(peer_addr));
                        return -1;
                }
                if (dev->oper_freq <= 0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Cannot connect to P2P Device " MACSTR
+                       p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
                                " with incomplete information",
                                MAC2STR(peer_addr));
                        return -1;
@@ -1208,10 +1337,33 @@ int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
                 */
        }
 
+       p2p->ssid_set = 0;
+       if (force_ssid) {
+               wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
+                                 force_ssid, force_ssid_len);
+               os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
+               p2p->ssid_len = force_ssid_len;
+               p2p->ssid_set = 1;
+       }
+
        dev->flags &= ~P2P_DEV_NOT_YET_READY;
        dev->flags &= ~P2P_DEV_USER_REJECTED;
        dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
        dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
+       if (pd_before_go_neg)
+               dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
+       else {
+               dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
+               /*
+                * Assign dialog token and tie breaker here to use the same
+                * values in each retry within the same GO Negotiation exchange.
+                */
+               dev->dialog_token++;
+               if (dev->dialog_token == 0)
+                       dev->dialog_token = 1;
+               dev->tie_breaker = p2p->next_tie_breaker;
+               p2p->next_tie_breaker = !p2p->next_tie_breaker;
+       }
        dev->connect_reqs = 0;
        dev->go_neg_req_sent = 0;
        dev->go_state = UNKNOWN_GO;
@@ -1228,24 +1380,17 @@ int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
                 * new GO Negotiation, e.g., when the pending frame was from a
                 * previous attempt at starting a GO Negotiation.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
-                       "previous pending Action frame TX that was waiting "
-                       "for p2p_scan completion");
+               p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
                os_free(p2p->after_scan_tx);
                p2p->after_scan_tx = NULL;
        }
 
        dev->wps_method = wps_method;
+       dev->oob_pw_id = oob_pw_id;
        dev->status = P2P_SC_SUCCESS;
 
-       if (force_freq)
-               dev->flags |= P2P_DEV_FORCE_FREQ;
-       else
-               dev->flags &= ~P2P_DEV_FORCE_FREQ;
-
        if (p2p->p2p_scan_running) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: p2p_scan running - delay connect send");
+               p2p_dbg(p2p, "p2p_scan running - delay connect send");
                p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
                os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
                return 0;
@@ -1259,28 +1404,38 @@ int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
                  enum p2p_wps_method wps_method,
                  int go_intent, const u8 *own_interface_addr,
-                 unsigned int force_freq, int persistent_group)
+                 unsigned int force_freq, int persistent_group,
+                 const u8 *force_ssid, size_t force_ssid_len,
+                 unsigned int pref_freq, u16 oob_pw_id)
 {
        struct p2p_device *dev;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Request to authorize group negotiation - peer=" MACSTR
+       p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
                "  GO Intent=%d  Intended Interface Address=" MACSTR
-               " wps_method=%d  persistent_group=%d",
+               " wps_method=%d  persistent_group=%d oob_pw_id=%u",
                MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
-               wps_method, persistent_group);
-
-       if (p2p_prepare_channel(p2p, force_freq) < 0)
-               return -1;
+               wps_method, persistent_group, oob_pw_id);
 
        dev = p2p_get_device(p2p, peer_addr);
        if (dev == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Cannot authorize unknown P2P Device " MACSTR,
+               p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
                        MAC2STR(peer_addr));
                return -1;
        }
 
+       if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
+                               15) < 0)
+               return -1;
+
+       p2p->ssid_set = 0;
+       if (force_ssid) {
+               wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
+                                 force_ssid, force_ssid_len);
+               os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
+               p2p->ssid_len = force_ssid_len;
+               p2p->ssid_set = 1;
+       }
+
        dev->flags &= ~P2P_DEV_NOT_YET_READY;
        dev->flags &= ~P2P_DEV_USER_REJECTED;
        dev->go_neg_req_sent = 0;
@@ -1290,13 +1445,9 @@ int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
        os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
 
        dev->wps_method = wps_method;
+       dev->oob_pw_id = oob_pw_id;
        dev->status = P2P_SC_SUCCESS;
 
-       if (force_freq)
-               dev->flags |= P2P_DEV_FORCE_FREQ;
-       else
-               dev->flags &= ~P2P_DEV_FORCE_FREQ;
-
        return 0;
 }
 
@@ -1304,18 +1455,16 @@ int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
                      struct p2p_device *dev, struct p2p_message *msg)
 {
-       os_get_time(&dev->last_seen);
+       os_get_reltime(&dev->last_seen);
 
-       p2p_copy_wps_info(dev, 0, msg);
+       p2p_copy_wps_info(p2p, dev, 0, msg);
 
        if (msg->listen_channel) {
                int freq;
-               freq = p2p_channel_to_freq((char *) msg->listen_channel,
-                                          msg->listen_channel[3],
+               freq = p2p_channel_to_freq(msg->listen_channel[3],
                                           msg->listen_channel[4]);
                if (freq < 0) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Unknown peer Listen channel: "
+                       p2p_dbg(p2p, "Unknown peer Listen channel: "
                                "country=%c%c(0x%02x) reg_class=%u channel=%u",
                                msg->listen_channel[0],
                                msg->listen_channel[1],
@@ -1323,22 +1472,24 @@ void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
                                msg->listen_channel[3],
                                msg->listen_channel[4]);
                } else {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
-                               "peer " MACSTR " Listen channel: %u -> %u MHz",
+                       p2p_dbg(p2p, "Update peer " MACSTR
+                               " Listen channel: %u -> %u MHz",
                                MAC2STR(dev->info.p2p_device_addr),
                                dev->listen_freq, freq);
                        dev->listen_freq = freq;
                }
        }
 
+       if (msg->wfd_subelems) {
+               wpabuf_free(dev->info.wfd_subelems);
+               dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
+       }
+
        if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
                dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Completed device entry based on data from "
-                       "GO Negotiation Request");
+               p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
        } else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Created device entry based on GO Neg Req: "
+               p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
                        MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
                        "listen_freq=%d",
                        MAC2STR(dev->info.p2p_device_addr),
@@ -1349,8 +1500,7 @@ void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
        dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
 
        if (dev->flags & P2P_DEV_USER_REJECTED) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Do not report rejected device");
+               p2p_dbg(p2p, "Do not report rejected device");
                return;
        }
 
@@ -1386,10 +1536,8 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
        int freqs;
        size_t i, j;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Negotiation with " MACSTR " completed (%s will be "
-               "GO)", MAC2STR(peer->info.p2p_device_addr),
-               go ? "local end" : "peer");
+       p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
+               MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
 
        os_memset(&res, 0, sizeof(res));
        res.role_go = go;
@@ -1405,8 +1553,7 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
 
        if (go) {
                /* Setup AP mode for WPS provisioning */
-               res.freq = p2p_channel_to_freq(p2p->cfg->country,
-                                              p2p->op_reg_class,
+               res.freq = p2p_channel_to_freq(p2p->op_reg_class,
                                               p2p->op_channel);
                os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
                res.ssid_len = p2p->ssid_len;
@@ -1419,8 +1566,15 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
                }
        }
 
+       p2p_channels_dump(p2p, "own channels", &p2p->channels);
+       p2p_channels_dump(p2p, "peer channels", &peer->channels);
        p2p_channels_intersect(&p2p->channels, &peer->channels,
                               &intersection);
+       if (go) {
+               p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
+               p2p_channels_dump(p2p, "intersection after no-GO removal",
+                                 &intersection);
+       }
        freqs = 0;
        for (i = 0; i < intersection.reg_classes; i++) {
                struct p2p_reg_class *c = &intersection.reg_class[i];
@@ -1430,8 +1584,7 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
                        int freq;
                        if (freqs + 1 == P2P_MAX_CHANNELS)
                                break;
-                       freq = p2p_channel_to_freq(peer->country, c->reg_class,
-                                                  c->channel[j]);
+                       freq = p2p_channel_to_freq(c->reg_class, c->channel[j]);
                        if (freq < 0)
                                continue;
                        res.freq_list[freqs++] = freq;
@@ -1444,6 +1597,7 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
        p2p->ssid_set = 0;
        peer->go_neg_req_sent = 0;
        peer->wps_method = WPS_NOT_READY;
+       peer->oob_pw_id = 0;
 
        p2p_set_state(p2p, P2P_PROVISIONING);
        p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
@@ -1453,8 +1607,7 @@ void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
                              const u8 *data, size_t len, int rx_freq)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
+       p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
        wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
 
        if (len < 1)
@@ -1475,6 +1628,7 @@ static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
                                           rx_freq);
                break;
        case P2P_INVITATION_RESP:
+               p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
                p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
                break;
        case P2P_PROV_DISC_REQ:
@@ -1490,8 +1644,7 @@ static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
                p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
                break;
        default:
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Unsupported P2P Public Action frame type %d",
+               p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
                        data[0]);
                break;
        }
@@ -1566,16 +1719,14 @@ void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
        len--;
 
        /* P2P action frame */
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
+       p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
        wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
 
        if (len < 1)
                return;
        switch (data[0]) {
        case P2P_NOA:
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Received P2P Action - Notice of Absence");
+               p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
                /* TODO */
                break;
        case P2P_PRESENCE_REQ:
@@ -1588,8 +1739,7 @@ void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
                p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
                break;
        default:
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Received P2P Action - unknown type %u", data[0]);
+               p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
                break;
        }
 }
@@ -1645,7 +1795,7 @@ static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
        if (dev) {
                if (dev->country[0] == 0 && msg.listen_channel)
                        os_memcpy(dev->country, msg.listen_channel, 3);
-               os_get_time(&dev->last_seen);
+               os_get_reltime(&dev->last_seen);
                p2p_parse_free(&msg);
                return; /* already known */
        }
@@ -1656,22 +1806,25 @@ static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
                return;
        }
 
-       os_get_time(&dev->last_seen);
+       os_get_reltime(&dev->last_seen);
        dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
 
        if (msg.listen_channel) {
                os_memcpy(dev->country, msg.listen_channel, 3);
-               dev->listen_freq = p2p_channel_to_freq(dev->country,
-                                                      msg.listen_channel[3],
+               dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
                                                       msg.listen_channel[4]);
        }
 
-       p2p_copy_wps_info(dev, 1, &msg);
+       p2p_copy_wps_info(p2p, dev, 1, &msg);
+
+       if (msg.wfd_subelems) {
+               wpabuf_free(dev->info.wfd_subelems);
+               dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
+       }
 
        p2p_parse_free(&msg);
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Created device entry based on Probe Req: " MACSTR
+       p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
                " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
                MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
                dev->info.group_capab, dev->info.device_name,
@@ -1687,7 +1840,7 @@ struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
 
        dev = p2p_get_device(p2p, addr);
        if (dev) {
-               os_get_time(&dev->last_seen);
+               os_get_reltime(&dev->last_seen);
                return dev; /* already known */
        }
 
@@ -1765,16 +1918,38 @@ struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
 {
        struct wpabuf *buf;
        u8 *len;
+       int pw_id = -1;
+       size_t extra = 0;
 
-       buf = wpabuf_alloc(1000);
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p->wfd_ie_probe_resp)
+               extra = wpabuf_len(p2p->wfd_ie_probe_resp);
+#endif /* CONFIG_WIFI_DISPLAY */
+
+       buf = wpabuf_alloc(1000 + extra);
        if (buf == NULL)
                return NULL;
 
-       p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
+       if (p2p->go_neg_peer) {
+               /* Advertise immediate availability of WPS credential */
+               pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
+       }
+
+       if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
+               p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
+               wpabuf_free(buf);
+               return NULL;
+       }
+
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p->wfd_ie_probe_resp)
+               wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
+#endif /* CONFIG_WIFI_DISPLAY */
 
        /* P2P IE */
        len = p2p_buf_add_ie_hdr(buf);
-       p2p_buf_add_capability(buf, p2p->dev_capab, 0);
+       p2p_buf_add_capability(buf, p2p->dev_capab &
+                              ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
        if (p2p->ext_listen_interval)
                p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
                                              p2p->ext_listen_interval);
@@ -1785,42 +1960,9 @@ struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
 }
 
 
-static int is_11b(u8 rate)
-{
-       return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
-}
-
-
-static int supp_rates_11b_only(struct ieee802_11_elems *elems)
-{
-       int num_11b = 0, num_others = 0;
-       int i;
-
-       if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
-               return 0;
-
-       for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
-               if (is_11b(elems->supp_rates[i]))
-                       num_11b++;
-               else
-                       num_others++;
-       }
-
-       for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
-            i++) {
-               if (is_11b(elems->ext_supp_rates[i]))
-                       num_11b++;
-               else
-                       num_others++;
-       }
-
-       return num_11b > 0 && num_others == 0;
-}
-
-
-static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
-                           const u8 *dst, const u8 *bssid, const u8 *ie,
-                           size_t ie_len)
+static enum p2p_probe_req_status
+p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
+               const u8 *bssid, const u8 *ie, size_t ie_len)
 {
        struct ieee802_11_elems elems;
        struct wpabuf *buf;
@@ -1830,55 +1972,55 @@ static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
 
        if (!p2p->in_listen || !p2p->drv_in_listen) {
                /* not in Listen state - ignore Probe Request */
-               return;
+               return P2P_PREQ_NOT_LISTEN;
        }
 
        if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
            ParseFailed) {
                /* Ignore invalid Probe Request frames */
-               return;
+               return P2P_PREQ_MALFORMED;
        }
 
        if (elems.p2p == NULL) {
                /* not a P2P probe - ignore it */
-               return;
+               return P2P_PREQ_NOT_P2P;
        }
 
        if (dst && !is_broadcast_ether_addr(dst) &&
            os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
                /* Not sent to the broadcast address or our P2P Device Address
                 */
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
 
        if (bssid && !is_broadcast_ether_addr(bssid)) {
                /* Not sent to the Wildcard BSSID */
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
 
        if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
            os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
            0) {
                /* not using P2P Wildcard SSID - ignore */
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
 
        if (supp_rates_11b_only(&elems)) {
                /* Indicates support for 11b rates only */
-               return;
+               return P2P_PREQ_NOT_P2P;
        }
 
        os_memset(&msg, 0, sizeof(msg));
        if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
                /* Could not parse P2P attributes */
-               return;
+               return P2P_PREQ_NOT_P2P;
        }
 
        if (msg.device_id &&
-           os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN != 0)) {
+           os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
                /* Device ID did not match */
                p2p_parse_free(&msg);
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
 
        /* Check Requested Device Type match */
@@ -1886,15 +2028,16 @@ static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
            !p2p_match_dev_type(p2p, msg.wps_attributes)) {
                /* No match with Requested Device Type */
                p2p_parse_free(&msg);
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
        p2p_parse_free(&msg);
 
-       if (!p2p->cfg->send_probe_resp)
-               return; /* Response generated elsewhere */
+       if (!p2p->cfg->send_probe_resp) {
+               /* Response generated elsewhere */
+               return P2P_PREQ_NOT_PROCESSED;
+       }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Reply to P2P Probe Request in Listen state");
+       p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
 
        /*
         * We do not really have a specific BSS that this frame is advertising,
@@ -1904,12 +2047,12 @@ static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
         */
        ies = p2p_build_probe_resp_ies(p2p);
        if (ies == NULL)
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
 
        buf = wpabuf_alloc(200 + wpabuf_len(ies));
        if (buf == NULL) {
                wpabuf_free(ies);
-               return;
+               return P2P_PREQ_NOT_PROCESSED;
        }
 
        resp = NULL;
@@ -1952,26 +2095,31 @@ static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
        p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
 
        wpabuf_free(buf);
+
+       return P2P_PREQ_NOT_PROCESSED;
 }
 
 
-int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
-                    const u8 *bssid, const u8 *ie, size_t ie_len)
+enum p2p_probe_req_status
+p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
+                const u8 *bssid, const u8 *ie, size_t ie_len)
 {
+       enum p2p_probe_req_status res;
+
        p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
 
-       p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
+       res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
 
        if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
            p2p->go_neg_peer &&
            os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
-           == 0) {
+           == 0 &&
+           !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
                /* Received a Probe Request from GO Negotiation peer */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Found GO Negotiation peer - try to start GO "
-                       "negotiation from timeout");
+               p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
+               eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
                eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
-               return 1;
+               return P2P_PREQ_PROCESSED;
        }
 
        if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
@@ -1979,14 +2127,12 @@ int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
            os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
            == 0) {
                /* Received a Probe Request from Invite peer */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Found Invite peer - try to start Invite from "
-                       "timeout");
+               p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
                eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
-               return 1;
+               return P2P_PREQ_PROCESSED;
        }
 
-       return 0;
+       return res;
 }
 
 
@@ -2048,20 +2194,31 @@ int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
        struct p2p_device *peer;
        size_t tmplen;
        int res;
+       size_t extra = 0;
 
        if (!p2p_group)
                return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
 
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p->wfd_ie_assoc_req)
+               extra = wpabuf_len(p2p->wfd_ie_assoc_req);
+#endif /* CONFIG_WIFI_DISPLAY */
+
        /*
         * (Re)Association Request - P2P IE
         * P2P Capability attribute (shall be present)
         * Extended Listen Timing (may be present)
         * P2P Device Info attribute (shall be present)
         */
-       tmp = wpabuf_alloc(200);
+       tmp = wpabuf_alloc(200 + extra);
        if (tmp == NULL)
                return -1;
 
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p->wfd_ie_assoc_req)
+               wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
+#endif /* CONFIG_WIFI_DISPLAY */
+
        peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
 
        lpos = p2p_buf_add_ie_hdr(tmp);
@@ -2100,29 +2257,37 @@ int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
 }
 
 
-int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
+int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
 {
-       struct wpabuf *p2p_ie;
        struct p2p_message msg;
 
-       p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
-                                            P2P_IE_VENDOR_TYPE);
-       if (p2p_ie == NULL)
-               return -1;
        os_memset(&msg, 0, sizeof(msg));
-       if (p2p_parse_p2p_ie(p2p_ie, &msg)) {
-               wpabuf_free(p2p_ie);
+       if (p2p_parse_p2p_ie(p2p_ie, &msg))
                return -1;
-       }
 
-       if (msg.p2p_device_addr == NULL) {
-               wpabuf_free(p2p_ie);
-               return -1;
+       if (msg.p2p_device_addr) {
+               os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
+               return 0;
+       } else if (msg.device_id) {
+               os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
+               return 0;
        }
+       return -1;
+}
+
+
+int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
+{
+       struct wpabuf *p2p_ie;
+       int ret;
 
-       os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
+       p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
+                                            P2P_IE_VENDOR_TYPE);
+       if (p2p_ie == NULL)
+               return -1;
+       ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
        wpabuf_free(p2p_ie);
-       return 0;
+       return ret;
 }
 
 
@@ -2137,24 +2302,20 @@ static void p2p_clear_go_neg(struct p2p_data *p2p)
 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
 {
        if (p2p->go_neg_peer == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No pending Group Formation - "
-                       "ignore WPS registration success notification");
+               p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
                return; /* No pending Group Formation */
        }
 
        if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
            0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Ignore WPS registration success notification "
-                       "for " MACSTR " (GO Negotiation peer " MACSTR ")",
+               p2p_dbg(p2p, "Ignore WPS registration success notification for "
+                       MACSTR " (GO Negotiation peer " MACSTR ")",
                        MAC2STR(mac_addr),
                        MAC2STR(p2p->go_neg_peer->intended_addr));
                return; /* Ignore unexpected peer address */
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Group Formation completed successfully with " MACSTR,
+       p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
                MAC2STR(mac_addr));
 
        p2p_clear_go_neg(p2p);
@@ -2164,14 +2325,11 @@ void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
 void p2p_group_formation_failed(struct p2p_data *p2p)
 {
        if (p2p->go_neg_peer == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No pending Group Formation - "
-                       "ignore group formation failure notification");
+               p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
                return; /* No pending Group Formation */
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Group Formation failed with " MACSTR,
+       p2p_dbg(p2p, "Group Formation failed with " MACSTR,
                MAC2STR(p2p->go_neg_peer->intended_addr));
 
        p2p_clear_go_neg(p2p);
@@ -2200,9 +2358,20 @@ struct p2p_data * p2p_init(const struct p2p_config *cfg)
                p2p->cfg->model_number = os_strdup(cfg->model_number);
        if (cfg->serial_number)
                p2p->cfg->serial_number = os_strdup(cfg->serial_number);
+       if (cfg->pref_chan) {
+               p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
+                                               sizeof(struct p2p_channel));
+               if (p2p->cfg->pref_chan) {
+                       os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
+                                 cfg->num_pref_chan *
+                                 sizeof(struct p2p_channel));
+               } else
+                       p2p->cfg->num_pref_chan = 0;
+       }
 
        p2p->min_disc_int = 1;
        p2p->max_disc_int = 3;
+       p2p->max_disc_tu = -1;
 
        os_get_random(&p2p->next_tie_breaker, 1);
        p2p->next_tie_breaker &= 0x01;
@@ -2218,15 +2387,37 @@ struct p2p_data * p2p_init(const struct p2p_config *cfg)
        eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
                               p2p_expiration_timeout, p2p, NULL);
 
+       p2p->go_timeout = 100;
+       p2p->client_timeout = 20;
+
+       p2p_dbg(p2p, "initialized");
+       p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
+       p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
+
        return p2p;
 }
 
 
 void p2p_deinit(struct p2p_data *p2p)
 {
+#ifdef CONFIG_WIFI_DISPLAY
+       wpabuf_free(p2p->wfd_ie_beacon);
+       wpabuf_free(p2p->wfd_ie_probe_req);
+       wpabuf_free(p2p->wfd_ie_probe_resp);
+       wpabuf_free(p2p->wfd_ie_assoc_req);
+       wpabuf_free(p2p->wfd_ie_invitation);
+       wpabuf_free(p2p->wfd_ie_prov_disc_req);
+       wpabuf_free(p2p->wfd_ie_prov_disc_resp);
+       wpabuf_free(p2p->wfd_ie_go_neg);
+       wpabuf_free(p2p->wfd_dev_info);
+       wpabuf_free(p2p->wfd_assoc_bssid);
+       wpabuf_free(p2p->wfd_coupled_sink_info);
+#endif /* CONFIG_WIFI_DISPLAY */
+
        eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
        eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
        eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
+       eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
        p2p_flush(p2p);
        p2p_free_req_dev_types(p2p);
        os_free(p2p->cfg->dev_name);
@@ -2234,10 +2425,12 @@ void p2p_deinit(struct p2p_data *p2p)
        os_free(p2p->cfg->model_name);
        os_free(p2p->cfg->model_number);
        os_free(p2p->cfg->serial_number);
+       os_free(p2p->cfg->pref_chan);
        os_free(p2p->groups);
        wpabuf_free(p2p->sd_resp);
        os_free(p2p->after_scan_tx);
        p2p_remove_wps_vendor_extensions(p2p);
+       os_free(p2p->no_go_freq.range);
        os_free(p2p);
 }
 
@@ -2245,11 +2438,7 @@ void p2p_deinit(struct p2p_data *p2p)
 void p2p_flush(struct p2p_data *p2p)
 {
        struct p2p_device *dev, *prev;
-       p2p_clear_timeout(p2p);
-       p2p_set_state(p2p, P2P_IDLE);
-       p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
-       p2p->go_neg_peer = NULL;
-       eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
+       p2p_stop_find(p2p);
        dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
                              list) {
                dl_list_del(&dev->list);
@@ -2269,13 +2458,13 @@ int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
        if (dev == NULL)
                return -1;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
-               MAC2STR(addr));
+       p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
 
        if (p2p->go_neg_peer == dev)
                p2p->go_neg_peer = NULL;
 
        dev->wps_method = WPS_NOT_READY;
+       dev->oob_pw_id = 0;
        dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
        dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
 
@@ -2442,8 +2631,7 @@ void p2p_continue_find(struct p2p_data *p2p)
                                break;
                } else if (dev->req_config_methods &&
                           !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
-                               "pending Provision Discovery Request to "
+                       p2p_dbg(p2p, "Send pending Provision Discovery Request to "
                                MACSTR " (config methods 0x%x)",
                                MAC2STR(dev->info.p2p_device_addr),
                                dev->req_config_methods);
@@ -2452,14 +2640,13 @@ void p2p_continue_find(struct p2p_data *p2p)
                }
        }
 
-       p2p_listen_in_find(p2p);
+       p2p_listen_in_find(p2p, 1);
 }
 
 
 static void p2p_sd_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Service Discovery Query TX callback: success=%d",
+       p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
                success);
        p2p->pending_action_state = P2P_NO_PENDING_ACTION;
 
@@ -2473,8 +2660,7 @@ static void p2p_sd_cb(struct p2p_data *p2p, int success)
        }
 
        if (p2p->sd_peer == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No SD peer entry known");
+               p2p_dbg(p2p, "No SD peer entry known");
                p2p_continue_find(p2p);
                return;
        }
@@ -2498,8 +2684,7 @@ static void p2p_retry_pd(struct p2p_data *p2p)
 
        /*
         * Retry the prov disc req attempt only for the peer that the user had
-        * requested for and provided a join has not been initiated on it
-        * in the meantime.
+        * requested.
         */
 
        dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
@@ -2508,15 +2693,14 @@ static void p2p_retry_pd(struct p2p_data *p2p)
                        continue;
                if (!dev->req_config_methods)
                        continue;
-               if (dev->flags & P2P_DEV_PD_FOR_JOIN)
-                       continue;
 
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
-                       "pending Provision Discovery Request to "
+               p2p_dbg(p2p, "Send pending Provision Discovery Request to "
                        MACSTR " (config methods 0x%x)",
                        MAC2STR(dev->info.p2p_device_addr),
                        dev->req_config_methods);
-               p2p_send_prov_disc_req(p2p, dev, 0, 0);
+               p2p_send_prov_disc_req(p2p, dev,
+                                      dev->flags & P2P_DEV_PD_FOR_JOIN,
+                                      p2p->pd_force_freq);
                return;
        }
 }
@@ -2524,8 +2708,7 @@ static void p2p_retry_pd(struct p2p_data *p2p)
 
 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Provision Discovery Request TX callback: success=%d",
+       p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
                success);
 
        /*
@@ -2541,7 +2724,13 @@ static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
        if (!success) {
                p2p->pending_action_state = P2P_NO_PENDING_ACTION;
 
-               if (p2p->state != P2P_IDLE)
+               if (p2p->user_initiated_pd &&
+                   (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
+               {
+                       /* Retry request from timeout to avoid busy loops */
+                       p2p->pending_action_state = P2P_PENDING_PD;
+                       p2p_set_timeout(p2p, 0, 50000);
+               } else if (p2p->state != P2P_IDLE)
                        p2p_continue_find(p2p);
                else if (p2p->user_initiated_pd) {
                        p2p->pending_action_state = P2P_PENDING_PD;
@@ -2567,20 +2756,26 @@ static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
 
 
 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
-                        int level, const u8 *ies, size_t ies_len)
+                        struct os_reltime *rx_time, int level, const u8 *ies,
+                        size_t ies_len)
 {
-       p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
-
-       if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
-           os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
-           == 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Found GO Negotiation peer - try to start GO "
-                       "negotiation");
-               p2p_connect_send(p2p, p2p->go_neg_peer);
-               return 1;
+       if (os_reltime_before(rx_time, &p2p->find_start)) {
+               /*
+                * The driver may have cached (e.g., in cfg80211 BSS table) the
+                * scan results for relatively long time. To avoid reporting
+                * stale information, update P2P peers only based on results
+                * that have based on frames received after the last p2p_find
+                * operation was started.
+                */
+               p2p_dbg(p2p, "Ignore old scan result for " MACSTR
+                       " (rx_time=%u.%06u)",
+                       MAC2STR(bssid), (unsigned int) rx_time->sec,
+                       (unsigned int) rx_time->usec);
+               return 0;
        }
 
+       p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
+
        return 0;
 }
 
@@ -2588,8 +2783,7 @@ int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
 void p2p_scan_res_handled(struct p2p_data *p2p)
 {
        if (!p2p->p2p_scan_running) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
-                       "running, but scan results received");
+               p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
        }
        p2p->p2p_scan_running = 0;
        eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
@@ -2603,8 +2797,16 @@ void p2p_scan_res_handled(struct p2p_data *p2p)
 
 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
 {
-       u8 *len = p2p_buf_add_ie_hdr(ies);
-       p2p_buf_add_capability(ies, p2p->dev_capab, 0);
+       u8 *len;
+
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p->wfd_ie_probe_req)
+               wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
+#endif /* CONFIG_WIFI_DISPLAY */
+
+       len = p2p_buf_add_ie_hdr(ies);
+       p2p_buf_add_capability(ies, p2p->dev_capab &
+                              ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
        if (dev_id)
                p2p_buf_add_device_id(ies, dev_id);
        if (p2p->cfg->reg_class && p2p->cfg->channel)
@@ -2621,7 +2823,14 @@ void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
 
 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
 {
-       return 100;
+       size_t len = 100;
+
+#ifdef CONFIG_WIFI_DISPLAY
+       if (p2p && p2p->wfd_ie_probe_req)
+               len += wpabuf_len(p2p->wfd_ie_probe_req);
+#endif /* CONFIG_WIFI_DISPLAY */
+
+       return len;
 }
 
 
@@ -2634,31 +2843,29 @@ int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
 {
        struct p2p_device *dev = p2p->go_neg_peer;
+       int timeout;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Negotiation Request TX callback: success=%d",
-               success);
+       p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
 
        if (dev == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No pending GO Negotiation");
+               p2p_dbg(p2p, "No pending GO Negotiation");
                return;
        }
 
        if (success) {
-               dev->go_neg_req_sent++;
                if (dev->flags & P2P_DEV_USER_REJECTED) {
                        p2p_set_state(p2p, P2P_IDLE);
                        return;
                }
+       } else if (dev->go_neg_req_sent) {
+               /* Cancel the increment from p2p_connect_send() on failure */
+               dev->go_neg_req_sent--;
        }
 
        if (!success &&
            (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
            !is_zero_ether_addr(dev->member_in_go_dev)) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Peer " MACSTR " did not acknowledge request - "
-                       "try to use device discoverability through its GO",
+               p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
                        MAC2STR(dev->info.p2p_device_addr));
                p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
                p2p_send_dev_disc_req(p2p, dev);
@@ -2670,34 +2877,49 @@ static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
         * channel.
         */
        p2p_set_state(p2p, P2P_CONNECT);
-       p2p_set_timeout(p2p, 0, 100000);
+       timeout = success ? 500000 : 100000;
+       if (!success && p2p->go_neg_peer &&
+           (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
+               unsigned int r;
+               /*
+                * Peer is expected to wait our response and we will skip the
+                * listen phase. Add some randomness to the wait time here to
+                * make it less likely to hit cases where we could end up in
+                * sync with peer not listening.
+                */
+               os_get_random((u8 *) &r, sizeof(r));
+               timeout += r % 100000;
+       }
+       p2p_set_timeout(p2p, 0, timeout);
 }
 
 
 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Negotiation Response TX callback: success=%d",
+       p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
                success);
        if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Ignore TX callback event - GO Negotiation is "
-                       "not running anymore");
+               p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
                return;
        }
        p2p_set_state(p2p, P2P_CONNECT);
-       p2p_set_timeout(p2p, 0, 100000);
+       p2p_set_timeout(p2p, 0, 500000);
 }
 
 
-static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
+static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
+                                      const u8 *addr)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Negotiation Response (failure) TX callback: "
-               "success=%d", success);
+       p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
        if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
                p2p_go_neg_failed(p2p, p2p->go_neg_peer,
                                  p2p->go_neg_peer->status);
+       } else if (success) {
+               struct p2p_device *dev;
+               dev = p2p_get_device(p2p, addr);
+               if (dev &&
+                   dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
+                       dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
        }
 }
 
@@ -2707,9 +2929,7 @@ static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
 {
        struct p2p_device *dev;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: GO Negotiation Confirm TX callback: result=%d",
-               result);
+       p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
        p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
        if (result == P2P_SEND_ACTION_FAILED) {
                p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
@@ -2726,10 +2946,7 @@ static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
                 * peer did indeed receive the frame, continue regardless of
                 * the TX status.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Assume GO Negotiation Confirm TX was actually "
-                       "received by the peer even though Ack was not "
-                       "reported");
+               p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
        }
 
        dev = p2p->go_neg_peer;
@@ -2747,8 +2964,7 @@ void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
        enum p2p_pending_action_state state;
        int success;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
+       p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
                " src=" MACSTR " bssid=" MACSTR " result=%d",
                p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
                MAC2STR(bssid), result);
@@ -2757,7 +2973,17 @@ void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
        p2p->pending_action_state = P2P_NO_PENDING_ACTION;
        switch (state) {
        case P2P_NO_PENDING_ACTION:
-               break;
+               if (p2p->after_scan_tx_in_progress) {
+                       p2p->after_scan_tx_in_progress = 0;
+                       if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
+                           p2p_run_after_scan(p2p))
+                               break;
+                       if (p2p->state == P2P_SEARCH) {
+                               p2p_dbg(p2p, "Continue find after after_scan_tx completion");
+                               p2p_continue_find(p2p);
+                       }
+               }
+               break;
        case P2P_PENDING_GO_NEG_REQUEST:
                p2p_go_neg_req_cb(p2p, success);
                break;
@@ -2765,7 +2991,7 @@ void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
                p2p_go_neg_resp_cb(p2p, success);
                break;
        case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
-               p2p_go_neg_resp_failure_cb(p2p, success);
+               p2p_go_neg_resp_failure_cb(p2p, success, dst);
                break;
        case P2P_PENDING_GO_NEG_CONFIRM:
                p2p_go_neg_conf_cb(p2p, result);
@@ -2792,6 +3018,8 @@ void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
                p2p_go_disc_req_cb(p2p, success);
                break;
        }
+
+       p2p->after_scan_tx_in_progress = 0;
 }
 
 
@@ -2799,23 +3027,18 @@ void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
                   unsigned int duration)
 {
        if (freq == p2p->pending_client_disc_freq) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Client discoverability remain-awake completed");
+               p2p_dbg(p2p, "Client discoverability remain-awake completed");
                p2p->pending_client_disc_freq = 0;
                return;
        }
 
        if (freq != p2p->pending_listen_freq) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Unexpected listen callback for freq=%u "
-                       "duration=%u (pending_listen_freq=%u)",
+               p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
                        freq, duration, p2p->pending_listen_freq);
                return;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
-               "callback",
+       p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
                p2p->pending_listen_sec, p2p->pending_listen_usec,
                p2p->pending_listen_freq);
        p2p->in_listen = 1;
@@ -2836,17 +3059,14 @@ void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
 
 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
-               "state (freq=%u)", freq);
+       p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
        p2p->drv_in_listen = 0;
        if (p2p->in_listen)
                return 0; /* Internal timeout will trigger the next step */
 
        if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
                if (p2p->go_neg_peer->connect_reqs >= 120) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Timeout on sending GO Negotiation "
-                               "Request without getting response");
+                       p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
                        p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
                        return 0;
                }
@@ -2864,9 +3084,24 @@ int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
                          * operation while in p2p_find. Avoid an attempt to
                          * restart a scan here.
                          */
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
-                               "already in progress - do not try to start a "
-                               "new one");
+                       p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
+                       return 1;
+               }
+               if (p2p->pending_listen_freq) {
+                       /*
+                        * Better wait a bit if the driver is unable to start
+                        * offchannel operation for some reason. p2p_search()
+                        * will be started from internal timeout.
+                        */
+                       p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
+                       p2p_set_timeout(p2p, 0, 100000);
+                       return 1;
+               }
+               if (p2p->search_delay) {
+                       p2p_dbg(p2p, "Delay search operation by %u ms",
+                               p2p->search_delay);
+                       p2p_set_timeout(p2p, p2p->search_delay / 1000,
+                                       (p2p->search_delay % 1000) * 1000);
                        return 1;
                }
                p2p_search(p2p);
@@ -2880,8 +3115,27 @@ int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
 static void p2p_timeout_connect(struct p2p_data *p2p)
 {
        p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
+       if (p2p->go_neg_peer &&
+           (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
+               p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
+               p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
+               return;
+       }
+       if (p2p->go_neg_peer &&
+           (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
+           p2p->go_neg_peer->connect_reqs < 120) {
+               p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
+               p2p_connect_send(p2p, p2p->go_neg_peer);
+               return;
+       }
+       if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
+               p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
+               p2p_set_state(p2p, P2P_CONNECT_LISTEN);
+               p2p_set_timeout(p2p, 0, 30000);
+               return;
+       }
        p2p_set_state(p2p, P2P_CONNECT_LISTEN);
-       p2p_listen_in_find(p2p);
+       p2p_listen_in_find(p2p, 0);
 }
 
 
@@ -2889,16 +3143,12 @@ static void p2p_timeout_connect_listen(struct p2p_data *p2p)
 {
        if (p2p->go_neg_peer) {
                if (p2p->drv_in_listen) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
-                               "still in Listen state; wait for it to "
-                               "complete");
+                       p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
                        return;
                }
 
                if (p2p->go_neg_peer->connect_reqs >= 120) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Timeout on sending GO Negotiation "
-                               "Request without getting response");
+                       p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
                        p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
                        return;
                }
@@ -2912,13 +3162,13 @@ static void p2p_timeout_connect_listen(struct p2p_data *p2p)
 
 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
 {
-       /*
-        * TODO: could remain constantly in Listen state for some time if there
-        * are no other concurrent uses for the radio. For now, go to listen
-        * state once per second to give other uses a chance to use the radio.
-        */
        p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
-       p2p_set_timeout(p2p, 0, 500000);
+
+       if (p2p->cfg->is_concurrent_session_active &&
+           p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
+               p2p_set_timeout(p2p, 0, 500000);
+       else
+               p2p_set_timeout(p2p, 0, 200000);
 }
 
 
@@ -2927,32 +3177,26 @@ static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
        struct p2p_device *dev = p2p->go_neg_peer;
 
        if (dev == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Unknown GO Neg peer - stop GO Neg wait");
+               p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
                return;
        }
 
        dev->wait_count++;
        if (dev->wait_count >= 120) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Timeout on waiting peer to become ready for GO "
-                       "Negotiation");
+               p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation");
                p2p_go_neg_failed(p2p, dev, -1);
                return;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Go to Listen state while waiting for the peer to become "
-               "ready for GO Negotiation");
+       p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
        p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
-       p2p_listen_in_find(p2p);
+       p2p_listen_in_find(p2p, 0);
 }
 
 
 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Service Discovery Query timeout");
+       p2p_dbg(p2p, "Service Discovery Query timeout");
        if (p2p->sd_peer) {
                p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
                p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
@@ -2964,8 +3208,7 @@ static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
 
 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Provision Discovery Request timeout");
+       p2p_dbg(p2p, "Provision Discovery Request timeout");
        p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
        p2p_continue_find(p2p);
 }
@@ -2983,16 +3226,29 @@ static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
        if (!p2p->user_initiated_pd)
                return;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: User initiated Provision Discovery Request timeout");
+       p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
 
        if (p2p->pd_retries) {
                p2p->pd_retries--;
                p2p_retry_pd(p2p);
        } else {
+               struct p2p_device *dev;
+               int for_join = 0;
+
+               dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
+                       if (os_memcmp(p2p->pending_pd_devaddr,
+                                     dev->info.p2p_device_addr, ETH_ALEN) != 0)
+                               continue;
+                       if (dev->req_config_methods &&
+                           (dev->flags & P2P_DEV_PD_FOR_JOIN))
+                               for_join = 1;
+               }
+
                if (p2p->cfg->prov_disc_fail)
                        p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
                                                 p2p->pending_pd_devaddr,
+                                                for_join ?
+                                                P2P_PROV_DISC_TIMEOUT_JOIN :
                                                 P2P_PROV_DISC_TIMEOUT);
                p2p_reset_pending_pd(p2p);
        }
@@ -3008,12 +3264,11 @@ static void p2p_timeout_invite(struct p2p_data *p2p)
                 * Better remain on operating channel instead of listen channel
                 * when running a group.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
-                       "active GO role - wait on operating channel");
+               p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
                p2p_set_timeout(p2p, 0, 100000);
                return;
        }
-       p2p_listen_in_find(p2p);
+       p2p_listen_in_find(p2p, 0);
 }
 
 
@@ -3025,11 +3280,12 @@ static void p2p_timeout_invite_listen(struct p2p_data *p2p)
                                p2p->invite_go_dev_addr);
        } else {
                if (p2p->invite_peer) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Invitation Request retry limit reached");
+                       p2p_dbg(p2p, "Invitation Request retry limit reached");
                        if (p2p->cfg->invitation_result)
                                p2p->cfg->invitation_result(
-                                       p2p->cfg->cb_ctx, -1, NULL);
+                                       p2p->cfg->cb_ctx, -1, NULL, NULL,
+                                       p2p->invite_peer->info.p2p_device_addr,
+                                       0);
                }
                p2p_set_state(p2p, P2P_IDLE);
        }
@@ -3040,8 +3296,7 @@ static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
 {
        struct p2p_data *p2p = eloop_ctx;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
-               p2p_state_txt(p2p->state));
+       p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
 
        p2p->in_listen = 0;
 
@@ -3055,6 +3310,15 @@ static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
                /* Check if we timed out waiting for PD req */
                if (p2p->pending_action_state == P2P_PENDING_PD)
                        p2p_timeout_prov_disc_req(p2p);
+               if (p2p->search_delay && !p2p->in_search_delay) {
+                       p2p_dbg(p2p, "Delay search operation by %u ms",
+                               p2p->search_delay);
+                       p2p->in_search_delay = 1;
+                       p2p_set_timeout(p2p, p2p->search_delay / 1000,
+                                       (p2p->search_delay % 1000) * 1000);
+                       break;
+               }
+               p2p->in_search_delay = 0;
                p2p_search(p2p);
                break;
        case P2P_CONNECT:
@@ -3071,9 +3335,7 @@ static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
                        p2p_timeout_prov_disc_req(p2p);
 
                if (p2p->ext_listen_only) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                               "P2P: Extended Listen Timing - Listen State "
-                               "completed");
+                       p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
                        p2p->ext_listen_only = 0;
                        p2p_set_state(p2p, P2P_IDLE);
                }
@@ -3098,8 +3360,6 @@ static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
        case P2P_INVITE_LISTEN:
                p2p_timeout_invite_listen(p2p);
                break;
-       case P2P_SEARCH_WHEN_READY:
-               break;
        }
 }
 
@@ -3109,11 +3369,10 @@ int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
        struct p2p_device *dev;
 
        dev = p2p_get_device(p2p, peer_addr);
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
-               "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
+       p2p_dbg(p2p, "Local request to reject connection attempts by peer "
+               MACSTR, MAC2STR(peer_addr));
        if (dev == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
-                       " unknown", MAC2STR(peer_addr));
+               p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
                return -1;
        }
        dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
@@ -3133,6 +3392,8 @@ const char * p2p_wps_method_text(enum p2p_wps_method method)
                return "Keypad";
        case WPS_PBC:
                return "PBC";
+       case WPS_NFC:
+               return "NFC";
        }
 
        return "??";
@@ -3183,7 +3444,7 @@ int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
        struct p2p_device *dev;
        int res;
        char *pos, *end;
-       struct os_time now;
+       struct os_reltime now;
 
        if (info == NULL)
                return -1;
@@ -3194,7 +3455,7 @@ int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
        pos = buf;
        end = buf + buflen;
 
-       os_get_time(&now);
+       os_get_reltime(&now);
        res = os_snprintf(pos, end - pos,
                          "age=%d\n"
                          "listen_freq=%d\n"
@@ -3283,6 +3544,24 @@ int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
                pos += res;
        }
 
+#ifdef CONFIG_WIFI_DISPLAY
+       if (dev->info.wfd_subelems) {
+               res = os_snprintf(pos, end - pos, "wfd_subelems=");
+               if (res < 0 || res >= end - pos)
+                       return pos - buf;
+               pos += res;
+
+               pos += wpa_snprintf_hex(pos, end - pos,
+                                       wpabuf_head(dev->info.wfd_subelems),
+                                       wpabuf_len(dev->info.wfd_subelems));
+
+               res = os_snprintf(pos, end - pos, "\n");
+               if (res < 0 || res >= end - pos)
+                       return pos - buf;
+               pos += res;
+       }
+#endif /* CONFIG_WIFI_DISPLAY */
+
        return pos - buf;
 }
 
@@ -3296,12 +3575,10 @@ int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
 {
        if (enabled) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
-                       "discoverability enabled");
+               p2p_dbg(p2p, "Client discoverability enabled");
                p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
        } else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
-                       "discoverability disabled");
+               p2p_dbg(p2p, "Client discoverability disabled");
                p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
        }
 }
@@ -3350,9 +3627,9 @@ int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
 {
        struct wpabuf *req;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
-               "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
-               "int1=%u dur2=%u int2=%u",
+       p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
+               " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
+               "dur2=%u int2=%u",
                MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
                freq, duration1, interval1, duration2, interval2);
 
@@ -3365,8 +3642,7 @@ int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
        if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
                            go_interface_addr,
                            wpabuf_head(req), wpabuf_len(req), 200) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to send Action frame");
+               p2p_dbg(p2p, "Failed to send Action frame");
        }
        wpabuf_free(req);
 
@@ -3412,8 +3688,7 @@ static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
        u8 noa[50];
        int noa_len;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Received P2P Action - P2P Presence Request");
+       p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
 
        for (g = 0; g < p2p->num_groups; g++) {
                if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
@@ -3423,23 +3698,20 @@ static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
                }
        }
        if (group == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Ignore P2P Presence Request for unknown group "
+               p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
                        MACSTR, MAC2STR(da));
                return;
        }
 
        if (p2p_parse(data, len, &msg) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to parse P2P Presence Request");
+               p2p_dbg(p2p, "Failed to parse P2P Presence Request");
                status = P2P_SC_FAIL_INVALID_PARAMS;
                goto fail;
        }
        parsed = 1;
 
        if (msg.noa == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No NoA attribute in P2P Presence Request");
+               p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
                status = P2P_SC_FAIL_INVALID_PARAMS;
                goto fail;
        }
@@ -3463,8 +3735,7 @@ fail:
        p2p->pending_action_state = P2P_NO_PENDING_ACTION;
        if (p2p_send_action(p2p, rx_freq, sa, da, da,
                            wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to send Action frame");
+               p2p_dbg(p2p, "Failed to send Action frame");
        }
        wpabuf_free(resp);
 }
@@ -3475,33 +3746,32 @@ static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
 {
        struct p2p_message msg;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Received P2P Action - P2P Presence Response");
+       p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
 
        if (p2p_parse(data, len, &msg) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Failed to parse P2P Presence Response");
+               p2p_dbg(p2p, "Failed to parse P2P Presence Response");
                return;
        }
 
        if (msg.status == NULL || msg.noa == NULL) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: No Status or NoA attribute in P2P Presence "
-                       "Response");
+               p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
                p2p_parse_free(&msg);
                return;
        }
 
+       if (p2p->cfg->presence_resp) {
+               p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
+                                       msg.noa, msg.noa_len);
+       }
+
        if (*msg.status) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: P2P Presence Request was rejected: status %u",
+               p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
                        *msg.status);
                p2p_parse_free(&msg);
                return;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: P2P Presence Request was accepted");
+       p2p_dbg(p2p, "P2P Presence Request was accepted");
        wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
                    msg.noa, msg.noa_len);
        /* TODO: process NoA */
@@ -3527,25 +3797,20 @@ static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
                 * running at an inconvenient time. As a workaround, allow new
                 * Extended Listen operation to be started.
                 */
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
-                       "Extended Listen operation had not been completed - "
-                       "try again");
+               p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
                p2p->ext_listen_only = 0;
                p2p_set_state(p2p, P2P_IDLE);
        }
 
        if (p2p->state != P2P_IDLE) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
-                       "Listen timeout in active state (%s)",
-                       p2p_state_txt(p2p->state));
+               p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
                return;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
+       p2p_dbg(p2p, "Extended Listen timeout");
        p2p->ext_listen_only = 1;
        if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
-                       "Listen state for Extended Listen Timing");
+               p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
                p2p->ext_listen_only = 0;
        }
 }
@@ -3556,25 +3821,22 @@ int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
 {
        if (period > 65535 || interval > 65535 || period > interval ||
            (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Invalid Extended Listen Timing request: "
-                       "period=%u interval=%u", period, interval);
+               p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
+                       period, interval);
                return -1;
        }
 
        eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
 
        if (interval == 0) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-                       "P2P: Disabling Extended Listen Timing");
+               p2p_dbg(p2p, "Disabling Extended Listen Timing");
                p2p->ext_listen_period = 0;
                p2p->ext_listen_interval = 0;
                return 0;
        }
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
-               "P2P: Enabling Extended Listen Timing: period %u msec, "
-               "interval %u msec", period, interval);
+       p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
+               period, interval);
        p2p->ext_listen_period = period;
        p2p->ext_listen_interval = interval;
        p2p->ext_listen_interval_sec = interval / 1000;
@@ -3602,8 +3864,7 @@ void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
        if (msg.minor_reason_code == NULL)
                return;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
-               "P2P: Deauthentication notification BSSID " MACSTR
+       p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
                " reason_code=%u minor_reason_code=%u",
                MAC2STR(bssid), reason_code, *msg.minor_reason_code);
 
@@ -3625,8 +3886,7 @@ void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
        if (msg.minor_reason_code == NULL)
                return;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
-               "P2P: Disassociation notification BSSID " MACSTR
+       p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
                " reason_code=%u minor_reason_code=%u",
                MAC2STR(bssid), reason_code, *msg.minor_reason_code);
 
@@ -3637,12 +3897,10 @@ void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
 {
        if (enabled) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
-                       "Device operations enabled");
+               p2p_dbg(p2p, "Managed P2P Device operations enabled");
                p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
        } else {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
-                       "Device operations disabled");
+               p2p_dbg(p2p, "Managed P2P Device operations disabled");
                p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
        }
 }
@@ -3650,11 +3908,11 @@ void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
 
 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
 {
-       if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
+       if (p2p_channel_to_freq(reg_class, channel) < 0)
                return -1;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
-               "reg_class %u channel %u", reg_class, channel);
+       p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
+               reg_class, channel);
        p2p->cfg->reg_class = reg_class;
        p2p->cfg->channel = channel;
 
@@ -3664,7 +3922,7 @@ int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
 
 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
 {
-       wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
+       p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
        if (postfix == NULL) {
                p2p->cfg->ssid_postfix_len = 0;
                return 0;
@@ -3680,12 +3938,11 @@ int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
                         int cfg_op_channel)
 {
-       if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
-           < 0)
+       if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
                return -1;
 
-       wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
-               "reg_class %u channel %u", op_reg_class, op_channel);
+       p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
+               op_reg_class, op_channel);
        p2p->cfg->op_reg_class = op_reg_class;
        p2p->cfg->op_channel = op_channel;
        p2p->cfg->cfg_op_channel = cfg_op_channel;
@@ -3693,6 +3950,53 @@ int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
 }
 
 
+int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
+                     const struct p2p_channel *pref_chan)
+{
+       struct p2p_channel *n;
+
+       if (pref_chan) {
+               n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
+               if (n == NULL)
+                       return -1;
+               os_memcpy(n, pref_chan,
+                         num_pref_chan * sizeof(struct p2p_channel));
+       } else
+               n = NULL;
+
+       os_free(p2p->cfg->pref_chan);
+       p2p->cfg->pref_chan = n;
+       p2p->cfg->num_pref_chan = num_pref_chan;
+
+       return 0;
+}
+
+
+int p2p_set_no_go_freq(struct p2p_data *p2p,
+                      const struct wpa_freq_range_list *list)
+{
+       struct wpa_freq_range *tmp;
+
+       if (list == NULL || list->num == 0) {
+               os_free(p2p->no_go_freq.range);
+               p2p->no_go_freq.range = NULL;
+               p2p->no_go_freq.num = 0;
+               return 0;
+       }
+
+       tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
+       if (tmp == NULL)
+               return -1;
+       os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
+       os_free(p2p->no_go_freq.range);
+       p2p->no_go_freq.range = tmp;
+       p2p->no_go_freq.num = list->num;
+       p2p_dbg(p2p, "Updated no GO chan list");
+
+       return 0;
+}
+
+
 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
                           u8 *iface_addr)
 {
@@ -3719,18 +4023,16 @@ void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
 {
        os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
        if (is_zero_ether_addr(p2p->peer_filter))
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
-                       "filter");
+               p2p_dbg(p2p, "Disable peer filter");
        else
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
-                       "filter for " MACSTR, MAC2STR(p2p->peer_filter));
+               p2p_dbg(p2p, "Enable peer filter for " MACSTR,
+                       MAC2STR(p2p->peer_filter));
 }
 
 
 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
-               enabled ? "enabled" : "disabled");
+       p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
        if (p2p->cross_connect == enabled)
                return;
        p2p->cross_connect = enabled;
@@ -3751,16 +4053,22 @@ int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
 
 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
+       p2p_dbg(p2p, "Intra BSS distribution %s",
                enabled ? "enabled" : "disabled");
        p2p->cfg->p2p_intra_bss = enabled;
 }
 
 
-void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
+void p2p_update_channel_list(struct p2p_data *p2p,
+                            const struct p2p_channels *chan,
+                            const struct p2p_channels *cli_chan)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
+       p2p_dbg(p2p, "Update channel list");
        os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
+       p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
+       os_memcpy(&p2p->cfg->cli_channels, cli_chan,
+                 sizeof(struct p2p_channels));
+       p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
 }
 
 
@@ -3769,11 +4077,9 @@ int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
                    size_t len, unsigned int wait_time)
 {
        if (p2p->p2p_scan_running) {
-               wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
-                       "frame TX until p2p_scan completes");
+               p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
                if (p2p->after_scan_tx) {
-                       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
-                               "previous pending Action frame TX");
+                       p2p_dbg(p2p, "Dropped previous pending Action frame TX");
                        os_free(p2p->after_scan_tx);
                }
                p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
@@ -3798,14 +4104,21 @@ int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
                           int freq_overall)
 {
-       wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
-               "  5 GHz: %d,  overall: %d", freq_24, freq_5, freq_overall);
+       p2p_dbg(p2p, "Best channel: 2.4 GHz: %d,  5 GHz: %d,  overall: %d",
+               freq_24, freq_5, freq_overall);
        p2p->best_freq_24 = freq_24;
        p2p->best_freq_5 = freq_5;
        p2p->best_freq_overall = freq_overall;
 }
 
 
+void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
+{
+       p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
+       p2p->own_freq_preference = freq;
+}
+
+
 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
 {
        if (p2p == NULL || p2p->go_neg_peer == NULL)
@@ -3859,5 +4172,402 @@ int p2p_in_progress(struct p2p_data *p2p)
 {
        if (p2p == NULL)
                return 0;
+       if (p2p->state == P2P_SEARCH)
+               return 2;
        return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
 }
+
+
+void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
+                           u8 client_timeout)
+{
+       if (p2p) {
+               p2p->go_timeout = go_timeout;
+               p2p->client_timeout = client_timeout;
+       }
+}
+
+
+#ifdef CONFIG_WIFI_DISPLAY
+
+static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
+{
+       size_t g;
+       struct p2p_group *group;
+
+       for (g = 0; g < p2p->num_groups; g++) {
+               group = p2p->groups[g];
+               p2p_group_force_beacon_update_ies(group);
+       }
+}
+
+
+int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_beacon);
+       p2p->wfd_ie_beacon = ie;
+       p2p_update_wfd_ie_groups(p2p);
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_probe_req);
+       p2p->wfd_ie_probe_req = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_probe_resp);
+       p2p->wfd_ie_probe_resp = ie;
+       p2p_update_wfd_ie_groups(p2p);
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_assoc_req);
+       p2p->wfd_ie_assoc_req = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_invitation);
+       p2p->wfd_ie_invitation = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_prov_disc_req);
+       p2p->wfd_ie_prov_disc_req = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_prov_disc_resp);
+       p2p->wfd_ie_prov_disc_resp = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
+{
+       wpabuf_free(p2p->wfd_ie_go_neg);
+       p2p->wfd_ie_go_neg = ie;
+       return 0;
+}
+
+
+int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
+{
+       wpabuf_free(p2p->wfd_dev_info);
+       if (elem) {
+               p2p->wfd_dev_info = wpabuf_dup(elem);
+               if (p2p->wfd_dev_info == NULL)
+                       return -1;
+       } else
+               p2p->wfd_dev_info = NULL;
+
+       return 0;
+}
+
+
+int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
+{
+       wpabuf_free(p2p->wfd_assoc_bssid);
+       if (elem) {
+               p2p->wfd_assoc_bssid = wpabuf_dup(elem);
+               if (p2p->wfd_assoc_bssid == NULL)
+                       return -1;
+       } else
+               p2p->wfd_assoc_bssid = NULL;
+
+       return 0;
+}
+
+
+int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
+                                 const struct wpabuf *elem)
+{
+       wpabuf_free(p2p->wfd_coupled_sink_info);
+       if (elem) {
+               p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
+               if (p2p->wfd_coupled_sink_info == NULL)
+                       return -1;
+       } else
+               p2p->wfd_coupled_sink_info = NULL;
+
+       return 0;
+}
+
+#endif /* CONFIG_WIFI_DISPLAY */
+
+
+int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
+                    int max_disc_tu)
+{
+       if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
+               return -1;
+
+       p2p->min_disc_int = min_disc_int;
+       p2p->max_disc_int = max_disc_int;
+       p2p->max_disc_tu = max_disc_tu;
+       p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
+               min_disc_int, max_disc_int, max_disc_tu);
+
+       return 0;
+}
+
+
+void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
+{
+       va_list ap;
+       char buf[500];
+
+       if (!p2p->cfg->debug_print)
+               return;
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       buf[sizeof(buf) - 1] = '\0';
+       va_end(ap);
+       p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
+}
+
+
+void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
+{
+       va_list ap;
+       char buf[500];
+
+       if (!p2p->cfg->debug_print)
+               return;
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       buf[sizeof(buf) - 1] = '\0';
+       va_end(ap);
+       p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
+}
+
+
+void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
+{
+       va_list ap;
+       char buf[500];
+
+       if (!p2p->cfg->debug_print)
+               return;
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       buf[sizeof(buf) - 1] = '\0';
+       va_end(ap);
+       p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
+}
+
+
+#ifdef CONFIG_WPS_NFC
+
+static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
+                                             int client_freq)
+{
+       struct wpabuf *buf;
+       u8 op_class, channel;
+       enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
+
+       buf = wpabuf_alloc(1000);
+       if (buf == NULL)
+               return NULL;
+
+       op_class = p2p->cfg->reg_class;
+       channel = p2p->cfg->channel;
+
+       p2p_buf_add_capability(buf, p2p->dev_capab &
+                              ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
+       p2p_buf_add_device_info(buf, p2p, NULL);
+
+       if (p2p->num_groups > 0) {
+               role = P2P_GO_IN_A_GROUP;
+               p2p_freq_to_channel(p2p_group_get_freq(p2p->groups[0]),
+                                   &op_class, &channel);
+       } else if (client_freq > 0) {
+               role = P2P_CLIENT_IN_A_GROUP;
+               p2p_freq_to_channel(client_freq, &op_class, &channel);
+       }
+
+       p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
+                                      channel, role);
+
+       if (p2p->num_groups > 0) {
+               /* Limit number of clients to avoid very long message */
+               p2p_buf_add_group_info(p2p->groups[0], buf, 5);
+               p2p_group_buf_add_id(p2p->groups[0], buf);
+       }
+
+       return buf;
+}
+
+
+struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
+                                          int client_freq)
+{
+       return p2p_build_nfc_handover(p2p, client_freq);
+}
+
+
+struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
+                                          int client_freq)
+{
+       return p2p_build_nfc_handover(p2p, client_freq);
+}
+
+
+int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
+                                       struct p2p_nfc_params *params)
+{
+       struct p2p_message msg;
+       struct p2p_device *dev;
+       const u8 *p2p_dev_addr;
+       int freq;
+       enum p2p_role_indication role;
+
+       params->next_step = NO_ACTION;
+
+       if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
+                                  params->p2p_attr, params->p2p_len, &msg)) {
+               p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
+               p2p_parse_free(&msg);
+               return -1;
+       }
+
+       if (msg.p2p_device_addr)
+               p2p_dev_addr = msg.p2p_device_addr;
+       else if (msg.device_id)
+               p2p_dev_addr = msg.device_id;
+       else {
+               p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
+               p2p_parse_free(&msg);
+               return -1;
+       }
+
+       if (msg.oob_dev_password) {
+               os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
+                         msg.oob_dev_password_len);
+               params->oob_dev_pw_len = msg.oob_dev_password_len;
+       }
+
+       dev = p2p_create_device(p2p, p2p_dev_addr);
+       if (dev == NULL) {
+               p2p_parse_free(&msg);
+               return -1;
+       }
+
+       params->peer = &dev->info;
+
+       os_get_reltime(&dev->last_seen);
+       dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
+       p2p_copy_wps_info(p2p, dev, 0, &msg);
+
+       if (!msg.oob_go_neg_channel) {
+               p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
+               return -1;
+       }
+
+       if (msg.oob_go_neg_channel[3] == 0 &&
+           msg.oob_go_neg_channel[4] == 0)
+               freq = 0;
+       else
+               freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
+                                          msg.oob_go_neg_channel[4]);
+       if (freq < 0) {
+               p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
+               return -1;
+       }
+       role = msg.oob_go_neg_channel[5];
+
+       if (role == P2P_GO_IN_A_GROUP) {
+               p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
+               params->go_freq = freq;
+       } else
+               p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
+       dev->oob_go_neg_freq = freq;
+
+       if (!params->sel && role != P2P_GO_IN_A_GROUP) {
+               freq = p2p_channel_to_freq(p2p->cfg->reg_class,
+                                          p2p->cfg->channel);
+               if (freq < 0) {
+                       p2p_dbg(p2p, "Own listen channel not known");
+                       return -1;
+               }
+               p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
+               dev->oob_go_neg_freq = freq;
+       }
+
+       if (msg.group_id) {
+               os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
+               params->go_ssid_len = msg.group_id_len - ETH_ALEN;
+               os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
+                         params->go_ssid_len);
+       }
+
+       p2p_parse_free(&msg);
+
+       if (dev->flags & P2P_DEV_USER_REJECTED) {
+               p2p_dbg(p2p, "Do not report rejected device");
+               return 0;
+       }
+
+       if (!(dev->flags & P2P_DEV_REPORTED)) {
+               p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
+                                   !(dev->flags & P2P_DEV_REPORTED_ONCE));
+               dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
+       }
+
+       if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
+               params->next_step = BOTH_GO;
+       else if (role == P2P_GO_IN_A_GROUP)
+               params->next_step = JOIN_GROUP;
+       else if (role == P2P_CLIENT_IN_A_GROUP) {
+               dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
+               params->next_step = PEER_CLIENT;
+       } else if (p2p->num_groups > 0)
+               params->next_step = AUTH_JOIN;
+       else if (params->sel)
+               params->next_step = INIT_GO_NEG;
+       else
+               params->next_step = RESP_GO_NEG;
+
+       return 0;
+}
+
+
+void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
+                                     int go_intent,
+                                     const u8 *own_interface_addr)
+{
+
+       p2p->authorized_oob_dev_pw_id = dev_pw_id;
+       if (dev_pw_id == 0) {
+               p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
+               return;
+       }
+
+       p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
+               dev_pw_id);
+
+       p2p->go_intent = go_intent;
+       os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
+}
+
+#endif /* CONFIG_WPS_NFC */