P2P: Fix p2p_cancel processing during group formation
[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)
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);
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         wpas_p2p_group_add_persistent(wpa_s, ssid,
2322                                       ssid->mode == WPAS_MODE_P2P_GO,
2323                                       wpa_s->p2p_persistent_go_freq,
2324                                       wpa_s->p2p_go_ht40);
2325 }
2326
2327
2328 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2329                                     unsigned int freq)
2330 {
2331         unsigned int i;
2332
2333         if (global->p2p_disallow_freq == NULL)
2334                 return 0;
2335
2336         for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2337                 if (freq >= global->p2p_disallow_freq[i].min &&
2338                     freq <= global->p2p_disallow_freq[i].max)
2339                         return 1;
2340         }
2341
2342         return 0;
2343 }
2344
2345
2346 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2347 {
2348         reg->channel[reg->channels] = chan;
2349         reg->channels++;
2350 }
2351
2352
2353 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2354                                      struct p2p_channels *chan)
2355 {
2356         int i, cla = 0;
2357
2358         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2359                    "band");
2360
2361         /* Operating class 81 - 2.4 GHz band channels 1..13 */
2362         chan->reg_class[cla].reg_class = 81;
2363         chan->reg_class[cla].channels = 0;
2364         for (i = 0; i < 11; i++) {
2365                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2366                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2367         }
2368         if (chan->reg_class[cla].channels)
2369                 cla++;
2370
2371         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2372                    "band");
2373
2374         /* Operating class 115 - 5 GHz, channels 36-48 */
2375         chan->reg_class[cla].reg_class = 115;
2376         chan->reg_class[cla].channels = 0;
2377         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2378                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2379         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2380                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2381         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2382                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2383         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2384                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2385         if (chan->reg_class[cla].channels)
2386                 cla++;
2387
2388         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2389                    "band");
2390
2391         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2392         chan->reg_class[cla].reg_class = 124;
2393         chan->reg_class[cla].channels = 0;
2394         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2395                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2396         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2397                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2398         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2399                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2400         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2401                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2402         if (chan->reg_class[cla].channels)
2403                 cla++;
2404
2405         chan->reg_classes = cla;
2406         return 0;
2407 }
2408
2409
2410 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2411                                           u16 num_modes,
2412                                           enum hostapd_hw_mode mode)
2413 {
2414         u16 i;
2415
2416         for (i = 0; i < num_modes; i++) {
2417                 if (modes[i].mode == mode)
2418                         return &modes[i];
2419         }
2420
2421         return NULL;
2422 }
2423
2424
2425 static int has_channel(struct wpa_global *global,
2426                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
2427 {
2428         int i;
2429         unsigned int freq;
2430
2431         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2432                 chan * 5;
2433         if (wpas_p2p_disallowed_freq(global, freq))
2434                 return 0;
2435
2436         for (i = 0; i < mode->num_channels; i++) {
2437                 if (mode->channels[i].chan == chan) {
2438                         if (flags)
2439                                 *flags = mode->channels[i].flag;
2440                         return !(mode->channels[i].flag &
2441                                  (HOSTAPD_CHAN_DISABLED |
2442                                   HOSTAPD_CHAN_PASSIVE_SCAN |
2443                                   HOSTAPD_CHAN_NO_IBSS |
2444                                   HOSTAPD_CHAN_RADAR));
2445                 }
2446         }
2447
2448         return 0;
2449 }
2450
2451
2452 struct p2p_oper_class_map {
2453         enum hostapd_hw_mode mode;
2454         u8 op_class;
2455         u8 min_chan;
2456         u8 max_chan;
2457         u8 inc;
2458         enum { BW20, BW40PLUS, BW40MINUS } bw;
2459 };
2460
2461 static struct p2p_oper_class_map op_class[] = {
2462         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2463 #if 0 /* Do not enable HT40 on 2 GHz for now */
2464         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2465         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2466 #endif
2467         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2468         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2469         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2470         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2471         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2472         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2473         { -1, 0, 0, 0, 0, BW20 }
2474 };
2475
2476
2477 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2478                                    struct hostapd_hw_modes *mode,
2479                                    u8 channel, u8 bw)
2480 {
2481         int flag;
2482
2483         if (!has_channel(wpa_s->global, mode, channel, &flag))
2484                 return -1;
2485         if (bw == BW40MINUS &&
2486             (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2487              !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2488                 return 0;
2489         if (bw == BW40PLUS &&
2490             (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2491              !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2492                 return 0;
2493         return 1;
2494 }
2495
2496
2497 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2498                                    struct p2p_channels *chan)
2499 {
2500         struct hostapd_hw_modes *mode;
2501         int cla, op;
2502
2503         if (wpa_s->hw.modes == NULL) {
2504                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2505                            "of all supported channels; assume dualband "
2506                            "support");
2507                 return wpas_p2p_default_channels(wpa_s, chan);
2508         }
2509
2510         cla = 0;
2511
2512         for (op = 0; op_class[op].op_class; op++) {
2513                 struct p2p_oper_class_map *o = &op_class[op];
2514                 u8 ch;
2515                 struct p2p_reg_class *reg = NULL;
2516
2517                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2518                 if (mode == NULL)
2519                         continue;
2520                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2521                         if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2522                                 continue;
2523                         if (reg == NULL) {
2524                                 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2525                                            "class %u", o->op_class);
2526                                 reg = &chan->reg_class[cla];
2527                                 cla++;
2528                                 reg->reg_class = o->op_class;
2529                         }
2530                         reg->channel[reg->channels] = ch;
2531                         reg->channels++;
2532                 }
2533                 if (reg) {
2534                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2535                                     reg->channel, reg->channels);
2536                 }
2537         }
2538
2539         chan->reg_classes = cla;
2540
2541         return 0;
2542 }
2543
2544
2545 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2546                            struct hostapd_hw_modes *mode, u8 channel)
2547 {
2548         int op, ret;
2549
2550         for (op = 0; op_class[op].op_class; op++) {
2551                 struct p2p_oper_class_map *o = &op_class[op];
2552                 u8 ch;
2553
2554                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2555                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2556                             o->bw == BW20 || ch != channel)
2557                                 continue;
2558                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2559                         if (ret < 0)
2560                                 continue;
2561                         else if (ret > 0)
2562                                 return (o->bw == BW40MINUS) ? -1 : 1;
2563                         else
2564                                 return 0;
2565                 }
2566         }
2567         return 0;
2568 }
2569
2570
2571 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2572                         size_t buf_len)
2573 {
2574         struct wpa_supplicant *wpa_s = ctx;
2575
2576         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2577                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2578                         break;
2579         }
2580         if (wpa_s == NULL)
2581                 return -1;
2582
2583         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2584 }
2585
2586
2587 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2588 {
2589         struct wpa_supplicant *wpa_s = ctx;
2590
2591         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2592                 struct wpa_ssid *ssid = wpa_s->current_ssid;
2593                 if (ssid == NULL)
2594                         continue;
2595                 if (ssid->mode != WPAS_MODE_INFRA)
2596                         continue;
2597                 if (wpa_s->wpa_state != WPA_COMPLETED &&
2598                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2599                         continue;
2600                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2601                         return 1;
2602         }
2603
2604         return 0;
2605 }
2606
2607
2608 /**
2609  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2610  * @global: Pointer to global data from wpa_supplicant_init()
2611  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2612  * Returns: 0 on success, -1 on failure
2613  */
2614 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2615 {
2616         struct p2p_config p2p;
2617         unsigned int r;
2618         int i;
2619
2620         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2621                 return 0;
2622
2623         if (global->p2p)
2624                 return 0;
2625
2626         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2627                 struct p2p_params params;
2628
2629                 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2630                 os_memset(&params, 0, sizeof(params));
2631                 params.dev_name = wpa_s->conf->device_name;
2632                 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2633                           WPS_DEV_TYPE_LEN);
2634                 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2635                 os_memcpy(params.sec_dev_type,
2636                           wpa_s->conf->sec_device_type,
2637                           params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2638
2639                 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2640                         return -1;
2641
2642                 return 0;
2643         }
2644
2645         os_memset(&p2p, 0, sizeof(p2p));
2646         p2p.msg_ctx = wpa_s;
2647         p2p.cb_ctx = wpa_s;
2648         p2p.p2p_scan = wpas_p2p_scan;
2649         p2p.send_action = wpas_send_action;
2650         p2p.send_action_done = wpas_send_action_done;
2651         p2p.go_neg_completed = wpas_go_neg_completed;
2652         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2653         p2p.dev_found = wpas_dev_found;
2654         p2p.dev_lost = wpas_dev_lost;
2655         p2p.start_listen = wpas_start_listen;
2656         p2p.stop_listen = wpas_stop_listen;
2657         p2p.send_probe_resp = wpas_send_probe_resp;
2658         p2p.sd_request = wpas_sd_request;
2659         p2p.sd_response = wpas_sd_response;
2660         p2p.prov_disc_req = wpas_prov_disc_req;
2661         p2p.prov_disc_resp = wpas_prov_disc_resp;
2662         p2p.prov_disc_fail = wpas_prov_disc_fail;
2663         p2p.invitation_process = wpas_invitation_process;
2664         p2p.invitation_received = wpas_invitation_received;
2665         p2p.invitation_result = wpas_invitation_result;
2666         p2p.get_noa = wpas_get_noa;
2667         p2p.go_connected = wpas_go_connected;
2668
2669         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2670         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2671         p2p.dev_name = wpa_s->conf->device_name;
2672         p2p.manufacturer = wpa_s->conf->manufacturer;
2673         p2p.model_name = wpa_s->conf->model_name;
2674         p2p.model_number = wpa_s->conf->model_number;
2675         p2p.serial_number = wpa_s->conf->serial_number;
2676         if (wpa_s->wps) {
2677                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2678                 p2p.config_methods = wpa_s->wps->config_methods;
2679         }
2680
2681         if (wpa_s->conf->p2p_listen_reg_class &&
2682             wpa_s->conf->p2p_listen_channel) {
2683                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2684                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2685         } else {
2686                 p2p.reg_class = 81;
2687                 /*
2688                  * Pick one of the social channels randomly as the listen
2689                  * channel.
2690                  */
2691                 os_get_random((u8 *) &r, sizeof(r));
2692                 p2p.channel = 1 + (r % 3) * 5;
2693         }
2694         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2695
2696         if (wpa_s->conf->p2p_oper_reg_class &&
2697             wpa_s->conf->p2p_oper_channel) {
2698                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2699                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2700                 p2p.cfg_op_channel = 1;
2701                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2702                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2703
2704         } else {
2705                 p2p.op_reg_class = 81;
2706                 /*
2707                  * Use random operation channel from (1, 6, 11) if no other
2708                  * preference is indicated.
2709                  */
2710                 os_get_random((u8 *) &r, sizeof(r));
2711                 p2p.op_channel = 1 + (r % 3) * 5;
2712                 p2p.cfg_op_channel = 0;
2713                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2714                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2715         }
2716         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2717                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2718                 p2p.country[2] = 0x04;
2719         } else
2720                 os_memcpy(p2p.country, "XX\x04", 3);
2721
2722         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2723                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2724                            "channel list");
2725                 return -1;
2726         }
2727
2728         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2729                   WPS_DEV_TYPE_LEN);
2730
2731         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2732         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2733                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2734
2735         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2736                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2737
2738         p2p.max_peers = 100;
2739
2740         if (wpa_s->conf->p2p_ssid_postfix) {
2741                 p2p.ssid_postfix_len =
2742                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2743                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2744                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2745                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2746                           p2p.ssid_postfix_len);
2747         }
2748
2749         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2750
2751         global->p2p = p2p_init(&p2p);
2752         if (global->p2p == NULL)
2753                 return -1;
2754         global->p2p_init_wpa_s = wpa_s;
2755
2756         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2757                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2758                         continue;
2759                 p2p_add_wps_vendor_extension(
2760                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2761         }
2762
2763         return 0;
2764 }
2765
2766
2767 /**
2768  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2769  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2770  *
2771  * This function deinitialize per-interface P2P data.
2772  */
2773 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2774 {
2775         if (wpa_s->driver && wpa_s->drv_priv)
2776                 wpa_drv_probe_req_report(wpa_s, 0);
2777
2778         if (wpa_s->go_params) {
2779                 /* Clear any stored provisioning info */
2780                 p2p_clear_provisioning_info(
2781                         wpa_s->global->p2p,
2782                         wpa_s->go_params->peer_device_addr);
2783         }
2784
2785         os_free(wpa_s->go_params);
2786         wpa_s->go_params = NULL;
2787         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2788         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2789         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2790         wpa_s->p2p_long_listen = 0;
2791         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2792         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2793         wpas_p2p_remove_pending_group_interface(wpa_s);
2794
2795         /* TODO: remove group interface from the driver if this wpa_s instance
2796          * is on top of a P2P group interface */
2797 }
2798
2799
2800 /**
2801  * wpas_p2p_deinit_global - Deinitialize global P2P module
2802  * @global: Pointer to global data from wpa_supplicant_init()
2803  *
2804  * This function deinitializes the global (per device) P2P module.
2805  */
2806 void wpas_p2p_deinit_global(struct wpa_global *global)
2807 {
2808         struct wpa_supplicant *wpa_s, *tmp;
2809
2810         wpa_s = global->ifaces;
2811         if (wpa_s)
2812                 wpas_p2p_service_flush(wpa_s);
2813
2814         if (global->p2p == NULL)
2815                 return;
2816
2817         /* Remove remaining P2P group interfaces */
2818         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2819                 wpa_s = wpa_s->next;
2820         while (wpa_s) {
2821                 tmp = global->ifaces;
2822                 while (tmp &&
2823                        (tmp == wpa_s ||
2824                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2825                         tmp = tmp->next;
2826                 }
2827                 if (tmp == NULL)
2828                         break;
2829                 /* Disconnect from the P2P group and deinit the interface */
2830                 wpas_p2p_disconnect(tmp);
2831         }
2832
2833         /*
2834          * Deinit GO data on any possibly remaining interface (if main
2835          * interface is used as GO).
2836          */
2837         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2838                 if (wpa_s->ap_iface)
2839                         wpas_p2p_group_deinit(wpa_s);
2840         }
2841
2842         p2p_deinit(global->p2p);
2843         global->p2p = NULL;
2844         global->p2p_init_wpa_s = NULL;
2845 }
2846
2847
2848 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2849 {
2850         if (wpa_s->drv_flags &
2851             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2852              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2853                 return 1; /* P2P group requires a new interface in every case
2854                            */
2855         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2856                 return 0; /* driver does not support concurrent operations */
2857         if (wpa_s->global->ifaces->next)
2858                 return 1; /* more that one interface already in use */
2859         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2860                 return 1; /* this interface is already in use */
2861         return 0;
2862 }
2863
2864
2865 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2866                                  const u8 *peer_addr,
2867                                  enum p2p_wps_method wps_method,
2868                                  int go_intent, const u8 *own_interface_addr,
2869                                  unsigned int force_freq, int persistent_group,
2870                                  struct wpa_ssid *ssid)
2871 {
2872         if (persistent_group && wpa_s->conf->persistent_reconnect)
2873                 persistent_group = 2;
2874
2875         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2876                 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2877                                            go_intent, own_interface_addr,
2878                                            force_freq, persistent_group);
2879         }
2880
2881         /*
2882          * Increase GO config timeout if HT40 is used since it takes some time
2883          * to scan channels for coex purposes before the BSS can be started.
2884          */
2885         p2p_set_config_timeout(wpa_s->global->p2p,
2886                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2887
2888         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2889                            go_intent, own_interface_addr, force_freq,
2890                            persistent_group, ssid ? ssid->ssid : NULL,
2891                            ssid ? ssid->ssid_len : 0,
2892                            wpa_s->p2p_pd_before_go_neg);
2893 }
2894
2895
2896 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2897                                 const u8 *peer_addr,
2898                                 enum p2p_wps_method wps_method,
2899                                 int go_intent, const u8 *own_interface_addr,
2900                                 unsigned int force_freq, int persistent_group,
2901                                 struct wpa_ssid *ssid)
2902 {
2903         if (persistent_group && wpa_s->conf->persistent_reconnect)
2904                 persistent_group = 2;
2905
2906         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2907                 return -1;
2908
2909         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2910                              go_intent, own_interface_addr, force_freq,
2911                              persistent_group, ssid ? ssid->ssid : NULL,
2912                              ssid ? ssid->ssid_len : 0);
2913 }
2914
2915
2916 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2917 {
2918         wpa_s->p2p_join_scan_count++;
2919         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2920                    wpa_s->p2p_join_scan_count);
2921         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2922                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2923                            " for join operationg - stop join attempt",
2924                            MAC2STR(wpa_s->pending_join_iface_addr));
2925                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2926                 if (wpa_s->p2p_auto_pd) {
2927                         wpa_s->p2p_auto_pd = 0;
2928                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2929                                 " p2p_dev_addr=" MACSTR " status=N/A",
2930                                 MAC2STR(wpa_s->pending_join_dev_addr));
2931                         return;
2932                 }
2933                 wpa_msg(wpa_s->parent, MSG_INFO,
2934                         P2P_EVENT_GROUP_FORMATION_FAILURE);
2935         }
2936 }
2937
2938
2939 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2940 {
2941         struct wpa_supplicant *wpa_s = eloop_ctx;
2942         if (!wpa_s->pending_pd_before_join)
2943                 return;
2944         /*
2945          * Provision Discovery Response may have been lost - try to connect
2946          * anyway since we do not need any information from this PD.
2947          */
2948         wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2949                    "try to connect anyway");
2950         wpas_p2p_join_start(wpa_s);
2951 }
2952
2953
2954 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2955 {
2956         struct wpa_supplicant *iface;
2957         int shared_freq;
2958         u8 bssid[ETH_ALEN];
2959
2960         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2961                 return 0;
2962
2963         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2964                 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2965                         continue;
2966                 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2967                         continue;
2968                 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2969                     iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2970                         shared_freq = iface->current_ssid->frequency;
2971                 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2972                         shared_freq = iface->assoc_freq;
2973                 else
2974                         shared_freq = 0;
2975
2976                 if (shared_freq && freq != shared_freq) {
2977                         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2978                                    "connected on %d MHz - new connection on "
2979                                    "%d MHz", iface->ifname, shared_freq, freq);
2980                         return 1;
2981                 }
2982         }
2983
2984         shared_freq = wpa_drv_shared_freq(wpa_s);
2985         if (shared_freq > 0 && shared_freq != freq) {
2986                 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2987                            "virtual interface connected on %d MHz - new "
2988                            "connection on %d MHz", shared_freq, freq);
2989                 return 1;
2990         }
2991
2992         return 0;
2993 }
2994
2995
2996 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2997                             const u8 *peer_dev_addr)
2998 {
2999         struct wpa_bss *bss;
3000         int updated;
3001
3002         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3003         if (bss == NULL)
3004                 return -1;
3005         if (bss->last_update_idx < wpa_s->bss_update_idx) {
3006                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3007                            "last scan");
3008                 return 0;
3009         }
3010
3011         updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3012         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3013                    "%ld.%06ld (%supdated in last scan)",
3014                    bss->last_update.sec, bss->last_update.usec,
3015                    updated ? "": "not ");
3016
3017         return updated;
3018 }
3019
3020
3021 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3022                                    struct wpa_scan_results *scan_res)
3023 {
3024         struct wpa_bss *bss;
3025         int freq;
3026         u8 iface_addr[ETH_ALEN];
3027
3028         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3029
3030         if (wpa_s->global->p2p_disabled)
3031                 return;
3032
3033         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3034                    scan_res ? (int) scan_res->num : -1,
3035                    wpa_s->p2p_auto_join ? "auto_" : "");
3036
3037         if (scan_res)
3038                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3039
3040         if (wpa_s->p2p_auto_pd) {
3041                 int join = wpas_p2p_peer_go(wpa_s,
3042                                             wpa_s->pending_join_dev_addr);
3043                 if (join == 0 &&
3044                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3045                         wpa_s->auto_pd_scan_retry++;
3046                         bss = wpa_bss_get_bssid(wpa_s,
3047                                                 wpa_s->pending_join_dev_addr);
3048                         if (bss) {
3049                                 freq = bss->freq;
3050                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3051                                            "the peer " MACSTR " at %d MHz",
3052                                            wpa_s->auto_pd_scan_retry,
3053                                            MAC2STR(wpa_s->
3054                                                    pending_join_dev_addr),
3055                                            freq);
3056                                 wpas_p2p_join_scan_req(wpa_s, freq);
3057                                 return;
3058                         }
3059                 }
3060
3061                 if (join < 0)
3062                         join = 0;
3063
3064                 wpa_s->p2p_auto_pd = 0;
3065                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3066                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3067                            MAC2STR(wpa_s->pending_join_dev_addr), join);
3068                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3069                                       wpa_s->pending_join_dev_addr,
3070                                       wpa_s->pending_pd_config_methods, join,
3071                                       0) < 0) {
3072                         wpa_s->p2p_auto_pd = 0;
3073                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3074                                 " p2p_dev_addr=" MACSTR " status=N/A",
3075                                 MAC2STR(wpa_s->pending_join_dev_addr));
3076                 }
3077                 return;
3078         }
3079
3080         if (wpa_s->p2p_auto_join) {
3081                 int join = wpas_p2p_peer_go(wpa_s,
3082                                             wpa_s->pending_join_dev_addr);
3083                 if (join < 0) {
3084                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3085                                    "running a GO -> use GO Negotiation");
3086                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3087                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3088                                          wpa_s->p2p_persistent_group, 0, 0, 0,
3089                                          wpa_s->p2p_go_intent,
3090                                          wpa_s->p2p_connect_freq,
3091                                          wpa_s->p2p_persistent_id,
3092                                          wpa_s->p2p_pd_before_go_neg,
3093                                          wpa_s->p2p_go_ht40);
3094                         return;
3095                 }
3096
3097                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3098                            "try to join the group", join ? "" :
3099                            " in older scan");
3100                 if (!join)
3101                         wpa_s->p2p_fallback_to_go_neg = 1;
3102         }
3103
3104         freq = p2p_get_oper_freq(wpa_s->global->p2p,
3105                                  wpa_s->pending_join_iface_addr);
3106         if (freq < 0 &&
3107             p2p_get_interface_addr(wpa_s->global->p2p,
3108                                    wpa_s->pending_join_dev_addr,
3109                                    iface_addr) == 0 &&
3110             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3111         {
3112                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3113                            "address for join from " MACSTR " to " MACSTR
3114                            " based on newly discovered P2P peer entry",
3115                            MAC2STR(wpa_s->pending_join_iface_addr),
3116                            MAC2STR(iface_addr));
3117                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3118                           ETH_ALEN);
3119
3120                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3121                                          wpa_s->pending_join_iface_addr);
3122         }
3123         if (freq >= 0) {
3124                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3125                            "from P2P peer table: %d MHz", freq);
3126         }
3127         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3128         if (bss) {
3129                 freq = bss->freq;
3130                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3131                            "from BSS table: %d MHz", freq);
3132         }
3133         if (freq > 0) {
3134                 u16 method;
3135
3136                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3137                         wpa_msg(wpa_s->parent, MSG_INFO,
3138                                 P2P_EVENT_GROUP_FORMATION_FAILURE
3139                                 "reason=FREQ_CONFLICT");
3140                         return;
3141                 }
3142
3143                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3144                            "prior to joining an existing group (GO " MACSTR
3145                            " freq=%u MHz)",
3146                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
3147                 wpa_s->pending_pd_before_join = 1;
3148
3149                 switch (wpa_s->pending_join_wps_method) {
3150                 case WPS_PIN_DISPLAY:
3151                         method = WPS_CONFIG_KEYPAD;
3152                         break;
3153                 case WPS_PIN_KEYPAD:
3154                         method = WPS_CONFIG_DISPLAY;
3155                         break;
3156                 case WPS_PBC:
3157                         method = WPS_CONFIG_PUSHBUTTON;
3158                         break;
3159                 default:
3160                         method = 0;
3161                         break;
3162                 }
3163
3164                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3165                                                wpa_s->pending_join_dev_addr) ==
3166                      method)) {
3167                         /*
3168                          * We have already performed provision discovery for
3169                          * joining the group. Proceed directly to join
3170                          * operation without duplicated provision discovery. */
3171                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3172                                    "with " MACSTR " already done - proceed to "
3173                                    "join",
3174                                    MAC2STR(wpa_s->pending_join_dev_addr));
3175                         wpa_s->pending_pd_before_join = 0;
3176                         goto start;
3177                 }
3178
3179                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3180                                       wpa_s->pending_join_dev_addr, method, 1,
3181                                       freq) < 0) {
3182                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3183                                    "Discovery Request before joining an "
3184                                    "existing group");
3185                         wpa_s->pending_pd_before_join = 0;
3186                         goto start;
3187                 }
3188
3189                 /*
3190                  * Actual join operation will be started from the Action frame
3191                  * TX status callback (if no ACK is received) or when the
3192                  * Provision Discovery Response is received. Use a short
3193                  * timeout as a backup mechanism should the Provision Discovery
3194                  * Response be lost for any reason.
3195                  */
3196                 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3197                                      NULL);
3198                 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3199                                        wpa_s, NULL);
3200                 return;
3201         }
3202
3203         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3204         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3205         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3206         wpas_p2p_check_join_scan_limit(wpa_s);
3207         return;
3208
3209 start:
3210         /* Start join operation immediately */
3211         wpas_p2p_join_start(wpa_s);
3212 }
3213
3214
3215 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3216 {
3217         int ret;
3218         struct wpa_driver_scan_params params;
3219         struct wpabuf *wps_ie, *ies;
3220         size_t ielen;
3221         int freqs[2] = { 0, 0 };
3222
3223         os_memset(&params, 0, sizeof(params));
3224
3225         /* P2P Wildcard SSID */
3226         params.num_ssids = 1;
3227         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3228         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3229
3230         wpa_s->wps->dev.p2p = 1;
3231         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3232                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3233                                         NULL);
3234         if (wps_ie == NULL) {
3235                 wpas_p2p_scan_res_join(wpa_s, NULL);
3236                 return;
3237         }
3238
3239         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3240         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3241         if (ies == NULL) {
3242                 wpabuf_free(wps_ie);
3243                 wpas_p2p_scan_res_join(wpa_s, NULL);
3244                 return;
3245         }
3246         wpabuf_put_buf(ies, wps_ie);
3247         wpabuf_free(wps_ie);
3248
3249         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3250
3251         params.p2p_probe = 1;
3252         params.extra_ies = wpabuf_head(ies);
3253         params.extra_ies_len = wpabuf_len(ies);
3254         if (freq > 0) {
3255                 freqs[0] = freq;
3256                 params.freqs = freqs;
3257         }
3258
3259         /*
3260          * Run a scan to update BSS table and start Provision Discovery once
3261          * the new scan results become available.
3262          */
3263         ret = wpa_drv_scan(wpa_s, &params);
3264         if (!ret)
3265                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3266
3267         wpabuf_free(ies);
3268
3269         if (ret) {
3270                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3271                            "try again later");
3272                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3273                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3274                 wpas_p2p_check_join_scan_limit(wpa_s);
3275         }
3276 }
3277
3278
3279 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3280 {
3281         struct wpa_supplicant *wpa_s = eloop_ctx;
3282         wpas_p2p_join_scan_req(wpa_s, 0);
3283 }
3284
3285
3286 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3287                          const u8 *dev_addr, enum p2p_wps_method wps_method,
3288                          int auto_join)
3289 {
3290         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3291                    MACSTR " dev " MACSTR ")%s",
3292                    MAC2STR(iface_addr), MAC2STR(dev_addr),
3293                    auto_join ? " (auto_join)" : "");
3294
3295         wpa_s->p2p_auto_pd = 0;
3296         wpa_s->p2p_auto_join = !!auto_join;
3297         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3298         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3299         wpa_s->pending_join_wps_method = wps_method;
3300
3301         /* Make sure we are not running find during connection establishment */
3302         wpas_p2p_stop_find(wpa_s);
3303
3304         wpa_s->p2p_join_scan_count = 0;
3305         wpas_p2p_join_scan(wpa_s, NULL);
3306         return 0;
3307 }
3308
3309
3310 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3311 {
3312         struct wpa_supplicant *group;
3313         struct p2p_go_neg_results res;
3314         struct wpa_bss *bss;
3315
3316         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3317         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3318         if (group == NULL)
3319                 return -1;
3320         if (group != wpa_s) {
3321                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3322                           sizeof(group->p2p_pin));
3323                 group->p2p_wps_method = wpa_s->p2p_wps_method;
3324         }
3325
3326         group->p2p_in_provisioning = 1;
3327         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3328
3329         os_memset(&res, 0, sizeof(res));
3330         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3331                   ETH_ALEN);
3332         res.wps_method = wpa_s->pending_join_wps_method;
3333         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3334         if (bss) {
3335                 res.freq = bss->freq;
3336                 res.ssid_len = bss->ssid_len;
3337                 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3338         }
3339
3340         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3341                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3342                            "starting client");
3343                 wpa_drv_cancel_remain_on_channel(wpa_s);
3344                 wpa_s->off_channel_freq = 0;
3345                 wpa_s->roc_waiting_drv_freq = 0;
3346         }
3347         wpas_start_wps_enrollee(group, &res);
3348
3349         /*
3350          * Allow a longer timeout for join-a-running-group than normal 15
3351          * second group formation timeout since the GO may not have authorized
3352          * our connection yet.
3353          */
3354         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3355         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3356                                wpa_s, NULL);
3357
3358         return 0;
3359 }
3360
3361
3362 /**
3363  * wpas_p2p_connect - Request P2P Group Formation to be started
3364  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3365  * @peer_addr: Address of the peer P2P Device
3366  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3367  * @persistent_group: Whether to create a persistent group
3368  * @auto_join: Whether to select join vs. GO Negotiation automatically
3369  * @join: Whether to join an existing group (as a client) instead of starting
3370  *      Group Owner negotiation; @peer_addr is BSSID in that case
3371  * @auth: Whether to only authorize the connection instead of doing that and
3372  *      initiating Group Owner negotiation
3373  * @go_intent: GO Intent or -1 to use default
3374  * @freq: Frequency for the group or 0 for auto-selection
3375  * @persistent_id: Persistent group credentials to use for forcing GO
3376  *      parameters or -1 to generate new values (SSID/passphrase)
3377  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3378  *      interoperability workaround when initiating group formation
3379  * @ht40: Start GO with 40 MHz channel width
3380  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3381  *      failure, -2 on failure due to channel not currently available,
3382  *      -3 if forced channel is not supported
3383  */
3384 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3385                      const char *pin, enum p2p_wps_method wps_method,
3386                      int persistent_group, int auto_join, int join, int auth,
3387                      int go_intent, int freq, int persistent_id, int pd,
3388                      int ht40)
3389 {
3390         int force_freq = 0, oper_freq = 0;
3391         u8 bssid[ETH_ALEN];
3392         int ret = 0;
3393         enum wpa_driver_if_type iftype;
3394         const u8 *if_addr;
3395         struct wpa_ssid *ssid = NULL;
3396
3397         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3398                 return -1;
3399
3400         if (persistent_id >= 0) {
3401                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3402                 if (ssid == NULL || ssid->disabled != 2 ||
3403                     ssid->mode != WPAS_MODE_P2P_GO)
3404                         return -1;
3405         }
3406
3407         if (go_intent < 0)
3408                 go_intent = wpa_s->conf->p2p_go_intent;
3409
3410         if (!auth)
3411                 wpa_s->p2p_long_listen = 0;
3412
3413         wpa_s->p2p_wps_method = wps_method;
3414         wpa_s->p2p_persistent_group = !!persistent_group;
3415         wpa_s->p2p_persistent_id = persistent_id;
3416         wpa_s->p2p_go_intent = go_intent;
3417         wpa_s->p2p_connect_freq = freq;
3418         wpa_s->p2p_fallback_to_go_neg = 0;
3419         wpa_s->p2p_pd_before_go_neg = !!pd;
3420         wpa_s->p2p_go_ht40 = !!ht40;
3421
3422         if (pin)
3423                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3424         else if (wps_method == WPS_PIN_DISPLAY) {
3425                 ret = wps_generate_pin();
3426                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3427                             ret);
3428                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3429                            wpa_s->p2p_pin);
3430         } else
3431                 wpa_s->p2p_pin[0] = '\0';
3432
3433         if (join || auto_join) {
3434                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3435                 if (auth) {
3436                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3437                                    "connect a running group from " MACSTR,
3438                                    MAC2STR(peer_addr));
3439                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3440                         return ret;
3441                 }
3442                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3443                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3444                                            iface_addr) < 0) {
3445                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3446                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3447                                          dev_addr);
3448                 }
3449                 if (auto_join) {
3450                         os_get_time(&wpa_s->p2p_auto_started);
3451                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3452                                    "%ld.%06ld",
3453                                    wpa_s->p2p_auto_started.sec,
3454                                    wpa_s->p2p_auto_started.usec);
3455                 }
3456                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3457                                   auto_join) < 0)
3458                         return -1;
3459                 return ret;
3460         }
3461
3462         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3463             wpa_s->assoc_freq)
3464                 oper_freq = wpa_s->assoc_freq;
3465         else {
3466                 oper_freq = wpa_drv_shared_freq(wpa_s);
3467                 if (oper_freq < 0)
3468                         oper_freq = 0;
3469         }
3470
3471         if (freq > 0) {
3472                 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3473                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3474                                    "(%u MHz) is not supported for P2P uses",
3475                                    freq);
3476                         return -3;
3477                 }
3478
3479                 if (oper_freq > 0 && freq != oper_freq &&
3480                     !(wpa_s->drv_flags &
3481                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3482                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3483                                    "on %u MHz while connected on another "
3484                                    "channel (%u MHz)", freq, oper_freq);
3485                         return -2;
3486                 }
3487                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3488                            "requested channel (%u MHz)", freq);
3489                 force_freq = freq;
3490         } else if (oper_freq > 0 &&
3491                    !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3492                 if (!(wpa_s->drv_flags &
3493                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3494                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3495                                    "while connected on non-P2P supported "
3496                                    "channel (%u MHz)", oper_freq);
3497                         return -2;
3498                 }
3499                 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3500                            "(%u MHz) not available for P2P - try to use "
3501                            "another channel", oper_freq);
3502                 force_freq = 0;
3503         } else if (oper_freq > 0) {
3504                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3505                            "channel we are already using (%u MHz) on another "
3506                            "interface", oper_freq);
3507                 force_freq = oper_freq;
3508         }
3509
3510         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3511
3512         if (wpa_s->create_p2p_iface) {
3513                 /* Prepare to add a new interface for the group */
3514                 iftype = WPA_IF_P2P_GROUP;
3515                 if (go_intent == 15)
3516                         iftype = WPA_IF_P2P_GO;
3517                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3518                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3519                                    "interface for the group");
3520                         return -1;
3521                 }
3522
3523                 if_addr = wpa_s->pending_interface_addr;
3524         } else
3525                 if_addr = wpa_s->own_addr;
3526
3527         if (auth) {
3528                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3529                                          go_intent, if_addr,
3530                                          force_freq, persistent_group, ssid) <
3531                     0)
3532                         return -1;
3533                 return ret;
3534         }
3535
3536         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3537                                   go_intent, if_addr, force_freq,
3538                                   persistent_group, ssid) < 0) {
3539                 if (wpa_s->create_p2p_iface)
3540                         wpas_p2p_remove_pending_group_interface(wpa_s);
3541                 return -1;
3542         }
3543         return ret;
3544 }
3545
3546
3547 /**
3548  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3549  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3550  * @freq: Frequency of the channel in MHz
3551  * @duration: Duration of the stay on the channel in milliseconds
3552  *
3553  * This callback is called when the driver indicates that it has started the
3554  * requested remain-on-channel duration.
3555  */
3556 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3557                                    unsigned int freq, unsigned int duration)
3558 {
3559         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3560                 return;
3561         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3562                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3563                               wpa_s->pending_listen_duration);
3564                 wpa_s->pending_listen_freq = 0;
3565         }
3566 }
3567
3568
3569 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3570                                  unsigned int timeout)
3571 {
3572         /* Limit maximum Listen state time based on driver limitation. */
3573         if (timeout > wpa_s->max_remain_on_chan)
3574                 timeout = wpa_s->max_remain_on_chan;
3575
3576         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3577                 return wpa_drv_p2p_listen(wpa_s, timeout);
3578
3579         return p2p_listen(wpa_s->global->p2p, timeout);
3580 }
3581
3582
3583 /**
3584  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3585  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3586  * @freq: Frequency of the channel in MHz
3587  *
3588  * This callback is called when the driver indicates that a remain-on-channel
3589  * operation has been completed, i.e., the duration on the requested channel
3590  * has timed out.
3591  */
3592 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3593                                           unsigned int freq)
3594 {
3595         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3596                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
3597                    wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3598         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3599                 return;
3600         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3601                 return; /* P2P module started a new operation */
3602         if (wpa_s->pending_action_tx)
3603                 return;
3604         if (wpa_s->p2p_long_listen > 0)
3605                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3606         if (wpa_s->p2p_long_listen > 0) {
3607                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3608                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3609         }
3610 }
3611
3612
3613 /**
3614  * wpas_p2p_group_remove - Remove a P2P group
3615  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3616  * @ifname: Network interface name of the group interface or "*" to remove all
3617  *      groups
3618  * Returns: 0 on success, -1 on failure
3619  *
3620  * This function is used to remove a P2P group. This can be used to disconnect
3621  * from a group in which the local end is a P2P Client or to end a P2P Group in
3622  * case the local end is the Group Owner. If a virtual network interface was
3623  * created for this group, that interface will be removed. Otherwise, only the
3624  * configured P2P group network will be removed from the interface.
3625  */
3626 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3627 {
3628         struct wpa_global *global = wpa_s->global;
3629
3630         if (os_strcmp(ifname, "*") == 0) {
3631                 struct wpa_supplicant *prev;
3632                 wpa_s = global->ifaces;
3633                 while (wpa_s) {
3634                         prev = wpa_s;
3635                         wpa_s = wpa_s->next;
3636                         wpas_p2p_disconnect(prev);
3637                 }
3638                 return 0;
3639         }
3640
3641         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3642                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3643                         break;
3644         }
3645
3646         return wpas_p2p_disconnect(wpa_s);
3647 }
3648
3649
3650 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3651                                    struct p2p_go_neg_results *params,
3652                                    int freq, int ht40)
3653 {
3654         u8 bssid[ETH_ALEN];
3655         int res;
3656
3657         os_memset(params, 0, sizeof(*params));
3658         params->role_go = 1;
3659         params->ht40 = ht40;
3660         if (freq) {
3661                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3662                            "frequency %d MHz", freq);
3663                 params->freq = freq;
3664         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3665                    wpa_s->conf->p2p_oper_channel >= 1 &&
3666                    wpa_s->conf->p2p_oper_channel <= 11) {
3667                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3668                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3669                            "frequency %d MHz", params->freq);
3670         } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3671                    wpa_s->conf->p2p_oper_reg_class == 124) {
3672                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3673                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3674                            "frequency %d MHz", params->freq);
3675         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3676                    wpa_s->best_overall_freq > 0 &&
3677                    p2p_supported_freq(wpa_s->global->p2p,
3678                                       wpa_s->best_overall_freq)) {
3679                 params->freq = wpa_s->best_overall_freq;
3680                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3681                            "channel %d MHz", params->freq);
3682         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3683                    wpa_s->best_24_freq > 0 &&
3684                    p2p_supported_freq(wpa_s->global->p2p,
3685                                       wpa_s->best_24_freq)) {
3686                 params->freq = wpa_s->best_24_freq;
3687                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3688                            "channel %d MHz", params->freq);
3689         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3690                    wpa_s->best_5_freq > 0 &&
3691                    p2p_supported_freq(wpa_s->global->p2p,
3692                                       wpa_s->best_5_freq)) {
3693                 params->freq = wpa_s->best_5_freq;
3694                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3695                            "channel %d MHz", params->freq);
3696         } else {
3697                 int chan;
3698                 for (chan = 0; chan < 11; chan++) {
3699                         params->freq = 2412 + chan * 5;
3700                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
3701                                                       params->freq))
3702                                 break;
3703                 }
3704                 if (chan == 11) {
3705                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3706                                    "allowed");
3707                         return -1;
3708                 }
3709                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3710                            "known)", params->freq);
3711         }
3712
3713         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3714             wpa_s->assoc_freq && !freq) {
3715                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3716                            "already using");
3717                 params->freq = wpa_s->assoc_freq;
3718         }
3719
3720         res = wpa_drv_shared_freq(wpa_s);
3721         if (res > 0 && !freq) {
3722                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3723                            "already using on a shared interface");
3724                 params->freq = res;
3725         } else if (res > 0 && freq != res &&
3726                    !(wpa_s->drv_flags &
3727                      WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3728                 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3729                            "while connected on another channel (%u MHz)",
3730                            freq, res);
3731                 return -1;
3732         }
3733
3734         return 0;
3735 }
3736
3737
3738 static struct wpa_supplicant *
3739 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3740                          int go)
3741 {
3742         struct wpa_supplicant *group_wpa_s;
3743
3744         if (!wpas_p2p_create_iface(wpa_s)) {
3745                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3746                         "operations");
3747                 return wpa_s;
3748         }
3749
3750         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3751                                          WPA_IF_P2P_CLIENT) < 0) {
3752                 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
3753                 return NULL;
3754         }
3755         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3756         if (group_wpa_s == NULL) {
3757                 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
3758                         "interface");
3759                 wpas_p2p_remove_pending_group_interface(wpa_s);
3760                 return NULL;
3761         }
3762
3763         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
3764                 group_wpa_s->ifname);
3765         return group_wpa_s;
3766 }
3767
3768
3769 /**
3770  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3771  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3772  * @persistent_group: Whether to create a persistent group
3773  * @freq: Frequency for the group or 0 to indicate no hardcoding
3774  * Returns: 0 on success, -1 on failure
3775  *
3776  * This function creates a new P2P group with the local end as the Group Owner,
3777  * i.e., without using Group Owner Negotiation.
3778  */
3779 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3780                        int freq, int ht40)
3781 {
3782         struct p2p_go_neg_results params;
3783         unsigned int r;
3784
3785         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3786                 return -1;
3787
3788         /* Make sure we are not running find during connection establishment */
3789         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3790         wpas_p2p_stop_find(wpa_s);
3791
3792         if (freq == 2) {
3793                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3794                            "band");
3795                 if (wpa_s->best_24_freq > 0 &&
3796                     p2p_supported_freq(wpa_s->global->p2p,
3797                                        wpa_s->best_24_freq)) {
3798                         freq = wpa_s->best_24_freq;
3799                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3800                                    "channel: %d MHz", freq);
3801                 } else {
3802                         os_get_random((u8 *) &r, sizeof(r));
3803                         freq = 2412 + (r % 3) * 25;
3804                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3805                                    "channel: %d MHz", freq);
3806                 }
3807         }
3808
3809         if (freq == 5) {
3810                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3811                            "band");
3812                 if (wpa_s->best_5_freq > 0 &&
3813                     p2p_supported_freq(wpa_s->global->p2p,
3814                                        wpa_s->best_5_freq)) {
3815                         freq = wpa_s->best_5_freq;
3816                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3817                                    "channel: %d MHz", freq);
3818                 } else {
3819                         os_get_random((u8 *) &r, sizeof(r));
3820                         freq = 5180 + (r % 4) * 20;
3821                         if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3822                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3823                                            "5 GHz channel for P2P group");
3824                                 return -1;
3825                         }
3826                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3827                                    "channel: %d MHz", freq);
3828                 }
3829         }
3830
3831         if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3832                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3833                            "(%u MHz) is not supported for P2P uses",
3834                            freq);
3835                 return -1;
3836         }
3837
3838         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3839                 return -1;
3840         if (params.freq &&
3841             !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3842                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3843                            "(%u MHz) is not supported for P2P uses",
3844                            params.freq);
3845                 return -1;
3846         }
3847         p2p_go_params(wpa_s->global->p2p, &params);
3848         params.persistent_group = persistent_group;
3849
3850         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3851         if (wpa_s == NULL)
3852                 return -1;
3853         wpas_start_wps_go(wpa_s, &params, 0);
3854
3855         return 0;
3856 }
3857
3858
3859 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3860                                  struct wpa_ssid *params, int addr_allocated)
3861 {
3862         struct wpa_ssid *ssid;
3863
3864         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3865         if (wpa_s == NULL)
3866                 return -1;
3867
3868         wpa_supplicant_ap_deinit(wpa_s);
3869
3870         ssid = wpa_config_add_network(wpa_s->conf);
3871         if (ssid == NULL)
3872                 return -1;
3873         wpa_config_set_network_defaults(ssid);
3874         ssid->temporary = 1;
3875         ssid->proto = WPA_PROTO_RSN;
3876         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3877         ssid->group_cipher = WPA_CIPHER_CCMP;
3878         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3879         ssid->ssid = os_malloc(params->ssid_len);
3880         if (ssid->ssid == NULL) {
3881                 wpa_config_remove_network(wpa_s->conf, ssid->id);
3882                 return -1;
3883         }
3884         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3885         ssid->ssid_len = params->ssid_len;
3886         ssid->p2p_group = 1;
3887         ssid->export_keys = 1;
3888         if (params->psk_set) {
3889                 os_memcpy(ssid->psk, params->psk, 32);
3890                 ssid->psk_set = 1;
3891         }
3892         if (params->passphrase)
3893                 ssid->passphrase = os_strdup(params->passphrase);
3894
3895         wpa_supplicant_select_network(wpa_s, ssid);
3896
3897         wpa_s->show_group_started = 1;
3898
3899         return 0;
3900 }
3901
3902
3903 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3904                                   struct wpa_ssid *ssid, int addr_allocated,
3905                                   int freq, int ht40)
3906 {
3907         struct p2p_go_neg_results params;
3908         int go = 0;
3909
3910         if (ssid->disabled != 2 || ssid->ssid == NULL)
3911                 return -1;
3912
3913         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3914             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3915                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3916                            "already running");
3917                 return 0;
3918         }
3919
3920         /* Make sure we are not running find during connection establishment */
3921         wpas_p2p_stop_find(wpa_s);
3922
3923         wpa_s->p2p_fallback_to_go_neg = 0;
3924
3925         if (ssid->mode == WPAS_MODE_INFRA)
3926                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3927
3928         if (ssid->mode != WPAS_MODE_P2P_GO)
3929                 return -1;
3930
3931         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3932                 return -1;
3933
3934         params.role_go = 1;
3935         if (ssid->passphrase == NULL ||
3936             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3937                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3938                            "group");
3939                 return -1;
3940         }
3941         os_strlcpy(params.passphrase, ssid->passphrase,
3942                    sizeof(params.passphrase));
3943         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3944         params.ssid_len = ssid->ssid_len;
3945         params.persistent_group = 1;
3946
3947         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3948         if (wpa_s == NULL)
3949                 return -1;
3950
3951         wpas_start_wps_go(wpa_s, &params, 0);
3952
3953         return 0;
3954 }
3955
3956
3957 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3958                                struct wpabuf *proberesp_ies)
3959 {
3960         struct wpa_supplicant *wpa_s = ctx;
3961         if (wpa_s->ap_iface) {
3962                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3963                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3964                         wpabuf_free(beacon_ies);
3965                         wpabuf_free(proberesp_ies);
3966                         return;
3967                 }
3968                 if (beacon_ies) {
3969                         wpabuf_free(hapd->p2p_beacon_ie);
3970                         hapd->p2p_beacon_ie = beacon_ies;
3971                 }
3972                 wpabuf_free(hapd->p2p_probe_resp_ie);
3973                 hapd->p2p_probe_resp_ie = proberesp_ies;
3974         } else {
3975                 wpabuf_free(beacon_ies);
3976                 wpabuf_free(proberesp_ies);
3977         }
3978         wpa_supplicant_ap_update_beacon(wpa_s);
3979 }
3980
3981
3982 static void wpas_p2p_idle_update(void *ctx, int idle)
3983 {
3984         struct wpa_supplicant *wpa_s = ctx;
3985         if (!wpa_s->ap_iface)
3986                 return;
3987         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3988         if (idle)
3989                 wpas_p2p_set_group_idle_timeout(wpa_s);
3990         else
3991                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3992 }
3993
3994
3995 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3996                                        struct wpa_ssid *ssid)
3997 {
3998         struct p2p_group *group;
3999         struct p2p_group_config *cfg;
4000
4001         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4002                 return NULL;
4003         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4004                 return NULL;
4005
4006         cfg = os_zalloc(sizeof(*cfg));
4007         if (cfg == NULL)
4008                 return NULL;
4009
4010         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4011                 cfg->persistent_group = 2;
4012         else if (ssid->p2p_persistent_group)
4013                 cfg->persistent_group = 1;
4014         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4015         if (wpa_s->max_stations &&
4016             wpa_s->max_stations < wpa_s->conf->max_num_sta)
4017                 cfg->max_clients = wpa_s->max_stations;
4018         else
4019                 cfg->max_clients = wpa_s->conf->max_num_sta;
4020         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4021         cfg->ssid_len = ssid->ssid_len;
4022         cfg->cb_ctx = wpa_s;
4023         cfg->ie_update = wpas_p2p_ie_update;
4024         cfg->idle_update = wpas_p2p_idle_update;
4025
4026         group = p2p_group_init(wpa_s->global->p2p, cfg);
4027         if (group == NULL)
4028                 os_free(cfg);
4029         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4030                 p2p_group_notif_formation_done(group);
4031         wpa_s->p2p_group = group;
4032         return group;
4033 }
4034
4035
4036 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4037                           int registrar)
4038 {
4039         struct wpa_ssid *ssid = wpa_s->current_ssid;
4040
4041         if (!wpa_s->p2p_in_provisioning) {
4042                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4043                            "provisioning not in progress");
4044                 return;
4045         }
4046
4047         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4048                 u8 go_dev_addr[ETH_ALEN];
4049                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4050                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4051                                           ssid->ssid_len);
4052                 /* Clear any stored provisioning info */
4053                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4054         }
4055
4056         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4057                              NULL);
4058         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4059                 /*
4060                  * Use a separate timeout for initial data connection to
4061                  * complete to allow the group to be removed automatically if
4062                  * something goes wrong in this step before the P2P group idle
4063                  * timeout mechanism is taken into use.
4064                  */
4065                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4066                                        wpas_p2p_group_formation_timeout,
4067                                        wpa_s->parent, NULL);
4068         }
4069         if (wpa_s->global->p2p)
4070                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4071         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4072                 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4073         wpas_group_formation_completed(wpa_s, 1);
4074 }
4075
4076
4077 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4078                          struct wps_event_fail *fail)
4079 {
4080         if (!wpa_s->p2p_in_provisioning) {
4081                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4082                            "provisioning not in progress");
4083                 return;
4084         }
4085
4086         if (wpa_s->go_params) {
4087                 p2p_clear_provisioning_info(
4088                         wpa_s->global->p2p,
4089                         wpa_s->go_params->peer_device_addr);
4090         }
4091
4092         wpas_notify_p2p_wps_failed(wpa_s, fail);
4093 }
4094
4095
4096 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4097                        const char *config_method,
4098                        enum wpas_p2p_prov_disc_use use)
4099 {
4100         u16 config_methods;
4101
4102         wpa_s->p2p_fallback_to_go_neg = 0;
4103         wpa_s->pending_pd_use = NORMAL_PD;
4104         if (os_strncmp(config_method, "display", 7) == 0)
4105                 config_methods = WPS_CONFIG_DISPLAY;
4106         else if (os_strncmp(config_method, "keypad", 6) == 0)
4107                 config_methods = WPS_CONFIG_KEYPAD;
4108         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4109                  os_strncmp(config_method, "pushbutton", 10) == 0)
4110                 config_methods = WPS_CONFIG_PUSHBUTTON;
4111         else {
4112                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4113                 return -1;
4114         }
4115
4116         if (use == WPAS_P2P_PD_AUTO) {
4117                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4118                 wpa_s->pending_pd_config_methods = config_methods;
4119                 wpa_s->p2p_auto_pd = 1;
4120                 wpa_s->p2p_auto_join = 0;
4121                 wpa_s->pending_pd_before_join = 0;
4122                 wpa_s->auto_pd_scan_retry = 0;
4123                 wpas_p2p_stop_find(wpa_s);
4124                 wpa_s->p2p_join_scan_count = 0;
4125                 os_get_time(&wpa_s->p2p_auto_started);
4126                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4127                            wpa_s->p2p_auto_started.sec,
4128                            wpa_s->p2p_auto_started.usec);
4129                 wpas_p2p_join_scan(wpa_s, NULL);
4130                 return 0;
4131         }
4132
4133         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4134                 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4135                                                  config_methods,
4136                                                  use == WPAS_P2P_PD_FOR_JOIN);
4137         }
4138
4139         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4140                 return -1;
4141
4142         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4143                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4144                                  0);
4145 }
4146
4147
4148 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4149                               char *end)
4150 {
4151         return p2p_scan_result_text(ies, ies_len, buf, end);
4152 }
4153
4154
4155 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4156 {
4157         if (!wpa_s->pending_action_tx)
4158                 return;
4159
4160         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4161                    "operation request");
4162         wpabuf_free(wpa_s->pending_action_tx);
4163         wpa_s->pending_action_tx = NULL;
4164 }
4165
4166
4167 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4168                   enum p2p_discovery_type type,
4169                   unsigned int num_req_dev_types, const u8 *req_dev_types,
4170                   const u8 *dev_id, unsigned int search_delay)
4171 {
4172         wpas_p2p_clear_pending_action_tx(wpa_s);
4173         wpa_s->p2p_long_listen = 0;
4174
4175         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4176                 return wpa_drv_p2p_find(wpa_s, timeout, type);
4177
4178         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4179             wpa_s->p2p_in_provisioning)
4180                 return -1;
4181
4182         wpa_supplicant_cancel_sched_scan(wpa_s);
4183
4184         return p2p_find(wpa_s->global->p2p, timeout, type,
4185                         num_req_dev_types, req_dev_types, dev_id,
4186                         search_delay);
4187 }
4188
4189
4190 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4191 {
4192         wpas_p2p_clear_pending_action_tx(wpa_s);
4193         wpa_s->p2p_long_listen = 0;
4194         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4195         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4196         wpa_s->global->p2p_cb_on_scan_complete = 0;
4197
4198         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4199                 wpa_drv_p2p_stop_find(wpa_s);
4200                 return;
4201         }
4202
4203         if (wpa_s->global->p2p)
4204                 p2p_stop_find(wpa_s->global->p2p);
4205
4206         wpas_p2p_remove_pending_group_interface(wpa_s);
4207 }
4208
4209
4210 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4211 {
4212         struct wpa_supplicant *wpa_s = eloop_ctx;
4213         wpa_s->p2p_long_listen = 0;
4214 }
4215
4216
4217 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4218 {
4219         int res;
4220
4221         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4222                 return -1;
4223
4224         wpa_supplicant_cancel_sched_scan(wpa_s);
4225         wpas_p2p_clear_pending_action_tx(wpa_s);
4226
4227         if (timeout == 0) {
4228                 /*
4229                  * This is a request for unlimited Listen state. However, at
4230                  * least for now, this is mapped to a Listen state for one
4231                  * hour.
4232                  */
4233                 timeout = 3600;
4234         }
4235         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4236         wpa_s->p2p_long_listen = 0;
4237
4238         /*
4239          * Stop previous find/listen operation to avoid trying to request a new
4240          * remain-on-channel operation while the driver is still running the
4241          * previous one.
4242          */
4243         if (wpa_s->global->p2p)
4244                 p2p_stop_find(wpa_s->global->p2p);
4245
4246         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4247         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4248                 wpa_s->p2p_long_listen = timeout * 1000;
4249                 eloop_register_timeout(timeout, 0,
4250                                        wpas_p2p_long_listen_timeout,
4251                                        wpa_s, NULL);
4252         }
4253
4254         return res;
4255 }
4256
4257
4258 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4259                           u8 *buf, size_t len, int p2p_group)
4260 {
4261         struct wpabuf *p2p_ie;
4262         int ret;
4263
4264         if (wpa_s->global->p2p_disabled)
4265                 return -1;
4266         if (wpa_s->global->p2p == NULL)
4267                 return -1;
4268         if (bss == NULL)
4269                 return -1;
4270
4271         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4272         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4273                                p2p_group, p2p_ie);
4274         wpabuf_free(p2p_ie);
4275
4276         return ret;
4277 }
4278
4279
4280 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4281                           const u8 *dst, const u8 *bssid,
4282                           const u8 *ie, size_t ie_len, int ssi_signal)
4283 {
4284         if (wpa_s->global->p2p_disabled)
4285                 return 0;
4286         if (wpa_s->global->p2p == NULL)
4287                 return 0;
4288
4289         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4290                                  ie, ie_len)) {
4291         case P2P_PREQ_NOT_P2P:
4292                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4293                                  ssi_signal);
4294                 /* fall through */
4295         case P2P_PREQ_MALFORMED:
4296         case P2P_PREQ_NOT_LISTEN:
4297         case P2P_PREQ_NOT_PROCESSED:
4298         default: /* make gcc happy */
4299                 return 0;
4300         case P2P_PREQ_PROCESSED:
4301                 return 1;
4302         }
4303 }
4304
4305
4306 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4307                         const u8 *sa, const u8 *bssid,
4308                         u8 category, const u8 *data, size_t len, int freq)
4309 {
4310         if (wpa_s->global->p2p_disabled)
4311                 return;
4312         if (wpa_s->global->p2p == NULL)
4313                 return;
4314
4315         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4316                       freq);
4317 }
4318
4319
4320 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4321 {
4322         if (wpa_s->global->p2p_disabled)
4323                 return;
4324         if (wpa_s->global->p2p == NULL)
4325                 return;
4326
4327         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4328 }
4329
4330
4331 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4332 {
4333         p2p_group_deinit(wpa_s->p2p_group);
4334         wpa_s->p2p_group = NULL;
4335
4336         wpa_s->ap_configured_cb = NULL;
4337         wpa_s->ap_configured_cb_ctx = NULL;
4338         wpa_s->ap_configured_cb_data = NULL;
4339         wpa_s->connect_without_scan = NULL;
4340 }
4341
4342
4343 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4344 {
4345         wpa_s->p2p_long_listen = 0;
4346
4347         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4348                 return wpa_drv_p2p_reject(wpa_s, addr);
4349
4350         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4351                 return -1;
4352
4353         return p2p_reject(wpa_s->global->p2p, addr);
4354 }
4355
4356
4357 /* Invite to reinvoke a persistent group */
4358 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4359                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4360                     int ht40)
4361 {
4362         enum p2p_invite_role role;
4363         u8 *bssid = NULL;
4364
4365         wpa_s->p2p_persistent_go_freq = freq;
4366         wpa_s->p2p_go_ht40 = !!ht40;
4367         if (ssid->mode == WPAS_MODE_P2P_GO) {
4368                 role = P2P_INVITE_ROLE_GO;
4369                 if (peer_addr == NULL) {
4370                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4371                                    "address in invitation command");
4372                         return -1;
4373                 }
4374                 if (wpas_p2p_create_iface(wpa_s)) {
4375                         if (wpas_p2p_add_group_interface(wpa_s,
4376                                                          WPA_IF_P2P_GO) < 0) {
4377                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
4378                                            "allocate a new interface for the "
4379                                            "group");
4380                                 return -1;
4381                         }
4382                         bssid = wpa_s->pending_interface_addr;
4383                 } else
4384                         bssid = wpa_s->own_addr;
4385         } else {
4386                 role = P2P_INVITE_ROLE_CLIENT;
4387                 peer_addr = ssid->bssid;
4388         }
4389         wpa_s->pending_invite_ssid_id = ssid->id;
4390
4391         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4392                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4393                                           ssid->ssid, ssid->ssid_len,
4394                                           go_dev_addr, 1);
4395
4396         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4397                 return -1;
4398
4399         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4400                           ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4401 }
4402
4403
4404 /* Invite to join an active group */
4405 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4406                           const u8 *peer_addr, const u8 *go_dev_addr)
4407 {
4408         struct wpa_global *global = wpa_s->global;
4409         enum p2p_invite_role role;
4410         u8 *bssid = NULL;
4411         struct wpa_ssid *ssid;
4412         int persistent;
4413
4414         wpa_s->p2p_persistent_go_freq = 0;
4415         wpa_s->p2p_go_ht40 = 0;
4416
4417         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4418                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4419                         break;
4420         }
4421         if (wpa_s == NULL) {
4422                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4423                 return -1;
4424         }
4425
4426         ssid = wpa_s->current_ssid;
4427         if (ssid == NULL) {
4428                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4429                            "invitation");
4430                 return -1;
4431         }
4432
4433         persistent = ssid->p2p_persistent_group &&
4434                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4435                                         ssid->ssid, ssid->ssid_len);
4436
4437         if (ssid->mode == WPAS_MODE_P2P_GO) {
4438                 role = P2P_INVITE_ROLE_ACTIVE_GO;
4439                 bssid = wpa_s->own_addr;
4440                 if (go_dev_addr == NULL)
4441                         go_dev_addr = wpa_s->global->p2p_dev_addr;
4442         } else {
4443                 role = P2P_INVITE_ROLE_CLIENT;
4444                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4445                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4446                                    "invite to current group");
4447                         return -1;
4448                 }
4449                 bssid = wpa_s->bssid;
4450                 if (go_dev_addr == NULL &&
4451                     !is_zero_ether_addr(wpa_s->go_dev_addr))
4452                         go_dev_addr = wpa_s->go_dev_addr;
4453         }
4454         wpa_s->parent->pending_invite_ssid_id = -1;
4455
4456         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4457                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4458                                           ssid->ssid, ssid->ssid_len,
4459                                           go_dev_addr, persistent);
4460
4461         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4462                 return -1;
4463
4464         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4465                           ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4466                           go_dev_addr, persistent);
4467 }
4468
4469
4470 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4471 {
4472         struct wpa_ssid *ssid = wpa_s->current_ssid;
4473         const char *ssid_txt;
4474         u8 go_dev_addr[ETH_ALEN];
4475         int network_id = -1;
4476         int persistent;
4477         int freq;
4478
4479         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4480                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4481                                      wpa_s->parent, NULL);
4482         }
4483
4484         if (!wpa_s->show_group_started || !ssid)
4485                 goto done;
4486
4487         wpa_s->show_group_started = 0;
4488
4489         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4490         os_memset(go_dev_addr, 0, ETH_ALEN);
4491         if (ssid->bssid_set)
4492                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4493         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4494                                                ssid->ssid_len);
4495         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4496
4497         if (wpa_s->global->p2p_group_formation == wpa_s)
4498                 wpa_s->global->p2p_group_formation = NULL;
4499
4500         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4501                 (int) wpa_s->assoc_freq;
4502         if (ssid->passphrase == NULL && ssid->psk_set) {
4503                 char psk[65];
4504                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4505                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4506                         "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4507                         MACSTR "%s",
4508                         wpa_s->ifname, ssid_txt, freq, psk,
4509                         MAC2STR(go_dev_addr),
4510                         persistent ? " [PERSISTENT]" : "");
4511         } else {
4512                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4513                         "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4514                         "go_dev_addr=" MACSTR "%s",
4515                         wpa_s->ifname, ssid_txt, freq,
4516                         ssid->passphrase ? ssid->passphrase : "",
4517                         MAC2STR(go_dev_addr),
4518                         persistent ? " [PERSISTENT]" : "");
4519         }
4520
4521         if (persistent)
4522                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4523                                                              ssid, go_dev_addr);
4524         if (network_id < 0)
4525                 network_id = ssid->id;
4526         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4527
4528 done:
4529         if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4530             wpa_s->global->p2p != NULL) {
4531                 wpa_s->global->p2p_cb_on_scan_complete = 0;
4532                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4533                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4534                                 "continued after successful connection");
4535                         p2p_increase_search_delay(
4536                                 wpa_s->global->p2p,
4537                                 wpas_p2p_search_delay(wpa_s));
4538                 }
4539         }
4540 }
4541
4542
4543 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4544                           u32 interval1, u32 duration2, u32 interval2)
4545 {
4546         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4547                 return -1;
4548         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4549                 return -1;
4550
4551         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4552             wpa_s->current_ssid == NULL ||
4553             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4554                 return -1;
4555
4556         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4557                                 wpa_s->own_addr, wpa_s->assoc_freq,
4558                                 duration1, interval1, duration2, interval2);
4559 }
4560
4561
4562 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4563                         unsigned int interval)
4564 {
4565         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4566                 return -1;
4567
4568         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4569                 return -1;
4570
4571         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4572 }
4573
4574
4575 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4576 {
4577         return wpa_s->current_ssid != NULL &&
4578                 wpa_s->current_ssid->p2p_group &&
4579                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4580 }
4581
4582
4583 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4584 {
4585         struct wpa_supplicant *wpa_s = eloop_ctx;
4586
4587         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4588                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4589                            "disabled");
4590                 return;
4591         }
4592
4593         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4594                    "group");
4595         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4596 }
4597
4598
4599 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4600 {
4601         int timeout;
4602
4603         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4604                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4605
4606         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4607                 return;
4608
4609         timeout = wpa_s->conf->p2p_group_idle;
4610         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4611             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4612             timeout = P2P_MAX_CLIENT_IDLE;
4613
4614         if (timeout == 0)
4615                 return;
4616
4617         if (timeout < 0) {
4618                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4619                         timeout = 0; /* special client mode no-timeout */
4620                 else
4621                         return;
4622         }
4623
4624         if (wpa_s->p2p_in_provisioning) {
4625                 /*
4626                  * Use the normal group formation timeout during the
4627                  * provisioning phase to avoid terminating this process too
4628                  * early due to group idle timeout.
4629                  */
4630                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4631                            "during provisioning");
4632                 return;
4633         }
4634
4635         if (wpa_s->show_group_started) {
4636                 /*
4637                  * Use the normal group formation timeout between the end of
4638                  * the provisioning phase and completion of 4-way handshake to
4639                  * avoid terminating this process too early due to group idle
4640                  * timeout.
4641                  */
4642                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4643                            "while waiting for initial 4-way handshake to "
4644                            "complete");
4645                 return;
4646         }
4647
4648         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4649                    timeout);
4650         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4651                                wpa_s, NULL);
4652 }
4653
4654
4655 /* Returns 1 if the interface was removed */
4656 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4657                           u16 reason_code, const u8 *ie, size_t ie_len,
4658                           int locally_generated)
4659 {
4660         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4661                 return 0;
4662         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4663                 return 0;
4664
4665         if (!locally_generated)
4666                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4667                                  ie_len);
4668
4669         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4670             wpa_s->current_ssid &&
4671             wpa_s->current_ssid->p2p_group &&
4672             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4673                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4674                            "session is ending");
4675                 if (wpas_p2p_group_delete(wpa_s,
4676                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4677                     > 0)
4678                         return 1;
4679         }
4680
4681         return 0;
4682 }
4683
4684
4685 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4686                              u16 reason_code, const u8 *ie, size_t ie_len,
4687                              int locally_generated)
4688 {
4689         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4690                 return;
4691         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4692                 return;
4693
4694         if (!locally_generated)
4695                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4696                                    ie_len);
4697 }
4698
4699
4700 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4701 {
4702         struct p2p_data *p2p = wpa_s->global->p2p;
4703
4704         if (p2p == NULL)
4705                 return;
4706
4707         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4708                 return;
4709
4710         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4711                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4712
4713         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4714                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4715
4716         if (wpa_s->wps &&
4717             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4718                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4719
4720         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4721                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4722
4723         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4724                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4725                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4726                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4727                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4728         }
4729
4730         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4731                 p2p_set_sec_dev_types(p2p,
4732                                       (void *) wpa_s->conf->sec_device_type,
4733                                       wpa_s->conf->num_sec_device_types);
4734
4735         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4736                 int i;
4737                 p2p_remove_wps_vendor_extensions(p2p);
4738                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4739                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4740                                 continue;
4741                         p2p_add_wps_vendor_extension(
4742                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
4743                 }
4744         }
4745
4746         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4747             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4748                 char country[3];
4749                 country[0] = wpa_s->conf->country[0];
4750                 country[1] = wpa_s->conf->country[1];
4751                 country[2] = 0x04;
4752                 p2p_set_country(p2p, country);
4753         }
4754
4755         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4756                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4757                                      wpa_s->conf->p2p_ssid_postfix ?
4758                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4759                                      0);
4760         }
4761
4762         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4763                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4764
4765         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4766                 u8 reg_class, channel;
4767                 int ret;
4768                 unsigned int r;
4769                 if (wpa_s->conf->p2p_listen_reg_class &&
4770                     wpa_s->conf->p2p_listen_channel) {
4771                         reg_class = wpa_s->conf->p2p_listen_reg_class;
4772                         channel = wpa_s->conf->p2p_listen_channel;
4773                 } else {
4774                         reg_class = 81;
4775                         /*
4776                          * Pick one of the social channels randomly as the
4777                          * listen channel.
4778                          */
4779                         os_get_random((u8 *) &r, sizeof(r));
4780                         channel = 1 + (r % 3) * 5;
4781                 }
4782                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4783                 if (ret)
4784                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4785                                    "failed: %d", ret);
4786         }
4787         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4788                 u8 op_reg_class, op_channel, cfg_op_channel;
4789                 int ret = 0;
4790                 unsigned int r;
4791                 if (wpa_s->conf->p2p_oper_reg_class &&
4792                     wpa_s->conf->p2p_oper_channel) {
4793                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4794                         op_channel = wpa_s->conf->p2p_oper_channel;
4795                         cfg_op_channel = 1;
4796                 } else {
4797                         op_reg_class = 81;
4798                         /*
4799                          * Use random operation channel from (1, 6, 11)
4800                          *if no other preference is indicated.
4801                          */
4802                         os_get_random((u8 *) &r, sizeof(r));
4803                         op_channel = 1 + (r % 3) * 5;
4804                         cfg_op_channel = 0;
4805                 }
4806                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4807                                            cfg_op_channel);
4808                 if (ret)
4809                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4810                                    "failed: %d", ret);
4811         }
4812
4813         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4814                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4815                                       wpa_s->conf->p2p_pref_chan) < 0) {
4816                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4817                                    "update failed");
4818                 }
4819         }
4820 }
4821
4822
4823 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4824                      int duration)
4825 {
4826         if (!wpa_s->ap_iface)
4827                 return -1;
4828         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4829                                    duration);
4830 }
4831
4832
4833 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4834 {
4835         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4836                 return -1;
4837         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4838                 return -1;
4839
4840         wpa_s->global->cross_connection = enabled;
4841         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4842
4843         if (!enabled) {
4844                 struct wpa_supplicant *iface;
4845
4846                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4847                 {
4848                         if (iface->cross_connect_enabled == 0)
4849                                 continue;
4850
4851                         iface->cross_connect_enabled = 0;
4852                         iface->cross_connect_in_use = 0;
4853                         wpa_msg(iface->parent, MSG_INFO,
4854                                 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4855                                 iface->ifname, iface->cross_connect_uplink);
4856                 }
4857         }
4858
4859         return 0;
4860 }
4861
4862
4863 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4864 {
4865         struct wpa_supplicant *iface;
4866
4867         if (!uplink->global->cross_connection)
4868                 return;
4869
4870         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4871                 if (!iface->cross_connect_enabled)
4872                         continue;
4873                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4874                     0)
4875                         continue;
4876                 if (iface->ap_iface == NULL)
4877                         continue;
4878                 if (iface->cross_connect_in_use)
4879                         continue;
4880
4881                 iface->cross_connect_in_use = 1;
4882                 wpa_msg(iface->parent, MSG_INFO,
4883                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4884                         iface->ifname, iface->cross_connect_uplink);
4885         }
4886 }
4887
4888
4889 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4890 {
4891         struct wpa_supplicant *iface;
4892
4893         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4894                 if (!iface->cross_connect_enabled)
4895                         continue;
4896                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4897                     0)
4898                         continue;
4899                 if (!iface->cross_connect_in_use)
4900                         continue;
4901
4902                 wpa_msg(iface->parent, MSG_INFO,
4903                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4904                         iface->ifname, iface->cross_connect_uplink);
4905                 iface->cross_connect_in_use = 0;
4906         }
4907 }
4908
4909
4910 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4911 {
4912         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4913             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4914             wpa_s->cross_connect_disallowed)
4915                 wpas_p2p_disable_cross_connect(wpa_s);
4916         else
4917                 wpas_p2p_enable_cross_connect(wpa_s);
4918         if (!wpa_s->ap_iface &&
4919             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4920                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4921 }
4922
4923
4924 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4925 {
4926         wpas_p2p_disable_cross_connect(wpa_s);
4927         if (!wpa_s->ap_iface &&
4928             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4929                                          wpa_s, NULL))
4930                 wpas_p2p_set_group_idle_timeout(wpa_s);
4931 }
4932
4933
4934 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4935 {
4936         struct wpa_supplicant *iface;
4937
4938         if (!wpa_s->global->cross_connection)
4939                 return;
4940
4941         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4942                 if (iface == wpa_s)
4943                         continue;
4944                 if (iface->drv_flags &
4945                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4946                         continue;
4947                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4948                         continue;
4949
4950                 wpa_s->cross_connect_enabled = 1;
4951                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4952                            sizeof(wpa_s->cross_connect_uplink));
4953                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4954                            "%s to %s whenever uplink is available",
4955                            wpa_s->ifname, wpa_s->cross_connect_uplink);
4956
4957                 if (iface->ap_iface || iface->current_ssid == NULL ||
4958                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
4959                     iface->cross_connect_disallowed ||
4960                     iface->wpa_state != WPA_COMPLETED)
4961                         break;
4962
4963                 wpa_s->cross_connect_in_use = 1;
4964                 wpa_msg(wpa_s->parent, MSG_INFO,
4965                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4966                         wpa_s->ifname, wpa_s->cross_connect_uplink);
4967                 break;
4968         }
4969 }
4970
4971
4972 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4973 {
4974         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4975             !wpa_s->p2p_in_provisioning)
4976                 return 0; /* not P2P client operation */
4977
4978         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4979                    "session overlap");
4980         if (wpa_s != wpa_s->parent)
4981                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4982
4983         if (wpa_s->global->p2p)
4984                 p2p_group_formation_failed(wpa_s->global->p2p);
4985
4986         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4987                              wpa_s->parent, NULL);
4988
4989         wpas_group_formation_completed(wpa_s, 0);
4990         return 1;
4991 }
4992
4993
4994 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4995 {
4996         struct p2p_channels chan;
4997
4998         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4999                 return;
5000
5001         os_memset(&chan, 0, sizeof(chan));
5002         if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5003                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5004                            "channel list");
5005                 return;
5006         }
5007
5008         p2p_update_channel_list(wpa_s->global->p2p, &chan);
5009 }
5010
5011
5012 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5013 {
5014         struct wpa_global *global = wpa_s->global;
5015         int found = 0;
5016         const u8 *peer;
5017
5018         if (global->p2p == NULL)
5019                 return -1;
5020
5021         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5022
5023         if (wpa_s->pending_interface_name[0] &&
5024             !is_zero_ether_addr(wpa_s->pending_interface_addr))
5025                 found = 1;
5026
5027         peer = p2p_get_go_neg_peer(global->p2p);
5028         if (peer) {
5029                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5030                            MACSTR, MAC2STR(peer));
5031                 p2p_unauthorize(global->p2p, peer);
5032                 found = 1;
5033         }
5034
5035         wpas_p2p_stop_find(wpa_s);
5036
5037         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5038                 if (wpa_s == global->p2p_group_formation &&
5039                     (wpa_s->p2p_in_provisioning ||
5040                      wpa_s->parent->pending_interface_type ==
5041                      WPA_IF_P2P_CLIENT)) {
5042                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5043                                    "formation found - cancelling",
5044                                    wpa_s->ifname);
5045                         found = 1;
5046                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5047                                              wpa_s->parent, NULL);
5048                         if (wpa_s->p2p_in_provisioning) {
5049                                 wpas_group_formation_completed(wpa_s, 0);
5050                                 break;
5051                         }
5052                         wpas_p2p_group_delete(wpa_s,
5053                                               P2P_GROUP_REMOVAL_REQUESTED);
5054                         break;
5055                 }
5056         }
5057
5058         if (!found) {
5059                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5060                 return -1;
5061         }
5062
5063         return 0;
5064 }
5065
5066
5067 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5068 {
5069         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5070                 return;
5071
5072         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5073                    "being available anymore");
5074         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5075 }
5076
5077
5078 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5079                                    int freq_24, int freq_5, int freq_overall)
5080 {
5081         struct p2p_data *p2p = wpa_s->global->p2p;
5082         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5083                 return;
5084         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5085 }
5086
5087
5088 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5089 {
5090         u8 peer[ETH_ALEN];
5091         struct p2p_data *p2p = wpa_s->global->p2p;
5092
5093         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5094                 return -1;
5095
5096         if (hwaddr_aton(addr, peer))
5097                 return -1;
5098
5099         return p2p_unauthorize(p2p, peer);
5100 }
5101
5102
5103 /**
5104  * wpas_p2p_disconnect - Disconnect from a P2P Group
5105  * @wpa_s: Pointer to wpa_supplicant data
5106  * Returns: 0 on success, -1 on failure
5107  *
5108  * This can be used to disconnect from a group in which the local end is a P2P
5109  * Client or to end a P2P Group in case the local end is the Group Owner. If a
5110  * virtual network interface was created for this group, that interface will be
5111  * removed. Otherwise, only the configured P2P group network will be removed
5112  * from the interface.
5113  */
5114 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5115 {
5116
5117         if (wpa_s == NULL)
5118                 return -1;
5119
5120         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5121                 -1 : 0;
5122 }
5123
5124
5125 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5126 {
5127         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5128                 return 0;
5129
5130         return p2p_in_progress(wpa_s->global->p2p);
5131 }
5132
5133
5134 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5135                               struct wpa_ssid *ssid)
5136 {
5137         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5138             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5139                                  wpa_s->parent, NULL) > 0) {
5140                 /**
5141                  * Remove the network by scheduling the group formation
5142                  * timeout to happen immediately. The teardown code
5143                  * needs to be scheduled to run asynch later so that we
5144                  * don't delete data from under ourselves unexpectedly.
5145                  * Calling wpas_p2p_group_formation_timeout directly
5146                  * causes a series of crashes in WPS failure scenarios.
5147                  */
5148                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5149                            "P2P group network getting removed");
5150                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5151                                        wpa_s->parent, NULL);
5152         }
5153 }
5154
5155
5156 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5157                                           const u8 *addr, const u8 *ssid,
5158                                           size_t ssid_len)
5159 {
5160         struct wpa_ssid *s;
5161         size_t i;
5162
5163         for (s = wpa_s->conf->ssid; s; s = s->next) {
5164                 if (s->disabled != 2)
5165                         continue;
5166                 if (ssid &&
5167                     (ssid_len != s->ssid_len ||
5168                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
5169                         continue;
5170                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5171                         return s; /* peer is GO in the persistent group */
5172                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5173                         continue;
5174                 for (i = 0; i < s->num_p2p_clients; i++) {
5175                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5176                                       addr, ETH_ALEN) == 0)
5177                                 return s; /* peer is P2P client in persistent
5178                                            * group */
5179                 }
5180         }
5181
5182         return NULL;
5183 }
5184
5185
5186 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5187                                        const u8 *addr)
5188 {
5189         if (addr == NULL)
5190                 return;
5191         wpas_p2p_add_persistent_group_client(wpa_s, addr);
5192 }
5193
5194
5195 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5196                                         int group_added)
5197 {
5198         struct wpa_supplicant *group = wpa_s;
5199         eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5200         if (wpa_s->global->p2p_group_formation)
5201                 group = wpa_s->global->p2p_group_formation;
5202         wpa_s = wpa_s->parent;
5203         offchannel_send_action_done(wpa_s);
5204         if (group_added)
5205                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5206         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5207         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5208                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5209                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5210                          wpa_s->p2p_persistent_id,
5211                          wpa_s->p2p_pd_before_go_neg,
5212                          wpa_s->p2p_go_ht40);
5213 }
5214
5215
5216 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5217 {
5218         if (!wpa_s->p2p_fallback_to_go_neg ||
5219             wpa_s->p2p_in_provisioning <= 5)
5220                 return 0;
5221
5222         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5223                 return 0; /* peer operating as a GO */
5224
5225         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5226                 "fallback to GO Negotiation");
5227         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5228
5229         return 1;
5230 }
5231
5232
5233 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5234 {
5235         const char *rn, *rn2;
5236         struct wpa_supplicant *ifs;
5237
5238         if (wpa_s->wpa_state > WPA_SCANNING) {
5239                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5240                         "concurrent operation",
5241                         P2P_CONCURRENT_SEARCH_DELAY);
5242                 return P2P_CONCURRENT_SEARCH_DELAY;
5243         }
5244
5245         if (!wpa_s->driver->get_radio_name)
5246                 return 0;
5247         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5248         if (rn == NULL || rn[0] == '\0')
5249                 return 0;
5250
5251         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5252                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5253                         continue;
5254
5255                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5256                 if (!rn2 || os_strcmp(rn, rn2) != 0)
5257                         continue;
5258                 if (ifs->wpa_state > WPA_SCANNING) {
5259                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5260                                 "delay due to concurrent operation on "
5261                                 "interface %s",
5262                                 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5263                         return P2P_CONCURRENT_SEARCH_DELAY;
5264                 }
5265         }
5266
5267         return 0;
5268 }