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