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