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