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