P2P: Add support for automatic channel selection at GO
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
index 3fb872d..2f6ae89 100644 (file)
@@ -46,6 +46,8 @@ static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
                         const u8 *dev_addr, enum p2p_wps_method wps_method);
 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
+static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
+static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
 
 
 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
@@ -65,7 +67,7 @@ static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
                                         bss->freq, bss->level,
                                         (const u8 *) (bss + 1),
                                         bss->ie_len) > 0)
-                       return;
+                       break;
        }
 
        p2p_scan_res_handled(wpa_s->global->p2p);
@@ -175,6 +177,9 @@ static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
 {
        struct wpa_ssid *ssid;
        char *gtype;
+       const char *reason;
+
+       eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
 
        ssid = wpa_s->current_ssid;
        if (ssid == NULL) {
@@ -208,8 +213,22 @@ static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
                        wpa_s->ifname, wpa_s->cross_connect_uplink);
        }
-       wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s",
-               wpa_s->ifname, gtype);
+       switch (wpa_s->removal_reason) {
+       case P2P_GROUP_REMOVAL_REQUESTED:
+               reason = " reason=REQUESTED";
+               break;
+       case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
+               reason = " reason=IDLE";
+               break;
+       case P2P_GROUP_REMOVAL_UNAVAILABLE:
+               reason = " reason=UNAVAILABLE";
+               break;
+       default:
+               reason = "";
+               break;
+       }
+       wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
+               wpa_s->ifname, gtype, reason);
        if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
                struct wpa_global *global;
                char *ifname;
@@ -451,15 +470,17 @@ static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
                        MAC2STR(go_dev_addr),
                        persistent ? " [PERSISTENT]" : "");
                wpas_p2p_cross_connect_setup(wpa_s);
+               wpas_p2p_set_group_idle_timeout(wpa_s);
        } else {
                wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
                        "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
                        "go_dev_addr=" MACSTR "%s",
-                       wpa_s->ifname, ssid_txt, ssid->frequency,
+                       wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
                        ssid && ssid->passphrase ? ssid->passphrase : "",
                        MAC2STR(go_dev_addr),
                        persistent ? " [PERSISTENT]" : "");
                wpas_p2p_cross_connect_setup(wpa_s);
+               wpas_p2p_set_group_idle_timeout(wpa_s);
        }
 
        if (persistent)
@@ -468,6 +489,35 @@ static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
 }
 
 
+static struct wpa_supplicant *
+wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
+{
+       struct wpa_supplicant *iface;
+
+       if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
+               return wpa_s;
+
+       /*
+        * Try to find a group interface that matches with the source address.
+        */
+       iface = wpa_s->global->ifaces;
+       while (iface) {
+               if (os_memcmp(wpa_s->pending_action_src,
+                             iface->own_addr, ETH_ALEN) == 0)
+                       break;
+               iface = iface->next;
+       }
+       if (iface) {
+               wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
+                          "instead of interface %s for Action TX",
+                          iface->ifname, wpa_s->ifname);
+               return iface;
+       }
+
+       return wpa_s;
+}
+
+
 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
 {
        struct wpa_supplicant *wpa_s = eloop_ctx;
@@ -484,12 +534,26 @@ static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
        if (wpa_s->pending_action_tx == NULL)
                return;
 
+       /*
+        * This call is likely going to be on the P2P device instance if the
+        * driver uses a separate interface for that purpose. However, some
+        * Action frames are actually sent within a P2P Group and when that is
+        * the case, we need to follow power saving (e.g., GO buffering the
+        * frame for a client in PS mode or a client following the advertised
+        * NoA from its GO). To make that easier for the driver, select the
+        * correct group interface here.
+        */
+       iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
+
        if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
-           wpa_s->pending_action_freq != 0) {
+           wpa_s->pending_action_freq != 0 &&
+           wpa_s->pending_action_freq != iface->assoc_freq) {
                wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
-                          "waiting for another freq=%u (off_channel_freq=%u)",
+                          "waiting for another freq=%u (off_channel_freq=%u "
+                          "assoc_freq=%u)",
                           wpa_s->pending_action_freq,
-                          wpa_s->off_channel_freq);
+                          wpa_s->off_channel_freq,
+                          iface->assoc_freq);
                if (without_roc && wpa_s->off_channel_freq == 0) {
                        /*
                         * We may get here if wpas_send_action() found us to be
@@ -505,44 +569,15 @@ static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
                                           "driver to remain on channel (%u "
                                           "MHz) for Action Frame TX",
                                           wpa_s->pending_action_freq);
-                       } else
+                       } else {
+                               wpa_s->off_channel_freq = 0;
                                wpa_s->roc_waiting_drv_freq =
                                        wpa_s->pending_action_freq;
+                       }
                }
                return;
        }
 
-       /*
-        * This call is likely going to be on the P2P device instance if the
-        * driver uses a separate interface for that purpose. However, some
-        * Action frames are actually sent within a P2P Group and when that is
-        * the case, we need to follow power saving (e.g., GO buffering the
-        * frame for a client in PS mode or a client following the advertised
-        * NoA from its GO). To make that easier for the driver, select the
-        * correct group interface here.
-        */
-       if (os_memcmp(wpa_s->pending_action_src, wpa_s->own_addr, ETH_ALEN) !=
-           0) {
-               /*
-                * Try to find a group interface that matches with the source
-                * address.
-                */
-               iface = wpa_s->global->ifaces;
-               while (iface) {
-                       if (os_memcmp(wpa_s->pending_action_src,
-                                     iface->own_addr, ETH_ALEN) == 0)
-                               break;
-                       iface = iface->next;
-               }
-               if (iface) {
-                       wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
-                                  "instead of interface %s for Action TX",
-                                  iface->ifname, wpa_s->ifname);
-               } else
-                       iface = wpa_s;
-       } else
-               iface = wpa_s;
-
        wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
                   MACSTR " using interface %s",
                   MAC2STR(wpa_s->pending_action_dst), iface->ifname);
