P2P: Allow discoverable interval for p2p_find to be configured
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2  * wpa_supplicant - P2P
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
17 #include "p2p/p2p.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/p2p_hostapd.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "ap.h"
26 #include "config_ssid.h"
27 #include "config.h"
28 #include "notify.h"
29 #include "scan.h"
30 #include "bss.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
34
35
36 /*
37  * How many times to try to scan to find the GO before giving up on join
38  * request.
39  */
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
42 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
44 #ifndef P2P_MAX_CLIENT_IDLE
45 /*
46  * How many seconds to try to reconnect to the GO when connection in P2P client
47  * role has been lost.
48  */
49 #define P2P_MAX_CLIENT_IDLE 10
50 #endif /* P2P_MAX_CLIENT_IDLE */
51
52 #ifndef P2P_MAX_INITIAL_CONN_WAIT
53 /*
54  * How many seconds to wait for initial 4-way handshake to get completed after
55  * WPS provisioning step.
56  */
57 #define P2P_MAX_INITIAL_CONN_WAIT 10
58 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
59
60 #ifndef P2P_CONCURRENT_SEARCH_DELAY
61 #define P2P_CONCURRENT_SEARCH_DELAY 500
62 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
63
64 enum p2p_group_removal_reason {
65         P2P_GROUP_REMOVAL_UNKNOWN,
66         P2P_GROUP_REMOVAL_SILENT,
67         P2P_GROUP_REMOVAL_FORMATION_FAILED,
68         P2P_GROUP_REMOVAL_REQUESTED,
69         P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
70         P2P_GROUP_REMOVAL_UNAVAILABLE,
71         P2P_GROUP_REMOVAL_GO_ENDING_SESSION
72 };
73
74
75 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
76 static struct wpa_supplicant *
77 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
78                          int go);
79 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
80 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
81 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
82 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
83                          const u8 *dev_addr, enum p2p_wps_method wps_method,
84                          int auto_join);
85 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
86                                             void *timeout_ctx);
87 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
88 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
89 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
90 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
91 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
92                                         int group_added);
93
94
95 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
96                                       struct wpa_scan_results *scan_res)
97 {
98         size_t i;
99
100         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
101                 return;
102
103         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
104                    (int) scan_res->num);
105
106         for (i = 0; i < scan_res->num; i++) {
107                 struct wpa_scan_res *bss = scan_res->res[i];
108                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
109                                          bss->freq, bss->level,
110                                          (const u8 *) (bss + 1),
111                                          bss->ie_len) > 0)
112                         break;
113         }
114
115         p2p_scan_res_handled(wpa_s->global->p2p);
116 }
117
118
119 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
120                          unsigned int num_req_dev_types,
121                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
122 {
123         struct wpa_supplicant *wpa_s = ctx;
124         struct wpa_supplicant *ifs;
125         struct wpa_driver_scan_params params;
126         int ret;
127         struct wpabuf *wps_ie, *ies;
128         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
129         size_t ielen;
130
131         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
132                 return -1;
133
134         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
135                 if (ifs->sta_scan_pending &&
136                     wpas_p2p_in_progress(wpa_s) == 2) {
137                         wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
138                                    "pending station mode scan to be "
139                                    "completed on interface %s", ifs->ifname);
140                         wpa_s->global->p2p_cb_on_scan_complete = 1;
141                         wpa_supplicant_req_scan(ifs, 0, 0);
142                         return 1;
143                 }
144         }
145
146         os_memset(&params, 0, sizeof(params));
147
148         /* P2P Wildcard SSID */
149         params.num_ssids = 1;
150         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
151         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
152
153         wpa_s->wps->dev.p2p = 1;
154         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
155                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
156                                         num_req_dev_types, req_dev_types);
157         if (wps_ie == NULL)
158                 return -1;
159
160         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
161         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
162         if (ies == NULL) {
163                 wpabuf_free(wps_ie);
164                 return -1;
165         }
166         wpabuf_put_buf(ies, wps_ie);
167         wpabuf_free(wps_ie);
168
169         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
170
171         params.p2p_probe = 1;
172         params.extra_ies = wpabuf_head(ies);
173         params.extra_ies_len = wpabuf_len(ies);
174
175         switch (type) {
176         case P2P_SCAN_SOCIAL:
177                 params.freqs = social_channels;
178                 break;
179         case P2P_SCAN_FULL:
180                 break;
181         case P2P_SCAN_SOCIAL_PLUS_ONE:
182                 social_channels[3] = freq;
183                 params.freqs = social_channels;
184                 break;
185         }
186
187         ret = wpa_drv_scan(wpa_s, &params);
188
189         wpabuf_free(ies);
190
191         if (ret) {
192                 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
193                         if (ifs->scanning ||
194                             ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
195                                 wpa_s->global->p2p_cb_on_scan_complete = 1;
196                                 ret = 1;
197                                 break;
198                         }
199                 }
200         } else
201                 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
202
203         return ret;
204 }
205
206
207 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
208 {
209         switch (p2p_group_interface) {
210         case P2P_GROUP_INTERFACE_PENDING:
211                 return WPA_IF_P2P_GROUP;
212         case P2P_GROUP_INTERFACE_GO:
213                 return WPA_IF_P2P_GO;
214         case P2P_GROUP_INTERFACE_CLIENT:
215                 return WPA_IF_P2P_CLIENT;
216         }
217
218         return WPA_IF_P2P_GROUP;
219 }
220
221
222 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
223                                                   const u8 *ssid,
224                                                   size_t ssid_len, int *go)
225 {
226         struct wpa_ssid *s;
227
228         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
229                 for (s = wpa_s->conf->ssid; s; s = s->next) {
230                         if (s->disabled != 0 || !s->p2p_group ||
231                             s->ssid_len != ssid_len ||
232                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
233                                 continue;
234                         if (s->mode == WPAS_MODE_P2P_GO &&
235                             s != wpa_s->current_ssid)
236                                 continue;
237                         if (go)
238                                 *go = s->mode == WPAS_MODE_P2P_GO;
239                         return wpa_s;
240                 }
241         }
242
243         return NULL;
244 }
245
246
247 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
248                                  enum p2p_group_removal_reason removal_reason)
249 {
250         struct wpa_ssid *ssid;
251         char *gtype;
252         const char *reason;
253
254         ssid = wpa_s->current_ssid;
255         if (ssid == NULL) {
256                 /*
257                  * The current SSID was not known, but there may still be a
258                  * pending P2P group interface waiting for provisioning or a
259                  * P2P group that is trying to reconnect.
260                  */
261                 ssid = wpa_s->conf->ssid;
262                 while (ssid) {
263                         if (ssid->p2p_group && ssid->disabled != 2)
264                                 break;
265                         ssid = ssid->next;
266                 }
267                 if (ssid == NULL &&
268                         wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
269                 {
270                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
271                                    "not found");
272                         return -1;
273                 }
274         }
275         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
276                 gtype = "GO";
277         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
278                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
279                 wpa_s->reassociate = 0;
280                 wpa_s->disconnected = 1;
281                 wpa_supplicant_deauthenticate(wpa_s,
282                                               WLAN_REASON_DEAUTH_LEAVING);
283                 gtype = "client";
284         } else
285                 gtype = "GO";
286         if (wpa_s->cross_connect_in_use) {
287                 wpa_s->cross_connect_in_use = 0;
288                 wpa_msg(wpa_s->parent, MSG_INFO,
289                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
290                         wpa_s->ifname, wpa_s->cross_connect_uplink);
291         }
292         switch (removal_reason) {
293         case P2P_GROUP_REMOVAL_REQUESTED:
294                 reason = " reason=REQUESTED";
295                 break;
296         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
297                 reason = " reason=FORMATION_FAILED";
298                 break;
299         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
300                 reason = " reason=IDLE";
301                 break;
302         case P2P_GROUP_REMOVAL_UNAVAILABLE:
303                 reason = " reason=UNAVAILABLE";
304                 break;
305         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
306                 reason = " reason=GO_ENDING_SESSION";
307                 break;
308         default:
309                 reason = "";
310                 break;
311         }
312         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
313                 wpa_msg(wpa_s->parent, MSG_INFO,
314                         P2P_EVENT_GROUP_REMOVED "%s %s%s",
315                         wpa_s->ifname, gtype, reason);
316         }
317
318         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
319                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
320
321         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
322                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
323
324         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
325                 struct wpa_global *global;
326                 char *ifname;
327                 enum wpa_driver_if_type type;
328                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
329                         wpa_s->ifname);
330                 global = wpa_s->global;
331                 ifname = os_strdup(wpa_s->ifname);
332                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
333                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
334                 wpa_s = global->ifaces;
335                 if (wpa_s && ifname)
336                         wpa_drv_if_remove(wpa_s, type, ifname);
337                 os_free(ifname);
338                 return 1;
339         }
340
341         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
342         if (ssid && (ssid->p2p_group ||
343                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
344                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
345                 int id = ssid->id;
346                 if (ssid == wpa_s->current_ssid) {
347                         wpa_sm_set_config(wpa_s->wpa, NULL);
348                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
349                         wpa_s->current_ssid = NULL;
350                 }
351                 /*
352                  * Networks objects created during any P2P activities are not
353                  * exposed out as they might/will confuse certain non-P2P aware
354                  * applications since these network objects won't behave like
355                  * regular ones.
356                  *
357                  * Likewise, we don't send out network removed signals for such
358                  * network objects.
359                  */
360                 wpa_config_remove_network(wpa_s->conf, id);
361                 wpa_supplicant_clear_status(wpa_s);
362                 wpa_supplicant_cancel_sched_scan(wpa_s);
363                 wpa_s->sta_scan_pending = 0;
364         } else {
365                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
366                            "found");
367         }
368         if (wpa_s->ap_iface)
369                 wpa_supplicant_ap_deinit(wpa_s);
370         else
371                 wpa_drv_deinit_p2p_cli(wpa_s);
372
373         return 0;
374 }
375
376
377 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
378                                      u8 *go_dev_addr,
379                                      const u8 *ssid, size_t ssid_len)
380 {
381         struct wpa_bss *bss;
382         const u8 *bssid;
383         struct wpabuf *p2p;
384         u8 group_capab;
385         const u8 *addr;
386
387         if (wpa_s->go_params)
388                 bssid = wpa_s->go_params->peer_interface_addr;
389         else
390                 bssid = wpa_s->bssid;
391
392         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
393         if (bss == NULL) {
394                 u8 iface_addr[ETH_ALEN];
395                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
396                                            iface_addr) == 0)
397                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
398         }
399         if (bss == NULL) {
400                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
401                            "group is persistent - BSS " MACSTR " not found",
402                            MAC2STR(bssid));
403                 return 0;
404         }
405
406         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
407         if (p2p == NULL) {
408                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
409                            "group is persistent - BSS " MACSTR
410                            " did not include P2P IE", MAC2STR(bssid));
411                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
412                             (u8 *) (bss + 1), bss->ie_len);
413                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
414                             ((u8 *) bss + 1) + bss->ie_len,
415                             bss->beacon_ie_len);
416                 return 0;
417         }
418
419         group_capab = p2p_get_group_capab(p2p);
420         addr = p2p_get_go_dev_addr(p2p);
421         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
422                    "group_capab=0x%x", group_capab);
423         if (addr) {
424                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
425                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
426                            MAC2STR(addr));
427         } else
428                 os_memset(go_dev_addr, 0, ETH_ALEN);
429         wpabuf_free(p2p);
430
431         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
432                    "go_dev_addr=" MACSTR,
433                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
434
435         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
436 }
437
438
439 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
440                                            struct wpa_ssid *ssid,
441                                            const u8 *go_dev_addr)
442 {
443         struct wpa_ssid *s;
444         int changed = 0;
445
446         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
447                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
448         for (s = wpa_s->conf->ssid; s; s = s->next) {
449                 if (s->disabled == 2 &&
450                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
451                     s->ssid_len == ssid->ssid_len &&
452                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
453                         break;
454         }
455
456         if (s) {
457                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
458                            "entry");
459                 if (ssid->passphrase && !s->passphrase)
460                         changed = 1;
461                 else if (ssid->passphrase && s->passphrase &&
462                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
463                         changed = 1;
464         } else {
465                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
466                            "entry");
467                 changed = 1;
468                 s = wpa_config_add_network(wpa_s->conf);
469                 if (s == NULL)
470                         return -1;
471
472                 /*
473                  * Instead of network_added we emit persistent_group_added
474                  * notification. Also to keep the defense checks in
475                  * persistent_group obj registration method, we set the
476                  * relevant flags in s to designate it as a persistent group.
477                  */
478                 s->p2p_group = 1;
479                 s->p2p_persistent_group = 1;
480                 wpas_notify_persistent_group_added(wpa_s, s);
481                 wpa_config_set_network_defaults(s);
482         }
483
484         s->p2p_group = 1;
485         s->p2p_persistent_group = 1;
486         s->disabled = 2;
487         s->bssid_set = 1;
488         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
489         s->mode = ssid->mode;
490         s->auth_alg = WPA_AUTH_ALG_OPEN;
491         s->key_mgmt = WPA_KEY_MGMT_PSK;
492         s->proto = WPA_PROTO_RSN;
493         s->pairwise_cipher = WPA_CIPHER_CCMP;
494         s->export_keys = 1;
495         if (ssid->passphrase) {
496                 os_free(s->passphrase);
497                 s->passphrase = os_strdup(ssid->passphrase);
498         }
499         if (ssid->psk_set) {
500                 s->psk_set = 1;
501                 os_memcpy(s->psk, ssid->psk, 32);
502         }
503         if (s->passphrase && !s->psk_set)
504                 wpa_config_update_psk(s);
505         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
506                 os_free(s->ssid);
507                 s->ssid = os_malloc(ssid->ssid_len);
508         }
509         if (s->ssid) {
510                 s->ssid_len = ssid->ssid_len;
511                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
512         }
513
514 #ifndef CONFIG_NO_CONFIG_WRITE
515         if (changed && wpa_s->conf->update_config &&
516             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
517                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
518         }
519 #endif /* CONFIG_NO_CONFIG_WRITE */
520
521         return s->id;
522 }
523
524
525 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
526                                                  const u8 *addr)
527 {
528         struct wpa_ssid *ssid, *s;
529         u8 *n;
530         size_t i;
531         int found = 0;
532
533         ssid = wpa_s->current_ssid;
534         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
535             !ssid->p2p_persistent_group)
536                 return;
537
538         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
539                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
540                         continue;
541
542                 if (s->ssid_len == ssid->ssid_len &&
543                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
544                         break;
545         }
546
547         if (s == NULL)
548                 return;
549
550         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
551                 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
552                               ETH_ALEN) != 0)
553                         continue;
554
555                 if (i == s->num_p2p_clients - 1)
556                         return; /* already the most recent entry */
557
558                 /* move the entry to mark it most recent */
559                 os_memmove(s->p2p_client_list + i * ETH_ALEN,
560                            s->p2p_client_list + (i + 1) * ETH_ALEN,
561                            (s->num_p2p_clients - i - 1) * ETH_ALEN);
562                 os_memcpy(s->p2p_client_list +
563                           (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
564                 found = 1;
565                 break;
566         }
567
568         if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
569                 n = os_realloc_array(s->p2p_client_list,
570                                      s->num_p2p_clients + 1, ETH_ALEN);
571                 if (n == NULL)
572                         return;
573                 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
574                 s->p2p_client_list = n;
575                 s->num_p2p_clients++;
576         } else if (!found) {
577                 /* Not enough room for an additional entry - drop the oldest
578                  * entry */
579                 os_memmove(s->p2p_client_list,
580                            s->p2p_client_list + ETH_ALEN,
581                            (s->num_p2p_clients - 1) * ETH_ALEN);
582                 os_memcpy(s->p2p_client_list +
583                           (s->num_p2p_clients - 1) * ETH_ALEN,
584                           addr, ETH_ALEN);
585         }
586
587 #ifndef CONFIG_NO_CONFIG_WRITE
588         if (wpa_s->parent->conf->update_config &&
589             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
590                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
591 #endif /* CONFIG_NO_CONFIG_WRITE */
592 }
593
594
595 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
596                                            int success)
597 {
598         struct wpa_ssid *ssid;
599         const char *ssid_txt;
600         int client;
601         int persistent;
602         u8 go_dev_addr[ETH_ALEN];
603         int network_id = -1;
604
605         /*
606          * This callback is likely called for the main interface. Update wpa_s
607          * to use the group interface if a new interface was created for the
608          * group.
609          */
610         if (wpa_s->global->p2p_group_formation)
611                 wpa_s = wpa_s->global->p2p_group_formation;
612         wpa_s->global->p2p_group_formation = NULL;
613         wpa_s->p2p_in_provisioning = 0;
614
615         if (!success) {
616                 wpa_msg(wpa_s->parent, MSG_INFO,
617                         P2P_EVENT_GROUP_FORMATION_FAILURE);
618                 wpas_p2p_group_delete(wpa_s,
619                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
620                 return;
621         }
622
623         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
624
625         ssid = wpa_s->current_ssid;
626         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
627                 ssid->mode = WPAS_MODE_P2P_GO;
628                 p2p_group_notif_formation_done(wpa_s->p2p_group);
629                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
630         }
631
632         persistent = 0;
633         if (ssid) {
634                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
635                 client = ssid->mode == WPAS_MODE_INFRA;
636                 if (ssid->mode == WPAS_MODE_P2P_GO) {
637                         persistent = ssid->p2p_persistent_group;
638                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
639                                   ETH_ALEN);
640                 } else
641                         persistent = wpas_p2p_persistent_group(wpa_s,
642                                                                go_dev_addr,
643                                                                ssid->ssid,
644                                                                ssid->ssid_len);
645         } else {
646                 ssid_txt = "";
647                 client = wpa_s->p2p_group_interface ==
648                         P2P_GROUP_INTERFACE_CLIENT;
649                 os_memset(go_dev_addr, 0, ETH_ALEN);
650         }
651
652         wpa_s->show_group_started = 0;
653         if (client) {
654                 /*
655                  * Indicate event only after successfully completed 4-way
656                  * handshake, i.e., when the interface is ready for data
657                  * packets.
658                  */
659                 wpa_s->show_group_started = 1;
660         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
661                 char psk[65];
662                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
663                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
664                         "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
665                         "%s",
666                         wpa_s->ifname, ssid_txt, ssid->frequency, psk,
667                         MAC2STR(go_dev_addr),
668                         persistent ? " [PERSISTENT]" : "");
669                 wpas_p2p_cross_connect_setup(wpa_s);
670                 wpas_p2p_set_group_idle_timeout(wpa_s);
671         } else {
672                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
673                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
674                         "go_dev_addr=" MACSTR "%s",
675                         wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
676                         ssid && ssid->passphrase ? ssid->passphrase : "",
677                         MAC2STR(go_dev_addr),
678                         persistent ? " [PERSISTENT]" : "");
679                 wpas_p2p_cross_connect_setup(wpa_s);
680                 wpas_p2p_set_group_idle_timeout(wpa_s);
681         }
682
683         if (persistent)
684                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
685                                                              ssid, go_dev_addr);
686         if (network_id < 0 && ssid)
687                 network_id = ssid->id;
688         if (!client)
689                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
690 }
691
692
693 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
694                                            unsigned int freq,
695                                            const u8 *dst, const u8 *src,
696                                            const u8 *bssid,
697                                            const u8 *data, size_t data_len,
698                                            enum offchannel_send_action_result
699                                            result)
700 {
701         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
702
703         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
704                 return;
705         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
706                 return;
707
708         switch (result) {
709         case OFFCHANNEL_SEND_ACTION_SUCCESS:
710                 res = P2P_SEND_ACTION_SUCCESS;
711                 break;
712         case OFFCHANNEL_SEND_ACTION_NO_ACK:
713                 res = P2P_SEND_ACTION_NO_ACK;
714                 break;
715         case OFFCHANNEL_SEND_ACTION_FAILED:
716                 res = P2P_SEND_ACTION_FAILED;
717                 break;
718         }
719
720         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
721
722         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
723             wpa_s->pending_pd_before_join &&
724             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
725              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
726                 wpa_s->pending_pd_before_join = 0;
727                 if (wpa_s->p2p_fallback_to_go_neg) {
728                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
729                                 "during p2p_connect-auto");
730                         wpas_p2p_fallback_to_go_neg(wpa_s, 0);
731                         return;
732                 }
733
734                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
735                            "join-existing-group operation (no ACK for PD "
736                            "Req)");
737                 wpas_p2p_join_start(wpa_s);
738         }
739 }
740
741
742 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
743                             const u8 *src, const u8 *bssid, const u8 *buf,
744                             size_t len, unsigned int wait_time)
745 {
746         struct wpa_supplicant *wpa_s = ctx;
747         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
748                                       wait_time,
749                                       wpas_p2p_send_action_tx_status, 1);
750 }
751
752
753 static void wpas_send_action_done(void *ctx)
754 {
755         struct wpa_supplicant *wpa_s = ctx;
756         offchannel_send_action_done(wpa_s);
757 }
758
759
760 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
761                                     struct p2p_go_neg_results *params)
762 {
763         if (wpa_s->go_params == NULL) {
764                 wpa_s->go_params = os_malloc(sizeof(*params));
765                 if (wpa_s->go_params == NULL)
766                         return -1;
767         }
768         os_memcpy(wpa_s->go_params, params, sizeof(*params));
769         return 0;
770 }
771
772
773 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
774                                     struct p2p_go_neg_results *res)
775 {
776         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
777                    MAC2STR(res->peer_interface_addr));
778         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
779                           res->ssid, res->ssid_len);
780         wpa_supplicant_ap_deinit(wpa_s);
781         wpas_copy_go_neg_results(wpa_s, res);
782         if (res->wps_method == WPS_PBC)
783                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
784         else {
785                 u16 dev_pw_id = DEV_PW_DEFAULT;
786                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
787                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
788                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
789                                    wpa_s->p2p_pin, 1, dev_pw_id);
790         }
791 }
792
793
794 static void p2p_go_configured(void *ctx, void *data)
795 {
796         struct wpa_supplicant *wpa_s = ctx;
797         struct p2p_go_neg_results *params = data;
798         struct wpa_ssid *ssid;
799         int network_id = -1;
800
801         ssid = wpa_s->current_ssid;
802         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
803                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
804                 if (wpa_s->global->p2p_group_formation == wpa_s)
805                         wpa_s->global->p2p_group_formation = NULL;
806                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
807                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
808                         "go_dev_addr=" MACSTR "%s",
809                         wpa_s->ifname,
810                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
811                         ssid->frequency,
812                         params->passphrase ? params->passphrase : "",
813                         MAC2STR(wpa_s->global->p2p_dev_addr),
814                         params->persistent_group ? " [PERSISTENT]" : "");
815
816                 if (params->persistent_group)
817                         network_id = wpas_p2p_store_persistent_group(
818                                 wpa_s->parent, ssid,
819                                 wpa_s->global->p2p_dev_addr);
820                 if (network_id < 0)
821                         network_id = ssid->id;
822                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
823                 wpas_p2p_cross_connect_setup(wpa_s);
824                 wpas_p2p_set_group_idle_timeout(wpa_s);
825                 return;
826         }
827
828         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
829         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
830                                               params->peer_interface_addr)) {
831                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
832                            "filtering");
833                 return;
834         }
835         if (params->wps_method == WPS_PBC)
836                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
837                                           params->peer_device_addr);
838         else if (wpa_s->p2p_pin[0])
839                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
840                                           wpa_s->p2p_pin, NULL, 0, 0);
841         os_free(wpa_s->go_params);
842         wpa_s->go_params = NULL;
843 }
844
845
846 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
847                               struct p2p_go_neg_results *params,
848                               int group_formation)
849 {
850         struct wpa_ssid *ssid;
851
852         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
853         if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
854                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
855                         "results");
856                 return;
857         }
858
859         ssid = wpa_config_add_network(wpa_s->conf);
860         if (ssid == NULL) {
861                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
862                 return;
863         }
864
865         wpa_s->show_group_started = 0;
866
867         wpa_config_set_network_defaults(ssid);
868         ssid->temporary = 1;
869         ssid->p2p_group = 1;
870         ssid->p2p_persistent_group = params->persistent_group;
871         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
872                 WPAS_MODE_P2P_GO;
873         ssid->frequency = params->freq;
874         ssid->ht40 = params->ht40;
875         ssid->ssid = os_zalloc(params->ssid_len + 1);
876         if (ssid->ssid) {
877                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
878                 ssid->ssid_len = params->ssid_len;
879         }
880         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
881         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
882         ssid->proto = WPA_PROTO_RSN;
883         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
884         ssid->passphrase = os_strdup(params->passphrase);
885         if (ssid->passphrase == NULL) {
886                 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy passphrase for "
887                         "GO");
888                 wpa_config_remove_network(wpa_s->conf, ssid->id);
889                 return;
890         }
891         wpa_config_update_psk(ssid);
892         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
893
894         wpa_s->ap_configured_cb = p2p_go_configured;
895         wpa_s->ap_configured_cb_ctx = wpa_s;
896         wpa_s->ap_configured_cb_data = wpa_s->go_params;
897         wpa_s->connect_without_scan = ssid;
898         wpa_s->reassociate = 1;
899         wpa_s->disconnected = 0;
900         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
901                 "start GO)");
902         wpa_supplicant_req_scan(wpa_s, 0, 0);
903 }
904
905
906 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
907                                   const struct wpa_supplicant *src)
908 {
909         struct wpa_config *d;
910         const struct wpa_config *s;
911
912         d = dst->conf;
913         s = src->conf;
914
915 #define C(n) if (s->n) d->n = os_strdup(s->n)
916         C(device_name);
917         C(manufacturer);
918         C(model_name);
919         C(model_number);
920         C(serial_number);
921         C(config_methods);
922 #undef C
923
924         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
925         os_memcpy(d->sec_device_type, s->sec_device_type,
926                   sizeof(d->sec_device_type));
927         d->num_sec_device_types = s->num_sec_device_types;
928
929         d->p2p_group_idle = s->p2p_group_idle;
930         d->p2p_intra_bss = s->p2p_intra_bss;
931         d->persistent_reconnect = s->persistent_reconnect;
932         d->max_num_sta = s->max_num_sta;
933         d->pbc_in_m1 = s->pbc_in_m1;
934 }
935
936
937 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
938                                         enum wpa_driver_if_type type)
939 {
940         char ifname[120], force_ifname[120];
941
942         if (wpa_s->pending_interface_name[0]) {
943                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
944                            "- skip creation of a new one");
945                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
946                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
947                                    "unknown?! ifname='%s'",
948                                    wpa_s->pending_interface_name);
949                         return -1;
950                 }
951                 return 0;
952         }
953
954         os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
955                     wpa_s->p2p_group_idx);
956         if (os_strlen(ifname) >= IFNAMSIZ &&
957             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
958                 /* Try to avoid going over the IFNAMSIZ length limit */
959                 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
960                             wpa_s->p2p_group_idx);
961         }
962         force_ifname[0] = '\0';
963
964         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
965                    ifname);
966         wpa_s->p2p_group_idx++;
967
968         wpa_s->pending_interface_type = type;
969         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
970                            wpa_s->pending_interface_addr, NULL) < 0) {
971                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
972                            "interface");
973                 return -1;
974         }
975
976         if (force_ifname[0]) {
977                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
978                            force_ifname);
979                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
980                            sizeof(wpa_s->pending_interface_name));
981         } else
982                 os_strlcpy(wpa_s->pending_interface_name, ifname,
983                            sizeof(wpa_s->pending_interface_name));
984         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
985                    MACSTR, wpa_s->pending_interface_name,
986                    MAC2STR(wpa_s->pending_interface_addr));
987
988         return 0;
989 }
990
991
992 static void wpas_p2p_remove_pending_group_interface(
993         struct wpa_supplicant *wpa_s)
994 {
995         if (!wpa_s->pending_interface_name[0] ||
996             is_zero_ether_addr(wpa_s->pending_interface_addr))
997                 return; /* No pending virtual interface */
998
999         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1000                    wpa_s->pending_interface_name);
1001         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1002                           wpa_s->pending_interface_name);
1003         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1004         wpa_s->pending_interface_name[0] = '\0';
1005 }
1006
1007
1008 static struct wpa_supplicant *
1009 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1010 {
1011         struct wpa_interface iface;
1012         struct wpa_supplicant *group_wpa_s;
1013
1014         if (!wpa_s->pending_interface_name[0]) {
1015                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1016                 if (!wpas_p2p_create_iface(wpa_s))
1017                         return NULL;
1018                 /*
1019                  * Something has forced us to remove the pending interface; try
1020                  * to create a new one and hope for the best that we will get
1021                  * the same local address.
1022                  */
1023                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1024                                                  WPA_IF_P2P_CLIENT) < 0)
1025                         return NULL;
1026         }
1027
1028         os_memset(&iface, 0, sizeof(iface));
1029         iface.ifname = wpa_s->pending_interface_name;
1030         iface.driver = wpa_s->driver->name;
1031         iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1032         iface.driver_param = wpa_s->conf->driver_param;
1033         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1034         if (group_wpa_s == NULL) {
1035                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1036                            "wpa_supplicant interface");
1037                 return NULL;
1038         }
1039         wpa_s->pending_interface_name[0] = '\0';
1040         group_wpa_s->parent = wpa_s;
1041         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1042                 P2P_GROUP_INTERFACE_CLIENT;
1043         wpa_s->global->p2p_group_formation = group_wpa_s;
1044
1045         wpas_p2p_clone_config(group_wpa_s, wpa_s);
1046
1047         return group_wpa_s;
1048 }
1049
1050
1051 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1052                                              void *timeout_ctx)
1053 {
1054         struct wpa_supplicant *wpa_s = eloop_ctx;
1055         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1056         if (wpa_s->global->p2p)
1057                 p2p_group_formation_failed(wpa_s->global->p2p);
1058         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1059                 wpa_drv_p2p_group_formation_failed(wpa_s);
1060         wpas_group_formation_completed(wpa_s, 0);
1061 }
1062
1063
1064 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1065 {
1066         struct wpa_supplicant *wpa_s = ctx;
1067
1068         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1069                 wpa_drv_cancel_remain_on_channel(wpa_s);
1070                 wpa_s->off_channel_freq = 0;
1071                 wpa_s->roc_waiting_drv_freq = 0;
1072         }
1073
1074         if (res->status) {
1075                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1076                         res->status);
1077                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1078                 wpas_p2p_remove_pending_group_interface(wpa_s);
1079                 return;
1080         }
1081
1082         if (wpa_s->p2p_go_ht40)
1083                 res->ht40 = 1;
1084
1085         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1086         wpas_notify_p2p_go_neg_completed(wpa_s, res);
1087
1088         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1089                 struct wpa_ssid *ssid;
1090                 ssid = wpa_config_get_network(wpa_s->conf,
1091                                               wpa_s->p2p_persistent_id);
1092                 if (ssid && ssid->disabled == 2 &&
1093                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1094                         size_t len = os_strlen(ssid->passphrase);
1095                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1096                                    "on requested persistent group");
1097                         os_memcpy(res->passphrase, ssid->passphrase, len);
1098                         res->passphrase[len] = '\0';
1099                 }
1100         }
1101
1102         if (wpa_s->create_p2p_iface) {
1103                 struct wpa_supplicant *group_wpa_s =
1104                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
1105                 if (group_wpa_s == NULL) {
1106                         wpas_p2p_remove_pending_group_interface(wpa_s);
1107                         return;
1108                 }
1109                 if (group_wpa_s != wpa_s) {
1110                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1111                                   sizeof(group_wpa_s->p2p_pin));
1112                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1113                 }
1114                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1115                 wpa_s->pending_interface_name[0] = '\0';
1116                 group_wpa_s->p2p_in_provisioning = 1;
1117
1118                 if (res->role_go)
1119                         wpas_start_wps_go(group_wpa_s, res, 1);
1120                 else
1121                         wpas_start_wps_enrollee(group_wpa_s, res);
1122         } else {
1123                 wpa_s->p2p_in_provisioning = 1;
1124                 wpa_s->global->p2p_group_formation = wpa_s;
1125
1126                 if (res->role_go)
1127                         wpas_start_wps_go(wpa_s, res, 1);
1128                 else
1129                         wpas_start_wps_enrollee(ctx, res);
1130         }
1131
1132         wpa_s->p2p_long_listen = 0;
1133         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1134
1135         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1136         eloop_register_timeout(15 + res->peer_config_timeout / 100,
1137                                (res->peer_config_timeout % 100) * 10000,
1138                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
1139 }
1140
1141
1142 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1143 {
1144         struct wpa_supplicant *wpa_s = ctx;
1145         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1146                 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1147
1148         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1149 }
1150
1151
1152 void wpas_dev_found(void *ctx, const u8 *addr,
1153                     const struct p2p_peer_info *info,
1154                     int new_device)
1155 {
1156 #ifndef CONFIG_NO_STDOUT_DEBUG
1157         struct wpa_supplicant *wpa_s = ctx;
1158         char devtype[WPS_DEV_TYPE_BUFSIZE];
1159
1160         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1161                 " p2p_dev_addr=" MACSTR
1162                 " pri_dev_type=%s name='%s' config_methods=0x%x "
1163                 "dev_capab=0x%x group_capab=0x%x",
1164                 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1165                 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1166                                      sizeof(devtype)),
1167                 info->device_name, info->config_methods,
1168                 info->dev_capab, info->group_capab);
1169 #endif /* CONFIG_NO_STDOUT_DEBUG */
1170
1171         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1172 }
1173
1174
1175 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1176 {
1177         struct wpa_supplicant *wpa_s = ctx;
1178
1179         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1180                 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1181
1182         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1183 }
1184
1185
1186 static int wpas_start_listen(void *ctx, unsigned int freq,
1187                              unsigned int duration,
1188                              const struct wpabuf *probe_resp_ie)
1189 {
1190         struct wpa_supplicant *wpa_s = ctx;
1191
1192         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1193
1194         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1195                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1196                            "report received Probe Request frames");
1197                 return -1;
1198         }
1199
1200         wpa_s->pending_listen_freq = freq;
1201         wpa_s->pending_listen_duration = duration;
1202
1203         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1204                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1205                            "to remain on channel (%u MHz) for Listen "
1206                            "state", freq);
1207                 wpa_s->pending_listen_freq = 0;
1208                 return -1;
1209         }
1210         wpa_s->off_channel_freq = 0;
1211         wpa_s->roc_waiting_drv_freq = freq;
1212
1213         return 0;
1214 }
1215
1216
1217 static void wpas_stop_listen(void *ctx)
1218 {
1219         struct wpa_supplicant *wpa_s = ctx;
1220         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1221                 wpa_drv_cancel_remain_on_channel(wpa_s);
1222                 wpa_s->off_channel_freq = 0;
1223                 wpa_s->roc_waiting_drv_freq = 0;
1224         }
1225         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1226         wpa_drv_probe_req_report(wpa_s, 0);
1227 }
1228
1229
1230 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1231 {
1232         struct wpa_supplicant *wpa_s = ctx;
1233         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1234 }
1235
1236
1237 static struct p2p_srv_bonjour *
1238 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1239                              const struct wpabuf *query)
1240 {
1241         struct p2p_srv_bonjour *bsrv;
1242         size_t len;
1243
1244         len = wpabuf_len(query);
1245         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1246                          struct p2p_srv_bonjour, list) {
1247                 if (len == wpabuf_len(bsrv->query) &&
1248                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1249                               len) == 0)
1250                         return bsrv;
1251         }
1252         return NULL;
1253 }
1254
1255
1256 static struct p2p_srv_upnp *
1257 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1258                           const char *service)
1259 {
1260         struct p2p_srv_upnp *usrv;
1261
1262         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1263                          struct p2p_srv_upnp, list) {
1264                 if (version == usrv->version &&
1265                     os_strcmp(service, usrv->service) == 0)
1266                         return usrv;
1267         }
1268         return NULL;
1269 }
1270
1271
1272 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1273                                         u8 srv_trans_id)
1274 {
1275         u8 *len_pos;
1276
1277         if (wpabuf_tailroom(resp) < 5)
1278                 return;
1279
1280         /* Length (to be filled) */
1281         len_pos = wpabuf_put(resp, 2);
1282         wpabuf_put_u8(resp, srv_proto);
1283         wpabuf_put_u8(resp, srv_trans_id);
1284         /* Status Code */
1285         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1286         /* Response Data: empty */
1287         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1288 }
1289
1290
1291 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1292                                 struct wpabuf *resp, u8 srv_trans_id)
1293 {
1294         struct p2p_srv_bonjour *bsrv;
1295         u8 *len_pos;
1296
1297         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1298
1299         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1300                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1301                 return;
1302         }
1303
1304         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1305                          struct p2p_srv_bonjour, list) {
1306                 if (wpabuf_tailroom(resp) <
1307                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1308                         return;
1309                 /* Length (to be filled) */
1310                 len_pos = wpabuf_put(resp, 2);
1311                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1312                 wpabuf_put_u8(resp, srv_trans_id);
1313                 /* Status Code */
1314                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1315                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1316                                   wpabuf_head(bsrv->resp),
1317                                   wpabuf_len(bsrv->resp));
1318                 /* Response Data */
1319                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1320                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1321                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1322                              2);
1323         }
1324 }
1325
1326
1327 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1328                                 struct wpabuf *resp, u8 srv_trans_id,
1329                                 const u8 *query, size_t query_len)
1330 {
1331         struct p2p_srv_bonjour *bsrv;
1332         struct wpabuf buf;
1333         u8 *len_pos;
1334
1335         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1336                           query, query_len);
1337         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1338                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1339                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1340                                             srv_trans_id);
1341                 return;
1342         }
1343
1344         if (query_len == 0) {
1345                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1346                 return;
1347         }
1348
1349         if (wpabuf_tailroom(resp) < 5)
1350                 return;
1351         /* Length (to be filled) */
1352         len_pos = wpabuf_put(resp, 2);
1353         wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1354         wpabuf_put_u8(resp, srv_trans_id);
1355
1356         wpabuf_set(&buf, query, query_len);
1357         bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1358         if (bsrv == NULL) {
1359                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1360                            "available");
1361
1362                 /* Status Code */
1363                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1364                 /* Response Data: empty */
1365                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1366                              2);
1367                 return;
1368         }
1369
1370         /* Status Code */
1371         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1372         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1373                           wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1374
1375         if (wpabuf_tailroom(resp) >=
1376             wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1377                 /* Response Data */
1378                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1379                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1380         }
1381         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1382 }
1383
1384
1385 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1386                              struct wpabuf *resp, u8 srv_trans_id)
1387 {
1388         struct p2p_srv_upnp *usrv;
1389         u8 *len_pos;
1390
1391         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1392
1393         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1394                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1395                 return;
1396         }
1397
1398         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1399                          struct p2p_srv_upnp, list) {
1400                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1401                         return;
1402
1403                 /* Length (to be filled) */
1404                 len_pos = wpabuf_put(resp, 2);
1405                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1406                 wpabuf_put_u8(resp, srv_trans_id);
1407
1408                 /* Status Code */
1409                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1410                 /* Response Data */
1411                 wpabuf_put_u8(resp, usrv->version);
1412                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1413                            usrv->service);
1414                 wpabuf_put_str(resp, usrv->service);
1415                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1416                              2);
1417         }
1418 }
1419
1420
1421 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1422                              struct wpabuf *resp, u8 srv_trans_id,
1423                              const u8 *query, size_t query_len)
1424 {
1425         struct p2p_srv_upnp *usrv;
1426         u8 *len_pos;
1427         u8 version;
1428         char *str;
1429         int count = 0;
1430
1431         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1432                           query, query_len);
1433
1434         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1435                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1436                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1437                                             srv_trans_id);
1438                 return;
1439         }
1440
1441         if (query_len == 0) {
1442                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1443                 return;
1444         }
1445
1446         if (wpabuf_tailroom(resp) < 5)
1447                 return;
1448
1449         /* Length (to be filled) */
1450         len_pos = wpabuf_put(resp, 2);
1451         wpabuf_put_u8(resp, P2P_SERV_UPNP);
1452         wpabuf_put_u8(resp, srv_trans_id);
1453
1454         version = query[0];
1455         str = os_malloc(query_len);
1456         if (str == NULL)
1457                 return;
1458         os_memcpy(str, query + 1, query_len - 1);
1459         str[query_len - 1] = '\0';
1460
1461         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1462                          struct p2p_srv_upnp, list) {
1463                 if (version != usrv->version)
1464                         continue;
1465
1466                 if (os_strcmp(str, "ssdp:all") != 0 &&
1467                     os_strstr(usrv->service, str) == NULL)
1468                         continue;
1469
1470                 if (wpabuf_tailroom(resp) < 2)
1471                         break;
1472                 if (count == 0) {
1473                         /* Status Code */
1474                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1475                         /* Response Data */
1476                         wpabuf_put_u8(resp, version);
1477                 } else
1478                         wpabuf_put_u8(resp, ',');
1479
1480                 count++;
1481
1482                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1483                            usrv->service);
1484                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1485                         break;
1486                 wpabuf_put_str(resp, usrv->service);
1487         }
1488         os_free(str);
1489
1490         if (count == 0) {
1491                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1492                            "available");
1493                 /* Status Code */
1494                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1495                 /* Response Data: empty */
1496         }
1497
1498         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1499 }
1500
1501
1502 #ifdef CONFIG_WIFI_DISPLAY
1503 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1504                             struct wpabuf *resp, u8 srv_trans_id,
1505                             const u8 *query, size_t query_len)
1506 {
1507         const u8 *pos;
1508         u8 role;
1509         u8 *len_pos;
1510
1511         wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1512
1513         if (!wpa_s->global->wifi_display) {
1514                 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1515                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1516                                             srv_trans_id);
1517                 return;
1518         }
1519
1520         if (query_len < 1) {
1521                 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1522                            "Role");
1523                 return;
1524         }
1525
1526         if (wpabuf_tailroom(resp) < 5)
1527                 return;
1528
1529         pos = query;
1530         role = *pos++;
1531         wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1532
1533         /* TODO: role specific handling */
1534
1535         /* Length (to be filled) */
1536         len_pos = wpabuf_put(resp, 2);
1537         wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1538         wpabuf_put_u8(resp, srv_trans_id);
1539         wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1540
1541         while (pos < query + query_len) {
1542                 if (*pos < MAX_WFD_SUBELEMS &&
1543                     wpa_s->global->wfd_subelem[*pos] &&
1544                     wpabuf_tailroom(resp) >=
1545                     wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1546                         wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1547                                    "subelement %u", *pos);
1548                         wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1549                 }
1550                 pos++;
1551         }
1552
1553         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1554 }
1555 #endif /* CONFIG_WIFI_DISPLAY */
1556
1557
1558 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1559                      u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1560 {
1561         struct wpa_supplicant *wpa_s = ctx;
1562         const u8 *pos = tlvs;
1563         const u8 *end = tlvs + tlvs_len;
1564         const u8 *tlv_end;
1565         u16 slen;
1566         struct wpabuf *resp;
1567         u8 srv_proto, srv_trans_id;
1568         size_t buf_len;
1569         char *buf;
1570
1571         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1572                     tlvs, tlvs_len);
1573         buf_len = 2 * tlvs_len + 1;
1574         buf = os_malloc(buf_len);
1575         if (buf) {
1576                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1577                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1578                              MACSTR " %u %u %s",
1579                              freq, MAC2STR(sa), dialog_token, update_indic,
1580                              buf);
1581                 os_free(buf);
1582         }
1583
1584         if (wpa_s->p2p_sd_over_ctrl_iface) {
1585                 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1586                                            update_indic, tlvs, tlvs_len);
1587                 return; /* to be processed by an external program */
1588         }
1589
1590         resp = wpabuf_alloc(10000);
1591         if (resp == NULL)
1592                 return;
1593
1594         while (pos + 1 < end) {
1595                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1596                 slen = WPA_GET_LE16(pos);
1597                 pos += 2;
1598                 if (pos + slen > end || slen < 2) {
1599                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1600                                    "length");
1601                         wpabuf_free(resp);
1602                         return;
1603                 }
1604                 tlv_end = pos + slen;
1605
1606                 srv_proto = *pos++;
1607                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1608                            srv_proto);
1609                 srv_trans_id = *pos++;
1610                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1611                            srv_trans_id);
1612
1613                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1614                             pos, tlv_end - pos);
1615
1616
1617                 if (wpa_s->force_long_sd) {
1618                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1619                                    "response");
1620                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1621                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1622                         goto done;
1623                 }
1624
1625                 switch (srv_proto) {
1626                 case P2P_SERV_ALL_SERVICES:
1627                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1628                                    "for all services");
1629                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1630                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1631                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1632                                            "discovery protocols available");
1633                                 wpas_sd_add_proto_not_avail(
1634                                         resp, P2P_SERV_ALL_SERVICES,
1635                                         srv_trans_id);
1636                                 break;
1637                         }
1638                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1639                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1640                         break;
1641                 case P2P_SERV_BONJOUR:
1642                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1643                                             pos, tlv_end - pos);
1644                         break;
1645                 case P2P_SERV_UPNP:
1646                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1647                                          pos, tlv_end - pos);
1648                         break;
1649 #ifdef CONFIG_WIFI_DISPLAY
1650                 case P2P_SERV_WIFI_DISPLAY:
1651                         wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1652                                         pos, tlv_end - pos);
1653                         break;
1654 #endif /* CONFIG_WIFI_DISPLAY */
1655                 default:
1656                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1657                                    "protocol %u", srv_proto);
1658                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1659                                                     srv_trans_id);
1660                         break;
1661                 }
1662
1663                 pos = tlv_end;
1664         }
1665
1666 done:
1667         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1668                                    update_indic, tlvs, tlvs_len);
1669
1670         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1671
1672         wpabuf_free(resp);
1673 }
1674
1675
1676 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1677                       const u8 *tlvs, size_t tlvs_len)
1678 {
1679         struct wpa_supplicant *wpa_s = ctx;
1680         const u8 *pos = tlvs;
1681         const u8 *end = tlvs + tlvs_len;
1682         const u8 *tlv_end;
1683         u16 slen;
1684         size_t buf_len;
1685         char *buf;
1686
1687         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1688                     tlvs, tlvs_len);
1689         if (tlvs_len > 1500) {
1690                 /* TODO: better way for handling this */
1691                 wpa_msg_ctrl(wpa_s, MSG_INFO,
1692                              P2P_EVENT_SERV_DISC_RESP MACSTR
1693                              " %u <long response: %u bytes>",
1694                              MAC2STR(sa), update_indic,
1695                              (unsigned int) tlvs_len);
1696         } else {
1697                 buf_len = 2 * tlvs_len + 1;
1698                 buf = os_malloc(buf_len);
1699                 if (buf) {
1700                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1701                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1702                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1703                                      MAC2STR(sa), update_indic, buf);
1704                         os_free(buf);
1705                 }
1706         }
1707
1708         while (pos < end) {
1709                 u8 srv_proto, srv_trans_id, status;
1710
1711                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1712                 slen = WPA_GET_LE16(pos);
1713                 pos += 2;
1714                 if (pos + slen > end || slen < 3) {
1715                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1716                                    "length");
1717                         return;
1718                 }
1719                 tlv_end = pos + slen;
1720
1721                 srv_proto = *pos++;
1722                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1723                            srv_proto);
1724                 srv_trans_id = *pos++;
1725                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1726                            srv_trans_id);
1727                 status = *pos++;
1728                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1729                            status);
1730
1731                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1732                             pos, tlv_end - pos);
1733
1734                 pos = tlv_end;
1735         }
1736
1737         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1738 }
1739
1740
1741 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1742                         const struct wpabuf *tlvs)
1743 {
1744         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1745                 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1746         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1747                 return 0;
1748         return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1749 }
1750
1751
1752 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1753                              u8 version, const char *query)
1754 {
1755         struct wpabuf *tlvs;
1756         u64 ret;
1757
1758         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1759         if (tlvs == NULL)
1760                 return 0;
1761         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1762         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1763         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1764         wpabuf_put_u8(tlvs, version);
1765         wpabuf_put_str(tlvs, query);
1766         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1767         wpabuf_free(tlvs);
1768         return ret;
1769 }
1770
1771
1772 #ifdef CONFIG_WIFI_DISPLAY
1773
1774 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1775                                    const struct wpabuf *tlvs)
1776 {
1777         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1778                 return 0;
1779         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1780                 return 0;
1781         return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1782 }
1783
1784
1785 #define MAX_WFD_SD_SUBELEMS 20
1786
1787 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1788                                 const char *subelems)
1789 {
1790         u8 *len;
1791         const char *pos;
1792         int val;
1793         int count = 0;
1794
1795         len = wpabuf_put(tlvs, 2);
1796         wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1797         wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1798
1799         wpabuf_put_u8(tlvs, role);
1800
1801         pos = subelems;
1802         while (*pos) {
1803                 val = atoi(pos);
1804                 if (val >= 0 && val < 256) {
1805                         wpabuf_put_u8(tlvs, val);
1806                         count++;
1807                         if (count == MAX_WFD_SD_SUBELEMS)
1808                                 break;
1809                 }
1810                 pos = os_strchr(pos + 1, ',');
1811                 if (pos == NULL)
1812                         break;
1813                 pos++;
1814         }
1815
1816         WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1817 }
1818
1819
1820 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1821                                      const u8 *dst, const char *role)
1822 {
1823         struct wpabuf *tlvs;
1824         u64 ret;
1825         const char *subelems;
1826         u8 id = 1;
1827
1828         subelems = os_strchr(role, ' ');
1829         if (subelems == NULL)
1830                 return 0;
1831         subelems++;
1832
1833         tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1834         if (tlvs == NULL)
1835                 return 0;
1836
1837         if (os_strstr(role, "[source]"))
1838                 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1839         if (os_strstr(role, "[pri-sink]"))
1840                 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1841         if (os_strstr(role, "[sec-sink]"))
1842                 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1843         if (os_strstr(role, "[source+sink]"))
1844                 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1845
1846         ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1847         wpabuf_free(tlvs);
1848         return ret;
1849 }
1850
1851 #endif /* CONFIG_WIFI_DISPLAY */
1852
1853
1854 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1855 {
1856         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1857                 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1858         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1859                 return -1;
1860         return p2p_sd_cancel_request(wpa_s->global->p2p,
1861                                      (void *) (uintptr_t) req);
1862 }
1863
1864
1865 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1866                           const u8 *dst, u8 dialog_token,
1867                           const struct wpabuf *resp_tlvs)
1868 {
1869         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1870                 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1871                                         resp_tlvs);
1872                 return;
1873         }
1874         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1875                 return;
1876         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1877                         resp_tlvs);
1878 }
1879
1880
1881 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1882 {
1883         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1884                 wpa_drv_p2p_service_update(wpa_s);
1885                 return;
1886         }
1887         if (wpa_s->global->p2p)
1888                 p2p_sd_service_update(wpa_s->global->p2p);
1889 }
1890
1891
1892 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1893 {
1894         dl_list_del(&bsrv->list);
1895         wpabuf_free(bsrv->query);
1896         wpabuf_free(bsrv->resp);
1897         os_free(bsrv);
1898 }
1899
1900
1901 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1902 {
1903         dl_list_del(&usrv->list);
1904         os_free(usrv->service);
1905         os_free(usrv);
1906 }
1907
1908
1909 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1910 {
1911         struct p2p_srv_bonjour *bsrv, *bn;
1912         struct p2p_srv_upnp *usrv, *un;
1913
1914         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1915                               struct p2p_srv_bonjour, list)
1916                 wpas_p2p_srv_bonjour_free(bsrv);
1917
1918         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1919                               struct p2p_srv_upnp, list)
1920                 wpas_p2p_srv_upnp_free(usrv);
1921
1922         wpas_p2p_sd_service_update(wpa_s);
1923 }
1924
1925
1926 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1927                                  struct wpabuf *query, struct wpabuf *resp)
1928 {
1929         struct p2p_srv_bonjour *bsrv;
1930
1931         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1932         if (bsrv) {
1933                 wpabuf_free(query);
1934                 wpabuf_free(bsrv->resp);
1935                 bsrv->resp = resp;
1936                 return 0;
1937         }
1938
1939         bsrv = os_zalloc(sizeof(*bsrv));
1940         if (bsrv == NULL)
1941                 return -1;
1942         bsrv->query = query;
1943         bsrv->resp = resp;
1944         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1945
1946         wpas_p2p_sd_service_update(wpa_s);
1947         return 0;
1948 }
1949
1950
1951 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1952                                  const struct wpabuf *query)
1953 {
1954         struct p2p_srv_bonjour *bsrv;
1955
1956         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1957         if (bsrv == NULL)
1958                 return -1;
1959         wpas_p2p_srv_bonjour_free(bsrv);
1960         wpas_p2p_sd_service_update(wpa_s);
1961         return 0;
1962 }
1963
1964
1965 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1966                               const char *service)
1967 {
1968         struct p2p_srv_upnp *usrv;
1969
1970         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1971                 return 0; /* Already listed */
1972         usrv = os_zalloc(sizeof(*usrv));
1973         if (usrv == NULL)
1974                 return -1;
1975         usrv->version = version;
1976         usrv->service = os_strdup(service);
1977         if (usrv->service == NULL) {
1978                 os_free(usrv);
1979                 return -1;
1980         }
1981         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1982
1983         wpas_p2p_sd_service_update(wpa_s);
1984         return 0;
1985 }
1986
1987
1988 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1989                               const char *service)
1990 {
1991         struct p2p_srv_upnp *usrv;
1992
1993         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1994         if (usrv == NULL)
1995                 return -1;
1996         wpas_p2p_srv_upnp_free(usrv);
1997         wpas_p2p_sd_service_update(wpa_s);
1998         return 0;
1999 }
2000
2001
2002 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2003                                          const u8 *peer, const char *params,
2004                                          unsigned int generated_pin)
2005 {
2006         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2007                 MAC2STR(peer), generated_pin, params);
2008 }
2009
2010
2011 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2012                                         const u8 *peer, const char *params)
2013 {
2014         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2015                 MAC2STR(peer), params);
2016 }
2017
2018
2019 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2020                         const u8 *dev_addr, const u8 *pri_dev_type,
2021                         const char *dev_name, u16 supp_config_methods,
2022                         u8 dev_capab, u8 group_capab, const u8 *group_id,
2023                         size_t group_id_len)
2024 {
2025         struct wpa_supplicant *wpa_s = ctx;
2026         char devtype[WPS_DEV_TYPE_BUFSIZE];
2027         char params[300];
2028         u8 empty_dev_type[8];
2029         unsigned int generated_pin = 0;
2030         struct wpa_supplicant *group = NULL;
2031
2032         if (group_id) {
2033                 for (group = wpa_s->global->ifaces; group; group = group->next)
2034                 {
2035                         struct wpa_ssid *s = group->current_ssid;
2036                         if (s != NULL &&
2037                             s->mode == WPAS_MODE_P2P_GO &&
2038                             group_id_len - ETH_ALEN == s->ssid_len &&
2039                             os_memcmp(group_id + ETH_ALEN, s->ssid,
2040                                       s->ssid_len) == 0)
2041                                 break;
2042                 }
2043         }
2044
2045         if (pri_dev_type == NULL) {
2046                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2047                 pri_dev_type = empty_dev_type;
2048         }
2049         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2050                     " pri_dev_type=%s name='%s' config_methods=0x%x "
2051                     "dev_capab=0x%x group_capab=0x%x%s%s",
2052                     MAC2STR(dev_addr),
2053                     wps_dev_type_bin2str(pri_dev_type, devtype,
2054                                          sizeof(devtype)),
2055                     dev_name, supp_config_methods, dev_capab, group_capab,
2056                     group ? " group=" : "",
2057                     group ? group->ifname : "");
2058         params[sizeof(params) - 1] = '\0';
2059
2060         if (config_methods & WPS_CONFIG_DISPLAY) {
2061                 generated_pin = wps_generate_pin();
2062                 wpas_prov_disc_local_display(wpa_s, peer, params,
2063                                              generated_pin);
2064         } else if (config_methods & WPS_CONFIG_KEYPAD)
2065                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2066         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2067                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2068                         "%s", MAC2STR(peer), params);
2069
2070         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2071                                             P2P_PROV_DISC_SUCCESS,
2072                                             config_methods, generated_pin);
2073 }
2074
2075
2076 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2077 {
2078         struct wpa_supplicant *wpa_s = ctx;
2079         unsigned int generated_pin = 0;
2080         char params[20];
2081
2082         if (wpa_s->pending_pd_before_join &&
2083             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2084              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2085                 wpa_s->pending_pd_before_join = 0;
2086                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2087                            "join-existing-group operation");
2088                 wpas_p2p_join_start(wpa_s);
2089                 return;
2090         }
2091
2092         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2093             wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2094                 os_snprintf(params, sizeof(params), " peer_go=%d",
2095                             wpa_s->pending_pd_use == AUTO_PD_JOIN);
2096         else
2097                 params[0] = '\0';
2098
2099         if (config_methods & WPS_CONFIG_DISPLAY)
2100                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2101         else if (config_methods & WPS_CONFIG_KEYPAD) {
2102                 generated_pin = wps_generate_pin();
2103                 wpas_prov_disc_local_display(wpa_s, peer, params,
2104                                              generated_pin);
2105         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2106                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2107                         "%s", MAC2STR(peer), params);
2108
2109         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2110                                             P2P_PROV_DISC_SUCCESS,
2111                                             config_methods, generated_pin);
2112 }
2113
2114
2115 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2116                                 enum p2p_prov_disc_status status)
2117 {
2118         struct wpa_supplicant *wpa_s = ctx;
2119
2120         if (wpa_s->p2p_fallback_to_go_neg) {
2121                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2122                         "failed - fall back to GO Negotiation");
2123                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2124                 return;
2125         }
2126
2127         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2128                 " p2p_dev_addr=" MACSTR " status=%d",
2129                 MAC2STR(peer), status);
2130
2131         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2132                                             status, 0, 0);
2133 }
2134
2135
2136 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2137                                   const u8 *go_dev_addr, const u8 *ssid,
2138                                   size_t ssid_len, int *go, u8 *group_bssid,
2139                                   int *force_freq, int persistent_group)
2140 {
2141         struct wpa_supplicant *wpa_s = ctx;
2142         struct wpa_ssid *s;
2143         u8 cur_bssid[ETH_ALEN];
2144         int res;
2145         struct wpa_supplicant *grp;
2146
2147         if (!persistent_group) {
2148                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2149                            " to join an active group", MAC2STR(sa));
2150                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2151                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2152                      == 0 ||
2153                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2154                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2155                                    "authorized invitation");
2156                         goto accept_inv;
2157                 }
2158                 /*
2159                  * Do not accept the invitation automatically; notify user and
2160                  * request approval.
2161                  */
2162                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2163         }
2164
2165         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2166         if (grp) {
2167                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2168                            "running persistent group");
2169                 if (*go)
2170                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2171                 goto accept_inv;
2172         }
2173
2174         if (!wpa_s->conf->persistent_reconnect)
2175                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2176
2177         for (s = wpa_s->conf->ssid; s; s = s->next) {
2178                 if (s->disabled == 2 &&
2179                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2180                     s->ssid_len == ssid_len &&
2181                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2182                         break;
2183         }
2184
2185         if (!s) {
2186                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2187                            " requested reinvocation of an unknown group",
2188                            MAC2STR(sa));
2189                 return P2P_SC_FAIL_UNKNOWN_GROUP;
2190         }
2191
2192         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2193                 *go = 1;
2194                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2195                         wpa_printf(MSG_DEBUG, "P2P: The only available "
2196                                    "interface is already in use - reject "
2197                                    "invitation");
2198                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2199                 }
2200                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2201         } else if (s->mode == WPAS_MODE_P2P_GO) {
2202                 *go = 1;
2203                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2204                 {
2205                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2206                                    "interface address for the group");
2207                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2208                 }
2209                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2210                           ETH_ALEN);
2211         }
2212
2213 accept_inv:
2214         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2215             wpa_s->assoc_freq) {
2216                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2217                            "the channel we are already using");
2218                 *force_freq = wpa_s->assoc_freq;
2219         }
2220
2221         res = wpa_drv_shared_freq(wpa_s);
2222         if (res > 0) {
2223                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2224                            "with the channel we are already using on a "
2225                            "shared interface");
2226                 *force_freq = res;
2227         }
2228
2229         return P2P_SC_SUCCESS;
2230 }
2231
2232
2233 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2234                                      const u8 *ssid, size_t ssid_len,
2235                                      const u8 *go_dev_addr, u8 status,
2236                                      int op_freq)
2237 {
2238         struct wpa_supplicant *wpa_s = ctx;
2239         struct wpa_ssid *s;
2240
2241         for (s = wpa_s->conf->ssid; s; s = s->next) {
2242                 if (s->disabled == 2 &&
2243                     s->ssid_len == ssid_len &&
2244                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2245                         break;
2246         }
2247
2248         if (status == P2P_SC_SUCCESS) {
2249                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2250                            " was accepted; op_freq=%d MHz",
2251                            MAC2STR(sa), op_freq);
2252                 if (s) {
2253                         int go = s->mode == WPAS_MODE_P2P_GO;
2254                         wpas_p2p_group_add_persistent(
2255                                 wpa_s, s, go, go ? op_freq : 0, 0);
2256                 } else if (bssid) {
2257                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2258                                       wpa_s->p2p_wps_method, 0);
2259                 }
2260                 return;
2261         }
2262
2263         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2264                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2265                            " was rejected (status %u)", MAC2STR(sa), status);
2266                 return;
2267         }
2268
2269         if (!s) {
2270                 if (bssid) {
2271                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2272                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2273                                 " bssid=" MACSTR " unknown-network",
2274                                 MAC2STR(sa), MAC2STR(go_dev_addr),
2275                                 MAC2STR(bssid));
2276                 } else {
2277                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2278                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2279                                 " unknown-network",
2280                                 MAC2STR(sa), MAC2STR(go_dev_addr));
2281                 }
2282                 return;
2283         }
2284
2285         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2286                 " persistent=%d", MAC2STR(sa), s->id);
2287 }
2288
2289
2290 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2291 {
2292         struct wpa_supplicant *wpa_s = ctx;
2293         struct wpa_ssid *ssid;
2294
2295         if (bssid) {
2296                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2297                         "status=%d " MACSTR,
2298                         status, MAC2STR(bssid));
2299         } else {
2300                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2301                         "status=%d ", status);
2302         }
2303         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2304
2305         if (wpa_s->pending_invite_ssid_id == -1)
2306                 return; /* Invitation to active group */
2307
2308         if (status != P2P_SC_SUCCESS) {
2309                 wpas_p2p_remove_pending_group_interface(wpa_s);
2310                 return;
2311         }
2312
2313         ssid = wpa_config_get_network(wpa_s->conf,
2314                                       wpa_s->pending_invite_ssid_id);
2315         if (ssid == NULL) {
2316                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2317                            "data matching with invitation");
2318                 return;
2319         }
2320
2321         /*
2322          * The peer could have missed our ctrl::ack frame for Invitation
2323          * Response and continue retransmitting the frame. To reduce the
2324          * likelihood of the peer not getting successful TX status for the
2325          * Invitation Response frame, wait a short time here before starting
2326          * the persistent group so that we will remain on the current channel to
2327          * acknowledge any possible retransmission from the peer.
2328          */
2329         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2330                 "starting persistent group");
2331         os_sleep(0, 50000);
2332
2333         wpas_p2p_group_add_persistent(wpa_s, ssid,
2334                                       ssid->mode == WPAS_MODE_P2P_GO,
2335                                       wpa_s->p2p_persistent_go_freq,
2336                                       wpa_s->p2p_go_ht40);
2337 }
2338
2339
2340 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2341                                     unsigned int freq)
2342 {
2343         unsigned int i;
2344
2345         if (global->p2p_disallow_freq == NULL)
2346                 return 0;
2347
2348         for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2349                 if (freq >= global->p2p_disallow_freq[i].min &&
2350                     freq <= global->p2p_disallow_freq[i].max)
2351                         return 1;
2352         }
2353
2354         return 0;
2355 }
2356
2357
2358 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2359 {
2360         reg->channel[reg->channels] = chan;
2361         reg->channels++;
2362 }
2363
2364
2365 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2366                                      struct p2p_channels *chan)
2367 {
2368         int i, cla = 0;
2369
2370         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2371                    "band");
2372
2373         /* Operating class 81 - 2.4 GHz band channels 1..13 */
2374         chan->reg_class[cla].reg_class = 81;
2375         chan->reg_class[cla].channels = 0;
2376         for (i = 0; i < 11; i++) {
2377                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2378                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2379         }
2380         if (chan->reg_class[cla].channels)
2381                 cla++;
2382
2383         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2384                    "band");
2385
2386         /* Operating class 115 - 5 GHz, channels 36-48 */
2387         chan->reg_class[cla].reg_class = 115;
2388         chan->reg_class[cla].channels = 0;
2389         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2390                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2391         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2392                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2393         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2394                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2395         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2396                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2397         if (chan->reg_class[cla].channels)
2398                 cla++;
2399
2400         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2401                    "band");
2402
2403         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2404         chan->reg_class[cla].reg_class = 124;
2405         chan->reg_class[cla].channels = 0;
2406         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2407                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2408         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2409                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2410         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2411                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2412         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2413                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2414         if (chan->reg_class[cla].channels)
2415                 cla++;
2416
2417         chan->reg_classes = cla;
2418         return 0;
2419 }
2420
2421
2422 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2423                                           u16 num_modes,
2424                                           enum hostapd_hw_mode mode)
2425 {
2426         u16 i;
2427
2428         for (i = 0; i < num_modes; i++) {
2429                 if (modes[i].mode == mode)
2430                         return &modes[i];
2431         }
2432
2433         return NULL;
2434 }
2435
2436
2437 static int has_channel(struct wpa_global *global,
2438                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
2439 {
2440         int i;
2441         unsigned int freq;
2442
2443         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2444                 chan * 5;
2445         if (wpas_p2p_disallowed_freq(global, freq))
2446                 return 0;
2447
2448         for (i = 0; i < mode->num_channels; i++) {
2449                 if (mode->channels[i].chan == chan) {
2450                         if (flags)
2451                                 *flags = mode->channels[i].flag;
2452                         return !(mode->channels[i].flag &
2453                                  (HOSTAPD_CHAN_DISABLED |
2454                                   HOSTAPD_CHAN_PASSIVE_SCAN |
2455                                   HOSTAPD_CHAN_NO_IBSS |
2456                                   HOSTAPD_CHAN_RADAR));
2457                 }
2458         }
2459
2460         return 0;
2461 }
2462
2463
2464 struct p2p_oper_class_map {
2465         enum hostapd_hw_mode mode;
2466         u8 op_class;
2467         u8 min_chan;
2468         u8 max_chan;
2469         u8 inc;
2470         enum { BW20, BW40PLUS, BW40MINUS } bw;
2471 };
2472
2473 static struct p2p_oper_class_map op_class[] = {
2474         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2475 #if 0 /* Do not enable HT40 on 2 GHz for now */
2476         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2477         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2478 #endif
2479         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2480         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2481         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2482         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2483         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2484         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2485         { -1, 0, 0, 0, 0, BW20 }
2486 };
2487
2488
2489 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2490                                    struct hostapd_hw_modes *mode,
2491                                    u8 channel, u8 bw)
2492 {
2493         int flag;
2494
2495         if (!has_channel(wpa_s->global, mode, channel, &flag))
2496                 return -1;
2497         if (bw == BW40MINUS &&
2498             (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2499              !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2500                 return 0;
2501         if (bw == BW40PLUS &&
2502             (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2503              !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2504                 return 0;
2505         return 1;
2506 }
2507
2508
2509 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2510                                    struct p2p_channels *chan)
2511 {
2512         struct hostapd_hw_modes *mode;
2513         int cla, op;
2514
2515         if (wpa_s->hw.modes == NULL) {
2516                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2517                            "of all supported channels; assume dualband "
2518                            "support");
2519                 return wpas_p2p_default_channels(wpa_s, chan);
2520         }
2521
2522         cla = 0;
2523
2524         for (op = 0; op_class[op].op_class; op++) {
2525                 struct p2p_oper_class_map *o = &op_class[op];
2526                 u8 ch;
2527                 struct p2p_reg_class *reg = NULL;
2528
2529                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2530                 if (mode == NULL)
2531                         continue;
2532                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2533                         if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2534                                 continue;
2535                         if (reg == NULL) {
2536                                 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2537                                            "class %u", o->op_class);
2538                                 reg = &chan->reg_class[cla];
2539                                 cla++;
2540                                 reg->reg_class = o->op_class;
2541                         }
2542                         reg->channel[reg->channels] = ch;
2543                         reg->channels++;
2544                 }
2545                 if (reg) {
2546                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2547                                     reg->channel, reg->channels);
2548                 }
2549         }
2550
2551         chan->reg_classes = cla;
2552
2553         return 0;
2554 }
2555
2556
2557 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2558                            struct hostapd_hw_modes *mode, u8 channel)
2559 {
2560         int op, ret;
2561
2562         for (op = 0; op_class[op].op_class; op++) {
2563                 struct p2p_oper_class_map *o = &op_class[op];
2564                 u8 ch;
2565
2566                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2567                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2568                             o->bw == BW20 || ch != channel)
2569                                 continue;
2570                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2571                         if (ret < 0)
2572                                 continue;
2573                         else if (ret > 0)
2574                                 return (o->bw == BW40MINUS) ? -1 : 1;
2575                         else
2576                                 return 0;
2577                 }
2578         }
2579         return 0;
2580 }
2581
2582
2583 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2584                         size_t buf_len)
2585 {
2586         struct wpa_supplicant *wpa_s = ctx;
2587
2588         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2589                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2590                         break;
2591         }
2592         if (wpa_s == NULL)
2593                 return -1;
2594
2595         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2596 }
2597
2598
2599 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2600 {
2601         struct wpa_supplicant *wpa_s = ctx;
2602
2603         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2604                 struct wpa_ssid *ssid = wpa_s->current_ssid;
2605                 if (ssid == NULL)
2606                         continue;
2607                 if (ssid->mode != WPAS_MODE_INFRA)
2608                         continue;
2609                 if (wpa_s->wpa_state != WPA_COMPLETED &&
2610                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2611                         continue;
2612                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2613                         return 1;
2614         }
2615
2616         return 0;
2617 }
2618
2619
2620 /**
2621  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2622  * @global: Pointer to global data from wpa_supplicant_init()
2623  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2624  * Returns: 0 on success, -1 on failure
2625  */
2626 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2627 {
2628         struct p2p_config p2p;
2629         unsigned int r;
2630         int i;
2631
2632         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2633                 return 0;
2634
2635         if (global->p2p)
2636                 return 0;
2637
2638         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2639                 struct p2p_params params;
2640
2641                 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2642                 os_memset(&params, 0, sizeof(params));
2643                 params.dev_name = wpa_s->conf->device_name;
2644                 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2645                           WPS_DEV_TYPE_LEN);
2646                 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2647                 os_memcpy(params.sec_dev_type,
2648                           wpa_s->conf->sec_device_type,
2649                           params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2650
2651                 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2652                         return -1;
2653
2654                 return 0;
2655         }
2656
2657         os_memset(&p2p, 0, sizeof(p2p));
2658         p2p.msg_ctx = wpa_s;
2659         p2p.cb_ctx = wpa_s;
2660         p2p.p2p_scan = wpas_p2p_scan;
2661         p2p.send_action = wpas_send_action;
2662         p2p.send_action_done = wpas_send_action_done;
2663         p2p.go_neg_completed = wpas_go_neg_completed;
2664         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2665         p2p.dev_found = wpas_dev_found;
2666         p2p.dev_lost = wpas_dev_lost;
2667         p2p.start_listen = wpas_start_listen;
2668         p2p.stop_listen = wpas_stop_listen;
2669         p2p.send_probe_resp = wpas_send_probe_resp;
2670         p2p.sd_request = wpas_sd_request;
2671         p2p.sd_response = wpas_sd_response;
2672         p2p.prov_disc_req = wpas_prov_disc_req;
2673         p2p.prov_disc_resp = wpas_prov_disc_resp;
2674         p2p.prov_disc_fail = wpas_prov_disc_fail;
2675         p2p.invitation_process = wpas_invitation_process;
2676         p2p.invitation_received = wpas_invitation_received;
2677         p2p.invitation_result = wpas_invitation_result;
2678         p2p.get_noa = wpas_get_noa;
2679         p2p.go_connected = wpas_go_connected;
2680
2681         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2682         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2683         p2p.dev_name = wpa_s->conf->device_name;
2684         p2p.manufacturer = wpa_s->conf->manufacturer;
2685         p2p.model_name = wpa_s->conf->model_name;
2686         p2p.model_number = wpa_s->conf->model_number;
2687         p2p.serial_number = wpa_s->conf->serial_number;
2688         if (wpa_s->wps) {
2689                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2690                 p2p.config_methods = wpa_s->wps->config_methods;
2691         }
2692
2693         if (wpa_s->conf->p2p_listen_reg_class &&
2694             wpa_s->conf->p2p_listen_channel) {
2695                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2696                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2697         } else {
2698                 p2p.reg_class = 81;
2699                 /*
2700                  * Pick one of the social channels randomly as the listen
2701                  * channel.
2702                  */
2703                 os_get_random((u8 *) &r, sizeof(r));
2704                 p2p.channel = 1 + (r % 3) * 5;
2705         }
2706         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2707
2708         if (wpa_s->conf->p2p_oper_reg_class &&
2709             wpa_s->conf->p2p_oper_channel) {
2710                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2711                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2712                 p2p.cfg_op_channel = 1;
2713                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2714                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2715
2716         } else {
2717                 p2p.op_reg_class = 81;
2718                 /*
2719                  * Use random operation channel from (1, 6, 11) if no other
2720                  * preference is indicated.
2721                  */
2722                 os_get_random((u8 *) &r, sizeof(r));
2723                 p2p.op_channel = 1 + (r % 3) * 5;
2724                 p2p.cfg_op_channel = 0;
2725                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2726                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2727         }
2728         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2729                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2730                 p2p.country[2] = 0x04;
2731         } else
2732                 os_memcpy(p2p.country, "XX\x04", 3);
2733
2734         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2735                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2736                            "channel list");
2737                 return -1;
2738         }
2739
2740         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2741                   WPS_DEV_TYPE_LEN);
2742
2743         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2744         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2745                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2746
2747         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2748                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2749
2750         p2p.max_peers = 100;
2751
2752         if (wpa_s->conf->p2p_ssid_postfix) {
2753                 p2p.ssid_postfix_len =
2754                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2755                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2756                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2757                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2758                           p2p.ssid_postfix_len);
2759         }
2760
2761         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2762
2763         p2p.max_listen = wpa_s->max_remain_on_chan;
2764
2765         global->p2p = p2p_init(&p2p);
2766         if (global->p2p == NULL)
2767                 return -1;
2768         global->p2p_init_wpa_s = wpa_s;
2769
2770         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2771                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2772                         continue;
2773                 p2p_add_wps_vendor_extension(
2774                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2775         }
2776
2777         return 0;
2778 }
2779
2780
2781 /**
2782  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2783  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2784  *
2785  * This function deinitialize per-interface P2P data.
2786  */
2787 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2788 {
2789         if (wpa_s->driver && wpa_s->drv_priv)
2790                 wpa_drv_probe_req_report(wpa_s, 0);
2791
2792         if (wpa_s->go_params) {
2793                 /* Clear any stored provisioning info */
2794                 p2p_clear_provisioning_info(
2795                         wpa_s->global->p2p,
2796                         wpa_s->go_params->peer_device_addr);
2797         }
2798
2799         os_free(wpa_s->go_params);
2800         wpa_s->go_params = NULL;
2801         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2802         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2803         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2804         wpa_s->p2p_long_listen = 0;
2805         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2806         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2807         wpas_p2p_remove_pending_group_interface(wpa_s);
2808
2809         /* TODO: remove group interface from the driver if this wpa_s instance
2810          * is on top of a P2P group interface */
2811 }
2812
2813
2814 /**
2815  * wpas_p2p_deinit_global - Deinitialize global P2P module
2816  * @global: Pointer to global data from wpa_supplicant_init()
2817  *
2818  * This function deinitializes the global (per device) P2P module.
2819  */
2820 void wpas_p2p_deinit_global(struct wpa_global *global)
2821 {
2822         struct wpa_supplicant *wpa_s, *tmp;
2823
2824         wpa_s = global->ifaces;
2825         if (wpa_s)
2826                 wpas_p2p_service_flush(wpa_s);
2827
2828         if (global->p2p == NULL)
2829                 return;
2830
2831         /* Remove remaining P2P group interfaces */
2832         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2833                 wpa_s = wpa_s->next;
2834         while (wpa_s) {
2835                 tmp = global->ifaces;
2836                 while (tmp &&
2837                        (tmp == wpa_s ||
2838                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2839                         tmp = tmp->next;
2840                 }
2841                 if (tmp == NULL)
2842                         break;
2843                 /* Disconnect from the P2P group and deinit the interface */
2844                 wpas_p2p_disconnect(tmp);
2845         }
2846
2847         /*
2848          * Deinit GO data on any possibly remaining interface (if main
2849          * interface is used as GO).
2850          */
2851         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2852                 if (wpa_s->ap_iface)
2853                         wpas_p2p_group_deinit(wpa_s);
2854         }
2855
2856         p2p_deinit(global->p2p);
2857         global->p2p = NULL;
2858         global->p2p_init_wpa_s = NULL;
2859 }
2860
2861
2862 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2863 {
2864         if (wpa_s->conf->p2p_no_group_iface)
2865                 return 0; /* separate interface disabled per configuration */
2866         if (wpa_s->drv_flags &
2867             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2868              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2869                 return 1; /* P2P group requires a new interface in every case
2870                            */
2871         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2872                 return 0; /* driver does not support concurrent operations */
2873         if (wpa_s->global->ifaces->next)
2874                 return 1; /* more that one interface already in use */
2875         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2876                 return 1; /* this interface is already in use */
2877         return 0;
2878 }
2879
2880
2881 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2882                                  const u8 *peer_addr,
2883                                  enum p2p_wps_method wps_method,
2884                                  int go_intent, const u8 *own_interface_addr,
2885                                  unsigned int force_freq, int persistent_group,
2886                                  struct wpa_ssid *ssid, unsigned int pref_freq)
2887 {
2888         if (persistent_group && wpa_s->conf->persistent_reconnect)
2889                 persistent_group = 2;
2890
2891         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2892                 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2893                                            go_intent, own_interface_addr,
2894                                            force_freq, persistent_group);
2895         }
2896
2897         /*
2898          * Increase GO config timeout if HT40 is used since it takes some time
2899          * to scan channels for coex purposes before the BSS can be started.
2900          */
2901         p2p_set_config_timeout(wpa_s->global->p2p,
2902                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2903
2904         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2905                            go_intent, own_interface_addr, force_freq,
2906                            persistent_group, ssid ? ssid->ssid : NULL,
2907                            ssid ? ssid->ssid_len : 0,
2908                            wpa_s->p2p_pd_before_go_neg, pref_freq);
2909 }
2910
2911
2912 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2913                                 const u8 *peer_addr,
2914                                 enum p2p_wps_method wps_method,
2915                                 int go_intent, const u8 *own_interface_addr,
2916                                 unsigned int force_freq, int persistent_group,
2917                                 struct wpa_ssid *ssid, unsigned int pref_freq)
2918 {
2919         if (persistent_group && wpa_s->conf->persistent_reconnect)
2920                 persistent_group = 2;
2921
2922         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2923                 return -1;
2924
2925         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2926                              go_intent, own_interface_addr, force_freq,
2927                              persistent_group, ssid ? ssid->ssid : NULL,
2928                              ssid ? ssid->ssid_len : 0, pref_freq);
2929 }
2930
2931
2932 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2933 {
2934         wpa_s->p2p_join_scan_count++;
2935         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2936                    wpa_s->p2p_join_scan_count);
2937         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2938                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2939                            " for join operationg - stop join attempt",
2940                            MAC2STR(wpa_s->pending_join_iface_addr));
2941                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2942                 if (wpa_s->p2p_auto_pd) {
2943                         wpa_s->p2p_auto_pd = 0;
2944                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2945                                 " p2p_dev_addr=" MACSTR " status=N/A",
2946                                 MAC2STR(wpa_s->pending_join_dev_addr));
2947                         return;
2948                 }
2949                 wpa_msg(wpa_s->parent, MSG_INFO,
2950                         P2P_EVENT_GROUP_FORMATION_FAILURE);
2951         }
2952 }
2953
2954
2955 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2956 {
2957         struct wpa_supplicant *wpa_s = eloop_ctx;
2958         if (!wpa_s->pending_pd_before_join)
2959                 return;
2960         wpa_s->pending_pd_before_join = 0;
2961         /*
2962          * Provision Discovery Response may have been lost - try to connect
2963          * anyway since we do not need any information from this PD.
2964          */
2965         wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2966                    "try to connect anyway");
2967         wpas_p2p_join_start(wpa_s);
2968 }
2969
2970
2971 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2972 {
2973         struct wpa_supplicant *iface;
2974         int shared_freq;
2975         u8 bssid[ETH_ALEN];
2976
2977         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2978                 return 0;
2979
2980         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2981                 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2982                         continue;
2983                 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2984                         continue;
2985                 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2986                     iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2987                         shared_freq = iface->current_ssid->frequency;
2988                 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2989                         shared_freq = iface->assoc_freq;
2990                 else
2991                         shared_freq = 0;
2992
2993                 if (shared_freq && freq != shared_freq) {
2994                         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2995                                    "connected on %d MHz - new connection on "
2996                                    "%d MHz", iface->ifname, shared_freq, freq);
2997                         return 1;
2998                 }
2999         }
3000
3001         shared_freq = wpa_drv_shared_freq(wpa_s);
3002         if (shared_freq > 0 && shared_freq != freq) {
3003                 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3004                            "virtual interface connected on %d MHz - new "
3005                            "connection on %d MHz", shared_freq, freq);
3006                 return 1;
3007         }
3008
3009         return 0;
3010 }
3011
3012
3013 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3014                             const u8 *peer_dev_addr)
3015 {
3016         struct wpa_bss *bss;
3017         int updated;
3018
3019         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3020         if (bss == NULL)
3021                 return -1;
3022         if (bss->last_update_idx < wpa_s->bss_update_idx) {
3023                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3024                            "last scan");
3025                 return 0;
3026         }
3027
3028         updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3029         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3030                    "%ld.%06ld (%supdated in last scan)",
3031                    bss->last_update.sec, bss->last_update.usec,
3032                    updated ? "": "not ");
3033
3034         return updated;
3035 }
3036
3037
3038 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3039                                    struct wpa_scan_results *scan_res)
3040 {
3041         struct wpa_bss *bss;
3042         int freq;
3043         u8 iface_addr[ETH_ALEN];
3044
3045         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3046
3047         if (wpa_s->global->p2p_disabled)
3048                 return;
3049
3050         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3051                    scan_res ? (int) scan_res->num : -1,
3052                    wpa_s->p2p_auto_join ? "auto_" : "");
3053
3054         if (scan_res)
3055                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3056
3057         if (wpa_s->p2p_auto_pd) {
3058                 int join = wpas_p2p_peer_go(wpa_s,
3059                                             wpa_s->pending_join_dev_addr);
3060                 if (join == 0 &&
3061                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3062                         wpa_s->auto_pd_scan_retry++;
3063                         bss = wpa_bss_get_bssid(wpa_s,
3064                                                 wpa_s->pending_join_dev_addr);
3065                         if (bss) {
3066                                 freq = bss->freq;
3067                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3068                                            "the peer " MACSTR " at %d MHz",
3069                                            wpa_s->auto_pd_scan_retry,
3070                                            MAC2STR(wpa_s->
3071                                                    pending_join_dev_addr),
3072                                            freq);
3073                                 wpas_p2p_join_scan_req(wpa_s, freq);
3074                                 return;
3075                         }
3076                 }
3077
3078                 if (join < 0)
3079                         join = 0;
3080
3081                 wpa_s->p2p_auto_pd = 0;
3082                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3083                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3084                            MAC2STR(wpa_s->pending_join_dev_addr), join);
3085                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3086                                       wpa_s->pending_join_dev_addr,
3087                                       wpa_s->pending_pd_config_methods, join,
3088                                       0) < 0) {
3089                         wpa_s->p2p_auto_pd = 0;
3090                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3091                                 " p2p_dev_addr=" MACSTR " status=N/A",
3092                                 MAC2STR(wpa_s->pending_join_dev_addr));
3093                 }
3094                 return;
3095         }
3096
3097         if (wpa_s->p2p_auto_join) {
3098                 int join = wpas_p2p_peer_go(wpa_s,
3099                                             wpa_s->pending_join_dev_addr);
3100                 if (join < 0) {
3101                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3102                                    "running a GO -> use GO Negotiation");
3103                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3104                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3105                                          wpa_s->p2p_persistent_group, 0, 0, 0,
3106                                          wpa_s->p2p_go_intent,
3107                                          wpa_s->p2p_connect_freq,
3108                                          wpa_s->p2p_persistent_id,
3109                                          wpa_s->p2p_pd_before_go_neg,
3110                                          wpa_s->p2p_go_ht40);
3111                         return;
3112                 }
3113
3114                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3115                            "try to join the group", join ? "" :
3116                            " in older scan");
3117                 if (!join)
3118                         wpa_s->p2p_fallback_to_go_neg = 1;
3119         }
3120
3121         freq = p2p_get_oper_freq(wpa_s->global->p2p,
3122                                  wpa_s->pending_join_iface_addr);
3123         if (freq < 0 &&
3124             p2p_get_interface_addr(wpa_s->global->p2p,
3125                                    wpa_s->pending_join_dev_addr,
3126                                    iface_addr) == 0 &&
3127             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3128         {
3129                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3130                            "address for join from " MACSTR " to " MACSTR
3131                            " based on newly discovered P2P peer entry",
3132                            MAC2STR(wpa_s->pending_join_iface_addr),
3133                            MAC2STR(iface_addr));
3134                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3135                           ETH_ALEN);
3136
3137                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3138                                          wpa_s->pending_join_iface_addr);
3139         }
3140         if (freq >= 0) {
3141                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3142                            "from P2P peer table: %d MHz", freq);
3143         }
3144         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3145         if (bss) {
3146                 freq = bss->freq;
3147                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3148                            "from BSS table: %d MHz", freq);
3149         }
3150         if (freq > 0) {
3151                 u16 method;
3152
3153                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3154                         wpa_msg(wpa_s->parent, MSG_INFO,
3155                                 P2P_EVENT_GROUP_FORMATION_FAILURE
3156                                 "reason=FREQ_CONFLICT");
3157                         return;
3158                 }
3159
3160                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3161                            "prior to joining an existing group (GO " MACSTR
3162                            " freq=%u MHz)",
3163                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
3164                 wpa_s->pending_pd_before_join = 1;
3165
3166                 switch (wpa_s->pending_join_wps_method) {
3167                 case WPS_PIN_DISPLAY:
3168                         method = WPS_CONFIG_KEYPAD;
3169                         break;
3170                 case WPS_PIN_KEYPAD:
3171                         method = WPS_CONFIG_DISPLAY;
3172                         break;
3173                 case WPS_PBC:
3174                         method = WPS_CONFIG_PUSHBUTTON;
3175                         break;
3176                 default:
3177                         method = 0;
3178                         break;
3179                 }
3180
3181                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3182                                                wpa_s->pending_join_dev_addr) ==
3183                      method)) {
3184                         /*
3185                          * We have already performed provision discovery for
3186                          * joining the group. Proceed directly to join
3187                          * operation without duplicated provision discovery. */
3188                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3189                                    "with " MACSTR " already done - proceed to "
3190                                    "join",
3191                                    MAC2STR(wpa_s->pending_join_dev_addr));
3192                         wpa_s->pending_pd_before_join = 0;
3193                         goto start;
3194                 }
3195
3196                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3197                                       wpa_s->pending_join_dev_addr, method, 1,
3198                                       freq) < 0) {
3199                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3200                                    "Discovery Request before joining an "
3201                                    "existing group");
3202                         wpa_s->pending_pd_before_join = 0;
3203                         goto start;
3204                 }
3205
3206                 /*
3207                  * Actual join operation will be started from the Action frame
3208                  * TX status callback (if no ACK is received) or when the
3209                  * Provision Discovery Response is received. Use a short
3210                  * timeout as a backup mechanism should the Provision Discovery
3211                  * Response be lost for any reason.
3212                  */
3213                 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3214                                      NULL);
3215                 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3216                                        wpa_s, NULL);
3217                 return;
3218         }
3219
3220         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3221         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3222         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3223         wpas_p2p_check_join_scan_limit(wpa_s);
3224         return;
3225
3226 start:
3227         /* Start join operation immediately */
3228         wpas_p2p_join_start(wpa_s);
3229 }
3230
3231
3232 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3233 {
3234         int ret;
3235         struct wpa_driver_scan_params params;
3236         struct wpabuf *wps_ie, *ies;
3237         size_t ielen;
3238         int freqs[2] = { 0, 0 };
3239
3240         os_memset(&params, 0, sizeof(params));
3241
3242         /* P2P Wildcard SSID */
3243         params.num_ssids = 1;
3244         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3245         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3246
3247         wpa_s->wps->dev.p2p = 1;
3248         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3249                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3250                                         NULL);
3251         if (wps_ie == NULL) {
3252                 wpas_p2p_scan_res_join(wpa_s, NULL);
3253                 return;
3254         }
3255
3256         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3257         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3258         if (ies == NULL) {
3259                 wpabuf_free(wps_ie);
3260                 wpas_p2p_scan_res_join(wpa_s, NULL);
3261                 return;
3262         }
3263         wpabuf_put_buf(ies, wps_ie);
3264         wpabuf_free(wps_ie);
3265
3266         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3267
3268         params.p2p_probe = 1;
3269         params.extra_ies = wpabuf_head(ies);
3270         params.extra_ies_len = wpabuf_len(ies);
3271         if (freq > 0) {
3272                 freqs[0] = freq;
3273                 params.freqs = freqs;
3274         }
3275
3276         /*
3277          * Run a scan to update BSS table and start Provision Discovery once
3278          * the new scan results become available.
3279          */
3280         ret = wpa_drv_scan(wpa_s, &params);
3281         if (!ret)
3282                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3283
3284         wpabuf_free(ies);
3285
3286         if (ret) {
3287                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3288                            "try again later");
3289                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3290                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3291                 wpas_p2p_check_join_scan_limit(wpa_s);
3292         }
3293 }
3294
3295
3296 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3297 {
3298         struct wpa_supplicant *wpa_s = eloop_ctx;
3299         wpas_p2p_join_scan_req(wpa_s, 0);
3300 }
3301
3302
3303 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3304                          const u8 *dev_addr, enum p2p_wps_method wps_method,
3305                          int auto_join)
3306 {
3307         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3308                    MACSTR " dev " MACSTR ")%s",
3309                    MAC2STR(iface_addr), MAC2STR(dev_addr),
3310                    auto_join ? " (auto_join)" : "");
3311
3312         wpa_s->p2p_auto_pd = 0;
3313         wpa_s->p2p_auto_join = !!auto_join;
3314         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3315         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3316         wpa_s->pending_join_wps_method = wps_method;
3317
3318         /* Make sure we are not running find during connection establishment */
3319         wpas_p2p_stop_find(wpa_s);
3320
3321         wpa_s->p2p_join_scan_count = 0;
3322         wpas_p2p_join_scan(wpa_s, NULL);
3323         return 0;
3324 }
3325
3326
3327 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3328 {
3329         struct wpa_supplicant *group;
3330         struct p2p_go_neg_results res;
3331         struct wpa_bss *bss;
3332
3333         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3334         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3335         if (group == NULL)
3336                 return -1;
3337         if (group != wpa_s) {
3338                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3339                           sizeof(group->p2p_pin));
3340                 group->p2p_wps_method = wpa_s->p2p_wps_method;
3341         }
3342
3343         group->p2p_in_provisioning = 1;
3344         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3345
3346         os_memset(&res, 0, sizeof(res));
3347         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3348                   ETH_ALEN);
3349         res.wps_method = wpa_s->pending_join_wps_method;
3350         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3351         if (bss) {
3352                 res.freq = bss->freq;
3353                 res.ssid_len = bss->ssid_len;
3354                 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3355         }
3356
3357         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3358                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3359                            "starting client");
3360                 wpa_drv_cancel_remain_on_channel(wpa_s);
3361                 wpa_s->off_channel_freq = 0;
3362                 wpa_s->roc_waiting_drv_freq = 0;
3363         }
3364         wpas_start_wps_enrollee(group, &res);
3365
3366         /*
3367          * Allow a longer timeout for join-a-running-group than normal 15
3368          * second group formation timeout since the GO may not have authorized
3369          * our connection yet.
3370          */
3371         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3372         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3373                                wpa_s, NULL);
3374
3375         return 0;
3376 }
3377
3378
3379 /**
3380  * wpas_p2p_connect - Request P2P Group Formation to be started
3381  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3382  * @peer_addr: Address of the peer P2P Device
3383  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3384  * @persistent_group: Whether to create a persistent group
3385  * @auto_join: Whether to select join vs. GO Negotiation automatically
3386  * @join: Whether to join an existing group (as a client) instead of starting
3387  *      Group Owner negotiation; @peer_addr is BSSID in that case
3388  * @auth: Whether to only authorize the connection instead of doing that and
3389  *      initiating Group Owner negotiation
3390  * @go_intent: GO Intent or -1 to use default
3391  * @freq: Frequency for the group or 0 for auto-selection
3392  * @persistent_id: Persistent group credentials to use for forcing GO
3393  *      parameters or -1 to generate new values (SSID/passphrase)
3394  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3395  *      interoperability workaround when initiating group formation
3396  * @ht40: Start GO with 40 MHz channel width
3397  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3398  *      failure, -2 on failure due to channel not currently available,
3399  *      -3 if forced channel is not supported
3400  */
3401 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3402                      const char *pin, enum p2p_wps_method wps_method,
3403                      int persistent_group, int auto_join, int join, int auth,
3404                      int go_intent, int freq, int persistent_id, int pd,
3405                      int ht40)
3406 {
3407         int force_freq = 0, pref_freq = 0, oper_freq = 0;
3408         u8 bssid[ETH_ALEN];
3409         int ret = 0;
3410         enum wpa_driver_if_type iftype;
3411         const u8 *if_addr;
3412         struct wpa_ssid *ssid = NULL;
3413
3414         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3415                 return -1;
3416
3417         if (persistent_id >= 0) {
3418                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3419                 if (ssid == NULL || ssid->disabled != 2 ||
3420                     ssid->mode != WPAS_MODE_P2P_GO)
3421                         return -1;
3422         }
3423
3424         if (go_intent < 0)
3425                 go_intent = wpa_s->conf->p2p_go_intent;
3426
3427         if (!auth)
3428                 wpa_s->p2p_long_listen = 0;
3429
3430         wpa_s->p2p_wps_method = wps_method;
3431         wpa_s->p2p_persistent_group = !!persistent_group;
3432         wpa_s->p2p_persistent_id = persistent_id;
3433         wpa_s->p2p_go_intent = go_intent;
3434         wpa_s->p2p_connect_freq = freq;
3435         wpa_s->p2p_fallback_to_go_neg = 0;
3436         wpa_s->p2p_pd_before_go_neg = !!pd;
3437         wpa_s->p2p_go_ht40 = !!ht40;
3438
3439         if (pin)
3440                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3441         else if (wps_method == WPS_PIN_DISPLAY) {
3442                 ret = wps_generate_pin();
3443                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3444                             ret);
3445                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3446                            wpa_s->p2p_pin);
3447         } else
3448                 wpa_s->p2p_pin[0] = '\0';
3449
3450         if (join || auto_join) {
3451                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3452                 if (auth) {
3453                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3454                                    "connect a running group from " MACSTR,
3455                                    MAC2STR(peer_addr));
3456                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3457                         return ret;
3458                 }
3459                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3460                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3461                                            iface_addr) < 0) {
3462                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3463                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3464                                          dev_addr);
3465                 }
3466                 if (auto_join) {
3467                         os_get_time(&wpa_s->p2p_auto_started);
3468                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3469                                    "%ld.%06ld",
3470                                    wpa_s->p2p_auto_started.sec,
3471                                    wpa_s->p2p_auto_started.usec);
3472                 }
3473                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3474                                   auto_join) < 0)
3475                         return -1;
3476                 return ret;
3477         }
3478
3479         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3480             wpa_s->assoc_freq)
3481                 oper_freq = wpa_s->assoc_freq;
3482         else {
3483                 oper_freq = wpa_drv_shared_freq(wpa_s);
3484                 if (oper_freq < 0)
3485                         oper_freq = 0;
3486         }
3487
3488         if (freq > 0) {
3489                 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3490                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3491                                    "(%u MHz) is not supported for P2P uses",
3492                                    freq);
3493                         return -3;
3494                 }
3495
3496                 if (oper_freq > 0 && freq != oper_freq &&
3497                     !(wpa_s->drv_flags &
3498                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3499                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3500                                    "on %u MHz while connected on another "
3501                                    "channel (%u MHz)", freq, oper_freq);
3502                         return -2;
3503                 }
3504                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3505                            "requested channel (%u MHz)", freq);
3506                 force_freq = freq;
3507         } else if (oper_freq > 0 &&
3508                    !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3509                 if (!(wpa_s->drv_flags &
3510                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3511                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3512                                    "while connected on non-P2P supported "
3513                                    "channel (%u MHz)", oper_freq);
3514                         return -2;
3515                 }
3516                 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3517                            "(%u MHz) not available for P2P - try to use "
3518                            "another channel", oper_freq);
3519                 force_freq = 0;
3520         } else if (oper_freq > 0 &&
3521                    (wpa_s->drv_flags &
3522                     WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3523                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3524                            "are already using (%u MHz) on another interface",
3525                            oper_freq);
3526                 pref_freq = oper_freq;
3527         } else if (oper_freq > 0) {
3528                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3529                            "channel we are already using (%u MHz) on another "
3530                            "interface", oper_freq);
3531                 force_freq = oper_freq;
3532         }
3533
3534         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3535
3536         if (wpa_s->create_p2p_iface) {
3537                 /* Prepare to add a new interface for the group */
3538                 iftype = WPA_IF_P2P_GROUP;
3539                 if (go_intent == 15)
3540                         iftype = WPA_IF_P2P_GO;
3541                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3542                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3543                                    "interface for the group");
3544                         return -1;
3545                 }
3546
3547                 if_addr = wpa_s->pending_interface_addr;
3548         } else
3549                 if_addr = wpa_s->own_addr;
3550
3551         if (auth) {
3552                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3553                                          go_intent, if_addr,
3554                                          force_freq, persistent_group, ssid,
3555                                          pref_freq) < 0)
3556                         return -1;
3557                 return ret;
3558         }
3559
3560         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3561                                   go_intent, if_addr, force_freq,
3562                                   persistent_group, ssid, pref_freq) < 0) {
3563                 if (wpa_s->create_p2p_iface)
3564                         wpas_p2p_remove_pending_group_interface(wpa_s);
3565                 return -1;
3566         }
3567         return ret;
3568 }
3569
3570
3571 /**
3572  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3573  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3574  * @freq: Frequency of the channel in MHz
3575  * @duration: Duration of the stay on the channel in milliseconds
3576  *
3577  * This callback is called when the driver indicates that it has started the
3578  * requested remain-on-channel duration.
3579  */
3580 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3581                                    unsigned int freq, unsigned int duration)
3582 {
3583         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3584                 return;
3585         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3586                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3587                               wpa_s->pending_listen_duration);
3588                 wpa_s->pending_listen_freq = 0;
3589         }
3590 }
3591
3592
3593 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3594                                  unsigned int timeout)
3595 {
3596         /* Limit maximum Listen state time based on driver limitation. */
3597         if (timeout > wpa_s->max_remain_on_chan)
3598                 timeout = wpa_s->max_remain_on_chan;
3599
3600         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3601                 return wpa_drv_p2p_listen(wpa_s, timeout);
3602
3603         return p2p_listen(wpa_s->global->p2p, timeout);
3604 }
3605
3606
3607 /**
3608  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3609  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3610  * @freq: Frequency of the channel in MHz
3611  *
3612  * This callback is called when the driver indicates that a remain-on-channel
3613  * operation has been completed, i.e., the duration on the requested channel
3614  * has timed out.
3615  */
3616 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3617                                           unsigned int freq)
3618 {
3619         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3620                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
3621                    wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3622         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3623                 return;
3624         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3625                 return; /* P2P module started a new operation */
3626         if (wpa_s->pending_action_tx)
3627                 return;
3628         if (wpa_s->p2p_long_listen > 0)
3629                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3630         if (wpa_s->p2p_long_listen > 0) {
3631                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3632                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3633         }
3634 }
3635
3636
3637 /**
3638  * wpas_p2p_group_remove - Remove a P2P group
3639  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3640  * @ifname: Network interface name of the group interface or "*" to remove all
3641  *      groups
3642  * Returns: 0 on success, -1 on failure
3643  *
3644  * This function is used to remove a P2P group. This can be used to disconnect
3645  * from a group in which the local end is a P2P Client or to end a P2P Group in
3646  * case the local end is the Group Owner. If a virtual network interface was
3647  * created for this group, that interface will be removed. Otherwise, only the
3648  * configured P2P group network will be removed from the interface.
3649  */
3650 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3651 {
3652         struct wpa_global *global = wpa_s->global;
3653
3654         if (os_strcmp(ifname, "*") == 0) {
3655                 struct wpa_supplicant *prev;
3656                 wpa_s = global->ifaces;
3657                 while (wpa_s) {
3658                         prev = wpa_s;
3659                         wpa_s = wpa_s->next;
3660                         wpas_p2p_disconnect(prev);
3661                 }
3662                 return 0;
3663         }
3664
3665         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3666                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3667                         break;
3668         }
3669
3670         return wpas_p2p_disconnect(wpa_s);
3671 }
3672
3673
3674 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3675                                    struct p2p_go_neg_results *params,
3676                                    int freq, int ht40)
3677 {
3678         u8 bssid[ETH_ALEN];
3679         int res;
3680
3681         os_memset(params, 0, sizeof(*params));
3682         params->role_go = 1;
3683         params->ht40 = ht40;
3684         if (freq) {
3685                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3686                            "frequency %d MHz", freq);
3687                 params->freq = freq;
3688         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3689                    wpa_s->conf->p2p_oper_channel >= 1 &&
3690                    wpa_s->conf->p2p_oper_channel <= 11) {
3691                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3692                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3693                            "frequency %d MHz", params->freq);
3694         } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3695                    wpa_s->conf->p2p_oper_reg_class == 124) {
3696                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3697                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3698                            "frequency %d MHz", params->freq);
3699         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3700                    wpa_s->best_overall_freq > 0 &&
3701                    p2p_supported_freq(wpa_s->global->p2p,
3702                                       wpa_s->best_overall_freq)) {
3703                 params->freq = wpa_s->best_overall_freq;
3704                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3705                            "channel %d MHz", params->freq);
3706         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3707                    wpa_s->best_24_freq > 0 &&
3708                    p2p_supported_freq(wpa_s->global->p2p,
3709                                       wpa_s->best_24_freq)) {
3710                 params->freq = wpa_s->best_24_freq;
3711                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3712                            "channel %d MHz", params->freq);
3713         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3714                    wpa_s->best_5_freq > 0 &&
3715                    p2p_supported_freq(wpa_s->global->p2p,
3716                                       wpa_s->best_5_freq)) {
3717                 params->freq = wpa_s->best_5_freq;
3718                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3719                            "channel %d MHz", params->freq);
3720         } else {
3721                 int chan;
3722                 for (chan = 0; chan < 11; chan++) {
3723                         params->freq = 2412 + chan * 5;
3724                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
3725                                                       params->freq))
3726                                 break;
3727                 }
3728                 if (chan == 11) {
3729                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3730                                    "allowed");
3731                         return -1;
3732                 }
3733                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3734                            "known)", params->freq);
3735         }
3736
3737         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3738             wpa_s->assoc_freq && !freq) {
3739                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3740                            "already using");
3741                 params->freq = wpa_s->assoc_freq;
3742         }
3743
3744         res = wpa_drv_shared_freq(wpa_s);
3745         if (res > 0 && !freq) {
3746                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3747                            "already using on a shared interface");
3748                 params->freq = res;
3749         } else if (res > 0 && freq != res &&
3750                    !(wpa_s->drv_flags &
3751                      WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3752                 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3753                            "while connected on another channel (%u MHz)",
3754                            freq, res);
3755                 return -1;
3756         }
3757
3758         return 0;
3759 }
3760
3761
3762 static struct wpa_supplicant *
3763 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3764                          int go)
3765 {
3766         struct wpa_supplicant *group_wpa_s;
3767
3768         if (!wpas_p2p_create_iface(wpa_s)) {
3769                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3770                         "operations");
3771                 return wpa_s;
3772         }
3773
3774         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3775                                          WPA_IF_P2P_CLIENT) < 0) {
3776                 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
3777                 return NULL;
3778         }
3779         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3780         if (group_wpa_s == NULL) {
3781                 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
3782                         "interface");
3783                 wpas_p2p_remove_pending_group_interface(wpa_s);
3784                 return NULL;
3785         }
3786
3787         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
3788                 group_wpa_s->ifname);
3789         return group_wpa_s;
3790 }
3791
3792
3793 /**
3794  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3795  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3796  * @persistent_group: Whether to create a persistent group
3797  * @freq: Frequency for the group or 0 to indicate no hardcoding
3798  * Returns: 0 on success, -1 on failure
3799  *
3800  * This function creates a new P2P group with the local end as the Group Owner,
3801  * i.e., without using Group Owner Negotiation.
3802  */
3803 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3804                        int freq, int ht40)
3805 {
3806         struct p2p_go_neg_results params;
3807         unsigned int r;
3808
3809         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3810                 return -1;
3811
3812         /* Make sure we are not running find during connection establishment */
3813         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3814         wpas_p2p_stop_find(wpa_s);
3815
3816         if (freq == 2) {
3817                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3818                            "band");
3819                 if (wpa_s->best_24_freq > 0 &&
3820                     p2p_supported_freq(wpa_s->global->p2p,
3821                                        wpa_s->best_24_freq)) {
3822                         freq = wpa_s->best_24_freq;
3823                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3824                                    "channel: %d MHz", freq);
3825                 } else {
3826                         os_get_random((u8 *) &r, sizeof(r));
3827                         freq = 2412 + (r % 3) * 25;
3828                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3829                                    "channel: %d MHz", freq);
3830                 }
3831         }
3832
3833         if (freq == 5) {
3834                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3835                            "band");
3836                 if (wpa_s->best_5_freq > 0 &&
3837                     p2p_supported_freq(wpa_s->global->p2p,
3838                                        wpa_s->best_5_freq)) {
3839                         freq = wpa_s->best_5_freq;
3840                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3841                                    "channel: %d MHz", freq);
3842                 } else {
3843                         os_get_random((u8 *) &r, sizeof(r));
3844                         freq = 5180 + (r % 4) * 20;
3845                         if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3846                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3847                                            "5 GHz channel for P2P group");
3848                                 return -1;
3849                         }
3850                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3851                                    "channel: %d MHz", freq);
3852                 }
3853         }
3854
3855         if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3856                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3857                            "(%u MHz) is not supported for P2P uses",
3858                            freq);
3859                 return -1;
3860         }
3861
3862         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3863                 return -1;
3864         if (params.freq &&
3865             !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3866                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3867                            "(%u MHz) is not supported for P2P uses",
3868                            params.freq);
3869                 return -1;
3870         }
3871         p2p_go_params(wpa_s->global->p2p, &params);
3872         params.persistent_group = persistent_group;
3873
3874         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3875         if (wpa_s == NULL)
3876                 return -1;
3877         wpas_start_wps_go(wpa_s, &params, 0);
3878
3879         return 0;
3880 }
3881
3882
3883 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3884                                  struct wpa_ssid *params, int addr_allocated)
3885 {
3886         struct wpa_ssid *ssid;
3887
3888         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3889         if (wpa_s == NULL)
3890                 return -1;
3891
3892         wpa_supplicant_ap_deinit(wpa_s);
3893
3894         ssid = wpa_config_add_network(wpa_s->conf);
3895         if (ssid == NULL)
3896                 return -1;
3897         wpa_config_set_network_defaults(ssid);
3898         ssid->temporary = 1;
3899         ssid->proto = WPA_PROTO_RSN;
3900         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3901         ssid->group_cipher = WPA_CIPHER_CCMP;
3902         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3903         ssid->ssid = os_malloc(params->ssid_len);
3904         if (ssid->ssid == NULL) {
3905                 wpa_config_remove_network(wpa_s->conf, ssid->id);
3906                 return -1;
3907         }
3908         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3909         ssid->ssid_len = params->ssid_len;
3910         ssid->p2p_group = 1;
3911         ssid->export_keys = 1;
3912         if (params->psk_set) {
3913                 os_memcpy(ssid->psk, params->psk, 32);
3914                 ssid->psk_set = 1;
3915         }
3916         if (params->passphrase)
3917                 ssid->passphrase = os_strdup(params->passphrase);
3918
3919         wpa_supplicant_select_network(wpa_s, ssid);
3920
3921         wpa_s->show_group_started = 1;
3922
3923         return 0;
3924 }
3925
3926
3927 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3928                                   struct wpa_ssid *ssid, int addr_allocated,
3929                                   int freq, int ht40)
3930 {
3931         struct p2p_go_neg_results params;
3932         int go = 0;
3933
3934         if (ssid->disabled != 2 || ssid->ssid == NULL)
3935                 return -1;
3936
3937         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3938             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3939                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3940                            "already running");
3941                 return 0;
3942         }
3943
3944         /* Make sure we are not running find during connection establishment */
3945         wpas_p2p_stop_find(wpa_s);
3946
3947         wpa_s->p2p_fallback_to_go_neg = 0;
3948
3949         if (ssid->mode == WPAS_MODE_INFRA)
3950                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3951
3952         if (ssid->mode != WPAS_MODE_P2P_GO)
3953                 return -1;
3954
3955         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3956                 return -1;
3957
3958         params.role_go = 1;
3959         if (ssid->passphrase == NULL ||
3960             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3961                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3962                            "group");
3963                 return -1;
3964         }
3965         os_strlcpy(params.passphrase, ssid->passphrase,
3966                    sizeof(params.passphrase));
3967         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3968         params.ssid_len = ssid->ssid_len;
3969         params.persistent_group = 1;
3970
3971         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3972         if (wpa_s == NULL)
3973                 return -1;
3974
3975         wpas_start_wps_go(wpa_s, &params, 0);
3976
3977         return 0;
3978 }
3979
3980
3981 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3982                                struct wpabuf *proberesp_ies)
3983 {
3984         struct wpa_supplicant *wpa_s = ctx;
3985         if (wpa_s->ap_iface) {
3986                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3987                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3988                         wpabuf_free(beacon_ies);
3989                         wpabuf_free(proberesp_ies);
3990                         return;
3991                 }
3992                 if (beacon_ies) {
3993                         wpabuf_free(hapd->p2p_beacon_ie);
3994                         hapd->p2p_beacon_ie = beacon_ies;
3995                 }
3996                 wpabuf_free(hapd->p2p_probe_resp_ie);
3997                 hapd->p2p_probe_resp_ie = proberesp_ies;
3998         } else {
3999                 wpabuf_free(beacon_ies);
4000                 wpabuf_free(proberesp_ies);
4001         }
4002         wpa_supplicant_ap_update_beacon(wpa_s);
4003 }
4004
4005
4006 static void wpas_p2p_idle_update(void *ctx, int idle)
4007 {
4008         struct wpa_supplicant *wpa_s = ctx;
4009         if (!wpa_s->ap_iface)
4010                 return;
4011         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4012         if (idle)
4013                 wpas_p2p_set_group_idle_timeout(wpa_s);
4014         else
4015                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4016 }
4017
4018
4019 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4020                                        struct wpa_ssid *ssid)
4021 {
4022         struct p2p_group *group;
4023         struct p2p_group_config *cfg;
4024
4025         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4026                 return NULL;
4027         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4028                 return NULL;
4029
4030         cfg = os_zalloc(sizeof(*cfg));
4031         if (cfg == NULL)
4032                 return NULL;
4033
4034         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4035                 cfg->persistent_group = 2;
4036         else if (ssid->p2p_persistent_group)
4037                 cfg->persistent_group = 1;
4038         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4039         if (wpa_s->max_stations &&
4040             wpa_s->max_stations < wpa_s->conf->max_num_sta)
4041                 cfg->max_clients = wpa_s->max_stations;
4042         else
4043                 cfg->max_clients = wpa_s->conf->max_num_sta;
4044         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4045         cfg->ssid_len = ssid->ssid_len;
4046         cfg->cb_ctx = wpa_s;
4047         cfg->ie_update = wpas_p2p_ie_update;
4048         cfg->idle_update = wpas_p2p_idle_update;
4049
4050         group = p2p_group_init(wpa_s->global->p2p, cfg);
4051         if (group == NULL)
4052                 os_free(cfg);
4053         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4054                 p2p_group_notif_formation_done(group);
4055         wpa_s->p2p_group = group;
4056         return group;
4057 }
4058
4059
4060 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4061                           int registrar)
4062 {
4063         struct wpa_ssid *ssid = wpa_s->current_ssid;
4064
4065         if (!wpa_s->p2p_in_provisioning) {
4066                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4067                            "provisioning not in progress");
4068                 return;
4069         }
4070
4071         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4072                 u8 go_dev_addr[ETH_ALEN];
4073                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4074                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4075                                           ssid->ssid_len);
4076                 /* Clear any stored provisioning info */
4077                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4078         }
4079
4080         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4081                              NULL);
4082         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4083                 /*
4084                  * Use a separate timeout for initial data connection to
4085                  * complete to allow the group to be removed automatically if
4086                  * something goes wrong in this step before the P2P group idle
4087                  * timeout mechanism is taken into use.
4088                  */
4089                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4090                                        wpas_p2p_group_formation_timeout,
4091                                        wpa_s->parent, NULL);
4092         }
4093         if (wpa_s->global->p2p)
4094                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4095         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4096                 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4097         wpas_group_formation_completed(wpa_s, 1);
4098 }
4099
4100
4101 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4102                          struct wps_event_fail *fail)
4103 {
4104         if (!wpa_s->p2p_in_provisioning) {
4105                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4106                            "provisioning not in progress");
4107                 return;
4108         }
4109
4110         if (wpa_s->go_params) {
4111                 p2p_clear_provisioning_info(
4112                         wpa_s->global->p2p,
4113                         wpa_s->go_params->peer_device_addr);
4114         }
4115
4116         wpas_notify_p2p_wps_failed(wpa_s, fail);
4117 }
4118
4119
4120 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4121                        const char *config_method,
4122                        enum wpas_p2p_prov_disc_use use)
4123 {
4124         u16 config_methods;
4125
4126         wpa_s->p2p_fallback_to_go_neg = 0;
4127         wpa_s->pending_pd_use = NORMAL_PD;
4128         if (os_strncmp(config_method, "display", 7) == 0)
4129                 config_methods = WPS_CONFIG_DISPLAY;
4130         else if (os_strncmp(config_method, "keypad", 6) == 0)
4131                 config_methods = WPS_CONFIG_KEYPAD;
4132         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4133                  os_strncmp(config_method, "pushbutton", 10) == 0)
4134                 config_methods = WPS_CONFIG_PUSHBUTTON;
4135         else {
4136                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4137                 return -1;
4138         }
4139
4140         if (use == WPAS_P2P_PD_AUTO) {
4141                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4142                 wpa_s->pending_pd_config_methods = config_methods;
4143                 wpa_s->p2p_auto_pd = 1;
4144                 wpa_s->p2p_auto_join = 0;
4145                 wpa_s->pending_pd_before_join = 0;
4146                 wpa_s->auto_pd_scan_retry = 0;
4147                 wpas_p2p_stop_find(wpa_s);
4148                 wpa_s->p2p_join_scan_count = 0;
4149                 os_get_time(&wpa_s->p2p_auto_started);
4150                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4151                            wpa_s->p2p_auto_started.sec,
4152                            wpa_s->p2p_auto_started.usec);
4153                 wpas_p2p_join_scan(wpa_s, NULL);
4154                 return 0;
4155         }
4156
4157         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4158                 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4159                                                  config_methods,
4160                                                  use == WPAS_P2P_PD_FOR_JOIN);
4161         }
4162
4163         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4164                 return -1;
4165
4166         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4167                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4168                                  0);
4169 }
4170
4171
4172 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4173                               char *end)
4174 {
4175         return p2p_scan_result_text(ies, ies_len, buf, end);
4176 }
4177
4178
4179 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4180 {
4181         if (!wpa_s->pending_action_tx)
4182                 return;
4183
4184         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4185                    "operation request");
4186         wpabuf_free(wpa_s->pending_action_tx);
4187         wpa_s->pending_action_tx = NULL;
4188 }
4189
4190
4191 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4192                   enum p2p_discovery_type type,
4193                   unsigned int num_req_dev_types, const u8 *req_dev_types,
4194                   const u8 *dev_id, unsigned int search_delay)
4195 {
4196         wpas_p2p_clear_pending_action_tx(wpa_s);
4197         wpa_s->p2p_long_listen = 0;
4198
4199         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4200                 return wpa_drv_p2p_find(wpa_s, timeout, type);
4201
4202         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4203             wpa_s->p2p_in_provisioning)
4204                 return -1;
4205
4206         wpa_supplicant_cancel_sched_scan(wpa_s);
4207
4208         return p2p_find(wpa_s->global->p2p, timeout, type,
4209                         num_req_dev_types, req_dev_types, dev_id,
4210                         search_delay);
4211 }
4212
4213
4214 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4215 {
4216         wpas_p2p_clear_pending_action_tx(wpa_s);
4217         wpa_s->p2p_long_listen = 0;
4218         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4219         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4220         wpa_s->global->p2p_cb_on_scan_complete = 0;
4221
4222         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4223                 wpa_drv_p2p_stop_find(wpa_s);
4224                 return;
4225         }
4226
4227         if (wpa_s->global->p2p)
4228                 p2p_stop_find(wpa_s->global->p2p);
4229
4230         wpas_p2p_remove_pending_group_interface(wpa_s);
4231 }
4232
4233
4234 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4235 {
4236         struct wpa_supplicant *wpa_s = eloop_ctx;
4237         wpa_s->p2p_long_listen = 0;
4238 }
4239
4240
4241 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4242 {
4243         int res;
4244
4245         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4246                 return -1;
4247
4248         wpa_supplicant_cancel_sched_scan(wpa_s);
4249         wpas_p2p_clear_pending_action_tx(wpa_s);
4250
4251         if (timeout == 0) {
4252                 /*
4253                  * This is a request for unlimited Listen state. However, at
4254                  * least for now, this is mapped to a Listen state for one
4255                  * hour.
4256                  */
4257                 timeout = 3600;
4258         }
4259         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4260         wpa_s->p2p_long_listen = 0;
4261
4262         /*
4263          * Stop previous find/listen operation to avoid trying to request a new
4264          * remain-on-channel operation while the driver is still running the
4265          * previous one.
4266          */
4267         if (wpa_s->global->p2p)
4268                 p2p_stop_find(wpa_s->global->p2p);
4269
4270         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4271         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4272                 wpa_s->p2p_long_listen = timeout * 1000;
4273                 eloop_register_timeout(timeout, 0,
4274                                        wpas_p2p_long_listen_timeout,
4275                                        wpa_s, NULL);
4276         }
4277
4278         return res;
4279 }
4280
4281
4282 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4283                           u8 *buf, size_t len, int p2p_group)
4284 {
4285         struct wpabuf *p2p_ie;
4286         int ret;
4287
4288         if (wpa_s->global->p2p_disabled)
4289                 return -1;
4290         if (wpa_s->global->p2p == NULL)
4291                 return -1;
4292         if (bss == NULL)
4293                 return -1;
4294
4295         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4296         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4297                                p2p_group, p2p_ie);
4298         wpabuf_free(p2p_ie);
4299
4300         return ret;
4301 }
4302
4303
4304 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4305                           const u8 *dst, const u8 *bssid,
4306                           const u8 *ie, size_t ie_len, int ssi_signal)
4307 {
4308         if (wpa_s->global->p2p_disabled)
4309                 return 0;
4310         if (wpa_s->global->p2p == NULL)
4311                 return 0;
4312
4313         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4314                                  ie, ie_len)) {
4315         case P2P_PREQ_NOT_P2P:
4316                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4317                                  ssi_signal);
4318                 /* fall through */
4319         case P2P_PREQ_MALFORMED:
4320         case P2P_PREQ_NOT_LISTEN:
4321         case P2P_PREQ_NOT_PROCESSED:
4322         default: /* make gcc happy */
4323                 return 0;
4324         case P2P_PREQ_PROCESSED:
4325                 return 1;
4326         }
4327 }
4328
4329
4330 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4331                         const u8 *sa, const u8 *bssid,
4332                         u8 category, const u8 *data, size_t len, int freq)
4333 {
4334         if (wpa_s->global->p2p_disabled)
4335                 return;
4336         if (wpa_s->global->p2p == NULL)
4337                 return;
4338
4339         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4340                       freq);
4341 }
4342
4343
4344 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4345 {
4346         if (wpa_s->global->p2p_disabled)
4347                 return;
4348         if (wpa_s->global->p2p == NULL)
4349                 return;
4350
4351         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4352 }
4353
4354
4355 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4356 {
4357         p2p_group_deinit(wpa_s->p2p_group);
4358         wpa_s->p2p_group = NULL;
4359
4360         wpa_s->ap_configured_cb = NULL;
4361         wpa_s->ap_configured_cb_ctx = NULL;
4362         wpa_s->ap_configured_cb_data = NULL;
4363         wpa_s->connect_without_scan = NULL;
4364 }
4365
4366
4367 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4368 {
4369         wpa_s->p2p_long_listen = 0;
4370
4371         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4372                 return wpa_drv_p2p_reject(wpa_s, addr);
4373
4374         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4375                 return -1;
4376
4377         return p2p_reject(wpa_s->global->p2p, addr);
4378 }
4379
4380
4381 /* Invite to reinvoke a persistent group */
4382 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4383                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4384                     int ht40)
4385 {
4386         enum p2p_invite_role role;
4387         u8 *bssid = NULL;
4388
4389         wpa_s->p2p_persistent_go_freq = freq;
4390         wpa_s->p2p_go_ht40 = !!ht40;
4391         if (ssid->mode == WPAS_MODE_P2P_GO) {
4392                 role = P2P_INVITE_ROLE_GO;
4393                 if (peer_addr == NULL) {
4394                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4395                                    "address in invitation command");
4396                         return -1;
4397                 }
4398                 if (wpas_p2p_create_iface(wpa_s)) {
4399                         if (wpas_p2p_add_group_interface(wpa_s,
4400                                                          WPA_IF_P2P_GO) < 0) {
4401                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
4402                                            "allocate a new interface for the "
4403                                            "group");
4404                                 return -1;
4405                         }
4406                         bssid = wpa_s->pending_interface_addr;
4407                 } else
4408                         bssid = wpa_s->own_addr;
4409         } else {
4410                 role = P2P_INVITE_ROLE_CLIENT;
4411                 peer_addr = ssid->bssid;
4412         }
4413         wpa_s->pending_invite_ssid_id = ssid->id;
4414
4415         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4416                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4417                                           ssid->ssid, ssid->ssid_len,
4418                                           go_dev_addr, 1);
4419
4420         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4421                 return -1;
4422
4423         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4424                           ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4425 }
4426
4427
4428 /* Invite to join an active group */
4429 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4430                           const u8 *peer_addr, const u8 *go_dev_addr)
4431 {
4432         struct wpa_global *global = wpa_s->global;
4433         enum p2p_invite_role role;
4434         u8 *bssid = NULL;
4435         struct wpa_ssid *ssid;
4436         int persistent;
4437
4438         wpa_s->p2p_persistent_go_freq = 0;
4439         wpa_s->p2p_go_ht40 = 0;
4440
4441         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4442                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4443                         break;
4444         }
4445         if (wpa_s == NULL) {
4446                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4447                 return -1;
4448         }
4449
4450         ssid = wpa_s->current_ssid;
4451         if (ssid == NULL) {
4452                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4453                            "invitation");
4454                 return -1;
4455         }
4456
4457         persistent = ssid->p2p_persistent_group &&
4458                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4459                                         ssid->ssid, ssid->ssid_len);
4460
4461         if (ssid->mode == WPAS_MODE_P2P_GO) {
4462                 role = P2P_INVITE_ROLE_ACTIVE_GO;
4463                 bssid = wpa_s->own_addr;
4464                 if (go_dev_addr == NULL)
4465                         go_dev_addr = wpa_s->global->p2p_dev_addr;
4466         } else {
4467                 role = P2P_INVITE_ROLE_CLIENT;
4468                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4469                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4470                                    "invite to current group");
4471                         return -1;
4472                 }
4473                 bssid = wpa_s->bssid;
4474                 if (go_dev_addr == NULL &&
4475                     !is_zero_ether_addr(wpa_s->go_dev_addr))
4476                         go_dev_addr = wpa_s->go_dev_addr;
4477         }
4478         wpa_s->parent->pending_invite_ssid_id = -1;
4479
4480         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4481                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4482                                           ssid->ssid, ssid->ssid_len,
4483                                           go_dev_addr, persistent);
4484
4485         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4486                 return -1;
4487
4488         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4489                           ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4490                           go_dev_addr, persistent);
4491 }
4492
4493
4494 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4495 {
4496         struct wpa_ssid *ssid = wpa_s->current_ssid;
4497         const char *ssid_txt;
4498         u8 go_dev_addr[ETH_ALEN];
4499         int network_id = -1;
4500         int persistent;
4501         int freq;
4502
4503         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4504                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4505                                      wpa_s->parent, NULL);
4506         }
4507
4508         if (!wpa_s->show_group_started || !ssid)
4509                 goto done;
4510
4511         wpa_s->show_group_started = 0;
4512
4513         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4514         os_memset(go_dev_addr, 0, ETH_ALEN);
4515         if (ssid->bssid_set)
4516                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4517         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4518                                                ssid->ssid_len);
4519         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4520
4521         if (wpa_s->global->p2p_group_formation == wpa_s)
4522                 wpa_s->global->p2p_group_formation = NULL;
4523
4524         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4525                 (int) wpa_s->assoc_freq;
4526         if (ssid->passphrase == NULL && ssid->psk_set) {
4527                 char psk[65];
4528                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4529                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4530                         "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4531                         MACSTR "%s",
4532                         wpa_s->ifname, ssid_txt, freq, psk,
4533                         MAC2STR(go_dev_addr),
4534                         persistent ? " [PERSISTENT]" : "");
4535         } else {
4536                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4537                         "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4538                         "go_dev_addr=" MACSTR "%s",
4539                         wpa_s->ifname, ssid_txt, freq,
4540                         ssid->passphrase ? ssid->passphrase : "",
4541                         MAC2STR(go_dev_addr),
4542                         persistent ? " [PERSISTENT]" : "");
4543         }
4544
4545         if (persistent)
4546                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4547                                                              ssid, go_dev_addr);
4548         if (network_id < 0)
4549                 network_id = ssid->id;
4550         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4551
4552 done:
4553         if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4554             wpa_s->global->p2p != NULL) {
4555                 wpa_s->global->p2p_cb_on_scan_complete = 0;
4556                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4557                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4558                                 "continued after successful connection");
4559                         p2p_increase_search_delay(
4560                                 wpa_s->global->p2p,
4561                                 wpas_p2p_search_delay(wpa_s));
4562                 }
4563         }
4564 }
4565
4566
4567 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4568                           u32 interval1, u32 duration2, u32 interval2)
4569 {
4570         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4571                 return -1;
4572         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4573                 return -1;
4574
4575         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4576             wpa_s->current_ssid == NULL ||
4577             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4578                 return -1;
4579
4580         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4581                                 wpa_s->own_addr, wpa_s->assoc_freq,
4582                                 duration1, interval1, duration2, interval2);
4583 }
4584
4585
4586 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4587                         unsigned int interval)
4588 {
4589         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4590                 return -1;
4591
4592         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4593                 return -1;
4594
4595         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4596 }
4597
4598
4599 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4600 {
4601         if (wpa_s->current_ssid == NULL) {
4602                 /*
4603                  * current_ssid can be clearead when P2P client interface gets
4604                  * disconnected, so assume this interface was used as P2P
4605                  * client.
4606                  */
4607                 return 1;
4608         }
4609         return wpa_s->current_ssid->p2p_group &&
4610                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4611 }
4612
4613
4614 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4615 {
4616         struct wpa_supplicant *wpa_s = eloop_ctx;
4617
4618         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4619                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4620                            "disabled");
4621                 return;
4622         }
4623
4624         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4625                    "group");
4626         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4627 }
4628
4629
4630 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4631 {
4632         int timeout;
4633
4634         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4635                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4636
4637         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4638                 return;
4639
4640         timeout = wpa_s->conf->p2p_group_idle;
4641         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4642             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4643             timeout = P2P_MAX_CLIENT_IDLE;
4644
4645         if (timeout == 0)
4646                 return;
4647
4648         if (timeout < 0) {
4649                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4650                         timeout = 0; /* special client mode no-timeout */
4651                 else
4652                         return;
4653         }
4654
4655         if (wpa_s->p2p_in_provisioning) {
4656                 /*
4657                  * Use the normal group formation timeout during the
4658                  * provisioning phase to avoid terminating this process too
4659                  * early due to group idle timeout.
4660                  */
4661                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4662                            "during provisioning");
4663                 return;
4664         }
4665
4666         if (wpa_s->show_group_started) {
4667                 /*
4668                  * Use the normal group formation timeout between the end of
4669                  * the provisioning phase and completion of 4-way handshake to
4670                  * avoid terminating this process too early due to group idle
4671                  * timeout.
4672                  */
4673                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4674                            "while waiting for initial 4-way handshake to "
4675                            "complete");
4676                 return;
4677         }
4678
4679         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4680                    timeout);
4681         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4682                                wpa_s, NULL);
4683 }
4684
4685
4686 /* Returns 1 if the interface was removed */
4687 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4688                           u16 reason_code, const u8 *ie, size_t ie_len,
4689                           int locally_generated)
4690 {
4691         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4692                 return 0;
4693         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4694                 return 0;
4695
4696         if (!locally_generated)
4697                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4698                                  ie_len);
4699
4700         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4701             wpa_s->current_ssid &&
4702             wpa_s->current_ssid->p2p_group &&
4703             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4704                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4705                            "session is ending");
4706                 if (wpas_p2p_group_delete(wpa_s,
4707                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4708                     > 0)
4709                         return 1;
4710         }
4711
4712         return 0;
4713 }
4714
4715
4716 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4717                              u16 reason_code, const u8 *ie, size_t ie_len,
4718                              int locally_generated)
4719 {
4720         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4721                 return;
4722         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4723                 return;
4724
4725         if (!locally_generated)
4726                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4727                                    ie_len);
4728 }
4729
4730
4731 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4732 {
4733         struct p2p_data *p2p = wpa_s->global->p2p;
4734
4735         if (p2p == NULL)
4736                 return;
4737
4738         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4739                 return;
4740
4741         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4742                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4743
4744         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4745                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4746
4747         if (wpa_s->wps &&
4748             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4749                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4750
4751         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4752                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4753
4754         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4755                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4756                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4757                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4758                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4759         }
4760
4761         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4762                 p2p_set_sec_dev_types(p2p,
4763                                       (void *) wpa_s->conf->sec_device_type,
4764                                       wpa_s->conf->num_sec_device_types);
4765
4766         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4767                 int i;
4768                 p2p_remove_wps_vendor_extensions(p2p);
4769                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4770                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4771                                 continue;
4772                         p2p_add_wps_vendor_extension(
4773                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
4774                 }
4775         }
4776
4777         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4778             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4779                 char country[3];
4780                 country[0] = wpa_s->conf->country[0];
4781                 country[1] = wpa_s->conf->country[1];
4782                 country[2] = 0x04;
4783                 p2p_set_country(p2p, country);
4784         }
4785
4786         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4787                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4788                                      wpa_s->conf->p2p_ssid_postfix ?
4789                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4790                                      0);
4791         }
4792
4793         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4794                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4795
4796         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4797                 u8 reg_class, channel;
4798                 int ret;
4799                 unsigned int r;
4800                 if (wpa_s->conf->p2p_listen_reg_class &&
4801                     wpa_s->conf->p2p_listen_channel) {
4802                         reg_class = wpa_s->conf->p2p_listen_reg_class;
4803                         channel = wpa_s->conf->p2p_listen_channel;
4804                 } else {
4805                         reg_class = 81;
4806                         /*
4807                          * Pick one of the social channels randomly as the
4808                          * listen channel.
4809                          */
4810                         os_get_random((u8 *) &r, sizeof(r));
4811                         channel = 1 + (r % 3) * 5;
4812                 }
4813                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4814                 if (ret)
4815                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4816                                    "failed: %d", ret);
4817         }
4818         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4819                 u8 op_reg_class, op_channel, cfg_op_channel;
4820                 int ret = 0;
4821                 unsigned int r;
4822                 if (wpa_s->conf->p2p_oper_reg_class &&
4823                     wpa_s->conf->p2p_oper_channel) {
4824                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4825                         op_channel = wpa_s->conf->p2p_oper_channel;
4826                         cfg_op_channel = 1;
4827                 } else {
4828                         op_reg_class = 81;
4829                         /*
4830                          * Use random operation channel from (1, 6, 11)
4831                          *if no other preference is indicated.
4832                          */
4833                         os_get_random((u8 *) &r, sizeof(r));
4834                         op_channel = 1 + (r % 3) * 5;
4835                         cfg_op_channel = 0;
4836                 }
4837                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4838                                            cfg_op_channel);
4839                 if (ret)
4840                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4841                                    "failed: %d", ret);
4842         }
4843
4844         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4845                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4846                                       wpa_s->conf->p2p_pref_chan) < 0) {
4847                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4848                                    "update failed");
4849                 }
4850         }
4851 }
4852
4853
4854 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4855                      int duration)
4856 {
4857         if (!wpa_s->ap_iface)
4858                 return -1;
4859         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4860                                    duration);
4861 }
4862
4863
4864 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4865 {
4866         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4867                 return -1;
4868         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4869                 return -1;
4870
4871         wpa_s->global->cross_connection = enabled;
4872         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4873
4874         if (!enabled) {
4875                 struct wpa_supplicant *iface;
4876
4877                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4878                 {
4879                         if (iface->cross_connect_enabled == 0)
4880                                 continue;
4881
4882                         iface->cross_connect_enabled = 0;
4883                         iface->cross_connect_in_use = 0;
4884                         wpa_msg(iface->parent, MSG_INFO,
4885                                 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4886                                 iface->ifname, iface->cross_connect_uplink);
4887                 }
4888         }
4889
4890         return 0;
4891 }
4892
4893
4894 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4895 {
4896         struct wpa_supplicant *iface;
4897
4898         if (!uplink->global->cross_connection)
4899                 return;
4900
4901         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4902                 if (!iface->cross_connect_enabled)
4903                         continue;
4904                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4905                     0)
4906                         continue;
4907                 if (iface->ap_iface == NULL)
4908                         continue;
4909                 if (iface->cross_connect_in_use)
4910                         continue;
4911
4912                 iface->cross_connect_in_use = 1;
4913                 wpa_msg(iface->parent, MSG_INFO,
4914                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4915                         iface->ifname, iface->cross_connect_uplink);
4916         }
4917 }
4918
4919
4920 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4921 {
4922         struct wpa_supplicant *iface;
4923
4924         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4925                 if (!iface->cross_connect_enabled)
4926                         continue;
4927                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4928                     0)
4929                         continue;
4930                 if (!iface->cross_connect_in_use)
4931                         continue;
4932
4933                 wpa_msg(iface->parent, MSG_INFO,
4934                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4935                         iface->ifname, iface->cross_connect_uplink);
4936                 iface->cross_connect_in_use = 0;
4937         }
4938 }
4939
4940
4941 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4942 {
4943         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4944             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4945             wpa_s->cross_connect_disallowed)
4946                 wpas_p2p_disable_cross_connect(wpa_s);
4947         else
4948                 wpas_p2p_enable_cross_connect(wpa_s);
4949         if (!wpa_s->ap_iface &&
4950             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4951                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4952 }
4953
4954
4955 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4956 {
4957         wpas_p2p_disable_cross_connect(wpa_s);
4958         if (!wpa_s->ap_iface &&
4959             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4960                                          wpa_s, NULL))
4961                 wpas_p2p_set_group_idle_timeout(wpa_s);
4962 }
4963
4964
4965 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4966 {
4967         struct wpa_supplicant *iface;
4968
4969         if (!wpa_s->global->cross_connection)
4970                 return;
4971
4972         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4973                 if (iface == wpa_s)
4974                         continue;
4975                 if (iface->drv_flags &
4976                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4977                         continue;
4978                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4979                         continue;
4980
4981                 wpa_s->cross_connect_enabled = 1;
4982                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4983                            sizeof(wpa_s->cross_connect_uplink));
4984                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4985                            "%s to %s whenever uplink is available",
4986                            wpa_s->ifname, wpa_s->cross_connect_uplink);
4987
4988                 if (iface->ap_iface || iface->current_ssid == NULL ||
4989                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
4990                     iface->cross_connect_disallowed ||
4991                     iface->wpa_state != WPA_COMPLETED)
4992                         break;
4993
4994                 wpa_s->cross_connect_in_use = 1;
4995                 wpa_msg(wpa_s->parent, MSG_INFO,
4996                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4997                         wpa_s->ifname, wpa_s->cross_connect_uplink);
4998                 break;
4999         }
5000 }
5001
5002
5003 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5004 {
5005         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5006             !wpa_s->p2p_in_provisioning)
5007                 return 0; /* not P2P client operation */
5008
5009         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5010                    "session overlap");
5011         if (wpa_s != wpa_s->parent)
5012                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5013
5014         if (wpa_s->global->p2p)
5015                 p2p_group_formation_failed(wpa_s->global->p2p);
5016
5017         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5018                              wpa_s->parent, NULL);
5019
5020         wpas_group_formation_completed(wpa_s, 0);
5021         return 1;
5022 }
5023
5024
5025 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5026 {
5027         struct p2p_channels chan;
5028
5029         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5030                 return;
5031
5032         os_memset(&chan, 0, sizeof(chan));
5033         if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5034                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5035                            "channel list");
5036                 return;
5037         }
5038
5039         p2p_update_channel_list(wpa_s->global->p2p, &chan);
5040 }
5041
5042
5043 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5044 {
5045         struct wpa_global *global = wpa_s->global;
5046         int found = 0;
5047         const u8 *peer;
5048
5049         if (global->p2p == NULL)
5050                 return -1;
5051
5052         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5053
5054         if (wpa_s->pending_interface_name[0] &&
5055             !is_zero_ether_addr(wpa_s->pending_interface_addr))
5056                 found = 1;
5057
5058         peer = p2p_get_go_neg_peer(global->p2p);
5059         if (peer) {
5060                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5061                            MACSTR, MAC2STR(peer));
5062                 p2p_unauthorize(global->p2p, peer);
5063                 found = 1;
5064         }
5065
5066         wpas_p2p_stop_find(wpa_s);
5067
5068         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5069                 if (wpa_s == global->p2p_group_formation &&
5070                     (wpa_s->p2p_in_provisioning ||
5071                      wpa_s->parent->pending_interface_type ==
5072                      WPA_IF_P2P_CLIENT)) {
5073                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5074                                    "formation found - cancelling",
5075                                    wpa_s->ifname);
5076                         found = 1;
5077                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5078                                              wpa_s->parent, NULL);
5079                         if (wpa_s->p2p_in_provisioning) {
5080                                 wpas_group_formation_completed(wpa_s, 0);
5081                                 break;
5082                         }
5083                         wpas_p2p_group_delete(wpa_s,
5084                                               P2P_GROUP_REMOVAL_REQUESTED);
5085                         break;
5086                 }
5087         }
5088
5089         if (!found) {
5090                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5091                 return -1;
5092         }
5093
5094         return 0;
5095 }
5096
5097
5098 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5099 {
5100         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5101                 return;
5102
5103         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5104                    "being available anymore");
5105         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5106 }
5107
5108
5109 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5110                                    int freq_24, int freq_5, int freq_overall)
5111 {
5112         struct p2p_data *p2p = wpa_s->global->p2p;
5113         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5114                 return;
5115         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5116 }
5117
5118
5119 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5120 {
5121         u8 peer[ETH_ALEN];
5122         struct p2p_data *p2p = wpa_s->global->p2p;
5123
5124         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5125                 return -1;
5126
5127         if (hwaddr_aton(addr, peer))
5128                 return -1;
5129
5130         return p2p_unauthorize(p2p, peer);
5131 }
5132
5133
5134 /**
5135  * wpas_p2p_disconnect - Disconnect from a P2P Group
5136  * @wpa_s: Pointer to wpa_supplicant data
5137  * Returns: 0 on success, -1 on failure
5138  *
5139  * This can be used to disconnect from a group in which the local end is a P2P
5140  * Client or to end a P2P Group in case the local end is the Group Owner. If a
5141  * virtual network interface was created for this group, that interface will be
5142  * removed. Otherwise, only the configured P2P group network will be removed
5143  * from the interface.
5144  */
5145 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5146 {
5147
5148         if (wpa_s == NULL)
5149                 return -1;
5150
5151         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5152                 -1 : 0;
5153 }
5154
5155
5156 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5157 {
5158         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5159                 return 0;
5160
5161         return p2p_in_progress(wpa_s->global->p2p);
5162 }
5163
5164
5165 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5166                               struct wpa_ssid *ssid)
5167 {
5168         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5169             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5170                                  wpa_s->parent, NULL) > 0) {
5171                 /**
5172                  * Remove the network by scheduling the group formation
5173                  * timeout to happen immediately. The teardown code
5174                  * needs to be scheduled to run asynch later so that we
5175                  * don't delete data from under ourselves unexpectedly.
5176                  * Calling wpas_p2p_group_formation_timeout directly
5177                  * causes a series of crashes in WPS failure scenarios.
5178                  */
5179                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5180                            "P2P group network getting removed");
5181                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5182                                        wpa_s->parent, NULL);
5183         }
5184 }
5185
5186
5187 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5188                                           const u8 *addr, const u8 *ssid,
5189                                           size_t ssid_len)
5190 {
5191         struct wpa_ssid *s;
5192         size_t i;
5193
5194         for (s = wpa_s->conf->ssid; s; s = s->next) {
5195                 if (s->disabled != 2)
5196                         continue;
5197                 if (ssid &&
5198                     (ssid_len != s->ssid_len ||
5199                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
5200                         continue;
5201                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5202                         return s; /* peer is GO in the persistent group */
5203                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5204                         continue;
5205                 for (i = 0; i < s->num_p2p_clients; i++) {
5206                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5207                                       addr, ETH_ALEN) == 0)
5208                                 return s; /* peer is P2P client in persistent
5209                                            * group */
5210                 }
5211         }
5212
5213         return NULL;
5214 }
5215
5216
5217 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5218                                        const u8 *addr)
5219 {
5220         if (addr == NULL)
5221                 return;
5222         wpas_p2p_add_persistent_group_client(wpa_s, addr);
5223 }
5224
5225
5226 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5227                                         int group_added)
5228 {
5229         struct wpa_supplicant *group = wpa_s;
5230         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5231         if (wpa_s->global->p2p_group_formation)
5232                 group = wpa_s->global->p2p_group_formation;
5233         wpa_s = wpa_s->parent;
5234         offchannel_send_action_done(wpa_s);
5235         if (group_added)
5236                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5237         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5238         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5239                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5240                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5241                          wpa_s->p2p_persistent_id,
5242                          wpa_s->p2p_pd_before_go_neg,
5243                          wpa_s->p2p_go_ht40);
5244 }
5245
5246
5247 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5248 {
5249         if (!wpa_s->p2p_fallback_to_go_neg ||
5250             wpa_s->p2p_in_provisioning <= 5)
5251                 return 0;
5252
5253         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5254                 return 0; /* peer operating as a GO */
5255
5256         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5257                 "fallback to GO Negotiation");
5258         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5259
5260         return 1;
5261 }
5262
5263
5264 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5265 {
5266         const char *rn, *rn2;
5267         struct wpa_supplicant *ifs;
5268
5269         if (wpa_s->wpa_state > WPA_SCANNING) {
5270                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5271                         "concurrent operation",
5272                         P2P_CONCURRENT_SEARCH_DELAY);
5273                 return P2P_CONCURRENT_SEARCH_DELAY;
5274         }
5275
5276         if (!wpa_s->driver->get_radio_name)
5277                 return 0;
5278         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5279         if (rn == NULL || rn[0] == '\0')
5280                 return 0;
5281
5282         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5283                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5284                         continue;
5285
5286                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5287                 if (!rn2 || os_strcmp(rn, rn2) != 0)
5288                         continue;
5289                 if (ifs->wpa_state > WPA_SCANNING) {
5290                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5291                                 "delay due to concurrent operation on "
5292                                 "interface %s",
5293                                 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5294                         return P2P_CONCURRENT_SEARCH_DELAY;
5295                 }
5296         }
5297
5298         return 0;
5299 }