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