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