OSDN Git Service

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