]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/p2p_supplicant.c
P2P: Add notification for P2P device found
[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
1154static int wpas_start_listen(void *ctx, unsigned int freq,
1155 unsigned int duration,
1156 const struct wpabuf *probe_resp_ie)
1157{
1158 struct wpa_supplicant *wpa_s = ctx;
1159
0e2e565a 1160 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
b22128ef
JM
1161
1162 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1163 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1164 "report received Probe Request frames");
1165 return -1;
1166 }
1167
1168 wpa_s->pending_listen_freq = freq;
1169 wpa_s->pending_listen_duration = duration;
1170
1171 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1172 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1173 "to remain on channel (%u MHz) for Listen "
1174 "state", freq);
1175 wpa_s->pending_listen_freq = 0;
1176 return -1;
1177 }
09d660b9 1178 wpa_s->off_channel_freq = 0;
07a30a66 1179 wpa_s->roc_waiting_drv_freq = freq;
b22128ef
JM
1180
1181 return 0;
1182}
1183
1184
1185static void wpas_stop_listen(void *ctx)
1186{
1187 struct wpa_supplicant *wpa_s = ctx;
6cb22d2f 1188 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
b22128ef
JM
1189 wpa_drv_cancel_remain_on_channel(wpa_s);
1190 wpa_s->off_channel_freq = 0;
07a30a66 1191 wpa_s->roc_waiting_drv_freq = 0;
b22128ef 1192 }
839b33ad 1193 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
b22128ef
JM
1194 wpa_drv_probe_req_report(wpa_s, 0);
1195}
1196
1197
1198static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1199{
1200 struct wpa_supplicant *wpa_s = ctx;
1201 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1202}
1203
1204
1205static struct p2p_srv_bonjour *
1206wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1207 const struct wpabuf *query)
1208{
1209 struct p2p_srv_bonjour *bsrv;
1210 size_t len;
1211
1212 len = wpabuf_len(query);
1213 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1214 struct p2p_srv_bonjour, list) {
1215 if (len == wpabuf_len(bsrv->query) &&
1216 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1217 len) == 0)
1218 return bsrv;
1219 }
1220 return NULL;
1221}
1222
1223
1224static struct p2p_srv_upnp *
1225wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1226 const char *service)
1227{
1228 struct p2p_srv_upnp *usrv;
1229
1230 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1231 struct p2p_srv_upnp, list) {
1232 if (version == usrv->version &&
1233 os_strcmp(service, usrv->service) == 0)
1234 return usrv;
1235 }
1236 return NULL;
1237}
1238
1239
1240static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1241 u8 srv_trans_id)
1242{
1243 u8 *len_pos;
1244
1245 if (wpabuf_tailroom(resp) < 5)
1246 return;
1247
1248 /* Length (to be filled) */
1249 len_pos = wpabuf_put(resp, 2);
1250 wpabuf_put_u8(resp, srv_proto);
1251 wpabuf_put_u8(resp, srv_trans_id);
1252 /* Status Code */
1253 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1254 /* Response Data: empty */
1255 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1256}
1257
1258
1259static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1260 struct wpabuf *resp, u8 srv_trans_id)
1261{
1262 struct p2p_srv_bonjour *bsrv;
1263 u8 *len_pos;
1264
1265 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1266
1267 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1268 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1269 return;
1270 }
1271
1272 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1273 struct p2p_srv_bonjour, list) {
1274 if (wpabuf_tailroom(resp) <
1275 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1276 return;
1277 /* Length (to be filled) */
1278 len_pos = wpabuf_put(resp, 2);
1279 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1280 wpabuf_put_u8(resp, srv_trans_id);
1281 /* Status Code */
1282 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1283 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1284 wpabuf_head(bsrv->resp),
1285 wpabuf_len(bsrv->resp));
1286 /* Response Data */
1287 wpabuf_put_buf(resp, bsrv->query); /* Key */
1288 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1289 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1290 2);
1291 }
1292}
1293
1294
1295static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1296 struct wpabuf *resp, u8 srv_trans_id,
1297 const u8 *query, size_t query_len)
1298{
1299 struct p2p_srv_bonjour *bsrv;
1300 struct wpabuf buf;
1301 u8 *len_pos;
1302
1303 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1304 query, query_len);
1305 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1306 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1307 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1308 srv_trans_id);
1309 return;
1310 }
1311
1312 if (query_len == 0) {
1313 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1314 return;
1315 }
1316
1317 if (wpabuf_tailroom(resp) < 5)
1318 return;
1319 /* Length (to be filled) */
1320 len_pos = wpabuf_put(resp, 2);
1321 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1322 wpabuf_put_u8(resp, srv_trans_id);
1323
1324 wpabuf_set(&buf, query, query_len);
1325 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1326 if (bsrv == NULL) {
1327 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1328 "available");
1329
1330 /* Status Code */
bf608cad 1331 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1332 /* Response Data: empty */
1333 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1334 2);
1335 return;
1336 }
1337
1338 /* Status Code */
1339 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1340 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1341 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1342
1343 if (wpabuf_tailroom(resp) >=
1344 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1345 /* Response Data */
1346 wpabuf_put_buf(resp, bsrv->query); /* Key */
1347 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1348 }
1349 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1350}
1351
1352
1353static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1354 struct wpabuf *resp, u8 srv_trans_id)
1355{
1356 struct p2p_srv_upnp *usrv;
1357 u8 *len_pos;
1358
1359 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1360
1361 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1362 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1363 return;
1364 }
1365
1366 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1367 struct p2p_srv_upnp, list) {
1368 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1369 return;
1370
1371 /* Length (to be filled) */
1372 len_pos = wpabuf_put(resp, 2);
1373 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1374 wpabuf_put_u8(resp, srv_trans_id);
1375
1376 /* Status Code */
1377 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1378 /* Response Data */
1379 wpabuf_put_u8(resp, usrv->version);
1380 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1381 usrv->service);
1382 wpabuf_put_str(resp, usrv->service);
1383 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1384 2);
1385 }
1386}
1387
1388
1389static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1390 struct wpabuf *resp, u8 srv_trans_id,
1391 const u8 *query, size_t query_len)
1392{
1393 struct p2p_srv_upnp *usrv;
1394 u8 *len_pos;
1395 u8 version;
1396 char *str;
1397 int count = 0;
1398
1399 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1400 query, query_len);
1401
1402 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1403 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1404 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1405 srv_trans_id);
1406 return;
1407 }
1408
1409 if (query_len == 0) {
1410 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1411 return;
1412 }
1413
b22128ef
JM
1414 if (wpabuf_tailroom(resp) < 5)
1415 return;
1416
1417 /* Length (to be filled) */
1418 len_pos = wpabuf_put(resp, 2);
1419 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1420 wpabuf_put_u8(resp, srv_trans_id);
1421
79222cfa
JM
1422 version = query[0];
1423 str = os_malloc(query_len);
1424 if (str == NULL)
1425 return;
1426 os_memcpy(str, query + 1, query_len - 1);
1427 str[query_len - 1] = '\0';
1428
b22128ef
JM
1429 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1430 struct p2p_srv_upnp, list) {
1431 if (version != usrv->version)
1432 continue;
1433
1434 if (os_strcmp(str, "ssdp:all") != 0 &&
1435 os_strstr(usrv->service, str) == NULL)
1436 continue;
1437
1438 if (wpabuf_tailroom(resp) < 2)
1439 break;
1440 if (count == 0) {
1441 /* Status Code */
1442 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1443 /* Response Data */
1444 wpabuf_put_u8(resp, version);
1445 } else
1446 wpabuf_put_u8(resp, ',');
1447
1448 count++;
1449
1450 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1451 usrv->service);
1452 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1453 break;
1454 wpabuf_put_str(resp, usrv->service);
1455 }
1456
1457 if (count == 0) {
1458 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1459 "available");
1460 /* Status Code */
bf608cad 1461 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1462 /* Response Data: empty */
1463 }
1464
1465 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1466}
1467
1468
1469void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1470 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1471{
1472 struct wpa_supplicant *wpa_s = ctx;
1473 const u8 *pos = tlvs;
1474 const u8 *end = tlvs + tlvs_len;
1475 const u8 *tlv_end;
1476 u16 slen;
1477 struct wpabuf *resp;
1478 u8 srv_proto, srv_trans_id;
1479 size_t buf_len;
1480 char *buf;
1481
1482 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1483 tlvs, tlvs_len);
1484 buf_len = 2 * tlvs_len + 1;
1485 buf = os_malloc(buf_len);
1486 if (buf) {
1487 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1488 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1489 MACSTR " %u %u %s",
1490 freq, MAC2STR(sa), dialog_token, update_indic,
1491 buf);
1492 os_free(buf);
1493 }
1494
1495 if (wpa_s->p2p_sd_over_ctrl_iface)
1496 return; /* to be processed by an external program */
1497
1498 resp = wpabuf_alloc(10000);
1499 if (resp == NULL)
1500 return;
1501
1502 while (pos + 1 < end) {
1503 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1504 slen = WPA_GET_LE16(pos);
1505 pos += 2;
1506 if (pos + slen > end || slen < 2) {
1507 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1508 "length");
1509 wpabuf_free(resp);
1510 return;
1511 }
1512 tlv_end = pos + slen;
1513
1514 srv_proto = *pos++;
1515 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1516 srv_proto);
1517 srv_trans_id = *pos++;
1518 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1519 srv_trans_id);
1520
1521 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1522 pos, tlv_end - pos);
1523
6e6963ea
JM
1524
1525 if (wpa_s->force_long_sd) {
1526 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1527 "response");
1528 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1529 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1530 goto done;
1531 }
1532
b22128ef
JM
1533 switch (srv_proto) {
1534 case P2P_SERV_ALL_SERVICES:
1535 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1536 "for all services");
1537 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1538 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1539 wpa_printf(MSG_DEBUG, "P2P: No service "
1540 "discovery protocols available");
1541 wpas_sd_add_proto_not_avail(
1542 resp, P2P_SERV_ALL_SERVICES,
1543 srv_trans_id);
1544 break;
1545 }
1546 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1547 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1548 break;
1549 case P2P_SERV_BONJOUR:
1550 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1551 pos, tlv_end - pos);
1552 break;
1553 case P2P_SERV_UPNP:
1554 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1555 pos, tlv_end - pos);
1556 break;
1557 default:
1558 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1559 "protocol %u", srv_proto);
1560 wpas_sd_add_proto_not_avail(resp, srv_proto,
1561 srv_trans_id);
1562 break;
1563 }
1564
1565 pos = tlv_end;
1566 }
1567
6e6963ea 1568done:
b22128ef
JM
1569 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1570
1571 wpabuf_free(resp);
1572}
1573
1574
1575void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1576 const u8 *tlvs, size_t tlvs_len)
1577{
1578 struct wpa_supplicant *wpa_s = ctx;
1579 const u8 *pos = tlvs;
1580 const u8 *end = tlvs + tlvs_len;
1581 const u8 *tlv_end;
1582 u16 slen;
1583 size_t buf_len;
1584 char *buf;
1585
1586 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1587 tlvs, tlvs_len);
18708aad
JM
1588 if (tlvs_len > 1500) {
1589 /* TODO: better way for handling this */
1590 wpa_msg_ctrl(wpa_s, MSG_INFO,
1591 P2P_EVENT_SERV_DISC_RESP MACSTR
1592 " %u <long response: %u bytes>",
1593 MAC2STR(sa), update_indic,
1594 (unsigned int) tlvs_len);
1595 } else {
1596 buf_len = 2 * tlvs_len + 1;
1597 buf = os_malloc(buf_len);
1598 if (buf) {
1599 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1600 wpa_msg_ctrl(wpa_s, MSG_INFO,
1601 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1602 MAC2STR(sa), update_indic, buf);
1603 os_free(buf);
1604 }
b22128ef
JM
1605 }
1606
1607 while (pos < end) {
1608 u8 srv_proto, srv_trans_id, status;
1609
1610 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1611 slen = WPA_GET_LE16(pos);
1612 pos += 2;
1613 if (pos + slen > end || slen < 3) {
1614 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1615 "length");
1616 return;
1617 }
1618 tlv_end = pos + slen;
1619
1620 srv_proto = *pos++;
1621 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1622 srv_proto);
1623 srv_trans_id = *pos++;
1624 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1625 srv_trans_id);
1626 status = *pos++;
1627 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1628 status);
1629
1630 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1631 pos, tlv_end - pos);
1632
1633 pos = tlv_end;
1634 }
1635}
1636
1637
1638void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1639 const struct wpabuf *tlvs)
1640{
3ac17eba
JM
1641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1642 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
9526fd29
JM
1643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1644 return NULL;
b22128ef
JM
1645 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1646}
1647
1648
1649void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1650 u8 version, const char *query)
1651{
1652 struct wpabuf *tlvs;
1653 void *ret;
1654
1655 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1656 if (tlvs == NULL)
1657 return NULL;
1658 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1659 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1660 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1661 wpabuf_put_u8(tlvs, version);
1662 wpabuf_put_str(tlvs, query);
1663 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1664 wpabuf_free(tlvs);
1665 return ret;
1666}
1667
1668
1669int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1670{
3ac17eba
JM
1671 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1672 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
9526fd29
JM
1673 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1674 return -1;
b22128ef
JM
1675 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1676}
1677
1678
1679void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1680 const u8 *dst, u8 dialog_token,
1681 const struct wpabuf *resp_tlvs)
1682{
3ac17eba
JM
1683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1684 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1685 resp_tlvs);
1686 return;
1687 }
9526fd29
JM
1688 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1689 return;
b22128ef
JM
1690 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1691 resp_tlvs);
1692}
1693
1694
1695void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1696{
3ac17eba
JM
1697 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1698 wpa_drv_p2p_service_update(wpa_s);
1699 return;
1700 }
9526fd29
JM
1701 if (wpa_s->global->p2p)
1702 p2p_sd_service_update(wpa_s->global->p2p);
b22128ef
JM
1703}
1704
1705
1706static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1707{
1708 dl_list_del(&bsrv->list);
1709 wpabuf_free(bsrv->query);
1710 wpabuf_free(bsrv->resp);
1711 os_free(bsrv);
1712}
1713
1714
1715static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1716{
1717 dl_list_del(&usrv->list);
1718 os_free(usrv->service);
1719 os_free(usrv);
1720}
1721
1722
1723void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1724{
1725 struct p2p_srv_bonjour *bsrv, *bn;
1726 struct p2p_srv_upnp *usrv, *un;
1727
1728 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1729 struct p2p_srv_bonjour, list)
1730 wpas_p2p_srv_bonjour_free(bsrv);
1731
1732 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1733 struct p2p_srv_upnp, list)
1734 wpas_p2p_srv_upnp_free(usrv);
1735
1736 wpas_p2p_sd_service_update(wpa_s);
1737}
1738
1739
1740int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1741 struct wpabuf *query, struct wpabuf *resp)
1742{
1743 struct p2p_srv_bonjour *bsrv;
1744
1745 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1746 if (bsrv) {
1747 wpabuf_free(query);
1748 wpabuf_free(bsrv->resp);
1749 bsrv->resp = resp;
1750 return 0;
1751 }
1752
1753 bsrv = os_zalloc(sizeof(*bsrv));
1754 if (bsrv == NULL)
1755 return -1;
1756 bsrv->query = query;
1757 bsrv->resp = resp;
1758 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1759
1760 wpas_p2p_sd_service_update(wpa_s);
1761 return 0;
1762}
1763
1764
1765int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1766 const struct wpabuf *query)
1767{
1768 struct p2p_srv_bonjour *bsrv;
1769
1770 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1771 if (bsrv == NULL)
1772 return -1;
1773 wpas_p2p_srv_bonjour_free(bsrv);
1774 wpas_p2p_sd_service_update(wpa_s);
1775 return 0;
1776}
1777
1778
1779int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1780 const char *service)
1781{
1782 struct p2p_srv_upnp *usrv;
1783
1784 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1785 return 0; /* Already listed */
1786 usrv = os_zalloc(sizeof(*usrv));
1787 if (usrv == NULL)
1788 return -1;
1789 usrv->version = version;
1790 usrv->service = os_strdup(service);
1791 if (usrv->service == NULL) {
1792 os_free(usrv);
1793 return -1;
1794 }
1795 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1796
1797 wpas_p2p_sd_service_update(wpa_s);
1798 return 0;
1799}
1800
1801
1802int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1803 const char *service)
1804{
1805 struct p2p_srv_upnp *usrv;
1806
1807 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1808 if (usrv == NULL)
1809 return -1;
1810 wpas_p2p_srv_upnp_free(usrv);
1811 wpas_p2p_sd_service_update(wpa_s);
1812 return 0;
1813}
1814
1815
1816static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
bbeee9b0
JB
1817 const u8 *peer, const char *params,
1818 unsigned int generated_pin)
b22128ef
JM
1819{
1820 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
bbeee9b0 1821 MAC2STR(peer), generated_pin, params);
b22128ef
JM
1822}
1823
1824
1825static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1826 const u8 *peer, const char *params)
1827{
1828 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1829 MAC2STR(peer), params);
1830}
1831
1832
1833void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1834 const u8 *dev_addr, const u8 *pri_dev_type,
1835 const char *dev_name, u16 supp_config_methods,
1836 u8 dev_capab, u8 group_capab)
1837{
1838 struct wpa_supplicant *wpa_s = ctx;
1839 char devtype[WPS_DEV_TYPE_BUFSIZE];
1840 char params[200];
1841 u8 empty_dev_type[8];
bbeee9b0 1842 unsigned int generated_pin = 0;
b22128ef
JM
1843
1844 if (pri_dev_type == NULL) {
1845 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1846 pri_dev_type = empty_dev_type;
1847 }
1848 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1849 " pri_dev_type=%s name='%s' config_methods=0x%x "
1850 "dev_capab=0x%x group_capab=0x%x",
1851 MAC2STR(dev_addr),
1852 wps_dev_type_bin2str(pri_dev_type, devtype,
1853 sizeof(devtype)),
1854 dev_name, supp_config_methods, dev_capab, group_capab);
1855 params[sizeof(params) - 1] = '\0';
1856
bbeee9b0
JB
1857 if (config_methods & WPS_CONFIG_DISPLAY) {
1858 generated_pin = wps_generate_pin();
1859 wpas_prov_disc_local_display(wpa_s, peer, params,
1860 generated_pin);
1861 } else if (config_methods & WPS_CONFIG_KEYPAD)
b22128ef
JM
1862 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1863 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1864 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1865 "%s", MAC2STR(peer), params);
1866}
1867
1868
1869void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1870{
1871 struct wpa_supplicant *wpa_s = ctx;
bbeee9b0
JB
1872 unsigned int generated_pin = 0;
1873
b22128ef
JM
1874 if (config_methods & WPS_CONFIG_DISPLAY)
1875 wpas_prov_disc_local_keypad(wpa_s, peer, "");
bbeee9b0
JB
1876 else if (config_methods & WPS_CONFIG_KEYPAD) {
1877 generated_pin = wps_generate_pin();
1878 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1879 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
b22128ef
JM
1880 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1881 MAC2STR(peer));
a482883f
JM
1882
1883 if (wpa_s->pending_pd_before_join &&
1884 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1885 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1886 wpa_s->pending_pd_before_join = 0;
1887 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1888 "join-existing-group operation");
1889 wpas_p2p_join_start(wpa_s);
1890 }
b22128ef
JM
1891}
1892
1893
1894static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1895 const u8 *go_dev_addr, const u8 *ssid,
1896 size_t ssid_len, int *go, u8 *group_bssid,
1897 int *force_freq, int persistent_group)
1898{
1899 struct wpa_supplicant *wpa_s = ctx;
1900 struct wpa_ssid *s;
1901 u8 cur_bssid[ETH_ALEN];
1902 int res;
ab72eb52 1903 struct wpa_supplicant *grp;
b22128ef
JM
1904
1905 if (!persistent_group) {
1906 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1907 " to join an active group", MAC2STR(sa));
108def93 1908 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
131cb37c
JM
1909 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1910 == 0 ||
1911 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
108def93
JM
1912 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1913 "authorized invitation");
1914 goto accept_inv;
1915 }
b22128ef
JM
1916 /*
1917 * Do not accept the invitation automatically; notify user and
1918 * request approval.
1919 */
1920 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1921 }
1922
ab72eb52
JM
1923 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1924 if (grp) {
1925 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1926 "running persistent group");
1927 if (*go)
1928 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1929 goto accept_inv;
1930 }
1931
b22128ef
JM
1932 if (!wpa_s->conf->persistent_reconnect)
1933 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1934
1935 for (s = wpa_s->conf->ssid; s; s = s->next) {
1936 if (s->disabled == 2 &&
1937 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1938 s->ssid_len == ssid_len &&
1939 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1940 break;
1941 }
1942
1943 if (!s) {
1944 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1945 " requested reinvocation of an unknown group",
1946 MAC2STR(sa));
1947 return P2P_SC_FAIL_UNKNOWN_GROUP;
1948 }
1949
1950 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1951 *go = 1;
1952 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1953 wpa_printf(MSG_DEBUG, "P2P: The only available "
1954 "interface is already in use - reject "
1955 "invitation");
1956 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1957 }
1958 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1959 } else if (s->mode == WPAS_MODE_P2P_GO) {
1960 *go = 1;
1961 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1962 {
1963 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1964 "interface address for the group");
1965 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1966 }
1967 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1968 ETH_ALEN);
1969 }
1970
108def93 1971accept_inv:
b22128ef
JM
1972 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1973 wpa_s->assoc_freq) {
1974 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1975 "the channel we are already using");
1976 *force_freq = wpa_s->assoc_freq;
1977 }
1978
1979 res = wpa_drv_shared_freq(wpa_s);
1980 if (res > 0) {
1981 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1982 "with the channel we are already using on a "
1983 "shared interface");
1984 *force_freq = res;
1985 }
1986
1987 return P2P_SC_SUCCESS;
1988}
1989
1990
1991static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1992 const u8 *ssid, size_t ssid_len,
1993 const u8 *go_dev_addr, u8 status,
1994 int op_freq)
1995{
1996 struct wpa_supplicant *wpa_s = ctx;
1997 struct wpa_ssid *s;
1998
1999 for (s = wpa_s->conf->ssid; s; s = s->next) {
2000 if (s->disabled == 2 &&
2001 s->ssid_len == ssid_len &&
2002 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2003 break;
2004 }
2005
2006 if (status == P2P_SC_SUCCESS) {
2007 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2008 " was accepted; op_freq=%d MHz",
2009 MAC2STR(sa), op_freq);
2010 if (s) {
2011 wpas_p2p_group_add_persistent(
2012 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2049af2b 2013 } else if (bssid) {
108def93
JM
2014 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2015 wpa_s->p2p_wps_method);
b22128ef
JM
2016 }
2017 return;
2018 }
2019
2020 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2021 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2022 " was rejected (status %u)", MAC2STR(sa), status);
2023 return;
2024 }
2025
2026 if (!s) {
2027 if (bssid) {
2028 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2029 "sa=" MACSTR " go_dev_addr=" MACSTR
2030 " bssid=" MACSTR " unknown-network",
2031 MAC2STR(sa), MAC2STR(go_dev_addr),
2032 MAC2STR(bssid));
2033 } else {
2034 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2035 "sa=" MACSTR " go_dev_addr=" MACSTR
2036 " unknown-network",
2037 MAC2STR(sa), MAC2STR(go_dev_addr));
2038 }
2039 return;
2040 }
2041
2042 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2043 " persistent=%d", MAC2STR(sa), s->id);
2044}
2045
2046
2047static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2048{
2049 struct wpa_supplicant *wpa_s = ctx;
2050 struct wpa_ssid *ssid;
2051
2052 if (bssid) {
2053 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2054 "status=%d " MACSTR,
2055 status, MAC2STR(bssid));
2056 } else {
2057 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2058 "status=%d ", status);
2059 }
2060
706887fc
JM
2061 if (wpa_s->pending_invite_ssid_id == -1)
2062 return; /* Invitation to active group */
2063
b22128ef
JM
2064 if (status != P2P_SC_SUCCESS) {
2065 wpas_p2p_remove_pending_group_interface(wpa_s);
2066 return;
2067 }
2068
2069 ssid = wpa_config_get_network(wpa_s->conf,
2070 wpa_s->pending_invite_ssid_id);
2071 if (ssid == NULL) {
2072 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2073 "data matching with invitation");
2074 return;
2075 }
2076
2077 wpas_p2p_group_add_persistent(wpa_s, ssid,
2078 ssid->mode == WPAS_MODE_P2P_GO, 0);
2079}
2080
2081
ac8d1011
JM
2082static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2083 struct p2p_channels *chan)
2084{
2085 int i, cla = 0;
2086
2087 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2088 "band");
2089
2090 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2091 chan->reg_class[cla].reg_class = 81;
2092 chan->reg_class[cla].channels = 11;
2093 for (i = 0; i < 11; i++)
2094 chan->reg_class[cla].channel[i] = i + 1;
2095 cla++;
2096
2097 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2098 "band");
2099
2100 /* Operating class 115 - 5 GHz, channels 36-48 */
2101 chan->reg_class[cla].reg_class = 115;
2102 chan->reg_class[cla].channels = 4;
2103 chan->reg_class[cla].channel[0] = 36;
2104 chan->reg_class[cla].channel[1] = 40;
2105 chan->reg_class[cla].channel[2] = 44;
2106 chan->reg_class[cla].channel[3] = 48;
2107 cla++;
2108
2109 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2110 "band");
2111
2112 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2113 chan->reg_class[cla].reg_class = 124;
2114 chan->reg_class[cla].channels = 4;
2115 chan->reg_class[cla].channel[0] = 149;
2116 chan->reg_class[cla].channel[1] = 153;
2117 chan->reg_class[cla].channel[2] = 157;
2118 chan->reg_class[cla].channel[3] = 161;
2119 cla++;
2120
2121 chan->reg_classes = cla;
2122 return 0;
2123}
2124
2125
2126static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2127 u16 num_modes,
2128 enum hostapd_hw_mode mode)
2129{
2130 u16 i;
2131
2132 for (i = 0; i < num_modes; i++) {
2133 if (modes[i].mode == mode)
2134 return &modes[i];
2135 }
2136
2137 return NULL;
2138}
2139
2140
51222429 2141static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
ac8d1011
JM
2142{
2143 int i;
2144
2145 for (i = 0; i < mode->num_channels; i++) {
2146 if (mode->channels[i].chan == chan) {
51222429
JM
2147 if (flags)
2148 *flags = mode->channels[i].flag;
ac8d1011
JM
2149 return !(mode->channels[i].flag &
2150 (HOSTAPD_CHAN_DISABLED |
2151 HOSTAPD_CHAN_PASSIVE_SCAN |
2152 HOSTAPD_CHAN_NO_IBSS |
2153 HOSTAPD_CHAN_RADAR));
2154 }
2155 }
2156
2157 return 0;
2158}
2159
2160
2161struct p2p_oper_class_map {
2162 enum hostapd_hw_mode mode;
2163 u8 op_class;
2164 u8 min_chan;
2165 u8 max_chan;
2166 u8 inc;
51222429 2167 enum { BW20, BW40PLUS, BW40MINUS } bw;
ac8d1011
JM
2168};
2169
b22128ef 2170static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
ac8d1011 2171 struct p2p_channels *chan)
b22128ef 2172{
ac8d1011 2173 struct hostapd_hw_modes *modes, *mode;
b22128ef 2174 u16 num_modes, flags;
ac8d1011
JM
2175 int cla, op;
2176 struct p2p_oper_class_map op_class[] = {
51222429
JM
2177 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2178 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2179#if 0 /* Do not enable HT40 on 2 GHz for now */
2180 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2181 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
ac8d1011 2182#endif
51222429
JM
2183 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2184 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2185 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2186 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2187 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2188 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2189 { -1, 0, 0, 0, 0, BW20 }
ac8d1011 2190 };
b22128ef
JM
2191
2192 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2193 if (modes == NULL) {
2194 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2195 "of all supported channels; assume dualband "
2196 "support");
ac8d1011 2197 return wpas_p2p_default_channels(wpa_s, chan);
b22128ef
JM
2198 }
2199
2200 cla = 0;
2201
ac8d1011
JM
2202 for (op = 0; op_class[op].op_class; op++) {
2203 struct p2p_oper_class_map *o = &op_class[op];
2204 u8 ch;
2205 struct p2p_reg_class *reg = NULL;
b22128ef 2206
ac8d1011
JM
2207 mode = get_mode(modes, num_modes, o->mode);
2208 if (mode == NULL)
2209 continue;
2210 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
51222429
JM
2211 int flag;
2212 if (!has_channel(mode, ch, &flag))
2213 continue;
2214 if (o->bw == BW40MINUS &&
2215 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
5254eb7e 2216 !has_channel(mode, ch - 4, NULL)))
51222429
JM
2217 continue;
2218 if (o->bw == BW40PLUS &&
2219 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
5254eb7e 2220 !has_channel(mode, ch + 4, NULL)))
ac8d1011
JM
2221 continue;
2222 if (reg == NULL) {
2223 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2224 "class %u", o->op_class);
2225 reg = &chan->reg_class[cla];
2226 cla++;
2227 reg->reg_class = o->op_class;
2228 }
2229 reg->channel[reg->channels] = ch;
2230 reg->channels++;
2231 }
2232 if (reg) {
2233 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2234 reg->channel, reg->channels);
2235 }
b22128ef
JM
2236 }
2237
ac8d1011 2238 chan->reg_classes = cla;
b22128ef 2239
ac8d1011 2240 ieee80211_sta_free_hw_features(modes, num_modes);
b22128ef
JM
2241
2242 return 0;
2243}
2244
2245
2246static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2247 size_t buf_len)
2248{
2249 struct wpa_supplicant *wpa_s = ctx;
2250
2251 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2252 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2253 break;
2254 }
2255 if (wpa_s == NULL)
2256 return -1;
2257
2258 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2259}
2260
2261
2262/**
2263 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2264 * @global: Pointer to global data from wpa_supplicant_init()
2265 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2266 * Returns: 0 on success, -1 on failure
2267 */
2268int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2269{
2270 struct p2p_config p2p;
2271 unsigned int r;
2272 int i;
2273
2274 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2275 return 0;
2276
2277#ifdef CONFIG_CLIENT_MLME
2278 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2279 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2280 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2281 }
2282#endif /* CONFIG_CLIENT_MLME */
2283
2284 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2285 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2286 /* Continue anyway; this is not really a fatal error */
2287 }
2288
2289 if (global->p2p)
2290 return 0;
2291
3ac17eba
JM
2292 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2293 struct p2p_params params;
2294
2295 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2296 os_memset(&params, 0, sizeof(params));
2297 params.dev_name = wpa_s->conf->device_name;
2298 if (wpa_s->conf->device_type &&
2299 wps_dev_type_str2bin(wpa_s->conf->device_type,
2300 params.pri_dev_type) < 0) {
2301 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2302 return -1;
2303 }
2304 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2305 if (wpa_s->conf->sec_device_type[i] == NULL)
2306 continue;
2307 if (wps_dev_type_str2bin(
2308 wpa_s->conf->sec_device_type[i],
2309 params.sec_dev_type[
2310 params.num_sec_dev_types]) < 0) {
2311 wpa_printf(MSG_ERROR, "P2P: Invalid "
2312 "sec_device_type");
2313 return -1;
2314 }
2315 params.num_sec_dev_types++;
2316 if (params.num_sec_dev_types == DRV_MAX_SEC_DEV_TYPES)
2317 break;
2318 }
2319 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2320 return -1;
2321
2322 return 0;
2323 }
2324
b22128ef
JM
2325 os_memset(&p2p, 0, sizeof(p2p));
2326 p2p.msg_ctx = wpa_s;
2327 p2p.cb_ctx = wpa_s;
2328 p2p.p2p_scan = wpas_p2p_scan;
2329 p2p.send_action = wpas_send_action;
2330 p2p.send_action_done = wpas_send_action_done;
2331 p2p.go_neg_completed = wpas_go_neg_completed;
2332 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2333 p2p.dev_found = wpas_dev_found;
2334 p2p.start_listen = wpas_start_listen;
2335 p2p.stop_listen = wpas_stop_listen;
2336 p2p.send_probe_resp = wpas_send_probe_resp;
2337 p2p.sd_request = wpas_sd_request;
2338 p2p.sd_response = wpas_sd_response;
2339 p2p.prov_disc_req = wpas_prov_disc_req;
2340 p2p.prov_disc_resp = wpas_prov_disc_resp;
2341 p2p.invitation_process = wpas_invitation_process;
2342 p2p.invitation_received = wpas_invitation_received;
2343 p2p.invitation_result = wpas_invitation_result;
2344 p2p.get_noa = wpas_get_noa;
2345
2346 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2347 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2348 p2p.dev_name = wpa_s->conf->device_name;
2349
2350 if (wpa_s->conf->p2p_listen_reg_class &&
2351 wpa_s->conf->p2p_listen_channel) {
2352 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2353 p2p.channel = wpa_s->conf->p2p_listen_channel;
2354 } else {
2355 p2p.reg_class = 81;
2356 /*
2357 * Pick one of the social channels randomly as the listen
2358 * channel.
2359 */
2360 os_get_random((u8 *) &r, sizeof(r));
2361 p2p.channel = 1 + (r % 3) * 5;
2362 }
7cfc4ac3 2363 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
b22128ef
JM
2364
2365 if (wpa_s->conf->p2p_oper_reg_class &&
2366 wpa_s->conf->p2p_oper_channel) {
2367 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2368 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
2369 p2p.cfg_op_channel = 1;
2370 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2371 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2372
b22128ef
JM
2373 } else {
2374 p2p.op_reg_class = 81;
2375 /*
7cfc4ac3
AGS
2376 * Use random operation channel from (1, 6, 11) if no other
2377 * preference is indicated.
b22128ef 2378 */
b7412dab 2379 os_get_random((u8 *) &r, sizeof(r));
7cfc4ac3
AGS
2380 p2p.op_channel = 1 + (r % 3) * 5;
2381 p2p.cfg_op_channel = 0;
2382 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2383 "%d:%d", p2p.op_reg_class, p2p.op_channel);
b22128ef 2384 }
b22128ef
JM
2385 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2386 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2387 p2p.country[2] = 0x04;
2388 } else
aaca6505 2389 os_memcpy(p2p.country, "XX\x04", 3);
b22128ef 2390
ac8d1011 2391 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
b22128ef
JM
2392 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2393 "channel list");
2394 return -1;
2395 }
2396
2397 if (wpa_s->conf->device_type &&
2398 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2399 0) {
2400 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2401 return -1;
2402 }
2403
2404 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2405 if (wpa_s->conf->sec_device_type[i] == NULL)
2406 continue;
2407 if (wps_dev_type_str2bin(
2408 wpa_s->conf->sec_device_type[i],
2409 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2410 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2411 return -1;
2412 }
2413 p2p.num_sec_dev_types++;
2414 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2415 break;
2416 }
2417
2418 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2419 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2420
de979d8f 2421 p2p.max_peers = 100;
b22128ef
JM
2422
2423 if (wpa_s->conf->p2p_ssid_postfix) {
2424 p2p.ssid_postfix_len =
2425 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2426 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2427 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2428 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2429 p2p.ssid_postfix_len);
2430 }
2431
0f66abd2
SS
2432 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2433
b22128ef
JM
2434 global->p2p = p2p_init(&p2p);
2435 if (global->p2p == NULL)
2436 return -1;
2437
2438 return 0;
2439}
2440
2441
2442/**
2443 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2444 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2445 *
2446 * This function deinitialize per-interface P2P data.
2447 */
2448void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2449{
2450 if (wpa_s->driver && wpa_s->drv_priv)
2451 wpa_drv_probe_req_report(wpa_s, 0);
2452 os_free(wpa_s->go_params);
2453 wpa_s->go_params = NULL;
2454 wpabuf_free(wpa_s->pending_action_tx);
2455 wpa_s->pending_action_tx = NULL;
2456 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2457 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ef922c4a 2458 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef
JM
2459 wpa_s->p2p_long_listen = 0;
2460 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3071e181 2461 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
b22128ef
JM
2462 wpas_p2p_remove_pending_group_interface(wpa_s);
2463
2464 /* TODO: remove group interface from the driver if this wpa_s instance
2465 * is on top of a P2P group interface */
2466}
2467
2468
2469/**
2470 * wpas_p2p_deinit_global - Deinitialize global P2P module
2471 * @global: Pointer to global data from wpa_supplicant_init()
2472 *
2473 * This function deinitializes the global (per device) P2P module.
2474 */
2475void wpas_p2p_deinit_global(struct wpa_global *global)
2476{
2477 struct wpa_supplicant *wpa_s, *tmp;
2478 char *ifname;
2479
2480 if (global->p2p == NULL)
2481 return;
2482
2483 /* Remove remaining P2P group interfaces */
2484 wpa_s = global->ifaces;
2a43101e
JM
2485 if (wpa_s)
2486 wpas_p2p_service_flush(wpa_s);
b22128ef
JM
2487 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2488 wpa_s = wpa_s->next;
2489 while (wpa_s) {
2490 enum wpa_driver_if_type type;
2491 tmp = global->ifaces;
2492 while (tmp &&
2493 (tmp == wpa_s ||
2494 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2495 tmp = tmp->next;
2496 }
2497 if (tmp == NULL)
2498 break;
2499 ifname = os_strdup(tmp->ifname);
2500 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2501 wpa_supplicant_remove_iface(global, tmp);
2502 if (ifname)
2503 wpa_drv_if_remove(wpa_s, type, ifname);
2504 os_free(ifname);
2505 }
2506
743ef799
JM
2507 /*
2508 * Deinit GO data on any possibly remaining interface (if main
2509 * interface is used as GO).
2510 */
2511 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2512 if (wpa_s->ap_iface)
2513 wpas_p2p_group_deinit(wpa_s);
2514 }
2515
b22128ef
JM
2516 p2p_deinit(global->p2p);
2517 global->p2p = NULL;
2518}
2519
2520
2521static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2522{
971e357f
JM
2523 if (wpa_s->drv_flags &
2524 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2525 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
b22128ef
JM
2526 return 1; /* P2P group requires a new interface in every case
2527 */
2528 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2529 return 0; /* driver does not support concurrent operations */
2530 if (wpa_s->global->ifaces->next)
2531 return 1; /* more that one interface already in use */
2532 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2533 return 1; /* this interface is already in use */
2534 return 0;
2535}
2536
2537
2538static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2539 const u8 *peer_addr,
2540 enum p2p_wps_method wps_method,
2541 int go_intent, const u8 *own_interface_addr,
2542 unsigned int force_freq, int persistent_group)
2543{
3ac17eba
JM
2544 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2545 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2546 go_intent, own_interface_addr,
2547 force_freq, persistent_group);
2548 }
2549
b22128ef
JM
2550 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2551 go_intent, own_interface_addr, force_freq,
2552 persistent_group);
2553}
2554
2555
2556static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2557 const u8 *peer_addr,
2558 enum p2p_wps_method wps_method,
2559 int go_intent, const u8 *own_interface_addr,
2560 unsigned int force_freq, int persistent_group)
2561{
3ac17eba
JM
2562 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2563 return -1;
2564
b22128ef
JM
2565 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2566 go_intent, own_interface_addr, force_freq,
2567 persistent_group);
2568}
2569
2570
9b1ab931
JM
2571static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2572{
2573 wpa_s->p2p_join_scan_count++;
2574 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2575 wpa_s->p2p_join_scan_count);
2576 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2577 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2578 " for join operationg - stop join attempt",
2579 MAC2STR(wpa_s->pending_join_iface_addr));
2580 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2581 wpa_msg(wpa_s->parent, MSG_INFO,
2582 P2P_EVENT_GROUP_FORMATION_FAILURE);
2583 }
2584}
2585
2586
ef922c4a
JM
2587static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2588 struct wpa_scan_results *scan_res)
b22128ef
JM
2589{
2590 struct wpa_bss *bss;
f8d0131a 2591 int freq;
54960629 2592 u8 iface_addr[ETH_ALEN];
b22128ef 2593
ef922c4a 2594 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef 2595
ef922c4a
JM
2596 if (wpa_s->global->p2p_disabled)
2597 return;
b22128ef 2598
ef922c4a
JM
2599 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2600 scan_res ? (int) scan_res->num : -1);
2601
2602 if (scan_res)
2603 wpas_p2p_scan_res_handler(wpa_s, scan_res);
b22128ef 2604
f8d0131a
JM
2605 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2606 wpa_s->pending_join_iface_addr);
54960629
AL
2607 if (freq < 0 &&
2608 p2p_get_interface_addr(wpa_s->global->p2p,
2609 wpa_s->pending_join_dev_addr,
2610 iface_addr) == 0 &&
2611 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2612 {
2613 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2614 "address for join from " MACSTR " to " MACSTR
2615 " based on newly discovered P2P peer entry",
2616 MAC2STR(wpa_s->pending_join_iface_addr),
2617 MAC2STR(iface_addr));
2618 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2619 ETH_ALEN);
2620
2621 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2622 wpa_s->pending_join_iface_addr);
2623 }
f8d0131a
JM
2624 if (freq >= 0) {
2625 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2626 "from P2P peer table: %d MHz", freq);
2627 }
ef922c4a 2628 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
b22128ef 2629 if (bss) {
f8d0131a
JM
2630 freq = bss->freq;
2631 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2632 "from BSS table: %d MHz", freq);
2633 }
2634 if (freq > 0) {
b22128ef
JM
2635 u16 method;
2636
2637 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2638 "prior to joining an existing group (GO " MACSTR
2639 " freq=%u MHz)",
f8d0131a 2640 MAC2STR(wpa_s->pending_join_dev_addr), freq);
b22128ef
JM
2641 wpa_s->pending_pd_before_join = 1;
2642
ef922c4a 2643 switch (wpa_s->pending_join_wps_method) {
b22128ef
JM
2644 case WPS_PIN_LABEL:
2645 case WPS_PIN_DISPLAY:
2646 method = WPS_CONFIG_KEYPAD;
2647 break;
2648 case WPS_PIN_KEYPAD:
2649 method = WPS_CONFIG_DISPLAY;
2650 break;
2651 case WPS_PBC:
2652 method = WPS_CONFIG_PUSHBUTTON;
2653 break;
2654 default:
2655 method = 0;
2656 break;
2657 }
2658
ef922c4a
JM
2659 if (p2p_prov_disc_req(wpa_s->global->p2p,
2660 wpa_s->pending_join_dev_addr, method, 1)
b22128ef
JM
2661 < 0) {
2662 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2663 "Discovery Request before joining an "
2664 "existing group");
2665 wpa_s->pending_pd_before_join = 0;
2666 goto start;
2667 }
2668
2669 /*
2670 * Actual join operation will be started from the Action frame
2671 * TX status callback.
2672 */
ef922c4a 2673 return;
b22128ef
JM
2674 }
2675
9b1ab931
JM
2676 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2677 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2678 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2679 wpas_p2p_check_join_scan_limit(wpa_s);
2680 return;
b22128ef
JM
2681
2682start:
2683 /* Start join operation immediately */
ef922c4a
JM
2684 wpas_p2p_join_start(wpa_s);
2685}
2686
2687
2688static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2689{
2690 struct wpa_supplicant *wpa_s = eloop_ctx;
2691 int ret;
2692 struct wpa_driver_scan_params params;
2693 struct wpabuf *wps_ie, *ies;
2694
2695 os_memset(&params, 0, sizeof(params));
2696
2697 /* P2P Wildcard SSID */
2698 params.num_ssids = 1;
2699 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2700 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2701
2702 wpa_s->wps->dev.p2p = 1;
2703 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2704 WPS_REQ_ENROLLEE);
2705 if (wps_ie == NULL) {
2706 wpas_p2p_scan_res_join(wpa_s, NULL);
2707 return;
2708 }
2709
2710 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2711 if (ies == NULL) {
2712 wpabuf_free(wps_ie);
2713 wpas_p2p_scan_res_join(wpa_s, NULL);
2714 return;
2715 }
2716 wpabuf_put_buf(ies, wps_ie);
2717 wpabuf_free(wps_ie);
2718
2719 p2p_scan_ie(wpa_s->global->p2p, ies);
2720
2721 params.extra_ies = wpabuf_head(ies);
2722 params.extra_ies_len = wpabuf_len(ies);
2723
2724 /*
2725 * Run a scan to update BSS table and start Provision Discovery once
2726 * the new scan results become available.
2727 */
2728 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2729 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2730 ret = ieee80211_sta_req_scan(wpa_s, &params);
2731 else
2732 ret = wpa_drv_scan(wpa_s, &params);
2733
2734 wpabuf_free(ies);
2735
2736 if (ret) {
2737 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2738 "try again later");
2739 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2740 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
9b1ab931 2741 wpas_p2p_check_join_scan_limit(wpa_s);
ef922c4a
JM
2742 }
2743}
2744
2745
2746static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2747 const u8 *dev_addr, enum p2p_wps_method wps_method)
2748{
2749 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2750 MACSTR " dev " MACSTR ")",
2751 MAC2STR(iface_addr), MAC2STR(dev_addr));
2752
2753 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2754 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2755 wpa_s->pending_join_wps_method = wps_method;
2756
2757 /* Make sure we are not running find during connection establishment */
2758 wpas_p2p_stop_find(wpa_s);
2759
9b1ab931 2760 wpa_s->p2p_join_scan_count = 0;
ef922c4a
JM
2761 wpas_p2p_join_scan(wpa_s, NULL);
2762 return 0;
b22128ef
JM
2763}
2764
2765
2766static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2767{
2768 struct wpa_supplicant *group;
2769 struct p2p_go_neg_results res;
2770
2771 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2772 if (group == NULL)
2773 return -1;
3c5126a4 2774 if (group != wpa_s) {
b22128ef
JM
2775 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2776 sizeof(group->p2p_pin));
3c5126a4
JM
2777 group->p2p_wps_method = wpa_s->p2p_wps_method;
2778 }
b22128ef
JM
2779
2780 group->p2p_in_provisioning = 1;
2781
2782 os_memset(&res, 0, sizeof(res));
2783 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2784 ETH_ALEN);
2785 res.wps_method = wpa_s->pending_join_wps_method;
2786 wpas_start_wps_enrollee(group, &res);
2787
3094d483
JM
2788 /*
2789 * Allow a longer timeout for join-a-running-group than normal 15
2790 * second group formation timeout since the GO may not have authorized
2791 * our connection yet.
2792 */
2793 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2794 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2795 wpa_s, NULL);
2796
b22128ef
JM
2797 return 0;
2798}
2799
2800
2801/**
2802 * wpas_p2p_connect - Request P2P Group Formation to be started
2803 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2804 * @peer_addr: Address of the peer P2P Device
2805 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2806 * @persistent_group: Whether to create a persistent group
2807 * @join: Whether to join an existing group (as a client) instead of starting
2808 * Group Owner negotiation; @peer_addr is BSSID in that case
2809 * @auth: Whether to only authorize the connection instead of doing that and
2810 * initiating Group Owner negotiation
2811 * @go_intent: GO Intent or -1 to use default
2812 * @freq: Frequency for the group or 0 for auto-selection
d054a462
JM
2813 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2814 * failure, -2 on failure due to channel not currently available,
2815 * -3 if forced channel is not supported
b22128ef
JM
2816 */
2817int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2818 const char *pin, enum p2p_wps_method wps_method,
2819 int persistent_group, int join, int auth, int go_intent,
2820 int freq)
2821{
d054a462 2822 int force_freq = 0, oper_freq = 0;
b22128ef
JM
2823 u8 bssid[ETH_ALEN];
2824 int ret = 0;
2825 enum wpa_driver_if_type iftype;
2826
9526fd29
JM
2827 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2828 return -1;
2829
b22128ef
JM
2830 if (go_intent < 0)
2831 go_intent = wpa_s->conf->p2p_go_intent;
2832
2833 if (!auth)
2834 wpa_s->p2p_long_listen = 0;
2835
3c5126a4
JM
2836 wpa_s->p2p_wps_method = wps_method;
2837
b22128ef
JM
2838 if (pin)
2839 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2840 else if (wps_method == WPS_PIN_DISPLAY) {
2841 ret = wps_generate_pin();
2842 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2843 ret);
2844 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2845 wpa_s->p2p_pin);
2846 } else
2847 wpa_s->p2p_pin[0] = '\0';
2848
2849 if (join) {
4147a2cc 2850 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
108def93
JM
2851 if (auth) {
2852 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2853 "connect a running group from " MACSTR,
2854 MAC2STR(peer_addr));
2855 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2856 return ret;
2857 }
4147a2cc 2858 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
b22128ef 2859 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4147a2cc 2860 iface_addr) < 0) {
b22128ef 2861 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4147a2cc
JM
2862 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2863 dev_addr);
2864 }
2865 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
b22128ef
JM
2866 0)
2867 return -1;
2868 return ret;
2869 }
2870
d054a462
JM
2871 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2872 wpa_s->assoc_freq)
2873 oper_freq = wpa_s->assoc_freq;
b22128ef 2874 else {
d054a462
JM
2875 oper_freq = wpa_drv_shared_freq(wpa_s);
2876 if (oper_freq < 0)
2877 oper_freq = 0;
b22128ef
JM
2878 }
2879
d054a462
JM
2880 if (freq > 0) {
2881 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2882 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2883 "(%u MHz) is not supported for P2P uses",
2884 freq);
2885 return -3;
2886 }
2887
2888 if (oper_freq > 0 && freq != oper_freq &&
2889 !(wpa_s->drv_flags &
2890 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2891 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2892 "on %u MHz while connected on another "
2893 "channel (%u MHz)", freq, oper_freq);
2894 return -2;
2895 }
2896 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2897 "requested channel (%u MHz)", freq);
2898 force_freq = freq;
2899 } else if (oper_freq > 0 &&
2900 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2901 if (!(wpa_s->drv_flags &
2902 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2903 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2904 "while connected on non-P2P supported "
2905 "channel (%u MHz)", oper_freq);
2906 return -2;
2907 }
2908 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2909 "(%u MHz) not available for P2P - try to use "
2910 "another channel", oper_freq);
2911 force_freq = 0;
2912 } else if (oper_freq > 0) {
b22128ef
JM
2913 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2914 "channel we are already using (%u MHz) on another "
d054a462
JM
2915 "interface", oper_freq);
2916 force_freq = oper_freq;
b22128ef
JM
2917 }
2918
2919 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2920
2921 if (!wpa_s->create_p2p_iface) {
2922 if (auth) {
2923 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2924 go_intent, wpa_s->own_addr,
2925 force_freq, persistent_group)
2926 < 0)
2927 return -1;
2928 return ret;
2929 }
2930 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2931 go_intent, wpa_s->own_addr,
2932 force_freq, persistent_group) < 0)
2933 return -1;
2934 return ret;
2935 }
2936
2937 /* Prepare to add a new interface for the group */
2938 iftype = WPA_IF_P2P_GROUP;
2939 if (join)
2940 iftype = WPA_IF_P2P_CLIENT;
2941 else if (go_intent == 15)
2942 iftype = WPA_IF_P2P_GO;
2943 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2944 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2945 "interface for the group");
2946 return -1;
2947 }
2948
2949 if (auth) {
2950 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2951 go_intent,
2952 wpa_s->pending_interface_addr,
2953 force_freq, persistent_group) < 0)
2954 return -1;
2955 return ret;
2956 }
2957 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2958 wpa_s->pending_interface_addr,
2959 force_freq, persistent_group) < 0) {
2960 wpas_p2p_remove_pending_group_interface(wpa_s);
2961 return -1;
2962 }
2963 return ret;
2964}
2965
2966
2967/**
2968 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2969 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2970 * @freq: Frequency of the channel in MHz
2971 * @duration: Duration of the stay on the channel in milliseconds
2972 *
2973 * This callback is called when the driver indicates that it has started the
2974 * requested remain-on-channel duration.
2975 */
2976void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2977 unsigned int freq, unsigned int duration)
2978{
9526fd29
JM
2979 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2980 return;
07a30a66 2981 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
2982 wpa_s->off_channel_freq = freq;
2983 wpas_send_action_cb(wpa_s, NULL);
2984 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2985 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2986 wpa_s->pending_listen_duration);
2987 wpa_s->pending_listen_freq = 0;
2988 }
2989}
2990
2991
2992static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2993 unsigned int timeout)
2994{
2995 /* Limit maximum Listen state time based on driver limitation. */
2996 if (timeout > wpa_s->max_remain_on_chan)
2997 timeout = wpa_s->max_remain_on_chan;
2998
3ac17eba
JM
2999 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3000 return wpa_drv_p2p_listen(wpa_s, timeout);
3001
b22128ef
JM
3002 return p2p_listen(wpa_s->global->p2p, timeout);
3003}
3004
3005
3006/**
3007 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3008 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3009 * @freq: Frequency of the channel in MHz
3010 *
3011 * This callback is called when the driver indicates that a remain-on-channel
3012 * operation has been completed, i.e., the duration on the requested channel
3013 * has timed out.
3014 */
3015void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3016 unsigned int freq)
3017{
1cc3a29d 3018 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
11fb02be 3019 "(p2p_long_listen=%d ms pending_action_tx=%p)",
1cc3a29d 3020 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
b22128ef 3021 wpa_s->off_channel_freq = 0;
9526fd29
JM
3022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3023 return;
b22128ef
JM
3024 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3025 return; /* P2P module started a new operation */
3026 if (wpa_s->pending_action_tx)
3027 return;
3028 if (wpa_s->p2p_long_listen > 0)
11fb02be 3029 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
b22128ef
JM
3030 if (wpa_s->p2p_long_listen > 0) {
3031 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
11fb02be 3032 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
b22128ef
JM
3033 }
3034}
3035
3036
3037/**
3038 * wpas_p2p_group_remove - Remove a P2P group
3039 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3040 * @ifname: Network interface name of the group interface or "*" to remove all
3041 * groups
3042 * Returns: 0 on success, -1 on failure
3043 *
3044 * This function is used to remove a P2P group. This can be used to disconnect
3045 * from a group in which the local end is a P2P Client or to end a P2P Group in
3046 * case the local end is the Group Owner. If a virtual network interface was
3047 * created for this group, that interface will be removed. Otherwise, only the
3048 * configured P2P group network will be removed from the interface.
3049 */
3050int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3051{
3052 struct wpa_global *global = wpa_s->global;
3053
3054 if (os_strcmp(ifname, "*") == 0) {
3055 struct wpa_supplicant *prev;
3056 wpa_s = global->ifaces;
3057 while (wpa_s) {
3058 prev = wpa_s;
3059 wpa_s = wpa_s->next;
3103f345 3060 wpas_p2p_disconnect(prev);
b22128ef
JM
3061 }
3062 return 0;
3063 }
3064
3065 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3066 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3067 break;
3068 }
3069
3103f345 3070 return wpas_p2p_disconnect(wpa_s);
b22128ef
JM
3071}
3072
3073
3074static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3075 struct p2p_go_neg_results *params,
3076 int freq)
3077{
3078 u8 bssid[ETH_ALEN];
3079 int res;
3080
3081 os_memset(params, 0, sizeof(*params));
3082 params->role_go = 1;
7cfc4ac3
AGS
3083 if (freq) {
3084 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3085 "frequency %d MHz", freq);
b22128ef 3086 params->freq = freq;
7cfc4ac3
AGS
3087 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3088 wpa_s->conf->p2p_oper_channel >= 1 &&
3089 wpa_s->conf->p2p_oper_channel <= 11) {
b22128ef 3090 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3091 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3092 "frequency %d MHz", params->freq);
3093 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3094 wpa_s->conf->p2p_oper_reg_class == 118) {
b22128ef 3095 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3096 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3097 "frequency %d MHz", params->freq);
3098 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3099 wpa_s->best_overall_freq > 0 &&
3100 p2p_supported_freq(wpa_s->global->p2p,
3101 wpa_s->best_overall_freq)) {
3102 params->freq = wpa_s->best_overall_freq;
3103 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3104 "channel %d MHz", params->freq);
3105 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3106 wpa_s->best_24_freq > 0 &&
3107 p2p_supported_freq(wpa_s->global->p2p,
3108 wpa_s->best_24_freq)) {
3109 params->freq = wpa_s->best_24_freq;
3110 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3111 "channel %d MHz", params->freq);
3112 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3113 wpa_s->best_5_freq > 0 &&
3114 p2p_supported_freq(wpa_s->global->p2p,
3115 wpa_s->best_5_freq)) {
3116 params->freq = wpa_s->best_5_freq;
3117 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3118 "channel %d MHz", params->freq);
3119 } else {
3120 params->freq = 2412;
3121 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3122 "known)", params->freq);
3123 }
3124
b22128ef
JM
3125 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3126 wpa_s->assoc_freq && !freq) {
3127 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3128 "already using");
3129 params->freq = wpa_s->assoc_freq;
3130 }
3131
3132 res = wpa_drv_shared_freq(wpa_s);
3133 if (res > 0 && !freq) {
3134 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3135 "already using on a shared interface");
3136 params->freq = res;
3137 }
3138}
3139
3140
3141static struct wpa_supplicant *
3142wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3143 int go)
3144{
3145 struct wpa_supplicant *group_wpa_s;
3146
3147 if (!wpas_p2p_create_iface(wpa_s))
3148 return wpa_s;
3149
3150 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3151 WPA_IF_P2P_CLIENT) < 0)
3152 return NULL;
3153 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3154 if (group_wpa_s == NULL) {
3155 wpas_p2p_remove_pending_group_interface(wpa_s);
3156 return NULL;
3157 }
3158
3159 return group_wpa_s;
3160}
3161
3162
3163/**
3164 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3165 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3166 * @persistent_group: Whether to create a persistent group
3167 * @freq: Frequency for the group or 0 to indicate no hardcoding
3168 * Returns: 0 on success, -1 on failure
3169 *
3170 * This function creates a new P2P group with the local end as the Group Owner,
3171 * i.e., without using Group Owner Negotiation.
3172 */
3173int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3174 int freq)
3175{
3176 struct p2p_go_neg_results params;
7cfc4ac3
AGS
3177 unsigned int r;
3178
9526fd29
JM
3179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3180 return -1;
3181
7cfc4ac3
AGS
3182 if (freq == 2) {
3183 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3184 "band");
3185 if (wpa_s->best_24_freq > 0 &&
3186 p2p_supported_freq(wpa_s->global->p2p,
3187 wpa_s->best_24_freq)) {
3188 freq = wpa_s->best_24_freq;
3189 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3190 "channel: %d MHz", freq);
3191 } else {
3192 os_get_random((u8 *) &r, sizeof(r));
3193 freq = 2412 + (r % 3) * 25;
3194 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3195 "channel: %d MHz", freq);
3196 }
3197 }
3198
3199 if (freq == 5) {
3200 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3201 "band");
3202 if (wpa_s->best_5_freq > 0 &&
3203 p2p_supported_freq(wpa_s->global->p2p,
3204 wpa_s->best_5_freq)) {
3205 freq = wpa_s->best_5_freq;
3206 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3207 "channel: %d MHz", freq);
3208 } else {
3209 os_get_random((u8 *) &r, sizeof(r));
3210 freq = 5180 + (r % 4) * 20;
3211 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3212 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3213 "5 GHz channel for P2P group");
3214 return -1;
3215 }
3216 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3217 "channel: %d MHz", freq);
3218 }
3219 }
b22128ef 3220
4ae4650b
JM
3221 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3222 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3223 "(%u MHz) is not supported for P2P uses",
3224 freq);
3225 return -1;
3226 }
3227
b22128ef
JM
3228 wpas_p2p_init_go_params(wpa_s, &params, freq);
3229 p2p_go_params(wpa_s->global->p2p, &params);
3230 params.persistent_group = persistent_group;
3231
3232 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3233 if (wpa_s == NULL)
3234 return -1;
3235 wpas_start_wps_go(wpa_s, &params, 0);
3236
3237 return 0;
3238}
3239
3240
3241static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3242 struct wpa_ssid *params, int addr_allocated)
3243{
3244 struct wpa_ssid *ssid;
3245
3246 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3247 if (wpa_s == NULL)
3248 return -1;
3249
3250 wpa_supplicant_ap_deinit(wpa_s);
3251
3252 ssid = wpa_config_add_network(wpa_s->conf);
3253 if (ssid == NULL)
3254 return -1;
3255 wpas_notify_network_added(wpa_s, ssid);
3256 wpa_config_set_network_defaults(ssid);
3257 ssid->temporary = 1;
3258 ssid->proto = WPA_PROTO_RSN;
3259 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3260 ssid->group_cipher = WPA_CIPHER_CCMP;
3261 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3262 ssid->ssid = os_malloc(params->ssid_len);
3263 if (ssid->ssid == NULL) {
3264 wpas_notify_network_removed(wpa_s, ssid);
3265 wpa_config_remove_network(wpa_s->conf, ssid->id);
3266 return -1;
3267 }
3268 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3269 ssid->ssid_len = params->ssid_len;
3270 ssid->p2p_group = 1;
d1c8ac88 3271 ssid->export_keys = 1;
b22128ef
JM
3272 if (params->psk_set) {
3273 os_memcpy(ssid->psk, params->psk, 32);
3274 ssid->psk_set = 1;
3275 }
3276 if (params->passphrase)
3277 ssid->passphrase = os_strdup(params->passphrase);
3278
3279 wpa_supplicant_select_network(wpa_s, ssid);
3280
3281 wpa_s->show_group_started = 1;
3282
3283 return 0;
3284}
3285
3286
3287int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3288 struct wpa_ssid *ssid, int addr_allocated,
3289 int freq)
3290{
3291 struct p2p_go_neg_results params;
6c0da49f 3292 int go = 0;
b22128ef
JM
3293
3294 if (ssid->disabled != 2 || ssid->ssid == NULL)
3295 return -1;
3296
6c0da49f
JM
3297 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3298 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3299 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3300 "already running");
3301 return 0;
3302 }
3303
9d39057c
JM
3304 /* Make sure we are not running find during connection establishment */
3305 wpas_p2p_stop_find(wpa_s);
b22128ef
JM
3306
3307 if (ssid->mode == WPAS_MODE_INFRA)
3308 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3309
3310 if (ssid->mode != WPAS_MODE_P2P_GO)
3311 return -1;
3312
3313 wpas_p2p_init_go_params(wpa_s, &params, freq);
3314
3315 params.role_go = 1;
3316 if (ssid->passphrase == NULL ||
3317 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3318 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3319 "group");
3320 return -1;
3321 }
3322 os_strlcpy(params.passphrase, ssid->passphrase,
3323 sizeof(params.passphrase));
3324 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3325 params.ssid_len = ssid->ssid_len;
3326 params.persistent_group = 1;
3327
3328 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3329 if (wpa_s == NULL)
3330 return -1;
3331
3332 wpas_start_wps_go(wpa_s, &params, 0);
3333
3334 return 0;
3335}
3336
3337
3338static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3339 struct wpabuf *proberesp_ies)
3340{
3341 struct wpa_supplicant *wpa_s = ctx;
3342 if (wpa_s->ap_iface) {
3343 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3344 if (beacon_ies) {
3345 wpabuf_free(hapd->p2p_beacon_ie);
3346 hapd->p2p_beacon_ie = beacon_ies;
3347 }
3348 wpabuf_free(hapd->p2p_probe_resp_ie);
3349 hapd->p2p_probe_resp_ie = proberesp_ies;
3350 } else {
3351 wpabuf_free(beacon_ies);
3352 wpabuf_free(proberesp_ies);
3353 }
3354 wpa_supplicant_ap_update_beacon(wpa_s);
3355}
3356
3357
3071e181
JM
3358static void wpas_p2p_idle_update(void *ctx, int idle)
3359{
3360 struct wpa_supplicant *wpa_s = ctx;
3361 if (!wpa_s->ap_iface)
3362 return;
3363 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3364 if (idle)
3365 wpas_p2p_set_group_idle_timeout(wpa_s);
3366 else
3367 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3368}
3369
3370
b22128ef
JM
3371struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3372 int persistent_group,
3373 int group_formation)
3374{
3375 struct p2p_group *group;
3376 struct p2p_group_config *cfg;
3377
3ac17eba
JM
3378 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3379 return NULL;
9526fd29
JM
3380 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3381 return NULL;
3ac17eba 3382
b22128ef
JM
3383 cfg = os_zalloc(sizeof(*cfg));
3384 if (cfg == NULL)
3385 return NULL;
3386
3387 cfg->persistent_group = persistent_group;
3388 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3f4ce13f
JM
3389 if (wpa_s->max_stations &&
3390 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3391 cfg->max_clients = wpa_s->max_stations;
3392 else
3393 cfg->max_clients = wpa_s->conf->max_num_sta;
b22128ef
JM
3394 cfg->cb_ctx = wpa_s;
3395 cfg->ie_update = wpas_p2p_ie_update;
3071e181 3396 cfg->idle_update = wpas_p2p_idle_update;
b22128ef
JM
3397
3398 group = p2p_group_init(wpa_s->global->p2p, cfg);
3399 if (group == NULL)
3400 os_free(cfg);
3401 if (!group_formation)
3402 p2p_group_notif_formation_done(group);
3403 wpa_s->p2p_group = group;
3404 return group;
3405}
3406
3407
3408void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3409 int registrar)
3410{
3411 if (!wpa_s->p2p_in_provisioning) {
3412 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3413 "provisioning not in progress");
3414 return;
3415 }
3416
3417 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3418 NULL);
3419 if (wpa_s->global->p2p)
3420 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3ac17eba
JM
3421 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3422 wpa_drv_wps_success_cb(wpa_s, peer_addr);
b22128ef
JM
3423 wpas_group_formation_completed(wpa_s, 1);
3424}
3425
3426
3427int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3428 const char *config_method)
3429{
3430 u16 config_methods;
3431
3432 if (os_strcmp(config_method, "display") == 0)
3433 config_methods = WPS_CONFIG_DISPLAY;
3434 else if (os_strcmp(config_method, "keypad") == 0)
3435 config_methods = WPS_CONFIG_KEYPAD;
3436 else if (os_strcmp(config_method, "pbc") == 0 ||
3437 os_strcmp(config_method, "pushbutton") == 0)
3438 config_methods = WPS_CONFIG_PUSHBUTTON;
3439 else
3440 return -1;
3441
3ac17eba
JM
3442 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3443 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3444 config_methods);
3445 }
3446
9526fd29 3447 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef
JM
3448 return -1;
3449
3450 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3451 config_methods, 0);
3452}
3453
3454
3455int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3456 char *end)
3457{
3458 return p2p_scan_result_text(ies, ies_len, buf, end);
3459}
3460
3461
1cc3a29d
JM
3462static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3463{
3464 if (!wpa_s->pending_action_tx)
3465 return;
3466
3467 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3468 "operation request");
3469 wpabuf_free(wpa_s->pending_action_tx);
3470 wpa_s->pending_action_tx = NULL;
3471}
3472
3473
b22128ef
JM
3474int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3475 enum p2p_discovery_type type)
3476{
1cc3a29d 3477 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef
JM
3478 wpa_s->p2p_long_listen = 0;
3479
3ac17eba
JM
3480 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3481 return wpa_drv_p2p_find(wpa_s, timeout, type);
3482
9526fd29
JM
3483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3484 return -1;
3485
b22128ef
JM
3486 return p2p_find(wpa_s->global->p2p, timeout, type);
3487}
3488
3489
3490void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3491{
1cc3a29d 3492 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef 3493 wpa_s->p2p_long_listen = 0;
9d39057c 3494 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
ef922c4a 3495 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef 3496
3ac17eba
JM
3497 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3498 wpa_drv_p2p_stop_find(wpa_s);
3499 return;
3500 }
3501
9526fd29
JM
3502 if (wpa_s->global->p2p)
3503 p2p_stop_find(wpa_s->global->p2p);
b22128ef
JM
3504
3505 wpas_p2p_remove_pending_group_interface(wpa_s);
3506}
3507
3508
3509static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3510{
3511 struct wpa_supplicant *wpa_s = eloop_ctx;
3512 wpa_s->p2p_long_listen = 0;
3513}
3514
3515
3516int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3517{
3518 int res;
3519
9526fd29
JM
3520 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3521 return -1;
3522
1cc3a29d
JM
3523 wpas_p2p_clear_pending_action_tx(wpa_s);
3524
b22128ef
JM
3525 if (timeout == 0) {
3526 /*
3527 * This is a request for unlimited Listen state. However, at
3528 * least for now, this is mapped to a Listen state for one
3529 * hour.
3530 */
3531 timeout = 3600;
3532 }
3533 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3534 wpa_s->p2p_long_listen = 0;
3535
3536 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3537 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
11fb02be 3538 wpa_s->p2p_long_listen = timeout * 1000;
b22128ef
JM
3539 eloop_register_timeout(timeout, 0,
3540 wpas_p2p_long_listen_timeout,
3541 wpa_s, NULL);
3542 }
3543
3544 return res;
3545}
3546
3547
4c08c0bd 3548int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
b22128ef
JM
3549 u8 *buf, size_t len, int p2p_group)
3550{
4c08c0bd
JM
3551 struct wpabuf *p2p_ie;
3552 int ret;
3553
b22128ef
JM
3554 if (wpa_s->global->p2p_disabled)
3555 return -1;
3556 if (wpa_s->global->p2p == NULL)
3557 return -1;
e1f1509b
JM
3558 if (bss == NULL)
3559 return -1;
b22128ef 3560
4c08c0bd
JM
3561 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3562 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3563 p2p_group, p2p_ie);
3564 wpabuf_free(p2p_ie);
3565
3566 return ret;
b22128ef
JM
3567}
3568
3569
3570int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3571 const u8 *ie, size_t ie_len)
3572{
3573 if (wpa_s->global->p2p_disabled)
3574 return 0;
3575 if (wpa_s->global->p2p == NULL)
3576 return 0;
3577
3578 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3579}
3580
3581
3582void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3583 const u8 *sa, const u8 *bssid,
3584 u8 category, const u8 *data, size_t len, int freq)
3585{
3586 if (wpa_s->global->p2p_disabled)
3587 return;
3588 if (wpa_s->global->p2p == NULL)
3589 return;
3590
3591 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3592 freq);
3593}
3594
3595
3596void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3597{
3598 if (wpa_s->global->p2p_disabled)
3599 return;
3600 if (wpa_s->global->p2p == NULL)
3601 return;
3602
3603 p2p_scan_ie(wpa_s->global->p2p, ies);
3604}
3605
3606
3607void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3608{
3609 p2p_group_deinit(wpa_s->p2p_group);
3610 wpa_s->p2p_group = NULL;
3611}
3612
3613
3614int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3615{
3616 wpa_s->p2p_long_listen = 0;
3617
3ac17eba
JM
3618 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3619 return wpa_drv_p2p_reject(wpa_s, addr);
3620
9526fd29
JM
3621 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3622 return -1;
3623
b22128ef
JM
3624 return p2p_reject(wpa_s->global->p2p, addr);
3625}
3626
3627
3628/* Invite to reinvoke a persistent group */
3629int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3630 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3631{
3632 enum p2p_invite_role role;
3633 u8 *bssid = NULL;
3634
3635 if (ssid->mode == WPAS_MODE_P2P_GO) {
3636 role = P2P_INVITE_ROLE_GO;
3637 if (peer_addr == NULL) {
3638 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3639 "address in invitation command");
3640 return -1;
3641 }
3642 if (wpas_p2p_create_iface(wpa_s)) {
3643 if (wpas_p2p_add_group_interface(wpa_s,
3644 WPA_IF_P2P_GO) < 0) {
3645 wpa_printf(MSG_ERROR, "P2P: Failed to "
3646 "allocate a new interface for the "
3647 "group");
3648 return -1;
3649 }
3650 bssid = wpa_s->pending_interface_addr;
3651 } else
3652 bssid = wpa_s->own_addr;
3653 } else {
3654 role = P2P_INVITE_ROLE_CLIENT;
3655 peer_addr = ssid->bssid;
3656 }
3657 wpa_s->pending_invite_ssid_id = ssid->id;
3658
3ac17eba
JM
3659 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3660 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3661 ssid->ssid, ssid->ssid_len,
3662 go_dev_addr, 1);
3663
9526fd29
JM
3664 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3665 return -1;
3666
b22128ef
JM
3667 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3668 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3669}
3670
3671
3672/* Invite to join an active group */
3673int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3674 const u8 *peer_addr, const u8 *go_dev_addr)
3675{
3676 struct wpa_global *global = wpa_s->global;
3677 enum p2p_invite_role role;
3678 u8 *bssid = NULL;
3679 struct wpa_ssid *ssid;
3680
3681 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3682 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3683 break;
3684 }
3685 if (wpa_s == NULL) {
3686 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3687 return -1;
3688 }
3689
3690 ssid = wpa_s->current_ssid;
3691 if (ssid == NULL) {
3692 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3693 "invitation");
3694 return -1;
3695 }
3696
3697 if (ssid->mode == WPAS_MODE_P2P_GO) {
3698 role = P2P_INVITE_ROLE_ACTIVE_GO;
3699 bssid = wpa_s->own_addr;
3700 if (go_dev_addr == NULL)
2e062d5d 3701 go_dev_addr = wpa_s->parent->own_addr;
b22128ef
JM
3702 } else {
3703 role = P2P_INVITE_ROLE_CLIENT;
3704 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3705 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3706 "invite to current group");
3707 return -1;
3708 }
3709 bssid = wpa_s->bssid;
3710 if (go_dev_addr == NULL &&
3711 !is_zero_ether_addr(wpa_s->go_dev_addr))
3712 go_dev_addr = wpa_s->go_dev_addr;
3713 }
bb79dc72 3714 wpa_s->parent->pending_invite_ssid_id = -1;
b22128ef 3715
3ac17eba
JM
3716 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3717 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3718 ssid->ssid, ssid->ssid_len,
3719 go_dev_addr, 0);
3720
9526fd29
JM
3721 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3722 return -1;
3723
b22128ef 3724 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
d9d6a58c
JM
3725 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3726 go_dev_addr, 0);
b22128ef
JM
3727}
3728
3729
3730void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3731{
3732 struct wpa_ssid *ssid = wpa_s->current_ssid;
3733 const char *ssid_txt;
3734 u8 go_dev_addr[ETH_ALEN];
3735 int persistent;
3736
3737 if (!wpa_s->show_group_started || !ssid)
3738 return;
3739
3740 wpa_s->show_group_started = 0;
3741
3742 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3743 os_memset(go_dev_addr, 0, ETH_ALEN);
3744 if (ssid->bssid_set)
3745 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3746 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3747 ssid->ssid_len);
3748 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3749
a0a9f3b0
JM
3750 if (wpa_s->global->p2p_group_formation == wpa_s)
3751 wpa_s->global->p2p_group_formation = NULL;
3752
b22128ef
JM
3753 if (ssid->passphrase == NULL && ssid->psk_set) {
3754 char psk[65];
3755 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3756 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
3757 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3758 MACSTR "%s",
3759 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3760 MAC2STR(go_dev_addr),
b22128ef
JM
3761 persistent ? " [PERSISTENT]" : "");
3762 } else {
3763 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
3764 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3765 "go_dev_addr=" MACSTR "%s",
3766 wpa_s->ifname, ssid_txt, ssid->frequency,
b22128ef
JM
3767 ssid->passphrase ? ssid->passphrase : "",
3768 MAC2STR(go_dev_addr),
3769 persistent ? " [PERSISTENT]" : "");
3770 }
3771
3772 if (persistent)
3773 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3774 go_dev_addr);
3775}
3776
3777
3778int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3779 u32 interval1, u32 duration2, u32 interval2)
3780{
3ac17eba
JM
3781 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3782 return -1;
9526fd29
JM
3783 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3784 return -1;
3ac17eba 3785
b22128ef
JM
3786 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3787 wpa_s->current_ssid == NULL ||
3788 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3789 return -1;
3790
3791 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3792 wpa_s->own_addr, wpa_s->assoc_freq,
3793 duration1, interval1, duration2, interval2);
3794}
3795
3796
3797int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3798 unsigned int interval)
3799{
3ac17eba
JM
3800 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3801 return -1;
3802
9526fd29
JM
3803 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3804 return -1;
3805
b22128ef
JM
3806 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3807}
3808
3809
3071e181
JM
3810static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3811{
3812 struct wpa_supplicant *wpa_s = eloop_ctx;
3813
3814 if (wpa_s->conf->p2p_group_idle == 0) {
3815 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3816 "disabled");
3817 return;
3818 }
3819
3820 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3821 "group");
3822 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3823 wpas_p2p_group_delete(wpa_s);
3824}
3825
3826
3827static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3828{
3829 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3830 if (wpa_s->conf->p2p_group_idle == 0)
3831 return;
3832
3833 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3834 return;
3835
3836 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3837 wpa_s->conf->p2p_group_idle);
3838 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3839 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3840}
3841
3842
b22128ef
JM
3843void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3844 u16 reason_code, const u8 *ie, size_t ie_len)
3845{
9526fd29 3846 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 3847 return;
3ac17eba
JM
3848 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3849 return;
b22128ef
JM
3850
3851 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3852}
3853
3854
3855void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3856 u16 reason_code, const u8 *ie, size_t ie_len)
3857{
9526fd29 3858 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 3859 return;
3ac17eba
JM
3860 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3861 return;
b22128ef
JM
3862
3863 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3864}
3865
3866
3867void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3868{
3869 struct p2p_data *p2p = wpa_s->global->p2p;
3870
3871 if (p2p == NULL)
3872 return;
3873
4c010834
KL
3874 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3875 return;
3876
b22128ef
JM
3877 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3878 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3879
3880 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3881 u8 pri_dev_type[8];
3882 if (wpa_s->conf->device_type) {
3883 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3884 pri_dev_type) < 0) {
3885 wpa_printf(MSG_ERROR, "P2P: Invalid "
3886 "device_type");
3887 } else
3888 p2p_set_pri_dev_type(p2p, pri_dev_type);
3889 }
3890 }
3891
3892 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3893 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3894 size_t num = 0;
3895 int i;
3896 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3897 if (wpa_s->conf->sec_device_type[i] == NULL)
3898 continue;
3899 if (wps_dev_type_str2bin(
3900 wpa_s->conf->sec_device_type[i],
3901 sec_dev_type[num]) < 0) {
3902 wpa_printf(MSG_ERROR, "P2P: Invalid "
3903 "sec_device_type");
3904 continue;
3905 }
3906 num++;
3907 if (num == P2P_SEC_DEVICE_TYPES)
3908 break;
3909 }
3910 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3911 }
3912
3913 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3914 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3915 char country[3];
3916 country[0] = wpa_s->conf->country[0];
3917 country[1] = wpa_s->conf->country[1];
3918 country[2] = 0x04;
3919 p2p_set_country(p2p, country);
3920 }
3921
3922 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3923 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3924 wpa_s->conf->p2p_ssid_postfix ?
3925 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3926 0);
3927 }
0f66abd2
SS
3928
3929 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
3930 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
b22128ef 3931}
aefb53bd
JM
3932
3933
3934int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
3935 int duration)
3936{
3937 if (!wpa_s->ap_iface)
3938 return -1;
3939 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
3940 duration);
3941}
72044390
JM
3942
3943
3944int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
3945{
9526fd29 3946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
72044390
JM
3947 return -1;
3948 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3949 return -1;
3950
3951 wpa_s->global->cross_connection = enabled;
3952 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
3953
3954 if (!enabled) {
3955 struct wpa_supplicant *iface;
3956
3957 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
3958 {
3959 if (iface->cross_connect_enabled == 0)
3960 continue;
3961
3962 iface->cross_connect_enabled = 0;
3963 iface->cross_connect_in_use = 0;
3964 wpa_msg(iface->parent, MSG_INFO,
3965 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3966 iface->ifname, iface->cross_connect_uplink);
3967 }
3968 }
3969
3970 return 0;
3971}
3972
3973
3974static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
3975{
3976 struct wpa_supplicant *iface;
3977
3978 if (!uplink->global->cross_connection)
3979 return;
3980
3981 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3982 if (!iface->cross_connect_enabled)
3983 continue;
3984 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3985 0)
3986 continue;
3987 if (iface->ap_iface == NULL)
3988 continue;
3989 if (iface->cross_connect_in_use)
3990 continue;
3991
3992 iface->cross_connect_in_use = 1;
3993 wpa_msg(iface->parent, MSG_INFO,
3994 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3995 iface->ifname, iface->cross_connect_uplink);
3996 }
3997}
3998
3999
4000static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4001{
4002 struct wpa_supplicant *iface;
4003
4004 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4005 if (!iface->cross_connect_enabled)
4006 continue;
4007 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4008 0)
4009 continue;
4010 if (!iface->cross_connect_in_use)
4011 continue;
4012
4013 wpa_msg(iface->parent, MSG_INFO,
4014 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4015 iface->ifname, iface->cross_connect_uplink);
4016 iface->cross_connect_in_use = 0;
4017 }
4018}
4019
4020
4021void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4022{
4023 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4024 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4025 wpa_s->cross_connect_disallowed)
4026 wpas_p2p_disable_cross_connect(wpa_s);
4027 else
4028 wpas_p2p_enable_cross_connect(wpa_s);
3071e181
JM
4029 if (!wpa_s->ap_iface)
4030 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
72044390
JM
4031}
4032
4033
4034void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4035{
4036 wpas_p2p_disable_cross_connect(wpa_s);
3071e181
JM
4037 if (!wpa_s->ap_iface)
4038 wpas_p2p_set_group_idle_timeout(wpa_s);
72044390
JM
4039}
4040
4041
4042static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4043{
4044 struct wpa_supplicant *iface;
4045
4046 if (!wpa_s->global->cross_connection)
4047 return;
4048
4049 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4050 if (iface == wpa_s)
4051 continue;
4052 if (iface->drv_flags &
4053 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4054 continue;
4055 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4056 continue;
4057
4058 wpa_s->cross_connect_enabled = 1;
4059 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4060 sizeof(wpa_s->cross_connect_uplink));
4061 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4062 "%s to %s whenever uplink is available",
4063 wpa_s->ifname, wpa_s->cross_connect_uplink);
4064
4065 if (iface->ap_iface || iface->current_ssid == NULL ||
4066 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4067 iface->cross_connect_disallowed ||
4068 iface->wpa_state != WPA_COMPLETED)
4069 break;
4070
4071 wpa_s->cross_connect_in_use = 1;
4072 wpa_msg(wpa_s->parent, MSG_INFO,
4073 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4074 wpa_s->ifname, wpa_s->cross_connect_uplink);
4075 break;
4076 }
4077}
b73bf0a7
JM
4078
4079
4080int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4081{
4082 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4083 !wpa_s->p2p_in_provisioning)
4084 return 0; /* not P2P client operation */
4085
4086 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4087 "session overlap");
148bb37f
JM
4088 if (wpa_s != wpa_s->parent)
4089 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
0a14e3ec
SS
4090
4091 if (wpa_s->global->p2p)
4092 p2p_group_formation_failed(wpa_s->global->p2p);
4093
4094 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4095 wpa_s->parent, NULL);
4096
b73bf0a7
JM
4097 wpas_group_formation_completed(wpa_s, 0);
4098 return 1;
4099}
b5c9da8d
JM
4100
4101
4102void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4103{
4104 struct p2p_channels chan;
4105
4106 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4107 return;
4108
4109 os_memset(&chan, 0, sizeof(chan));
4110 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4111 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4112 "channel list");
4113 return;
4114 }
4115
4116 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4117}
59eba7a2
JM
4118
4119
4120int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4121{
4122 struct wpa_global *global = wpa_s->global;
4123 int found = 0;
231bbd03 4124 const u8 *peer;
59eba7a2 4125
9526fd29
JM
4126 if (global->p2p == NULL)
4127 return -1;
4128
59eba7a2
JM
4129 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4130
4131 if (wpa_s->pending_interface_name[0] &&
4132 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4133 found = 1;
4134
231bbd03
SS
4135 peer = p2p_get_go_neg_peer(global->p2p);
4136 if (peer) {
4137 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4138 MACSTR, MAC2STR(peer));
4139 p2p_unauthorize(global->p2p, peer);
4140 }
4141
59eba7a2
JM
4142 wpas_p2p_stop_find(wpa_s);
4143
4144 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4145 if (wpa_s == global->p2p_group_formation &&
a0a9f3b0
JM
4146 (wpa_s->p2p_in_provisioning ||
4147 wpa_s->parent->pending_interface_type ==
4148 WPA_IF_P2P_CLIENT)) {
59eba7a2
JM
4149 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4150 "formation found - cancelling",
4151 wpa_s->ifname);
4152 found = 1;
45fee6f0
SS
4153 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4154 wpa_s->parent, NULL);
59eba7a2
JM
4155 wpas_p2p_group_delete(wpa_s);
4156 break;
4157 }
4158 }
4159
4160 if (!found) {
4161 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4162 return -1;
4163 }
4164
4165 return 0;
4166}
c973f386
JM
4167
4168
4169void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4170{
4171 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4172 return;
4173
4174 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4175 "being available anymore");
4176 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4177 wpas_p2p_group_delete(wpa_s);
4178}
7cfc4ac3
AGS
4179
4180
4181void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4182 int freq_24, int freq_5, int freq_overall)
4183{
4184 struct p2p_data *p2p = wpa_s->global->p2p;
4185 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4186 return;
4187 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4188}
9d562b79
SS
4189
4190
4191int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4192{
4193 u8 peer[ETH_ALEN];
4194 struct p2p_data *p2p = wpa_s->global->p2p;
4195
4196 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4197 return -1;
4198
4199 if (hwaddr_aton(addr, peer))
4200 return -1;
4201
4202 return p2p_unauthorize(p2p, peer);
4203}
3103f345
JB
4204
4205
4206/**
4207 * wpas_p2p_disconnect - Disconnect from a P2P Group
4208 * @wpa_s: Pointer to wpa_supplicant data
4209 * Returns: 0 on success, -1 on failure
4210 *
4211 * This can be used to disconnect from a group in which the local end is a P2P
4212 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4213 * virtual network interface was created for this group, that interface will be
4214 * removed. Otherwise, only the configured P2P group network will be removed
4215 * from the interface.
4216 */
4217int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4218{
4219
4220 if (wpa_s == NULL)
4221 return -1;
4222
4223 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4224 wpas_p2p_group_delete(wpa_s);
4225
4226 return 0;
4227}