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