@@ -562,13 +597,15 @@ static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
                wpas_send_action_tx_status(
                        wpa_s, wpa_s->pending_action_dst,
                        wpabuf_head(wpa_s->pending_action_tx),
-                       wpabuf_len(wpa_s->pending_action_tx), 0);
+                       wpabuf_len(wpa_s->pending_action_tx),
+                       P2P_SEND_ACTION_FAILED);
        }
 }
 
 
 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
-                               const u8 *data, size_t data_len, int ack)
+                               const u8 *data, size_t data_len,
+                               enum p2p_send_action_result result)
 {
        if (wpa_s->global->p2p_disabled)
                return;
@@ -592,7 +629,7 @@ void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
                           wpa_s->pending_action_dst,
                           wpa_s->pending_action_src,
                           wpa_s->pending_action_bssid,
-                          ack);
+                          result);
 
        if (wpa_s->pending_pd_before_join &&
            (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
@@ -635,6 +672,16 @@ static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
        os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
        wpa_s->pending_action_freq = freq;
 
+       if (freq) {
+               struct wpa_supplicant *tx_iface;
+               tx_iface = wpas_get_tx_interface(wpa_s, src);
+               if (tx_iface->assoc_freq == freq) {
+                       wpa_printf(MSG_DEBUG, "P2P: Already on requested "
+                                  "channel (TX interface operating channel)");
+                       freq = 0;
+               }
+       }
+
        if (wpa_s->off_channel_freq == freq || freq == 0) {
                wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
                           "send Action frame immediately");
@@ -664,6 +711,7 @@ static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
                           "Frame TX", freq);
                return -1;
        }
+       wpa_s->off_channel_freq = 0;
        wpa_s->roc_waiting_drv_freq = freq;
 
        return 0;
@@ -676,7 +724,7 @@ static void wpas_send_action_done(void *ctx)
        wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
        wpabuf_free(wpa_s->pending_action_tx);
        wpa_s->pending_action_tx = NULL;
-       if (wpa_s->off_channel_freq) {
+       if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
                wpa_drv_cancel_remain_on_channel(wpa_s);
                wpa_s->off_channel_freq = 0;
                wpa_s->roc_waiting_drv_freq = 0;
@@ -700,13 +748,14 @@ static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
                                    struct p2p_go_neg_results *res)
 {
+       wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
+                  MAC2STR(res->peer_interface_addr));
        wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
                          res->ssid, res->ssid_len);
        wpa_supplicant_ap_deinit(wpa_s);
        wpas_copy_go_neg_results(wpa_s, res);
        if (res->wps_method == WPS_PBC)
