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