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