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