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