-               wpas_wps_start_pbc(wpa_s, NULL /* res->peer_interface_addr */,
-                                  1);
+               wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
        else {
                u16 dev_pw_id = DEV_PW_DEFAULT;
                if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
@@ -740,6 +789,7 @@ static void p2p_go_configured(void *ctx, void *data)
                                wpa_s->parent, ssid,
                                wpa_s->parent->own_addr);
                wpas_p2p_cross_connect_setup(wpa_s);
+               wpas_p2p_set_group_idle_timeout(wpa_s);
                return;
        }
 
@@ -819,6 +869,9 @@ static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
        C(device_type);
        C(config_methods);
 #undef C
+
+       d->p2p_group_idle = s->p2p_group_idle;
+       d->p2p_intra_bss = s->p2p_intra_bss;
 }
 
 
@@ -895,7 +948,16 @@ wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
 
        if (!wpa_s->pending_interface_name[0]) {
                wpa_printf(MSG_ERROR, "P2P: No pending group interface");
-               return NULL;
+               if (!wpas_p2p_create_iface(wpa_s))
+                       return NULL;
+               /*
+                * Something has forced us to remove the pending interface; try
+                * to create a new one and hope for the best that we will get
+                * the same local address.
+                */
+               if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
+                                                WPA_IF_P2P_CLIENT) < 0)
+                       return NULL;
        }
 
        os_memset(&iface, 0, sizeof(iface));
@@ -936,7 +998,7 @@ void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
 {
        struct wpa_supplicant *wpa_s = ctx;
 
-       if (wpa_s->off_channel_freq) {
+       if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
                wpa_drv_cancel_remain_on_channel(wpa_s);
                wpa_s->off_channel_freq = 0;
                wpa_s->roc_waiting_drv_freq = 0;
@@ -1039,6 +1101,7 @@ static int wpas_start_listen(void *ctx, unsigned int freq,
                wpa_s->pending_listen_freq = 0;
                return -1;
        }
+       wpa_s->off_channel_freq = 0;
        wpa_s->roc_waiting_drv_freq = freq;
 
        return 0;
@@ -1273,13 +1336,6 @@ static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
                return;
        }
 
-       version = query[0];
-       str = os_malloc(query_len);
-       if (str == NULL)
-               return;
-       os_memcpy(str, query + 1, query_len - 1);
-       str[query_len - 1] = '\0';
-
        if (wpabuf_tailroom(resp) < 5)
                return;
 
@@ -1288,6 +1344,13 @@ static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
        wpabuf_put_u8(resp, P2P_SERV_UPNP);
        wpabuf_put_u8(resp, srv_trans_id);
 
+       version = query[0];
+       str = os_malloc(query_len);
+       if (str == NULL)
+               return;
+       os_memcpy(str, query + 1, query_len - 1);
+       str[query_len - 1] = '\0';
+
        dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
                         struct p2p_srv_upnp, list) {
                if (version != usrv->version)
@@ -1904,151 +1967,153 @@ static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
 }
 
 
