OSDN Git Service

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