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