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