+static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
+                                    struct p2p_channels *chan)
+{
+       int i, cla = 0;
+
+       wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
+                  "band");
+
+       /* Operating class 81 - 2.4 GHz band channels 1..13 */
+       chan->reg_class[cla].reg_class = 81;
+       chan->reg_class[cla].channels = 11;
+       for (i = 0; i < 11; i++)
+               chan->reg_class[cla].channel[i] = i + 1;
+       cla++;
+
+       wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
+                  "band");
+
+       /* Operating class 115 - 5 GHz, channels 36-48 */
+       chan->reg_class[cla].reg_class = 115;
+       chan->reg_class[cla].channels = 4;
+       chan->reg_class[cla].channel[0] = 36;
+       chan->reg_class[cla].channel[1] = 40;
+       chan->reg_class[cla].channel[2] = 44;
+       chan->reg_class[cla].channel[3] = 48;
+       cla++;
+
+       wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
+                  "band");
+
+       /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
+       chan->reg_class[cla].reg_class = 124;
+       chan->reg_class[cla].channels = 4;
+       chan->reg_class[cla].channel[0] = 149;
+       chan->reg_class[cla].channel[1] = 153;
+       chan->reg_class[cla].channel[2] = 157;
+       chan->reg_class[cla].channel[3] = 161;
+       cla++;
+
+       chan->reg_classes = cla;
+       return 0;
+}
+
+
+static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
+                                         u16 num_modes,
+                                         enum hostapd_hw_mode mode)
+{
+       u16 i;
+
+       for (i = 0; i < num_modes; i++) {
+               if (modes[i].mode == mode)
+                       return &modes[i];
+       }
+
+       return NULL;
+}
+
+
+static int has_channel(struct hostapd_hw_modes *mode, u8 chan)
+{
+       int i;
+
+       for (i = 0; i < mode->num_channels; i++) {
+               if (mode->channels[i].chan == chan) {
+                       return !(mode->channels[i].flag &
+                                (HOSTAPD_CHAN_DISABLED |
+                                 HOSTAPD_CHAN_PASSIVE_SCAN |
+                                 HOSTAPD_CHAN_NO_IBSS |
+                                 HOSTAPD_CHAN_RADAR));
+               }
+       }
+
+       return 0;
+}
+
+
+struct p2p_oper_class_map {
+       enum hostapd_hw_mode mode;
+       u8 op_class;
+       u8 min_chan;
+       u8 max_chan;
+       u8 inc;
+};
+
 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
-                                  struct p2p_config *p2p)
+                                  struct p2p_channels *chan)
 {
-       struct hostapd_hw_modes *modes;
+       struct hostapd_hw_modes *modes, *mode;
        u16 num_modes, flags;
-       int i, cla;
-       int band24 = 0, band5_low = 0, band5_high = 0;
-
-       /* TODO: more detailed selection of channels within reg_class based on
-        * driver capabilities */
+       int cla, op;
+       struct p2p_oper_class_map op_class[] = {
+               { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1 },
+               { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1 },
+               { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4 },
+               { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4 },
+#if 0 /* TODO: 40 MHz channels */
+               { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1 },
+               { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1 },
+               { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8 },
+               { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8 },
+               { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8 },
+               { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8 },
+#endif
+               { -1, 0, 0, 0, 0 }
+       };
 
        modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
        if (modes == NULL) {
                wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
                           "of all supported channels; assume dualband "
                           "support");
-               band24 = band5_low = band5_high = 1;
-       } else {
-               for (i = 0; i < num_modes; i++) {
-                       struct hostapd_hw_modes *mode;
-                       mode = &modes[i];
-                       if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
-                               band24 = 1;
-                       } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
-                               int j;
-                               for (j = 0; j < mode->num_channels; j++) {
-                                       struct hostapd_channel_data *ch;
-                                       ch = &mode->channels[j];
-                                       if (ch->chan == 36)
-                                               band5_low = 1;
-                                       else if (ch->chan == 157)
-                                               band5_high = 1;
-                               }
-                       }
-               }
+               return wpas_p2p_default_channels(wpa_s, chan);
        }
 
        cla = 0;
 
