Track whether scan was started by us or an external program
[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/sta_info.h"
21 #include "ap/ap_drv_ops.h"
22 #include "ap/p2p_hostapd.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "rsn_supp/wpa.h"
25 #include "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "ap.h"
28 #include "config_ssid.h"
29 #include "config.h"
30 #include "notify.h"
31 #include "scan.h"
32 #include "bss.h"
33 #include "offchannel.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
36 #include "wifi_display.h"
37
38
39 /*
40  * How many times to try to scan to find the GO before giving up on join
41  * request.
42  */
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
44
45 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
46
47 #ifndef P2P_MAX_CLIENT_IDLE
48 /*
49  * How many seconds to try to reconnect to the GO when connection in P2P client
50  * role has been lost.
51  */
52 #define P2P_MAX_CLIENT_IDLE 10
53 #endif /* P2P_MAX_CLIENT_IDLE */
54
55 #ifndef P2P_MAX_INITIAL_CONN_WAIT
56 /*
57  * How many seconds to wait for initial 4-way handshake to get completed after
58  * WPS provisioning step.
59  */
60 #define P2P_MAX_INITIAL_CONN_WAIT 10
61 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
62
63 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
64 /*
65  * How many seconds to wait for initial 4-way handshake to get completed after
66  * WPS provisioning step on the GO. This controls the extra time the P2P
67  * operation is considered to be in progress (e.g., to delay other scans) after
68  * WPS provisioning has been completed on the GO during group formation.
69  */
70 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
71 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
72
73 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
74 /*
75  * How many seconds to wait for initial 4-way handshake to get completed after
76  * re-invocation of a persistent group on the GO when the client is expected
77  * to connect automatically (no user interaction).
78  */
79 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
80 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
81
82 #ifndef P2P_CONCURRENT_SEARCH_DELAY
83 #define P2P_CONCURRENT_SEARCH_DELAY 500
84 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
85
86 #define P2P_MGMT_DEVICE_PREFIX          "p2p-dev-"
87
88 enum p2p_group_removal_reason {
89         P2P_GROUP_REMOVAL_UNKNOWN,
90         P2P_GROUP_REMOVAL_SILENT,
91         P2P_GROUP_REMOVAL_FORMATION_FAILED,
92         P2P_GROUP_REMOVAL_REQUESTED,
93         P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94         P2P_GROUP_REMOVAL_UNAVAILABLE,
95         P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
96         P2P_GROUP_REMOVAL_PSK_FAILURE,
97         P2P_GROUP_REMOVAL_FREQ_CONFLICT
98 };
99
100
101 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102 static struct wpa_supplicant *
103 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104                          int go);
105 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
107 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
108 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
109                          const u8 *dev_addr, enum p2p_wps_method wps_method,
110                          int auto_join);
111 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
112 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
113 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
114 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
116                                              void *timeout_ctx);
117 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
118 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
119                                         int group_added);
120 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
121
122
123 /*
124  * Get the number of concurrent channels that the HW can operate, but that are
125  * currently not in use by any of the wpa_supplicant interfaces.
126  */
127 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
128 {
129         int *freqs;
130         int num, unused;
131
132         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
133         if (!freqs)
134                 return -1;
135
136         num = get_shared_radio_freqs(wpa_s, freqs,
137                                      wpa_s->num_multichan_concurrent);
138         os_free(freqs);
139
140         unused = wpa_s->num_multichan_concurrent - num;
141         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
142         return unused;
143 }
144
145
146 /*
147  * Get the frequencies that are currently in use by one or more of the virtual
148  * interfaces, and that are also valid for P2P operation.
149  */
150 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
151                                      int *p2p_freqs, unsigned int len)
152 {
153         int *freqs;
154         unsigned int num, i, j;
155
156         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
157         if (!freqs)
158                 return -1;
159
160         num = get_shared_radio_freqs(wpa_s, freqs,
161                                      wpa_s->num_multichan_concurrent);
162
163         os_memset(p2p_freqs, 0, sizeof(int) * len);
164
165         for (i = 0, j = 0; i < num && j < len; i++) {
166                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
167                         p2p_freqs[j++] = freqs[i];
168         }
169
170         os_free(freqs);
171
172         dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
173
174         return j;
175 }
176
177
178 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
179                                              int freq)
180 {
181         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
182                 return;
183         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
184             freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
185             wpas_p2p_num_unused_channels(wpa_s) > 0) {
186                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
187                            freq);
188                 freq = 0;
189         }
190         p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191 }
192
193
194 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195                                       struct wpa_scan_results *scan_res)
196 {
197         size_t i;
198
199         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200                 return;
201
202         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203                    (int) scan_res->num);
204
205         for (i = 0; i < scan_res->num; i++) {
206                 struct wpa_scan_res *bss = scan_res->res[i];
207                 struct os_reltime time_tmp_age, entry_ts;
208                 const u8 *ies;
209                 size_t ies_len;
210
211                 time_tmp_age.sec = bss->age / 1000;
212                 time_tmp_age.usec = (bss->age % 1000) * 1000;
213                 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
214
215                 ies = (const u8 *) (bss + 1);
216                 ies_len = bss->ie_len;
217                 if (bss->beacon_ie_len > 0 &&
218                     !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219                     wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220                         wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221                                    MACSTR, MAC2STR(bss->bssid));
222                         ies = ies + ies_len;
223                         ies_len = bss->beacon_ie_len;
224                 }
225
226
227                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
228                                          bss->freq, &entry_ts, bss->level,
229                                          ies, ies_len) > 0)
230                         break;
231         }
232
233         p2p_scan_res_handled(wpa_s->global->p2p);
234 }
235
236
237 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238                          unsigned int num_req_dev_types,
239                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
240 {
241         struct wpa_supplicant *wpa_s = ctx;
242         struct wpa_supplicant *ifs;
243         struct wpa_driver_scan_params params;
244         int ret;
245         struct wpabuf *wps_ie, *ies;
246         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
247         size_t ielen;
248
249         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250                 return -1;
251
252         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253                 if (ifs->sta_scan_pending &&
254                     (wpas_scan_scheduled(ifs) || ifs->scanning) &&
255                     wpas_p2p_in_progress(wpa_s) == 2) {
256                         wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257                                    "pending station mode scan to be "
258                                    "completed on interface %s", ifs->ifname);
259                         wpa_s->global->p2p_cb_on_scan_complete = 1;
260                         wpa_supplicant_req_scan(ifs, 0, 0);
261                         return 1;
262                 }
263         }
264
265         os_memset(&params, 0, sizeof(params));
266
267         /* P2P Wildcard SSID */
268         params.num_ssids = 1;
269         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272         wpa_s->wps->dev.p2p = 1;
273         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
275                                         num_req_dev_types, req_dev_types);
276         if (wps_ie == NULL)
277                 return -1;
278
279         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
281         if (ies == NULL) {
282                 wpabuf_free(wps_ie);
283                 return -1;
284         }
285         wpabuf_put_buf(ies, wps_ie);
286         wpabuf_free(wps_ie);
287
288         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
289
290         params.p2p_probe = 1;
291         params.extra_ies = wpabuf_head(ies);
292         params.extra_ies_len = wpabuf_len(ies);
293
294         switch (type) {
295         case P2P_SCAN_SOCIAL:
296                 params.freqs = social_channels;
297                 break;
298         case P2P_SCAN_FULL:
299                 break;
300         case P2P_SCAN_SOCIAL_PLUS_ONE:
301                 social_channels[3] = freq;
302                 params.freqs = social_channels;
303                 break;
304         }
305
306         ret = wpa_drv_scan(wpa_s, &params);
307
308         wpabuf_free(ies);
309
310         if (ret) {
311                 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312                         if (ifs->scanning ||
313                             ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314                                 wpa_s->global->p2p_cb_on_scan_complete = 1;
315                                 ret = 1;
316                                 break;
317                         }
318                 }
319         } else {
320                 os_get_reltime(&wpa_s->scan_trigger_time);
321                 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
322                 wpa_s->own_scan_requested = 1;
323         }
324
325         return ret;
326 }
327
328
329 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
330 {
331         switch (p2p_group_interface) {
332         case P2P_GROUP_INTERFACE_PENDING:
333                 return WPA_IF_P2P_GROUP;
334         case P2P_GROUP_INTERFACE_GO:
335                 return WPA_IF_P2P_GO;
336         case P2P_GROUP_INTERFACE_CLIENT:
337                 return WPA_IF_P2P_CLIENT;
338         }
339
340         return WPA_IF_P2P_GROUP;
341 }
342
343
344 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
345                                                   const u8 *ssid,
346                                                   size_t ssid_len, int *go)
347 {
348         struct wpa_ssid *s;
349
350         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
351                 for (s = wpa_s->conf->ssid; s; s = s->next) {
352                         if (s->disabled != 0 || !s->p2p_group ||
353                             s->ssid_len != ssid_len ||
354                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
355                                 continue;
356                         if (s->mode == WPAS_MODE_P2P_GO &&
357                             s != wpa_s->current_ssid)
358                                 continue;
359                         if (go)
360                                 *go = s->mode == WPAS_MODE_P2P_GO;
361                         return wpa_s;
362                 }
363         }
364
365         return NULL;
366 }
367
368
369 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
370                                  enum p2p_group_removal_reason removal_reason)
371 {
372         struct wpa_ssid *ssid;
373         char *gtype;
374         const char *reason;
375
376         ssid = wpa_s->current_ssid;
377         if (ssid == NULL) {
378                 /*
379                  * The current SSID was not known, but there may still be a
380                  * pending P2P group interface waiting for provisioning or a
381                  * P2P group that is trying to reconnect.
382                  */
383                 ssid = wpa_s->conf->ssid;
384                 while (ssid) {
385                         if (ssid->p2p_group && ssid->disabled != 2)
386                                 break;
387                         ssid = ssid->next;
388                 }
389                 if (ssid == NULL &&
390                         wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
391                 {
392                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
393                                    "not found");
394                         return -1;
395                 }
396         }
397         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
398                 gtype = "GO";
399         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
400                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
401                 wpa_s->reassociate = 0;
402                 wpa_s->disconnected = 1;
403                 wpa_supplicant_deauthenticate(wpa_s,
404                                               WLAN_REASON_DEAUTH_LEAVING);
405                 gtype = "client";
406         } else
407                 gtype = "GO";
408         if (wpa_s->cross_connect_in_use) {
409                 wpa_s->cross_connect_in_use = 0;
410                 wpa_msg_global(wpa_s->parent, MSG_INFO,
411                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
412                                wpa_s->ifname, wpa_s->cross_connect_uplink);
413         }
414         switch (removal_reason) {
415         case P2P_GROUP_REMOVAL_REQUESTED:
416                 reason = " reason=REQUESTED";
417                 break;
418         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
419                 reason = " reason=FORMATION_FAILED";
420                 break;
421         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
422                 reason = " reason=IDLE";
423                 break;
424         case P2P_GROUP_REMOVAL_UNAVAILABLE:
425                 reason = " reason=UNAVAILABLE";
426                 break;
427         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
428                 reason = " reason=GO_ENDING_SESSION";
429                 break;
430         case P2P_GROUP_REMOVAL_PSK_FAILURE:
431                 reason = " reason=PSK_FAILURE";
432                 break;
433         case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434                 reason = " reason=FREQ_CONFLICT";
435                 break;
436         default:
437                 reason = "";
438                 break;
439         }
440         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
441                 wpa_msg_global(wpa_s->parent, MSG_INFO,
442                                P2P_EVENT_GROUP_REMOVED "%s %s%s",
443                                wpa_s->ifname, gtype, reason);
444         }
445
446         if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
447                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
448         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
449                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
450         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
451                                  wpa_s->parent, NULL) > 0) {
452                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
453                            "timeout");
454                 wpa_s->p2p_in_provisioning = 0;
455         }
456
457         /*
458          * Make sure wait for the first client does not remain active after the
459          * group has been removed.
460          */
461         wpa_s->global->p2p_go_wait_client.sec = 0;
462
463         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
464                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
465
466         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
467                 struct wpa_global *global;
468                 char *ifname;
469                 enum wpa_driver_if_type type;
470                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
471                         wpa_s->ifname);
472                 global = wpa_s->global;
473                 ifname = os_strdup(wpa_s->ifname);
474                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
475                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
476                 wpa_s = global->ifaces;
477                 if (wpa_s && ifname)
478                         wpa_drv_if_remove(wpa_s, type, ifname);
479                 os_free(ifname);
480                 return 1;
481         }
482
483         if (!wpa_s->p2p_go_group_formation_completed) {
484                 wpa_s->global->p2p_group_formation = NULL;
485                 wpa_s->p2p_in_provisioning = 0;
486         }
487
488         wpa_s->show_group_started = 0;
489         os_free(wpa_s->go_params);
490         wpa_s->go_params = NULL;
491
492         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
493         if (ssid && (ssid->p2p_group ||
494                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
495                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
496                 int id = ssid->id;
497                 if (ssid == wpa_s->current_ssid) {
498                         wpa_sm_set_config(wpa_s->wpa, NULL);
499                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
500                         wpa_s->current_ssid = NULL;
501                 }
502                 /*
503                  * Networks objects created during any P2P activities are not
504                  * exposed out as they might/will confuse certain non-P2P aware
505                  * applications since these network objects won't behave like
506                  * regular ones.
507                  *
508                  * Likewise, we don't send out network removed signals for such
509                  * network objects.
510                  */
511                 wpa_config_remove_network(wpa_s->conf, id);
512                 wpa_supplicant_clear_status(wpa_s);
513                 wpa_supplicant_cancel_sched_scan(wpa_s);
514                 wpa_s->sta_scan_pending = 0;
515         } else {
516                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
517                            "found");
518         }
519         if (wpa_s->ap_iface)
520                 wpa_supplicant_ap_deinit(wpa_s);
521         else
522                 wpa_drv_deinit_p2p_cli(wpa_s);
523
524         return 0;
525 }
526
527
528 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
529                                      u8 *go_dev_addr,
530                                      const u8 *ssid, size_t ssid_len)
531 {
532         struct wpa_bss *bss;
533         const u8 *bssid;
534         struct wpabuf *p2p;
535         u8 group_capab;
536         const u8 *addr;
537
538         if (wpa_s->go_params)
539                 bssid = wpa_s->go_params->peer_interface_addr;
540         else
541                 bssid = wpa_s->bssid;
542
543         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
544         if (bss == NULL) {
545                 u8 iface_addr[ETH_ALEN];
546                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
547                                            iface_addr) == 0)
548                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
549         }
550         if (bss == NULL) {
551                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
552                            "group is persistent - BSS " MACSTR " not found",
553                            MAC2STR(bssid));
554                 return 0;
555         }
556
557         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
558         if (p2p == NULL)
559                 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
560                                                          P2P_IE_VENDOR_TYPE);
561         if (p2p == NULL) {
562                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
563                            "group is persistent - BSS " MACSTR
564                            " did not include P2P IE", MAC2STR(bssid));
565                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
566                             (u8 *) (bss + 1), bss->ie_len);
567                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
568                             ((u8 *) bss + 1) + bss->ie_len,
569                             bss->beacon_ie_len);
570                 return 0;
571         }
572
573         group_capab = p2p_get_group_capab(p2p);
574         addr = p2p_get_go_dev_addr(p2p);
575         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
576                    "group_capab=0x%x", group_capab);
577         if (addr) {
578                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
579                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
580                            MAC2STR(addr));
581         } else
582                 os_memset(go_dev_addr, 0, ETH_ALEN);
583         wpabuf_free(p2p);
584
585         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
586                    "go_dev_addr=" MACSTR,
587                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
588
589         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
590 }
591
592
593 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
594                                            struct wpa_ssid *ssid,
595                                            const u8 *go_dev_addr)
596 {
597         struct wpa_ssid *s;
598         int changed = 0;
599
600         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
601                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
602         for (s = wpa_s->conf->ssid; s; s = s->next) {
603                 if (s->disabled == 2 &&
604                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
605                     s->ssid_len == ssid->ssid_len &&
606                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
607                         break;
608         }
609
610         if (s) {
611                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
612                            "entry");
613                 if (ssid->passphrase && !s->passphrase)
614                         changed = 1;
615                 else if (ssid->passphrase && s->passphrase &&
616                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
617                         changed = 1;
618         } else {
619                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
620                            "entry");
621                 changed = 1;
622                 s = wpa_config_add_network(wpa_s->conf);
623                 if (s == NULL)
624                         return -1;
625
626                 /*
627                  * Instead of network_added we emit persistent_group_added
628                  * notification. Also to keep the defense checks in
629                  * persistent_group obj registration method, we set the
630                  * relevant flags in s to designate it as a persistent group.
631                  */
632                 s->p2p_group = 1;
633                 s->p2p_persistent_group = 1;
634                 wpas_notify_persistent_group_added(wpa_s, s);
635                 wpa_config_set_network_defaults(s);
636         }
637
638         s->p2p_group = 1;
639         s->p2p_persistent_group = 1;
640         s->disabled = 2;
641         s->bssid_set = 1;
642         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
643         s->mode = ssid->mode;
644         s->auth_alg = WPA_AUTH_ALG_OPEN;
645         s->key_mgmt = WPA_KEY_MGMT_PSK;
646         s->proto = WPA_PROTO_RSN;
647         s->pairwise_cipher = WPA_CIPHER_CCMP;
648         s->export_keys = 1;
649         if (ssid->passphrase) {
650                 os_free(s->passphrase);
651                 s->passphrase = os_strdup(ssid->passphrase);
652         }
653         if (ssid->psk_set) {
654                 s->psk_set = 1;
655                 os_memcpy(s->psk, ssid->psk, 32);
656         }
657         if (s->passphrase && !s->psk_set)
658                 wpa_config_update_psk(s);
659         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
660                 os_free(s->ssid);
661                 s->ssid = os_malloc(ssid->ssid_len);
662         }
663         if (s->ssid) {
664                 s->ssid_len = ssid->ssid_len;
665                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
666         }
667         if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
668                 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
669                 wpa_s->global->add_psk = NULL;
670                 changed = 1;
671         }
672
673 #ifndef CONFIG_NO_CONFIG_WRITE
674         if (changed && wpa_s->conf->update_config &&
675             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
676                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
677         }
678 #endif /* CONFIG_NO_CONFIG_WRITE */
679
680         return s->id;
681 }
682
683
684 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
685                                                  const u8 *addr)
686 {
687         struct wpa_ssid *ssid, *s;
688         u8 *n;
689         size_t i;
690         int found = 0;
691
692         ssid = wpa_s->current_ssid;
693         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
694             !ssid->p2p_persistent_group)
695                 return;
696
697         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
698                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
699                         continue;
700
701                 if (s->ssid_len == ssid->ssid_len &&
702                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
703                         break;
704         }
705
706         if (s == NULL)
707                 return;
708
709         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
710                 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
711                               ETH_ALEN) != 0)
712                         continue;
713
714                 if (i == s->num_p2p_clients - 1)
715                         return; /* already the most recent entry */
716
717                 /* move the entry to mark it most recent */
718                 os_memmove(s->p2p_client_list + i * ETH_ALEN,
719                            s->p2p_client_list + (i + 1) * ETH_ALEN,
720                            (s->num_p2p_clients - i - 1) * ETH_ALEN);
721                 os_memcpy(s->p2p_client_list +
722                           (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
723                 found = 1;
724                 break;
725         }
726
727         if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
728                 n = os_realloc_array(s->p2p_client_list,
729                                      s->num_p2p_clients + 1, ETH_ALEN);
730                 if (n == NULL)
731                         return;
732                 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
733                 s->p2p_client_list = n;
734                 s->num_p2p_clients++;
735         } else if (!found) {
736                 /* Not enough room for an additional entry - drop the oldest
737                  * entry */
738                 os_memmove(s->p2p_client_list,
739                            s->p2p_client_list + ETH_ALEN,
740                            (s->num_p2p_clients - 1) * ETH_ALEN);
741                 os_memcpy(s->p2p_client_list +
742                           (s->num_p2p_clients - 1) * ETH_ALEN,
743                           addr, ETH_ALEN);
744         }
745
746 #ifndef CONFIG_NO_CONFIG_WRITE
747         if (wpa_s->parent->conf->update_config &&
748             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
749                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
750 #endif /* CONFIG_NO_CONFIG_WRITE */
751 }
752
753
754 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
755                                            int success)
756 {
757         struct wpa_ssid *ssid;
758         const char *ssid_txt;
759         int client;
760         int persistent;
761         u8 go_dev_addr[ETH_ALEN];
762         int network_id = -1;
763
764         /*
765          * This callback is likely called for the main interface. Update wpa_s
766          * to use the group interface if a new interface was created for the
767          * group.
768          */
769         if (wpa_s->global->p2p_group_formation)
770                 wpa_s = wpa_s->global->p2p_group_formation;
771         if (wpa_s->p2p_go_group_formation_completed) {
772                 wpa_s->global->p2p_group_formation = NULL;
773                 wpa_s->p2p_in_provisioning = 0;
774         }
775
776         if (!success) {
777                 wpa_msg_global(wpa_s->parent, MSG_INFO,
778                                P2P_EVENT_GROUP_FORMATION_FAILURE);
779                 wpas_p2p_group_delete(wpa_s,
780                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
781                 return;
782         }
783
784         wpa_msg_global(wpa_s->parent, MSG_INFO,
785                        P2P_EVENT_GROUP_FORMATION_SUCCESS);
786
787         ssid = wpa_s->current_ssid;
788         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
789                 ssid->mode = WPAS_MODE_P2P_GO;
790                 p2p_group_notif_formation_done(wpa_s->p2p_group);
791                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
792         }
793
794         persistent = 0;
795         if (ssid) {
796                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
797                 client = ssid->mode == WPAS_MODE_INFRA;
798                 if (ssid->mode == WPAS_MODE_P2P_GO) {
799                         persistent = ssid->p2p_persistent_group;
800                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
801                                   ETH_ALEN);
802                 } else
803                         persistent = wpas_p2p_persistent_group(wpa_s,
804                                                                go_dev_addr,
805                                                                ssid->ssid,
806                                                                ssid->ssid_len);
807         } else {
808                 ssid_txt = "";
809                 client = wpa_s->p2p_group_interface ==
810                         P2P_GROUP_INTERFACE_CLIENT;
811                 os_memset(go_dev_addr, 0, ETH_ALEN);
812         }
813
814         wpa_s->show_group_started = 0;
815         if (client) {
816                 /*
817                  * Indicate event only after successfully completed 4-way
818                  * handshake, i.e., when the interface is ready for data
819                  * packets.
820                  */
821                 wpa_s->show_group_started = 1;
822         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
823                 char psk[65];
824                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
825                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
826                                "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
827                                MACSTR "%s",
828                                wpa_s->ifname, ssid_txt, ssid->frequency, psk,
829                                MAC2STR(go_dev_addr),
830                                persistent ? " [PERSISTENT]" : "");
831                 wpas_p2p_cross_connect_setup(wpa_s);
832                 wpas_p2p_set_group_idle_timeout(wpa_s);
833         } else {
834                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
835                                "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
836                                "go_dev_addr=" MACSTR "%s",
837                                wpa_s->ifname, ssid_txt,
838                                ssid ? ssid->frequency : 0,
839                                ssid && ssid->passphrase ? ssid->passphrase : "",
840                                MAC2STR(go_dev_addr),
841                                persistent ? " [PERSISTENT]" : "");
842                 wpas_p2p_cross_connect_setup(wpa_s);
843                 wpas_p2p_set_group_idle_timeout(wpa_s);
844         }
845
846         if (persistent)
847                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
848                                                              ssid, go_dev_addr);
849         else {
850                 os_free(wpa_s->global->add_psk);
851                 wpa_s->global->add_psk = NULL;
852         }
853         if (network_id < 0 && ssid)
854                 network_id = ssid->id;
855         if (!client) {
856                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
857                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
858         }
859 }
860
861
862 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
863                                            unsigned int freq,
864                                            const u8 *dst, const u8 *src,
865                                            const u8 *bssid,
866                                            const u8 *data, size_t data_len,
867                                            enum offchannel_send_action_result
868                                            result)
869 {
870         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
871
872         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
873                 return;
874         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
875                 return;
876
877         switch (result) {
878         case OFFCHANNEL_SEND_ACTION_SUCCESS:
879                 res = P2P_SEND_ACTION_SUCCESS;
880                 break;
881         case OFFCHANNEL_SEND_ACTION_NO_ACK:
882                 res = P2P_SEND_ACTION_NO_ACK;
883                 break;
884         case OFFCHANNEL_SEND_ACTION_FAILED:
885                 res = P2P_SEND_ACTION_FAILED;
886                 break;
887         }
888
889         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
890
891         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
892             wpa_s->pending_pd_before_join &&
893             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
894              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
895             wpa_s->p2p_fallback_to_go_neg) {
896                 wpa_s->pending_pd_before_join = 0;
897                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
898                         "during p2p_connect-auto");
899                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
900                 return;
901         }
902 }
903
904
905 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
906                             const u8 *src, const u8 *bssid, const u8 *buf,
907                             size_t len, unsigned int wait_time)
908 {
909         struct wpa_supplicant *wpa_s = ctx;
910         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
911                                       wait_time,
912                                       wpas_p2p_send_action_tx_status, 1);
913 }
914
915
916 static void wpas_send_action_done(void *ctx)
917 {
918         struct wpa_supplicant *wpa_s = ctx;
919         offchannel_send_action_done(wpa_s);
920 }
921
922
923 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
924                                     struct p2p_go_neg_results *params)
925 {
926         if (wpa_s->go_params == NULL) {
927                 wpa_s->go_params = os_malloc(sizeof(*params));
928                 if (wpa_s->go_params == NULL)
929                         return -1;
930         }
931         os_memcpy(wpa_s->go_params, params, sizeof(*params));
932         return 0;
933 }
934
935
936 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
937                                     struct p2p_go_neg_results *res)
938 {
939         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
940                    MAC2STR(res->peer_interface_addr));
941         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
942                           res->ssid, res->ssid_len);
943         wpa_supplicant_ap_deinit(wpa_s);
944         wpas_copy_go_neg_results(wpa_s, res);
945         if (res->wps_method == WPS_PBC)
946                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
947         else {
948                 u16 dev_pw_id = DEV_PW_DEFAULT;
949                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
950                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
951                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
952                                    wpa_s->p2p_pin, 1, dev_pw_id);
953         }
954 }
955
956
957 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
958                                   struct wpa_ssid *ssid)
959 {
960         struct wpa_ssid *persistent;
961         struct psk_list_entry *psk;
962         struct hostapd_data *hapd;
963
964         if (!wpa_s->ap_iface)
965                 return;
966
967         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
968                                              ssid->ssid_len);
969         if (persistent == NULL)
970                 return;
971
972         hapd = wpa_s->ap_iface->bss[0];
973
974         dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
975                          list) {
976                 struct hostapd_wpa_psk *hpsk;
977
978                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
979                         MACSTR " psk=%d",
980                         MAC2STR(psk->addr), psk->p2p);
981                 hpsk = os_zalloc(sizeof(*hpsk));
982                 if (hpsk == NULL)
983                         break;
984                 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
985                 if (psk->p2p)
986                         os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
987                 else
988                         os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
989                 hpsk->next = hapd->conf->ssid.wpa_psk;
990                 hapd->conf->ssid.wpa_psk = hpsk;
991         }
992 }
993
994
995 static void p2p_go_configured(void *ctx, void *data)
996 {
997         struct wpa_supplicant *wpa_s = ctx;
998         struct p2p_go_neg_results *params = data;
999         struct wpa_ssid *ssid;
1000         int network_id = -1;
1001
1002         ssid = wpa_s->current_ssid;
1003         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1004                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1005                 if (wpa_s->global->p2p_group_formation == wpa_s)
1006                         wpa_s->global->p2p_group_formation = NULL;
1007                 if (os_strlen(params->passphrase) > 0) {
1008                         wpa_msg_global(wpa_s->parent, MSG_INFO,
1009                                        P2P_EVENT_GROUP_STARTED
1010                                        "%s GO ssid=\"%s\" freq=%d "
1011                                        "passphrase=\"%s\" go_dev_addr=" MACSTR
1012                                        "%s", wpa_s->ifname,
1013                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1014                                        ssid->frequency, params->passphrase,
1015                                        MAC2STR(wpa_s->global->p2p_dev_addr),
1016                                        params->persistent_group ?
1017                                        " [PERSISTENT]" : "");
1018                 } else {
1019                         char psk[65];
1020                         wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1021                                          sizeof(params->psk));
1022                         wpa_msg_global(wpa_s->parent, MSG_INFO,
1023                                        P2P_EVENT_GROUP_STARTED
1024                                        "%s GO ssid=\"%s\" freq=%d psk=%s "
1025                                        "go_dev_addr=" MACSTR "%s",
1026                                        wpa_s->ifname,
1027                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1028                                        ssid->frequency, psk,
1029                                        MAC2STR(wpa_s->global->p2p_dev_addr),
1030                                        params->persistent_group ?
1031                                        " [PERSISTENT]" : "");
1032                 }
1033
1034                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1035                 if (params->persistent_group) {
1036                         network_id = wpas_p2p_store_persistent_group(
1037                                 wpa_s->parent, ssid,
1038                                 wpa_s->global->p2p_dev_addr);
1039                         wpas_p2p_add_psk_list(wpa_s, ssid);
1040                 }
1041                 if (network_id < 0)
1042                         network_id = ssid->id;
1043                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1044                 wpas_p2p_cross_connect_setup(wpa_s);
1045                 wpas_p2p_set_group_idle_timeout(wpa_s);
1046
1047                 if (wpa_s->p2p_first_connection_timeout) {
1048                         wpa_dbg(wpa_s, MSG_DEBUG,
1049                                 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1050                                 wpa_s->p2p_first_connection_timeout);
1051                         wpa_s->p2p_go_group_formation_completed = 0;
1052                         wpa_s->global->p2p_group_formation = wpa_s;
1053                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1054                                              wpa_s->parent, NULL);
1055                         eloop_register_timeout(
1056                                 wpa_s->p2p_first_connection_timeout, 0,
1057                                 wpas_p2p_group_formation_timeout,
1058                                 wpa_s->parent, NULL);
1059                 }
1060
1061                 return;
1062         }
1063
1064         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1065         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1066                                               params->peer_interface_addr)) {
1067                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1068                            "filtering");
1069                 return;
1070         }
1071         if (params->wps_method == WPS_PBC)
1072                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1073                                           params->peer_device_addr);
1074         else if (wpa_s->p2p_pin[0])
1075                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1076                                           wpa_s->p2p_pin, NULL, 0, 0);
1077         os_free(wpa_s->go_params);
1078         wpa_s->go_params = NULL;
1079 }
1080
1081
1082 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1083                               struct p2p_go_neg_results *params,
1084                               int group_formation)
1085 {
1086         struct wpa_ssid *ssid;
1087
1088         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1089         if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1090                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1091                         "results");
1092                 return;
1093         }
1094
1095         ssid = wpa_config_add_network(wpa_s->conf);
1096         if (ssid == NULL) {
1097                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1098                 return;
1099         }
1100
1101         wpa_s->show_group_started = 0;
1102
1103         wpa_config_set_network_defaults(ssid);
1104         ssid->temporary = 1;
1105         ssid->p2p_group = 1;
1106         ssid->p2p_persistent_group = params->persistent_group;
1107         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1108                 WPAS_MODE_P2P_GO;
1109         ssid->frequency = params->freq;
1110         ssid->ht40 = params->ht40;
1111         ssid->vht = params->vht;
1112         ssid->ssid = os_zalloc(params->ssid_len + 1);
1113         if (ssid->ssid) {
1114                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1115                 ssid->ssid_len = params->ssid_len;
1116         }
1117         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1118         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1119         ssid->proto = WPA_PROTO_RSN;
1120         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1121         if (os_strlen(params->passphrase) > 0) {
1122                 ssid->passphrase = os_strdup(params->passphrase);
1123                 if (ssid->passphrase == NULL) {
1124                         wpa_msg_global(wpa_s, MSG_ERROR,
1125                                        "P2P: Failed to copy passphrase for GO");
1126                         wpa_config_remove_network(wpa_s->conf, ssid->id);
1127                         return;
1128                 }
1129         } else
1130                 ssid->passphrase = NULL;
1131         ssid->psk_set = params->psk_set;
1132         if (ssid->psk_set)
1133                 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1134         else if (ssid->passphrase)
1135                 wpa_config_update_psk(ssid);
1136         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1137
1138         wpa_s->ap_configured_cb = p2p_go_configured;
1139         wpa_s->ap_configured_cb_ctx = wpa_s;
1140         wpa_s->ap_configured_cb_data = wpa_s->go_params;
1141         wpa_s->connect_without_scan = ssid;
1142         wpa_s->reassociate = 1;
1143         wpa_s->disconnected = 0;
1144         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1145                 "start GO)");
1146         wpa_supplicant_req_scan(wpa_s, 0, 0);
1147 }
1148
1149
1150 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1151                                   const struct wpa_supplicant *src)
1152 {
1153         struct wpa_config *d;
1154         const struct wpa_config *s;
1155
1156         d = dst->conf;
1157         s = src->conf;
1158
1159 #define C(n) if (s->n) d->n = os_strdup(s->n)
1160         C(device_name);
1161         C(manufacturer);
1162         C(model_name);
1163         C(model_number);
1164         C(serial_number);
1165         C(config_methods);
1166 #undef C
1167
1168         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1169         os_memcpy(d->sec_device_type, s->sec_device_type,
1170                   sizeof(d->sec_device_type));
1171         d->num_sec_device_types = s->num_sec_device_types;
1172
1173         d->p2p_group_idle = s->p2p_group_idle;
1174         d->p2p_intra_bss = s->p2p_intra_bss;
1175         d->persistent_reconnect = s->persistent_reconnect;
1176         d->max_num_sta = s->max_num_sta;
1177         d->pbc_in_m1 = s->pbc_in_m1;
1178         d->ignore_old_scan_res = s->ignore_old_scan_res;
1179         d->beacon_int = s->beacon_int;
1180         d->disassoc_low_ack = s->disassoc_low_ack;
1181         d->disable_scan_offload = s->disable_scan_offload;
1182 }
1183
1184
1185 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1186                                       char *ifname, size_t len)
1187 {
1188         char *ifname_ptr = wpa_s->ifname;
1189
1190         if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1191                        os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1192                 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1193         }
1194
1195         os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1196         if (os_strlen(ifname) >= IFNAMSIZ &&
1197             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1198                 /* Try to avoid going over the IFNAMSIZ length limit */
1199                 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1200         }
1201 }
1202
1203
1204 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1205                                         enum wpa_driver_if_type type)
1206 {
1207         char ifname[120], force_ifname[120];
1208
1209         if (wpa_s->pending_interface_name[0]) {
1210                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1211                            "- skip creation of a new one");
1212                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1213                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1214                                    "unknown?! ifname='%s'",
1215                                    wpa_s->pending_interface_name);
1216                         return -1;
1217                 }
1218                 return 0;
1219         }
1220
1221         wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1222         force_ifname[0] = '\0';
1223
1224         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1225                    ifname);
1226         wpa_s->p2p_group_idx++;
1227
1228         wpa_s->pending_interface_type = type;
1229         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1230                            wpa_s->pending_interface_addr, NULL) < 0) {
1231                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1232                            "interface");
1233                 return -1;
1234         }
1235
1236         if (force_ifname[0]) {
1237                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1238                            force_ifname);
1239                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1240                            sizeof(wpa_s->pending_interface_name));
1241         } else
1242                 os_strlcpy(wpa_s->pending_interface_name, ifname,
1243                            sizeof(wpa_s->pending_interface_name));
1244         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1245                    MACSTR, wpa_s->pending_interface_name,
1246                    MAC2STR(wpa_s->pending_interface_addr));
1247
1248         return 0;
1249 }
1250
1251
1252 static void wpas_p2p_remove_pending_group_interface(
1253         struct wpa_supplicant *wpa_s)
1254 {
1255         if (!wpa_s->pending_interface_name[0] ||
1256             is_zero_ether_addr(wpa_s->pending_interface_addr))
1257                 return; /* No pending virtual interface */
1258
1259         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1260                    wpa_s->pending_interface_name);
1261         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1262                           wpa_s->pending_interface_name);
1263         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1264         wpa_s->pending_interface_name[0] = '\0';
1265 }
1266
1267
1268 static struct wpa_supplicant *
1269 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1270 {
1271         struct wpa_interface iface;
1272         struct wpa_supplicant *group_wpa_s;
1273
1274         if (!wpa_s->pending_interface_name[0]) {
1275                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1276                 if (!wpas_p2p_create_iface(wpa_s))
1277                         return NULL;
1278                 /*
1279                  * Something has forced us to remove the pending interface; try
1280                  * to create a new one and hope for the best that we will get
1281                  * the same local address.
1282                  */
1283                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1284                                                  WPA_IF_P2P_CLIENT) < 0)
1285                         return NULL;
1286         }
1287
1288         os_memset(&iface, 0, sizeof(iface));
1289         iface.ifname = wpa_s->pending_interface_name;
1290         iface.driver = wpa_s->driver->name;
1291         if (wpa_s->conf->ctrl_interface == NULL &&
1292             wpa_s->parent != wpa_s &&
1293             wpa_s->p2p_mgmt &&
1294             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1295                 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1296         else
1297                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1298         iface.driver_param = wpa_s->conf->driver_param;
1299         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1300         if (group_wpa_s == NULL) {
1301                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1302                            "wpa_supplicant interface");
1303                 return NULL;
1304         }
1305         wpa_s->pending_interface_name[0] = '\0';
1306         group_wpa_s->parent = wpa_s;
1307         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1308                 P2P_GROUP_INTERFACE_CLIENT;
1309         wpa_s->global->p2p_group_formation = group_wpa_s;
1310
1311         wpas_p2p_clone_config(group_wpa_s, wpa_s);
1312
1313         return group_wpa_s;
1314 }
1315
1316
1317 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1318                                              void *timeout_ctx)
1319 {
1320         struct wpa_supplicant *wpa_s = eloop_ctx;
1321         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1322         wpas_p2p_group_formation_failed(wpa_s);
1323 }
1324
1325
1326 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1327 {
1328         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1329                              wpa_s->parent, NULL);
1330         if (wpa_s->global->p2p)
1331                 p2p_group_formation_failed(wpa_s->global->p2p);
1332         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1333                 wpa_drv_p2p_group_formation_failed(wpa_s);
1334         wpas_group_formation_completed(wpa_s, 0);
1335 }
1336
1337
1338 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1339 {
1340         if (wpa_s->global->p2p_group_formation != wpa_s)
1341                 return;
1342         /* Speed up group formation timeout since this cannot succeed */
1343         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1344                              wpa_s->parent, NULL);
1345         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1346                                wpa_s->parent, NULL);
1347 }
1348
1349
1350 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1351 {
1352         struct wpa_supplicant *wpa_s = ctx;
1353
1354         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1355                 wpa_drv_cancel_remain_on_channel(wpa_s);
1356                 wpa_s->off_channel_freq = 0;
1357                 wpa_s->roc_waiting_drv_freq = 0;
1358         }
1359
1360         if (res->status) {
1361                 wpa_msg_global(wpa_s, MSG_INFO,
1362                                P2P_EVENT_GO_NEG_FAILURE "status=%d",
1363                                res->status);
1364                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1365                 wpas_p2p_remove_pending_group_interface(wpa_s);
1366                 return;
1367         }
1368
1369         if (wpa_s->p2p_go_ht40)
1370                 res->ht40 = 1;
1371         if (wpa_s->p2p_go_vht)
1372                 res->vht = 1;
1373
1374         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1375                        "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1376                        " wps_method=%s",
1377                        res->role_go ? "GO" : "client", res->freq, res->ht40,
1378                        MAC2STR(res->peer_device_addr),
1379                        MAC2STR(res->peer_interface_addr),
1380                        p2p_wps_method_text(res->wps_method));
1381         wpas_notify_p2p_go_neg_completed(wpa_s, res);
1382
1383         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1384                 struct wpa_ssid *ssid;
1385                 ssid = wpa_config_get_network(wpa_s->conf,
1386                                               wpa_s->p2p_persistent_id);
1387                 if (ssid && ssid->disabled == 2 &&
1388                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1389                         size_t len = os_strlen(ssid->passphrase);
1390                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1391                                    "on requested persistent group");
1392                         os_memcpy(res->passphrase, ssid->passphrase, len);
1393                         res->passphrase[len] = '\0';
1394                 }
1395         }
1396
1397         if (wpa_s->create_p2p_iface) {
1398                 struct wpa_supplicant *group_wpa_s =
1399                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
1400                 if (group_wpa_s == NULL) {
1401                         wpas_p2p_remove_pending_group_interface(wpa_s);
1402                         return;
1403                 }
1404                 if (group_wpa_s != wpa_s) {
1405                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1406                                   sizeof(group_wpa_s->p2p_pin));
1407                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1408                 }
1409                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1410                 wpa_s->pending_interface_name[0] = '\0';
1411                 group_wpa_s->p2p_in_provisioning = 1;
1412
1413                 if (res->role_go)
1414                         wpas_start_wps_go(group_wpa_s, res, 1);
1415                 else
1416                         wpas_start_wps_enrollee(group_wpa_s, res);
1417         } else {
1418                 wpa_s->p2p_in_provisioning = 1;
1419                 wpa_s->global->p2p_group_formation = wpa_s;
1420
1421                 if (res->role_go)
1422                         wpas_start_wps_go(wpa_s, res, 1);
1423                 else
1424                         wpas_start_wps_enrollee(ctx, res);
1425         }
1426
1427         wpa_s->p2p_long_listen = 0;
1428         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1429
1430         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1431         eloop_register_timeout(15 + res->peer_config_timeout / 100,
1432                                (res->peer_config_timeout % 100) * 10000,
1433                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
1434 }
1435
1436
1437 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1438 {
1439         struct wpa_supplicant *wpa_s = ctx;
1440         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1441                        " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1442
1443         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1444 }
1445
1446
1447 void wpas_dev_found(void *ctx, const u8 *addr,
1448                     const struct p2p_peer_info *info,
1449                     int new_device)
1450 {
1451 #ifndef CONFIG_NO_STDOUT_DEBUG
1452         struct wpa_supplicant *wpa_s = ctx;
1453         char devtype[WPS_DEV_TYPE_BUFSIZE];
1454         char *wfd_dev_info_hex = NULL;
1455
1456 #ifdef CONFIG_WIFI_DISPLAY
1457         wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1458                                                     WFD_SUBELEM_DEVICE_INFO);
1459 #endif /* CONFIG_WIFI_DISPLAY */
1460
1461         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1462                        " p2p_dev_addr=" MACSTR
1463                        " pri_dev_type=%s name='%s' config_methods=0x%x "
1464                        "dev_capab=0x%x group_capab=0x%x%s%s",
1465                        MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1466                        wps_dev_type_bin2str(info->pri_dev_type, devtype,
1467                                             sizeof(devtype)),
1468                        info->device_name, info->config_methods,
1469                        info->dev_capab, info->group_capab,
1470                        wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1471                        wfd_dev_info_hex ? wfd_dev_info_hex : "");
1472 #endif /* CONFIG_NO_STDOUT_DEBUG */
1473
1474         os_free(wfd_dev_info_hex);
1475
1476         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1477 }
1478
1479
1480 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1481 {
1482         struct wpa_supplicant *wpa_s = ctx;
1483
1484         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1485                        "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1486
1487         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1488 }
1489
1490
1491 static void wpas_find_stopped(void *ctx)
1492 {
1493         struct wpa_supplicant *wpa_s = ctx;
1494         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1495 }
1496
1497
1498 static int wpas_start_listen(void *ctx, unsigned int freq,
1499                              unsigned int duration,
1500                              const struct wpabuf *probe_resp_ie)
1501 {
1502         struct wpa_supplicant *wpa_s = ctx;
1503
1504         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1505
1506         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1507                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1508                            "report received Probe Request frames");
1509                 return -1;
1510         }
1511
1512         wpa_s->pending_listen_freq = freq;
1513         wpa_s->pending_listen_duration = duration;
1514
1515         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1516                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1517                            "to remain on channel (%u MHz) for Listen "
1518                            "state", freq);
1519                 wpa_s->pending_listen_freq = 0;
1520                 return -1;
1521         }
1522         wpa_s->off_channel_freq = 0;
1523         wpa_s->roc_waiting_drv_freq = freq;
1524
1525         return 0;
1526 }
1527
1528
1529 static void wpas_stop_listen(void *ctx)
1530 {
1531         struct wpa_supplicant *wpa_s = ctx;
1532         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1533                 wpa_drv_cancel_remain_on_channel(wpa_s);
1534                 wpa_s->off_channel_freq = 0;
1535                 wpa_s->roc_waiting_drv_freq = 0;
1536         }
1537         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1538         wpa_drv_probe_req_report(wpa_s, 0);
1539 }
1540
1541
1542 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1543 {
1544         struct wpa_supplicant *wpa_s = ctx;
1545         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1546 }
1547
1548
1549 /*
1550  * DNS Header section is used only to calculate compression pointers, so the
1551  * contents of this data does not matter, but the length needs to be reserved
1552  * in the virtual packet.
1553  */
1554 #define DNS_HEADER_LEN 12
1555
1556 /*
1557  * 27-octet in-memory packet from P2P specification containing two implied
1558  * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1559  */
1560 #define P2P_SD_IN_MEMORY_LEN 27
1561
1562 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1563                                        u8 **spos, const u8 *end)
1564 {
1565         while (*spos < end) {
1566                 u8 val = ((*spos)[0] & 0xc0) >> 6;
1567                 int len;
1568
1569                 if (val == 1 || val == 2) {
1570                         /* These are reserved values in RFC 1035 */
1571                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1572                                    "sequence starting with 0x%x", val);
1573                         return -1;
1574                 }
1575
1576                 if (val == 3) {
1577                         u16 offset;
1578                         u8 *spos_tmp;
1579
1580                         /* Offset */
1581                         if (*spos + 2 > end) {
1582                                 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1583                                            "DNS offset field");
1584                                 return -1;
1585                         }
1586
1587                         offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1588                         if (offset >= *spos - start) {
1589                                 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1590                                            "pointer offset %u", offset);
1591                                 return -1;
1592                         }
1593
1594                         (*spos) += 2;
1595                         spos_tmp = start + offset;
1596                         return p2p_sd_dns_uncompress_label(upos, uend, start,
1597                                                            &spos_tmp,
1598                                                            *spos - 2);
1599                 }
1600
1601                 /* Label */
1602                 len = (*spos)[0] & 0x3f;
1603                 if (len == 0)
1604                         return 0;
1605
1606                 (*spos)++;
1607                 if (*spos + len > end) {
1608                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1609                                    "sequence - no room for label with length "
1610                                    "%u", len);
1611                         return -1;
1612                 }
1613
1614                 if (*upos + len + 2 > uend)
1615                         return -2;
1616
1617                 os_memcpy(*upos, *spos, len);
1618                 *spos += len;
1619                 *upos += len;
1620                 (*upos)[0] = '.';
1621                 (*upos)++;
1622                 (*upos)[0] = '\0';
1623         }
1624
1625         return 0;
1626 }
1627
1628
1629 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1630  * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1631  * not large enough */
1632 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1633                                  size_t msg_len, size_t offset)
1634 {
1635         /* 27-octet in-memory packet from P2P specification */
1636         const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1637                 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1638         u8 *tmp, *end, *spos;
1639         char *upos, *uend;
1640         int ret = 0;
1641
1642         if (buf_len < 2)
1643                 return -1;
1644         if (offset > msg_len)
1645                 return -1;
1646
1647         tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1648         if (tmp == NULL)
1649                 return -1;
1650         spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1651         end = spos + msg_len;
1652         spos += offset;
1653
1654         os_memset(tmp, 0, DNS_HEADER_LEN);
1655         os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1656         os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1657
1658         upos = buf;
1659         uend = buf + buf_len;
1660
1661         ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1662         if (ret) {
1663                 os_free(tmp);
1664                 return ret;
1665         }
1666
1667         if (upos == buf) {
1668                 upos[0] = '.';
1669                 upos[1] = '\0';
1670         } else if (upos[-1] == '.')
1671                 upos[-1] = '\0';
1672
1673         os_free(tmp);
1674         return 0;
1675 }
1676
1677
1678 static struct p2p_srv_bonjour *
1679 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1680                              const struct wpabuf *query)
1681 {
1682         struct p2p_srv_bonjour *bsrv;
1683         size_t len;
1684
1685         len = wpabuf_len(query);
1686         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1687                          struct p2p_srv_bonjour, list) {
1688                 if (len == wpabuf_len(bsrv->query) &&
1689                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1690                               len) == 0)
1691                         return bsrv;
1692         }
1693         return NULL;
1694 }
1695
1696
1697 static struct p2p_srv_upnp *
1698 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1699                           const char *service)
1700 {
1701         struct p2p_srv_upnp *usrv;
1702
1703         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1704                          struct p2p_srv_upnp, list) {
1705                 if (version == usrv->version &&
1706                     os_strcmp(service, usrv->service) == 0)
1707                         return usrv;
1708         }
1709         return NULL;
1710 }
1711
1712
1713 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1714                                         u8 srv_trans_id)
1715 {
1716         u8 *len_pos;
1717
1718         if (wpabuf_tailroom(resp) < 5)
1719                 return;
1720
1721         /* Length (to be filled) */
1722         len_pos = wpabuf_put(resp, 2);
1723         wpabuf_put_u8(resp, srv_proto);
1724         wpabuf_put_u8(resp, srv_trans_id);
1725         /* Status Code */
1726         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1727         /* Response Data: empty */
1728         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1729 }
1730
1731
1732 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1733                                 struct wpabuf *resp, u8 srv_trans_id)
1734 {
1735         struct p2p_srv_bonjour *bsrv;
1736         u8 *len_pos;
1737
1738         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1739
1740         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1741                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1742                 return;
1743         }
1744
1745         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1746                          struct p2p_srv_bonjour, list) {
1747                 if (wpabuf_tailroom(resp) <
1748                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1749                         return;
1750                 /* Length (to be filled) */
1751                 len_pos = wpabuf_put(resp, 2);
1752                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1753                 wpabuf_put_u8(resp, srv_trans_id);
1754                 /* Status Code */
1755                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1756                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1757                                   wpabuf_head(bsrv->resp),
1758                                   wpabuf_len(bsrv->resp));
1759                 /* Response Data */
1760                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1761                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1762                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1763                              2);
1764         }
1765 }
1766
1767
1768 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1769                                size_t query_len)
1770 {
1771         char str_rx[256], str_srv[256];
1772
1773         if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1774                 return 0; /* Too short to include DNS Type and Version */
1775         if (os_memcmp(query + query_len - 3,
1776                       wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1777                       3) != 0)
1778                 return 0; /* Mismatch in DNS Type or Version */
1779         if (query_len == wpabuf_len(bsrv->query) &&
1780             os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1781                 return 1; /* Binary match */
1782
1783         if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1784                                   0))
1785                 return 0; /* Failed to uncompress query */
1786         if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1787                                   wpabuf_head(bsrv->query),
1788                                   wpabuf_len(bsrv->query) - 3, 0))
1789                 return 0; /* Failed to uncompress service */
1790
1791         return os_strcmp(str_rx, str_srv) == 0;
1792 }
1793
1794
1795 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1796                                 struct wpabuf *resp, u8 srv_trans_id,
1797                                 const u8 *query, size_t query_len)
1798 {
1799         struct p2p_srv_bonjour *bsrv;
1800         u8 *len_pos;
1801         int matches = 0;
1802
1803         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1804                           query, query_len);
1805         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1806                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1807                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1808                                             srv_trans_id);
1809                 return;
1810         }
1811
1812         if (query_len == 0) {
1813                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1814                 return;
1815         }
1816
1817         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1818                          struct p2p_srv_bonjour, list) {
1819                 if (!match_bonjour_query(bsrv, query, query_len))
1820                         continue;
1821
1822                 if (wpabuf_tailroom(resp) <
1823                     5 + query_len + wpabuf_len(bsrv->resp))
1824                         return;
1825
1826                 matches++;
1827
1828                 /* Length (to be filled) */
1829                 len_pos = wpabuf_put(resp, 2);
1830                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1831                 wpabuf_put_u8(resp, srv_trans_id);
1832
1833                 /* Status Code */
1834                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1835                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1836                                   wpabuf_head(bsrv->resp),
1837                                   wpabuf_len(bsrv->resp));
1838
1839                 /* Response Data */
1840                 wpabuf_put_data(resp, query, query_len); /* Key */
1841                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1842
1843                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1844         }
1845
1846         if (matches == 0) {
1847                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1848                            "available");
1849                 if (wpabuf_tailroom(resp) < 5)
1850                         return;
1851
1852                 /* Length (to be filled) */
1853                 len_pos = wpabuf_put(resp, 2);
1854                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1855                 wpabuf_put_u8(resp, srv_trans_id);
1856
1857                 /* Status Code */
1858                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1859                 /* Response Data: empty */
1860                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1861                              2);
1862         }
1863 }
1864
1865
1866 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1867                              struct wpabuf *resp, u8 srv_trans_id)
1868 {
1869         struct p2p_srv_upnp *usrv;
1870         u8 *len_pos;
1871
1872         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1873
1874         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1875                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1876                 return;
1877         }
1878
1879         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1880                          struct p2p_srv_upnp, list) {
1881                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1882                         return;
1883
1884                 /* Length (to be filled) */
1885                 len_pos = wpabuf_put(resp, 2);
1886                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1887                 wpabuf_put_u8(resp, srv_trans_id);
1888
1889                 /* Status Code */
1890                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1891                 /* Response Data */
1892                 wpabuf_put_u8(resp, usrv->version);
1893                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1894                            usrv->service);
1895                 wpabuf_put_str(resp, usrv->service);
1896                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1897                              2);
1898         }
1899 }
1900
1901
1902 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1903                              struct wpabuf *resp, u8 srv_trans_id,
1904                              const u8 *query, size_t query_len)
1905 {
1906         struct p2p_srv_upnp *usrv;
1907         u8 *len_pos;
1908         u8 version;
1909         char *str;
1910         int count = 0;
1911
1912         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1913                           query, query_len);
1914
1915         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1916                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1917                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1918                                             srv_trans_id);
1919                 return;
1920         }
1921
1922         if (query_len == 0) {
1923                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1924                 return;
1925         }
1926
1927         if (wpabuf_tailroom(resp) < 5)
1928                 return;
1929
1930         /* Length (to be filled) */
1931         len_pos = wpabuf_put(resp, 2);
1932         wpabuf_put_u8(resp, P2P_SERV_UPNP);
1933         wpabuf_put_u8(resp, srv_trans_id);
1934
1935         version = query[0];
1936         str = os_malloc(query_len);
1937         if (str == NULL)
1938                 return;
1939         os_memcpy(str, query + 1, query_len - 1);
1940         str[query_len - 1] = '\0';
1941
1942         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1943                          struct p2p_srv_upnp, list) {
1944                 if (version != usrv->version)
1945                         continue;
1946
1947                 if (os_strcmp(str, "ssdp:all") != 0 &&
1948                     os_strstr(usrv->service, str) == NULL)
1949                         continue;
1950
1951                 if (wpabuf_tailroom(resp) < 2)
1952                         break;
1953                 if (count == 0) {
1954                         /* Status Code */
1955                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1956                         /* Response Data */
1957                         wpabuf_put_u8(resp, version);
1958                 } else
1959                         wpabuf_put_u8(resp, ',');
1960
1961                 count++;
1962
1963                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1964                            usrv->service);
1965                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1966                         break;
1967                 wpabuf_put_str(resp, usrv->service);
1968         }
1969         os_free(str);
1970
1971         if (count == 0) {
1972                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1973                            "available");
1974                 /* Status Code */
1975                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1976                 /* Response Data: empty */
1977         }
1978
1979         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1980 }
1981
1982
1983 #ifdef CONFIG_WIFI_DISPLAY
1984 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1985                             struct wpabuf *resp, u8 srv_trans_id,
1986                             const u8 *query, size_t query_len)
1987 {
1988         const u8 *pos;
1989         u8 role;
1990         u8 *len_pos;
1991
1992         wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1993
1994         if (!wpa_s->global->wifi_display) {
1995                 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1996                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1997                                             srv_trans_id);
1998                 return;
1999         }
2000
2001         if (query_len < 1) {
2002                 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2003                            "Role");
2004                 return;
2005         }
2006
2007         if (wpabuf_tailroom(resp) < 5)
2008                 return;
2009
2010         pos = query;
2011         role = *pos++;
2012         wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2013
2014         /* TODO: role specific handling */
2015
2016         /* Length (to be filled) */
2017         len_pos = wpabuf_put(resp, 2);
2018         wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2019         wpabuf_put_u8(resp, srv_trans_id);
2020         wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2021
2022         while (pos < query + query_len) {
2023                 if (*pos < MAX_WFD_SUBELEMS &&
2024                     wpa_s->global->wfd_subelem[*pos] &&
2025                     wpabuf_tailroom(resp) >=
2026                     wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2027                         wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2028                                    "subelement %u", *pos);
2029                         wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2030                 }
2031                 pos++;
2032         }
2033
2034         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2035 }
2036 #endif /* CONFIG_WIFI_DISPLAY */
2037
2038
2039 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2040                      u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2041 {
2042         struct wpa_supplicant *wpa_s = ctx;
2043         const u8 *pos = tlvs;
2044         const u8 *end = tlvs + tlvs_len;
2045         const u8 *tlv_end;
2046         u16 slen;
2047         struct wpabuf *resp;
2048         u8 srv_proto, srv_trans_id;
2049         size_t buf_len;
2050         char *buf;
2051
2052         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2053                     tlvs, tlvs_len);
2054         buf_len = 2 * tlvs_len + 1;
2055         buf = os_malloc(buf_len);
2056         if (buf) {
2057                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2058                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2059                              MACSTR " %u %u %s",
2060                              freq, MAC2STR(sa), dialog_token, update_indic,
2061                              buf);
2062                 os_free(buf);
2063         }
2064
2065         if (wpa_s->p2p_sd_over_ctrl_iface) {
2066                 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2067                                            update_indic, tlvs, tlvs_len);
2068                 return; /* to be processed by an external program */
2069         }
2070
2071         resp = wpabuf_alloc(10000);
2072         if (resp == NULL)
2073                 return;
2074
2075         while (pos + 1 < end) {
2076                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2077                 slen = WPA_GET_LE16(pos);
2078                 pos += 2;
2079                 if (pos + slen > end || slen < 2) {
2080                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2081                                    "length");
2082                         wpabuf_free(resp);
2083                         return;
2084                 }
2085                 tlv_end = pos + slen;
2086
2087                 srv_proto = *pos++;
2088                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2089                            srv_proto);
2090                 srv_trans_id = *pos++;
2091                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2092                            srv_trans_id);
2093
2094                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2095                             pos, tlv_end - pos);
2096
2097
2098                 if (wpa_s->force_long_sd) {
2099                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2100                                    "response");
2101                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2102                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2103                         goto done;
2104                 }
2105
2106                 switch (srv_proto) {
2107                 case P2P_SERV_ALL_SERVICES:
2108                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2109                                    "for all services");
2110                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2111                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2112                                 wpa_printf(MSG_DEBUG, "P2P: No service "
2113                                            "discovery protocols available");
2114                                 wpas_sd_add_proto_not_avail(
2115                                         resp, P2P_SERV_ALL_SERVICES,
2116                                         srv_trans_id);
2117                                 break;
2118                         }
2119                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2120                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2121                         break;
2122                 case P2P_SERV_BONJOUR:
2123                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2124                                             pos, tlv_end - pos);
2125                         break;
2126                 case P2P_SERV_UPNP:
2127                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2128                                          pos, tlv_end - pos);
2129                         break;
2130 #ifdef CONFIG_WIFI_DISPLAY
2131                 case P2P_SERV_WIFI_DISPLAY:
2132                         wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2133                                         pos, tlv_end - pos);
2134                         break;
2135 #endif /* CONFIG_WIFI_DISPLAY */
2136                 default:
2137                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2138                                    "protocol %u", srv_proto);
2139                         wpas_sd_add_proto_not_avail(resp, srv_proto,
2140                                                     srv_trans_id);
2141                         break;
2142                 }
2143
2144                 pos = tlv_end;
2145         }
2146
2147 done:
2148         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2149                                    update_indic, tlvs, tlvs_len);
2150
2151         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2152
2153         wpabuf_free(resp);
2154 }
2155
2156
2157 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2158                       const u8 *tlvs, size_t tlvs_len)
2159 {
2160         struct wpa_supplicant *wpa_s = ctx;
2161         const u8 *pos = tlvs;
2162         const u8 *end = tlvs + tlvs_len;
2163         const u8 *tlv_end;
2164         u16 slen;
2165         size_t buf_len;
2166         char *buf;
2167
2168         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2169                     tlvs, tlvs_len);
2170         if (tlvs_len > 1500) {
2171                 /* TODO: better way for handling this */
2172                 wpa_msg_ctrl(wpa_s, MSG_INFO,
2173                              P2P_EVENT_SERV_DISC_RESP MACSTR
2174                              " %u <long response: %u bytes>",
2175                              MAC2STR(sa), update_indic,
2176                              (unsigned int) tlvs_len);
2177         } else {
2178                 buf_len = 2 * tlvs_len + 1;
2179                 buf = os_malloc(buf_len);
2180                 if (buf) {
2181                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2182                         wpa_msg_ctrl(wpa_s, MSG_INFO,
2183                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2184                                      MAC2STR(sa), update_indic, buf);
2185                         os_free(buf);
2186                 }
2187         }
2188
2189         while (pos < end) {
2190                 u8 srv_proto, srv_trans_id, status;
2191
2192                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2193                 slen = WPA_GET_LE16(pos);
2194                 pos += 2;
2195                 if (pos + slen > end || slen < 3) {
2196                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2197                                    "length");
2198                         return;
2199                 }
2200                 tlv_end = pos + slen;
2201
2202                 srv_proto = *pos++;
2203                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2204                            srv_proto);
2205                 srv_trans_id = *pos++;
2206                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2207                            srv_trans_id);
2208                 status = *pos++;
2209                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2210                            status);
2211
2212                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2213                             pos, tlv_end - pos);
2214
2215                 pos = tlv_end;
2216         }
2217
2218         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2219 }
2220
2221
2222 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2223                         const struct wpabuf *tlvs)
2224 {
2225         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2226                 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
2227         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2228                 return 0;
2229         return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2230 }
2231
2232
2233 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2234                              u8 version, const char *query)
2235 {
2236         struct wpabuf *tlvs;
2237         u64 ret;
2238
2239         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2240         if (tlvs == NULL)
2241                 return 0;
2242         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2243         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2244         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2245         wpabuf_put_u8(tlvs, version);
2246         wpabuf_put_str(tlvs, query);
2247         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2248         wpabuf_free(tlvs);
2249         return ret;
2250 }
2251
2252
2253 #ifdef CONFIG_WIFI_DISPLAY
2254
2255 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2256                                    const struct wpabuf *tlvs)
2257 {
2258         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2259                 return 0;
2260         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2261                 return 0;
2262         return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2263 }
2264
2265
2266 #define MAX_WFD_SD_SUBELEMS 20
2267
2268 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2269                                 const char *subelems)
2270 {
2271         u8 *len;
2272         const char *pos;
2273         int val;
2274         int count = 0;
2275
2276         len = wpabuf_put(tlvs, 2);
2277         wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2278         wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2279
2280         wpabuf_put_u8(tlvs, role);
2281
2282         pos = subelems;
2283         while (*pos) {
2284                 val = atoi(pos);
2285                 if (val >= 0 && val < 256) {
2286                         wpabuf_put_u8(tlvs, val);
2287                         count++;
2288                         if (count == MAX_WFD_SD_SUBELEMS)
2289                                 break;
2290                 }
2291                 pos = os_strchr(pos + 1, ',');
2292                 if (pos == NULL)
2293                         break;
2294                 pos++;
2295         }
2296
2297         WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2298 }
2299
2300
2301 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2302                                      const u8 *dst, const char *role)
2303 {
2304         struct wpabuf *tlvs;
2305         u64 ret;
2306         const char *subelems;
2307         u8 id = 1;
2308
2309         subelems = os_strchr(role, ' ');
2310         if (subelems == NULL)
2311                 return 0;
2312         subelems++;
2313
2314         tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2315         if (tlvs == NULL)
2316                 return 0;
2317
2318         if (os_strstr(role, "[source]"))
2319                 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2320         if (os_strstr(role, "[pri-sink]"))
2321                 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2322         if (os_strstr(role, "[sec-sink]"))
2323                 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2324         if (os_strstr(role, "[source+sink]"))
2325                 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2326
2327         ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2328         wpabuf_free(tlvs);
2329         return ret;
2330 }
2331
2332 #endif /* CONFIG_WIFI_DISPLAY */
2333
2334
2335 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2336 {
2337         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2338                 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2339         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2340                 return -1;
2341         return p2p_sd_cancel_request(wpa_s->global->p2p,
2342                                      (void *) (uintptr_t) req);
2343 }
2344
2345
2346 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2347                           const u8 *dst, u8 dialog_token,
2348                           const struct wpabuf *resp_tlvs)
2349 {
2350         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2351                 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2352                                         resp_tlvs);
2353                 return;
2354         }
2355         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2356                 return;
2357         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2358                         resp_tlvs);
2359 }
2360
2361
2362 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2363 {
2364         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2365                 wpa_drv_p2p_service_update(wpa_s);
2366                 return;
2367         }
2368         if (wpa_s->global->p2p)
2369                 p2p_sd_service_update(wpa_s->global->p2p);
2370 }
2371
2372
2373 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2374 {
2375         dl_list_del(&bsrv->list);
2376         wpabuf_free(bsrv->query);
2377         wpabuf_free(bsrv->resp);
2378         os_free(bsrv);
2379 }
2380
2381
2382 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2383 {
2384         dl_list_del(&usrv->list);
2385         os_free(usrv->service);
2386         os_free(usrv);
2387 }
2388
2389
2390 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2391 {
2392         struct p2p_srv_bonjour *bsrv, *bn;
2393         struct p2p_srv_upnp *usrv, *un;
2394
2395         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2396                               struct p2p_srv_bonjour, list)
2397                 wpas_p2p_srv_bonjour_free(bsrv);
2398
2399         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2400                               struct p2p_srv_upnp, list)
2401                 wpas_p2p_srv_upnp_free(usrv);
2402
2403         wpas_p2p_sd_service_update(wpa_s);
2404 }
2405
2406
2407 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2408                                  struct wpabuf *query, struct wpabuf *resp)
2409 {
2410         struct p2p_srv_bonjour *bsrv;
2411
2412         bsrv = os_zalloc(sizeof(*bsrv));
2413         if (bsrv == NULL)
2414                 return -1;
2415         bsrv->query = query;
2416         bsrv->resp = resp;
2417         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2418
2419         wpas_p2p_sd_service_update(wpa_s);
2420         return 0;
2421 }
2422
2423
2424 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2425                                  const struct wpabuf *query)
2426 {
2427         struct p2p_srv_bonjour *bsrv;
2428
2429         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2430         if (bsrv == NULL)
2431                 return -1;
2432         wpas_p2p_srv_bonjour_free(bsrv);
2433         wpas_p2p_sd_service_update(wpa_s);
2434         return 0;
2435 }
2436
2437
2438 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2439                               const char *service)
2440 {
2441         struct p2p_srv_upnp *usrv;
2442
2443         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2444                 return 0; /* Already listed */
2445         usrv = os_zalloc(sizeof(*usrv));
2446         if (usrv == NULL)
2447                 return -1;
2448         usrv->version = version;
2449         usrv->service = os_strdup(service);
2450         if (usrv->service == NULL) {
2451                 os_free(usrv);
2452                 return -1;
2453         }
2454         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2455
2456         wpas_p2p_sd_service_update(wpa_s);
2457         return 0;
2458 }
2459
2460
2461 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2462                               const char *service)
2463 {
2464         struct p2p_srv_upnp *usrv;
2465
2466         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2467         if (usrv == NULL)
2468                 return -1;
2469         wpas_p2p_srv_upnp_free(usrv);
2470         wpas_p2p_sd_service_update(wpa_s);
2471         return 0;
2472 }
2473
2474
2475 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2476                                          const u8 *peer, const char *params,
2477                                          unsigned int generated_pin)
2478 {
2479         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2480                        " %08d%s", MAC2STR(peer), generated_pin, params);
2481 }
2482
2483
2484 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2485                                         const u8 *peer, const char *params)
2486 {
2487         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2488                        "%s", MAC2STR(peer), params);
2489 }
2490
2491
2492 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2493                         const u8 *dev_addr, const u8 *pri_dev_type,
2494                         const char *dev_name, u16 supp_config_methods,
2495                         u8 dev_capab, u8 group_capab, const u8 *group_id,
2496                         size_t group_id_len)
2497 {
2498         struct wpa_supplicant *wpa_s = ctx;
2499         char devtype[WPS_DEV_TYPE_BUFSIZE];
2500         char params[300];
2501         u8 empty_dev_type[8];
2502         unsigned int generated_pin = 0;
2503         struct wpa_supplicant *group = NULL;
2504
2505         if (group_id) {
2506                 for (group = wpa_s->global->ifaces; group; group = group->next)
2507                 {
2508                         struct wpa_ssid *s = group->current_ssid;
2509                         if (s != NULL &&
2510                             s->mode == WPAS_MODE_P2P_GO &&
2511                             group_id_len - ETH_ALEN == s->ssid_len &&
2512                             os_memcmp(group_id + ETH_ALEN, s->ssid,
2513                                       s->ssid_len) == 0)
2514                                 break;
2515                 }
2516         }
2517
2518         if (pri_dev_type == NULL) {
2519                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2520                 pri_dev_type = empty_dev_type;
2521         }
2522         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2523                     " pri_dev_type=%s name='%s' config_methods=0x%x "
2524                     "dev_capab=0x%x group_capab=0x%x%s%s",
2525                     MAC2STR(dev_addr),
2526                     wps_dev_type_bin2str(pri_dev_type, devtype,
2527                                          sizeof(devtype)),
2528                     dev_name, supp_config_methods, dev_capab, group_capab,
2529                     group ? " group=" : "",
2530                     group ? group->ifname : "");
2531         params[sizeof(params) - 1] = '\0';
2532
2533         if (config_methods & WPS_CONFIG_DISPLAY) {
2534                 generated_pin = wps_generate_pin();
2535                 wpas_prov_disc_local_display(wpa_s, peer, params,
2536                                              generated_pin);
2537         } else if (config_methods & WPS_CONFIG_KEYPAD)
2538                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2539         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2540                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2541                                MACSTR "%s", MAC2STR(peer), params);
2542
2543         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2544                                             P2P_PROV_DISC_SUCCESS,
2545                                             config_methods, generated_pin);
2546 }
2547
2548
2549 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2550 {
2551         struct wpa_supplicant *wpa_s = ctx;
2552         unsigned int generated_pin = 0;
2553         char params[20];
2554
2555         if (wpa_s->pending_pd_before_join &&
2556             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2557              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2558                 wpa_s->pending_pd_before_join = 0;
2559                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2560                            "join-existing-group operation");
2561                 wpas_p2p_join_start(wpa_s);
2562                 return;
2563         }
2564
2565         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2566             wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2567                 os_snprintf(params, sizeof(params), " peer_go=%d",
2568                             wpa_s->pending_pd_use == AUTO_PD_JOIN);
2569         else
2570                 params[0] = '\0';
2571
2572         if (config_methods & WPS_CONFIG_DISPLAY)
2573                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2574         else if (config_methods & WPS_CONFIG_KEYPAD) {
2575                 generated_pin = wps_generate_pin();
2576                 wpas_prov_disc_local_display(wpa_s, peer, params,
2577                                              generated_pin);
2578         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2579                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2580                                MACSTR "%s", MAC2STR(peer), params);
2581
2582         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2583                                             P2P_PROV_DISC_SUCCESS,
2584                                             config_methods, generated_pin);
2585 }
2586
2587
2588 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2589                                 enum p2p_prov_disc_status status)
2590 {
2591         struct wpa_supplicant *wpa_s = ctx;
2592
2593         if (wpa_s->p2p_fallback_to_go_neg) {
2594                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2595                         "failed - fall back to GO Negotiation");
2596                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2597                 return;
2598         }
2599
2600         if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2601                 wpa_s->pending_pd_before_join = 0;
2602                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2603                            "join-existing-group operation (no ACK for PD "
2604                            "Req attempts)");
2605                 wpas_p2p_join_start(wpa_s);
2606                 return;
2607         }
2608
2609         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2610                        " p2p_dev_addr=" MACSTR " status=%d",
2611                        MAC2STR(peer), status);
2612
2613         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2614                                             status, 0, 0);
2615 }
2616
2617
2618 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2619 {
2620         if (channels == NULL)
2621                 return 1; /* Assume no restrictions */
2622         return p2p_channels_includes_freq(channels, freq);
2623
2624 }
2625
2626
2627 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2628                                   const u8 *go_dev_addr, const u8 *ssid,
2629                                   size_t ssid_len, int *go, u8 *group_bssid,
2630                                   int *force_freq, int persistent_group,
2631                                   const struct p2p_channels *channels)
2632 {
2633         struct wpa_supplicant *wpa_s = ctx;
2634         struct wpa_ssid *s;
2635         int res;
2636         struct wpa_supplicant *grp;
2637
2638         if (!persistent_group) {
2639                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2640                            " to join an active group", MAC2STR(sa));
2641                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2642                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2643                      == 0 ||
2644                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2645                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2646                                    "authorized invitation");
2647                         goto accept_inv;
2648                 }
2649                 /*
2650                  * Do not accept the invitation automatically; notify user and
2651                  * request approval.
2652                  */
2653                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2654         }
2655
2656         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2657         if (grp) {
2658                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2659                            "running persistent group");
2660                 if (*go)
2661                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2662                 goto accept_inv;
2663         }
2664
2665         if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2666             os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2667                 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2668                            "invitation to re-invoke a persistent group");
2669         } else if (!wpa_s->conf->persistent_reconnect)
2670                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2671
2672         for (s = wpa_s->conf->ssid; s; s = s->next) {
2673                 if (s->disabled == 2 &&
2674                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2675                     s->ssid_len == ssid_len &&
2676                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2677                         break;
2678         }
2679
2680         if (!s) {
2681                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2682                            " requested reinvocation of an unknown group",
2683                            MAC2STR(sa));
2684                 return P2P_SC_FAIL_UNKNOWN_GROUP;
2685         }
2686
2687         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2688                 *go = 1;
2689                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2690                         wpa_printf(MSG_DEBUG, "P2P: The only available "
2691                                    "interface is already in use - reject "
2692                                    "invitation");
2693                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2694                 }
2695                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2696         } else if (s->mode == WPAS_MODE_P2P_GO) {
2697                 *go = 1;
2698                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2699                 {
2700                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2701                                    "interface address for the group");
2702                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2703                 }
2704                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2705                           ETH_ALEN);
2706         }
2707
2708 accept_inv:
2709         wpas_p2p_set_own_freq_preference(wpa_s, 0);
2710
2711         /* Get one of the frequencies currently in use */
2712         if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2713                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2714                 wpas_p2p_set_own_freq_preference(wpa_s, res);
2715
2716                 if (wpa_s->num_multichan_concurrent < 2 ||
2717                     wpas_p2p_num_unused_channels(wpa_s) < 1) {
2718                         wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2719                         *force_freq = res;
2720                 }
2721         }
2722
2723         if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2724             wpas_p2p_num_unused_channels(wpa_s) > 0) {
2725                 if (*go == 0) {
2726                         /* We are the client */
2727                         wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2728                                    "running a GO but we are capable of MCC, "
2729                                    "figure out the best channel to use");
2730                         *force_freq = 0;
2731                 } else if (!freq_included(channels, *force_freq)) {
2732                         /* We are the GO, and *force_freq is not in the
2733                          * intersection */
2734                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2735                                    "in intersection but we are capable of MCC, "
2736                                    "figure out the best channel to use",
2737                                    *force_freq);
2738                         *force_freq = 0;
2739                 }
2740         }
2741
2742         return P2P_SC_SUCCESS;
2743 }
2744
2745
2746 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2747                                      const u8 *ssid, size_t ssid_len,
2748                                      const u8 *go_dev_addr, u8 status,
2749                                      int op_freq)
2750 {
2751         struct wpa_supplicant *wpa_s = ctx;
2752         struct wpa_ssid *s;
2753
2754         for (s = wpa_s->conf->ssid; s; s = s->next) {
2755                 if (s->disabled == 2 &&
2756                     s->ssid_len == ssid_len &&
2757                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2758                         break;
2759         }
2760
2761         if (status == P2P_SC_SUCCESS) {
2762                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2763                            " was accepted; op_freq=%d MHz",
2764                            MAC2STR(sa), op_freq);
2765                 if (s) {
2766                         int go = s->mode == WPAS_MODE_P2P_GO;
2767                         wpas_p2p_group_add_persistent(
2768                                 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
2769                                 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2770                 } else if (bssid) {
2771                         wpa_s->user_initiated_pd = 0;
2772                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2773                                       wpa_s->p2p_wps_method, 0);
2774                 }
2775                 return;
2776         }
2777
2778         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2779                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2780                            " was rejected (status %u)", MAC2STR(sa), status);
2781                 return;
2782         }
2783
2784         if (!s) {
2785                 if (bssid) {
2786                         wpa_msg_global(wpa_s, MSG_INFO,
2787                                        P2P_EVENT_INVITATION_RECEIVED
2788                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2789                                        " bssid=" MACSTR " unknown-network",
2790                                        MAC2STR(sa), MAC2STR(go_dev_addr),
2791                                        MAC2STR(bssid));
2792                 } else {
2793                         wpa_msg_global(wpa_s, MSG_INFO,
2794                                        P2P_EVENT_INVITATION_RECEIVED
2795                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2796                                        " unknown-network",
2797                                        MAC2STR(sa), MAC2STR(go_dev_addr));
2798                 }
2799                 return;
2800         }
2801
2802         if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2803                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2804                                "sa=" MACSTR " persistent=%d freq=%d",
2805                                MAC2STR(sa), s->id, op_freq);
2806         } else {
2807                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2808                                "sa=" MACSTR " persistent=%d",
2809                                MAC2STR(sa), s->id);
2810         }
2811 }
2812
2813
2814 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2815                                         struct wpa_ssid *ssid,
2816                                         const u8 *peer, int inv)
2817 {
2818         size_t i;
2819
2820         if (ssid == NULL)
2821                 return;
2822
2823         for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2824                 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2825                               ETH_ALEN) == 0)
2826                         break;
2827         }
2828         if (i >= ssid->num_p2p_clients) {
2829                 if (ssid->mode != WPAS_MODE_P2P_GO &&
2830                     os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2831                         wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2832                                    "due to invitation result", ssid->id);
2833                         wpas_notify_network_removed(wpa_s, ssid);
2834                         wpa_config_remove_network(wpa_s->conf, ssid->id);
2835                         return;
2836                 }
2837                 return; /* Peer not found in client list */
2838         }
2839
2840         wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2841                    "group %d client list%s",
2842                    MAC2STR(peer), ssid->id,
2843                    inv ? " due to invitation result" : "");
2844         os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2845                    ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2846                    (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2847         ssid->num_p2p_clients--;
2848 #ifndef CONFIG_NO_CONFIG_WRITE
2849         if (wpa_s->parent->conf->update_config &&
2850             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2851                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2852 #endif /* CONFIG_NO_CONFIG_WRITE */
2853 }
2854
2855
2856 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2857                                           const u8 *peer)
2858 {
2859         struct wpa_ssid *ssid;
2860
2861         wpa_s = wpa_s->global->p2p_invite_group;
2862         if (wpa_s == NULL)
2863                 return; /* No known invitation group */
2864         ssid = wpa_s->current_ssid;
2865         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2866             !ssid->p2p_persistent_group)
2867                 return; /* Not operating as a GO in persistent group */
2868         ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2869                                        ssid->ssid, ssid->ssid_len);
2870         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2871 }
2872
2873
2874 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2875                                    const struct p2p_channels *channels,
2876                                    const u8 *peer, int neg_freq)
2877 {
2878         struct wpa_supplicant *wpa_s = ctx;
2879         struct wpa_ssid *ssid;
2880         int freq;
2881
2882         if (bssid) {
2883                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2884                                "status=%d " MACSTR,
2885                                status, MAC2STR(bssid));
2886         } else {
2887                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2888                                "status=%d ", status);
2889         }
2890         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2891
2892         wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2893                    status, MAC2STR(peer));
2894         if (wpa_s->pending_invite_ssid_id == -1) {
2895                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2896                         wpas_remove_persistent_client(wpa_s, peer);
2897                 return; /* Invitation to active group */
2898         }
2899
2900         if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2901                 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2902                            "invitation exchange to indicate readiness for "
2903                            "re-invocation");
2904         }
2905
2906         if (status != P2P_SC_SUCCESS) {
2907                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2908                         ssid = wpa_config_get_network(
2909                                 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2910                         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2911                 }
2912                 wpas_p2p_remove_pending_group_interface(wpa_s);
2913                 return;
2914         }
2915
2916         ssid = wpa_config_get_network(wpa_s->conf,
2917                                       wpa_s->pending_invite_ssid_id);
2918         if (ssid == NULL) {
2919                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2920                            "data matching with invitation");
2921                 return;
2922         }
2923
2924         /*
2925          * The peer could have missed our ctrl::ack frame for Invitation
2926          * Response and continue retransmitting the frame. To reduce the
2927          * likelihood of the peer not getting successful TX status for the
2928          * Invitation Response frame, wait a short time here before starting
2929          * the persistent group so that we will remain on the current channel to
2930          * acknowledge any possible retransmission from the peer.
2931          */
2932         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2933                 "starting persistent group");
2934         os_sleep(0, 50000);
2935
2936         freq = wpa_s->p2p_persistent_go_freq;
2937         if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2938             freq_included(channels, neg_freq)) {
2939                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2940                         neg_freq);
2941                 freq = neg_freq;
2942         }
2943
2944         wpas_p2p_group_add_persistent(wpa_s, ssid,
2945                                       ssid->mode == WPAS_MODE_P2P_GO,
2946                                       freq,
2947                                       wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2948                                       channels,
2949                                       ssid->mode == WPAS_MODE_P2P_GO ?
2950                                       P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2951                                       0);
2952 }
2953
2954
2955 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2956                                     unsigned int freq)
2957 {
2958         return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2959 }
2960
2961
2962 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2963 {
2964         reg->channel[reg->channels] = chan;
2965         reg->channels++;
2966 }
2967
2968
2969 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2970                                      struct p2p_channels *chan,
2971                                      struct p2p_channels *cli_chan)
2972 {
2973         int i, cla = 0;
2974
2975         os_memset(cli_chan, 0, sizeof(*cli_chan));
2976
2977         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2978                    "band");
2979
2980         /* Operating class 81 - 2.4 GHz band channels 1..13 */
2981         chan->reg_class[cla].reg_class = 81;
2982         chan->reg_class[cla].channels = 0;
2983         for (i = 0; i < 11; i++) {
2984                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2985                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2986         }
2987         if (chan->reg_class[cla].channels)
2988                 cla++;
2989
2990         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2991                    "band");
2992
2993         /* Operating class 115 - 5 GHz, channels 36-48 */
2994         chan->reg_class[cla].reg_class = 115;
2995         chan->reg_class[cla].channels = 0;
2996         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2997                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2998         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2999                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3000         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3001                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3002         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3003                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3004         if (chan->reg_class[cla].channels)
3005                 cla++;
3006
3007         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3008                    "band");
3009
3010         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3011         chan->reg_class[cla].reg_class = 124;
3012         chan->reg_class[cla].channels = 0;
3013         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3014                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3015         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3016                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3017         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3018                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3019         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3020                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3021         if (chan->reg_class[cla].channels)
3022                 cla++;
3023
3024         chan->reg_classes = cla;
3025         return 0;
3026 }
3027
3028
3029 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3030                                           u16 num_modes,
3031                                           enum hostapd_hw_mode mode)
3032 {
3033         u16 i;
3034
3035         for (i = 0; i < num_modes; i++) {
3036                 if (modes[i].mode == mode)
3037                         return &modes[i];
3038         }
3039
3040         return NULL;
3041 }
3042
3043
3044 enum chan_allowed {
3045         NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3046 };
3047
3048 static int has_channel(struct wpa_global *global,
3049                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
3050 {
3051         int i;
3052         unsigned int freq;
3053
3054         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3055                 chan * 5;
3056         if (wpas_p2p_disallowed_freq(global, freq))
3057                 return NOT_ALLOWED;
3058
3059         for (i = 0; i < mode->num_channels; i++) {
3060                 if (mode->channels[i].chan == chan) {
3061                         if (flags)
3062                                 *flags = mode->channels[i].flag;
3063                         if (mode->channels[i].flag &
3064                             (HOSTAPD_CHAN_DISABLED |
3065                              HOSTAPD_CHAN_RADAR))
3066                                 return NOT_ALLOWED;
3067                         if (mode->channels[i].flag &
3068                             (HOSTAPD_CHAN_PASSIVE_SCAN |
3069                              HOSTAPD_CHAN_NO_IBSS))
3070                                 return PASSIVE_ONLY;
3071                         return ALLOWED;
3072                 }
3073         }
3074
3075         return NOT_ALLOWED;
3076 }
3077
3078
3079 struct p2p_oper_class_map {
3080         enum hostapd_hw_mode mode;
3081         u8 op_class;
3082         u8 min_chan;
3083         u8 max_chan;
3084         u8 inc;
3085         enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3086 };
3087
3088 static struct p2p_oper_class_map op_class[] = {
3089         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3090 #if 0 /* Do not enable HT40 on 2 GHz for now */
3091         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3092         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3093 #endif
3094         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3095         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3096         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3097         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3098         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3099         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3100
3101         /*
3102          * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3103          * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3104          * 80 MHz, but currently use the following definition for simplicity
3105          * (these center frequencies are not actual channels, which makes
3106          * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3107          * removing invalid channels.
3108          */
3109         { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3110         { -1, 0, 0, 0, 0, BW20 }
3111 };
3112
3113
3114 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3115                                      struct hostapd_hw_modes *mode,
3116                                      u8 channel)
3117 {
3118         u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3119         unsigned int i;
3120
3121         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3122                 return 0;
3123
3124         for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3125                 /*
3126                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3127                  * so the center channel is 6 channels away from the start/end.
3128                  */
3129                 if (channel >= center_channels[i] - 6 &&
3130                     channel <= center_channels[i] + 6)
3131                         return center_channels[i];
3132
3133         return 0;
3134 }
3135
3136
3137 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3138                                                struct hostapd_hw_modes *mode,
3139                                                u8 channel, u8 bw)
3140 {
3141         u8 center_chan;
3142         int i, flags;
3143         enum chan_allowed res, ret = ALLOWED;
3144
3145         center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3146         if (!center_chan)
3147                 return NOT_ALLOWED;
3148         if (center_chan >= 58 && center_chan <= 138)
3149                 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3150
3151         /* check all the channels are available */
3152         for (i = 0; i < 4; i++) {
3153                 int adj_chan = center_chan - 6 + i * 4;
3154
3155                 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3156                 if (res == NOT_ALLOWED)
3157                         return NOT_ALLOWED;
3158                 if (res == PASSIVE_ONLY)
3159                         ret = PASSIVE_ONLY;
3160
3161                 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3162                         return NOT_ALLOWED;
3163                 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3164                         return NOT_ALLOWED;
3165                 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3166                         return NOT_ALLOWED;
3167                 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3168                         return NOT_ALLOWED;
3169         }
3170
3171         return ret;
3172 }
3173
3174
3175 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3176                                                  struct hostapd_hw_modes *mode,
3177                                                  u8 channel, u8 bw)
3178 {
3179         int flag;
3180         enum chan_allowed res, res2;
3181
3182         res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3183         if (bw == BW40MINUS) {
3184                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3185                         return NOT_ALLOWED;
3186                 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3187         } else if (bw == BW40PLUS) {
3188                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3189                         return NOT_ALLOWED;
3190                 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3191         } else if (bw == BW80) {
3192                 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3193         }
3194
3195         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3196                 return NOT_ALLOWED;
3197         if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3198                 return PASSIVE_ONLY;
3199         return res;
3200 }
3201
3202
3203 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3204                                    struct p2p_channels *chan,
3205                                    struct p2p_channels *cli_chan)
3206 {
3207         struct hostapd_hw_modes *mode;
3208         int cla, op, cli_cla;
3209
3210         if (wpa_s->hw.modes == NULL) {
3211                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3212                            "of all supported channels; assume dualband "
3213                            "support");
3214                 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3215         }
3216
3217         cla = cli_cla = 0;
3218
3219         for (op = 0; op_class[op].op_class; op++) {
3220                 struct p2p_oper_class_map *o = &op_class[op];
3221                 u8 ch;
3222                 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3223
3224                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3225                 if (mode == NULL)
3226                         continue;
3227                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3228                         enum chan_allowed res;
3229                         res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3230                         if (res == ALLOWED) {
3231                                 if (reg == NULL) {
3232                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3233                                                    o->op_class);
3234                                         reg = &chan->reg_class[cla];
3235                                         cla++;
3236                                         reg->reg_class = o->op_class;
3237                                 }
3238                                 reg->channel[reg->channels] = ch;
3239                                 reg->channels++;
3240                         } else if (res == PASSIVE_ONLY &&
3241                                    wpa_s->conf->p2p_add_cli_chan) {
3242                                 if (cli_reg == NULL) {
3243                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3244                                                    o->op_class);
3245                                         cli_reg = &cli_chan->reg_class[cli_cla];
3246                                         cli_cla++;
3247                                         cli_reg->reg_class = o->op_class;
3248                                 }
3249                                 cli_reg->channel[cli_reg->channels] = ch;
3250                                 cli_reg->channels++;
3251                         }
3252                 }
3253                 if (reg) {
3254                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3255                                     reg->channel, reg->channels);
3256                 }
3257                 if (cli_reg) {
3258                         wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3259                                     cli_reg->channel, cli_reg->channels);
3260                 }
3261         }
3262
3263         chan->reg_classes = cla;
3264         cli_chan->reg_classes = cli_cla;
3265
3266         return 0;
3267 }
3268
3269
3270 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3271                            struct hostapd_hw_modes *mode, u8 channel)
3272 {
3273         int op;
3274         enum chan_allowed ret;
3275
3276         for (op = 0; op_class[op].op_class; op++) {
3277                 struct p2p_oper_class_map *o = &op_class[op];
3278                 u8 ch;
3279
3280                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3281                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3282                             o->bw == BW20 || ch != channel)
3283                                 continue;
3284                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3285                         if (ret == ALLOWED)
3286                                 return (o->bw == BW40MINUS) ? -1 : 1;
3287                 }
3288         }
3289         return 0;
3290 }
3291
3292
3293 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3294                               struct hostapd_hw_modes *mode, u8 channel)
3295 {
3296         if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3297                 return 0;
3298
3299         return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3300 }
3301
3302
3303 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3304                         size_t buf_len)
3305 {
3306         struct wpa_supplicant *wpa_s = ctx;
3307
3308         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3309                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3310                         break;
3311         }
3312         if (wpa_s == NULL)
3313                 return -1;
3314
3315         return wpa_drv_get_noa(wpa_s, buf, buf_len);
3316 }
3317
3318
3319 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3320 {
3321         struct wpa_supplicant *wpa_s = ctx;
3322
3323         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3324                 struct wpa_ssid *ssid = wpa_s->current_ssid;
3325                 if (ssid == NULL)
3326                         continue;
3327                 if (ssid->mode != WPAS_MODE_INFRA)
3328                         continue;
3329                 if (wpa_s->wpa_state != WPA_COMPLETED &&
3330                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3331                         continue;
3332                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3333                         return 1;
3334         }
3335
3336         return 0;
3337 }
3338
3339
3340 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3341 {
3342         struct wpa_supplicant *wpa_s = ctx;
3343         wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3344 }
3345
3346
3347 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3348 {
3349         struct wpa_interface iface;
3350         struct wpa_supplicant *p2pdev_wpa_s;
3351         char ifname[100];
3352         char force_name[100];
3353         int ret;
3354
3355         os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3356                     wpa_s->ifname);
3357         force_name[0] = '\0';
3358         wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3359         ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3360                              force_name, wpa_s->pending_interface_addr, NULL);
3361         if (ret < 0) {
3362                 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3363                 return ret;
3364         }
3365         os_strlcpy(wpa_s->pending_interface_name, ifname,
3366                    sizeof(wpa_s->pending_interface_name));
3367
3368         os_memset(&iface, 0, sizeof(iface));
3369         iface.p2p_mgmt = 1;
3370         iface.ifname = wpa_s->pending_interface_name;
3371         iface.driver = wpa_s->driver->name;
3372         iface.driver_param = wpa_s->conf->driver_param;
3373         iface.confname = wpa_s->confname;
3374         p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3375         if (!p2pdev_wpa_s) {
3376                 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3377                 return -1;
3378         }
3379         p2pdev_wpa_s->parent = wpa_s;
3380
3381         wpa_s->pending_interface_name[0] = '\0';
3382         return 0;
3383 }
3384
3385
3386 /**
3387  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3388  * @global: Pointer to global data from wpa_supplicant_init()
3389  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3390  * Returns: 0 on success, -1 on failure
3391  */
3392 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3393 {
3394         struct p2p_config p2p;
3395         unsigned int r;
3396         int i;
3397
3398         if (wpa_s->conf->p2p_disabled)
3399                 return 0;
3400
3401         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3402                 return 0;
3403
3404         if (global->p2p)
3405                 return 0;
3406
3407         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3408                 struct p2p_params params;
3409
3410                 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3411                 os_memset(&params, 0, sizeof(params));
3412                 params.dev_name = wpa_s->conf->device_name;
3413                 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3414                           WPS_DEV_TYPE_LEN);
3415                 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3416                 os_memcpy(params.sec_dev_type,
3417                           wpa_s->conf->sec_device_type,
3418                           params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3419
3420                 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3421                         return -1;
3422
3423                 return 0;
3424         }
3425
3426         os_memset(&p2p, 0, sizeof(p2p));
3427         p2p.cb_ctx = wpa_s;
3428         p2p.debug_print = wpas_p2p_debug_print;
3429         p2p.p2p_scan = wpas_p2p_scan;
3430         p2p.send_action = wpas_send_action;
3431         p2p.send_action_done = wpas_send_action_done;
3432         p2p.go_neg_completed = wpas_go_neg_completed;
3433         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3434         p2p.dev_found = wpas_dev_found;
3435         p2p.dev_lost = wpas_dev_lost;
3436         p2p.find_stopped = wpas_find_stopped;
3437         p2p.start_listen = wpas_start_listen;
3438         p2p.stop_listen = wpas_stop_listen;
3439         p2p.send_probe_resp = wpas_send_probe_resp;
3440         p2p.sd_request = wpas_sd_request;
3441         p2p.sd_response = wpas_sd_response;
3442         p2p.prov_disc_req = wpas_prov_disc_req;
3443         p2p.prov_disc_resp = wpas_prov_disc_resp;
3444         p2p.prov_disc_fail = wpas_prov_disc_fail;
3445         p2p.invitation_process = wpas_invitation_process;
3446         p2p.invitation_received = wpas_invitation_received;
3447         p2p.invitation_result = wpas_invitation_result;
3448         p2p.get_noa = wpas_get_noa;
3449         p2p.go_connected = wpas_go_connected;
3450
3451         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3452         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3453         p2p.dev_name = wpa_s->conf->device_name;
3454         p2p.manufacturer = wpa_s->conf->manufacturer;
3455         p2p.model_name = wpa_s->conf->model_name;
3456         p2p.model_number = wpa_s->conf->model_number;
3457         p2p.serial_number = wpa_s->conf->serial_number;
3458         if (wpa_s->wps) {
3459                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3460                 p2p.config_methods = wpa_s->wps->config_methods;
3461         }
3462
3463         if (wpa_s->conf->p2p_listen_reg_class &&
3464             wpa_s->conf->p2p_listen_channel) {
3465                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3466                 p2p.channel = wpa_s->conf->p2p_listen_channel;
3467         } else {
3468                 p2p.reg_class = 81;
3469                 /*
3470                  * Pick one of the social channels randomly as the listen
3471                  * channel.
3472                  */
3473                 os_get_random((u8 *) &r, sizeof(r));
3474                 p2p.channel = 1 + (r % 3) * 5;
3475         }
3476         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3477
3478         if (wpa_s->conf->p2p_oper_reg_class &&
3479             wpa_s->conf->p2p_oper_channel) {
3480                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3481                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3482                 p2p.cfg_op_channel = 1;
3483                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3484                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
3485
3486         } else {
3487                 p2p.op_reg_class = 81;
3488                 /*
3489                  * Use random operation channel from (1, 6, 11) if no other
3490                  * preference is indicated.
3491                  */
3492                 os_get_random((u8 *) &r, sizeof(r));
3493                 p2p.op_channel = 1 + (r % 3) * 5;
3494                 p2p.cfg_op_channel = 0;
3495                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3496                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
3497         }
3498
3499         if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3500                 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3501                 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3502         }
3503
3504         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3505                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3506                 p2p.country[2] = 0x04;
3507         } else
3508                 os_memcpy(p2p.country, "XX\x04", 3);
3509
3510         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3511                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3512                            "channel list");
3513                 return -1;
3514         }
3515
3516         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3517                   WPS_DEV_TYPE_LEN);
3518
3519         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3520         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3521                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3522
3523         p2p.concurrent_operations = !!(wpa_s->drv_flags &
3524                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3525
3526         p2p.max_peers = 100;
3527
3528         if (wpa_s->conf->p2p_ssid_postfix) {
3529                 p2p.ssid_postfix_len =
3530                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
3531                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3532                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3533                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3534                           p2p.ssid_postfix_len);
3535         }
3536
3537         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3538
3539         p2p.max_listen = wpa_s->max_remain_on_chan;
3540
3541         global->p2p = p2p_init(&p2p);
3542         if (global->p2p == NULL)
3543                 return -1;
3544         global->p2p_init_wpa_s = wpa_s;
3545
3546         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3547                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3548                         continue;
3549                 p2p_add_wps_vendor_extension(
3550                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3551         }
3552
3553         p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3554
3555         return 0;
3556 }
3557
3558
3559 /**
3560  * wpas_p2p_deinit - Deinitialize per-interface P2P data
3561  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3562  *
3563  * This function deinitialize per-interface P2P data.
3564  */
3565 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3566 {
3567         if (wpa_s->driver && wpa_s->drv_priv)
3568                 wpa_drv_probe_req_report(wpa_s, 0);
3569
3570         if (wpa_s->go_params) {
3571                 /* Clear any stored provisioning info */
3572                 p2p_clear_provisioning_info(
3573                         wpa_s->global->p2p,
3574                         wpa_s->go_params->peer_device_addr);
3575         }
3576
3577         os_free(wpa_s->go_params);
3578         wpa_s->go_params = NULL;
3579         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3580         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3581         wpa_s->p2p_long_listen = 0;
3582         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3583         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3584         wpas_p2p_remove_pending_group_interface(wpa_s);
3585         eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3586
3587         /* TODO: remove group interface from the driver if this wpa_s instance
3588          * is on top of a P2P group interface */
3589 }
3590
3591
3592 /**
3593  * wpas_p2p_deinit_global - Deinitialize global P2P module
3594  * @global: Pointer to global data from wpa_supplicant_init()
3595  *
3596  * This function deinitializes the global (per device) P2P module.
3597  */
3598 void wpas_p2p_deinit_global(struct wpa_global *global)
3599 {
3600         struct wpa_supplicant *wpa_s, *tmp;
3601
3602         wpa_s = global->ifaces;
3603         if (wpa_s)
3604                 wpas_p2p_service_flush(wpa_s);
3605
3606         if (global->p2p == NULL)
3607                 return;
3608
3609         /* Remove remaining P2P group interfaces */
3610         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3611                 wpa_s = wpa_s->next;
3612         while (wpa_s) {
3613                 tmp = global->ifaces;
3614                 while (tmp &&
3615                        (tmp == wpa_s ||
3616                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3617                         tmp = tmp->next;
3618                 }
3619                 if (tmp == NULL)
3620                         break;
3621                 /* Disconnect from the P2P group and deinit the interface */
3622                 wpas_p2p_disconnect(tmp);
3623         }
3624
3625         /*
3626          * Deinit GO data on any possibly remaining interface (if main
3627          * interface is used as GO).
3628          */
3629         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3630                 if (wpa_s->ap_iface)
3631                         wpas_p2p_group_deinit(wpa_s);
3632         }
3633
3634         p2p_deinit(global->p2p);
3635         global->p2p = NULL;
3636         global->p2p_init_wpa_s = NULL;
3637 }
3638
3639
3640 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3641 {
3642         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3643             wpa_s->conf->p2p_no_group_iface)
3644                 return 0; /* separate interface disabled per configuration */
3645         if (wpa_s->drv_flags &
3646             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3647              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3648                 return 1; /* P2P group requires a new interface in every case
3649                            */
3650         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3651                 return 0; /* driver does not support concurrent operations */
3652         if (wpa_s->global->ifaces->next)
3653                 return 1; /* more that one interface already in use */
3654         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3655                 return 1; /* this interface is already in use */
3656         return 0;
3657 }
3658
3659
3660 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3661                                  const u8 *peer_addr,
3662                                  enum p2p_wps_method wps_method,
3663                                  int go_intent, const u8 *own_interface_addr,
3664                                  unsigned int force_freq, int persistent_group,
3665                                  struct wpa_ssid *ssid, unsigned int pref_freq)
3666 {
3667         if (persistent_group && wpa_s->conf->persistent_reconnect)
3668                 persistent_group = 2;
3669
3670         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3671                 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3672                                            go_intent, own_interface_addr,
3673                                            force_freq, persistent_group);
3674         }
3675
3676         /*
3677          * Increase GO config timeout if HT40 is used since it takes some time
3678          * to scan channels for coex purposes before the BSS can be started.
3679          */
3680         p2p_set_config_timeout(wpa_s->global->p2p,
3681                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3682
3683         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3684                            go_intent, own_interface_addr, force_freq,
3685                            persistent_group, ssid ? ssid->ssid : NULL,
3686                            ssid ? ssid->ssid_len : 0,
3687                            wpa_s->p2p_pd_before_go_neg, pref_freq);
3688 }
3689
3690
3691 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3692                                 const u8 *peer_addr,
3693                                 enum p2p_wps_method wps_method,
3694                                 int go_intent, const u8 *own_interface_addr,
3695                                 unsigned int force_freq, int persistent_group,
3696                                 struct wpa_ssid *ssid, unsigned int pref_freq)
3697 {
3698         if (persistent_group && wpa_s->conf->persistent_reconnect)
3699                 persistent_group = 2;
3700
3701         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3702                 return -1;
3703
3704         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3705                              go_intent, own_interface_addr, force_freq,
3706                              persistent_group, ssid ? ssid->ssid : NULL,
3707                              ssid ? ssid->ssid_len : 0, pref_freq);
3708 }
3709
3710
3711 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3712 {
3713         wpa_s->p2p_join_scan_count++;
3714         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3715                    wpa_s->p2p_join_scan_count);
3716         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3717                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3718                            " for join operationg - stop join attempt",
3719                            MAC2STR(wpa_s->pending_join_iface_addr));
3720                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3721                 if (wpa_s->p2p_auto_pd) {
3722                         wpa_s->p2p_auto_pd = 0;
3723                         wpa_msg_global(wpa_s, MSG_INFO,
3724                                        P2P_EVENT_PROV_DISC_FAILURE
3725                                        " p2p_dev_addr=" MACSTR " status=N/A",
3726                                        MAC2STR(wpa_s->pending_join_dev_addr));
3727                         return;
3728                 }
3729                 wpa_msg_global(wpa_s->parent, MSG_INFO,
3730                                P2P_EVENT_GROUP_FORMATION_FAILURE);
3731         }
3732 }
3733
3734
3735 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3736 {
3737         int *freqs, res, num, i;
3738
3739         if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3740                 /* Multiple channels are supported and not all are in use */
3741                 return 0;
3742         }
3743
3744         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3745         if (!freqs)
3746                 return 1;
3747
3748         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3749                                         wpa_s->num_multichan_concurrent);
3750         if (num < 0) {
3751                 res = 1;
3752                 goto exit_free;
3753         }
3754
3755         for (i = 0; i < num; i++) {
3756                 if (freqs[i] == freq) {
3757                         wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3758                                    freq);
3759                         res = 0;
3760                         goto exit_free;
3761                 }
3762         }
3763
3764         res = 1;
3765
3766 exit_free:
3767         os_free(freqs);
3768         return res;
3769 }
3770
3771
3772 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3773                             const u8 *peer_dev_addr)
3774 {
3775         struct wpa_bss *bss;
3776         int updated;
3777
3778         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3779         if (bss == NULL)
3780                 return -1;
3781         if (bss->last_update_idx < wpa_s->bss_update_idx) {
3782                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3783                            "last scan");
3784                 return 0;
3785         }
3786
3787         updated = os_reltime_before(&wpa_s->p2p_auto_started,
3788                                     &bss->last_update);
3789         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3790                    "%ld.%06ld (%supdated in last scan)",
3791                    bss->last_update.sec, bss->last_update.usec,
3792                    updated ? "": "not ");
3793
3794         return updated;
3795 }
3796
3797
3798 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3799                                    struct wpa_scan_results *scan_res)
3800 {
3801         struct wpa_bss *bss;
3802         int freq;
3803         u8 iface_addr[ETH_ALEN];
3804
3805         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3806
3807         if (wpa_s->global->p2p_disabled)
3808                 return;
3809
3810         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3811                    scan_res ? (int) scan_res->num : -1,
3812                    wpa_s->p2p_auto_join ? "auto_" : "");
3813
3814         if (scan_res)
3815                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3816
3817         if (wpa_s->p2p_auto_pd) {
3818                 int join = wpas_p2p_peer_go(wpa_s,
3819                                             wpa_s->pending_join_dev_addr);
3820                 if (join == 0 &&
3821                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3822                         wpa_s->auto_pd_scan_retry++;
3823                         bss = wpa_bss_get_bssid_latest(
3824                                 wpa_s, wpa_s->pending_join_dev_addr);
3825                         if (bss) {
3826                                 freq = bss->freq;
3827                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3828                                            "the peer " MACSTR " at %d MHz",
3829                                            wpa_s->auto_pd_scan_retry,
3830                                            MAC2STR(wpa_s->
3831                                                    pending_join_dev_addr),
3832                                            freq);
3833                                 wpas_p2p_join_scan_req(wpa_s, freq);
3834                                 return;
3835                         }
3836                 }
3837
3838                 if (join < 0)
3839                         join = 0;
3840
3841                 wpa_s->p2p_auto_pd = 0;
3842                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3843                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3844                            MAC2STR(wpa_s->pending_join_dev_addr), join);
3845                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3846                                       wpa_s->pending_join_dev_addr,
3847                                       wpa_s->pending_pd_config_methods, join,
3848                                       0, wpa_s->user_initiated_pd) < 0) {
3849                         wpa_s->p2p_auto_pd = 0;
3850                         wpa_msg_global(wpa_s, MSG_INFO,
3851                                        P2P_EVENT_PROV_DISC_FAILURE
3852                                        " p2p_dev_addr=" MACSTR " status=N/A",
3853                                        MAC2STR(wpa_s->pending_join_dev_addr));
3854                 }
3855                 return;
3856         }
3857
3858         if (wpa_s->p2p_auto_join) {
3859                 int join = wpas_p2p_peer_go(wpa_s,
3860                                             wpa_s->pending_join_dev_addr);
3861                 if (join < 0) {
3862                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3863                                    "running a GO -> use GO Negotiation");
3864                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3865                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3866                                          wpa_s->p2p_persistent_group, 0, 0, 0,
3867                                          wpa_s->p2p_go_intent,
3868                                          wpa_s->p2p_connect_freq,
3869                                          wpa_s->p2p_persistent_id,
3870                                          wpa_s->p2p_pd_before_go_neg,
3871                                          wpa_s->p2p_go_ht40,
3872                                          wpa_s->p2p_go_vht);
3873                         return;
3874                 }
3875
3876                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3877                            "try to join the group", join ? "" :
3878                            " in older scan");
3879                 if (!join)
3880                         wpa_s->p2p_fallback_to_go_neg = 1;
3881         }
3882
3883         freq = p2p_get_oper_freq(wpa_s->global->p2p,
3884                                  wpa_s->pending_join_iface_addr);
3885         if (freq < 0 &&
3886             p2p_get_interface_addr(wpa_s->global->p2p,
3887                                    wpa_s->pending_join_dev_addr,
3888                                    iface_addr) == 0 &&
3889             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3890         {
3891                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3892                            "address for join from " MACSTR " to " MACSTR
3893                            " based on newly discovered P2P peer entry",
3894                            MAC2STR(wpa_s->pending_join_iface_addr),
3895                            MAC2STR(iface_addr));
3896                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3897                           ETH_ALEN);
3898
3899                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3900                                          wpa_s->pending_join_iface_addr);
3901         }
3902         if (freq >= 0) {
3903                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3904                            "from P2P peer table: %d MHz", freq);
3905         }
3906         bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3907         if (bss) {
3908                 freq = bss->freq;
3909                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3910                            "from BSS table: %d MHz (SSID %s)", freq,
3911                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
3912         }
3913         if (freq > 0) {
3914                 u16 method;
3915
3916                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3917                         wpa_msg_global(wpa_s->parent, MSG_INFO,
3918                                        P2P_EVENT_GROUP_FORMATION_FAILURE
3919                                        "reason=FREQ_CONFLICT");
3920                         return;
3921                 }
3922
3923                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3924                            "prior to joining an existing group (GO " MACSTR
3925                            " freq=%u MHz)",
3926                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
3927                 wpa_s->pending_pd_before_join = 1;
3928
3929                 switch (wpa_s->pending_join_wps_method) {
3930                 case WPS_PIN_DISPLAY:
3931                         method = WPS_CONFIG_KEYPAD;
3932                         break;
3933                 case WPS_PIN_KEYPAD:
3934                         method = WPS_CONFIG_DISPLAY;
3935                         break;
3936                 case WPS_PBC:
3937                         method = WPS_CONFIG_PUSHBUTTON;
3938                         break;
3939                 default:
3940                         method = 0;
3941                         break;
3942                 }
3943
3944                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3945                                                wpa_s->pending_join_dev_addr) ==
3946                      method)) {
3947                         /*
3948                          * We have already performed provision discovery for
3949                          * joining the group. Proceed directly to join
3950                          * operation without duplicated provision discovery. */
3951                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3952                                    "with " MACSTR " already done - proceed to "
3953                                    "join",
3954                                    MAC2STR(wpa_s->pending_join_dev_addr));
3955                         wpa_s->pending_pd_before_join = 0;
3956                         goto start;
3957                 }
3958
3959                 if (p2p_prov_disc_req(wpa_s->global->p2p,
3960                                       wpa_s->pending_join_dev_addr, method, 1,
3961                                       freq, wpa_s->user_initiated_pd) < 0) {
3962                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3963                                    "Discovery Request before joining an "
3964                                    "existing group");
3965                         wpa_s->pending_pd_before_join = 0;
3966                         goto start;
3967                 }
3968                 return;
3969         }
3970
3971         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3972         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3973         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3974         wpas_p2p_check_join_scan_limit(wpa_s);
3975         return;
3976
3977 start:
3978         /* Start join operation immediately */
3979         wpas_p2p_join_start(wpa_s);
3980 }
3981
3982
3983 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3984 {
3985         int ret;
3986         struct wpa_driver_scan_params params;
3987         struct wpabuf *wps_ie, *ies;
3988         size_t ielen;
3989         int freqs[2] = { 0, 0 };
3990
3991         os_memset(&params, 0, sizeof(params));
3992
3993         /* P2P Wildcard SSID */
3994         params.num_ssids = 1;
3995         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3996         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3997
3998         wpa_s->wps->dev.p2p = 1;
3999         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4000                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4001                                         NULL);
4002         if (wps_ie == NULL) {
4003                 wpas_p2p_scan_res_join(wpa_s, NULL);
4004                 return;
4005         }
4006
4007         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4008         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4009         if (ies == NULL) {
4010                 wpabuf_free(wps_ie);
4011                 wpas_p2p_scan_res_join(wpa_s, NULL);
4012                 return;
4013         }
4014         wpabuf_put_buf(ies, wps_ie);
4015         wpabuf_free(wps_ie);
4016
4017         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4018
4019         params.p2p_probe = 1;
4020         params.extra_ies = wpabuf_head(ies);
4021         params.extra_ies_len = wpabuf_len(ies);
4022         if (freq > 0) {
4023                 freqs[0] = freq;
4024                 params.freqs = freqs;
4025         }
4026
4027         /*
4028          * Run a scan to update BSS table and start Provision Discovery once
4029          * the new scan results become available.
4030          */
4031         ret = wpa_drv_scan(wpa_s, &params);
4032         if (!ret) {
4033                 os_get_reltime(&wpa_s->scan_trigger_time);
4034                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4035                 wpa_s->own_scan_requested = 1;
4036         }
4037
4038         wpabuf_free(ies);
4039
4040         if (ret) {
4041                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4042                            "try again later");
4043                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4044                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4045                 wpas_p2p_check_join_scan_limit(wpa_s);
4046         }
4047 }
4048
4049
4050 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4051 {
4052         struct wpa_supplicant *wpa_s = eloop_ctx;
4053         wpas_p2p_join_scan_req(wpa_s, 0);
4054 }
4055
4056
4057 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4058                          const u8 *dev_addr, enum p2p_wps_method wps_method,
4059                          int auto_join)
4060 {
4061         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4062                    MACSTR " dev " MACSTR ")%s",
4063                    MAC2STR(iface_addr), MAC2STR(dev_addr),
4064                    auto_join ? " (auto_join)" : "");
4065
4066         wpa_s->p2p_auto_pd = 0;
4067         wpa_s->p2p_auto_join = !!auto_join;
4068         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4069         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4070         wpa_s->pending_join_wps_method = wps_method;
4071
4072         /* Make sure we are not running find during connection establishment */
4073         wpas_p2p_stop_find(wpa_s);
4074
4075         wpa_s->p2p_join_scan_count = 0;
4076         wpas_p2p_join_scan(wpa_s, NULL);
4077         return 0;
4078 }
4079
4080
4081 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4082 {
4083         struct wpa_supplicant *group;
4084         struct p2p_go_neg_results res;
4085         struct wpa_bss *bss;
4086
4087         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4088         if (group == NULL)
4089                 return -1;
4090         if (group != wpa_s) {
4091                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4092                           sizeof(group->p2p_pin));
4093                 group->p2p_wps_method = wpa_s->p2p_wps_method;
4094         } else {
4095                 /*
4096                  * Need to mark the current interface for p2p_group_formation
4097                  * when a separate group interface is not used. This is needed
4098                  * to allow p2p_cancel stop a pending p2p_connect-join.
4099                  * wpas_p2p_init_group_interface() addresses this for the case
4100                  * where a separate group interface is used.
4101                  */
4102                 wpa_s->global->p2p_group_formation = wpa_s;
4103         }
4104
4105         group->p2p_in_provisioning = 1;
4106         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4107
4108         os_memset(&res, 0, sizeof(res));
4109         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4110                   ETH_ALEN);
4111         res.wps_method = wpa_s->pending_join_wps_method;
4112         bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
4113         if (bss) {
4114                 res.freq = bss->freq;
4115                 res.ssid_len = bss->ssid_len;
4116                 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4117                 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4118                            "from BSS table: %d MHz (SSID %s)", bss->freq,
4119                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
4120         }
4121
4122         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4123                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4124                            "starting client");
4125                 wpa_drv_cancel_remain_on_channel(wpa_s);
4126                 wpa_s->off_channel_freq = 0;
4127                 wpa_s->roc_waiting_drv_freq = 0;
4128         }
4129         wpas_start_wps_enrollee(group, &res);
4130
4131         /*
4132          * Allow a longer timeout for join-a-running-group than normal 15
4133          * second group formation timeout since the GO may not have authorized
4134          * our connection yet.
4135          */
4136         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4137         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4138                                wpa_s, NULL);
4139
4140         return 0;
4141 }
4142
4143
4144 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4145                                 int *force_freq, int *pref_freq, int go)
4146 {
4147         int *freqs, res;
4148         unsigned int freq_in_use = 0, num, i;
4149
4150         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4151         if (!freqs)
4152                 return -1;
4153
4154         num = get_shared_radio_freqs(wpa_s, freqs,
4155                                      wpa_s->num_multichan_concurrent);
4156         wpa_printf(MSG_DEBUG,
4157                    "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4158                    freq, wpa_s->num_multichan_concurrent, num);
4159
4160         if (freq > 0) {
4161                 int ret;
4162                 if (go)
4163                         ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4164                 else
4165                         ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4166                 if (!ret) {
4167                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4168                                    "(%u MHz) is not supported for P2P uses",
4169                                    freq);
4170                         res = -3;
4171                         goto exit_free;
4172                 }
4173
4174                 for (i = 0; i < num; i++) {
4175                         if (freqs[i] == freq)
4176                                 freq_in_use = 1;
4177                 }
4178
4179                 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4180                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4181                                    freq);
4182                         res = -2;
4183                         goto exit_free;
4184                 }
4185                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4186                            "requested channel (%u MHz)", freq);
4187                 *force_freq = freq;
4188                 goto exit_ok;
4189         }
4190
4191         for (i = 0; i < num; i++) {
4192                 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4193                         continue;
4194
4195                 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4196                         wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4197                                    freqs[i]);
4198                         *pref_freq = freqs[i];
4199                 } else {
4200                         wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4201                                    freqs[i]);
4202                         *force_freq = freqs[i];
4203                 }
4204                 break;
4205         }
4206
4207         if (i == num) {
4208                 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4209                         wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4210                         *force_freq = 0;
4211                 } else if (num < wpa_s->num_multichan_concurrent) {
4212                         wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4213                         *force_freq = 0;
4214                 } else {
4215                         wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4216                         res = -2;
4217                         goto exit_free;
4218                 }
4219         }
4220
4221 exit_ok:
4222         res = 0;
4223 exit_free:
4224         os_free(freqs);
4225         return res;
4226 }
4227
4228
4229 /**
4230  * wpas_p2p_connect - Request P2P Group Formation to be started
4231  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4232  * @peer_addr: Address of the peer P2P Device
4233  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4234  * @persistent_group: Whether to create a persistent group
4235  * @auto_join: Whether to select join vs. GO Negotiation automatically
4236  * @join: Whether to join an existing group (as a client) instead of starting
4237  *      Group Owner negotiation; @peer_addr is BSSID in that case
4238  * @auth: Whether to only authorize the connection instead of doing that and
4239  *      initiating Group Owner negotiation
4240  * @go_intent: GO Intent or -1 to use default
4241  * @freq: Frequency for the group or 0 for auto-selection
4242  * @persistent_id: Persistent group credentials to use for forcing GO
4243  *      parameters or -1 to generate new values (SSID/passphrase)
4244  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4245  *      interoperability workaround when initiating group formation
4246  * @ht40: Start GO with 40 MHz channel width
4247  * @vht:  Start GO with VHT support
4248  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4249  *      failure, -2 on failure due to channel not currently available,
4250  *      -3 if forced channel is not supported
4251  */
4252 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4253                      const char *pin, enum p2p_wps_method wps_method,
4254                      int persistent_group, int auto_join, int join, int auth,
4255                      int go_intent, int freq, int persistent_id, int pd,
4256                      int ht40, int vht)
4257 {
4258         int force_freq = 0, pref_freq = 0;
4259         int ret = 0, res;
4260         enum wpa_driver_if_type iftype;
4261         const u8 *if_addr;
4262         struct wpa_ssid *ssid = NULL;
4263
4264         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4265                 return -1;
4266
4267         if (persistent_id >= 0) {
4268                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4269                 if (ssid == NULL || ssid->disabled != 2 ||
4270                     ssid->mode != WPAS_MODE_P2P_GO)
4271                         return -1;
4272         }
4273
4274         os_free(wpa_s->global->add_psk);
4275         wpa_s->global->add_psk = NULL;
4276
4277         if (go_intent < 0)
4278                 go_intent = wpa_s->conf->p2p_go_intent;
4279
4280         if (!auth)
4281                 wpa_s->p2p_long_listen = 0;
4282
4283         wpa_s->p2p_wps_method = wps_method;
4284         wpa_s->p2p_persistent_group = !!persistent_group;
4285         wpa_s->p2p_persistent_id = persistent_id;
4286         wpa_s->p2p_go_intent = go_intent;
4287         wpa_s->p2p_connect_freq = freq;
4288         wpa_s->p2p_fallback_to_go_neg = 0;
4289         wpa_s->p2p_pd_before_go_neg = !!pd;
4290         wpa_s->p2p_go_ht40 = !!ht40;
4291         wpa_s->p2p_go_vht = !!vht;
4292
4293         if (pin)
4294                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4295         else if (wps_method == WPS_PIN_DISPLAY) {
4296                 ret = wps_generate_pin();
4297                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4298                             ret);
4299                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4300                            wpa_s->p2p_pin);
4301         } else
4302                 wpa_s->p2p_pin[0] = '\0';
4303
4304         if (join || auto_join) {
4305                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4306                 if (auth) {
4307                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4308                                    "connect a running group from " MACSTR,
4309                                    MAC2STR(peer_addr));
4310                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4311                         return ret;
4312                 }
4313                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4314                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4315                                            iface_addr) < 0) {
4316                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4317                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4318                                          dev_addr);
4319                 }
4320                 if (auto_join) {
4321                         os_get_reltime(&wpa_s->p2p_auto_started);
4322                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4323                                    "%ld.%06ld",
4324                                    wpa_s->p2p_auto_started.sec,
4325                                    wpa_s->p2p_auto_started.usec);
4326                 }
4327                 wpa_s->user_initiated_pd = 1;
4328                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4329                                   auto_join) < 0)
4330                         return -1;
4331                 return ret;
4332         }
4333
4334         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4335                                    go_intent == 15);
4336         if (res)
4337                 return res;
4338         wpas_p2p_set_own_freq_preference(wpa_s,
4339                                          force_freq ? force_freq : pref_freq);
4340
4341         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4342
4343         if (wpa_s->create_p2p_iface) {
4344                 /* Prepare to add a new interface for the group */
4345                 iftype = WPA_IF_P2P_GROUP;
4346                 if (go_intent == 15)
4347                         iftype = WPA_IF_P2P_GO;
4348                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4349                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4350                                    "interface for the group");
4351                         return -1;
4352                 }
4353
4354                 if_addr = wpa_s->pending_interface_addr;
4355         } else
4356                 if_addr = wpa_s->own_addr;
4357
4358         if (auth) {
4359                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4360                                          go_intent, if_addr,
4361                                          force_freq, persistent_group, ssid,
4362                                          pref_freq) < 0)
4363                         return -1;
4364                 return ret;
4365         }
4366
4367         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4368                                   go_intent, if_addr, force_freq,
4369                                   persistent_group, ssid, pref_freq) < 0) {
4370                 if (wpa_s->create_p2p_iface)
4371                         wpas_p2p_remove_pending_group_interface(wpa_s);
4372                 return -1;
4373         }
4374         return ret;
4375 }
4376
4377
4378 /**
4379  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4380  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4381  * @freq: Frequency of the channel in MHz
4382  * @duration: Duration of the stay on the channel in milliseconds
4383  *
4384  * This callback is called when the driver indicates that it has started the
4385  * requested remain-on-channel duration.
4386  */
4387 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4388                                    unsigned int freq, unsigned int duration)
4389 {
4390         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4391                 return;
4392         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4393                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4394                               wpa_s->pending_listen_duration);
4395                 wpa_s->pending_listen_freq = 0;
4396         }
4397 }
4398
4399
4400 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4401                                  unsigned int timeout)
4402 {
4403         /* Limit maximum Listen state time based on driver limitation. */
4404         if (timeout > wpa_s->max_remain_on_chan)
4405                 timeout = wpa_s->max_remain_on_chan;
4406
4407         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4408                 return wpa_drv_p2p_listen(wpa_s, timeout);
4409
4410         return p2p_listen(wpa_s->global->p2p, timeout);
4411 }
4412
4413
4414 /**
4415  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4416  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4417  * @freq: Frequency of the channel in MHz
4418  *
4419  * This callback is called when the driver indicates that a remain-on-channel
4420  * operation has been completed, i.e., the duration on the requested channel
4421  * has timed out.
4422  */
4423 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4424                                           unsigned int freq)
4425 {
4426         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4427                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
4428                    wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4429         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4430                 return;
4431         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4432                 return; /* P2P module started a new operation */
4433         if (offchannel_pending_action_tx(wpa_s))
4434                 return;
4435         if (wpa_s->p2p_long_listen > 0)
4436                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4437         if (wpa_s->p2p_long_listen > 0) {
4438                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4439                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4440         } else {
4441                 /*
4442                  * When listen duration is over, stop listen & update p2p_state
4443                  * to IDLE.
4444                  */
4445                 p2p_stop_listen(wpa_s->global->p2p);
4446         }
4447 }
4448
4449
4450 /**
4451  * wpas_p2p_group_remove - Remove a P2P group
4452  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4453  * @ifname: Network interface name of the group interface or "*" to remove all
4454  *      groups
4455  * Returns: 0 on success, -1 on failure
4456  *
4457  * This function is used to remove a P2P group. This can be used to disconnect
4458  * from a group in which the local end is a P2P Client or to end a P2P Group in
4459  * case the local end is the Group Owner. If a virtual network interface was
4460  * created for this group, that interface will be removed. Otherwise, only the
4461  * configured P2P group network will be removed from the interface.
4462  */
4463 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4464 {
4465         struct wpa_global *global = wpa_s->global;
4466
4467         if (os_strcmp(ifname, "*") == 0) {
4468                 struct wpa_supplicant *prev;
4469                 wpa_s = global->ifaces;
4470                 while (wpa_s) {
4471                         prev = wpa_s;
4472                         wpa_s = wpa_s->next;
4473                         if (prev->p2p_group_interface !=
4474                             NOT_P2P_GROUP_INTERFACE ||
4475                             (prev->current_ssid &&
4476                              prev->current_ssid->p2p_group))
4477                                 wpas_p2p_disconnect(prev);
4478                 }
4479                 return 0;
4480         }
4481
4482         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4483                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4484                         break;
4485         }
4486
4487         return wpas_p2p_disconnect(wpa_s);
4488 }
4489
4490
4491 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4492 {
4493         unsigned int r;
4494
4495         if (freq == 2) {
4496                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4497                            "band");
4498                 if (wpa_s->best_24_freq > 0 &&
4499                     p2p_supported_freq_go(wpa_s->global->p2p,
4500                                           wpa_s->best_24_freq)) {
4501                         freq = wpa_s->best_24_freq;
4502                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4503                                    "channel: %d MHz", freq);
4504                 } else {
4505                         os_get_random((u8 *) &r, sizeof(r));
4506                         freq = 2412 + (r % 3) * 25;
4507                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4508                                    "channel: %d MHz", freq);
4509                 }
4510         }
4511
4512         if (freq == 5) {
4513                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4514                            "band");
4515                 if (wpa_s->best_5_freq > 0 &&
4516                     p2p_supported_freq_go(wpa_s->global->p2p,
4517                                        wpa_s->best_5_freq)) {
4518                         freq = wpa_s->best_5_freq;
4519                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4520                                    "channel: %d MHz", freq);
4521                 } else {
4522                         os_get_random((u8 *) &r, sizeof(r));
4523                         freq = 5180 + (r % 4) * 20;
4524                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4525                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4526                                            "5 GHz channel for P2P group");
4527                                 return -1;
4528                         }
4529                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4530                                    "channel: %d MHz", freq);
4531                 }
4532         }
4533
4534         if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4535                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4536                            "(%u MHz) is not supported for P2P uses",
4537                            freq);
4538                 return -1;
4539         }
4540
4541         return freq;
4542 }
4543
4544
4545 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4546                                    struct p2p_go_neg_results *params,
4547                                    int freq, int ht40, int vht,
4548                                    const struct p2p_channels *channels)
4549 {
4550         int res, *freqs;
4551         unsigned int pref_freq;
4552         unsigned int num, i;
4553
4554         os_memset(params, 0, sizeof(*params));
4555         params->role_go = 1;
4556         params->ht40 = ht40;
4557         params->vht = vht;
4558         if (freq) {
4559                 if (!freq_included(channels, freq)) {
4560                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4561                                    "accepted", freq);
4562                         return -1;
4563                 }
4564                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4565                            "frequency %d MHz", freq);
4566                 params->freq = freq;
4567         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4568                    wpa_s->conf->p2p_oper_channel >= 1 &&
4569                    wpa_s->conf->p2p_oper_channel <= 11 &&
4570                    freq_included(channels,
4571                                  2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4572                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4573                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4574                            "frequency %d MHz", params->freq);
4575         } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4576                     wpa_s->conf->p2p_oper_reg_class == 116 ||
4577                     wpa_s->conf->p2p_oper_reg_class == 117 ||
4578                     wpa_s->conf->p2p_oper_reg_class == 124 ||
4579                     wpa_s->conf->p2p_oper_reg_class == 126 ||
4580                     wpa_s->conf->p2p_oper_reg_class == 127) &&
4581                    freq_included(channels,
4582                                  5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4583                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4584                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4585                            "frequency %d MHz", params->freq);
4586         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4587                    wpa_s->best_overall_freq > 0 &&
4588                    p2p_supported_freq_go(wpa_s->global->p2p,
4589                                          wpa_s->best_overall_freq) &&
4590                    freq_included(channels, wpa_s->best_overall_freq)) {
4591                 params->freq = wpa_s->best_overall_freq;
4592                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4593                            "channel %d MHz", params->freq);
4594         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4595                    wpa_s->best_24_freq > 0 &&
4596                    p2p_supported_freq_go(wpa_s->global->p2p,
4597                                          wpa_s->best_24_freq) &&
4598                    freq_included(channels, wpa_s->best_24_freq)) {
4599                 params->freq = wpa_s->best_24_freq;
4600                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4601                            "channel %d MHz", params->freq);
4602         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4603                    wpa_s->best_5_freq > 0 &&
4604                    p2p_supported_freq_go(wpa_s->global->p2p,
4605                                          wpa_s->best_5_freq) &&
4606                    freq_included(channels, wpa_s->best_5_freq)) {
4607                 params->freq = wpa_s->best_5_freq;
4608                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4609                            "channel %d MHz", params->freq);
4610         } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4611                                                   channels))) {
4612                 params->freq = pref_freq;
4613                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4614                            "channels", params->freq);
4615         } else {
4616                 int chan;
4617                 for (chan = 0; chan < 11; chan++) {
4618                         params->freq = 2412 + chan * 5;
4619                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
4620                                                       params->freq) &&
4621                             freq_included(channels, params->freq))
4622                                 break;
4623                 }
4624                 if (chan == 11) {
4625                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4626                                    "allowed");
4627                         return -1;
4628                 }
4629                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4630                            "known)", params->freq);
4631         }
4632
4633         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4634         if (!freqs)
4635                 return -1;
4636
4637         res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4638                                         wpa_s->num_multichan_concurrent);
4639         if (res < 0) {
4640                 os_free(freqs);
4641                 return -1;
4642         }
4643         num = res;
4644
4645         for (i = 0; i < num; i++) {
4646                 if (freq && freqs[i] == freq)
4647                         break;
4648                 if (!freq && freq_included(channels, freqs[i])) {
4649                         wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4650                                    freqs[i]);
4651                         params->freq = freqs[i];
4652                         break;
4653                 }
4654         }
4655
4656         if (i == num) {
4657                 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4658                         if (freq)
4659                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4660                         else
4661                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4662                         os_free(freqs);
4663                         return -1;
4664                 } else if (num == 0) {
4665                         wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4666                 } else {
4667                         wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4668                 }
4669         }
4670
4671         os_free(freqs);
4672         return 0;
4673 }
4674
4675
4676 static struct wpa_supplicant *
4677 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4678                          int go)
4679 {
4680         struct wpa_supplicant *group_wpa_s;
4681
4682         if (!wpas_p2p_create_iface(wpa_s)) {
4683                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4684                         "operations");
4685                 wpa_s->p2p_first_connection_timeout = 0;
4686                 return wpa_s;
4687         }
4688
4689         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4690                                          WPA_IF_P2P_CLIENT) < 0) {
4691                 wpa_msg_global(wpa_s, MSG_ERROR,
4692                                "P2P: Failed to add group interface");
4693                 return NULL;
4694         }
4695         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4696         if (group_wpa_s == NULL) {
4697                 wpa_msg_global(wpa_s, MSG_ERROR,
4698                                "P2P: Failed to initialize group interface");
4699                 wpas_p2p_remove_pending_group_interface(wpa_s);
4700                 return NULL;
4701         }
4702
4703         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4704                 group_wpa_s->ifname);
4705         group_wpa_s->p2p_first_connection_timeout = 0;
4706         return group_wpa_s;
4707 }
4708
4709
4710 /**
4711  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4712  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4713  * @persistent_group: Whether to create a persistent group
4714  * @freq: Frequency for the group or 0 to indicate no hardcoding
4715  * @ht40: Start GO with 40 MHz channel width
4716  * @vht:  Start GO with VHT support
4717  * Returns: 0 on success, -1 on failure
4718  *
4719  * This function creates a new P2P group with the local end as the Group Owner,
4720  * i.e., without using Group Owner Negotiation.
4721  */
4722 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4723                        int freq, int ht40, int vht)
4724 {
4725         struct p2p_go_neg_results params;
4726
4727         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4728                 return -1;
4729
4730         os_free(wpa_s->global->add_psk);
4731         wpa_s->global->add_psk = NULL;
4732
4733         /* Make sure we are not running find during connection establishment */
4734         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4735         wpas_p2p_stop_find_oper(wpa_s);
4736
4737         freq = wpas_p2p_select_go_freq(wpa_s, freq);
4738         if (freq < 0)
4739                 return -1;
4740
4741         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
4742                 return -1;
4743         if (params.freq &&
4744             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
4745                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4746                            "(%u MHz) is not supported for P2P uses",
4747                            params.freq);
4748                 return -1;
4749         }
4750         p2p_go_params(wpa_s->global->p2p, &params);
4751         params.persistent_group = persistent_group;
4752
4753         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4754         if (wpa_s == NULL)
4755                 return -1;
4756         wpas_start_wps_go(wpa_s, &params, 0);
4757
4758         return 0;
4759 }
4760
4761
4762 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4763                                  struct wpa_ssid *params, int addr_allocated)
4764 {
4765         struct wpa_ssid *ssid;
4766
4767         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4768         if (wpa_s == NULL)
4769                 return -1;
4770         wpa_s->p2p_last_4way_hs_fail = NULL;
4771
4772         wpa_supplicant_ap_deinit(wpa_s);
4773
4774         ssid = wpa_config_add_network(wpa_s->conf);
4775         if (ssid == NULL)
4776                 return -1;
4777         wpa_config_set_network_defaults(ssid);
4778         ssid->temporary = 1;
4779         ssid->proto = WPA_PROTO_RSN;
4780         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4781         ssid->group_cipher = WPA_CIPHER_CCMP;
4782         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4783         ssid->ssid = os_malloc(params->ssid_len);
4784         if (ssid->ssid == NULL) {
4785                 wpa_config_remove_network(wpa_s->conf, ssid->id);
4786                 return -1;
4787         }
4788         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4789         ssid->ssid_len = params->ssid_len;
4790         ssid->p2p_group = 1;
4791         ssid->export_keys = 1;
4792         if (params->psk_set) {
4793                 os_memcpy(ssid->psk, params->psk, 32);
4794                 ssid->psk_set = 1;
4795         }
4796         if (params->passphrase)
4797                 ssid->passphrase = os_strdup(params->passphrase);
4798
4799         wpa_s->show_group_started = 1;
4800
4801         wpa_supplicant_select_network(wpa_s, ssid);
4802
4803         return 0;
4804 }
4805
4806
4807 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4808                                   struct wpa_ssid *ssid, int addr_allocated,
4809                                   int freq, int ht40, int vht,
4810                                   const struct p2p_channels *channels,
4811                                   int connection_timeout)
4812 {
4813         struct p2p_go_neg_results params;
4814         int go = 0;
4815
4816         if (ssid->disabled != 2 || ssid->ssid == NULL)
4817                 return -1;
4818
4819         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4820             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4821                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4822                            "already running");
4823                 return 0;
4824         }
4825
4826         os_free(wpa_s->global->add_psk);
4827         wpa_s->global->add_psk = NULL;
4828
4829         /* Make sure we are not running find during connection establishment */
4830         wpas_p2p_stop_find_oper(wpa_s);
4831
4832         wpa_s->p2p_fallback_to_go_neg = 0;
4833
4834         if (ssid->mode == WPAS_MODE_INFRA)
4835                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4836
4837         if (ssid->mode != WPAS_MODE_P2P_GO)
4838                 return -1;
4839
4840         freq = wpas_p2p_select_go_freq(wpa_s, freq);
4841         if (freq < 0)
4842                 return -1;
4843
4844         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
4845                 return -1;
4846
4847         params.role_go = 1;
4848         params.psk_set = ssid->psk_set;
4849         if (params.psk_set)
4850                 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4851         if (ssid->passphrase) {
4852                 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4853                         wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4854                                    "persistent group");
4855                         return -1;
4856                 }
4857                 os_strlcpy(params.passphrase, ssid->passphrase,
4858                            sizeof(params.passphrase));
4859         }
4860         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4861         params.ssid_len = ssid->ssid_len;
4862         params.persistent_group = 1;
4863
4864         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4865         if (wpa_s == NULL)
4866                 return -1;
4867
4868         wpa_s->p2p_first_connection_timeout = connection_timeout;
4869         wpas_start_wps_go(wpa_s, &params, 0);
4870
4871         return 0;
4872 }
4873
4874
4875 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4876                                struct wpabuf *proberesp_ies)
4877 {
4878         struct wpa_supplicant *wpa_s = ctx;
4879         if (wpa_s->ap_iface) {
4880                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4881                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4882                         wpabuf_free(beacon_ies);
4883                         wpabuf_free(proberesp_ies);
4884                         return;
4885                 }
4886                 if (beacon_ies) {
4887                         wpabuf_free(hapd->p2p_beacon_ie);
4888                         hapd->p2p_beacon_ie = beacon_ies;
4889                 }
4890                 wpabuf_free(hapd->p2p_probe_resp_ie);
4891                 hapd->p2p_probe_resp_ie = proberesp_ies;
4892         } else {
4893                 wpabuf_free(beacon_ies);
4894                 wpabuf_free(proberesp_ies);
4895         }
4896         wpa_supplicant_ap_update_beacon(wpa_s);
4897 }
4898
4899
4900 static void wpas_p2p_idle_update(void *ctx, int idle)
4901 {
4902         struct wpa_supplicant *wpa_s = ctx;
4903         if (!wpa_s->ap_iface)
4904                 return;
4905         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4906         if (idle)
4907                 wpas_p2p_set_group_idle_timeout(wpa_s);
4908         else
4909                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4910 }
4911
4912
4913 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4914                                        struct wpa_ssid *ssid)
4915 {
4916         struct p2p_group *group;
4917         struct p2p_group_config *cfg;
4918
4919         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4920                 return NULL;
4921         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4922                 return NULL;
4923
4924         cfg = os_zalloc(sizeof(*cfg));
4925         if (cfg == NULL)
4926                 return NULL;
4927
4928         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4929                 cfg->persistent_group = 2;
4930         else if (ssid->p2p_persistent_group)
4931                 cfg->persistent_group = 1;
4932         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4933         if (wpa_s->max_stations &&
4934             wpa_s->max_stations < wpa_s->conf->max_num_sta)
4935                 cfg->max_clients = wpa_s->max_stations;
4936         else
4937                 cfg->max_clients = wpa_s->conf->max_num_sta;
4938         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4939         cfg->ssid_len = ssid->ssid_len;
4940         cfg->cb_ctx = wpa_s;
4941         cfg->ie_update = wpas_p2p_ie_update;
4942         cfg->idle_update = wpas_p2p_idle_update;
4943
4944         group = p2p_group_init(wpa_s->global->p2p, cfg);
4945         if (group == NULL)
4946                 os_free(cfg);
4947         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4948                 p2p_group_notif_formation_done(group);
4949         wpa_s->p2p_group = group;
4950         return group;
4951 }
4952
4953
4954 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4955                           int registrar)
4956 {
4957         struct wpa_ssid *ssid = wpa_s->current_ssid;
4958
4959         if (!wpa_s->p2p_in_provisioning) {
4960                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4961                            "provisioning not in progress");
4962                 return;
4963         }
4964
4965         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4966                 u8 go_dev_addr[ETH_ALEN];
4967                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4968                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4969                                           ssid->ssid_len);
4970                 /* Clear any stored provisioning info */
4971                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4972         }
4973
4974         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4975                              NULL);
4976         wpa_s->p2p_go_group_formation_completed = 1;
4977         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4978                 /*
4979                  * Use a separate timeout for initial data connection to
4980                  * complete to allow the group to be removed automatically if
4981                  * something goes wrong in this step before the P2P group idle
4982                  * timeout mechanism is taken into use.
4983                  */
4984                 wpa_dbg(wpa_s, MSG_DEBUG,
4985                         "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4986                         P2P_MAX_INITIAL_CONN_WAIT);
4987                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4988                                        wpas_p2p_group_formation_timeout,
4989                                        wpa_s->parent, NULL);
4990         } else if (ssid) {
4991                 /*
4992                  * Use a separate timeout for initial data connection to
4993                  * complete to allow the group to be removed automatically if
4994                  * the client does not complete data connection successfully.
4995                  */
4996                 wpa_dbg(wpa_s, MSG_DEBUG,
4997                         "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4998                         P2P_MAX_INITIAL_CONN_WAIT_GO);
4999                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5000                                        wpas_p2p_group_formation_timeout,
5001                                        wpa_s->parent, NULL);
5002                 /*
5003                  * Complete group formation on first successful data connection
5004                  */
5005                 wpa_s->p2p_go_group_formation_completed = 0;
5006         }
5007         if (wpa_s->global->p2p)
5008                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5009         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5010                 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5011         wpas_group_formation_completed(wpa_s, 1);
5012 }
5013
5014
5015 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5016                          struct wps_event_fail *fail)
5017 {
5018         if (!wpa_s->p2p_in_provisioning) {
5019                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5020                            "provisioning not in progress");
5021                 return;
5022         }
5023
5024         if (wpa_s->go_params) {
5025                 p2p_clear_provisioning_info(
5026                         wpa_s->global->p2p,
5027                         wpa_s->go_params->peer_device_addr);
5028         }
5029
5030         wpas_notify_p2p_wps_failed(wpa_s, fail);
5031 }
5032
5033
5034 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5035                        const char *config_method,
5036                        enum wpas_p2p_prov_disc_use use)
5037 {
5038         u16 config_methods;
5039
5040         wpa_s->p2p_fallback_to_go_neg = 0;
5041         wpa_s->pending_pd_use = NORMAL_PD;
5042         if (os_strncmp(config_method, "display", 7) == 0)
5043                 config_methods = WPS_CONFIG_DISPLAY;
5044         else if (os_strncmp(config_method, "keypad", 6) == 0)
5045                 config_methods = WPS_CONFIG_KEYPAD;
5046         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5047                  os_strncmp(config_method, "pushbutton", 10) == 0)
5048                 config_methods = WPS_CONFIG_PUSHBUTTON;
5049         else {
5050                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5051                 return -1;
5052         }
5053
5054         if (use == WPAS_P2P_PD_AUTO) {
5055                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5056                 wpa_s->pending_pd_config_methods = config_methods;
5057                 wpa_s->p2p_auto_pd = 1;
5058                 wpa_s->p2p_auto_join = 0;
5059                 wpa_s->pending_pd_before_join = 0;
5060                 wpa_s->auto_pd_scan_retry = 0;
5061                 wpas_p2p_stop_find(wpa_s);
5062                 wpa_s->p2p_join_scan_count = 0;
5063                 os_get_reltime(&wpa_s->p2p_auto_started);
5064                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5065                            wpa_s->p2p_auto_started.sec,
5066                            wpa_s->p2p_auto_started.usec);
5067                 wpas_p2p_join_scan(wpa_s, NULL);
5068                 return 0;
5069         }
5070
5071         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5072                 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
5073                                                  config_methods,
5074                                                  use == WPAS_P2P_PD_FOR_JOIN);
5075         }
5076
5077         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5078                 return -1;
5079
5080         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5081                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5082                                  0, 1);
5083 }
5084
5085
5086 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5087                               char *end)
5088 {
5089         return p2p_scan_result_text(ies, ies_len, buf, end);
5090 }
5091
5092
5093 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5094 {
5095         if (!offchannel_pending_action_tx(wpa_s))
5096                 return;
5097
5098         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5099                    "operation request");
5100         offchannel_clear_pending_action_tx(wpa_s);
5101 }
5102
5103
5104 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5105                   enum p2p_discovery_type type,
5106                   unsigned int num_req_dev_types, const u8 *req_dev_types,
5107                   const u8 *dev_id, unsigned int search_delay)
5108 {
5109         wpas_p2p_clear_pending_action_tx(wpa_s);
5110         wpa_s->p2p_long_listen = 0;
5111
5112         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5113                 return wpa_drv_p2p_find(wpa_s, timeout, type);
5114
5115         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5116             wpa_s->p2p_in_provisioning)
5117                 return -1;
5118
5119         wpa_supplicant_cancel_sched_scan(wpa_s);
5120
5121         return p2p_find(wpa_s->global->p2p, timeout, type,
5122                         num_req_dev_types, req_dev_types, dev_id,
5123                         search_delay);
5124 }
5125
5126
5127 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5128 {
5129         wpas_p2p_clear_pending_action_tx(wpa_s);
5130         wpa_s->p2p_long_listen = 0;
5131         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5132         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5133         wpa_s->global->p2p_cb_on_scan_complete = 0;
5134
5135         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5136                 wpa_drv_p2p_stop_find(wpa_s);
5137                 return 1;
5138         }
5139
5140         if (wpa_s->global->p2p)
5141                 p2p_stop_find(wpa_s->global->p2p);
5142
5143         return 0;
5144 }
5145
5146
5147 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5148 {
5149         if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5150                 return;
5151         wpas_p2p_remove_pending_group_interface(wpa_s);
5152 }
5153
5154
5155 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5156 {
5157         struct wpa_supplicant *wpa_s = eloop_ctx;
5158         wpa_s->p2p_long_listen = 0;
5159 }
5160
5161
5162 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5163 {
5164         int res;
5165
5166         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5167                 return -1;
5168
5169         wpa_supplicant_cancel_sched_scan(wpa_s);
5170         wpas_p2p_clear_pending_action_tx(wpa_s);
5171
5172         if (timeout == 0) {
5173                 /*
5174                  * This is a request for unlimited Listen state. However, at
5175                  * least for now, this is mapped to a Listen state for one
5176                  * hour.
5177                  */
5178                 timeout = 3600;
5179         }
5180         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5181         wpa_s->p2p_long_listen = 0;
5182
5183         /*
5184          * Stop previous find/listen operation to avoid trying to request a new
5185          * remain-on-channel operation while the driver is still running the
5186          * previous one.
5187          */
5188         if (wpa_s->global->p2p)
5189                 p2p_stop_find(wpa_s->global->p2p);
5190
5191         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5192         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5193                 wpa_s->p2p_long_listen = timeout * 1000;
5194                 eloop_register_timeout(timeout, 0,
5195                                        wpas_p2p_long_listen_timeout,
5196                                        wpa_s, NULL);
5197         }
5198
5199         return res;
5200 }
5201
5202
5203 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5204                           u8 *buf, size_t len, int p2p_group)
5205 {
5206         struct wpabuf *p2p_ie;
5207         int ret;
5208
5209         if (wpa_s->global->p2p_disabled)
5210                 return -1;
5211         if (wpa_s->global->p2p == NULL)
5212                 return -1;
5213         if (bss == NULL)
5214                 return -1;
5215
5216         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5217         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5218                                p2p_group, p2p_ie);
5219         wpabuf_free(p2p_ie);
5220
5221         return ret;
5222 }
5223
5224
5225 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5226                           const u8 *dst, const u8 *bssid,
5227                           const u8 *ie, size_t ie_len, int ssi_signal)
5228 {
5229         if (wpa_s->global->p2p_disabled)
5230                 return 0;
5231         if (wpa_s->global->p2p == NULL)
5232                 return 0;
5233
5234         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5235                                  ie, ie_len)) {
5236         case P2P_PREQ_NOT_P2P:
5237                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5238                                  ssi_signal);
5239                 /* fall through */
5240         case P2P_PREQ_MALFORMED:
5241         case P2P_PREQ_NOT_LISTEN:
5242         case P2P_PREQ_NOT_PROCESSED:
5243         default: /* make gcc happy */
5244                 return 0;
5245         case P2P_PREQ_PROCESSED:
5246                 return 1;
5247         }
5248 }
5249
5250
5251 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5252                         const u8 *sa, const u8 *bssid,
5253                         u8 category, const u8 *data, size_t len, int freq)
5254 {
5255         if (wpa_s->global->p2p_disabled)
5256                 return;
5257         if (wpa_s->global->p2p == NULL)
5258                 return;
5259
5260         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5261                       freq);
5262 }
5263
5264
5265 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5266 {
5267         if (wpa_s->global->p2p_disabled)
5268                 return;
5269         if (wpa_s->global->p2p == NULL)
5270                 return;
5271
5272         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5273 }
5274
5275
5276 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5277 {
5278         p2p_group_deinit(wpa_s->p2p_group);
5279         wpa_s->p2p_group = NULL;
5280
5281         wpa_s->ap_configured_cb = NULL;
5282         wpa_s->ap_configured_cb_ctx = NULL;
5283         wpa_s->ap_configured_cb_data = NULL;
5284         wpa_s->connect_without_scan = NULL;
5285 }
5286
5287
5288 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5289 {
5290         wpa_s->p2p_long_listen = 0;
5291
5292         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5293                 return wpa_drv_p2p_reject(wpa_s, addr);
5294
5295         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5296                 return -1;
5297
5298         return p2p_reject(wpa_s->global->p2p, addr);
5299 }
5300
5301
5302 /* Invite to reinvoke a persistent group */
5303 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5304                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5305                     int ht40, int vht, int pref_freq)
5306 {
5307         enum p2p_invite_role role;
5308         u8 *bssid = NULL;
5309         int force_freq = 0;
5310         int res;
5311         int no_pref_freq_given = pref_freq == 0;
5312
5313         wpa_s->global->p2p_invite_group = NULL;
5314         if (peer_addr)
5315                 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5316         else
5317                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5318
5319         wpa_s->p2p_persistent_go_freq = freq;
5320         wpa_s->p2p_go_ht40 = !!ht40;
5321         if (ssid->mode == WPAS_MODE_P2P_GO) {
5322                 role = P2P_INVITE_ROLE_GO;
5323                 if (peer_addr == NULL) {
5324                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5325                                    "address in invitation command");
5326                         return -1;
5327                 }
5328                 if (wpas_p2p_create_iface(wpa_s)) {
5329                         if (wpas_p2p_add_group_interface(wpa_s,
5330                                                          WPA_IF_P2P_GO) < 0) {
5331                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
5332                                            "allocate a new interface for the "
5333                                            "group");
5334                                 return -1;
5335                         }
5336                         bssid = wpa_s->pending_interface_addr;
5337                 } else
5338                         bssid = wpa_s->own_addr;
5339         } else {
5340                 role = P2P_INVITE_ROLE_CLIENT;
5341                 peer_addr = ssid->bssid;
5342         }
5343         wpa_s->pending_invite_ssid_id = ssid->id;
5344
5345         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5346                                    role == P2P_INVITE_ROLE_GO);
5347         if (res)
5348                 return res;
5349
5350         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5351                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5352                                           ssid->ssid, ssid->ssid_len,
5353                                           go_dev_addr, 1);
5354
5355         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5356                 return -1;
5357
5358         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5359             no_pref_freq_given && pref_freq > 0 &&
5360             wpa_s->num_multichan_concurrent > 1 &&
5361             wpas_p2p_num_unused_channels(wpa_s) > 0) {
5362                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5363                            pref_freq);
5364                 pref_freq = 0;
5365         }
5366
5367         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5368                           ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5369                           1, pref_freq);
5370 }
5371
5372
5373 /* Invite to join an active group */
5374 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5375                           const u8 *peer_addr, const u8 *go_dev_addr)
5376 {
5377         struct wpa_global *global = wpa_s->global;
5378         enum p2p_invite_role role;
5379         u8 *bssid = NULL;
5380         struct wpa_ssid *ssid;
5381         int persistent;
5382         int freq = 0, force_freq = 0, pref_freq = 0;
5383         int res;
5384
5385         wpa_s->p2p_persistent_go_freq = 0;
5386         wpa_s->p2p_go_ht40 = 0;
5387         wpa_s->p2p_go_vht = 0;
5388
5389         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5390                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5391                         break;
5392         }
5393         if (wpa_s == NULL) {
5394                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5395                 return -1;
5396         }
5397
5398         ssid = wpa_s->current_ssid;
5399         if (ssid == NULL) {
5400                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5401                            "invitation");
5402                 return -1;
5403         }
5404
5405         wpa_s->global->p2p_invite_group = wpa_s;
5406         persistent = ssid->p2p_persistent_group &&
5407                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5408                                         ssid->ssid, ssid->ssid_len);
5409
5410         if (ssid->mode == WPAS_MODE_P2P_GO) {
5411                 role = P2P_INVITE_ROLE_ACTIVE_GO;
5412                 bssid = wpa_s->own_addr;
5413                 if (go_dev_addr == NULL)
5414                         go_dev_addr = wpa_s->global->p2p_dev_addr;
5415                 freq = ssid->frequency;
5416         } else {
5417                 role = P2P_INVITE_ROLE_CLIENT;
5418                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5419                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5420                                    "invite to current group");
5421                         return -1;
5422                 }
5423                 bssid = wpa_s->bssid;
5424                 if (go_dev_addr == NULL &&
5425                     !is_zero_ether_addr(wpa_s->go_dev_addr))
5426                         go_dev_addr = wpa_s->go_dev_addr;
5427                 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5428                         (int) wpa_s->assoc_freq;
5429         }
5430         wpa_s->parent->pending_invite_ssid_id = -1;
5431
5432         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5433                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5434                                           ssid->ssid, ssid->ssid_len,
5435                                           go_dev_addr, persistent);
5436
5437         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5438                 return -1;
5439
5440         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5441                                    role == P2P_INVITE_ROLE_ACTIVE_GO);
5442         if (res)
5443                 return res;
5444         wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5445
5446         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5447                           ssid->ssid, ssid->ssid_len, force_freq,
5448                           go_dev_addr, persistent, pref_freq);
5449 }
5450
5451
5452 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5453 {
5454         struct wpa_ssid *ssid = wpa_s->current_ssid;
5455         const char *ssid_txt;
5456         u8 go_dev_addr[ETH_ALEN];
5457         int network_id = -1;
5458         int persistent;
5459         int freq;
5460
5461         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5462                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5463                                      wpa_s->parent, NULL);
5464         }
5465
5466         if (!wpa_s->show_group_started || !ssid)
5467                 goto done;
5468
5469         wpa_s->show_group_started = 0;
5470
5471         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5472         os_memset(go_dev_addr, 0, ETH_ALEN);
5473         if (ssid->bssid_set)
5474                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5475         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5476                                                ssid->ssid_len);
5477         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5478
5479         if (wpa_s->global->p2p_group_formation == wpa_s)
5480                 wpa_s->global->p2p_group_formation = NULL;
5481
5482         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5483                 (int) wpa_s->assoc_freq;
5484         if (ssid->passphrase == NULL && ssid->psk_set) {
5485                 char psk[65];
5486                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5487                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5488                                "%s client ssid=\"%s\" freq=%d psk=%s "
5489                                "go_dev_addr=" MACSTR "%s",
5490                                wpa_s->ifname, ssid_txt, freq, psk,
5491                                MAC2STR(go_dev_addr),
5492                                persistent ? " [PERSISTENT]" : "");
5493         } else {
5494                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5495                                "%s client ssid=\"%s\" freq=%d "
5496                                "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5497                                wpa_s->ifname, ssid_txt, freq,
5498                                ssid->passphrase ? ssid->passphrase : "",
5499                                MAC2STR(go_dev_addr),
5500                                persistent ? " [PERSISTENT]" : "");
5501         }
5502
5503         if (persistent)
5504                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5505                                                              ssid, go_dev_addr);
5506         if (network_id < 0)
5507                 network_id = ssid->id;
5508         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5509
5510 done:
5511         wpas_p2p_continue_after_scan(wpa_s);
5512 }
5513
5514
5515 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5516                           u32 interval1, u32 duration2, u32 interval2)
5517 {
5518         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5519                 return -1;
5520         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5521                 return -1;
5522
5523         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5524             wpa_s->current_ssid == NULL ||
5525             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5526                 return -1;
5527
5528         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5529                                 wpa_s->own_addr, wpa_s->assoc_freq,
5530                                 duration1, interval1, duration2, interval2);
5531 }
5532
5533
5534 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5535                         unsigned int interval)
5536 {
5537         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5538                 return -1;
5539
5540         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5541                 return -1;
5542
5543         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5544 }
5545
5546
5547 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5548 {
5549         if (wpa_s->current_ssid == NULL) {
5550                 /*
5551                  * current_ssid can be cleared when P2P client interface gets
5552                  * disconnected, so assume this interface was used as P2P
5553                  * client.
5554                  */
5555                 return 1;
5556         }
5557         return wpa_s->current_ssid->p2p_group &&
5558                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5559 }
5560
5561
5562 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5563 {
5564         struct wpa_supplicant *wpa_s = eloop_ctx;
5565
5566         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5567                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5568                            "disabled");
5569                 return;
5570         }
5571
5572         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5573                    "group");
5574         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5575 }
5576
5577
5578 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5579 {
5580         int timeout;
5581
5582         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5583                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5584
5585         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5586                 return;
5587
5588         timeout = wpa_s->conf->p2p_group_idle;
5589         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5590             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5591             timeout = P2P_MAX_CLIENT_IDLE;
5592
5593         if (timeout == 0)
5594                 return;
5595
5596         if (timeout < 0) {
5597                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5598                         timeout = 0; /* special client mode no-timeout */
5599                 else
5600                         return;
5601         }
5602
5603         if (wpa_s->p2p_in_provisioning) {
5604                 /*
5605                  * Use the normal group formation timeout during the
5606                  * provisioning phase to avoid terminating this process too
5607                  * early due to group idle timeout.
5608                  */
5609                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5610                            "during provisioning");
5611                 return;
5612         }
5613
5614         if (wpa_s->show_group_started) {
5615                 /*
5616                  * Use the normal group formation timeout between the end of
5617                  * the provisioning phase and completion of 4-way handshake to
5618                  * avoid terminating this process too early due to group idle
5619                  * timeout.
5620                  */
5621                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5622                            "while waiting for initial 4-way handshake to "
5623                            "complete");
5624                 return;
5625         }
5626
5627         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5628                    timeout);
5629         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5630                                wpa_s, NULL);
5631 }
5632
5633
5634 /* Returns 1 if the interface was removed */
5635 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5636                           u16 reason_code, const u8 *ie, size_t ie_len,
5637                           int locally_generated)
5638 {
5639         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5640                 return 0;
5641         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5642                 return 0;
5643
5644         if (!locally_generated)
5645                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5646                                  ie_len);
5647
5648         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5649             wpa_s->current_ssid &&
5650             wpa_s->current_ssid->p2p_group &&
5651             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5652                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5653                            "session is ending");
5654                 if (wpas_p2p_group_delete(wpa_s,
5655                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5656                     > 0)
5657                         return 1;
5658         }
5659
5660         return 0;
5661 }
5662
5663
5664 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5665                              u16 reason_code, const u8 *ie, size_t ie_len,
5666                              int locally_generated)
5667 {
5668         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5669                 return;
5670         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5671                 return;
5672
5673         if (!locally_generated)
5674                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5675                                    ie_len);
5676 }
5677
5678
5679 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5680 {
5681         struct p2p_data *p2p = wpa_s->global->p2p;
5682
5683         if (p2p == NULL)
5684                 return;
5685
5686         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5687                 return;
5688
5689         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5690                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5691
5692         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5693                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5694
5695         if (wpa_s->wps &&
5696             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5697                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5698
5699         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5700                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5701
5702         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5703                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5704                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5705                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5706                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5707         }
5708
5709         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5710                 p2p_set_sec_dev_types(p2p,
5711                                       (void *) wpa_s->conf->sec_device_type,
5712                                       wpa_s->conf->num_sec_device_types);
5713
5714         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5715                 int i;
5716                 p2p_remove_wps_vendor_extensions(p2p);
5717                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5718                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5719                                 continue;
5720                         p2p_add_wps_vendor_extension(
5721                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
5722                 }
5723         }
5724
5725         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5726             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5727                 char country[3];
5728                 country[0] = wpa_s->conf->country[0];
5729                 country[1] = wpa_s->conf->country[1];
5730                 country[2] = 0x04;
5731                 p2p_set_country(p2p, country);
5732         }
5733
5734         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5735                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5736                                      wpa_s->conf->p2p_ssid_postfix ?
5737                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5738                                      0);
5739         }
5740
5741         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5742                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5743
5744         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5745                 u8 reg_class, channel;
5746                 int ret;
5747                 unsigned int r;
5748                 if (wpa_s->conf->p2p_listen_reg_class &&
5749                     wpa_s->conf->p2p_listen_channel) {
5750                         reg_class = wpa_s->conf->p2p_listen_reg_class;
5751                         channel = wpa_s->conf->p2p_listen_channel;
5752                 } else {
5753                         reg_class = 81;
5754                         /*
5755                          * Pick one of the social channels randomly as the
5756                          * listen channel.
5757                          */
5758                         os_get_random((u8 *) &r, sizeof(r));
5759                         channel = 1 + (r % 3) * 5;
5760                 }
5761                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5762                 if (ret)
5763                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5764                                    "failed: %d", ret);
5765         }
5766         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5767                 u8 op_reg_class, op_channel, cfg_op_channel;
5768                 int ret = 0;
5769                 unsigned int r;
5770                 if (wpa_s->conf->p2p_oper_reg_class &&
5771                     wpa_s->conf->p2p_oper_channel) {
5772                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5773                         op_channel = wpa_s->conf->p2p_oper_channel;
5774                         cfg_op_channel = 1;
5775                 } else {
5776                         op_reg_class = 81;
5777                         /*
5778                          * Use random operation channel from (1, 6, 11)
5779                          *if no other preference is indicated.
5780                          */
5781                         os_get_random((u8 *) &r, sizeof(r));
5782                         op_channel = 1 + (r % 3) * 5;
5783                         cfg_op_channel = 0;
5784                 }
5785                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5786                                            cfg_op_channel);
5787                 if (ret)
5788                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5789                                    "failed: %d", ret);
5790         }
5791
5792         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5793                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5794                                       wpa_s->conf->p2p_pref_chan) < 0) {
5795                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5796                                    "update failed");
5797                 }
5798
5799                 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5800                         wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5801                                    "update failed");
5802                 }
5803         }
5804 }
5805
5806
5807 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5808                      int duration)
5809 {
5810         if (!wpa_s->ap_iface)
5811                 return -1;
5812         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5813                                    duration);
5814 }
5815
5816
5817 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5818 {
5819         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5820                 return -1;
5821         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5822                 return -1;
5823
5824         wpa_s->global->cross_connection = enabled;
5825         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5826
5827         if (!enabled) {
5828                 struct wpa_supplicant *iface;
5829
5830                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5831                 {
5832                         if (iface->cross_connect_enabled == 0)
5833                                 continue;
5834
5835                         iface->cross_connect_enabled = 0;
5836                         iface->cross_connect_in_use = 0;
5837                         wpa_msg_global(iface->parent, MSG_INFO,
5838                                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5839                                        iface->ifname,
5840                                        iface->cross_connect_uplink);
5841                 }
5842         }
5843
5844         return 0;
5845 }
5846
5847
5848 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5849 {
5850         struct wpa_supplicant *iface;
5851
5852         if (!uplink->global->cross_connection)
5853                 return;
5854
5855         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5856                 if (!iface->cross_connect_enabled)
5857                         continue;
5858                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5859                     0)
5860                         continue;
5861                 if (iface->ap_iface == NULL)
5862                         continue;
5863                 if (iface->cross_connect_in_use)
5864                         continue;
5865
5866                 iface->cross_connect_in_use = 1;
5867                 wpa_msg_global(iface->parent, MSG_INFO,
5868                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5869                                iface->ifname, iface->cross_connect_uplink);
5870         }
5871 }
5872
5873
5874 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5875 {
5876         struct wpa_supplicant *iface;
5877
5878         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5879                 if (!iface->cross_connect_enabled)
5880                         continue;
5881                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5882                     0)
5883                         continue;
5884                 if (!iface->cross_connect_in_use)
5885                         continue;
5886
5887                 wpa_msg_global(iface->parent, MSG_INFO,
5888                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5889                                iface->ifname, iface->cross_connect_uplink);
5890                 iface->cross_connect_in_use = 0;
5891         }
5892 }
5893
5894
5895 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5896 {
5897         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5898             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5899             wpa_s->cross_connect_disallowed)
5900                 wpas_p2p_disable_cross_connect(wpa_s);
5901         else
5902                 wpas_p2p_enable_cross_connect(wpa_s);
5903         if (!wpa_s->ap_iface &&
5904             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5905                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5906 }
5907
5908
5909 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5910 {
5911         wpas_p2p_disable_cross_connect(wpa_s);
5912         if (!wpa_s->ap_iface &&
5913             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5914                                          wpa_s, NULL))
5915                 wpas_p2p_set_group_idle_timeout(wpa_s);
5916 }
5917
5918
5919 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5920 {
5921         struct wpa_supplicant *iface;
5922
5923         if (!wpa_s->global->cross_connection)
5924                 return;
5925
5926         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5927                 if (iface == wpa_s)
5928                         continue;
5929                 if (iface->drv_flags &
5930                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5931                         continue;
5932                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5933                         continue;
5934
5935                 wpa_s->cross_connect_enabled = 1;
5936                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5937                            sizeof(wpa_s->cross_connect_uplink));
5938                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5939                            "%s to %s whenever uplink is available",
5940                            wpa_s->ifname, wpa_s->cross_connect_uplink);
5941
5942                 if (iface->ap_iface || iface->current_ssid == NULL ||
5943                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
5944                     iface->cross_connect_disallowed ||
5945                     iface->wpa_state != WPA_COMPLETED)
5946                         break;
5947
5948                 wpa_s->cross_connect_in_use = 1;
5949                 wpa_msg_global(wpa_s->parent, MSG_INFO,
5950                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5951                                wpa_s->ifname, wpa_s->cross_connect_uplink);
5952                 break;
5953         }
5954 }
5955
5956
5957 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5958 {
5959         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5960             !wpa_s->p2p_in_provisioning)
5961                 return 0; /* not P2P client operation */
5962
5963         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5964                    "session overlap");
5965         if (wpa_s != wpa_s->parent)
5966                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5967         wpas_p2p_group_formation_failed(wpa_s);
5968         return 1;
5969 }
5970
5971
5972 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5973 {
5974         struct p2p_channels chan, cli_chan;
5975
5976         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5977                 return;
5978
5979         os_memset(&chan, 0, sizeof(chan));
5980         os_memset(&cli_chan, 0, sizeof(cli_chan));
5981         if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
5982                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5983                            "channel list");
5984                 return;
5985         }
5986
5987         p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
5988 }
5989
5990
5991 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5992                                      struct wpa_scan_results *scan_res)
5993 {
5994         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5995 }
5996
5997
5998 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5999 {
6000         struct wpa_global *global = wpa_s->global;
6001         int found = 0;
6002         const u8 *peer;
6003
6004         if (global->p2p == NULL)
6005                 return -1;
6006
6007         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6008
6009         if (wpa_s->pending_interface_name[0] &&
6010             !is_zero_ether_addr(wpa_s->pending_interface_addr))
6011                 found = 1;
6012
6013         peer = p2p_get_go_neg_peer(global->p2p);
6014         if (peer) {
6015                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6016                            MACSTR, MAC2STR(peer));
6017                 p2p_unauthorize(global->p2p, peer);
6018                 found = 1;
6019         }
6020
6021         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6022                 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6023                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6024                 found = 1;
6025         }
6026
6027         if (wpa_s->pending_pd_before_join) {
6028                 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6029                 wpa_s->pending_pd_before_join = 0;
6030                 found = 1;
6031         }
6032
6033         wpas_p2p_stop_find(wpa_s);
6034
6035         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6036                 if (wpa_s == global->p2p_group_formation &&
6037                     (wpa_s->p2p_in_provisioning ||
6038                      wpa_s->parent->pending_interface_type ==
6039                      WPA_IF_P2P_CLIENT)) {
6040                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6041                                    "formation found - cancelling",
6042                                    wpa_s->ifname);
6043                         found = 1;
6044                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6045                                              wpa_s->parent, NULL);
6046                         if (wpa_s->p2p_in_provisioning) {
6047                                 wpas_group_formation_completed(wpa_s, 0);
6048                                 break;
6049                         }
6050                         wpas_p2p_group_delete(wpa_s,
6051                                               P2P_GROUP_REMOVAL_REQUESTED);
6052                         break;
6053                 }
6054         }
6055
6056         if (!found) {
6057                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6058                 return -1;
6059         }
6060
6061         return 0;
6062 }
6063
6064
6065 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6066 {
6067         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6068                 return;
6069
6070         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6071                    "being available anymore");
6072         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6073 }
6074
6075
6076 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6077                                    int freq_24, int freq_5, int freq_overall)
6078 {
6079         struct p2p_data *p2p = wpa_s->global->p2p;
6080         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6081                 return;
6082         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6083 }
6084
6085
6086 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6087 {
6088         u8 peer[ETH_ALEN];
6089         struct p2p_data *p2p = wpa_s->global->p2p;
6090
6091         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6092                 return -1;
6093
6094         if (hwaddr_aton(addr, peer))
6095                 return -1;
6096
6097         return p2p_unauthorize(p2p, peer);
6098 }
6099
6100
6101 /**
6102  * wpas_p2p_disconnect - Disconnect from a P2P Group
6103  * @wpa_s: Pointer to wpa_supplicant data
6104  * Returns: 0 on success, -1 on failure
6105  *
6106  * This can be used to disconnect from a group in which the local end is a P2P
6107  * Client or to end a P2P Group in case the local end is the Group Owner. If a
6108  * virtual network interface was created for this group, that interface will be
6109  * removed. Otherwise, only the configured P2P group network will be removed
6110  * from the interface.
6111  */
6112 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6113 {
6114
6115         if (wpa_s == NULL)
6116                 return -1;
6117
6118         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6119                 -1 : 0;
6120 }
6121
6122
6123 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6124 {
6125         int ret;
6126
6127         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6128                 return 0;
6129
6130         ret = p2p_in_progress(wpa_s->global->p2p);
6131         if (ret == 0) {
6132                 /*
6133                  * Check whether there is an ongoing WPS provisioning step (or
6134                  * other parts of group formation) on another interface since
6135                  * p2p_in_progress() does not report this to avoid issues for
6136                  * scans during such provisioning step.
6137                  */
6138                 if (wpa_s->global->p2p_group_formation &&
6139                     wpa_s->global->p2p_group_formation != wpa_s) {
6140                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6141                                 "in group formation",
6142                                 wpa_s->global->p2p_group_formation->ifname);
6143                         ret = 1;
6144                 }
6145         }
6146
6147         if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6148                 struct os_reltime now;
6149                 os_get_reltime(&now);
6150                 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6151                                        P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6152                         /* Wait for the first client has expired */
6153                         wpa_s->global->p2p_go_wait_client.sec = 0;
6154                 } else {
6155                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6156                         ret = 1;
6157                 }
6158         }
6159
6160         return ret;
6161 }
6162
6163
6164 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6165                               struct wpa_ssid *ssid)
6166 {
6167         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6168             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6169                                  wpa_s->parent, NULL) > 0) {
6170                 /**
6171                  * Remove the network by scheduling the group formation
6172                  * timeout to happen immediately. The teardown code
6173                  * needs to be scheduled to run asynch later so that we
6174                  * don't delete data from under ourselves unexpectedly.
6175                  * Calling wpas_p2p_group_formation_timeout directly
6176                  * causes a series of crashes in WPS failure scenarios.
6177                  */
6178                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6179                            "P2P group network getting removed");
6180                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6181                                        wpa_s->parent, NULL);
6182         }
6183 }
6184
6185
6186 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6187                                           const u8 *addr, const u8 *ssid,
6188                                           size_t ssid_len)
6189 {
6190         struct wpa_ssid *s;
6191         size_t i;
6192
6193         for (s = wpa_s->conf->ssid; s; s = s->next) {
6194                 if (s->disabled != 2)
6195                         continue;
6196                 if (ssid &&
6197                     (ssid_len != s->ssid_len ||
6198                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
6199                         continue;
6200                 if (addr == NULL) {
6201                         if (s->mode == WPAS_MODE_P2P_GO)
6202                                 return s;
6203                         continue;
6204                 }
6205                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6206                         return s; /* peer is GO in the persistent group */
6207                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6208                         continue;
6209                 for (i = 0; i < s->num_p2p_clients; i++) {
6210                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6211                                       addr, ETH_ALEN) == 0)
6212                                 return s; /* peer is P2P client in persistent
6213                                            * group */
6214                 }
6215         }
6216
6217         return NULL;
6218 }
6219
6220
6221 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6222                                        const u8 *addr)
6223 {
6224         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6225                                  wpa_s->parent, NULL) > 0) {
6226                 /*
6227                  * This can happen if WPS provisioning step is not terminated
6228                  * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6229                  * peer was able to connect, there is no need to time out group
6230                  * formation after this, though. In addition, this is used with
6231                  * the initial connection wait on the GO as a separate formation
6232                  * timeout and as such, expected to be hit after the initial WPS
6233                  * provisioning step.
6234                  */
6235                 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6236         }
6237         if (!wpa_s->p2p_go_group_formation_completed) {
6238                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6239                 wpa_s->p2p_go_group_formation_completed = 1;
6240                 wpa_s->global->p2p_group_formation = NULL;
6241                 wpa_s->p2p_in_provisioning = 0;
6242         }
6243         wpa_s->global->p2p_go_wait_client.sec = 0;
6244         if (addr == NULL)
6245                 return;
6246         wpas_p2p_add_persistent_group_client(wpa_s, addr);
6247 }
6248
6249
6250 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6251                                         int group_added)
6252 {
6253         struct wpa_supplicant *group = wpa_s;
6254         if (wpa_s->global->p2p_group_formation)
6255                 group = wpa_s->global->p2p_group_formation;
6256         wpa_s = wpa_s->parent;
6257         offchannel_send_action_done(wpa_s);
6258         if (group_added)
6259                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6260         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6261         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6262                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6263                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6264                          wpa_s->p2p_persistent_id,
6265                          wpa_s->p2p_pd_before_go_neg,
6266                          wpa_s->p2p_go_ht40,
6267                          wpa_s->p2p_go_vht);
6268 }
6269
6270
6271 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6272 {
6273         if (!wpa_s->p2p_fallback_to_go_neg ||
6274             wpa_s->p2p_in_provisioning <= 5)
6275                 return 0;
6276
6277         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6278                 return 0; /* peer operating as a GO */
6279
6280         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6281                 "fallback to GO Negotiation");
6282         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6283
6284         return 1;
6285 }
6286
6287
6288 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6289 {
6290         struct wpa_supplicant *ifs;
6291
6292         if (wpa_s->wpa_state > WPA_SCANNING) {
6293                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6294                         "concurrent operation",
6295                         P2P_CONCURRENT_SEARCH_DELAY);
6296                 return P2P_CONCURRENT_SEARCH_DELAY;
6297         }
6298
6299         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6300                          radio_list) {
6301                 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6302                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6303                                 "delay due to concurrent operation on "
6304                                 "interface %s",
6305                                 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6306                         return P2P_CONCURRENT_SEARCH_DELAY;
6307                 }
6308         }
6309
6310         return 0;
6311 }
6312
6313
6314 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6315 {
6316         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6317                 "pending anymore (sta_scan_pending=%d "
6318                 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6319                 wpa_s->global->p2p_cb_on_scan_complete);
6320         wpa_s->sta_scan_pending = 0;
6321
6322         if (!wpa_s->global->p2p_cb_on_scan_complete)
6323                 return;
6324         wpa_s->global->p2p_cb_on_scan_complete = 0;
6325
6326         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6327                 return;
6328
6329         if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6330                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6331                         "continued after successful connection");
6332                 p2p_increase_search_delay(wpa_s->global->p2p,
6333                                           wpas_p2p_search_delay(wpa_s));
6334         }
6335 }
6336
6337
6338 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6339                                      struct wpa_ssid *s, const u8 *addr,
6340                                      int iface_addr)
6341 {
6342         struct psk_list_entry *psk, *tmp;
6343         int changed = 0;
6344
6345         dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6346                               list) {
6347                 if ((iface_addr && !psk->p2p &&
6348                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6349                     (!iface_addr && psk->p2p &&
6350                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6351                         wpa_dbg(wpa_s, MSG_DEBUG,
6352                                 "P2P: Remove persistent group PSK list entry for "
6353                                 MACSTR " p2p=%u",
6354                                 MAC2STR(psk->addr), psk->p2p);
6355                         dl_list_del(&psk->list);
6356                         os_free(psk);
6357                         changed++;
6358                 }
6359         }
6360
6361         return changed;
6362 }
6363
6364
6365 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6366                          const u8 *p2p_dev_addr,
6367                          const u8 *psk, size_t psk_len)
6368 {
6369         struct wpa_ssid *ssid = wpa_s->current_ssid;
6370         struct wpa_ssid *persistent;
6371         struct psk_list_entry *p;
6372
6373         if (psk_len != sizeof(p->psk))
6374                 return;
6375
6376         if (p2p_dev_addr) {
6377                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6378                         " p2p_dev_addr=" MACSTR,
6379                         MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6380                 if (is_zero_ether_addr(p2p_dev_addr))
6381                         p2p_dev_addr = NULL;
6382         } else {
6383                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6384                         MAC2STR(mac_addr));
6385         }
6386
6387         if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6388                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6389                 /* To be added to persistent group once created */
6390                 if (wpa_s->global->add_psk == NULL) {
6391                         wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6392                         if (wpa_s->global->add_psk == NULL)
6393                                 return;
6394                 }
6395                 p = wpa_s->global->add_psk;
6396                 if (p2p_dev_addr) {
6397                         p->p2p = 1;
6398                         os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6399                 } else {
6400                         p->p2p = 0;
6401                         os_memcpy(p->addr, mac_addr, ETH_ALEN);
6402                 }
6403                 os_memcpy(p->psk, psk, psk_len);
6404                 return;
6405         }
6406
6407         if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6408                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6409                 return;
6410         }
6411
6412         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6413                                              ssid->ssid_len);
6414         if (!persistent) {
6415                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6416                 return;
6417         }
6418
6419         p = os_zalloc(sizeof(*p));
6420         if (p == NULL)
6421                 return;
6422         if (p2p_dev_addr) {
6423                 p->p2p = 1;
6424                 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6425         } else {
6426                 p->p2p = 0;
6427                 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6428         }
6429         os_memcpy(p->psk, psk, psk_len);
6430
6431         if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6432                 struct psk_list_entry *last;
6433                 last = dl_list_last(&persistent->psk_list,
6434                                     struct psk_list_entry, list);
6435                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6436                         MACSTR " (p2p=%u) to make room for a new one",
6437                         MAC2STR(last->addr), last->p2p);
6438                 dl_list_del(&last->list);
6439                 os_free(last);
6440         }
6441
6442         wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6443                                   p2p_dev_addr ? p2p_dev_addr : mac_addr,
6444                                   p2p_dev_addr == NULL);
6445         if (p2p_dev_addr) {
6446                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6447                         MACSTR, MAC2STR(p2p_dev_addr));
6448         } else {
6449                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6450                         MAC2STR(mac_addr));
6451         }
6452         dl_list_add(&persistent->psk_list, &p->list);
6453
6454 #ifndef CONFIG_NO_CONFIG_WRITE
6455         if (wpa_s->parent->conf->update_config &&
6456             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6457                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6458 #endif /* CONFIG_NO_CONFIG_WRITE */
6459 }
6460
6461
6462 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6463                                 struct wpa_ssid *s, const u8 *addr,
6464                                 int iface_addr)
6465 {
6466         int res;
6467
6468         res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6469         if (res > 0) {
6470 #ifndef CONFIG_NO_CONFIG_WRITE
6471                 if (wpa_s->conf->update_config &&
6472                     wpa_config_write(wpa_s->confname, wpa_s->conf))
6473                         wpa_dbg(wpa_s, MSG_DEBUG,
6474                                 "P2P: Failed to update configuration");
6475 #endif /* CONFIG_NO_CONFIG_WRITE */
6476         }
6477 }
6478
6479
6480 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6481                                       const u8 *peer, int iface_addr)
6482 {
6483         struct hostapd_data *hapd;
6484         struct hostapd_wpa_psk *psk, *prev, *rem;
6485         struct sta_info *sta;
6486
6487         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6488             wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6489                 return;
6490
6491         /* Remove per-station PSK entry */
6492         hapd = wpa_s->ap_iface->bss[0];
6493         prev = NULL;
6494         psk = hapd->conf->ssid.wpa_psk;
6495         while (psk) {
6496                 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6497                     (!iface_addr &&
6498                      os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6499                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6500                                 MACSTR " iface_addr=%d",
6501                                 MAC2STR(peer), iface_addr);
6502                         if (prev)
6503                                 prev->next = psk->next;
6504                         else
6505                                 hapd->conf->ssid.wpa_psk = psk->next;
6506                         rem = psk;
6507                         psk = psk->next;
6508                         os_free(rem);
6509                 } else {
6510                         prev = psk;
6511                         psk = psk->next;
6512                 }
6513         }
6514
6515         /* Disconnect from group */
6516         if (iface_addr)
6517                 sta = ap_get_sta(hapd, peer);
6518         else
6519                 sta = ap_get_sta_p2p(hapd, peer);
6520         if (sta) {
6521                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6522                         " (iface_addr=%d) from group",
6523                         MAC2STR(peer), iface_addr);
6524                 hostapd_drv_sta_deauth(hapd, sta->addr,
6525                                        WLAN_REASON_DEAUTH_LEAVING);
6526                 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6527         }
6528 }
6529
6530
6531 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6532                             int iface_addr)
6533 {
6534         struct wpa_ssid *s;
6535         struct wpa_supplicant *w;
6536
6537         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6538
6539         /* Remove from any persistent group */
6540         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6541                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6542                         continue;
6543                 if (!iface_addr)
6544                         wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6545                 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6546         }
6547
6548         /* Remove from any operating group */
6549         for (w = wpa_s->global->ifaces; w; w = w->next)
6550                 wpas_p2p_remove_client_go(w, peer, iface_addr);
6551 }
6552
6553
6554 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6555 {
6556         struct wpa_supplicant *wpa_s = eloop_ctx;
6557         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6558 }
6559
6560
6561 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6562 {
6563         struct wpa_supplicant *wpa_s = eloop_ctx;
6564
6565         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6566         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6567 }
6568
6569
6570 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6571                                         struct wpa_ssid *ssid)
6572 {
6573         struct wpa_supplicant *iface;
6574
6575         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6576                 if (!iface->current_ssid ||
6577                     iface->current_ssid->frequency == freq ||
6578                     (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6579                      !iface->current_ssid->p2p_group))
6580                         continue;
6581
6582                 /* Remove the connection with least priority */
6583                 if (!wpas_is_p2p_prioritized(iface)) {
6584                         /* STA connection has priority over existing
6585                          * P2P connection, so remove the interface. */
6586                         wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6587                         eloop_register_timeout(0, 0,
6588                                                wpas_p2p_group_freq_conflict,
6589                                                iface, NULL);
6590                         /* If connection in progress is P2P connection, do not
6591                          * proceed for the connection. */
6592                         if (wpa_s == iface)
6593                                 return -1;
6594                         else
6595                                 return 0;
6596                 } else {
6597                         /* P2P connection has priority, disable the STA network
6598                          */
6599                         wpa_supplicant_disable_network(wpa_s->global->ifaces,
6600                                                        ssid);
6601                         wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6602                                 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6603                         os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6604                                   ETH_ALEN);
6605                         /* If P2P connection is in progress, continue
6606                          * connecting...*/
6607                         if (wpa_s == iface)
6608                                 return 0;
6609                         else
6610                                 return -1;
6611                 }
6612         }
6613
6614         return 0;
6615 }
6616
6617
6618 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6619 {
6620         struct wpa_ssid *ssid = wpa_s->current_ssid;
6621
6622         if (ssid == NULL || !ssid->p2p_group)
6623                 return 0;
6624
6625         if (wpa_s->p2p_last_4way_hs_fail &&
6626             wpa_s->p2p_last_4way_hs_fail == ssid) {
6627                 u8 go_dev_addr[ETH_ALEN];
6628                 struct wpa_ssid *persistent;
6629
6630                 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6631                                               ssid->ssid,
6632                                               ssid->ssid_len) <= 0) {
6633                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6634                         goto disconnect;
6635                 }
6636
6637                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6638                         MACSTR, MAC2STR(go_dev_addr));
6639                 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6640                                                      ssid->ssid,
6641                                                      ssid->ssid_len);
6642                 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6643                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6644                         goto disconnect;
6645                 }
6646                 wpa_msg_global(wpa_s->parent, MSG_INFO,
6647                                P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6648                                persistent->id);
6649         disconnect:
6650                 wpa_s->p2p_last_4way_hs_fail = NULL;
6651                 /*
6652                  * Remove the group from a timeout to avoid issues with caller
6653                  * continuing to use the interface if this is on a P2P group
6654                  * interface.
6655                  */
6656                 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6657                                        wpa_s, NULL);
6658                 return 1;
6659         }
6660
6661         wpa_s->p2p_last_4way_hs_fail = ssid;
6662         return 0;
6663 }