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