-       if (band24) {
-               wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
-                          "2.4 GHz band");
-
-               /* Operating class 81 - 2.4 GHz band channels 1..13 */
-               p2p->channels.reg_class[cla].reg_class = 81;
-#if 0
-               p2p->channels.reg_class[cla].channels = 13;
-               for (i = 0; i < 13; i++)
-                       p2p->channels.reg_class[cla].channel[i] = i + 1;
-#else
-               p2p->channels.reg_class[cla].channels = 11;
-               for (i = 0; i < 11; i++)
-                       p2p->channels.reg_class[cla].channel[i] = i + 1;
-#endif
-               cla++;
-
-#if 0
-               /* Operating class 82 - 2.4 GHz band channel 14 */
-               p2p->channels.reg_class[cla].reg_class = 82;
-               p2p->channels.reg_class[cla].channels = 1;
-               p2p->channels.reg_class[cla].channel[0] = 14;
-               cla++;
-#endif
-
-#if 0
-               /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 83;
-               p2p->channels.reg_class[cla].channels = 9;
-               for (i = 0; i < 9; i++)
-                       p2p->channels.reg_class[cla].channel[i] = i + 1;
-               cla++;
-
-               /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 84;
-               p2p->channels.reg_class[cla].channels = 9;
-               for (i = 0; i < 9; i++)
-                       p2p->channels.reg_class[cla].channel[i] = i + 5;
-               cla++;
-#endif
-       }
+       for (op = 0; op_class[op].op_class; op++) {
+               struct p2p_oper_class_map *o = &op_class[op];
+               u8 ch;
+               struct p2p_reg_class *reg = NULL;
 
-       if (band5_low) {
-               wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
-                          "lower 5 GHz band");
-
-               /* Operating class 115 - 5 GHz, channels 36-48 */
-               p2p->channels.reg_class[cla].reg_class = 115;
-               p2p->channels.reg_class[cla].channels = 4;
-               p2p->channels.reg_class[cla].channel[0] = 36;
-               p2p->channels.reg_class[cla].channel[1] = 40;
-               p2p->channels.reg_class[cla].channel[2] = 44;
-               p2p->channels.reg_class[cla].channel[3] = 48;
-               cla++;
-
-#if 0
-               /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 116;
-               p2p->channels.reg_class[cla].channels = 2;
-               p2p->channels.reg_class[cla].channel[0] = 36;
-               p2p->channels.reg_class[cla].channel[1] = 44;
-               cla++;
-
-               /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 117;
-               p2p->channels.reg_class[cla].channels = 2;
-               p2p->channels.reg_class[cla].channel[0] = 40;
-               p2p->channels.reg_class[cla].channel[1] = 48;
-               cla++;
-#endif
-       }
-
-       if (band5_high) {
-               wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
-                          "higher 5 GHz band");
-
-               /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
-               p2p->channels.reg_class[cla].reg_class = 124;
-               p2p->channels.reg_class[cla].channels = 4;
-               p2p->channels.reg_class[cla].channel[0] = 149;
-               p2p->channels.reg_class[cla].channel[1] = 153;
-               p2p->channels.reg_class[cla].channel[2] = 157;
-               p2p->channels.reg_class[cla].channel[3] = 161;
-               cla++;
-
-#if 0
-               /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 126;
-               p2p->channels.reg_class[cla].channels = 2;
-               p2p->channels.reg_class[cla].channel[0] = 149;
-               p2p->channels.reg_class[cla].channel[1] = 157;
-               cla++;
-
-               /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
-               p2p->channels.reg_class[cla].reg_class = 127;
-               p2p->channels.reg_class[cla].channels = 2;
-               p2p->channels.reg_class[cla].channel[0] = 153;
-               p2p->channels.reg_class[cla].channel[1] = 161;
-               cla++;
-#endif
+               mode = get_mode(modes, num_modes, o->mode);
+               if (mode == NULL)
+                       continue;
+               for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
+                       if (!has_channel(mode, ch))
+                               continue;
+                       if (reg == NULL) {
+                               wpa_printf(MSG_DEBUG, "P2P: Add operating "
+                                          "class %u", o->op_class);
+                               reg = &chan->reg_class[cla];
+                               cla++;
+                               reg->reg_class = o->op_class;
+                       }
+                       reg->channel[reg->channels] = ch;
+                       reg->channels++;
+               }
+               if (reg) {
+                       wpa_hexdump(MSG_DEBUG, "P2P: Channels",
+                                   reg->channel, reg->channels);
+               }
        }
 
-       p2p->channels.reg_classes = cla;
+       chan->reg_classes = cla;
 
-       if (modes)
-               ieee80211_sta_free_hw_features(modes, num_modes);
+       ieee80211_sta_free_hw_features(modes, num_modes);
 
        return 0;
 }
@@ -2138,29 +2203,35 @@ int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
                os_get_random((u8 *) &r, sizeof(r));
                p2p.channel = 1 + (r % 3) * 5;
        }
+       wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
 
        if (wpa_s->conf->p2p_oper_reg_class &&
            wpa_s->conf->p2p_oper_channel) {
                p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
                p2p.op_channel = wpa_s->conf->p2p_oper_channel;
+               p2p.cfg_op_channel = 1;
+               wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
+                          "%d:%d", p2p.op_reg_class, p2p.op_channel);
+
        } else {
                p2p.op_reg_class = 81;
                /*
-                * For initial tests, pick the operation channel randomly.
-                * TODO: Use scan results (etc.) to select the best channel.
+                * Use random operation channel from (1, 6, 11) if no other
+                * preference is indicated.
                 */
-               p2p.op_channel = 1 + r % 11;
+               os_get_random((u8 *) &r, sizeof(r));
+               p2p.op_channel = 1 + (r % 3) * 5;
+               p2p.cfg_op_channel = 0;
+               wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
+                          "%d:%d", p2p.op_reg_class, p2p.op_channel);
        }
