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