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