-       wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d  "
-                  "Own preferred operation channel: %d",
-                  p2p.channel, p2p.op_channel);
        if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
                os_memcpy(p2p.country, wpa_s->conf->country, 2);
                p2p.country[2] = 0x04;
        } else
                os_memcpy(p2p.country, "US\x04", 3);
 
-       if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
+       if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
                wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
                           "channel list");
                return -1;
@@ -2190,7 +2261,7 @@ int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
        p2p.concurrent_operations = !!(wpa_s->drv_flags &
                                       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
 
-       p2p.max_peers = wpa_s->max_stations ? wpa_s->max_stations : 100;
+       p2p.max_peers = 100;
 
        if (wpa_s->conf->p2p_ssid_postfix) {
                p2p.ssid_postfix_len =
@@ -2230,6 +2301,7 @@ void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
        eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
        wpa_s->p2p_long_listen = 0;
        eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
+       eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
        wpas_p2p_remove_pending_group_interface(wpa_s);
 
        /* TODO: remove group interface from the driver if this wpa_s instance
@@ -2531,14 +2603,16 @@ static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  *     initiating Group Owner negotiation
  * @go_intent: GO Intent or -1 to use default
  * @freq: Frequency for the group or 0 for auto-selection
- * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
+ * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
+ *     failure, -2 on failure due to channel not currently available,
+ *     -3 if forced channel is not supported
  */
 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
                     const char *pin, enum p2p_wps_method wps_method,
                     int persistent_group, int join, int auth, int go_intent,
                     int freq)
 {
-       int force_freq = 0;
+       int force_freq = 0, oper_freq = 0;
        u8 bssid[ETH_ALEN];
        int ret = 0;
        enum wpa_driver_if_type iftype;
@@ -2584,21 +2658,52 @@ int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
                return ret;
        }
 
-       if (freq > 0)
-               force_freq = freq;
-       else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
-                wpa_s->assoc_freq)
-               force_freq = wpa_s->assoc_freq;
+       if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
+           wpa_s->assoc_freq)
+               oper_freq = wpa_s->assoc_freq;
        else {
-               force_freq = wpa_drv_shared_freq(wpa_s);
-               if (force_freq < 0)
-                       force_freq = 0;
+               oper_freq = wpa_drv_shared_freq(wpa_s);
+               if (oper_freq < 0)
+                       oper_freq = 0;
        }
 
-       if (force_freq > 0) {
+       if (freq > 0) {
+               if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
+                       wpa_printf(MSG_DEBUG, "P2P: The forced channel "
+                                  "(%u MHz) is not supported for P2P uses",
+                                  freq);
+                       return -3;
+               }
+
+               if (oper_freq > 0 && freq != oper_freq &&
+                   !(wpa_s->drv_flags &
+                     WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
+                       wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
+                                  "on %u MHz while connected on another "
+                                  "channel (%u MHz)", freq, oper_freq);
+                       return -2;
+               }
+               wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
+                          "requested channel (%u MHz)", freq);
+               force_freq = freq;
+       } else if (oper_freq > 0 &&
+                  !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
+               if (!(wpa_s->drv_flags &
+                     WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
+                       wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
+                                  "while connected on non-P2P supported "
+                                  "channel (%u MHz)", oper_freq);
+                       return -2;
+               }
+               wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
+                          "(%u MHz) not available for P2P - try to use "
+                          "another channel", oper_freq);
+               force_freq = 0;
+       } else if (oper_freq > 0) {
                wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
                           "channel we are already using (%u MHz) on another "
-                          "interface", force_freq);
+                          "interface", oper_freq);
+               force_freq = oper_freq;
        }
 
        wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
@@ -2735,6 +2840,7 @@ int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
                while (wpa_s) {
                        prev = wpa_s;
                        wpa_s = wpa_s->next;
+                       prev->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
                        wpas_p2p_group_delete(prev);
                }
                return 0;
