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