OSDN Git Service

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