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