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