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