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