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