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