OSDN Git Service

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