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