P2PS: Re-factor wpas_p2p_get_go_group() and wpas_p2p_group_go_ssid()
[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 static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
549 {
550         return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
551                 !wpa_s->current_ssid->disabled &&
552                 wpa_s->current_ssid->p2p_group &&
553                 wpa_s->current_ssid->p2p_persistent_group;
554 }
555
556
557 static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
558 {
559         return p2p_is_active_persistent_group(wpa_s) &&
560                 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
561 }
562
563
564 /* Find an interface for a P2P group where we are the GO */
565 static struct wpa_supplicant *
566 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
567 {
568         struct wpa_supplicant *save = NULL;
569
570         if (!wpa_s)
571                 return NULL;
572
573         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
574                 if (!p2p_is_active_persistent_go(wpa_s))
575                         continue;
576
577                 /* Prefer a group with connected clients */
578                 if (p2p_get_group_num_members(wpa_s->p2p_group))
579                         return wpa_s;
580                 save = wpa_s;
581         }
582
583         /* No group with connected clients, so pick the one without (if any) */
584         return save;
585 }
586
587
588 /* Find an active P2P group where we are the GO */
589 static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
590                                                 u8 *bssid)
591 {
592         struct wpa_supplicant *go = wpas_p2p_get_go_group(wpa_s);
593
594         if (!go)
595                 return NULL;
596
597         os_memcpy(bssid, go->own_addr, ETH_ALEN);
598         return go->current_ssid;
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 static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2696                                struct sta_info *sta, void *ctx)
2697 {
2698         int *ecsa_support = ctx;
2699
2700         *ecsa_support &= sta->ecsa_supported;
2701
2702         return 0;
2703 }
2704
2705
2706 /* Check if all the peers support eCSA */
2707 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2708 {
2709         int ecsa_support = 1;
2710
2711         ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2712                         &ecsa_support);
2713
2714         return ecsa_support;
2715 }
2716
2717
2718 /**
2719  * Pick the best frequency to use from all the currently used frequencies.
2720  */
2721 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2722                                         struct wpa_used_freq_data *freqs,
2723                                         unsigned int num)
2724 {
2725         unsigned int i, c;
2726
2727         /* find a candidate freq that is supported by P2P */
2728         for (c = 0; c < num; c++)
2729                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2730                         break;
2731
2732         if (c == num)
2733                 return 0;
2734
2735         /* once we have a candidate, try to find a 'better' one */
2736         for (i = c + 1; i < num; i++) {
2737                 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2738                         continue;
2739
2740                 /*
2741                  * 1. Infrastructure station interfaces have higher preference.
2742                  * 2. P2P Clients have higher preference.
2743                  * 3. All others.
2744                  */
2745                 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2746                         c = i;
2747                         break;
2748                 }
2749
2750                 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2751                         c = i;
2752         }
2753         return freqs[c].freq;
2754 }
2755
2756
2757 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2758                                   const u8 *go_dev_addr, const u8 *ssid,
2759                                   size_t ssid_len, int *go, u8 *group_bssid,
2760                                   int *force_freq, int persistent_group,
2761                                   const struct p2p_channels *channels,
2762                                   int dev_pw_id)
2763 {
2764         struct wpa_supplicant *wpa_s = ctx;
2765         struct wpa_ssid *s;
2766         struct wpa_used_freq_data *freqs;
2767         struct wpa_supplicant *grp;
2768         int best_freq;
2769
2770         if (!persistent_group) {
2771                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2772                            " to join an active group (SSID: %s)",
2773                            MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2774                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2775                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2776                      == 0 ||
2777                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2778                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2779                                    "authorized invitation");
2780                         goto accept_inv;
2781                 }
2782
2783 #ifdef CONFIG_WPS_NFC
2784                 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2785                     dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
2786                         wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2787                         wpa_s->p2p_wps_method = WPS_NFC;
2788                         wpa_s->pending_join_wps_method = WPS_NFC;
2789                         os_memcpy(wpa_s->pending_join_dev_addr,
2790                                   go_dev_addr, ETH_ALEN);
2791                         os_memcpy(wpa_s->pending_join_iface_addr,
2792                                   bssid, ETH_ALEN);
2793                         goto accept_inv;
2794                 }
2795 #endif /* CONFIG_WPS_NFC */
2796
2797                 /*
2798                  * Do not accept the invitation automatically; notify user and
2799                  * request approval.
2800                  */
2801                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2802         }
2803
2804         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2805         if (grp) {
2806                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2807                            "running persistent group");
2808                 if (*go)
2809                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2810                 goto accept_inv;
2811         }
2812
2813         if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2814             os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2815                 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2816                            "invitation to re-invoke a persistent group");
2817                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
2818         } else if (!wpa_s->conf->persistent_reconnect)
2819                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2820
2821         for (s = wpa_s->conf->ssid; s; s = s->next) {
2822                 if (s->disabled == 2 &&
2823                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2824                     s->ssid_len == ssid_len &&
2825                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2826                         break;
2827         }
2828
2829         if (!s) {
2830                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2831                            " requested reinvocation of an unknown group",
2832                            MAC2STR(sa));
2833                 return P2P_SC_FAIL_UNKNOWN_GROUP;
2834         }
2835
2836         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2837                 *go = 1;
2838                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2839                         wpa_printf(MSG_DEBUG, "P2P: The only available "
2840                                    "interface is already in use - reject "
2841                                    "invitation");
2842                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2843                 }
2844                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2845         } else if (s->mode == WPAS_MODE_P2P_GO) {
2846                 *go = 1;
2847                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2848                 {
2849                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2850                                    "interface address for the group");
2851                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2852                 }
2853                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2854                           ETH_ALEN);
2855         }
2856
2857 accept_inv:
2858         wpas_p2p_set_own_freq_preference(wpa_s, 0);
2859
2860         best_freq = 0;
2861         freqs = os_calloc(wpa_s->num_multichan_concurrent,
2862                           sizeof(struct wpa_used_freq_data));
2863         if (freqs) {
2864                 int num_channels = wpa_s->num_multichan_concurrent;
2865                 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2866                 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2867                 os_free(freqs);
2868         }
2869
2870         /* Get one of the frequencies currently in use */
2871         if (best_freq > 0) {
2872                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2873                 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
2874
2875                 if (wpa_s->num_multichan_concurrent < 2 ||
2876                     wpas_p2p_num_unused_channels(wpa_s) < 1) {
2877                         wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2878                         *force_freq = best_freq;
2879                 }
2880         }
2881
2882         if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2883             wpas_p2p_num_unused_channels(wpa_s) > 0) {
2884                 if (*go == 0) {
2885                         /* We are the client */
2886                         wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2887                                    "running a GO but we are capable of MCC, "
2888                                    "figure out the best channel to use");
2889                         *force_freq = 0;
2890                 } else if (!freq_included(wpa_s, channels, *force_freq)) {
2891                         /* We are the GO, and *force_freq is not in the
2892                          * intersection */
2893                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2894                                    "in intersection but we are capable of MCC, "
2895                                    "figure out the best channel to use",
2896                                    *force_freq);
2897                         *force_freq = 0;
2898                 }
2899         }
2900
2901         return P2P_SC_SUCCESS;
2902 }
2903
2904
2905 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2906                                      const u8 *ssid, size_t ssid_len,
2907                                      const u8 *go_dev_addr, u8 status,
2908                                      int op_freq)
2909 {
2910         struct wpa_supplicant *wpa_s = ctx;
2911         struct wpa_ssid *s;
2912
2913         for (s = wpa_s->conf->ssid; s; s = s->next) {
2914                 if (s->disabled == 2 &&
2915                     s->ssid_len == ssid_len &&
2916                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2917                         break;
2918         }
2919
2920         if (status == P2P_SC_SUCCESS) {
2921                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2922                            " was accepted; op_freq=%d MHz, SSID=%s",
2923                            MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
2924                 if (s) {
2925                         int go = s->mode == WPAS_MODE_P2P_GO;
2926                         wpas_p2p_group_add_persistent(
2927                                 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
2928                                 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
2929                                 1);
2930                 } else if (bssid) {
2931                         wpa_s->user_initiated_pd = 0;
2932                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2933                                       wpa_s->p2p_wps_method, 0, op_freq,
2934                                       ssid, ssid_len);
2935                 }
2936                 return;
2937         }
2938
2939         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2940                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2941                            " was rejected (status %u)", MAC2STR(sa), status);
2942                 return;
2943         }
2944
2945         if (!s) {
2946                 if (bssid) {
2947                         wpa_msg_global(wpa_s, MSG_INFO,
2948                                        P2P_EVENT_INVITATION_RECEIVED
2949                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2950                                        " bssid=" MACSTR " unknown-network",
2951                                        MAC2STR(sa), MAC2STR(go_dev_addr),
2952                                        MAC2STR(bssid));
2953                 } else {
2954                         wpa_msg_global(wpa_s, MSG_INFO,
2955                                        P2P_EVENT_INVITATION_RECEIVED
2956                                        "sa=" MACSTR " go_dev_addr=" MACSTR
2957                                        " unknown-network",
2958                                        MAC2STR(sa), MAC2STR(go_dev_addr));
2959                 }
2960                 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
2961                                                     bssid, 0, op_freq);
2962                 return;
2963         }
2964
2965         if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2966                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2967                                "sa=" MACSTR " persistent=%d freq=%d",
2968                                MAC2STR(sa), s->id, op_freq);
2969         } else {
2970                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2971                                "sa=" MACSTR " persistent=%d",
2972                                MAC2STR(sa), s->id);
2973         }
2974         wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
2975                                             s->id, op_freq);
2976 }
2977
2978
2979 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2980                                         struct wpa_ssid *ssid,
2981                                         const u8 *peer, int inv)
2982 {
2983         size_t i;
2984         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
2985
2986         if (ssid == NULL)
2987                 return;
2988
2989         for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2990                 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
2991                               ETH_ALEN) == 0)
2992                         break;
2993         }
2994         if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
2995                 if (ssid->mode != WPAS_MODE_P2P_GO &&
2996                     os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2997                         wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2998                                    "due to invitation result", ssid->id);
2999                         wpas_notify_network_removed(wpa_s, ssid);
3000                         wpa_config_remove_network(wpa_s->conf, ssid->id);
3001                         return;
3002                 }
3003                 return; /* Peer not found in client list */
3004         }
3005
3006         wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3007                    "group %d client list%s",
3008                    MAC2STR(peer), ssid->id,
3009                    inv ? " due to invitation result" : "");
3010         os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3011                    ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3012                    (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3013         ssid->num_p2p_clients--;
3014         if (p2p_wpa_s->conf->update_config &&
3015             wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
3016                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3017 }
3018
3019
3020 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3021                                           const u8 *peer)
3022 {
3023         struct wpa_ssid *ssid;
3024
3025         wpa_s = wpa_s->global->p2p_invite_group;
3026         if (wpa_s == NULL)
3027                 return; /* No known invitation group */
3028         ssid = wpa_s->current_ssid;
3029         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3030             !ssid->p2p_persistent_group)
3031                 return; /* Not operating as a GO in persistent group */
3032         ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3033                                        ssid->ssid, ssid->ssid_len);
3034         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3035 }
3036
3037
3038 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3039                                    const struct p2p_channels *channels,
3040                                    const u8 *peer, int neg_freq,
3041                                    int peer_oper_freq)
3042 {
3043         struct wpa_supplicant *wpa_s = ctx;
3044         struct wpa_ssid *ssid;
3045         int freq;
3046
3047         if (bssid) {
3048                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3049                                "status=%d " MACSTR,
3050                                status, MAC2STR(bssid));
3051         } else {
3052                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3053                                "status=%d ", status);
3054         }
3055         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3056
3057         wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3058                    status, MAC2STR(peer));
3059         if (wpa_s->pending_invite_ssid_id == -1) {
3060                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3061                         wpas_remove_persistent_client(wpa_s, peer);
3062                 return; /* Invitation to active group */
3063         }
3064
3065         if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3066                 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3067                            "invitation exchange to indicate readiness for "
3068                            "re-invocation");
3069         }
3070
3071         if (status != P2P_SC_SUCCESS) {
3072                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3073                         ssid = wpa_config_get_network(
3074                                 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3075                         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3076                 }
3077                 wpas_p2p_remove_pending_group_interface(wpa_s);
3078                 return;
3079         }
3080
3081         ssid = wpa_config_get_network(wpa_s->conf,
3082                                       wpa_s->pending_invite_ssid_id);
3083         if (ssid == NULL) {
3084                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3085                            "data matching with invitation");
3086                 return;
3087         }
3088
3089         /*
3090          * The peer could have missed our ctrl::ack frame for Invitation
3091          * Response and continue retransmitting the frame. To reduce the
3092          * likelihood of the peer not getting successful TX status for the
3093          * Invitation Response frame, wait a short time here before starting
3094          * the persistent group so that we will remain on the current channel to
3095          * acknowledge any possible retransmission from the peer.
3096          */
3097         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3098                 "starting persistent group");
3099         os_sleep(0, 50000);
3100
3101         if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3102             freq_included(wpa_s, channels, neg_freq))
3103                 freq = neg_freq;
3104         else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3105                  freq_included(wpa_s, channels, peer_oper_freq))
3106                 freq = peer_oper_freq;
3107         else
3108                 freq = 0;
3109
3110         wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3111                    freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3112         wpas_p2p_group_add_persistent(wpa_s, ssid,
3113                                       ssid->mode == WPAS_MODE_P2P_GO,
3114                                       wpa_s->p2p_persistent_go_freq,
3115                                       freq,
3116                                       wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3117                                       channels,
3118                                       ssid->mode == WPAS_MODE_P2P_GO ?
3119                                       P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3120                                       0, 1);
3121 }
3122
3123
3124 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3125                                     unsigned int freq)
3126 {
3127         if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3128                 return 1;
3129         return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3130 }
3131
3132
3133 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3134 {
3135         reg->channel[reg->channels] = chan;
3136         reg->channels++;
3137 }
3138
3139
3140 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3141                                      struct p2p_channels *chan,
3142                                      struct p2p_channels *cli_chan)
3143 {
3144         int i, cla = 0;
3145
3146         wpa_s->global->p2p_24ghz_social_channels = 1;
3147
3148         os_memset(cli_chan, 0, sizeof(*cli_chan));
3149
3150         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3151                    "band");
3152
3153         /* Operating class 81 - 2.4 GHz band channels 1..13 */
3154         chan->reg_class[cla].reg_class = 81;
3155         chan->reg_class[cla].channels = 0;
3156         for (i = 0; i < 11; i++) {
3157                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3158                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3159         }
3160         if (chan->reg_class[cla].channels)
3161                 cla++;
3162
3163         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3164                    "band");
3165
3166         /* Operating class 115 - 5 GHz, channels 36-48 */
3167         chan->reg_class[cla].reg_class = 115;
3168         chan->reg_class[cla].channels = 0;
3169         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3170                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3171         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3172                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3173         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3174                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3175         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3176                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3177         if (chan->reg_class[cla].channels)
3178                 cla++;
3179
3180         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3181                    "band");
3182
3183         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3184         chan->reg_class[cla].reg_class = 124;
3185         chan->reg_class[cla].channels = 0;
3186         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3187                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3188         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3189                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3190         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3191                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3192         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3193                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3194         if (chan->reg_class[cla].channels)
3195                 cla++;
3196
3197         chan->reg_classes = cla;
3198         return 0;
3199 }
3200
3201
3202 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3203                                           u16 num_modes,
3204                                           enum hostapd_hw_mode mode)
3205 {
3206         u16 i;
3207
3208         for (i = 0; i < num_modes; i++) {
3209                 if (modes[i].mode == mode)
3210                         return &modes[i];
3211         }
3212
3213         return NULL;
3214 }
3215
3216
3217 enum chan_allowed {
3218         NOT_ALLOWED, NO_IR, ALLOWED
3219 };
3220
3221 static int has_channel(struct wpa_global *global,
3222                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
3223 {
3224         int i;
3225         unsigned int freq;
3226
3227         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3228                 chan * 5;
3229         if (wpas_p2p_disallowed_freq(global, freq))
3230                 return NOT_ALLOWED;
3231
3232         for (i = 0; i < mode->num_channels; i++) {
3233                 if (mode->channels[i].chan == chan) {
3234                         if (flags)
3235                                 *flags = mode->channels[i].flag;
3236                         if (mode->channels[i].flag &
3237                             (HOSTAPD_CHAN_DISABLED |
3238                              HOSTAPD_CHAN_RADAR))
3239                                 return NOT_ALLOWED;
3240                         if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3241                                 return NO_IR;
3242                         return ALLOWED;
3243                 }
3244         }
3245
3246         return NOT_ALLOWED;
3247 }
3248
3249
3250 struct p2p_oper_class_map {
3251         enum hostapd_hw_mode mode;
3252         u8 op_class;
3253         u8 min_chan;
3254         u8 max_chan;
3255         u8 inc;
3256         enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3257 };
3258
3259 static const struct p2p_oper_class_map op_class[] = {
3260         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3261 #if 0 /* Do not enable HT40 on 2 GHz for now */
3262         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3263         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3264 #endif
3265         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3266         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3267         { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
3268         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3269         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3270         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3271         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3272
3273         /*
3274          * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3275          * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3276          * 80 MHz, but currently use the following definition for simplicity
3277          * (these center frequencies are not actual channels, which makes
3278          * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3279          * removing invalid channels.
3280          */
3281         { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3282         { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3283         { -1, 0, 0, 0, 0, BW20 }
3284 };
3285
3286
3287 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3288                                      struct hostapd_hw_modes *mode,
3289                                      u8 channel)
3290 {
3291         u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3292         unsigned int i;
3293
3294         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3295                 return 0;
3296
3297         for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3298                 /*
3299                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3300                  * so the center channel is 6 channels away from the start/end.
3301                  */
3302                 if (channel >= center_channels[i] - 6 &&
3303                     channel <= center_channels[i] + 6)
3304                         return center_channels[i];
3305
3306         return 0;
3307 }
3308
3309
3310 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3311                                                struct hostapd_hw_modes *mode,
3312                                                u8 channel, u8 bw)
3313 {
3314         u8 center_chan;
3315         int i, flags;
3316         enum chan_allowed res, ret = ALLOWED;
3317
3318         center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3319         if (!center_chan)
3320                 return NOT_ALLOWED;
3321         if (center_chan >= 58 && center_chan <= 138)
3322                 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3323
3324         /* check all the channels are available */
3325         for (i = 0; i < 4; i++) {
3326                 int adj_chan = center_chan - 6 + i * 4;
3327
3328                 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3329                 if (res == NOT_ALLOWED)
3330                         return NOT_ALLOWED;
3331                 if (res == NO_IR)
3332                         ret = NO_IR;
3333
3334                 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3335                         return NOT_ALLOWED;
3336                 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3337                         return NOT_ALLOWED;
3338                 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3339                         return NOT_ALLOWED;
3340                 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3341                         return NOT_ALLOWED;
3342         }
3343
3344         return ret;
3345 }
3346
3347
3348 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3349                                                  struct hostapd_hw_modes *mode,
3350                                                  u8 channel, u8 bw)
3351 {
3352         int flag = 0;
3353         enum chan_allowed res, res2;
3354
3355         res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3356         if (bw == BW40MINUS) {
3357                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3358                         return NOT_ALLOWED;
3359                 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3360         } else if (bw == BW40PLUS) {
3361                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3362                         return NOT_ALLOWED;
3363                 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3364         } else if (bw == BW80) {
3365                 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3366         }
3367
3368         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3369                 return NOT_ALLOWED;
3370         if (res == NO_IR || res2 == NO_IR)
3371                 return NO_IR;
3372         return res;
3373 }
3374
3375
3376 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3377                                    struct p2p_channels *chan,
3378                                    struct p2p_channels *cli_chan)
3379 {
3380         struct hostapd_hw_modes *mode;
3381         int cla, op, cli_cla;
3382
3383         if (wpa_s->hw.modes == NULL) {
3384                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3385                            "of all supported channels; assume dualband "
3386                            "support");
3387                 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3388         }
3389
3390         cla = cli_cla = 0;
3391
3392         for (op = 0; op_class[op].op_class; op++) {
3393                 const struct p2p_oper_class_map *o = &op_class[op];
3394                 u8 ch;
3395                 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3396
3397                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3398                 if (mode == NULL)
3399                         continue;
3400                 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3401                         wpa_s->global->p2p_24ghz_social_channels = 1;
3402                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3403                         enum chan_allowed res;
3404                         res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3405                         if (res == ALLOWED) {
3406                                 if (reg == NULL) {
3407                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3408                                                    o->op_class);
3409                                         reg = &chan->reg_class[cla];
3410                                         cla++;
3411                                         reg->reg_class = o->op_class;
3412                                 }
3413                                 reg->channel[reg->channels] = ch;
3414                                 reg->channels++;
3415                         } else if (res == NO_IR &&
3416                                    wpa_s->conf->p2p_add_cli_chan) {
3417                                 if (cli_reg == NULL) {
3418                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3419                                                    o->op_class);
3420                                         cli_reg = &cli_chan->reg_class[cli_cla];
3421                                         cli_cla++;
3422                                         cli_reg->reg_class = o->op_class;
3423                                 }
3424                                 cli_reg->channel[cli_reg->channels] = ch;
3425                                 cli_reg->channels++;
3426                         }
3427                 }
3428                 if (reg) {
3429                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3430                                     reg->channel, reg->channels);
3431                 }
3432                 if (cli_reg) {
3433                         wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3434                                     cli_reg->channel, cli_reg->channels);
3435                 }
3436         }
3437
3438         chan->reg_classes = cla;
3439         cli_chan->reg_classes = cli_cla;
3440
3441         return 0;
3442 }
3443
3444
3445 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3446                            struct hostapd_hw_modes *mode, u8 channel)
3447 {
3448         int op;
3449         enum chan_allowed ret;
3450
3451         for (op = 0; op_class[op].op_class; op++) {
3452                 const struct p2p_oper_class_map *o = &op_class[op];
3453                 u8 ch;
3454
3455                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3456                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3457                             (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3458                             ch != channel)
3459                                 continue;
3460                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3461                         if (ret == ALLOWED)
3462                                 return (o->bw == BW40MINUS) ? -1 : 1;
3463                 }
3464         }
3465         return 0;
3466 }
3467
3468
3469 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3470                               struct hostapd_hw_modes *mode, u8 channel)
3471 {
3472         if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3473                 return 0;
3474
3475         return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3476 }
3477
3478
3479 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3480                         size_t buf_len)
3481 {
3482         struct wpa_supplicant *wpa_s = ctx;
3483
3484         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3485                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3486                         break;
3487         }
3488         if (wpa_s == NULL)
3489                 return -1;
3490
3491         return wpa_drv_get_noa(wpa_s, buf, buf_len);
3492 }
3493
3494
3495 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3496                                               const u8 *ssid, size_t ssid_len)
3497 {
3498         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3499                 struct wpa_ssid *s = wpa_s->current_ssid;
3500                 if (s == NULL)
3501                         continue;
3502                 if (s->mode != WPAS_MODE_P2P_GO &&
3503                     s->mode != WPAS_MODE_AP &&
3504                     s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3505                         continue;
3506                 if (s->ssid_len != ssid_len ||
3507                     os_memcmp(ssid, s->ssid, ssid_len) != 0)
3508                         continue;
3509                 return wpa_s;
3510         }
3511
3512         return NULL;
3513
3514 }
3515
3516
3517 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3518                                                   const u8 *peer_dev_addr)
3519 {
3520         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3521                 struct wpa_ssid *ssid = wpa_s->current_ssid;
3522                 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3523                         continue;
3524                 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3525                         return wpa_s;
3526         }
3527
3528         return NULL;
3529 }
3530
3531
3532 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3533 {
3534         struct wpa_supplicant *wpa_s = ctx;
3535
3536         return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3537 }
3538
3539
3540 static int wpas_is_concurrent_session_active(void *ctx)
3541 {
3542         struct wpa_supplicant *wpa_s = ctx;
3543         struct wpa_supplicant *ifs;
3544
3545         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3546                 if (ifs == wpa_s)
3547                         continue;
3548                 if (ifs->wpa_state > WPA_ASSOCIATED)
3549                         return 1;
3550         }
3551         return 0;
3552 }
3553
3554
3555 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3556 {
3557         struct wpa_supplicant *wpa_s = ctx;
3558         wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3559 }
3560
3561
3562 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3563                                   const char *conf_p2p_dev)
3564 {
3565         struct wpa_interface iface;
3566         struct wpa_supplicant *p2pdev_wpa_s;
3567         char ifname[100];
3568         char force_name[100];
3569         int ret;
3570
3571         ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3572                           wpa_s->ifname);
3573         if (os_snprintf_error(sizeof(ifname), ret))
3574                 return -1;
3575         force_name[0] = '\0';
3576         wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3577         ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3578                              force_name, wpa_s->pending_interface_addr, NULL);
3579         if (ret < 0) {
3580                 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3581                 return ret;
3582         }
3583         os_strlcpy(wpa_s->pending_interface_name, ifname,
3584                    sizeof(wpa_s->pending_interface_name));
3585
3586         os_memset(&iface, 0, sizeof(iface));
3587         iface.p2p_mgmt = 1;
3588         iface.ifname = wpa_s->pending_interface_name;
3589         iface.driver = wpa_s->driver->name;
3590         iface.driver_param = wpa_s->conf->driver_param;
3591
3592         /*
3593          * If a P2P Device configuration file was given, use it as the interface
3594          * configuration file (instead of using parent's configuration file.
3595          */
3596         if (conf_p2p_dev) {
3597                 iface.confname = conf_p2p_dev;
3598                 iface.ctrl_interface = NULL;
3599         } else {
3600                 iface.confname = wpa_s->confname;
3601                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3602         }
3603
3604         p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3605         if (!p2pdev_wpa_s) {
3606                 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3607                 return -1;
3608         }
3609
3610         wpa_s->pending_interface_name[0] = '\0';
3611         return 0;
3612 }
3613
3614
3615 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3616                                const u8 *noa, size_t noa_len)
3617 {
3618         struct wpa_supplicant *wpa_s, *intf = ctx;
3619         char hex[100];
3620
3621         for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3622                 if (wpa_s->waiting_presence_resp)
3623                         break;
3624         }
3625         if (!wpa_s) {
3626                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3627                 return;
3628         }
3629         wpa_s->waiting_presence_resp = 0;
3630
3631         wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3632         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3633                 " status=%u noa=%s", MAC2STR(src), status, hex);
3634 }
3635
3636
3637 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3638                                      size_t ssid_len, u8 *go_dev_addr,
3639                                      u8 *ret_ssid, size_t *ret_ssid_len,
3640                                      u8 *intended_iface_addr)
3641 {
3642         struct wpa_supplicant *wpa_s = ctx;
3643         struct wpa_ssid *s;
3644
3645         s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3646         if (s) {
3647                 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3648                 *ret_ssid_len = s->ssid_len;
3649                 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3650
3651                 if (s->mode != WPAS_MODE_P2P_GO) {
3652                         os_memset(intended_iface_addr, 0, ETH_ALEN);
3653                 } else if (wpas_p2p_create_iface(wpa_s)) {
3654                         if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3655                                 return 0;
3656
3657                         os_memcpy(intended_iface_addr,
3658                                   wpa_s->pending_interface_addr, ETH_ALEN);
3659                 } else {
3660                         os_memcpy(intended_iface_addr, wpa_s->own_addr,
3661                                   ETH_ALEN);
3662                 }
3663                 return 1;
3664         }
3665
3666         return 0;
3667 }
3668
3669
3670 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3671                             u8 *ssid, size_t *ssid_len, int *group_iface)
3672 {
3673         struct wpa_supplicant *wpa_s = ctx;
3674         struct wpa_ssid *s;
3675         u8 bssid[ETH_ALEN];
3676
3677         /*
3678          * group_iface will be set to 1 only if a dedicated interface for P2P
3679          * role is required. First, we try to reuse an active GO. However,
3680          * if it is not present, we will try to reactivate an existing
3681          * persistent group and set group_iface to 1, so the caller will know
3682          * that the pending interface should be used.
3683          */
3684         *group_iface = 0;
3685         s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3686         if (!s) {
3687                 s = wpas_p2p_get_persistent_go(wpa_s);
3688                 *group_iface = wpas_p2p_create_iface(wpa_s);
3689                 if (s)
3690                         os_memcpy(bssid, s->bssid, ETH_ALEN);
3691                 else
3692                         return 0;
3693         }
3694
3695         os_memcpy(intended_addr, bssid, ETH_ALEN);
3696         os_memcpy(ssid, s->ssid, s->ssid_len);
3697         *ssid_len = s->ssid_len;
3698
3699         return 1;
3700 }
3701
3702
3703 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3704                                     const u8 *ssid, size_t ssid_len)
3705 {
3706         struct wpa_supplicant *wpa_s = ctx;
3707         struct wpa_ssid *s;
3708         int save_config = 0;
3709         size_t i;
3710
3711         /* Start with our first choice of Persistent Groups */
3712         while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3713                 if (go && ssid && ssid_len &&
3714                     s->ssid_len == ssid_len &&
3715                     os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3716                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
3717                         break;
3718
3719                 /* Remove stale persistent group */
3720                 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3721                         wpa_config_remove_network(wpa_s->conf, s->id);
3722                         save_config = 1;
3723                         continue;
3724                 }
3725
3726                 for (i = 0; i < s->num_p2p_clients; i++) {
3727                         if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3728                                       peer, ETH_ALEN) != 0)
3729                                 continue;
3730
3731                         os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3732                                    s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3733                                    (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3734                         break;
3735                 }
3736                 s->num_p2p_clients--;
3737                 save_config = 1;
3738         }
3739
3740         if (save_config)
3741                 p2p_config_write(wpa_s);
3742
3743         /* Return TRUE if valid SSID remains */
3744         return s != NULL;
3745 }
3746
3747
3748 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3749                                        const u8 *feat_cap, size_t feat_cap_len)
3750 {
3751         static const char pref[] = " feature_cap=";
3752         int ret;
3753
3754         buf[0] = '\0';
3755
3756         /*
3757          * We expect a feature capability to contain at least one byte to be
3758          * reported. The string buffer provided by the caller function is
3759          * expected to be big enough to contain all bytes of the attribute for
3760          * known specifications. This function truncates the reported bytes if
3761          * the feature capability data exceeds the string buffer size.
3762          */
3763         if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3764                 return;
3765
3766         os_memcpy(buf, pref, sizeof(pref));
3767         ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3768                                buf_len - sizeof(pref) + 1,
3769                                feat_cap, feat_cap_len);
3770
3771         if (ret != (2 * (int) feat_cap_len))
3772                 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3773 }
3774
3775
3776 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3777                                     const u8 *adv_mac, const u8 *ses_mac,
3778                                     const u8 *grp_mac, u32 adv_id, u32 ses_id,
3779                                     u8 conncap, int passwd_id,
3780                                     const u8 *persist_ssid,
3781                                     size_t persist_ssid_size, int response_done,
3782                                     int prov_start, const char *session_info,
3783                                     const u8 *feat_cap, size_t feat_cap_len)
3784 {
3785         struct wpa_supplicant *wpa_s = ctx;
3786         u8 mac[ETH_ALEN];
3787         struct wpa_ssid *persistent_go, *stale, *s;
3788         int save_config = 0;
3789         struct wpa_supplicant *go_wpa_s;
3790         char feat_cap_str[256];
3791
3792         if (!dev)
3793                 return;
3794
3795         os_memset(mac, 0, ETH_ALEN);
3796         if (!adv_mac)
3797                 adv_mac = mac;
3798         if (!ses_mac)
3799                 ses_mac = mac;
3800         if (!grp_mac)
3801                 grp_mac = mac;
3802
3803         wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3804                                    feat_cap, feat_cap_len);
3805
3806         if (prov_start) {
3807                 if (session_info == NULL) {
3808                         wpa_msg_global(wpa_s, MSG_INFO,
3809                                        P2P_EVENT_P2PS_PROVISION_START MACSTR
3810                                        " adv_id=%x conncap=%x"
3811                                        " adv_mac=" MACSTR
3812                                        " session=%x mac=" MACSTR
3813                                        " dev_passwd_id=%d%s",
3814                                        MAC2STR(dev), adv_id, conncap,
3815                                        MAC2STR(adv_mac),
3816                                        ses_id, MAC2STR(ses_mac),
3817                                        passwd_id, feat_cap_str);
3818                 } else {
3819                         wpa_msg_global(wpa_s, MSG_INFO,
3820                                        P2P_EVENT_P2PS_PROVISION_START MACSTR
3821                                        " adv_id=%x conncap=%x"
3822                                        " adv_mac=" MACSTR
3823                                        " session=%x mac=" MACSTR
3824                                        " dev_passwd_id=%d info='%s'%s",
3825                                        MAC2STR(dev), adv_id, conncap,
3826                                        MAC2STR(adv_mac),
3827                                        ses_id, MAC2STR(ses_mac),
3828                                        passwd_id, session_info, feat_cap_str);
3829                 }
3830                 return;
3831         }
3832
3833         go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3834         persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3835
3836         if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3837                 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3838                         wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3839
3840                 if (persistent_go && !persistent_go->num_p2p_clients) {
3841                         /* remove empty persistent GO */
3842                         wpa_config_remove_network(wpa_s->conf,
3843                                                   persistent_go->id);
3844                 }
3845
3846                 wpa_msg_global(wpa_s, MSG_INFO,
3847                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3848                                " status=%d"
3849                                " adv_id=%x adv_mac=" MACSTR
3850                                " session=%x mac=" MACSTR "%s",
3851                                MAC2STR(dev), status,
3852                                adv_id, MAC2STR(adv_mac),
3853                                ses_id, MAC2STR(ses_mac), feat_cap_str);
3854                 return;
3855         }
3856
3857         /* Clean up stale persistent groups with this device */
3858         s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3859                                     persist_ssid_size);
3860
3861         if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
3862             is_zero_ether_addr(grp_mac)) {
3863                 wpa_dbg(wpa_s, MSG_ERROR,
3864                         "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
3865                 return;
3866         }
3867
3868         for (;;) {
3869                 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3870                 if (!stale)
3871                         break;
3872
3873                 if (s && s->ssid_len == stale->ssid_len &&
3874                     os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3875                     os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3876                         break;
3877
3878                 /* Remove stale persistent group */
3879                 if (stale->mode != WPAS_MODE_P2P_GO ||
3880                     stale->num_p2p_clients <= 1) {
3881                         wpa_config_remove_network(wpa_s->conf, stale->id);
3882                 } else {
3883                         size_t i;
3884
3885                         for (i = 0; i < stale->num_p2p_clients; i++) {
3886                                 if (os_memcmp(stale->p2p_client_list +
3887                                               i * ETH_ALEN,
3888                                               dev, ETH_ALEN) == 0) {
3889                                         os_memmove(stale->p2p_client_list +
3890                                                    i * ETH_ALEN,
3891                                                    stale->p2p_client_list +
3892                                                    (i + 1) * ETH_ALEN,
3893                                                    (stale->num_p2p_clients -
3894                                                     i - 1) * ETH_ALEN);
3895                                         break;
3896                                 }
3897                         }
3898                         stale->num_p2p_clients--;
3899                 }
3900                 save_config = 1;
3901         }
3902
3903         if (save_config)
3904                 p2p_config_write(wpa_s);
3905
3906         if (s) {
3907                 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3908                         wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3909
3910                 if (persistent_go && s != persistent_go &&
3911                     !persistent_go->num_p2p_clients) {
3912                         /* remove empty persistent GO */
3913                         wpa_config_remove_network(wpa_s->conf,
3914                                                   persistent_go->id);
3915                         /* Save config */
3916                 }
3917
3918                 wpa_msg_global(wpa_s, MSG_INFO,
3919                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3920                                " status=%d"
3921                                " adv_id=%x adv_mac=" MACSTR
3922                                " session=%x mac=" MACSTR
3923                                " persist=%d%s",
3924                                MAC2STR(dev), status,
3925                                adv_id, MAC2STR(adv_mac),
3926                                ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
3927                 return;
3928         }
3929
3930         if (conncap == P2PS_SETUP_GROUP_OWNER) {
3931                 /*
3932                  * We need to copy the interface name. Simply saving a
3933                  * pointer isn't enough, since if we use pending_interface_name
3934                  * it will be overwritten when the group is added.
3935                  */
3936                 char go_ifname[100];
3937
3938                 go_ifname[0] = '\0';
3939                 if (!go_wpa_s) {
3940                         wpa_s->global->pending_p2ps_group = 1;
3941
3942                         if (!wpas_p2p_create_iface(wpa_s))
3943                                 os_memcpy(go_ifname, wpa_s->ifname,
3944                                           sizeof(go_ifname));
3945                         else if (wpa_s->pending_interface_name[0])
3946                                 os_memcpy(go_ifname,
3947                                           wpa_s->pending_interface_name,
3948                                           sizeof(go_ifname));
3949
3950                         if (!go_ifname[0]) {
3951                                 wpas_p2ps_prov_complete(
3952                                         wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3953                                         dev, adv_mac, ses_mac,
3954                                         grp_mac, adv_id, ses_id, 0, 0,
3955                                         NULL, 0, 0, 0, NULL, NULL, 0);
3956                                 return;
3957                         }
3958
3959                         /* If PD Resp complete, start up the GO */
3960                         if (response_done && persistent_go) {
3961                                 wpas_p2p_group_add_persistent(
3962                                         wpa_s, persistent_go,
3963                                         0, 0, 0, 0, 0, NULL,
3964                                         persistent_go->mode ==
3965                                         WPAS_MODE_P2P_GO ?
3966                                         P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3967                                         0, 0);
3968                         } else if (response_done) {
3969                                 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3970                         }
3971
3972                         if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3973                                 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
3974                                           ETH_ALEN);
3975                                 wpa_s->p2ps_method_config_any = 1;
3976                         }
3977                 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3978                         os_memcpy(go_ifname, go_wpa_s->ifname,
3979                                   sizeof(go_ifname));
3980
3981                         if (is_zero_ether_addr(grp_mac)) {
3982                                 wpa_dbg(go_wpa_s, MSG_DEBUG,
3983                                         "P2P: Setting PIN-1 for ANY");
3984                                 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
3985                                                           "12345670", NULL, 0,
3986                                                           0);
3987                         } else {
3988                                 wpa_dbg(go_wpa_s, MSG_DEBUG,
3989                                         "P2P: Setting PIN-1 for " MACSTR,
3990                                         MAC2STR(grp_mac));
3991                                 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
3992                                                           "12345670", NULL, 0,
3993                                                           0);
3994                         }
3995
3996                         os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
3997                         wpa_s->p2ps_method_config_any = 1;
3998                 }
3999
4000                 wpa_msg_global(wpa_s, MSG_INFO,
4001                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4002                                " status=%d conncap=%x"
4003                                " adv_id=%x adv_mac=" MACSTR
4004                                " session=%x mac=" MACSTR
4005                                " dev_passwd_id=%d go=%s%s",
4006                                MAC2STR(dev), status, conncap,
4007                                adv_id, MAC2STR(adv_mac),
4008                                ses_id, MAC2STR(ses_mac),
4009                                passwd_id, go_ifname, feat_cap_str);
4010                 return;
4011         }
4012
4013         if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4014                 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4015
4016         if (persistent_go && !persistent_go->num_p2p_clients) {
4017                 /* remove empty persistent GO */
4018                 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4019         }
4020
4021         if (conncap == P2PS_SETUP_CLIENT) {
4022                 wpa_msg_global(wpa_s, MSG_INFO,
4023                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4024                                " status=%d conncap=%x"
4025                                " adv_id=%x adv_mac=" MACSTR
4026                                " session=%x mac=" MACSTR
4027                                " dev_passwd_id=%d join=" MACSTR "%s",
4028                                MAC2STR(dev), status, conncap,
4029                                adv_id, MAC2STR(adv_mac),
4030                                ses_id, MAC2STR(ses_mac),
4031                                passwd_id, MAC2STR(grp_mac), feat_cap_str);
4032         } else {
4033                 wpa_msg_global(wpa_s, MSG_INFO,
4034                                P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4035                                " status=%d conncap=%x"
4036                                " adv_id=%x adv_mac=" MACSTR
4037                                " session=%x mac=" MACSTR
4038                                " dev_passwd_id=%d%s",
4039                                MAC2STR(dev), status, conncap,
4040                                adv_id, MAC2STR(adv_mac),
4041                                ses_id, MAC2STR(ses_mac),
4042                                passwd_id, feat_cap_str);
4043         }
4044 }
4045
4046
4047 static int _wpas_p2p_in_progress(void *ctx)
4048 {
4049         struct wpa_supplicant *wpa_s = ctx;
4050         return wpas_p2p_in_progress(wpa_s);
4051 }
4052
4053
4054 static int wpas_prov_disc_resp_cb(void *ctx)
4055 {
4056         struct wpa_supplicant *wpa_s = ctx;
4057         struct wpa_ssid *persistent_go;
4058
4059         if (!wpa_s->global->pending_p2ps_group)
4060                 return 0;
4061
4062         wpa_s->global->pending_p2ps_group = 0;
4063
4064         if (wpas_p2p_get_go_group(wpa_s))
4065                 return 0;
4066         persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4067
4068         if (persistent_go) {
4069                 wpas_p2p_group_add_persistent(
4070                         wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
4071                         persistent_go->mode == WPAS_MODE_P2P_GO ?
4072                         P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4073         } else {
4074                 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4075         }
4076
4077         return 1;
4078 }
4079
4080
4081 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4082                                        unsigned int *len,
4083                                        unsigned int *freq_list)
4084 {
4085         struct wpa_supplicant *wpa_s = ctx;
4086
4087         return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4088                                           WPA_IF_P2P_CLIENT, len, freq_list);
4089 }
4090
4091
4092 /**
4093  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4094  * @global: Pointer to global data from wpa_supplicant_init()
4095  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4096  * Returns: 0 on success, -1 on failure
4097  */
4098 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4099 {
4100         struct p2p_config p2p;
4101         int i;
4102
4103         if (wpa_s->conf->p2p_disabled)
4104                 return 0;
4105
4106         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4107                 return 0;
4108
4109         if (global->p2p)
4110                 return 0;
4111
4112         os_memset(&p2p, 0, sizeof(p2p));
4113         p2p.cb_ctx = wpa_s;
4114         p2p.debug_print = wpas_p2p_debug_print;
4115         p2p.p2p_scan = wpas_p2p_scan;
4116         p2p.send_action = wpas_send_action;
4117         p2p.send_action_done = wpas_send_action_done;
4118         p2p.go_neg_completed = wpas_go_neg_completed;
4119         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4120         p2p.dev_found = wpas_dev_found;
4121         p2p.dev_lost = wpas_dev_lost;
4122         p2p.find_stopped = wpas_find_stopped;
4123         p2p.start_listen = wpas_start_listen;
4124         p2p.stop_listen = wpas_stop_listen;
4125         p2p.send_probe_resp = wpas_send_probe_resp;
4126         p2p.sd_request = wpas_sd_request;
4127         p2p.sd_response = wpas_sd_response;
4128         p2p.prov_disc_req = wpas_prov_disc_req;
4129         p2p.prov_disc_resp = wpas_prov_disc_resp;
4130         p2p.prov_disc_fail = wpas_prov_disc_fail;
4131         p2p.invitation_process = wpas_invitation_process;
4132         p2p.invitation_received = wpas_invitation_received;
4133         p2p.invitation_result = wpas_invitation_result;
4134         p2p.get_noa = wpas_get_noa;
4135         p2p.go_connected = wpas_go_connected;
4136         p2p.presence_resp = wpas_presence_resp;
4137         p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4138         p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4139         p2p.get_persistent_group = wpas_get_persistent_group;
4140         p2p.get_go_info = wpas_get_go_info;
4141         p2p.remove_stale_groups = wpas_remove_stale_groups;
4142         p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4143         p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4144         p2p.p2ps_group_capability = p2ps_group_capability;
4145         p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4146
4147         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4148         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4149         p2p.dev_name = wpa_s->conf->device_name;
4150         p2p.manufacturer = wpa_s->conf->manufacturer;
4151         p2p.model_name = wpa_s->conf->model_name;
4152         p2p.model_number = wpa_s->conf->model_number;
4153         p2p.serial_number = wpa_s->conf->serial_number;
4154         if (wpa_s->wps) {
4155                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4156                 p2p.config_methods = wpa_s->wps->config_methods;
4157         }
4158
4159         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4160                 wpa_printf(MSG_ERROR,
4161                            "P2P: Failed to configure supported channel list");
4162                 return -1;
4163         }
4164
4165         if (wpa_s->conf->p2p_listen_reg_class &&
4166             wpa_s->conf->p2p_listen_channel) {
4167                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4168                 p2p.channel = wpa_s->conf->p2p_listen_channel;
4169                 p2p.channel_forced = 1;
4170         } else {
4171                 /*
4172                  * Pick one of the social channels randomly as the listen
4173                  * channel.
4174                  */
4175                 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4176                                                  &p2p.channel) != 0) {
4177                         wpa_printf(MSG_INFO,
4178                                    "P2P: No social channels supported by the driver - do not enable P2P");
4179                         return 0;
4180                 }
4181                 p2p.channel_forced = 0;
4182         }
4183         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4184                    p2p.reg_class, p2p.channel);
4185
4186         if (wpa_s->conf->p2p_oper_reg_class &&
4187             wpa_s->conf->p2p_oper_channel) {
4188                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4189                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4190                 p2p.cfg_op_channel = 1;
4191                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4192                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
4193
4194         } else {
4195                 /*
4196                  * Use random operation channel from 2.4 GHz band social
4197                  * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4198                  * other preference is indicated.
4199                  */
4200                 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4201                                                  &p2p.op_channel) != 0) {
4202                         wpa_printf(MSG_ERROR,
4203                                    "P2P: Failed to select random social channel as operation channel");
4204                         return -1;
4205                 }
4206                 p2p.cfg_op_channel = 0;
4207                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4208                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
4209         }
4210
4211         if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4212                 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4213                 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4214         }
4215
4216         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4217                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4218                 p2p.country[2] = 0x04;
4219         } else
4220                 os_memcpy(p2p.country, "XX\x04", 3);
4221
4222         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4223                   WPS_DEV_TYPE_LEN);
4224
4225         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4226         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4227                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4228
4229         p2p.concurrent_operations = !!(wpa_s->drv_flags &
4230                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4231
4232         p2p.max_peers = 100;
4233
4234         if (wpa_s->conf->p2p_ssid_postfix) {
4235                 p2p.ssid_postfix_len =
4236                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
4237                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4238                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4239                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4240                           p2p.ssid_postfix_len);
4241         }
4242
4243         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4244
4245         p2p.max_listen = wpa_s->max_remain_on_chan;
4246
4247         if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4248             wpa_s->conf->p2p_passphrase_len <= 63)
4249                 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4250         else
4251                 p2p.passphrase_len = 8;
4252
4253         global->p2p = p2p_init(&p2p);
4254         if (global->p2p == NULL)
4255                 return -1;
4256         global->p2p_init_wpa_s = wpa_s;
4257
4258         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4259                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4260                         continue;
4261                 p2p_add_wps_vendor_extension(
4262                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4263         }
4264
4265         p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4266
4267         return 0;
4268 }
4269
4270
4271 /**
4272  * wpas_p2p_deinit - Deinitialize per-interface P2P data
4273  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4274  *
4275  * This function deinitialize per-interface P2P data.
4276  */
4277 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4278 {
4279         if (wpa_s->driver && wpa_s->drv_priv)
4280                 wpa_drv_probe_req_report(wpa_s, 0);
4281
4282         if (wpa_s->go_params) {
4283                 /* Clear any stored provisioning info */
4284                 p2p_clear_provisioning_info(
4285                         wpa_s->global->p2p,
4286                         wpa_s->go_params->peer_device_addr);
4287         }
4288
4289         os_free(wpa_s->go_params);
4290         wpa_s->go_params = NULL;
4291         eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4292         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4293         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4294         wpa_s->p2p_long_listen = 0;
4295         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4296         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4297         wpas_p2p_remove_pending_group_interface(wpa_s);
4298         eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4299         wpas_p2p_listen_work_done(wpa_s);
4300         if (wpa_s->p2p_send_action_work) {
4301                 os_free(wpa_s->p2p_send_action_work->ctx);
4302                 radio_work_done(wpa_s->p2p_send_action_work);
4303                 wpa_s->p2p_send_action_work = NULL;
4304         }
4305         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4306
4307         wpabuf_free(wpa_s->p2p_oob_dev_pw);
4308         wpa_s->p2p_oob_dev_pw = NULL;
4309
4310         os_free(wpa_s->p2p_group_common_freqs);
4311         wpa_s->p2p_group_common_freqs = NULL;
4312         wpa_s->p2p_group_common_freqs_num = 0;
4313
4314         /* TODO: remove group interface from the driver if this wpa_s instance
4315          * is on top of a P2P group interface */
4316 }
4317
4318
4319 /**
4320  * wpas_p2p_deinit_global - Deinitialize global P2P module
4321  * @global: Pointer to global data from wpa_supplicant_init()
4322  *
4323  * This function deinitializes the global (per device) P2P module.
4324  */
4325 static void wpas_p2p_deinit_global(struct wpa_global *global)
4326 {
4327         struct wpa_supplicant *wpa_s, *tmp;
4328
4329         wpa_s = global->ifaces;
4330
4331         wpas_p2p_service_flush(global->p2p_init_wpa_s);
4332
4333         /* Remove remaining P2P group interfaces */
4334         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4335                 wpa_s = wpa_s->next;
4336         while (wpa_s) {
4337                 tmp = global->ifaces;
4338                 while (tmp &&
4339                        (tmp == wpa_s ||
4340                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4341                         tmp = tmp->next;
4342                 }
4343                 if (tmp == NULL)
4344                         break;
4345                 /* Disconnect from the P2P group and deinit the interface */
4346                 wpas_p2p_disconnect(tmp);
4347         }
4348
4349         /*
4350          * Deinit GO data on any possibly remaining interface (if main
4351          * interface is used as GO).
4352          */
4353         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4354                 if (wpa_s->ap_iface)
4355                         wpas_p2p_group_deinit(wpa_s);
4356         }
4357
4358         p2p_deinit(global->p2p);
4359         global->p2p = NULL;
4360         global->p2p_init_wpa_s = NULL;
4361 }
4362
4363
4364 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4365 {
4366         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4367             wpa_s->conf->p2p_no_group_iface)
4368                 return 0; /* separate interface disabled per configuration */
4369         if (wpa_s->drv_flags &
4370             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4371              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4372                 return 1; /* P2P group requires a new interface in every case
4373                            */
4374         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4375                 return 0; /* driver does not support concurrent operations */
4376         if (wpa_s->global->ifaces->next)
4377                 return 1; /* more that one interface already in use */
4378         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4379                 return 1; /* this interface is already in use */
4380         return 0;
4381 }
4382
4383
4384 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4385                                  const u8 *peer_addr,
4386                                  enum p2p_wps_method wps_method,
4387                                  int go_intent, const u8 *own_interface_addr,
4388                                  unsigned int force_freq, int persistent_group,
4389                                  struct wpa_ssid *ssid, unsigned int pref_freq)
4390 {
4391         if (persistent_group && wpa_s->conf->persistent_reconnect)
4392                 persistent_group = 2;
4393
4394         /*
4395          * Increase GO config timeout if HT40 is used since it takes some time
4396          * to scan channels for coex purposes before the BSS can be started.
4397          */
4398         p2p_set_config_timeout(wpa_s->global->p2p,
4399                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4400
4401         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4402                            go_intent, own_interface_addr, force_freq,
4403                            persistent_group, ssid ? ssid->ssid : NULL,
4404                            ssid ? ssid->ssid_len : 0,
4405                            wpa_s->p2p_pd_before_go_neg, pref_freq,
4406                            wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4407                            0);
4408 }
4409
4410
4411 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4412                                 const u8 *peer_addr,
4413                                 enum p2p_wps_method wps_method,
4414                                 int go_intent, const u8 *own_interface_addr,
4415                                 unsigned int force_freq, int persistent_group,
4416                                 struct wpa_ssid *ssid, unsigned int pref_freq)
4417 {
4418         if (persistent_group && wpa_s->conf->persistent_reconnect)
4419                 persistent_group = 2;
4420
4421         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4422                              go_intent, own_interface_addr, force_freq,
4423                              persistent_group, ssid ? ssid->ssid : NULL,
4424                              ssid ? ssid->ssid_len : 0, pref_freq,
4425                              wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4426                              0);
4427 }
4428
4429
4430 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4431 {
4432         wpa_s->p2p_join_scan_count++;
4433         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4434                    wpa_s->p2p_join_scan_count);
4435         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4436                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4437                            " for join operationg - stop join attempt",
4438                            MAC2STR(wpa_s->pending_join_iface_addr));
4439                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4440                 if (wpa_s->p2p_auto_pd) {
4441                         wpa_s->p2p_auto_pd = 0;
4442                         wpa_msg_global(wpa_s, MSG_INFO,
4443                                        P2P_EVENT_PROV_DISC_FAILURE
4444                                        " p2p_dev_addr=" MACSTR " status=N/A",
4445                                        MAC2STR(wpa_s->pending_join_dev_addr));
4446                         return;
4447                 }
4448                 wpa_msg_global(wpa_s->parent, MSG_INFO,
4449                                P2P_EVENT_GROUP_FORMATION_FAILURE);
4450                 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4451         }
4452 }
4453
4454
4455 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4456 {
4457         int res;
4458         unsigned int num, i;
4459         struct wpa_used_freq_data *freqs;
4460
4461         if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4462                 /* Multiple channels are supported and not all are in use */
4463                 return 0;
4464         }
4465
4466         freqs = os_calloc(wpa_s->num_multichan_concurrent,
4467                           sizeof(struct wpa_used_freq_data));
4468         if (!freqs)
4469                 return 1;
4470
4471         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4472                                         wpa_s->num_multichan_concurrent);
4473
4474         for (i = 0; i < num; i++) {
4475                 if (freqs[i].freq == freq) {
4476                         wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4477                                    freq);
4478                         res = 0;
4479                         goto exit_free;
4480                 }
4481         }
4482
4483         wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4484         res = 1;
4485
4486 exit_free:
4487         os_free(freqs);
4488         return res;
4489 }
4490
4491
4492 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4493                             const u8 *peer_dev_addr)
4494 {
4495         struct wpa_bss *bss;
4496         int updated;
4497
4498         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4499         if (bss == NULL)
4500                 return -1;
4501         if (bss->last_update_idx < wpa_s->bss_update_idx) {
4502                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4503                            "last scan");
4504                 return 0;
4505         }
4506
4507         updated = os_reltime_before(&wpa_s->p2p_auto_started,
4508                                     &bss->last_update);
4509         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4510                    "%ld.%06ld (%supdated in last scan)",
4511                    bss->last_update.sec, bss->last_update.usec,
4512                    updated ? "": "not ");
4513
4514         return updated;
4515 }
4516
4517
4518 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4519                                    struct wpa_scan_results *scan_res)
4520 {
4521         struct wpa_bss *bss = NULL;
4522         int freq;
4523         u8 iface_addr[ETH_ALEN];
4524
4525         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4526
4527         if (wpa_s->global->p2p_disabled)
4528                 return;
4529
4530         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4531                    scan_res ? (int) scan_res->num : -1,
4532                    wpa_s->p2p_auto_join ? "auto_" : "");
4533
4534         if (scan_res)
4535                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4536
4537         if (wpa_s->p2p_auto_pd) {
4538                 int join = wpas_p2p_peer_go(wpa_s,
4539                                             wpa_s->pending_join_dev_addr);
4540                 if (join == 0 &&
4541                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4542                         wpa_s->auto_pd_scan_retry++;
4543                         bss = wpa_bss_get_bssid_latest(
4544                                 wpa_s, wpa_s->pending_join_dev_addr);
4545                         if (bss) {
4546                                 freq = bss->freq;
4547                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4548                                            "the peer " MACSTR " at %d MHz",
4549                                            wpa_s->auto_pd_scan_retry,
4550                                            MAC2STR(wpa_s->
4551                                                    pending_join_dev_addr),
4552                                            freq);
4553                                 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4554                                 return;
4555                         }
4556                 }
4557
4558                 if (join < 0)
4559                         join = 0;
4560
4561                 wpa_s->p2p_auto_pd = 0;
4562                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4563                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4564                            MAC2STR(wpa_s->pending_join_dev_addr), join);
4565                 if (p2p_prov_disc_req(wpa_s->global->p2p,
4566                                       wpa_s->pending_join_dev_addr, NULL,
4567                                       wpa_s->pending_pd_config_methods, join,
4568                                       0, wpa_s->user_initiated_pd) < 0) {
4569                         wpa_s->p2p_auto_pd = 0;
4570                         wpa_msg_global(wpa_s, MSG_INFO,
4571                                        P2P_EVENT_PROV_DISC_FAILURE
4572                                        " p2p_dev_addr=" MACSTR " status=N/A",
4573                                        MAC2STR(wpa_s->pending_join_dev_addr));
4574                 }
4575                 return;
4576         }
4577
4578         if (wpa_s->p2p_auto_join) {
4579                 int join = wpas_p2p_peer_go(wpa_s,
4580                                             wpa_s->pending_join_dev_addr);
4581                 if (join < 0) {
4582                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4583                                    "running a GO -> use GO Negotiation");
4584                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4585                                        P2P_EVENT_FALLBACK_TO_GO_NEG
4586                                        "reason=peer-not-running-GO");
4587                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4588                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4589                                          wpa_s->p2p_persistent_group, 0, 0, 0,
4590                                          wpa_s->p2p_go_intent,
4591                                          wpa_s->p2p_connect_freq,
4592                                          wpa_s->p2p_persistent_id,
4593                                          wpa_s->p2p_pd_before_go_neg,
4594                                          wpa_s->p2p_go_ht40,
4595                                          wpa_s->p2p_go_vht);
4596                         return;
4597                 }
4598
4599                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4600                            "try to join the group", join ? "" :
4601                            " in older scan");
4602                 if (!join) {
4603                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4604                                        P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
4605                         wpa_s->p2p_fallback_to_go_neg = 1;
4606                 }
4607         }
4608
4609         freq = p2p_get_oper_freq(wpa_s->global->p2p,
4610                                  wpa_s->pending_join_iface_addr);
4611         if (freq < 0 &&
4612             p2p_get_interface_addr(wpa_s->global->p2p,
4613                                    wpa_s->pending_join_dev_addr,
4614                                    iface_addr) == 0 &&
4615             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4616             && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4617                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4618                            "address for join from " MACSTR " to " MACSTR
4619                            " based on newly discovered P2P peer entry",
4620                            MAC2STR(wpa_s->pending_join_iface_addr),
4621                            MAC2STR(iface_addr));
4622                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4623                           ETH_ALEN);
4624
4625                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4626                                          wpa_s->pending_join_iface_addr);
4627         }
4628         if (freq >= 0) {
4629                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4630                            "from P2P peer table: %d MHz", freq);
4631         }
4632         if (wpa_s->p2p_join_ssid_len) {
4633                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4634                            MACSTR " and SSID %s",
4635                            MAC2STR(wpa_s->pending_join_iface_addr),
4636                            wpa_ssid_txt(wpa_s->p2p_join_ssid,
4637                                         wpa_s->p2p_join_ssid_len));
4638                 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4639                                   wpa_s->p2p_join_ssid,
4640                                   wpa_s->p2p_join_ssid_len);
4641         }
4642         if (!bss) {
4643                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4644                            MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4645                 bss = wpa_bss_get_bssid_latest(wpa_s,
4646                                                wpa_s->pending_join_iface_addr);
4647         }
4648         if (bss) {
4649                 u8 dev_addr[ETH_ALEN];
4650
4651                 freq = bss->freq;
4652                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4653                            "from BSS table: %d MHz (SSID %s)", freq,
4654                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
4655                 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4656                                        dev_addr) == 0 &&
4657                     os_memcmp(wpa_s->pending_join_dev_addr,
4658                               wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4659                     os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4660                               ETH_ALEN) != 0) {
4661                         wpa_printf(MSG_DEBUG,
4662                                    "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4663                                    MAC2STR(dev_addr),
4664                                    MAC2STR(wpa_s->pending_join_dev_addr));
4665                         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4666                                   ETH_ALEN);
4667                 }
4668         }
4669         if (freq > 0) {
4670                 u16 method;
4671
4672                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4673                         wpa_msg_global(wpa_s->parent, MSG_INFO,
4674                                        P2P_EVENT_GROUP_FORMATION_FAILURE
4675                                        "reason=FREQ_CONFLICT");
4676                         wpas_notify_p2p_group_formation_failure(
4677                                 wpa_s, "FREQ_CONFLICT");
4678                         return;
4679                 }
4680
4681                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4682                            "prior to joining an existing group (GO " MACSTR
4683                            " freq=%u MHz)",
4684                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
4685                 wpa_s->pending_pd_before_join = 1;
4686
4687                 switch (wpa_s->pending_join_wps_method) {
4688                 case WPS_PIN_DISPLAY:
4689                         method = WPS_CONFIG_KEYPAD;
4690                         break;
4691                 case WPS_PIN_KEYPAD:
4692                         method = WPS_CONFIG_DISPLAY;
4693                         break;
4694                 case WPS_PBC:
4695                         method = WPS_CONFIG_PUSHBUTTON;
4696                         break;
4697                 case WPS_P2PS:
4698                         method = WPS_CONFIG_P2PS;
4699                         break;
4700                 default:
4701                         method = 0;
4702                         break;
4703                 }
4704
4705                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4706                                                wpa_s->pending_join_dev_addr) ==
4707                      method)) {
4708                         /*
4709                          * We have already performed provision discovery for
4710                          * joining the group. Proceed directly to join
4711                          * operation without duplicated provision discovery. */
4712                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4713                                    "with " MACSTR " already done - proceed to "
4714                                    "join",
4715                                    MAC2STR(wpa_s->pending_join_dev_addr));
4716                         wpa_s->pending_pd_before_join = 0;
4717                         goto start;
4718                 }
4719
4720                 if (p2p_prov_disc_req(wpa_s->global->p2p,
4721                                       wpa_s->pending_join_dev_addr,
4722                                       NULL, method, 1,
4723                                       freq, wpa_s->user_initiated_pd) < 0) {
4724                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4725                                    "Discovery Request before joining an "
4726                                    "existing group");
4727                         wpa_s->pending_pd_before_join = 0;
4728                         goto start;
4729                 }
4730                 return;
4731         }
4732
4733         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4734         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4735         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4736         wpas_p2p_check_join_scan_limit(wpa_s);
4737         return;
4738
4739 start:
4740         /* Start join operation immediately */
4741         wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4742 }
4743
4744
4745 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4746                                    const u8 *ssid, size_t ssid_len)
4747 {
4748         int ret;
4749         struct wpa_driver_scan_params params;
4750         struct wpabuf *wps_ie, *ies;
4751         size_t ielen;
4752         int freqs[2] = { 0, 0 };
4753
4754         os_memset(&params, 0, sizeof(params));
4755
4756         /* P2P Wildcard SSID */
4757         params.num_ssids = 1;
4758         if (ssid && ssid_len) {
4759                 params.ssids[0].ssid = ssid;
4760                 params.ssids[0].ssid_len = ssid_len;
4761                 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4762                 wpa_s->p2p_join_ssid_len = ssid_len;
4763         } else {
4764                 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4765                 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4766                 wpa_s->p2p_join_ssid_len = 0;
4767         }
4768
4769         wpa_s->wps->dev.p2p = 1;
4770         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4771                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4772                                         NULL);
4773         if (wps_ie == NULL) {
4774                 wpas_p2p_scan_res_join(wpa_s, NULL);
4775                 return;
4776         }
4777
4778         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4779         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4780         if (ies == NULL) {
4781                 wpabuf_free(wps_ie);
4782                 wpas_p2p_scan_res_join(wpa_s, NULL);
4783                 return;
4784         }
4785         wpabuf_put_buf(ies, wps_ie);
4786         wpabuf_free(wps_ie);
4787
4788         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4789
4790         params.p2p_probe = 1;
4791         params.extra_ies = wpabuf_head(ies);
4792         params.extra_ies_len = wpabuf_len(ies);
4793
4794         if (!freq) {
4795                 int oper_freq;
4796                 /*
4797                  * If freq is not provided, check the operating freq of the GO
4798                  * and use a single channel scan on if possible.
4799                  */
4800                 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4801                                               wpa_s->pending_join_iface_addr);
4802                 if (oper_freq > 0)
4803                         freq = oper_freq;
4804         }
4805         if (freq > 0) {
4806                 freqs[0] = freq;
4807                 params.freqs = freqs;
4808         }
4809
4810         /*
4811          * Run a scan to update BSS table and start Provision Discovery once
4812          * the new scan results become available.
4813          */
4814         ret = wpa_drv_scan(wpa_s, &params);
4815         if (!ret) {
4816                 os_get_reltime(&wpa_s->scan_trigger_time);
4817                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4818                 wpa_s->own_scan_requested = 1;
4819         }
4820
4821         wpabuf_free(ies);
4822
4823         if (ret) {
4824                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4825                            "try again later");
4826                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4827                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4828                 wpas_p2p_check_join_scan_limit(wpa_s);
4829         }
4830 }
4831
4832
4833 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4834 {
4835         struct wpa_supplicant *wpa_s = eloop_ctx;
4836         wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4837 }
4838
4839
4840 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4841                          const u8 *dev_addr, enum p2p_wps_method wps_method,
4842                          int auto_join, int op_freq,
4843                          const u8 *ssid, size_t ssid_len)
4844 {
4845         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4846                    MACSTR " dev " MACSTR " op_freq=%d)%s",
4847                    MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4848                    auto_join ? " (auto_join)" : "");
4849         if (ssid && ssid_len) {
4850                 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4851                            wpa_ssid_txt(ssid, ssid_len));
4852         }
4853
4854         wpa_s->p2p_auto_pd = 0;
4855         wpa_s->p2p_auto_join = !!auto_join;
4856         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4857         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4858         wpa_s->pending_join_wps_method = wps_method;
4859
4860         /* Make sure we are not running find during connection establishment */
4861         wpas_p2p_stop_find(wpa_s);
4862
4863         wpa_s->p2p_join_scan_count = 0;
4864         wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4865         return 0;
4866 }
4867
4868
4869 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4870                                const u8 *ssid, size_t ssid_len)
4871 {
4872         struct wpa_supplicant *group;
4873         struct p2p_go_neg_results res;
4874         struct wpa_bss *bss;
4875
4876         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4877         if (group == NULL)
4878                 return -1;
4879         if (group != wpa_s) {
4880                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4881                           sizeof(group->p2p_pin));
4882                 group->p2p_wps_method = wpa_s->p2p_wps_method;
4883         } else {
4884                 /*
4885                  * Need to mark the current interface for p2p_group_formation
4886                  * when a separate group interface is not used. This is needed
4887                  * to allow p2p_cancel stop a pending p2p_connect-join.
4888                  * wpas_p2p_init_group_interface() addresses this for the case
4889                  * where a separate group interface is used.
4890                  */
4891                 wpa_s->global->p2p_group_formation = wpa_s;
4892         }
4893
4894         group->p2p_in_provisioning = 1;
4895         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4896
4897         os_memset(&res, 0, sizeof(res));
4898         os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4899         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4900                   ETH_ALEN);
4901         res.wps_method = wpa_s->pending_join_wps_method;
4902         if (freq && ssid && ssid_len) {
4903                 res.freq = freq;
4904                 res.ssid_len = ssid_len;
4905                 os_memcpy(res.ssid, ssid, ssid_len);
4906         } else {
4907                 bss = wpa_bss_get_bssid_latest(wpa_s,
4908                                                wpa_s->pending_join_iface_addr);
4909                 if (bss) {
4910                         res.freq = bss->freq;
4911                         res.ssid_len = bss->ssid_len;
4912                         os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4913                         wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4914                                    bss->freq,
4915                                    wpa_ssid_txt(bss->ssid, bss->ssid_len));
4916                 }
4917         }
4918
4919         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4920                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4921                            "starting client");
4922                 wpa_drv_cancel_remain_on_channel(wpa_s);
4923                 wpa_s->off_channel_freq = 0;
4924                 wpa_s->roc_waiting_drv_freq = 0;
4925         }
4926         wpas_start_wps_enrollee(group, &res);
4927
4928         /*
4929          * Allow a longer timeout for join-a-running-group than normal 15
4930          * second group formation timeout since the GO may not have authorized
4931          * our connection yet.
4932          */
4933         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4934         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4935                                wpa_s, NULL);
4936
4937         return 0;
4938 }
4939
4940
4941 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4942                                 int *force_freq, int *pref_freq, int go,
4943                                 unsigned int *pref_freq_list,
4944                                 unsigned int *num_pref_freq)
4945 {
4946         struct wpa_used_freq_data *freqs;
4947         int res, best_freq, num_unused;
4948         unsigned int freq_in_use = 0, num, i, max_pref_freq;
4949
4950         max_pref_freq = *num_pref_freq;
4951         *num_pref_freq = 0;
4952
4953         freqs = os_calloc(wpa_s->num_multichan_concurrent,
4954                           sizeof(struct wpa_used_freq_data));
4955         if (!freqs)
4956                 return -1;
4957
4958         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4959                                         wpa_s->num_multichan_concurrent);
4960
4961         /*
4962          * It is possible that the total number of used frequencies is bigger
4963          * than the number of frequencies used for P2P, so get the system wide
4964          * number of unused frequencies.
4965          */
4966         num_unused = wpas_p2p_num_unused_channels(wpa_s);
4967
4968         wpa_printf(MSG_DEBUG,
4969                    "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4970                    freq, wpa_s->num_multichan_concurrent, num, num_unused);
4971
4972         if (freq > 0) {
4973                 int ret;
4974                 if (go)
4975                         ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4976                 else
4977                         ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4978                 if (!ret) {
4979                         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4980                             ieee80211_is_dfs(freq)) {
4981                                 /*
4982                                  * If freq is a DFS channel and DFS is offloaded
4983                                  * to the driver, allow P2P GO to use it.
4984                                  */
4985                                 wpa_printf(MSG_DEBUG,
4986                                            "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4987                                            freq);
4988                         } else {
4989                                 wpa_printf(MSG_DEBUG,
4990                                            "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4991                                            freq);
4992                                 res = -3;
4993                                 goto exit_free;
4994                         }
4995                 }
4996
4997                 for (i = 0; i < num; i++) {
4998                         if (freqs[i].freq == freq)
4999                                 freq_in_use = 1;
5000                 }
5001
5002                 if (num_unused <= 0 && !freq_in_use) {
5003                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5004                                    freq);
5005                         res = -2;
5006                         goto exit_free;
5007                 }
5008                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5009                            "requested channel (%u MHz)", freq);
5010                 *force_freq = freq;
5011                 goto exit_ok;
5012         }
5013
5014         best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5015
5016         if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5017                 enum wpa_driver_if_type iface_type;
5018
5019                 if (go)
5020                         iface_type = WPA_IF_P2P_GO;
5021                 else
5022                         iface_type = WPA_IF_P2P_CLIENT;
5023
5024                 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5025                            best_freq, go);
5026
5027                 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5028                                                  &max_pref_freq,
5029                                                  pref_freq_list);
5030                 if (!res && max_pref_freq > 0) {
5031                         *num_pref_freq = max_pref_freq;
5032                         i = 0;
5033                         while (wpas_p2p_disallowed_freq(wpa_s->global,
5034                                                         pref_freq_list[i]) &&
5035                                i < *num_pref_freq) {
5036                                 wpa_printf(MSG_DEBUG,
5037                                            "P2P: preferred_freq_list[%d]=%d is disallowed",
5038                                            i, pref_freq_list[i]);
5039                                 i++;
5040                         }
5041                         if (i != *num_pref_freq) {
5042                                 best_freq = pref_freq_list[i];
5043                                 wpa_printf(MSG_DEBUG,
5044                                            "P2P: Using preferred_freq_list[%d]=%d",
5045                                            i, best_freq);
5046                         } else {
5047                                 wpa_printf(MSG_DEBUG,
5048                                            "P2P: All driver preferred frequencies are disallowed for P2P use");
5049                                 *num_pref_freq = 0;
5050                         }
5051                 } else {
5052                         wpa_printf(MSG_DEBUG,
5053                                    "P2P: No preferred frequency list available");
5054                 }
5055         }
5056
5057         /* We have a candidate frequency to use */
5058         if (best_freq > 0) {
5059                 if (*pref_freq == 0 && num_unused > 0) {
5060                         wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5061                                    best_freq);
5062                         *pref_freq = best_freq;
5063                 } else {
5064                         wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5065                                    best_freq);
5066                         *force_freq = best_freq;
5067                 }
5068         } else if (num_unused > 0) {
5069                 wpa_printf(MSG_DEBUG,
5070                            "P2P: Current operating channels are not available for P2P. Try to use another channel");
5071                 *force_freq = 0;
5072         } else {
5073                 wpa_printf(MSG_DEBUG,
5074                            "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5075                 res = -2;
5076                 goto exit_free;
5077         }
5078
5079 exit_ok:
5080         res = 0;
5081 exit_free:
5082         os_free(freqs);
5083         return res;
5084 }
5085
5086
5087 /**
5088  * wpas_p2p_connect - Request P2P Group Formation to be started
5089  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5090  * @peer_addr: Address of the peer P2P Device
5091  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5092  * @persistent_group: Whether to create a persistent group
5093  * @auto_join: Whether to select join vs. GO Negotiation automatically
5094  * @join: Whether to join an existing group (as a client) instead of starting
5095  *      Group Owner negotiation; @peer_addr is BSSID in that case
5096  * @auth: Whether to only authorize the connection instead of doing that and
5097  *      initiating Group Owner negotiation
5098  * @go_intent: GO Intent or -1 to use default
5099  * @freq: Frequency for the group or 0 for auto-selection
5100  * @persistent_id: Persistent group credentials to use for forcing GO
5101  *      parameters or -1 to generate new values (SSID/passphrase)
5102  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5103  *      interoperability workaround when initiating group formation
5104  * @ht40: Start GO with 40 MHz channel width
5105  * @vht:  Start GO with VHT support
5106  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5107  *      failure, -2 on failure due to channel not currently available,
5108  *      -3 if forced channel is not supported
5109  */
5110 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5111                      const char *pin, enum p2p_wps_method wps_method,
5112                      int persistent_group, int auto_join, int join, int auth,
5113                      int go_intent, int freq, int persistent_id, int pd,
5114                      int ht40, int vht)
5115 {
5116         int force_freq = 0, pref_freq = 0;
5117         int ret = 0, res;
5118         enum wpa_driver_if_type iftype;
5119         const u8 *if_addr;
5120         struct wpa_ssid *ssid = NULL;
5121         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5122
5123         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5124                 return -1;
5125
5126         if (persistent_id >= 0) {
5127                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5128                 if (ssid == NULL || ssid->disabled != 2 ||
5129                     ssid->mode != WPAS_MODE_P2P_GO)
5130                         return -1;
5131         }
5132
5133         os_free(wpa_s->global->add_psk);
5134         wpa_s->global->add_psk = NULL;
5135
5136         wpa_s->global->p2p_fail_on_wps_complete = 0;
5137         wpa_s->global->pending_p2ps_group = 0;
5138         wpa_s->p2ps_method_config_any = 0;
5139
5140         if (go_intent < 0)
5141                 go_intent = wpa_s->conf->p2p_go_intent;
5142
5143         if (!auth)
5144                 wpa_s->p2p_long_listen = 0;
5145
5146         wpa_s->p2p_wps_method = wps_method;
5147         wpa_s->p2p_persistent_group = !!persistent_group;
5148         wpa_s->p2p_persistent_id = persistent_id;
5149         wpa_s->p2p_go_intent = go_intent;
5150         wpa_s->p2p_connect_freq = freq;
5151         wpa_s->p2p_fallback_to_go_neg = 0;
5152         wpa_s->p2p_pd_before_go_neg = !!pd;
5153         wpa_s->p2p_go_ht40 = !!ht40;
5154         wpa_s->p2p_go_vht = !!vht;
5155
5156         if (pin)
5157                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5158         else if (wps_method == WPS_PIN_DISPLAY) {
5159                 ret = wps_generate_pin();
5160                 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5161                                   "%08d", ret);
5162                 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5163                         wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5164                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5165                            wpa_s->p2p_pin);
5166         } else
5167                 wpa_s->p2p_pin[0] = '\0';
5168
5169         if (join || auto_join) {
5170                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5171                 if (auth) {
5172                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5173                                    "connect a running group from " MACSTR,
5174                                    MAC2STR(peer_addr));
5175                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5176                         return ret;
5177                 }
5178                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5179                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5180                                            iface_addr) < 0) {
5181                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5182                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5183                                          dev_addr);
5184                 }
5185                 if (auto_join) {
5186                         os_get_reltime(&wpa_s->p2p_auto_started);
5187                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5188                                    "%ld.%06ld",
5189                                    wpa_s->p2p_auto_started.sec,
5190                                    wpa_s->p2p_auto_started.usec);
5191                 }
5192                 wpa_s->user_initiated_pd = 1;
5193                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5194                                   auto_join, freq, NULL, 0) < 0)
5195                         return -1;
5196                 return ret;
5197         }
5198
5199         size = P2P_MAX_PREF_CHANNELS;
5200         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5201                                    go_intent == 15, pref_freq_list, &size);
5202         if (res)
5203                 return res;
5204         wpas_p2p_set_own_freq_preference(wpa_s,
5205                                          force_freq ? force_freq : pref_freq);
5206
5207         p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5208
5209         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5210
5211         if (wpa_s->create_p2p_iface) {
5212                 /* Prepare to add a new interface for the group */
5213                 iftype = WPA_IF_P2P_GROUP;
5214                 if (go_intent == 15)
5215                         iftype = WPA_IF_P2P_GO;
5216                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5217                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5218                                    "interface for the group");
5219                         return -1;
5220                 }
5221
5222                 if_addr = wpa_s->pending_interface_addr;
5223         } else {
5224                 if_addr = wpa_s->own_addr;
5225                 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5226         }
5227
5228         if (auth) {
5229                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5230                                          go_intent, if_addr,
5231                                          force_freq, persistent_group, ssid,
5232                                          pref_freq) < 0)
5233                         return -1;
5234                 return ret;
5235         }
5236
5237         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5238                                   go_intent, if_addr, force_freq,
5239                                   persistent_group, ssid, pref_freq) < 0) {
5240                 if (wpa_s->create_p2p_iface)
5241                         wpas_p2p_remove_pending_group_interface(wpa_s);
5242                 return -1;
5243         }
5244         return ret;
5245 }
5246
5247
5248 /**
5249  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5250  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5251  * @freq: Frequency of the channel in MHz
5252  * @duration: Duration of the stay on the channel in milliseconds
5253  *
5254  * This callback is called when the driver indicates that it has started the
5255  * requested remain-on-channel duration.
5256  */
5257 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5258                                    unsigned int freq, unsigned int duration)
5259 {
5260         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5261                 return;
5262         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)",
5263                    wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5264                    wpa_s->roc_waiting_drv_freq, freq, duration);
5265         if (wpa_s->off_channel_freq &&
5266             wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5267                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5268                               wpa_s->pending_listen_duration);
5269                 wpa_s->pending_listen_freq = 0;
5270         } else {
5271                 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5272                            wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5273                            freq, duration);
5274         }
5275 }
5276
5277
5278 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5279 {
5280         /* Limit maximum Listen state time based on driver limitation. */
5281         if (timeout > wpa_s->max_remain_on_chan)
5282                 timeout = wpa_s->max_remain_on_chan;
5283
5284         return p2p_listen(wpa_s->global->p2p, timeout);
5285 }
5286
5287
5288 /**
5289  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5290  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5291  * @freq: Frequency of the channel in MHz
5292  *
5293  * This callback is called when the driver indicates that a remain-on-channel
5294  * operation has been completed, i.e., the duration on the requested channel
5295  * has timed out.
5296  */
5297 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5298                                           unsigned int freq)
5299 {
5300         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5301                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
5302                    wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5303         wpas_p2p_listen_work_done(wpa_s);
5304         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5305                 return;
5306         if (wpa_s->p2p_long_listen > 0)
5307                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5308         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5309                 return; /* P2P module started a new operation */
5310         if (offchannel_pending_action_tx(wpa_s))
5311                 return;
5312         if (wpa_s->p2p_long_listen > 0) {
5313                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5314                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5315         } else {
5316                 /*
5317                  * When listen duration is over, stop listen & update p2p_state
5318                  * to IDLE.
5319                  */
5320                 p2p_stop_listen(wpa_s->global->p2p);
5321         }
5322 }
5323
5324
5325 /**
5326  * wpas_p2p_group_remove - Remove a P2P group
5327  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5328  * @ifname: Network interface name of the group interface or "*" to remove all
5329  *      groups
5330  * Returns: 0 on success, -1 on failure
5331  *
5332  * This function is used to remove a P2P group. This can be used to disconnect
5333  * from a group in which the local end is a P2P Client or to end a P2P Group in
5334  * case the local end is the Group Owner. If a virtual network interface was
5335  * created for this group, that interface will be removed. Otherwise, only the
5336  * configured P2P group network will be removed from the interface.
5337  */
5338 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5339 {
5340         struct wpa_global *global = wpa_s->global;
5341         struct wpa_supplicant *calling_wpa_s = wpa_s;
5342
5343         if (os_strcmp(ifname, "*") == 0) {
5344                 struct wpa_supplicant *prev;
5345                 wpa_s = global->ifaces;
5346                 while (wpa_s) {
5347                         prev = wpa_s;
5348                         wpa_s = wpa_s->next;
5349                         if (prev->p2p_group_interface !=
5350                             NOT_P2P_GROUP_INTERFACE ||
5351                             (prev->current_ssid &&
5352                              prev->current_ssid->p2p_group))
5353                                 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5354                 }
5355                 return 0;
5356         }
5357
5358         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5359                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5360                         break;
5361         }
5362
5363         return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5364 }
5365
5366
5367 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5368 {
5369         unsigned int r;
5370
5371         if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5372                 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5373                 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5374                 int res;
5375
5376                 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5377                                                  &size, pref_freq_list);
5378                 if (!res && size > 0) {
5379                         i = 0;
5380                         while (wpas_p2p_disallowed_freq(wpa_s->global,
5381                                                         pref_freq_list[i]) &&
5382                                i < size) {
5383                                 wpa_printf(MSG_DEBUG,
5384                                            "P2P: preferred_freq_list[%d]=%d is disallowed",
5385                                            i, pref_freq_list[i]);
5386                                 i++;
5387                         }
5388                         if (i != size) {
5389                                 freq = pref_freq_list[i];
5390                                 wpa_printf(MSG_DEBUG,
5391                                            "P2P: Using preferred_freq_list[%d]=%d",
5392                                            i, freq);
5393                         } else {
5394                                 wpa_printf(MSG_DEBUG,
5395                                            "P2P: All driver preferred frequencies are disallowed for P2P use");
5396                         }
5397                 } else {
5398                         wpa_printf(MSG_DEBUG,
5399                                    "P2P: No preferred frequency list available");
5400                 }
5401         }
5402
5403         if (freq == 2) {
5404                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5405                            "band");
5406                 if (wpa_s->best_24_freq > 0 &&
5407                     p2p_supported_freq_go(wpa_s->global->p2p,
5408                                           wpa_s->best_24_freq)) {
5409                         freq = wpa_s->best_24_freq;
5410                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5411                                    "channel: %d MHz", freq);
5412                 } else {
5413                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5414                                 return -1;
5415                         freq = 2412 + (r % 3) * 25;
5416                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5417                                    "channel: %d MHz", freq);
5418                 }
5419         }
5420
5421         if (freq == 5) {
5422                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5423                            "band");
5424                 if (wpa_s->best_5_freq > 0 &&
5425                     p2p_supported_freq_go(wpa_s->global->p2p,
5426                                        wpa_s->best_5_freq)) {
5427                         freq = wpa_s->best_5_freq;
5428                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5429                                    "channel: %d MHz", freq);
5430                 } else {
5431                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5432                                 return -1;
5433                         freq = 5180 + (r % 4) * 20;
5434                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5435                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5436                                            "5 GHz channel for P2P group");
5437                                 return -1;
5438                         }
5439                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5440                                    "channel: %d MHz", freq);
5441                 }
5442         }
5443
5444         if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5445                 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5446                     ieee80211_is_dfs(freq)) {
5447                         /*
5448                          * If freq is a DFS channel and DFS is offloaded to the
5449                          * driver, allow P2P GO to use it.
5450                          */
5451                         wpa_printf(MSG_DEBUG, "P2P: "
5452                                    "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5453                                    __func__, freq);
5454                         return freq;
5455                 }
5456                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5457                            "(%u MHz) is not supported for P2P uses",
5458                            freq);
5459                 return -1;
5460         }
5461
5462         return freq;
5463 }
5464
5465
5466 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5467                                       const struct p2p_channels *channels,
5468                                       int freq)
5469 {
5470         if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5471             p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5472             freq_included(wpa_s, channels, freq))
5473                 return 1;
5474         return 0;
5475 }
5476
5477
5478 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5479                                             struct p2p_go_neg_results *params,
5480                                             const struct p2p_channels *channels)
5481 {
5482         unsigned int i, r;
5483
5484         /* first try some random selection of the social channels */
5485         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5486                 return;
5487
5488         for (i = 0; i < 3; i++) {
5489                 params->freq = 2412 + ((r + i) % 3) * 25;
5490                 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5491                         goto out;
5492         }
5493
5494         /* try all other channels in operating class 81 */
5495         for (i = 0; i < 11; i++) {
5496                 params->freq = 2412 + i * 5;
5497
5498                 /* skip social channels; covered in the previous loop */
5499                 if (params->freq == 2412 ||
5500                     params->freq == 2437 ||
5501                     params->freq == 2462)
5502                         continue;
5503
5504                 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5505                         goto out;
5506         }
5507
5508         /* try all channels in operating class 115 */
5509         for (i = 0; i < 4; i++) {
5510                 params->freq = 5180 + i * 20;
5511                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5512                     freq_included(wpa_s, channels, params->freq) &&
5513                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5514                         goto out;
5515         }
5516
5517         /* try all channels in operating class 124 */
5518         for (i = 0; i < 4; i++) {
5519                 params->freq = 5745 + i * 20;
5520                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5521                     freq_included(wpa_s, channels, params->freq) &&
5522                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5523                         goto out;
5524         }
5525
5526         /* try social channel class 180 channel 2 */
5527         params->freq = 58320 + 1 * 2160;
5528         if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5529             freq_included(wpa_s, channels, params->freq) &&
5530             p2p_supported_freq(wpa_s->global->p2p, params->freq))
5531                 goto out;
5532
5533         /* try all channels in reg. class 180 */
5534         for (i = 0; i < 4; i++) {
5535                 params->freq = 58320 + i * 2160;
5536                 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5537                     freq_included(wpa_s, channels, params->freq) &&
5538                     p2p_supported_freq(wpa_s->global->p2p, params->freq))
5539                         goto out;
5540         }
5541
5542         params->freq = 0;
5543         wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
5544         return;
5545 out:
5546         wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5547                    params->freq);
5548 }
5549
5550
5551 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5552                                    struct p2p_go_neg_results *params,
5553                                    int freq, int ht40, int vht,
5554                                    const struct p2p_channels *channels)
5555 {
5556         struct wpa_used_freq_data *freqs;
5557         unsigned int cand;
5558         unsigned int num, i;
5559         int ignore_no_freqs = 0;
5560
5561         os_memset(params, 0, sizeof(*params));
5562         params->role_go = 1;
5563         params->ht40 = ht40;
5564         params->vht = vht;
5565
5566         freqs = os_calloc(wpa_s->num_multichan_concurrent,
5567                           sizeof(struct wpa_used_freq_data));
5568         if (!freqs)
5569                 return -1;
5570
5571         num = get_shared_radio_freqs_data(wpa_s, freqs,
5572                                           wpa_s->num_multichan_concurrent);
5573
5574         if (wpa_s->current_ssid &&
5575             wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5576             wpa_s->wpa_state == WPA_COMPLETED) {
5577                 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5578                            __func__);
5579
5580                 /*
5581                  * If the frequency selection is done for an active P2P GO that
5582                  * is not sharing a frequency, allow to select a new frequency
5583                  * even if there are no unused frequencies as we are about to
5584                  * move the P2P GO so its frequency can be re-used.
5585                  */
5586                 for (i = 0; i < num; i++) {
5587                         if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5588                             freqs[i].flags == 0) {
5589                                 ignore_no_freqs = 1;
5590                                 break;
5591                         }
5592                 }
5593         }
5594
5595         /* try using the forced freq */
5596         if (freq) {
5597                 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5598                         wpa_printf(MSG_DEBUG,
5599                                    "P2P: Forced GO freq %d MHz not accepted",
5600                                    freq);
5601                         goto fail;
5602                 }
5603
5604                 for (i = 0; i < num; i++) {
5605                         if (freqs[i].freq == freq) {
5606                                 wpa_printf(MSG_DEBUG,
5607                                            "P2P: forced freq (%d MHz) is also shared",
5608                                            freq);
5609                                 params->freq = freq;
5610                                 goto success;
5611                         }
5612                 }
5613
5614                 if (!ignore_no_freqs &&
5615                     wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5616                         wpa_printf(MSG_DEBUG,
5617                                    "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5618                                    freq);
5619                         goto fail;
5620                 }
5621
5622                 wpa_printf(MSG_DEBUG,
5623                            "P2P: force GO freq (%d MHz) on a free channel",
5624                            freq);
5625                 params->freq = freq;
5626                 goto success;
5627         }
5628
5629         /* consider using one of the shared frequencies */
5630         if (num) {
5631                 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5632                 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5633                         wpa_printf(MSG_DEBUG,
5634                                    "P2P: Use shared freq (%d MHz) for GO",
5635                                    freq);
5636                         params->freq = cand;
5637                         goto success;
5638                 }
5639
5640                 /* try using one of the shared freqs */
5641                 for (i = 0; i < num; i++) {
5642                         if (wpas_p2p_supported_freq_go(wpa_s, channels,
5643                                                        freqs[i].freq)) {
5644                                 wpa_printf(MSG_DEBUG,
5645                                            "P2P: Use shared freq (%d MHz) for GO",
5646                                            freq);
5647                                 params->freq = freqs[i].freq;
5648                                 goto success;
5649                         }
5650                 }
5651         }
5652
5653         if (!ignore_no_freqs &&
5654             wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5655                 wpa_printf(MSG_DEBUG,
5656                            "P2P: Cannot force GO on any of the channels we are already using");
5657                 goto fail;
5658         }
5659
5660         /* try using the setting from the configuration file */
5661         if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5662             wpa_s->conf->p2p_oper_channel >= 1 &&
5663             wpa_s->conf->p2p_oper_channel <= 11 &&
5664             wpas_p2p_supported_freq_go(
5665                     wpa_s, channels,
5666                     2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5667                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5668                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5669                            "frequency %d MHz", params->freq);
5670                 goto success;
5671         }
5672
5673         if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5674              wpa_s->conf->p2p_oper_reg_class == 116 ||
5675              wpa_s->conf->p2p_oper_reg_class == 117 ||
5676              wpa_s->conf->p2p_oper_reg_class == 124 ||
5677              wpa_s->conf->p2p_oper_reg_class == 125 ||
5678              wpa_s->conf->p2p_oper_reg_class == 126 ||
5679              wpa_s->conf->p2p_oper_reg_class == 127) &&
5680             wpas_p2p_supported_freq_go(wpa_s, channels,
5681                                        5000 +
5682                                        5 * wpa_s->conf->p2p_oper_channel)) {
5683                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5684                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5685                            "frequency %d MHz", params->freq);
5686                 goto success;
5687         }
5688
5689         /* Try using best channels */
5690         if (wpa_s->conf->p2p_oper_channel == 0 &&
5691             wpa_s->best_overall_freq > 0 &&
5692             wpas_p2p_supported_freq_go(wpa_s, channels,
5693                                        wpa_s->best_overall_freq)) {
5694                 params->freq = wpa_s->best_overall_freq;
5695                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5696                            "channel %d MHz", params->freq);
5697                 goto success;
5698         }
5699
5700         if (wpa_s->conf->p2p_oper_channel == 0 &&
5701             wpa_s->best_24_freq > 0 &&
5702             wpas_p2p_supported_freq_go(wpa_s, channels,
5703                                        wpa_s->best_24_freq)) {
5704                 params->freq = wpa_s->best_24_freq;
5705                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5706                            "channel %d MHz", params->freq);
5707                 goto success;
5708         }
5709
5710         if (wpa_s->conf->p2p_oper_channel == 0 &&
5711             wpa_s->best_5_freq > 0 &&
5712             wpas_p2p_supported_freq_go(wpa_s, channels,
5713                                        wpa_s->best_5_freq)) {
5714                 params->freq = wpa_s->best_5_freq;
5715                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5716                            "channel %d MHz", params->freq);
5717                 goto success;
5718         }
5719
5720         /* try using preferred channels */
5721         cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5722         if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5723                 params->freq = cand;
5724                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5725                            "channels", params->freq);
5726                 goto success;
5727         }
5728
5729         /* Try using one of the group common freqs */
5730         if (wpa_s->p2p_group_common_freqs) {
5731                 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5732                         cand = wpa_s->p2p_group_common_freqs[i];
5733                         if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5734                                 params->freq = cand;
5735                                 wpa_printf(MSG_DEBUG,
5736                                            "P2P: Use freq %d MHz common with the peer",
5737                                            params->freq);
5738                                 goto success;
5739                         }
5740                 }
5741         }
5742
5743         /* no preference, select some channel */
5744         wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5745
5746         if (params->freq == 0) {
5747                 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5748                 goto fail;
5749         }
5750
5751 success:
5752         os_free(freqs);
5753         return 0;
5754 fail:
5755         os_free(freqs);
5756         return -1;
5757 }
5758
5759
5760 static struct wpa_supplicant *
5761 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5762                          int go)
5763 {
5764         struct wpa_supplicant *group_wpa_s;
5765
5766         if (!wpas_p2p_create_iface(wpa_s)) {
5767                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5768                         "operations");
5769                 wpa_s->p2p_first_connection_timeout = 0;
5770                 return wpa_s;
5771         }
5772
5773         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5774                                          WPA_IF_P2P_CLIENT) < 0) {
5775                 wpa_msg_global(wpa_s, MSG_ERROR,
5776                                "P2P: Failed to add group interface");
5777                 return NULL;
5778         }
5779         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5780         if (group_wpa_s == NULL) {
5781                 wpa_msg_global(wpa_s, MSG_ERROR,
5782                                "P2P: Failed to initialize group interface");
5783                 wpas_p2p_remove_pending_group_interface(wpa_s);
5784                 return NULL;
5785         }
5786
5787         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5788                 group_wpa_s->ifname);
5789         group_wpa_s->p2p_first_connection_timeout = 0;
5790         return group_wpa_s;
5791 }
5792
5793
5794 /**
5795  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5796  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5797  * @persistent_group: Whether to create a persistent group
5798  * @freq: Frequency for the group or 0 to indicate no hardcoding
5799  * @ht40: Start GO with 40 MHz channel width
5800  * @vht:  Start GO with VHT support
5801  * Returns: 0 on success, -1 on failure
5802  *
5803  * This function creates a new P2P group with the local end as the Group Owner,
5804  * i.e., without using Group Owner Negotiation.
5805  */
5806 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5807                        int freq, int ht40, int vht)
5808 {
5809         struct p2p_go_neg_results params;
5810
5811         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5812                 return -1;
5813
5814         os_free(wpa_s->global->add_psk);
5815         wpa_s->global->add_psk = NULL;
5816
5817         /* Make sure we are not running find during connection establishment */
5818         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5819         wpas_p2p_stop_find_oper(wpa_s);
5820
5821         freq = wpas_p2p_select_go_freq(wpa_s, freq);
5822         if (freq < 0)
5823                 return -1;
5824
5825         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
5826                 return -1;
5827         if (params.freq &&
5828             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5829                 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5830                     ieee80211_is_dfs(params.freq)) {
5831                         /*
5832                          * If freq is a DFS channel and DFS is offloaded to the
5833                          * driver, allow P2P GO to use it.
5834                          */
5835                         wpa_printf(MSG_DEBUG,
5836                                    "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5837                                 __func__, params.freq);
5838                 } else {
5839                         wpa_printf(MSG_DEBUG,
5840                                    "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5841                                    params.freq);
5842                         return -1;
5843                 }
5844         }
5845         p2p_go_params(wpa_s->global->p2p, &params);
5846         params.persistent_group = persistent_group;
5847
5848         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5849         if (wpa_s == NULL)
5850                 return -1;
5851         wpas_start_wps_go(wpa_s, &params, 0);
5852
5853         return 0;
5854 }
5855
5856
5857 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5858                                  struct wpa_ssid *params, int addr_allocated,
5859                                  int freq, int force_scan)
5860 {
5861         struct wpa_ssid *ssid;
5862
5863         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5864         if (wpa_s == NULL)
5865                 return -1;
5866         if (force_scan)
5867                 os_get_reltime(&wpa_s->scan_min_time);
5868         wpa_s->p2p_last_4way_hs_fail = NULL;
5869
5870         wpa_supplicant_ap_deinit(wpa_s);
5871
5872         ssid = wpa_config_add_network(wpa_s->conf);
5873         if (ssid == NULL)
5874                 return -1;
5875         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5876         wpa_config_set_network_defaults(ssid);
5877         ssid->temporary = 1;
5878         ssid->proto = WPA_PROTO_RSN;
5879         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5880         ssid->group_cipher = WPA_CIPHER_CCMP;
5881         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5882         ssid->ssid = os_malloc(params->ssid_len);
5883         if (ssid->ssid == NULL) {
5884                 wpa_config_remove_network(wpa_s->conf, ssid->id);
5885                 return -1;
5886         }
5887         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5888         ssid->ssid_len = params->ssid_len;
5889         ssid->p2p_group = 1;
5890         ssid->export_keys = 1;
5891         if (params->psk_set) {
5892                 os_memcpy(ssid->psk, params->psk, 32);
5893                 ssid->psk_set = 1;
5894         }
5895         if (params->passphrase)
5896                 ssid->passphrase = os_strdup(params->passphrase);
5897
5898         wpa_s->show_group_started = 1;
5899         wpa_s->p2p_in_invitation = 1;
5900         wpa_s->p2p_invite_go_freq = freq;
5901
5902         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5903                              NULL);
5904         eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5905                                wpas_p2p_group_formation_timeout,
5906                                wpa_s->parent, NULL);
5907         wpa_supplicant_select_network(wpa_s, ssid);
5908
5909         return 0;
5910 }
5911
5912
5913 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5914                                   struct wpa_ssid *ssid, int addr_allocated,
5915                                   int force_freq, int neg_freq, int ht40,
5916                                   int vht, const struct p2p_channels *channels,
5917                                   int connection_timeout, int force_scan)
5918 {
5919         struct p2p_go_neg_results params;
5920         int go = 0, freq;
5921
5922         if (ssid->disabled != 2 || ssid->ssid == NULL)
5923                 return -1;
5924
5925         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5926             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5927                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5928                            "already running");
5929                 if (go == 0 &&
5930                     eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5931                                          wpa_s->parent, NULL)) {
5932                         /*
5933                          * This can happen if Invitation Response frame was lost
5934                          * and the peer (GO of a persistent group) tries to
5935                          * invite us again. Reschedule the timeout to avoid
5936                          * terminating the wait for the connection too early
5937                          * since we now know that the peer is still trying to
5938                          * invite us instead of having already started the GO.
5939                          */
5940                         wpa_printf(MSG_DEBUG,
5941                                    "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5942                         eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5943                                                wpas_p2p_group_formation_timeout,
5944                                                wpa_s->parent, NULL);
5945                 }
5946                 return 0;
5947         }
5948
5949         os_free(wpa_s->global->add_psk);
5950         wpa_s->global->add_psk = NULL;
5951
5952         /* Make sure we are not running find during connection establishment */
5953         wpas_p2p_stop_find_oper(wpa_s);
5954
5955         wpa_s->p2p_fallback_to_go_neg = 0;
5956
5957         if (ssid->mode == WPAS_MODE_P2P_GO) {
5958                 if (force_freq > 0) {
5959                         freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5960                         if (freq < 0)
5961                                 return -1;
5962                 } else {
5963                         freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5964                         if (freq < 0 ||
5965                             (freq > 0 && !freq_included(wpa_s, channels, freq)))
5966                                 freq = 0;
5967                 }
5968         } else if (ssid->mode == WPAS_MODE_INFRA) {
5969                 freq = neg_freq;
5970                 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
5971                         struct os_reltime now;
5972                         struct wpa_bss *bss =
5973                                 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
5974
5975                         os_get_reltime(&now);
5976                         if (bss &&
5977                             !os_reltime_expired(&now, &bss->last_update, 5) &&
5978                             freq_included(wpa_s, channels, bss->freq))
5979                                 freq = bss->freq;
5980                         else
5981                                 freq = 0;
5982                 }
5983
5984                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
5985                                              force_scan);
5986         } else {
5987                 return -1;
5988         }
5989
5990         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
5991                 return -1;
5992
5993         params.role_go = 1;
5994         params.psk_set = ssid->psk_set;
5995         if (params.psk_set)
5996                 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5997         if (ssid->passphrase) {
5998                 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5999                         wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6000                                    "persistent group");
6001                         return -1;
6002                 }
6003                 os_strlcpy(params.passphrase, ssid->passphrase,
6004                            sizeof(params.passphrase));
6005         }
6006         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6007         params.ssid_len = ssid->ssid_len;
6008         params.persistent_group = 1;
6009
6010         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6011         if (wpa_s == NULL)
6012                 return -1;
6013
6014         p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6015
6016         wpa_s->p2p_first_connection_timeout = connection_timeout;
6017         wpas_start_wps_go(wpa_s, &params, 0);
6018
6019         return 0;
6020 }
6021
6022
6023 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6024                                struct wpabuf *proberesp_ies)
6025 {
6026         struct wpa_supplicant *wpa_s = ctx;
6027         if (wpa_s->ap_iface) {
6028                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6029                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6030                         wpabuf_free(beacon_ies);
6031                         wpabuf_free(proberesp_ies);
6032                         return;
6033                 }
6034                 if (beacon_ies) {
6035                         wpabuf_free(hapd->p2p_beacon_ie);
6036                         hapd->p2p_beacon_ie = beacon_ies;
6037                 }
6038                 wpabuf_free(hapd->p2p_probe_resp_ie);
6039                 hapd->p2p_probe_resp_ie = proberesp_ies;
6040         } else {
6041                 wpabuf_free(beacon_ies);
6042                 wpabuf_free(proberesp_ies);
6043         }
6044         wpa_supplicant_ap_update_beacon(wpa_s);
6045 }
6046
6047
6048 static void wpas_p2p_idle_update(void *ctx, int idle)
6049 {
6050         struct wpa_supplicant *wpa_s = ctx;
6051         if (!wpa_s->ap_iface)
6052                 return;
6053         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6054         if (idle) {
6055                 if (wpa_s->global->p2p_fail_on_wps_complete &&
6056                     wpa_s->p2p_in_provisioning) {
6057                         wpas_p2p_grpform_fail_after_wps(wpa_s);
6058                         return;
6059                 }
6060                 wpas_p2p_set_group_idle_timeout(wpa_s);
6061         } else
6062                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6063 }
6064
6065
6066 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6067                                        struct wpa_ssid *ssid)
6068 {
6069         struct p2p_group *group;
6070         struct p2p_group_config *cfg;
6071
6072         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6073                 return NULL;
6074
6075         cfg = os_zalloc(sizeof(*cfg));
6076         if (cfg == NULL)
6077                 return NULL;
6078
6079         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6080                 cfg->persistent_group = 2;
6081         else if (ssid->p2p_persistent_group)
6082                 cfg->persistent_group = 1;
6083         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6084         if (wpa_s->max_stations &&
6085             wpa_s->max_stations < wpa_s->conf->max_num_sta)
6086                 cfg->max_clients = wpa_s->max_stations;
6087         else
6088                 cfg->max_clients = wpa_s->conf->max_num_sta;
6089         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6090         cfg->ssid_len = ssid->ssid_len;
6091         cfg->freq = ssid->frequency;
6092         cfg->cb_ctx = wpa_s;
6093         cfg->ie_update = wpas_p2p_ie_update;
6094         cfg->idle_update = wpas_p2p_idle_update;
6095
6096         group = p2p_group_init(wpa_s->global->p2p, cfg);
6097         if (group == NULL)
6098                 os_free(cfg);
6099         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6100                 p2p_group_notif_formation_done(group);
6101         wpa_s->p2p_group = group;
6102         return group;
6103 }
6104
6105
6106 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6107                           int registrar)
6108 {
6109         struct wpa_ssid *ssid = wpa_s->current_ssid;
6110
6111         if (!wpa_s->p2p_in_provisioning) {
6112                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6113                            "provisioning not in progress");
6114                 return;
6115         }
6116
6117         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6118                 u8 go_dev_addr[ETH_ALEN];
6119                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6120                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6121                                           ssid->ssid_len);
6122                 /* Clear any stored provisioning info */
6123                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6124         }
6125
6126         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6127                              NULL);
6128         wpa_s->p2p_go_group_formation_completed = 1;
6129         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6130                 /*
6131                  * Use a separate timeout for initial data connection to
6132                  * complete to allow the group to be removed automatically if
6133                  * something goes wrong in this step before the P2P group idle
6134                  * timeout mechanism is taken into use.
6135                  */
6136                 wpa_dbg(wpa_s, MSG_DEBUG,
6137                         "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6138                         P2P_MAX_INITIAL_CONN_WAIT);
6139                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6140                                        wpas_p2p_group_formation_timeout,
6141                                        wpa_s->parent, NULL);
6142         } else if (ssid) {
6143                 /*
6144                  * Use a separate timeout for initial data connection to
6145                  * complete to allow the group to be removed automatically if
6146                  * the client does not complete data connection successfully.
6147                  */
6148                 wpa_dbg(wpa_s, MSG_DEBUG,
6149                         "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6150                         P2P_MAX_INITIAL_CONN_WAIT_GO);
6151                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6152                                        wpas_p2p_group_formation_timeout,
6153                                        wpa_s->parent, NULL);
6154                 /*
6155                  * Complete group formation on first successful data connection
6156                  */
6157                 wpa_s->p2p_go_group_formation_completed = 0;
6158         }
6159         if (wpa_s->global->p2p)
6160                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6161         wpas_group_formation_completed(wpa_s, 1, 0);
6162 }
6163
6164
6165 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6166                          struct wps_event_fail *fail)
6167 {
6168         if (!wpa_s->p2p_in_provisioning) {
6169                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6170                            "provisioning not in progress");
6171                 return;
6172         }
6173
6174         if (wpa_s->go_params) {
6175                 p2p_clear_provisioning_info(
6176                         wpa_s->global->p2p,
6177                         wpa_s->go_params->peer_device_addr);
6178         }
6179
6180         wpas_notify_p2p_wps_failed(wpa_s, fail);
6181
6182         if (wpa_s == wpa_s->global->p2p_group_formation) {
6183                 /*
6184                  * Allow some time for the failed WPS negotiation exchange to
6185                  * complete, but remove the group since group formation cannot
6186                  * succeed after provisioning failure.
6187                  */
6188                 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6189                 wpa_s->global->p2p_fail_on_wps_complete = 1;
6190                 eloop_deplete_timeout(0, 50000,
6191                                       wpas_p2p_group_formation_timeout,
6192                                       wpa_s->parent, NULL);
6193         }
6194 }
6195
6196
6197 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6198 {
6199         if (!wpa_s->global->p2p_fail_on_wps_complete ||
6200             !wpa_s->p2p_in_provisioning)
6201                 return 0;
6202
6203         wpas_p2p_grpform_fail_after_wps(wpa_s);
6204
6205         return 1;
6206 }
6207
6208
6209 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6210                        const char *config_method,
6211                        enum wpas_p2p_prov_disc_use use,
6212                        struct p2ps_provision *p2ps_prov)
6213 {
6214         u16 config_methods;
6215
6216         wpa_s->global->pending_p2ps_group = 0;
6217         wpa_s->p2p_fallback_to_go_neg = 0;
6218         wpa_s->pending_pd_use = NORMAL_PD;
6219         if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6220                 p2ps_prov->conncap = p2ps_group_capability(
6221                         wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6222                 wpa_printf(MSG_DEBUG,
6223                            "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6224                            __func__, p2ps_prov->conncap,
6225                            p2ps_prov->adv_id, p2ps_prov->conncap,
6226                            p2ps_prov->status, p2ps_prov->info);
6227
6228                 config_methods = 0;
6229         } else if (os_strncmp(config_method, "display", 7) == 0)
6230                 config_methods = WPS_CONFIG_DISPLAY;
6231         else if (os_strncmp(config_method, "keypad", 6) == 0)
6232                 config_methods = WPS_CONFIG_KEYPAD;
6233         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6234                  os_strncmp(config_method, "pushbutton", 10) == 0)
6235                 config_methods = WPS_CONFIG_PUSHBUTTON;
6236         else {
6237                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6238                 os_free(p2ps_prov);
6239                 return -1;
6240         }
6241
6242         if (use == WPAS_P2P_PD_AUTO) {
6243                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6244                 wpa_s->pending_pd_config_methods = config_methods;
6245                 wpa_s->p2p_auto_pd = 1;
6246                 wpa_s->p2p_auto_join = 0;
6247                 wpa_s->pending_pd_before_join = 0;
6248                 wpa_s->auto_pd_scan_retry = 0;
6249                 wpas_p2p_stop_find(wpa_s);
6250                 wpa_s->p2p_join_scan_count = 0;
6251                 os_get_reltime(&wpa_s->p2p_auto_started);
6252                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6253                            wpa_s->p2p_auto_started.sec,
6254                            wpa_s->p2p_auto_started.usec);
6255                 wpas_p2p_join_scan(wpa_s, NULL);
6256                 return 0;
6257         }
6258
6259         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6260                 os_free(p2ps_prov);
6261                 return -1;
6262         }
6263
6264         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6265                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6266                                  0, 1);
6267 }
6268
6269
6270 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6271                               char *end)
6272 {
6273         return p2p_scan_result_text(ies, ies_len, buf, end);
6274 }
6275
6276
6277 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6278 {
6279         if (!offchannel_pending_action_tx(wpa_s))
6280                 return;
6281
6282         wpas_p2p_action_tx_clear(wpa_s);
6283
6284         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6285                    "operation request");
6286         offchannel_clear_pending_action_tx(wpa_s);
6287 }
6288
6289
6290 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6291                   enum p2p_discovery_type type,
6292                   unsigned int num_req_dev_types, const u8 *req_dev_types,
6293                   const u8 *dev_id, unsigned int search_delay,
6294                   u8 seek_cnt, const char **seek_string, int freq)
6295 {
6296         wpas_p2p_clear_pending_action_tx(wpa_s);
6297         wpa_s->p2p_long_listen = 0;
6298
6299         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6300             wpa_s->p2p_in_provisioning)
6301                 return -1;
6302
6303         wpa_supplicant_cancel_sched_scan(wpa_s);
6304
6305         return p2p_find(wpa_s->global->p2p, timeout, type,
6306                         num_req_dev_types, req_dev_types, dev_id,
6307                         search_delay, seek_cnt, seek_string, freq);
6308 }
6309
6310
6311 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6312                                             struct wpa_scan_results *scan_res)
6313 {
6314         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6315
6316         if (wpa_s->p2p_scan_work) {
6317                 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6318                 wpa_s->p2p_scan_work = NULL;
6319                 radio_work_done(work);
6320         }
6321
6322         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6323                 return;
6324
6325         /*
6326          * Indicate that results have been processed so that the P2P module can
6327          * continue pending tasks.
6328          */
6329         p2p_scan_res_handled(wpa_s->global->p2p);
6330 }
6331
6332
6333 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6334 {
6335         wpas_p2p_clear_pending_action_tx(wpa_s);
6336         wpa_s->p2p_long_listen = 0;
6337         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6338         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6339
6340         if (wpa_s->global->p2p)
6341                 p2p_stop_find(wpa_s->global->p2p);
6342
6343         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6344                 wpa_printf(MSG_DEBUG,
6345                            "P2P: Do not consider the scan results after stop_find");
6346                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6347         }
6348 }
6349
6350
6351 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6352 {
6353         wpas_p2p_stop_find_oper(wpa_s);
6354         if (!wpa_s->global->pending_group_iface_for_p2ps)
6355                 wpas_p2p_remove_pending_group_interface(wpa_s);
6356 }
6357
6358
6359 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6360 {
6361         struct wpa_supplicant *wpa_s = eloop_ctx;
6362         wpa_s->p2p_long_listen = 0;
6363 }
6364
6365
6366 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6367 {
6368         int res;
6369
6370         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6371                 return -1;
6372
6373         wpa_supplicant_cancel_sched_scan(wpa_s);
6374         wpas_p2p_clear_pending_action_tx(wpa_s);
6375
6376         if (timeout == 0) {
6377                 /*
6378                  * This is a request for unlimited Listen state. However, at
6379                  * least for now, this is mapped to a Listen state for one
6380                  * hour.
6381                  */
6382                 timeout = 3600;
6383         }
6384         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6385         wpa_s->p2p_long_listen = 0;
6386
6387         /*
6388          * Stop previous find/listen operation to avoid trying to request a new
6389          * remain-on-channel operation while the driver is still running the
6390          * previous one.
6391          */
6392         if (wpa_s->global->p2p)
6393                 p2p_stop_find(wpa_s->global->p2p);
6394
6395         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6396         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6397                 wpa_s->p2p_long_listen = timeout * 1000;
6398                 eloop_register_timeout(timeout, 0,
6399                                        wpas_p2p_long_listen_timeout,
6400                                        wpa_s, NULL);
6401         }
6402
6403         return res;
6404 }
6405
6406
6407 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6408                           u8 *buf, size_t len, int p2p_group)
6409 {
6410         struct wpabuf *p2p_ie;
6411         int ret;
6412
6413         if (wpa_s->global->p2p_disabled)
6414                 return -1;
6415         /*
6416          * Advertize mandatory cross connection capability even on
6417          * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6418          */
6419         if (wpa_s->conf->p2p_disabled && p2p_group)
6420                 return -1;
6421         if (wpa_s->global->p2p == NULL)
6422                 return -1;
6423         if (bss == NULL)
6424                 return -1;
6425
6426         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6427         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6428                                p2p_group, p2p_ie);
6429         wpabuf_free(p2p_ie);
6430
6431         return ret;
6432 }
6433
6434
6435 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6436                           const u8 *dst, const u8 *bssid,
6437                           const u8 *ie, size_t ie_len,
6438                           unsigned int rx_freq, int ssi_signal)
6439 {
6440         if (wpa_s->global->p2p_disabled)
6441                 return 0;
6442         if (wpa_s->global->p2p == NULL)
6443                 return 0;
6444
6445         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6446                                  ie, ie_len, rx_freq)) {
6447         case P2P_PREQ_NOT_P2P:
6448                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6449                                  ssi_signal);
6450                 /* fall through */
6451         case P2P_PREQ_MALFORMED:
6452         case P2P_PREQ_NOT_LISTEN:
6453         case P2P_PREQ_NOT_PROCESSED:
6454         default: /* make gcc happy */
6455                 return 0;
6456         case P2P_PREQ_PROCESSED:
6457                 return 1;
6458         }
6459 }
6460
6461
6462 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6463                         const u8 *sa, const u8 *bssid,
6464                         u8 category, const u8 *data, size_t len, int freq)
6465 {
6466         if (wpa_s->global->p2p_disabled)
6467                 return;
6468         if (wpa_s->global->p2p == NULL)
6469                 return;
6470
6471         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6472                       freq);
6473 }
6474
6475
6476 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6477 {
6478         if (wpa_s->global->p2p_disabled)
6479                 return;
6480         if (wpa_s->global->p2p == NULL)
6481                 return;
6482
6483         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6484 }
6485
6486
6487 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6488 {
6489         p2p_group_deinit(wpa_s->p2p_group);
6490         wpa_s->p2p_group = NULL;
6491
6492         wpa_s->ap_configured_cb = NULL;
6493         wpa_s->ap_configured_cb_ctx = NULL;
6494         wpa_s->ap_configured_cb_data = NULL;
6495         wpa_s->connect_without_scan = NULL;
6496 }
6497
6498
6499 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6500 {
6501         wpa_s->p2p_long_listen = 0;
6502
6503         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6504                 return -1;
6505
6506         return p2p_reject(wpa_s->global->p2p, addr);
6507 }
6508
6509
6510 /* Invite to reinvoke a persistent group */
6511 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6512                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6513                     int ht40, int vht, int pref_freq)
6514 {
6515         enum p2p_invite_role role;
6516         u8 *bssid = NULL;
6517         int force_freq = 0;
6518         int res;
6519         int no_pref_freq_given = pref_freq == 0;
6520         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6521
6522         wpa_s->global->p2p_invite_group = NULL;
6523         if (peer_addr)
6524                 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6525         else
6526                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6527
6528         wpa_s->p2p_persistent_go_freq = freq;
6529         wpa_s->p2p_go_ht40 = !!ht40;
6530         if (ssid->mode == WPAS_MODE_P2P_GO) {
6531                 role = P2P_INVITE_ROLE_GO;
6532                 if (peer_addr == NULL) {
6533                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6534                                    "address in invitation command");
6535                         return -1;
6536                 }
6537                 if (wpas_p2p_create_iface(wpa_s)) {
6538                         if (wpas_p2p_add_group_interface(wpa_s,
6539                                                          WPA_IF_P2P_GO) < 0) {
6540                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
6541                                            "allocate a new interface for the "
6542                                            "group");
6543                                 return -1;
6544                         }
6545                         bssid = wpa_s->pending_interface_addr;
6546                 } else
6547                         bssid = wpa_s->own_addr;
6548         } else {
6549                 role = P2P_INVITE_ROLE_CLIENT;
6550                 peer_addr = ssid->bssid;
6551         }
6552         wpa_s->pending_invite_ssid_id = ssid->id;
6553
6554         size = P2P_MAX_PREF_CHANNELS;
6555         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6556                                    role == P2P_INVITE_ROLE_GO,
6557                                    pref_freq_list, &size);
6558         if (res)
6559                 return res;
6560         p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6561
6562         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6563                 return -1;
6564
6565         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6566             no_pref_freq_given && pref_freq > 0 &&
6567             wpa_s->num_multichan_concurrent > 1 &&
6568             wpas_p2p_num_unused_channels(wpa_s) > 0) {
6569                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6570                            pref_freq);
6571                 pref_freq = 0;
6572         }
6573
6574         /*
6575          * Stop any find/listen operations before invitation and possibly
6576          * connection establishment.
6577          */
6578         wpas_p2p_stop_find_oper(wpa_s);
6579
6580         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6581                           ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6582                           1, pref_freq, -1);
6583 }
6584
6585
6586 /* Invite to join an active group */
6587 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6588                           const u8 *peer_addr, const u8 *go_dev_addr)
6589 {
6590         struct wpa_global *global = wpa_s->global;
6591         enum p2p_invite_role role;
6592         u8 *bssid = NULL;
6593         struct wpa_ssid *ssid;
6594         int persistent;
6595         int freq = 0, force_freq = 0, pref_freq = 0;
6596         int res;
6597         unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
6598
6599         wpa_s->p2p_persistent_go_freq = 0;
6600         wpa_s->p2p_go_ht40 = 0;
6601         wpa_s->p2p_go_vht = 0;
6602
6603         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6604                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6605                         break;
6606         }
6607         if (wpa_s == NULL) {
6608                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6609                 return -1;
6610         }
6611
6612         ssid = wpa_s->current_ssid;
6613         if (ssid == NULL) {
6614                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6615                            "invitation");
6616                 return -1;
6617         }
6618
6619         wpa_s->global->p2p_invite_group = wpa_s;
6620         persistent = ssid->p2p_persistent_group &&
6621                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6622                                         ssid->ssid, ssid->ssid_len);
6623
6624         if (ssid->mode == WPAS_MODE_P2P_GO) {
6625                 role = P2P_INVITE_ROLE_ACTIVE_GO;
6626                 bssid = wpa_s->own_addr;
6627                 if (go_dev_addr == NULL)
6628                         go_dev_addr = wpa_s->global->p2p_dev_addr;
6629                 freq = ssid->frequency;
6630         } else {
6631                 role = P2P_INVITE_ROLE_CLIENT;
6632                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6633                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6634                                    "invite to current group");
6635                         return -1;
6636                 }
6637                 bssid = wpa_s->bssid;
6638                 if (go_dev_addr == NULL &&
6639                     !is_zero_ether_addr(wpa_s->go_dev_addr))
6640                         go_dev_addr = wpa_s->go_dev_addr;
6641                 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6642                         (int) wpa_s->assoc_freq;
6643         }
6644         wpa_s->parent->pending_invite_ssid_id = -1;
6645
6646         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6647                 return -1;
6648
6649         size = P2P_MAX_PREF_CHANNELS;
6650         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6651                                    role == P2P_INVITE_ROLE_ACTIVE_GO,
6652                                    pref_freq_list, &size);
6653         if (res)
6654                 return res;
6655         wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6656
6657         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6658                           ssid->ssid, ssid->ssid_len, force_freq,
6659                           go_dev_addr, persistent, pref_freq, -1);
6660 }
6661
6662
6663 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6664 {
6665         struct wpa_ssid *ssid = wpa_s->current_ssid;
6666         u8 go_dev_addr[ETH_ALEN];
6667         int network_id = -1;
6668         int persistent;
6669         int freq;
6670         u8 ip[3 * 4];
6671         char ip_addr[100];
6672
6673         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6674                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6675                                      wpa_s->parent, NULL);
6676         }
6677
6678         if (!wpa_s->show_group_started || !ssid)
6679                 return;
6680
6681         wpa_s->show_group_started = 0;
6682
6683         os_memset(go_dev_addr, 0, ETH_ALEN);
6684         if (ssid->bssid_set)
6685                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6686         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6687                                                ssid->ssid_len);
6688         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6689
6690         if (wpa_s->global->p2p_group_formation == wpa_s)
6691                 wpa_s->global->p2p_group_formation = NULL;
6692
6693         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6694                 (int) wpa_s->assoc_freq;
6695
6696         ip_addr[0] = '\0';
6697         if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6698                 int res;
6699
6700                 res = os_snprintf(ip_addr, sizeof(ip_addr),
6701                                   " ip_addr=%u.%u.%u.%u "
6702                                   "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6703                                   ip[0], ip[1], ip[2], ip[3],
6704                                   ip[4], ip[5], ip[6], ip[7],
6705                                   ip[8], ip[9], ip[10], ip[11]);
6706                 if (os_snprintf_error(sizeof(ip_addr), res))
6707                         ip_addr[0] = '\0';
6708         }
6709
6710         wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6711                                ssid->passphrase == NULL && ssid->psk_set ?
6712                                ssid->psk : NULL,
6713                                ssid->passphrase, go_dev_addr, persistent,
6714                                ip_addr);
6715
6716         if (persistent)
6717                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6718                                                              ssid, go_dev_addr);
6719         if (network_id < 0)
6720                 network_id = ssid->id;
6721         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6722 }
6723
6724
6725 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6726                           u32 interval1, u32 duration2, u32 interval2)
6727 {
6728         int ret;
6729
6730         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6731                 return -1;
6732
6733         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6734             wpa_s->current_ssid == NULL ||
6735             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6736                 return -1;
6737
6738         ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6739                                wpa_s->own_addr, wpa_s->assoc_freq,
6740                                duration1, interval1, duration2, interval2);
6741         if (ret == 0)
6742                 wpa_s->waiting_presence_resp = 1;
6743
6744         return ret;
6745 }
6746
6747
6748 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6749                         unsigned int interval)
6750 {
6751         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6752                 return -1;
6753
6754         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6755 }
6756
6757
6758 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6759 {
6760         if (wpa_s->current_ssid == NULL) {
6761                 /*
6762                  * current_ssid can be cleared when P2P client interface gets
6763                  * disconnected, so assume this interface was used as P2P
6764                  * client.
6765                  */
6766                 return 1;
6767         }
6768         return wpa_s->current_ssid->p2p_group &&
6769                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6770 }
6771
6772
6773 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6774 {
6775         struct wpa_supplicant *wpa_s = eloop_ctx;
6776
6777         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6778                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6779                            "disabled");
6780                 return;
6781         }
6782
6783         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6784                    "group");
6785         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6786 }
6787
6788
6789 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6790 {
6791         int timeout;
6792
6793         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6794                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6795
6796         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6797                 return;
6798
6799         timeout = wpa_s->conf->p2p_group_idle;
6800         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6801             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6802             timeout = P2P_MAX_CLIENT_IDLE;
6803
6804         if (timeout == 0)
6805                 return;
6806
6807         if (timeout < 0) {
6808                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6809                         timeout = 0; /* special client mode no-timeout */
6810                 else
6811                         return;
6812         }
6813
6814         if (wpa_s->p2p_in_provisioning) {
6815                 /*
6816                  * Use the normal group formation timeout during the
6817                  * provisioning phase to avoid terminating this process too
6818                  * early due to group idle timeout.
6819                  */
6820                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6821                            "during provisioning");
6822                 return;
6823         }
6824
6825         if (wpa_s->show_group_started) {
6826                 /*
6827                  * Use the normal group formation timeout between the end of
6828                  * the provisioning phase and completion of 4-way handshake to
6829                  * avoid terminating this process too early due to group idle
6830                  * timeout.
6831                  */
6832                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6833                            "while waiting for initial 4-way handshake to "
6834                            "complete");
6835                 return;
6836         }
6837
6838         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6839                    timeout);
6840         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6841                                wpa_s, NULL);
6842 }
6843
6844
6845 /* Returns 1 if the interface was removed */
6846 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6847                           u16 reason_code, const u8 *ie, size_t ie_len,
6848                           int locally_generated)
6849 {
6850         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6851                 return 0;
6852
6853         if (!locally_generated)
6854                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6855                                  ie_len);
6856
6857         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6858             wpa_s->current_ssid &&
6859             wpa_s->current_ssid->p2p_group &&
6860             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6861                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6862                            "session is ending");
6863                 if (wpas_p2p_group_delete(wpa_s,
6864                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6865                     > 0)
6866                         return 1;
6867         }
6868
6869         return 0;
6870 }
6871
6872
6873 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6874                              u16 reason_code, const u8 *ie, size_t ie_len,
6875                              int locally_generated)
6876 {
6877         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6878                 return;
6879
6880         if (!locally_generated)
6881                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6882                                    ie_len);
6883 }
6884
6885
6886 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6887 {
6888         struct p2p_data *p2p = wpa_s->global->p2p;
6889
6890         if (p2p == NULL)
6891                 return;
6892
6893         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6894                 return;
6895
6896         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6897                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6898
6899         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6900                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6901
6902         if (wpa_s->wps &&
6903             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6904                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6905
6906         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6907                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6908
6909         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6910                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6911                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6912                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6913                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6914         }
6915
6916         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6917                 p2p_set_sec_dev_types(p2p,
6918                                       (void *) wpa_s->conf->sec_device_type,
6919                                       wpa_s->conf->num_sec_device_types);
6920
6921         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6922                 int i;
6923                 p2p_remove_wps_vendor_extensions(p2p);
6924                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6925                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6926                                 continue;
6927                         p2p_add_wps_vendor_extension(
6928                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
6929                 }
6930         }
6931
6932         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6933             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6934                 char country[3];
6935                 country[0] = wpa_s->conf->country[0];
6936                 country[1] = wpa_s->conf->country[1];
6937                 country[2] = 0x04;
6938                 p2p_set_country(p2p, country);
6939         }
6940
6941         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6942                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6943                                      wpa_s->conf->p2p_ssid_postfix ?
6944                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6945                                      0);
6946         }
6947
6948         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6949                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6950
6951         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6952                 u8 reg_class, channel;
6953                 int ret;
6954                 unsigned int r;
6955                 u8 channel_forced;
6956
6957                 if (wpa_s->conf->p2p_listen_reg_class &&
6958                     wpa_s->conf->p2p_listen_channel) {
6959                         reg_class = wpa_s->conf->p2p_listen_reg_class;
6960                         channel = wpa_s->conf->p2p_listen_channel;
6961                         channel_forced = 1;
6962                 } else {
6963                         reg_class = 81;
6964                         /*
6965                          * Pick one of the social channels randomly as the
6966                          * listen channel.
6967                          */
6968                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6969                                 channel = 1;
6970                         else
6971                                 channel = 1 + (r % 3) * 5;
6972                         channel_forced = 0;
6973                 }
6974                 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6975                                              channel_forced);
6976                 if (ret)
6977                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6978                                    "failed: %d", ret);
6979         }
6980         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6981                 u8 op_reg_class, op_channel, cfg_op_channel;
6982                 int ret = 0;
6983                 unsigned int r;
6984                 if (wpa_s->conf->p2p_oper_reg_class &&
6985                     wpa_s->conf->p2p_oper_channel) {
6986                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6987                         op_channel = wpa_s->conf->p2p_oper_channel;
6988                         cfg_op_channel = 1;
6989                 } else {
6990                         op_reg_class = 81;
6991                         /*
6992                          * Use random operation channel from (1, 6, 11)
6993                          *if no other preference is indicated.
6994                          */
6995                         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6996                                 op_channel = 1;
6997                         else
6998                                 op_channel = 1 + (r % 3) * 5;
6999                         cfg_op_channel = 0;
7000                 }
7001                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7002                                            cfg_op_channel);
7003                 if (ret)
7004                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7005                                    "failed: %d", ret);
7006         }
7007
7008         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7009                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7010                                       wpa_s->conf->p2p_pref_chan) < 0) {
7011                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7012                                    "update failed");
7013                 }
7014
7015                 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7016                         wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7017                                    "update failed");
7018                 }
7019         }
7020
7021         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7022                 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7023 }
7024
7025
7026 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7027                      int duration)
7028 {
7029         if (!wpa_s->ap_iface)
7030                 return -1;
7031         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7032                                    duration);
7033 }
7034
7035
7036 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7037 {
7038         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7039                 return -1;
7040
7041         wpa_s->global->cross_connection = enabled;
7042         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7043
7044         if (!enabled) {
7045                 struct wpa_supplicant *iface;
7046
7047                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7048                 {
7049                         if (iface->cross_connect_enabled == 0)
7050                                 continue;
7051
7052                         iface->cross_connect_enabled = 0;
7053                         iface->cross_connect_in_use = 0;
7054                         wpa_msg_global(iface->parent, MSG_INFO,
7055                                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7056                                        iface->ifname,
7057                                        iface->cross_connect_uplink);
7058                 }
7059         }
7060
7061         return 0;
7062 }
7063
7064
7065 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7066 {
7067         struct wpa_supplicant *iface;
7068
7069         if (!uplink->global->cross_connection)
7070                 return;
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->ap_iface == NULL)
7079                         continue;
7080                 if (iface->cross_connect_in_use)
7081                         continue;
7082
7083                 iface->cross_connect_in_use = 1;
7084                 wpa_msg_global(iface->parent, MSG_INFO,
7085                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7086                                iface->ifname, iface->cross_connect_uplink);
7087         }
7088 }
7089
7090
7091 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7092 {
7093         struct wpa_supplicant *iface;
7094
7095         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7096                 if (!iface->cross_connect_enabled)
7097                         continue;
7098                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7099                     0)
7100                         continue;
7101                 if (!iface->cross_connect_in_use)
7102                         continue;
7103
7104                 wpa_msg_global(iface->parent, MSG_INFO,
7105                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7106                                iface->ifname, iface->cross_connect_uplink);
7107                 iface->cross_connect_in_use = 0;
7108         }
7109 }
7110
7111
7112 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7113 {
7114         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7115             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7116             wpa_s->cross_connect_disallowed)
7117                 wpas_p2p_disable_cross_connect(wpa_s);
7118         else
7119                 wpas_p2p_enable_cross_connect(wpa_s);
7120         if (!wpa_s->ap_iface &&
7121             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7122                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7123 }
7124
7125
7126 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7127 {
7128         wpas_p2p_disable_cross_connect(wpa_s);
7129         if (!wpa_s->ap_iface &&
7130             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7131                                          wpa_s, NULL))
7132                 wpas_p2p_set_group_idle_timeout(wpa_s);
7133 }
7134
7135
7136 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7137 {
7138         struct wpa_supplicant *iface;
7139
7140         if (!wpa_s->global->cross_connection)
7141                 return;
7142
7143         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7144                 if (iface == wpa_s)
7145                         continue;
7146                 if (iface->drv_flags &
7147                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7148                         continue;
7149                 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7150                     iface != wpa_s->parent)
7151                         continue;
7152
7153                 wpa_s->cross_connect_enabled = 1;
7154                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7155                            sizeof(wpa_s->cross_connect_uplink));
7156                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7157                            "%s to %s whenever uplink is available",
7158                            wpa_s->ifname, wpa_s->cross_connect_uplink);
7159
7160                 if (iface->ap_iface || iface->current_ssid == NULL ||
7161                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
7162                     iface->cross_connect_disallowed ||
7163                     iface->wpa_state != WPA_COMPLETED)
7164                         break;
7165
7166                 wpa_s->cross_connect_in_use = 1;
7167                 wpa_msg_global(wpa_s->parent, MSG_INFO,
7168                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7169                                wpa_s->ifname, wpa_s->cross_connect_uplink);
7170                 break;
7171         }
7172 }
7173
7174
7175 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7176 {
7177         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7178             !wpa_s->p2p_in_provisioning)
7179                 return 0; /* not P2P client operation */
7180
7181         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7182                    "session overlap");
7183         if (wpa_s != wpa_s->parent)
7184                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
7185         wpas_p2p_group_formation_failed(wpa_s, 0);
7186         return 1;
7187 }
7188
7189
7190 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7191 {
7192         struct wpa_supplicant *wpa_s = eloop_ctx;
7193         wpas_p2p_notif_pbc_overlap(wpa_s);
7194 }
7195
7196
7197 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7198                                   enum wpas_p2p_channel_update_trig trig)
7199 {
7200         struct p2p_channels chan, cli_chan;
7201         struct wpa_used_freq_data *freqs = NULL;
7202         unsigned int num = wpa_s->num_multichan_concurrent;
7203
7204         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7205                 return;
7206
7207         freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7208         if (!freqs)
7209                 return;
7210
7211         num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7212
7213         os_memset(&chan, 0, sizeof(chan));
7214         os_memset(&cli_chan, 0, sizeof(cli_chan));
7215         if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7216                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7217                            "channel list");
7218                 return;
7219         }
7220
7221         p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7222
7223         wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7224
7225         /*
7226          * The used frequencies map changed, so it is possible that a GO is
7227          * using a channel that is no longer valid for P2P use. It is also
7228          * possible that due to policy consideration, it would be preferable to
7229          * move it to a frequency already used by other station interfaces.
7230          */
7231         wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7232
7233         os_free(freqs);
7234 }
7235
7236
7237 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7238                                      struct wpa_scan_results *scan_res)
7239 {
7240         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7241 }
7242
7243
7244 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7245 {
7246         struct wpa_global *global = wpa_s->global;
7247         int found = 0;
7248         const u8 *peer;
7249
7250         if (global->p2p == NULL)
7251                 return -1;
7252
7253         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7254
7255         if (wpa_s->pending_interface_name[0] &&
7256             !is_zero_ether_addr(wpa_s->pending_interface_addr))
7257                 found = 1;
7258
7259         peer = p2p_get_go_neg_peer(global->p2p);
7260         if (peer) {
7261                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7262                            MACSTR, MAC2STR(peer));
7263                 p2p_unauthorize(global->p2p, peer);
7264                 found = 1;
7265         }
7266
7267         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7268                 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7269                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7270                 found = 1;
7271         }
7272
7273         if (wpa_s->pending_pd_before_join) {
7274                 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7275                 wpa_s->pending_pd_before_join = 0;
7276                 found = 1;
7277         }
7278
7279         wpas_p2p_stop_find(wpa_s);
7280
7281         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7282                 if (wpa_s == global->p2p_group_formation &&
7283                     (wpa_s->p2p_in_provisioning ||
7284                      wpa_s->parent->pending_interface_type ==
7285                      WPA_IF_P2P_CLIENT)) {
7286                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7287                                    "formation found - cancelling",
7288                                    wpa_s->ifname);
7289                         found = 1;
7290                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7291                                              wpa_s->parent, NULL);
7292                         if (wpa_s->p2p_in_provisioning) {
7293                                 wpas_group_formation_completed(wpa_s, 0, 0);
7294                                 break;
7295                         }
7296                         wpas_p2p_group_delete(wpa_s,
7297                                               P2P_GROUP_REMOVAL_REQUESTED);
7298                         break;
7299                 } else if (wpa_s->p2p_in_invitation) {
7300                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7301                                    wpa_s->ifname);
7302                         found = 1;
7303                         wpas_p2p_group_formation_failed(wpa_s, 0);
7304                 }
7305         }
7306
7307         if (!found) {
7308                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7309                 return -1;
7310         }
7311
7312         return 0;
7313 }
7314
7315
7316 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7317 {
7318         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7319                 return;
7320
7321         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7322                    "being available anymore");
7323         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7324 }
7325
7326
7327 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7328                                    int freq_24, int freq_5, int freq_overall)
7329 {
7330         struct p2p_data *p2p = wpa_s->global->p2p;
7331         if (p2p == NULL)
7332                 return;
7333         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7334 }
7335
7336
7337 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7338 {
7339         u8 peer[ETH_ALEN];
7340         struct p2p_data *p2p = wpa_s->global->p2p;
7341
7342         if (p2p == NULL)
7343                 return -1;
7344
7345         if (hwaddr_aton(addr, peer))
7346                 return -1;
7347
7348         return p2p_unauthorize(p2p, peer);
7349 }
7350
7351
7352 /**
7353  * wpas_p2p_disconnect - Disconnect from a P2P Group
7354  * @wpa_s: Pointer to wpa_supplicant data
7355  * Returns: 0 on success, -1 on failure
7356  *
7357  * This can be used to disconnect from a group in which the local end is a P2P
7358  * Client or to end a P2P Group in case the local end is the Group Owner. If a
7359  * virtual network interface was created for this group, that interface will be
7360  * removed. Otherwise, only the configured P2P group network will be removed
7361  * from the interface.
7362  */
7363 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7364 {
7365
7366         if (wpa_s == NULL)
7367                 return -1;
7368
7369         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7370                 -1 : 0;
7371 }
7372
7373
7374 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7375 {
7376         int ret;
7377
7378         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7379                 return 0;
7380
7381         ret = p2p_in_progress(wpa_s->global->p2p);
7382         if (ret == 0) {
7383                 /*
7384                  * Check whether there is an ongoing WPS provisioning step (or
7385                  * other parts of group formation) on another interface since
7386                  * p2p_in_progress() does not report this to avoid issues for
7387                  * scans during such provisioning step.
7388                  */
7389                 if (wpa_s->global->p2p_group_formation &&
7390                     wpa_s->global->p2p_group_formation != wpa_s) {
7391                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7392                                 "in group formation",
7393                                 wpa_s->global->p2p_group_formation->ifname);
7394                         ret = 1;
7395                 }
7396         }
7397
7398         if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7399                 struct os_reltime now;
7400                 os_get_reltime(&now);
7401                 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7402                                        P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7403                         /* Wait for the first client has expired */
7404                         wpa_s->global->p2p_go_wait_client.sec = 0;
7405                 } else {
7406                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7407                         ret = 1;
7408                 }
7409         }
7410
7411         return ret;
7412 }
7413
7414
7415 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7416                               struct wpa_ssid *ssid)
7417 {
7418         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7419             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7420                                  wpa_s->parent, NULL) > 0) {
7421                 /**
7422                  * Remove the network by scheduling the group formation
7423                  * timeout to happen immediately. The teardown code
7424                  * needs to be scheduled to run asynch later so that we
7425                  * don't delete data from under ourselves unexpectedly.
7426                  * Calling wpas_p2p_group_formation_timeout directly
7427                  * causes a series of crashes in WPS failure scenarios.
7428                  */
7429                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7430                            "P2P group network getting removed");
7431                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7432                                        wpa_s->parent, NULL);
7433         }
7434 }
7435
7436
7437 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7438                                           const u8 *addr, const u8 *ssid,
7439                                           size_t ssid_len)
7440 {
7441         struct wpa_ssid *s;
7442         size_t i;
7443
7444         for (s = wpa_s->conf->ssid; s; s = s->next) {
7445                 if (s->disabled != 2)
7446                         continue;
7447                 if (ssid &&
7448                     (ssid_len != s->ssid_len ||
7449                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
7450                         continue;
7451                 if (addr == NULL) {
7452                         if (s->mode == WPAS_MODE_P2P_GO)
7453                                 return s;
7454                         continue;
7455                 }
7456                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7457                         return s; /* peer is GO in the persistent group */
7458                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7459                         continue;
7460                 for (i = 0; i < s->num_p2p_clients; i++) {
7461                         if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7462                                       addr, ETH_ALEN) == 0)
7463                                 return s; /* peer is P2P client in persistent
7464                                            * group */
7465                 }
7466         }
7467
7468         return NULL;
7469 }
7470
7471
7472 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7473                                        const u8 *addr)
7474 {
7475         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7476                                  wpa_s->parent, NULL) > 0) {
7477                 /*
7478                  * This can happen if WPS provisioning step is not terminated
7479                  * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7480                  * peer was able to connect, there is no need to time out group
7481                  * formation after this, though. In addition, this is used with
7482                  * the initial connection wait on the GO as a separate formation
7483                  * timeout and as such, expected to be hit after the initial WPS
7484                  * provisioning step.
7485                  */
7486                 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7487
7488                 if (!wpa_s->p2p_go_group_formation_completed &&
7489                     !wpa_s->group_formation_reported) {
7490                         /*
7491                          * GO has not yet notified group formation success since
7492                          * the WPS step was not completed cleanly. Do that
7493                          * notification now since the P2P Client was able to
7494                          * connect and as such, must have received the
7495                          * credential from the WPS step.
7496                          */
7497                         if (wpa_s->global->p2p)
7498                                 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7499                         wpas_group_formation_completed(wpa_s, 1, 0);
7500                 }
7501         }
7502         if (!wpa_s->p2p_go_group_formation_completed) {
7503                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7504                 wpa_s->p2p_go_group_formation_completed = 1;
7505                 wpa_s->global->p2p_group_formation = NULL;
7506                 wpa_s->p2p_in_provisioning = 0;
7507                 wpa_s->p2p_in_invitation = 0;
7508         }
7509         wpa_s->global->p2p_go_wait_client.sec = 0;
7510         if (addr == NULL)
7511                 return;
7512         wpas_p2p_add_persistent_group_client(wpa_s, addr);
7513 }
7514
7515
7516 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7517                                        int group_added)
7518 {
7519         struct wpa_supplicant *group = wpa_s;
7520         int ret = 0;
7521
7522         if (wpa_s->global->p2p_group_formation)
7523                 group = wpa_s->global->p2p_group_formation;
7524         wpa_s = wpa_s->global->p2p_init_wpa_s;
7525         offchannel_send_action_done(wpa_s);
7526         if (group_added)
7527                 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7528         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7529         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7530                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7531                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7532                          wpa_s->p2p_persistent_id,
7533                          wpa_s->p2p_pd_before_go_neg,
7534                          wpa_s->p2p_go_ht40,
7535                          wpa_s->p2p_go_vht);
7536         return ret;
7537 }
7538
7539
7540 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7541 {
7542         int res;
7543
7544         if (!wpa_s->p2p_fallback_to_go_neg ||
7545             wpa_s->p2p_in_provisioning <= 5)
7546                 return 0;
7547
7548         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7549                 return 0; /* peer operating as a GO */
7550
7551         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7552                 "fallback to GO Negotiation");
7553         wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7554                        "reason=GO-not-found");
7555         res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7556
7557         return res == 1 ? 2 : 1;
7558 }
7559
7560
7561 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7562 {
7563         struct wpa_supplicant *ifs;
7564
7565         if (wpa_s->wpa_state > WPA_SCANNING) {
7566                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7567                         "concurrent operation",
7568                         wpa_s->conf->p2p_search_delay);
7569                 return wpa_s->conf->p2p_search_delay;
7570         }
7571
7572         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7573                          radio_list) {
7574                 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7575                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7576                                 "delay due to concurrent operation on "
7577                                 "interface %s",
7578                                 wpa_s->conf->p2p_search_delay,
7579                                 ifs->ifname);
7580                         return wpa_s->conf->p2p_search_delay;
7581                 }
7582         }
7583
7584         return 0;
7585 }
7586
7587
7588 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7589                                      struct wpa_ssid *s, const u8 *addr,
7590                                      int iface_addr)
7591 {
7592         struct psk_list_entry *psk, *tmp;
7593         int changed = 0;
7594
7595         dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7596                               list) {
7597                 if ((iface_addr && !psk->p2p &&
7598                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7599                     (!iface_addr && psk->p2p &&
7600                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7601                         wpa_dbg(wpa_s, MSG_DEBUG,
7602                                 "P2P: Remove persistent group PSK list entry for "
7603                                 MACSTR " p2p=%u",
7604                                 MAC2STR(psk->addr), psk->p2p);
7605                         dl_list_del(&psk->list);
7606                         os_free(psk);
7607                         changed++;
7608                 }
7609         }
7610
7611         return changed;
7612 }
7613
7614
7615 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7616                          const u8 *p2p_dev_addr,
7617                          const u8 *psk, size_t psk_len)
7618 {
7619         struct wpa_ssid *ssid = wpa_s->current_ssid;
7620         struct wpa_ssid *persistent;
7621         struct psk_list_entry *p, *last;
7622
7623         if (psk_len != sizeof(p->psk))
7624                 return;
7625
7626         if (p2p_dev_addr) {
7627                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7628                         " p2p_dev_addr=" MACSTR,
7629                         MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7630                 if (is_zero_ether_addr(p2p_dev_addr))
7631                         p2p_dev_addr = NULL;
7632         } else {
7633                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7634                         MAC2STR(mac_addr));
7635         }
7636
7637         if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7638                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7639                 /* To be added to persistent group once created */
7640                 if (wpa_s->global->add_psk == NULL) {
7641                         wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7642                         if (wpa_s->global->add_psk == NULL)
7643                                 return;
7644                 }
7645                 p = wpa_s->global->add_psk;
7646                 if (p2p_dev_addr) {
7647                         p->p2p = 1;
7648                         os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7649                 } else {
7650                         p->p2p = 0;
7651                         os_memcpy(p->addr, mac_addr, ETH_ALEN);
7652                 }
7653                 os_memcpy(p->psk, psk, psk_len);
7654                 return;
7655         }
7656
7657         if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7658                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7659                 return;
7660         }
7661
7662         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7663                                              ssid->ssid_len);
7664         if (!persistent) {
7665                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7666                 return;
7667         }
7668
7669         p = os_zalloc(sizeof(*p));
7670         if (p == NULL)
7671                 return;
7672         if (p2p_dev_addr) {
7673                 p->p2p = 1;
7674                 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7675         } else {
7676                 p->p2p = 0;
7677                 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7678         }
7679         os_memcpy(p->psk, psk, psk_len);
7680
7681         if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7682             (last = dl_list_last(&persistent->psk_list,
7683                                  struct psk_list_entry, list))) {
7684                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7685                         MACSTR " (p2p=%u) to make room for a new one",
7686                         MAC2STR(last->addr), last->p2p);
7687                 dl_list_del(&last->list);
7688                 os_free(last);
7689         }
7690
7691         wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7692                                   p2p_dev_addr ? p2p_dev_addr : mac_addr,
7693                                   p2p_dev_addr == NULL);
7694         if (p2p_dev_addr) {
7695                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7696                         MACSTR, MAC2STR(p2p_dev_addr));
7697         } else {
7698                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7699                         MAC2STR(mac_addr));
7700         }
7701         dl_list_add(&persistent->psk_list, &p->list);
7702
7703         if (wpa_s->parent->conf->update_config &&
7704             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7705                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7706 }
7707
7708
7709 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7710                                 struct wpa_ssid *s, const u8 *addr,
7711                                 int iface_addr)
7712 {
7713         int res;
7714
7715         res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7716         if (res > 0 && wpa_s->conf->update_config &&
7717             wpa_config_write(wpa_s->confname, wpa_s->conf))
7718                 wpa_dbg(wpa_s, MSG_DEBUG,
7719                         "P2P: Failed to update configuration");
7720 }
7721
7722
7723 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7724                                       const u8 *peer, int iface_addr)
7725 {
7726         struct hostapd_data *hapd;
7727         struct hostapd_wpa_psk *psk, *prev, *rem;
7728         struct sta_info *sta;
7729
7730         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7731             wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7732                 return;
7733
7734         /* Remove per-station PSK entry */
7735         hapd = wpa_s->ap_iface->bss[0];
7736         prev = NULL;
7737         psk = hapd->conf->ssid.wpa_psk;
7738         while (psk) {
7739                 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7740                     (!iface_addr &&
7741                      os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7742                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7743                                 MACSTR " iface_addr=%d",
7744                                 MAC2STR(peer), iface_addr);
7745                         if (prev)
7746                                 prev->next = psk->next;
7747                         else
7748                                 hapd->conf->ssid.wpa_psk = psk->next;
7749                         rem = psk;
7750                         psk = psk->next;
7751                         os_free(rem);
7752                 } else {
7753                         prev = psk;
7754                         psk = psk->next;
7755                 }
7756         }
7757
7758         /* Disconnect from group */
7759         if (iface_addr)
7760                 sta = ap_get_sta(hapd, peer);
7761         else
7762                 sta = ap_get_sta_p2p(hapd, peer);
7763         if (sta) {
7764                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7765                         " (iface_addr=%d) from group",
7766                         MAC2STR(peer), iface_addr);
7767                 hostapd_drv_sta_deauth(hapd, sta->addr,
7768                                        WLAN_REASON_DEAUTH_LEAVING);
7769                 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7770         }
7771 }
7772
7773
7774 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7775                             int iface_addr)
7776 {
7777         struct wpa_ssid *s;
7778         struct wpa_supplicant *w;
7779         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
7780
7781         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7782
7783         /* Remove from any persistent group */
7784         for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
7785                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7786                         continue;
7787                 if (!iface_addr)
7788                         wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7789                 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
7790         }
7791
7792         /* Remove from any operating group */
7793         for (w = wpa_s->global->ifaces; w; w = w->next)
7794                 wpas_p2p_remove_client_go(w, peer, iface_addr);
7795 }
7796
7797
7798 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7799 {
7800         struct wpa_supplicant *wpa_s = eloop_ctx;
7801         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7802 }
7803
7804
7805 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7806 {
7807         struct wpa_supplicant *wpa_s = eloop_ctx;
7808
7809         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7810         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7811 }
7812
7813
7814 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7815                                         struct wpa_ssid *ssid)
7816 {
7817         struct wpa_supplicant *iface;
7818
7819         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7820                 if (!iface->current_ssid ||
7821                     iface->current_ssid->frequency == freq ||
7822                     (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7823                      !iface->current_ssid->p2p_group))
7824                         continue;
7825
7826                 /* Remove the connection with least priority */
7827                 if (!wpas_is_p2p_prioritized(iface)) {
7828                         /* STA connection has priority over existing
7829                          * P2P connection, so remove the interface. */
7830                         wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7831                         eloop_register_timeout(0, 0,
7832                                                wpas_p2p_group_freq_conflict,
7833                                                iface, NULL);
7834                         /* If connection in progress is P2P connection, do not
7835                          * proceed for the connection. */
7836                         if (wpa_s == iface)
7837                                 return -1;
7838                         else
7839                                 return 0;
7840                 } else {
7841                         /* P2P connection has priority, disable the STA network
7842                          */
7843                         wpa_supplicant_disable_network(wpa_s->global->ifaces,
7844                                                        ssid);
7845                         wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7846                                 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7847                         os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7848                                   ETH_ALEN);
7849                         /* If P2P connection is in progress, continue
7850                          * connecting...*/
7851                         if (wpa_s == iface)
7852                                 return 0;
7853                         else
7854                                 return -1;
7855                 }
7856         }
7857
7858         return 0;
7859 }
7860
7861
7862 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7863 {
7864         struct wpa_ssid *ssid = wpa_s->current_ssid;
7865
7866         if (ssid == NULL || !ssid->p2p_group)
7867                 return 0;
7868
7869         if (wpa_s->p2p_last_4way_hs_fail &&
7870             wpa_s->p2p_last_4way_hs_fail == ssid) {
7871                 u8 go_dev_addr[ETH_ALEN];
7872                 struct wpa_ssid *persistent;
7873
7874                 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7875                                               ssid->ssid,
7876                                               ssid->ssid_len) <= 0) {
7877                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7878                         goto disconnect;
7879                 }
7880
7881                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7882                         MACSTR, MAC2STR(go_dev_addr));
7883                 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7884                                                      ssid->ssid,
7885                                                      ssid->ssid_len);
7886                 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7887                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7888                         goto disconnect;
7889                 }
7890                 wpa_msg_global(wpa_s->parent, MSG_INFO,
7891                                P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7892                                persistent->id);
7893         disconnect:
7894                 wpa_s->p2p_last_4way_hs_fail = NULL;
7895                 /*
7896                  * Remove the group from a timeout to avoid issues with caller
7897                  * continuing to use the interface if this is on a P2P group
7898                  * interface.
7899                  */
7900                 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7901                                        wpa_s, NULL);
7902                 return 1;
7903         }
7904
7905         wpa_s->p2p_last_4way_hs_fail = ssid;
7906         return 0;
7907 }
7908
7909
7910 #ifdef CONFIG_WPS_NFC
7911
7912 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7913                                              struct wpabuf *p2p)
7914 {
7915         struct wpabuf *ret;
7916         size_t wsc_len;
7917
7918         if (p2p == NULL) {
7919                 wpabuf_free(wsc);
7920                 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7921                 return NULL;
7922         }
7923
7924         wsc_len = wsc ? wpabuf_len(wsc) : 0;
7925         ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7926         if (ret == NULL) {
7927                 wpabuf_free(wsc);
7928                 wpabuf_free(p2p);
7929                 return NULL;
7930         }
7931
7932         wpabuf_put_be16(ret, wsc_len);
7933         if (wsc)
7934                 wpabuf_put_buf(ret, wsc);
7935         wpabuf_put_be16(ret, wpabuf_len(p2p));
7936         wpabuf_put_buf(ret, p2p);
7937
7938         wpabuf_free(wsc);
7939         wpabuf_free(p2p);
7940         wpa_hexdump_buf(MSG_DEBUG,
7941                         "P2P: Generated NFC connection handover message", ret);
7942
7943         if (ndef && ret) {
7944                 struct wpabuf *tmp;
7945                 tmp = ndef_build_p2p(ret);
7946                 wpabuf_free(ret);
7947                 if (tmp == NULL) {
7948                         wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7949                         return NULL;
7950                 }
7951                 ret = tmp;
7952         }
7953
7954         return ret;
7955 }
7956
7957
7958 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7959                              struct wpa_ssid **ssid, u8 *go_dev_addr)
7960 {
7961         struct wpa_supplicant *iface;
7962
7963         if (go_dev_addr)
7964                 os_memset(go_dev_addr, 0, ETH_ALEN);
7965         if (ssid)
7966                 *ssid = NULL;
7967         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7968                 if (iface->wpa_state < WPA_ASSOCIATING ||
7969                     iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7970                     !iface->current_ssid->p2p_group ||
7971                     iface->current_ssid->mode != WPAS_MODE_INFRA)
7972                         continue;
7973                 if (ssid)
7974                         *ssid = iface->current_ssid;
7975                 if (go_dev_addr)
7976                         os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7977                 return iface->assoc_freq;
7978         }
7979         return 0;
7980 }
7981
7982
7983 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7984                                           int ndef)
7985 {
7986         struct wpabuf *wsc, *p2p;
7987         struct wpa_ssid *ssid;
7988         u8 go_dev_addr[ETH_ALEN];
7989         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7990
7991         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7992                 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7993                 return NULL;
7994         }
7995
7996         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7997             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7998                            &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7999                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8000                 return NULL;
8001         }
8002
8003         if (cli_freq == 0) {
8004                 wsc = wps_build_nfc_handover_req_p2p(
8005                         wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8006         } else
8007                 wsc = NULL;
8008         p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8009                                          go_dev_addr, ssid ? ssid->ssid : NULL,
8010                                          ssid ? ssid->ssid_len : 0);
8011
8012         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8013 }
8014
8015
8016 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8017                                           int ndef, int tag)
8018 {
8019         struct wpabuf *wsc, *p2p;
8020         struct wpa_ssid *ssid;
8021         u8 go_dev_addr[ETH_ALEN];
8022         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8023
8024         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8025                 return NULL;
8026
8027         if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8028             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8029                            &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8030                 return NULL;
8031
8032         if (cli_freq == 0) {
8033                 wsc = wps_build_nfc_handover_sel_p2p(
8034                         wpa_s->parent->wps,
8035                         tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8036                         DEV_PW_NFC_CONNECTION_HANDOVER,
8037                         wpa_s->conf->wps_nfc_dh_pubkey,
8038                         tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8039         } else
8040                 wsc = NULL;
8041         p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8042                                          go_dev_addr, ssid ? ssid->ssid : NULL,
8043                                          ssid ? ssid->ssid_len : 0);
8044
8045         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8046 }
8047
8048
8049 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8050                                    struct p2p_nfc_params *params)
8051 {
8052         wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8053                    "connection handover (freq=%d)",
8054                    params->go_freq);
8055
8056         if (params->go_freq && params->go_ssid_len) {
8057                 wpa_s->p2p_wps_method = WPS_NFC;
8058                 wpa_s->pending_join_wps_method = WPS_NFC;
8059                 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8060                 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8061                           ETH_ALEN);
8062                 return wpas_p2p_join_start(wpa_s, params->go_freq,
8063                                            params->go_ssid,
8064                                            params->go_ssid_len);
8065         }
8066
8067         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8068                                 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8069                                 params->go_freq, -1, 0, 1, 1);
8070 }
8071
8072
8073 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8074                                   struct p2p_nfc_params *params, int tag)
8075 {
8076         int res, persistent;
8077         struct wpa_ssid *ssid;
8078
8079         wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8080                    "connection handover");
8081         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8082                 ssid = wpa_s->current_ssid;
8083                 if (ssid == NULL)
8084                         continue;
8085                 if (ssid->mode != WPAS_MODE_P2P_GO)
8086                         continue;
8087                 if (wpa_s->ap_iface == NULL)
8088                         continue;
8089                 break;
8090         }
8091         if (wpa_s == NULL) {
8092                 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8093                 return -1;
8094         }
8095
8096         if (wpa_s->parent->p2p_oob_dev_pw_id !=
8097             DEV_PW_NFC_CONNECTION_HANDOVER &&
8098             !wpa_s->parent->p2p_oob_dev_pw) {
8099                 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8100                 return -1;
8101         }
8102         res = wpas_ap_wps_add_nfc_pw(
8103                 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8104                 wpa_s->parent->p2p_oob_dev_pw,
8105                 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8106                 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8107         if (res)
8108                 return res;
8109
8110         if (!tag) {
8111                 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8112                 return 0;
8113         }
8114
8115         if (!params->peer ||
8116             !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8117                 return 0;
8118
8119         wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8120                    " to join", MAC2STR(params->peer->p2p_device_addr));
8121
8122         wpa_s->global->p2p_invite_group = wpa_s;
8123         persistent = ssid->p2p_persistent_group &&
8124                 wpas_p2p_get_persistent(wpa_s->parent,
8125                                         params->peer->p2p_device_addr,
8126                                         ssid->ssid, ssid->ssid_len);
8127         wpa_s->parent->pending_invite_ssid_id = -1;
8128
8129         return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8130                           P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8131                           ssid->ssid, ssid->ssid_len, ssid->frequency,
8132                           wpa_s->global->p2p_dev_addr, persistent, 0,
8133                           wpa_s->parent->p2p_oob_dev_pw_id);
8134 }
8135
8136
8137 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8138                                     struct p2p_nfc_params *params,
8139                                     int forced_freq)
8140 {
8141         wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8142                    "connection handover");
8143         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8144                                 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8145                                 forced_freq, -1, 0, 1, 1);
8146 }
8147
8148
8149 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8150                                     struct p2p_nfc_params *params,
8151                                     int forced_freq)
8152 {
8153         int res;
8154
8155         wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8156                    "connection handover");
8157         res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8158                                WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8159                                forced_freq, -1, 0, 1, 1);
8160         if (res)
8161                 return res;
8162
8163         res = wpas_p2p_listen(wpa_s, 60);
8164         if (res) {
8165                 p2p_unauthorize(wpa_s->global->p2p,
8166                                 params->peer->p2p_device_addr);
8167         }
8168
8169         return res;
8170 }
8171
8172
8173 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8174                                             const struct wpabuf *data,
8175                                             int sel, int tag, int forced_freq)
8176 {
8177         const u8 *pos, *end;
8178         u16 len, id;
8179         struct p2p_nfc_params params;
8180         int res;
8181
8182         os_memset(&params, 0, sizeof(params));
8183         params.sel = sel;
8184
8185         wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8186
8187         pos = wpabuf_head(data);
8188         end = pos + wpabuf_len(data);
8189
8190         if (end - pos < 2) {
8191                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8192                            "attributes");
8193                 return -1;
8194         }
8195         len = WPA_GET_BE16(pos);
8196         pos += 2;
8197         if (len > end - pos) {
8198                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8199                            "attributes");
8200                 return -1;
8201         }
8202         params.wsc_attr = pos;
8203         params.wsc_len = len;
8204         pos += len;
8205
8206         if (end - pos < 2) {
8207                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8208                            "attributes");
8209                 return -1;
8210         }
8211         len = WPA_GET_BE16(pos);
8212         pos += 2;
8213         if (len > end - pos) {
8214                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8215                            "attributes");
8216                 return -1;
8217         }
8218         params.p2p_attr = pos;
8219         params.p2p_len = len;
8220         pos += len;
8221
8222         wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8223                     params.wsc_attr, params.wsc_len);
8224         wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8225                     params.p2p_attr, params.p2p_len);
8226         if (pos < end) {
8227                 wpa_hexdump(MSG_DEBUG,
8228                             "P2P: Ignored extra data after P2P attributes",
8229                             pos, end - pos);
8230         }
8231
8232         res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8233         if (res)
8234                 return res;
8235
8236         if (params.next_step == NO_ACTION)
8237                 return 0;
8238
8239         if (params.next_step == BOTH_GO) {
8240                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8241                         MAC2STR(params.peer->p2p_device_addr));
8242                 return 0;
8243         }
8244
8245         if (params.next_step == PEER_CLIENT) {
8246                 if (!is_zero_ether_addr(params.go_dev_addr)) {
8247                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8248                                 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8249                                 " ssid=\"%s\"",
8250                                 MAC2STR(params.peer->p2p_device_addr),
8251                                 params.go_freq,
8252                                 MAC2STR(params.go_dev_addr),
8253                                 wpa_ssid_txt(params.go_ssid,
8254                                              params.go_ssid_len));
8255                 } else {
8256                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8257                                 "peer=" MACSTR " freq=%d",
8258                                 MAC2STR(params.peer->p2p_device_addr),
8259                                 params.go_freq);
8260                 }
8261                 return 0;
8262         }
8263
8264         if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8265                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8266                         MACSTR, MAC2STR(params.peer->p2p_device_addr));
8267                 return 0;
8268         }
8269
8270         wpabuf_free(wpa_s->p2p_oob_dev_pw);
8271         wpa_s->p2p_oob_dev_pw = NULL;
8272
8273         if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8274                 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8275                            "received");
8276                 return -1;
8277         }
8278
8279         id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8280         wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8281         wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8282                     params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8283         os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8284                   params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8285         wpa_s->p2p_peer_oob_pk_hash_known = 1;
8286
8287         if (tag) {
8288                 if (id < 0x10) {
8289                         wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8290                                    "peer OOB Device Password Id %u", id);
8291                         return -1;
8292                 }
8293                 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8294                            "Device Password Id %u", id);
8295                 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8296                                 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8297                                 params.oob_dev_pw_len -
8298                                 WPS_OOB_PUBKEY_HASH_LEN - 2);
8299                 wpa_s->p2p_oob_dev_pw_id = id;
8300                 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8301                         params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8302                         params.oob_dev_pw_len -
8303                         WPS_OOB_PUBKEY_HASH_LEN - 2);
8304                 if (wpa_s->p2p_oob_dev_pw == NULL)
8305                         return -1;
8306
8307                 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8308                     wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8309                                    &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8310                         return -1;
8311         } else {
8312                 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8313                            "without Device Password");
8314                 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8315         }
8316
8317         switch (params.next_step) {
8318         case NO_ACTION:
8319         case BOTH_GO:
8320         case PEER_CLIENT:
8321                 /* already covered above */
8322                 return 0;
8323         case JOIN_GROUP:
8324                 return wpas_p2p_nfc_join_group(wpa_s, &params);
8325         case AUTH_JOIN:
8326                 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8327         case INIT_GO_NEG:
8328                 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8329         case RESP_GO_NEG:
8330                 /* TODO: use own OOB Dev Pw */
8331                 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8332         }
8333
8334         return -1;
8335 }
8336
8337
8338 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8339                              const struct wpabuf *data, int forced_freq)
8340 {
8341         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8342                 return -1;
8343
8344         return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8345 }
8346
8347
8348 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8349                                  const struct wpabuf *req,
8350                                  const struct wpabuf *sel, int forced_freq)
8351 {
8352         struct wpabuf *tmp;
8353         int ret;
8354
8355         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8356                 return -1;
8357
8358         wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8359
8360         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8361                           wpabuf_head(req), wpabuf_len(req));
8362         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8363                           wpabuf_head(sel), wpabuf_len(sel));
8364         if (forced_freq)
8365                 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8366         tmp = ndef_parse_p2p(init ? sel : req);
8367         if (tmp == NULL) {
8368                 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8369                 return -1;
8370         }
8371
8372         ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8373                                                forced_freq);
8374         wpabuf_free(tmp);
8375
8376         return ret;
8377 }
8378
8379
8380 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8381 {
8382         const u8 *if_addr;
8383         int go_intent = wpa_s->conf->p2p_go_intent;
8384         struct wpa_supplicant *iface;
8385
8386         if (wpa_s->global->p2p == NULL)
8387                 return -1;
8388
8389         if (!enabled) {
8390                 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8391                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8392                 {
8393                         if (!iface->ap_iface)
8394                                 continue;
8395                         hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8396                 }
8397                 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8398                                                  0, NULL);
8399                 if (wpa_s->p2p_nfc_tag_enabled)
8400                         wpas_p2p_remove_pending_group_interface(wpa_s);
8401                 wpa_s->p2p_nfc_tag_enabled = 0;
8402                 return 0;
8403         }
8404
8405         if (wpa_s->global->p2p_disabled)
8406                 return -1;
8407
8408         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8409             wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8410             wpa_s->conf->wps_nfc_dev_pw == NULL ||
8411             wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8412                 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8413                            "to allow static handover cases");
8414                 return -1;
8415         }
8416
8417         wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8418
8419         wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8420         wpabuf_free(wpa_s->p2p_oob_dev_pw);
8421         wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8422         if (wpa_s->p2p_oob_dev_pw == NULL)
8423                 return -1;
8424         wpa_s->p2p_peer_oob_pk_hash_known = 0;
8425
8426         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8427             wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8428                 /*
8429                  * P2P Group Interface present and the command came on group
8430                  * interface, so enable the token for the current interface.
8431                  */
8432                 wpa_s->create_p2p_iface = 0;
8433         } else {
8434                 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8435         }
8436
8437         if (wpa_s->create_p2p_iface) {
8438                 enum wpa_driver_if_type iftype;
8439                 /* Prepare to add a new interface for the group */
8440                 iftype = WPA_IF_P2P_GROUP;
8441                 if (go_intent == 15)
8442                         iftype = WPA_IF_P2P_GO;
8443                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8444                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8445                                    "interface for the group");
8446                         return -1;
8447                 }
8448
8449                 if_addr = wpa_s->pending_interface_addr;
8450         } else
8451                 if_addr = wpa_s->own_addr;
8452
8453         wpa_s->p2p_nfc_tag_enabled = enabled;
8454
8455         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8456                 struct hostapd_data *hapd;
8457                 if (iface->ap_iface == NULL)
8458                         continue;
8459                 hapd = iface->ap_iface->bss[0];
8460                 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8461                 hapd->conf->wps_nfc_dh_pubkey =
8462                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8463                 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8464                 hapd->conf->wps_nfc_dh_privkey =
8465                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8466                 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8467                 hapd->conf->wps_nfc_dev_pw =
8468                         wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8469                 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8470
8471                 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8472                         wpa_dbg(iface, MSG_DEBUG,
8473                                 "P2P: Failed to enable NFC Tag for GO");
8474                 }
8475         }
8476         p2p_set_authorized_oob_dev_pw_id(
8477                 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8478                 if_addr);
8479
8480         return 0;
8481 }
8482
8483 #endif /* CONFIG_WPS_NFC */
8484
8485
8486 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8487                                              struct wpa_used_freq_data *freqs,
8488                                              unsigned int num)
8489 {
8490         u8 curr_chan, cand, chan;
8491         unsigned int i;
8492
8493         /*
8494          * If possible, optimize the Listen channel to be a channel that is
8495          * already used by one of the other interfaces.
8496          */
8497         if (!wpa_s->conf->p2p_optimize_listen_chan)
8498                 return;
8499
8500         if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8501                 return;
8502
8503         curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8504         for (i = 0, cand = 0; i < num; i++) {
8505                 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8506                 if (curr_chan == chan) {
8507                         cand = 0;
8508                         break;
8509                 }
8510
8511                 if (chan == 1 || chan == 6 || chan == 11)
8512                         cand = chan;
8513         }
8514
8515         if (cand) {
8516                 wpa_dbg(wpa_s, MSG_DEBUG,
8517                         "P2P: Update Listen channel to %u based on operating channel",
8518                         cand);
8519                 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8520         }
8521 }
8522
8523
8524 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
8525 {
8526         struct hostapd_config *conf;
8527         struct p2p_go_neg_results params;
8528         struct csa_settings csa_settings;
8529         struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8530         int old_freq = current_ssid->frequency;
8531         int ret;
8532
8533         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8534                 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8535                 return -1;
8536         }
8537
8538         /*
8539          * TODO: This function may not always work correctly. For example,
8540          * when we have a running GO and a BSS on a DFS channel.
8541          */
8542         if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
8543                 wpa_dbg(wpa_s, MSG_DEBUG,
8544                         "P2P CSA: Failed to select new frequency for GO");
8545                 return -1;
8546         }
8547
8548         if (current_ssid->frequency == params.freq) {
8549                 wpa_dbg(wpa_s, MSG_DEBUG,
8550                         "P2P CSA: Selected same frequency - not moving GO");
8551                 return 0;
8552         }
8553
8554         conf = hostapd_config_defaults();
8555         if (!conf) {
8556                 wpa_dbg(wpa_s, MSG_DEBUG,
8557                         "P2P CSA: Failed to allocate default config");
8558                 return -1;
8559         }
8560
8561         current_ssid->frequency = params.freq;
8562         if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8563                 wpa_dbg(wpa_s, MSG_DEBUG,
8564                         "P2P CSA: Failed to create new GO config");
8565                 ret = -1;
8566                 goto out;
8567         }
8568
8569         if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8570                 wpa_dbg(wpa_s, MSG_DEBUG,
8571                         "P2P CSA: CSA to a different band is not supported");
8572                 ret = -1;
8573                 goto out;
8574         }
8575
8576         os_memset(&csa_settings, 0, sizeof(csa_settings));
8577         csa_settings.cs_count = P2P_GO_CSA_COUNT;
8578         csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8579         csa_settings.freq_params.freq = params.freq;
8580         csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8581         csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8582         csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8583
8584         if (conf->ieee80211ac) {
8585                 int freq1 = 0, freq2 = 0;
8586                 u8 chan, opclass;
8587
8588                 if (ieee80211_freq_to_channel_ext(params.freq,
8589                                                   conf->secondary_channel,
8590                                                   conf->vht_oper_chwidth,
8591                                                   &opclass, &chan) ==
8592                     NUM_HOSTAPD_MODES) {
8593                         wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8594                         ret = -1;
8595                         goto out;
8596                 }
8597
8598                 if (conf->vht_oper_centr_freq_seg0_idx)
8599                         freq1 = ieee80211_chan_to_freq(
8600                                 NULL, opclass,
8601                                 conf->vht_oper_centr_freq_seg0_idx);
8602
8603                 if (conf->vht_oper_centr_freq_seg1_idx)
8604                         freq2 = ieee80211_chan_to_freq(
8605                                 NULL, opclass,
8606                                 conf->vht_oper_centr_freq_seg1_idx);
8607
8608                 if (freq1 < 0 || freq2 < 0) {
8609                         wpa_dbg(wpa_s, MSG_DEBUG,
8610                                 "P2P CSA: Selected invalid VHT center freqs");
8611                         ret = -1;
8612                         goto out;
8613                 }
8614
8615                 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8616                 csa_settings.freq_params.center_freq1 = freq1;
8617                 csa_settings.freq_params.center_freq2 = freq2;
8618
8619                 switch (conf->vht_oper_chwidth) {
8620                 case VHT_CHANWIDTH_80MHZ:
8621                 case VHT_CHANWIDTH_80P80MHZ:
8622                         csa_settings.freq_params.bandwidth = 80;
8623                         break;
8624                 case VHT_CHANWIDTH_160MHZ:
8625                         csa_settings.freq_params.bandwidth = 160;
8626                         break;
8627                 }
8628         }
8629
8630         ret = ap_switch_channel(wpa_s, &csa_settings);
8631 out:
8632         current_ssid->frequency = old_freq;
8633         hostapd_config_free(conf);
8634         return ret;
8635 }
8636
8637
8638 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8639 {
8640         struct p2p_go_neg_results params;
8641         struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8642
8643         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8644
8645         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8646                 current_ssid->frequency);
8647
8648         /* Stop the AP functionality */
8649         /* TODO: Should do this in a way that does not indicated to possible
8650          * P2P Clients in the group that the group is terminated. */
8651         wpa_supplicant_ap_deinit(wpa_s);
8652
8653         /* Reselect the GO frequency */
8654         if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, NULL)) {
8655                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8656                 wpas_p2p_group_delete(wpa_s,
8657                                       P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8658                 return;
8659         }
8660         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8661                 params.freq);
8662
8663         if (params.freq &&
8664             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8665                 wpa_printf(MSG_DEBUG,
8666                            "P2P: Selected freq (%u MHz) is not valid for P2P",
8667                            params.freq);
8668                 wpas_p2p_group_delete(wpa_s,
8669                                       P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8670                 return;
8671         }
8672
8673         /* Update the frequency */
8674         current_ssid->frequency = params.freq;
8675         wpa_s->connect_without_scan = current_ssid;
8676         wpa_s->reassociate = 1;
8677         wpa_s->disconnected = 0;
8678         wpa_supplicant_req_scan(wpa_s, 0, 0);
8679 }
8680
8681
8682 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8683 {
8684         struct wpa_supplicant *wpa_s = eloop_ctx;
8685
8686         if (!wpa_s->ap_iface || !wpa_s->current_ssid)
8687                 return;
8688
8689         wpas_p2p_go_update_common_freqs(wpa_s);
8690
8691         /* Do not move GO in the middle of a CSA */
8692         if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8693                 wpa_printf(MSG_DEBUG,
8694                            "P2P: CSA is in progress - not moving GO");
8695                 return;
8696         }
8697
8698         /*
8699          * First, try a channel switch flow. If it is not supported or fails,
8700          * take down the GO and bring it up again.
8701          */
8702         if (wpas_p2p_move_go_csa(wpa_s) < 0)
8703                 wpas_p2p_move_go_no_csa(wpa_s);
8704 }
8705
8706
8707 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
8708 {
8709         struct wpa_supplicant *wpa_s = eloop_ctx;
8710         struct wpa_used_freq_data *freqs = NULL;
8711         unsigned int num = wpa_s->num_multichan_concurrent;
8712
8713         freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8714         if (!freqs)
8715                 return;
8716
8717         num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8718
8719         /* Previous attempt to move a GO was not possible -- try again. */
8720         wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
8721                                      WPAS_P2P_CHANNEL_UPDATE_ANY);
8722
8723         os_free(freqs);
8724 }
8725
8726
8727 /*
8728  * Consider moving a GO from its currently used frequency:
8729  * 1. It is possible that due to regulatory consideration the frequency
8730  *    can no longer be used and there is a need to evacuate the GO.
8731  * 2. It is possible that due to MCC considerations, it would be preferable
8732  *    to move the GO to a channel that is currently used by some other
8733  *    station interface.
8734  *
8735  * In case a frequency that became invalid is once again valid, cancel a
8736  * previously initiated GO frequency change.
8737  */
8738 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
8739                                             struct wpa_used_freq_data *freqs,
8740                                             unsigned int num)
8741 {
8742         unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
8743         unsigned int timeout;
8744         int freq;
8745
8746         wpas_p2p_go_update_common_freqs(wpa_s);
8747
8748         freq = wpa_s->current_ssid->frequency;
8749         for (i = 0, invalid_freq = 0; i < num; i++) {
8750                 if (freqs[i].freq == freq) {
8751                         flags = freqs[i].flags;
8752
8753                         /* The channel is invalid, must change it */
8754                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
8755                                 wpa_dbg(wpa_s, MSG_DEBUG,
8756                                         "P2P: Freq=%d MHz no longer valid for GO",
8757                                         freq);
8758                                 invalid_freq = 1;
8759                         }
8760                 } else if (freqs[i].flags == 0) {
8761                         /* Freq is not used by any other station interface */
8762                         continue;
8763                 } else if (!p2p_supported_freq(wpa_s->global->p2p,
8764                                                freqs[i].freq)) {
8765                         /* Freq is not valid for P2P use cases */
8766                         continue;
8767                 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8768                            P2P_GO_FREQ_MOVE_SCM) {
8769                         policy_move = 1;
8770                 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
8771                            P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
8772                            wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8773                         policy_move = 1;
8774                 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
8775                             P2P_GO_FREQ_MOVE_SCM_ECSA) &&
8776                            wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
8777                         if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
8778                                 policy_move = 1;
8779                         } else if ((wpa_s->drv_flags &
8780                                     WPA_DRIVER_FLAGS_AP_CSA) &&
8781                                    wpas_p2p_go_clients_support_ecsa(wpa_s)) {
8782                                 u8 chan;
8783
8784                                 /*
8785                                  * We do not support CSA between bands, so move
8786                                  * GO only within the same band.
8787                                  */
8788                                 if (wpa_s->ap_iface->current_mode->mode ==
8789                                     ieee80211_freq_to_chan(freqs[i].freq,
8790                                                            &chan))
8791                                         policy_move = 1;
8792                         }
8793                 }
8794         }
8795
8796         wpa_dbg(wpa_s, MSG_DEBUG,
8797                 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
8798                 invalid_freq, policy_move, flags);
8799
8800         /*
8801          * The channel is valid, or we are going to have a policy move, so
8802          * cancel timeout.
8803          */
8804         if (!invalid_freq || policy_move) {
8805                 wpa_dbg(wpa_s, MSG_DEBUG,
8806                         "P2P: Cancel a GO move from freq=%d MHz", freq);
8807                 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8808
8809                 if (wpas_p2p_in_progress(wpa_s)) {
8810                         wpa_dbg(wpa_s, MSG_DEBUG,
8811                                 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
8812                         eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
8813                                              wpa_s, NULL);
8814                         eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8815                                                wpas_p2p_reconsider_moving_go,
8816                                                wpa_s, NULL);
8817                         return;
8818                 }
8819         }
8820
8821         if (!invalid_freq && (!policy_move || flags != 0)) {
8822                 wpa_dbg(wpa_s, MSG_DEBUG,
8823                         "P2P: Not initiating a GO frequency change");
8824                 return;
8825         }
8826
8827         /*
8828          * Do not consider moving GO if it is in the middle of a CSA. When the
8829          * CSA is finished this flow should be retriggered.
8830          */
8831         if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8832                 wpa_dbg(wpa_s, MSG_DEBUG,
8833                         "P2P: Not initiating a GO frequency change - CSA is in progress");
8834                 return;
8835         }
8836
8837         if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
8838                 timeout = P2P_GO_FREQ_CHANGE_TIME;
8839         else
8840                 timeout = 0;
8841
8842         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
8843                 freq, timeout);
8844         eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
8845         eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
8846 }
8847
8848
8849 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
8850                                          struct wpa_used_freq_data *freqs,
8851                                          unsigned int num,
8852                                          enum wpas_p2p_channel_update_trig trig)
8853 {
8854         struct wpa_supplicant *ifs;
8855
8856         eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
8857                              NULL);
8858
8859         /*
8860          * Travers all the radio interfaces, and for each GO interface, check
8861          * if there is a need to move the GO from the frequency it is using,
8862          * or in case the frequency is valid again, cancel the evacuation flow.
8863          */
8864         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8865                          radio_list) {
8866                 if (ifs->current_ssid == NULL ||
8867                     ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
8868                         continue;
8869
8870                 /*
8871                  * The GO was just started or completed channel switch, no need
8872                  * to move it.
8873                  */
8874                 if (wpa_s == ifs &&
8875                     (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
8876                      trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
8877                         wpa_dbg(wpa_s, MSG_DEBUG,
8878                                 "P2P: GO move - schedule re-consideration");
8879                         eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
8880                                                wpas_p2p_reconsider_moving_go,
8881                                                wpa_s, NULL);
8882                         continue;
8883                 }
8884
8885                 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
8886         }
8887 }
8888
8889
8890 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8891 {
8892         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8893                 return;
8894
8895         wpas_p2p_update_channel_list(wpa_s,
8896                                      WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
8897 }
8898
8899
8900 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8901 {
8902         if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8903                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8904                         "the management interface is being removed");
8905                 wpas_p2p_deinit_global(wpa_s->global);
8906         }
8907 }
8908
8909
8910 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8911 {
8912         if (wpa_s->ap_iface->bss)
8913                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8914         wpas_p2p_group_deinit(wpa_s);
8915 }