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