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