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