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