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