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