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