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