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