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