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