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