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