OSDN Git Service

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