@@ -2748,6 +2854,7 @@ int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
        if (wpa_s == NULL)
                return -1;
 
+       wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
        wpas_p2p_group_delete(wpa_s);
 
        return 0;
@@ -2763,16 +2870,48 @@ static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
 
        os_memset(params, 0, sizeof(*params));
        params->role_go = 1;
-       params->freq = 2412;
-       if (freq)
+       if (freq) {
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
+                          "frequency %d MHz", freq);
                params->freq = freq;
-       else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
-                wpa_s->conf->p2p_oper_channel >= 1 &&
-                wpa_s->conf->p2p_oper_channel <= 11)
+       else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
+                  wpa_s->conf->p2p_oper_channel >= 1 &&
+                  wpa_s->conf->p2p_oper_channel <= 11) {
                params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
-       else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
-                wpa_s->conf->p2p_oper_reg_class == 118)
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
+                          "frequency %d MHz", params->freq);
+       } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
+                  wpa_s->conf->p2p_oper_reg_class == 118) {
                params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
+                          "frequency %d MHz", params->freq);
+       } else if (wpa_s->conf->p2p_oper_channel == 0 &&
+                  wpa_s->best_overall_freq > 0 &&
+                  p2p_supported_freq(wpa_s->global->p2p,
+                                     wpa_s->best_overall_freq)) {
+               params->freq = wpa_s->best_overall_freq;
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
+                          "channel %d MHz", params->freq);
+       } else if (wpa_s->conf->p2p_oper_channel == 0 &&
+                  wpa_s->best_24_freq > 0 &&
+                  p2p_supported_freq(wpa_s->global->p2p,
+                                     wpa_s->best_24_freq)) {
+               params->freq = wpa_s->best_24_freq;
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
+                          "channel %d MHz", params->freq);
+       } else if (wpa_s->conf->p2p_oper_channel == 0 &&
+                  wpa_s->best_5_freq > 0 &&
+                  p2p_supported_freq(wpa_s->global->p2p,
+                                     wpa_s->best_5_freq)) {
+               params->freq = wpa_s->best_5_freq;
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
+                          "channel %d MHz", params->freq);
+       } else {
+               params->freq = 2412;
+               wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
+                          "known)", params->freq);
+       }
+
        if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
            wpa_s->assoc_freq && !freq) {
                wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
@@ -2825,6 +2964,53 @@ int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
                       int freq)
 {
        struct p2p_go_neg_results params;
+       unsigned int r;
+
+       if (freq == 2) {
+               wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
+                          "band");
+               if (wpa_s->best_24_freq > 0 &&
+                   p2p_supported_freq(wpa_s->global->p2p,
+                                      wpa_s->best_24_freq)) {
+                       freq = wpa_s->best_24_freq;
+                       wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
+                                  "channel: %d MHz", freq);
+               } else {
+                       os_get_random((u8 *) &r, sizeof(r));
+                       freq = 2412 + (r % 3) * 25;
+                       wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
+                                  "channel: %d MHz", freq);
+               }
+       }
+
+       if (freq == 5) {
+               wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
+                          "band");
+               if (wpa_s->best_5_freq > 0 &&
+                   p2p_supported_freq(wpa_s->global->p2p,
+                                      wpa_s->best_5_freq)) {
+                       freq = wpa_s->best_5_freq;
+                       wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
+                                  "channel: %d MHz", freq);
+               } else {
+                       os_get_random((u8 *) &r, sizeof(r));
+                       freq = 5180 + (r % 4) * 20;
+                       if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
+                               wpa_printf(MSG_DEBUG, "P2P: Could not select "
+                                          "5 GHz channel for P2P group");
+                               return -1;
+                       }
+                       wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
+                                  "channel: %d MHz", freq);
+               }
+       }
+
+       if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
+               wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
+                          "(%u MHz) is not supported for P2P uses",
+                          freq);
+               return -1;
+       }
 
        wpas_p2p_init_go_params(wpa_s, &params, freq);
        p2p_go_params(wpa_s->global->p2p, &params);
@@ -2947,6 +3133,19 @@ static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
 }
 
 
