]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/p2p_supplicant.c
P2P: Use consistent Device Capability in Beacon/Probe Response
[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"
aefb53bd 20#include "ap/p2p_hostapd.h"
20a0b03d
JM
21#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
b22128ef
JM
23#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
b22128ef
JM
28#include "notify.h"
29#include "scan.h"
30#include "bss.h"
24f6497c 31#include "offchannel.h"
b22128ef
JM
32#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
9b1ab931
JM
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
c8106615
JM
42#ifndef P2P_MAX_CLIENT_IDLE
43/*
44 * How many seconds to try to reconnect to the GO when connection in P2P client
45 * role has been lost.
46 */
47#define P2P_MAX_CLIENT_IDLE 10
48#endif /* P2P_MAX_CLIENT_IDLE */
49
361cdf34
JM
50#ifndef P2P_MAX_INITIAL_CONN_WAIT
51/*
52 * How many seconds to wait for initial 4-way handshake to get completed after
53 * WPS provisioning step.
54 */
55#define P2P_MAX_INITIAL_CONN_WAIT 10
56#endif /* P2P_MAX_INITIAL_CONN_WAIT */
57
9b1ab931 58
b22128ef
JM
59static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
60static struct wpa_supplicant *
61wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
62 int go);
63static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
ef922c4a 64static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
108def93 65static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
b31be3a0
JM
66 const u8 *dev_addr, enum p2p_wps_method wps_method,
67 int auto_join);
f63b8542
JM
68static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
69 void *timeout_ctx);
b22128ef 70static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
72044390 71static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
3071e181
JM
72static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
73static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
aa9bb764
JM
74static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
75 int group_added);
b22128ef
JM
76
77
78static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
79 struct wpa_scan_results *scan_res)
80{
81 size_t i;
82
9526fd29 83 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef
JM
84 return;
85
86 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
87 (int) scan_res->num);
88
89 for (i = 0; i < scan_res->num; i++) {
90 struct wpa_scan_res *bss = scan_res->res[i];
91 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
92 bss->freq, bss->level,
93 (const u8 *) (bss + 1),
94 bss->ie_len) > 0)
9bcf9541 95 break;
b22128ef
JM
96 }
97
98 p2p_scan_res_handled(wpa_s->global->p2p);
99}
100
101
046ef4aa
JMB
102static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
103 unsigned int num_req_dev_types,
360182ed 104 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
b22128ef
JM
105{
106 struct wpa_supplicant *wpa_s = ctx;
107 struct wpa_driver_scan_params params;
108 int ret;
109 struct wpabuf *wps_ie, *ies;
110 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
206e1f42 111 size_t ielen;
39185dfa 112 int was_in_p2p_scan;
b22128ef 113
9526fd29
JM
114 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
115 return -1;
116
b22128ef
JM
117 os_memset(&params, 0, sizeof(params));
118
119 /* P2P Wildcard SSID */
120 params.num_ssids = 1;
121 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
122 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
123
124 wpa_s->wps->dev.p2p = 1;
360182ed
JM
125 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
126 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
046ef4aa 127 num_req_dev_types, req_dev_types);
b22128ef
JM
128 if (wps_ie == NULL)
129 return -1;
130
206e1f42
JM
131 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
132 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
b22128ef
JM
133 if (ies == NULL) {
134 wpabuf_free(wps_ie);
135 return -1;
136 }
137 wpabuf_put_buf(ies, wps_ie);
138 wpabuf_free(wps_ie);
139
6d92fa6e 140 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
b22128ef 141
47185fc7 142 params.p2p_probe = 1;
b22128ef
JM
143 params.extra_ies = wpabuf_head(ies);
144 params.extra_ies_len = wpabuf_len(ies);
145
146 switch (type) {
147 case P2P_SCAN_SOCIAL:
148 params.freqs = social_channels;
149 break;
150 case P2P_SCAN_FULL:
151 break;
152 case P2P_SCAN_SPECIFIC:
153 social_channels[0] = freq;
154 social_channels[1] = 0;
155 params.freqs = social_channels;
156 break;
157 case P2P_SCAN_SOCIAL_PLUS_ONE:
158 social_channels[3] = freq;
159 params.freqs = social_channels;
160 break;
161 }
162
39185dfa 163 was_in_p2p_scan = wpa_s->scan_res_handler == wpas_p2p_scan_res_handler;
b22128ef 164 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
17fbb751 165 ret = wpa_drv_scan(wpa_s, &params);
b22128ef
JM
166
167 wpabuf_free(ies);
168
39185dfa 169 if (ret) {
0c96fd6d 170 wpa_s->scan_res_handler = NULL;
39185dfa
JM
171 if (wpa_s->scanning || was_in_p2p_scan) {
172 wpa_s->p2p_cb_on_scan_complete = 1;
173 ret = 1;
174 }
175 }
0c96fd6d 176
b22128ef
JM
177 return ret;
178}
179
180
b22128ef
JM
181static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
182{
183 switch (p2p_group_interface) {
184 case P2P_GROUP_INTERFACE_PENDING:
185 return WPA_IF_P2P_GROUP;
186 case P2P_GROUP_INTERFACE_GO:
187 return WPA_IF_P2P_GO;
188 case P2P_GROUP_INTERFACE_CLIENT:
189 return WPA_IF_P2P_CLIENT;
190 }
191
192 return WPA_IF_P2P_GROUP;
193}
194
195
6c0da49f
JM
196static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
197 const u8 *ssid,
198 size_t ssid_len, int *go)
199{
200 struct wpa_ssid *s;
201
202 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
203 for (s = wpa_s->conf->ssid; s; s = s->next) {
204 if (s->disabled != 0 || !s->p2p_group ||
205 s->ssid_len != ssid_len ||
206 os_memcmp(ssid, s->ssid, ssid_len) != 0)
207 continue;
208 if (s->mode == WPAS_MODE_P2P_GO &&
209 s != wpa_s->current_ssid)
210 continue;
211 if (go)
212 *go = s->mode == WPAS_MODE_P2P_GO;
213 return wpa_s;
214 }
215 }
216
217 return NULL;
218}
219
220
aa9bb764 221static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, int silent)
b22128ef
JM
222{
223 struct wpa_ssid *ssid;
224 char *gtype;
3071e181
JM
225 const char *reason;
226
b22128ef
JM
227 ssid = wpa_s->current_ssid;
228 if (ssid == NULL) {
229 /*
230 * The current SSID was not known, but there may still be a
231 * pending P2P group interface waiting for provisioning.
232 */
233 ssid = wpa_s->conf->ssid;
234 while (ssid) {
235 if (ssid->p2p_group &&
236 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
237 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
238 break;
239 ssid = ssid->next;
240 }
241 }
242 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
243 gtype = "GO";
244 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
245 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
246 wpa_s->reassociate = 0;
247 wpa_s->disconnected = 1;
248 wpa_supplicant_deauthenticate(wpa_s,
249 WLAN_REASON_DEAUTH_LEAVING);
250 gtype = "client";
251 } else
252 gtype = "GO";
72044390
JM
253 if (wpa_s->cross_connect_in_use) {
254 wpa_s->cross_connect_in_use = 0;
255 wpa_msg(wpa_s->parent, MSG_INFO,
256 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
257 wpa_s->ifname, wpa_s->cross_connect_uplink);
258 }
3071e181
JM
259 switch (wpa_s->removal_reason) {
260 case P2P_GROUP_REMOVAL_REQUESTED:
261 reason = " reason=REQUESTED";
262 break;
263 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
264 reason = " reason=IDLE";
265 break;
c973f386
JM
266 case P2P_GROUP_REMOVAL_UNAVAILABLE:
267 reason = " reason=UNAVAILABLE";
268 break;
3fc14102
JM
269 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
270 reason = " reason=GO_ENDING_SESSION";
271 break;
3071e181
JM
272 default:
273 reason = "";
274 break;
275 }
aa9bb764
JM
276 if (!silent) {
277 wpa_msg(wpa_s->parent, MSG_INFO,
278 P2P_EVENT_GROUP_REMOVED "%s %s%s",
279 wpa_s->ifname, gtype, reason);
280 }
408af93e 281
dddc7045
JM
282 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
283 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
a7a30b90 284
aa9bb764 285 if (!silent && ssid)
408af93e
JB
286 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
287
b22128ef
JM
288 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
289 struct wpa_global *global;
290 char *ifname;
291 enum wpa_driver_if_type type;
292 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
293 wpa_s->ifname);
294 global = wpa_s->global;
295 ifname = os_strdup(wpa_s->ifname);
296 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
df509539 297 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
b22128ef
JM
298 wpa_s = global->ifaces;
299 if (wpa_s && ifname)
300 wpa_drv_if_remove(wpa_s, type, ifname);
301 os_free(ifname);
302 return;
303 }
304
305 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
306 if (ssid && (ssid->p2p_group ||
307 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
308 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
309 int id = ssid->id;
20a0b03d
JM
310 if (ssid == wpa_s->current_ssid) {
311 wpa_sm_set_config(wpa_s->wpa, NULL);
312 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
b22128ef 313 wpa_s->current_ssid = NULL;
20a0b03d 314 }
c2762e41
JS
315 /*
316 * Networks objects created during any P2P activities are not
317 * exposed out as they might/will confuse certain non-P2P aware
318 * applications since these network objects won't behave like
319 * regular ones.
320 *
321 * Likewise, we don't send out network removed signals for such
322 * network objects.
323 */
b22128ef
JM
324 wpa_config_remove_network(wpa_s->conf, id);
325 wpa_supplicant_clear_status(wpa_s);
433cd2ce 326 wpa_supplicant_cancel_sched_scan(wpa_s);
b22128ef
JM
327 } else {
328 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
329 "found");
330 }
3c29244e
EP
331 if (wpa_s->ap_iface)
332 wpa_supplicant_ap_deinit(wpa_s);
333 else
334 wpa_drv_deinit_p2p_cli(wpa_s);
b22128ef
JM
335}
336
337
338static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
339 u8 *go_dev_addr,
340 const u8 *ssid, size_t ssid_len)
341{
342 struct wpa_bss *bss;
343 const u8 *bssid;
344 struct wpabuf *p2p;
345 u8 group_capab;
346 const u8 *addr;
347
348 if (wpa_s->go_params)
349 bssid = wpa_s->go_params->peer_interface_addr;
350 else
351 bssid = wpa_s->bssid;
352
353 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
354 if (bss == NULL) {
355 u8 iface_addr[ETH_ALEN];
356 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
357 iface_addr) == 0)
358 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
359 }
360 if (bss == NULL) {
361 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
362 "group is persistent - BSS " MACSTR " not found",
363 MAC2STR(bssid));
364 return 0;
365 }
366
367 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
368 if (p2p == NULL) {
369 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
370 "group is persistent - BSS " MACSTR
371 " did not include P2P IE", MAC2STR(bssid));
372 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
373 (u8 *) (bss + 1), bss->ie_len);
374 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
375 ((u8 *) bss + 1) + bss->ie_len,
376 bss->beacon_ie_len);
377 return 0;
378 }
379
380 group_capab = p2p_get_group_capab(p2p);
381 addr = p2p_get_go_dev_addr(p2p);
382 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
383 "group_capab=0x%x", group_capab);
384 if (addr) {
385 os_memcpy(go_dev_addr, addr, ETH_ALEN);
386 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
387 MAC2STR(addr));
388 } else
389 os_memset(go_dev_addr, 0, ETH_ALEN);
390 wpabuf_free(p2p);
391
392 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
393 "go_dev_addr=" MACSTR,
394 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
395
396 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
397}
398
399
4b6baa2f
JMB
400static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
401 struct wpa_ssid *ssid,
402 const u8 *go_dev_addr)
b22128ef
JM
403{
404 struct wpa_ssid *s;
405 int changed = 0;
406
407 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
408 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
409 for (s = wpa_s->conf->ssid; s; s = s->next) {
410 if (s->disabled == 2 &&
411 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
412 s->ssid_len == ssid->ssid_len &&
413 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
414 break;
415 }
416
417 if (s) {
418 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
419 "entry");
420 if (ssid->passphrase && !s->passphrase)
421 changed = 1;
422 else if (ssid->passphrase && s->passphrase &&
423 os_strcmp(ssid->passphrase, s->passphrase) != 0)
424 changed = 1;
425 } else {
426 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
427 "entry");
428 changed = 1;
429 s = wpa_config_add_network(wpa_s->conf);
430 if (s == NULL)
4b6baa2f 431 return -1;
c2762e41
JS
432
433 /*
434 * Instead of network_added we emit persistent_group_added
435 * notification. Also to keep the defense checks in
436 * persistent_group obj registration method, we set the
437 * relevant flags in s to designate it as a persistent group.
438 */
439 s->p2p_group = 1;
440 s->p2p_persistent_group = 1;
441 wpas_notify_persistent_group_added(wpa_s, s);
b22128ef
JM
442 wpa_config_set_network_defaults(s);
443 }
444
445 s->p2p_group = 1;
446 s->p2p_persistent_group = 1;
447 s->disabled = 2;
448 s->bssid_set = 1;
449 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
450 s->mode = ssid->mode;
451 s->auth_alg = WPA_AUTH_ALG_OPEN;
452 s->key_mgmt = WPA_KEY_MGMT_PSK;
453 s->proto = WPA_PROTO_RSN;
454 s->pairwise_cipher = WPA_CIPHER_CCMP;
d1c8ac88 455 s->export_keys = 1;
b22128ef
JM
456 if (ssid->passphrase) {
457 os_free(s->passphrase);
458 s->passphrase = os_strdup(ssid->passphrase);
459 }
460 if (ssid->psk_set) {
461 s->psk_set = 1;
462 os_memcpy(s->psk, ssid->psk, 32);
463 }
464 if (s->passphrase && !s->psk_set)
465 wpa_config_update_psk(s);
466 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
467 os_free(s->ssid);
468 s->ssid = os_malloc(ssid->ssid_len);
469 }
470 if (s->ssid) {
471 s->ssid_len = ssid->ssid_len;
472 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
473 }
474
475#ifndef CONFIG_NO_CONFIG_WRITE
476 if (changed && wpa_s->conf->update_config &&
477 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
478 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
479 }
480#endif /* CONFIG_NO_CONFIG_WRITE */
4b6baa2f
JMB
481
482 return s->id;
b22128ef
JM
483}
484
485
fbdcfd57
JM
486static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
487 const u8 *addr)
488{
489 struct wpa_ssid *ssid, *s;
490 u8 *n;
491 size_t i;
492
493 ssid = wpa_s->current_ssid;
494 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
495 !ssid->p2p_persistent_group)
496 return;
497
498 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
499 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
500 continue;
501
502 if (s->ssid_len == ssid->ssid_len &&
503 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
504 break;
505 }
506
507 if (s == NULL)
508 return;
509
510 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
511 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
512 ETH_ALEN) == 0)
513 return; /* already in list */
514 }
515
516 n = os_realloc(s->p2p_client_list,
517 (s->num_p2p_clients + 1) * ETH_ALEN);
518 if (n == NULL)
519 return;
520 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
521 s->p2p_client_list = n;
522 s->num_p2p_clients++;
523
524#ifndef CONFIG_NO_CONFIG_WRITE
525 if (wpa_s->parent->conf->update_config &&
526 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
527 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
528#endif /* CONFIG_NO_CONFIG_WRITE */
529}
530
531
b22128ef
JM
532static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
533 int success)
534{
535 struct wpa_ssid *ssid;
536 const char *ssid_txt;
537 int client;
538 int persistent;
539 u8 go_dev_addr[ETH_ALEN];
4b6baa2f 540 int network_id = -1;
b22128ef
JM
541
542 /*
543 * This callback is likely called for the main interface. Update wpa_s
544 * to use the group interface if a new interface was created for the
545 * group.
546 */
547 if (wpa_s->global->p2p_group_formation)
548 wpa_s = wpa_s->global->p2p_group_formation;
d1b024c9 549 wpa_s->global->p2p_group_formation = NULL;
b22128ef
JM
550 wpa_s->p2p_in_provisioning = 0;
551
552 if (!success) {
553 wpa_msg(wpa_s->parent, MSG_INFO,
554 P2P_EVENT_GROUP_FORMATION_FAILURE);
aa9bb764 555 wpas_p2p_group_delete(wpa_s, 0);
b22128ef
JM
556 return;
557 }
558
559 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
560
561 ssid = wpa_s->current_ssid;
562 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
563 ssid->mode = WPAS_MODE_P2P_GO;
564 p2p_group_notif_formation_done(wpa_s->p2p_group);
565 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
566 }
567
568 persistent = 0;
569 if (ssid) {
570 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
571 client = ssid->mode == WPAS_MODE_INFRA;
572 if (ssid->mode == WPAS_MODE_P2P_GO) {
573 persistent = ssid->p2p_persistent_group;
d7e70476 574 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
b22128ef
JM
575 ETH_ALEN);
576 } else
577 persistent = wpas_p2p_persistent_group(wpa_s,
578 go_dev_addr,
579 ssid->ssid,
580 ssid->ssid_len);
581 } else {
582 ssid_txt = "";
583 client = wpa_s->p2p_group_interface ==
584 P2P_GROUP_INTERFACE_CLIENT;
62c0d27e 585 os_memset(go_dev_addr, 0, ETH_ALEN);
b22128ef
JM
586 }
587
588 wpa_s->show_group_started = 0;
589 if (client) {
590 /*
591 * Indicate event only after successfully completed 4-way
592 * handshake, i.e., when the interface is ready for data
593 * packets.
594 */
595 wpa_s->show_group_started = 1;
596 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
597 char psk[65];
598 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
599 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
600 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
601 "%s",
602 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
603 MAC2STR(go_dev_addr),
b22128ef 604 persistent ? " [PERSISTENT]" : "");
72044390 605 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 606 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
607 } else {
608 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
609 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
610 "go_dev_addr=" MACSTR "%s",
d394a22f 611 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
b22128ef
JM
612 ssid && ssid->passphrase ? ssid->passphrase : "",
613 MAC2STR(go_dev_addr),
614 persistent ? " [PERSISTENT]" : "");
72044390 615 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 616 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
617 }
618
619 if (persistent)
4b6baa2f
JMB
620 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
621 ssid, go_dev_addr);
21fe9e75 622 if (network_id < 0 && ssid)
4b6baa2f
JMB
623 network_id = ssid->id;
624 if (!client)
625 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
b22128ef
JM
626}
627
628
24f6497c
JM
629static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
630 unsigned int freq,
631 const u8 *dst, const u8 *src,
632 const u8 *bssid,
633 const u8 *data, size_t data_len,
634 enum offchannel_send_action_result
635 result)
534525ff 636{
24f6497c 637 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
b22128ef 638
9526fd29 639 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef 640 return;
3ac17eba
JM
641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
642 return;
b22128ef 643
24f6497c
JM
644 switch (result) {
645 case OFFCHANNEL_SEND_ACTION_SUCCESS:
646 res = P2P_SEND_ACTION_SUCCESS;
647 break;
648 case OFFCHANNEL_SEND_ACTION_NO_ACK:
649 res = P2P_SEND_ACTION_NO_ACK;
650 break;
651 case OFFCHANNEL_SEND_ACTION_FAILED:
652 res = P2P_SEND_ACTION_FAILED;
653 break;
b22128ef
JM
654 }
655
24f6497c 656 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
b22128ef 657
f63b8542
JM
658 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
659 wpa_s->pending_pd_before_join &&
24f6497c
JM
660 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
661 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
b22128ef 662 wpa_s->pending_pd_before_join = 0;
aa9bb764
JM
663 if (wpa_s->p2p_fallback_to_go_neg) {
664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
665 "during p2p_connect-auto");
666 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
667 return;
668 }
669
b22128ef 670 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
f63b8542
JM
671 "join-existing-group operation (no ACK for PD "
672 "Req)");
b22128ef
JM
673 wpas_p2p_join_start(wpa_s);
674 }
675}
676
677
678static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
679 const u8 *src, const u8 *bssid, const u8 *buf,
680 size_t len, unsigned int wait_time)
681{
682 struct wpa_supplicant *wpa_s = ctx;
24f6497c
JM
683 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
684 wait_time,
b106173a 685 wpas_p2p_send_action_tx_status, 1);
b22128ef
JM
686}
687
688
689static void wpas_send_action_done(void *ctx)
690{
691 struct wpa_supplicant *wpa_s = ctx;
24f6497c 692 offchannel_send_action_done(wpa_s);
b22128ef
JM
693}
694
695
696static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
697 struct p2p_go_neg_results *params)
698{
699 if (wpa_s->go_params == NULL) {
700 wpa_s->go_params = os_malloc(sizeof(*params));
701 if (wpa_s->go_params == NULL)
702 return -1;
703 }
704 os_memcpy(wpa_s->go_params, params, sizeof(*params));
705 return 0;
706}
707
708
709static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
710 struct p2p_go_neg_results *res)
711{
3b29972c
JM
712 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
713 MAC2STR(res->peer_interface_addr));
e9a7ae41
JM
714 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
715 res->ssid, res->ssid_len);
b22128ef
JM
716 wpa_supplicant_ap_deinit(wpa_s);
717 wpas_copy_go_neg_results(wpa_s, res);
718 if (res->wps_method == WPS_PBC)
3b29972c 719 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
3c5126a4
JM
720 else {
721 u16 dev_pw_id = DEV_PW_DEFAULT;
722 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
723 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
b22128ef 724 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
3c5126a4
JM
725 wpa_s->p2p_pin, 1, dev_pw_id);
726 }
b22128ef
JM
727}
728
729
730static void p2p_go_configured(void *ctx, void *data)
731{
732 struct wpa_supplicant *wpa_s = ctx;
733 struct p2p_go_neg_results *params = data;
734 struct wpa_ssid *ssid;
4b6baa2f 735 int network_id = -1;
b22128ef
JM
736
737 ssid = wpa_s->current_ssid;
738 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
739 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
d1b024c9
JM
740 if (wpa_s->global->p2p_group_formation == wpa_s)
741 wpa_s->global->p2p_group_formation = NULL;
b22128ef 742 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
743 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
744 "go_dev_addr=" MACSTR "%s",
b22128ef
JM
745 wpa_s->ifname,
746 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
c481048f 747 ssid->frequency,
b22128ef 748 params->passphrase ? params->passphrase : "",
d7e70476 749 MAC2STR(wpa_s->global->p2p_dev_addr),
b22128ef 750 params->persistent_group ? " [PERSISTENT]" : "");
4b6baa2f 751
b22128ef 752 if (params->persistent_group)
4b6baa2f 753 network_id = wpas_p2p_store_persistent_group(
b22128ef 754 wpa_s->parent, ssid,
d7e70476 755 wpa_s->global->p2p_dev_addr);
4b6baa2f
JMB
756 if (network_id < 0)
757 network_id = ssid->id;
758 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
72044390 759 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 760 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
761 return;
762 }
763
764 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
765 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
766 params->peer_interface_addr)) {
767 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
768 "filtering");
769 return;
770 }
771 if (params->wps_method == WPS_PBC)
d601247c 772 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
c3daaf33 773 params->peer_device_addr);
b22128ef
JM
774 else if (wpa_s->p2p_pin[0])
775 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
776 wpa_s->p2p_pin, NULL, 0);
777 os_free(wpa_s->go_params);
778 wpa_s->go_params = NULL;
779}
780
781
782static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
783 struct p2p_go_neg_results *params,
784 int group_formation)
785{
786 struct wpa_ssid *ssid;
787
788 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
789 return;
790
791 ssid = wpa_config_add_network(wpa_s->conf);
792 if (ssid == NULL)
793 return;
794
a9e02d59
JM
795 wpa_s->show_group_started = 0;
796
b22128ef
JM
797 wpa_config_set_network_defaults(ssid);
798 ssid->temporary = 1;
799 ssid->p2p_group = 1;
800 ssid->p2p_persistent_group = params->persistent_group;
801 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
802 WPAS_MODE_P2P_GO;
803 ssid->frequency = params->freq;
804 ssid->ssid = os_zalloc(params->ssid_len + 1);
805 if (ssid->ssid) {
806 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
807 ssid->ssid_len = params->ssid_len;
808 }
809 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
810 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
811 ssid->proto = WPA_PROTO_RSN;
812 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
813 ssid->passphrase = os_strdup(params->passphrase);
814
815 wpa_s->ap_configured_cb = p2p_go_configured;
816 wpa_s->ap_configured_cb_ctx = wpa_s;
817 wpa_s->ap_configured_cb_data = wpa_s->go_params;
7dcdcfd6 818 wpa_s->connect_without_scan = ssid;
b22128ef
JM
819 wpa_s->reassociate = 1;
820 wpa_s->disconnected = 0;
821 wpa_supplicant_req_scan(wpa_s, 0, 0);
822}
823
824
825static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
826 const struct wpa_supplicant *src)
827{
828 struct wpa_config *d;
829 const struct wpa_config *s;
830
831 d = dst->conf;
832 s = src->conf;
833
834#define C(n) if (s->n) d->n = os_strdup(s->n)
835 C(device_name);
836 C(manufacturer);
837 C(model_name);
838 C(model_number);
839 C(serial_number);
b22128ef
JM
840 C(config_methods);
841#undef C
3071e181 842
2f646b6e
JB
843 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
844 os_memcpy(d->sec_device_type, s->sec_device_type,
845 sizeof(d->sec_device_type));
846 d->num_sec_device_types = s->num_sec_device_types;
847
3071e181 848 d->p2p_group_idle = s->p2p_group_idle;
b029bd33 849 d->p2p_intra_bss = s->p2p_intra_bss;
acc247b2 850 d->persistent_reconnect = s->persistent_reconnect;
f571b593 851 d->max_num_sta = s->max_num_sta;
1298c145 852 d->pbc_in_m1 = s->pbc_in_m1;
b22128ef
JM
853}
854
855
856static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
857 enum wpa_driver_if_type type)
858{
859 char ifname[120], force_ifname[120];
860
861 if (wpa_s->pending_interface_name[0]) {
862 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
863 "- skip creation of a new one");
864 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
865 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
866 "unknown?! ifname='%s'",
867 wpa_s->pending_interface_name);
868 return -1;
869 }
870 return 0;
871 }
872
a1eca5cf 873 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
b22128ef 874 wpa_s->p2p_group_idx);
a1eca5cf
JM
875 if (os_strlen(ifname) >= IFNAMSIZ &&
876 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
877 /* Try to avoid going over the IFNAMSIZ length limit */
878 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
879 wpa_s->p2p_group_idx);
880 }
b22128ef
JM
881 force_ifname[0] = '\0';
882
883 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
884 ifname);
885 wpa_s->p2p_group_idx++;
886
887 wpa_s->pending_interface_type = type;
888 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
e17a2477 889 wpa_s->pending_interface_addr, NULL) < 0) {
b22128ef
JM
890 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
891 "interface");
892 return -1;
893 }
894
895 if (force_ifname[0]) {
896 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
897 force_ifname);
898 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
899 sizeof(wpa_s->pending_interface_name));
900 } else
901 os_strlcpy(wpa_s->pending_interface_name, ifname,
902 sizeof(wpa_s->pending_interface_name));
903 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
904 MACSTR, wpa_s->pending_interface_name,
905 MAC2STR(wpa_s->pending_interface_addr));
906
907 return 0;
908}
909
910
911static void wpas_p2p_remove_pending_group_interface(
912 struct wpa_supplicant *wpa_s)
913{
914 if (!wpa_s->pending_interface_name[0] ||
915 is_zero_ether_addr(wpa_s->pending_interface_addr))
916 return; /* No pending virtual interface */
917
918 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
919 wpa_s->pending_interface_name);
920 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
921 wpa_s->pending_interface_name);
922 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
923 wpa_s->pending_interface_name[0] = '\0';
924}
925
926
927static struct wpa_supplicant *
928wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
929{
930 struct wpa_interface iface;
931 struct wpa_supplicant *group_wpa_s;
932
933 if (!wpa_s->pending_interface_name[0]) {
934 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
d75e8806
JM
935 if (!wpas_p2p_create_iface(wpa_s))
936 return NULL;
937 /*
938 * Something has forced us to remove the pending interface; try
939 * to create a new one and hope for the best that we will get
940 * the same local address.
941 */
942 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
943 WPA_IF_P2P_CLIENT) < 0)
944 return NULL;
b22128ef
JM
945 }
946
947 os_memset(&iface, 0, sizeof(iface));
948 iface.ifname = wpa_s->pending_interface_name;
949 iface.driver = wpa_s->driver->name;
950 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
951 iface.driver_param = wpa_s->conf->driver_param;
952 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
953 if (group_wpa_s == NULL) {
954 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
955 "wpa_supplicant interface");
956 return NULL;
957 }
958 wpa_s->pending_interface_name[0] = '\0';
959 group_wpa_s->parent = wpa_s;
960 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
961 P2P_GROUP_INTERFACE_CLIENT;
962 wpa_s->global->p2p_group_formation = group_wpa_s;
963
964 wpas_p2p_clone_config(group_wpa_s, wpa_s);
965
966 return group_wpa_s;
967}
968
969
970static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
971 void *timeout_ctx)
972{
973 struct wpa_supplicant *wpa_s = eloop_ctx;
974 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
975 if (wpa_s->global->p2p)
976 p2p_group_formation_failed(wpa_s->global->p2p);
3ac17eba
JM
977 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
978 wpa_drv_p2p_group_formation_failed(wpa_s);
b22128ef
JM
979 wpas_group_formation_completed(wpa_s, 0);
980}
981
982
983void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
984{
985 struct wpa_supplicant *wpa_s = ctx;
986
8eada5ca 987 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
b22128ef
JM
988 wpa_drv_cancel_remain_on_channel(wpa_s);
989 wpa_s->off_channel_freq = 0;
07a30a66 990 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
991 }
992
993 if (res->status) {
994 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
995 res->status);
e5a359cf 996 wpas_notify_p2p_go_neg_completed(wpa_s, res);
b22128ef
JM
997 wpas_p2p_remove_pending_group_interface(wpa_s);
998 return;
999 }
1000
1001 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
e5a359cf 1002 wpas_notify_p2p_go_neg_completed(wpa_s, res);
b22128ef 1003
23c84252
JM
1004 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1005 struct wpa_ssid *ssid;
1006 ssid = wpa_config_get_network(wpa_s->conf,
1007 wpa_s->p2p_persistent_id);
1008 if (ssid && ssid->disabled == 2 &&
1009 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1010 size_t len = os_strlen(ssid->passphrase);
1011 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1012 "on requested persistent group");
1013 os_memcpy(res->passphrase, ssid->passphrase, len);
1014 res->passphrase[len] = '\0';
1015 }
1016 }
1017
b22128ef
JM
1018 if (wpa_s->create_p2p_iface) {
1019 struct wpa_supplicant *group_wpa_s =
1020 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1021 if (group_wpa_s == NULL) {
1022 wpas_p2p_remove_pending_group_interface(wpa_s);
1023 return;
1024 }
3c5126a4 1025 if (group_wpa_s != wpa_s) {
b22128ef
JM
1026 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1027 sizeof(group_wpa_s->p2p_pin));
3c5126a4
JM
1028 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1029 }
b22128ef
JM
1030 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1031 wpa_s->pending_interface_name[0] = '\0';
1032 group_wpa_s->p2p_in_provisioning = 1;
1033
1034 if (res->role_go)
1035 wpas_start_wps_go(group_wpa_s, res, 1);
1036 else
1037 wpas_start_wps_enrollee(group_wpa_s, res);
1038 } else {
1039 wpa_s->p2p_in_provisioning = 1;
1040 wpa_s->global->p2p_group_formation = wpa_s;
1041
1042 if (res->role_go)
1043 wpas_start_wps_go(wpa_s, res, 1);
1044 else
1045 wpas_start_wps_enrollee(ctx, res);
1046 }
1047
1048 wpa_s->p2p_long_listen = 0;
1049 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1050
1051 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ae3e3421
JM
1052 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1053 (res->peer_config_timeout % 100) * 10000,
1054 wpas_p2p_group_formation_timeout, wpa_s, NULL);
b22128ef
JM
1055}
1056
1057
3dfda83d 1058void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
b22128ef
JM
1059{
1060 struct wpa_supplicant *wpa_s = ctx;
3dfda83d
JM
1061 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1062 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
32d1bce0
KRK
1063
1064 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
b22128ef
JM
1065}
1066
1067
c5db8e51 1068void wpas_dev_found(void *ctx, const u8 *addr,
8fd7dc1b
JB
1069 const struct p2p_peer_info *info,
1070 int new_device)
b22128ef 1071{
5506d184 1072#ifndef CONFIG_NO_STDOUT_DEBUG
b22128ef
JM
1073 struct wpa_supplicant *wpa_s = ctx;
1074 char devtype[WPS_DEV_TYPE_BUFSIZE];
c5db8e51 1075
b22128ef
JM
1076 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1077 " p2p_dev_addr=" MACSTR
1078 " pri_dev_type=%s name='%s' config_methods=0x%x "
1079 "dev_capab=0x%x group_capab=0x%x",
c5db8e51
KRK
1080 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1081 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1082 sizeof(devtype)),
1083 info->device_name, info->config_methods,
1084 info->dev_capab, info->group_capab);
5506d184 1085#endif /* CONFIG_NO_STDOUT_DEBUG */
d642d2d2
JB
1086
1087 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
b22128ef
JM
1088}
1089
1090
56eeb8f2
JB
1091static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1092{
1093 struct wpa_supplicant *wpa_s = ctx;
1094
3074d8f1
JJ
1095 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1096 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1097
56eeb8f2
JB
1098 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1099}
1100
1101
b22128ef
JM
1102static int wpas_start_listen(void *ctx, unsigned int freq,
1103 unsigned int duration,
1104 const struct wpabuf *probe_resp_ie)
1105{
1106 struct wpa_supplicant *wpa_s = ctx;
1107
0e2e565a 1108 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
b22128ef
JM
1109
1110 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1111 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1112 "report received Probe Request frames");
1113 return -1;
1114 }
1115
1116 wpa_s->pending_listen_freq = freq;
1117 wpa_s->pending_listen_duration = duration;
1118
1119 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1120 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1121 "to remain on channel (%u MHz) for Listen "
1122 "state", freq);
1123 wpa_s->pending_listen_freq = 0;
1124 return -1;
1125 }
09d660b9 1126 wpa_s->off_channel_freq = 0;
07a30a66 1127 wpa_s->roc_waiting_drv_freq = freq;
b22128ef
JM
1128
1129 return 0;
1130}
1131
1132
1133static void wpas_stop_listen(void *ctx)
1134{
1135 struct wpa_supplicant *wpa_s = ctx;
6cb22d2f 1136 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
b22128ef
JM
1137 wpa_drv_cancel_remain_on_channel(wpa_s);
1138 wpa_s->off_channel_freq = 0;
07a30a66 1139 wpa_s->roc_waiting_drv_freq = 0;
b22128ef 1140 }
839b33ad 1141 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
b22128ef
JM
1142 wpa_drv_probe_req_report(wpa_s, 0);
1143}
1144
1145
1146static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1147{
1148 struct wpa_supplicant *wpa_s = ctx;
9dbf53fe 1149 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
b22128ef
JM
1150}
1151
1152
1153static struct p2p_srv_bonjour *
1154wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1155 const struct wpabuf *query)
1156{
1157 struct p2p_srv_bonjour *bsrv;
1158 size_t len;
1159
1160 len = wpabuf_len(query);
1161 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1162 struct p2p_srv_bonjour, list) {
1163 if (len == wpabuf_len(bsrv->query) &&
1164 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1165 len) == 0)
1166 return bsrv;
1167 }
1168 return NULL;
1169}
1170
1171
1172static struct p2p_srv_upnp *
1173wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1174 const char *service)
1175{
1176 struct p2p_srv_upnp *usrv;
1177
1178 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1179 struct p2p_srv_upnp, list) {
1180 if (version == usrv->version &&
1181 os_strcmp(service, usrv->service) == 0)
1182 return usrv;
1183 }
1184 return NULL;
1185}
1186
1187
1188static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1189 u8 srv_trans_id)
1190{
1191 u8 *len_pos;
1192
1193 if (wpabuf_tailroom(resp) < 5)
1194 return;
1195
1196 /* Length (to be filled) */
1197 len_pos = wpabuf_put(resp, 2);
1198 wpabuf_put_u8(resp, srv_proto);
1199 wpabuf_put_u8(resp, srv_trans_id);
1200 /* Status Code */
1201 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1202 /* Response Data: empty */
1203 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1204}
1205
1206
1207static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1208 struct wpabuf *resp, u8 srv_trans_id)
1209{
1210 struct p2p_srv_bonjour *bsrv;
1211 u8 *len_pos;
1212
1213 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1214
1215 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1216 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1217 return;
1218 }
1219
1220 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1221 struct p2p_srv_bonjour, list) {
1222 if (wpabuf_tailroom(resp) <
1223 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1224 return;
1225 /* Length (to be filled) */
1226 len_pos = wpabuf_put(resp, 2);
1227 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1228 wpabuf_put_u8(resp, srv_trans_id);
1229 /* Status Code */
1230 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1231 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1232 wpabuf_head(bsrv->resp),
1233 wpabuf_len(bsrv->resp));
1234 /* Response Data */
1235 wpabuf_put_buf(resp, bsrv->query); /* Key */
1236 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1237 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1238 2);
1239 }
1240}
1241
1242
1243static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1244 struct wpabuf *resp, u8 srv_trans_id,
1245 const u8 *query, size_t query_len)
1246{
1247 struct p2p_srv_bonjour *bsrv;
1248 struct wpabuf buf;
1249 u8 *len_pos;
1250
1251 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1252 query, query_len);
1253 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1254 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1255 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1256 srv_trans_id);
1257 return;
1258 }
1259
1260 if (query_len == 0) {
1261 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1262 return;
1263 }
1264
1265 if (wpabuf_tailroom(resp) < 5)
1266 return;
1267 /* Length (to be filled) */
1268 len_pos = wpabuf_put(resp, 2);
1269 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1270 wpabuf_put_u8(resp, srv_trans_id);
1271
1272 wpabuf_set(&buf, query, query_len);
1273 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1274 if (bsrv == NULL) {
1275 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1276 "available");
1277
1278 /* Status Code */
bf608cad 1279 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1280 /* Response Data: empty */
1281 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1282 2);
1283 return;
1284 }
1285
1286 /* Status Code */
1287 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1288 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1289 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1290
1291 if (wpabuf_tailroom(resp) >=
1292 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1293 /* Response Data */
1294 wpabuf_put_buf(resp, bsrv->query); /* Key */
1295 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1296 }
1297 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1298}
1299
1300
1301static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1302 struct wpabuf *resp, u8 srv_trans_id)
1303{
1304 struct p2p_srv_upnp *usrv;
1305 u8 *len_pos;
1306
1307 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1308
1309 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1310 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1311 return;
1312 }
1313
1314 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1315 struct p2p_srv_upnp, list) {
1316 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1317 return;
1318
1319 /* Length (to be filled) */
1320 len_pos = wpabuf_put(resp, 2);
1321 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1322 wpabuf_put_u8(resp, srv_trans_id);
1323
1324 /* Status Code */
1325 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1326 /* Response Data */
1327 wpabuf_put_u8(resp, usrv->version);
1328 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1329 usrv->service);
1330 wpabuf_put_str(resp, usrv->service);
1331 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1332 2);
1333 }
1334}
1335
1336
1337static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1338 struct wpabuf *resp, u8 srv_trans_id,
1339 const u8 *query, size_t query_len)
1340{
1341 struct p2p_srv_upnp *usrv;
1342 u8 *len_pos;
1343 u8 version;
1344 char *str;
1345 int count = 0;
1346
1347 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1348 query, query_len);
1349
1350 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1351 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1352 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1353 srv_trans_id);
1354 return;
1355 }
1356
1357 if (query_len == 0) {
1358 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1359 return;
1360 }
1361
b22128ef
JM
1362 if (wpabuf_tailroom(resp) < 5)
1363 return;
1364
1365 /* Length (to be filled) */
1366 len_pos = wpabuf_put(resp, 2);
1367 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1368 wpabuf_put_u8(resp, srv_trans_id);
1369
79222cfa
JM
1370 version = query[0];
1371 str = os_malloc(query_len);
1372 if (str == NULL)
1373 return;
1374 os_memcpy(str, query + 1, query_len - 1);
1375 str[query_len - 1] = '\0';
1376
b22128ef
JM
1377 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1378 struct p2p_srv_upnp, list) {
1379 if (version != usrv->version)
1380 continue;
1381
1382 if (os_strcmp(str, "ssdp:all") != 0 &&
1383 os_strstr(usrv->service, str) == NULL)
1384 continue;
1385
1386 if (wpabuf_tailroom(resp) < 2)
1387 break;
1388 if (count == 0) {
1389 /* Status Code */
1390 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1391 /* Response Data */
1392 wpabuf_put_u8(resp, version);
1393 } else
1394 wpabuf_put_u8(resp, ',');
1395
1396 count++;
1397
1398 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1399 usrv->service);
1400 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1401 break;
1402 wpabuf_put_str(resp, usrv->service);
1403 }
714b8b53 1404 os_free(str);
b22128ef
JM
1405
1406 if (count == 0) {
1407 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1408 "available");
1409 /* Status Code */
bf608cad 1410 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1411 /* Response Data: empty */
1412 }
1413
1414 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1415}
1416
1417
1418void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1419 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1420{
1421 struct wpa_supplicant *wpa_s = ctx;
1422 const u8 *pos = tlvs;
1423 const u8 *end = tlvs + tlvs_len;
1424 const u8 *tlv_end;
1425 u16 slen;
1426 struct wpabuf *resp;
1427 u8 srv_proto, srv_trans_id;
1428 size_t buf_len;
1429 char *buf;
1430
1431 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1432 tlvs, tlvs_len);
1433 buf_len = 2 * tlvs_len + 1;
1434 buf = os_malloc(buf_len);
1435 if (buf) {
1436 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1437 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1438 MACSTR " %u %u %s",
1439 freq, MAC2STR(sa), dialog_token, update_indic,
1440 buf);
1441 os_free(buf);
1442 }
1443
3b655312 1444 if (wpa_s->p2p_sd_over_ctrl_iface) {
1445 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1446 update_indic, tlvs, tlvs_len);
b22128ef 1447 return; /* to be processed by an external program */
3b655312 1448 }
b22128ef
JM
1449
1450 resp = wpabuf_alloc(10000);
1451 if (resp == NULL)
1452 return;
1453
1454 while (pos + 1 < end) {
1455 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1456 slen = WPA_GET_LE16(pos);
1457 pos += 2;
1458 if (pos + slen > end || slen < 2) {
1459 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1460 "length");
1461 wpabuf_free(resp);
1462 return;
1463 }
1464 tlv_end = pos + slen;
1465
1466 srv_proto = *pos++;
1467 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1468 srv_proto);
1469 srv_trans_id = *pos++;
1470 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1471 srv_trans_id);
1472
1473 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1474 pos, tlv_end - pos);
1475
6e6963ea
JM
1476
1477 if (wpa_s->force_long_sd) {
1478 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1479 "response");
1480 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1481 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1482 goto done;
1483 }
1484
b22128ef
JM
1485 switch (srv_proto) {
1486 case P2P_SERV_ALL_SERVICES:
1487 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1488 "for all services");
1489 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1490 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1491 wpa_printf(MSG_DEBUG, "P2P: No service "
1492 "discovery protocols available");
1493 wpas_sd_add_proto_not_avail(
1494 resp, P2P_SERV_ALL_SERVICES,
1495 srv_trans_id);
1496 break;
1497 }
1498 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1499 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1500 break;
1501 case P2P_SERV_BONJOUR:
1502 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1503 pos, tlv_end - pos);
1504 break;
1505 case P2P_SERV_UPNP:
1506 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1507 pos, tlv_end - pos);
1508 break;
1509 default:
1510 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1511 "protocol %u", srv_proto);
1512 wpas_sd_add_proto_not_avail(resp, srv_proto,
1513 srv_trans_id);
1514 break;
1515 }
1516
1517 pos = tlv_end;
1518 }
1519
6e6963ea 1520done:
e1653cac
KRK
1521 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1522 update_indic, tlvs, tlvs_len);
1523
b22128ef
JM
1524 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1525
1526 wpabuf_free(resp);
1527}
1528
1529
1530void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1531 const u8 *tlvs, size_t tlvs_len)
1532{
1533 struct wpa_supplicant *wpa_s = ctx;
1534 const u8 *pos = tlvs;
1535 const u8 *end = tlvs + tlvs_len;
1536 const u8 *tlv_end;
1537 u16 slen;
1538 size_t buf_len;
1539 char *buf;
1540
1541 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1542 tlvs, tlvs_len);
18708aad
JM
1543 if (tlvs_len > 1500) {
1544 /* TODO: better way for handling this */
1545 wpa_msg_ctrl(wpa_s, MSG_INFO,
1546 P2P_EVENT_SERV_DISC_RESP MACSTR
1547 " %u <long response: %u bytes>",
1548 MAC2STR(sa), update_indic,
1549 (unsigned int) tlvs_len);
1550 } else {
1551 buf_len = 2 * tlvs_len + 1;
1552 buf = os_malloc(buf_len);
1553 if (buf) {
1554 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1555 wpa_msg_ctrl(wpa_s, MSG_INFO,
1556 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1557 MAC2STR(sa), update_indic, buf);
1558 os_free(buf);
1559 }
b22128ef
JM
1560 }
1561
1562 while (pos < end) {
1563 u8 srv_proto, srv_trans_id, status;
1564
1565 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1566 slen = WPA_GET_LE16(pos);
1567 pos += 2;
1568 if (pos + slen > end || slen < 3) {
1569 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1570 "length");
1571 return;
1572 }
1573 tlv_end = pos + slen;
1574
1575 srv_proto = *pos++;
1576 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1577 srv_proto);
1578 srv_trans_id = *pos++;
1579 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1580 srv_trans_id);
1581 status = *pos++;
1582 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1583 status);
1584
1585 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1586 pos, tlv_end - pos);
1587
1588 pos = tlv_end;
1589 }
43a26f60
KRK
1590
1591 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
b22128ef
JM
1592}
1593
1594
7165c5dc
JM
1595u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1596 const struct wpabuf *tlvs)
b22128ef 1597{
3ac17eba 1598 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
7165c5dc 1599 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
9526fd29 1600 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7165c5dc
JM
1601 return 0;
1602 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
b22128ef
JM
1603}
1604
1605
7165c5dc
JM
1606u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1607 u8 version, const char *query)
b22128ef
JM
1608{
1609 struct wpabuf *tlvs;
7165c5dc 1610 u64 ret;
b22128ef
JM
1611
1612 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1613 if (tlvs == NULL)
7165c5dc 1614 return 0;
b22128ef
JM
1615 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1616 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1617 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1618 wpabuf_put_u8(tlvs, version);
1619 wpabuf_put_str(tlvs, query);
1620 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1621 wpabuf_free(tlvs);
1622 return ret;
1623}
1624
1625
7165c5dc 1626int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
b22128ef 1627{
3ac17eba 1628 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
7165c5dc 1629 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
9526fd29
JM
1630 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1631 return -1;
7165c5dc
JM
1632 return p2p_sd_cancel_request(wpa_s->global->p2p,
1633 (void *) (uintptr_t) req);
b22128ef
JM
1634}
1635
1636
1637void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1638 const u8 *dst, u8 dialog_token,
1639 const struct wpabuf *resp_tlvs)
1640{
3ac17eba
JM
1641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1642 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1643 resp_tlvs);
1644 return;
1645 }
9526fd29
JM
1646 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1647 return;
b22128ef
JM
1648 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1649 resp_tlvs);
1650}
1651
1652
1653void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1654{
3ac17eba
JM
1655 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1656 wpa_drv_p2p_service_update(wpa_s);
1657 return;
1658 }
9526fd29
JM
1659 if (wpa_s->global->p2p)
1660 p2p_sd_service_update(wpa_s->global->p2p);
b22128ef
JM
1661}
1662
1663
1664static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1665{
1666 dl_list_del(&bsrv->list);
1667 wpabuf_free(bsrv->query);
1668 wpabuf_free(bsrv->resp);
1669 os_free(bsrv);
1670}
1671
1672
1673static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1674{
1675 dl_list_del(&usrv->list);
1676 os_free(usrv->service);
1677 os_free(usrv);
1678}
1679
1680
1681void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1682{
1683 struct p2p_srv_bonjour *bsrv, *bn;
1684 struct p2p_srv_upnp *usrv, *un;
1685
1686 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1687 struct p2p_srv_bonjour, list)
1688 wpas_p2p_srv_bonjour_free(bsrv);
1689
1690 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1691 struct p2p_srv_upnp, list)
1692 wpas_p2p_srv_upnp_free(usrv);
1693
1694 wpas_p2p_sd_service_update(wpa_s);
1695}
1696
1697
1698int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1699 struct wpabuf *query, struct wpabuf *resp)
1700{
1701 struct p2p_srv_bonjour *bsrv;
1702
1703 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1704 if (bsrv) {
1705 wpabuf_free(query);
1706 wpabuf_free(bsrv->resp);
1707 bsrv->resp = resp;
1708 return 0;
1709 }
1710
1711 bsrv = os_zalloc(sizeof(*bsrv));
1712 if (bsrv == NULL)
1713 return -1;
1714 bsrv->query = query;
1715 bsrv->resp = resp;
1716 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1717
1718 wpas_p2p_sd_service_update(wpa_s);
1719 return 0;
1720}
1721
1722
1723int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1724 const struct wpabuf *query)
1725{
1726 struct p2p_srv_bonjour *bsrv;
1727
1728 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1729 if (bsrv == NULL)
1730 return -1;
1731 wpas_p2p_srv_bonjour_free(bsrv);
1732 wpas_p2p_sd_service_update(wpa_s);
1733 return 0;
1734}
1735
1736
1737int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1738 const char *service)
1739{
1740 struct p2p_srv_upnp *usrv;
1741
1742 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1743 return 0; /* Already listed */
1744 usrv = os_zalloc(sizeof(*usrv));
1745 if (usrv == NULL)
1746 return -1;
1747 usrv->version = version;
1748 usrv->service = os_strdup(service);
1749 if (usrv->service == NULL) {
1750 os_free(usrv);
1751 return -1;
1752 }
1753 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1754
1755 wpas_p2p_sd_service_update(wpa_s);
1756 return 0;
1757}
1758
1759
1760int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1761 const char *service)
1762{
1763 struct p2p_srv_upnp *usrv;
1764
1765 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1766 if (usrv == NULL)
1767 return -1;
1768 wpas_p2p_srv_upnp_free(usrv);
1769 wpas_p2p_sd_service_update(wpa_s);
1770 return 0;
1771}
1772
1773
1774static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
bbeee9b0
JB
1775 const u8 *peer, const char *params,
1776 unsigned int generated_pin)
b22128ef
JM
1777{
1778 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
bbeee9b0 1779 MAC2STR(peer), generated_pin, params);
b22128ef
JM
1780}
1781
1782
1783static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1784 const u8 *peer, const char *params)
1785{
1786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1787 MAC2STR(peer), params);
1788}
1789
1790
1791void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1792 const u8 *dev_addr, const u8 *pri_dev_type,
1793 const char *dev_name, u16 supp_config_methods,
c3f42784
JM
1794 u8 dev_capab, u8 group_capab, const u8 *group_id,
1795 size_t group_id_len)
b22128ef
JM
1796{
1797 struct wpa_supplicant *wpa_s = ctx;
1798 char devtype[WPS_DEV_TYPE_BUFSIZE];
c3f42784 1799 char params[300];
b22128ef 1800 u8 empty_dev_type[8];
bbeee9b0 1801 unsigned int generated_pin = 0;
c3f42784
JM
1802 struct wpa_supplicant *group = NULL;
1803
1804 if (group_id) {
1805 for (group = wpa_s->global->ifaces; group; group = group->next)
1806 {
1807 struct wpa_ssid *s = group->current_ssid;
1808 if (s != NULL &&
1809 s->mode == WPAS_MODE_P2P_GO &&
1810 group_id_len - ETH_ALEN == s->ssid_len &&
1811 os_memcmp(group_id + ETH_ALEN, s->ssid,
1812 s->ssid_len) == 0)
1813 break;
1814 }
1815 }
b22128ef
JM
1816
1817 if (pri_dev_type == NULL) {
1818 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1819 pri_dev_type = empty_dev_type;
1820 }
1821 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1822 " pri_dev_type=%s name='%s' config_methods=0x%x "
c3f42784 1823 "dev_capab=0x%x group_capab=0x%x%s%s",
b22128ef
JM
1824 MAC2STR(dev_addr),
1825 wps_dev_type_bin2str(pri_dev_type, devtype,
1826 sizeof(devtype)),
c3f42784
JM
1827 dev_name, supp_config_methods, dev_capab, group_capab,
1828 group ? " group=" : "",
1829 group ? group->ifname : "");
b22128ef
JM
1830 params[sizeof(params) - 1] = '\0';
1831
bbeee9b0
JB
1832 if (config_methods & WPS_CONFIG_DISPLAY) {
1833 generated_pin = wps_generate_pin();
1834 wpas_prov_disc_local_display(wpa_s, peer, params,
1835 generated_pin);
1836 } else if (config_methods & WPS_CONFIG_KEYPAD)
b22128ef
JM
1837 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1838 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1839 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1840 "%s", MAC2STR(peer), params);
dd8a7e05
JB
1841
1842 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1843 P2P_PROV_DISC_SUCCESS,
1844 config_methods, generated_pin);
b22128ef
JM
1845}
1846
1847
1848void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1849{
1850 struct wpa_supplicant *wpa_s = ctx;
bbeee9b0 1851 unsigned int generated_pin = 0;
0918c4bf 1852 char params[20];
bbeee9b0 1853
c1931635
JM
1854 if (wpa_s->pending_pd_before_join &&
1855 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1856 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1857 wpa_s->pending_pd_before_join = 0;
1858 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1859 "join-existing-group operation");
1860 wpas_p2p_join_start(wpa_s);
1861 return;
1862 }
1863
0918c4bf
JM
1864 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1865 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1866 os_snprintf(params, sizeof(params), " peer_go=%d",
1867 wpa_s->pending_pd_use == AUTO_PD_JOIN);
1868 else
1869 params[0] = '\0';
1870
b22128ef 1871 if (config_methods & WPS_CONFIG_DISPLAY)
0918c4bf 1872 wpas_prov_disc_local_keypad(wpa_s, peer, params);
bbeee9b0
JB
1873 else if (config_methods & WPS_CONFIG_KEYPAD) {
1874 generated_pin = wps_generate_pin();
0918c4bf
JM
1875 wpas_prov_disc_local_display(wpa_s, peer, params,
1876 generated_pin);
bbeee9b0 1877 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
0918c4bf
JM
1878 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1879 "%s", MAC2STR(peer), params);
a482883f 1880
dd8a7e05
JB
1881 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1882 P2P_PROV_DISC_SUCCESS,
1883 config_methods, generated_pin);
b22128ef
JM
1884}
1885
1886
19df9b07
JM
1887static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1888 enum p2p_prov_disc_status status)
dd8a7e05
JB
1889{
1890 struct wpa_supplicant *wpa_s = ctx;
1891
aa9bb764
JM
1892 if (wpa_s->p2p_fallback_to_go_neg) {
1893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
1894 "failed - fall back to GO Negotiation");
1895 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1896 return;
1897 }
1898
f65a239b
DG
1899 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1900 " p2p_dev_addr=" MACSTR " status=%d",
25a94f52 1901 MAC2STR(peer), status);
f65a239b 1902
dd8a7e05
JB
1903 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1904 status, 0, 0);
1905}
1906
1907
b22128ef
JM
1908static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1909 const u8 *go_dev_addr, const u8 *ssid,
1910 size_t ssid_len, int *go, u8 *group_bssid,
1911 int *force_freq, int persistent_group)
1912{
1913 struct wpa_supplicant *wpa_s = ctx;
1914 struct wpa_ssid *s;
1915 u8 cur_bssid[ETH_ALEN];
1916 int res;
ab72eb52 1917 struct wpa_supplicant *grp;
b22128ef
JM
1918
1919 if (!persistent_group) {
1920 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1921 " to join an active group", MAC2STR(sa));
108def93 1922 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
131cb37c
JM
1923 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1924 == 0 ||
1925 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
108def93
JM
1926 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1927 "authorized invitation");
1928 goto accept_inv;
1929 }
b22128ef
JM
1930 /*
1931 * Do not accept the invitation automatically; notify user and
1932 * request approval.
1933 */
1934 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1935 }
1936
ab72eb52
JM
1937 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1938 if (grp) {
1939 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1940 "running persistent group");
1941 if (*go)
1942 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1943 goto accept_inv;
1944 }
1945
b22128ef
JM
1946 if (!wpa_s->conf->persistent_reconnect)
1947 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1948
1949 for (s = wpa_s->conf->ssid; s; s = s->next) {
1950 if (s->disabled == 2 &&
1951 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1952 s->ssid_len == ssid_len &&
1953 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1954 break;
1955 }
1956
1957 if (!s) {
1958 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1959 " requested reinvocation of an unknown group",
1960 MAC2STR(sa));
1961 return P2P_SC_FAIL_UNKNOWN_GROUP;
1962 }
1963
1964 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1965 *go = 1;
1966 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1967 wpa_printf(MSG_DEBUG, "P2P: The only available "
1968 "interface is already in use - reject "
1969 "invitation");
1970 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1971 }
1972 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1973 } else if (s->mode == WPAS_MODE_P2P_GO) {
1974 *go = 1;
1975 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1976 {
1977 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1978 "interface address for the group");
1979 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1980 }
1981 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1982 ETH_ALEN);
1983 }
1984
108def93 1985accept_inv:
b22128ef
JM
1986 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1987 wpa_s->assoc_freq) {
1988 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1989 "the channel we are already using");
1990 *force_freq = wpa_s->assoc_freq;
1991 }
1992
1993 res = wpa_drv_shared_freq(wpa_s);
1994 if (res > 0) {
1995 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1996 "with the channel we are already using on a "
1997 "shared interface");
1998 *force_freq = res;
1999 }
2000
2001 return P2P_SC_SUCCESS;
2002}
2003
2004
2005static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2006 const u8 *ssid, size_t ssid_len,
2007 const u8 *go_dev_addr, u8 status,
2008 int op_freq)
2009{
2010 struct wpa_supplicant *wpa_s = ctx;
2011 struct wpa_ssid *s;
2012
2013 for (s = wpa_s->conf->ssid; s; s = s->next) {
2014 if (s->disabled == 2 &&
2015 s->ssid_len == ssid_len &&
2016 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2017 break;
2018 }
2019
2020 if (status == P2P_SC_SUCCESS) {
2021 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2022 " was accepted; op_freq=%d MHz",
2023 MAC2STR(sa), op_freq);
2024 if (s) {
2025 wpas_p2p_group_add_persistent(
2026 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2049af2b 2027 } else if (bssid) {
108def93 2028 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
b31be3a0 2029 wpa_s->p2p_wps_method, 0);
b22128ef
JM
2030 }
2031 return;
2032 }
2033
2034 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2035 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2036 " was rejected (status %u)", MAC2STR(sa), status);
2037 return;
2038 }
2039
2040 if (!s) {
2041 if (bssid) {
2042 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2043 "sa=" MACSTR " go_dev_addr=" MACSTR
2044 " bssid=" MACSTR " unknown-network",
2045 MAC2STR(sa), MAC2STR(go_dev_addr),
2046 MAC2STR(bssid));
2047 } else {
2048 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2049 "sa=" MACSTR " go_dev_addr=" MACSTR
2050 " unknown-network",
2051 MAC2STR(sa), MAC2STR(go_dev_addr));
2052 }
2053 return;
2054 }
2055
2056 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2057 " persistent=%d", MAC2STR(sa), s->id);
2058}
2059
2060
2061static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2062{
2063 struct wpa_supplicant *wpa_s = ctx;
2064 struct wpa_ssid *ssid;
2065
2066 if (bssid) {
2067 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2068 "status=%d " MACSTR,
2069 status, MAC2STR(bssid));
2070 } else {
2071 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2072 "status=%d ", status);
2073 }
5ccdf84f 2074 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
b22128ef 2075
706887fc
JM
2076 if (wpa_s->pending_invite_ssid_id == -1)
2077 return; /* Invitation to active group */
2078
b22128ef
JM
2079 if (status != P2P_SC_SUCCESS) {
2080 wpas_p2p_remove_pending_group_interface(wpa_s);
2081 return;
2082 }
2083
2084 ssid = wpa_config_get_network(wpa_s->conf,
2085 wpa_s->pending_invite_ssid_id);
2086 if (ssid == NULL) {
2087 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2088 "data matching with invitation");
2089 return;
2090 }
2091
2092 wpas_p2p_group_add_persistent(wpa_s, ssid,
2093 ssid->mode == WPAS_MODE_P2P_GO, 0);
2094}
2095
2096
6f3bc72b
JM
2097static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2098 unsigned int freq)
2099{
2100 unsigned int i;
2101
2102 if (global->p2p_disallow_freq == NULL)
2103 return 0;
2104
2105 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2106 if (freq >= global->p2p_disallow_freq[i].min &&
2107 freq <= global->p2p_disallow_freq[i].max)
2108 return 1;
2109 }
2110
2111 return 0;
2112}
2113
2114
2115static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2116{
2117 reg->channel[reg->channels] = chan;
2118 reg->channels++;
2119}
2120
2121
ac8d1011
JM
2122static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2123 struct p2p_channels *chan)
2124{
2125 int i, cla = 0;
2126
2127 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2128 "band");
2129
2130 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2131 chan->reg_class[cla].reg_class = 81;
6f3bc72b
JM
2132 chan->reg_class[cla].channels = 0;
2133 for (i = 0; i < 11; i++) {
2134 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2135 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2136 }
2137 if (chan->reg_class[cla].channels)
2138 cla++;
ac8d1011
JM
2139
2140 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2141 "band");
2142
2143 /* Operating class 115 - 5 GHz, channels 36-48 */
2144 chan->reg_class[cla].reg_class = 115;
6f3bc72b
JM
2145 chan->reg_class[cla].channels = 0;
2146 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2147 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2148 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2149 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2150 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2151 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2152 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2153 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2154 if (chan->reg_class[cla].channels)
2155 cla++;
ac8d1011
JM
2156
2157 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2158 "band");
2159
2160 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2161 chan->reg_class[cla].reg_class = 124;
6f3bc72b
JM
2162 chan->reg_class[cla].channels = 0;
2163 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2164 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2165 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2166 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2167 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2168 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2169 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2170 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2171 if (chan->reg_class[cla].channels)
2172 cla++;
ac8d1011
JM
2173
2174 chan->reg_classes = cla;
2175 return 0;
2176}
2177
2178
2179static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2180 u16 num_modes,
2181 enum hostapd_hw_mode mode)
2182{
2183 u16 i;
2184
2185 for (i = 0; i < num_modes; i++) {
2186 if (modes[i].mode == mode)
2187 return &modes[i];
2188 }
2189
2190 return NULL;
2191}
2192
2193
6f3bc72b
JM
2194static int has_channel(struct wpa_global *global,
2195 struct hostapd_hw_modes *mode, u8 chan, int *flags)
ac8d1011
JM
2196{
2197 int i;
6f3bc72b
JM
2198 unsigned int freq;
2199
2200 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2201 chan * 5;
2202 if (wpas_p2p_disallowed_freq(global, freq))
2203 return 0;
ac8d1011
JM
2204
2205 for (i = 0; i < mode->num_channels; i++) {
2206 if (mode->channels[i].chan == chan) {
51222429
JM
2207 if (flags)
2208 *flags = mode->channels[i].flag;
ac8d1011
JM
2209 return !(mode->channels[i].flag &
2210 (HOSTAPD_CHAN_DISABLED |
2211 HOSTAPD_CHAN_PASSIVE_SCAN |
2212 HOSTAPD_CHAN_NO_IBSS |
2213 HOSTAPD_CHAN_RADAR));
2214 }
2215 }
2216
2217 return 0;
2218}
2219
2220
2221struct p2p_oper_class_map {
2222 enum hostapd_hw_mode mode;
2223 u8 op_class;
2224 u8 min_chan;
2225 u8 max_chan;
2226 u8 inc;
51222429 2227 enum { BW20, BW40PLUS, BW40MINUS } bw;
ac8d1011
JM
2228};
2229
b22128ef 2230static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
ac8d1011 2231 struct p2p_channels *chan)
b22128ef 2232{
6bf731e8 2233 struct hostapd_hw_modes *mode;
ac8d1011
JM
2234 int cla, op;
2235 struct p2p_oper_class_map op_class[] = {
51222429
JM
2236 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2237 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2238#if 0 /* Do not enable HT40 on 2 GHz for now */
2239 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2240 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
ac8d1011 2241#endif
51222429
JM
2242 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2243 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2244 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2245 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2246 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2247 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2248 { -1, 0, 0, 0, 0, BW20 }
ac8d1011 2249 };
b22128ef 2250
6bf731e8 2251 if (wpa_s->hw.modes == NULL) {
b22128ef
JM
2252 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2253 "of all supported channels; assume dualband "
2254 "support");
ac8d1011 2255 return wpas_p2p_default_channels(wpa_s, chan);
b22128ef
JM
2256 }
2257
2258 cla = 0;
2259
ac8d1011
JM
2260 for (op = 0; op_class[op].op_class; op++) {
2261 struct p2p_oper_class_map *o = &op_class[op];
2262 u8 ch;
2263 struct p2p_reg_class *reg = NULL;
b22128ef 2264
6bf731e8 2265 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
ac8d1011
JM
2266 if (mode == NULL)
2267 continue;
2268 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
51222429 2269 int flag;
6f3bc72b 2270 if (!has_channel(wpa_s->global, mode, ch, &flag))
51222429
JM
2271 continue;
2272 if (o->bw == BW40MINUS &&
2273 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
6f3bc72b 2274 !has_channel(wpa_s->global, mode, ch - 4, NULL)))
51222429
JM
2275 continue;
2276 if (o->bw == BW40PLUS &&
2277 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
6f3bc72b 2278 !has_channel(wpa_s->global, mode, ch + 4, NULL)))
ac8d1011
JM
2279 continue;
2280 if (reg == NULL) {
2281 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2282 "class %u", o->op_class);
2283 reg = &chan->reg_class[cla];
2284 cla++;
2285 reg->reg_class = o->op_class;
2286 }
2287 reg->channel[reg->channels] = ch;
2288 reg->channels++;
2289 }
2290 if (reg) {
2291 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2292 reg->channel, reg->channels);
2293 }
b22128ef
JM
2294 }
2295
ac8d1011 2296 chan->reg_classes = cla;
b22128ef 2297
b22128ef
JM
2298 return 0;
2299}
2300
2301
2302static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2303 size_t buf_len)
2304{
2305 struct wpa_supplicant *wpa_s = ctx;
2306
2307 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2308 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2309 break;
2310 }
2311 if (wpa_s == NULL)
2312 return -1;
2313
2314 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2315}
2316
2317
b1aebbc4
JM
2318static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2319{
2320 struct wpa_supplicant *wpa_s = ctx;
2321
2322 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2323 struct wpa_ssid *ssid = wpa_s->current_ssid;
2324 if (ssid == NULL)
2325 continue;
2326 if (ssid->mode != WPAS_MODE_INFRA)
2327 continue;
d9a0f666
PN
2328 if (wpa_s->wpa_state != WPA_COMPLETED &&
2329 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
b1aebbc4
JM
2330 continue;
2331 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2332 return 1;
2333 }
2334
2335 return 0;
2336}
2337
2338
b22128ef
JM
2339/**
2340 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2341 * @global: Pointer to global data from wpa_supplicant_init()
2342 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2343 * Returns: 0 on success, -1 on failure
2344 */
2345int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2346{
2347 struct p2p_config p2p;
2348 unsigned int r;
f95cac27 2349 int i;
b22128ef
JM
2350
2351 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2352 return 0;
2353
b22128ef
JM
2354 if (global->p2p)
2355 return 0;
2356
3ac17eba
JM
2357 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2358 struct p2p_params params;
2359
2360 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2361 os_memset(&params, 0, sizeof(params));
2362 params.dev_name = wpa_s->conf->device_name;
2f646b6e
JB
2363 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2364 WPS_DEV_TYPE_LEN);
2365 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2366 os_memcpy(params.sec_dev_type,
2367 wpa_s->conf->sec_device_type,
2368 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2369
3ac17eba
JM
2370 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2371 return -1;
2372
2373 return 0;
2374 }
2375
b22128ef
JM
2376 os_memset(&p2p, 0, sizeof(p2p));
2377 p2p.msg_ctx = wpa_s;
2378 p2p.cb_ctx = wpa_s;
2379 p2p.p2p_scan = wpas_p2p_scan;
2380 p2p.send_action = wpas_send_action;
2381 p2p.send_action_done = wpas_send_action_done;
2382 p2p.go_neg_completed = wpas_go_neg_completed;
2383 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2384 p2p.dev_found = wpas_dev_found;
56eeb8f2 2385 p2p.dev_lost = wpas_dev_lost;
b22128ef
JM
2386 p2p.start_listen = wpas_start_listen;
2387 p2p.stop_listen = wpas_stop_listen;
2388 p2p.send_probe_resp = wpas_send_probe_resp;
2389 p2p.sd_request = wpas_sd_request;
2390 p2p.sd_response = wpas_sd_response;
2391 p2p.prov_disc_req = wpas_prov_disc_req;
2392 p2p.prov_disc_resp = wpas_prov_disc_resp;
dd8a7e05 2393 p2p.prov_disc_fail = wpas_prov_disc_fail;
b22128ef
JM
2394 p2p.invitation_process = wpas_invitation_process;
2395 p2p.invitation_received = wpas_invitation_received;
2396 p2p.invitation_result = wpas_invitation_result;
2397 p2p.get_noa = wpas_get_noa;
b1aebbc4 2398 p2p.go_connected = wpas_go_connected;
b22128ef
JM
2399
2400 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
d7e70476 2401 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
b22128ef 2402 p2p.dev_name = wpa_s->conf->device_name;
b6e01800
JM
2403 p2p.manufacturer = wpa_s->conf->manufacturer;
2404 p2p.model_name = wpa_s->conf->model_name;
2405 p2p.model_number = wpa_s->conf->model_number;
2406 p2p.serial_number = wpa_s->conf->serial_number;
2407 if (wpa_s->wps) {
2408 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2409 p2p.config_methods = wpa_s->wps->config_methods;
2410 }
b22128ef
JM
2411
2412 if (wpa_s->conf->p2p_listen_reg_class &&
2413 wpa_s->conf->p2p_listen_channel) {
2414 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2415 p2p.channel = wpa_s->conf->p2p_listen_channel;
2416 } else {
2417 p2p.reg_class = 81;
2418 /*
2419 * Pick one of the social channels randomly as the listen
2420 * channel.
2421 */
2422 os_get_random((u8 *) &r, sizeof(r));
2423 p2p.channel = 1 + (r % 3) * 5;
2424 }
7cfc4ac3 2425 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
b22128ef
JM
2426
2427 if (wpa_s->conf->p2p_oper_reg_class &&
2428 wpa_s->conf->p2p_oper_channel) {
2429 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2430 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
2431 p2p.cfg_op_channel = 1;
2432 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2433 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2434
b22128ef
JM
2435 } else {
2436 p2p.op_reg_class = 81;
2437 /*
7cfc4ac3
AGS
2438 * Use random operation channel from (1, 6, 11) if no other
2439 * preference is indicated.
b22128ef 2440 */
b7412dab 2441 os_get_random((u8 *) &r, sizeof(r));
7cfc4ac3
AGS
2442 p2p.op_channel = 1 + (r % 3) * 5;
2443 p2p.cfg_op_channel = 0;
2444 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2445 "%d:%d", p2p.op_reg_class, p2p.op_channel);
b22128ef 2446 }
b22128ef
JM
2447 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2448 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2449 p2p.country[2] = 0x04;
2450 } else
aaca6505 2451 os_memcpy(p2p.country, "XX\x04", 3);
b22128ef 2452
ac8d1011 2453 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
b22128ef
JM
2454 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2455 "channel list");
2456 return -1;
2457 }
2458
2f646b6e
JB
2459 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2460 WPS_DEV_TYPE_LEN);
b22128ef 2461
2f646b6e
JB
2462 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2463 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2464 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
b22128ef
JM
2465
2466 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2467 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2468
de979d8f 2469 p2p.max_peers = 100;
b22128ef
JM
2470
2471 if (wpa_s->conf->p2p_ssid_postfix) {
2472 p2p.ssid_postfix_len =
2473 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2474 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2475 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2476 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2477 p2p.ssid_postfix_len);
2478 }
2479
0f66abd2
SS
2480 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2481
b22128ef
JM
2482 global->p2p = p2p_init(&p2p);
2483 if (global->p2p == NULL)
2484 return -1;
ab28911d 2485 global->p2p_init_wpa_s = wpa_s;
b22128ef 2486
f95cac27
JMB
2487 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2488 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2489 continue;
2490 p2p_add_wps_vendor_extension(
2491 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2492 }
2493
b22128ef
JM
2494 return 0;
2495}
2496
2497
2498/**
2499 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2500 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2501 *
2502 * This function deinitialize per-interface P2P data.
2503 */
2504void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2505{
2506 if (wpa_s->driver && wpa_s->drv_priv)
2507 wpa_drv_probe_req_report(wpa_s, 0);
ec437d9e
JJ
2508
2509 if (wpa_s->go_params) {
2510 /* Clear any stored provisioning info */
2511 p2p_clear_provisioning_info(
2512 wpa_s->global->p2p,
10531d21 2513 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
2514 }
2515
b22128ef
JM
2516 os_free(wpa_s->go_params);
2517 wpa_s->go_params = NULL;
b22128ef 2518 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ef922c4a 2519 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
f63b8542 2520 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
b22128ef
JM
2521 wpa_s->p2p_long_listen = 0;
2522 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3071e181 2523 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
b22128ef
JM
2524 wpas_p2p_remove_pending_group_interface(wpa_s);
2525
2526 /* TODO: remove group interface from the driver if this wpa_s instance
2527 * is on top of a P2P group interface */
2528}
2529
2530
2531/**
2532 * wpas_p2p_deinit_global - Deinitialize global P2P module
2533 * @global: Pointer to global data from wpa_supplicant_init()
2534 *
2535 * This function deinitializes the global (per device) P2P module.
2536 */
2537void wpas_p2p_deinit_global(struct wpa_global *global)
2538{
2539 struct wpa_supplicant *wpa_s, *tmp;
b22128ef
JM
2540
2541 if (global->p2p == NULL)
2542 return;
2543
2544 /* Remove remaining P2P group interfaces */
2545 wpa_s = global->ifaces;
2a43101e
JM
2546 if (wpa_s)
2547 wpas_p2p_service_flush(wpa_s);
b22128ef
JM
2548 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2549 wpa_s = wpa_s->next;
2550 while (wpa_s) {
b22128ef
JM
2551 tmp = global->ifaces;
2552 while (tmp &&
2553 (tmp == wpa_s ||
2554 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2555 tmp = tmp->next;
2556 }
2557 if (tmp == NULL)
2558 break;
103b8f4d
NS
2559 /* Disconnect from the P2P group and deinit the interface */
2560 wpas_p2p_disconnect(tmp);
b22128ef
JM
2561 }
2562
743ef799
JM
2563 /*
2564 * Deinit GO data on any possibly remaining interface (if main
2565 * interface is used as GO).
2566 */
2567 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2568 if (wpa_s->ap_iface)
2569 wpas_p2p_group_deinit(wpa_s);
2570 }
2571
b22128ef
JM
2572 p2p_deinit(global->p2p);
2573 global->p2p = NULL;
ab28911d 2574 global->p2p_init_wpa_s = NULL;
b22128ef
JM
2575}
2576
2577
2578static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2579{
971e357f
JM
2580 if (wpa_s->drv_flags &
2581 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2582 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
b22128ef
JM
2583 return 1; /* P2P group requires a new interface in every case
2584 */
2585 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2586 return 0; /* driver does not support concurrent operations */
2587 if (wpa_s->global->ifaces->next)
2588 return 1; /* more that one interface already in use */
2589 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2590 return 1; /* this interface is already in use */
2591 return 0;
2592}
2593
2594
2595static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2596 const u8 *peer_addr,
2597 enum p2p_wps_method wps_method,
2598 int go_intent, const u8 *own_interface_addr,
23c84252
JM
2599 unsigned int force_freq, int persistent_group,
2600 struct wpa_ssid *ssid)
b22128ef 2601{
acc247b2
JM
2602 if (persistent_group && wpa_s->conf->persistent_reconnect)
2603 persistent_group = 2;
2604
3ac17eba
JM
2605 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2606 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2607 go_intent, own_interface_addr,
2608 force_freq, persistent_group);
2609 }
2610
b22128ef
JM
2611 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2612 go_intent, own_interface_addr, force_freq,
23c84252 2613 persistent_group, ssid ? ssid->ssid : NULL,
3bc462cb
JM
2614 ssid ? ssid->ssid_len : 0,
2615 wpa_s->p2p_pd_before_go_neg);
b22128ef
JM
2616}
2617
2618
2619static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2620 const u8 *peer_addr,
2621 enum p2p_wps_method wps_method,
2622 int go_intent, const u8 *own_interface_addr,
23c84252
JM
2623 unsigned int force_freq, int persistent_group,
2624 struct wpa_ssid *ssid)
b22128ef 2625{
acc247b2
JM
2626 if (persistent_group && wpa_s->conf->persistent_reconnect)
2627 persistent_group = 2;
2628
3ac17eba
JM
2629 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2630 return -1;
2631
b22128ef
JM
2632 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2633 go_intent, own_interface_addr, force_freq,
23c84252
JM
2634 persistent_group, ssid ? ssid->ssid : NULL,
2635 ssid ? ssid->ssid_len : 0);
b22128ef
JM
2636}
2637
2638
9b1ab931
JM
2639static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2640{
2641 wpa_s->p2p_join_scan_count++;
2642 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2643 wpa_s->p2p_join_scan_count);
2644 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2645 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2646 " for join operationg - stop join attempt",
2647 MAC2STR(wpa_s->pending_join_iface_addr));
2648 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
0918c4bf
JM
2649 if (wpa_s->p2p_auto_pd) {
2650 wpa_s->p2p_auto_pd = 0;
2651 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2652 " p2p_dev_addr=" MACSTR " status=N/A",
2653 MAC2STR(wpa_s->pending_join_dev_addr));
2654 return;
2655 }
9b1ab931
JM
2656 wpa_msg(wpa_s->parent, MSG_INFO,
2657 P2P_EVENT_GROUP_FORMATION_FAILURE);
2658 }
2659}
2660
2661
f63b8542
JM
2662static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2663{
2664 struct wpa_supplicant *wpa_s = eloop_ctx;
2665 if (!wpa_s->pending_pd_before_join)
2666 return;
2667 /*
2668 * Provision Discovery Response may have been lost - try to connect
2669 * anyway since we do not need any information from this PD.
2670 */
2671 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2672 "try to connect anyway");
2673 wpas_p2p_join_start(wpa_s);
2674}
2675
2676
4b156206
JM
2677static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2678{
2679 struct wpa_supplicant *iface;
2680 int shared_freq;
2681 u8 bssid[ETH_ALEN];
2682
2683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2684 return 0;
2685
2686 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2687 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2688 continue;
2689 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2690 continue;
86ae2e8a
JM
2691 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2692 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2693 shared_freq = iface->current_ssid->frequency;
2694 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2695 shared_freq = iface->assoc_freq;
2696 else
2697 shared_freq = 0;
2698
2699 if (shared_freq && freq != shared_freq) {
2700 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2701 "connected on %d MHz - new connection on "
2702 "%d MHz", iface->ifname, shared_freq, freq);
2703 return 1;
4b156206
JM
2704 }
2705 }
2706
2707 shared_freq = wpa_drv_shared_freq(wpa_s);
2708 if (shared_freq > 0 && shared_freq != freq) {
2709 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2710 "virtual interface connected on %d MHz - new "
2711 "connection on %d MHz", shared_freq, freq);
2712 return 1;
2713 }
2714
2715 return 0;
2716}
2717
2718
b31be3a0
JM
2719static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2720 const u8 *peer_dev_addr)
2721{
2722 struct wpa_bss *bss;
2723 int updated;
2724
2725 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2726 if (bss == NULL)
aa9bb764 2727 return -1;
b31be3a0
JM
2728 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2729 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2730 "last scan");
2731 return 0;
2732 }
2733
2734 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2735 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2736 "%ld.%06ld (%supdated in last scan)",
2737 bss->last_update.sec, bss->last_update.usec,
2738 updated ? "": "not ");
2739
2740 return updated;
2741}
2742
2743
ef922c4a
JM
2744static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2745 struct wpa_scan_results *scan_res)
b22128ef
JM
2746{
2747 struct wpa_bss *bss;
f8d0131a 2748 int freq;
54960629 2749 u8 iface_addr[ETH_ALEN];
b22128ef 2750
ef922c4a 2751 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef 2752
ef922c4a
JM
2753 if (wpa_s->global->p2p_disabled)
2754 return;
b22128ef 2755
b31be3a0
JM
2756 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2757 scan_res ? (int) scan_res->num : -1,
2758 wpa_s->p2p_auto_join ? "auto_" : "");
ef922c4a
JM
2759
2760 if (scan_res)
2761 wpas_p2p_scan_res_handler(wpa_s, scan_res);
b22128ef 2762
0918c4bf
JM
2763 if (wpa_s->p2p_auto_pd) {
2764 int join = wpas_p2p_peer_go(wpa_s,
2765 wpa_s->pending_join_dev_addr);
aa9bb764
JM
2766 if (join < 0)
2767 join = 0;
0918c4bf
JM
2768 wpa_s->p2p_auto_pd = 0;
2769 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2770 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2771 MAC2STR(wpa_s->pending_join_dev_addr), join);
2772 if (p2p_prov_disc_req(wpa_s->global->p2p,
2773 wpa_s->pending_join_dev_addr,
2774 wpa_s->pending_pd_config_methods, join,
2775 0) < 0) {
2776 wpa_s->p2p_auto_pd = 0;
2777 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2778 " p2p_dev_addr=" MACSTR " status=N/A",
2779 MAC2STR(wpa_s->pending_join_dev_addr));
2780 }
2781 return;
2782 }
2783
b31be3a0 2784 if (wpa_s->p2p_auto_join) {
aa9bb764
JM
2785 int join = wpas_p2p_peer_go(wpa_s,
2786 wpa_s->pending_join_dev_addr);
2787 if (join < 0) {
b31be3a0
JM
2788 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2789 "running a GO -> use GO Negotiation");
2790 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2791 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2792 wpa_s->p2p_persistent_group, 0, 0, 0,
2793 wpa_s->p2p_go_intent,
23c84252 2794 wpa_s->p2p_connect_freq,
3bc462cb
JM
2795 wpa_s->p2p_persistent_id,
2796 wpa_s->p2p_pd_before_go_neg);
b31be3a0
JM
2797 return;
2798 }
2799
aa9bb764
JM
2800 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2801 "try to join the group", join ? "" :
2802 " in older scan");
2803 if (!join)
2804 wpa_s->p2p_fallback_to_go_neg = 1;
b31be3a0
JM
2805 }
2806
f8d0131a
JM
2807 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2808 wpa_s->pending_join_iface_addr);
54960629
AL
2809 if (freq < 0 &&
2810 p2p_get_interface_addr(wpa_s->global->p2p,
2811 wpa_s->pending_join_dev_addr,
2812 iface_addr) == 0 &&
2813 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2814 {
2815 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2816 "address for join from " MACSTR " to " MACSTR
2817 " based on newly discovered P2P peer entry",
2818 MAC2STR(wpa_s->pending_join_iface_addr),
2819 MAC2STR(iface_addr));
2820 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2821 ETH_ALEN);
2822
2823 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2824 wpa_s->pending_join_iface_addr);
2825 }
f8d0131a
JM
2826 if (freq >= 0) {
2827 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2828 "from P2P peer table: %d MHz", freq);
2829 }
ef922c4a 2830 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
b22128ef 2831 if (bss) {
f8d0131a
JM
2832 freq = bss->freq;
2833 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2834 "from BSS table: %d MHz", freq);
2835 }
2836 if (freq > 0) {
b22128ef
JM
2837 u16 method;
2838
4b156206
JM
2839 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2840 wpa_msg(wpa_s->parent, MSG_INFO,
2841 P2P_EVENT_GROUP_FORMATION_FAILURE
2842 "reason=FREQ_CONFLICT");
2843 return;
2844 }
2845
b22128ef
JM
2846 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2847 "prior to joining an existing group (GO " MACSTR
2848 " freq=%u MHz)",
f8d0131a 2849 MAC2STR(wpa_s->pending_join_dev_addr), freq);
b22128ef
JM
2850 wpa_s->pending_pd_before_join = 1;
2851
ef922c4a 2852 switch (wpa_s->pending_join_wps_method) {
b22128ef
JM
2853 case WPS_PIN_DISPLAY:
2854 method = WPS_CONFIG_KEYPAD;
2855 break;
2856 case WPS_PIN_KEYPAD:
2857 method = WPS_CONFIG_DISPLAY;
2858 break;
2859 case WPS_PBC:
2860 method = WPS_CONFIG_PUSHBUTTON;
2861 break;
2862 default:
2863 method = 0;
2864 break;
2865 }
2866
ec437d9e
JJ
2867 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2868 wpa_s->pending_join_dev_addr) ==
2869 method)) {
2870 /*
2871 * We have already performed provision discovery for
2872 * joining the group. Proceed directly to join
2873 * operation without duplicated provision discovery. */
e3a0706b 2874 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
ec437d9e
JJ
2875 "with " MACSTR " already done - proceed to "
2876 "join",
2877 MAC2STR(wpa_s->pending_join_dev_addr));
2878 wpa_s->pending_pd_before_join = 0;
2879 goto start;
2880 }
2881
ef922c4a 2882 if (p2p_prov_disc_req(wpa_s->global->p2p,
1ef2f7ff
JM
2883 wpa_s->pending_join_dev_addr, method, 1,
2884 freq) < 0) {
b22128ef
JM
2885 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2886 "Discovery Request before joining an "
2887 "existing group");
2888 wpa_s->pending_pd_before_join = 0;
2889 goto start;
2890 }
2891
2892 /*
2893 * Actual join operation will be started from the Action frame
f63b8542
JM
2894 * TX status callback (if no ACK is received) or when the
2895 * Provision Discovery Response is received. Use a short
2896 * timeout as a backup mechanism should the Provision Discovery
2897 * Response be lost for any reason.
b22128ef 2898 */
f63b8542
JM
2899 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2900 NULL);
2901 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2902 wpa_s, NULL);
ef922c4a 2903 return;
b22128ef
JM
2904 }
2905
9b1ab931
JM
2906 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2907 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2908 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2909 wpas_p2p_check_join_scan_limit(wpa_s);
2910 return;
b22128ef
JM
2911
2912start:
2913 /* Start join operation immediately */
ef922c4a
JM
2914 wpas_p2p_join_start(wpa_s);
2915}
2916
2917
2918static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2919{
2920 struct wpa_supplicant *wpa_s = eloop_ctx;
2921 int ret;
2922 struct wpa_driver_scan_params params;
2923 struct wpabuf *wps_ie, *ies;
206e1f42 2924 size_t ielen;
ef922c4a
JM
2925
2926 os_memset(&params, 0, sizeof(params));
2927
2928 /* P2P Wildcard SSID */
2929 params.num_ssids = 1;
2930 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2931 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2932
2933 wpa_s->wps->dev.p2p = 1;
360182ed
JM
2934 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
2935 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
2936 NULL);
ef922c4a
JM
2937 if (wps_ie == NULL) {
2938 wpas_p2p_scan_res_join(wpa_s, NULL);
2939 return;
2940 }
2941
206e1f42
JM
2942 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2943 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
ef922c4a
JM
2944 if (ies == NULL) {
2945 wpabuf_free(wps_ie);
2946 wpas_p2p_scan_res_join(wpa_s, NULL);
2947 return;
2948 }
2949 wpabuf_put_buf(ies, wps_ie);
2950 wpabuf_free(wps_ie);
2951
6d92fa6e 2952 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
ef922c4a 2953
47185fc7 2954 params.p2p_probe = 1;
ef922c4a
JM
2955 params.extra_ies = wpabuf_head(ies);
2956 params.extra_ies_len = wpabuf_len(ies);
2957
2958 /*
2959 * Run a scan to update BSS table and start Provision Discovery once
2960 * the new scan results become available.
2961 */
17fbb751 2962 ret = wpa_drv_scan(wpa_s, &params);
205e6474
JM
2963 if (!ret)
2964 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
ef922c4a
JM
2965
2966 wpabuf_free(ies);
2967
2968 if (ret) {
2969 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2970 "try again later");
2971 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2972 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
9b1ab931 2973 wpas_p2p_check_join_scan_limit(wpa_s);
ef922c4a
JM
2974 }
2975}
2976
2977
2978static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
b31be3a0
JM
2979 const u8 *dev_addr, enum p2p_wps_method wps_method,
2980 int auto_join)
ef922c4a
JM
2981{
2982 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
b31be3a0
JM
2983 MACSTR " dev " MACSTR ")%s",
2984 MAC2STR(iface_addr), MAC2STR(dev_addr),
2985 auto_join ? " (auto_join)" : "");
ef922c4a 2986
0918c4bf 2987 wpa_s->p2p_auto_pd = 0;
b31be3a0 2988 wpa_s->p2p_auto_join = !!auto_join;
ef922c4a
JM
2989 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2990 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2991 wpa_s->pending_join_wps_method = wps_method;
2992
2993 /* Make sure we are not running find during connection establishment */
2994 wpas_p2p_stop_find(wpa_s);
2995
9b1ab931 2996 wpa_s->p2p_join_scan_count = 0;
ef922c4a
JM
2997 wpas_p2p_join_scan(wpa_s, NULL);
2998 return 0;
b22128ef
JM
2999}
3000
3001
3002static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3003{
3004 struct wpa_supplicant *group;
3005 struct p2p_go_neg_results res;
8e64f258 3006 struct wpa_bss *bss;
b22128ef 3007
f63b8542 3008 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
b22128ef
JM
3009 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3010 if (group == NULL)
3011 return -1;
3c5126a4 3012 if (group != wpa_s) {
b22128ef
JM
3013 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3014 sizeof(group->p2p_pin));
3c5126a4
JM
3015 group->p2p_wps_method = wpa_s->p2p_wps_method;
3016 }
b22128ef
JM
3017
3018 group->p2p_in_provisioning = 1;
aa9bb764 3019 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
b22128ef
JM
3020
3021 os_memset(&res, 0, sizeof(res));
3022 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3023 ETH_ALEN);
3024 res.wps_method = wpa_s->pending_join_wps_method;
8e64f258
JM
3025 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3026 if (bss) {
3027 res.freq = bss->freq;
3028 res.ssid_len = bss->ssid_len;
3029 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3030 }
3031
e91829f9
JM
3032 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3033 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3034 "starting client");
3035 wpa_drv_cancel_remain_on_channel(wpa_s);
3036 wpa_s->off_channel_freq = 0;
3037 wpa_s->roc_waiting_drv_freq = 0;
3038 }
b22128ef
JM
3039 wpas_start_wps_enrollee(group, &res);
3040
3094d483
JM
3041 /*
3042 * Allow a longer timeout for join-a-running-group than normal 15
3043 * second group formation timeout since the GO may not have authorized
3044 * our connection yet.
3045 */
3046 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3047 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3048 wpa_s, NULL);
3049
b22128ef
JM
3050 return 0;
3051}
3052
3053
3054/**
3055 * wpas_p2p_connect - Request P2P Group Formation to be started
3056 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3057 * @peer_addr: Address of the peer P2P Device
3058 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3059 * @persistent_group: Whether to create a persistent group
b31be3a0 3060 * @auto_join: Whether to select join vs. GO Negotiation automatically
b22128ef
JM
3061 * @join: Whether to join an existing group (as a client) instead of starting
3062 * Group Owner negotiation; @peer_addr is BSSID in that case
3063 * @auth: Whether to only authorize the connection instead of doing that and
3064 * initiating Group Owner negotiation
3065 * @go_intent: GO Intent or -1 to use default
3066 * @freq: Frequency for the group or 0 for auto-selection
23c84252
JM
3067 * @persistent_id: Persistent group credentials to use for forcing GO
3068 * parameters or -1 to generate new values (SSID/passphrase)
3bc462cb
JM
3069 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3070 * interoperability workaround when initiating group formation
d054a462
JM
3071 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3072 * failure, -2 on failure due to channel not currently available,
3073 * -3 if forced channel is not supported
b22128ef
JM
3074 */
3075int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3076 const char *pin, enum p2p_wps_method wps_method,
b31be3a0 3077 int persistent_group, int auto_join, int join, int auth,
3bc462cb 3078 int go_intent, int freq, int persistent_id, int pd)
b22128ef 3079{
d054a462 3080 int force_freq = 0, oper_freq = 0;
b22128ef
JM
3081 u8 bssid[ETH_ALEN];
3082 int ret = 0;
3083 enum wpa_driver_if_type iftype;
6cad95db 3084 const u8 *if_addr;
23c84252 3085 struct wpa_ssid *ssid = NULL;
b22128ef 3086
9526fd29
JM
3087 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3088 return -1;
3089
23c84252
JM
3090 if (persistent_id >= 0) {
3091 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3092 if (ssid == NULL || ssid->disabled != 2 ||
3093 ssid->mode != WPAS_MODE_P2P_GO)
3094 return -1;
3095 }
3096
b22128ef
JM
3097 if (go_intent < 0)
3098 go_intent = wpa_s->conf->p2p_go_intent;
3099
3100 if (!auth)
3101 wpa_s->p2p_long_listen = 0;
3102
3c5126a4 3103 wpa_s->p2p_wps_method = wps_method;
b31be3a0 3104 wpa_s->p2p_persistent_group = !!persistent_group;
23c84252 3105 wpa_s->p2p_persistent_id = persistent_id;
b31be3a0
JM
3106 wpa_s->p2p_go_intent = go_intent;
3107 wpa_s->p2p_connect_freq = freq;
aa9bb764 3108 wpa_s->p2p_fallback_to_go_neg = 0;
3bc462cb 3109 wpa_s->p2p_pd_before_go_neg = !!pd;
3c5126a4 3110
b22128ef
JM
3111 if (pin)
3112 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3113 else if (wps_method == WPS_PIN_DISPLAY) {
3114 ret = wps_generate_pin();
3115 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3116 ret);
3117 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3118 wpa_s->p2p_pin);
3119 } else
3120 wpa_s->p2p_pin[0] = '\0';
3121
b31be3a0 3122 if (join || auto_join) {
4147a2cc 3123 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
108def93
JM
3124 if (auth) {
3125 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3126 "connect a running group from " MACSTR,
3127 MAC2STR(peer_addr));
3128 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3129 return ret;
3130 }
4147a2cc 3131 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
b22128ef 3132 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4147a2cc 3133 iface_addr) < 0) {
b22128ef 3134 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4147a2cc
JM
3135 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3136 dev_addr);
3137 }
b31be3a0
JM
3138 if (auto_join) {
3139 os_get_time(&wpa_s->p2p_auto_started);
3140 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3141 "%ld.%06ld",
3142 wpa_s->p2p_auto_started.sec,
3143 wpa_s->p2p_auto_started.usec);
3144 }
3145 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3146 auto_join) < 0)
b22128ef
JM
3147 return -1;
3148 return ret;
3149 }
3150
d054a462
JM
3151 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3152 wpa_s->assoc_freq)
3153 oper_freq = wpa_s->assoc_freq;
b22128ef 3154 else {
d054a462
JM
3155 oper_freq = wpa_drv_shared_freq(wpa_s);
3156 if (oper_freq < 0)
3157 oper_freq = 0;
b22128ef
JM
3158 }
3159
d054a462
JM
3160 if (freq > 0) {
3161 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3162 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3163 "(%u MHz) is not supported for P2P uses",
3164 freq);
3165 return -3;
3166 }
3167
3168 if (oper_freq > 0 && freq != oper_freq &&
3169 !(wpa_s->drv_flags &
3170 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3171 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3172 "on %u MHz while connected on another "
3173 "channel (%u MHz)", freq, oper_freq);
3174 return -2;
3175 }
3176 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3177 "requested channel (%u MHz)", freq);
3178 force_freq = freq;
3179 } else if (oper_freq > 0 &&
3180 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3181 if (!(wpa_s->drv_flags &
3182 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3183 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3184 "while connected on non-P2P supported "
3185 "channel (%u MHz)", oper_freq);
3186 return -2;
3187 }
3188 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3189 "(%u MHz) not available for P2P - try to use "
3190 "another channel", oper_freq);
3191 force_freq = 0;
3192 } else if (oper_freq > 0) {
b22128ef
JM
3193 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3194 "channel we are already using (%u MHz) on another "
d054a462
JM
3195 "interface", oper_freq);
3196 force_freq = oper_freq;
b22128ef
JM
3197 }
3198
3199 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3200
6cad95db
JB
3201 if (wpa_s->create_p2p_iface) {
3202 /* Prepare to add a new interface for the group */
3203 iftype = WPA_IF_P2P_GROUP;
3204 if (go_intent == 15)
3205 iftype = WPA_IF_P2P_GO;
3206 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3207 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3208 "interface for the group");
b22128ef 3209 return -1;
6cad95db 3210 }
b22128ef 3211
6cad95db
JB
3212 if_addr = wpa_s->pending_interface_addr;
3213 } else
3214 if_addr = wpa_s->own_addr;
b22128ef
JM
3215
3216 if (auth) {
3217 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6cad95db 3218 go_intent, if_addr,
23c84252
JM
3219 force_freq, persistent_group, ssid) <
3220 0)
b22128ef
JM
3221 return -1;
3222 return ret;
3223 }
6cad95db
JB
3224
3225 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3226 go_intent, if_addr, force_freq,
23c84252 3227 persistent_group, ssid) < 0) {
6cad95db
JB
3228 if (wpa_s->create_p2p_iface)
3229 wpas_p2p_remove_pending_group_interface(wpa_s);
b22128ef
JM
3230 return -1;
3231 }
3232 return ret;
3233}
3234
3235
3236/**
3237 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3238 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3239 * @freq: Frequency of the channel in MHz
3240 * @duration: Duration of the stay on the channel in milliseconds
3241 *
3242 * This callback is called when the driver indicates that it has started the
3243 * requested remain-on-channel duration.
3244 */
3245void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3246 unsigned int freq, unsigned int duration)
3247{
9526fd29
JM
3248 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3249 return;
b22128ef
JM
3250 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3251 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3252 wpa_s->pending_listen_duration);
3253 wpa_s->pending_listen_freq = 0;
3254 }
3255}
3256
3257
3258static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3259 unsigned int timeout)
3260{
3261 /* Limit maximum Listen state time based on driver limitation. */
3262 if (timeout > wpa_s->max_remain_on_chan)
3263 timeout = wpa_s->max_remain_on_chan;
3264
3ac17eba
JM
3265 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3266 return wpa_drv_p2p_listen(wpa_s, timeout);
3267
b22128ef
JM
3268 return p2p_listen(wpa_s->global->p2p, timeout);
3269}
3270
3271
3272/**
3273 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3274 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3275 * @freq: Frequency of the channel in MHz
3276 *
3277 * This callback is called when the driver indicates that a remain-on-channel
3278 * operation has been completed, i.e., the duration on the requested channel
3279 * has timed out.
3280 */
3281void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3282 unsigned int freq)
3283{
1cc3a29d 3284 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
11fb02be 3285 "(p2p_long_listen=%d ms pending_action_tx=%p)",
1cc3a29d 3286 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
9526fd29
JM
3287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3288 return;
b22128ef
JM
3289 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3290 return; /* P2P module started a new operation */
3291 if (wpa_s->pending_action_tx)
3292 return;
3293 if (wpa_s->p2p_long_listen > 0)
11fb02be 3294 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
b22128ef
JM
3295 if (wpa_s->p2p_long_listen > 0) {
3296 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
11fb02be 3297 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
b22128ef
JM
3298 }
3299}
3300
3301
3302/**
3303 * wpas_p2p_group_remove - Remove a P2P group
3304 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3305 * @ifname: Network interface name of the group interface or "*" to remove all
3306 * groups
3307 * Returns: 0 on success, -1 on failure
3308 *
3309 * This function is used to remove a P2P group. This can be used to disconnect
3310 * from a group in which the local end is a P2P Client or to end a P2P Group in
3311 * case the local end is the Group Owner. If a virtual network interface was
3312 * created for this group, that interface will be removed. Otherwise, only the
3313 * configured P2P group network will be removed from the interface.
3314 */
3315int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3316{
3317 struct wpa_global *global = wpa_s->global;
3318
3319 if (os_strcmp(ifname, "*") == 0) {
3320 struct wpa_supplicant *prev;
3321 wpa_s = global->ifaces;
3322 while (wpa_s) {
3323 prev = wpa_s;
3324 wpa_s = wpa_s->next;
3103f345 3325 wpas_p2p_disconnect(prev);
b22128ef
JM
3326 }
3327 return 0;
3328 }
3329
3330 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3331 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3332 break;
3333 }
3334
3103f345 3335 return wpas_p2p_disconnect(wpa_s);
b22128ef
JM
3336}
3337
3338
b8349523
NKG
3339static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3340 struct p2p_go_neg_results *params,
3341 int freq)
b22128ef
JM
3342{
3343 u8 bssid[ETH_ALEN];
3344 int res;
3345
3346 os_memset(params, 0, sizeof(*params));
3347 params->role_go = 1;
7cfc4ac3
AGS
3348 if (freq) {
3349 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3350 "frequency %d MHz", freq);
b22128ef 3351 params->freq = freq;
7cfc4ac3
AGS
3352 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3353 wpa_s->conf->p2p_oper_channel >= 1 &&
3354 wpa_s->conf->p2p_oper_channel <= 11) {
b22128ef 3355 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3356 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3357 "frequency %d MHz", params->freq);
3358 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
64f99265 3359 wpa_s->conf->p2p_oper_reg_class == 124) {
b22128ef 3360 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3361 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3362 "frequency %d MHz", params->freq);
3363 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3364 wpa_s->best_overall_freq > 0 &&
3365 p2p_supported_freq(wpa_s->global->p2p,
3366 wpa_s->best_overall_freq)) {
3367 params->freq = wpa_s->best_overall_freq;
3368 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3369 "channel %d MHz", params->freq);
3370 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3371 wpa_s->best_24_freq > 0 &&
3372 p2p_supported_freq(wpa_s->global->p2p,
3373 wpa_s->best_24_freq)) {
3374 params->freq = wpa_s->best_24_freq;
3375 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3376 "channel %d MHz", params->freq);
3377 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3378 wpa_s->best_5_freq > 0 &&
3379 p2p_supported_freq(wpa_s->global->p2p,
3380 wpa_s->best_5_freq)) {
3381 params->freq = wpa_s->best_5_freq;
3382 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3383 "channel %d MHz", params->freq);
3384 } else {
6f3bc72b
JM
3385 int chan;
3386 for (chan = 0; chan < 11; chan++) {
3387 params->freq = 2412 + chan * 5;
3388 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3389 params->freq))
3390 break;
3391 }
3392 if (chan == 11) {
3393 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3394 "allowed");
3395 return -1;
3396 }
7cfc4ac3
AGS
3397 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3398 "known)", params->freq);
3399 }
3400
b22128ef
JM
3401 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3402 wpa_s->assoc_freq && !freq) {
3403 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3404 "already using");
3405 params->freq = wpa_s->assoc_freq;
3406 }
3407
3408 res = wpa_drv_shared_freq(wpa_s);
3409 if (res > 0 && !freq) {
3410 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3411 "already using on a shared interface");
3412 params->freq = res;
b8349523
NKG
3413 } else if (res > 0 && freq != res &&
3414 !(wpa_s->drv_flags &
3415 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3416 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3417 "while connected on another channel (%u MHz)",
3418 freq, res);
3419 return -1;
b22128ef 3420 }
b8349523
NKG
3421
3422 return 0;
b22128ef
JM
3423}
3424
3425
3426static struct wpa_supplicant *
3427wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3428 int go)
3429{
3430 struct wpa_supplicant *group_wpa_s;
3431
3432 if (!wpas_p2p_create_iface(wpa_s))
3433 return wpa_s;
3434
3435 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3436 WPA_IF_P2P_CLIENT) < 0)
3437 return NULL;
3438 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3439 if (group_wpa_s == NULL) {
3440 wpas_p2p_remove_pending_group_interface(wpa_s);
3441 return NULL;
3442 }
3443
3444 return group_wpa_s;
3445}
3446
3447
3448/**
3449 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3450 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3451 * @persistent_group: Whether to create a persistent group
3452 * @freq: Frequency for the group or 0 to indicate no hardcoding
3453 * Returns: 0 on success, -1 on failure
3454 *
3455 * This function creates a new P2P group with the local end as the Group Owner,
3456 * i.e., without using Group Owner Negotiation.
3457 */
3458int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3459 int freq)
3460{
3461 struct p2p_go_neg_results params;
7cfc4ac3
AGS
3462 unsigned int r;
3463
9526fd29
JM
3464 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3465 return -1;
3466
2d4f15d6
JJ
3467 /* Make sure we are not running find during connection establishment */
3468 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3469 wpas_p2p_stop_find(wpa_s);
3470
7cfc4ac3
AGS
3471 if (freq == 2) {
3472 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3473 "band");
3474 if (wpa_s->best_24_freq > 0 &&
3475 p2p_supported_freq(wpa_s->global->p2p,
3476 wpa_s->best_24_freq)) {
3477 freq = wpa_s->best_24_freq;
3478 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3479 "channel: %d MHz", freq);
3480 } else {
3481 os_get_random((u8 *) &r, sizeof(r));
3482 freq = 2412 + (r % 3) * 25;
3483 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3484 "channel: %d MHz", freq);
3485 }
3486 }
3487
3488 if (freq == 5) {
3489 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3490 "band");
3491 if (wpa_s->best_5_freq > 0 &&
3492 p2p_supported_freq(wpa_s->global->p2p,
3493 wpa_s->best_5_freq)) {
3494 freq = wpa_s->best_5_freq;
3495 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3496 "channel: %d MHz", freq);
3497 } else {
3498 os_get_random((u8 *) &r, sizeof(r));
3499 freq = 5180 + (r % 4) * 20;
3500 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3501 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3502 "5 GHz channel for P2P group");
3503 return -1;
3504 }
3505 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3506 "channel: %d MHz", freq);
3507 }
3508 }
b22128ef 3509
4ae4650b
JM
3510 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3511 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3512 "(%u MHz) is not supported for P2P uses",
3513 freq);
3514 return -1;
3515 }
3516
b8349523
NKG
3517 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3518 return -1;
f4329aa2
JM
3519 if (params.freq &&
3520 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3521 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3522 "(%u MHz) is not supported for P2P uses",
3523 params.freq);
3524 return -1;
3525 }
b22128ef
JM
3526 p2p_go_params(wpa_s->global->p2p, &params);
3527 params.persistent_group = persistent_group;
3528
3529 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3530 if (wpa_s == NULL)
3531 return -1;
3532 wpas_start_wps_go(wpa_s, &params, 0);
3533
3534 return 0;
3535}
3536
3537
3538static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3539 struct wpa_ssid *params, int addr_allocated)
3540{
3541 struct wpa_ssid *ssid;
3542
3543 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3544 if (wpa_s == NULL)
3545 return -1;
3546
3547 wpa_supplicant_ap_deinit(wpa_s);
3548
3549 ssid = wpa_config_add_network(wpa_s->conf);
3550 if (ssid == NULL)
3551 return -1;
b22128ef
JM
3552 wpa_config_set_network_defaults(ssid);
3553 ssid->temporary = 1;
3554 ssid->proto = WPA_PROTO_RSN;
3555 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3556 ssid->group_cipher = WPA_CIPHER_CCMP;
3557 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3558 ssid->ssid = os_malloc(params->ssid_len);
3559 if (ssid->ssid == NULL) {
b22128ef
JM
3560 wpa_config_remove_network(wpa_s->conf, ssid->id);
3561 return -1;
3562 }
3563 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3564 ssid->ssid_len = params->ssid_len;
3565 ssid->p2p_group = 1;
d1c8ac88 3566 ssid->export_keys = 1;
b22128ef
JM
3567 if (params->psk_set) {
3568 os_memcpy(ssid->psk, params->psk, 32);
3569 ssid->psk_set = 1;
3570 }
3571 if (params->passphrase)
3572 ssid->passphrase = os_strdup(params->passphrase);
3573
3574 wpa_supplicant_select_network(wpa_s, ssid);
3575
3576 wpa_s->show_group_started = 1;
3577
3578 return 0;
3579}
3580
3581
3582int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3583 struct wpa_ssid *ssid, int addr_allocated,
3584 int freq)
3585{
3586 struct p2p_go_neg_results params;
6c0da49f 3587 int go = 0;
b22128ef
JM
3588
3589 if (ssid->disabled != 2 || ssid->ssid == NULL)
3590 return -1;
3591
6c0da49f
JM
3592 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3593 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3594 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3595 "already running");
3596 return 0;
3597 }
3598
9d39057c
JM
3599 /* Make sure we are not running find during connection establishment */
3600 wpas_p2p_stop_find(wpa_s);
b22128ef 3601
aa9bb764
JM
3602 wpa_s->p2p_fallback_to_go_neg = 0;
3603
b22128ef
JM
3604 if (ssid->mode == WPAS_MODE_INFRA)
3605 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3606
3607 if (ssid->mode != WPAS_MODE_P2P_GO)
3608 return -1;
3609
b8349523
NKG
3610 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3611 return -1;
b22128ef
JM
3612
3613 params.role_go = 1;
3614 if (ssid->passphrase == NULL ||
3615 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3616 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3617 "group");
3618 return -1;
3619 }
3620 os_strlcpy(params.passphrase, ssid->passphrase,
3621 sizeof(params.passphrase));
3622 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3623 params.ssid_len = ssid->ssid_len;
3624 params.persistent_group = 1;
3625
3626 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3627 if (wpa_s == NULL)
3628 return -1;
3629
3630 wpas_start_wps_go(wpa_s, &params, 0);
3631
3632 return 0;
3633}
3634
3635
3636static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3637 struct wpabuf *proberesp_ies)
3638{
3639 struct wpa_supplicant *wpa_s = ctx;
3640 if (wpa_s->ap_iface) {
3641 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
adc33680
JM
3642 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3643 wpabuf_free(beacon_ies);
3644 wpabuf_free(proberesp_ies);
3645 return;
3646 }
b22128ef
JM
3647 if (beacon_ies) {
3648 wpabuf_free(hapd->p2p_beacon_ie);
3649 hapd->p2p_beacon_ie = beacon_ies;
3650 }
3651 wpabuf_free(hapd->p2p_probe_resp_ie);
3652 hapd->p2p_probe_resp_ie = proberesp_ies;
3653 } else {
3654 wpabuf_free(beacon_ies);
3655 wpabuf_free(proberesp_ies);
3656 }
3657 wpa_supplicant_ap_update_beacon(wpa_s);
3658}
3659
3660
3071e181
JM
3661static void wpas_p2p_idle_update(void *ctx, int idle)
3662{
3663 struct wpa_supplicant *wpa_s = ctx;
3664 if (!wpa_s->ap_iface)
3665 return;
3666 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3667 if (idle)
3668 wpas_p2p_set_group_idle_timeout(wpa_s);
3669 else
3670 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3671}
3672
3673
b22128ef 3674struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6f251b6b 3675 struct wpa_ssid *ssid)
b22128ef
JM
3676{
3677 struct p2p_group *group;
3678 struct p2p_group_config *cfg;
3679
3ac17eba
JM
3680 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3681 return NULL;
9526fd29
JM
3682 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3683 return NULL;
3ac17eba 3684
b22128ef
JM
3685 cfg = os_zalloc(sizeof(*cfg));
3686 if (cfg == NULL)
3687 return NULL;
3688
6f251b6b 3689 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
acc247b2 3690 cfg->persistent_group = 2;
6f251b6b 3691 else if (ssid->p2p_persistent_group)
acc247b2 3692 cfg->persistent_group = 1;
b22128ef 3693 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3f4ce13f
JM
3694 if (wpa_s->max_stations &&
3695 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3696 cfg->max_clients = wpa_s->max_stations;
3697 else
3698 cfg->max_clients = wpa_s->conf->max_num_sta;
6f251b6b
JM
3699 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3700 cfg->ssid_len = ssid->ssid_len;
b22128ef
JM
3701 cfg->cb_ctx = wpa_s;
3702 cfg->ie_update = wpas_p2p_ie_update;
3071e181 3703 cfg->idle_update = wpas_p2p_idle_update;
b22128ef
JM
3704
3705 group = p2p_group_init(wpa_s->global->p2p, cfg);
3706 if (group == NULL)
3707 os_free(cfg);
6f251b6b 3708 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
b22128ef
JM
3709 p2p_group_notif_formation_done(group);
3710 wpa_s->p2p_group = group;
3711 return group;
3712}
3713
3714
3715void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3716 int registrar)
3717{
10531d21
JM
3718 struct wpa_ssid *ssid = wpa_s->current_ssid;
3719
b22128ef
JM
3720 if (!wpa_s->p2p_in_provisioning) {
3721 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3722 "provisioning not in progress");
3723 return;
3724 }
3725
10531d21
JM
3726 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3727 u8 go_dev_addr[ETH_ALEN];
3728 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3729 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3730 ssid->ssid_len);
3731 /* Clear any stored provisioning info */
3732 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3733 }
ec437d9e 3734
b22128ef
JM
3735 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3736 NULL);
361cdf34
JM
3737 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3738 /*
3739 * Use a separate timeout for initial data connection to
3740 * complete to allow the group to be removed automatically if
3741 * something goes wrong in this step before the P2P group idle
3742 * timeout mechanism is taken into use.
3743 */
3744 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3745 wpas_p2p_group_formation_timeout,
ad853202 3746 wpa_s->parent, NULL);
361cdf34 3747 }
b22128ef
JM
3748 if (wpa_s->global->p2p)
3749 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3ac17eba
JM
3750 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3751 wpa_drv_wps_success_cb(wpa_s, peer_addr);
b22128ef
JM
3752 wpas_group_formation_completed(wpa_s, 1);
3753}
3754
3755
3734552f
JS
3756void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3757 struct wps_event_fail *fail)
3758{
3759 if (!wpa_s->p2p_in_provisioning) {
3760 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3761 "provisioning not in progress");
3762 return;
3763 }
ec437d9e
JJ
3764
3765 if (wpa_s->go_params) {
3766 p2p_clear_provisioning_info(
3767 wpa_s->global->p2p,
10531d21 3768 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
3769 }
3770
3734552f
JS
3771 wpas_notify_p2p_wps_failed(wpa_s, fail);
3772}
3773
3774
b22128ef 3775int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
0918c4bf
JM
3776 const char *config_method,
3777 enum wpas_p2p_prov_disc_use use)
b22128ef
JM
3778{
3779 u16 config_methods;
3780
aa9bb764 3781 wpa_s->p2p_fallback_to_go_neg = 0;
0918c4bf 3782 wpa_s->pending_pd_use = NORMAL_PD;
8c5f7309 3783 if (os_strncmp(config_method, "display", 7) == 0)
b22128ef 3784 config_methods = WPS_CONFIG_DISPLAY;
8c5f7309 3785 else if (os_strncmp(config_method, "keypad", 6) == 0)
b22128ef 3786 config_methods = WPS_CONFIG_KEYPAD;
8c5f7309
JJ
3787 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3788 os_strncmp(config_method, "pushbutton", 10) == 0)
b22128ef 3789 config_methods = WPS_CONFIG_PUSHBUTTON;
8c5f7309
JJ
3790 else {
3791 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
b22128ef 3792 return -1;
8c5f7309 3793 }
b22128ef 3794
0918c4bf
JM
3795 if (use == WPAS_P2P_PD_AUTO) {
3796 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3797 wpa_s->pending_pd_config_methods = config_methods;
3798 wpa_s->p2p_auto_pd = 1;
3799 wpa_s->p2p_auto_join = 0;
3800 wpa_s->pending_pd_before_join = 0;
3801 wpas_p2p_stop_find(wpa_s);
3802 wpa_s->p2p_join_scan_count = 0;
3803 wpas_p2p_join_scan(wpa_s, NULL);
3804 return 0;
3805 }
3806
3ac17eba
JM
3807 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3808 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
0918c4bf
JM
3809 config_methods,
3810 use == WPAS_P2P_PD_FOR_JOIN);
3ac17eba
JM
3811 }
3812
9526fd29 3813 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef
JM
3814 return -1;
3815
3816 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
0918c4bf
JM
3817 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3818 0);
b22128ef
JM
3819}
3820
3821
3822int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3823 char *end)
3824{
3825 return p2p_scan_result_text(ies, ies_len, buf, end);
3826}
3827
3828
1cc3a29d
JM
3829static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3830{
3831 if (!wpa_s->pending_action_tx)
3832 return;
3833
3834 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3835 "operation request");
3836 wpabuf_free(wpa_s->pending_action_tx);
3837 wpa_s->pending_action_tx = NULL;
3838}
3839
3840
b22128ef 3841int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
046ef4aa 3842 enum p2p_discovery_type type,
6d92fa6e
JM
3843 unsigned int num_req_dev_types, const u8 *req_dev_types,
3844 const u8 *dev_id)
b22128ef 3845{
1cc3a29d 3846 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef
JM
3847 wpa_s->p2p_long_listen = 0;
3848
3ac17eba
JM
3849 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3850 return wpa_drv_p2p_find(wpa_s, timeout, type);
3851
9526fd29
JM
3852 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3853 return -1;
3854
433cd2ce
JM
3855 wpa_supplicant_cancel_sched_scan(wpa_s);
3856
046ef4aa 3857 return p2p_find(wpa_s->global->p2p, timeout, type,
6d92fa6e 3858 num_req_dev_types, req_dev_types, dev_id);
b22128ef
JM
3859}
3860
3861
3862void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3863{
1cc3a29d 3864 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef 3865 wpa_s->p2p_long_listen = 0;
9d39057c 3866 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
ef922c4a 3867 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
39185dfa 3868 wpa_s->p2p_cb_on_scan_complete = 0;
b22128ef 3869
3ac17eba
JM
3870 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3871 wpa_drv_p2p_stop_find(wpa_s);
3872 return;
3873 }
3874
9526fd29
JM
3875 if (wpa_s->global->p2p)
3876 p2p_stop_find(wpa_s->global->p2p);
b22128ef
JM
3877
3878 wpas_p2p_remove_pending_group_interface(wpa_s);
3879}
3880
3881
3882static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3883{
3884 struct wpa_supplicant *wpa_s = eloop_ctx;
3885 wpa_s->p2p_long_listen = 0;
3886}
3887
3888
3889int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3890{
3891 int res;
3892
9526fd29
JM
3893 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3894 return -1;
3895
433cd2ce 3896 wpa_supplicant_cancel_sched_scan(wpa_s);
1cc3a29d
JM
3897 wpas_p2p_clear_pending_action_tx(wpa_s);
3898
b22128ef
JM
3899 if (timeout == 0) {
3900 /*
3901 * This is a request for unlimited Listen state. However, at
3902 * least for now, this is mapped to a Listen state for one
3903 * hour.
3904 */
3905 timeout = 3600;
3906 }
3907 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3908 wpa_s->p2p_long_listen = 0;
3909
2bb747e2
JM
3910 /*
3911 * Stop previous find/listen operation to avoid trying to request a new
3912 * remain-on-channel operation while the driver is still running the
3913 * previous one.
3914 */
3915 if (wpa_s->global->p2p)
3916 p2p_stop_find(wpa_s->global->p2p);
3917
b22128ef
JM
3918 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3919 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
11fb02be 3920 wpa_s->p2p_long_listen = timeout * 1000;
b22128ef
JM
3921 eloop_register_timeout(timeout, 0,
3922 wpas_p2p_long_listen_timeout,
3923 wpa_s, NULL);
3924 }
3925
3926 return res;
3927}
3928
3929
4c08c0bd 3930int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
b22128ef
JM
3931 u8 *buf, size_t len, int p2p_group)
3932{
4c08c0bd
JM
3933 struct wpabuf *p2p_ie;
3934 int ret;
3935
b22128ef
JM
3936 if (wpa_s->global->p2p_disabled)
3937 return -1;
3938 if (wpa_s->global->p2p == NULL)
3939 return -1;
e1f1509b
JM
3940 if (bss == NULL)
3941 return -1;
b22128ef 3942
4c08c0bd
JM
3943 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3944 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3945 p2p_group, p2p_ie);
3946 wpabuf_free(p2p_ie);
3947
3948 return ret;
b22128ef
JM
3949}
3950
3951
3952int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
04a85e44 3953 const u8 *dst, const u8 *bssid,
baf513d6 3954 const u8 *ie, size_t ie_len, int ssi_signal)
b22128ef
JM
3955{
3956 if (wpa_s->global->p2p_disabled)
3957 return 0;
3958 if (wpa_s->global->p2p == NULL)
3959 return 0;
3960
2d43d37f
JB
3961 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3962 ie, ie_len)) {
3963 case P2P_PREQ_NOT_P2P:
3964 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
3965 ssi_signal);
3966 /* fall through */
3967 case P2P_PREQ_MALFORMED:
3968 case P2P_PREQ_NOT_LISTEN:
3969 case P2P_PREQ_NOT_PROCESSED:
3970 default: /* make gcc happy */
3971 return 0;
3972 case P2P_PREQ_PROCESSED:
3973 return 1;
3974 }
b22128ef
JM
3975}
3976
3977
3978void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3979 const u8 *sa, const u8 *bssid,
3980 u8 category, const u8 *data, size_t len, int freq)
3981{
3982 if (wpa_s->global->p2p_disabled)
3983 return;
3984 if (wpa_s->global->p2p == NULL)
3985 return;
3986
3987 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3988 freq);
3989}
3990
3991
3992void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3993{
3994 if (wpa_s->global->p2p_disabled)
3995 return;
3996 if (wpa_s->global->p2p == NULL)
3997 return;
3998
6d92fa6e 3999 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
b22128ef
JM
4000}
4001
4002
4003void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4004{
4005 p2p_group_deinit(wpa_s->p2p_group);
4006 wpa_s->p2p_group = NULL;
a7fd39bb
JD
4007
4008 wpa_s->ap_configured_cb = NULL;
4009 wpa_s->ap_configured_cb_ctx = NULL;
4010 wpa_s->ap_configured_cb_data = NULL;
4011 wpa_s->connect_without_scan = NULL;
b22128ef
JM
4012}
4013
4014
4015int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4016{
4017 wpa_s->p2p_long_listen = 0;
4018
3ac17eba
JM
4019 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4020 return wpa_drv_p2p_reject(wpa_s, addr);
4021
9526fd29
JM
4022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4023 return -1;
4024
b22128ef
JM
4025 return p2p_reject(wpa_s->global->p2p, addr);
4026}
4027
4028
4029/* Invite to reinvoke a persistent group */
4030int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4031 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4032{
4033 enum p2p_invite_role role;
4034 u8 *bssid = NULL;
4035
4036 if (ssid->mode == WPAS_MODE_P2P_GO) {
4037 role = P2P_INVITE_ROLE_GO;
4038 if (peer_addr == NULL) {
4039 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4040 "address in invitation command");
4041 return -1;
4042 }
4043 if (wpas_p2p_create_iface(wpa_s)) {
4044 if (wpas_p2p_add_group_interface(wpa_s,
4045 WPA_IF_P2P_GO) < 0) {
4046 wpa_printf(MSG_ERROR, "P2P: Failed to "
4047 "allocate a new interface for the "
4048 "group");
4049 return -1;
4050 }
4051 bssid = wpa_s->pending_interface_addr;
4052 } else
4053 bssid = wpa_s->own_addr;
4054 } else {
4055 role = P2P_INVITE_ROLE_CLIENT;
4056 peer_addr = ssid->bssid;
4057 }
4058 wpa_s->pending_invite_ssid_id = ssid->id;
4059
3ac17eba
JM
4060 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4061 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4062 ssid->ssid, ssid->ssid_len,
4063 go_dev_addr, 1);
4064
9526fd29
JM
4065 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4066 return -1;
4067
b22128ef
JM
4068 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4069 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4070}
4071
4072
4073/* Invite to join an active group */
4074int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4075 const u8 *peer_addr, const u8 *go_dev_addr)
4076{
4077 struct wpa_global *global = wpa_s->global;
4078 enum p2p_invite_role role;
4079 u8 *bssid = NULL;
4080 struct wpa_ssid *ssid;
c427ac92 4081 int persistent;
b22128ef
JM
4082
4083 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4084 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4085 break;
4086 }
4087 if (wpa_s == NULL) {
4088 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4089 return -1;
4090 }
4091
4092 ssid = wpa_s->current_ssid;
4093 if (ssid == NULL) {
4094 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4095 "invitation");
4096 return -1;
4097 }
4098
c427ac92
JM
4099 persistent = ssid->p2p_persistent_group &&
4100 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4101 ssid->ssid, ssid->ssid_len);
4102
b22128ef
JM
4103 if (ssid->mode == WPAS_MODE_P2P_GO) {
4104 role = P2P_INVITE_ROLE_ACTIVE_GO;
4105 bssid = wpa_s->own_addr;
4106 if (go_dev_addr == NULL)
d7e70476 4107 go_dev_addr = wpa_s->global->p2p_dev_addr;
b22128ef
JM
4108 } else {
4109 role = P2P_INVITE_ROLE_CLIENT;
4110 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4111 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4112 "invite to current group");
4113 return -1;
4114 }
4115 bssid = wpa_s->bssid;
4116 if (go_dev_addr == NULL &&
4117 !is_zero_ether_addr(wpa_s->go_dev_addr))
4118 go_dev_addr = wpa_s->go_dev_addr;
4119 }
bb79dc72 4120 wpa_s->parent->pending_invite_ssid_id = -1;
b22128ef 4121
3ac17eba
JM
4122 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4123 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4124 ssid->ssid, ssid->ssid_len,
c427ac92 4125 go_dev_addr, persistent);
3ac17eba 4126
9526fd29
JM
4127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4128 return -1;
4129
b22128ef 4130 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
d9d6a58c 4131 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
c427ac92 4132 go_dev_addr, persistent);
b22128ef
JM
4133}
4134
4135
4136void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4137{
4138 struct wpa_ssid *ssid = wpa_s->current_ssid;
4139 const char *ssid_txt;
4140 u8 go_dev_addr[ETH_ALEN];
4b6baa2f 4141 int network_id = -1;
b22128ef 4142 int persistent;
b49d6ccb 4143 int freq;
b22128ef 4144
361cdf34
JM
4145 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4146 NULL);
4147
b22128ef
JM
4148 if (!wpa_s->show_group_started || !ssid)
4149 return;
4150
4151 wpa_s->show_group_started = 0;
4152
4153 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4154 os_memset(go_dev_addr, 0, ETH_ALEN);
4155 if (ssid->bssid_set)
4156 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4157 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4158 ssid->ssid_len);
4159 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4160
a0a9f3b0
JM
4161 if (wpa_s->global->p2p_group_formation == wpa_s)
4162 wpa_s->global->p2p_group_formation = NULL;
4163
b49d6ccb
JJ
4164 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4165 (int) wpa_s->assoc_freq;
b22128ef
JM
4166 if (ssid->passphrase == NULL && ssid->psk_set) {
4167 char psk[65];
4168 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4169 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4170 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4171 MACSTR "%s",
b49d6ccb 4172 wpa_s->ifname, ssid_txt, freq, psk,
c481048f 4173 MAC2STR(go_dev_addr),
b22128ef
JM
4174 persistent ? " [PERSISTENT]" : "");
4175 } else {
4176 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4177 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4178 "go_dev_addr=" MACSTR "%s",
b49d6ccb 4179 wpa_s->ifname, ssid_txt, freq,
b22128ef
JM
4180 ssid->passphrase ? ssid->passphrase : "",
4181 MAC2STR(go_dev_addr),
4182 persistent ? " [PERSISTENT]" : "");
4183 }
4184
4185 if (persistent)
4b6baa2f
JMB
4186 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4187 ssid, go_dev_addr);
4188 if (network_id < 0)
4189 network_id = ssid->id;
4190 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
b22128ef
JM
4191}
4192
4193
4194int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4195 u32 interval1, u32 duration2, u32 interval2)
4196{
3ac17eba
JM
4197 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4198 return -1;
9526fd29
JM
4199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4200 return -1;
3ac17eba 4201
b22128ef
JM
4202 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4203 wpa_s->current_ssid == NULL ||
4204 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4205 return -1;
4206
4207 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4208 wpa_s->own_addr, wpa_s->assoc_freq,
4209 duration1, interval1, duration2, interval2);
4210}
4211
4212
4213int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4214 unsigned int interval)
4215{
3ac17eba
JM
4216 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4217 return -1;
4218
9526fd29
JM
4219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4220 return -1;
4221
b22128ef
JM
4222 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4223}
4224
4225
c8106615
JM
4226static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4227{
4228 return wpa_s->current_ssid != NULL &&
4229 wpa_s->current_ssid->p2p_group &&
4230 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4231}
4232
4233
3071e181
JM
4234static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4235{
4236 struct wpa_supplicant *wpa_s = eloop_ctx;
4237
c8106615 4238 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
3071e181
JM
4239 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4240 "disabled");
4241 return;
4242 }
4243
4244 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4245 "group");
4246 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
aa9bb764 4247 wpas_p2p_group_delete(wpa_s, 0);
3071e181
JM
4248}
4249
4250
4251static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4252{
5f482d55 4253 int timeout;
c8106615 4254
dddc7045
JM
4255 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4256 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4257
c8106615 4258 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3071e181
JM
4259 return;
4260
c8106615
JM
4261 timeout = wpa_s->conf->p2p_group_idle;
4262 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4263 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4264 timeout = P2P_MAX_CLIENT_IDLE;
4265
4266 if (timeout == 0)
3071e181
JM
4267 return;
4268
5f482d55
JM
4269 if (timeout < 0) {
4270 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4271 timeout = 0; /* special client mode no-timeout */
4272 else
4273 return;
4274 }
4275
8c472816
JM
4276 if (wpa_s->p2p_in_provisioning) {
4277 /*
4278 * Use the normal group formation timeout during the
4279 * provisioning phase to avoid terminating this process too
4280 * early due to group idle timeout.
4281 */
4282 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4283 "during provisioning");
4284 return;
4285 }
4286
361cdf34
JM
4287 if (wpa_s->show_group_started) {
4288 /*
4289 * Use the normal group formation timeout between the end of
4290 * the provisioning phase and completion of 4-way handshake to
4291 * avoid terminating this process too early due to group idle
4292 * timeout.
4293 */
4294 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4295 "while waiting for initial 4-way handshake to "
4296 "complete");
4297 return;
4298 }
4299
3071e181 4300 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
c8106615
JM
4301 timeout);
4302 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4303 wpa_s, NULL);
3071e181
JM
4304}
4305
4306
b22128ef 4307void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3fc14102
JM
4308 u16 reason_code, const u8 *ie, size_t ie_len,
4309 int locally_generated)
b22128ef 4310{
9526fd29 4311 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 4312 return;
3ac17eba
JM
4313 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4314 return;
b22128ef 4315
3fc14102
JM
4316 if (!locally_generated)
4317 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4318 ie_len);
4319
4320 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4321 wpa_s->current_ssid &&
4322 wpa_s->current_ssid->p2p_group &&
4323 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4324 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4325 "session is ending");
4326 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
aa9bb764 4327 wpas_p2p_group_delete(wpa_s, 0);
3fc14102 4328 }
b22128ef
JM
4329}
4330
4331
4332void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3fc14102
JM
4333 u16 reason_code, const u8 *ie, size_t ie_len,
4334 int locally_generated)
b22128ef 4335{
9526fd29 4336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 4337 return;
3ac17eba
JM
4338 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4339 return;
b22128ef 4340
3fc14102
JM
4341 if (!locally_generated)
4342 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4343 ie_len);
b22128ef
JM
4344}
4345
4346
4347void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4348{
4349 struct p2p_data *p2p = wpa_s->global->p2p;
4350
4351 if (p2p == NULL)
4352 return;
4353
4c010834
KL
4354 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4355 return;
4356
b22128ef
JM
4357 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4358 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4359
2f646b6e
JB
4360 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4361 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
b22128ef 4362
b6e01800
JM
4363 if (wpa_s->wps &&
4364 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4365 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4366
4367 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4368 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4369
4370 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4371 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4372 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4373 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4374 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4375 }
4376
2f646b6e
JB
4377 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4378 p2p_set_sec_dev_types(p2p,
4379 (void *) wpa_s->conf->sec_device_type,
4380 wpa_s->conf->num_sec_device_types);
b22128ef 4381
f95cac27
JMB
4382 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4383 int i;
4384 p2p_remove_wps_vendor_extensions(p2p);
4385 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4386 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4387 continue;
4388 p2p_add_wps_vendor_extension(
4389 p2p, wpa_s->conf->wps_vendor_ext[i]);
4390 }
4391 }
4392
b22128ef
JM
4393 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4394 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4395 char country[3];
4396 country[0] = wpa_s->conf->country[0];
4397 country[1] = wpa_s->conf->country[1];
4398 country[2] = 0x04;
4399 p2p_set_country(p2p, country);
4400 }
4401
4402 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4403 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4404 wpa_s->conf->p2p_ssid_postfix ?
4405 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4406 0);
4407 }
0f66abd2
SS
4408
4409 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4410 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
2463ba70
JS
4411
4412 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4413 u8 reg_class, channel;
4414 int ret;
4415 unsigned int r;
4416 if (wpa_s->conf->p2p_listen_reg_class &&
4417 wpa_s->conf->p2p_listen_channel) {
4418 reg_class = wpa_s->conf->p2p_listen_reg_class;
4419 channel = wpa_s->conf->p2p_listen_channel;
4420 } else {
4421 reg_class = 81;
4422 /*
4423 * Pick one of the social channels randomly as the
4424 * listen channel.
4425 */
4426 os_get_random((u8 *) &r, sizeof(r));
4427 channel = 1 + (r % 3) * 5;
4428 }
4429 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4430 if (ret)
4431 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4432 "failed: %d", ret);
4433 }
4434 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4435 u8 op_reg_class, op_channel, cfg_op_channel;
4436 int ret = 0;
4437 unsigned int r;
4438 if (wpa_s->conf->p2p_oper_reg_class &&
4439 wpa_s->conf->p2p_oper_channel) {
4440 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4441 op_channel = wpa_s->conf->p2p_oper_channel;
4442 cfg_op_channel = 1;
4443 } else {
4444 op_reg_class = 81;
4445 /*
4446 * Use random operation channel from (1, 6, 11)
4447 *if no other preference is indicated.
4448 */
4449 os_get_random((u8 *) &r, sizeof(r));
4450 op_channel = 1 + (r % 3) * 5;
4451 cfg_op_channel = 0;
4452 }
4453 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4454 cfg_op_channel);
4455 if (ret)
4456 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4457 "failed: %d", ret);
4458 }
21d996f7
JM
4459
4460 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4461 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4462 wpa_s->conf->p2p_pref_chan) < 0) {
4463 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4464 "update failed");
4465 }
4466 }
b22128ef 4467}
aefb53bd
JM
4468
4469
4470int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4471 int duration)
4472{
4473 if (!wpa_s->ap_iface)
4474 return -1;
4475 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4476 duration);
4477}
72044390
JM
4478
4479
4480int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4481{
9526fd29 4482 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
72044390
JM
4483 return -1;
4484 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4485 return -1;
4486
4487 wpa_s->global->cross_connection = enabled;
4488 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4489
4490 if (!enabled) {
4491 struct wpa_supplicant *iface;
4492
4493 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4494 {
4495 if (iface->cross_connect_enabled == 0)
4496 continue;
4497
4498 iface->cross_connect_enabled = 0;
4499 iface->cross_connect_in_use = 0;
4500 wpa_msg(iface->parent, MSG_INFO,
4501 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4502 iface->ifname, iface->cross_connect_uplink);
4503 }
4504 }
4505
4506 return 0;
4507}
4508
4509
4510static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4511{
4512 struct wpa_supplicant *iface;
4513
4514 if (!uplink->global->cross_connection)
4515 return;
4516
4517 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4518 if (!iface->cross_connect_enabled)
4519 continue;
4520 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4521 0)
4522 continue;
4523 if (iface->ap_iface == NULL)
4524 continue;
4525 if (iface->cross_connect_in_use)
4526 continue;
4527
4528 iface->cross_connect_in_use = 1;
4529 wpa_msg(iface->parent, MSG_INFO,
4530 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4531 iface->ifname, iface->cross_connect_uplink);
4532 }
4533}
4534
4535
4536static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4537{
4538 struct wpa_supplicant *iface;
4539
4540 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4541 if (!iface->cross_connect_enabled)
4542 continue;
4543 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4544 0)
4545 continue;
4546 if (!iface->cross_connect_in_use)
4547 continue;
4548
4549 wpa_msg(iface->parent, MSG_INFO,
4550 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4551 iface->ifname, iface->cross_connect_uplink);
4552 iface->cross_connect_in_use = 0;
4553 }
4554}
4555
4556
4557void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4558{
4559 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4560 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4561 wpa_s->cross_connect_disallowed)
4562 wpas_p2p_disable_cross_connect(wpa_s);
4563 else
4564 wpas_p2p_enable_cross_connect(wpa_s);
dddc7045
JM
4565 if (!wpa_s->ap_iface &&
4566 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4567 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
72044390
JM
4568}
4569
4570
4571void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4572{
4573 wpas_p2p_disable_cross_connect(wpa_s);
4c2c6751
JM
4574 if (!wpa_s->ap_iface &&
4575 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4576 wpa_s, NULL))
3071e181 4577 wpas_p2p_set_group_idle_timeout(wpa_s);
72044390
JM
4578}
4579
4580
4581static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4582{
4583 struct wpa_supplicant *iface;
4584
4585 if (!wpa_s->global->cross_connection)
4586 return;
4587
4588 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4589 if (iface == wpa_s)
4590 continue;
4591 if (iface->drv_flags &
4592 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4593 continue;
4594 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4595 continue;
4596
4597 wpa_s->cross_connect_enabled = 1;
4598 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4599 sizeof(wpa_s->cross_connect_uplink));
4600 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4601 "%s to %s whenever uplink is available",
4602 wpa_s->ifname, wpa_s->cross_connect_uplink);
4603
4604 if (iface->ap_iface || iface->current_ssid == NULL ||
4605 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4606 iface->cross_connect_disallowed ||
4607 iface->wpa_state != WPA_COMPLETED)
4608 break;
4609
4610 wpa_s->cross_connect_in_use = 1;
4611 wpa_msg(wpa_s->parent, MSG_INFO,
4612 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4613 wpa_s->ifname, wpa_s->cross_connect_uplink);
4614 break;
4615 }
4616}
b73bf0a7
JM
4617
4618
4619int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4620{
4621 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4622 !wpa_s->p2p_in_provisioning)
4623 return 0; /* not P2P client operation */
4624
4625 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4626 "session overlap");
148bb37f
JM
4627 if (wpa_s != wpa_s->parent)
4628 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
0a14e3ec
SS
4629
4630 if (wpa_s->global->p2p)
4631 p2p_group_formation_failed(wpa_s->global->p2p);
4632
4633 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4634 wpa_s->parent, NULL);
4635
b73bf0a7
JM
4636 wpas_group_formation_completed(wpa_s, 0);
4637 return 1;
4638}
b5c9da8d
JM
4639
4640
4641void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4642{
4643 struct p2p_channels chan;
4644
4645 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4646 return;
4647
4648 os_memset(&chan, 0, sizeof(chan));
4649 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4650 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4651 "channel list");
4652 return;
4653 }
4654
4655 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4656}
59eba7a2
JM
4657
4658
4659int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4660{
4661 struct wpa_global *global = wpa_s->global;
4662 int found = 0;
231bbd03 4663 const u8 *peer;
59eba7a2 4664
9526fd29
JM
4665 if (global->p2p == NULL)
4666 return -1;
4667
59eba7a2
JM
4668 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4669
4670 if (wpa_s->pending_interface_name[0] &&
4671 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4672 found = 1;
4673
231bbd03
SS
4674 peer = p2p_get_go_neg_peer(global->p2p);
4675 if (peer) {
4676 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4677 MACSTR, MAC2STR(peer));
4678 p2p_unauthorize(global->p2p, peer);
75c208b9 4679 found = 1;
231bbd03
SS
4680 }
4681
59eba7a2
JM
4682 wpas_p2p_stop_find(wpa_s);
4683
4684 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4685 if (wpa_s == global->p2p_group_formation &&
a0a9f3b0
JM
4686 (wpa_s->p2p_in_provisioning ||
4687 wpa_s->parent->pending_interface_type ==
4688 WPA_IF_P2P_CLIENT)) {
59eba7a2
JM
4689 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4690 "formation found - cancelling",
4691 wpa_s->ifname);
4692 found = 1;
45fee6f0
SS
4693 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4694 wpa_s->parent, NULL);
aa9bb764 4695 wpas_p2p_group_delete(wpa_s, 0);
59eba7a2
JM
4696 break;
4697 }
4698 }
4699
4700 if (!found) {
4701 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4702 return -1;
4703 }
4704
4705 return 0;
4706}
c973f386
JM
4707
4708
4709void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4710{
4711 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4712 return;
4713
4714 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4715 "being available anymore");
4716 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
aa9bb764 4717 wpas_p2p_group_delete(wpa_s, 0);
c973f386 4718}
7cfc4ac3
AGS
4719
4720
4721void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4722 int freq_24, int freq_5, int freq_overall)
4723{
4724 struct p2p_data *p2p = wpa_s->global->p2p;
4725 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4726 return;
4727 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4728}
9d562b79
SS
4729
4730
4731int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4732{
4733 u8 peer[ETH_ALEN];
4734 struct p2p_data *p2p = wpa_s->global->p2p;
4735
4736 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4737 return -1;
4738
4739 if (hwaddr_aton(addr, peer))
4740 return -1;
4741
4742 return p2p_unauthorize(p2p, peer);
4743}
3103f345
JB
4744
4745
4746/**
4747 * wpas_p2p_disconnect - Disconnect from a P2P Group
4748 * @wpa_s: Pointer to wpa_supplicant data
4749 * Returns: 0 on success, -1 on failure
4750 *
4751 * This can be used to disconnect from a group in which the local end is a P2P
4752 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4753 * virtual network interface was created for this group, that interface will be
4754 * removed. Otherwise, only the configured P2P group network will be removed
4755 * from the interface.
4756 */
4757int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4758{
4759
4760 if (wpa_s == NULL)
4761 return -1;
4762
4763 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
aa9bb764 4764 wpas_p2p_group_delete(wpa_s, 0);
3103f345
JB
4765
4766 return 0;
4767}
303f60d3
JM
4768
4769
4770int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4771{
4772 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4773 return 0;
4774
4775 return p2p_in_progress(wpa_s->global->p2p);
4776}
502618f7
JM
4777
4778
4779void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4780 struct wpa_ssid *ssid)
502618f7
JM
4781{
4782 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4783 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4784 wpa_s->parent, NULL) > 0) {
eb6f8c2b
AC
4785 /**
4786 * Remove the network by scheduling the group formation
4787 * timeout to happen immediately. The teardown code
4788 * needs to be scheduled to run asynch later so that we
4789 * don't delete data from under ourselves unexpectedly.
4790 * Calling wpas_p2p_group_formation_timeout directly
4791 * causes a series of crashes in WPS failure scenarios.
4792 */
502618f7
JM
4793 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4794 "P2P group network getting removed");
eb6f8c2b
AC
4795 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4796 wpa_s->parent, NULL);
502618f7
JM
4797 }
4798}
87f841a1
JM
4799
4800
4801struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
c427ac92
JM
4802 const u8 *addr, const u8 *ssid,
4803 size_t ssid_len)
87f841a1
JM
4804{
4805 struct wpa_ssid *s;
fbdcfd57 4806 size_t i;
87f841a1
JM
4807
4808 for (s = wpa_s->conf->ssid; s; s = s->next) {
fbdcfd57
JM
4809 if (s->disabled != 2)
4810 continue;
c427ac92
JM
4811 if (ssid &&
4812 (ssid_len != s->ssid_len ||
4813 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4814 continue;
fbdcfd57
JM
4815 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4816 return s; /* peer is GO in the persistent group */
4817 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4818 continue;
4819 for (i = 0; i < s->num_p2p_clients; i++) {
4820 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4821 addr, ETH_ALEN) == 0)
4822 return s; /* peer is P2P client in persistent
4823 * group */
4824 }
87f841a1
JM
4825 }
4826
4827 return NULL;
4828}
fbdcfd57
JM
4829
4830
4831void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4832 const u8 *addr)
4833{
4834 if (addr == NULL)
4835 return;
4836 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4837}
aa9bb764
JM
4838
4839
4840static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4841 int group_added)
4842{
4843 struct wpa_supplicant *group = wpa_s;
4844 if (wpa_s->global->p2p_group_formation)
4845 group = wpa_s->global->p2p_group_formation;
4846 wpa_s = wpa_s->parent;
b80eb89d 4847 offchannel_send_action_done(wpa_s);
aa9bb764
JM
4848 if (group_added)
4849 wpas_p2p_group_delete(group, 1);
4850 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4851 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4852 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4853 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
3bc462cb
JM
4854 wpa_s->p2p_persistent_id,
4855 wpa_s->p2p_pd_before_go_neg);
aa9bb764
JM
4856}
4857
4858
4859int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4860{
4861 if (!wpa_s->p2p_fallback_to_go_neg ||
4862 wpa_s->p2p_in_provisioning <= 5)
4863 return 0;
4864
b80eb89d
JM
4865 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4866 return 0; /* peer operating as a GO */
4867
aa9bb764
JM
4868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4869 "fallback to GO Negotiation");
4870 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4871
4872 return 1;
4873}