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