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