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