+static void wpas_p2p_idle_update(void *ctx, int idle)
+{
+       struct wpa_supplicant *wpa_s = ctx;
+       if (!wpa_s->ap_iface)
+               return;
+       wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
+       if (idle)
+               wpas_p2p_set_group_idle_timeout(wpa_s);
+       else
+               eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
+}
+
+
 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
                                       int persistent_group,
                                       int group_formation)
@@ -2960,8 +3159,14 @@ struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
 
        cfg->persistent_group = persistent_group;
        os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
+       if (wpa_s->max_stations &&
+           wpa_s->max_stations < wpa_s->conf->max_num_sta)
+               cfg->max_clients = wpa_s->max_stations;
+       else
+               cfg->max_clients = wpa_s->conf->max_num_sta;
        cfg->cb_ctx = wpa_s;
        cfg->ie_update = wpas_p2p_ie_update;
+       cfg->idle_update = wpas_p2p_idle_update;
 
        group = p2p_group_init(wpa_s->global->p2p, cfg);
        if (group == NULL)
@@ -3270,6 +3475,9 @@ void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
                                               ssid->ssid_len);
        os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
 
+       if (wpa_s->global->p2p_group_formation == wpa_s)
+               wpa_s->global->p2p_group_formation = NULL;
+
        if (ssid->passphrase == NULL && ssid->psk_set) {
                char psk[65];
                wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
@@ -3316,6 +3524,39 @@ int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
 }
 
 
+static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
+{
+       struct wpa_supplicant *wpa_s = eloop_ctx;
+
+       if (wpa_s->conf->p2p_group_idle == 0) {
+               wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
+                          "disabled");
+               return;
+       }
+
+       wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
+                  "group");
+       wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
+       wpas_p2p_group_delete(wpa_s);
+}
+
+
+static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
+{
+       eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
+       if (wpa_s->conf->p2p_group_idle == 0)
+               return;
+
+       if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
+               return;
+
+       wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
+                  wpa_s->conf->p2p_group_idle);
+       eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
+                              wpas_p2p_group_idle_timeout, wpa_s, NULL);
+}
+
+
 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
                           u16 reason_code, const u8 *ie, size_t ie_len)
 {
@@ -3498,12 +3739,16 @@ void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
                wpas_p2p_disable_cross_connect(wpa_s);
        else
                wpas_p2p_enable_cross_connect(wpa_s);
+       if (!wpa_s->ap_iface)
+               eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
 }
 
 
 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
 {
        wpas_p2p_disable_cross_connect(wpa_s);
+       if (!wpa_s->ap_iface)
+               wpas_p2p_set_group_idle_timeout(wpa_s);
 }
 
 
@@ -3556,3 +3801,79 @@ int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
        wpas_group_formation_completed(wpa_s, 0);
        return 1;
 }
+
+
+void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
+{
+       struct p2p_channels chan;
+
+       if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
+               return;
+
+       os_memset(&chan, 0, sizeof(chan));
+       if (wpas_p2p_setup_channels(wpa_s, &chan)) {
+               wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
+                          "channel list");
+               return;
+       }
+
+       p2p_update_channel_list(wpa_s->global->p2p, &chan);
+}
+
+
+int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
+{
+       struct wpa_global *global = wpa_s->global;
+       int found = 0;
+
+       wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
+
+       if (wpa_s->pending_interface_name[0] &&
+           !is_zero_ether_addr(wpa_s->pending_interface_addr))
+               found = 1;
+
+       wpas_p2p_stop_find(wpa_s);
+
+       for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
+               if (wpa_s == global->p2p_group_formation &&
+                   (wpa_s->p2p_in_provisioning ||
+                    wpa_s->parent->pending_interface_type ==
+                    WPA_IF_P2P_CLIENT)) {
+                       wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
+                                  "formation found - cancelling",
+                                  wpa_s->ifname);
+                       found = 1;
+                       wpas_p2p_group_delete(wpa_s);
+                       break;
+               }
+       }
+
+       if (!found) {
+               wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
+               return -1;
+       }
+
+       return 0;
+}
+
+
+void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
+{
+       if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
+               return;
+
+       wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
+                  "being available anymore");
+       wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
+       wpas_p2p_group_delete(wpa_s);
+}
+
+
+void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
+                                  int freq_24, int freq_5, int freq_overall)
+{
+       struct p2p_data *p2p = wpa_s->global->p2p;
+       if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
+               return;
+       p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
+}