]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/p2p_supplicant.c
HS 2.0: Add Hotspot 2.0 ANQP elements to Interworking queries
[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;
2485
f95cac27
JMB
2486 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2487 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2488 continue;
2489 p2p_add_wps_vendor_extension(
2490 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2491 }
2492
b22128ef
JM
2493 return 0;
2494}
2495
2496
2497/**
2498 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2499 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2500 *
2501 * This function deinitialize per-interface P2P data.
2502 */
2503void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2504{
2505 if (wpa_s->driver && wpa_s->drv_priv)
2506 wpa_drv_probe_req_report(wpa_s, 0);
ec437d9e
JJ
2507
2508 if (wpa_s->go_params) {
2509 /* Clear any stored provisioning info */
2510 p2p_clear_provisioning_info(
2511 wpa_s->global->p2p,
10531d21 2512 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
2513 }
2514
b22128ef
JM
2515 os_free(wpa_s->go_params);
2516 wpa_s->go_params = NULL;
b22128ef 2517 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ef922c4a 2518 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
f63b8542 2519 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
b22128ef
JM
2520 wpa_s->p2p_long_listen = 0;
2521 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3071e181 2522 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
b22128ef
JM
2523 wpas_p2p_remove_pending_group_interface(wpa_s);
2524
2525 /* TODO: remove group interface from the driver if this wpa_s instance
2526 * is on top of a P2P group interface */
2527}
2528
2529
2530/**
2531 * wpas_p2p_deinit_global - Deinitialize global P2P module
2532 * @global: Pointer to global data from wpa_supplicant_init()
2533 *
2534 * This function deinitializes the global (per device) P2P module.
2535 */
2536void wpas_p2p_deinit_global(struct wpa_global *global)
2537{
2538 struct wpa_supplicant *wpa_s, *tmp;
b22128ef
JM
2539
2540 if (global->p2p == NULL)
2541 return;
2542
2543 /* Remove remaining P2P group interfaces */
2544 wpa_s = global->ifaces;
2a43101e
JM
2545 if (wpa_s)
2546 wpas_p2p_service_flush(wpa_s);
b22128ef
JM
2547 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2548 wpa_s = wpa_s->next;
2549 while (wpa_s) {
b22128ef
JM
2550 tmp = global->ifaces;
2551 while (tmp &&
2552 (tmp == wpa_s ||
2553 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2554 tmp = tmp->next;
2555 }
2556 if (tmp == NULL)
2557 break;
103b8f4d
NS
2558 /* Disconnect from the P2P group and deinit the interface */
2559 wpas_p2p_disconnect(tmp);
b22128ef
JM
2560 }
2561
743ef799
JM
2562 /*
2563 * Deinit GO data on any possibly remaining interface (if main
2564 * interface is used as GO).
2565 */
2566 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2567 if (wpa_s->ap_iface)
2568 wpas_p2p_group_deinit(wpa_s);
2569 }
2570
b22128ef
JM
2571 p2p_deinit(global->p2p);
2572 global->p2p = NULL;
2573}
2574
2575
2576static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2577{
971e357f
JM
2578 if (wpa_s->drv_flags &
2579 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2580 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
b22128ef
JM
2581 return 1; /* P2P group requires a new interface in every case
2582 */
2583 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2584 return 0; /* driver does not support concurrent operations */
2585 if (wpa_s->global->ifaces->next)
2586 return 1; /* more that one interface already in use */
2587 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2588 return 1; /* this interface is already in use */
2589 return 0;
2590}
2591
2592
2593static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2594 const u8 *peer_addr,
2595 enum p2p_wps_method wps_method,
2596 int go_intent, const u8 *own_interface_addr,
23c84252
JM
2597 unsigned int force_freq, int persistent_group,
2598 struct wpa_ssid *ssid)
b22128ef 2599{
acc247b2
JM
2600 if (persistent_group && wpa_s->conf->persistent_reconnect)
2601 persistent_group = 2;
2602
3ac17eba
JM
2603 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2604 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2605 go_intent, own_interface_addr,
2606 force_freq, persistent_group);
2607 }
2608
b22128ef
JM
2609 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2610 go_intent, own_interface_addr, force_freq,
23c84252
JM
2611 persistent_group, ssid ? ssid->ssid : NULL,
2612 ssid ? ssid->ssid_len : 0);
b22128ef
JM
2613}
2614
2615
2616static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2617 const u8 *peer_addr,
2618 enum p2p_wps_method wps_method,
2619 int go_intent, const u8 *own_interface_addr,
23c84252
JM
2620 unsigned int force_freq, int persistent_group,
2621 struct wpa_ssid *ssid)
b22128ef 2622{
acc247b2
JM
2623 if (persistent_group && wpa_s->conf->persistent_reconnect)
2624 persistent_group = 2;
2625
3ac17eba
JM
2626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2627 return -1;
2628
b22128ef
JM
2629 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2630 go_intent, own_interface_addr, force_freq,
23c84252
JM
2631 persistent_group, ssid ? ssid->ssid : NULL,
2632 ssid ? ssid->ssid_len : 0);
b22128ef
JM
2633}
2634
2635
9b1ab931
JM
2636static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2637{
2638 wpa_s->p2p_join_scan_count++;
2639 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2640 wpa_s->p2p_join_scan_count);
2641 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2642 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2643 " for join operationg - stop join attempt",
2644 MAC2STR(wpa_s->pending_join_iface_addr));
2645 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
0918c4bf
JM
2646 if (wpa_s->p2p_auto_pd) {
2647 wpa_s->p2p_auto_pd = 0;
2648 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2649 " p2p_dev_addr=" MACSTR " status=N/A",
2650 MAC2STR(wpa_s->pending_join_dev_addr));
2651 return;
2652 }
9b1ab931
JM
2653 wpa_msg(wpa_s->parent, MSG_INFO,
2654 P2P_EVENT_GROUP_FORMATION_FAILURE);
2655 }
2656}
2657
2658
f63b8542
JM
2659static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2660{
2661 struct wpa_supplicant *wpa_s = eloop_ctx;
2662 if (!wpa_s->pending_pd_before_join)
2663 return;
2664 /*
2665 * Provision Discovery Response may have been lost - try to connect
2666 * anyway since we do not need any information from this PD.
2667 */
2668 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2669 "try to connect anyway");
2670 wpas_p2p_join_start(wpa_s);
2671}
2672
2673
4b156206
JM
2674static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2675{
2676 struct wpa_supplicant *iface;
2677 int shared_freq;
2678 u8 bssid[ETH_ALEN];
2679
2680 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2681 return 0;
2682
2683 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2684 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2685 continue;
2686 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2687 continue;
86ae2e8a
JM
2688 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2689 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2690 shared_freq = iface->current_ssid->frequency;
2691 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2692 shared_freq = iface->assoc_freq;
2693 else
2694 shared_freq = 0;
2695
2696 if (shared_freq && freq != shared_freq) {
2697 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2698 "connected on %d MHz - new connection on "
2699 "%d MHz", iface->ifname, shared_freq, freq);
2700 return 1;
4b156206
JM
2701 }
2702 }
2703
2704 shared_freq = wpa_drv_shared_freq(wpa_s);
2705 if (shared_freq > 0 && shared_freq != freq) {
2706 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2707 "virtual interface connected on %d MHz - new "
2708 "connection on %d MHz", shared_freq, freq);
2709 return 1;
2710 }
2711
2712 return 0;
2713}
2714
2715
b31be3a0
JM
2716static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2717 const u8 *peer_dev_addr)
2718{
2719 struct wpa_bss *bss;
2720 int updated;
2721
2722 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2723 if (bss == NULL)
aa9bb764 2724 return -1;
b31be3a0
JM
2725 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2726 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2727 "last scan");
2728 return 0;
2729 }
2730
2731 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2732 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2733 "%ld.%06ld (%supdated in last scan)",
2734 bss->last_update.sec, bss->last_update.usec,
2735 updated ? "": "not ");
2736
2737 return updated;
2738}
2739
2740
ef922c4a
JM
2741static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2742 struct wpa_scan_results *scan_res)
b22128ef
JM
2743{
2744 struct wpa_bss *bss;
f8d0131a 2745 int freq;
54960629 2746 u8 iface_addr[ETH_ALEN];
b22128ef 2747
ef922c4a 2748 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef 2749
ef922c4a
JM
2750 if (wpa_s->global->p2p_disabled)
2751 return;
b22128ef 2752
b31be3a0
JM
2753 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2754 scan_res ? (int) scan_res->num : -1,
2755 wpa_s->p2p_auto_join ? "auto_" : "");
ef922c4a
JM
2756
2757 if (scan_res)
2758 wpas_p2p_scan_res_handler(wpa_s, scan_res);
b22128ef 2759
0918c4bf
JM
2760 if (wpa_s->p2p_auto_pd) {
2761 int join = wpas_p2p_peer_go(wpa_s,
2762 wpa_s->pending_join_dev_addr);
aa9bb764
JM
2763 if (join < 0)
2764 join = 0;
0918c4bf
JM
2765 wpa_s->p2p_auto_pd = 0;
2766 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2767 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2768 MAC2STR(wpa_s->pending_join_dev_addr), join);
2769 if (p2p_prov_disc_req(wpa_s->global->p2p,
2770 wpa_s->pending_join_dev_addr,
2771 wpa_s->pending_pd_config_methods, join,
2772 0) < 0) {
2773 wpa_s->p2p_auto_pd = 0;
2774 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2775 " p2p_dev_addr=" MACSTR " status=N/A",
2776 MAC2STR(wpa_s->pending_join_dev_addr));
2777 }
2778 return;
2779 }
2780
b31be3a0 2781 if (wpa_s->p2p_auto_join) {
aa9bb764
JM
2782 int join = wpas_p2p_peer_go(wpa_s,
2783 wpa_s->pending_join_dev_addr);
2784 if (join < 0) {
b31be3a0
JM
2785 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2786 "running a GO -> use GO Negotiation");
2787 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2788 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2789 wpa_s->p2p_persistent_group, 0, 0, 0,
2790 wpa_s->p2p_go_intent,
23c84252
JM
2791 wpa_s->p2p_connect_freq,
2792 wpa_s->p2p_persistent_id);
b31be3a0
JM
2793 return;
2794 }
2795
aa9bb764
JM
2796 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2797 "try to join the group", join ? "" :
2798 " in older scan");
2799 if (!join)
2800 wpa_s->p2p_fallback_to_go_neg = 1;
b31be3a0
JM
2801 }
2802
f8d0131a
JM
2803 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2804 wpa_s->pending_join_iface_addr);
54960629
AL
2805 if (freq < 0 &&
2806 p2p_get_interface_addr(wpa_s->global->p2p,
2807 wpa_s->pending_join_dev_addr,
2808 iface_addr) == 0 &&
2809 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2810 {
2811 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2812 "address for join from " MACSTR " to " MACSTR
2813 " based on newly discovered P2P peer entry",
2814 MAC2STR(wpa_s->pending_join_iface_addr),
2815 MAC2STR(iface_addr));
2816 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2817 ETH_ALEN);
2818
2819 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2820 wpa_s->pending_join_iface_addr);
2821 }
f8d0131a
JM
2822 if (freq >= 0) {
2823 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2824 "from P2P peer table: %d MHz", freq);
2825 }
ef922c4a 2826 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
b22128ef 2827 if (bss) {
f8d0131a
JM
2828 freq = bss->freq;
2829 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2830 "from BSS table: %d MHz", freq);
2831 }
2832 if (freq > 0) {
b22128ef
JM
2833 u16 method;
2834
4b156206
JM
2835 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2836 wpa_msg(wpa_s->parent, MSG_INFO,
2837 P2P_EVENT_GROUP_FORMATION_FAILURE
2838 "reason=FREQ_CONFLICT");
2839 return;
2840 }
2841
b22128ef
JM
2842 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2843 "prior to joining an existing group (GO " MACSTR
2844 " freq=%u MHz)",
f8d0131a 2845 MAC2STR(wpa_s->pending_join_dev_addr), freq);
b22128ef
JM
2846 wpa_s->pending_pd_before_join = 1;
2847
ef922c4a 2848 switch (wpa_s->pending_join_wps_method) {
b22128ef
JM
2849 case WPS_PIN_DISPLAY:
2850 method = WPS_CONFIG_KEYPAD;
2851 break;
2852 case WPS_PIN_KEYPAD:
2853 method = WPS_CONFIG_DISPLAY;
2854 break;
2855 case WPS_PBC:
2856 method = WPS_CONFIG_PUSHBUTTON;
2857 break;
2858 default:
2859 method = 0;
2860 break;
2861 }
2862
ec437d9e
JJ
2863 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2864 wpa_s->pending_join_dev_addr) ==
2865 method)) {
2866 /*
2867 * We have already performed provision discovery for
2868 * joining the group. Proceed directly to join
2869 * operation without duplicated provision discovery. */
e3a0706b 2870 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
ec437d9e
JJ
2871 "with " MACSTR " already done - proceed to "
2872 "join",
2873 MAC2STR(wpa_s->pending_join_dev_addr));
2874 wpa_s->pending_pd_before_join = 0;
2875 goto start;
2876 }
2877
ef922c4a 2878 if (p2p_prov_disc_req(wpa_s->global->p2p,
1ef2f7ff
JM
2879 wpa_s->pending_join_dev_addr, method, 1,
2880 freq) < 0) {
b22128ef
JM
2881 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2882 "Discovery Request before joining an "
2883 "existing group");
2884 wpa_s->pending_pd_before_join = 0;
2885 goto start;
2886 }
2887
2888 /*
2889 * Actual join operation will be started from the Action frame
f63b8542
JM
2890 * TX status callback (if no ACK is received) or when the
2891 * Provision Discovery Response is received. Use a short
2892 * timeout as a backup mechanism should the Provision Discovery
2893 * Response be lost for any reason.
b22128ef 2894 */
f63b8542
JM
2895 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2896 NULL);
2897 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2898 wpa_s, NULL);
ef922c4a 2899 return;
b22128ef
JM
2900 }
2901
9b1ab931
JM
2902 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2903 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2904 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2905 wpas_p2p_check_join_scan_limit(wpa_s);
2906 return;
b22128ef
JM
2907
2908start:
2909 /* Start join operation immediately */
ef922c4a
JM
2910 wpas_p2p_join_start(wpa_s);
2911}
2912
2913
2914static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2915{
2916 struct wpa_supplicant *wpa_s = eloop_ctx;
2917 int ret;
2918 struct wpa_driver_scan_params params;
2919 struct wpabuf *wps_ie, *ies;
206e1f42 2920 size_t ielen;
ef922c4a
JM
2921
2922 os_memset(&params, 0, sizeof(params));
2923
2924 /* P2P Wildcard SSID */
2925 params.num_ssids = 1;
2926 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2927 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2928
2929 wpa_s->wps->dev.p2p = 1;
360182ed
JM
2930 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
2931 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
2932 NULL);
ef922c4a
JM
2933 if (wps_ie == NULL) {
2934 wpas_p2p_scan_res_join(wpa_s, NULL);
2935 return;
2936 }
2937
206e1f42
JM
2938 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2939 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
ef922c4a
JM
2940 if (ies == NULL) {
2941 wpabuf_free(wps_ie);
2942 wpas_p2p_scan_res_join(wpa_s, NULL);
2943 return;
2944 }
2945 wpabuf_put_buf(ies, wps_ie);
2946 wpabuf_free(wps_ie);
2947
6d92fa6e 2948 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
ef922c4a 2949
47185fc7 2950 params.p2p_probe = 1;
ef922c4a
JM
2951 params.extra_ies = wpabuf_head(ies);
2952 params.extra_ies_len = wpabuf_len(ies);
2953
2954 /*
2955 * Run a scan to update BSS table and start Provision Discovery once
2956 * the new scan results become available.
2957 */
17fbb751 2958 ret = wpa_drv_scan(wpa_s, &params);
205e6474
JM
2959 if (!ret)
2960 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
ef922c4a
JM
2961
2962 wpabuf_free(ies);
2963
2964 if (ret) {
2965 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2966 "try again later");
2967 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2968 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
9b1ab931 2969 wpas_p2p_check_join_scan_limit(wpa_s);
ef922c4a
JM
2970 }
2971}
2972
2973
2974static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
b31be3a0
JM
2975 const u8 *dev_addr, enum p2p_wps_method wps_method,
2976 int auto_join)
ef922c4a
JM
2977{
2978 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
b31be3a0
JM
2979 MACSTR " dev " MACSTR ")%s",
2980 MAC2STR(iface_addr), MAC2STR(dev_addr),
2981 auto_join ? " (auto_join)" : "");
ef922c4a 2982
0918c4bf 2983 wpa_s->p2p_auto_pd = 0;
b31be3a0 2984 wpa_s->p2p_auto_join = !!auto_join;
ef922c4a
JM
2985 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2986 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2987 wpa_s->pending_join_wps_method = wps_method;
2988
2989 /* Make sure we are not running find during connection establishment */
2990 wpas_p2p_stop_find(wpa_s);
2991
9b1ab931 2992 wpa_s->p2p_join_scan_count = 0;
ef922c4a
JM
2993 wpas_p2p_join_scan(wpa_s, NULL);
2994 return 0;
b22128ef
JM
2995}
2996
2997
2998static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2999{
3000 struct wpa_supplicant *group;
3001 struct p2p_go_neg_results res;
8e64f258 3002 struct wpa_bss *bss;
b22128ef 3003
f63b8542 3004 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
b22128ef
JM
3005 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3006 if (group == NULL)
3007 return -1;
3c5126a4 3008 if (group != wpa_s) {
b22128ef
JM
3009 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3010 sizeof(group->p2p_pin));
3c5126a4
JM
3011 group->p2p_wps_method = wpa_s->p2p_wps_method;
3012 }
b22128ef
JM
3013
3014 group->p2p_in_provisioning = 1;
aa9bb764 3015 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
b22128ef
JM
3016
3017 os_memset(&res, 0, sizeof(res));
3018 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3019 ETH_ALEN);
3020 res.wps_method = wpa_s->pending_join_wps_method;
8e64f258
JM
3021 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3022 if (bss) {
3023 res.freq = bss->freq;
3024 res.ssid_len = bss->ssid_len;
3025 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3026 }
3027
e91829f9
JM
3028 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3029 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3030 "starting client");
3031 wpa_drv_cancel_remain_on_channel(wpa_s);
3032 wpa_s->off_channel_freq = 0;
3033 wpa_s->roc_waiting_drv_freq = 0;
3034 }
b22128ef
JM
3035 wpas_start_wps_enrollee(group, &res);
3036
3094d483
JM
3037 /*
3038 * Allow a longer timeout for join-a-running-group than normal 15
3039 * second group formation timeout since the GO may not have authorized
3040 * our connection yet.
3041 */
3042 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3043 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3044 wpa_s, NULL);
3045
b22128ef
JM
3046 return 0;
3047}
3048
3049
3050/**
3051 * wpas_p2p_connect - Request P2P Group Formation to be started
3052 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3053 * @peer_addr: Address of the peer P2P Device
3054 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3055 * @persistent_group: Whether to create a persistent group
b31be3a0 3056 * @auto_join: Whether to select join vs. GO Negotiation automatically
b22128ef
JM
3057 * @join: Whether to join an existing group (as a client) instead of starting
3058 * Group Owner negotiation; @peer_addr is BSSID in that case
3059 * @auth: Whether to only authorize the connection instead of doing that and
3060 * initiating Group Owner negotiation
3061 * @go_intent: GO Intent or -1 to use default
3062 * @freq: Frequency for the group or 0 for auto-selection
23c84252
JM
3063 * @persistent_id: Persistent group credentials to use for forcing GO
3064 * parameters or -1 to generate new values (SSID/passphrase)
d054a462
JM
3065 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3066 * failure, -2 on failure due to channel not currently available,
3067 * -3 if forced channel is not supported
b22128ef
JM
3068 */
3069int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3070 const char *pin, enum p2p_wps_method wps_method,
b31be3a0 3071 int persistent_group, int auto_join, int join, int auth,
23c84252 3072 int go_intent, int freq, int persistent_id)
b22128ef 3073{
d054a462 3074 int force_freq = 0, oper_freq = 0;
b22128ef
JM
3075 u8 bssid[ETH_ALEN];
3076 int ret = 0;
3077 enum wpa_driver_if_type iftype;
6cad95db 3078 const u8 *if_addr;
23c84252 3079 struct wpa_ssid *ssid = NULL;
b22128ef 3080
9526fd29
JM
3081 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3082 return -1;
3083
23c84252
JM
3084 if (persistent_id >= 0) {
3085 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3086 if (ssid == NULL || ssid->disabled != 2 ||
3087 ssid->mode != WPAS_MODE_P2P_GO)
3088 return -1;
3089 }
3090
b22128ef
JM
3091 if (go_intent < 0)
3092 go_intent = wpa_s->conf->p2p_go_intent;
3093
3094 if (!auth)
3095 wpa_s->p2p_long_listen = 0;
3096
3c5126a4 3097 wpa_s->p2p_wps_method = wps_method;
b31be3a0 3098 wpa_s->p2p_persistent_group = !!persistent_group;
23c84252 3099 wpa_s->p2p_persistent_id = persistent_id;
b31be3a0
JM
3100 wpa_s->p2p_go_intent = go_intent;
3101 wpa_s->p2p_connect_freq = freq;
aa9bb764 3102 wpa_s->p2p_fallback_to_go_neg = 0;
3c5126a4 3103
b22128ef
JM
3104 if (pin)
3105 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3106 else if (wps_method == WPS_PIN_DISPLAY) {
3107 ret = wps_generate_pin();
3108 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3109 ret);
3110 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3111 wpa_s->p2p_pin);
3112 } else
3113 wpa_s->p2p_pin[0] = '\0';
3114
b31be3a0 3115 if (join || auto_join) {
4147a2cc 3116 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
108def93
JM
3117 if (auth) {
3118 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3119 "connect a running group from " MACSTR,
3120 MAC2STR(peer_addr));
3121 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3122 return ret;
3123 }
4147a2cc 3124 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
b22128ef 3125 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4147a2cc 3126 iface_addr) < 0) {
b22128ef 3127 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4147a2cc
JM
3128 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3129 dev_addr);
3130 }
b31be3a0
JM
3131 if (auto_join) {
3132 os_get_time(&wpa_s->p2p_auto_started);
3133 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3134 "%ld.%06ld",
3135 wpa_s->p2p_auto_started.sec,
3136 wpa_s->p2p_auto_started.usec);
3137 }
3138 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3139 auto_join) < 0)
b22128ef
JM
3140 return -1;
3141 return ret;
3142 }
3143
d054a462
JM
3144 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3145 wpa_s->assoc_freq)
3146 oper_freq = wpa_s->assoc_freq;
b22128ef 3147 else {
d054a462
JM
3148 oper_freq = wpa_drv_shared_freq(wpa_s);
3149 if (oper_freq < 0)
3150 oper_freq = 0;
b22128ef
JM
3151 }
3152
d054a462
JM
3153 if (freq > 0) {
3154 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3155 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3156 "(%u MHz) is not supported for P2P uses",
3157 freq);
3158 return -3;
3159 }
3160
3161 if (oper_freq > 0 && freq != oper_freq &&
3162 !(wpa_s->drv_flags &
3163 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3164 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3165 "on %u MHz while connected on another "
3166 "channel (%u MHz)", freq, oper_freq);
3167 return -2;
3168 }
3169 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3170 "requested channel (%u MHz)", freq);
3171 force_freq = freq;
3172 } else if (oper_freq > 0 &&
3173 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3174 if (!(wpa_s->drv_flags &
3175 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3176 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3177 "while connected on non-P2P supported "
3178 "channel (%u MHz)", oper_freq);
3179 return -2;
3180 }
3181 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3182 "(%u MHz) not available for P2P - try to use "
3183 "another channel", oper_freq);
3184 force_freq = 0;
3185 } else if (oper_freq > 0) {
b22128ef
JM
3186 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3187 "channel we are already using (%u MHz) on another "
d054a462
JM
3188 "interface", oper_freq);
3189 force_freq = oper_freq;
b22128ef
JM
3190 }
3191
3192 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3193
6cad95db
JB
3194 if (wpa_s->create_p2p_iface) {
3195 /* Prepare to add a new interface for the group */
3196 iftype = WPA_IF_P2P_GROUP;
3197 if (go_intent == 15)
3198 iftype = WPA_IF_P2P_GO;
3199 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3200 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3201 "interface for the group");
b22128ef 3202 return -1;
6cad95db 3203 }
b22128ef 3204
6cad95db
JB
3205 if_addr = wpa_s->pending_interface_addr;
3206 } else
3207 if_addr = wpa_s->own_addr;
b22128ef
JM
3208
3209 if (auth) {
3210 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6cad95db 3211 go_intent, if_addr,
23c84252
JM
3212 force_freq, persistent_group, ssid) <
3213 0)
b22128ef
JM
3214 return -1;
3215 return ret;
3216 }
6cad95db
JB
3217
3218 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3219 go_intent, if_addr, force_freq,
23c84252 3220 persistent_group, ssid) < 0) {
6cad95db
JB
3221 if (wpa_s->create_p2p_iface)
3222 wpas_p2p_remove_pending_group_interface(wpa_s);
b22128ef
JM
3223 return -1;
3224 }
3225 return ret;
3226}
3227
3228
3229/**
3230 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3231 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3232 * @freq: Frequency of the channel in MHz
3233 * @duration: Duration of the stay on the channel in milliseconds
3234 *
3235 * This callback is called when the driver indicates that it has started the
3236 * requested remain-on-channel duration.
3237 */
3238void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3239 unsigned int freq, unsigned int duration)
3240{
9526fd29
JM
3241 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3242 return;
b22128ef
JM
3243 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3244 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3245 wpa_s->pending_listen_duration);
3246 wpa_s->pending_listen_freq = 0;
3247 }
3248}
3249
3250
3251static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3252 unsigned int timeout)
3253{
3254 /* Limit maximum Listen state time based on driver limitation. */
3255 if (timeout > wpa_s->max_remain_on_chan)
3256 timeout = wpa_s->max_remain_on_chan;
3257
3ac17eba
JM
3258 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3259 return wpa_drv_p2p_listen(wpa_s, timeout);
3260
b22128ef
JM
3261 return p2p_listen(wpa_s->global->p2p, timeout);
3262}
3263
3264
3265/**
3266 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3267 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3268 * @freq: Frequency of the channel in MHz
3269 *
3270 * This callback is called when the driver indicates that a remain-on-channel
3271 * operation has been completed, i.e., the duration on the requested channel
3272 * has timed out.
3273 */
3274void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3275 unsigned int freq)
3276{
1cc3a29d 3277 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
11fb02be 3278 "(p2p_long_listen=%d ms pending_action_tx=%p)",
1cc3a29d 3279 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
9526fd29
JM
3280 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3281 return;
b22128ef
JM
3282 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3283 return; /* P2P module started a new operation */
3284 if (wpa_s->pending_action_tx)
3285 return;
3286 if (wpa_s->p2p_long_listen > 0)
11fb02be 3287 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
b22128ef
JM
3288 if (wpa_s->p2p_long_listen > 0) {
3289 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
11fb02be 3290 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
b22128ef
JM
3291 }
3292}
3293
3294
3295/**
3296 * wpas_p2p_group_remove - Remove a P2P group
3297 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3298 * @ifname: Network interface name of the group interface or "*" to remove all
3299 * groups
3300 * Returns: 0 on success, -1 on failure
3301 *
3302 * This function is used to remove a P2P group. This can be used to disconnect
3303 * from a group in which the local end is a P2P Client or to end a P2P Group in
3304 * case the local end is the Group Owner. If a virtual network interface was
3305 * created for this group, that interface will be removed. Otherwise, only the
3306 * configured P2P group network will be removed from the interface.
3307 */
3308int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3309{
3310 struct wpa_global *global = wpa_s->global;
3311
3312 if (os_strcmp(ifname, "*") == 0) {
3313 struct wpa_supplicant *prev;
3314 wpa_s = global->ifaces;
3315 while (wpa_s) {
3316 prev = wpa_s;
3317 wpa_s = wpa_s->next;
3103f345 3318 wpas_p2p_disconnect(prev);
b22128ef
JM
3319 }
3320 return 0;
3321 }
3322
3323 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3324 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3325 break;
3326 }
3327
3103f345 3328 return wpas_p2p_disconnect(wpa_s);
b22128ef
JM
3329}
3330
3331
b8349523
NKG
3332static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3333 struct p2p_go_neg_results *params,
3334 int freq)
b22128ef
JM
3335{
3336 u8 bssid[ETH_ALEN];
3337 int res;
3338
3339 os_memset(params, 0, sizeof(*params));
3340 params->role_go = 1;
7cfc4ac3
AGS
3341 if (freq) {
3342 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3343 "frequency %d MHz", freq);
b22128ef 3344 params->freq = freq;
7cfc4ac3
AGS
3345 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3346 wpa_s->conf->p2p_oper_channel >= 1 &&
3347 wpa_s->conf->p2p_oper_channel <= 11) {
b22128ef 3348 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3349 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3350 "frequency %d MHz", params->freq);
3351 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
64f99265 3352 wpa_s->conf->p2p_oper_reg_class == 124) {
b22128ef 3353 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3354 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3355 "frequency %d MHz", params->freq);
3356 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3357 wpa_s->best_overall_freq > 0 &&
3358 p2p_supported_freq(wpa_s->global->p2p,
3359 wpa_s->best_overall_freq)) {
3360 params->freq = wpa_s->best_overall_freq;
3361 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3362 "channel %d MHz", params->freq);
3363 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3364 wpa_s->best_24_freq > 0 &&
3365 p2p_supported_freq(wpa_s->global->p2p,
3366 wpa_s->best_24_freq)) {
3367 params->freq = wpa_s->best_24_freq;
3368 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3369 "channel %d MHz", params->freq);
3370 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3371 wpa_s->best_5_freq > 0 &&
3372 p2p_supported_freq(wpa_s->global->p2p,
3373 wpa_s->best_5_freq)) {
3374 params->freq = wpa_s->best_5_freq;
3375 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3376 "channel %d MHz", params->freq);
3377 } else {
6f3bc72b
JM
3378 int chan;
3379 for (chan = 0; chan < 11; chan++) {
3380 params->freq = 2412 + chan * 5;
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3382 params->freq))
3383 break;
3384 }
3385 if (chan == 11) {
3386 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3387 "allowed");
3388 return -1;
3389 }
7cfc4ac3
AGS
3390 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3391 "known)", params->freq);
3392 }
3393
b22128ef
JM
3394 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3395 wpa_s->assoc_freq && !freq) {
3396 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3397 "already using");
3398 params->freq = wpa_s->assoc_freq;
3399 }
3400
3401 res = wpa_drv_shared_freq(wpa_s);
3402 if (res > 0 && !freq) {
3403 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3404 "already using on a shared interface");
3405 params->freq = res;
b8349523
NKG
3406 } else if (res > 0 && freq != res &&
3407 !(wpa_s->drv_flags &
3408 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3409 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3410 "while connected on another channel (%u MHz)",
3411 freq, res);
3412 return -1;
b22128ef 3413 }
b8349523
NKG
3414
3415 return 0;
b22128ef
JM
3416}
3417
3418
3419static struct wpa_supplicant *
3420wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3421 int go)
3422{
3423 struct wpa_supplicant *group_wpa_s;
3424
3425 if (!wpas_p2p_create_iface(wpa_s))
3426 return wpa_s;
3427
3428 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3429 WPA_IF_P2P_CLIENT) < 0)
3430 return NULL;
3431 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3432 if (group_wpa_s == NULL) {
3433 wpas_p2p_remove_pending_group_interface(wpa_s);
3434 return NULL;
3435 }
3436
3437 return group_wpa_s;
3438}
3439
3440
3441/**
3442 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3443 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3444 * @persistent_group: Whether to create a persistent group
3445 * @freq: Frequency for the group or 0 to indicate no hardcoding
3446 * Returns: 0 on success, -1 on failure
3447 *
3448 * This function creates a new P2P group with the local end as the Group Owner,
3449 * i.e., without using Group Owner Negotiation.
3450 */
3451int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3452 int freq)
3453{
3454 struct p2p_go_neg_results params;
7cfc4ac3
AGS
3455 unsigned int r;
3456
9526fd29
JM
3457 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3458 return -1;
3459
2d4f15d6
JJ
3460 /* Make sure we are not running find during connection establishment */
3461 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3462 wpas_p2p_stop_find(wpa_s);
3463
7cfc4ac3
AGS
3464 if (freq == 2) {
3465 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3466 "band");
3467 if (wpa_s->best_24_freq > 0 &&
3468 p2p_supported_freq(wpa_s->global->p2p,
3469 wpa_s->best_24_freq)) {
3470 freq = wpa_s->best_24_freq;
3471 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3472 "channel: %d MHz", freq);
3473 } else {
3474 os_get_random((u8 *) &r, sizeof(r));
3475 freq = 2412 + (r % 3) * 25;
3476 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3477 "channel: %d MHz", freq);
3478 }
3479 }
3480
3481 if (freq == 5) {
3482 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3483 "band");
3484 if (wpa_s->best_5_freq > 0 &&
3485 p2p_supported_freq(wpa_s->global->p2p,
3486 wpa_s->best_5_freq)) {
3487 freq = wpa_s->best_5_freq;
3488 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3489 "channel: %d MHz", freq);
3490 } else {
3491 os_get_random((u8 *) &r, sizeof(r));
3492 freq = 5180 + (r % 4) * 20;
3493 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3494 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3495 "5 GHz channel for P2P group");
3496 return -1;
3497 }
3498 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3499 "channel: %d MHz", freq);
3500 }
3501 }
b22128ef 3502
4ae4650b
JM
3503 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3504 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3505 "(%u MHz) is not supported for P2P uses",
3506 freq);
3507 return -1;
3508 }
3509
b8349523
NKG
3510 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3511 return -1;
f4329aa2
JM
3512 if (params.freq &&
3513 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3514 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3515 "(%u MHz) is not supported for P2P uses",
3516 params.freq);
3517 return -1;
3518 }
b22128ef
JM
3519 p2p_go_params(wpa_s->global->p2p, &params);
3520 params.persistent_group = persistent_group;
3521
3522 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3523 if (wpa_s == NULL)
3524 return -1;
3525 wpas_start_wps_go(wpa_s, &params, 0);
3526
3527 return 0;
3528}
3529
3530
3531static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3532 struct wpa_ssid *params, int addr_allocated)
3533{
3534 struct wpa_ssid *ssid;
3535
3536 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3537 if (wpa_s == NULL)
3538 return -1;
3539
3540 wpa_supplicant_ap_deinit(wpa_s);
3541
3542 ssid = wpa_config_add_network(wpa_s->conf);
3543 if (ssid == NULL)
3544 return -1;
b22128ef
JM
3545 wpa_config_set_network_defaults(ssid);
3546 ssid->temporary = 1;
3547 ssid->proto = WPA_PROTO_RSN;
3548 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3549 ssid->group_cipher = WPA_CIPHER_CCMP;
3550 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3551 ssid->ssid = os_malloc(params->ssid_len);
3552 if (ssid->ssid == NULL) {
b22128ef
JM
3553 wpa_config_remove_network(wpa_s->conf, ssid->id);
3554 return -1;
3555 }
3556 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3557 ssid->ssid_len = params->ssid_len;
3558 ssid->p2p_group = 1;
d1c8ac88 3559 ssid->export_keys = 1;
b22128ef
JM
3560 if (params->psk_set) {
3561 os_memcpy(ssid->psk, params->psk, 32);
3562 ssid->psk_set = 1;
3563 }
3564 if (params->passphrase)
3565 ssid->passphrase = os_strdup(params->passphrase);
3566
3567 wpa_supplicant_select_network(wpa_s, ssid);
3568
3569 wpa_s->show_group_started = 1;
3570
3571 return 0;
3572}
3573
3574
3575int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3576 struct wpa_ssid *ssid, int addr_allocated,
3577 int freq)
3578{
3579 struct p2p_go_neg_results params;
6c0da49f 3580 int go = 0;
b22128ef
JM
3581
3582 if (ssid->disabled != 2 || ssid->ssid == NULL)
3583 return -1;
3584
6c0da49f
JM
3585 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3586 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3587 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3588 "already running");
3589 return 0;
3590 }
3591
9d39057c
JM
3592 /* Make sure we are not running find during connection establishment */
3593 wpas_p2p_stop_find(wpa_s);
b22128ef 3594
aa9bb764
JM
3595 wpa_s->p2p_fallback_to_go_neg = 0;
3596
b22128ef
JM
3597 if (ssid->mode == WPAS_MODE_INFRA)
3598 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3599
3600 if (ssid->mode != WPAS_MODE_P2P_GO)
3601 return -1;
3602
b8349523
NKG
3603 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3604 return -1;
b22128ef
JM
3605
3606 params.role_go = 1;
3607 if (ssid->passphrase == NULL ||
3608 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3609 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3610 "group");
3611 return -1;
3612 }
3613 os_strlcpy(params.passphrase, ssid->passphrase,
3614 sizeof(params.passphrase));
3615 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3616 params.ssid_len = ssid->ssid_len;
3617 params.persistent_group = 1;
3618
3619 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3620 if (wpa_s == NULL)
3621 return -1;
3622
3623 wpas_start_wps_go(wpa_s, &params, 0);
3624
3625 return 0;
3626}
3627
3628
3629static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3630 struct wpabuf *proberesp_ies)
3631{
3632 struct wpa_supplicant *wpa_s = ctx;
3633 if (wpa_s->ap_iface) {
3634 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
adc33680
JM
3635 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3636 wpabuf_free(beacon_ies);
3637 wpabuf_free(proberesp_ies);
3638 return;
3639 }
b22128ef
JM
3640 if (beacon_ies) {
3641 wpabuf_free(hapd->p2p_beacon_ie);
3642 hapd->p2p_beacon_ie = beacon_ies;
3643 }
3644 wpabuf_free(hapd->p2p_probe_resp_ie);
3645 hapd->p2p_probe_resp_ie = proberesp_ies;
3646 } else {
3647 wpabuf_free(beacon_ies);
3648 wpabuf_free(proberesp_ies);
3649 }
3650 wpa_supplicant_ap_update_beacon(wpa_s);
3651}
3652
3653
3071e181
JM
3654static void wpas_p2p_idle_update(void *ctx, int idle)
3655{
3656 struct wpa_supplicant *wpa_s = ctx;
3657 if (!wpa_s->ap_iface)
3658 return;
3659 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3660 if (idle)
3661 wpas_p2p_set_group_idle_timeout(wpa_s);
3662 else
3663 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3664}
3665
3666
b22128ef 3667struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6f251b6b 3668 struct wpa_ssid *ssid)
b22128ef
JM
3669{
3670 struct p2p_group *group;
3671 struct p2p_group_config *cfg;
3672
3ac17eba
JM
3673 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3674 return NULL;
9526fd29
JM
3675 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3676 return NULL;
3ac17eba 3677
b22128ef
JM
3678 cfg = os_zalloc(sizeof(*cfg));
3679 if (cfg == NULL)
3680 return NULL;
3681
6f251b6b 3682 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
acc247b2 3683 cfg->persistent_group = 2;
6f251b6b 3684 else if (ssid->p2p_persistent_group)
acc247b2 3685 cfg->persistent_group = 1;
b22128ef 3686 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3f4ce13f
JM
3687 if (wpa_s->max_stations &&
3688 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3689 cfg->max_clients = wpa_s->max_stations;
3690 else
3691 cfg->max_clients = wpa_s->conf->max_num_sta;
6f251b6b
JM
3692 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3693 cfg->ssid_len = ssid->ssid_len;
b22128ef
JM
3694 cfg->cb_ctx = wpa_s;
3695 cfg->ie_update = wpas_p2p_ie_update;
3071e181 3696 cfg->idle_update = wpas_p2p_idle_update;
b22128ef
JM
3697
3698 group = p2p_group_init(wpa_s->global->p2p, cfg);
3699 if (group == NULL)
3700 os_free(cfg);
6f251b6b 3701 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
b22128ef
JM
3702 p2p_group_notif_formation_done(group);
3703 wpa_s->p2p_group = group;
3704 return group;
3705}
3706
3707
3708void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3709 int registrar)
3710{
10531d21
JM
3711 struct wpa_ssid *ssid = wpa_s->current_ssid;
3712
b22128ef
JM
3713 if (!wpa_s->p2p_in_provisioning) {
3714 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3715 "provisioning not in progress");
3716 return;
3717 }
3718
10531d21
JM
3719 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3720 u8 go_dev_addr[ETH_ALEN];
3721 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3722 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3723 ssid->ssid_len);
3724 /* Clear any stored provisioning info */
3725 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3726 }
ec437d9e 3727
b22128ef
JM
3728 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3729 NULL);
361cdf34
JM
3730 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3731 /*
3732 * Use a separate timeout for initial data connection to
3733 * complete to allow the group to be removed automatically if
3734 * something goes wrong in this step before the P2P group idle
3735 * timeout mechanism is taken into use.
3736 */
3737 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3738 wpas_p2p_group_formation_timeout,
3739 wpa_s, NULL);
3740 }
b22128ef
JM
3741 if (wpa_s->global->p2p)
3742 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3ac17eba
JM
3743 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3744 wpa_drv_wps_success_cb(wpa_s, peer_addr);
b22128ef
JM
3745 wpas_group_formation_completed(wpa_s, 1);
3746}
3747
3748
3734552f
JS
3749void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3750 struct wps_event_fail *fail)
3751{
3752 if (!wpa_s->p2p_in_provisioning) {
3753 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3754 "provisioning not in progress");
3755 return;
3756 }
ec437d9e
JJ
3757
3758 if (wpa_s->go_params) {
3759 p2p_clear_provisioning_info(
3760 wpa_s->global->p2p,
10531d21 3761 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
3762 }
3763
3734552f
JS
3764 wpas_notify_p2p_wps_failed(wpa_s, fail);
3765}
3766
3767
b22128ef 3768int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
0918c4bf
JM
3769 const char *config_method,
3770 enum wpas_p2p_prov_disc_use use)
b22128ef
JM
3771{
3772 u16 config_methods;
3773
aa9bb764 3774 wpa_s->p2p_fallback_to_go_neg = 0;
0918c4bf 3775 wpa_s->pending_pd_use = NORMAL_PD;
8c5f7309 3776 if (os_strncmp(config_method, "display", 7) == 0)
b22128ef 3777 config_methods = WPS_CONFIG_DISPLAY;
8c5f7309 3778 else if (os_strncmp(config_method, "keypad", 6) == 0)
b22128ef 3779 config_methods = WPS_CONFIG_KEYPAD;
8c5f7309
JJ
3780 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3781 os_strncmp(config_method, "pushbutton", 10) == 0)
b22128ef 3782 config_methods = WPS_CONFIG_PUSHBUTTON;
8c5f7309
JJ
3783 else {
3784 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
b22128ef 3785 return -1;
8c5f7309 3786 }
b22128ef 3787
0918c4bf
JM
3788 if (use == WPAS_P2P_PD_AUTO) {
3789 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3790 wpa_s->pending_pd_config_methods = config_methods;
3791 wpa_s->p2p_auto_pd = 1;
3792 wpa_s->p2p_auto_join = 0;
3793 wpa_s->pending_pd_before_join = 0;
3794 wpas_p2p_stop_find(wpa_s);
3795 wpa_s->p2p_join_scan_count = 0;
3796 wpas_p2p_join_scan(wpa_s, NULL);
3797 return 0;
3798 }
3799
3ac17eba
JM
3800 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3801 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
0918c4bf
JM
3802 config_methods,
3803 use == WPAS_P2P_PD_FOR_JOIN);
3ac17eba
JM
3804 }
3805
9526fd29 3806 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef
JM
3807 return -1;
3808
3809 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
0918c4bf
JM
3810 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3811 0);
b22128ef
JM
3812}
3813
3814
3815int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3816 char *end)
3817{
3818 return p2p_scan_result_text(ies, ies_len, buf, end);
3819}
3820
3821
1cc3a29d
JM
3822static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3823{
3824 if (!wpa_s->pending_action_tx)
3825 return;
3826
3827 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3828 "operation request");
3829 wpabuf_free(wpa_s->pending_action_tx);
3830 wpa_s->pending_action_tx = NULL;
3831}
3832
3833
b22128ef 3834int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
046ef4aa 3835 enum p2p_discovery_type type,
6d92fa6e
JM
3836 unsigned int num_req_dev_types, const u8 *req_dev_types,
3837 const u8 *dev_id)
b22128ef 3838{
1cc3a29d 3839 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef
JM
3840 wpa_s->p2p_long_listen = 0;
3841
3ac17eba
JM
3842 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3843 return wpa_drv_p2p_find(wpa_s, timeout, type);
3844
9526fd29
JM
3845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3846 return -1;
3847
433cd2ce
JM
3848 wpa_supplicant_cancel_sched_scan(wpa_s);
3849
046ef4aa 3850 return p2p_find(wpa_s->global->p2p, timeout, type,
6d92fa6e 3851 num_req_dev_types, req_dev_types, dev_id);
b22128ef
JM
3852}
3853
3854
3855void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3856{
1cc3a29d 3857 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef 3858 wpa_s->p2p_long_listen = 0;
9d39057c 3859 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
ef922c4a 3860 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
39185dfa 3861 wpa_s->p2p_cb_on_scan_complete = 0;
b22128ef 3862
3ac17eba
JM
3863 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3864 wpa_drv_p2p_stop_find(wpa_s);
3865 return;
3866 }
3867
9526fd29
JM
3868 if (wpa_s->global->p2p)
3869 p2p_stop_find(wpa_s->global->p2p);
b22128ef
JM
3870
3871 wpas_p2p_remove_pending_group_interface(wpa_s);
3872}
3873
3874
3875static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3876{
3877 struct wpa_supplicant *wpa_s = eloop_ctx;
3878 wpa_s->p2p_long_listen = 0;
3879}
3880
3881
3882int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3883{
3884 int res;
3885
9526fd29
JM
3886 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3887 return -1;
3888
433cd2ce 3889 wpa_supplicant_cancel_sched_scan(wpa_s);
1cc3a29d
JM
3890 wpas_p2p_clear_pending_action_tx(wpa_s);
3891
b22128ef
JM
3892 if (timeout == 0) {
3893 /*
3894 * This is a request for unlimited Listen state. However, at
3895 * least for now, this is mapped to a Listen state for one
3896 * hour.
3897 */
3898 timeout = 3600;
3899 }
3900 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3901 wpa_s->p2p_long_listen = 0;
3902
2bb747e2
JM
3903 /*
3904 * Stop previous find/listen operation to avoid trying to request a new
3905 * remain-on-channel operation while the driver is still running the
3906 * previous one.
3907 */
3908 if (wpa_s->global->p2p)
3909 p2p_stop_find(wpa_s->global->p2p);
3910
b22128ef
JM
3911 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3912 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
11fb02be 3913 wpa_s->p2p_long_listen = timeout * 1000;
b22128ef
JM
3914 eloop_register_timeout(timeout, 0,
3915 wpas_p2p_long_listen_timeout,
3916 wpa_s, NULL);
3917 }
3918
3919 return res;
3920}
3921
3922
4c08c0bd 3923int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
b22128ef
JM
3924 u8 *buf, size_t len, int p2p_group)
3925{
4c08c0bd
JM
3926 struct wpabuf *p2p_ie;
3927 int ret;
3928
b22128ef
JM
3929 if (wpa_s->global->p2p_disabled)
3930 return -1;
3931 if (wpa_s->global->p2p == NULL)
3932 return -1;
e1f1509b
JM
3933 if (bss == NULL)
3934 return -1;
b22128ef 3935
4c08c0bd
JM
3936 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3937 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3938 p2p_group, p2p_ie);
3939 wpabuf_free(p2p_ie);
3940
3941 return ret;
b22128ef
JM
3942}
3943
3944
3945int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
04a85e44 3946 const u8 *dst, const u8 *bssid,
baf513d6 3947 const u8 *ie, size_t ie_len, int ssi_signal)
b22128ef
JM
3948{
3949 if (wpa_s->global->p2p_disabled)
3950 return 0;
3951 if (wpa_s->global->p2p == NULL)
3952 return 0;
3953
2d43d37f
JB
3954 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3955 ie, ie_len)) {
3956 case P2P_PREQ_NOT_P2P:
3957 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
3958 ssi_signal);
3959 /* fall through */
3960 case P2P_PREQ_MALFORMED:
3961 case P2P_PREQ_NOT_LISTEN:
3962 case P2P_PREQ_NOT_PROCESSED:
3963 default: /* make gcc happy */
3964 return 0;
3965 case P2P_PREQ_PROCESSED:
3966 return 1;
3967 }
b22128ef
JM
3968}
3969
3970
3971void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3972 const u8 *sa, const u8 *bssid,
3973 u8 category, const u8 *data, size_t len, int freq)
3974{
3975 if (wpa_s->global->p2p_disabled)
3976 return;
3977 if (wpa_s->global->p2p == NULL)
3978 return;
3979
3980 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3981 freq);
3982}
3983
3984
3985void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3986{
3987 if (wpa_s->global->p2p_disabled)
3988 return;
3989 if (wpa_s->global->p2p == NULL)
3990 return;
3991
6d92fa6e 3992 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
b22128ef
JM
3993}
3994
3995
3996void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3997{
3998 p2p_group_deinit(wpa_s->p2p_group);
3999 wpa_s->p2p_group = NULL;
a7fd39bb
JD
4000
4001 wpa_s->ap_configured_cb = NULL;
4002 wpa_s->ap_configured_cb_ctx = NULL;
4003 wpa_s->ap_configured_cb_data = NULL;
4004 wpa_s->connect_without_scan = NULL;
b22128ef
JM
4005}
4006
4007
4008int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4009{
4010 wpa_s->p2p_long_listen = 0;
4011
3ac17eba
JM
4012 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4013 return wpa_drv_p2p_reject(wpa_s, addr);
4014
9526fd29
JM
4015 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4016 return -1;
4017
b22128ef
JM
4018 return p2p_reject(wpa_s->global->p2p, addr);
4019}
4020
4021
4022/* Invite to reinvoke a persistent group */
4023int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4024 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4025{
4026 enum p2p_invite_role role;
4027 u8 *bssid = NULL;
4028
4029 if (ssid->mode == WPAS_MODE_P2P_GO) {
4030 role = P2P_INVITE_ROLE_GO;
4031 if (peer_addr == NULL) {
4032 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4033 "address in invitation command");
4034 return -1;
4035 }
4036 if (wpas_p2p_create_iface(wpa_s)) {
4037 if (wpas_p2p_add_group_interface(wpa_s,
4038 WPA_IF_P2P_GO) < 0) {
4039 wpa_printf(MSG_ERROR, "P2P: Failed to "
4040 "allocate a new interface for the "
4041 "group");
4042 return -1;
4043 }
4044 bssid = wpa_s->pending_interface_addr;
4045 } else
4046 bssid = wpa_s->own_addr;
4047 } else {
4048 role = P2P_INVITE_ROLE_CLIENT;
4049 peer_addr = ssid->bssid;
4050 }
4051 wpa_s->pending_invite_ssid_id = ssid->id;
4052
3ac17eba
JM
4053 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4054 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4055 ssid->ssid, ssid->ssid_len,
4056 go_dev_addr, 1);
4057
9526fd29
JM
4058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4059 return -1;
4060
b22128ef
JM
4061 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4062 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4063}
4064
4065
4066/* Invite to join an active group */
4067int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4068 const u8 *peer_addr, const u8 *go_dev_addr)
4069{
4070 struct wpa_global *global = wpa_s->global;
4071 enum p2p_invite_role role;
4072 u8 *bssid = NULL;
4073 struct wpa_ssid *ssid;
c427ac92 4074 int persistent;
b22128ef
JM
4075
4076 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4077 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4078 break;
4079 }
4080 if (wpa_s == NULL) {
4081 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4082 return -1;
4083 }
4084
4085 ssid = wpa_s->current_ssid;
4086 if (ssid == NULL) {
4087 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4088 "invitation");
4089 return -1;
4090 }
4091
c427ac92
JM
4092 persistent = ssid->p2p_persistent_group &&
4093 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4094 ssid->ssid, ssid->ssid_len);
4095
b22128ef
JM
4096 if (ssid->mode == WPAS_MODE_P2P_GO) {
4097 role = P2P_INVITE_ROLE_ACTIVE_GO;
4098 bssid = wpa_s->own_addr;
4099 if (go_dev_addr == NULL)
d7e70476 4100 go_dev_addr = wpa_s->global->p2p_dev_addr;
b22128ef
JM
4101 } else {
4102 role = P2P_INVITE_ROLE_CLIENT;
4103 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4104 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4105 "invite to current group");
4106 return -1;
4107 }
4108 bssid = wpa_s->bssid;
4109 if (go_dev_addr == NULL &&
4110 !is_zero_ether_addr(wpa_s->go_dev_addr))
4111 go_dev_addr = wpa_s->go_dev_addr;
4112 }
bb79dc72 4113 wpa_s->parent->pending_invite_ssid_id = -1;
b22128ef 4114
3ac17eba
JM
4115 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4116 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4117 ssid->ssid, ssid->ssid_len,
c427ac92 4118 go_dev_addr, persistent);
3ac17eba 4119
9526fd29
JM
4120 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4121 return -1;
4122
b22128ef 4123 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
d9d6a58c 4124 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
c427ac92 4125 go_dev_addr, persistent);
b22128ef
JM
4126}
4127
4128
4129void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4130{
4131 struct wpa_ssid *ssid = wpa_s->current_ssid;
4132 const char *ssid_txt;
4133 u8 go_dev_addr[ETH_ALEN];
4b6baa2f 4134 int network_id = -1;
b22128ef 4135 int persistent;
b49d6ccb 4136 int freq;
b22128ef 4137
361cdf34
JM
4138 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4139 NULL);
4140
b22128ef
JM
4141 if (!wpa_s->show_group_started || !ssid)
4142 return;
4143
4144 wpa_s->show_group_started = 0;
4145
4146 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4147 os_memset(go_dev_addr, 0, ETH_ALEN);
4148 if (ssid->bssid_set)
4149 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4150 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4151 ssid->ssid_len);
4152 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4153
a0a9f3b0
JM
4154 if (wpa_s->global->p2p_group_formation == wpa_s)
4155 wpa_s->global->p2p_group_formation = NULL;
4156
b49d6ccb
JJ
4157 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4158 (int) wpa_s->assoc_freq;
b22128ef
JM
4159 if (ssid->passphrase == NULL && ssid->psk_set) {
4160 char psk[65];
4161 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4162 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4163 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4164 MACSTR "%s",
b49d6ccb 4165 wpa_s->ifname, ssid_txt, freq, psk,
c481048f 4166 MAC2STR(go_dev_addr),
b22128ef
JM
4167 persistent ? " [PERSISTENT]" : "");
4168 } else {
4169 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4170 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4171 "go_dev_addr=" MACSTR "%s",
b49d6ccb 4172 wpa_s->ifname, ssid_txt, freq,
b22128ef
JM
4173 ssid->passphrase ? ssid->passphrase : "",
4174 MAC2STR(go_dev_addr),
4175 persistent ? " [PERSISTENT]" : "");
4176 }
4177
4178 if (persistent)
4b6baa2f
JMB
4179 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4180 ssid, go_dev_addr);
4181 if (network_id < 0)
4182 network_id = ssid->id;
4183 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
b22128ef
JM
4184}
4185
4186
4187int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4188 u32 interval1, u32 duration2, u32 interval2)
4189{
3ac17eba
JM
4190 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4191 return -1;
9526fd29
JM
4192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4193 return -1;
3ac17eba 4194
b22128ef
JM
4195 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4196 wpa_s->current_ssid == NULL ||
4197 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4198 return -1;
4199
4200 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4201 wpa_s->own_addr, wpa_s->assoc_freq,
4202 duration1, interval1, duration2, interval2);
4203}
4204
4205
4206int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4207 unsigned int interval)
4208{
3ac17eba
JM
4209 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4210 return -1;
4211
9526fd29
JM
4212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4213 return -1;
4214
b22128ef
JM
4215 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4216}
4217
4218
c8106615
JM
4219static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4220{
4221 return wpa_s->current_ssid != NULL &&
4222 wpa_s->current_ssid->p2p_group &&
4223 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4224}
4225
4226
3071e181
JM
4227static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4228{
4229 struct wpa_supplicant *wpa_s = eloop_ctx;
4230
c8106615 4231 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
3071e181
JM
4232 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4233 "disabled");
4234 return;
4235 }
4236
4237 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4238 "group");
4239 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
aa9bb764 4240 wpas_p2p_group_delete(wpa_s, 0);
3071e181
JM
4241}
4242
4243
4244static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4245{
5f482d55 4246 int timeout;
c8106615 4247
dddc7045
JM
4248 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4249 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4250
c8106615 4251 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3071e181
JM
4252 return;
4253
c8106615
JM
4254 timeout = wpa_s->conf->p2p_group_idle;
4255 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4256 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4257 timeout = P2P_MAX_CLIENT_IDLE;
4258
4259 if (timeout == 0)
3071e181
JM
4260 return;
4261
5f482d55
JM
4262 if (timeout < 0) {
4263 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4264 timeout = 0; /* special client mode no-timeout */
4265 else
4266 return;
4267 }
4268
8c472816
JM
4269 if (wpa_s->p2p_in_provisioning) {
4270 /*
4271 * Use the normal group formation timeout during the
4272 * provisioning phase to avoid terminating this process too
4273 * early due to group idle timeout.
4274 */
4275 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4276 "during provisioning");
4277 return;
4278 }
4279
361cdf34
JM
4280 if (wpa_s->show_group_started) {
4281 /*
4282 * Use the normal group formation timeout between the end of
4283 * the provisioning phase and completion of 4-way handshake to
4284 * avoid terminating this process too early due to group idle
4285 * timeout.
4286 */
4287 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4288 "while waiting for initial 4-way handshake to "
4289 "complete");
4290 return;
4291 }
4292
3071e181 4293 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
c8106615
JM
4294 timeout);
4295 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4296 wpa_s, NULL);
3071e181
JM
4297}
4298
4299
b22128ef 4300void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3fc14102
JM
4301 u16 reason_code, const u8 *ie, size_t ie_len,
4302 int locally_generated)
b22128ef 4303{
9526fd29 4304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 4305 return;
3ac17eba
JM
4306 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4307 return;
b22128ef 4308
3fc14102
JM
4309 if (!locally_generated)
4310 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4311 ie_len);
4312
4313 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4314 wpa_s->current_ssid &&
4315 wpa_s->current_ssid->p2p_group &&
4316 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4317 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4318 "session is ending");
4319 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
aa9bb764 4320 wpas_p2p_group_delete(wpa_s, 0);
3fc14102 4321 }
b22128ef
JM
4322}
4323
4324
4325void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3fc14102
JM
4326 u16 reason_code, const u8 *ie, size_t ie_len,
4327 int locally_generated)
b22128ef 4328{
9526fd29 4329 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 4330 return;
3ac17eba
JM
4331 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4332 return;
b22128ef 4333
3fc14102
JM
4334 if (!locally_generated)
4335 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4336 ie_len);
b22128ef
JM
4337}
4338
4339
4340void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4341{
4342 struct p2p_data *p2p = wpa_s->global->p2p;
4343
4344 if (p2p == NULL)
4345 return;
4346
4c010834
KL
4347 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4348 return;
4349
b22128ef
JM
4350 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4351 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4352
2f646b6e
JB
4353 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4354 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
b22128ef 4355
b6e01800
JM
4356 if (wpa_s->wps &&
4357 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4358 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4359
4360 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4361 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4362
4363 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4364 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4365 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4366 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4367 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4368 }
4369
2f646b6e
JB
4370 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4371 p2p_set_sec_dev_types(p2p,
4372 (void *) wpa_s->conf->sec_device_type,
4373 wpa_s->conf->num_sec_device_types);
b22128ef 4374
f95cac27
JMB
4375 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4376 int i;
4377 p2p_remove_wps_vendor_extensions(p2p);
4378 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4379 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4380 continue;
4381 p2p_add_wps_vendor_extension(
4382 p2p, wpa_s->conf->wps_vendor_ext[i]);
4383 }
4384 }
4385
b22128ef
JM
4386 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4387 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4388 char country[3];
4389 country[0] = wpa_s->conf->country[0];
4390 country[1] = wpa_s->conf->country[1];
4391 country[2] = 0x04;
4392 p2p_set_country(p2p, country);
4393 }
4394
4395 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4396 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4397 wpa_s->conf->p2p_ssid_postfix ?
4398 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4399 0);
4400 }
0f66abd2
SS
4401
4402 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4403 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
2463ba70
JS
4404
4405 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4406 u8 reg_class, channel;
4407 int ret;
4408 unsigned int r;
4409 if (wpa_s->conf->p2p_listen_reg_class &&
4410 wpa_s->conf->p2p_listen_channel) {
4411 reg_class = wpa_s->conf->p2p_listen_reg_class;
4412 channel = wpa_s->conf->p2p_listen_channel;
4413 } else {
4414 reg_class = 81;
4415 /*
4416 * Pick one of the social channels randomly as the
4417 * listen channel.
4418 */
4419 os_get_random((u8 *) &r, sizeof(r));
4420 channel = 1 + (r % 3) * 5;
4421 }
4422 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4423 if (ret)
4424 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4425 "failed: %d", ret);
4426 }
4427 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4428 u8 op_reg_class, op_channel, cfg_op_channel;
4429 int ret = 0;
4430 unsigned int r;
4431 if (wpa_s->conf->p2p_oper_reg_class &&
4432 wpa_s->conf->p2p_oper_channel) {
4433 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4434 op_channel = wpa_s->conf->p2p_oper_channel;
4435 cfg_op_channel = 1;
4436 } else {
4437 op_reg_class = 81;
4438 /*
4439 * Use random operation channel from (1, 6, 11)
4440 *if no other preference is indicated.
4441 */
4442 os_get_random((u8 *) &r, sizeof(r));
4443 op_channel = 1 + (r % 3) * 5;
4444 cfg_op_channel = 0;
4445 }
4446 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4447 cfg_op_channel);
4448 if (ret)
4449 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4450 "failed: %d", ret);
4451 }
21d996f7
JM
4452
4453 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4454 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4455 wpa_s->conf->p2p_pref_chan) < 0) {
4456 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4457 "update failed");
4458 }
4459 }
b22128ef 4460}
aefb53bd
JM
4461
4462
4463int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4464 int duration)
4465{
4466 if (!wpa_s->ap_iface)
4467 return -1;
4468 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4469 duration);
4470}
72044390
JM
4471
4472
4473int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4474{
9526fd29 4475 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
72044390
JM
4476 return -1;
4477 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4478 return -1;
4479
4480 wpa_s->global->cross_connection = enabled;
4481 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4482
4483 if (!enabled) {
4484 struct wpa_supplicant *iface;
4485
4486 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4487 {
4488 if (iface->cross_connect_enabled == 0)
4489 continue;
4490
4491 iface->cross_connect_enabled = 0;
4492 iface->cross_connect_in_use = 0;
4493 wpa_msg(iface->parent, MSG_INFO,
4494 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4495 iface->ifname, iface->cross_connect_uplink);
4496 }
4497 }
4498
4499 return 0;
4500}
4501
4502
4503static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4504{
4505 struct wpa_supplicant *iface;
4506
4507 if (!uplink->global->cross_connection)
4508 return;
4509
4510 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4511 if (!iface->cross_connect_enabled)
4512 continue;
4513 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4514 0)
4515 continue;
4516 if (iface->ap_iface == NULL)
4517 continue;
4518 if (iface->cross_connect_in_use)
4519 continue;
4520
4521 iface->cross_connect_in_use = 1;
4522 wpa_msg(iface->parent, MSG_INFO,
4523 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4524 iface->ifname, iface->cross_connect_uplink);
4525 }
4526}
4527
4528
4529static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4530{
4531 struct wpa_supplicant *iface;
4532
4533 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4534 if (!iface->cross_connect_enabled)
4535 continue;
4536 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4537 0)
4538 continue;
4539 if (!iface->cross_connect_in_use)
4540 continue;
4541
4542 wpa_msg(iface->parent, MSG_INFO,
4543 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4544 iface->ifname, iface->cross_connect_uplink);
4545 iface->cross_connect_in_use = 0;
4546 }
4547}
4548
4549
4550void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4551{
4552 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4553 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4554 wpa_s->cross_connect_disallowed)
4555 wpas_p2p_disable_cross_connect(wpa_s);
4556 else
4557 wpas_p2p_enable_cross_connect(wpa_s);
dddc7045
JM
4558 if (!wpa_s->ap_iface &&
4559 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4560 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
72044390
JM
4561}
4562
4563
4564void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4565{
4566 wpas_p2p_disable_cross_connect(wpa_s);
4c2c6751
JM
4567 if (!wpa_s->ap_iface &&
4568 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4569 wpa_s, NULL))
3071e181 4570 wpas_p2p_set_group_idle_timeout(wpa_s);
72044390
JM
4571}
4572
4573
4574static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4575{
4576 struct wpa_supplicant *iface;
4577
4578 if (!wpa_s->global->cross_connection)
4579 return;
4580
4581 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4582 if (iface == wpa_s)
4583 continue;
4584 if (iface->drv_flags &
4585 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4586 continue;
4587 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4588 continue;
4589
4590 wpa_s->cross_connect_enabled = 1;
4591 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4592 sizeof(wpa_s->cross_connect_uplink));
4593 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4594 "%s to %s whenever uplink is available",
4595 wpa_s->ifname, wpa_s->cross_connect_uplink);
4596
4597 if (iface->ap_iface || iface->current_ssid == NULL ||
4598 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4599 iface->cross_connect_disallowed ||
4600 iface->wpa_state != WPA_COMPLETED)
4601 break;
4602
4603 wpa_s->cross_connect_in_use = 1;
4604 wpa_msg(wpa_s->parent, MSG_INFO,
4605 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4606 wpa_s->ifname, wpa_s->cross_connect_uplink);
4607 break;
4608 }
4609}
b73bf0a7
JM
4610
4611
4612int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4613{
4614 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4615 !wpa_s->p2p_in_provisioning)
4616 return 0; /* not P2P client operation */
4617
4618 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4619 "session overlap");
148bb37f
JM
4620 if (wpa_s != wpa_s->parent)
4621 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
0a14e3ec
SS
4622
4623 if (wpa_s->global->p2p)
4624 p2p_group_formation_failed(wpa_s->global->p2p);
4625
4626 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4627 wpa_s->parent, NULL);
4628
b73bf0a7
JM
4629 wpas_group_formation_completed(wpa_s, 0);
4630 return 1;
4631}
b5c9da8d
JM
4632
4633
4634void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4635{
4636 struct p2p_channels chan;
4637
4638 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4639 return;
4640
4641 os_memset(&chan, 0, sizeof(chan));
4642 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4643 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4644 "channel list");
4645 return;
4646 }
4647
4648 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4649}
59eba7a2
JM
4650
4651
4652int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4653{
4654 struct wpa_global *global = wpa_s->global;
4655 int found = 0;
231bbd03 4656 const u8 *peer;
59eba7a2 4657
9526fd29
JM
4658 if (global->p2p == NULL)
4659 return -1;
4660
59eba7a2
JM
4661 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4662
4663 if (wpa_s->pending_interface_name[0] &&
4664 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4665 found = 1;
4666
231bbd03
SS
4667 peer = p2p_get_go_neg_peer(global->p2p);
4668 if (peer) {
4669 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4670 MACSTR, MAC2STR(peer));
4671 p2p_unauthorize(global->p2p, peer);
75c208b9 4672 found = 1;
231bbd03
SS
4673 }
4674
59eba7a2
JM
4675 wpas_p2p_stop_find(wpa_s);
4676
4677 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4678 if (wpa_s == global->p2p_group_formation &&
a0a9f3b0
JM
4679 (wpa_s->p2p_in_provisioning ||
4680 wpa_s->parent->pending_interface_type ==
4681 WPA_IF_P2P_CLIENT)) {
59eba7a2
JM
4682 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4683 "formation found - cancelling",
4684 wpa_s->ifname);
4685 found = 1;
45fee6f0
SS
4686 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4687 wpa_s->parent, NULL);
aa9bb764 4688 wpas_p2p_group_delete(wpa_s, 0);
59eba7a2
JM
4689 break;
4690 }
4691 }
4692
4693 if (!found) {
4694 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4695 return -1;
4696 }
4697
4698 return 0;
4699}
c973f386
JM
4700
4701
4702void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4703{
4704 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4705 return;
4706
4707 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4708 "being available anymore");
4709 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
aa9bb764 4710 wpas_p2p_group_delete(wpa_s, 0);
c973f386 4711}
7cfc4ac3
AGS
4712
4713
4714void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4715 int freq_24, int freq_5, int freq_overall)
4716{
4717 struct p2p_data *p2p = wpa_s->global->p2p;
4718 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4719 return;
4720 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4721}
9d562b79
SS
4722
4723
4724int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4725{
4726 u8 peer[ETH_ALEN];
4727 struct p2p_data *p2p = wpa_s->global->p2p;
4728
4729 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4730 return -1;
4731
4732 if (hwaddr_aton(addr, peer))
4733 return -1;
4734
4735 return p2p_unauthorize(p2p, peer);
4736}
3103f345
JB
4737
4738
4739/**
4740 * wpas_p2p_disconnect - Disconnect from a P2P Group
4741 * @wpa_s: Pointer to wpa_supplicant data
4742 * Returns: 0 on success, -1 on failure
4743 *
4744 * This can be used to disconnect from a group in which the local end is a P2P
4745 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4746 * virtual network interface was created for this group, that interface will be
4747 * removed. Otherwise, only the configured P2P group network will be removed
4748 * from the interface.
4749 */
4750int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4751{
4752
4753 if (wpa_s == NULL)
4754 return -1;
4755
4756 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
aa9bb764 4757 wpas_p2p_group_delete(wpa_s, 0);
3103f345
JB
4758
4759 return 0;
4760}
303f60d3
JM
4761
4762
4763int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4764{
4765 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4766 return 0;
4767
4768 return p2p_in_progress(wpa_s->global->p2p);
4769}
502618f7
JM
4770
4771
4772void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4773 struct wpa_ssid *ssid)
502618f7
JM
4774{
4775 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4776 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4777 wpa_s->parent, NULL) > 0) {
eb6f8c2b
AC
4778 /**
4779 * Remove the network by scheduling the group formation
4780 * timeout to happen immediately. The teardown code
4781 * needs to be scheduled to run asynch later so that we
4782 * don't delete data from under ourselves unexpectedly.
4783 * Calling wpas_p2p_group_formation_timeout directly
4784 * causes a series of crashes in WPS failure scenarios.
4785 */
502618f7
JM
4786 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4787 "P2P group network getting removed");
eb6f8c2b
AC
4788 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4789 wpa_s->parent, NULL);
502618f7
JM
4790 }
4791}
87f841a1
JM
4792
4793
4794struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
c427ac92
JM
4795 const u8 *addr, const u8 *ssid,
4796 size_t ssid_len)
87f841a1
JM
4797{
4798 struct wpa_ssid *s;
fbdcfd57 4799 size_t i;
87f841a1
JM
4800
4801 for (s = wpa_s->conf->ssid; s; s = s->next) {
fbdcfd57
JM
4802 if (s->disabled != 2)
4803 continue;
c427ac92
JM
4804 if (ssid &&
4805 (ssid_len != s->ssid_len ||
4806 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4807 continue;
fbdcfd57
JM
4808 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4809 return s; /* peer is GO in the persistent group */
4810 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4811 continue;
4812 for (i = 0; i < s->num_p2p_clients; i++) {
4813 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4814 addr, ETH_ALEN) == 0)
4815 return s; /* peer is P2P client in persistent
4816 * group */
4817 }
87f841a1
JM
4818 }
4819
4820 return NULL;
4821}
fbdcfd57
JM
4822
4823
4824void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4825 const u8 *addr)
4826{
4827 if (addr == NULL)
4828 return;
4829 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4830}
aa9bb764
JM
4831
4832
4833static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4834 int group_added)
4835{
4836 struct wpa_supplicant *group = wpa_s;
4837 if (wpa_s->global->p2p_group_formation)
4838 group = wpa_s->global->p2p_group_formation;
4839 wpa_s = wpa_s->parent;
4840 if (group_added)
4841 wpas_p2p_group_delete(group, 1);
4842 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4843 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4844 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4845 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4846 wpa_s->p2p_persistent_id);
4847}
4848
4849
4850int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4851{
4852 if (!wpa_s->p2p_fallback_to_go_neg ||
4853 wpa_s->p2p_in_provisioning <= 5)
4854 return 0;
4855
4856 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4857 "fallback to GO Negotiation");
4858 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4859
4860 return 1;
4861}