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