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