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