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