P2P: Cancel group formation when deleting a group during group formation
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2  * wpa_supplicant - P2P
3  * Copyright (c) 2009-2010, Atheros Communications
4  * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9
10 #include "includes.h"
11
12 #include "common.h"
13 #include "eloop.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "wps/wps_i.h"
18 #include "p2p/p2p.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "ap/dfs.h"
26 #include "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
29 #include "driver_i.h"
30 #include "ap.h"
31 #include "config_ssid.h"
32 #include "config.h"
33 #include "notify.h"
34 #include "scan.h"
35 #include "bss.h"
36 #include "offchannel.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
39 #include "wifi_display.h"
40
41
42 /*
43  * How many times to try to scan to find the GO before giving up on join
44  * request.
45  */
46 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
48 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
50 #ifndef P2P_MAX_CLIENT_IDLE
51 /*
52  * How many seconds to try to reconnect to the GO when connection in P2P client
53  * role has been lost.
54  */
55 #define P2P_MAX_CLIENT_IDLE 10
56 #endif /* P2P_MAX_CLIENT_IDLE */
57
58 #ifndef P2P_MAX_INITIAL_CONN_WAIT
59 /*
60  * How many seconds to wait for initial 4-way handshake to get completed after
61  * WPS provisioning step or after the re-invocation of a persistent group on a
62  * P2P Client.
63  */
64 #define P2P_MAX_INITIAL_CONN_WAIT 10
65 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
66
67 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68 /*
69  * How many seconds to wait for initial 4-way handshake to get completed after
70  * WPS provisioning step on the GO. This controls the extra time the P2P
71  * operation is considered to be in progress (e.g., to delay other scans) after
72  * WPS provisioning has been completed on the GO during group formation.
73  */
74 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
75 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76
77 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78 /*
79  * How many seconds to wait for initial 4-way handshake to get completed after
80  * re-invocation of a persistent group on the GO when the client is expected
81  * to connect automatically (no user interaction).
82  */
83 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
84 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
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, int freq,
106                                const u8 *ssid, size_t ssid_len);
107 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
108                                    const u8 *ssid, size_t ssid_len);
109 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
110 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
111                          const u8 *dev_addr, enum p2p_wps_method wps_method,
112                          int auto_join, int freq,
113                          const u8 *ssid, size_t ssid_len);
114 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
116 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
117 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
118 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
119                                              void *timeout_ctx);
120 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
121 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122                                        int group_added);
123 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
124 static void wpas_stop_listen(void *ctx);
125 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
126 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
127 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
128                                         enum wpa_driver_if_type type);
129 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
130                                             int already_deleted);
131
132
133 /*
134  * Get the number of concurrent channels that the HW can operate, but that are
135  * currently not in use by any of the wpa_supplicant interfaces.
136  */
137 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
138 {
139         int *freqs;
140         int num, unused;
141
142         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
143         if (!freqs)
144                 return -1;
145
146         num = get_shared_radio_freqs(wpa_s, freqs,
147                                      wpa_s->num_multichan_concurrent);
148         os_free(freqs);
149
150         unused = wpa_s->num_multichan_concurrent - num;
151         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
152         return unused;
153 }
154
155
156 /*
157  * Get the frequencies that are currently in use by one or more of the virtual
158  * interfaces, and that are also valid for P2P operation.
159  */
160 static unsigned int
161 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
162                           struct wpa_used_freq_data *p2p_freqs,
163                           unsigned int len)
164 {
165         struct wpa_used_freq_data *freqs;
166         unsigned int num, i, j;
167
168         freqs = os_calloc(wpa_s->num_multichan_concurrent,
169                           sizeof(struct wpa_used_freq_data));
170         if (!freqs)
171                 return 0;
172
173         num = get_shared_radio_freqs_data(wpa_s, freqs,
174                                           wpa_s->num_multichan_concurrent);
175
176         os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
177
178         for (i = 0, j = 0; i < num && j < len; i++) {
179                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
180                         p2p_freqs[j++] = freqs[i];
181         }
182
183         os_free(freqs);
184
185         dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
186
187         return j;
188 }
189
190
191 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
192                                              int freq)
193 {
194         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
195                 return;
196
197         /* Use the wpa_s used to control the P2P Device operation */
198         wpa_s = wpa_s->global->p2p_init_wpa_s;
199
200         if (wpa_s->conf->p2p_ignore_shared_freq &&
201             freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
202             wpas_p2p_num_unused_channels(wpa_s) > 0) {
203                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
204                            freq);
205                 freq = 0;
206         }
207         p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
208 }
209
210
211 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
212                                       struct wpa_scan_results *scan_res)
213 {
214         size_t i;
215
216         if (wpa_s->p2p_scan_work) {
217                 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
218                 wpa_s->p2p_scan_work = NULL;
219                 radio_work_done(work);
220         }
221
222         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
223                 return;
224
225         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
226                    (int) scan_res->num);
227
228         for (i = 0; i < scan_res->num; i++) {
229                 struct wpa_scan_res *bss = scan_res->res[i];
230                 struct os_reltime time_tmp_age, entry_ts;
231                 const u8 *ies;
232                 size_t ies_len;
233
234                 time_tmp_age.sec = bss->age / 1000;
235                 time_tmp_age.usec = (bss->age % 1000) * 1000;
236                 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
237
238                 ies = (const u8 *) (bss + 1);
239                 ies_len = bss->ie_len;
240                 if (bss->beacon_ie_len > 0 &&
241                     !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
242                     wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
243                         wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
244                                    MACSTR, MAC2STR(bss->bssid));
245                         ies = ies + ies_len;
246                         ies_len = bss->beacon_ie_len;
247                 }
248
249
250                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
251                                          bss->freq, &entry_ts, bss->level,
252                                          ies, ies_len) > 0)
253                         break;
254         }
255
256         p2p_scan_res_handled(wpa_s->global->p2p);
257 }
258
259
260 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
261 {
262         struct wpa_supplicant *wpa_s = work->wpa_s;
263         struct wpa_driver_scan_params *params = work->ctx;
264         int ret;
265
266         if (deinit) {
267                 if (!work->started) {
268                         wpa_scan_free_params(params);
269                         return;
270                 }
271
272                 wpa_s->p2p_scan_work = NULL;
273                 return;
274         }
275
276         ret = wpa_drv_scan(wpa_s, params);
277         wpa_scan_free_params(params);
278         work->ctx = NULL;
279         if (ret) {
280                 radio_work_done(work);
281                 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
282                 return;
283         }
284
285         p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
286         os_get_reltime(&wpa_s->scan_trigger_time);
287         wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
288         wpa_s->own_scan_requested = 1;
289         wpa_s->p2p_scan_work = work;
290 }
291
292
293 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
294                                           int freq)
295 {
296         if (wpa_s->global->p2p_24ghz_social_channels &&
297             (freq == 2412 || freq == 2437 || freq == 2462)) {
298                 /*
299                  * Search all social channels regardless of whether these have
300                  * been disabled for P2P operating channel use to avoid missing
301                  * peers.
302                  */
303                 return 1;
304         }
305         return p2p_supported_freq(wpa_s->global->p2p, freq);
306 }
307
308
309 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
310                          unsigned int num_req_dev_types,
311                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
312 {
313         struct wpa_supplicant *wpa_s = ctx;
314         struct wpa_driver_scan_params *params = NULL;
315         struct wpabuf *wps_ie, *ies;
316         unsigned int num_channels = 0;
317         int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
318         size_t ielen;
319         u8 *n, i;
320
321         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
322                 return -1;
323
324         if (wpa_s->p2p_scan_work) {
325                 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
326                 return -1;
327         }
328
329         params = os_zalloc(sizeof(*params));
330         if (params == NULL)
331                 return -1;
332
333         /* P2P Wildcard SSID */
334         params->num_ssids = 1;
335         n = os_malloc(P2P_WILDCARD_SSID_LEN);
336         if (n == NULL)
337                 goto fail;
338         os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
339         params->ssids[0].ssid = n;
340         params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
341
342         wpa_s->wps->dev.p2p = 1;
343         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
344                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
345                                         num_req_dev_types, req_dev_types);
346         if (wps_ie == NULL)
347                 goto fail;
348
349         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
350         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
351         if (ies == NULL) {
352                 wpabuf_free(wps_ie);
353                 goto fail;
354         }
355         wpabuf_put_buf(ies, wps_ie);
356         wpabuf_free(wps_ie);
357
358         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
359
360         params->p2p_probe = 1;
361         n = os_malloc(wpabuf_len(ies));
362         if (n == NULL) {
363                 wpabuf_free(ies);
364                 goto fail;
365         }
366         os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
367         params->extra_ies = n;
368         params->extra_ies_len = wpabuf_len(ies);
369         wpabuf_free(ies);
370
371         switch (type) {
372         case P2P_SCAN_SOCIAL:
373                 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
374                                           sizeof(int));
375                 if (params->freqs == NULL)
376                         goto fail;
377                 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
378                         if (wpas_p2p_search_social_channel(
379                                     wpa_s, social_channels_freq[i]))
380                                 params->freqs[num_channels++] =
381                                         social_channels_freq[i];
382                 }
383                 params->freqs[num_channels++] = 0;
384                 break;
385         case P2P_SCAN_FULL:
386                 break;
387         case P2P_SCAN_SPECIFIC:
388                 params->freqs = os_calloc(2, sizeof(int));
389                 if (params->freqs == NULL)
390                         goto fail;
391                 params->freqs[0] = freq;
392                 params->freqs[1] = 0;
393                 break;
394         case P2P_SCAN_SOCIAL_PLUS_ONE:
395                 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
396                                           sizeof(int));
397                 if (params->freqs == NULL)
398                         goto fail;
399                 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
400                         if (wpas_p2p_search_social_channel(
401                                     wpa_s, social_channels_freq[i]))
402                                 params->freqs[num_channels++] =
403                                         social_channels_freq[i];
404                 }
405                 if (p2p_supported_freq(wpa_s->global->p2p, freq))
406                         params->freqs[num_channels++] = freq;
407                 params->freqs[num_channels++] = 0;
408                 break;
409         }
410
411         radio_remove_works(wpa_s, "p2p-scan", 0);
412         if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
413                            params) < 0)
414                 goto fail;
415         return 0;
416
417 fail:
418         wpa_scan_free_params(params);
419         return -1;
420 }
421
422
423 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
424 {
425         switch (p2p_group_interface) {
426         case P2P_GROUP_INTERFACE_PENDING:
427                 return WPA_IF_P2P_GROUP;
428         case P2P_GROUP_INTERFACE_GO:
429                 return WPA_IF_P2P_GO;
430         case P2P_GROUP_INTERFACE_CLIENT:
431                 return WPA_IF_P2P_CLIENT;
432         }
433
434         return WPA_IF_P2P_GROUP;
435 }
436
437
438 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
439                                                   const u8 *ssid,
440                                                   size_t ssid_len, int *go)
441 {
442         struct wpa_ssid *s;
443
444         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
445                 for (s = wpa_s->conf->ssid; s; s = s->next) {
446                         if (s->disabled != 0 || !s->p2p_group ||
447                             s->ssid_len != ssid_len ||
448                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
449                                 continue;
450                         if (s->mode == WPAS_MODE_P2P_GO &&
451                             s != wpa_s->current_ssid)
452                                 continue;
453                         if (go)
454                                 *go = s->mode == WPAS_MODE_P2P_GO;
455                         return wpa_s;
456                 }
457         }
458
459         return NULL;
460 }
461
462
463 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
464 {
465         struct wpa_supplicant *wpa_s = eloop_ctx;
466         wpa_printf(MSG_DEBUG,
467                    "P2P: Complete previously requested removal of %s",
468                    wpa_s->ifname);
469         wpas_p2p_disconnect(wpa_s);
470 }
471
472
473 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
474                                       struct wpa_supplicant *calling_wpa_s)
475 {
476         if (calling_wpa_s == wpa_s && wpa_s &&
477             wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
478                 /*
479                  * The calling wpa_s instance is going to be removed. Do that
480                  * from an eloop callback to keep the instance available until
481                  * the caller has returned. This my be needed, e.g., to provide
482                  * control interface responses on the per-interface socket.
483                  */
484                 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
485                                            wpa_s, NULL) < 0)
486                         return -1;
487                 return 0;
488         }
489
490         return wpas_p2p_disconnect(wpa_s);
491 }
492
493
494 /* Determine total number of clients in active groups where we are the GO */
495 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
496 {
497         unsigned int count = 0;
498         struct wpa_ssid *s;
499
500         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
501                 for (s = wpa_s->conf->ssid; s; s = s->next) {
502                         wpa_printf(MSG_DEBUG,
503                                    "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
504                                    wpa_s, s, s->disabled, s->p2p_group,
505                                    s->mode);
506                         if (!s->disabled && s->p2p_group &&
507                             s->mode == WPAS_MODE_P2P_GO) {
508                                 count += p2p_get_group_num_members(
509                                         wpa_s->p2p_group);
510                         }
511                 }
512         }
513
514         return count;
515 }
516
517
518 /* Find an interface for a P2P group where we are the GO */
519 static struct wpa_supplicant *
520 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
521 {
522         struct wpa_supplicant *save = NULL;
523         struct wpa_ssid *s;
524
525         if (!wpa_s)
526                 return NULL;
527
528         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
529                 for (s = wpa_s->conf->ssid; s; s = s->next) {
530                         if (s->disabled || !s->p2p_group ||
531                             s->mode != WPAS_MODE_P2P_GO)
532                                 continue;
533
534                         /* Prefer a group with connected clients */
535                         if (p2p_get_group_num_members(wpa_s->p2p_group))
536                                 return wpa_s;
537                         save = wpa_s;
538                 }
539         }
540
541         /* No group with connected clients, so pick the one without (if any) */
542         return save;
543 }
544
545
546 /* Find an active P2P group where we are the GO */
547 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
548                                                 u8 *bssid)
549 {
550         struct wpa_ssid *s, *empty = NULL;
551
552         if (!wpa_s)
553                 return 0;
554
555         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
556                 for (s = wpa_s->conf->ssid; s; s = s->next) {
557                         if (s->disabled || !s->p2p_group ||
558                             s->mode != WPAS_MODE_P2P_GO)
559                                 continue;
560
561                         os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
562                         if (p2p_get_group_num_members(wpa_s->p2p_group))
563                                 return s;
564                         empty = s;
565                 }
566         }
567
568         return empty;
569 }
570
571
572 /* Find a persistent group where we are the GO */
573 static struct wpa_ssid *
574 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
575 {
576         struct wpa_ssid *s;
577
578         for (s = wpa_s->conf->ssid; s; s = s->next) {
579                 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
580                         return s;
581         }
582
583         return NULL;
584 }
585
586
587 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
588 {
589         struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
590         struct wpa_ssid *s;
591         u8 conncap = P2PS_SETUP_NONE;
592         unsigned int owned_members = 0;
593         unsigned int owner = 0;
594         unsigned int client = 0;
595         struct wpa_supplicant *go_wpa_s;
596         struct wpa_ssid *persistent_go;
597         int p2p_no_group_iface;
598
599         wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
600
601         /*
602          * For non-concurrent capable devices:
603          * If persistent_go, then no new.
604          * If GO, then no client.
605          * If client, then no GO.
606          */
607         go_wpa_s = wpas_p2p_get_go_group(wpa_s);
608         persistent_go = wpas_p2p_get_persistent_go(wpa_s);
609         p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
610
611         wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
612                    go_wpa_s, persistent_go);
613
614         for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
615              tmp_wpa_s = tmp_wpa_s->next) {
616                 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
617                         wpa_printf(MSG_DEBUG,
618                                    "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
619                                    tmp_wpa_s, s, s->disabled,
620                                    s->p2p_group, s->mode);
621                         if (!s->disabled && s->p2p_group) {
622                                 if (s->mode == WPAS_MODE_P2P_GO) {
623                                         owned_members +=
624                                                 p2p_get_group_num_members(
625                                                         tmp_wpa_s->p2p_group);
626                                         owner++;
627                                 } else
628                                         client++;
629                         }
630                 }
631         }
632
633         /* If not concurrent, restrict our choices */
634         if (p2p_no_group_iface) {
635                 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
636
637                 if (client)
638                         return P2PS_SETUP_NONE;
639
640                 if (go_wpa_s) {
641                         if (role == P2PS_SETUP_CLIENT ||
642                             incoming == P2PS_SETUP_GROUP_OWNER ||
643                             p2p_client_limit_reached(go_wpa_s->p2p_group))
644                                 return P2PS_SETUP_NONE;
645
646                         return P2PS_SETUP_GROUP_OWNER;
647                 }
648
649                 if (persistent_go) {
650                         if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
651                                 if (!incoming)
652                                         return P2PS_SETUP_GROUP_OWNER |
653                                                 P2PS_SETUP_CLIENT;
654                                 if (incoming == P2PS_SETUP_NEW) {
655                                         u8 r;
656
657                                         if (os_get_random(&r, sizeof(r)) < 0 ||
658                                             (r & 1))
659                                                 return P2PS_SETUP_CLIENT;
660                                         return P2PS_SETUP_GROUP_OWNER;
661                                 }
662                         }
663                 }
664         }
665
666         /* If a required role has been specified, handle it here */
667         if (role && role != P2PS_SETUP_NEW) {
668                 switch (incoming) {
669                 case P2PS_SETUP_NONE:
670                 case P2PS_SETUP_NEW:
671                 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
672                 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
673                         conncap = role;
674                         goto grp_owner;
675
676                 case P2PS_SETUP_GROUP_OWNER:
677                         /*
678                          * Must be a complimentary role - cannot be a client to
679                          * more than one peer.
680                          */
681                         if (incoming == role || client)
682                                 return P2PS_SETUP_NONE;
683
684                         return P2PS_SETUP_CLIENT;
685
686                 case P2PS_SETUP_CLIENT:
687                         /* Must be a complimentary role */
688                         if (incoming != role) {
689                                 conncap = P2PS_SETUP_GROUP_OWNER;
690                                 goto grp_owner;
691                         }
692
693                 default:
694                         return P2PS_SETUP_NONE;
695                 }
696         }
697
698         /*
699          * For now, we only will support ownership of one group, and being a
700          * client of one group. Therefore, if we have either an existing GO
701          * group, or an existing client group, we will not do a new GO
702          * negotiation, but rather try to re-use the existing groups.
703          */
704         switch (incoming) {
705         case P2PS_SETUP_NONE:
706         case P2PS_SETUP_NEW:
707                 if (client)
708                         conncap = P2PS_SETUP_GROUP_OWNER;
709                 else if (!owned_members)
710                         conncap = P2PS_SETUP_NEW;
711                 else if (incoming == P2PS_SETUP_NONE)
712                         conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
713                 else
714                         conncap = P2PS_SETUP_CLIENT;
715                 break;
716
717         case P2PS_SETUP_CLIENT:
718                 conncap = P2PS_SETUP_GROUP_OWNER;
719                 break;
720
721         case P2PS_SETUP_GROUP_OWNER:
722                 if (!client)
723                         conncap = P2PS_SETUP_CLIENT;
724                 break;
725
726         case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
727         case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
728                 if (client)
729                         conncap = P2PS_SETUP_GROUP_OWNER;
730                 else {
731                         u8 r;
732
733                         if (os_get_random(&r, sizeof(r)) < 0 ||
734                             (r & 1))
735                                 conncap = P2PS_SETUP_CLIENT;
736                         else
737                                 conncap = P2PS_SETUP_GROUP_OWNER;
738                 }
739                 break;
740
741         default:
742                 return P2PS_SETUP_NONE;
743         }
744
745 grp_owner:
746         if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
747             (!incoming && (conncap & P2PS_SETUP_NEW))) {
748                 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
749                         conncap &= ~P2PS_SETUP_GROUP_OWNER;
750                 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
751                            owner, owned_members, conncap);
752
753                 s = wpas_p2p_get_persistent_go(wpa_s);
754
755                 if (!s && !owner && p2p_no_group_iface) {
756                         p2p_set_intended_addr(wpa_s->global->p2p,
757                                               wpa_s->own_addr);
758                 } else if (!s && !owner) {
759                         if (wpas_p2p_add_group_interface(wpa_s,
760                                                          WPA_IF_P2P_GO) < 0) {
761                                 wpa_printf(MSG_ERROR,
762                                            "P2P: Failed to allocate a new interface for the group");
763                                 return P2PS_SETUP_NONE;
764                         }
765                         wpa_s->global->pending_group_iface_for_p2ps = 1;
766                         p2p_set_intended_addr(wpa_s->global->p2p,
767                                               wpa_s->pending_interface_addr);
768                 }
769         }
770
771         return conncap;
772 }
773
774
775 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
776                                  enum p2p_group_removal_reason removal_reason)
777 {
778         struct wpa_ssid *ssid;
779         char *gtype;
780         const char *reason;
781
782         ssid = wpa_s->current_ssid;
783         if (ssid == NULL) {
784                 /*
785                  * The current SSID was not known, but there may still be a
786                  * pending P2P group interface waiting for provisioning or a
787                  * P2P group that is trying to reconnect.
788                  */
789                 ssid = wpa_s->conf->ssid;
790                 while (ssid) {
791                         if (ssid->p2p_group && ssid->disabled != 2)
792                                 break;
793                         ssid = ssid->next;
794                 }
795                 if (ssid == NULL &&
796                         wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
797                 {
798                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
799                                    "not found");
800                         return -1;
801                 }
802         }
803         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
804                 gtype = "GO";
805         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
806                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
807                 wpa_s->reassociate = 0;
808                 wpa_s->disconnected = 1;
809                 gtype = "client";
810         } else
811                 gtype = "GO";
812
813         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
814                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
815
816         if (os_strcmp(gtype, "client") == 0) {
817                 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
818                 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
819                                                 wpa_s, NULL)) {
820                         wpa_printf(MSG_DEBUG,
821                                    "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
822                         removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
823                         eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
824                                              wpa_s, NULL);
825                 }
826         }
827
828         if (wpa_s->cross_connect_in_use) {
829                 wpa_s->cross_connect_in_use = 0;
830                 wpa_msg_global(wpa_s->parent, MSG_INFO,
831                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
832                                wpa_s->ifname, wpa_s->cross_connect_uplink);
833         }
834         switch (removal_reason) {
835         case P2P_GROUP_REMOVAL_REQUESTED:
836                 reason = " reason=REQUESTED";
837                 break;
838         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
839                 reason = " reason=FORMATION_FAILED";
840                 break;
841         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
842                 reason = " reason=IDLE";
843                 break;
844         case P2P_GROUP_REMOVAL_UNAVAILABLE:
845                 reason = " reason=UNAVAILABLE";
846                 break;
847         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
848                 reason = " reason=GO_ENDING_SESSION";
849                 break;
850         case P2P_GROUP_REMOVAL_PSK_FAILURE:
851                 reason = " reason=PSK_FAILURE";
852                 break;
853         case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
854                 reason = " reason=FREQ_CONFLICT";
855                 break;
856         default:
857                 reason = "";
858                 break;
859         }
860         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
861                 wpa_msg_global(wpa_s->parent, MSG_INFO,
862                                P2P_EVENT_GROUP_REMOVED "%s %s%s",
863                                wpa_s->ifname, gtype, reason);
864         }
865
866         if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
867                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
868         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
869                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
870         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
871                                  wpa_s->parent, NULL) > 0) {
872                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
873                            "timeout");
874                 wpa_s->p2p_in_provisioning = 0;
875                 wpas_p2p_group_formation_failed(wpa_s, 1);
876         }
877
878         wpa_s->p2p_in_invitation = 0;
879
880         /*
881          * Make sure wait for the first client does not remain active after the
882          * group has been removed.
883          */
884         wpa_s->global->p2p_go_wait_client.sec = 0;
885
886         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
887                 struct wpa_global *global;
888                 char *ifname;
889                 enum wpa_driver_if_type type;
890                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
891                         wpa_s->ifname);
892                 global = wpa_s->global;
893                 ifname = os_strdup(wpa_s->ifname);
894                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
895                 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
896                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
897                 wpa_s = global->ifaces;
898                 if (wpa_s && ifname)
899                         wpa_drv_if_remove(wpa_s, type, ifname);
900                 os_free(ifname);
901                 return 1;
902         }
903
904         if (!wpa_s->p2p_go_group_formation_completed) {
905                 wpa_s->global->p2p_group_formation = NULL;
906                 wpa_s->p2p_in_provisioning = 0;
907         }
908
909         wpa_s->show_group_started = 0;
910         os_free(wpa_s->go_params);
911         wpa_s->go_params = NULL;
912
913         os_free(wpa_s->p2p_group_common_freqs);
914         wpa_s->p2p_group_common_freqs = NULL;
915         wpa_s->p2p_group_common_freqs_num = 0;
916
917         wpa_s->waiting_presence_resp = 0;
918
919         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
920         if (ssid && (ssid->p2p_group ||
921                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
922                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
923                 int id = ssid->id;
924                 if (ssid == wpa_s->current_ssid) {
925                         wpa_sm_set_config(wpa_s->wpa, NULL);
926                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
927                         wpa_s->current_ssid = NULL;
928                 }
929                 /*
930                  * Networks objects created during any P2P activities are not
931                  * exposed out as they might/will confuse certain non-P2P aware
932                  * applications since these network objects won't behave like
933                  * regular ones.
934                  *
935                  * Likewise, we don't send out network removed signals for such
936                  * network objects.
937                  */
938                 wpa_config_remove_network(wpa_s->conf, id);
939                 wpa_supplicant_clear_status(wpa_s);
940                 wpa_supplicant_cancel_sched_scan(wpa_s);
941         } else {
942                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
943                            "found");
944         }
945         if (wpa_s->ap_iface)
946                 wpa_supplicant_ap_deinit(wpa_s);
947         else
948                 wpa_drv_deinit_p2p_cli(wpa_s);
949
950         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
951
952         return 0;
953 }
954
955
956 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
957                                      u8 *go_dev_addr,
958                                      const u8 *ssid, size_t ssid_len)
959 {
960         struct wpa_bss *bss;
961         const u8 *bssid;
962         struct wpabuf *p2p;
963         u8 group_capab;
964         const u8 *addr;
965
966         if (wpa_s->go_params)
967                 bssid = wpa_s->go_params->peer_interface_addr;
968         else
969                 bssid = wpa_s->bssid;
970
971         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
972         if (bss == NULL && wpa_s->go_params &&
973             !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
974                 bss = wpa_bss_get_p2p_dev_addr(
975                         wpa_s, wpa_s->go_params->peer_device_addr);
976         if (bss == NULL) {
977                 u8 iface_addr[ETH_ALEN];
978                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
979                                            iface_addr) == 0)
980                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
981         }
982         if (bss == NULL) {
983                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
984                            "group is persistent - BSS " MACSTR " not found",
985                            MAC2STR(bssid));
986                 return 0;
987         }
988
989         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
990         if (p2p == NULL)
991                 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
992                                                          P2P_IE_VENDOR_TYPE);
993         if (p2p == NULL) {
994                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
995                            "group is persistent - BSS " MACSTR
996                            " did not include P2P IE", MAC2STR(bssid));
997                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
998                             (u8 *) (bss + 1), bss->ie_len);
999                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1000                             ((u8 *) bss + 1) + bss->ie_len,
1001                             bss->beacon_ie_len);
1002                 return 0;
1003         }
1004
1005         group_capab = p2p_get_group_capab(p2p);
1006         addr = p2p_get_go_dev_addr(p2p);
1007         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1008                    "group_capab=0x%x", group_capab);
1009         if (addr) {
1010                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1011                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1012                            MAC2STR(addr));
1013         } else
1014                 os_memset(go_dev_addr, 0, ETH_ALEN);
1015         wpabuf_free(p2p);
1016
1017         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1018                    "go_dev_addr=" MACSTR,
1019                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1020
1021         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1022 }
1023
1024
1025 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1026                                            struct wpa_ssid *ssid,
1027                                            const u8 *go_dev_addr)
1028 {
1029         struct wpa_ssid *s;
1030         int changed = 0;
1031
1032         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1033                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1034         for (s = wpa_s->conf->ssid; s; s = s->next) {
1035                 if (s->disabled == 2 &&
1036                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1037                     s->ssid_len == ssid->ssid_len &&
1038                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1039                         break;
1040         }
1041
1042         if (s) {
1043                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1044                            "entry");
1045                 if (ssid->passphrase && !s->passphrase)
1046                         changed = 1;
1047                 else if (ssid->passphrase && s->passphrase &&
1048                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
1049                         changed = 1;
1050         } else {
1051                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1052                            "entry");
1053                 changed = 1;
1054                 s = wpa_config_add_network(wpa_s->conf);
1055                 if (s == NULL)
1056                         return -1;
1057
1058                 /*
1059                  * Instead of network_added we emit persistent_group_added
1060                  * notification. Also to keep the defense checks in
1061                  * persistent_group obj registration method, we set the
1062                  * relevant flags in s to designate it as a persistent group.
1063                  */
1064                 s->p2p_group = 1;
1065                 s->p2p_persistent_group = 1;
1066                 wpas_notify_persistent_group_added(wpa_s, s);
1067                 wpa_config_set_network_defaults(s);
1068         }
1069
1070         s->p2p_group = 1;
1071         s->p2p_persistent_group = 1;
1072         s->disabled = 2;
1073         s->bssid_set = 1;
1074         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1075         s->mode = ssid->mode;
1076         s->auth_alg = WPA_AUTH_ALG_OPEN;
1077         s->key_mgmt = WPA_KEY_MGMT_PSK;
1078         s->proto = WPA_PROTO_RSN;
1079         s->pairwise_cipher = WPA_CIPHER_CCMP;
1080         s->export_keys = 1;
1081         if (ssid->passphrase) {
1082                 os_free(s->passphrase);
1083                 s->passphrase = os_strdup(ssid->passphrase);
1084         }
1085         if (ssid->psk_set) {
1086                 s->psk_set = 1;
1087                 os_memcpy(s->psk, ssid->psk, 32);
1088         }
1089         if (s->passphrase && !s->psk_set)
1090                 wpa_config_update_psk(s);
1091         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1092                 os_free(s->ssid);
1093                 s->ssid = os_malloc(ssid->ssid_len);
1094         }
1095         if (s->ssid) {
1096                 s->ssid_len = ssid->ssid_len;
1097                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1098         }
1099         if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1100                 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1101                 wpa_s->global->add_psk = NULL;
1102                 changed = 1;
1103         }
1104
1105         if (changed && wpa_s->conf->update_config &&
1106             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1107                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1108         }
1109
1110         return s->id;
1111 }
1112
1113
1114 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1115                                                  const u8 *addr)
1116 {
1117         struct wpa_ssid *ssid, *s;
1118         u8 *n;
1119         size_t i;
1120         int found = 0;
1121         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1122
1123         ssid = wpa_s->current_ssid;
1124         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1125             !ssid->p2p_persistent_group)
1126                 return;
1127
1128         for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1129                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1130                         continue;
1131
1132                 if (s->ssid_len == ssid->ssid_len &&
1133                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1134                         break;
1135         }
1136
1137         if (s == NULL)
1138                 return;
1139
1140         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1141                 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1142                               ETH_ALEN) != 0)
1143                         continue;
1144
1145                 if (i == s->num_p2p_clients - 1)
1146                         return; /* already the most recent entry */
1147
1148                 /* move the entry to mark it most recent */
1149                 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1150                            s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1151                            (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1152                 os_memcpy(s->p2p_client_list +
1153                           (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1154                           ETH_ALEN);
1155                 os_memset(s->p2p_client_list +
1156                           (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1157                           0xff, ETH_ALEN);
1158                 found = 1;
1159                 break;
1160         }
1161
1162         if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1163                 n = os_realloc_array(s->p2p_client_list,
1164                                      s->num_p2p_clients + 1, 2 * ETH_ALEN);
1165                 if (n == NULL)
1166                         return;
1167                 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1168                           ETH_ALEN);
1169                 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1170                           0xff, ETH_ALEN);
1171                 s->p2p_client_list = n;
1172                 s->num_p2p_clients++;
1173         } else if (!found && s->p2p_client_list) {
1174                 /* Not enough room for an additional entry - drop the oldest
1175                  * entry */
1176                 os_memmove(s->p2p_client_list,
1177                            s->p2p_client_list + 2 * ETH_ALEN,
1178                            (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1179                 os_memcpy(s->p2p_client_list +
1180                           (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1181                           addr, ETH_ALEN);
1182                 os_memset(s->p2p_client_list +
1183                           (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1184                           0xff, ETH_ALEN);
1185         }
1186
1187         if (p2p_wpa_s->conf->update_config &&
1188             wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1189                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1190 }
1191
1192
1193 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1194                                    int go, struct wpa_ssid *ssid, int freq,
1195                                    const u8 *psk, const char *passphrase,
1196                                    const u8 *go_dev_addr, int persistent,
1197                                    const char *extra)
1198 {
1199         const char *ssid_txt;
1200         char psk_txt[65];
1201
1202         if (psk)
1203                 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1204         else
1205                 psk_txt[0] = '\0';
1206
1207         if (ssid)
1208                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1209         else
1210                 ssid_txt = "";
1211
1212         if (passphrase && passphrase[0] == '\0')
1213                 passphrase = NULL;
1214
1215         /*
1216          * Include PSK/passphrase only in the control interface message and
1217          * leave it out from the debug log entry.
1218          */
1219         wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1220                             P2P_EVENT_GROUP_STARTED
1221                             "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1222                             MACSTR "%s%s",
1223                             wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1224                             psk ? " psk=" : "", psk_txt,
1225                             passphrase ? " passphrase=\"" : "",
1226                             passphrase ? passphrase : "",
1227                             passphrase ? "\"" : "",
1228                             MAC2STR(go_dev_addr),
1229                             persistent ? " [PERSISTENT]" : "", extra);
1230         wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1231                    "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1232                    wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1233                    MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1234                    extra);
1235 }
1236
1237
1238 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1239                                            int success, int already_deleted)
1240 {
1241         struct wpa_ssid *ssid;
1242         int client;
1243         int persistent;
1244         u8 go_dev_addr[ETH_ALEN];
1245         int network_id = -1;
1246
1247         /*
1248          * This callback is likely called for the main interface. Update wpa_s
1249          * to use the group interface if a new interface was created for the
1250          * group.
1251          */
1252         if (wpa_s->global->p2p_group_formation)
1253                 wpa_s = wpa_s->global->p2p_group_formation;
1254         if (wpa_s->p2p_go_group_formation_completed) {
1255                 wpa_s->global->p2p_group_formation = NULL;
1256                 wpa_s->p2p_in_provisioning = 0;
1257         }
1258         wpa_s->p2p_in_invitation = 0;
1259         wpa_s->group_formation_reported = 1;
1260
1261         if (!success) {
1262                 wpa_msg_global(wpa_s->parent, MSG_INFO,
1263                                P2P_EVENT_GROUP_FORMATION_FAILURE);
1264                 if (already_deleted)
1265                         return;
1266                 wpas_p2p_group_delete(wpa_s,
1267                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
1268                 return;
1269         }
1270
1271         wpa_msg_global(wpa_s->parent, MSG_INFO,
1272                        P2P_EVENT_GROUP_FORMATION_SUCCESS);
1273
1274         ssid = wpa_s->current_ssid;
1275         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1276                 ssid->mode = WPAS_MODE_P2P_GO;
1277                 p2p_group_notif_formation_done(wpa_s->p2p_group);
1278                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1279         }
1280
1281         persistent = 0;
1282         if (ssid) {
1283                 client = ssid->mode == WPAS_MODE_INFRA;
1284                 if (ssid->mode == WPAS_MODE_P2P_GO) {
1285                         persistent = ssid->p2p_persistent_group;
1286                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1287                                   ETH_ALEN);
1288                 } else
1289                         persistent = wpas_p2p_persistent_group(wpa_s,
1290                                                                go_dev_addr,
1291                                                                ssid->ssid,
1292                                                                ssid->ssid_len);
1293         } else {
1294                 client = wpa_s->p2p_group_interface ==
1295                         P2P_GROUP_INTERFACE_CLIENT;
1296                 os_memset(go_dev_addr, 0, ETH_ALEN);
1297         }
1298
1299         wpa_s->show_group_started = 0;
1300         if (client) {
1301                 /*
1302                  * Indicate event only after successfully completed 4-way
1303                  * handshake, i.e., when the interface is ready for data
1304                  * packets.
1305                  */
1306                 wpa_s->show_group_started = 1;
1307         } else {
1308                 wpas_p2p_group_started(wpa_s, 1, ssid,
1309                                        ssid ? ssid->frequency : 0,
1310                                        ssid && ssid->passphrase == NULL &&
1311                                        ssid->psk_set ? ssid->psk : NULL,
1312                                        ssid ? ssid->passphrase : NULL,
1313                                        go_dev_addr, persistent, "");
1314                 wpas_p2p_cross_connect_setup(wpa_s);
1315                 wpas_p2p_set_group_idle_timeout(wpa_s);
1316         }
1317
1318         if (persistent)
1319                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1320                                                              ssid, go_dev_addr);
1321         else {
1322                 os_free(wpa_s->global->add_psk);
1323                 wpa_s->global->add_psk = NULL;
1324         }
1325         if (network_id < 0 && ssid)
1326                 network_id = ssid->id;
1327         if (!client) {
1328                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1329                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1330         }
1331 }
1332
1333
1334 struct send_action_work {
1335         unsigned int freq;
1336         u8 dst[ETH_ALEN];
1337         u8 src[ETH_ALEN];
1338         u8 bssid[ETH_ALEN];
1339         size_t len;
1340         unsigned int wait_time;
1341         u8 buf[0];
1342 };
1343
1344
1345 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1346                                               void *timeout_ctx)
1347 {
1348         struct wpa_supplicant *wpa_s = eloop_ctx;
1349
1350         if (!wpa_s->p2p_send_action_work)
1351                 return;
1352
1353         wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1354         os_free(wpa_s->p2p_send_action_work->ctx);
1355         radio_work_done(wpa_s->p2p_send_action_work);
1356         wpa_s->p2p_send_action_work = NULL;
1357 }
1358
1359
1360 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1361 {
1362         if (wpa_s->p2p_send_action_work) {
1363                 struct send_action_work *awork;
1364                 awork = wpa_s->p2p_send_action_work->ctx;
1365                 if (awork->wait_time == 0) {
1366                         os_free(awork);
1367                         radio_work_done(wpa_s->p2p_send_action_work);
1368                         wpa_s->p2p_send_action_work = NULL;
1369                 } else {
1370                         /*
1371                          * In theory, this should not be needed, but number of
1372                          * places in the P2P code is still using non-zero wait
1373                          * time for the last Action frame in the sequence and
1374                          * some of these do not call send_action_done().
1375                          */
1376                         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1377                                              wpa_s, NULL);
1378                         eloop_register_timeout(
1379                                 0, awork->wait_time * 1000,
1380                                 wpas_p2p_send_action_work_timeout,
1381                                 wpa_s, NULL);
1382                 }
1383         }
1384 }
1385
1386
1387 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1388                                            unsigned int freq,
1389                                            const u8 *dst, const u8 *src,
1390                                            const u8 *bssid,
1391                                            const u8 *data, size_t data_len,
1392                                            enum offchannel_send_action_result
1393                                            result)
1394 {
1395         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1396
1397         wpas_p2p_action_tx_clear(wpa_s);
1398
1399         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1400                 return;
1401
1402         switch (result) {
1403         case OFFCHANNEL_SEND_ACTION_SUCCESS:
1404                 res = P2P_SEND_ACTION_SUCCESS;
1405                 break;
1406         case OFFCHANNEL_SEND_ACTION_NO_ACK:
1407                 res = P2P_SEND_ACTION_NO_ACK;
1408                 break;
1409         case OFFCHANNEL_SEND_ACTION_FAILED:
1410                 res = P2P_SEND_ACTION_FAILED;
1411                 break;
1412         }
1413
1414         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1415
1416         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1417             wpa_s->pending_pd_before_join &&
1418             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1419              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1420             wpa_s->p2p_fallback_to_go_neg) {
1421                 wpa_s->pending_pd_before_join = 0;
1422                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1423                         "during p2p_connect-auto");
1424                 wpa_msg_global(wpa_s->parent, MSG_INFO,
1425                                P2P_EVENT_FALLBACK_TO_GO_NEG
1426                                "reason=no-ACK-to-PD-Req");
1427                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1428                 return;
1429         }
1430 }
1431
1432
1433 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1434 {
1435         struct wpa_supplicant *wpa_s = work->wpa_s;
1436         struct send_action_work *awork = work->ctx;
1437
1438         if (deinit) {
1439                 if (work->started) {
1440                         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1441                                              wpa_s, NULL);
1442                         wpa_s->p2p_send_action_work = NULL;
1443                         offchannel_send_action_done(wpa_s);
1444                 }
1445                 os_free(awork);
1446                 return;
1447         }
1448
1449         if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1450                                    awork->bssid, awork->buf, awork->len,
1451                                    awork->wait_time,
1452                                    wpas_p2p_send_action_tx_status, 1) < 0) {
1453                 os_free(awork);
1454                 radio_work_done(work);
1455                 return;
1456         }
1457         wpa_s->p2p_send_action_work = work;
1458 }
1459
1460
1461 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1462                                  unsigned int freq, const u8 *dst,
1463                                  const u8 *src, const u8 *bssid, const u8 *buf,
1464                                  size_t len, unsigned int wait_time)
1465 {
1466         struct send_action_work *awork;
1467
1468         if (wpa_s->p2p_send_action_work) {
1469                 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1470                 return -1;
1471         }
1472
1473         awork = os_zalloc(sizeof(*awork) + len);
1474         if (awork == NULL)
1475                 return -1;
1476
1477         awork->freq = freq;
1478         os_memcpy(awork->dst, dst, ETH_ALEN);
1479         os_memcpy(awork->src, src, ETH_ALEN);
1480         os_memcpy(awork->bssid, bssid, ETH_ALEN);
1481         awork->len = len;
1482         awork->wait_time = wait_time;
1483         os_memcpy(awork->buf, buf, len);
1484
1485         if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1486                            wpas_send_action_cb, awork) < 0) {
1487                 os_free(awork);
1488                 return -1;
1489         }
1490
1491         return 0;
1492 }
1493
1494
1495 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1496                             const u8 *src, const u8 *bssid, const u8 *buf,
1497                             size_t len, unsigned int wait_time)
1498 {
1499         struct wpa_supplicant *wpa_s = ctx;
1500         int listen_freq = -1, send_freq = -1;
1501
1502         if (wpa_s->p2p_listen_work)
1503                 listen_freq = wpa_s->p2p_listen_work->freq;
1504         if (wpa_s->p2p_send_action_work)
1505                 send_freq = wpa_s->p2p_send_action_work->freq;
1506         if (listen_freq != (int) freq && send_freq != (int) freq) {
1507                 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1508                            listen_freq, send_freq);
1509                 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1510                                              len, wait_time);
1511         }
1512
1513         wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1514         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1515                                       wait_time,
1516                                       wpas_p2p_send_action_tx_status, 1);
1517 }
1518
1519
1520 static void wpas_send_action_done(void *ctx)
1521 {
1522         struct wpa_supplicant *wpa_s = ctx;
1523
1524         if (wpa_s->p2p_send_action_work) {
1525                 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1526                                      wpa_s, NULL);
1527                 os_free(wpa_s->p2p_send_action_work->ctx);
1528                 radio_work_done(wpa_s->p2p_send_action_work);
1529                 wpa_s->p2p_send_action_work = NULL;
1530         }
1531
1532         offchannel_send_action_done(wpa_s);
1533 }
1534
1535
1536 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1537                                     struct p2p_go_neg_results *params)
1538 {
1539         if (wpa_s->go_params == NULL) {
1540                 wpa_s->go_params = os_malloc(sizeof(*params));
1541                 if (wpa_s->go_params == NULL)
1542                         return -1;
1543         }
1544         os_memcpy(wpa_s->go_params, params, sizeof(*params));
1545         return 0;
1546 }
1547
1548
1549 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1550                                     struct p2p_go_neg_results *res)
1551 {
1552         wpa_s->group_formation_reported = 0;
1553         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1554                    " dev_addr " MACSTR " wps_method %d",
1555                    MAC2STR(res->peer_interface_addr),
1556                    MAC2STR(res->peer_device_addr), res->wps_method);
1557         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1558                           res->ssid, res->ssid_len);
1559         wpa_supplicant_ap_deinit(wpa_s);
1560         wpas_copy_go_neg_results(wpa_s, res);
1561         if (res->wps_method == WPS_PBC) {
1562                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1563 #ifdef CONFIG_WPS_NFC
1564         } else if (res->wps_method == WPS_NFC) {
1565                 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1566                                    res->peer_interface_addr,
1567                                    wpa_s->parent->p2p_oob_dev_pw,
1568                                    wpa_s->parent->p2p_oob_dev_pw_id, 1,
1569                                    wpa_s->parent->p2p_oob_dev_pw_id ==
1570                                    DEV_PW_NFC_CONNECTION_HANDOVER ?
1571                                    wpa_s->parent->p2p_peer_oob_pubkey_hash :
1572                                    NULL,
1573                                    NULL, 0, 0);
1574 #endif /* CONFIG_WPS_NFC */
1575         } else {
1576                 u16 dev_pw_id = DEV_PW_DEFAULT;
1577                 if (wpa_s->p2p_wps_method == WPS_P2PS)
1578                         dev_pw_id = DEV_PW_P2PS_DEFAULT;
1579                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1580                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1581                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1582                                    wpa_s->p2p_pin, 1, dev_pw_id);
1583         }
1584 }
1585
1586
1587 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1588                                   struct wpa_ssid *ssid)
1589 {
1590         struct wpa_ssid *persistent;
1591         struct psk_list_entry *psk;
1592         struct hostapd_data *hapd;
1593
1594         if (!wpa_s->ap_iface)
1595                 return;
1596
1597         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1598                                              ssid->ssid_len);
1599         if (persistent == NULL)
1600                 return;
1601
1602         hapd = wpa_s->ap_iface->bss[0];
1603
1604         dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1605                          list) {
1606                 struct hostapd_wpa_psk *hpsk;
1607
1608                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1609                         MACSTR " psk=%d",
1610                         MAC2STR(psk->addr), psk->p2p);
1611                 hpsk = os_zalloc(sizeof(*hpsk));
1612                 if (hpsk == NULL)
1613                         break;
1614                 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1615                 if (psk->p2p)
1616                         os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1617                 else
1618                         os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1619                 hpsk->next = hapd->conf->ssid.wpa_psk;
1620                 hapd->conf->ssid.wpa_psk = hpsk;
1621         }
1622 }
1623
1624
1625 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1626 {
1627         unsigned int i;
1628
1629         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1630                 wpa_s->p2p_group_common_freqs_num);
1631
1632         for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1633                 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1634                         i, wpa_s->p2p_group_common_freqs[i]);
1635 }
1636
1637
1638 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1639                                            struct p2p_go_neg_results *params)
1640 {
1641         unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1642
1643         wpa_s->p2p_group_common_freqs_num = 0;
1644         os_free(wpa_s->p2p_group_common_freqs);
1645         wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1646         if (!wpa_s->p2p_group_common_freqs)
1647                 return;
1648
1649         for (i = 0; i < len; i++) {
1650                 if (!wpa_s->go_params->freq_list[i])
1651                         break;
1652                 wpa_s->p2p_group_common_freqs[i] =
1653                         wpa_s->go_params->freq_list[i];
1654         }
1655         wpa_s->p2p_group_common_freqs_num = i;
1656 }
1657
1658
1659 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1660 {
1661 #ifndef CONFIG_NO_CONFIG_WRITE
1662         if (wpa_s->parent->conf->update_config &&
1663             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1664                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1665 #endif /* CONFIG_NO_CONFIG_WRITE */
1666 }
1667
1668
1669 static void p2p_go_configured(void *ctx, void *data)
1670 {
1671         struct wpa_supplicant *wpa_s = ctx;
1672         struct p2p_go_neg_results *params = data;
1673         struct wpa_ssid *ssid;
1674         int network_id = -1;
1675
1676         p2p_go_save_group_common_freqs(wpa_s, params);
1677         p2p_go_dump_common_freqs(wpa_s);
1678
1679         ssid = wpa_s->current_ssid;
1680         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1681                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1682                 if (wpa_s->global->p2p_group_formation == wpa_s)
1683                         wpa_s->global->p2p_group_formation = NULL;
1684                 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1685                                        params->passphrase[0] == '\0' ?
1686                                        params->psk : NULL,
1687                                        params->passphrase,
1688                                        wpa_s->global->p2p_dev_addr,
1689                                        params->persistent_group, "");
1690                 wpa_s->group_formation_reported = 1;
1691
1692                 if (wpa_s->parent->p2ps_join_addr_valid) {
1693                         wpa_dbg(wpa_s, MSG_DEBUG,
1694                                 "P2PS: Setting default PIN for " MACSTR,
1695                                 MAC2STR(wpa_s->parent->p2ps_join_addr));
1696                         wpa_supplicant_ap_wps_pin(wpa_s,
1697                                                   wpa_s->parent->p2ps_join_addr,
1698                                                   "12345670", NULL, 0, 0);
1699                         wpa_s->parent->p2ps_join_addr_valid = 0;
1700                 }
1701
1702                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1703                 if (params->persistent_group) {
1704                         network_id = wpas_p2p_store_persistent_group(
1705                                 wpa_s->parent, ssid,
1706                                 wpa_s->global->p2p_dev_addr);
1707                         wpas_p2p_add_psk_list(wpa_s, ssid);
1708                 }
1709                 if (network_id < 0)
1710                         network_id = ssid->id;
1711                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1712                 wpas_p2p_cross_connect_setup(wpa_s);
1713                 wpas_p2p_set_group_idle_timeout(wpa_s);
1714
1715                 if (wpa_s->p2p_first_connection_timeout) {
1716                         wpa_dbg(wpa_s, MSG_DEBUG,
1717                                 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1718                                 wpa_s->p2p_first_connection_timeout);
1719                         wpa_s->p2p_go_group_formation_completed = 0;
1720                         wpa_s->global->p2p_group_formation = wpa_s;
1721                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1722                                              wpa_s->parent, NULL);
1723                         eloop_register_timeout(
1724                                 wpa_s->p2p_first_connection_timeout, 0,
1725                                 wpas_p2p_group_formation_timeout,
1726                                 wpa_s->parent, NULL);
1727                 }
1728
1729                 return;
1730         }
1731
1732         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1733         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1734                                               params->peer_interface_addr)) {
1735                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1736                            "filtering");
1737                 return;
1738         }
1739         if (params->wps_method == WPS_PBC) {
1740                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1741                                           params->peer_device_addr);
1742 #ifdef CONFIG_WPS_NFC
1743         } else if (params->wps_method == WPS_NFC) {
1744                 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1745                     DEV_PW_NFC_CONNECTION_HANDOVER &&
1746                     !wpa_s->parent->p2p_oob_dev_pw) {
1747                         wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1748                         return;
1749                 }
1750                 wpas_ap_wps_add_nfc_pw(
1751                         wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1752                         wpa_s->parent->p2p_oob_dev_pw,
1753                         wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1754                         wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1755 #endif /* CONFIG_WPS_NFC */
1756         } else if (wpa_s->p2p_pin[0])
1757                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1758                                           wpa_s->p2p_pin, NULL, 0, 0);
1759         os_free(wpa_s->go_params);
1760         wpa_s->go_params = NULL;
1761 }
1762
1763
1764 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1765                               struct p2p_go_neg_results *params,
1766                               int group_formation)
1767 {
1768         struct wpa_ssid *ssid;
1769
1770         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1771         if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1772                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1773                         "results");
1774                 return;
1775         }
1776
1777         ssid = wpa_config_add_network(wpa_s->conf);
1778         if (ssid == NULL) {
1779                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1780                 return;
1781         }
1782
1783         wpa_s->show_group_started = 0;
1784         wpa_s->p2p_go_group_formation_completed = 0;
1785         wpa_s->group_formation_reported = 0;
1786         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1787
1788         wpa_config_set_network_defaults(ssid);
1789         ssid->temporary = 1;
1790         ssid->p2p_group = 1;
1791         ssid->p2p_persistent_group = params->persistent_group;
1792         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1793                 WPAS_MODE_P2P_GO;
1794         ssid->frequency = params->freq;
1795         ssid->ht40 = params->ht40;
1796         ssid->vht = params->vht;
1797         ssid->ssid = os_zalloc(params->ssid_len + 1);
1798         if (ssid->ssid) {
1799                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1800                 ssid->ssid_len = params->ssid_len;
1801         }
1802         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1803         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1804         ssid->proto = WPA_PROTO_RSN;
1805         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1806         ssid->group_cipher = WPA_CIPHER_CCMP;
1807         if (params->freq > 56160) {
1808                 /*
1809                  * Enable GCMP instead of CCMP as pairwise_cipher and
1810                  * group_cipher in 60 GHz.
1811                  */
1812                 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1813                 ssid->group_cipher = WPA_CIPHER_GCMP;
1814         }
1815         if (os_strlen(params->passphrase) > 0) {
1816                 ssid->passphrase = os_strdup(params->passphrase);
1817                 if (ssid->passphrase == NULL) {
1818                         wpa_msg_global(wpa_s, MSG_ERROR,
1819                                        "P2P: Failed to copy passphrase for GO");
1820                         wpa_config_remove_network(wpa_s->conf, ssid->id);
1821                         return;
1822                 }
1823         } else
1824                 ssid->passphrase = NULL;
1825         ssid->psk_set = params->psk_set;
1826         if (ssid->psk_set)
1827                 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1828         else if (ssid->passphrase)
1829                 wpa_config_update_psk(ssid);
1830         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1831
1832         wpa_s->ap_configured_cb = p2p_go_configured;
1833         wpa_s->ap_configured_cb_ctx = wpa_s;
1834         wpa_s->ap_configured_cb_data = wpa_s->go_params;
1835         wpa_s->scan_req = NORMAL_SCAN_REQ;
1836         wpa_s->connect_without_scan = ssid;
1837         wpa_s->reassociate = 1;
1838         wpa_s->disconnected = 0;
1839         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1840                 "start GO)");
1841         wpa_supplicant_req_scan(wpa_s, 0, 0);
1842 }
1843
1844
1845 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1846                                   const struct wpa_supplicant *src)
1847 {
1848         struct wpa_config *d;
1849         const struct wpa_config *s;
1850
1851         d = dst->conf;
1852         s = src->conf;
1853
1854 #define C(n) if (s->n) d->n = os_strdup(s->n)
1855         C(device_name);
1856         C(manufacturer);
1857         C(model_name);
1858         C(model_number);
1859         C(serial_number);
1860         C(config_methods);
1861 #undef C
1862
1863         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1864         os_memcpy(d->sec_device_type, s->sec_device_type,
1865                   sizeof(d->sec_device_type));
1866         d->num_sec_device_types = s->num_sec_device_types;
1867
1868         d->p2p_group_idle = s->p2p_group_idle;
1869         d->p2p_intra_bss = s->p2p_intra_bss;
1870         d->persistent_reconnect = s->persistent_reconnect;
1871         d->max_num_sta = s->max_num_sta;
1872         d->pbc_in_m1 = s->pbc_in_m1;
1873         d->ignore_old_scan_res = s->ignore_old_scan_res;
1874         d->beacon_int = s->beacon_int;
1875         d->dtim_period = s->dtim_period;
1876         d->p2p_go_ctwindow = s->p2p_go_ctwindow;
1877         d->disassoc_low_ack = s->disassoc_low_ack;
1878         d->disable_scan_offload = s->disable_scan_offload;
1879         d->passive_scan = s->passive_scan;
1880
1881         if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1882                 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1883                 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1884         }
1885         d->p2p_cli_probe = s->p2p_cli_probe;
1886 }
1887
1888
1889 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1890                                       char *ifname, size_t len)
1891 {
1892         char *ifname_ptr = wpa_s->ifname;
1893
1894         if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1895                        os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1896                 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1897         }
1898
1899         os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1900         if (os_strlen(ifname) >= IFNAMSIZ &&
1901             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1902                 int res;
1903
1904                 /* Try to avoid going over the IFNAMSIZ length limit */
1905                 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1906                 if (os_snprintf_error(len, res) && len)
1907                         ifname[len - 1] = '\0';
1908         }
1909 }
1910
1911
1912 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1913                                         enum wpa_driver_if_type type)
1914 {
1915         char ifname[120], force_ifname[120];
1916
1917         if (wpa_s->pending_interface_name[0]) {
1918                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1919                            "- skip creation of a new one");
1920                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1921                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1922                                    "unknown?! ifname='%s'",
1923                                    wpa_s->pending_interface_name);
1924                         return -1;
1925                 }
1926                 return 0;
1927         }
1928
1929         wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1930         force_ifname[0] = '\0';
1931
1932         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1933                    ifname);
1934         wpa_s->p2p_group_idx++;
1935
1936         wpa_s->pending_interface_type = type;
1937         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1938                            wpa_s->pending_interface_addr, NULL) < 0) {
1939                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1940                            "interface");
1941                 return -1;
1942         }
1943
1944         if (force_ifname[0]) {
1945                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1946                            force_ifname);
1947                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1948                            sizeof(wpa_s->pending_interface_name));
1949         } else
1950                 os_strlcpy(wpa_s->pending_interface_name, ifname,
1951                            sizeof(wpa_s->pending_interface_name));
1952         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1953                    MACSTR, wpa_s->pending_interface_name,
1954                    MAC2STR(wpa_s->pending_interface_addr));
1955
1956         return 0;
1957 }
1958
1959
1960 static void wpas_p2p_remove_pending_group_interface(
1961         struct wpa_supplicant *wpa_s)
1962 {
1963         if (!wpa_s->pending_interface_name[0] ||
1964             is_zero_ether_addr(wpa_s->pending_interface_addr))
1965                 return; /* No pending virtual interface */
1966
1967         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1968                    wpa_s->pending_interface_name);
1969         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1970                           wpa_s->pending_interface_name);
1971         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1972         wpa_s->pending_interface_name[0] = '\0';
1973         wpa_s->global->pending_group_iface_for_p2ps = 0;
1974 }
1975
1976
1977 static struct wpa_supplicant *
1978 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1979 {
1980         struct wpa_interface iface;
1981         struct wpa_supplicant *group_wpa_s;
1982
1983         if (!wpa_s->pending_interface_name[0]) {
1984                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1985                 if (!wpas_p2p_create_iface(wpa_s))
1986                         return NULL;
1987                 /*
1988                  * Something has forced us to remove the pending interface; try
1989                  * to create a new one and hope for the best that we will get
1990                  * the same local address.
1991                  */
1992                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1993                                                  WPA_IF_P2P_CLIENT) < 0)
1994                         return NULL;
1995         }
1996
1997         os_memset(&iface, 0, sizeof(iface));
1998         iface.ifname = wpa_s->pending_interface_name;
1999         iface.driver = wpa_s->driver->name;
2000         if (wpa_s->conf->ctrl_interface == NULL &&
2001             wpa_s->parent != wpa_s &&
2002             wpa_s->p2p_mgmt &&
2003             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2004                 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2005         else
2006                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2007         iface.driver_param = wpa_s->conf->driver_param;
2008         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2009         if (group_wpa_s == NULL) {
2010                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2011                            "wpa_supplicant interface");
2012                 return NULL;
2013         }
2014         wpa_s->pending_interface_name[0] = '\0';
2015         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2016                 P2P_GROUP_INTERFACE_CLIENT;
2017         wpa_s->global->p2p_group_formation = group_wpa_s;
2018         wpa_s->global->pending_group_iface_for_p2ps = 0;
2019
2020         wpas_p2p_clone_config(group_wpa_s, wpa_s);
2021
2022         return group_wpa_s;
2023 }
2024
2025
2026 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2027                                              void *timeout_ctx)
2028 {
2029         struct wpa_supplicant *wpa_s = eloop_ctx;
2030         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2031         wpas_p2p_group_formation_failed(wpa_s, 0);
2032 }
2033
2034
2035 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2036                                             int already_deleted)
2037 {
2038         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2039                              wpa_s->parent, NULL);
2040         if (wpa_s->global->p2p)
2041                 p2p_group_formation_failed(wpa_s->global->p2p);
2042         wpas_group_formation_completed(wpa_s, 0, already_deleted);
2043 }
2044
2045
2046 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2047 {
2048         wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2049         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2050                              wpa_s->parent, NULL);
2051         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2052                                wpa_s->parent, NULL);
2053         wpa_s->global->p2p_fail_on_wps_complete = 0;
2054 }
2055
2056
2057 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2058 {
2059         if (wpa_s->global->p2p_group_formation != wpa_s)
2060                 return;
2061         /* Speed up group formation timeout since this cannot succeed */
2062         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2063                              wpa_s->parent, NULL);
2064         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2065                                wpa_s->parent, NULL);
2066 }
2067
2068
2069 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2070 {
2071         struct wpa_supplicant *wpa_s = ctx;
2072
2073         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2074                 wpa_drv_cancel_remain_on_channel(wpa_s);
2075                 wpa_s->off_channel_freq = 0;
2076                 wpa_s->roc_waiting_drv_freq = 0;
2077         }
2078
2079         if (res->status) {
2080                 wpa_msg_global(wpa_s, MSG_INFO,
2081                                P2P_EVENT_GO_NEG_FAILURE "status=%d",
2082                                res->status);
2083                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2084                 wpas_p2p_remove_pending_group_interface(wpa_s);
2085                 return;
2086         }
2087
2088         if (!res->role_go) {
2089                 /* Inform driver of the operating channel of GO. */
2090                 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2091         }
2092
2093         if (wpa_s->p2p_go_ht40)
2094                 res->ht40 = 1;
2095         if (wpa_s->p2p_go_vht)
2096                 res->vht = 1;
2097
2098         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2099                        "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2100                        " wps_method=%s",
2101                        res->role_go ? "GO" : "client", res->freq, res->ht40,
2102                        MAC2STR(res->peer_device_addr),
2103                        MAC2STR(res->peer_interface_addr),
2104                        p2p_wps_method_text(res->wps_method));
2105         wpas_notify_p2p_go_neg_completed(wpa_s, res);
2106
2107         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2108                 struct wpa_ssid *ssid;
2109                 ssid = wpa_config_get_network(wpa_s->conf,
2110                                               wpa_s->p2p_persistent_id);
2111                 if (ssid && ssid->disabled == 2 &&
2112                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2113                         size_t len = os_strlen(ssid->passphrase);
2114                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2115                                    "on requested persistent group");
2116                         os_memcpy(res->passphrase, ssid->passphrase, len);
2117                         res->passphrase[len] = '\0';
2118                 }
2119         }
2120
2121         if (wpa_s->create_p2p_iface) {
2122                 struct wpa_supplicant *group_wpa_s =
2123                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
2124                 if (group_wpa_s == NULL) {
2125                         wpas_p2p_remove_pending_group_interface(wpa_s);
2126                         eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2127                                              wpa_s, NULL);
2128                         wpas_p2p_group_formation_failed(wpa_s, 1);
2129                         return;
2130                 }
2131                 if (group_wpa_s != wpa_s) {
2132                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2133                                   sizeof(group_wpa_s->p2p_pin));
2134                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2135                 }
2136                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2137                 wpa_s->pending_interface_name[0] = '\0';
2138                 group_wpa_s->p2p_in_provisioning = 1;
2139
2140                 if (res->role_go)
2141                         wpas_start_wps_go(group_wpa_s, res, 1);
2142                 else
2143                         wpas_start_wps_enrollee(group_wpa_s, res);
2144         } else {
2145                 wpa_s->p2p_in_provisioning = 1;
2146                 wpa_s->global->p2p_group_formation = wpa_s;
2147
2148                 if (res->role_go)
2149                         wpas_start_wps_go(wpa_s, res, 1);
2150                 else
2151                         wpas_start_wps_enrollee(ctx, res);
2152         }
2153
2154         wpa_s->p2p_long_listen = 0;
2155         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2156
2157         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2158         eloop_register_timeout(15 + res->peer_config_timeout / 100,
2159                                (res->peer_config_timeout % 100) * 10000,
2160                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
2161 }
2162
2163
2164 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2165                                u8 go_intent)
2166 {
2167         struct wpa_supplicant *wpa_s = ctx;
2168         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2169                        " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2170                        dev_passwd_id, go_intent);
2171
2172         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2173 }
2174
2175
2176 static void wpas_dev_found(void *ctx, const u8 *addr,
2177                            const struct p2p_peer_info *info,
2178                            int new_device)
2179 {
2180 #ifndef CONFIG_NO_STDOUT_DEBUG
2181         struct wpa_supplicant *wpa_s = ctx;
2182         char devtype[WPS_DEV_TYPE_BUFSIZE];
2183         char *wfd_dev_info_hex = NULL;
2184
2185 #ifdef CONFIG_WIFI_DISPLAY
2186         wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2187                                                     WFD_SUBELEM_DEVICE_INFO);
2188 #endif /* CONFIG_WIFI_DISPLAY */
2189
2190         if (info->p2ps_instance) {
2191                 char str[256];
2192                 const u8 *buf = wpabuf_head(info->p2ps_instance);
2193                 size_t len = wpabuf_len(info->p2ps_instance);
2194
2195                 while (len) {
2196                         u32 id;
2197                         u16 methods;
2198                         u8 str_len;
2199
2200                         if (len < 4 + 2 + 1)
2201                                 break;
2202                         id = WPA_GET_LE32(buf);
2203                         buf += sizeof(u32);
2204                         methods = WPA_GET_BE16(buf);
2205                         buf += sizeof(u16);
2206                         str_len = *buf++;
2207                         if (str_len > len - 4 - 2 - 1)
2208                                 break;
2209                         os_memcpy(str, buf, str_len);
2210                         str[str_len] = '\0';
2211                         buf += str_len;
2212                         len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2213
2214                         wpa_msg_global(wpa_s, MSG_INFO,
2215                                        P2P_EVENT_DEVICE_FOUND MACSTR
2216                                        " p2p_dev_addr=" MACSTR
2217                                        " pri_dev_type=%s name='%s'"
2218                                        " config_methods=0x%x"
2219                                        " dev_capab=0x%x"
2220                                        " group_capab=0x%x"
2221                                        " adv_id=%x asp_svc=%s%s",
2222                                        MAC2STR(addr),
2223                                        MAC2STR(info->p2p_device_addr),
2224                                        wps_dev_type_bin2str(
2225                                                info->pri_dev_type,
2226                                                devtype, sizeof(devtype)),
2227                                        info->device_name, methods,
2228                                        info->dev_capab, info->group_capab,
2229                                        id, str,
2230                                        info->vendor_elems ?
2231                                        " vendor_elems=1" : "");
2232                 }
2233                 goto done;
2234         }
2235
2236         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2237                        " p2p_dev_addr=" MACSTR
2238                        " pri_dev_type=%s name='%s' config_methods=0x%x "
2239                        "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2240                        MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2241                        wps_dev_type_bin2str(info->pri_dev_type, devtype,
2242                                             sizeof(devtype)),
2243                        info->device_name, info->config_methods,
2244                        info->dev_capab, info->group_capab,
2245                        wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2246                        wfd_dev_info_hex ? wfd_dev_info_hex : "",
2247                        info->vendor_elems ? " vendor_elems=1" : "",
2248                        new_device);
2249
2250 done:
2251         os_free(wfd_dev_info_hex);
2252 #endif /* CONFIG_NO_STDOUT_DEBUG */
2253
2254         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2255 }
2256
2257
2258 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2259 {
2260         struct wpa_supplicant *wpa_s = ctx;
2261
2262         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2263                        "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2264
2265         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2266 }
2267
2268
2269 static void wpas_find_stopped(void *ctx)
2270 {
2271         struct wpa_supplicant *wpa_s = ctx;
2272         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2273         wpas_notify_p2p_find_stopped(wpa_s);
2274 }
2275
2276
2277 struct wpas_p2p_listen_work {
2278         unsigned int freq;
2279         unsigned int duration;
2280         struct wpabuf *probe_resp_ie;
2281 };
2282
2283
2284 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2285 {
2286         if (lwork == NULL)
2287                 return;
2288         wpabuf_free(lwork->probe_resp_ie);
2289         os_free(lwork);
2290 }
2291
2292
2293 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2294 {
2295         struct wpas_p2p_listen_work *lwork;
2296
2297         if (!wpa_s->p2p_listen_work)
2298                 return;
2299
2300         lwork = wpa_s->p2p_listen_work->ctx;
2301         wpas_p2p_listen_work_free(lwork);
2302         radio_work_done(wpa_s->p2p_listen_work);
2303         wpa_s->p2p_listen_work = NULL;
2304 }
2305
2306
2307 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2308 {
2309         struct wpa_supplicant *wpa_s = work->wpa_s;
2310         struct wpas_p2p_listen_work *lwork = work->ctx;
2311         unsigned int duration;
2312
2313         if (deinit) {
2314                 if (work->started) {
2315                         wpa_s->p2p_listen_work = NULL;
2316                         wpas_stop_listen(wpa_s);
2317                 }
2318                 wpas_p2p_listen_work_free(lwork);
2319                 return;
2320         }
2321
2322         wpa_s->p2p_listen_work = work;
2323
2324         wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2325
2326         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2327                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2328                            "report received Probe Request frames");
2329                 wpas_p2p_listen_work_done(wpa_s);
2330                 return;
2331         }
2332
2333         wpa_s->pending_listen_freq = lwork->freq;
2334         wpa_s->pending_listen_duration = lwork->duration;
2335
2336         duration = lwork->duration;
2337 #ifdef CONFIG_TESTING_OPTIONS
2338         if (wpa_s->extra_roc_dur) {
2339                 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2340                            duration, duration + wpa_s->extra_roc_dur);
2341                 duration += wpa_s->extra_roc_dur;
2342         }
2343 #endif /* CONFIG_TESTING_OPTIONS */
2344
2345         if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2346                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2347                            "to remain on channel (%u MHz) for Listen "
2348                            "state", lwork->freq);
2349                 wpas_p2p_listen_work_done(wpa_s);
2350                 wpa_s->pending_listen_freq = 0;
2351                 return;
2352         }
2353         wpa_s->off_channel_freq = 0;
2354         wpa_s->roc_waiting_drv_freq = lwork->freq;
2355 }
2356
2357
2358 static int wpas_start_listen(void *ctx, unsigned int freq,
2359                              unsigned int duration,
2360                              const struct wpabuf *probe_resp_ie)
2361 {
2362         struct wpa_supplicant *wpa_s = ctx;
2363         struct wpas_p2p_listen_work *lwork;
2364
2365         if (wpa_s->p2p_listen_work) {
2366                 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2367                 return -1;
2368         }
2369
2370         lwork = os_zalloc(sizeof(*lwork));
2371         if (lwork == NULL)
2372                 return -1;
2373         lwork->freq = freq;
2374         lwork->duration = duration;
2375         if (probe_resp_ie) {
2376                 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2377                 if (lwork->probe_resp_ie == NULL) {
2378                         wpas_p2p_listen_work_free(lwork);
2379                         return -1;
2380                 }
2381         }
2382
2383         if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2384                            lwork) < 0) {
2385                 wpas_p2p_listen_work_free(lwork);
2386                 return -1;
2387         }
2388
2389         return 0;
2390 }
2391
2392
2393 static void wpas_stop_listen(void *ctx)
2394 {
2395         struct wpa_supplicant *wpa_s = ctx;
2396         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2397                 wpa_drv_cancel_remain_on_channel(wpa_s);
2398                 wpa_s->off_channel_freq = 0;
2399                 wpa_s->roc_waiting_drv_freq = 0;
2400         }
2401         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2402
2403         /*
2404          * Don't cancel Probe Request RX reporting for a connected P2P Client
2405          * handling Probe Request frames.
2406          */
2407         if (!wpa_s->p2p_cli_probe)
2408                 wpa_drv_probe_req_report(wpa_s, 0);
2409
2410         wpas_p2p_listen_work_done(wpa_s);
2411 }
2412
2413
2414 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2415                                 unsigned int freq)
2416 {
2417         struct wpa_supplicant *wpa_s = ctx;
2418         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2419                                  freq);
2420 }
2421
2422
2423 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2424                                          const u8 *peer, const char *params,
2425                                          unsigned int generated_pin)
2426 {
2427         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2428                        " %08d%s", MAC2STR(peer), generated_pin, params);
2429 }
2430
2431
2432 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2433                                         const u8 *peer, const char *params)
2434 {
2435         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2436                        "%s", MAC2STR(peer), params);
2437 }
2438
2439
2440 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2441                                const u8 *dev_addr, const u8 *pri_dev_type,
2442                                const char *dev_name, u16 supp_config_methods,
2443                                u8 dev_capab, u8 group_capab, const u8 *group_id,
2444                                size_t group_id_len)
2445 {
2446         struct wpa_supplicant *wpa_s = ctx;
2447         char devtype[WPS_DEV_TYPE_BUFSIZE];
2448         char params[300];
2449         u8 empty_dev_type[8];
2450         unsigned int generated_pin = 0;
2451         struct wpa_supplicant *group = NULL;
2452         int res;
2453
2454         if (group_id) {
2455                 for (group = wpa_s->global->ifaces; group; group = group->next)
2456                 {
2457                         struct wpa_ssid *s = group->current_ssid;
2458                         if (s != NULL &&
2459                             s->mode == WPAS_MODE_P2P_GO &&
2460                             group_id_len - ETH_ALEN == s->ssid_len &&
2461                             os_memcmp(group_id + ETH_ALEN, s->ssid,
2462                                       s->ssid_len) == 0)
2463                                 break;
2464                 }
2465         }
2466
2467         if (pri_dev_type == NULL) {
2468                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2469                 pri_dev_type = empty_dev_type;
2470         }
2471         res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2472                           " pri_dev_type=%s name='%s' config_methods=0x%x "
2473                           "dev_capab=0x%x group_capab=0x%x%s%s",
2474                           MAC2STR(dev_addr),
2475                           wps_dev_type_bin2str(pri_dev_type, devtype,
2476                                                sizeof(devtype)),
2477                           dev_name, supp_config_methods, dev_capab, group_capab,
2478                           group ? " group=" : "",
2479                           group ? group->ifname : "");
2480         if (os_snprintf_error(sizeof(params), res))
2481                 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2482         params[sizeof(params) - 1] = '\0';
2483
2484         if (config_methods & WPS_CONFIG_DISPLAY) {
2485                 generated_pin = wps_generate_pin();
2486                 wpas_prov_disc_local_display(wpa_s, peer, params,
2487                                              generated_pin);
2488         } else if (config_methods & WPS_CONFIG_KEYPAD)
2489                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2490         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2491                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2492                                MACSTR "%s", MAC2STR(peer), params);
2493
2494         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2495                                             P2P_PROV_DISC_SUCCESS,
2496                                             config_methods, generated_pin);
2497 }
2498
2499
2500 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2501 {
2502         struct wpa_supplicant *wpa_s = ctx;
2503         unsigned int generated_pin = 0;
2504         char params[20];
2505
2506         if (wpa_s->pending_pd_before_join &&
2507             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2508              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2509                 wpa_s->pending_pd_before_join = 0;
2510                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2511                            "join-existing-group operation");
2512                 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2513                 return;
2514         }
2515
2516         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2517             wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2518                 int res;
2519
2520                 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2521                                   wpa_s->pending_pd_use == AUTO_PD_JOIN);
2522                 if (os_snprintf_error(sizeof(params), res))
2523                         params[sizeof(params) - 1] = '\0';
2524         } else
2525                 params[0] = '\0';
2526
2527         if (config_methods & WPS_CONFIG_DISPLAY)
2528                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2529         else if (config_methods & WPS_CONFIG_KEYPAD) {
2530                 generated_pin = wps_generate_pin();
2531                 wpas_prov_disc_local_display(wpa_s, peer, params,
2532                                              generated_pin);
2533         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2534                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2535                                MACSTR "%s", MAC2STR(peer), params);
2536
2537         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2538                                             P2P_PROV_DISC_SUCCESS,
2539                                             config_methods, generated_pin);
2540 }
2541
2542
2543 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2544                                 enum p2p_prov_disc_status status,
2545                                 u32 adv_id, const u8 *adv_mac,
2546                                 const char *deferred_session_resp)
2547 {
2548         struct wpa_supplicant *wpa_s = ctx;
2549
2550         if (wpa_s->p2p_fallback_to_go_neg) {
2551                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2552                         "failed - fall back to GO Negotiation");
2553                 wpa_msg_global(wpa_s->parent, MSG_INFO,
2554                                P2P_EVENT_FALLBACK_TO_GO_NEG
2555                                "reason=PD-failed");
2556                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2557                 return;
2558         }
2559
2560         if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2561                 wpa_s->pending_pd_before_join = 0;
2562                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2563                            "join-existing-group operation (no ACK for PD "
2564                            "Req attempts)");
2565                 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2566                 return;
2567         }
2568
2569         if (adv_id && adv_mac && deferred_session_resp) {
2570                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2571                                " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2572                                " deferred_session_resp='%s'",
2573                                MAC2STR(peer), status, adv_id,
2574                                deferred_session_resp);
2575         } else if (adv_id && adv_mac) {
2576                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2577                                " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2578                                MAC2STR(peer), status, adv_id);
2579         } else {
2580                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2581                                " p2p_dev_addr=" MACSTR " status=%d",
2582                                MAC2STR(peer), status);
2583         }
2584
2585         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2586                                             status, 0, 0);
2587 }
2588
2589
2590 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2591 {
2592         if (channels == NULL)
2593                 return 1; /* Assume no restrictions */
2594         return p2p_channels_includes_freq(channels, freq);
2595
2596 }
2597
2598
2599 /**
2600  * Pick the best frequency to use from all the currently used frequencies.
2601  */
2602 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2603                                         struct wpa_used_freq_data *freqs,
2604                                         unsigned int num)
2605 {
2606         unsigned int i, c;
2607
2608         /* find a candidate freq that is supported by P2P */
2609         for (c = 0; c < num; c++)
2610                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2611                         break;
2612
2613         if (c == num)
2614                 return 0;
2615
2616         /* once we have a candidate, try to find a 'better' one */
2617         for (i = c + 1; i < num; i++) {
2618                 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2619                         continue;
2620
2621                 /*
2622                  * 1. Infrastructure station interfaces have higher preference.
2623                  * 2. P2P Clients have higher preference.
2624                  * 3. All others.
2625                  */
2626                 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2627                         c = i;
2628                         break;
2629                 }
2630
2631                 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2632                         c = i;
2633         }
2634         return freqs[c].freq;
2635 }
2636
2637
2638 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2639                                   const u8 *go_dev_addr, const u8 *ssid,
2640                                   size_t ssid_len, int *go, u8 *group_bssid,
2641                                   int *force_freq, int persistent_group,
2642                                   const struct p2p_channels *channels,
2643                                   int dev_pw_id)
2644 {
2645         struct wpa_supplicant *wpa_s = ctx;
2646         struct wpa_ssid *s;
2647         struct wpa_used_freq_data *freqs;
2648         struct wpa_supplicant *grp;
2649         int best_freq;
2650
2651         if (!persistent_group) {
2652                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2653                            " to join an active group (SSID: %s)",
2654                            MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2655                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2656                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2657                      == 0 ||
2658                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2659                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2660                                    "authorized invitation");
2661                         goto accept_inv;
2662                 }
2663
2664 #ifdef CONFIG_WPS_NFC
2665                 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2666                     dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2667                         wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2668                         wpa_s->p2p_wps_method = WPS_NFC;
2669                         wpa_s->pending_join_wps_method = WPS_NFC;
2670                         os_memcpy(wpa_s->pending_join_dev_addr,
2671                                   go_dev_addr, ETH_ALEN);
2672                         os_memcpy(wpa_s->pending_join_iface_addr,
2673                                   bssid, ETH_ALEN);
2674                         goto accept_inv;
2675                 }
2676 #endif /* CONFIG_WPS_NFC */
2677
2678                 /*
2679                  * Do not accept the invitation automatically; notify user and
2680                  * request approval.
2681                  */
2682                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2683         }
2684
2685         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2686         if (grp) {
2687                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2688                            "running persistent group");
2689                 if (*go)
2690                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2691                 goto accept_inv;
2692         }
2693
2694         if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2695             os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2696                 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2697                            "invitation to re-invoke a persistent group");
2698                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2699         } else if (!wpa_s->conf->persistent_reconnect)
2700                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2701
2702         for (s = wpa_s->conf->ssid; s; s = s->next) {
2703                 if (s->disabled == 2 &&
2704                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2705                     s->ssid_len == ssid_len &&
2706                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2707                         break;
2708         }
2709
2710         if (!s) {
2711                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2712                            " requested reinvocation of an unknown group",
2713                            MAC2STR(sa));
2714                 return P2P_SC_FAIL_UNKNOWN_GROUP;
2715         }
2716
2717         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2718                 *go = 1;
2719                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2720                         wpa_printf(MSG_DEBUG, "P2P: The only available "
2721                                    "interface is already in use - reject "
2722                                    "invitation");
2723                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2724                 }
2725                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2726         } else if (s->mode == WPAS_MODE_P2P_GO) {
2727                 *go = 1;
2728                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2729                 {
2730                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2731                                    "interface address for the group");
2732                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2733                 }
2734                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2735                           ETH_ALEN);
2736         }
2737
2738 accept_inv:
2739         wpas_p2p_set_own_freq_preference(wpa_s, 0);
2740
2741         best_freq = 0;
2742         freqs = os_calloc(wpa_s->num_multichan_concurrent,
2743                           sizeof(struct wpa_used_freq_data));
2744         if (freqs) {
2745                 int num_channels = wpa_s->num_multichan_concurrent;
2746                 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2747                 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2748                 os_free(freqs);
2749         }
2750
2751         /* Get one of the frequencies currently in use */
2752         if (best_freq > 0) {
2753                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2754                 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2755
2756                 if (wpa_s->num_multichan_concurrent < 2 ||
2757                     wpas_p2p_num_unused_channels(wpa_s) < 1) {
2758                         wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2759                         *force_freq = best_freq;
2760                 }
2761         }
2762
2763         if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2764             wpas_p2p_num_unused_channels(wpa_s) > 0) {
2765                 if (*go == 0) {
2766                         /* We are the client */
2767                         wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2768                                    "running a GO but we are capable of MCC, "
2769                                    "figure out the best channel to use");
2770                         *force_freq = 0;
2771                 } else if (!freq_included(channels, *force_freq)) {
2772                         /* We are the GO, and *force_freq is not in the
2773                          * intersection */
2774                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2775                                    "in intersection but we are capable of MCC, "
2776                                    "figure out the best channel to use",
2777                                    *force_freq);
2778                         *force_freq = 0;
2779                 }
2780         }
2781
2782         return P2P_SC_SUCCESS;
2783 }
2784
2785
2786 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2787                                      const u8 *ssid, size_t ssid_len,
2788                                      const u8 *go_dev_addr, u8 status,
2789                                      int op_freq)
2790 {
2791         struct wpa_supplicant *wpa_s = ctx;
2792         struct wpa_ssid *s;
2793
2794         for (s = wpa_s->conf->ssid; s; s = s->next) {
2795                 if (s->disabled == 2 &&
2796                     s->ssid_len == ssid_len &&
2797                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2798                         break;
2799         }
2800
2801         if (status == P2P_SC_SUCCESS) {
2802                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2803                            " was accepted; op_freq=%d MHz, SSID=%s",
2804                            MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2805                 if (s) {
2806                         int go = s->mode == WPAS_MODE_P2P_GO;
2807                         wpas_p2p_group_add_persistent(
2808                                 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2809                                 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2810                 } else if (bssid) {
2811                         wpa_s->user_initiated_pd = 0;
2812                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2813                                       wpa_s->p2p_wps_method, 0, op_freq,
2814                                       ssid, ssid_len);
2815                 }
2816                 return;
2817         }
2818
2819         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2820                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2821                            " was rejected (status %u)", MAC2STR(sa), status);
2822                 return;
2823         }
2824
2825         if (!s) {
2826                 if (bssid) {
2827                         wpa_msg_global(wpa_s, MSG_INFO,
2828                                        P2P_EVENT_INVITATION_RECEIVED
2829                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2830                                        " bssid=" MACSTR " unknown-network",
2831                                        MAC2STR(sa), MAC2STR(go_dev_addr),
2832                                        MAC2STR(bssid));
2833                 } else {
2834                         wpa_msg_global(wpa_s, MSG_INFO,
2835                                        P2P_EVENT_INVITATION_RECEIVED
2836                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2837                                        " unknown-network",
2838                                        MAC2STR(sa), MAC2STR(go_dev_addr));
2839                 }
2840                 return;
2841         }
2842
2843         if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2844                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2845                                "sa=" MACSTR " persistent=%d freq=%d",
2846                                MAC2STR(sa), s->id, op_freq);
2847         } else {
2848                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2849                                "sa=" MACSTR " persistent=%d",
2850                                MAC2STR(sa), s->id);
2851         }
2852 }
2853
2854
2855 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2856                                         struct wpa_ssid *ssid,
2857                                         const u8 *peer, int inv)
2858 {
2859         size_t i;
2860         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2861
2862         if (ssid == NULL)
2863                 return;
2864
2865         for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2866                 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2867                               ETH_ALEN) == 0)
2868                         break;
2869         }
2870         if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2871                 if (ssid->mode != WPAS_MODE_P2P_GO &&
2872                     os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2873                         wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2874                                    "due to invitation result", ssid->id);
2875                         wpas_notify_network_removed(wpa_s, ssid);
2876                         wpa_config_remove_network(wpa_s->conf, ssid->id);
2877                         return;
2878                 }
2879                 return; /* Peer not found in client list */
2880         }
2881
2882         wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2883                    "group %d client list%s",
2884                    MAC2STR(peer), ssid->id,
2885                    inv ? " due to invitation result" : "");
2886         os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2887                    ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2888                    (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
2889         ssid->num_p2p_clients--;
2890         if (p2p_wpa_s->conf->update_config &&
2891             wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
2892                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2893 }
2894
2895
2896 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2897                                           const u8 *peer)
2898 {
2899         struct wpa_ssid *ssid;
2900
2901         wpa_s = wpa_s->global->p2p_invite_group;
2902         if (wpa_s == NULL)
2903                 return; /* No known invitation group */
2904         ssid = wpa_s->current_ssid;
2905         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2906             !ssid->p2p_persistent_group)
2907                 return; /* Not operating as a GO in persistent group */
2908         ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2909                                        ssid->ssid, ssid->ssid_len);
2910         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2911 }
2912
2913
2914 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2915                                    const struct p2p_channels *channels,
2916                                    const u8 *peer, int neg_freq,
2917                                    int peer_oper_freq)
2918 {
2919         struct wpa_supplicant *wpa_s = ctx;
2920         struct wpa_ssid *ssid;
2921         int freq;
2922
2923         if (bssid) {
2924                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2925                                "status=%d " MACSTR,
2926                                status, MAC2STR(bssid));
2927         } else {
2928                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2929                                "status=%d ", status);
2930         }
2931         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2932
2933         wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2934                    status, MAC2STR(peer));
2935         if (wpa_s->pending_invite_ssid_id == -1) {
2936                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2937                         wpas_remove_persistent_client(wpa_s, peer);
2938                 return; /* Invitation to active group */
2939         }
2940
2941         if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2942                 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2943                            "invitation exchange to indicate readiness for "
2944                            "re-invocation");
2945         }
2946
2947         if (status != P2P_SC_SUCCESS) {
2948                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2949                         ssid = wpa_config_get_network(
2950                                 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2951                         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2952                 }
2953                 wpas_p2p_remove_pending_group_interface(wpa_s);
2954                 return;
2955         }
2956
2957         ssid = wpa_config_get_network(wpa_s->conf,
2958                                       wpa_s->pending_invite_ssid_id);
2959         if (ssid == NULL) {
2960                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2961                            "data matching with invitation");
2962                 return;
2963         }
2964
2965         /*
2966          * The peer could have missed our ctrl::ack frame for Invitation
2967          * Response and continue retransmitting the frame. To reduce the
2968          * likelihood of the peer not getting successful TX status for the
2969          * Invitation Response frame, wait a short time here before starting
2970          * the persistent group so that we will remain on the current channel to
2971          * acknowledge any possible retransmission from the peer.
2972          */
2973         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2974                 "starting persistent group");
2975         os_sleep(0, 50000);
2976
2977         if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2978             freq_included(channels, neg_freq))
2979                 freq = neg_freq;
2980         else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2981                  freq_included(channels, peer_oper_freq))
2982                 freq = peer_oper_freq;
2983         else
2984                 freq = 0;
2985
2986         wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2987                    freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2988         wpas_p2p_group_add_persistent(wpa_s, ssid,
2989                                       ssid->mode == WPAS_MODE_P2P_GO,
2990                                       wpa_s->p2p_persistent_go_freq,
2991                                       freq,
2992                                       wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2993                                       channels,
2994                                       ssid->mode == WPAS_MODE_P2P_GO ?
2995                                       P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2996                                       0);
2997 }
2998
2999
3000 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3001                                     unsigned int freq)
3002 {
3003         if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3004                 return 1;
3005         return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3006 }
3007
3008
3009 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3010 {
3011         reg->channel[reg->channels] = chan;
3012         reg->channels++;
3013 }
3014
3015
3016 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3017                                      struct p2p_channels *chan,
3018                                      struct p2p_channels *cli_chan)
3019 {
3020         int i, cla = 0;
3021
3022         wpa_s->global->p2p_24ghz_social_channels = 1;
3023
3024         os_memset(cli_chan, 0, sizeof(*cli_chan));
3025
3026         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3027                    "band");
3028
3029         /* Operating class 81 - 2.4 GHz band channels 1..13 */
3030         chan->reg_class[cla].reg_class = 81;
3031         chan->reg_class[cla].channels = 0;
3032         for (i = 0; i < 11; i++) {
3033                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3034                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3035         }
3036         if (chan->reg_class[cla].channels)
3037                 cla++;
3038
3039         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3040                    "band");
3041
3042         /* Operating class 115 - 5 GHz, channels 36-48 */
3043         chan->reg_class[cla].reg_class = 115;
3044         chan->reg_class[cla].channels = 0;
3045         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3046                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3047         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3048                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3049         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3050                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3051         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3052                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3053         if (chan->reg_class[cla].channels)
3054                 cla++;
3055
3056         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3057                    "band");
3058
3059         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3060         chan->reg_class[cla].reg_class = 124;
3061         chan->reg_class[cla].channels = 0;
3062         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3063                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3064         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3065                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3066         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3067                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3068         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3069                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3070         if (chan->reg_class[cla].channels)
3071                 cla++;
3072
3073         chan->reg_classes = cla;
3074         return 0;
3075 }
3076
3077
3078 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3079                                           u16 num_modes,
3080                                           enum hostapd_hw_mode mode)
3081 {
3082         u16 i;
3083
3084         for (i = 0; i < num_modes; i++) {
3085                 if (modes[i].mode == mode)
3086                         return &modes[i];
3087         }
3088
3089         return NULL;
3090 }
3091
3092
3093 enum chan_allowed {
3094         NOT_ALLOWED, NO_IR, ALLOWED
3095 };
3096
3097 static int has_channel(struct wpa_global *global,
3098                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
3099 {
3100         int i;
3101         unsigned int freq;
3102
3103         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3104                 chan * 5;
3105         if (wpas_p2p_disallowed_freq(global, freq))
3106                 return NOT_ALLOWED;
3107
3108         for (i = 0; i < mode->num_channels; i++) {
3109                 if (mode->channels[i].chan == chan) {
3110                         if (flags)
3111                                 *flags = mode->channels[i].flag;
3112                         if (mode->channels[i].flag &
3113                             (HOSTAPD_CHAN_DISABLED |
3114                              HOSTAPD_CHAN_RADAR))
3115                                 return NOT_ALLOWED;
3116                         if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3117                                 return NO_IR;
3118                         return ALLOWED;
3119                 }
3120         }
3121
3122         return NOT_ALLOWED;
3123 }
3124
3125
3126 struct p2p_oper_class_map {
3127         enum hostapd_hw_mode mode;
3128         u8 op_class;
3129         u8 min_chan;
3130         u8 max_chan;
3131         u8 inc;
3132         enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3133 };
3134
3135 static const struct p2p_oper_class_map op_class[] = {
3136         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3137 #if 0 /* Do not enable HT40 on 2 GHz for now */
3138         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3139         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3140 #endif
3141         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3142         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3143         { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3144         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3145         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3146         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3147         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3148
3149         /*
3150          * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3151          * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3152          * 80 MHz, but currently use the following definition for simplicity
3153          * (these center frequencies are not actual channels, which makes
3154          * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3155          * removing invalid channels.
3156          */
3157         { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3158         { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3159         { -1, 0, 0, 0, 0, BW20 }
3160 };
3161
3162
3163 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3164                                      struct hostapd_hw_modes *mode,
3165                                      u8 channel)
3166 {
3167         u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3168         unsigned int i;
3169
3170         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3171                 return 0;
3172
3173         for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3174                 /*
3175                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3176                  * so the center channel is 6 channels away from the start/end.
3177                  */
3178                 if (channel >= center_channels[i] - 6 &&
3179                     channel <= center_channels[i] + 6)
3180                         return center_channels[i];
3181
3182         return 0;
3183 }
3184
3185
3186 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3187                                                struct hostapd_hw_modes *mode,
3188                                                u8 channel, u8 bw)
3189 {
3190         u8 center_chan;
3191         int i, flags;
3192         enum chan_allowed res, ret = ALLOWED;
3193
3194         center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3195         if (!center_chan)
3196                 return NOT_ALLOWED;
3197         if (center_chan >= 58 && center_chan <= 138)
3198                 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3199
3200         /* check all the channels are available */
3201         for (i = 0; i < 4; i++) {
3202                 int adj_chan = center_chan - 6 + i * 4;
3203
3204                 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3205                 if (res == NOT_ALLOWED)
3206                         return NOT_ALLOWED;
3207                 if (res == NO_IR)
3208                         ret = NO_IR;
3209
3210                 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3211                         return NOT_ALLOWED;
3212                 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3213                         return NOT_ALLOWED;
3214                 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3215                         return NOT_ALLOWED;
3216                 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3217                         return NOT_ALLOWED;
3218         }
3219
3220         return ret;
3221 }
3222
3223
3224 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3225                                                  struct hostapd_hw_modes *mode,
3226                                                  u8 channel, u8 bw)
3227 {
3228         int flag = 0;
3229         enum chan_allowed res, res2;
3230
3231         res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3232         if (bw == BW40MINUS) {
3233                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3234                         return NOT_ALLOWED;
3235                 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3236         } else if (bw == BW40PLUS) {
3237                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3238                         return NOT_ALLOWED;
3239                 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3240         } else if (bw == BW80) {
3241                 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3242         }
3243
3244         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3245                 return NOT_ALLOWED;
3246         if (res == NO_IR || res2 == NO_IR)
3247                 return NO_IR;
3248         return res;
3249 }
3250
3251
3252 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3253                                    struct p2p_channels *chan,
3254                                    struct p2p_channels *cli_chan)
3255 {
3256         struct hostapd_hw_modes *mode;
3257         int cla, op, cli_cla;
3258
3259         if (wpa_s->hw.modes == NULL) {
3260                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3261                            "of all supported channels; assume dualband "
3262                            "support");
3263                 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3264         }
3265
3266         cla = cli_cla = 0;
3267
3268         for (op = 0; op_class[op].op_class; op++) {
3269                 const struct p2p_oper_class_map *o = &op_class[op];
3270                 u8 ch;
3271                 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3272
3273                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3274                 if (mode == NULL)
3275                         continue;
3276                 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3277                         wpa_s->global->p2p_24ghz_social_channels = 1;
3278                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3279                         enum chan_allowed res;
3280                         res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3281                         if (res == ALLOWED) {
3282                                 if (reg == NULL) {
3283                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3284                                                    o->op_class);
3285                                         reg = &chan->reg_class[cla];
3286                                         cla++;
3287                                         reg->reg_class = o->op_class;
3288                                 }
3289                                 reg->channel[reg->channels] = ch;
3290                                 reg->channels++;
3291                         } else if (res == NO_IR &&
3292                                    wpa_s->conf->p2p_add_cli_chan) {
3293                                 if (cli_reg == NULL) {
3294                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3295                                                    o->op_class);
3296                                         cli_reg = &cli_chan->reg_class[cli_cla];
3297                                         cli_cla++;
3298                                         cli_reg->reg_class = o->op_class;
3299                                 }
3300                                 cli_reg->channel[cli_reg->channels] = ch;
3301                                 cli_reg->channels++;
3302                         }
3303                 }
3304                 if (reg) {
3305                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3306                                     reg->channel, reg->channels);
3307                 }
3308                 if (cli_reg) {
3309                         wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3310                                     cli_reg->channel, cli_reg->channels);
3311                 }
3312         }
3313
3314         chan->reg_classes = cla;
3315         cli_chan->reg_classes = cli_cla;
3316
3317         return 0;
3318 }
3319
3320
3321 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3322                            struct hostapd_hw_modes *mode, u8 channel)
3323 {
3324         int op;
3325         enum chan_allowed ret;
3326
3327         for (op = 0; op_class[op].op_class; op++) {
3328                 const struct p2p_oper_class_map *o = &op_class[op];
3329                 u8 ch;
3330
3331                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3332                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3333                             (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3334                             ch != channel)
3335                                 continue;
3336                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3337                         if (ret == ALLOWED)
3338                                 return (o->bw == BW40MINUS) ? -1 : 1;
3339                 }
3340         }
3341         return 0;
3342 }
3343
3344
3345 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3346                               struct hostapd_hw_modes *mode, u8 channel)
3347 {
3348         if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3349                 return 0;
3350
3351         return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3352 }
3353
3354
3355 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3356                         size_t buf_len)
3357 {
3358         struct wpa_supplicant *wpa_s = ctx;
3359
3360         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3361                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3362                         break;
3363         }
3364         if (wpa_s == NULL)
3365                 return -1;
3366
3367         return wpa_drv_get_noa(wpa_s, buf, buf_len);
3368 }
3369
3370
3371 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3372                                               const u8 *ssid, size_t ssid_len)
3373 {
3374         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3375                 struct wpa_ssid *s = wpa_s->current_ssid;
3376                 if (s == NULL)
3377                         continue;
3378                 if (s->mode != WPAS_MODE_P2P_GO &&
3379                     s->mode != WPAS_MODE_AP &&
3380                     s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3381                         continue;
3382                 if (s->ssid_len != ssid_len ||
3383                     os_memcmp(ssid, s->ssid, ssid_len) != 0)
3384                         continue;
3385                 return wpa_s;
3386         }
3387
3388         return NULL;
3389
3390 }
3391
3392
3393 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3394                                                   const u8 *peer_dev_addr)
3395 {
3396         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3397                 struct wpa_ssid *ssid = wpa_s->current_ssid;
3398                 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3399                         continue;
3400                 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3401                         return wpa_s;
3402         }
3403
3404         return NULL;
3405 }
3406
3407
3408 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3409 {
3410         struct wpa_supplicant *wpa_s = ctx;
3411
3412         return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3413 }
3414
3415
3416 static int wpas_is_concurrent_session_active(void *ctx)
3417 {
3418         struct wpa_supplicant *wpa_s = ctx;
3419         struct wpa_supplicant *ifs;
3420
3421         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3422                 if (ifs == wpa_s)
3423                         continue;
3424                 if (ifs->wpa_state > WPA_ASSOCIATED)
3425                         return 1;
3426         }
3427         return 0;
3428 }
3429
3430
3431 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3432 {
3433         struct wpa_supplicant *wpa_s = ctx;
3434         wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3435 }
3436
3437
3438 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3439                                   const char *conf_p2p_dev)
3440 {
3441         struct wpa_interface iface;
3442         struct wpa_supplicant *p2pdev_wpa_s;
3443         char ifname[100];
3444         char force_name[100];
3445         int ret;
3446
3447         ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3448                           wpa_s->ifname);
3449         if (os_snprintf_error(sizeof(ifname), ret))
3450                 return -1;
3451         force_name[0] = '\0';
3452         wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3453         ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3454                              force_name, wpa_s->pending_interface_addr, NULL);
3455         if (ret < 0) {
3456                 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3457                 return ret;
3458         }
3459         os_strlcpy(wpa_s->pending_interface_name, ifname,
3460                    sizeof(wpa_s->pending_interface_name));
3461
3462         os_memset(&iface, 0, sizeof(iface));
3463         iface.p2p_mgmt = 1;
3464         iface.ifname = wpa_s->pending_interface_name;
3465         iface.driver = wpa_s->driver->name;
3466         iface.driver_param = wpa_s->conf->driver_param;
3467
3468         /*
3469          * If a P2P Device configuration file was given, use it as the interface
3470          * configuration file (instead of using parent's configuration file.
3471          */
3472         if (conf_p2p_dev) {
3473                 iface.confname = conf_p2p_dev;
3474                 iface.ctrl_interface = NULL;
3475         } else {
3476                 iface.confname = wpa_s->confname;
3477                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3478         }
3479
3480         p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3481         if (!p2pdev_wpa_s) {
3482                 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3483                 return -1;
3484         }
3485
3486         wpa_s->pending_interface_name[0] = '\0';
3487         return 0;
3488 }
3489
3490
3491 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3492                                const u8 *noa, size_t noa_len)
3493 {
3494         struct wpa_supplicant *wpa_s, *intf = ctx;
3495         char hex[100];
3496
3497         for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3498                 if (wpa_s->waiting_presence_resp)
3499                         break;
3500         }
3501         if (!wpa_s) {
3502                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3503                 return;
3504         }
3505         wpa_s->waiting_presence_resp = 0;
3506
3507         wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3508         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3509                 " status=%u noa=%s", MAC2STR(src), status, hex);
3510 }
3511
3512
3513 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3514                                      size_t ssid_len, u8 *go_dev_addr,
3515                                      u8 *ret_ssid, size_t *ret_ssid_len,
3516                                      u8 *intended_iface_addr)
3517 {
3518         struct wpa_supplicant *wpa_s = ctx;
3519         struct wpa_ssid *s;
3520
3521         s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3522         if (s) {
3523                 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3524                 *ret_ssid_len = s->ssid_len;
3525                 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3526
3527                 if (s->mode != WPAS_MODE_P2P_GO) {
3528                         os_memset(intended_iface_addr, 0, ETH_ALEN);
3529                 } else if (wpas_p2p_create_iface(wpa_s)) {
3530                         if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3531                                 return 0;
3532
3533                         os_memcpy(intended_iface_addr,
3534                                   wpa_s->pending_interface_addr, ETH_ALEN);
3535                 } else {
3536                         os_memcpy(intended_iface_addr, wpa_s->own_addr,
3537                                   ETH_ALEN);
3538                 }
3539                 return 1;
3540         }
3541
3542         return 0;
3543 }
3544
3545
3546 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3547                             u8 *ssid, size_t *ssid_len, int *group_iface)
3548 {
3549         struct wpa_supplicant *wpa_s = ctx;
3550         struct wpa_ssid *s;
3551         u8 bssid[ETH_ALEN];
3552
3553         /*
3554          * group_iface will be set to 1 only if a dedicated interface for P2P
3555          * role is required. First, we try to reuse an active GO. However,
3556          * if it is not present, we will try to reactivate an existing
3557          * persistent group and set group_iface to 1, so the caller will know
3558          * that the pending interface should be used.
3559          */
3560         *group_iface = 0;
3561         s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3562         if (!s) {
3563                 s = wpas_p2p_get_persistent_go(wpa_s);
3564                 *group_iface = wpas_p2p_create_iface(wpa_s);
3565                 if (s)
3566                         os_memcpy(bssid, s->bssid, ETH_ALEN);
3567                 else
3568                         return 0;
3569         }
3570
3571         os_memcpy(intended_addr, bssid, ETH_ALEN);
3572         os_memcpy(ssid, s->ssid, s->ssid_len);
3573         *ssid_len = s->ssid_len;
3574
3575         return 1;
3576 }
3577
3578
3579 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3580                                     const u8 *ssid, size_t ssid_len)
3581 {
3582         struct wpa_supplicant *wpa_s = ctx;
3583         struct wpa_ssid *s;
3584         int save_config = 0;
3585         size_t i;
3586
3587         /* Start with our first choice of Persistent Groups */
3588         while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3589                 if (go && ssid && ssid_len &&
3590                     s->ssid_len == ssid_len &&
3591                     os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3592                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
3593                         break;
3594
3595                 /* Remove stale persistent group */
3596                 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3597                         wpa_config_remove_network(wpa_s->conf, s->id);
3598                         save_config = 1;
3599                         continue;
3600                 }
3601
3602                 for (i = 0; i < s->num_p2p_clients; i++) {
3603                         if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3604                                       peer, ETH_ALEN) != 0)
3605                                 continue;
3606
3607                         os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3608                                    s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3609                                    (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3610                         break;
3611                 }
3612                 s->num_p2p_clients--;
3613                 save_config = 1;
3614         }
3615
3616         if (save_config)
3617                 p2p_config_write(wpa_s);
3618
3619         /* Return TRUE if valid SSID remains */
3620         return s != NULL;
3621 }
3622
3623
3624 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3625                                        const u8 *feat_cap, size_t feat_cap_len)
3626 {
3627         static const char pref[] = " feature_cap=";
3628         int ret;
3629
3630         buf[0] = '\0';
3631
3632         /*
3633          * We expect a feature capability to contain at least one byte to be
3634          * reported. The string buffer provided by the caller function is
3635          * expected to be big enough to contain all bytes of the attribute for
3636          * known specifications. This function truncates the reported bytes if
3637          * the feature capability data exceeds the string buffer size.
3638          */
3639         if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3640                 return;
3641
3642         os_memcpy(buf, pref, sizeof(pref));
3643         ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3644                                buf_len - sizeof(pref) + 1,
3645                                feat_cap, feat_cap_len);
3646
3647         if (ret != (2 * (int) feat_cap_len))
3648                 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3649 }
3650
3651
3652 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3653                                     const u8 *adv_mac, const u8 *ses_mac,
3654                                     const u8 *grp_mac, u32 adv_id, u32 ses_id,
3655                                     u8 conncap, int passwd_id,
3656                                     const u8 *persist_ssid,
3657                                     size_t persist_ssid_size, int response_done,
3658                                     int prov_start, const char *session_info,
3659                                     const u8 *feat_cap, size_t feat_cap_len)
3660 {
3661         struct wpa_supplicant *wpa_s = ctx;
3662         u8 mac[ETH_ALEN];
3663         struct wpa_ssid *persistent_go, *stale, *s;
3664         int save_config = 0;
3665         struct wpa_supplicant *go_wpa_s;
3666         char feat_cap_str[256];
3667
3668         if (!dev)
3669                 return;
3670
3671         os_memset(mac, 0, ETH_ALEN);
3672         if (!adv_mac)
3673                 adv_mac = mac;
3674         if (!ses_mac)
3675                 ses_mac = mac;
3676         if (!grp_mac)
3677                 grp_mac = mac;
3678
3679         wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3680                                    feat_cap, feat_cap_len);
3681
3682         if (prov_start) {
3683                 if (session_info == NULL) {
3684                         wpa_msg_global(wpa_s, MSG_INFO,
3685                                        P2P_EVENT_P2PS_PROVISION_START MACSTR
3686                                        " adv_id=%x conncap=%x"
3687                                        " adv_mac=" MACSTR
3688                                        " session=%x mac=" MACSTR
3689                                        " dev_passwd_id=%d%s",
3690                                        MAC2STR(dev), adv_id, conncap,
3691                                        MAC2STR(adv_mac),
3692                                        ses_id, MAC2STR(ses_mac),
3693                                        passwd_id, feat_cap_str);
3694                 } else {
3695                         wpa_msg_global(wpa_s, MSG_INFO,
3696                                        P2P_EVENT_P2PS_PROVISION_START MACSTR
3697                                        " adv_id=%x conncap=%x"
3698                                        " adv_mac=" MACSTR
3699                                        " session=%x mac=" MACSTR
3700                                        " dev_passwd_id=%d info='%s'%s",
3701                                        MAC2STR(dev), adv_id, conncap,
3702                                        MAC2STR(adv_mac),
3703                                        ses_id, MAC2STR(ses_mac),
3704                                        passwd_id, session_info, feat_cap_str);
3705                 }
3706                 return;
3707         }
3708
3709         go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3710         persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3711
3712         if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3713                 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3714                         wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3715
3716                 if (persistent_go && !persistent_go->num_p2p_clients) {
3717                         /* remove empty persistent GO */
3718                         wpa_config_remove_network(wpa_s->conf,
3719                                                   persistent_go->id);
3720                 }
3721
3722                 wpa_msg_global(wpa_s, MSG_INFO,
3723                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3724                                " status=%d"
3725                                " adv_id=%x adv_mac=" MACSTR
3726                                " session=%x mac=" MACSTR "%s",
3727                                MAC2STR(dev), status,
3728                                adv_id, MAC2STR(adv_mac),
3729                                ses_id, MAC2STR(ses_mac), feat_cap_str);
3730                 return;
3731         }
3732
3733         /* Clean up stale persistent groups with this device */
3734         s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3735                                     persist_ssid_size);
3736
3737         if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3738             os_memcmp(grp_mac, mac, ETH_ALEN) == 0) {
3739                 wpa_dbg(wpa_s, MSG_ERROR,
3740                         "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3741                 return;
3742         }
3743
3744         for (;;) {
3745                 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3746                 if (!stale)
3747                         break;
3748
3749                 if (s && s->ssid_len == stale->ssid_len &&
3750                     os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3751                     os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3752                         break;
3753
3754                 /* Remove stale persistent group */
3755                 if (stale->mode != WPAS_MODE_P2P_GO ||
3756                     stale->num_p2p_clients <= 1) {
3757                         wpa_config_remove_network(wpa_s->conf, stale->id);
3758                 } else {
3759                         size_t i;
3760
3761                         for (i = 0; i < stale->num_p2p_clients; i++) {
3762                                 if (os_memcmp(stale->p2p_client_list +
3763                                               i * ETH_ALEN,
3764                                               dev, ETH_ALEN) == 0) {
3765                                         os_memmove(stale->p2p_client_list +
3766                                                    i * ETH_ALEN,
3767                                                    stale->p2p_client_list +
3768                                                    (i + 1) * ETH_ALEN,
3769                                                    (stale->num_p2p_clients -
3770                                                     i - 1) * ETH_ALEN);
3771                                         break;
3772                                 }
3773                         }
3774                         stale->num_p2p_clients--;
3775                 }
3776                 save_config = 1;
3777         }
3778
3779         if (save_config)
3780                 p2p_config_write(wpa_s);
3781
3782         if (s) {
3783                 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3784                         wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3785
3786                 if (persistent_go && s != persistent_go &&
3787                     !persistent_go->num_p2p_clients) {
3788                         /* remove empty persistent GO */
3789                         wpa_config_remove_network(wpa_s->conf,
3790                                                   persistent_go->id);
3791                         /* Save config */
3792                 }
3793
3794                 wpa_msg_global(wpa_s, MSG_INFO,
3795                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3796                                " status=%d"
3797                                " adv_id=%x adv_mac=" MACSTR
3798                                " session=%x mac=" MACSTR
3799                                " persist=%d%s",
3800                                MAC2STR(dev), status,
3801                                adv_id, MAC2STR(adv_mac),
3802                                ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3803                 return;
3804         }
3805
3806         if (conncap == P2PS_SETUP_GROUP_OWNER) {
3807                 /*
3808                  * We need to copy the interface name. Simply saving a
3809                  * pointer isn't enough, since if we use pending_interface_name
3810                  * it will be overwritten when the group is added.
3811                  */
3812                 char go_ifname[100];
3813
3814                 go_ifname[0] = '\0';
3815                 if (!go_wpa_s) {
3816                         wpa_s->global->pending_p2ps_group = 1;
3817
3818                         if (!wpas_p2p_create_iface(wpa_s))
3819                                 os_memcpy(go_ifname, wpa_s->ifname,
3820                                           sizeof(go_ifname));
3821                         else if (wpa_s->pending_interface_name[0])
3822                                 os_memcpy(go_ifname,
3823                                           wpa_s->pending_interface_name,
3824                                           sizeof(go_ifname));
3825
3826                         if (!go_ifname[0]) {
3827                                 wpas_p2ps_prov_complete(
3828                                         wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3829                                         dev, adv_mac, ses_mac,
3830                                         NULL, adv_id, ses_id, 0, 0,
3831                                         NULL, 0, 0, 0, NULL, NULL, 0);
3832                                 return;
3833                         }
3834
3835                         /* If PD Resp complete, start up the GO */
3836                         if (response_done && persistent_go) {
3837                                 wpas_p2p_group_add_persistent(
3838                                         wpa_s, persistent_go,
3839                                         0, 0, 0, 0, 0, NULL,
3840                                         persistent_go->mode ==
3841                                         WPAS_MODE_P2P_GO ?
3842                                         P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3843                                         0);
3844                         } else if (response_done) {
3845                                 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3846                         }
3847
3848                         if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3849                                 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3850                                 wpa_s->p2ps_join_addr_valid = 1;
3851                                 wpa_dbg(wpa_s, MSG_DEBUG,
3852                                         "P2PS: Saving PIN for " MACSTR,
3853                                         MAC2STR(dev));
3854                         }
3855                 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3856                         os_memcpy(go_ifname, go_wpa_s->ifname,
3857                                   sizeof(go_ifname));
3858
3859                         wpa_dbg(go_wpa_s, MSG_DEBUG,
3860                                 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3861                         wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3862                                                   NULL, 0, 0);
3863
3864                         os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3865                         wpa_s->p2ps_join_addr_valid = 1;
3866                         wpa_dbg(wpa_s, MSG_DEBUG,
3867                                 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3868                 }
3869
3870                 wpa_msg_global(wpa_s, MSG_INFO,
3871                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3872                                " status=%d conncap=%x"
3873                                " adv_id=%x adv_mac=" MACSTR
3874                                " session=%x mac=" MACSTR
3875                                " dev_passwd_id=%d go=%s%s",
3876                                MAC2STR(dev), status, conncap,
3877                                adv_id, MAC2STR(adv_mac),
3878                                ses_id, MAC2STR(ses_mac),
3879                                passwd_id, go_ifname, feat_cap_str);
3880                 return;
3881         }
3882
3883         if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3884                 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3885
3886         if (persistent_go && !persistent_go->num_p2p_clients) {
3887                 /* remove empty persistent GO */
3888                 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3889         }
3890
3891         if (conncap == P2PS_SETUP_CLIENT) {
3892                 wpa_msg_global(wpa_s, MSG_INFO,
3893                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3894                                " status=%d conncap=%x"
3895                                " adv_id=%x adv_mac=" MACSTR
3896                                " session=%x mac=" MACSTR
3897                                " dev_passwd_id=%d join=" MACSTR "%s",
3898                                MAC2STR(dev), status, conncap,
3899                                adv_id, MAC2STR(adv_mac),
3900                                ses_id, MAC2STR(ses_mac),
3901                                passwd_id, MAC2STR(grp_mac), feat_cap_str);
3902         } else {
3903                 wpa_msg_global(wpa_s, MSG_INFO,
3904                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3905                                " status=%d conncap=%x"
3906                                " adv_id=%x adv_mac=" MACSTR
3907                                " session=%x mac=" MACSTR
3908                                " dev_passwd_id=%d%s",
3909                                MAC2STR(dev), status, conncap,
3910                                adv_id, MAC2STR(adv_mac),
3911                                ses_id, MAC2STR(ses_mac),
3912                                passwd_id, feat_cap_str);
3913         }
3914 }
3915
3916
3917 static int _wpas_p2p_in_progress(void *ctx)
3918 {
3919         struct wpa_supplicant *wpa_s = ctx;
3920         return wpas_p2p_in_progress(wpa_s);
3921 }
3922
3923
3924 static int wpas_prov_disc_resp_cb(void *ctx)
3925 {
3926         struct wpa_supplicant *wpa_s = ctx;
3927         struct wpa_ssid *persistent_go;
3928
3929         if (!wpa_s->global->pending_p2ps_group)
3930                 return 0;
3931
3932         wpa_s->global->pending_p2ps_group = 0;
3933
3934         if (wpas_p2p_get_go_group(wpa_s))
3935                 return 0;
3936         persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3937
3938         if (persistent_go) {
3939                 wpas_p2p_group_add_persistent(
3940                         wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3941                         persistent_go->mode == WPAS_MODE_P2P_GO ?
3942                         P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3943         } else {
3944                 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3945         }
3946
3947         return 1;
3948 }
3949
3950
3951 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
3952                                        unsigned int *len,
3953                                        unsigned int *freq_list)
3954 {
3955         struct wpa_supplicant *wpa_s = ctx;
3956
3957         return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
3958                                           WPA_IF_P2P_CLIENT, len, freq_list);
3959 }
3960
3961
3962 /**
3963  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3964  * @global: Pointer to global data from wpa_supplicant_init()
3965  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3966  * Returns: 0 on success, -1 on failure
3967  */
3968 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3969 {
3970         struct p2p_config p2p;
3971         int i;
3972
3973         if (wpa_s->conf->p2p_disabled)
3974                 return 0;
3975
3976         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3977                 return 0;
3978
3979         if (global->p2p)
3980                 return 0;
3981
3982         os_memset(&p2p, 0, sizeof(p2p));
3983         p2p.cb_ctx = wpa_s;
3984         p2p.debug_print = wpas_p2p_debug_print;
3985         p2p.p2p_scan = wpas_p2p_scan;
3986         p2p.send_action = wpas_send_action;
3987         p2p.send_action_done = wpas_send_action_done;
3988         p2p.go_neg_completed = wpas_go_neg_completed;
3989         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3990         p2p.dev_found = wpas_dev_found;
3991         p2p.dev_lost = wpas_dev_lost;
3992         p2p.find_stopped = wpas_find_stopped;
3993         p2p.start_listen = wpas_start_listen;
3994         p2p.stop_listen = wpas_stop_listen;
3995         p2p.send_probe_resp = wpas_send_probe_resp;
3996         p2p.sd_request = wpas_sd_request;
3997         p2p.sd_response = wpas_sd_response;
3998         p2p.prov_disc_req = wpas_prov_disc_req;
3999         p2p.prov_disc_resp = wpas_prov_disc_resp;
4000         p2p.prov_disc_fail = wpas_prov_disc_fail;
4001         p2p.invitation_process = wpas_invitation_process;
4002         p2p.invitation_received = wpas_invitation_received;
4003         p2p.invitation_result = wpas_invitation_result;
4004         p2p.get_noa = wpas_get_noa;
4005         p2p.go_connected = wpas_go_connected;
4006         p2p.presence_resp = wpas_presence_resp;
4007         p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4008         p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4009         p2p.get_persistent_group = wpas_get_persistent_group;
4010         p2p.get_go_info = wpas_get_go_info;
4011         p2p.remove_stale_groups = wpas_remove_stale_groups;
4012         p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4013         p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4014         p2p.p2ps_group_capability = p2ps_group_capability;
4015         p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4016
4017         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4018         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4019         p2p.dev_name = wpa_s->conf->device_name;
4020         p2p.manufacturer = wpa_s->conf->manufacturer;
4021         p2p.model_name = wpa_s->conf->model_name;
4022         p2p.model_number = wpa_s->conf->model_number;
4023         p2p.serial_number = wpa_s->conf->serial_number;
4024         if (wpa_s->wps) {
4025                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4026                 p2p.config_methods = wpa_s->wps->config_methods;
4027         }
4028
4029         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4030                 wpa_printf(MSG_ERROR,
4031                            "P2P: Failed to configure supported channel list");
4032                 return -1;
4033         }
4034
4035         if (wpa_s->conf->p2p_listen_reg_class &&
4036             wpa_s->conf->p2p_listen_channel) {
4037                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4038                 p2p.channel = wpa_s->conf->p2p_listen_channel;
4039                 p2p.channel_forced = 1;
4040         } else {
4041                 /*
4042                  * Pick one of the social channels randomly as the listen
4043                  * channel.
4044                  */
4045                 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4046                                                  &p2p.channel) != 0) {
4047                         wpa_printf(MSG_INFO,
4048                                    "P2P: No social channels supported by the driver - do not enable P2P");
4049                         return 0;
4050                 }
4051                 p2p.channel_forced = 0;
4052         }
4053         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4054                    p2p.reg_class, p2p.channel);
4055
4056         if (wpa_s->conf->p2p_oper_reg_class &&
4057             wpa_s->conf->p2p_oper_channel) {
4058                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4059                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4060                 p2p.cfg_op_channel = 1;
4061                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4062                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
4063
4064         } else {
4065                 /*
4066                  * Use random operation channel from 2.4 GHz band social
4067                  * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4068                  * other preference is indicated.
4069                  */
4070                 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4071                                                  &p2p.op_channel) != 0) {
4072                         wpa_printf(MSG_ERROR,
4073                                    "P2P: Failed to select random social channel as operation channel");
4074                         return -1;
4075                 }
4076                 p2p.cfg_op_channel = 0;
4077                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4078                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
4079         }
4080
4081         if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4082                 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4083                 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4084         }
4085
4086         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4087                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4088                 p2p.country[2] = 0x04;
4089         } else
4090                 os_memcpy(p2p.country, "XX\x04", 3);
4091
4092         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4093                   WPS_DEV_TYPE_LEN);
4094
4095         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4096         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4097                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4098
4099         p2p.concurrent_operations = !!(wpa_s->drv_flags &
4100                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4101
4102         p2p.max_peers = 100;
4103
4104         if (wpa_s->conf->p2p_ssid_postfix) {
4105                 p2p.ssid_postfix_len =
4106                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
4107                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4108                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4109                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4110                           p2p.ssid_postfix_len);
4111         }
4112
4113         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4114
4115         p2p.max_listen = wpa_s->max_remain_on_chan;
4116
4117         if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4118             wpa_s->conf->p2p_passphrase_len <= 63)
4119                 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4120         else
4121                 p2p.passphrase_len = 8;
4122
4123         global->p2p = p2p_init(&p2p);
4124         if (global->p2p == NULL)
4125                 return -1;
4126         global->p2p_init_wpa_s = wpa_s;
4127
4128         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4129                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4130                         continue;
4131                 p2p_add_wps_vendor_extension(
4132                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4133         }
4134
4135         p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4136
4137         return 0;
4138 }
4139
4140
4141 /**
4142  * wpas_p2p_deinit - Deinitialize per-interface P2P data
4143  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4144  *
4145  * This function deinitialize per-interface P2P data.
4146  */
4147 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4148 {
4149         if (wpa_s->driver && wpa_s->drv_priv)
4150                 wpa_drv_probe_req_report(wpa_s, 0);
4151
4152         if (wpa_s->go_params) {
4153                 /* Clear any stored provisioning info */
4154                 p2p_clear_provisioning_info(
4155                         wpa_s->global->p2p,
4156                         wpa_s->go_params->peer_device_addr);
4157         }
4158
4159         os_free(wpa_s->go_params);
4160         wpa_s->go_params = NULL;
4161         eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4162         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4163         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4164         wpa_s->p2p_long_listen = 0;
4165         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4166         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4167         wpas_p2p_remove_pending_group_interface(wpa_s);
4168         eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4169         wpas_p2p_listen_work_done(wpa_s);
4170         if (wpa_s->p2p_send_action_work) {
4171                 os_free(wpa_s->p2p_send_action_work->ctx);
4172                 radio_work_done(wpa_s->p2p_send_action_work);
4173                 wpa_s->p2p_send_action_work = NULL;
4174         }
4175         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4176
4177         wpabuf_free(wpa_s->p2p_oob_dev_pw);
4178         wpa_s->p2p_oob_dev_pw = NULL;
4179
4180         os_free(wpa_s->p2p_group_common_freqs);
4181         wpa_s->p2p_group_common_freqs = NULL;
4182         wpa_s->p2p_group_common_freqs_num = 0;
4183
4184         /* TODO: remove group interface from the driver if this wpa_s instance
4185          * is on top of a P2P group interface */
4186 }
4187
4188
4189 /**
4190  * wpas_p2p_deinit_global - Deinitialize global P2P module
4191  * @global: Pointer to global data from wpa_supplicant_init()
4192  *
4193  * This function deinitializes the global (per device) P2P module.
4194  */
4195 static void wpas_p2p_deinit_global(struct wpa_global *global)
4196 {
4197         struct wpa_supplicant *wpa_s, *tmp;
4198
4199         wpa_s = global->ifaces;
4200
4201         wpas_p2p_service_flush(global->p2p_init_wpa_s);
4202
4203         /* Remove remaining P2P group interfaces */
4204         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4205                 wpa_s = wpa_s->next;
4206         while (wpa_s) {
4207                 tmp = global->ifaces;
4208                 while (tmp &&
4209                        (tmp == wpa_s ||
4210                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4211                         tmp = tmp->next;
4212                 }
4213                 if (tmp == NULL)
4214                         break;
4215                 /* Disconnect from the P2P group and deinit the interface */
4216                 wpas_p2p_disconnect(tmp);
4217         }
4218
4219         /*
4220          * Deinit GO data on any possibly remaining interface (if main
4221          * interface is used as GO).
4222          */
4223         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4224                 if (wpa_s->ap_iface)
4225                         wpas_p2p_group_deinit(wpa_s);
4226         }
4227
4228         p2p_deinit(global->p2p);
4229         global->p2p = NULL;
4230         global->p2p_init_wpa_s = NULL;
4231 }
4232
4233
4234 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4235 {
4236         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4237             wpa_s->conf->p2p_no_group_iface)
4238                 return 0; /* separate interface disabled per configuration */
4239         if (wpa_s->drv_flags &
4240             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4241              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4242                 return 1; /* P2P group requires a new interface in every case
4243                            */
4244         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4245                 return 0; /* driver does not support concurrent operations */
4246         if (wpa_s->global->ifaces->next)
4247                 return 1; /* more that one interface already in use */
4248         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4249                 return 1; /* this interface is already in use */
4250         return 0;
4251 }
4252
4253
4254 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4255                                  const u8 *peer_addr,
4256                                  enum p2p_wps_method wps_method,
4257                                  int go_intent, const u8 *own_interface_addr,
4258                                  unsigned int force_freq, int persistent_group,
4259                                  struct wpa_ssid *ssid, unsigned int pref_freq)
4260 {
4261         if (persistent_group && wpa_s->conf->persistent_reconnect)
4262                 persistent_group = 2;
4263
4264         /*
4265          * Increase GO config timeout if HT40 is used since it takes some time
4266          * to scan channels for coex purposes before the BSS can be started.
4267          */
4268         p2p_set_config_timeout(wpa_s->global->p2p,
4269                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4270
4271         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4272                            go_intent, own_interface_addr, force_freq,
4273                            persistent_group, ssid ? ssid->ssid : NULL,
4274                            ssid ? ssid->ssid_len : 0,
4275                            wpa_s->p2p_pd_before_go_neg, pref_freq,
4276                            wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4277                            0);
4278 }
4279
4280
4281 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4282                                 const u8 *peer_addr,
4283                                 enum p2p_wps_method wps_method,
4284                                 int go_intent, const u8 *own_interface_addr,
4285                                 unsigned int force_freq, int persistent_group,
4286                                 struct wpa_ssid *ssid, unsigned int pref_freq)
4287 {
4288         if (persistent_group && wpa_s->conf->persistent_reconnect)
4289                 persistent_group = 2;
4290
4291         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4292                              go_intent, own_interface_addr, force_freq,
4293                              persistent_group, ssid ? ssid->ssid : NULL,
4294                              ssid ? ssid->ssid_len : 0, pref_freq,
4295                              wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4296                              0);
4297 }
4298
4299
4300 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4301 {
4302         wpa_s->p2p_join_scan_count++;
4303         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4304                    wpa_s->p2p_join_scan_count);
4305         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4306                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4307                            " for join operationg - stop join attempt",
4308                            MAC2STR(wpa_s->pending_join_iface_addr));
4309                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4310                 if (wpa_s->p2p_auto_pd) {
4311                         wpa_s->p2p_auto_pd = 0;
4312                         wpa_msg_global(wpa_s, MSG_INFO,
4313                                        P2P_EVENT_PROV_DISC_FAILURE
4314                                        " p2p_dev_addr=" MACSTR " status=N/A",
4315                                        MAC2STR(wpa_s->pending_join_dev_addr));
4316                         return;
4317                 }
4318                 wpa_msg_global(wpa_s->parent, MSG_INFO,
4319                                P2P_EVENT_GROUP_FORMATION_FAILURE);
4320         }
4321 }
4322
4323
4324 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4325 {
4326         int res;
4327         unsigned int num, i;
4328         struct wpa_used_freq_data *freqs;
4329
4330         if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4331                 /* Multiple channels are supported and not all are in use */
4332                 return 0;
4333         }
4334
4335         freqs = os_calloc(wpa_s->num_multichan_concurrent,
4336                           sizeof(struct wpa_used_freq_data));
4337         if (!freqs)
4338                 return 1;
4339
4340         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4341                                         wpa_s->num_multichan_concurrent);
4342
4343         for (i = 0; i < num; i++) {
4344                 if (freqs[i].freq == freq) {
4345                         wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4346                                    freq);
4347                         res = 0;
4348                         goto exit_free;
4349                 }
4350         }
4351
4352         wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4353         res = 1;
4354
4355 exit_free:
4356         os_free(freqs);
4357         return res;
4358 }
4359
4360
4361 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4362                             const u8 *peer_dev_addr)
4363 {
4364         struct wpa_bss *bss;
4365         int updated;
4366
4367         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4368         if (bss == NULL)
4369                 return -1;
4370         if (bss->last_update_idx < wpa_s->bss_update_idx) {
4371                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4372                            "last scan");
4373                 return 0;
4374         }
4375
4376         updated = os_reltime_before(&wpa_s->p2p_auto_started,
4377                                     &bss->last_update);
4378         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4379                    "%ld.%06ld (%supdated in last scan)",
4380                    bss->last_update.sec, bss->last_update.usec,
4381                    updated ? "": "not ");
4382
4383         return updated;
4384 }
4385
4386
4387 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4388                                    struct wpa_scan_results *scan_res)
4389 {
4390         struct wpa_bss *bss = NULL;
4391         int freq;
4392         u8 iface_addr[ETH_ALEN];
4393
4394         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4395
4396         if (wpa_s->global->p2p_disabled)
4397                 return;
4398
4399         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4400                    scan_res ? (int) scan_res->num : -1,
4401                    wpa_s->p2p_auto_join ? "auto_" : "");
4402
4403         if (scan_res)
4404                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4405
4406         if (wpa_s->p2p_auto_pd) {
4407                 int join = wpas_p2p_peer_go(wpa_s,
4408                                             wpa_s->pending_join_dev_addr);
4409                 if (join == 0 &&
4410                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4411                         wpa_s->auto_pd_scan_retry++;
4412                         bss = wpa_bss_get_bssid_latest(
4413                                 wpa_s, wpa_s->pending_join_dev_addr);
4414                         if (bss) {
4415                                 freq = bss->freq;
4416                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4417                                            "the peer " MACSTR " at %d MHz",
4418                                            wpa_s->auto_pd_scan_retry,
4419                                            MAC2STR(wpa_s->
4420                                                    pending_join_dev_addr),
4421                                            freq);
4422                                 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4423                                 return;
4424                         }
4425                 }
4426
4427                 if (join < 0)
4428                         join = 0;
4429
4430                 wpa_s->p2p_auto_pd = 0;
4431                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4432                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4433                            MAC2STR(wpa_s->pending_join_dev_addr), join);
4434                 if (p2p_prov_disc_req(wpa_s->global->p2p,
4435                                       wpa_s->pending_join_dev_addr, NULL,
4436                                       wpa_s->pending_pd_config_methods, join,
4437                                       0, wpa_s->user_initiated_pd) < 0) {
4438                         wpa_s->p2p_auto_pd = 0;
4439                         wpa_msg_global(wpa_s, MSG_INFO,
4440                                        P2P_EVENT_PROV_DISC_FAILURE
4441                                        " p2p_dev_addr=" MACSTR " status=N/A",
4442                                        MAC2STR(wpa_s->pending_join_dev_addr));
4443                 }
4444                 return;
4445         }
4446
4447         if (wpa_s->p2p_auto_join) {
4448                 int join = wpas_p2p_peer_go(wpa_s,
4449                                             wpa_s->pending_join_dev_addr);
4450                 if (join < 0) {
4451                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4452                                    "running a GO -> use GO Negotiation");
4453                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4454                                        P2P_EVENT_FALLBACK_TO_GO_NEG
4455                                        "reason=peer-not-running-GO");
4456                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4457                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4458                                          wpa_s->p2p_persistent_group, 0, 0, 0,
4459                                          wpa_s->p2p_go_intent,
4460                                          wpa_s->p2p_connect_freq,
4461                                          wpa_s->p2p_persistent_id,
4462                                          wpa_s->p2p_pd_before_go_neg,
4463                                          wpa_s->p2p_go_ht40,
4464                                          wpa_s->p2p_go_vht);
4465                         return;
4466                 }
4467
4468                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4469                            "try to join the group", join ? "" :
4470                            " in older scan");
4471                 if (!join) {
4472                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4473                                        P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4474                         wpa_s->p2p_fallback_to_go_neg = 1;
4475                 }
4476         }
4477
4478         freq = p2p_get_oper_freq(wpa_s->global->p2p,
4479                                  wpa_s->pending_join_iface_addr);
4480         if (freq < 0 &&
4481             p2p_get_interface_addr(wpa_s->global->p2p,
4482                                    wpa_s->pending_join_dev_addr,
4483                                    iface_addr) == 0 &&
4484             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4485             && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4486                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4487                            "address for join from " MACSTR " to " MACSTR
4488                            " based on newly discovered P2P peer entry",
4489                            MAC2STR(wpa_s->pending_join_iface_addr),
4490                            MAC2STR(iface_addr));
4491                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4492                           ETH_ALEN);
4493
4494                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4495                                          wpa_s->pending_join_iface_addr);
4496         }
4497         if (freq >= 0) {
4498                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4499                            "from P2P peer table: %d MHz", freq);
4500         }
4501         if (wpa_s->p2p_join_ssid_len) {
4502                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4503                            MACSTR " and SSID %s",
4504                            MAC2STR(wpa_s->pending_join_iface_addr),
4505                            wpa_ssid_txt(wpa_s->p2p_join_ssid,
4506                                         wpa_s->p2p_join_ssid_len));
4507                 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4508                                   wpa_s->p2p_join_ssid,
4509                                   wpa_s->p2p_join_ssid_len);
4510         }
4511         if (!bss) {
4512                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4513                            MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4514                 bss = wpa_bss_get_bssid_latest(wpa_s,
4515                                                wpa_s->pending_join_iface_addr);
4516         }
4517         if (bss) {
4518                 u8 dev_addr[ETH_ALEN];
4519
4520                 freq = bss->freq;
4521                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4522                            "from BSS table: %d MHz (SSID %s)", freq,
4523                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
4524                 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4525                                        dev_addr) == 0 &&
4526                     os_memcmp(wpa_s->pending_join_dev_addr,
4527                               wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4528                     os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4529                               ETH_ALEN) != 0) {
4530                         wpa_printf(MSG_DEBUG,
4531                                    "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4532                                    MAC2STR(dev_addr),
4533                                    MAC2STR(wpa_s->pending_join_dev_addr));
4534                         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4535                                   ETH_ALEN);
4536                 }
4537         }
4538         if (freq > 0) {
4539                 u16 method;
4540
4541                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4542                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4543                                        P2P_EVENT_GROUP_FORMATION_FAILURE
4544                                        "reason=FREQ_CONFLICT");
4545                         return;
4546                 }
4547
4548                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4549                            "prior to joining an existing group (GO " MACSTR
4550                            " freq=%u MHz)",
4551                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
4552                 wpa_s->pending_pd_before_join = 1;
4553
4554                 switch (wpa_s->pending_join_wps_method) {
4555                 case WPS_PIN_DISPLAY:
4556                         method = WPS_CONFIG_KEYPAD;
4557                         break;
4558                 case WPS_PIN_KEYPAD:
4559                         method = WPS_CONFIG_DISPLAY;
4560                         break;
4561                 case WPS_PBC:
4562                         method = WPS_CONFIG_PUSHBUTTON;
4563                         break;
4564                 default:
4565                         method = 0;
4566                         break;
4567                 }
4568
4569                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4570                                                wpa_s->pending_join_dev_addr) ==
4571                      method)) {
4572                         /*
4573                          * We have already performed provision discovery for
4574                          * joining the group. Proceed directly to join
4575                          * operation without duplicated provision discovery. */
4576                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4577                                    "with " MACSTR " already done - proceed to "
4578                                    "join",
4579                                    MAC2STR(wpa_s->pending_join_dev_addr));
4580                         wpa_s->pending_pd_before_join = 0;
4581                         goto start;
4582                 }
4583
4584                 if (p2p_prov_disc_req(wpa_s->global->p2p,
4585                                       wpa_s->pending_join_dev_addr,
4586                                       NULL, method, 1,
4587                                       freq, wpa_s->user_initiated_pd) < 0) {
4588                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4589                                    "Discovery Request before joining an "
4590                                    "existing group");
4591                         wpa_s->pending_pd_before_join = 0;
4592                         goto start;
4593                 }
4594                 return;
4595         }
4596
4597         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4598         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4599         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4600         wpas_p2p_check_join_scan_limit(wpa_s);
4601         return;
4602
4603 start:
4604         /* Start join operation immediately */
4605         wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4606 }
4607
4608
4609 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4610                                    const u8 *ssid, size_t ssid_len)
4611 {
4612         int ret;
4613         struct wpa_driver_scan_params params;
4614         struct wpabuf *wps_ie, *ies;
4615         size_t ielen;
4616         int freqs[2] = { 0, 0 };
4617
4618         os_memset(&params, 0, sizeof(params));
4619
4620         /* P2P Wildcard SSID */
4621         params.num_ssids = 1;
4622         if (ssid && ssid_len) {
4623                 params.ssids[0].ssid = ssid;
4624                 params.ssids[0].ssid_len = ssid_len;
4625                 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4626                 wpa_s->p2p_join_ssid_len = ssid_len;
4627         } else {
4628                 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4629                 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4630                 wpa_s->p2p_join_ssid_len = 0;
4631         }
4632
4633         wpa_s->wps->dev.p2p = 1;
4634         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4635                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4636                                         NULL);
4637         if (wps_ie == NULL) {
4638                 wpas_p2p_scan_res_join(wpa_s, NULL);
4639                 return;
4640         }
4641
4642         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4643         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4644         if (ies == NULL) {
4645                 wpabuf_free(wps_ie);
4646                 wpas_p2p_scan_res_join(wpa_s, NULL);
4647                 return;
4648         }
4649         wpabuf_put_buf(ies, wps_ie);
4650         wpabuf_free(wps_ie);
4651
4652         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4653
4654         params.p2p_probe = 1;
4655         params.extra_ies = wpabuf_head(ies);
4656         params.extra_ies_len = wpabuf_len(ies);
4657
4658         if (!freq) {
4659                 int oper_freq;
4660                 /*
4661                  * If freq is not provided, check the operating freq of the GO
4662                  * and use a single channel scan on if possible.
4663                  */
4664                 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4665                                               wpa_s->pending_join_iface_addr);
4666                 if (oper_freq > 0)
4667                         freq = oper_freq;
4668         }
4669         if (freq > 0) {
4670                 freqs[0] = freq;
4671                 params.freqs = freqs;
4672         }
4673
4674         /*
4675          * Run a scan to update BSS table and start Provision Discovery once
4676          * the new scan results become available.
4677          */
4678         ret = wpa_drv_scan(wpa_s, &params);
4679         if (!ret) {
4680                 os_get_reltime(&wpa_s->scan_trigger_time);
4681                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4682                 wpa_s->own_scan_requested = 1;
4683         }
4684
4685         wpabuf_free(ies);
4686
4687         if (ret) {
4688                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4689                            "try again later");
4690                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4691                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4692                 wpas_p2p_check_join_scan_limit(wpa_s);
4693         }
4694 }
4695
4696
4697 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4698 {
4699         struct wpa_supplicant *wpa_s = eloop_ctx;
4700         wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4701 }
4702
4703
4704 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4705                          const u8 *dev_addr, enum p2p_wps_method wps_method,
4706                          int auto_join, int op_freq,
4707                          const u8 *ssid, size_t ssid_len)
4708 {
4709         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4710                    MACSTR " dev " MACSTR " op_freq=%d)%s",
4711                    MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4712                    auto_join ? " (auto_join)" : "");
4713         if (ssid && ssid_len) {
4714                 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4715                            wpa_ssid_txt(ssid, ssid_len));
4716         }
4717
4718         wpa_s->p2p_auto_pd = 0;
4719         wpa_s->p2p_auto_join = !!auto_join;
4720         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4721         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4722         wpa_s->pending_join_wps_method = wps_method;
4723
4724         /* Make sure we are not running find during connection establishment */
4725         wpas_p2p_stop_find(wpa_s);
4726
4727         wpa_s->p2p_join_scan_count = 0;
4728         wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4729         return 0;
4730 }
4731
4732
4733 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4734                                const u8 *ssid, size_t ssid_len)
4735 {
4736         struct wpa_supplicant *group;
4737         struct p2p_go_neg_results res;
4738         struct wpa_bss *bss;
4739
4740         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4741         if (group == NULL)
4742                 return -1;
4743         if (group != wpa_s) {
4744                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4745                           sizeof(group->p2p_pin));
4746                 group->p2p_wps_method = wpa_s->p2p_wps_method;
4747         } else {
4748                 /*
4749                  * Need to mark the current interface for p2p_group_formation
4750                  * when a separate group interface is not used. This is needed
4751                  * to allow p2p_cancel stop a pending p2p_connect-join.
4752                  * wpas_p2p_init_group_interface() addresses this for the case
4753                  * where a separate group interface is used.
4754                  */
4755                 wpa_s->global->p2p_group_formation = wpa_s;
4756         }
4757
4758         group->p2p_in_provisioning = 1;
4759         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4760
4761         os_memset(&res, 0, sizeof(res));
4762         os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4763         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4764                   ETH_ALEN);
4765         res.wps_method = wpa_s->pending_join_wps_method;
4766         if (freq && ssid && ssid_len) {
4767                 res.freq = freq;
4768                 res.ssid_len = ssid_len;
4769                 os_memcpy(res.ssid, ssid, ssid_len);
4770         } else {
4771                 bss = wpa_bss_get_bssid_latest(wpa_s,
4772                                                wpa_s->pending_join_iface_addr);
4773                 if (bss) {
4774                         res.freq = bss->freq;
4775                         res.ssid_len = bss->ssid_len;
4776                         os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4777                         wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4778                                    bss->freq,
4779                                    wpa_ssid_txt(bss->ssid, bss->ssid_len));
4780                 }
4781         }
4782
4783         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4784                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4785                            "starting client");
4786                 wpa_drv_cancel_remain_on_channel(wpa_s);
4787                 wpa_s->off_channel_freq = 0;
4788                 wpa_s->roc_waiting_drv_freq = 0;
4789         }
4790         wpas_start_wps_enrollee(group, &res);
4791
4792         /*
4793          * Allow a longer timeout for join-a-running-group than normal 15
4794          * second group formation timeout since the GO may not have authorized
4795          * our connection yet.
4796          */
4797         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4798         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4799                                wpa_s, NULL);
4800
4801         return 0;
4802 }
4803
4804
4805 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4806                                 int *force_freq, int *pref_freq, int go,
4807                                 unsigned int *pref_freq_list,
4808                                 unsigned int *num_pref_freq)
4809 {
4810         struct wpa_used_freq_data *freqs;
4811         int res, best_freq, num_unused;
4812         unsigned int freq_in_use = 0, num, i, max_pref_freq;
4813
4814         max_pref_freq = *num_pref_freq;
4815         *num_pref_freq = 0;
4816
4817         freqs = os_calloc(wpa_s->num_multichan_concurrent,
4818                           sizeof(struct wpa_used_freq_data));
4819         if (!freqs)
4820                 return -1;
4821
4822         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4823                                         wpa_s->num_multichan_concurrent);
4824
4825         /*
4826          * It is possible that the total number of used frequencies is bigger
4827          * than the number of frequencies used for P2P, so get the system wide
4828          * number of unused frequencies.
4829          */
4830         num_unused = wpas_p2p_num_unused_channels(wpa_s);
4831
4832         wpa_printf(MSG_DEBUG,
4833                    "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4834                    freq, wpa_s->num_multichan_concurrent, num, num_unused);
4835
4836         if (freq > 0) {
4837                 int ret;
4838                 if (go)
4839                         ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4840                 else
4841                         ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4842                 if (!ret) {
4843                         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4844                             ieee80211_is_dfs(freq)) {
4845                                 /*
4846                                  * If freq is a DFS channel and DFS is offloaded
4847                                  * to the driver, allow P2P GO to use it.
4848                                  */
4849                                 wpa_printf(MSG_DEBUG,
4850                                            "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4851                                            freq);
4852                         } else {
4853                                 wpa_printf(MSG_DEBUG,
4854                                            "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4855                                            freq);
4856                                 res = -3;
4857                                 goto exit_free;
4858                         }
4859                 }
4860
4861                 for (i = 0; i < num; i++) {
4862                         if (freqs[i].freq == freq)
4863                                 freq_in_use = 1;
4864                 }
4865
4866                 if (num_unused <= 0 && !freq_in_use) {
4867                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4868                                    freq);
4869                         res = -2;
4870                         goto exit_free;
4871                 }
4872                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4873                            "requested channel (%u MHz)", freq);
4874                 *force_freq = freq;
4875                 goto exit_ok;
4876         }
4877
4878         best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4879
4880         if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
4881                 enum wpa_driver_if_type iface_type;
4882
4883                 if (go)
4884                         iface_type = WPA_IF_P2P_GO;
4885                 else
4886                         iface_type = WPA_IF_P2P_CLIENT;
4887
4888                 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
4889                            best_freq, go);
4890
4891                 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
4892                                                  &max_pref_freq,
4893                                                  pref_freq_list);
4894                 if (!res && max_pref_freq > 0) {
4895                         *num_pref_freq = max_pref_freq;
4896                         i = 0;
4897                         while (wpas_p2p_disallowed_freq(wpa_s->global,
4898                                                         pref_freq_list[i]) &&
4899                                i < *num_pref_freq) {
4900                                 wpa_printf(MSG_DEBUG,
4901                                            "P2P: preferred_freq_list[%d]=%d is disallowed",
4902                                            i, pref_freq_list[i]);
4903                                 i++;
4904                         }
4905                         if (i != *num_pref_freq) {
4906                                 best_freq = pref_freq_list[i];
4907                                 wpa_printf(MSG_DEBUG,
4908                                            "P2P: Using preferred_freq_list[%d]=%d",
4909                                            i, best_freq);
4910                         } else {
4911                                 wpa_printf(MSG_DEBUG,
4912                                            "P2P: All driver preferred frequencies are disallowed for P2P use");
4913                                 *num_pref_freq = 0;
4914                         }
4915                 } else {
4916                         wpa_printf(MSG_DEBUG,
4917                                    "P2P: No preferred frequency list available");
4918                 }
4919         }
4920
4921         /* We have a candidate frequency to use */
4922         if (best_freq > 0) {
4923                 if (*pref_freq == 0 && num_unused > 0) {
4924                         wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4925                                    best_freq);
4926                         *pref_freq = best_freq;
4927                 } else {
4928                         wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4929                                    best_freq);
4930                         *force_freq = best_freq;
4931                 }
4932         } else if (num_unused > 0) {
4933                 wpa_printf(MSG_DEBUG,
4934                            "P2P: Current operating channels are not available for P2P. Try to use another channel");
4935                 *force_freq = 0;
4936         } else {
4937                 wpa_printf(MSG_DEBUG,
4938                            "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4939                 res = -2;
4940                 goto exit_free;
4941         }
4942
4943 exit_ok:
4944         res = 0;
4945 exit_free:
4946         os_free(freqs);
4947         return res;
4948 }
4949
4950
4951 /**
4952  * wpas_p2p_connect - Request P2P Group Formation to be started
4953  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4954  * @peer_addr: Address of the peer P2P Device
4955  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4956  * @persistent_group: Whether to create a persistent group
4957  * @auto_join: Whether to select join vs. GO Negotiation automatically
4958  * @join: Whether to join an existing group (as a client) instead of starting
4959  *      Group Owner negotiation; @peer_addr is BSSID in that case
4960  * @auth: Whether to only authorize the connection instead of doing that and
4961  *      initiating Group Owner negotiation
4962  * @go_intent: GO Intent or -1 to use default
4963  * @freq: Frequency for the group or 0 for auto-selection
4964  * @persistent_id: Persistent group credentials to use for forcing GO
4965  *      parameters or -1 to generate new values (SSID/passphrase)
4966  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4967  *      interoperability workaround when initiating group formation
4968  * @ht40: Start GO with 40 MHz channel width
4969  * @vht:  Start GO with VHT support
4970  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4971  *      failure, -2 on failure due to channel not currently available,
4972  *      -3 if forced channel is not supported
4973  */
4974 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4975                      const char *pin, enum p2p_wps_method wps_method,
4976                      int persistent_group, int auto_join, int join, int auth,
4977                      int go_intent, int freq, int persistent_id, int pd,
4978                      int ht40, int vht)
4979 {
4980         int force_freq = 0, pref_freq = 0;
4981         int ret = 0, res;
4982         enum wpa_driver_if_type iftype;
4983         const u8 *if_addr;
4984         struct wpa_ssid *ssid = NULL;
4985         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
4986
4987         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4988                 return -1;
4989
4990         if (persistent_id >= 0) {
4991                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4992                 if (ssid == NULL || ssid->disabled != 2 ||
4993                     ssid->mode != WPAS_MODE_P2P_GO)
4994                         return -1;
4995         }
4996
4997         os_free(wpa_s->global->add_psk);
4998         wpa_s->global->add_psk = NULL;
4999
5000         wpa_s->global->p2p_fail_on_wps_complete = 0;
5001         wpa_s->global->pending_p2ps_group = 0;
5002
5003         if (go_intent < 0)
5004                 go_intent = wpa_s->conf->p2p_go_intent;
5005
5006         if (!auth)
5007                 wpa_s->p2p_long_listen = 0;
5008
5009         wpa_s->p2p_wps_method = wps_method;
5010         wpa_s->p2p_persistent_group = !!persistent_group;
5011         wpa_s->p2p_persistent_id = persistent_id;
5012         wpa_s->p2p_go_intent = go_intent;
5013         wpa_s->p2p_connect_freq = freq;
5014         wpa_s->p2p_fallback_to_go_neg = 0;
5015         wpa_s->p2p_pd_before_go_neg = !!pd;
5016         wpa_s->p2p_go_ht40 = !!ht40;
5017         wpa_s->p2p_go_vht = !!vht;
5018
5019         if (pin)
5020                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5021         else if (wps_method == WPS_PIN_DISPLAY) {
5022                 ret = wps_generate_pin();
5023                 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5024                                   "%08d", ret);
5025                 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5026                         wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5027                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5028                            wpa_s->p2p_pin);
5029         } else
5030                 wpa_s->p2p_pin[0] = '\0';
5031
5032         if (join || auto_join) {
5033                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5034                 if (auth) {
5035                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5036                                    "connect a running group from " MACSTR,
5037                                    MAC2STR(peer_addr));
5038                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5039                         return ret;
5040                 }
5041                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5042                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5043                                            iface_addr) < 0) {
5044                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5045                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5046                                          dev_addr);
5047                 }
5048                 if (auto_join) {
5049                         os_get_reltime(&wpa_s->p2p_auto_started);
5050                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5051                                    "%ld.%06ld",
5052                                    wpa_s->p2p_auto_started.sec,
5053                                    wpa_s->p2p_auto_started.usec);
5054                 }
5055                 wpa_s->user_initiated_pd = 1;
5056                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5057                                   auto_join, freq, NULL, 0) < 0)
5058                         return -1;
5059                 return ret;
5060         }
5061
5062         size = P2P_MAX_PREF_CHANNELS;
5063         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5064                                    go_intent == 15, pref_freq_list, &size);
5065         if (res)
5066                 return res;
5067         wpas_p2p_set_own_freq_preference(wpa_s,
5068                                          force_freq ? force_freq : pref_freq);
5069
5070         p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5071
5072         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5073
5074         if (wpa_s->create_p2p_iface) {
5075                 /* Prepare to add a new interface for the group */
5076                 iftype = WPA_IF_P2P_GROUP;
5077                 if (go_intent == 15)
5078                         iftype = WPA_IF_P2P_GO;
5079                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5080                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5081                                    "interface for the group");
5082                         return -1;
5083                 }
5084
5085                 if_addr = wpa_s->pending_interface_addr;
5086         } else {
5087                 if_addr = wpa_s->own_addr;
5088                 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5089         }
5090
5091         if (auth) {
5092                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5093                                          go_intent, if_addr,
5094                                          force_freq, persistent_group, ssid,
5095                                          pref_freq) < 0)
5096                         return -1;
5097                 return ret;
5098         }
5099
5100         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5101                                   go_intent, if_addr, force_freq,
5102                                   persistent_group, ssid, pref_freq) < 0) {
5103                 if (wpa_s->create_p2p_iface)
5104                         wpas_p2p_remove_pending_group_interface(wpa_s);
5105                 return -1;
5106         }
5107         return ret;
5108 }
5109
5110
5111 /**
5112  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5113  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5114  * @freq: Frequency of the channel in MHz
5115  * @duration: Duration of the stay on the channel in milliseconds
5116  *
5117  * This callback is called when the driver indicates that it has started the
5118  * requested remain-on-channel duration.
5119  */
5120 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5121                                    unsigned int freq, unsigned int duration)
5122 {
5123         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5124                 return;
5125         wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
5126                    wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5127                    wpa_s->roc_waiting_drv_freq, freq, duration);
5128         if (wpa_s->off_channel_freq &&
5129             wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5130                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5131                               wpa_s->pending_listen_duration);
5132                 wpa_s->pending_listen_freq = 0;
5133         } else {
5134                 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5135                            wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5136                            freq, duration);
5137         }
5138 }
5139
5140
5141 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5142 {
5143         /* Limit maximum Listen state time based on driver limitation. */
5144         if (timeout > wpa_s->max_remain_on_chan)
5145                 timeout = wpa_s->max_remain_on_chan;
5146
5147         return p2p_listen(wpa_s->global->p2p, timeout);
5148 }
5149
5150
5151 /**
5152  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5153  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5154  * @freq: Frequency of the channel in MHz
5155  *
5156  * This callback is called when the driver indicates that a remain-on-channel
5157  * operation has been completed, i.e., the duration on the requested channel
5158  * has timed out.
5159  */
5160 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5161                                           unsigned int freq)
5162 {
5163         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5164                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
5165                    wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5166         wpas_p2p_listen_work_done(wpa_s);
5167         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5168                 return;
5169         if (wpa_s->p2p_long_listen > 0)
5170                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5171         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5172                 return; /* P2P module started a new operation */
5173         if (offchannel_pending_action_tx(wpa_s))
5174                 return;
5175         if (wpa_s->p2p_long_listen > 0) {
5176                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5177                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5178         } else {
5179                 /*
5180                  * When listen duration is over, stop listen & update p2p_state
5181                  * to IDLE.
5182                  */
5183                 p2p_stop_listen(wpa_s->global->p2p);
5184         }
5185 }
5186
5187
5188 /**
5189  * wpas_p2p_group_remove - Remove a P2P group
5190  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5191  * @ifname: Network interface name of the group interface or "*" to remove all
5192  *      groups
5193  * Returns: 0 on success, -1 on failure
5194  *
5195  * This function is used to remove a P2P group. This can be used to disconnect
5196  * from a group in which the local end is a P2P Client or to end a P2P Group in
5197  * case the local end is the Group Owner. If a virtual network interface was
5198  * created for this group, that interface will be removed. Otherwise, only the
5199  * configured P2P group network will be removed from the interface.
5200  */
5201 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5202 {
5203         struct wpa_global *global = wpa_s->global;
5204         struct wpa_supplicant *calling_wpa_s = wpa_s;
5205
5206         if (os_strcmp(ifname, "*") == 0) {
5207                 struct wpa_supplicant *prev;
5208                 wpa_s = global->ifaces;
5209                 while (wpa_s) {
5210                         prev = wpa_s;
5211                         wpa_s = wpa_s->next;
5212                         if (prev->p2p_group_interface !=
5213                             NOT_P2P_GROUP_INTERFACE ||
5214                             (prev->current_ssid &&
5215                              prev->current_ssid->p2p_group))
5216                                 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5217                 }
5218                 return 0;
5219         }
5220
5221         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5222                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5223                         break;
5224         }
5225
5226         return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5227 }
5228
5229
5230 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5231 {
5232         unsigned int r;
5233
5234         if (freq == 2) {
5235                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5236                            "band");
5237                 if (wpa_s->best_24_freq > 0 &&
5238                     p2p_supported_freq_go(wpa_s->global->p2p,
5239                                           wpa_s->best_24_freq)) {
5240                         freq = wpa_s->best_24_freq;
5241                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5242                                    "channel: %d MHz", freq);
5243                 } else {
5244                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5245                                 return -1;
5246                         freq = 2412 + (r % 3) * 25;
5247                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5248                                    "channel: %d MHz", freq);
5249                 }
5250         }
5251
5252         if (freq == 5) {
5253                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5254                            "band");
5255                 if (wpa_s->best_5_freq > 0 &&
5256                     p2p_supported_freq_go(wpa_s->global->p2p,
5257                                        wpa_s->best_5_freq)) {
5258                         freq = wpa_s->best_5_freq;
5259                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5260                                    "channel: %d MHz", freq);
5261                 } else {
5262                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5263                                 return -1;
5264                         freq = 5180 + (r % 4) * 20;
5265                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5266                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5267                                            "5 GHz channel for P2P group");
5268                                 return -1;
5269                         }
5270                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5271                                    "channel: %d MHz", freq);
5272                 }
5273         }
5274
5275         if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5276                 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5277                     ieee80211_is_dfs(freq)) {
5278                         /*
5279                          * If freq is a DFS channel and DFS is offloaded to the
5280                          * driver, allow P2P GO to use it.
5281                          */
5282                         wpa_printf(MSG_DEBUG, "P2P: "
5283                                    "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5284                                    __func__, freq);
5285                         return freq;
5286                 }
5287                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5288                            "(%u MHz) is not supported for P2P uses",
5289                            freq);
5290                 return -1;
5291         }
5292
5293         return freq;
5294 }
5295
5296
5297 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5298                                         struct p2p_go_neg_results *params,
5299                                         const struct p2p_channels *channels)
5300 {
5301         unsigned int i, r;
5302
5303         /* first try some random selection of the social channels */
5304         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5305                 return -1;
5306
5307         for (i = 0; i < 3; i++) {
5308                 params->freq = 2412 + ((r + i) % 3) * 25;
5309                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5310                     freq_included(channels, params->freq) &&
5311                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5312                         goto out;
5313         }
5314
5315         /* try all channels in reg. class 81 */
5316         for (i = 0; i < 11; i++) {
5317                 params->freq = 2412 + i * 5;
5318                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5319                     freq_included(channels, params->freq) &&
5320                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5321                         goto out;
5322         }
5323
5324         /* try all channels in operating class 115 */
5325         for (i = 0; i < 4; i++) {
5326                 params->freq = 5180 + i * 20;
5327                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5328                     freq_included(channels, params->freq) &&
5329                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5330                         goto out;
5331         }
5332
5333         /* try all channels in operating class 124 */
5334         for (i = 0; i < 4; i++) {
5335                 params->freq = 5745 + i * 20;
5336                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5337                     freq_included(channels, params->freq) &&
5338                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5339                         goto out;
5340         }
5341
5342         /* try social channel class 180 channel 2 */
5343         params->freq = 58320 + 1 * 2160;
5344         if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5345             freq_included(channels, params->freq) &&
5346             p2p_supported_freq(wpa_s->global->p2p, params->freq))
5347                 goto out;
5348
5349         /* try all channels in reg. class 180 */
5350         for (i = 0; i < 4; i++) {
5351                 params->freq = 58320 + i * 2160;
5352                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5353                     freq_included(channels, params->freq) &&
5354                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5355                         goto out;
5356         }
5357
5358         wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5359         return -1;
5360 out:
5361         wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5362                    params->freq);
5363         return 0;
5364 }
5365
5366
5367 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5368                                    struct p2p_go_neg_results *params,
5369                                    int freq, int ht40, int vht,
5370                                    const struct p2p_channels *channels)
5371 {
5372         struct wpa_used_freq_data *freqs;
5373         unsigned int pref_freq, cand_freq;
5374         unsigned int num, i;
5375
5376         os_memset(params, 0, sizeof(*params));
5377         params->role_go = 1;
5378         params->ht40 = ht40;
5379         params->vht = vht;
5380         if (freq) {
5381                 if (!freq_included(channels, freq)) {
5382                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5383                                    "accepted", freq);
5384                         return -1;
5385                 }
5386                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5387                            "frequency %d MHz", freq);
5388                 params->freq = freq;
5389         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5390                    wpa_s->conf->p2p_oper_channel >= 1 &&
5391                    wpa_s->conf->p2p_oper_channel <= 11 &&
5392                    freq_included(channels,
5393                                  2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5394                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5395                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5396                            "frequency %d MHz", params->freq);
5397         } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5398                     wpa_s->conf->p2p_oper_reg_class == 116 ||
5399                     wpa_s->conf->p2p_oper_reg_class == 117 ||
5400                     wpa_s->conf->p2p_oper_reg_class == 124 ||
5401                     wpa_s->conf->p2p_oper_reg_class == 125 ||
5402                     wpa_s->conf->p2p_oper_reg_class == 126 ||
5403                     wpa_s->conf->p2p_oper_reg_class == 127) &&
5404                    freq_included(channels,
5405                                  5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5406                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5407                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5408                            "frequency %d MHz", params->freq);
5409         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5410                    wpa_s->best_overall_freq > 0 &&
5411                    p2p_supported_freq_go(wpa_s->global->p2p,
5412                                          wpa_s->best_overall_freq) &&
5413                    freq_included(channels, wpa_s->best_overall_freq)) {
5414                 params->freq = wpa_s->best_overall_freq;
5415                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5416                            "channel %d MHz", params->freq);
5417         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5418                    wpa_s->best_24_freq > 0 &&
5419                    p2p_supported_freq_go(wpa_s->global->p2p,
5420                                          wpa_s->best_24_freq) &&
5421                    freq_included(channels, wpa_s->best_24_freq)) {
5422                 params->freq = wpa_s->best_24_freq;
5423                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5424                            "channel %d MHz", params->freq);
5425         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5426                    wpa_s->best_5_freq > 0 &&
5427                    p2p_supported_freq_go(wpa_s->global->p2p,
5428                                          wpa_s->best_5_freq) &&
5429                    freq_included(channels, wpa_s->best_5_freq)) {
5430                 params->freq = wpa_s->best_5_freq;
5431                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5432                            "channel %d MHz", params->freq);
5433         } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5434                                                   channels))) {
5435                 params->freq = pref_freq;
5436                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5437                            "channels", params->freq);
5438         } else {
5439                 /* no preference, select some channel */
5440                 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5441                         return -1;
5442         }
5443
5444         freqs = os_calloc(wpa_s->num_multichan_concurrent,
5445                           sizeof(struct wpa_used_freq_data));
5446         if (!freqs)
5447                 return -1;
5448
5449         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5450                                         wpa_s->num_multichan_concurrent);
5451
5452         cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5453
5454         /* First try the best used frequency if possible */
5455         if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5456                 params->freq = cand_freq;
5457         } else if (!freq) {
5458                 /* Try any of the used frequencies */
5459                 for (i = 0; i < num; i++) {
5460                         if (freq_included(channels, freqs[i].freq)) {
5461                                 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5462                                            freqs[i].freq);
5463                                 params->freq = freqs[i].freq;
5464                                 break;
5465                         }
5466                 }
5467
5468                 if (i == num) {
5469                         if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5470                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5471                                 os_free(freqs);
5472                                 return -1;
5473                         } else {
5474                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5475                         }
5476                 }
5477         } else {
5478                 for (i = 0; i < num; i++) {
5479                         if (freqs[i].freq == freq)
5480                                 break;
5481                 }
5482
5483                 if (i == num) {
5484                         if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5485                                 if (freq)
5486                                         wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5487                                 os_free(freqs);
5488                                 return -1;
5489                         } else {
5490                                 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5491                         }
5492                 }
5493         }
5494
5495         os_free(freqs);
5496         return 0;
5497 }
5498
5499
5500 static struct wpa_supplicant *
5501 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5502                          int go)
5503 {
5504         struct wpa_supplicant *group_wpa_s;
5505
5506         if (!wpas_p2p_create_iface(wpa_s)) {
5507                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5508                         "operations");
5509                 wpa_s->p2p_first_connection_timeout = 0;
5510                 return wpa_s;
5511         }
5512
5513         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5514                                          WPA_IF_P2P_CLIENT) < 0) {
5515                 wpa_msg_global(wpa_s, MSG_ERROR,
5516                                "P2P: Failed to add group interface");
5517                 return NULL;
5518         }
5519         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5520         if (group_wpa_s == NULL) {
5521                 wpa_msg_global(wpa_s, MSG_ERROR,
5522                                "P2P: Failed to initialize group interface");
5523                 wpas_p2p_remove_pending_group_interface(wpa_s);
5524                 return NULL;
5525         }
5526
5527         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5528                 group_wpa_s->ifname);
5529         group_wpa_s->p2p_first_connection_timeout = 0;
5530         return group_wpa_s;
5531 }
5532
5533
5534 /**
5535  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5536  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5537  * @persistent_group: Whether to create a persistent group
5538  * @freq: Frequency for the group or 0 to indicate no hardcoding
5539  * @ht40: Start GO with 40 MHz channel width
5540  * @vht:  Start GO with VHT support
5541  * Returns: 0 on success, -1 on failure
5542  *
5543  * This function creates a new P2P group with the local end as the Group Owner,
5544  * i.e., without using Group Owner Negotiation.
5545  */
5546 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5547                        int freq, int ht40, int vht)
5548 {
5549         struct p2p_go_neg_results params;
5550
5551         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5552                 return -1;
5553
5554         os_free(wpa_s->global->add_psk);
5555         wpa_s->global->add_psk = NULL;
5556
5557         /* Make sure we are not running find during connection establishment */
5558         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5559         wpas_p2p_stop_find_oper(wpa_s);
5560
5561         freq = wpas_p2p_select_go_freq(wpa_s, freq);
5562         if (freq < 0)
5563                 return -1;
5564
5565         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
5566                 return -1;
5567         if (params.freq &&
5568             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5569                 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5570                     ieee80211_is_dfs(params.freq)) {
5571                         /*
5572                          * If freq is a DFS channel and DFS is offloaded to the
5573                          * driver, allow P2P GO to use it.
5574                          */
5575                         wpa_printf(MSG_DEBUG,
5576                                    "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5577                                 __func__, params.freq);
5578                 } else {
5579                         wpa_printf(MSG_DEBUG,
5580                                    "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5581                                    params.freq);
5582                         return -1;
5583                 }
5584         }
5585         p2p_go_params(wpa_s->global->p2p, &params);
5586         params.persistent_group = persistent_group;
5587
5588         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5589         if (wpa_s == NULL)
5590                 return -1;
5591         wpas_start_wps_go(wpa_s, &params, 0);
5592
5593         return 0;
5594 }
5595
5596
5597 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5598                                  struct wpa_ssid *params, int addr_allocated,
5599                                  int freq)
5600 {
5601         struct wpa_ssid *ssid;
5602
5603         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5604         if (wpa_s == NULL)
5605                 return -1;
5606         wpa_s->p2p_last_4way_hs_fail = NULL;
5607
5608         wpa_supplicant_ap_deinit(wpa_s);
5609
5610         ssid = wpa_config_add_network(wpa_s->conf);
5611         if (ssid == NULL)
5612                 return -1;
5613         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5614         wpa_config_set_network_defaults(ssid);
5615         ssid->temporary = 1;
5616         ssid->proto = WPA_PROTO_RSN;
5617         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5618         ssid->group_cipher = WPA_CIPHER_CCMP;
5619         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5620         ssid->ssid = os_malloc(params->ssid_len);
5621         if (ssid->ssid == NULL) {
5622                 wpa_config_remove_network(wpa_s->conf, ssid->id);
5623                 return -1;
5624         }
5625         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5626         ssid->ssid_len = params->ssid_len;
5627         ssid->p2p_group = 1;
5628         ssid->export_keys = 1;
5629         if (params->psk_set) {
5630                 os_memcpy(ssid->psk, params->psk, 32);
5631                 ssid->psk_set = 1;
5632         }
5633         if (params->passphrase)
5634                 ssid->passphrase = os_strdup(params->passphrase);
5635
5636         wpa_s->show_group_started = 1;
5637         wpa_s->p2p_in_invitation = 1;
5638         wpa_s->p2p_invite_go_freq = freq;
5639
5640         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5641                              NULL);
5642         eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5643                                wpas_p2p_group_formation_timeout,
5644                                wpa_s->parent, NULL);
5645         wpa_supplicant_select_network(wpa_s, ssid);
5646
5647         return 0;
5648 }
5649
5650
5651 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5652                                   struct wpa_ssid *ssid, int addr_allocated,
5653                                   int force_freq, int neg_freq, int ht40,
5654                                   int vht, const struct p2p_channels *channels,
5655                                   int connection_timeout)
5656 {
5657         struct p2p_go_neg_results params;
5658         int go = 0, freq;
5659
5660         if (ssid->disabled != 2 || ssid->ssid == NULL)
5661                 return -1;
5662
5663         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5664             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5665                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5666                            "already running");
5667                 if (go == 0 &&
5668                     eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5669                                          wpa_s->parent, NULL)) {
5670                         /*
5671                          * This can happen if Invitation Response frame was lost
5672                          * and the peer (GO of a persistent group) tries to
5673                          * invite us again. Reschedule the timeout to avoid
5674                          * terminating the wait for the connection too early
5675                          * since we now know that the peer is still trying to
5676                          * invite us instead of having already started the GO.
5677                          */
5678                         wpa_printf(MSG_DEBUG,
5679                                    "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5680                         eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5681                                                wpas_p2p_group_formation_timeout,
5682                                                wpa_s->parent, NULL);
5683                 }
5684                 return 0;
5685         }
5686
5687         os_free(wpa_s->global->add_psk);
5688         wpa_s->global->add_psk = NULL;
5689
5690         /* Make sure we are not running find during connection establishment */
5691         wpas_p2p_stop_find_oper(wpa_s);
5692
5693         wpa_s->p2p_fallback_to_go_neg = 0;
5694
5695         if (ssid->mode == WPAS_MODE_P2P_GO) {
5696                 if (force_freq > 0) {
5697                         freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5698                         if (freq < 0)
5699                                 return -1;
5700                 } else {
5701                         freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5702                         if (freq < 0 ||
5703                             (freq > 0 && !freq_included(channels, freq)))
5704                                 freq = 0;
5705                 }
5706         } else if (ssid->mode == WPAS_MODE_INFRA) {
5707                 freq = neg_freq;
5708                 if (freq <= 0 || !freq_included(channels, freq)) {
5709                         struct os_reltime now;
5710                         struct wpa_bss *bss =
5711                                 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5712
5713                         os_get_reltime(&now);
5714                         if (bss &&
5715                             !os_reltime_expired(&now, &bss->last_update, 5) &&
5716                             freq_included(channels, bss->freq))
5717                                 freq = bss->freq;
5718                         else
5719                                 freq = 0;
5720                 }
5721
5722                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5723         } else {
5724                 return -1;
5725         }
5726
5727         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
5728                 return -1;
5729
5730         params.role_go = 1;
5731         params.psk_set = ssid->psk_set;
5732         if (params.psk_set)
5733                 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5734         if (ssid->passphrase) {
5735                 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5736                         wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5737                                    "persistent group");
5738                         return -1;
5739                 }
5740                 os_strlcpy(params.passphrase, ssid->passphrase,
5741                            sizeof(params.passphrase));
5742         }
5743         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5744         params.ssid_len = ssid->ssid_len;
5745         params.persistent_group = 1;
5746
5747         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5748         if (wpa_s == NULL)
5749                 return -1;
5750
5751         p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5752
5753         wpa_s->p2p_first_connection_timeout = connection_timeout;
5754         wpas_start_wps_go(wpa_s, &params, 0);
5755
5756         return 0;
5757 }
5758
5759
5760 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5761                                struct wpabuf *proberesp_ies)
5762 {
5763         struct wpa_supplicant *wpa_s = ctx;
5764         if (wpa_s->ap_iface) {
5765                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5766                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5767                         wpabuf_free(beacon_ies);
5768                         wpabuf_free(proberesp_ies);
5769                         return;
5770                 }
5771                 if (beacon_ies) {
5772                         wpabuf_free(hapd->p2p_beacon_ie);
5773                         hapd->p2p_beacon_ie = beacon_ies;
5774                 }
5775                 wpabuf_free(hapd->p2p_probe_resp_ie);
5776                 hapd->p2p_probe_resp_ie = proberesp_ies;
5777         } else {
5778                 wpabuf_free(beacon_ies);
5779                 wpabuf_free(proberesp_ies);
5780         }
5781         wpa_supplicant_ap_update_beacon(wpa_s);
5782 }
5783
5784
5785 static void wpas_p2p_idle_update(void *ctx, int idle)
5786 {
5787         struct wpa_supplicant *wpa_s = ctx;
5788         if (!wpa_s->ap_iface)
5789                 return;
5790         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5791         if (idle) {
5792                 if (wpa_s->global->p2p_fail_on_wps_complete &&
5793                     wpa_s->p2p_in_provisioning) {
5794                         wpas_p2p_grpform_fail_after_wps(wpa_s);
5795                         return;
5796                 }
5797                 wpas_p2p_set_group_idle_timeout(wpa_s);
5798         } else
5799                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5800 }
5801
5802
5803 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5804                                        struct wpa_ssid *ssid)
5805 {
5806         struct p2p_group *group;
5807         struct p2p_group_config *cfg;
5808
5809         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5810                 return NULL;
5811
5812         cfg = os_zalloc(sizeof(*cfg));
5813         if (cfg == NULL)
5814                 return NULL;
5815
5816         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5817                 cfg->persistent_group = 2;
5818         else if (ssid->p2p_persistent_group)
5819                 cfg->persistent_group = 1;
5820         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5821         if (wpa_s->max_stations &&
5822             wpa_s->max_stations < wpa_s->conf->max_num_sta)
5823                 cfg->max_clients = wpa_s->max_stations;
5824         else
5825                 cfg->max_clients = wpa_s->conf->max_num_sta;
5826         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5827         cfg->ssid_len = ssid->ssid_len;
5828         cfg->freq = ssid->frequency;
5829         cfg->cb_ctx = wpa_s;
5830         cfg->ie_update = wpas_p2p_ie_update;
5831         cfg->idle_update = wpas_p2p_idle_update;
5832
5833         group = p2p_group_init(wpa_s->global->p2p, cfg);
5834         if (group == NULL)
5835                 os_free(cfg);
5836         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5837                 p2p_group_notif_formation_done(group);
5838         wpa_s->p2p_group = group;
5839         return group;
5840 }
5841
5842
5843 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5844                           int registrar)
5845 {
5846         struct wpa_ssid *ssid = wpa_s->current_ssid;
5847
5848         if (!wpa_s->p2p_in_provisioning) {
5849                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5850                            "provisioning not in progress");
5851                 return;
5852         }
5853
5854         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5855                 u8 go_dev_addr[ETH_ALEN];
5856                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5857                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5858                                           ssid->ssid_len);
5859                 /* Clear any stored provisioning info */
5860                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5861         }
5862
5863         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5864                              NULL);
5865         wpa_s->p2p_go_group_formation_completed = 1;
5866         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5867                 /*
5868                  * Use a separate timeout for initial data connection to
5869                  * complete to allow the group to be removed automatically if
5870                  * something goes wrong in this step before the P2P group idle
5871                  * timeout mechanism is taken into use.
5872                  */
5873                 wpa_dbg(wpa_s, MSG_DEBUG,
5874                         "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5875                         P2P_MAX_INITIAL_CONN_WAIT);
5876                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5877                                        wpas_p2p_group_formation_timeout,
5878                                        wpa_s->parent, NULL);
5879         } else if (ssid) {
5880                 /*
5881                  * Use a separate timeout for initial data connection to
5882                  * complete to allow the group to be removed automatically if
5883                  * the client does not complete data connection successfully.
5884                  */
5885                 wpa_dbg(wpa_s, MSG_DEBUG,
5886                         "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5887                         P2P_MAX_INITIAL_CONN_WAIT_GO);
5888                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5889                                        wpas_p2p_group_formation_timeout,
5890                                        wpa_s->parent, NULL);
5891                 /*
5892                  * Complete group formation on first successful data connection
5893                  */
5894                 wpa_s->p2p_go_group_formation_completed = 0;
5895         }
5896         if (wpa_s->global->p2p)
5897                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5898         wpas_group_formation_completed(wpa_s, 1, 0);
5899 }
5900
5901
5902 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5903                          struct wps_event_fail *fail)
5904 {
5905         if (!wpa_s->p2p_in_provisioning) {
5906                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5907                            "provisioning not in progress");
5908                 return;
5909         }
5910
5911         if (wpa_s->go_params) {
5912                 p2p_clear_provisioning_info(
5913                         wpa_s->global->p2p,
5914                         wpa_s->go_params->peer_device_addr);
5915         }
5916
5917         wpas_notify_p2p_wps_failed(wpa_s, fail);
5918
5919         if (wpa_s == wpa_s->global->p2p_group_formation) {
5920                 /*
5921                  * Allow some time for the failed WPS negotiation exchange to
5922                  * complete, but remove the group since group formation cannot
5923                  * succeed after provisioning failure.
5924                  */
5925                 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5926                 wpa_s->global->p2p_fail_on_wps_complete = 1;
5927                 eloop_deplete_timeout(0, 50000,
5928                                       wpas_p2p_group_formation_timeout,
5929                                       wpa_s->parent, NULL);
5930         }
5931 }
5932
5933
5934 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5935 {
5936         if (!wpa_s->global->p2p_fail_on_wps_complete ||
5937             !wpa_s->p2p_in_provisioning)
5938                 return 0;
5939
5940         wpas_p2p_grpform_fail_after_wps(wpa_s);
5941
5942         return 1;
5943 }
5944
5945
5946 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5947                        const char *config_method,
5948                        enum wpas_p2p_prov_disc_use use,
5949                        struct p2ps_provision *p2ps_prov)
5950 {
5951         u16 config_methods;
5952
5953         wpa_s->global->pending_p2ps_group = 0;
5954         wpa_s->p2p_fallback_to_go_neg = 0;
5955         wpa_s->pending_pd_use = NORMAL_PD;
5956         if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5957                 p2ps_prov->conncap = p2ps_group_capability(
5958                         wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5959                 wpa_printf(MSG_DEBUG,
5960                            "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5961                            __func__, p2ps_prov->conncap,
5962                            p2ps_prov->adv_id, p2ps_prov->conncap,
5963                            p2ps_prov->status, p2ps_prov->info);
5964
5965                 config_methods = 0;
5966         } else if (os_strncmp(config_method, "display", 7) == 0)
5967                 config_methods = WPS_CONFIG_DISPLAY;
5968         else if (os_strncmp(config_method, "keypad", 6) == 0)
5969                 config_methods = WPS_CONFIG_KEYPAD;
5970         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5971                  os_strncmp(config_method, "pushbutton", 10) == 0)
5972                 config_methods = WPS_CONFIG_PUSHBUTTON;
5973         else {
5974                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5975                 os_free(p2ps_prov);
5976                 return -1;
5977         }
5978
5979         if (use == WPAS_P2P_PD_AUTO) {
5980                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5981                 wpa_s->pending_pd_config_methods = config_methods;
5982                 wpa_s->p2p_auto_pd = 1;
5983                 wpa_s->p2p_auto_join = 0;
5984                 wpa_s->pending_pd_before_join = 0;
5985                 wpa_s->auto_pd_scan_retry = 0;
5986                 wpas_p2p_stop_find(wpa_s);
5987                 wpa_s->p2p_join_scan_count = 0;
5988                 os_get_reltime(&wpa_s->p2p_auto_started);
5989                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5990                            wpa_s->p2p_auto_started.sec,
5991                            wpa_s->p2p_auto_started.usec);
5992                 wpas_p2p_join_scan(wpa_s, NULL);
5993                 return 0;
5994         }
5995
5996         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
5997                 os_free(p2ps_prov);
5998                 return -1;
5999         }
6000
6001         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6002                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6003                                  0, 1);
6004 }
6005
6006
6007 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6008                               char *end)
6009 {
6010         return p2p_scan_result_text(ies, ies_len, buf, end);
6011 }
6012
6013
6014 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6015 {
6016         if (!offchannel_pending_action_tx(wpa_s))
6017                 return;
6018
6019         wpas_p2p_action_tx_clear(wpa_s);
6020
6021         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6022                    "operation request");
6023         offchannel_clear_pending_action_tx(wpa_s);
6024 }
6025
6026
6027 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6028                   enum p2p_discovery_type type,
6029                   unsigned int num_req_dev_types, const u8 *req_dev_types,
6030                   const u8 *dev_id, unsigned int search_delay,
6031                   u8 seek_cnt, const char **seek_string, int freq)
6032 {
6033         wpas_p2p_clear_pending_action_tx(wpa_s);
6034         wpa_s->p2p_long_listen = 0;
6035
6036         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6037             wpa_s->p2p_in_provisioning)
6038                 return -1;
6039
6040         wpa_supplicant_cancel_sched_scan(wpa_s);
6041
6042         return p2p_find(wpa_s->global->p2p, timeout, type,
6043                         num_req_dev_types, req_dev_types, dev_id,
6044                         search_delay, seek_cnt, seek_string, freq);
6045 }
6046
6047
6048 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6049                                             struct wpa_scan_results *scan_res)
6050 {
6051         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6052
6053         if (wpa_s->p2p_scan_work) {
6054                 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6055                 wpa_s->p2p_scan_work = NULL;
6056                 radio_work_done(work);
6057         }
6058
6059         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6060                 return;
6061
6062         /*
6063          * Indicate that results have been processed so that the P2P module can
6064          * continue pending tasks.
6065          */
6066         p2p_scan_res_handled(wpa_s->global->p2p);
6067 }
6068
6069
6070 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6071 {
6072         wpas_p2p_clear_pending_action_tx(wpa_s);
6073         wpa_s->p2p_long_listen = 0;
6074         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6075         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6076
6077         if (wpa_s->global->p2p)
6078                 p2p_stop_find(wpa_s->global->p2p);
6079
6080         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6081                 wpa_printf(MSG_DEBUG,
6082                            "P2P: Do not consider the scan results after stop_find");
6083                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6084         }
6085 }
6086
6087
6088 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6089 {
6090         wpas_p2p_stop_find_oper(wpa_s);
6091         if (!wpa_s->global->pending_group_iface_for_p2ps)
6092                 wpas_p2p_remove_pending_group_interface(wpa_s);
6093 }
6094
6095
6096 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6097 {
6098         struct wpa_supplicant *wpa_s = eloop_ctx;
6099         wpa_s->p2p_long_listen = 0;
6100 }
6101
6102
6103 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6104 {
6105         int res;
6106
6107         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6108                 return -1;
6109
6110         wpa_supplicant_cancel_sched_scan(wpa_s);
6111         wpas_p2p_clear_pending_action_tx(wpa_s);
6112
6113         if (timeout == 0) {
6114                 /*
6115                  * This is a request for unlimited Listen state. However, at
6116                  * least for now, this is mapped to a Listen state for one
6117                  * hour.
6118                  */
6119                 timeout = 3600;
6120         }
6121         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6122         wpa_s->p2p_long_listen = 0;
6123
6124         /*
6125          * Stop previous find/listen operation to avoid trying to request a new
6126          * remain-on-channel operation while the driver is still running the
6127          * previous one.
6128          */
6129         if (wpa_s->global->p2p)
6130                 p2p_stop_find(wpa_s->global->p2p);
6131
6132         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6133         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6134                 wpa_s->p2p_long_listen = timeout * 1000;
6135                 eloop_register_timeout(timeout, 0,
6136                                        wpas_p2p_long_listen_timeout,
6137                                        wpa_s, NULL);
6138         }
6139
6140         return res;
6141 }
6142
6143
6144 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6145                           u8 *buf, size_t len, int p2p_group)
6146 {
6147         struct wpabuf *p2p_ie;
6148         int ret;
6149
6150         if (wpa_s->global->p2p_disabled)
6151                 return -1;
6152         /*
6153          * Advertize mandatory cross connection capability even on
6154          * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6155          */
6156         if (wpa_s->conf->p2p_disabled && p2p_group)
6157                 return -1;
6158         if (wpa_s->global->p2p == NULL)
6159                 return -1;
6160         if (bss == NULL)
6161                 return -1;
6162
6163         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6164         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6165                                p2p_group, p2p_ie);
6166         wpabuf_free(p2p_ie);
6167
6168         return ret;
6169 }
6170
6171
6172 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6173                           const u8 *dst, const u8 *bssid,
6174                           const u8 *ie, size_t ie_len,
6175                           unsigned int rx_freq, int ssi_signal)
6176 {
6177         if (wpa_s->global->p2p_disabled)
6178                 return 0;
6179         if (wpa_s->global->p2p == NULL)
6180                 return 0;
6181
6182         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6183                                  ie, ie_len, rx_freq)) {
6184         case P2P_PREQ_NOT_P2P:
6185                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6186                                  ssi_signal);
6187                 /* fall through */
6188         case P2P_PREQ_MALFORMED:
6189         case P2P_PREQ_NOT_LISTEN:
6190         case P2P_PREQ_NOT_PROCESSED:
6191         default: /* make gcc happy */
6192                 return 0;
6193         case P2P_PREQ_PROCESSED:
6194                 return 1;
6195         }
6196 }
6197
6198
6199 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6200                         const u8 *sa, const u8 *bssid,
6201                         u8 category, const u8 *data, size_t len, int freq)
6202 {
6203         if (wpa_s->global->p2p_disabled)
6204                 return;
6205         if (wpa_s->global->p2p == NULL)
6206                 return;
6207
6208         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6209                       freq);
6210 }
6211
6212
6213 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6214 {
6215         if (wpa_s->global->p2p_disabled)
6216                 return;
6217         if (wpa_s->global->p2p == NULL)
6218                 return;
6219
6220         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6221 }
6222
6223
6224 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6225 {
6226         p2p_group_deinit(wpa_s->p2p_group);
6227         wpa_s->p2p_group = NULL;
6228
6229         wpa_s->ap_configured_cb = NULL;
6230         wpa_s->ap_configured_cb_ctx = NULL;
6231         wpa_s->ap_configured_cb_data = NULL;
6232         wpa_s->connect_without_scan = NULL;
6233 }
6234
6235
6236 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6237 {
6238         wpa_s->p2p_long_listen = 0;
6239
6240         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6241                 return -1;
6242
6243         return p2p_reject(wpa_s->global->p2p, addr);
6244 }
6245
6246
6247 /* Invite to reinvoke a persistent group */
6248 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6249                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6250                     int ht40, int vht, int pref_freq)
6251 {
6252         enum p2p_invite_role role;
6253         u8 *bssid = NULL;
6254         int force_freq = 0;
6255         int res;
6256         int no_pref_freq_given = pref_freq == 0;
6257         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6258
6259         wpa_s->global->p2p_invite_group = NULL;
6260         if (peer_addr)
6261                 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6262         else
6263                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6264
6265         wpa_s->p2p_persistent_go_freq = freq;
6266         wpa_s->p2p_go_ht40 = !!ht40;
6267         if (ssid->mode == WPAS_MODE_P2P_GO) {
6268                 role = P2P_INVITE_ROLE_GO;
6269                 if (peer_addr == NULL) {
6270                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6271                                    "address in invitation command");
6272                         return -1;
6273                 }
6274                 if (wpas_p2p_create_iface(wpa_s)) {
6275                         if (wpas_p2p_add_group_interface(wpa_s,
6276                                                          WPA_IF_P2P_GO) < 0) {
6277                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
6278                                            "allocate a new interface for the "
6279                                            "group");
6280                                 return -1;
6281                         }
6282                         bssid = wpa_s->pending_interface_addr;
6283                 } else
6284                         bssid = wpa_s->own_addr;
6285         } else {
6286                 role = P2P_INVITE_ROLE_CLIENT;
6287                 peer_addr = ssid->bssid;
6288         }
6289         wpa_s->pending_invite_ssid_id = ssid->id;
6290
6291         size = P2P_MAX_PREF_CHANNELS;
6292         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6293                                    role == P2P_INVITE_ROLE_GO,
6294                                    pref_freq_list, &size);
6295         if (res)
6296                 return res;
6297
6298         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6299                 return -1;
6300
6301         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6302             no_pref_freq_given && pref_freq > 0 &&
6303             wpa_s->num_multichan_concurrent > 1 &&
6304             wpas_p2p_num_unused_channels(wpa_s) > 0) {
6305                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6306                            pref_freq);
6307                 pref_freq = 0;
6308         }
6309
6310         /*
6311          * Stop any find/listen operations before invitation and possibly
6312          * connection establishment.
6313          */
6314         wpas_p2p_stop_find_oper(wpa_s);
6315
6316         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6317                           ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6318                           1, pref_freq, -1);
6319 }
6320
6321
6322 /* Invite to join an active group */
6323 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6324                           const u8 *peer_addr, const u8 *go_dev_addr)
6325 {
6326         struct wpa_global *global = wpa_s->global;
6327         enum p2p_invite_role role;
6328         u8 *bssid = NULL;
6329         struct wpa_ssid *ssid;
6330         int persistent;
6331         int freq = 0, force_freq = 0, pref_freq = 0;
6332         int res;
6333         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6334
6335         wpa_s->p2p_persistent_go_freq = 0;
6336         wpa_s->p2p_go_ht40 = 0;
6337         wpa_s->p2p_go_vht = 0;
6338
6339         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6340                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6341                         break;
6342         }
6343         if (wpa_s == NULL) {
6344                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6345                 return -1;
6346         }
6347
6348         ssid = wpa_s->current_ssid;
6349         if (ssid == NULL) {
6350                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6351                            "invitation");
6352                 return -1;
6353         }
6354
6355         wpa_s->global->p2p_invite_group = wpa_s;
6356         persistent = ssid->p2p_persistent_group &&
6357                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6358                                         ssid->ssid, ssid->ssid_len);
6359
6360         if (ssid->mode == WPAS_MODE_P2P_GO) {
6361                 role = P2P_INVITE_ROLE_ACTIVE_GO;
6362                 bssid = wpa_s->own_addr;
6363                 if (go_dev_addr == NULL)
6364                         go_dev_addr = wpa_s->global->p2p_dev_addr;
6365                 freq = ssid->frequency;
6366         } else {
6367                 role = P2P_INVITE_ROLE_CLIENT;
6368                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6369                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6370                                    "invite to current group");
6371                         return -1;
6372                 }
6373                 bssid = wpa_s->bssid;
6374                 if (go_dev_addr == NULL &&
6375                     !is_zero_ether_addr(wpa_s->go_dev_addr))
6376                         go_dev_addr = wpa_s->go_dev_addr;
6377                 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6378                         (int) wpa_s->assoc_freq;
6379         }
6380         wpa_s->parent->pending_invite_ssid_id = -1;
6381
6382         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6383                 return -1;
6384
6385         size = P2P_MAX_PREF_CHANNELS;
6386         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6387                                    role == P2P_INVITE_ROLE_ACTIVE_GO,
6388                                    pref_freq_list, &size);
6389         if (res)
6390                 return res;
6391         wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6392
6393         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6394                           ssid->ssid, ssid->ssid_len, force_freq,
6395                           go_dev_addr, persistent, pref_freq, -1);
6396 }
6397
6398
6399 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6400 {
6401         struct wpa_ssid *ssid = wpa_s->current_ssid;
6402         u8 go_dev_addr[ETH_ALEN];
6403         int network_id = -1;
6404         int persistent;
6405         int freq;
6406         u8 ip[3 * 4];
6407         char ip_addr[100];
6408
6409         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6410                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6411                                      wpa_s->parent, NULL);
6412         }
6413
6414         if (!wpa_s->show_group_started || !ssid)
6415                 return;
6416
6417         wpa_s->show_group_started = 0;
6418
6419         os_memset(go_dev_addr, 0, ETH_ALEN);
6420         if (ssid->bssid_set)
6421                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6422         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6423                                                ssid->ssid_len);
6424         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6425
6426         if (wpa_s->global->p2p_group_formation == wpa_s)
6427                 wpa_s->global->p2p_group_formation = NULL;
6428
6429         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6430                 (int) wpa_s->assoc_freq;
6431
6432         ip_addr[0] = '\0';
6433         if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6434                 int res;
6435
6436                 res = os_snprintf(ip_addr, sizeof(ip_addr),
6437                                   " ip_addr=%u.%u.%u.%u "
6438                                   "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6439                                   ip[0], ip[1], ip[2], ip[3],
6440                                   ip[4], ip[5], ip[6], ip[7],
6441                                   ip[8], ip[9], ip[10], ip[11]);
6442                 if (os_snprintf_error(sizeof(ip_addr), res))
6443                         ip_addr[0] = '\0';
6444         }
6445
6446         wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6447                                ssid->passphrase == NULL && ssid->psk_set ?
6448                                ssid->psk : NULL,
6449                                ssid->passphrase, go_dev_addr, persistent,
6450                                ip_addr);
6451
6452         if (persistent)
6453                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6454                                                              ssid, go_dev_addr);
6455         if (network_id < 0)
6456                 network_id = ssid->id;
6457         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6458 }
6459
6460
6461 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6462                           u32 interval1, u32 duration2, u32 interval2)
6463 {
6464         int ret;
6465
6466         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6467                 return -1;
6468
6469         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6470             wpa_s->current_ssid == NULL ||
6471             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6472                 return -1;
6473
6474         ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6475                                wpa_s->own_addr, wpa_s->assoc_freq,
6476                                duration1, interval1, duration2, interval2);
6477         if (ret == 0)
6478                 wpa_s->waiting_presence_resp = 1;
6479
6480         return ret;
6481 }
6482
6483
6484 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6485                         unsigned int interval)
6486 {
6487         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6488                 return -1;
6489
6490         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6491 }
6492
6493
6494 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6495 {
6496         if (wpa_s->current_ssid == NULL) {
6497                 /*
6498                  * current_ssid can be cleared when P2P client interface gets
6499                  * disconnected, so assume this interface was used as P2P
6500                  * client.
6501                  */
6502                 return 1;
6503         }
6504         return wpa_s->current_ssid->p2p_group &&
6505                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6506 }
6507
6508
6509 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6510 {
6511         struct wpa_supplicant *wpa_s = eloop_ctx;
6512
6513         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6514                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6515                            "disabled");
6516                 return;
6517         }
6518
6519         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6520                    "group");
6521         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6522 }
6523
6524
6525 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6526 {
6527         int timeout;
6528
6529         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6530                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6531
6532         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6533                 return;
6534
6535         timeout = wpa_s->conf->p2p_group_idle;
6536         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6537             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6538             timeout = P2P_MAX_CLIENT_IDLE;
6539
6540         if (timeout == 0)
6541                 return;
6542
6543         if (timeout < 0) {
6544                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6545                         timeout = 0; /* special client mode no-timeout */
6546                 else
6547                         return;
6548         }
6549
6550         if (wpa_s->p2p_in_provisioning) {
6551                 /*
6552                  * Use the normal group formation timeout during the
6553                  * provisioning phase to avoid terminating this process too
6554                  * early due to group idle timeout.
6555                  */
6556                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6557                            "during provisioning");
6558                 return;
6559         }
6560
6561         if (wpa_s->show_group_started) {
6562                 /*
6563                  * Use the normal group formation timeout between the end of
6564                  * the provisioning phase and completion of 4-way handshake to
6565                  * avoid terminating this process too early due to group idle
6566                  * timeout.
6567                  */
6568                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6569                            "while waiting for initial 4-way handshake to "
6570                            "complete");
6571                 return;
6572         }
6573
6574         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6575                    timeout);
6576         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6577                                wpa_s, NULL);
6578 }
6579
6580
6581 /* Returns 1 if the interface was removed */
6582 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6583                           u16 reason_code, const u8 *ie, size_t ie_len,
6584                           int locally_generated)
6585 {
6586         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6587                 return 0;
6588
6589         if (!locally_generated)
6590                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6591                                  ie_len);
6592
6593         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6594             wpa_s->current_ssid &&
6595             wpa_s->current_ssid->p2p_group &&
6596             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6597                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6598                            "session is ending");
6599                 if (wpas_p2p_group_delete(wpa_s,
6600                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6601                     > 0)
6602                         return 1;
6603         }
6604
6605         return 0;
6606 }
6607
6608
6609 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6610                              u16 reason_code, const u8 *ie, size_t ie_len,
6611                              int locally_generated)
6612 {
6613         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6614                 return;
6615
6616         if (!locally_generated)
6617                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6618                                    ie_len);
6619 }
6620
6621
6622 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6623 {
6624         struct p2p_data *p2p = wpa_s->global->p2p;
6625
6626         if (p2p == NULL)
6627                 return;
6628
6629         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6630                 return;
6631
6632         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6633                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6634
6635         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6636                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6637
6638         if (wpa_s->wps &&
6639             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6640                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6641
6642         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6643                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6644
6645         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6646                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6647                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6648                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6649                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6650         }
6651
6652         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6653                 p2p_set_sec_dev_types(p2p,
6654                                       (void *) wpa_s->conf->sec_device_type,
6655                                       wpa_s->conf->num_sec_device_types);
6656
6657         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6658                 int i;
6659                 p2p_remove_wps_vendor_extensions(p2p);
6660                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6661                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6662                                 continue;
6663                         p2p_add_wps_vendor_extension(
6664                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
6665                 }
6666         }
6667
6668         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6669             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6670                 char country[3];
6671                 country[0] = wpa_s->conf->country[0];
6672                 country[1] = wpa_s->conf->country[1];
6673                 country[2] = 0x04;
6674                 p2p_set_country(p2p, country);
6675         }
6676
6677         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6678                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6679                                      wpa_s->conf->p2p_ssid_postfix ?
6680                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6681                                      0);
6682         }
6683
6684         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6685                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6686
6687         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6688                 u8 reg_class, channel;
6689                 int ret;
6690                 unsigned int r;
6691                 u8 channel_forced;
6692
6693                 if (wpa_s->conf->p2p_listen_reg_class &&
6694                     wpa_s->conf->p2p_listen_channel) {
6695                         reg_class = wpa_s->conf->p2p_listen_reg_class;
6696                         channel = wpa_s->conf->p2p_listen_channel;
6697                         channel_forced = 1;
6698                 } else {
6699                         reg_class = 81;
6700                         /*
6701                          * Pick one of the social channels randomly as the
6702                          * listen channel.
6703                          */
6704                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6705                                 channel = 1;
6706                         else
6707                                 channel = 1 + (r % 3) * 5;
6708                         channel_forced = 0;
6709                 }
6710                 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6711                                              channel_forced);
6712                 if (ret)
6713                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6714                                    "failed: %d", ret);
6715         }
6716         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6717                 u8 op_reg_class, op_channel, cfg_op_channel;
6718                 int ret = 0;
6719                 unsigned int r;
6720                 if (wpa_s->conf->p2p_oper_reg_class &&
6721                     wpa_s->conf->p2p_oper_channel) {
6722                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6723                         op_channel = wpa_s->conf->p2p_oper_channel;
6724                         cfg_op_channel = 1;
6725                 } else {
6726                         op_reg_class = 81;
6727                         /*
6728                          * Use random operation channel from (1, 6, 11)
6729                          *if no other preference is indicated.
6730                          */
6731                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6732                                 op_channel = 1;
6733                         else
6734                                 op_channel = 1 + (r % 3) * 5;
6735                         cfg_op_channel = 0;
6736                 }
6737                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6738                                            cfg_op_channel);
6739                 if (ret)
6740                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6741                                    "failed: %d", ret);
6742         }
6743
6744         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6745                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6746                                       wpa_s->conf->p2p_pref_chan) < 0) {
6747                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6748                                    "update failed");
6749                 }
6750
6751                 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6752                         wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6753                                    "update failed");
6754                 }
6755         }
6756
6757         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6758                 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6759 }
6760
6761
6762 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6763                      int duration)
6764 {
6765         if (!wpa_s->ap_iface)
6766                 return -1;
6767         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6768                                    duration);
6769 }
6770
6771
6772 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6773 {
6774         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6775                 return -1;
6776
6777         wpa_s->global->cross_connection = enabled;
6778         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6779
6780         if (!enabled) {
6781                 struct wpa_supplicant *iface;
6782
6783                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6784                 {
6785                         if (iface->cross_connect_enabled == 0)
6786                                 continue;
6787
6788                         iface->cross_connect_enabled = 0;
6789                         iface->cross_connect_in_use = 0;
6790                         wpa_msg_global(iface->parent, MSG_INFO,
6791                                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6792                                        iface->ifname,
6793                                        iface->cross_connect_uplink);
6794                 }
6795         }
6796
6797         return 0;
6798 }
6799
6800
6801 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6802 {
6803         struct wpa_supplicant *iface;
6804
6805         if (!uplink->global->cross_connection)
6806                 return;
6807
6808         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6809                 if (!iface->cross_connect_enabled)
6810                         continue;
6811                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6812                     0)
6813                         continue;
6814                 if (iface->ap_iface == NULL)
6815                         continue;
6816                 if (iface->cross_connect_in_use)
6817                         continue;
6818
6819                 iface->cross_connect_in_use = 1;
6820                 wpa_msg_global(iface->parent, MSG_INFO,
6821                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6822                                iface->ifname, iface->cross_connect_uplink);
6823         }
6824 }
6825
6826
6827 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6828 {
6829         struct wpa_supplicant *iface;
6830
6831         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6832                 if (!iface->cross_connect_enabled)
6833                         continue;
6834                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6835                     0)
6836                         continue;
6837                 if (!iface->cross_connect_in_use)
6838                         continue;
6839
6840                 wpa_msg_global(iface->parent, MSG_INFO,
6841                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6842                                iface->ifname, iface->cross_connect_uplink);
6843                 iface->cross_connect_in_use = 0;
6844         }
6845 }
6846
6847
6848 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6849 {
6850         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6851             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6852             wpa_s->cross_connect_disallowed)
6853                 wpas_p2p_disable_cross_connect(wpa_s);
6854         else
6855                 wpas_p2p_enable_cross_connect(wpa_s);
6856         if (!wpa_s->ap_iface &&
6857             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6858                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6859 }
6860
6861
6862 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6863 {
6864         wpas_p2p_disable_cross_connect(wpa_s);
6865         if (!wpa_s->ap_iface &&
6866             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6867                                          wpa_s, NULL))
6868                 wpas_p2p_set_group_idle_timeout(wpa_s);
6869 }
6870
6871
6872 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6873 {
6874         struct wpa_supplicant *iface;
6875
6876         if (!wpa_s->global->cross_connection)
6877                 return;
6878
6879         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6880                 if (iface == wpa_s)
6881                         continue;
6882                 if (iface->drv_flags &
6883                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6884                         continue;
6885                 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6886                     iface != wpa_s->parent)
6887                         continue;
6888
6889                 wpa_s->cross_connect_enabled = 1;
6890                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6891                            sizeof(wpa_s->cross_connect_uplink));
6892                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6893                            "%s to %s whenever uplink is available",
6894                            wpa_s->ifname, wpa_s->cross_connect_uplink);
6895
6896                 if (iface->ap_iface || iface->current_ssid == NULL ||
6897                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
6898                     iface->cross_connect_disallowed ||
6899                     iface->wpa_state != WPA_COMPLETED)
6900                         break;
6901
6902                 wpa_s->cross_connect_in_use = 1;
6903                 wpa_msg_global(wpa_s->parent, MSG_INFO,
6904                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6905                                wpa_s->ifname, wpa_s->cross_connect_uplink);
6906                 break;
6907         }
6908 }
6909
6910
6911 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6912 {
6913         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6914             !wpa_s->p2p_in_provisioning)
6915                 return 0; /* not P2P client operation */
6916
6917         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6918                    "session overlap");
6919         if (wpa_s != wpa_s->parent)
6920                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6921         wpas_p2p_group_formation_failed(wpa_s, 0);
6922         return 1;
6923 }
6924
6925
6926 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6927 {
6928         struct wpa_supplicant *wpa_s = eloop_ctx;
6929         wpas_p2p_notif_pbc_overlap(wpa_s);
6930 }
6931
6932
6933 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6934 {
6935         struct p2p_channels chan, cli_chan;
6936         struct wpa_supplicant *ifs;
6937
6938         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6939                 return;
6940
6941         os_memset(&chan, 0, sizeof(chan));
6942         os_memset(&cli_chan, 0, sizeof(cli_chan));
6943         if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6944                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6945                            "channel list");
6946                 return;
6947         }
6948
6949         p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6950
6951         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6952                 int freq;
6953                 if (!ifs->current_ssid ||
6954                     !ifs->current_ssid->p2p_group ||
6955                     (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6956                      ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6957                                 continue;
6958                 freq = ifs->current_ssid->frequency;
6959                 if (freq_included(&chan, freq)) {
6960                         wpa_dbg(ifs, MSG_DEBUG,
6961                                 "P2P GO operating frequency %d MHz in valid range",
6962                                 freq);
6963                         continue;
6964                 }
6965
6966                 wpa_dbg(ifs, MSG_DEBUG,
6967                         "P2P GO operating in invalid frequency %d MHz", freq);
6968                 /* TODO: Consider using CSA or removing the group within
6969                  * wpa_supplicant */
6970                 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6971         }
6972 }
6973
6974
6975 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6976                                      struct wpa_scan_results *scan_res)
6977 {
6978         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6979 }
6980
6981
6982 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6983 {
6984         struct wpa_global *global = wpa_s->global;
6985         int found = 0;
6986         const u8 *peer;
6987
6988         if (global->p2p == NULL)
6989                 return -1;
6990
6991         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6992
6993         if (wpa_s->pending_interface_name[0] &&
6994             !is_zero_ether_addr(wpa_s->pending_interface_addr))
6995                 found = 1;
6996
6997         peer = p2p_get_go_neg_peer(global->p2p);
6998         if (peer) {
6999                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7000                            MACSTR, MAC2STR(peer));
7001                 p2p_unauthorize(global->p2p, peer);
7002                 found = 1;
7003         }
7004
7005         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7006                 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7007                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7008                 found = 1;
7009         }
7010
7011         if (wpa_s->pending_pd_before_join) {
7012                 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7013                 wpa_s->pending_pd_before_join = 0;
7014                 found = 1;
7015         }
7016
7017         wpas_p2p_stop_find(wpa_s);
7018
7019         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7020                 if (wpa_s == global->p2p_group_formation &&
7021                     (wpa_s->p2p_in_provisioning ||
7022                      wpa_s->parent->pending_interface_type ==
7023                      WPA_IF_P2P_CLIENT)) {
7024                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7025                                    "formation found - cancelling",
7026                                    wpa_s->ifname);
7027                         found = 1;
7028                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7029                                              wpa_s->parent, NULL);
7030                         if (wpa_s->p2p_in_provisioning) {
7031                                 wpas_group_formation_completed(wpa_s, 0, 0);
7032                                 break;
7033                         }
7034                         wpas_p2p_group_delete(wpa_s,
7035                                               P2P_GROUP_REMOVAL_REQUESTED);
7036                         break;
7037                 } else if (wpa_s->p2p_in_invitation) {
7038                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7039                                    wpa_s->ifname);
7040                         found = 1;
7041                         wpas_p2p_group_formation_failed(wpa_s, 0);
7042                 }
7043         }
7044
7045         if (!found) {
7046                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7047                 return -1;
7048         }
7049
7050         return 0;
7051 }
7052
7053
7054 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7055 {
7056         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7057                 return;
7058
7059         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7060                    "being available anymore");
7061         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7062 }
7063
7064
7065 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7066                                    int freq_24, int freq_5, int freq_overall)
7067 {
7068         struct p2p_data *p2p = wpa_s->global->p2p;
7069         if (p2p == NULL)
7070                 return;
7071         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7072 }
7073
7074
7075 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7076 {
7077         u8 peer[ETH_ALEN];
7078         struct p2p_data *p2p = wpa_s->global->p2p;
7079
7080         if (p2p == NULL)
7081                 return -1;
7082
7083         if (hwaddr_aton(addr, peer))
7084                 return -1;
7085
7086         return p2p_unauthorize(p2p, peer);
7087 }
7088
7089
7090 /**
7091  * wpas_p2p_disconnect - Disconnect from a P2P Group
7092  * @wpa_s: Pointer to wpa_supplicant data
7093  * Returns: 0 on success, -1 on failure
7094  *
7095  * This can be used to disconnect from a group in which the local end is a P2P
7096  * Client or to end a P2P Group in case the local end is the Group Owner. If a
7097  * virtual network interface was created for this group, that interface will be
7098  * removed. Otherwise, only the configured P2P group network will be removed
7099  * from the interface.
7100  */
7101 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7102 {
7103
7104         if (wpa_s == NULL)
7105                 return -1;
7106
7107         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7108                 -1 : 0;
7109 }
7110
7111
7112 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7113 {
7114         int ret;
7115
7116         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7117                 return 0;
7118
7119         ret = p2p_in_progress(wpa_s->global->p2p);
7120         if (ret == 0) {
7121                 /*
7122                  * Check whether there is an ongoing WPS provisioning step (or
7123                  * other parts of group formation) on another interface since
7124                  * p2p_in_progress() does not report this to avoid issues for
7125                  * scans during such provisioning step.
7126                  */
7127                 if (wpa_s->global->p2p_group_formation &&
7128                     wpa_s->global->p2p_group_formation != wpa_s) {
7129                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7130                                 "in group formation",
7131                                 wpa_s->global->p2p_group_formation->ifname);
7132                         ret = 1;
7133                 }
7134         }
7135
7136         if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7137                 struct os_reltime now;
7138                 os_get_reltime(&now);
7139                 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7140                                        P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7141                         /* Wait for the first client has expired */
7142                         wpa_s->global->p2p_go_wait_client.sec = 0;
7143                 } else {
7144                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7145                         ret = 1;
7146                 }
7147         }
7148
7149         return ret;
7150 }
7151
7152
7153 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7154                               struct wpa_ssid *ssid)
7155 {
7156         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7157             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7158                                  wpa_s->parent, NULL) > 0) {
7159                 /**
7160                  * Remove the network by scheduling the group formation
7161                  * timeout to happen immediately. The teardown code
7162                  * needs to be scheduled to run asynch later so that we
7163                  * don't delete data from under ourselves unexpectedly.
7164                  * Calling wpas_p2p_group_formation_timeout directly
7165                  * causes a series of crashes in WPS failure scenarios.
7166                  */
7167                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7168                            "P2P group network getting removed");
7169                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7170                                        wpa_s->parent, NULL);
7171         }
7172 }
7173
7174
7175 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7176                                           const u8 *addr, const u8 *ssid,
7177                                           size_t ssid_len)
7178 {
7179         struct wpa_ssid *s;
7180         size_t i;
7181
7182         for (s = wpa_s->conf->ssid; s; s = s->next) {
7183                 if (s->disabled != 2)
7184                         continue;
7185                 if (ssid &&
7186                     (ssid_len != s->ssid_len ||
7187                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
7188                         continue;
7189                 if (addr == NULL) {
7190                         if (s->mode == WPAS_MODE_P2P_GO)
7191                                 return s;
7192                         continue;
7193                 }
7194                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7195                         return s; /* peer is GO in the persistent group */
7196                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7197                         continue;
7198                 for (i = 0; i < s->num_p2p_clients; i++) {
7199                         if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7200                                       addr, ETH_ALEN) == 0)
7201                                 return s; /* peer is P2P client in persistent
7202                                            * group */
7203                 }
7204         }
7205
7206         return NULL;
7207 }
7208
7209
7210 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7211                                        const u8 *addr)
7212 {
7213         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7214                                  wpa_s->parent, NULL) > 0) {
7215                 /*
7216                  * This can happen if WPS provisioning step is not terminated
7217                  * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7218                  * peer was able to connect, there is no need to time out group
7219                  * formation after this, though. In addition, this is used with
7220                  * the initial connection wait on the GO as a separate formation
7221                  * timeout and as such, expected to be hit after the initial WPS
7222                  * provisioning step.
7223                  */
7224                 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7225
7226                 if (!wpa_s->p2p_go_group_formation_completed &&
7227                     !wpa_s->group_formation_reported) {
7228                         /*
7229                          * GO has not yet notified group formation success since
7230                          * the WPS step was not completed cleanly. Do that
7231                          * notification now since the P2P Client was able to
7232                          * connect and as such, must have received the
7233                          * credential from the WPS step.
7234                          */
7235                         if (wpa_s->global->p2p)
7236                                 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7237                         wpas_group_formation_completed(wpa_s, 1, 0);
7238                 }
7239         }
7240         if (!wpa_s->p2p_go_group_formation_completed) {
7241                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7242                 wpa_s->p2p_go_group_formation_completed = 1;
7243                 wpa_s->global->p2p_group_formation = NULL;
7244                 wpa_s->p2p_in_provisioning = 0;
7245                 wpa_s->p2p_in_invitation = 0;
7246         }
7247         wpa_s->global->p2p_go_wait_client.sec = 0;
7248         if (addr == NULL)
7249                 return;
7250         wpas_p2p_add_persistent_group_client(wpa_s, addr);
7251 }
7252
7253
7254 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7255                                        int group_added)
7256 {
7257         struct wpa_supplicant *group = wpa_s;
7258         int ret = 0;
7259
7260         if (wpa_s->global->p2p_group_formation)
7261                 group = wpa_s->global->p2p_group_formation;
7262         wpa_s = wpa_s->global->p2p_init_wpa_s;
7263         offchannel_send_action_done(wpa_s);
7264         if (group_added)
7265                 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7266         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7267         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7268                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7269                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7270                          wpa_s->p2p_persistent_id,
7271                          wpa_s->p2p_pd_before_go_neg,
7272                          wpa_s->p2p_go_ht40,
7273                          wpa_s->p2p_go_vht);
7274         return ret;
7275 }
7276
7277
7278 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7279 {
7280         int res;
7281
7282         if (!wpa_s->p2p_fallback_to_go_neg ||
7283             wpa_s->p2p_in_provisioning <= 5)
7284                 return 0;
7285
7286         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7287                 return 0; /* peer operating as a GO */
7288
7289         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7290                 "fallback to GO Negotiation");
7291         wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7292                        "reason=GO-not-found");
7293         res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7294
7295         return res == 1 ? 2 : 1;
7296 }
7297
7298
7299 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7300 {
7301         struct wpa_supplicant *ifs;
7302
7303         if (wpa_s->wpa_state > WPA_SCANNING) {
7304                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7305                         "concurrent operation",
7306                         wpa_s->conf->p2p_search_delay);
7307                 return wpa_s->conf->p2p_search_delay;
7308         }
7309
7310         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7311                          radio_list) {
7312                 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7313                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7314                                 "delay due to concurrent operation on "
7315                                 "interface %s",
7316                                 wpa_s->conf->p2p_search_delay,
7317                                 ifs->ifname);
7318                         return wpa_s->conf->p2p_search_delay;
7319                 }
7320         }
7321
7322         return 0;
7323 }
7324
7325
7326 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7327                                      struct wpa_ssid *s, const u8 *addr,
7328                                      int iface_addr)
7329 {
7330         struct psk_list_entry *psk, *tmp;
7331         int changed = 0;
7332
7333         dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7334                               list) {
7335                 if ((iface_addr && !psk->p2p &&
7336                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7337                     (!iface_addr && psk->p2p &&
7338                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7339                         wpa_dbg(wpa_s, MSG_DEBUG,
7340                                 "P2P: Remove persistent group PSK list entry for "
7341                                 MACSTR " p2p=%u",
7342                                 MAC2STR(psk->addr), psk->p2p);
7343                         dl_list_del(&psk->list);
7344                         os_free(psk);
7345                         changed++;
7346                 }
7347         }
7348
7349         return changed;
7350 }
7351
7352
7353 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7354                          const u8 *p2p_dev_addr,
7355                          const u8 *psk, size_t psk_len)
7356 {
7357         struct wpa_ssid *ssid = wpa_s->current_ssid;
7358         struct wpa_ssid *persistent;
7359         struct psk_list_entry *p, *last;
7360
7361         if (psk_len != sizeof(p->psk))
7362                 return;
7363
7364         if (p2p_dev_addr) {
7365                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7366                         " p2p_dev_addr=" MACSTR,
7367                         MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7368                 if (is_zero_ether_addr(p2p_dev_addr))
7369                         p2p_dev_addr = NULL;
7370         } else {
7371                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7372                         MAC2STR(mac_addr));
7373         }
7374
7375         if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7376                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7377                 /* To be added to persistent group once created */
7378                 if (wpa_s->global->add_psk == NULL) {
7379                         wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7380                         if (wpa_s->global->add_psk == NULL)
7381                                 return;
7382                 }
7383                 p = wpa_s->global->add_psk;
7384                 if (p2p_dev_addr) {
7385                         p->p2p = 1;
7386                         os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7387                 } else {
7388                         p->p2p = 0;
7389                         os_memcpy(p->addr, mac_addr, ETH_ALEN);
7390                 }
7391                 os_memcpy(p->psk, psk, psk_len);
7392                 return;
7393         }
7394
7395         if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7396                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7397                 return;
7398         }
7399
7400         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7401                                              ssid->ssid_len);
7402         if (!persistent) {
7403                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7404                 return;
7405         }
7406
7407         p = os_zalloc(sizeof(*p));
7408         if (p == NULL)
7409                 return;
7410         if (p2p_dev_addr) {
7411                 p->p2p = 1;
7412                 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7413         } else {
7414                 p->p2p = 0;
7415                 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7416         }
7417         os_memcpy(p->psk, psk, psk_len);
7418
7419         if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7420             (last = dl_list_last(&persistent->psk_list,
7421                                  struct psk_list_entry, list))) {
7422                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7423                         MACSTR " (p2p=%u) to make room for a new one",
7424                         MAC2STR(last->addr), last->p2p);
7425                 dl_list_del(&last->list);
7426                 os_free(last);
7427         }
7428
7429         wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7430                                   p2p_dev_addr ? p2p_dev_addr : mac_addr,
7431                                   p2p_dev_addr == NULL);
7432         if (p2p_dev_addr) {
7433                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7434                         MACSTR, MAC2STR(p2p_dev_addr));
7435         } else {
7436                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7437                         MAC2STR(mac_addr));
7438         }
7439         dl_list_add(&persistent->psk_list, &p->list);
7440
7441         if (wpa_s->parent->conf->update_config &&
7442             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7443                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7444 }
7445
7446
7447 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7448                                 struct wpa_ssid *s, const u8 *addr,
7449                                 int iface_addr)
7450 {
7451         int res;
7452
7453         res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7454         if (res > 0 && wpa_s->conf->update_config &&
7455             wpa_config_write(wpa_s->confname, wpa_s->conf))
7456                 wpa_dbg(wpa_s, MSG_DEBUG,
7457                         "P2P: Failed to update configuration");
7458 }
7459
7460
7461 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7462                                       const u8 *peer, int iface_addr)
7463 {
7464         struct hostapd_data *hapd;
7465         struct hostapd_wpa_psk *psk, *prev, *rem;
7466         struct sta_info *sta;
7467
7468         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7469             wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7470                 return;
7471
7472         /* Remove per-station PSK entry */
7473         hapd = wpa_s->ap_iface->bss[0];
7474         prev = NULL;
7475         psk = hapd->conf->ssid.wpa_psk;
7476         while (psk) {
7477                 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7478                     (!iface_addr &&
7479                      os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7480                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7481                                 MACSTR " iface_addr=%d",
7482                                 MAC2STR(peer), iface_addr);
7483                         if (prev)
7484                                 prev->next = psk->next;
7485                         else
7486                                 hapd->conf->ssid.wpa_psk = psk->next;
7487                         rem = psk;
7488                         psk = psk->next;
7489                         os_free(rem);
7490                 } else {
7491                         prev = psk;
7492                         psk = psk->next;
7493                 }
7494         }
7495
7496         /* Disconnect from group */
7497         if (iface_addr)
7498                 sta = ap_get_sta(hapd, peer);
7499         else
7500                 sta = ap_get_sta_p2p(hapd, peer);
7501         if (sta) {
7502                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7503                         " (iface_addr=%d) from group",
7504                         MAC2STR(peer), iface_addr);
7505                 hostapd_drv_sta_deauth(hapd, sta->addr,
7506                                        WLAN_REASON_DEAUTH_LEAVING);
7507                 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7508         }
7509 }
7510
7511
7512 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7513                             int iface_addr)
7514 {
7515         struct wpa_ssid *s;
7516         struct wpa_supplicant *w;
7517         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7518
7519         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7520
7521         /* Remove from any persistent group */
7522         for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7523                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7524                         continue;
7525                 if (!iface_addr)
7526                         wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7527                 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7528         }
7529
7530         /* Remove from any operating group */
7531         for (w = wpa_s->global->ifaces; w; w = w->next)
7532                 wpas_p2p_remove_client_go(w, peer, iface_addr);
7533 }
7534
7535
7536 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7537 {
7538         struct wpa_supplicant *wpa_s = eloop_ctx;
7539         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7540 }
7541
7542
7543 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7544 {
7545         struct wpa_supplicant *wpa_s = eloop_ctx;
7546
7547         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7548         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7549 }
7550
7551
7552 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7553                                         struct wpa_ssid *ssid)
7554 {
7555         struct wpa_supplicant *iface;
7556
7557         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7558                 if (!iface->current_ssid ||
7559                     iface->current_ssid->frequency == freq ||
7560                     (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7561                      !iface->current_ssid->p2p_group))
7562                         continue;
7563
7564                 /* Remove the connection with least priority */
7565                 if (!wpas_is_p2p_prioritized(iface)) {
7566                         /* STA connection has priority over existing
7567                          * P2P connection, so remove the interface. */
7568                         wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7569                         eloop_register_timeout(0, 0,
7570                                                wpas_p2p_group_freq_conflict,
7571                                                iface, NULL);
7572                         /* If connection in progress is P2P connection, do not
7573                          * proceed for the connection. */
7574                         if (wpa_s == iface)
7575                                 return -1;
7576                         else
7577                                 return 0;
7578                 } else {
7579                         /* P2P connection has priority, disable the STA network
7580                          */
7581                         wpa_supplicant_disable_network(wpa_s->global->ifaces,
7582                                                        ssid);
7583                         wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7584                                 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7585                         os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7586                                   ETH_ALEN);
7587                         /* If P2P connection is in progress, continue
7588                          * connecting...*/
7589                         if (wpa_s == iface)
7590                                 return 0;
7591                         else
7592                                 return -1;
7593                 }
7594         }
7595
7596         return 0;
7597 }
7598
7599
7600 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7601 {
7602         struct wpa_ssid *ssid = wpa_s->current_ssid;
7603
7604         if (ssid == NULL || !ssid->p2p_group)
7605                 return 0;
7606
7607         if (wpa_s->p2p_last_4way_hs_fail &&
7608             wpa_s->p2p_last_4way_hs_fail == ssid) {
7609                 u8 go_dev_addr[ETH_ALEN];
7610                 struct wpa_ssid *persistent;
7611
7612                 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7613                                               ssid->ssid,
7614                                               ssid->ssid_len) <= 0) {
7615                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7616                         goto disconnect;
7617                 }
7618
7619                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7620                         MACSTR, MAC2STR(go_dev_addr));
7621                 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7622                                                      ssid->ssid,
7623                                                      ssid->ssid_len);
7624                 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7625                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7626                         goto disconnect;
7627                 }
7628                 wpa_msg_global(wpa_s->parent, MSG_INFO,
7629                                P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7630                                persistent->id);
7631         disconnect:
7632                 wpa_s->p2p_last_4way_hs_fail = NULL;
7633                 /*
7634                  * Remove the group from a timeout to avoid issues with caller
7635                  * continuing to use the interface if this is on a P2P group
7636                  * interface.
7637                  */
7638                 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7639                                        wpa_s, NULL);
7640                 return 1;
7641         }
7642
7643         wpa_s->p2p_last_4way_hs_fail = ssid;
7644         return 0;
7645 }
7646
7647
7648 #ifdef CONFIG_WPS_NFC
7649
7650 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7651                                              struct wpabuf *p2p)
7652 {
7653         struct wpabuf *ret;
7654         size_t wsc_len;
7655
7656         if (p2p == NULL) {
7657                 wpabuf_free(wsc);
7658                 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7659                 return NULL;
7660         }
7661
7662         wsc_len = wsc ? wpabuf_len(wsc) : 0;
7663         ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7664         if (ret == NULL) {
7665                 wpabuf_free(wsc);
7666                 wpabuf_free(p2p);
7667                 return NULL;
7668         }
7669
7670         wpabuf_put_be16(ret, wsc_len);
7671         if (wsc)
7672                 wpabuf_put_buf(ret, wsc);
7673         wpabuf_put_be16(ret, wpabuf_len(p2p));
7674         wpabuf_put_buf(ret, p2p);
7675
7676         wpabuf_free(wsc);
7677         wpabuf_free(p2p);
7678         wpa_hexdump_buf(MSG_DEBUG,
7679                         "P2P: Generated NFC connection handover message", ret);
7680
7681         if (ndef && ret) {
7682                 struct wpabuf *tmp;
7683                 tmp = ndef_build_p2p(ret);
7684                 wpabuf_free(ret);
7685                 if (tmp == NULL) {
7686                         wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7687                         return NULL;
7688                 }
7689                 ret = tmp;
7690         }
7691
7692         return ret;
7693 }
7694
7695
7696 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7697                              struct wpa_ssid **ssid, u8 *go_dev_addr)
7698 {
7699         struct wpa_supplicant *iface;
7700
7701         if (go_dev_addr)
7702                 os_memset(go_dev_addr, 0, ETH_ALEN);
7703         if (ssid)
7704                 *ssid = NULL;
7705         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7706                 if (iface->wpa_state < WPA_ASSOCIATING ||
7707                     iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7708                     !iface->current_ssid->p2p_group ||
7709                     iface->current_ssid->mode != WPAS_MODE_INFRA)
7710                         continue;
7711                 if (ssid)
7712                         *ssid = iface->current_ssid;
7713                 if (go_dev_addr)
7714                         os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7715                 return iface->assoc_freq;
7716         }
7717         return 0;
7718 }
7719
7720
7721 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7722                                           int ndef)
7723 {
7724         struct wpabuf *wsc, *p2p;
7725         struct wpa_ssid *ssid;
7726         u8 go_dev_addr[ETH_ALEN];
7727         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7728
7729         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7730                 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7731                 return NULL;
7732         }
7733
7734         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7735             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7736                            &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7737                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7738                 return NULL;
7739         }
7740
7741         if (cli_freq == 0) {
7742                 wsc = wps_build_nfc_handover_req_p2p(
7743                         wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7744         } else
7745                 wsc = NULL;
7746         p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7747                                          go_dev_addr, ssid ? ssid->ssid : NULL,
7748                                          ssid ? ssid->ssid_len : 0);
7749
7750         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7751 }
7752
7753
7754 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7755                                           int ndef, int tag)
7756 {
7757         struct wpabuf *wsc, *p2p;
7758         struct wpa_ssid *ssid;
7759         u8 go_dev_addr[ETH_ALEN];
7760         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7761
7762         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7763                 return NULL;
7764
7765         if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7766             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7767                            &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7768                 return NULL;
7769
7770         if (cli_freq == 0) {
7771                 wsc = wps_build_nfc_handover_sel_p2p(
7772                         wpa_s->parent->wps,
7773                         tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7774                         DEV_PW_NFC_CONNECTION_HANDOVER,
7775                         wpa_s->conf->wps_nfc_dh_pubkey,
7776                         tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7777         } else
7778                 wsc = NULL;
7779         p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7780                                          go_dev_addr, ssid ? ssid->ssid : NULL,
7781                                          ssid ? ssid->ssid_len : 0);
7782
7783         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7784 }
7785
7786
7787 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7788                                    struct p2p_nfc_params *params)
7789 {
7790         wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7791                    "connection handover (freq=%d)",
7792                    params->go_freq);
7793
7794         if (params->go_freq && params->go_ssid_len) {
7795                 wpa_s->p2p_wps_method = WPS_NFC;
7796                 wpa_s->pending_join_wps_method = WPS_NFC;
7797                 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7798                 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7799                           ETH_ALEN);
7800                 return wpas_p2p_join_start(wpa_s, params->go_freq,
7801                                            params->go_ssid,
7802                                            params->go_ssid_len);
7803         }
7804
7805         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7806                                 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7807                                 params->go_freq, -1, 0, 1, 1);
7808 }
7809
7810
7811 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7812                                   struct p2p_nfc_params *params, int tag)
7813 {
7814         int res, persistent;
7815         struct wpa_ssid *ssid;
7816
7817         wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7818                    "connection handover");
7819         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7820                 ssid = wpa_s->current_ssid;
7821                 if (ssid == NULL)
7822                         continue;
7823                 if (ssid->mode != WPAS_MODE_P2P_GO)
7824                         continue;
7825                 if (wpa_s->ap_iface == NULL)
7826                         continue;
7827                 break;
7828         }
7829         if (wpa_s == NULL) {
7830                 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7831                 return -1;
7832         }
7833
7834         if (wpa_s->parent->p2p_oob_dev_pw_id !=
7835             DEV_PW_NFC_CONNECTION_HANDOVER &&
7836             !wpa_s->parent->p2p_oob_dev_pw) {
7837                 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7838                 return -1;
7839         }
7840         res = wpas_ap_wps_add_nfc_pw(
7841                 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7842                 wpa_s->parent->p2p_oob_dev_pw,
7843                 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7844                 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7845         if (res)
7846                 return res;
7847
7848         if (!tag) {
7849                 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7850                 return 0;
7851         }
7852
7853         if (!params->peer ||
7854             !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7855                 return 0;
7856
7857         wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7858                    " to join", MAC2STR(params->peer->p2p_device_addr));
7859
7860         wpa_s->global->p2p_invite_group = wpa_s;
7861         persistent = ssid->p2p_persistent_group &&
7862                 wpas_p2p_get_persistent(wpa_s->parent,
7863                                         params->peer->p2p_device_addr,
7864                                         ssid->ssid, ssid->ssid_len);
7865         wpa_s->parent->pending_invite_ssid_id = -1;
7866
7867         return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7868                           P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7869                           ssid->ssid, ssid->ssid_len, ssid->frequency,
7870                           wpa_s->global->p2p_dev_addr, persistent, 0,
7871                           wpa_s->parent->p2p_oob_dev_pw_id);
7872 }
7873
7874
7875 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7876                                     struct p2p_nfc_params *params,
7877                                     int forced_freq)
7878 {
7879         wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7880                    "connection handover");
7881         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7882                                 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7883                                 forced_freq, -1, 0, 1, 1);
7884 }
7885
7886
7887 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7888                                     struct p2p_nfc_params *params,
7889                                     int forced_freq)
7890 {
7891         int res;
7892
7893         wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7894                    "connection handover");
7895         res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7896                                WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7897                                forced_freq, -1, 0, 1, 1);
7898         if (res)
7899                 return res;
7900
7901         res = wpas_p2p_listen(wpa_s, 60);
7902         if (res) {
7903                 p2p_unauthorize(wpa_s->global->p2p,
7904                                 params->peer->p2p_device_addr);
7905         }
7906
7907         return res;
7908 }
7909
7910
7911 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7912                                             const struct wpabuf *data,
7913                                             int sel, int tag, int forced_freq)
7914 {
7915         const u8 *pos, *end;
7916         u16 len, id;
7917         struct p2p_nfc_params params;
7918         int res;
7919
7920         os_memset(&params, 0, sizeof(params));
7921         params.sel = sel;
7922
7923         wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7924
7925         pos = wpabuf_head(data);
7926         end = pos + wpabuf_len(data);
7927
7928         if (end - pos < 2) {
7929                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7930                            "attributes");
7931                 return -1;
7932         }
7933         len = WPA_GET_BE16(pos);
7934         pos += 2;
7935         if (len > end - pos) {
7936                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7937                            "attributes");
7938                 return -1;
7939         }
7940         params.wsc_attr = pos;
7941         params.wsc_len = len;
7942         pos += len;
7943
7944         if (end - pos < 2) {
7945                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7946                            "attributes");
7947                 return -1;
7948         }
7949         len = WPA_GET_BE16(pos);
7950         pos += 2;
7951         if (len > end - pos) {
7952                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7953                            "attributes");
7954                 return -1;
7955         }
7956         params.p2p_attr = pos;
7957         params.p2p_len = len;
7958         pos += len;
7959
7960         wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7961                     params.wsc_attr, params.wsc_len);
7962         wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7963                     params.p2p_attr, params.p2p_len);
7964         if (pos < end) {
7965                 wpa_hexdump(MSG_DEBUG,
7966                             "P2P: Ignored extra data after P2P attributes",
7967                             pos, end - pos);
7968         }
7969
7970         res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7971         if (res)
7972                 return res;
7973
7974         if (params.next_step == NO_ACTION)
7975                 return 0;
7976
7977         if (params.next_step == BOTH_GO) {
7978                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7979                         MAC2STR(params.peer->p2p_device_addr));
7980                 return 0;
7981         }
7982
7983         if (params.next_step == PEER_CLIENT) {
7984                 if (!is_zero_ether_addr(params.go_dev_addr)) {
7985                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7986                                 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7987                                 " ssid=\"%s\"",
7988                                 MAC2STR(params.peer->p2p_device_addr),
7989                                 params.go_freq,
7990                                 MAC2STR(params.go_dev_addr),
7991                                 wpa_ssid_txt(params.go_ssid,
7992                                              params.go_ssid_len));
7993                 } else {
7994                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7995                                 "peer=" MACSTR " freq=%d",
7996                                 MAC2STR(params.peer->p2p_device_addr),
7997                                 params.go_freq);
7998                 }
7999                 return 0;
8000         }
8001
8002         if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8003                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8004                         MACSTR, MAC2STR(params.peer->p2p_device_addr));
8005                 return 0;
8006         }
8007
8008         wpabuf_free(wpa_s->p2p_oob_dev_pw);
8009         wpa_s->p2p_oob_dev_pw = NULL;
8010
8011         if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8012                 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8013                            "received");
8014                 return -1;
8015         }
8016
8017         id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8018         wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8019         wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8020                     params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8021         os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8022                   params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8023         wpa_s->p2p_peer_oob_pk_hash_known = 1;
8024
8025         if (tag) {
8026                 if (id < 0x10) {
8027                         wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8028                                    "peer OOB Device Password Id %u", id);
8029                         return -1;
8030                 }
8031                 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8032                            "Device Password Id %u", id);
8033                 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8034                                 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8035                                 params.oob_dev_pw_len -
8036                                 WPS_OOB_PUBKEY_HASH_LEN - 2);
8037                 wpa_s->p2p_oob_dev_pw_id = id;
8038                 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8039                         params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8040                         params.oob_dev_pw_len -
8041                         WPS_OOB_PUBKEY_HASH_LEN - 2);
8042                 if (wpa_s->p2p_oob_dev_pw == NULL)
8043                         return -1;
8044
8045                 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8046                     wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8047                                    &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8048                         return -1;
8049         } else {
8050                 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8051                            "without Device Password");
8052                 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8053         }
8054
8055         switch (params.next_step) {
8056         case NO_ACTION:
8057         case BOTH_GO:
8058         case PEER_CLIENT:
8059                 /* already covered above */
8060                 return 0;
8061         case JOIN_GROUP:
8062                 return wpas_p2p_nfc_join_group(wpa_s, &params);
8063         case AUTH_JOIN:
8064                 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8065         case INIT_GO_NEG:
8066                 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8067         case RESP_GO_NEG:
8068                 /* TODO: use own OOB Dev Pw */
8069                 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8070         }
8071
8072         return -1;
8073 }
8074
8075
8076 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8077                              const struct wpabuf *data, int forced_freq)
8078 {
8079         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8080                 return -1;
8081
8082         return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8083 }
8084
8085
8086 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8087                                  const struct wpabuf *req,
8088                                  const struct wpabuf *sel, int forced_freq)
8089 {
8090         struct wpabuf *tmp;
8091         int ret;
8092
8093         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8094                 return -1;
8095
8096         wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8097
8098         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8099                           wpabuf_head(req), wpabuf_len(req));
8100         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8101                           wpabuf_head(sel), wpabuf_len(sel));
8102         if (forced_freq)
8103                 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8104         tmp = ndef_parse_p2p(init ? sel : req);
8105         if (tmp == NULL) {
8106                 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8107                 return -1;
8108         }
8109
8110         ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8111                                                forced_freq);
8112         wpabuf_free(tmp);
8113
8114         return ret;
8115 }
8116
8117
8118 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8119 {
8120         const u8 *if_addr;
8121         int go_intent = wpa_s->conf->p2p_go_intent;
8122         struct wpa_supplicant *iface;
8123
8124         if (wpa_s->global->p2p == NULL)
8125                 return -1;
8126
8127         if (!enabled) {
8128                 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8129                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8130                 {
8131                         if (!iface->ap_iface)
8132                                 continue;
8133                         hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8134                 }
8135                 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8136                                                  0, NULL);
8137                 if (wpa_s->p2p_nfc_tag_enabled)
8138                         wpas_p2p_remove_pending_group_interface(wpa_s);
8139                 wpa_s->p2p_nfc_tag_enabled = 0;
8140                 return 0;
8141         }
8142
8143         if (wpa_s->global->p2p_disabled)
8144                 return -1;
8145
8146         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8147             wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8148             wpa_s->conf->wps_nfc_dev_pw == NULL ||
8149             wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8150                 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8151                            "to allow static handover cases");
8152                 return -1;
8153         }
8154
8155         wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8156
8157         wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8158         wpabuf_free(wpa_s->p2p_oob_dev_pw);
8159         wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8160         if (wpa_s->p2p_oob_dev_pw == NULL)
8161                 return -1;
8162         wpa_s->p2p_peer_oob_pk_hash_known = 0;
8163
8164         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8165             wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8166                 /*
8167                  * P2P Group Interface present and the command came on group
8168                  * interface, so enable the token for the current interface.
8169                  */
8170                 wpa_s->create_p2p_iface = 0;
8171         } else {
8172                 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8173         }
8174
8175         if (wpa_s->create_p2p_iface) {
8176                 enum wpa_driver_if_type iftype;
8177                 /* Prepare to add a new interface for the group */
8178                 iftype = WPA_IF_P2P_GROUP;
8179                 if (go_intent == 15)
8180                         iftype = WPA_IF_P2P_GO;
8181                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8182                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8183                                    "interface for the group");
8184                         return -1;
8185                 }
8186
8187                 if_addr = wpa_s->pending_interface_addr;
8188         } else
8189                 if_addr = wpa_s->own_addr;
8190
8191         wpa_s->p2p_nfc_tag_enabled = enabled;
8192
8193         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8194                 struct hostapd_data *hapd;
8195                 if (iface->ap_iface == NULL)
8196                         continue;
8197                 hapd = iface->ap_iface->bss[0];
8198                 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8199                 hapd->conf->wps_nfc_dh_pubkey =
8200                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8201                 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8202                 hapd->conf->wps_nfc_dh_privkey =
8203                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8204                 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8205                 hapd->conf->wps_nfc_dev_pw =
8206                         wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8207                 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8208
8209                 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8210                         wpa_dbg(iface, MSG_DEBUG,
8211                                 "P2P: Failed to enable NFC Tag for GO");
8212                 }
8213         }
8214         p2p_set_authorized_oob_dev_pw_id(
8215                 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8216                 if_addr);
8217
8218         return 0;
8219 }
8220
8221 #endif /* CONFIG_WPS_NFC */
8222
8223
8224 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8225                                              struct wpa_used_freq_data *freqs,
8226                                              unsigned int num)
8227 {
8228         u8 curr_chan, cand, chan;
8229         unsigned int i;
8230
8231         curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8232         for (i = 0, cand = 0; i < num; i++) {
8233                 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8234                 if (curr_chan == chan) {
8235                         cand = 0;
8236                         break;
8237                 }
8238
8239                 if (chan == 1 || chan == 6 || chan == 11)
8240                         cand = chan;
8241         }
8242
8243         if (cand) {
8244                 wpa_dbg(wpa_s, MSG_DEBUG,
8245                         "P2P: Update Listen channel to %u based on operating channel",
8246                         cand);
8247                 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8248         }
8249 }
8250
8251
8252 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8253 {
8254         struct wpa_used_freq_data *freqs;
8255         unsigned int num = wpa_s->num_multichan_concurrent;
8256
8257         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8258                 return;
8259
8260         /*
8261          * If possible, optimize the Listen channel to be a channel that is
8262          * already used by one of the other interfaces.
8263          */
8264         if (!wpa_s->conf->p2p_optimize_listen_chan)
8265                 return;
8266
8267         if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8268                 return;
8269
8270         freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8271         if (!freqs)
8272                 return;
8273
8274         num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8275
8276         wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8277         os_free(freqs);
8278 }
8279
8280
8281 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8282 {
8283         if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8284                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8285                         "the management interface is being removed");
8286                 wpas_p2p_deinit_global(wpa_s->global);
8287         }
8288 }
8289
8290
8291 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8292 {
8293         if (wpa_s->ap_iface->bss)
8294                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8295         wpas_p2p_group_deinit(wpa_s);
8296 }