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