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