]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/p2p_supplicant.c
P2P: Allow all channels in case of multi channel concurrency
[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
84286a22
SDU
42#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
c8106615
JM
44#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
49#define P2P_MAX_CLIENT_IDLE 10
50#endif /* P2P_MAX_CLIENT_IDLE */
51
361cdf34
JM
52#ifndef P2P_MAX_INITIAL_CONN_WAIT
53/*
54 * How many seconds to wait for initial 4-way handshake to get completed after
55 * WPS provisioning step.
56 */
57#define P2P_MAX_INITIAL_CONN_WAIT 10
58#endif /* P2P_MAX_INITIAL_CONN_WAIT */
59
05a77b3b
JM
60#ifndef P2P_CONCURRENT_SEARCH_DELAY
61#define P2P_CONCURRENT_SEARCH_DELAY 500
62#endif /* P2P_CONCURRENT_SEARCH_DELAY */
63
8dba4aef
JM
64enum p2p_group_removal_reason {
65 P2P_GROUP_REMOVAL_UNKNOWN,
66 P2P_GROUP_REMOVAL_SILENT,
67 P2P_GROUP_REMOVAL_FORMATION_FAILED,
68 P2P_GROUP_REMOVAL_REQUESTED,
69 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
70 P2P_GROUP_REMOVAL_UNAVAILABLE,
71 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
72};
73
9b1ab931 74
b22128ef
JM
75static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
76static struct wpa_supplicant *
77wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
78 int go);
79static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
84286a22 80static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
ef922c4a 81static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
108def93 82static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
b31be3a0
JM
83 const u8 *dev_addr, enum p2p_wps_method wps_method,
84 int auto_join);
b22128ef 85static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
72044390 86static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
3071e181
JM
87static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
88static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
77dfafd0
JM
89static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
90 void *timeout_ctx);
aa9bb764
JM
91static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
92 int group_added);
8713a2e6 93static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
b22128ef
JM
94
95
96static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
97 struct wpa_scan_results *scan_res)
98{
99 size_t i;
100
9526fd29 101 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef
JM
102 return;
103
104 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
105 (int) scan_res->num);
106
107 for (i = 0; i < scan_res->num; i++) {
108 struct wpa_scan_res *bss = scan_res->res[i];
c5f10e80
JM
109 struct os_time time_tmp_age, entry_ts;
110 time_tmp_age.sec = bss->age / 1000;
111 time_tmp_age.usec = (bss->age % 1000) * 1000;
112 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
b22128ef 113 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
c5f10e80 114 bss->freq, &entry_ts, bss->level,
b22128ef
JM
115 (const u8 *) (bss + 1),
116 bss->ie_len) > 0)
9bcf9541 117 break;
b22128ef
JM
118 }
119
120 p2p_scan_res_handled(wpa_s->global->p2p);
121}
122
123
046ef4aa
JMB
124static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
125 unsigned int num_req_dev_types,
360182ed 126 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
b22128ef
JM
127{
128 struct wpa_supplicant *wpa_s = ctx;
99fcd404 129 struct wpa_supplicant *ifs;
b22128ef
JM
130 struct wpa_driver_scan_params params;
131 int ret;
132 struct wpabuf *wps_ie, *ies;
133 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
206e1f42 134 size_t ielen;
b22128ef 135
9526fd29
JM
136 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
137 return -1;
138
99fcd404
JM
139 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
140 if (ifs->sta_scan_pending &&
141 wpas_p2p_in_progress(wpa_s) == 2) {
142 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
143 "pending station mode scan to be "
144 "completed on interface %s", ifs->ifname);
e665ca9a 145 wpa_s->global->p2p_cb_on_scan_complete = 1;
99fcd404
JM
146 wpa_supplicant_req_scan(ifs, 0, 0);
147 return 1;
148 }
149 }
150
b22128ef
JM
151 os_memset(&params, 0, sizeof(params));
152
153 /* P2P Wildcard SSID */
154 params.num_ssids = 1;
155 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
156 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
157
158 wpa_s->wps->dev.p2p = 1;
360182ed
JM
159 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
160 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
046ef4aa 161 num_req_dev_types, req_dev_types);
b22128ef
JM
162 if (wps_ie == NULL)
163 return -1;
164
206e1f42
JM
165 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
166 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
b22128ef
JM
167 if (ies == NULL) {
168 wpabuf_free(wps_ie);
169 return -1;
170 }
171 wpabuf_put_buf(ies, wps_ie);
172 wpabuf_free(wps_ie);
173
6d92fa6e 174 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
b22128ef 175
47185fc7 176 params.p2p_probe = 1;
b22128ef
JM
177 params.extra_ies = wpabuf_head(ies);
178 params.extra_ies_len = wpabuf_len(ies);
179
180 switch (type) {
181 case P2P_SCAN_SOCIAL:
182 params.freqs = social_channels;
183 break;
184 case P2P_SCAN_FULL:
185 break;
b22128ef
JM
186 case P2P_SCAN_SOCIAL_PLUS_ONE:
187 social_channels[3] = freq;
188 params.freqs = social_channels;
189 break;
190 }
191
17fbb751 192 ret = wpa_drv_scan(wpa_s, &params);
b22128ef
JM
193
194 wpabuf_free(ies);
195
39185dfa 196 if (ret) {
58d3760e
JM
197 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
198 if (ifs->scanning ||
199 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
200 wpa_s->global->p2p_cb_on_scan_complete = 1;
201 ret = 1;
202 break;
203 }
39185dfa 204 }
5b376333
JM
205 } else
206 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
0c96fd6d 207
b22128ef
JM
208 return ret;
209}
210
211
b22128ef
JM
212static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
213{
214 switch (p2p_group_interface) {
215 case P2P_GROUP_INTERFACE_PENDING:
216 return WPA_IF_P2P_GROUP;
217 case P2P_GROUP_INTERFACE_GO:
218 return WPA_IF_P2P_GO;
219 case P2P_GROUP_INTERFACE_CLIENT:
220 return WPA_IF_P2P_CLIENT;
221 }
222
223 return WPA_IF_P2P_GROUP;
224}
225
226
6c0da49f
JM
227static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
228 const u8 *ssid,
229 size_t ssid_len, int *go)
230{
231 struct wpa_ssid *s;
232
233 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
234 for (s = wpa_s->conf->ssid; s; s = s->next) {
235 if (s->disabled != 0 || !s->p2p_group ||
236 s->ssid_len != ssid_len ||
237 os_memcmp(ssid, s->ssid, ssid_len) != 0)
238 continue;
239 if (s->mode == WPAS_MODE_P2P_GO &&
240 s != wpa_s->current_ssid)
241 continue;
242 if (go)
243 *go = s->mode == WPAS_MODE_P2P_GO;
244 return wpa_s;
245 }
246 }
247
248 return NULL;
249}
250
251
8dba4aef
JM
252static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
253 enum p2p_group_removal_reason removal_reason)
b22128ef
JM
254{
255 struct wpa_ssid *ssid;
256 char *gtype;
3071e181
JM
257 const char *reason;
258
b22128ef
JM
259 ssid = wpa_s->current_ssid;
260 if (ssid == NULL) {
261 /*
262 * The current SSID was not known, but there may still be a
0c802384
JM
263 * pending P2P group interface waiting for provisioning or a
264 * P2P group that is trying to reconnect.
b22128ef
JM
265 */
266 ssid = wpa_s->conf->ssid;
267 while (ssid) {
6d22a377 268 if (ssid->p2p_group && ssid->disabled != 2)
b22128ef
JM
269 break;
270 ssid = ssid->next;
271 }
6f1ca696
JM
272 if (ssid == NULL &&
273 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
274 {
30ee7692
MN
275 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
276 "not found");
277 return -1;
278 }
b22128ef
JM
279 }
280 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
281 gtype = "GO";
282 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
283 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
284 wpa_s->reassociate = 0;
285 wpa_s->disconnected = 1;
286 wpa_supplicant_deauthenticate(wpa_s,
287 WLAN_REASON_DEAUTH_LEAVING);
288 gtype = "client";
289 } else
290 gtype = "GO";
72044390
JM
291 if (wpa_s->cross_connect_in_use) {
292 wpa_s->cross_connect_in_use = 0;
293 wpa_msg(wpa_s->parent, MSG_INFO,
294 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
295 wpa_s->ifname, wpa_s->cross_connect_uplink);
296 }
8dba4aef 297 switch (removal_reason) {
3071e181
JM
298 case P2P_GROUP_REMOVAL_REQUESTED:
299 reason = " reason=REQUESTED";
300 break;
8dba4aef
JM
301 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
302 reason = " reason=FORMATION_FAILED";
303 break;
3071e181
JM
304 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
305 reason = " reason=IDLE";
306 break;
c973f386
JM
307 case P2P_GROUP_REMOVAL_UNAVAILABLE:
308 reason = " reason=UNAVAILABLE";
309 break;
3fc14102
JM
310 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
311 reason = " reason=GO_ENDING_SESSION";
312 break;
3071e181
JM
313 default:
314 reason = "";
315 break;
316 }
8dba4aef 317 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
aa9bb764
JM
318 wpa_msg(wpa_s->parent, MSG_INFO,
319 P2P_EVENT_GROUP_REMOVED "%s %s%s",
320 wpa_s->ifname, gtype, reason);
321 }
408af93e 322
dddc7045
JM
323 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
324 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
77dfafd0
JM
325 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
326 wpa_s->parent, NULL) > 0)
327 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
328 "timeout");
a7a30b90 329
8dba4aef 330 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
408af93e
JB
331 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
332
b22128ef
JM
333 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
334 struct wpa_global *global;
335 char *ifname;
336 enum wpa_driver_if_type type;
337 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
338 wpa_s->ifname);
339 global = wpa_s->global;
340 ifname = os_strdup(wpa_s->ifname);
341 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
df509539 342 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
b22128ef
JM
343 wpa_s = global->ifaces;
344 if (wpa_s && ifname)
345 wpa_drv_if_remove(wpa_s, type, ifname);
346 os_free(ifname);
0aadd568 347 return 1;
b22128ef
JM
348 }
349
350 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
351 if (ssid && (ssid->p2p_group ||
352 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
353 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
354 int id = ssid->id;
20a0b03d
JM
355 if (ssid == wpa_s->current_ssid) {
356 wpa_sm_set_config(wpa_s->wpa, NULL);
357 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
b22128ef 358 wpa_s->current_ssid = NULL;
20a0b03d 359 }
c2762e41
JS
360 /*
361 * Networks objects created during any P2P activities are not
362 * exposed out as they might/will confuse certain non-P2P aware
363 * applications since these network objects won't behave like
364 * regular ones.
365 *
366 * Likewise, we don't send out network removed signals for such
367 * network objects.
368 */
b22128ef
JM
369 wpa_config_remove_network(wpa_s->conf, id);
370 wpa_supplicant_clear_status(wpa_s);
433cd2ce 371 wpa_supplicant_cancel_sched_scan(wpa_s);
83218d20 372 wpa_s->sta_scan_pending = 0;
b22128ef
JM
373 } else {
374 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
375 "found");
376 }
3c29244e
EP
377 if (wpa_s->ap_iface)
378 wpa_supplicant_ap_deinit(wpa_s);
379 else
380 wpa_drv_deinit_p2p_cli(wpa_s);
30ee7692
MN
381
382 return 0;
b22128ef
JM
383}
384
385
386static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
387 u8 *go_dev_addr,
388 const u8 *ssid, size_t ssid_len)
389{
390 struct wpa_bss *bss;
391 const u8 *bssid;
392 struct wpabuf *p2p;
393 u8 group_capab;
394 const u8 *addr;
395
396 if (wpa_s->go_params)
397 bssid = wpa_s->go_params->peer_interface_addr;
398 else
399 bssid = wpa_s->bssid;
400
401 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
402 if (bss == NULL) {
403 u8 iface_addr[ETH_ALEN];
404 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
405 iface_addr) == 0)
406 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
407 }
408 if (bss == NULL) {
409 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
410 "group is persistent - BSS " MACSTR " not found",
411 MAC2STR(bssid));
412 return 0;
413 }
414
415 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
416 if (p2p == NULL) {
417 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
418 "group is persistent - BSS " MACSTR
419 " did not include P2P IE", MAC2STR(bssid));
420 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
421 (u8 *) (bss + 1), bss->ie_len);
422 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
423 ((u8 *) bss + 1) + bss->ie_len,
424 bss->beacon_ie_len);
425 return 0;
426 }
427
428 group_capab = p2p_get_group_capab(p2p);
429 addr = p2p_get_go_dev_addr(p2p);
430 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
431 "group_capab=0x%x", group_capab);
432 if (addr) {
433 os_memcpy(go_dev_addr, addr, ETH_ALEN);
434 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
435 MAC2STR(addr));
436 } else
437 os_memset(go_dev_addr, 0, ETH_ALEN);
438 wpabuf_free(p2p);
439
440 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
441 "go_dev_addr=" MACSTR,
442 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
443
444 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
445}
446
447
4b6baa2f
JMB
448static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
449 struct wpa_ssid *ssid,
450 const u8 *go_dev_addr)
b22128ef
JM
451{
452 struct wpa_ssid *s;
453 int changed = 0;
454
455 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
456 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
457 for (s = wpa_s->conf->ssid; s; s = s->next) {
458 if (s->disabled == 2 &&
459 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
460 s->ssid_len == ssid->ssid_len &&
461 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
462 break;
463 }
464
465 if (s) {
466 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
467 "entry");
468 if (ssid->passphrase && !s->passphrase)
469 changed = 1;
470 else if (ssid->passphrase && s->passphrase &&
471 os_strcmp(ssid->passphrase, s->passphrase) != 0)
472 changed = 1;
473 } else {
474 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
475 "entry");
476 changed = 1;
477 s = wpa_config_add_network(wpa_s->conf);
478 if (s == NULL)
4b6baa2f 479 return -1;
c2762e41
JS
480
481 /*
482 * Instead of network_added we emit persistent_group_added
483 * notification. Also to keep the defense checks in
484 * persistent_group obj registration method, we set the
485 * relevant flags in s to designate it as a persistent group.
486 */
487 s->p2p_group = 1;
488 s->p2p_persistent_group = 1;
489 wpas_notify_persistent_group_added(wpa_s, s);
b22128ef
JM
490 wpa_config_set_network_defaults(s);
491 }
492
493 s->p2p_group = 1;
494 s->p2p_persistent_group = 1;
495 s->disabled = 2;
496 s->bssid_set = 1;
497 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
498 s->mode = ssid->mode;
499 s->auth_alg = WPA_AUTH_ALG_OPEN;
500 s->key_mgmt = WPA_KEY_MGMT_PSK;
501 s->proto = WPA_PROTO_RSN;
502 s->pairwise_cipher = WPA_CIPHER_CCMP;
d1c8ac88 503 s->export_keys = 1;
b22128ef
JM
504 if (ssid->passphrase) {
505 os_free(s->passphrase);
506 s->passphrase = os_strdup(ssid->passphrase);
507 }
508 if (ssid->psk_set) {
509 s->psk_set = 1;
510 os_memcpy(s->psk, ssid->psk, 32);
511 }
512 if (s->passphrase && !s->psk_set)
513 wpa_config_update_psk(s);
514 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
515 os_free(s->ssid);
516 s->ssid = os_malloc(ssid->ssid_len);
517 }
518 if (s->ssid) {
519 s->ssid_len = ssid->ssid_len;
520 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
521 }
522
523#ifndef CONFIG_NO_CONFIG_WRITE
524 if (changed && wpa_s->conf->update_config &&
525 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
526 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
527 }
528#endif /* CONFIG_NO_CONFIG_WRITE */
4b6baa2f
JMB
529
530 return s->id;
b22128ef
JM
531}
532
533
fbdcfd57
JM
534static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
535 const u8 *addr)
536{
537 struct wpa_ssid *ssid, *s;
538 u8 *n;
539 size_t i;
22316795 540 int found = 0;
fbdcfd57
JM
541
542 ssid = wpa_s->current_ssid;
543 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
544 !ssid->p2p_persistent_group)
545 return;
546
547 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
548 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
549 continue;
550
551 if (s->ssid_len == ssid->ssid_len &&
552 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
553 break;
554 }
555
556 if (s == NULL)
557 return;
558
559 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
560 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
22316795
JM
561 ETH_ALEN) != 0)
562 continue;
563
564 if (i == s->num_p2p_clients - 1)
565 return; /* already the most recent entry */
566
567 /* move the entry to mark it most recent */
568 os_memmove(s->p2p_client_list + i * ETH_ALEN,
569 s->p2p_client_list + (i + 1) * ETH_ALEN,
570 (s->num_p2p_clients - i - 1) * ETH_ALEN);
571 os_memcpy(s->p2p_client_list +
572 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
573 found = 1;
574 break;
fbdcfd57
JM
575 }
576
b4a5dfa9 577 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
22316795
JM
578 n = os_realloc_array(s->p2p_client_list,
579 s->num_p2p_clients + 1, ETH_ALEN);
580 if (n == NULL)
581 return;
582 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
583 s->p2p_client_list = n;
584 s->num_p2p_clients++;
b4a5dfa9
JM
585 } else if (!found) {
586 /* Not enough room for an additional entry - drop the oldest
587 * entry */
588 os_memmove(s->p2p_client_list,
589 s->p2p_client_list + ETH_ALEN,
590 (s->num_p2p_clients - 1) * ETH_ALEN);
591 os_memcpy(s->p2p_client_list +
592 (s->num_p2p_clients - 1) * ETH_ALEN,
593 addr, ETH_ALEN);
22316795 594 }
fbdcfd57
JM
595
596#ifndef CONFIG_NO_CONFIG_WRITE
597 if (wpa_s->parent->conf->update_config &&
598 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
599 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
600#endif /* CONFIG_NO_CONFIG_WRITE */
601}
602
603
b22128ef
JM
604static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
605 int success)
606{
607 struct wpa_ssid *ssid;
608 const char *ssid_txt;
609 int client;
610 int persistent;
611 u8 go_dev_addr[ETH_ALEN];
4b6baa2f 612 int network_id = -1;
b22128ef
JM
613
614 /*
615 * This callback is likely called for the main interface. Update wpa_s
616 * to use the group interface if a new interface was created for the
617 * group.
618 */
619 if (wpa_s->global->p2p_group_formation)
620 wpa_s = wpa_s->global->p2p_group_formation;
d1b024c9 621 wpa_s->global->p2p_group_formation = NULL;
b22128ef
JM
622 wpa_s->p2p_in_provisioning = 0;
623
624 if (!success) {
625 wpa_msg(wpa_s->parent, MSG_INFO,
626 P2P_EVENT_GROUP_FORMATION_FAILURE);
8dba4aef
JM
627 wpas_p2p_group_delete(wpa_s,
628 P2P_GROUP_REMOVAL_FORMATION_FAILED);
b22128ef
JM
629 return;
630 }
631
632 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
633
634 ssid = wpa_s->current_ssid;
635 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
636 ssid->mode = WPAS_MODE_P2P_GO;
637 p2p_group_notif_formation_done(wpa_s->p2p_group);
638 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
639 }
640
641 persistent = 0;
642 if (ssid) {
643 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
644 client = ssid->mode == WPAS_MODE_INFRA;
645 if (ssid->mode == WPAS_MODE_P2P_GO) {
646 persistent = ssid->p2p_persistent_group;
d7e70476 647 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
b22128ef
JM
648 ETH_ALEN);
649 } else
650 persistent = wpas_p2p_persistent_group(wpa_s,
651 go_dev_addr,
652 ssid->ssid,
653 ssid->ssid_len);
654 } else {
655 ssid_txt = "";
656 client = wpa_s->p2p_group_interface ==
657 P2P_GROUP_INTERFACE_CLIENT;
62c0d27e 658 os_memset(go_dev_addr, 0, ETH_ALEN);
b22128ef
JM
659 }
660
661 wpa_s->show_group_started = 0;
662 if (client) {
663 /*
664 * Indicate event only after successfully completed 4-way
665 * handshake, i.e., when the interface is ready for data
666 * packets.
667 */
668 wpa_s->show_group_started = 1;
669 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
670 char psk[65];
671 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
672 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
673 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
674 "%s",
675 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
676 MAC2STR(go_dev_addr),
b22128ef 677 persistent ? " [PERSISTENT]" : "");
72044390 678 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 679 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
680 } else {
681 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
682 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
683 "go_dev_addr=" MACSTR "%s",
d394a22f 684 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
b22128ef
JM
685 ssid && ssid->passphrase ? ssid->passphrase : "",
686 MAC2STR(go_dev_addr),
687 persistent ? " [PERSISTENT]" : "");
72044390 688 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 689 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
690 }
691
692 if (persistent)
4b6baa2f
JMB
693 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
694 ssid, go_dev_addr);
21fe9e75 695 if (network_id < 0 && ssid)
4b6baa2f
JMB
696 network_id = ssid->id;
697 if (!client)
698 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
b22128ef
JM
699}
700
701
24f6497c
JM
702static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
703 unsigned int freq,
704 const u8 *dst, const u8 *src,
705 const u8 *bssid,
706 const u8 *data, size_t data_len,
707 enum offchannel_send_action_result
708 result)
534525ff 709{
24f6497c 710 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
b22128ef 711
9526fd29 712 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef 713 return;
3ac17eba
JM
714 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
715 return;
b22128ef 716
24f6497c
JM
717 switch (result) {
718 case OFFCHANNEL_SEND_ACTION_SUCCESS:
719 res = P2P_SEND_ACTION_SUCCESS;
720 break;
721 case OFFCHANNEL_SEND_ACTION_NO_ACK:
722 res = P2P_SEND_ACTION_NO_ACK;
723 break;
724 case OFFCHANNEL_SEND_ACTION_FAILED:
725 res = P2P_SEND_ACTION_FAILED;
726 break;
b22128ef
JM
727 }
728
24f6497c 729 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
b22128ef 730
f63b8542
JM
731 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
732 wpa_s->pending_pd_before_join &&
24f6497c 733 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
175171ac
JM
734 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
735 wpa_s->p2p_fallback_to_go_neg) {
b22128ef 736 wpa_s->pending_pd_before_join = 0;
175171ac
JM
737 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
738 "during p2p_connect-auto");
739 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
740 return;
b22128ef
JM
741 }
742}
743
744
745static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
746 const u8 *src, const u8 *bssid, const u8 *buf,
747 size_t len, unsigned int wait_time)
748{
749 struct wpa_supplicant *wpa_s = ctx;
24f6497c
JM
750 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
751 wait_time,
b106173a 752 wpas_p2p_send_action_tx_status, 1);
b22128ef
JM
753}
754
755
756static void wpas_send_action_done(void *ctx)
757{
758 struct wpa_supplicant *wpa_s = ctx;
24f6497c 759 offchannel_send_action_done(wpa_s);
b22128ef
JM
760}
761
762
763static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
764 struct p2p_go_neg_results *params)
765{
766 if (wpa_s->go_params == NULL) {
767 wpa_s->go_params = os_malloc(sizeof(*params));
768 if (wpa_s->go_params == NULL)
769 return -1;
770 }
771 os_memcpy(wpa_s->go_params, params, sizeof(*params));
772 return 0;
773}
774
775
776static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
777 struct p2p_go_neg_results *res)
778{
3b29972c
JM
779 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
780 MAC2STR(res->peer_interface_addr));
e9a7ae41
JM
781 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
782 res->ssid, res->ssid_len);
b22128ef
JM
783 wpa_supplicant_ap_deinit(wpa_s);
784 wpas_copy_go_neg_results(wpa_s, res);
785 if (res->wps_method == WPS_PBC)
3b29972c 786 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
3c5126a4
JM
787 else {
788 u16 dev_pw_id = DEV_PW_DEFAULT;
789 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
790 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
b22128ef 791 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
3c5126a4
JM
792 wpa_s->p2p_pin, 1, dev_pw_id);
793 }
b22128ef
JM
794}
795
796
797static void p2p_go_configured(void *ctx, void *data)
798{
799 struct wpa_supplicant *wpa_s = ctx;
800 struct p2p_go_neg_results *params = data;
801 struct wpa_ssid *ssid;
4b6baa2f 802 int network_id = -1;
b22128ef
JM
803
804 ssid = wpa_s->current_ssid;
805 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
806 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
d1b024c9
JM
807 if (wpa_s->global->p2p_group_formation == wpa_s)
808 wpa_s->global->p2p_group_formation = NULL;
bb4d4deb
MH
809 if (os_strlen(params->passphrase) > 0) {
810 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
811 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
812 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
813 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
814 ssid->frequency, params->passphrase,
815 MAC2STR(wpa_s->global->p2p_dev_addr),
816 params->persistent_group ? " [PERSISTENT]" :
817 "");
818 } else {
819 char psk[65];
820 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
821 sizeof(params->psk));
822 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
823 "%s GO ssid=\"%s\" freq=%d psk=%s "
824 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
825 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
826 ssid->frequency, psk,
827 MAC2STR(wpa_s->global->p2p_dev_addr),
828 params->persistent_group ? " [PERSISTENT]" :
829 "");
830 }
4b6baa2f 831
b22128ef 832 if (params->persistent_group)
4b6baa2f 833 network_id = wpas_p2p_store_persistent_group(
b22128ef 834 wpa_s->parent, ssid,
d7e70476 835 wpa_s->global->p2p_dev_addr);
4b6baa2f
JMB
836 if (network_id < 0)
837 network_id = ssid->id;
838 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
72044390 839 wpas_p2p_cross_connect_setup(wpa_s);
3071e181 840 wpas_p2p_set_group_idle_timeout(wpa_s);
b22128ef
JM
841 return;
842 }
843
844 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
845 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
846 params->peer_interface_addr)) {
847 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
848 "filtering");
849 return;
850 }
851 if (params->wps_method == WPS_PBC)
d601247c 852 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
c3daaf33 853 params->peer_device_addr);
b22128ef
JM
854 else if (wpa_s->p2p_pin[0])
855 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
c423708f 856 wpa_s->p2p_pin, NULL, 0, 0);
b22128ef
JM
857 os_free(wpa_s->go_params);
858 wpa_s->go_params = NULL;
859}
860
861
862static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
863 struct p2p_go_neg_results *params,
864 int group_formation)
865{
866 struct wpa_ssid *ssid;
867
ac06fb12
JM
868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
869 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
870 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
871 "results");
b22128ef 872 return;
ac06fb12 873 }
b22128ef
JM
874
875 ssid = wpa_config_add_network(wpa_s->conf);
ac06fb12
JM
876 if (ssid == NULL) {
877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
b22128ef 878 return;
ac06fb12 879 }
b22128ef 880
a9e02d59
JM
881 wpa_s->show_group_started = 0;
882
b22128ef
JM
883 wpa_config_set_network_defaults(ssid);
884 ssid->temporary = 1;
885 ssid->p2p_group = 1;
886 ssid->p2p_persistent_group = params->persistent_group;
887 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
888 WPAS_MODE_P2P_GO;
889 ssid->frequency = params->freq;
7aeac985 890 ssid->ht40 = params->ht40;
b22128ef
JM
891 ssid->ssid = os_zalloc(params->ssid_len + 1);
892 if (ssid->ssid) {
893 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
894 ssid->ssid_len = params->ssid_len;
895 }
896 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
897 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
898 ssid->proto = WPA_PROTO_RSN;
899 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
bb4d4deb
MH
900 if (os_strlen(params->passphrase) > 0) {
901 ssid->passphrase = os_strdup(params->passphrase);
902 if (ssid->passphrase == NULL) {
903 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
904 "passphrase for GO");
905 wpa_config_remove_network(wpa_s->conf, ssid->id);
906 return;
907 }
908 } else
909 ssid->passphrase = NULL;
30c371e8
MH
910 ssid->psk_set = params->psk_set;
911 if (ssid->psk_set)
912 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
6350a148 913 else if (ssid->passphrase)
30c371e8 914 wpa_config_update_psk(ssid);
462a7439 915 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
b22128ef
JM
916
917 wpa_s->ap_configured_cb = p2p_go_configured;
918 wpa_s->ap_configured_cb_ctx = wpa_s;
919 wpa_s->ap_configured_cb_data = wpa_s->go_params;
7dcdcfd6 920 wpa_s->connect_without_scan = ssid;
b22128ef
JM
921 wpa_s->reassociate = 1;
922 wpa_s->disconnected = 0;
ac06fb12
JM
923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
924 "start GO)");
b22128ef
JM
925 wpa_supplicant_req_scan(wpa_s, 0, 0);
926}
927
928
929static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
930 const struct wpa_supplicant *src)
931{
932 struct wpa_config *d;
933 const struct wpa_config *s;
934
935 d = dst->conf;
936 s = src->conf;
937
938#define C(n) if (s->n) d->n = os_strdup(s->n)
939 C(device_name);
940 C(manufacturer);
941 C(model_name);
942 C(model_number);
943 C(serial_number);
b22128ef
JM
944 C(config_methods);
945#undef C
3071e181 946
2f646b6e
JB
947 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
948 os_memcpy(d->sec_device_type, s->sec_device_type,
949 sizeof(d->sec_device_type));
950 d->num_sec_device_types = s->num_sec_device_types;
951
3071e181 952 d->p2p_group_idle = s->p2p_group_idle;
b029bd33 953 d->p2p_intra_bss = s->p2p_intra_bss;
acc247b2 954 d->persistent_reconnect = s->persistent_reconnect;
f571b593 955 d->max_num_sta = s->max_num_sta;
1298c145 956 d->pbc_in_m1 = s->pbc_in_m1;
b22128ef
JM
957}
958
959
960static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
961 enum wpa_driver_if_type type)
962{
963 char ifname[120], force_ifname[120];
964
965 if (wpa_s->pending_interface_name[0]) {
966 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
967 "- skip creation of a new one");
968 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
969 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
970 "unknown?! ifname='%s'",
971 wpa_s->pending_interface_name);
972 return -1;
973 }
974 return 0;
975 }
976
a1eca5cf 977 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
b22128ef 978 wpa_s->p2p_group_idx);
a1eca5cf
JM
979 if (os_strlen(ifname) >= IFNAMSIZ &&
980 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
981 /* Try to avoid going over the IFNAMSIZ length limit */
982 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
983 wpa_s->p2p_group_idx);
984 }
b22128ef
JM
985 force_ifname[0] = '\0';
986
987 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
988 ifname);
989 wpa_s->p2p_group_idx++;
990
991 wpa_s->pending_interface_type = type;
992 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
e17a2477 993 wpa_s->pending_interface_addr, NULL) < 0) {
b22128ef
JM
994 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
995 "interface");
996 return -1;
997 }
998
999 if (force_ifname[0]) {
1000 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1001 force_ifname);
1002 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1003 sizeof(wpa_s->pending_interface_name));
1004 } else
1005 os_strlcpy(wpa_s->pending_interface_name, ifname,
1006 sizeof(wpa_s->pending_interface_name));
1007 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1008 MACSTR, wpa_s->pending_interface_name,
1009 MAC2STR(wpa_s->pending_interface_addr));
1010
1011 return 0;
1012}
1013
1014
1015static void wpas_p2p_remove_pending_group_interface(
1016 struct wpa_supplicant *wpa_s)
1017{
1018 if (!wpa_s->pending_interface_name[0] ||
1019 is_zero_ether_addr(wpa_s->pending_interface_addr))
1020 return; /* No pending virtual interface */
1021
1022 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1023 wpa_s->pending_interface_name);
1024 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1025 wpa_s->pending_interface_name);
1026 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1027 wpa_s->pending_interface_name[0] = '\0';
1028}
1029
1030
1031static struct wpa_supplicant *
1032wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1033{
1034 struct wpa_interface iface;
1035 struct wpa_supplicant *group_wpa_s;
1036
1037 if (!wpa_s->pending_interface_name[0]) {
1038 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
d75e8806
JM
1039 if (!wpas_p2p_create_iface(wpa_s))
1040 return NULL;
1041 /*
1042 * Something has forced us to remove the pending interface; try
1043 * to create a new one and hope for the best that we will get
1044 * the same local address.
1045 */
1046 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1047 WPA_IF_P2P_CLIENT) < 0)
1048 return NULL;
b22128ef
JM
1049 }
1050
1051 os_memset(&iface, 0, sizeof(iface));
1052 iface.ifname = wpa_s->pending_interface_name;
1053 iface.driver = wpa_s->driver->name;
1054 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1055 iface.driver_param = wpa_s->conf->driver_param;
1056 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1057 if (group_wpa_s == NULL) {
1058 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1059 "wpa_supplicant interface");
1060 return NULL;
1061 }
1062 wpa_s->pending_interface_name[0] = '\0';
1063 group_wpa_s->parent = wpa_s;
1064 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1065 P2P_GROUP_INTERFACE_CLIENT;
1066 wpa_s->global->p2p_group_formation = group_wpa_s;
1067
1068 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1069
1070 return group_wpa_s;
1071}
1072
1073
1074static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1075 void *timeout_ctx)
1076{
1077 struct wpa_supplicant *wpa_s = eloop_ctx;
1078 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1079 if (wpa_s->global->p2p)
1080 p2p_group_formation_failed(wpa_s->global->p2p);
3ac17eba
JM
1081 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1082 wpa_drv_p2p_group_formation_failed(wpa_s);
b22128ef
JM
1083 wpas_group_formation_completed(wpa_s, 0);
1084}
1085
1086
1087void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1088{
1089 struct wpa_supplicant *wpa_s = ctx;
1090
8eada5ca 1091 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
b22128ef
JM
1092 wpa_drv_cancel_remain_on_channel(wpa_s);
1093 wpa_s->off_channel_freq = 0;
07a30a66 1094 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
1095 }
1096
1097 if (res->status) {
1098 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1099 res->status);
e5a359cf 1100 wpas_notify_p2p_go_neg_completed(wpa_s, res);
b22128ef
JM
1101 wpas_p2p_remove_pending_group_interface(wpa_s);
1102 return;
1103 }
1104
e2308e4b
RM
1105 if (wpa_s->p2p_go_ht40)
1106 res->ht40 = 1;
1107
b22128ef 1108 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
e5a359cf 1109 wpas_notify_p2p_go_neg_completed(wpa_s, res);
b22128ef 1110
23c84252
JM
1111 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1112 struct wpa_ssid *ssid;
1113 ssid = wpa_config_get_network(wpa_s->conf,
1114 wpa_s->p2p_persistent_id);
1115 if (ssid && ssid->disabled == 2 &&
1116 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1117 size_t len = os_strlen(ssid->passphrase);
1118 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1119 "on requested persistent group");
1120 os_memcpy(res->passphrase, ssid->passphrase, len);
1121 res->passphrase[len] = '\0';
1122 }
1123 }
1124
b22128ef
JM
1125 if (wpa_s->create_p2p_iface) {
1126 struct wpa_supplicant *group_wpa_s =
1127 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1128 if (group_wpa_s == NULL) {
1129 wpas_p2p_remove_pending_group_interface(wpa_s);
1130 return;
1131 }
3c5126a4 1132 if (group_wpa_s != wpa_s) {
b22128ef
JM
1133 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1134 sizeof(group_wpa_s->p2p_pin));
3c5126a4
JM
1135 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1136 }
b22128ef
JM
1137 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1138 wpa_s->pending_interface_name[0] = '\0';
1139 group_wpa_s->p2p_in_provisioning = 1;
1140
1141 if (res->role_go)
1142 wpas_start_wps_go(group_wpa_s, res, 1);
1143 else
1144 wpas_start_wps_enrollee(group_wpa_s, res);
1145 } else {
1146 wpa_s->p2p_in_provisioning = 1;
1147 wpa_s->global->p2p_group_formation = wpa_s;
1148
1149 if (res->role_go)
1150 wpas_start_wps_go(wpa_s, res, 1);
1151 else
1152 wpas_start_wps_enrollee(ctx, res);
1153 }
1154
1155 wpa_s->p2p_long_listen = 0;
1156 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1157
1158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ae3e3421
JM
1159 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1160 (res->peer_config_timeout % 100) * 10000,
1161 wpas_p2p_group_formation_timeout, wpa_s, NULL);
b22128ef
JM
1162}
1163
1164
3dfda83d 1165void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
b22128ef
JM
1166{
1167 struct wpa_supplicant *wpa_s = ctx;
3dfda83d
JM
1168 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1169 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
32d1bce0
KRK
1170
1171 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
b22128ef
JM
1172}
1173
1174
c5db8e51 1175void wpas_dev_found(void *ctx, const u8 *addr,
8fd7dc1b
JB
1176 const struct p2p_peer_info *info,
1177 int new_device)
b22128ef 1178{
5506d184 1179#ifndef CONFIG_NO_STDOUT_DEBUG
b22128ef
JM
1180 struct wpa_supplicant *wpa_s = ctx;
1181 char devtype[WPS_DEV_TYPE_BUFSIZE];
c5db8e51 1182
b22128ef
JM
1183 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1184 " p2p_dev_addr=" MACSTR
1185 " pri_dev_type=%s name='%s' config_methods=0x%x "
1186 "dev_capab=0x%x group_capab=0x%x",
c5db8e51
KRK
1187 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1188 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1189 sizeof(devtype)),
1190 info->device_name, info->config_methods,
1191 info->dev_capab, info->group_capab);
5506d184 1192#endif /* CONFIG_NO_STDOUT_DEBUG */
d642d2d2
JB
1193
1194 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
b22128ef
JM
1195}
1196
1197
56eeb8f2
JB
1198static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1199{
1200 struct wpa_supplicant *wpa_s = ctx;
1201
3074d8f1
JJ
1202 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1203 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1204
56eeb8f2
JB
1205 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1206}
1207
1208
b22128ef
JM
1209static int wpas_start_listen(void *ctx, unsigned int freq,
1210 unsigned int duration,
1211 const struct wpabuf *probe_resp_ie)
1212{
1213 struct wpa_supplicant *wpa_s = ctx;
1214
0e2e565a 1215 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
b22128ef
JM
1216
1217 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1218 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1219 "report received Probe Request frames");
1220 return -1;
1221 }
1222
1223 wpa_s->pending_listen_freq = freq;
1224 wpa_s->pending_listen_duration = duration;
1225
1226 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1227 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1228 "to remain on channel (%u MHz) for Listen "
1229 "state", freq);
1230 wpa_s->pending_listen_freq = 0;
1231 return -1;
1232 }
09d660b9 1233 wpa_s->off_channel_freq = 0;
07a30a66 1234 wpa_s->roc_waiting_drv_freq = freq;
b22128ef
JM
1235
1236 return 0;
1237}
1238
1239
1240static void wpas_stop_listen(void *ctx)
1241{
1242 struct wpa_supplicant *wpa_s = ctx;
6cb22d2f 1243 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
b22128ef
JM
1244 wpa_drv_cancel_remain_on_channel(wpa_s);
1245 wpa_s->off_channel_freq = 0;
07a30a66 1246 wpa_s->roc_waiting_drv_freq = 0;
b22128ef 1247 }
839b33ad 1248 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
b22128ef
JM
1249 wpa_drv_probe_req_report(wpa_s, 0);
1250}
1251
1252
1253static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1254{
1255 struct wpa_supplicant *wpa_s = ctx;
9dbf53fe 1256 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
b22128ef
JM
1257}
1258
1259
8c9ad085
JM
1260/*
1261 * DNS Header section is used only to calculate compression pointers, so the
1262 * contents of this data does not matter, but the length needs to be reserved
1263 * in the virtual packet.
1264 */
1265#define DNS_HEADER_LEN 12
1266
1267/*
1268 * 27-octet in-memory packet from P2P specification containing two implied
1269 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1270 */
1271#define P2P_SD_IN_MEMORY_LEN 27
1272
1273static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1274 u8 **spos, const u8 *end)
1275{
1276 while (*spos < end) {
1277 u8 val = ((*spos)[0] & 0xc0) >> 6;
1278 int len;
1279
1280 if (val == 1 || val == 2) {
1281 /* These are reserved values in RFC 1035 */
1282 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1283 "sequence starting with 0x%x", val);
1284 return -1;
1285 }
1286
1287 if (val == 3) {
1288 u16 offset;
1289 u8 *spos_tmp;
1290
1291 /* Offset */
1292 if (*spos + 2 > end) {
1293 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1294 "DNS offset field");
1295 return -1;
1296 }
1297
1298 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1299 if (offset >= *spos - start) {
1300 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1301 "pointer offset %u", offset);
1302 return -1;
1303 }
1304
1305 (*spos) += 2;
1306 spos_tmp = start + offset;
1307 return p2p_sd_dns_uncompress_label(upos, uend, start,
1308 &spos_tmp,
1309 *spos - 2);
1310 }
1311
1312 /* Label */
1313 len = (*spos)[0] & 0x3f;
1314 if (len == 0)
1315 return 0;
1316
1317 (*spos)++;
1318 if (*spos + len > end) {
1319 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1320 "sequence - no room for label with length "
1321 "%u", len);
1322 return -1;
1323 }
1324
1325 if (*upos + len + 2 > uend)
1326 return -2;
1327
1328 os_memcpy(*upos, *spos, len);
1329 *spos += len;
1330 *upos += len;
1331 (*upos)[0] = '.';
1332 (*upos)++;
1333 (*upos)[0] = '\0';
1334 }
1335
1336 return 0;
1337}
1338
1339
1340/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1341 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1342 * not large enough */
1343static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1344 size_t msg_len, size_t offset)
1345{
1346 /* 27-octet in-memory packet from P2P specification */
1347 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1348 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1349 u8 *tmp, *end, *spos;
1350 char *upos, *uend;
1351 int ret = 0;
1352
1353 if (buf_len < 2)
1354 return -1;
1355 if (offset > msg_len)
1356 return -1;
1357
1358 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1359 if (tmp == NULL)
1360 return -1;
1361 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1362 end = spos + msg_len;
1363 spos += offset;
1364
1365 os_memset(tmp, 0, DNS_HEADER_LEN);
1366 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1367 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1368
1369 upos = buf;
1370 uend = buf + buf_len;
1371
1372 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1373 if (ret) {
1374 os_free(tmp);
1375 return ret;
1376 }
1377
1378 if (upos == buf) {
1379 upos[0] = '.';
1380 upos[1] = '\0';
1381 } else if (upos[-1] == '.')
1382 upos[-1] = '\0';
1383
1384 os_free(tmp);
1385 return 0;
1386}
1387
1388
b22128ef
JM
1389static struct p2p_srv_bonjour *
1390wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1391 const struct wpabuf *query)
1392{
1393 struct p2p_srv_bonjour *bsrv;
1394 size_t len;
1395
1396 len = wpabuf_len(query);
1397 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1398 struct p2p_srv_bonjour, list) {
1399 if (len == wpabuf_len(bsrv->query) &&
1400 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1401 len) == 0)
1402 return bsrv;
1403 }
1404 return NULL;
1405}
1406
1407
1408static struct p2p_srv_upnp *
1409wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1410 const char *service)
1411{
1412 struct p2p_srv_upnp *usrv;
1413
1414 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1415 struct p2p_srv_upnp, list) {
1416 if (version == usrv->version &&
1417 os_strcmp(service, usrv->service) == 0)
1418 return usrv;
1419 }
1420 return NULL;
1421}
1422
1423
1424static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1425 u8 srv_trans_id)
1426{
1427 u8 *len_pos;
1428
1429 if (wpabuf_tailroom(resp) < 5)
1430 return;
1431
1432 /* Length (to be filled) */
1433 len_pos = wpabuf_put(resp, 2);
1434 wpabuf_put_u8(resp, srv_proto);
1435 wpabuf_put_u8(resp, srv_trans_id);
1436 /* Status Code */
1437 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1438 /* Response Data: empty */
1439 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1440}
1441
1442
1443static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1444 struct wpabuf *resp, u8 srv_trans_id)
1445{
1446 struct p2p_srv_bonjour *bsrv;
1447 u8 *len_pos;
1448
1449 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1450
1451 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1452 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1453 return;
1454 }
1455
1456 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1457 struct p2p_srv_bonjour, list) {
1458 if (wpabuf_tailroom(resp) <
1459 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1460 return;
1461 /* Length (to be filled) */
1462 len_pos = wpabuf_put(resp, 2);
1463 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1464 wpabuf_put_u8(resp, srv_trans_id);
1465 /* Status Code */
1466 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1467 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1468 wpabuf_head(bsrv->resp),
1469 wpabuf_len(bsrv->resp));
1470 /* Response Data */
1471 wpabuf_put_buf(resp, bsrv->query); /* Key */
1472 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1473 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1474 2);
1475 }
1476}
1477
1478
8c9ad085
JM
1479static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1480 size_t query_len)
1481{
1482 char str_rx[256], str_srv[256];
1483
1484 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1485 return 0; /* Too short to include DNS Type and Version */
1486 if (os_memcmp(query + query_len - 3,
1487 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1488 3) != 0)
1489 return 0; /* Mismatch in DNS Type or Version */
1490 if (query_len == wpabuf_len(bsrv->query) &&
1491 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1492 return 1; /* Binary match */
1493
1494 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1495 0))
1496 return 0; /* Failed to uncompress query */
1497 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1498 wpabuf_head(bsrv->query),
1499 wpabuf_len(bsrv->query) - 3, 0))
1500 return 0; /* Failed to uncompress service */
1501
1502 return os_strcmp(str_rx, str_srv) == 0;
1503}
1504
1505
b22128ef
JM
1506static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1507 struct wpabuf *resp, u8 srv_trans_id,
1508 const u8 *query, size_t query_len)
1509{
1510 struct p2p_srv_bonjour *bsrv;
b22128ef 1511 u8 *len_pos;
560f8bda 1512 int matches = 0;
b22128ef
JM
1513
1514 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1515 query, query_len);
1516 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1517 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1518 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1519 srv_trans_id);
1520 return;
1521 }
1522
1523 if (query_len == 0) {
1524 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1525 return;
1526 }
1527
560f8bda
JM
1528 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1529 struct p2p_srv_bonjour, list) {
8c9ad085 1530 if (!match_bonjour_query(bsrv, query, query_len))
560f8bda
JM
1531 continue;
1532
1533 if (wpabuf_tailroom(resp) <
1534 5 + query_len + wpabuf_len(bsrv->resp))
1535 return;
1536
1537 matches++;
b22128ef 1538
560f8bda
JM
1539 /* Length (to be filled) */
1540 len_pos = wpabuf_put(resp, 2);
1541 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1542 wpabuf_put_u8(resp, srv_trans_id);
1543
1544 /* Status Code */
1545 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1546 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1547 wpabuf_head(bsrv->resp),
1548 wpabuf_len(bsrv->resp));
1549
1550 /* Response Data */
1551 wpabuf_put_data(resp, query, query_len); /* Key */
1552 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1553
1554 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1555 }
1556
1557 if (matches == 0) {
b22128ef
JM
1558 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1559 "available");
560f8bda
JM
1560 if (wpabuf_tailroom(resp) < 5)
1561 return;
1562
1563 /* Length (to be filled) */
1564 len_pos = wpabuf_put(resp, 2);
1565 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1566 wpabuf_put_u8(resp, srv_trans_id);
b22128ef
JM
1567
1568 /* Status Code */
bf608cad 1569 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1570 /* Response Data: empty */
1571 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1572 2);
b22128ef 1573 }
b22128ef
JM
1574}
1575
1576
1577static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1578 struct wpabuf *resp, u8 srv_trans_id)
1579{
1580 struct p2p_srv_upnp *usrv;
1581 u8 *len_pos;
1582
1583 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1584
1585 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1586 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1587 return;
1588 }
1589
1590 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1591 struct p2p_srv_upnp, list) {
1592 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1593 return;
1594
1595 /* Length (to be filled) */
1596 len_pos = wpabuf_put(resp, 2);
1597 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1598 wpabuf_put_u8(resp, srv_trans_id);
1599
1600 /* Status Code */
1601 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1602 /* Response Data */
1603 wpabuf_put_u8(resp, usrv->version);
1604 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1605 usrv->service);
1606 wpabuf_put_str(resp, usrv->service);
1607 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1608 2);
1609 }
1610}
1611
1612
1613static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1614 struct wpabuf *resp, u8 srv_trans_id,
1615 const u8 *query, size_t query_len)
1616{
1617 struct p2p_srv_upnp *usrv;
1618 u8 *len_pos;
1619 u8 version;
1620 char *str;
1621 int count = 0;
1622
1623 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1624 query, query_len);
1625
1626 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1627 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1628 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1629 srv_trans_id);
1630 return;
1631 }
1632
1633 if (query_len == 0) {
1634 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1635 return;
1636 }
1637
b22128ef
JM
1638 if (wpabuf_tailroom(resp) < 5)
1639 return;
1640
1641 /* Length (to be filled) */
1642 len_pos = wpabuf_put(resp, 2);
1643 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1644 wpabuf_put_u8(resp, srv_trans_id);
1645
79222cfa
JM
1646 version = query[0];
1647 str = os_malloc(query_len);
1648 if (str == NULL)
1649 return;
1650 os_memcpy(str, query + 1, query_len - 1);
1651 str[query_len - 1] = '\0';
1652
b22128ef
JM
1653 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1654 struct p2p_srv_upnp, list) {
1655 if (version != usrv->version)
1656 continue;
1657
1658 if (os_strcmp(str, "ssdp:all") != 0 &&
1659 os_strstr(usrv->service, str) == NULL)
1660 continue;
1661
1662 if (wpabuf_tailroom(resp) < 2)
1663 break;
1664 if (count == 0) {
1665 /* Status Code */
1666 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1667 /* Response Data */
1668 wpabuf_put_u8(resp, version);
1669 } else
1670 wpabuf_put_u8(resp, ',');
1671
1672 count++;
1673
1674 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1675 usrv->service);
1676 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1677 break;
1678 wpabuf_put_str(resp, usrv->service);
1679 }
714b8b53 1680 os_free(str);
b22128ef
JM
1681
1682 if (count == 0) {
1683 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1684 "available");
1685 /* Status Code */
bf608cad 1686 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
b22128ef
JM
1687 /* Response Data: empty */
1688 }
1689
1690 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1691}
1692
1693
6ffdc2f7
JM
1694#ifdef CONFIG_WIFI_DISPLAY
1695static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1696 struct wpabuf *resp, u8 srv_trans_id,
1697 const u8 *query, size_t query_len)
1698{
1699 const u8 *pos;
1700 u8 role;
1701 u8 *len_pos;
1702
1703 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1704
1705 if (!wpa_s->global->wifi_display) {
1706 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1707 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1708 srv_trans_id);
1709 return;
1710 }
1711
1712 if (query_len < 1) {
1713 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1714 "Role");
1715 return;
1716 }
1717
1718 if (wpabuf_tailroom(resp) < 5)
1719 return;
1720
1721 pos = query;
1722 role = *pos++;
1723 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1724
1725 /* TODO: role specific handling */
1726
1727 /* Length (to be filled) */
1728 len_pos = wpabuf_put(resp, 2);
1729 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1730 wpabuf_put_u8(resp, srv_trans_id);
1731 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1732
1733 while (pos < query + query_len) {
1734 if (*pos < MAX_WFD_SUBELEMS &&
1735 wpa_s->global->wfd_subelem[*pos] &&
1736 wpabuf_tailroom(resp) >=
1737 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1738 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1739 "subelement %u", *pos);
1740 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1741 }
1742 pos++;
1743 }
1744
1745 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1746}
1747#endif /* CONFIG_WIFI_DISPLAY */
1748
1749
b22128ef
JM
1750void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1751 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1752{
1753 struct wpa_supplicant *wpa_s = ctx;
1754 const u8 *pos = tlvs;
1755 const u8 *end = tlvs + tlvs_len;
1756 const u8 *tlv_end;
1757 u16 slen;
1758 struct wpabuf *resp;
1759 u8 srv_proto, srv_trans_id;
1760 size_t buf_len;
1761 char *buf;
1762
1763 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1764 tlvs, tlvs_len);
1765 buf_len = 2 * tlvs_len + 1;
1766 buf = os_malloc(buf_len);
1767 if (buf) {
1768 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1769 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1770 MACSTR " %u %u %s",
1771 freq, MAC2STR(sa), dialog_token, update_indic,
1772 buf);
1773 os_free(buf);
1774 }
1775
3b655312 1776 if (wpa_s->p2p_sd_over_ctrl_iface) {
1777 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1778 update_indic, tlvs, tlvs_len);
b22128ef 1779 return; /* to be processed by an external program */
3b655312 1780 }
b22128ef
JM
1781
1782 resp = wpabuf_alloc(10000);
1783 if (resp == NULL)
1784 return;
1785
1786 while (pos + 1 < end) {
1787 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1788 slen = WPA_GET_LE16(pos);
1789 pos += 2;
1790 if (pos + slen > end || slen < 2) {
1791 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1792 "length");
1793 wpabuf_free(resp);
1794 return;
1795 }
1796 tlv_end = pos + slen;
1797
1798 srv_proto = *pos++;
1799 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1800 srv_proto);
1801 srv_trans_id = *pos++;
1802 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1803 srv_trans_id);
1804
1805 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1806 pos, tlv_end - pos);
1807
6e6963ea
JM
1808
1809 if (wpa_s->force_long_sd) {
1810 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1811 "response");
1812 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1813 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1814 goto done;
1815 }
1816
b22128ef
JM
1817 switch (srv_proto) {
1818 case P2P_SERV_ALL_SERVICES:
1819 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1820 "for all services");
1821 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1822 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1823 wpa_printf(MSG_DEBUG, "P2P: No service "
1824 "discovery protocols available");
1825 wpas_sd_add_proto_not_avail(
1826 resp, P2P_SERV_ALL_SERVICES,
1827 srv_trans_id);
1828 break;
1829 }
1830 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1831 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1832 break;
1833 case P2P_SERV_BONJOUR:
1834 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1835 pos, tlv_end - pos);
1836 break;
1837 case P2P_SERV_UPNP:
1838 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1839 pos, tlv_end - pos);
1840 break;
6ffdc2f7
JM
1841#ifdef CONFIG_WIFI_DISPLAY
1842 case P2P_SERV_WIFI_DISPLAY:
1843 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1844 pos, tlv_end - pos);
1845 break;
1846#endif /* CONFIG_WIFI_DISPLAY */
b22128ef
JM
1847 default:
1848 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1849 "protocol %u", srv_proto);
1850 wpas_sd_add_proto_not_avail(resp, srv_proto,
1851 srv_trans_id);
1852 break;
1853 }
1854
1855 pos = tlv_end;
1856 }
1857
6e6963ea 1858done:
e1653cac
KRK
1859 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1860 update_indic, tlvs, tlvs_len);
1861
b22128ef
JM
1862 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1863
1864 wpabuf_free(resp);
1865}
1866
1867
1868void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1869 const u8 *tlvs, size_t tlvs_len)
1870{
1871 struct wpa_supplicant *wpa_s = ctx;
1872 const u8 *pos = tlvs;
1873 const u8 *end = tlvs + tlvs_len;
1874 const u8 *tlv_end;
1875 u16 slen;
1876 size_t buf_len;
1877 char *buf;
1878
1879 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1880 tlvs, tlvs_len);
18708aad
JM
1881 if (tlvs_len > 1500) {
1882 /* TODO: better way for handling this */
1883 wpa_msg_ctrl(wpa_s, MSG_INFO,
1884 P2P_EVENT_SERV_DISC_RESP MACSTR
1885 " %u <long response: %u bytes>",
1886 MAC2STR(sa), update_indic,
1887 (unsigned int) tlvs_len);
1888 } else {
1889 buf_len = 2 * tlvs_len + 1;
1890 buf = os_malloc(buf_len);
1891 if (buf) {
1892 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1893 wpa_msg_ctrl(wpa_s, MSG_INFO,
1894 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1895 MAC2STR(sa), update_indic, buf);
1896 os_free(buf);
1897 }
b22128ef
JM
1898 }
1899
1900 while (pos < end) {
1901 u8 srv_proto, srv_trans_id, status;
1902
1903 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1904 slen = WPA_GET_LE16(pos);
1905 pos += 2;
1906 if (pos + slen > end || slen < 3) {
1907 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1908 "length");
1909 return;
1910 }
1911 tlv_end = pos + slen;
1912
1913 srv_proto = *pos++;
1914 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1915 srv_proto);
1916 srv_trans_id = *pos++;
1917 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1918 srv_trans_id);
1919 status = *pos++;
1920 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1921 status);
1922
1923 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1924 pos, tlv_end - pos);
1925
1926 pos = tlv_end;
1927 }
43a26f60
KRK
1928
1929 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
b22128ef
JM
1930}
1931
1932
7165c5dc
JM
1933u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1934 const struct wpabuf *tlvs)
b22128ef 1935{
3ac17eba 1936 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
7165c5dc 1937 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
9526fd29 1938 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7165c5dc
JM
1939 return 0;
1940 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
b22128ef
JM
1941}
1942
1943
7165c5dc
JM
1944u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1945 u8 version, const char *query)
b22128ef
JM
1946{
1947 struct wpabuf *tlvs;
7165c5dc 1948 u64 ret;
b22128ef
JM
1949
1950 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1951 if (tlvs == NULL)
7165c5dc 1952 return 0;
b22128ef
JM
1953 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1954 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1955 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1956 wpabuf_put_u8(tlvs, version);
1957 wpabuf_put_str(tlvs, query);
1958 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1959 wpabuf_free(tlvs);
1960 return ret;
1961}
1962
1963
347d6a5b
JM
1964#ifdef CONFIG_WIFI_DISPLAY
1965
1966static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1967 const struct wpabuf *tlvs)
1968{
1969 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1970 return 0;
1971 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1972 return 0;
1973 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1974}
1975
1976
1977#define MAX_WFD_SD_SUBELEMS 20
1978
1979static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1980 const char *subelems)
1981{
1982 u8 *len;
1983 const char *pos;
1984 int val;
1985 int count = 0;
1986
1987 len = wpabuf_put(tlvs, 2);
1988 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1989 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1990
1991 wpabuf_put_u8(tlvs, role);
1992
1993 pos = subelems;
1994 while (*pos) {
1995 val = atoi(pos);
1996 if (val >= 0 && val < 256) {
1997 wpabuf_put_u8(tlvs, val);
1998 count++;
1999 if (count == MAX_WFD_SD_SUBELEMS)
2000 break;
2001 }
2002 pos = os_strchr(pos + 1, ',');
2003 if (pos == NULL)
2004 break;
2005 pos++;
2006 }
2007
2008 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2009}
2010
2011
2012u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2013 const u8 *dst, const char *role)
2014{
2015 struct wpabuf *tlvs;
2016 u64 ret;
2017 const char *subelems;
2018 u8 id = 1;
2019
2020 subelems = os_strchr(role, ' ');
2021 if (subelems == NULL)
2022 return 0;
2023 subelems++;
2024
2025 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2026 if (tlvs == NULL)
2027 return 0;
2028
2029 if (os_strstr(role, "[source]"))
2030 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2031 if (os_strstr(role, "[pri-sink]"))
2032 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2033 if (os_strstr(role, "[sec-sink]"))
2034 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2035 if (os_strstr(role, "[source+sink]"))
2036 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2037
2038 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2039 wpabuf_free(tlvs);
2040 return ret;
2041}
2042
2043#endif /* CONFIG_WIFI_DISPLAY */
2044
2045
7165c5dc 2046int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
b22128ef 2047{
3ac17eba 2048 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
7165c5dc 2049 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
9526fd29
JM
2050 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2051 return -1;
7165c5dc
JM
2052 return p2p_sd_cancel_request(wpa_s->global->p2p,
2053 (void *) (uintptr_t) req);
b22128ef
JM
2054}
2055
2056
2057void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2058 const u8 *dst, u8 dialog_token,
2059 const struct wpabuf *resp_tlvs)
2060{
3ac17eba
JM
2061 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2062 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2063 resp_tlvs);
2064 return;
2065 }
9526fd29
JM
2066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2067 return;
b22128ef
JM
2068 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2069 resp_tlvs);
2070}
2071
2072
2073void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2074{
3ac17eba
JM
2075 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2076 wpa_drv_p2p_service_update(wpa_s);
2077 return;
2078 }
9526fd29
JM
2079 if (wpa_s->global->p2p)
2080 p2p_sd_service_update(wpa_s->global->p2p);
b22128ef
JM
2081}
2082
2083
2084static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2085{
2086 dl_list_del(&bsrv->list);
2087 wpabuf_free(bsrv->query);
2088 wpabuf_free(bsrv->resp);
2089 os_free(bsrv);
2090}
2091
2092
2093static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2094{
2095 dl_list_del(&usrv->list);
2096 os_free(usrv->service);
2097 os_free(usrv);
2098}
2099
2100
2101void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2102{
2103 struct p2p_srv_bonjour *bsrv, *bn;
2104 struct p2p_srv_upnp *usrv, *un;
2105
2106 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2107 struct p2p_srv_bonjour, list)
2108 wpas_p2p_srv_bonjour_free(bsrv);
2109
2110 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2111 struct p2p_srv_upnp, list)
2112 wpas_p2p_srv_upnp_free(usrv);
2113
2114 wpas_p2p_sd_service_update(wpa_s);
2115}
2116
2117
2118int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2119 struct wpabuf *query, struct wpabuf *resp)
2120{
2121 struct p2p_srv_bonjour *bsrv;
2122
b22128ef
JM
2123 bsrv = os_zalloc(sizeof(*bsrv));
2124 if (bsrv == NULL)
2125 return -1;
2126 bsrv->query = query;
2127 bsrv->resp = resp;
2128 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2129
2130 wpas_p2p_sd_service_update(wpa_s);
2131 return 0;
2132}
2133
2134
2135int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2136 const struct wpabuf *query)
2137{
2138 struct p2p_srv_bonjour *bsrv;
2139
2140 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2141 if (bsrv == NULL)
2142 return -1;
2143 wpas_p2p_srv_bonjour_free(bsrv);
2144 wpas_p2p_sd_service_update(wpa_s);
2145 return 0;
2146}
2147
2148
2149int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2150 const char *service)
2151{
2152 struct p2p_srv_upnp *usrv;
2153
2154 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2155 return 0; /* Already listed */
2156 usrv = os_zalloc(sizeof(*usrv));
2157 if (usrv == NULL)
2158 return -1;
2159 usrv->version = version;
2160 usrv->service = os_strdup(service);
2161 if (usrv->service == NULL) {
2162 os_free(usrv);
2163 return -1;
2164 }
2165 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2166
2167 wpas_p2p_sd_service_update(wpa_s);
2168 return 0;
2169}
2170
2171
2172int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2173 const char *service)
2174{
2175 struct p2p_srv_upnp *usrv;
2176
2177 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2178 if (usrv == NULL)
2179 return -1;
2180 wpas_p2p_srv_upnp_free(usrv);
2181 wpas_p2p_sd_service_update(wpa_s);
2182 return 0;
2183}
2184
2185
2186static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
bbeee9b0
JB
2187 const u8 *peer, const char *params,
2188 unsigned int generated_pin)
b22128ef
JM
2189{
2190 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
bbeee9b0 2191 MAC2STR(peer), generated_pin, params);
b22128ef
JM
2192}
2193
2194
2195static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2196 const u8 *peer, const char *params)
2197{
2198 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2199 MAC2STR(peer), params);
2200}
2201
2202
2203void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2204 const u8 *dev_addr, const u8 *pri_dev_type,
2205 const char *dev_name, u16 supp_config_methods,
c3f42784
JM
2206 u8 dev_capab, u8 group_capab, const u8 *group_id,
2207 size_t group_id_len)
b22128ef
JM
2208{
2209 struct wpa_supplicant *wpa_s = ctx;
2210 char devtype[WPS_DEV_TYPE_BUFSIZE];
c3f42784 2211 char params[300];
b22128ef 2212 u8 empty_dev_type[8];
bbeee9b0 2213 unsigned int generated_pin = 0;
c3f42784
JM
2214 struct wpa_supplicant *group = NULL;
2215
2216 if (group_id) {
2217 for (group = wpa_s->global->ifaces; group; group = group->next)
2218 {
2219 struct wpa_ssid *s = group->current_ssid;
2220 if (s != NULL &&
2221 s->mode == WPAS_MODE_P2P_GO &&
2222 group_id_len - ETH_ALEN == s->ssid_len &&
2223 os_memcmp(group_id + ETH_ALEN, s->ssid,
2224 s->ssid_len) == 0)
2225 break;
2226 }
2227 }
b22128ef
JM
2228
2229 if (pri_dev_type == NULL) {
2230 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2231 pri_dev_type = empty_dev_type;
2232 }
2233 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2234 " pri_dev_type=%s name='%s' config_methods=0x%x "
c3f42784 2235 "dev_capab=0x%x group_capab=0x%x%s%s",
b22128ef
JM
2236 MAC2STR(dev_addr),
2237 wps_dev_type_bin2str(pri_dev_type, devtype,
2238 sizeof(devtype)),
c3f42784
JM
2239 dev_name, supp_config_methods, dev_capab, group_capab,
2240 group ? " group=" : "",
2241 group ? group->ifname : "");
b22128ef
JM
2242 params[sizeof(params) - 1] = '\0';
2243
bbeee9b0
JB
2244 if (config_methods & WPS_CONFIG_DISPLAY) {
2245 generated_pin = wps_generate_pin();
2246 wpas_prov_disc_local_display(wpa_s, peer, params,
2247 generated_pin);
2248 } else if (config_methods & WPS_CONFIG_KEYPAD)
b22128ef
JM
2249 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2250 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2251 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2252 "%s", MAC2STR(peer), params);
dd8a7e05
JB
2253
2254 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2255 P2P_PROV_DISC_SUCCESS,
2256 config_methods, generated_pin);
b22128ef
JM
2257}
2258
2259
2260void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2261{
2262 struct wpa_supplicant *wpa_s = ctx;
bbeee9b0 2263 unsigned int generated_pin = 0;
0918c4bf 2264 char params[20];
bbeee9b0 2265
c1931635
JM
2266 if (wpa_s->pending_pd_before_join &&
2267 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2268 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2269 wpa_s->pending_pd_before_join = 0;
2270 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2271 "join-existing-group operation");
2272 wpas_p2p_join_start(wpa_s);
2273 return;
2274 }
2275
0918c4bf
JM
2276 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2277 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2278 os_snprintf(params, sizeof(params), " peer_go=%d",
2279 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2280 else
2281 params[0] = '\0';
2282
b22128ef 2283 if (config_methods & WPS_CONFIG_DISPLAY)
0918c4bf 2284 wpas_prov_disc_local_keypad(wpa_s, peer, params);
bbeee9b0
JB
2285 else if (config_methods & WPS_CONFIG_KEYPAD) {
2286 generated_pin = wps_generate_pin();
0918c4bf
JM
2287 wpas_prov_disc_local_display(wpa_s, peer, params,
2288 generated_pin);
bbeee9b0 2289 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
0918c4bf
JM
2290 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2291 "%s", MAC2STR(peer), params);
a482883f 2292
dd8a7e05
JB
2293 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2294 P2P_PROV_DISC_SUCCESS,
2295 config_methods, generated_pin);
b22128ef
JM
2296}
2297
2298
19df9b07
JM
2299static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2300 enum p2p_prov_disc_status status)
dd8a7e05
JB
2301{
2302 struct wpa_supplicant *wpa_s = ctx;
2303
aa9bb764
JM
2304 if (wpa_s->p2p_fallback_to_go_neg) {
2305 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2306 "failed - fall back to GO Negotiation");
2307 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2308 return;
2309 }
2310
175171ac
JM
2311 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2312 wpa_s->pending_pd_before_join = 0;
2313 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2314 "join-existing-group operation (no ACK for PD "
2315 "Req attempts)");
2316 wpas_p2p_join_start(wpa_s);
2317 return;
2318 }
2319
f65a239b
DG
2320 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2321 " p2p_dev_addr=" MACSTR " status=%d",
25a94f52 2322 MAC2STR(peer), status);
f65a239b 2323
dd8a7e05
JB
2324 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2325 status, 0, 0);
2326}
2327
2328
b22128ef
JM
2329static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2330 const u8 *go_dev_addr, const u8 *ssid,
2331 size_t ssid_len, int *go, u8 *group_bssid,
2332 int *force_freq, int persistent_group)
2333{
2334 struct wpa_supplicant *wpa_s = ctx;
2335 struct wpa_ssid *s;
2336 u8 cur_bssid[ETH_ALEN];
2337 int res;
ab72eb52 2338 struct wpa_supplicant *grp;
b22128ef
JM
2339
2340 if (!persistent_group) {
2341 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2342 " to join an active group", MAC2STR(sa));
108def93 2343 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
131cb37c
JM
2344 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2345 == 0 ||
2346 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
108def93
JM
2347 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2348 "authorized invitation");
2349 goto accept_inv;
2350 }
b22128ef
JM
2351 /*
2352 * Do not accept the invitation automatically; notify user and
2353 * request approval.
2354 */
2355 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2356 }
2357
ab72eb52
JM
2358 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2359 if (grp) {
2360 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2361 "running persistent group");
2362 if (*go)
2363 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2364 goto accept_inv;
2365 }
2366
b22128ef
JM
2367 if (!wpa_s->conf->persistent_reconnect)
2368 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2369
2370 for (s = wpa_s->conf->ssid; s; s = s->next) {
2371 if (s->disabled == 2 &&
2372 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2373 s->ssid_len == ssid_len &&
2374 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2375 break;
2376 }
2377
2378 if (!s) {
2379 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2380 " requested reinvocation of an unknown group",
2381 MAC2STR(sa));
2382 return P2P_SC_FAIL_UNKNOWN_GROUP;
2383 }
2384
2385 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2386 *go = 1;
2387 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2388 wpa_printf(MSG_DEBUG, "P2P: The only available "
2389 "interface is already in use - reject "
2390 "invitation");
2391 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2392 }
2393 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2394 } else if (s->mode == WPAS_MODE_P2P_GO) {
2395 *go = 1;
2396 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2397 {
2398 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2399 "interface address for the group");
2400 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2401 }
2402 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2403 ETH_ALEN);
2404 }
2405
108def93 2406accept_inv:
b22128ef
JM
2407 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2408 wpa_s->assoc_freq) {
2409 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2410 "the channel we are already using");
2411 *force_freq = wpa_s->assoc_freq;
2412 }
2413
2414 res = wpa_drv_shared_freq(wpa_s);
2415 if (res > 0) {
2416 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2417 "with the channel we are already using on a "
2418 "shared interface");
2419 *force_freq = res;
2420 }
2421
2422 return P2P_SC_SUCCESS;
2423}
2424
2425
2426static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2427 const u8 *ssid, size_t ssid_len,
2428 const u8 *go_dev_addr, u8 status,
2429 int op_freq)
2430{
2431 struct wpa_supplicant *wpa_s = ctx;
2432 struct wpa_ssid *s;
2433
2434 for (s = wpa_s->conf->ssid; s; s = s->next) {
2435 if (s->disabled == 2 &&
2436 s->ssid_len == ssid_len &&
2437 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2438 break;
2439 }
2440
2441 if (status == P2P_SC_SUCCESS) {
2442 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2443 " was accepted; op_freq=%d MHz",
2444 MAC2STR(sa), op_freq);
2445 if (s) {
2cd07584 2446 int go = s->mode == WPAS_MODE_P2P_GO;
b22128ef 2447 wpas_p2p_group_add_persistent(
2cd07584 2448 wpa_s, s, go, go ? op_freq : 0, 0);
2049af2b 2449 } else if (bssid) {
67527166 2450 wpa_s->user_initiated_pd = 0;
108def93 2451 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
b31be3a0 2452 wpa_s->p2p_wps_method, 0);
b22128ef
JM
2453 }
2454 return;
2455 }
2456
2457 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2458 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2459 " was rejected (status %u)", MAC2STR(sa), status);
2460 return;
2461 }
2462
2463 if (!s) {
2464 if (bssid) {
2465 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2466 "sa=" MACSTR " go_dev_addr=" MACSTR
2467 " bssid=" MACSTR " unknown-network",
2468 MAC2STR(sa), MAC2STR(go_dev_addr),
2469 MAC2STR(bssid));
2470 } else {
2471 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2472 "sa=" MACSTR " go_dev_addr=" MACSTR
2473 " unknown-network",
2474 MAC2STR(sa), MAC2STR(go_dev_addr));
2475 }
2476 return;
2477 }
2478
2479 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2480 " persistent=%d", MAC2STR(sa), s->id);
2481}
2482
2483
2484static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2485{
2486 struct wpa_supplicant *wpa_s = ctx;
2487 struct wpa_ssid *ssid;
2488
2489 if (bssid) {
2490 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2491 "status=%d " MACSTR,
2492 status, MAC2STR(bssid));
2493 } else {
2494 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2495 "status=%d ", status);
2496 }
5ccdf84f 2497 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
b22128ef 2498
706887fc
JM
2499 if (wpa_s->pending_invite_ssid_id == -1)
2500 return; /* Invitation to active group */
2501
b22128ef
JM
2502 if (status != P2P_SC_SUCCESS) {
2503 wpas_p2p_remove_pending_group_interface(wpa_s);
2504 return;
2505 }
2506
2507 ssid = wpa_config_get_network(wpa_s->conf,
2508 wpa_s->pending_invite_ssid_id);
2509 if (ssid == NULL) {
2510 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2511 "data matching with invitation");
2512 return;
2513 }
2514
13ece96f
JM
2515 /*
2516 * The peer could have missed our ctrl::ack frame for Invitation
2517 * Response and continue retransmitting the frame. To reduce the
2518 * likelihood of the peer not getting successful TX status for the
2519 * Invitation Response frame, wait a short time here before starting
2520 * the persistent group so that we will remain on the current channel to
2521 * acknowledge any possible retransmission from the peer.
2522 */
2523 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2524 "starting persistent group");
2525 os_sleep(0, 50000);
2526
b22128ef 2527 wpas_p2p_group_add_persistent(wpa_s, ssid,
4d32c0c4
JM
2528 ssid->mode == WPAS_MODE_P2P_GO,
2529 wpa_s->p2p_persistent_go_freq,
2530 wpa_s->p2p_go_ht40);
b22128ef
JM
2531}
2532
2533
6f3bc72b
JM
2534static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2535 unsigned int freq)
2536{
2537 unsigned int i;
2538
2539 if (global->p2p_disallow_freq == NULL)
2540 return 0;
2541
2542 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2543 if (freq >= global->p2p_disallow_freq[i].min &&
2544 freq <= global->p2p_disallow_freq[i].max)
2545 return 1;
2546 }
2547
2548 return 0;
2549}
2550
2551
2552static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2553{
2554 reg->channel[reg->channels] = chan;
2555 reg->channels++;
2556}
2557
2558
ac8d1011
JM
2559static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2560 struct p2p_channels *chan)
2561{
2562 int i, cla = 0;
2563
2564 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2565 "band");
2566
2567 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2568 chan->reg_class[cla].reg_class = 81;
6f3bc72b
JM
2569 chan->reg_class[cla].channels = 0;
2570 for (i = 0; i < 11; i++) {
2571 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2572 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2573 }
2574 if (chan->reg_class[cla].channels)
2575 cla++;
ac8d1011
JM
2576
2577 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2578 "band");
2579
2580 /* Operating class 115 - 5 GHz, channels 36-48 */
2581 chan->reg_class[cla].reg_class = 115;
6f3bc72b
JM
2582 chan->reg_class[cla].channels = 0;
2583 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2584 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2585 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2586 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2587 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2588 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2589 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2590 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2591 if (chan->reg_class[cla].channels)
2592 cla++;
ac8d1011
JM
2593
2594 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2595 "band");
2596
2597 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2598 chan->reg_class[cla].reg_class = 124;
6f3bc72b
JM
2599 chan->reg_class[cla].channels = 0;
2600 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2601 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2602 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2603 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2604 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2605 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2606 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2607 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2608 if (chan->reg_class[cla].channels)
2609 cla++;
ac8d1011
JM
2610
2611 chan->reg_classes = cla;
2612 return 0;
2613}
2614
2615
2616static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2617 u16 num_modes,
2618 enum hostapd_hw_mode mode)
2619{
2620 u16 i;
2621
2622 for (i = 0; i < num_modes; i++) {
2623 if (modes[i].mode == mode)
2624 return &modes[i];
2625 }
2626
2627 return NULL;
2628}
2629
2630
6f3bc72b
JM
2631static int has_channel(struct wpa_global *global,
2632 struct hostapd_hw_modes *mode, u8 chan, int *flags)
ac8d1011
JM
2633{
2634 int i;
6f3bc72b
JM
2635 unsigned int freq;
2636
2637 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2638 chan * 5;
2639 if (wpas_p2p_disallowed_freq(global, freq))
2640 return 0;
ac8d1011
JM
2641
2642 for (i = 0; i < mode->num_channels; i++) {
2643 if (mode->channels[i].chan == chan) {
51222429
JM
2644 if (flags)
2645 *flags = mode->channels[i].flag;
ac8d1011
JM
2646 return !(mode->channels[i].flag &
2647 (HOSTAPD_CHAN_DISABLED |
2648 HOSTAPD_CHAN_PASSIVE_SCAN |
2649 HOSTAPD_CHAN_NO_IBSS |
2650 HOSTAPD_CHAN_RADAR));
2651 }
2652 }
2653
2654 return 0;
2655}
2656
2657
2658struct p2p_oper_class_map {
2659 enum hostapd_hw_mode mode;
2660 u8 op_class;
2661 u8 min_chan;
2662 u8 max_chan;
2663 u8 inc;
51222429 2664 enum { BW20, BW40PLUS, BW40MINUS } bw;
ac8d1011
JM
2665};
2666
931228aa
RM
2667static struct p2p_oper_class_map op_class[] = {
2668 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
931228aa
RM
2669#if 0 /* Do not enable HT40 on 2 GHz for now */
2670 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2671 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2672#endif
2673 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2674 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2675 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2676 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2677 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2678 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2679 { -1, 0, 0, 0, 0, BW20 }
2680};
2681
2682
2683static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2684 struct hostapd_hw_modes *mode,
2685 u8 channel, u8 bw)
2686{
2687 int flag;
2688
2689 if (!has_channel(wpa_s->global, mode, channel, &flag))
2690 return -1;
2691 if (bw == BW40MINUS &&
2692 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2693 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2694 return 0;
2695 if (bw == BW40PLUS &&
2696 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2697 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2698 return 0;
2699 return 1;
2700}
2701
2702
b22128ef 2703static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
ac8d1011 2704 struct p2p_channels *chan)
b22128ef 2705{
6bf731e8 2706 struct hostapd_hw_modes *mode;
ac8d1011 2707 int cla, op;
b22128ef 2708
6bf731e8 2709 if (wpa_s->hw.modes == NULL) {
b22128ef
JM
2710 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2711 "of all supported channels; assume dualband "
2712 "support");
ac8d1011 2713 return wpas_p2p_default_channels(wpa_s, chan);
b22128ef
JM
2714 }
2715
2716 cla = 0;
2717
ac8d1011
JM
2718 for (op = 0; op_class[op].op_class; op++) {
2719 struct p2p_oper_class_map *o = &op_class[op];
2720 u8 ch;
2721 struct p2p_reg_class *reg = NULL;
b22128ef 2722
6bf731e8 2723 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
ac8d1011
JM
2724 if (mode == NULL)
2725 continue;
2726 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
931228aa 2727 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
ac8d1011
JM
2728 continue;
2729 if (reg == NULL) {
2730 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2731 "class %u", o->op_class);
2732 reg = &chan->reg_class[cla];
2733 cla++;
2734 reg->reg_class = o->op_class;
2735 }
2736 reg->channel[reg->channels] = ch;
2737 reg->channels++;
2738 }
2739 if (reg) {
2740 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2741 reg->channel, reg->channels);
2742 }
b22128ef
JM
2743 }
2744
ac8d1011 2745 chan->reg_classes = cla;
b22128ef 2746
b22128ef
JM
2747 return 0;
2748}
2749
2750
7aeac985
RM
2751int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2752 struct hostapd_hw_modes *mode, u8 channel)
2753{
2754 int op, ret;
2755
2756 for (op = 0; op_class[op].op_class; op++) {
2757 struct p2p_oper_class_map *o = &op_class[op];
2758 u8 ch;
2759
2760 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2761 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2762 o->bw == BW20 || ch != channel)
2763 continue;
2764 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2765 if (ret < 0)
2766 continue;
2767 else if (ret > 0)
2768 return (o->bw == BW40MINUS) ? -1 : 1;
2769 else
2770 return 0;
2771 }
2772 }
2773 return 0;
2774}
2775
2776
b22128ef
JM
2777static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2778 size_t buf_len)
2779{
2780 struct wpa_supplicant *wpa_s = ctx;
2781
2782 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2783 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2784 break;
2785 }
2786 if (wpa_s == NULL)
2787 return -1;
2788
2789 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2790}
2791
2792
b1aebbc4
JM
2793static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2794{
2795 struct wpa_supplicant *wpa_s = ctx;
2796
2797 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2798 struct wpa_ssid *ssid = wpa_s->current_ssid;
2799 if (ssid == NULL)
2800 continue;
2801 if (ssid->mode != WPAS_MODE_INFRA)
2802 continue;
d9a0f666
PN
2803 if (wpa_s->wpa_state != WPA_COMPLETED &&
2804 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
b1aebbc4
JM
2805 continue;
2806 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2807 return 1;
2808 }
2809
2810 return 0;
2811}
2812
2813
b22128ef
JM
2814/**
2815 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2816 * @global: Pointer to global data from wpa_supplicant_init()
2817 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2818 * Returns: 0 on success, -1 on failure
2819 */
2820int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2821{
2822 struct p2p_config p2p;
2823 unsigned int r;
f95cac27 2824 int i;
b22128ef
JM
2825
2826 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2827 return 0;
2828
b22128ef
JM
2829 if (global->p2p)
2830 return 0;
2831
3ac17eba
JM
2832 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2833 struct p2p_params params;
2834
2835 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2836 os_memset(&params, 0, sizeof(params));
2837 params.dev_name = wpa_s->conf->device_name;
2f646b6e
JB
2838 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2839 WPS_DEV_TYPE_LEN);
2840 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2841 os_memcpy(params.sec_dev_type,
2842 wpa_s->conf->sec_device_type,
2843 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2844
3ac17eba
JM
2845 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2846 return -1;
2847
2848 return 0;
2849 }
2850
b22128ef
JM
2851 os_memset(&p2p, 0, sizeof(p2p));
2852 p2p.msg_ctx = wpa_s;
2853 p2p.cb_ctx = wpa_s;
2854 p2p.p2p_scan = wpas_p2p_scan;
2855 p2p.send_action = wpas_send_action;
2856 p2p.send_action_done = wpas_send_action_done;
2857 p2p.go_neg_completed = wpas_go_neg_completed;
2858 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2859 p2p.dev_found = wpas_dev_found;
56eeb8f2 2860 p2p.dev_lost = wpas_dev_lost;
b22128ef
JM
2861 p2p.start_listen = wpas_start_listen;
2862 p2p.stop_listen = wpas_stop_listen;
2863 p2p.send_probe_resp = wpas_send_probe_resp;
2864 p2p.sd_request = wpas_sd_request;
2865 p2p.sd_response = wpas_sd_response;
2866 p2p.prov_disc_req = wpas_prov_disc_req;
2867 p2p.prov_disc_resp = wpas_prov_disc_resp;
dd8a7e05 2868 p2p.prov_disc_fail = wpas_prov_disc_fail;
b22128ef
JM
2869 p2p.invitation_process = wpas_invitation_process;
2870 p2p.invitation_received = wpas_invitation_received;
2871 p2p.invitation_result = wpas_invitation_result;
2872 p2p.get_noa = wpas_get_noa;
b1aebbc4 2873 p2p.go_connected = wpas_go_connected;
b22128ef
JM
2874
2875 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
d7e70476 2876 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
b22128ef 2877 p2p.dev_name = wpa_s->conf->device_name;
b6e01800
JM
2878 p2p.manufacturer = wpa_s->conf->manufacturer;
2879 p2p.model_name = wpa_s->conf->model_name;
2880 p2p.model_number = wpa_s->conf->model_number;
2881 p2p.serial_number = wpa_s->conf->serial_number;
2882 if (wpa_s->wps) {
2883 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2884 p2p.config_methods = wpa_s->wps->config_methods;
2885 }
b22128ef
JM
2886
2887 if (wpa_s->conf->p2p_listen_reg_class &&
2888 wpa_s->conf->p2p_listen_channel) {
2889 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2890 p2p.channel = wpa_s->conf->p2p_listen_channel;
2891 } else {
2892 p2p.reg_class = 81;
2893 /*
2894 * Pick one of the social channels randomly as the listen
2895 * channel.
2896 */
2897 os_get_random((u8 *) &r, sizeof(r));
2898 p2p.channel = 1 + (r % 3) * 5;
2899 }
7cfc4ac3 2900 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
b22128ef
JM
2901
2902 if (wpa_s->conf->p2p_oper_reg_class &&
2903 wpa_s->conf->p2p_oper_channel) {
2904 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2905 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
2906 p2p.cfg_op_channel = 1;
2907 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2908 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2909
b22128ef
JM
2910 } else {
2911 p2p.op_reg_class = 81;
2912 /*
7cfc4ac3
AGS
2913 * Use random operation channel from (1, 6, 11) if no other
2914 * preference is indicated.
b22128ef 2915 */
b7412dab 2916 os_get_random((u8 *) &r, sizeof(r));
7cfc4ac3
AGS
2917 p2p.op_channel = 1 + (r % 3) * 5;
2918 p2p.cfg_op_channel = 0;
2919 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2920 "%d:%d", p2p.op_reg_class, p2p.op_channel);
b22128ef 2921 }
b22128ef
JM
2922 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2923 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2924 p2p.country[2] = 0x04;
2925 } else
aaca6505 2926 os_memcpy(p2p.country, "XX\x04", 3);
b22128ef 2927
ac8d1011 2928 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
b22128ef
JM
2929 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2930 "channel list");
2931 return -1;
2932 }
2933
2f646b6e
JB
2934 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2935 WPS_DEV_TYPE_LEN);
b22128ef 2936
2f646b6e
JB
2937 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2938 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2939 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
b22128ef
JM
2940
2941 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2942 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2943
de979d8f 2944 p2p.max_peers = 100;
b22128ef
JM
2945
2946 if (wpa_s->conf->p2p_ssid_postfix) {
2947 p2p.ssid_postfix_len =
2948 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2949 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2950 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2951 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2952 p2p.ssid_postfix_len);
2953 }
2954
0f66abd2
SS
2955 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2956
96beff11
JM
2957 p2p.max_listen = wpa_s->max_remain_on_chan;
2958
b22128ef
JM
2959 global->p2p = p2p_init(&p2p);
2960 if (global->p2p == NULL)
2961 return -1;
ab28911d 2962 global->p2p_init_wpa_s = wpa_s;
b22128ef 2963
f95cac27
JMB
2964 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2965 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2966 continue;
2967 p2p_add_wps_vendor_extension(
2968 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2969 }
2970
b22128ef
JM
2971 return 0;
2972}
2973
2974
2975/**
2976 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2977 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2978 *
2979 * This function deinitialize per-interface P2P data.
2980 */
2981void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2982{
2983 if (wpa_s->driver && wpa_s->drv_priv)
2984 wpa_drv_probe_req_report(wpa_s, 0);
ec437d9e
JJ
2985
2986 if (wpa_s->go_params) {
2987 /* Clear any stored provisioning info */
2988 p2p_clear_provisioning_info(
2989 wpa_s->global->p2p,
10531d21 2990 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
2991 }
2992
b22128ef
JM
2993 os_free(wpa_s->go_params);
2994 wpa_s->go_params = NULL;
b22128ef 2995 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
ef922c4a 2996 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef
JM
2997 wpa_s->p2p_long_listen = 0;
2998 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3071e181 2999 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
b22128ef
JM
3000 wpas_p2p_remove_pending_group_interface(wpa_s);
3001
3002 /* TODO: remove group interface from the driver if this wpa_s instance
3003 * is on top of a P2P group interface */
3004}
3005
3006
3007/**
3008 * wpas_p2p_deinit_global - Deinitialize global P2P module
3009 * @global: Pointer to global data from wpa_supplicant_init()
3010 *
3011 * This function deinitializes the global (per device) P2P module.
3012 */
3013void wpas_p2p_deinit_global(struct wpa_global *global)
3014{
3015 struct wpa_supplicant *wpa_s, *tmp;
b22128ef 3016
bf428a73
JM
3017 wpa_s = global->ifaces;
3018 if (wpa_s)
3019 wpas_p2p_service_flush(wpa_s);
3020
b22128ef
JM
3021 if (global->p2p == NULL)
3022 return;
3023
3024 /* Remove remaining P2P group interfaces */
b22128ef
JM
3025 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3026 wpa_s = wpa_s->next;
3027 while (wpa_s) {
b22128ef
JM
3028 tmp = global->ifaces;
3029 while (tmp &&
3030 (tmp == wpa_s ||
3031 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3032 tmp = tmp->next;
3033 }
3034 if (tmp == NULL)
3035 break;
103b8f4d
NS
3036 /* Disconnect from the P2P group and deinit the interface */
3037 wpas_p2p_disconnect(tmp);
b22128ef
JM
3038 }
3039
743ef799
JM
3040 /*
3041 * Deinit GO data on any possibly remaining interface (if main
3042 * interface is used as GO).
3043 */
3044 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3045 if (wpa_s->ap_iface)
3046 wpas_p2p_group_deinit(wpa_s);
3047 }
3048
b22128ef
JM
3049 p2p_deinit(global->p2p);
3050 global->p2p = NULL;
ab28911d 3051 global->p2p_init_wpa_s = NULL;
b22128ef
JM
3052}
3053
3054
3055static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3056{
d76cd41a
JM
3057 if (wpa_s->conf->p2p_no_group_iface)
3058 return 0; /* separate interface disabled per configuration */
971e357f
JM
3059 if (wpa_s->drv_flags &
3060 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3061 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
b22128ef
JM
3062 return 1; /* P2P group requires a new interface in every case
3063 */
3064 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3065 return 0; /* driver does not support concurrent operations */
3066 if (wpa_s->global->ifaces->next)
3067 return 1; /* more that one interface already in use */
3068 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3069 return 1; /* this interface is already in use */
3070 return 0;
3071}
3072
3073
3074static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3075 const u8 *peer_addr,
3076 enum p2p_wps_method wps_method,
3077 int go_intent, const u8 *own_interface_addr,
23c84252 3078 unsigned int force_freq, int persistent_group,
04a3e69d 3079 struct wpa_ssid *ssid, unsigned int pref_freq)
b22128ef 3080{
acc247b2
JM
3081 if (persistent_group && wpa_s->conf->persistent_reconnect)
3082 persistent_group = 2;
3083
3ac17eba
JM
3084 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3085 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3086 go_intent, own_interface_addr,
3087 force_freq, persistent_group);
3088 }
3089
4f219667
JM
3090 /*
3091 * Increase GO config timeout if HT40 is used since it takes some time
3092 * to scan channels for coex purposes before the BSS can be started.
3093 */
3094 p2p_set_config_timeout(wpa_s->global->p2p,
3095 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3096
b22128ef
JM
3097 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3098 go_intent, own_interface_addr, force_freq,
23c84252 3099 persistent_group, ssid ? ssid->ssid : NULL,
3bc462cb 3100 ssid ? ssid->ssid_len : 0,
04a3e69d 3101 wpa_s->p2p_pd_before_go_neg, pref_freq);
b22128ef
JM
3102}
3103
3104
3105static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3106 const u8 *peer_addr,
3107 enum p2p_wps_method wps_method,
3108 int go_intent, const u8 *own_interface_addr,
23c84252 3109 unsigned int force_freq, int persistent_group,
04a3e69d 3110 struct wpa_ssid *ssid, unsigned int pref_freq)
b22128ef 3111{
acc247b2
JM
3112 if (persistent_group && wpa_s->conf->persistent_reconnect)
3113 persistent_group = 2;
3114
3ac17eba
JM
3115 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3116 return -1;
3117
b22128ef
JM
3118 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3119 go_intent, own_interface_addr, force_freq,
23c84252 3120 persistent_group, ssid ? ssid->ssid : NULL,
04a3e69d 3121 ssid ? ssid->ssid_len : 0, pref_freq);
b22128ef
JM
3122}
3123
3124
9b1ab931
JM
3125static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3126{
3127 wpa_s->p2p_join_scan_count++;
3128 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3129 wpa_s->p2p_join_scan_count);
3130 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3131 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3132 " for join operationg - stop join attempt",
3133 MAC2STR(wpa_s->pending_join_iface_addr));
3134 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
0918c4bf
JM
3135 if (wpa_s->p2p_auto_pd) {
3136 wpa_s->p2p_auto_pd = 0;
3137 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3138 " p2p_dev_addr=" MACSTR " status=N/A",
3139 MAC2STR(wpa_s->pending_join_dev_addr));
3140 return;
3141 }
9b1ab931
JM
3142 wpa_msg(wpa_s->parent, MSG_INFO,
3143 P2P_EVENT_GROUP_FORMATION_FAILURE);
3144 }
3145}
3146
3147
4b156206
JM
3148static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3149{
3150 struct wpa_supplicant *iface;
3151 int shared_freq;
3152 u8 bssid[ETH_ALEN];
3153
3154 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3155 return 0;
3156
3157 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3158 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3159 continue;
3160 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3161 continue;
86ae2e8a
JM
3162 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3163 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3164 shared_freq = iface->current_ssid->frequency;
3165 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3166 shared_freq = iface->assoc_freq;
3167 else
3168 shared_freq = 0;
3169
3170 if (shared_freq && freq != shared_freq) {
3171 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3172 "connected on %d MHz - new connection on "
3173 "%d MHz", iface->ifname, shared_freq, freq);
3174 return 1;
4b156206
JM
3175 }
3176 }
3177
3178 shared_freq = wpa_drv_shared_freq(wpa_s);
3179 if (shared_freq > 0 && shared_freq != freq) {
3180 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3181 "virtual interface connected on %d MHz - new "
3182 "connection on %d MHz", shared_freq, freq);
3183 return 1;
3184 }
3185
3186 return 0;
3187}
3188
3189
b31be3a0
JM
3190static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3191 const u8 *peer_dev_addr)
3192{
3193 struct wpa_bss *bss;
3194 int updated;
3195
3196 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3197 if (bss == NULL)
aa9bb764 3198 return -1;
b31be3a0
JM
3199 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3200 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3201 "last scan");
3202 return 0;
3203 }
3204
3205 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3206 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3207 "%ld.%06ld (%supdated in last scan)",
3208 bss->last_update.sec, bss->last_update.usec,
3209 updated ? "": "not ");
3210
3211 return updated;
3212}
3213
3214
ef922c4a
JM
3215static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3216 struct wpa_scan_results *scan_res)
b22128ef
JM
3217{
3218 struct wpa_bss *bss;
f8d0131a 3219 int freq;
54960629 3220 u8 iface_addr[ETH_ALEN];
b22128ef 3221
ef922c4a 3222 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
b22128ef 3223
ef922c4a
JM
3224 if (wpa_s->global->p2p_disabled)
3225 return;
b22128ef 3226
b31be3a0
JM
3227 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3228 scan_res ? (int) scan_res->num : -1,
3229 wpa_s->p2p_auto_join ? "auto_" : "");
ef922c4a
JM
3230
3231 if (scan_res)
3232 wpas_p2p_scan_res_handler(wpa_s, scan_res);
b22128ef 3233
0918c4bf
JM
3234 if (wpa_s->p2p_auto_pd) {
3235 int join = wpas_p2p_peer_go(wpa_s,
3236 wpa_s->pending_join_dev_addr);
84286a22
SDU
3237 if (join == 0 &&
3238 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3239 wpa_s->auto_pd_scan_retry++;
3240 bss = wpa_bss_get_bssid(wpa_s,
3241 wpa_s->pending_join_dev_addr);
3242 if (bss) {
3243 freq = bss->freq;
3244 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3245 "the peer " MACSTR " at %d MHz",
3246 wpa_s->auto_pd_scan_retry,
3247 MAC2STR(wpa_s->
3248 pending_join_dev_addr),
3249 freq);
3250 wpas_p2p_join_scan_req(wpa_s, freq);
3251 return;
3252 }
3253 }
3254
aa9bb764
JM
3255 if (join < 0)
3256 join = 0;
84286a22 3257
0918c4bf
JM
3258 wpa_s->p2p_auto_pd = 0;
3259 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3260 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3261 MAC2STR(wpa_s->pending_join_dev_addr), join);
3262 if (p2p_prov_disc_req(wpa_s->global->p2p,
3263 wpa_s->pending_join_dev_addr,
3264 wpa_s->pending_pd_config_methods, join,
67527166 3265 0, wpa_s->user_initiated_pd) < 0) {
0918c4bf
JM
3266 wpa_s->p2p_auto_pd = 0;
3267 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3268 " p2p_dev_addr=" MACSTR " status=N/A",
3269 MAC2STR(wpa_s->pending_join_dev_addr));
3270 }
3271 return;
3272 }
3273
b31be3a0 3274 if (wpa_s->p2p_auto_join) {
aa9bb764
JM
3275 int join = wpas_p2p_peer_go(wpa_s,
3276 wpa_s->pending_join_dev_addr);
3277 if (join < 0) {
b31be3a0
JM
3278 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3279 "running a GO -> use GO Negotiation");
3280 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3281 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3282 wpa_s->p2p_persistent_group, 0, 0, 0,
3283 wpa_s->p2p_go_intent,
23c84252 3284 wpa_s->p2p_connect_freq,
3bc462cb 3285 wpa_s->p2p_persistent_id,
e2308e4b
RM
3286 wpa_s->p2p_pd_before_go_neg,
3287 wpa_s->p2p_go_ht40);
b31be3a0
JM
3288 return;
3289 }
3290
aa9bb764
JM
3291 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3292 "try to join the group", join ? "" :
3293 " in older scan");
3294 if (!join)
3295 wpa_s->p2p_fallback_to_go_neg = 1;
b31be3a0
JM
3296 }
3297
f8d0131a
JM
3298 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3299 wpa_s->pending_join_iface_addr);
54960629
AL
3300 if (freq < 0 &&
3301 p2p_get_interface_addr(wpa_s->global->p2p,
3302 wpa_s->pending_join_dev_addr,
3303 iface_addr) == 0 &&
3304 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3305 {
3306 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3307 "address for join from " MACSTR " to " MACSTR
3308 " based on newly discovered P2P peer entry",
3309 MAC2STR(wpa_s->pending_join_iface_addr),
3310 MAC2STR(iface_addr));
3311 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3312 ETH_ALEN);
3313
3314 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3315 wpa_s->pending_join_iface_addr);
3316 }
f8d0131a
JM
3317 if (freq >= 0) {
3318 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3319 "from P2P peer table: %d MHz", freq);
3320 }
ef922c4a 3321 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
b22128ef 3322 if (bss) {
f8d0131a
JM
3323 freq = bss->freq;
3324 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3325 "from BSS table: %d MHz", freq);
3326 }
3327 if (freq > 0) {
b22128ef
JM
3328 u16 method;
3329
4b156206
JM
3330 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3331 wpa_msg(wpa_s->parent, MSG_INFO,
3332 P2P_EVENT_GROUP_FORMATION_FAILURE
3333 "reason=FREQ_CONFLICT");
3334 return;
3335 }
3336
b22128ef
JM
3337 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3338 "prior to joining an existing group (GO " MACSTR
3339 " freq=%u MHz)",
f8d0131a 3340 MAC2STR(wpa_s->pending_join_dev_addr), freq);
b22128ef
JM
3341 wpa_s->pending_pd_before_join = 1;
3342
ef922c4a 3343 switch (wpa_s->pending_join_wps_method) {
b22128ef
JM
3344 case WPS_PIN_DISPLAY:
3345 method = WPS_CONFIG_KEYPAD;
3346 break;
3347 case WPS_PIN_KEYPAD:
3348 method = WPS_CONFIG_DISPLAY;
3349 break;
3350 case WPS_PBC:
3351 method = WPS_CONFIG_PUSHBUTTON;
3352 break;
3353 default:
3354 method = 0;
3355 break;
3356 }
3357
ec437d9e
JJ
3358 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3359 wpa_s->pending_join_dev_addr) ==
3360 method)) {
3361 /*
3362 * We have already performed provision discovery for
3363 * joining the group. Proceed directly to join
3364 * operation without duplicated provision discovery. */
e3a0706b 3365 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
ec437d9e
JJ
3366 "with " MACSTR " already done - proceed to "
3367 "join",
3368 MAC2STR(wpa_s->pending_join_dev_addr));
3369 wpa_s->pending_pd_before_join = 0;
3370 goto start;
3371 }
3372
ef922c4a 3373 if (p2p_prov_disc_req(wpa_s->global->p2p,
1ef2f7ff 3374 wpa_s->pending_join_dev_addr, method, 1,
67527166 3375 freq, wpa_s->user_initiated_pd) < 0) {
b22128ef
JM
3376 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3377 "Discovery Request before joining an "
3378 "existing group");
3379 wpa_s->pending_pd_before_join = 0;
3380 goto start;
3381 }
ef922c4a 3382 return;
b22128ef
JM
3383 }
3384
9b1ab931
JM
3385 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3386 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3387 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3388 wpas_p2p_check_join_scan_limit(wpa_s);
3389 return;
b22128ef
JM
3390
3391start:
3392 /* Start join operation immediately */
ef922c4a
JM
3393 wpas_p2p_join_start(wpa_s);
3394}
3395
3396
84286a22 3397static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
ef922c4a 3398{
ef922c4a
JM
3399 int ret;
3400 struct wpa_driver_scan_params params;
3401 struct wpabuf *wps_ie, *ies;
206e1f42 3402 size_t ielen;
84286a22 3403 int freqs[2] = { 0, 0 };
ef922c4a
JM
3404
3405 os_memset(&params, 0, sizeof(params));
3406
3407 /* P2P Wildcard SSID */
3408 params.num_ssids = 1;
3409 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3410 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3411
3412 wpa_s->wps->dev.p2p = 1;
360182ed
JM
3413 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3414 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3415 NULL);
ef922c4a
JM
3416 if (wps_ie == NULL) {
3417 wpas_p2p_scan_res_join(wpa_s, NULL);
3418 return;
3419 }
3420
206e1f42
JM
3421 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3422 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
ef922c4a
JM
3423 if (ies == NULL) {
3424 wpabuf_free(wps_ie);
3425 wpas_p2p_scan_res_join(wpa_s, NULL);
3426 return;
3427 }
3428 wpabuf_put_buf(ies, wps_ie);
3429 wpabuf_free(wps_ie);
3430
6d92fa6e 3431 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
ef922c4a 3432
47185fc7 3433 params.p2p_probe = 1;
ef922c4a
JM
3434 params.extra_ies = wpabuf_head(ies);
3435 params.extra_ies_len = wpabuf_len(ies);
84286a22
SDU
3436 if (freq > 0) {
3437 freqs[0] = freq;
3438 params.freqs = freqs;
3439 }
ef922c4a
JM
3440
3441 /*
3442 * Run a scan to update BSS table and start Provision Discovery once
3443 * the new scan results become available.
3444 */
17fbb751 3445 ret = wpa_drv_scan(wpa_s, &params);
205e6474
JM
3446 if (!ret)
3447 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
ef922c4a
JM
3448
3449 wpabuf_free(ies);
3450
3451 if (ret) {
3452 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3453 "try again later");
3454 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3455 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
9b1ab931 3456 wpas_p2p_check_join_scan_limit(wpa_s);
ef922c4a
JM
3457 }
3458}
3459
3460
84286a22
SDU
3461static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3462{
3463 struct wpa_supplicant *wpa_s = eloop_ctx;
3464 wpas_p2p_join_scan_req(wpa_s, 0);
3465}
3466
3467
ef922c4a 3468static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
b31be3a0
JM
3469 const u8 *dev_addr, enum p2p_wps_method wps_method,
3470 int auto_join)
ef922c4a
JM
3471{
3472 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
b31be3a0
JM
3473 MACSTR " dev " MACSTR ")%s",
3474 MAC2STR(iface_addr), MAC2STR(dev_addr),
3475 auto_join ? " (auto_join)" : "");
ef922c4a 3476
0918c4bf 3477 wpa_s->p2p_auto_pd = 0;
b31be3a0 3478 wpa_s->p2p_auto_join = !!auto_join;
ef922c4a
JM
3479 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3480 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3481 wpa_s->pending_join_wps_method = wps_method;
3482
3483 /* Make sure we are not running find during connection establishment */
3484 wpas_p2p_stop_find(wpa_s);
3485
9b1ab931 3486 wpa_s->p2p_join_scan_count = 0;
ef922c4a
JM
3487 wpas_p2p_join_scan(wpa_s, NULL);
3488 return 0;
b22128ef
JM
3489}
3490
3491
3492static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3493{
3494 struct wpa_supplicant *group;
3495 struct p2p_go_neg_results res;
8e64f258 3496 struct wpa_bss *bss;
b22128ef
JM
3497
3498 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3499 if (group == NULL)
3500 return -1;
3c5126a4 3501 if (group != wpa_s) {
b22128ef
JM
3502 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3503 sizeof(group->p2p_pin));
3c5126a4 3504 group->p2p_wps_method = wpa_s->p2p_wps_method;
2b79164f
JM
3505 } else {
3506 /*
3507 * Need to mark the current interface for p2p_group_formation
3508 * when a separate group interface is not used. This is needed
3509 * to allow p2p_cancel stop a pending p2p_connect-join.
3510 * wpas_p2p_init_group_interface() addresses this for the case
3511 * where a separate group interface is used.
3512 */
3513 wpa_s->global->p2p_group_formation = wpa_s;
3c5126a4 3514 }
b22128ef
JM
3515
3516 group->p2p_in_provisioning = 1;
aa9bb764 3517 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
b22128ef
JM
3518
3519 os_memset(&res, 0, sizeof(res));
3520 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3521 ETH_ALEN);
3522 res.wps_method = wpa_s->pending_join_wps_method;
8e64f258
JM
3523 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3524 if (bss) {
3525 res.freq = bss->freq;
3526 res.ssid_len = bss->ssid_len;
3527 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3528 }
3529
e91829f9
JM
3530 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3531 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3532 "starting client");
3533 wpa_drv_cancel_remain_on_channel(wpa_s);
3534 wpa_s->off_channel_freq = 0;
3535 wpa_s->roc_waiting_drv_freq = 0;
3536 }
b22128ef
JM
3537 wpas_start_wps_enrollee(group, &res);
3538
3094d483
JM
3539 /*
3540 * Allow a longer timeout for join-a-running-group than normal 15
3541 * second group formation timeout since the GO may not have authorized
3542 * our connection yet.
3543 */
3544 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3545 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3546 wpa_s, NULL);
3547
b22128ef
JM
3548 return 0;
3549}
3550
3551
3552/**
3553 * wpas_p2p_connect - Request P2P Group Formation to be started
3554 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3555 * @peer_addr: Address of the peer P2P Device
3556 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3557 * @persistent_group: Whether to create a persistent group
b31be3a0 3558 * @auto_join: Whether to select join vs. GO Negotiation automatically
b22128ef
JM
3559 * @join: Whether to join an existing group (as a client) instead of starting
3560 * Group Owner negotiation; @peer_addr is BSSID in that case
3561 * @auth: Whether to only authorize the connection instead of doing that and
3562 * initiating Group Owner negotiation
3563 * @go_intent: GO Intent or -1 to use default
3564 * @freq: Frequency for the group or 0 for auto-selection
23c84252
JM
3565 * @persistent_id: Persistent group credentials to use for forcing GO
3566 * parameters or -1 to generate new values (SSID/passphrase)
3bc462cb
JM
3567 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3568 * interoperability workaround when initiating group formation
e2308e4b 3569 * @ht40: Start GO with 40 MHz channel width
d054a462
JM
3570 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3571 * failure, -2 on failure due to channel not currently available,
3572 * -3 if forced channel is not supported
b22128ef
JM
3573 */
3574int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3575 const char *pin, enum p2p_wps_method wps_method,
b31be3a0 3576 int persistent_group, int auto_join, int join, int auth,
e2308e4b
RM
3577 int go_intent, int freq, int persistent_id, int pd,
3578 int ht40)
b22128ef 3579{
04a3e69d 3580 int force_freq = 0, pref_freq = 0, oper_freq = 0;
b22128ef
JM
3581 u8 bssid[ETH_ALEN];
3582 int ret = 0;
3583 enum wpa_driver_if_type iftype;
6cad95db 3584 const u8 *if_addr;
23c84252 3585 struct wpa_ssid *ssid = NULL;
b22128ef 3586
9526fd29
JM
3587 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3588 return -1;
3589
23c84252
JM
3590 if (persistent_id >= 0) {
3591 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3592 if (ssid == NULL || ssid->disabled != 2 ||
3593 ssid->mode != WPAS_MODE_P2P_GO)
3594 return -1;
3595 }
3596
b22128ef
JM
3597 if (go_intent < 0)
3598 go_intent = wpa_s->conf->p2p_go_intent;
3599
3600 if (!auth)
3601 wpa_s->p2p_long_listen = 0;
3602
3c5126a4 3603 wpa_s->p2p_wps_method = wps_method;
b31be3a0 3604 wpa_s->p2p_persistent_group = !!persistent_group;
23c84252 3605 wpa_s->p2p_persistent_id = persistent_id;
b31be3a0
JM
3606 wpa_s->p2p_go_intent = go_intent;
3607 wpa_s->p2p_connect_freq = freq;
aa9bb764 3608 wpa_s->p2p_fallback_to_go_neg = 0;
3bc462cb 3609 wpa_s->p2p_pd_before_go_neg = !!pd;
e2308e4b 3610 wpa_s->p2p_go_ht40 = !!ht40;
3c5126a4 3611
b22128ef
JM
3612 if (pin)
3613 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3614 else if (wps_method == WPS_PIN_DISPLAY) {
3615 ret = wps_generate_pin();
3616 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3617 ret);
3618 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3619 wpa_s->p2p_pin);
3620 } else
3621 wpa_s->p2p_pin[0] = '\0';
3622
b31be3a0 3623 if (join || auto_join) {
4147a2cc 3624 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
108def93
JM
3625 if (auth) {
3626 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3627 "connect a running group from " MACSTR,
3628 MAC2STR(peer_addr));
3629 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3630 return ret;
3631 }
4147a2cc 3632 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
b22128ef 3633 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4147a2cc 3634 iface_addr) < 0) {
b22128ef 3635 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4147a2cc
JM
3636 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3637 dev_addr);
3638 }
b31be3a0
JM
3639 if (auto_join) {
3640 os_get_time(&wpa_s->p2p_auto_started);
3641 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3642 "%ld.%06ld",
3643 wpa_s->p2p_auto_started.sec,
3644 wpa_s->p2p_auto_started.usec);
3645 }
67527166 3646 wpa_s->user_initiated_pd = 1;
b31be3a0
JM
3647 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3648 auto_join) < 0)
b22128ef
JM
3649 return -1;
3650 return ret;
3651 }
3652
d054a462
JM
3653 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3654 wpa_s->assoc_freq)
3655 oper_freq = wpa_s->assoc_freq;
b22128ef 3656 else {
d054a462
JM
3657 oper_freq = wpa_drv_shared_freq(wpa_s);
3658 if (oper_freq < 0)
3659 oper_freq = 0;
b22128ef
JM
3660 }
3661
d054a462
JM
3662 if (freq > 0) {
3663 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3664 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3665 "(%u MHz) is not supported for P2P uses",
3666 freq);
3667 return -3;
3668 }
3669
3670 if (oper_freq > 0 && freq != oper_freq &&
3671 !(wpa_s->drv_flags &
3672 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3673 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3674 "on %u MHz while connected on another "
3675 "channel (%u MHz)", freq, oper_freq);
3676 return -2;
3677 }
3678 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3679 "requested channel (%u MHz)", freq);
3680 force_freq = freq;
3681 } else if (oper_freq > 0 &&
3682 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3683 if (!(wpa_s->drv_flags &
3684 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3685 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3686 "while connected on non-P2P supported "
3687 "channel (%u MHz)", oper_freq);
3688 return -2;
3689 }
3690 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3691 "(%u MHz) not available for P2P - try to use "
3692 "another channel", oper_freq);
3693 force_freq = 0;
04a3e69d
JM
3694 } else if (oper_freq > 0 &&
3695 (wpa_s->drv_flags &
3696 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3697 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3698 "are already using (%u MHz) on another interface",
3699 oper_freq);
3700 pref_freq = oper_freq;
d054a462 3701 } else if (oper_freq > 0) {
b22128ef
JM
3702 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3703 "channel we are already using (%u MHz) on another "
d054a462
JM
3704 "interface", oper_freq);
3705 force_freq = oper_freq;
b22128ef
JM
3706 }
3707
3708 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3709
6cad95db
JB
3710 if (wpa_s->create_p2p_iface) {
3711 /* Prepare to add a new interface for the group */
3712 iftype = WPA_IF_P2P_GROUP;
3713 if (go_intent == 15)
3714 iftype = WPA_IF_P2P_GO;
3715 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3716 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3717 "interface for the group");
b22128ef 3718 return -1;
6cad95db 3719 }
b22128ef 3720
6cad95db
JB
3721 if_addr = wpa_s->pending_interface_addr;
3722 } else
3723 if_addr = wpa_s->own_addr;
b22128ef
JM
3724
3725 if (auth) {
3726 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
6cad95db 3727 go_intent, if_addr,
04a3e69d
JM
3728 force_freq, persistent_group, ssid,
3729 pref_freq) < 0)
b22128ef
JM
3730 return -1;
3731 return ret;
3732 }
6cad95db
JB
3733
3734 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3735 go_intent, if_addr, force_freq,
04a3e69d 3736 persistent_group, ssid, pref_freq) < 0) {
6cad95db
JB
3737 if (wpa_s->create_p2p_iface)
3738 wpas_p2p_remove_pending_group_interface(wpa_s);
b22128ef
JM
3739 return -1;
3740 }
3741 return ret;
3742}
3743
3744
3745/**
3746 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3747 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3748 * @freq: Frequency of the channel in MHz
3749 * @duration: Duration of the stay on the channel in milliseconds
3750 *
3751 * This callback is called when the driver indicates that it has started the
3752 * requested remain-on-channel duration.
3753 */
3754void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3755 unsigned int freq, unsigned int duration)
3756{
9526fd29
JM
3757 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3758 return;
b22128ef
JM
3759 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3760 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3761 wpa_s->pending_listen_duration);
3762 wpa_s->pending_listen_freq = 0;
3763 }
3764}
3765
3766
3767static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3768 unsigned int timeout)
3769{
3770 /* Limit maximum Listen state time based on driver limitation. */
3771 if (timeout > wpa_s->max_remain_on_chan)
3772 timeout = wpa_s->max_remain_on_chan;
3773
3ac17eba
JM
3774 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3775 return wpa_drv_p2p_listen(wpa_s, timeout);
3776
b22128ef
JM
3777 return p2p_listen(wpa_s->global->p2p, timeout);
3778}
3779
3780
3781/**
3782 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3783 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3784 * @freq: Frequency of the channel in MHz
3785 *
3786 * This callback is called when the driver indicates that a remain-on-channel
3787 * operation has been completed, i.e., the duration on the requested channel
3788 * has timed out.
3789 */
3790void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3791 unsigned int freq)
3792{
1cc3a29d 3793 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
11fb02be 3794 "(p2p_long_listen=%d ms pending_action_tx=%p)",
2f3101d8 3795 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
9526fd29
JM
3796 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3797 return;
b22128ef
JM
3798 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3799 return; /* P2P module started a new operation */
2f3101d8 3800 if (offchannel_pending_action_tx(wpa_s))
b22128ef
JM
3801 return;
3802 if (wpa_s->p2p_long_listen > 0)
11fb02be 3803 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
b22128ef
JM
3804 if (wpa_s->p2p_long_listen > 0) {
3805 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
11fb02be 3806 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
b22128ef
JM
3807 }
3808}
3809
3810
3811/**
3812 * wpas_p2p_group_remove - Remove a P2P group
3813 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3814 * @ifname: Network interface name of the group interface or "*" to remove all
3815 * groups
3816 * Returns: 0 on success, -1 on failure
3817 *
3818 * This function is used to remove a P2P group. This can be used to disconnect
3819 * from a group in which the local end is a P2P Client or to end a P2P Group in
3820 * case the local end is the Group Owner. If a virtual network interface was
3821 * created for this group, that interface will be removed. Otherwise, only the
3822 * configured P2P group network will be removed from the interface.
3823 */
3824int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3825{
3826 struct wpa_global *global = wpa_s->global;
3827
3828 if (os_strcmp(ifname, "*") == 0) {
3829 struct wpa_supplicant *prev;
3830 wpa_s = global->ifaces;
3831 while (wpa_s) {
3832 prev = wpa_s;
3833 wpa_s = wpa_s->next;
3103f345 3834 wpas_p2p_disconnect(prev);
b22128ef
JM
3835 }
3836 return 0;
3837 }
3838
3839 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3840 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3841 break;
3842 }
3843
3103f345 3844 return wpas_p2p_disconnect(wpa_s);
b22128ef
JM
3845}
3846
3847
b8349523
NKG
3848static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3849 struct p2p_go_neg_results *params,
7aeac985 3850 int freq, int ht40)
b22128ef
JM
3851{
3852 u8 bssid[ETH_ALEN];
3853 int res;
3854
3855 os_memset(params, 0, sizeof(*params));
3856 params->role_go = 1;
7aeac985 3857 params->ht40 = ht40;
7cfc4ac3
AGS
3858 if (freq) {
3859 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3860 "frequency %d MHz", freq);
b22128ef 3861 params->freq = freq;
7cfc4ac3
AGS
3862 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3863 wpa_s->conf->p2p_oper_channel >= 1 &&
3864 wpa_s->conf->p2p_oper_channel <= 11) {
b22128ef 3865 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3866 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3867 "frequency %d MHz", params->freq);
3868 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
409ca9a8
JM
3869 wpa_s->conf->p2p_oper_reg_class == 116 ||
3870 wpa_s->conf->p2p_oper_reg_class == 117 ||
3871 wpa_s->conf->p2p_oper_reg_class == 124 ||
3872 wpa_s->conf->p2p_oper_reg_class == 126 ||
3873 wpa_s->conf->p2p_oper_reg_class == 127) {
b22128ef 3874 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7cfc4ac3
AGS
3875 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3876 "frequency %d MHz", params->freq);
3877 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3878 wpa_s->best_overall_freq > 0 &&
3879 p2p_supported_freq(wpa_s->global->p2p,
3880 wpa_s->best_overall_freq)) {
3881 params->freq = wpa_s->best_overall_freq;
3882 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3883 "channel %d MHz", params->freq);
3884 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3885 wpa_s->best_24_freq > 0 &&
3886 p2p_supported_freq(wpa_s->global->p2p,
3887 wpa_s->best_24_freq)) {
3888 params->freq = wpa_s->best_24_freq;
3889 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3890 "channel %d MHz", params->freq);
3891 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3892 wpa_s->best_5_freq > 0 &&
3893 p2p_supported_freq(wpa_s->global->p2p,
3894 wpa_s->best_5_freq)) {
3895 params->freq = wpa_s->best_5_freq;
3896 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3897 "channel %d MHz", params->freq);
3898 } else {
6f3bc72b
JM
3899 int chan;
3900 for (chan = 0; chan < 11; chan++) {
3901 params->freq = 2412 + chan * 5;
3902 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3903 params->freq))
3904 break;
3905 }
3906 if (chan == 11) {
3907 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3908 "allowed");
3909 return -1;
3910 }
7cfc4ac3
AGS
3911 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3912 "known)", params->freq);
3913 }
3914
b22128ef
JM
3915 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3916 wpa_s->assoc_freq && !freq) {
3917 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3918 "already using");
3919 params->freq = wpa_s->assoc_freq;
3920 }
3921
3922 res = wpa_drv_shared_freq(wpa_s);
3923 if (res > 0 && !freq) {
3924 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3925 "already using on a shared interface");
3926 params->freq = res;
b8349523
NKG
3927 } else if (res > 0 && freq != res &&
3928 !(wpa_s->drv_flags &
3929 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3930 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3931 "while connected on another channel (%u MHz)",
3932 freq, res);
3933 return -1;
b22128ef 3934 }
b8349523
NKG
3935
3936 return 0;
b22128ef
JM
3937}
3938
3939
3940static struct wpa_supplicant *
3941wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3942 int go)
3943{
3944 struct wpa_supplicant *group_wpa_s;
3945
ac06fb12
JM
3946 if (!wpas_p2p_create_iface(wpa_s)) {
3947 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3948 "operations");
b22128ef 3949 return wpa_s;
ac06fb12 3950 }
b22128ef
JM
3951
3952 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
ac06fb12
JM
3953 WPA_IF_P2P_CLIENT) < 0) {
3954 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
b22128ef 3955 return NULL;
ac06fb12 3956 }
b22128ef
JM
3957 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3958 if (group_wpa_s == NULL) {
ac06fb12
JM
3959 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
3960 "interface");
b22128ef
JM
3961 wpas_p2p_remove_pending_group_interface(wpa_s);
3962 return NULL;
3963 }
3964
ac06fb12
JM
3965 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
3966 group_wpa_s->ifname);
b22128ef
JM
3967 return group_wpa_s;
3968}
3969
3970
3971/**
3972 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3973 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3974 * @persistent_group: Whether to create a persistent group
3975 * @freq: Frequency for the group or 0 to indicate no hardcoding
3976 * Returns: 0 on success, -1 on failure
3977 *
3978 * This function creates a new P2P group with the local end as the Group Owner,
3979 * i.e., without using Group Owner Negotiation.
3980 */
3981int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
7aeac985 3982 int freq, int ht40)
b22128ef
JM
3983{
3984 struct p2p_go_neg_results params;
7cfc4ac3
AGS
3985 unsigned int r;
3986
9526fd29
JM
3987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3988 return -1;
3989
2d4f15d6
JJ
3990 /* Make sure we are not running find during connection establishment */
3991 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
8713a2e6 3992 wpas_p2p_stop_find_oper(wpa_s);
2d4f15d6 3993
7cfc4ac3
AGS
3994 if (freq == 2) {
3995 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3996 "band");
3997 if (wpa_s->best_24_freq > 0 &&
3998 p2p_supported_freq(wpa_s->global->p2p,
3999 wpa_s->best_24_freq)) {
4000 freq = wpa_s->best_24_freq;
4001 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4002 "channel: %d MHz", freq);
4003 } else {
4004 os_get_random((u8 *) &r, sizeof(r));
4005 freq = 2412 + (r % 3) * 25;
4006 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4007 "channel: %d MHz", freq);
4008 }
4009 }
4010
4011 if (freq == 5) {
4012 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4013 "band");
4014 if (wpa_s->best_5_freq > 0 &&
4015 p2p_supported_freq(wpa_s->global->p2p,
4016 wpa_s->best_5_freq)) {
4017 freq = wpa_s->best_5_freq;
4018 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4019 "channel: %d MHz", freq);
4020 } else {
4021 os_get_random((u8 *) &r, sizeof(r));
4022 freq = 5180 + (r % 4) * 20;
4023 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4024 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4025 "5 GHz channel for P2P group");
4026 return -1;
4027 }
4028 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4029 "channel: %d MHz", freq);
4030 }
4031 }
b22128ef 4032
4ae4650b
JM
4033 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4034 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4035 "(%u MHz) is not supported for P2P uses",
4036 freq);
4037 return -1;
4038 }
4039
7aeac985 4040 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
b8349523 4041 return -1;
f4329aa2
JM
4042 if (params.freq &&
4043 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4044 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4045 "(%u MHz) is not supported for P2P uses",
4046 params.freq);
4047 return -1;
4048 }
b22128ef
JM
4049 p2p_go_params(wpa_s->global->p2p, &params);
4050 params.persistent_group = persistent_group;
4051
4052 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4053 if (wpa_s == NULL)
4054 return -1;
4055 wpas_start_wps_go(wpa_s, &params, 0);
4056
4057 return 0;
4058}
4059
4060
4061static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4062 struct wpa_ssid *params, int addr_allocated)
4063{
4064 struct wpa_ssid *ssid;
4065
4066 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4067 if (wpa_s == NULL)
4068 return -1;
4069
4070 wpa_supplicant_ap_deinit(wpa_s);
4071
4072 ssid = wpa_config_add_network(wpa_s->conf);
4073 if (ssid == NULL)
4074 return -1;
b22128ef
JM
4075 wpa_config_set_network_defaults(ssid);
4076 ssid->temporary = 1;
4077 ssid->proto = WPA_PROTO_RSN;
4078 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4079 ssid->group_cipher = WPA_CIPHER_CCMP;
4080 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4081 ssid->ssid = os_malloc(params->ssid_len);
4082 if (ssid->ssid == NULL) {
b22128ef
JM
4083 wpa_config_remove_network(wpa_s->conf, ssid->id);
4084 return -1;
4085 }
4086 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4087 ssid->ssid_len = params->ssid_len;
4088 ssid->p2p_group = 1;
d1c8ac88 4089 ssid->export_keys = 1;
b22128ef
JM
4090 if (params->psk_set) {
4091 os_memcpy(ssid->psk, params->psk, 32);
4092 ssid->psk_set = 1;
4093 }
4094 if (params->passphrase)
4095 ssid->passphrase = os_strdup(params->passphrase);
4096
4097 wpa_supplicant_select_network(wpa_s, ssid);
4098
4099 wpa_s->show_group_started = 1;
4100
4101 return 0;
4102}
4103
4104
4105int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4106 struct wpa_ssid *ssid, int addr_allocated,
7aeac985 4107 int freq, int ht40)
b22128ef
JM
4108{
4109 struct p2p_go_neg_results params;
6c0da49f 4110 int go = 0;
b22128ef
JM
4111
4112 if (ssid->disabled != 2 || ssid->ssid == NULL)
4113 return -1;
4114
6c0da49f
JM
4115 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4116 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4117 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4118 "already running");
4119 return 0;
4120 }
4121
9d39057c 4122 /* Make sure we are not running find during connection establishment */
8713a2e6 4123 wpas_p2p_stop_find_oper(wpa_s);
b22128ef 4124
aa9bb764
JM
4125 wpa_s->p2p_fallback_to_go_neg = 0;
4126
b22128ef
JM
4127 if (ssid->mode == WPAS_MODE_INFRA)
4128 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4129
4130 if (ssid->mode != WPAS_MODE_P2P_GO)
4131 return -1;
4132
7aeac985 4133 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
b8349523 4134 return -1;
b22128ef
JM
4135
4136 params.role_go = 1;
30c371e8
MH
4137 params.psk_set = ssid->psk_set;
4138 if (params.psk_set)
4139 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
bb4d4deb
MH
4140 if (ssid->passphrase) {
4141 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4142 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4143 "persistent group");
4144 return -1;
4145 }
4146 os_strlcpy(params.passphrase, ssid->passphrase,
4147 sizeof(params.passphrase));
b22128ef 4148 }
b22128ef
JM
4149 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4150 params.ssid_len = ssid->ssid_len;
4151 params.persistent_group = 1;
4152
4153 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4154 if (wpa_s == NULL)
4155 return -1;
4156
4157 wpas_start_wps_go(wpa_s, &params, 0);
4158
4159 return 0;
4160}
4161
4162
4163static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4164 struct wpabuf *proberesp_ies)
4165{
4166 struct wpa_supplicant *wpa_s = ctx;
4167 if (wpa_s->ap_iface) {
4168 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
adc33680
JM
4169 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4170 wpabuf_free(beacon_ies);
4171 wpabuf_free(proberesp_ies);
4172 return;
4173 }
b22128ef
JM
4174 if (beacon_ies) {
4175 wpabuf_free(hapd->p2p_beacon_ie);
4176 hapd->p2p_beacon_ie = beacon_ies;
4177 }
4178 wpabuf_free(hapd->p2p_probe_resp_ie);
4179 hapd->p2p_probe_resp_ie = proberesp_ies;
4180 } else {
4181 wpabuf_free(beacon_ies);
4182 wpabuf_free(proberesp_ies);
4183 }
4184 wpa_supplicant_ap_update_beacon(wpa_s);
4185}
4186
4187
3071e181
JM
4188static void wpas_p2p_idle_update(void *ctx, int idle)
4189{
4190 struct wpa_supplicant *wpa_s = ctx;
4191 if (!wpa_s->ap_iface)
4192 return;
4193 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4194 if (idle)
4195 wpas_p2p_set_group_idle_timeout(wpa_s);
4196 else
4197 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4198}
4199
4200
b22128ef 4201struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6f251b6b 4202 struct wpa_ssid *ssid)
b22128ef
JM
4203{
4204 struct p2p_group *group;
4205 struct p2p_group_config *cfg;
4206
3ac17eba
JM
4207 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4208 return NULL;
9526fd29
JM
4209 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4210 return NULL;
3ac17eba 4211
b22128ef
JM
4212 cfg = os_zalloc(sizeof(*cfg));
4213 if (cfg == NULL)
4214 return NULL;
4215
6f251b6b 4216 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
acc247b2 4217 cfg->persistent_group = 2;
6f251b6b 4218 else if (ssid->p2p_persistent_group)
acc247b2 4219 cfg->persistent_group = 1;
b22128ef 4220 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3f4ce13f
JM
4221 if (wpa_s->max_stations &&
4222 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4223 cfg->max_clients = wpa_s->max_stations;
4224 else
4225 cfg->max_clients = wpa_s->conf->max_num_sta;
6f251b6b
JM
4226 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4227 cfg->ssid_len = ssid->ssid_len;
b22128ef
JM
4228 cfg->cb_ctx = wpa_s;
4229 cfg->ie_update = wpas_p2p_ie_update;
3071e181 4230 cfg->idle_update = wpas_p2p_idle_update;
b22128ef
JM
4231
4232 group = p2p_group_init(wpa_s->global->p2p, cfg);
4233 if (group == NULL)
4234 os_free(cfg);
6f251b6b 4235 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
b22128ef
JM
4236 p2p_group_notif_formation_done(group);
4237 wpa_s->p2p_group = group;
4238 return group;
4239}
4240
4241
4242void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4243 int registrar)
4244{
10531d21
JM
4245 struct wpa_ssid *ssid = wpa_s->current_ssid;
4246
b22128ef
JM
4247 if (!wpa_s->p2p_in_provisioning) {
4248 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4249 "provisioning not in progress");
4250 return;
4251 }
4252
10531d21
JM
4253 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4254 u8 go_dev_addr[ETH_ALEN];
4255 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4256 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4257 ssid->ssid_len);
4258 /* Clear any stored provisioning info */
4259 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4260 }
ec437d9e 4261
b22128ef
JM
4262 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4263 NULL);
361cdf34
JM
4264 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4265 /*
4266 * Use a separate timeout for initial data connection to
4267 * complete to allow the group to be removed automatically if
4268 * something goes wrong in this step before the P2P group idle
4269 * timeout mechanism is taken into use.
4270 */
4271 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4272 wpas_p2p_group_formation_timeout,
ad853202 4273 wpa_s->parent, NULL);
361cdf34 4274 }
b22128ef
JM
4275 if (wpa_s->global->p2p)
4276 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3ac17eba
JM
4277 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4278 wpa_drv_wps_success_cb(wpa_s, peer_addr);
b22128ef
JM
4279 wpas_group_formation_completed(wpa_s, 1);
4280}
4281
4282
3734552f
JS
4283void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4284 struct wps_event_fail *fail)
4285{
4286 if (!wpa_s->p2p_in_provisioning) {
4287 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4288 "provisioning not in progress");
4289 return;
4290 }
ec437d9e
JJ
4291
4292 if (wpa_s->go_params) {
4293 p2p_clear_provisioning_info(
4294 wpa_s->global->p2p,
10531d21 4295 wpa_s->go_params->peer_device_addr);
ec437d9e
JJ
4296 }
4297
3734552f
JS
4298 wpas_notify_p2p_wps_failed(wpa_s, fail);
4299}
4300
4301
b22128ef 4302int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
0918c4bf
JM
4303 const char *config_method,
4304 enum wpas_p2p_prov_disc_use use)
b22128ef
JM
4305{
4306 u16 config_methods;
4307
aa9bb764 4308 wpa_s->p2p_fallback_to_go_neg = 0;
0918c4bf 4309 wpa_s->pending_pd_use = NORMAL_PD;
8c5f7309 4310 if (os_strncmp(config_method, "display", 7) == 0)
b22128ef 4311 config_methods = WPS_CONFIG_DISPLAY;
8c5f7309 4312 else if (os_strncmp(config_method, "keypad", 6) == 0)
b22128ef 4313 config_methods = WPS_CONFIG_KEYPAD;
8c5f7309
JJ
4314 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4315 os_strncmp(config_method, "pushbutton", 10) == 0)
b22128ef 4316 config_methods = WPS_CONFIG_PUSHBUTTON;
8c5f7309
JJ
4317 else {
4318 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
b22128ef 4319 return -1;
8c5f7309 4320 }
b22128ef 4321
0918c4bf
JM
4322 if (use == WPAS_P2P_PD_AUTO) {
4323 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4324 wpa_s->pending_pd_config_methods = config_methods;
4325 wpa_s->p2p_auto_pd = 1;
4326 wpa_s->p2p_auto_join = 0;
4327 wpa_s->pending_pd_before_join = 0;
84286a22 4328 wpa_s->auto_pd_scan_retry = 0;
0918c4bf
JM
4329 wpas_p2p_stop_find(wpa_s);
4330 wpa_s->p2p_join_scan_count = 0;
84286a22
SDU
4331 os_get_time(&wpa_s->p2p_auto_started);
4332 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4333 wpa_s->p2p_auto_started.sec,
4334 wpa_s->p2p_auto_started.usec);
0918c4bf
JM
4335 wpas_p2p_join_scan(wpa_s, NULL);
4336 return 0;
4337 }
4338
3ac17eba
JM
4339 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4340 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
0918c4bf
JM
4341 config_methods,
4342 use == WPAS_P2P_PD_FOR_JOIN);
3ac17eba
JM
4343 }
4344
9526fd29 4345 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
b22128ef
JM
4346 return -1;
4347
4348 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
0918c4bf 4349 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
67527166 4350 0, 1);
b22128ef
JM
4351}
4352
4353
4354int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4355 char *end)
4356{
4357 return p2p_scan_result_text(ies, ies_len, buf, end);
4358}
4359
4360
1cc3a29d
JM
4361static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4362{
2f3101d8 4363 if (!offchannel_pending_action_tx(wpa_s))
1cc3a29d
JM
4364 return;
4365
4366 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4367 "operation request");
2f3101d8 4368 offchannel_clear_pending_action_tx(wpa_s);
1cc3a29d
JM
4369}
4370
4371
b22128ef 4372int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
046ef4aa 4373 enum p2p_discovery_type type,
6d92fa6e 4374 unsigned int num_req_dev_types, const u8 *req_dev_types,
37448ede 4375 const u8 *dev_id, unsigned int search_delay)
b22128ef 4376{
1cc3a29d 4377 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef
JM
4378 wpa_s->p2p_long_listen = 0;
4379
3ac17eba
JM
4380 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4381 return wpa_drv_p2p_find(wpa_s, timeout, type);
4382
5bda43cd
SDU
4383 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4384 wpa_s->p2p_in_provisioning)
9526fd29
JM
4385 return -1;
4386
433cd2ce
JM
4387 wpa_supplicant_cancel_sched_scan(wpa_s);
4388
046ef4aa 4389 return p2p_find(wpa_s->global->p2p, timeout, type,
37448ede
JM
4390 num_req_dev_types, req_dev_types, dev_id,
4391 search_delay);
b22128ef
JM
4392}
4393
4394
8713a2e6 4395static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
b22128ef 4396{
1cc3a29d 4397 wpas_p2p_clear_pending_action_tx(wpa_s);
b22128ef 4398 wpa_s->p2p_long_listen = 0;
9d39057c 4399 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
ef922c4a 4400 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
e665ca9a 4401 wpa_s->global->p2p_cb_on_scan_complete = 0;
b22128ef 4402
3ac17eba
JM
4403 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4404 wpa_drv_p2p_stop_find(wpa_s);
8713a2e6 4405 return 1;
3ac17eba
JM
4406 }
4407
9526fd29
JM
4408 if (wpa_s->global->p2p)
4409 p2p_stop_find(wpa_s->global->p2p);
b22128ef 4410
8713a2e6
JM
4411 return 0;
4412}
4413
4414
4415void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4416{
4417 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4418 return;
b22128ef
JM
4419 wpas_p2p_remove_pending_group_interface(wpa_s);
4420}
4421
4422
4423static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4424{
4425 struct wpa_supplicant *wpa_s = eloop_ctx;
4426 wpa_s->p2p_long_listen = 0;
4427}
4428
4429
4430int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4431{
4432 int res;
4433
9526fd29
JM
4434 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4435 return -1;
4436
433cd2ce 4437 wpa_supplicant_cancel_sched_scan(wpa_s);
1cc3a29d
JM
4438 wpas_p2p_clear_pending_action_tx(wpa_s);
4439
b22128ef
JM
4440 if (timeout == 0) {
4441 /*
4442 * This is a request for unlimited Listen state. However, at
4443 * least for now, this is mapped to a Listen state for one
4444 * hour.
4445 */
4446 timeout = 3600;
4447 }
4448 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4449 wpa_s->p2p_long_listen = 0;
4450
2bb747e2
JM
4451 /*
4452 * Stop previous find/listen operation to avoid trying to request a new
4453 * remain-on-channel operation while the driver is still running the
4454 * previous one.
4455 */
4456 if (wpa_s->global->p2p)
4457 p2p_stop_find(wpa_s->global->p2p);
4458
b22128ef
JM
4459 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4460 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
11fb02be 4461 wpa_s->p2p_long_listen = timeout * 1000;
b22128ef
JM
4462 eloop_register_timeout(timeout, 0,
4463 wpas_p2p_long_listen_timeout,
4464 wpa_s, NULL);
4465 }
4466
4467 return res;
4468}
4469
4470
4c08c0bd 4471int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
b22128ef
JM
4472 u8 *buf, size_t len, int p2p_group)
4473{
4c08c0bd
JM
4474 struct wpabuf *p2p_ie;
4475 int ret;
4476
b22128ef
JM
4477 if (wpa_s->global->p2p_disabled)
4478 return -1;
4479 if (wpa_s->global->p2p == NULL)
4480 return -1;
e1f1509b
JM
4481 if (bss == NULL)
4482 return -1;
b22128ef 4483
4c08c0bd
JM
4484 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4485 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4486 p2p_group, p2p_ie);
4487 wpabuf_free(p2p_ie);
4488
4489 return ret;
b22128ef
JM
4490}
4491
4492
4493int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
04a85e44 4494 const u8 *dst, const u8 *bssid,
baf513d6 4495 const u8 *ie, size_t ie_len, int ssi_signal)
b22128ef
JM
4496{
4497 if (wpa_s->global->p2p_disabled)
4498 return 0;
4499 if (wpa_s->global->p2p == NULL)
4500 return 0;
4501
2d43d37f
JB
4502 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4503 ie, ie_len)) {
4504 case P2P_PREQ_NOT_P2P:
4505 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4506 ssi_signal);
4507 /* fall through */
4508 case P2P_PREQ_MALFORMED:
4509 case P2P_PREQ_NOT_LISTEN:
4510 case P2P_PREQ_NOT_PROCESSED:
4511 default: /* make gcc happy */
4512 return 0;
4513 case P2P_PREQ_PROCESSED:
4514 return 1;
4515 }
b22128ef
JM
4516}
4517
4518
4519void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4520 const u8 *sa, const u8 *bssid,
4521 u8 category, const u8 *data, size_t len, int freq)
4522{
4523 if (wpa_s->global->p2p_disabled)
4524 return;
4525 if (wpa_s->global->p2p == NULL)
4526 return;
4527
4528 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4529 freq);
4530}
4531
4532
4533void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4534{
4535 if (wpa_s->global->p2p_disabled)
4536 return;
4537 if (wpa_s->global->p2p == NULL)
4538 return;
4539
6d92fa6e 4540 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
b22128ef
JM
4541}
4542
4543
4544void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4545{
4546 p2p_group_deinit(wpa_s->p2p_group);
4547 wpa_s->p2p_group = NULL;
a7fd39bb
JD
4548
4549 wpa_s->ap_configured_cb = NULL;
4550 wpa_s->ap_configured_cb_ctx = NULL;
4551 wpa_s->ap_configured_cb_data = NULL;
4552 wpa_s->connect_without_scan = NULL;
b22128ef
JM
4553}
4554
4555
4556int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4557{
4558 wpa_s->p2p_long_listen = 0;
4559
3ac17eba
JM
4560 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4561 return wpa_drv_p2p_reject(wpa_s, addr);
4562
9526fd29
JM
4563 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4564 return -1;
4565
b22128ef
JM
4566 return p2p_reject(wpa_s->global->p2p, addr);
4567}
4568
4569
4570/* Invite to reinvoke a persistent group */
4571int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4d32c0c4
JM
4572 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4573 int ht40)
b22128ef
JM
4574{
4575 enum p2p_invite_role role;
79879f4a
DG
4576 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
4577 int force_freq = 0, pref_freq = 0, oper_freq = 0;
b22128ef 4578
4d32c0c4
JM
4579 wpa_s->p2p_persistent_go_freq = freq;
4580 wpa_s->p2p_go_ht40 = !!ht40;
b22128ef
JM
4581 if (ssid->mode == WPAS_MODE_P2P_GO) {
4582 role = P2P_INVITE_ROLE_GO;
4583 if (peer_addr == NULL) {
4584 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4585 "address in invitation command");
4586 return -1;
4587 }
4588 if (wpas_p2p_create_iface(wpa_s)) {
4589 if (wpas_p2p_add_group_interface(wpa_s,
4590 WPA_IF_P2P_GO) < 0) {
4591 wpa_printf(MSG_ERROR, "P2P: Failed to "
4592 "allocate a new interface for the "
4593 "group");
4594 return -1;
4595 }
4596 bssid = wpa_s->pending_interface_addr;
4597 } else
4598 bssid = wpa_s->own_addr;
4599 } else {
4600 role = P2P_INVITE_ROLE_CLIENT;
4601 peer_addr = ssid->bssid;
4602 }
4603 wpa_s->pending_invite_ssid_id = ssid->id;
4604
79879f4a
DG
4605 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4606 wpa_s->assoc_freq) {
4607 oper_freq = wpa_s->assoc_freq;
4608 if (bssid == NULL)
4609 bssid = bssid_buf;
4610 } else {
4611 oper_freq = wpa_drv_shared_freq(wpa_s);
4612 if (oper_freq < 0)
4613 oper_freq = 0;
4614 }
4615
4616 if (freq > 0) {
4617 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4618 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4619 "(%u MHz) is not supported for P2P uses",
4620 freq);
4621 return -3;
4622 }
4623
4624 if (oper_freq > 0 && freq != oper_freq &&
4625 !(wpa_s->drv_flags &
4626 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4627 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4628 "on %u MHz while connected on another "
4629 "channel (%u MHz)", freq, oper_freq);
4630 return -2;
4631 }
4632 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4633 "requested channel (%u MHz)", freq);
4634 force_freq = freq;
4635 } else if (oper_freq > 0 &&
4636 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4637 if (!(wpa_s->drv_flags &
4638 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4639 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4640 "while connected on non-P2P supported "
4641 "channel (%u MHz)", oper_freq);
4642 return -2;
4643 }
4644 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4645 "(%u MHz) not available for P2P - try to use "
4646 "another channel", oper_freq);
4647 force_freq = 0;
4648 } else if (oper_freq > 0 &&
4649 (wpa_s->drv_flags &
4650 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4651 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
4652 "are already using (%u MHz) on another interface",
4653 oper_freq);
4654 pref_freq = oper_freq;
4655 } else if (oper_freq > 0) {
4656 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4657 "channel we are already using (%u MHz) on another "
4658 "interface", oper_freq);
4659 force_freq = oper_freq;
4660 }
4661
3ac17eba
JM
4662 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4663 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4664 ssid->ssid, ssid->ssid_len,
4665 go_dev_addr, 1);
4666
9526fd29
JM
4667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4668 return -1;
4669
b22128ef 4670 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
79879f4a
DG
4671 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
4672 1, pref_freq);
b22128ef
JM
4673}
4674
4675
4676/* Invite to join an active group */
4677int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4678 const u8 *peer_addr, const u8 *go_dev_addr)
4679{
4680 struct wpa_global *global = wpa_s->global;
4681 enum p2p_invite_role role;
79879f4a 4682 u8 *bssid = NULL, bssid_buf[ETH_ALEN];
b22128ef 4683 struct wpa_ssid *ssid;
c427ac92 4684 int persistent;
79879f4a 4685 int force_freq = 0, oper_freq = 0, pref_freq = 0;
b22128ef 4686
4d32c0c4
JM
4687 wpa_s->p2p_persistent_go_freq = 0;
4688 wpa_s->p2p_go_ht40 = 0;
4689
b22128ef
JM
4690 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4691 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4692 break;
4693 }
4694 if (wpa_s == NULL) {
4695 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4696 return -1;
4697 }
4698
4699 ssid = wpa_s->current_ssid;
4700 if (ssid == NULL) {
4701 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4702 "invitation");
4703 return -1;
4704 }
4705
c427ac92
JM
4706 persistent = ssid->p2p_persistent_group &&
4707 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4708 ssid->ssid, ssid->ssid_len);
4709
b22128ef
JM
4710 if (ssid->mode == WPAS_MODE_P2P_GO) {
4711 role = P2P_INVITE_ROLE_ACTIVE_GO;
4712 bssid = wpa_s->own_addr;
4713 if (go_dev_addr == NULL)
d7e70476 4714 go_dev_addr = wpa_s->global->p2p_dev_addr;
b22128ef
JM
4715 } else {
4716 role = P2P_INVITE_ROLE_CLIENT;
4717 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4718 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4719 "invite to current group");
4720 return -1;
4721 }
4722 bssid = wpa_s->bssid;
4723 if (go_dev_addr == NULL &&
4724 !is_zero_ether_addr(wpa_s->go_dev_addr))
4725 go_dev_addr = wpa_s->go_dev_addr;
4726 }
bb79dc72 4727 wpa_s->parent->pending_invite_ssid_id = -1;
b22128ef 4728
3ac17eba
JM
4729 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4730 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4731 ssid->ssid, ssid->ssid_len,
c427ac92 4732 go_dev_addr, persistent);
3ac17eba 4733
9526fd29
JM
4734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4735 return -1;
4736
79879f4a
DG
4737 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid_buf) == 0 &&
4738 wpa_s->assoc_freq) {
4739 oper_freq = wpa_s->assoc_freq;
4740 if (bssid == NULL)
4741 bssid = bssid_buf;
4742 } else {
4743 oper_freq = wpa_drv_shared_freq(wpa_s);
4744 if (oper_freq < 0)
4745 oper_freq = 0;
4746 }
4747
4748 if (oper_freq > 0 &&
4749 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4750 if (!(wpa_s->drv_flags &
4751 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4752 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4753 "while connected on non-P2P supported "
4754 "channel (%u MHz)", oper_freq);
4755 return -2;
4756 }
4757 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4758 "(%u MHz) not available for P2P - try to use "
4759 "another channel", oper_freq);
4760 force_freq = 0;
4761 } else if (oper_freq > 0 &&
4762 (wpa_s->drv_flags &
4763 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4764 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
4765 "are already using (%u MHz) on another interface",
4766 oper_freq);
4767 pref_freq = oper_freq;
4768 } else if (oper_freq > 0) {
4769 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4770 "channel we are already using (%u MHz) on another "
4771 "interface", oper_freq);
4772 force_freq = oper_freq;
4773 }
4774
b22128ef 4775 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
79879f4a
DG
4776 ssid->ssid, ssid->ssid_len, force_freq,
4777 go_dev_addr, persistent, pref_freq);
b22128ef
JM
4778}
4779
4780
4781void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4782{
4783 struct wpa_ssid *ssid = wpa_s->current_ssid;
4784 const char *ssid_txt;
4785 u8 go_dev_addr[ETH_ALEN];
4b6baa2f 4786 int network_id = -1;
b22128ef 4787 int persistent;
b49d6ccb 4788 int freq;
b22128ef 4789
73ccd083
JM
4790 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4791 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4792 wpa_s->parent, NULL);
4793 }
361cdf34 4794
b22128ef 4795 if (!wpa_s->show_group_started || !ssid)
99fcd404 4796 goto done;
b22128ef
JM
4797
4798 wpa_s->show_group_started = 0;
4799
4800 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4801 os_memset(go_dev_addr, 0, ETH_ALEN);
4802 if (ssid->bssid_set)
4803 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4804 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4805 ssid->ssid_len);
4806 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4807
a0a9f3b0
JM
4808 if (wpa_s->global->p2p_group_formation == wpa_s)
4809 wpa_s->global->p2p_group_formation = NULL;
4810
b49d6ccb
JJ
4811 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4812 (int) wpa_s->assoc_freq;
b22128ef
JM
4813 if (ssid->passphrase == NULL && ssid->psk_set) {
4814 char psk[65];
4815 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4816 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4817 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4818 MACSTR "%s",
b49d6ccb 4819 wpa_s->ifname, ssid_txt, freq, psk,
c481048f 4820 MAC2STR(go_dev_addr),
b22128ef
JM
4821 persistent ? " [PERSISTENT]" : "");
4822 } else {
4823 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
c481048f
AC
4824 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4825 "go_dev_addr=" MACSTR "%s",
b49d6ccb 4826 wpa_s->ifname, ssid_txt, freq,
b22128ef
JM
4827 ssid->passphrase ? ssid->passphrase : "",
4828 MAC2STR(go_dev_addr),
4829 persistent ? " [PERSISTENT]" : "");
4830 }
4831
4832 if (persistent)
4b6baa2f
JMB
4833 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4834 ssid, go_dev_addr);
4835 if (network_id < 0)
4836 network_id = ssid->id;
4837 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
99fcd404
JM
4838
4839done:
e665ca9a 4840 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
99fcd404 4841 wpa_s->global->p2p != NULL) {
e665ca9a 4842 wpa_s->global->p2p_cb_on_scan_complete = 0;
99fcd404
JM
4843 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4844 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4845 "continued after successful connection");
4846 p2p_increase_search_delay(
4847 wpa_s->global->p2p,
4848 wpas_p2p_search_delay(wpa_s));
4849 }
4850 }
b22128ef
JM
4851}
4852
4853
4854int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4855 u32 interval1, u32 duration2, u32 interval2)
4856{
3ac17eba
JM
4857 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4858 return -1;
9526fd29
JM
4859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4860 return -1;
3ac17eba 4861
b22128ef
JM
4862 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4863 wpa_s->current_ssid == NULL ||
4864 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4865 return -1;
4866
4867 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4868 wpa_s->own_addr, wpa_s->assoc_freq,
4869 duration1, interval1, duration2, interval2);
4870}
4871
4872
4873int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4874 unsigned int interval)
4875{
3ac17eba
JM
4876 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4877 return -1;
4878
9526fd29
JM
4879 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4880 return -1;
4881
b22128ef
JM
4882 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4883}
4884
4885
c8106615
JM
4886static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4887{
5fbddfdc
JM
4888 if (wpa_s->current_ssid == NULL) {
4889 /*
c7deed74 4890 * current_ssid can be cleared when P2P client interface gets
5fbddfdc
JM
4891 * disconnected, so assume this interface was used as P2P
4892 * client.
4893 */
4894 return 1;
4895 }
4896 return wpa_s->current_ssid->p2p_group &&
c8106615
JM
4897 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4898}
4899
4900
3071e181
JM
4901static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4902{
4903 struct wpa_supplicant *wpa_s = eloop_ctx;
4904
c8106615 4905 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
3071e181
JM
4906 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4907 "disabled");
4908 return;
4909 }
4910
4911 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4912 "group");
8dba4aef 4913 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
3071e181
JM
4914}
4915
4916
4917static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4918{
5f482d55 4919 int timeout;
c8106615 4920
dddc7045
JM
4921 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4922 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4923
c8106615 4924 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3071e181
JM
4925 return;
4926
c8106615
JM
4927 timeout = wpa_s->conf->p2p_group_idle;
4928 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4929 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4930 timeout = P2P_MAX_CLIENT_IDLE;
4931
4932 if (timeout == 0)
3071e181
JM
4933 return;
4934
5f482d55
JM
4935 if (timeout < 0) {
4936 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4937 timeout = 0; /* special client mode no-timeout */
4938 else
4939 return;
4940 }
4941
8c472816
JM
4942 if (wpa_s->p2p_in_provisioning) {
4943 /*
4944 * Use the normal group formation timeout during the
4945 * provisioning phase to avoid terminating this process too
4946 * early due to group idle timeout.
4947 */
4948 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4949 "during provisioning");
4950 return;
4951 }
4952
361cdf34
JM
4953 if (wpa_s->show_group_started) {
4954 /*
4955 * Use the normal group formation timeout between the end of
4956 * the provisioning phase and completion of 4-way handshake to
4957 * avoid terminating this process too early due to group idle
4958 * timeout.
4959 */
4960 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4961 "while waiting for initial 4-way handshake to "
4962 "complete");
4963 return;
4964 }
4965
3071e181 4966 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
c8106615
JM
4967 timeout);
4968 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4969 wpa_s, NULL);
3071e181
JM
4970}
4971
4972
0aadd568
JM
4973/* Returns 1 if the interface was removed */
4974int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4975 u16 reason_code, const u8 *ie, size_t ie_len,
4976 int locally_generated)
b22128ef 4977{
9526fd29 4978 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
0aadd568 4979 return 0;
3ac17eba 4980 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
0aadd568 4981 return 0;
b22128ef 4982
3fc14102
JM
4983 if (!locally_generated)
4984 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4985 ie_len);
4986
4987 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4988 wpa_s->current_ssid &&
4989 wpa_s->current_ssid->p2p_group &&
4990 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4991 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4992 "session is ending");
0aadd568
JM
4993 if (wpas_p2p_group_delete(wpa_s,
4994 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4995 > 0)
4996 return 1;
3fc14102 4997 }
0aadd568
JM
4998
4999 return 0;
b22128ef
JM
5000}
5001
5002
5003void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3fc14102
JM
5004 u16 reason_code, const u8 *ie, size_t ie_len,
5005 int locally_generated)
b22128ef 5006{
9526fd29 5007 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
b22128ef 5008 return;
3ac17eba
JM
5009 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5010 return;
b22128ef 5011
3fc14102
JM
5012 if (!locally_generated)
5013 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5014 ie_len);
b22128ef
JM
5015}
5016
5017
5018void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5019{
5020 struct p2p_data *p2p = wpa_s->global->p2p;
5021
5022 if (p2p == NULL)
5023 return;
5024
4c010834
KL
5025 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5026 return;
5027
b22128ef
JM
5028 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5029 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5030
2f646b6e
JB
5031 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5032 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
b22128ef 5033
b6e01800
JM
5034 if (wpa_s->wps &&
5035 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5036 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5037
5038 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5039 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5040
5041 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5042 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5043 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5044 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5045 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5046 }
5047
2f646b6e
JB
5048 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5049 p2p_set_sec_dev_types(p2p,
5050 (void *) wpa_s->conf->sec_device_type,
5051 wpa_s->conf->num_sec_device_types);
b22128ef 5052
f95cac27
JMB
5053 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5054 int i;
5055 p2p_remove_wps_vendor_extensions(p2p);
5056 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5057 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5058 continue;
5059 p2p_add_wps_vendor_extension(
5060 p2p, wpa_s->conf->wps_vendor_ext[i]);
5061 }
5062 }
5063
b22128ef
JM
5064 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5065 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5066 char country[3];
5067 country[0] = wpa_s->conf->country[0];
5068 country[1] = wpa_s->conf->country[1];
5069 country[2] = 0x04;
5070 p2p_set_country(p2p, country);
5071 }
5072
5073 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5074 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5075 wpa_s->conf->p2p_ssid_postfix ?
5076 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5077 0);
5078 }
0f66abd2
SS
5079
5080 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5081 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
2463ba70
JS
5082
5083 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5084 u8 reg_class, channel;
5085 int ret;
5086 unsigned int r;
5087 if (wpa_s->conf->p2p_listen_reg_class &&
5088 wpa_s->conf->p2p_listen_channel) {
5089 reg_class = wpa_s->conf->p2p_listen_reg_class;
5090 channel = wpa_s->conf->p2p_listen_channel;
5091 } else {
5092 reg_class = 81;
5093 /*
5094 * Pick one of the social channels randomly as the
5095 * listen channel.
5096 */
5097 os_get_random((u8 *) &r, sizeof(r));
5098 channel = 1 + (r % 3) * 5;
5099 }
5100 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5101 if (ret)
5102 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5103 "failed: %d", ret);
5104 }
5105 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5106 u8 op_reg_class, op_channel, cfg_op_channel;
5107 int ret = 0;
5108 unsigned int r;
5109 if (wpa_s->conf->p2p_oper_reg_class &&
5110 wpa_s->conf->p2p_oper_channel) {
5111 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5112 op_channel = wpa_s->conf->p2p_oper_channel;
5113 cfg_op_channel = 1;
5114 } else {
5115 op_reg_class = 81;
5116 /*
5117 * Use random operation channel from (1, 6, 11)
5118 *if no other preference is indicated.
5119 */
5120 os_get_random((u8 *) &r, sizeof(r));
5121 op_channel = 1 + (r % 3) * 5;
5122 cfg_op_channel = 0;
5123 }
5124 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5125 cfg_op_channel);
5126 if (ret)
5127 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5128 "failed: %d", ret);
5129 }
21d996f7
JM
5130
5131 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5132 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5133 wpa_s->conf->p2p_pref_chan) < 0) {
5134 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5135 "update failed");
5136 }
5137 }
b22128ef 5138}
aefb53bd
JM
5139
5140
5141int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5142 int duration)
5143{
5144 if (!wpa_s->ap_iface)
5145 return -1;
5146 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5147 duration);
5148}
72044390
JM
5149
5150
5151int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5152{
9526fd29 5153 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
72044390
JM
5154 return -1;
5155 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5156 return -1;
5157
5158 wpa_s->global->cross_connection = enabled;
5159 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5160
5161 if (!enabled) {
5162 struct wpa_supplicant *iface;
5163
5164 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5165 {
5166 if (iface->cross_connect_enabled == 0)
5167 continue;
5168
5169 iface->cross_connect_enabled = 0;
5170 iface->cross_connect_in_use = 0;
5171 wpa_msg(iface->parent, MSG_INFO,
5172 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5173 iface->ifname, iface->cross_connect_uplink);
5174 }
5175 }
5176
5177 return 0;
5178}
5179
5180
5181static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5182{
5183 struct wpa_supplicant *iface;
5184
5185 if (!uplink->global->cross_connection)
5186 return;
5187
5188 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5189 if (!iface->cross_connect_enabled)
5190 continue;
5191 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5192 0)
5193 continue;
5194 if (iface->ap_iface == NULL)
5195 continue;
5196 if (iface->cross_connect_in_use)
5197 continue;
5198
5199 iface->cross_connect_in_use = 1;
5200 wpa_msg(iface->parent, MSG_INFO,
5201 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5202 iface->ifname, iface->cross_connect_uplink);
5203 }
5204}
5205
5206
5207static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5208{
5209 struct wpa_supplicant *iface;
5210
5211 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5212 if (!iface->cross_connect_enabled)
5213 continue;
5214 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5215 0)
5216 continue;
5217 if (!iface->cross_connect_in_use)
5218 continue;
5219
5220 wpa_msg(iface->parent, MSG_INFO,
5221 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5222 iface->ifname, iface->cross_connect_uplink);
5223 iface->cross_connect_in_use = 0;
5224 }
5225}
5226
5227
5228void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5229{
5230 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5231 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5232 wpa_s->cross_connect_disallowed)
5233 wpas_p2p_disable_cross_connect(wpa_s);
5234 else
5235 wpas_p2p_enable_cross_connect(wpa_s);
dddc7045
JM
5236 if (!wpa_s->ap_iface &&
5237 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5238 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
72044390
JM
5239}
5240
5241
5242void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5243{
5244 wpas_p2p_disable_cross_connect(wpa_s);
4c2c6751
JM
5245 if (!wpa_s->ap_iface &&
5246 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5247 wpa_s, NULL))
3071e181 5248 wpas_p2p_set_group_idle_timeout(wpa_s);
72044390
JM
5249}
5250
5251
5252static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5253{
5254 struct wpa_supplicant *iface;
5255
5256 if (!wpa_s->global->cross_connection)
5257 return;
5258
5259 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5260 if (iface == wpa_s)
5261 continue;
5262 if (iface->drv_flags &
5263 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5264 continue;
5265 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5266 continue;
5267
5268 wpa_s->cross_connect_enabled = 1;
5269 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5270 sizeof(wpa_s->cross_connect_uplink));
5271 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5272 "%s to %s whenever uplink is available",
5273 wpa_s->ifname, wpa_s->cross_connect_uplink);
5274
5275 if (iface->ap_iface || iface->current_ssid == NULL ||
5276 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5277 iface->cross_connect_disallowed ||
5278 iface->wpa_state != WPA_COMPLETED)
5279 break;
5280
5281 wpa_s->cross_connect_in_use = 1;
5282 wpa_msg(wpa_s->parent, MSG_INFO,
5283 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5284 wpa_s->ifname, wpa_s->cross_connect_uplink);
5285 break;
5286 }
5287}
b73bf0a7
JM
5288
5289
5290int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5291{
5292 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5293 !wpa_s->p2p_in_provisioning)
5294 return 0; /* not P2P client operation */
5295
5296 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5297 "session overlap");
148bb37f
JM
5298 if (wpa_s != wpa_s->parent)
5299 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
0a14e3ec
SS
5300
5301 if (wpa_s->global->p2p)
5302 p2p_group_formation_failed(wpa_s->global->p2p);
5303
5304 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5305 wpa_s->parent, NULL);
5306
b73bf0a7
JM
5307 wpas_group_formation_completed(wpa_s, 0);
5308 return 1;
5309}
b5c9da8d
JM
5310
5311
5312void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5313{
5314 struct p2p_channels chan;
5315
5316 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5317 return;
5318
5319 os_memset(&chan, 0, sizeof(chan));
5320 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5321 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5322 "channel list");
5323 return;
5324 }
5325
5326 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5327}
59eba7a2
JM
5328
5329
50178335
JM
5330static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5331 struct wpa_scan_results *scan_res)
5332{
5333 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5334}
5335
5336
59eba7a2
JM
5337int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5338{
5339 struct wpa_global *global = wpa_s->global;
5340 int found = 0;
231bbd03 5341 const u8 *peer;
59eba7a2 5342
9526fd29
JM
5343 if (global->p2p == NULL)
5344 return -1;
5345
59eba7a2
JM
5346 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5347
5348 if (wpa_s->pending_interface_name[0] &&
5349 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5350 found = 1;
5351
231bbd03
SS
5352 peer = p2p_get_go_neg_peer(global->p2p);
5353 if (peer) {
5354 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5355 MACSTR, MAC2STR(peer));
5356 p2p_unauthorize(global->p2p, peer);
75c208b9 5357 found = 1;
231bbd03
SS
5358 }
5359
50178335
JM
5360 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5361 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5362 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5363 found = 1;
5364 }
5365
5366 if (wpa_s->pending_pd_before_join) {
5367 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5368 wpa_s->pending_pd_before_join = 0;
5369 found = 1;
5370 }
5371
59eba7a2
JM
5372 wpas_p2p_stop_find(wpa_s);
5373
5374 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5375 if (wpa_s == global->p2p_group_formation &&
a0a9f3b0
JM
5376 (wpa_s->p2p_in_provisioning ||
5377 wpa_s->parent->pending_interface_type ==
5378 WPA_IF_P2P_CLIENT)) {
59eba7a2
JM
5379 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5380 "formation found - cancelling",
5381 wpa_s->ifname);
5382 found = 1;
45fee6f0
SS
5383 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5384 wpa_s->parent, NULL);
30b80389
JM
5385 if (wpa_s->p2p_in_provisioning) {
5386 wpas_group_formation_completed(wpa_s, 0);
5387 break;
5388 }
8dba4aef
JM
5389 wpas_p2p_group_delete(wpa_s,
5390 P2P_GROUP_REMOVAL_REQUESTED);
59eba7a2
JM
5391 break;
5392 }
5393 }
5394
5395 if (!found) {
5396 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5397 return -1;
5398 }
5399
5400 return 0;
5401}
c973f386
JM
5402
5403
5404void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5405{
5406 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5407 return;
5408
5409 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5410 "being available anymore");
8dba4aef 5411 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
c973f386 5412}
7cfc4ac3
AGS
5413
5414
5415void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5416 int freq_24, int freq_5, int freq_overall)
5417{
5418 struct p2p_data *p2p = wpa_s->global->p2p;
5419 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5420 return;
5421 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5422}
9d562b79
SS
5423
5424
5425int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5426{
5427 u8 peer[ETH_ALEN];
5428 struct p2p_data *p2p = wpa_s->global->p2p;
5429
5430 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5431 return -1;
5432
5433 if (hwaddr_aton(addr, peer))
5434 return -1;
5435
5436 return p2p_unauthorize(p2p, peer);
5437}
3103f345
JB
5438
5439
5440/**
5441 * wpas_p2p_disconnect - Disconnect from a P2P Group
5442 * @wpa_s: Pointer to wpa_supplicant data
5443 * Returns: 0 on success, -1 on failure
5444 *
5445 * This can be used to disconnect from a group in which the local end is a P2P
5446 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5447 * virtual network interface was created for this group, that interface will be
5448 * removed. Otherwise, only the configured P2P group network will be removed
5449 * from the interface.
5450 */
5451int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5452{
5453
5454 if (wpa_s == NULL)
5455 return -1;
5456
0aadd568
JM
5457 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5458 -1 : 0;
3103f345 5459}
303f60d3
JM
5460
5461
5462int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5463{
ec01d5f6
JM
5464 int ret;
5465
303f60d3
JM
5466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5467 return 0;
5468
ec01d5f6
JM
5469 ret = p2p_in_progress(wpa_s->global->p2p);
5470 if (ret == 0) {
5471 /*
5472 * Check whether there is an ongoing WPS provisioning step (or
5473 * other parts of group formation) on another interface since
5474 * p2p_in_progress() does not report this to avoid issues for
5475 * scans during such provisioning step.
5476 */
5477 if (wpa_s->global->p2p_group_formation &&
5478 wpa_s->global->p2p_group_formation != wpa_s) {
5479 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5480 "in group formation",
5481 wpa_s->global->p2p_group_formation->ifname);
5482 ret = 1;
5483 }
5484 }
5485
5486 return ret;
303f60d3 5487}
502618f7
JM
5488
5489
5490void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5491 struct wpa_ssid *ssid)
502618f7
JM
5492{
5493 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5494 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5495 wpa_s->parent, NULL) > 0) {
eb6f8c2b
AC
5496 /**
5497 * Remove the network by scheduling the group formation
5498 * timeout to happen immediately. The teardown code
5499 * needs to be scheduled to run asynch later so that we
5500 * don't delete data from under ourselves unexpectedly.
5501 * Calling wpas_p2p_group_formation_timeout directly
5502 * causes a series of crashes in WPS failure scenarios.
5503 */
502618f7
JM
5504 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5505 "P2P group network getting removed");
eb6f8c2b
AC
5506 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5507 wpa_s->parent, NULL);
502618f7
JM
5508 }
5509}
87f841a1
JM
5510
5511
5512struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
c427ac92
JM
5513 const u8 *addr, const u8 *ssid,
5514 size_t ssid_len)
87f841a1
JM
5515{
5516 struct wpa_ssid *s;
fbdcfd57 5517 size_t i;
87f841a1
JM
5518
5519 for (s = wpa_s->conf->ssid; s; s = s->next) {
fbdcfd57
JM
5520 if (s->disabled != 2)
5521 continue;
c427ac92
JM
5522 if (ssid &&
5523 (ssid_len != s->ssid_len ||
5524 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5525 continue;
fbdcfd57
JM
5526 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5527 return s; /* peer is GO in the persistent group */
5528 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5529 continue;
5530 for (i = 0; i < s->num_p2p_clients; i++) {
5531 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5532 addr, ETH_ALEN) == 0)
5533 return s; /* peer is P2P client in persistent
5534 * group */
5535 }
87f841a1
JM
5536 }
5537
5538 return NULL;
5539}
fbdcfd57
JM
5540
5541
5542void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5543 const u8 *addr)
5544{
5545 if (addr == NULL)
5546 return;
5547 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5548}
aa9bb764
JM
5549
5550
5551static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5552 int group_added)
5553{
5554 struct wpa_supplicant *group = wpa_s;
5555 if (wpa_s->global->p2p_group_formation)
5556 group = wpa_s->global->p2p_group_formation;
5557 wpa_s = wpa_s->parent;
b80eb89d 5558 offchannel_send_action_done(wpa_s);
aa9bb764 5559 if (group_added)
8dba4aef 5560 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
aa9bb764
JM
5561 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5562 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5563 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5564 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
3bc462cb 5565 wpa_s->p2p_persistent_id,
e2308e4b
RM
5566 wpa_s->p2p_pd_before_go_neg,
5567 wpa_s->p2p_go_ht40);
aa9bb764
JM
5568}
5569
5570
5571int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5572{
5573 if (!wpa_s->p2p_fallback_to_go_neg ||
5574 wpa_s->p2p_in_provisioning <= 5)
5575 return 0;
5576
b80eb89d
JM
5577 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5578 return 0; /* peer operating as a GO */
5579
aa9bb764
JM
5580 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5581 "fallback to GO Negotiation");
5582 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5583
5584 return 1;
5585}
05a77b3b
JM
5586
5587
5588unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5589{
5590 const char *rn, *rn2;
5591 struct wpa_supplicant *ifs;
5592
5593 if (wpa_s->wpa_state > WPA_SCANNING) {
5594 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5595 "concurrent operation",
5596 P2P_CONCURRENT_SEARCH_DELAY);
5597 return P2P_CONCURRENT_SEARCH_DELAY;
5598 }
5599
5600 if (!wpa_s->driver->get_radio_name)
5601 return 0;
5602 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5603 if (rn == NULL || rn[0] == '\0')
5604 return 0;
5605
5606 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5607 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5608 continue;
5609
5610 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5611 if (!rn2 || os_strcmp(rn, rn2) != 0)
5612 continue;
5613 if (ifs->wpa_state > WPA_SCANNING) {
5614 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5615 "delay due to concurrent operation on "
5616 "interface %s",
5617 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5618 return P2P_CONCURRENT_SEARCH_DELAY;
5619 }
5620 }
5621
5622 return 0;
5623}