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