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