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