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