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