3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
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"
26 #include "config_ssid.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
37 * How many times to try to scan to find the GO before giving up on join
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
42 #ifndef P2P_MAX_CLIENT_IDLE
44 * How many seconds to try to reconnect to the GO when connection in P2P client
47 #define P2P_MAX_CLIENT_IDLE 10
48 #endif /* P2P_MAX_CLIENT_IDLE */
50 #ifndef P2P_MAX_INITIAL_CONN_WAIT
52 * How many seconds to wait for initial 4-way handshake to get completed after
53 * WPS provisioning step.
55 #define P2P_MAX_INITIAL_CONN_WAIT 10
56 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
59 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
60 static struct wpa_supplicant *
61 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
63 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
64 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
65 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
66 const u8 *dev_addr, enum p2p_wps_method wps_method,
68 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
70 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
71 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
72 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
73 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
76 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
77 struct wpa_scan_results *scan_res)
81 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
84 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
87 for (i = 0; i < scan_res->num; i++) {
88 struct wpa_scan_res *bss = scan_res->res[i];
89 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
90 bss->freq, bss->level,
91 (const u8 *) (bss + 1),
96 p2p_scan_res_handled(wpa_s->global->p2p);
100 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
101 unsigned int num_req_dev_types,
102 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
104 struct wpa_supplicant *wpa_s = ctx;
105 struct wpa_driver_scan_params params;
107 struct wpabuf *wps_ie, *ies;
108 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
112 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
115 os_memset(¶ms, 0, sizeof(params));
117 /* P2P Wildcard SSID */
118 params.num_ssids = 1;
119 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
120 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
122 wpa_s->wps->dev.p2p = 1;
123 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
124 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
125 num_req_dev_types, req_dev_types);
129 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
130 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
135 wpabuf_put_buf(ies, wps_ie);
138 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
140 params.p2p_probe = 1;
141 params.extra_ies = wpabuf_head(ies);
142 params.extra_ies_len = wpabuf_len(ies);
145 case P2P_SCAN_SOCIAL:
146 params.freqs = social_channels;
150 case P2P_SCAN_SPECIFIC:
151 social_channels[0] = freq;
152 social_channels[1] = 0;
153 params.freqs = social_channels;
155 case P2P_SCAN_SOCIAL_PLUS_ONE:
156 social_channels[3] = freq;
157 params.freqs = social_channels;
161 was_in_p2p_scan = wpa_s->scan_res_handler == wpas_p2p_scan_res_handler;
162 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
163 ret = wpa_drv_scan(wpa_s, ¶ms);
168 wpa_s->scan_res_handler = NULL;
169 if (wpa_s->scanning || was_in_p2p_scan) {
170 wpa_s->p2p_cb_on_scan_complete = 1;
179 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
181 switch (p2p_group_interface) {
182 case P2P_GROUP_INTERFACE_PENDING:
183 return WPA_IF_P2P_GROUP;
184 case P2P_GROUP_INTERFACE_GO:
185 return WPA_IF_P2P_GO;
186 case P2P_GROUP_INTERFACE_CLIENT:
187 return WPA_IF_P2P_CLIENT;
190 return WPA_IF_P2P_GROUP;
194 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
196 size_t ssid_len, int *go)
200 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
201 for (s = wpa_s->conf->ssid; s; s = s->next) {
202 if (s->disabled != 0 || !s->p2p_group ||
203 s->ssid_len != ssid_len ||
204 os_memcmp(ssid, s->ssid, ssid_len) != 0)
206 if (s->mode == WPAS_MODE_P2P_GO &&
207 s != wpa_s->current_ssid)
210 *go = s->mode == WPAS_MODE_P2P_GO;
219 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
221 struct wpa_ssid *ssid;
225 ssid = wpa_s->current_ssid;
228 * The current SSID was not known, but there may still be a
229 * pending P2P group interface waiting for provisioning.
231 ssid = wpa_s->conf->ssid;
233 if (ssid->p2p_group &&
234 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
235 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
240 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
242 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
243 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
244 wpa_s->reassociate = 0;
245 wpa_s->disconnected = 1;
246 wpa_supplicant_deauthenticate(wpa_s,
247 WLAN_REASON_DEAUTH_LEAVING);
251 if (wpa_s->cross_connect_in_use) {
252 wpa_s->cross_connect_in_use = 0;
253 wpa_msg(wpa_s->parent, MSG_INFO,
254 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
255 wpa_s->ifname, wpa_s->cross_connect_uplink);
257 switch (wpa_s->removal_reason) {
258 case P2P_GROUP_REMOVAL_REQUESTED:
259 reason = " reason=REQUESTED";
261 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
262 reason = " reason=IDLE";
264 case P2P_GROUP_REMOVAL_UNAVAILABLE:
265 reason = " reason=UNAVAILABLE";
267 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
268 reason = " reason=GO_ENDING_SESSION";
274 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
275 wpa_s->ifname, gtype, reason);
277 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
278 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
281 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
283 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
284 struct wpa_global *global;
286 enum wpa_driver_if_type type;
287 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
289 global = wpa_s->global;
290 ifname = os_strdup(wpa_s->ifname);
291 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
292 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
293 wpa_s = global->ifaces;
295 wpa_drv_if_remove(wpa_s, type, ifname);
300 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
301 if (ssid && (ssid->p2p_group ||
302 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
303 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
305 if (ssid == wpa_s->current_ssid) {
306 wpa_sm_set_config(wpa_s->wpa, NULL);
307 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
308 wpa_s->current_ssid = NULL;
311 * Networks objects created during any P2P activities are not
312 * exposed out as they might/will confuse certain non-P2P aware
313 * applications since these network objects won't behave like
316 * Likewise, we don't send out network removed signals for such
319 wpa_config_remove_network(wpa_s->conf, id);
320 wpa_supplicant_clear_status(wpa_s);
321 wpa_supplicant_cancel_sched_scan(wpa_s);
323 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
327 wpa_supplicant_ap_deinit(wpa_s);
329 wpa_drv_deinit_p2p_cli(wpa_s);
333 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
335 const u8 *ssid, size_t ssid_len)
343 if (wpa_s->go_params)
344 bssid = wpa_s->go_params->peer_interface_addr;
346 bssid = wpa_s->bssid;
348 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
350 u8 iface_addr[ETH_ALEN];
351 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
353 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
356 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
357 "group is persistent - BSS " MACSTR " not found",
362 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
364 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
365 "group is persistent - BSS " MACSTR
366 " did not include P2P IE", MAC2STR(bssid));
367 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
368 (u8 *) (bss + 1), bss->ie_len);
369 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
370 ((u8 *) bss + 1) + bss->ie_len,
375 group_capab = p2p_get_group_capab(p2p);
376 addr = p2p_get_go_dev_addr(p2p);
377 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
378 "group_capab=0x%x", group_capab);
380 os_memcpy(go_dev_addr, addr, ETH_ALEN);
381 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
384 os_memset(go_dev_addr, 0, ETH_ALEN);
387 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
388 "go_dev_addr=" MACSTR,
389 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
391 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
395 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
396 struct wpa_ssid *ssid,
397 const u8 *go_dev_addr)
402 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
403 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
404 for (s = wpa_s->conf->ssid; s; s = s->next) {
405 if (s->disabled == 2 &&
406 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
407 s->ssid_len == ssid->ssid_len &&
408 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
413 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
415 if (ssid->passphrase && !s->passphrase)
417 else if (ssid->passphrase && s->passphrase &&
418 os_strcmp(ssid->passphrase, s->passphrase) != 0)
421 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
424 s = wpa_config_add_network(wpa_s->conf);
429 * Instead of network_added we emit persistent_group_added
430 * notification. Also to keep the defense checks in
431 * persistent_group obj registration method, we set the
432 * relevant flags in s to designate it as a persistent group.
435 s->p2p_persistent_group = 1;
436 wpas_notify_persistent_group_added(wpa_s, s);
437 wpa_config_set_network_defaults(s);
441 s->p2p_persistent_group = 1;
444 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
445 s->mode = ssid->mode;
446 s->auth_alg = WPA_AUTH_ALG_OPEN;
447 s->key_mgmt = WPA_KEY_MGMT_PSK;
448 s->proto = WPA_PROTO_RSN;
449 s->pairwise_cipher = WPA_CIPHER_CCMP;
451 if (ssid->passphrase) {
452 os_free(s->passphrase);
453 s->passphrase = os_strdup(ssid->passphrase);
457 os_memcpy(s->psk, ssid->psk, 32);
459 if (s->passphrase && !s->psk_set)
460 wpa_config_update_psk(s);
461 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
463 s->ssid = os_malloc(ssid->ssid_len);
466 s->ssid_len = ssid->ssid_len;
467 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
470 #ifndef CONFIG_NO_CONFIG_WRITE
471 if (changed && wpa_s->conf->update_config &&
472 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
473 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
475 #endif /* CONFIG_NO_CONFIG_WRITE */
481 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
484 struct wpa_ssid *ssid, *s;
488 ssid = wpa_s->current_ssid;
489 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
490 !ssid->p2p_persistent_group)
493 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
494 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
497 if (s->ssid_len == ssid->ssid_len &&
498 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
505 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
506 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
508 return; /* already in list */
511 n = os_realloc(s->p2p_client_list,
512 (s->num_p2p_clients + 1) * ETH_ALEN);
515 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
516 s->p2p_client_list = n;
517 s->num_p2p_clients++;
519 #ifndef CONFIG_NO_CONFIG_WRITE
520 if (wpa_s->parent->conf->update_config &&
521 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
522 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
523 #endif /* CONFIG_NO_CONFIG_WRITE */
527 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
530 struct wpa_ssid *ssid;
531 const char *ssid_txt;
534 u8 go_dev_addr[ETH_ALEN];
538 * This callback is likely called for the main interface. Update wpa_s
539 * to use the group interface if a new interface was created for the
542 if (wpa_s->global->p2p_group_formation)
543 wpa_s = wpa_s->global->p2p_group_formation;
544 wpa_s->global->p2p_group_formation = NULL;
545 wpa_s->p2p_in_provisioning = 0;
548 wpa_msg(wpa_s->parent, MSG_INFO,
549 P2P_EVENT_GROUP_FORMATION_FAILURE);
550 wpas_p2p_group_delete(wpa_s);
554 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
556 ssid = wpa_s->current_ssid;
557 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
558 ssid->mode = WPAS_MODE_P2P_GO;
559 p2p_group_notif_formation_done(wpa_s->p2p_group);
560 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
565 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
566 client = ssid->mode == WPAS_MODE_INFRA;
567 if (ssid->mode == WPAS_MODE_P2P_GO) {
568 persistent = ssid->p2p_persistent_group;
569 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
572 persistent = wpas_p2p_persistent_group(wpa_s,
578 client = wpa_s->p2p_group_interface ==
579 P2P_GROUP_INTERFACE_CLIENT;
580 os_memset(go_dev_addr, 0, ETH_ALEN);
583 wpa_s->show_group_started = 0;
586 * Indicate event only after successfully completed 4-way
587 * handshake, i.e., when the interface is ready for data
590 wpa_s->show_group_started = 1;
591 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
593 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
594 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
595 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
597 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
598 MAC2STR(go_dev_addr),
599 persistent ? " [PERSISTENT]" : "");
600 wpas_p2p_cross_connect_setup(wpa_s);
601 wpas_p2p_set_group_idle_timeout(wpa_s);
603 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
604 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
605 "go_dev_addr=" MACSTR "%s",
606 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
607 ssid && ssid->passphrase ? ssid->passphrase : "",
608 MAC2STR(go_dev_addr),
609 persistent ? " [PERSISTENT]" : "");
610 wpas_p2p_cross_connect_setup(wpa_s);
611 wpas_p2p_set_group_idle_timeout(wpa_s);
615 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
617 if (network_id < 0 && ssid)
618 network_id = ssid->id;
620 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
624 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
626 const u8 *dst, const u8 *src,
628 const u8 *data, size_t data_len,
629 enum offchannel_send_action_result
632 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
634 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
636 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
640 case OFFCHANNEL_SEND_ACTION_SUCCESS:
641 res = P2P_SEND_ACTION_SUCCESS;
643 case OFFCHANNEL_SEND_ACTION_NO_ACK:
644 res = P2P_SEND_ACTION_NO_ACK;
646 case OFFCHANNEL_SEND_ACTION_FAILED:
647 res = P2P_SEND_ACTION_FAILED;
651 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
653 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
654 wpa_s->pending_pd_before_join &&
655 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
656 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
657 wpa_s->pending_pd_before_join = 0;
658 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
659 "join-existing-group operation (no ACK for PD "
661 wpas_p2p_join_start(wpa_s);
666 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
667 const u8 *src, const u8 *bssid, const u8 *buf,
668 size_t len, unsigned int wait_time)
670 struct wpa_supplicant *wpa_s = ctx;
671 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
673 wpas_p2p_send_action_tx_status, 1);
677 static void wpas_send_action_done(void *ctx)
679 struct wpa_supplicant *wpa_s = ctx;
680 offchannel_send_action_done(wpa_s);
684 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
685 struct p2p_go_neg_results *params)
687 if (wpa_s->go_params == NULL) {
688 wpa_s->go_params = os_malloc(sizeof(*params));
689 if (wpa_s->go_params == NULL)
692 os_memcpy(wpa_s->go_params, params, sizeof(*params));
697 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
698 struct p2p_go_neg_results *res)
700 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
701 MAC2STR(res->peer_interface_addr));
702 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
703 res->ssid, res->ssid_len);
704 wpa_supplicant_ap_deinit(wpa_s);
705 wpas_copy_go_neg_results(wpa_s, res);
706 if (res->wps_method == WPS_PBC)
707 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
709 u16 dev_pw_id = DEV_PW_DEFAULT;
710 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
711 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
712 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
713 wpa_s->p2p_pin, 1, dev_pw_id);
718 static void p2p_go_configured(void *ctx, void *data)
720 struct wpa_supplicant *wpa_s = ctx;
721 struct p2p_go_neg_results *params = data;
722 struct wpa_ssid *ssid;
725 ssid = wpa_s->current_ssid;
726 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
727 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
728 if (wpa_s->global->p2p_group_formation == wpa_s)
729 wpa_s->global->p2p_group_formation = NULL;
730 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
731 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
732 "go_dev_addr=" MACSTR "%s",
734 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
736 params->passphrase ? params->passphrase : "",
737 MAC2STR(wpa_s->global->p2p_dev_addr),
738 params->persistent_group ? " [PERSISTENT]" : "");
740 if (params->persistent_group)
741 network_id = wpas_p2p_store_persistent_group(
743 wpa_s->global->p2p_dev_addr);
745 network_id = ssid->id;
746 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
747 wpas_p2p_cross_connect_setup(wpa_s);
748 wpas_p2p_set_group_idle_timeout(wpa_s);
752 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
753 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
754 params->peer_interface_addr)) {
755 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
759 if (params->wps_method == WPS_PBC)
760 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
761 params->peer_device_addr);
762 else if (wpa_s->p2p_pin[0])
763 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
764 wpa_s->p2p_pin, NULL, 0);
765 os_free(wpa_s->go_params);
766 wpa_s->go_params = NULL;
770 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
771 struct p2p_go_neg_results *params,
774 struct wpa_ssid *ssid;
776 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
779 ssid = wpa_config_add_network(wpa_s->conf);
783 wpa_s->show_group_started = 0;
785 wpa_config_set_network_defaults(ssid);
788 ssid->p2p_persistent_group = params->persistent_group;
789 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
791 ssid->frequency = params->freq;
792 ssid->ssid = os_zalloc(params->ssid_len + 1);
794 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
795 ssid->ssid_len = params->ssid_len;
797 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
798 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
799 ssid->proto = WPA_PROTO_RSN;
800 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
801 ssid->passphrase = os_strdup(params->passphrase);
803 wpa_s->ap_configured_cb = p2p_go_configured;
804 wpa_s->ap_configured_cb_ctx = wpa_s;
805 wpa_s->ap_configured_cb_data = wpa_s->go_params;
806 wpa_s->connect_without_scan = ssid;
807 wpa_s->reassociate = 1;
808 wpa_s->disconnected = 0;
809 wpa_supplicant_req_scan(wpa_s, 0, 0);
813 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
814 const struct wpa_supplicant *src)
816 struct wpa_config *d;
817 const struct wpa_config *s;
822 #define C(n) if (s->n) d->n = os_strdup(s->n)
831 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
832 os_memcpy(d->sec_device_type, s->sec_device_type,
833 sizeof(d->sec_device_type));
834 d->num_sec_device_types = s->num_sec_device_types;
836 d->p2p_group_idle = s->p2p_group_idle;
837 d->p2p_intra_bss = s->p2p_intra_bss;
838 d->persistent_reconnect = s->persistent_reconnect;
842 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
843 enum wpa_driver_if_type type)
845 char ifname[120], force_ifname[120];
847 if (wpa_s->pending_interface_name[0]) {
848 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
849 "- skip creation of a new one");
850 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
851 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
852 "unknown?! ifname='%s'",
853 wpa_s->pending_interface_name);
859 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
860 wpa_s->p2p_group_idx);
861 if (os_strlen(ifname) >= IFNAMSIZ &&
862 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
863 /* Try to avoid going over the IFNAMSIZ length limit */
864 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
865 wpa_s->p2p_group_idx);
867 force_ifname[0] = '\0';
869 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
871 wpa_s->p2p_group_idx++;
873 wpa_s->pending_interface_type = type;
874 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
875 wpa_s->pending_interface_addr, NULL) < 0) {
876 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
881 if (force_ifname[0]) {
882 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
884 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
885 sizeof(wpa_s->pending_interface_name));
887 os_strlcpy(wpa_s->pending_interface_name, ifname,
888 sizeof(wpa_s->pending_interface_name));
889 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
890 MACSTR, wpa_s->pending_interface_name,
891 MAC2STR(wpa_s->pending_interface_addr));
897 static void wpas_p2p_remove_pending_group_interface(
898 struct wpa_supplicant *wpa_s)
900 if (!wpa_s->pending_interface_name[0] ||
901 is_zero_ether_addr(wpa_s->pending_interface_addr))
902 return; /* No pending virtual interface */
904 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
905 wpa_s->pending_interface_name);
906 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
907 wpa_s->pending_interface_name);
908 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
909 wpa_s->pending_interface_name[0] = '\0';
913 static struct wpa_supplicant *
914 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
916 struct wpa_interface iface;
917 struct wpa_supplicant *group_wpa_s;
919 if (!wpa_s->pending_interface_name[0]) {
920 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
921 if (!wpas_p2p_create_iface(wpa_s))
924 * Something has forced us to remove the pending interface; try
925 * to create a new one and hope for the best that we will get
926 * the same local address.
928 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
929 WPA_IF_P2P_CLIENT) < 0)
933 os_memset(&iface, 0, sizeof(iface));
934 iface.ifname = wpa_s->pending_interface_name;
935 iface.driver = wpa_s->driver->name;
936 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
937 iface.driver_param = wpa_s->conf->driver_param;
938 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
939 if (group_wpa_s == NULL) {
940 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
941 "wpa_supplicant interface");
944 wpa_s->pending_interface_name[0] = '\0';
945 group_wpa_s->parent = wpa_s;
946 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
947 P2P_GROUP_INTERFACE_CLIENT;
948 wpa_s->global->p2p_group_formation = group_wpa_s;
950 wpas_p2p_clone_config(group_wpa_s, wpa_s);
956 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
959 struct wpa_supplicant *wpa_s = eloop_ctx;
960 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
961 if (wpa_s->global->p2p)
962 p2p_group_formation_failed(wpa_s->global->p2p);
963 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
964 wpa_drv_p2p_group_formation_failed(wpa_s);
965 wpas_group_formation_completed(wpa_s, 0);
969 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
971 struct wpa_supplicant *wpa_s = ctx;
973 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
974 wpa_drv_cancel_remain_on_channel(wpa_s);
975 wpa_s->off_channel_freq = 0;
976 wpa_s->roc_waiting_drv_freq = 0;
980 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
982 wpas_notify_p2p_go_neg_completed(wpa_s, res);
983 wpas_p2p_remove_pending_group_interface(wpa_s);
987 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
988 wpas_notify_p2p_go_neg_completed(wpa_s, res);
990 if (wpa_s->create_p2p_iface) {
991 struct wpa_supplicant *group_wpa_s =
992 wpas_p2p_init_group_interface(wpa_s, res->role_go);
993 if (group_wpa_s == NULL) {
994 wpas_p2p_remove_pending_group_interface(wpa_s);
997 if (group_wpa_s != wpa_s) {
998 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
999 sizeof(group_wpa_s->p2p_pin));
1000 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1002 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1003 wpa_s->pending_interface_name[0] = '\0';
1004 group_wpa_s->p2p_in_provisioning = 1;
1007 wpas_start_wps_go(group_wpa_s, res, 1);
1009 wpas_start_wps_enrollee(group_wpa_s, res);
1011 wpa_s->p2p_in_provisioning = 1;
1012 wpa_s->global->p2p_group_formation = wpa_s;
1015 wpas_start_wps_go(wpa_s, res, 1);
1017 wpas_start_wps_enrollee(ctx, res);
1020 wpa_s->p2p_long_listen = 0;
1021 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1023 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1024 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1025 (res->peer_config_timeout % 100) * 10000,
1026 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1030 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1032 struct wpa_supplicant *wpa_s = ctx;
1033 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1034 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1036 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1040 void wpas_dev_found(void *ctx, const u8 *addr,
1041 const struct p2p_peer_info *info,
1044 #ifndef CONFIG_NO_STDOUT_DEBUG
1045 struct wpa_supplicant *wpa_s = ctx;
1046 char devtype[WPS_DEV_TYPE_BUFSIZE];
1048 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1049 " p2p_dev_addr=" MACSTR
1050 " pri_dev_type=%s name='%s' config_methods=0x%x "
1051 "dev_capab=0x%x group_capab=0x%x",
1052 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1053 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1055 info->device_name, info->config_methods,
1056 info->dev_capab, info->group_capab);
1057 #endif /* CONFIG_NO_STDOUT_DEBUG */
1059 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1063 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1065 struct wpa_supplicant *wpa_s = ctx;
1067 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1068 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1070 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1074 static int wpas_start_listen(void *ctx, unsigned int freq,
1075 unsigned int duration,
1076 const struct wpabuf *probe_resp_ie)
1078 struct wpa_supplicant *wpa_s = ctx;
1080 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1082 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1083 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1084 "report received Probe Request frames");
1088 wpa_s->pending_listen_freq = freq;
1089 wpa_s->pending_listen_duration = duration;
1091 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1092 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1093 "to remain on channel (%u MHz) for Listen "
1095 wpa_s->pending_listen_freq = 0;
1098 wpa_s->off_channel_freq = 0;
1099 wpa_s->roc_waiting_drv_freq = freq;
1105 static void wpas_stop_listen(void *ctx)
1107 struct wpa_supplicant *wpa_s = ctx;
1108 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1109 wpa_drv_cancel_remain_on_channel(wpa_s);
1110 wpa_s->off_channel_freq = 0;
1111 wpa_s->roc_waiting_drv_freq = 0;
1113 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1114 wpa_drv_probe_req_report(wpa_s, 0);
1118 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1120 struct wpa_supplicant *wpa_s = ctx;
1121 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1125 static struct p2p_srv_bonjour *
1126 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1127 const struct wpabuf *query)
1129 struct p2p_srv_bonjour *bsrv;
1132 len = wpabuf_len(query);
1133 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1134 struct p2p_srv_bonjour, list) {
1135 if (len == wpabuf_len(bsrv->query) &&
1136 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1144 static struct p2p_srv_upnp *
1145 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1146 const char *service)
1148 struct p2p_srv_upnp *usrv;
1150 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1151 struct p2p_srv_upnp, list) {
1152 if (version == usrv->version &&
1153 os_strcmp(service, usrv->service) == 0)
1160 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1165 if (wpabuf_tailroom(resp) < 5)
1168 /* Length (to be filled) */
1169 len_pos = wpabuf_put(resp, 2);
1170 wpabuf_put_u8(resp, srv_proto);
1171 wpabuf_put_u8(resp, srv_trans_id);
1173 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1174 /* Response Data: empty */
1175 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1179 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1180 struct wpabuf *resp, u8 srv_trans_id)
1182 struct p2p_srv_bonjour *bsrv;
1185 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1187 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1188 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1192 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1193 struct p2p_srv_bonjour, list) {
1194 if (wpabuf_tailroom(resp) <
1195 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1197 /* Length (to be filled) */
1198 len_pos = wpabuf_put(resp, 2);
1199 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1200 wpabuf_put_u8(resp, srv_trans_id);
1202 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1203 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1204 wpabuf_head(bsrv->resp),
1205 wpabuf_len(bsrv->resp));
1207 wpabuf_put_buf(resp, bsrv->query); /* Key */
1208 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1209 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1215 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1216 struct wpabuf *resp, u8 srv_trans_id,
1217 const u8 *query, size_t query_len)
1219 struct p2p_srv_bonjour *bsrv;
1223 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1225 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1226 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1227 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1232 if (query_len == 0) {
1233 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1237 if (wpabuf_tailroom(resp) < 5)
1239 /* Length (to be filled) */
1240 len_pos = wpabuf_put(resp, 2);
1241 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1242 wpabuf_put_u8(resp, srv_trans_id);
1244 wpabuf_set(&buf, query, query_len);
1245 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1247 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1251 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1252 /* Response Data: empty */
1253 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1259 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1260 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1261 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1263 if (wpabuf_tailroom(resp) >=
1264 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1266 wpabuf_put_buf(resp, bsrv->query); /* Key */
1267 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1269 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1273 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1274 struct wpabuf *resp, u8 srv_trans_id)
1276 struct p2p_srv_upnp *usrv;
1279 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1281 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1282 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1286 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1287 struct p2p_srv_upnp, list) {
1288 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1291 /* Length (to be filled) */
1292 len_pos = wpabuf_put(resp, 2);
1293 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1294 wpabuf_put_u8(resp, srv_trans_id);
1297 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1299 wpabuf_put_u8(resp, usrv->version);
1300 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1302 wpabuf_put_str(resp, usrv->service);
1303 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1309 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1310 struct wpabuf *resp, u8 srv_trans_id,
1311 const u8 *query, size_t query_len)
1313 struct p2p_srv_upnp *usrv;
1319 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1322 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1323 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1324 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1329 if (query_len == 0) {
1330 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1334 if (wpabuf_tailroom(resp) < 5)
1337 /* Length (to be filled) */
1338 len_pos = wpabuf_put(resp, 2);
1339 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1340 wpabuf_put_u8(resp, srv_trans_id);
1343 str = os_malloc(query_len);
1346 os_memcpy(str, query + 1, query_len - 1);
1347 str[query_len - 1] = '\0';
1349 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1350 struct p2p_srv_upnp, list) {
1351 if (version != usrv->version)
1354 if (os_strcmp(str, "ssdp:all") != 0 &&
1355 os_strstr(usrv->service, str) == NULL)
1358 if (wpabuf_tailroom(resp) < 2)
1362 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1364 wpabuf_put_u8(resp, version);
1366 wpabuf_put_u8(resp, ',');
1370 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1372 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1374 wpabuf_put_str(resp, usrv->service);
1379 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1382 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1383 /* Response Data: empty */
1386 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1390 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1391 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1393 struct wpa_supplicant *wpa_s = ctx;
1394 const u8 *pos = tlvs;
1395 const u8 *end = tlvs + tlvs_len;
1398 struct wpabuf *resp;
1399 u8 srv_proto, srv_trans_id;
1403 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1405 buf_len = 2 * tlvs_len + 1;
1406 buf = os_malloc(buf_len);
1408 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1409 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1411 freq, MAC2STR(sa), dialog_token, update_indic,
1416 if (wpa_s->p2p_sd_over_ctrl_iface) {
1417 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1418 update_indic, tlvs, tlvs_len);
1419 return; /* to be processed by an external program */
1422 resp = wpabuf_alloc(10000);
1426 while (pos + 1 < end) {
1427 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1428 slen = WPA_GET_LE16(pos);
1430 if (pos + slen > end || slen < 2) {
1431 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1436 tlv_end = pos + slen;
1439 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1441 srv_trans_id = *pos++;
1442 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1445 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1446 pos, tlv_end - pos);
1449 if (wpa_s->force_long_sd) {
1450 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1452 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1453 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1457 switch (srv_proto) {
1458 case P2P_SERV_ALL_SERVICES:
1459 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1460 "for all services");
1461 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1462 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1463 wpa_printf(MSG_DEBUG, "P2P: No service "
1464 "discovery protocols available");
1465 wpas_sd_add_proto_not_avail(
1466 resp, P2P_SERV_ALL_SERVICES,
1470 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1471 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1473 case P2P_SERV_BONJOUR:
1474 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1475 pos, tlv_end - pos);
1478 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1479 pos, tlv_end - pos);
1482 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1483 "protocol %u", srv_proto);
1484 wpas_sd_add_proto_not_avail(resp, srv_proto,
1493 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1494 update_indic, tlvs, tlvs_len);
1496 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1502 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1503 const u8 *tlvs, size_t tlvs_len)
1505 struct wpa_supplicant *wpa_s = ctx;
1506 const u8 *pos = tlvs;
1507 const u8 *end = tlvs + tlvs_len;
1513 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1515 if (tlvs_len > 1500) {
1516 /* TODO: better way for handling this */
1517 wpa_msg_ctrl(wpa_s, MSG_INFO,
1518 P2P_EVENT_SERV_DISC_RESP MACSTR
1519 " %u <long response: %u bytes>",
1520 MAC2STR(sa), update_indic,
1521 (unsigned int) tlvs_len);
1523 buf_len = 2 * tlvs_len + 1;
1524 buf = os_malloc(buf_len);
1526 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1527 wpa_msg_ctrl(wpa_s, MSG_INFO,
1528 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1529 MAC2STR(sa), update_indic, buf);
1535 u8 srv_proto, srv_trans_id, status;
1537 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1538 slen = WPA_GET_LE16(pos);
1540 if (pos + slen > end || slen < 3) {
1541 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1545 tlv_end = pos + slen;
1548 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1550 srv_trans_id = *pos++;
1551 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1554 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1557 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1558 pos, tlv_end - pos);
1563 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1567 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1568 const struct wpabuf *tlvs)
1570 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1571 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1572 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1574 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1578 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1579 u8 version, const char *query)
1581 struct wpabuf *tlvs;
1584 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1587 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1588 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1589 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1590 wpabuf_put_u8(tlvs, version);
1591 wpabuf_put_str(tlvs, query);
1592 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1598 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1600 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1601 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1602 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1604 return p2p_sd_cancel_request(wpa_s->global->p2p,
1605 (void *) (uintptr_t) req);
1609 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1610 const u8 *dst, u8 dialog_token,
1611 const struct wpabuf *resp_tlvs)
1613 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1614 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1618 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1620 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1625 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1627 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1628 wpa_drv_p2p_service_update(wpa_s);
1631 if (wpa_s->global->p2p)
1632 p2p_sd_service_update(wpa_s->global->p2p);
1636 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1638 dl_list_del(&bsrv->list);
1639 wpabuf_free(bsrv->query);
1640 wpabuf_free(bsrv->resp);
1645 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1647 dl_list_del(&usrv->list);
1648 os_free(usrv->service);
1653 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1655 struct p2p_srv_bonjour *bsrv, *bn;
1656 struct p2p_srv_upnp *usrv, *un;
1658 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1659 struct p2p_srv_bonjour, list)
1660 wpas_p2p_srv_bonjour_free(bsrv);
1662 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1663 struct p2p_srv_upnp, list)
1664 wpas_p2p_srv_upnp_free(usrv);
1666 wpas_p2p_sd_service_update(wpa_s);
1670 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1671 struct wpabuf *query, struct wpabuf *resp)
1673 struct p2p_srv_bonjour *bsrv;
1675 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1678 wpabuf_free(bsrv->resp);
1683 bsrv = os_zalloc(sizeof(*bsrv));
1686 bsrv->query = query;
1688 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1690 wpas_p2p_sd_service_update(wpa_s);
1695 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1696 const struct wpabuf *query)
1698 struct p2p_srv_bonjour *bsrv;
1700 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1703 wpas_p2p_srv_bonjour_free(bsrv);
1704 wpas_p2p_sd_service_update(wpa_s);
1709 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1710 const char *service)
1712 struct p2p_srv_upnp *usrv;
1714 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1715 return 0; /* Already listed */
1716 usrv = os_zalloc(sizeof(*usrv));
1719 usrv->version = version;
1720 usrv->service = os_strdup(service);
1721 if (usrv->service == NULL) {
1725 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1727 wpas_p2p_sd_service_update(wpa_s);
1732 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1733 const char *service)
1735 struct p2p_srv_upnp *usrv;
1737 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1740 wpas_p2p_srv_upnp_free(usrv);
1741 wpas_p2p_sd_service_update(wpa_s);
1746 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1747 const u8 *peer, const char *params,
1748 unsigned int generated_pin)
1750 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1751 MAC2STR(peer), generated_pin, params);
1755 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1756 const u8 *peer, const char *params)
1758 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1759 MAC2STR(peer), params);
1763 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1764 const u8 *dev_addr, const u8 *pri_dev_type,
1765 const char *dev_name, u16 supp_config_methods,
1766 u8 dev_capab, u8 group_capab, const u8 *group_id,
1767 size_t group_id_len)
1769 struct wpa_supplicant *wpa_s = ctx;
1770 char devtype[WPS_DEV_TYPE_BUFSIZE];
1772 u8 empty_dev_type[8];
1773 unsigned int generated_pin = 0;
1774 struct wpa_supplicant *group = NULL;
1777 for (group = wpa_s->global->ifaces; group; group = group->next)
1779 struct wpa_ssid *s = group->current_ssid;
1781 s->mode == WPAS_MODE_P2P_GO &&
1782 group_id_len - ETH_ALEN == s->ssid_len &&
1783 os_memcmp(group_id + ETH_ALEN, s->ssid,
1789 if (pri_dev_type == NULL) {
1790 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1791 pri_dev_type = empty_dev_type;
1793 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1794 " pri_dev_type=%s name='%s' config_methods=0x%x "
1795 "dev_capab=0x%x group_capab=0x%x%s%s",
1797 wps_dev_type_bin2str(pri_dev_type, devtype,
1799 dev_name, supp_config_methods, dev_capab, group_capab,
1800 group ? " group=" : "",
1801 group ? group->ifname : "");
1802 params[sizeof(params) - 1] = '\0';
1804 if (config_methods & WPS_CONFIG_DISPLAY) {
1805 generated_pin = wps_generate_pin();
1806 wpas_prov_disc_local_display(wpa_s, peer, params,
1808 } else if (config_methods & WPS_CONFIG_KEYPAD)
1809 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1810 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1811 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1812 "%s", MAC2STR(peer), params);
1814 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1815 P2P_PROV_DISC_SUCCESS,
1816 config_methods, generated_pin);
1820 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1822 struct wpa_supplicant *wpa_s = ctx;
1823 unsigned int generated_pin = 0;
1826 if (wpa_s->pending_pd_before_join &&
1827 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1828 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1829 wpa_s->pending_pd_before_join = 0;
1830 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1831 "join-existing-group operation");
1832 wpas_p2p_join_start(wpa_s);
1836 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1837 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1838 os_snprintf(params, sizeof(params), " peer_go=%d",
1839 wpa_s->pending_pd_use == AUTO_PD_JOIN);
1843 if (config_methods & WPS_CONFIG_DISPLAY)
1844 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1845 else if (config_methods & WPS_CONFIG_KEYPAD) {
1846 generated_pin = wps_generate_pin();
1847 wpas_prov_disc_local_display(wpa_s, peer, params,
1849 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1850 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1851 "%s", MAC2STR(peer), params);
1853 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1854 P2P_PROV_DISC_SUCCESS,
1855 config_methods, generated_pin);
1859 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1860 enum p2p_prov_disc_status status)
1862 struct wpa_supplicant *wpa_s = ctx;
1864 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1865 " p2p_dev_addr=" MACSTR " status=%d",
1866 MAC2STR(peer), status);
1868 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1873 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1874 const u8 *go_dev_addr, const u8 *ssid,
1875 size_t ssid_len, int *go, u8 *group_bssid,
1876 int *force_freq, int persistent_group)
1878 struct wpa_supplicant *wpa_s = ctx;
1880 u8 cur_bssid[ETH_ALEN];
1882 struct wpa_supplicant *grp;
1884 if (!persistent_group) {
1885 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1886 " to join an active group", MAC2STR(sa));
1887 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1888 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1890 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1891 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1892 "authorized invitation");
1896 * Do not accept the invitation automatically; notify user and
1899 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1902 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1904 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1905 "running persistent group");
1907 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1911 if (!wpa_s->conf->persistent_reconnect)
1912 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1914 for (s = wpa_s->conf->ssid; s; s = s->next) {
1915 if (s->disabled == 2 &&
1916 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1917 s->ssid_len == ssid_len &&
1918 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1923 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1924 " requested reinvocation of an unknown group",
1926 return P2P_SC_FAIL_UNKNOWN_GROUP;
1929 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1931 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1932 wpa_printf(MSG_DEBUG, "P2P: The only available "
1933 "interface is already in use - reject "
1935 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1937 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1938 } else if (s->mode == WPAS_MODE_P2P_GO) {
1940 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1942 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1943 "interface address for the group");
1944 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1946 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1951 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1952 wpa_s->assoc_freq) {
1953 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1954 "the channel we are already using");
1955 *force_freq = wpa_s->assoc_freq;
1958 res = wpa_drv_shared_freq(wpa_s);
1960 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1961 "with the channel we are already using on a "
1962 "shared interface");
1966 return P2P_SC_SUCCESS;
1970 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1971 const u8 *ssid, size_t ssid_len,
1972 const u8 *go_dev_addr, u8 status,
1975 struct wpa_supplicant *wpa_s = ctx;
1978 for (s = wpa_s->conf->ssid; s; s = s->next) {
1979 if (s->disabled == 2 &&
1980 s->ssid_len == ssid_len &&
1981 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1985 if (status == P2P_SC_SUCCESS) {
1986 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1987 " was accepted; op_freq=%d MHz",
1988 MAC2STR(sa), op_freq);
1990 wpas_p2p_group_add_persistent(
1991 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1993 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1994 wpa_s->p2p_wps_method, 0);
1999 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2000 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2001 " was rejected (status %u)", MAC2STR(sa), status);
2007 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2008 "sa=" MACSTR " go_dev_addr=" MACSTR
2009 " bssid=" MACSTR " unknown-network",
2010 MAC2STR(sa), MAC2STR(go_dev_addr),
2013 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2014 "sa=" MACSTR " go_dev_addr=" MACSTR
2016 MAC2STR(sa), MAC2STR(go_dev_addr));
2021 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2022 " persistent=%d", MAC2STR(sa), s->id);
2026 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2028 struct wpa_supplicant *wpa_s = ctx;
2029 struct wpa_ssid *ssid;
2032 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2033 "status=%d " MACSTR,
2034 status, MAC2STR(bssid));
2036 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2037 "status=%d ", status);
2039 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2041 if (wpa_s->pending_invite_ssid_id == -1)
2042 return; /* Invitation to active group */
2044 if (status != P2P_SC_SUCCESS) {
2045 wpas_p2p_remove_pending_group_interface(wpa_s);
2049 ssid = wpa_config_get_network(wpa_s->conf,
2050 wpa_s->pending_invite_ssid_id);
2052 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2053 "data matching with invitation");
2057 wpas_p2p_group_add_persistent(wpa_s, ssid,
2058 ssid->mode == WPAS_MODE_P2P_GO, 0);
2062 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2067 if (global->p2p_disallow_freq == NULL)
2070 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2071 if (freq >= global->p2p_disallow_freq[i].min &&
2072 freq <= global->p2p_disallow_freq[i].max)
2080 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2082 reg->channel[reg->channels] = chan;
2087 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2088 struct p2p_channels *chan)
2092 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2095 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2096 chan->reg_class[cla].reg_class = 81;
2097 chan->reg_class[cla].channels = 0;
2098 for (i = 0; i < 11; i++) {
2099 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2100 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2102 if (chan->reg_class[cla].channels)
2105 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2108 /* Operating class 115 - 5 GHz, channels 36-48 */
2109 chan->reg_class[cla].reg_class = 115;
2110 chan->reg_class[cla].channels = 0;
2111 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2112 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2113 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2114 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2115 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2116 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2117 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2118 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2119 if (chan->reg_class[cla].channels)
2122 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2125 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2126 chan->reg_class[cla].reg_class = 124;
2127 chan->reg_class[cla].channels = 0;
2128 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2129 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2130 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2131 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2132 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2133 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2134 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2135 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2136 if (chan->reg_class[cla].channels)
2139 chan->reg_classes = cla;
2144 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2146 enum hostapd_hw_mode mode)
2150 for (i = 0; i < num_modes; i++) {
2151 if (modes[i].mode == mode)
2159 static int has_channel(struct wpa_global *global,
2160 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2165 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2167 if (wpas_p2p_disallowed_freq(global, freq))
2170 for (i = 0; i < mode->num_channels; i++) {
2171 if (mode->channels[i].chan == chan) {
2173 *flags = mode->channels[i].flag;
2174 return !(mode->channels[i].flag &
2175 (HOSTAPD_CHAN_DISABLED |
2176 HOSTAPD_CHAN_PASSIVE_SCAN |
2177 HOSTAPD_CHAN_NO_IBSS |
2178 HOSTAPD_CHAN_RADAR));
2186 struct p2p_oper_class_map {
2187 enum hostapd_hw_mode mode;
2192 enum { BW20, BW40PLUS, BW40MINUS } bw;
2195 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2196 struct p2p_channels *chan)
2198 struct hostapd_hw_modes *mode;
2200 struct p2p_oper_class_map op_class[] = {
2201 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2202 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2203 #if 0 /* Do not enable HT40 on 2 GHz for now */
2204 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2205 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2207 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2208 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2209 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2210 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2211 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2212 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2213 { -1, 0, 0, 0, 0, BW20 }
2216 if (wpa_s->hw.modes == NULL) {
2217 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2218 "of all supported channels; assume dualband "
2220 return wpas_p2p_default_channels(wpa_s, chan);
2225 for (op = 0; op_class[op].op_class; op++) {
2226 struct p2p_oper_class_map *o = &op_class[op];
2228 struct p2p_reg_class *reg = NULL;
2230 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2233 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2235 if (!has_channel(wpa_s->global, mode, ch, &flag))
2237 if (o->bw == BW40MINUS &&
2238 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2239 !has_channel(wpa_s->global, mode, ch - 4, NULL)))
2241 if (o->bw == BW40PLUS &&
2242 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2243 !has_channel(wpa_s->global, mode, ch + 4, NULL)))
2246 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2247 "class %u", o->op_class);
2248 reg = &chan->reg_class[cla];
2250 reg->reg_class = o->op_class;
2252 reg->channel[reg->channels] = ch;
2256 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2257 reg->channel, reg->channels);
2261 chan->reg_classes = cla;
2267 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2270 struct wpa_supplicant *wpa_s = ctx;
2272 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2273 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2279 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2283 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2285 struct wpa_supplicant *wpa_s = ctx;
2287 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2288 struct wpa_ssid *ssid = wpa_s->current_ssid;
2291 if (ssid->mode != WPAS_MODE_INFRA)
2293 if (wpa_s->wpa_state != WPA_COMPLETED &&
2294 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2296 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2305 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2306 * @global: Pointer to global data from wpa_supplicant_init()
2307 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2308 * Returns: 0 on success, -1 on failure
2310 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2312 struct p2p_config p2p;
2316 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2322 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2323 struct p2p_params params;
2325 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2326 os_memset(¶ms, 0, sizeof(params));
2327 params.dev_name = wpa_s->conf->device_name;
2328 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2330 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2331 os_memcpy(params.sec_dev_type,
2332 wpa_s->conf->sec_device_type,
2333 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2335 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2341 os_memset(&p2p, 0, sizeof(p2p));
2342 p2p.msg_ctx = wpa_s;
2344 p2p.p2p_scan = wpas_p2p_scan;
2345 p2p.send_action = wpas_send_action;
2346 p2p.send_action_done = wpas_send_action_done;
2347 p2p.go_neg_completed = wpas_go_neg_completed;
2348 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2349 p2p.dev_found = wpas_dev_found;
2350 p2p.dev_lost = wpas_dev_lost;
2351 p2p.start_listen = wpas_start_listen;
2352 p2p.stop_listen = wpas_stop_listen;
2353 p2p.send_probe_resp = wpas_send_probe_resp;
2354 p2p.sd_request = wpas_sd_request;
2355 p2p.sd_response = wpas_sd_response;
2356 p2p.prov_disc_req = wpas_prov_disc_req;
2357 p2p.prov_disc_resp = wpas_prov_disc_resp;
2358 p2p.prov_disc_fail = wpas_prov_disc_fail;
2359 p2p.invitation_process = wpas_invitation_process;
2360 p2p.invitation_received = wpas_invitation_received;
2361 p2p.invitation_result = wpas_invitation_result;
2362 p2p.get_noa = wpas_get_noa;
2363 p2p.go_connected = wpas_go_connected;
2365 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2366 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2367 p2p.dev_name = wpa_s->conf->device_name;
2368 p2p.manufacturer = wpa_s->conf->manufacturer;
2369 p2p.model_name = wpa_s->conf->model_name;
2370 p2p.model_number = wpa_s->conf->model_number;
2371 p2p.serial_number = wpa_s->conf->serial_number;
2373 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2374 p2p.config_methods = wpa_s->wps->config_methods;
2377 if (wpa_s->conf->p2p_listen_reg_class &&
2378 wpa_s->conf->p2p_listen_channel) {
2379 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2380 p2p.channel = wpa_s->conf->p2p_listen_channel;
2384 * Pick one of the social channels randomly as the listen
2387 os_get_random((u8 *) &r, sizeof(r));
2388 p2p.channel = 1 + (r % 3) * 5;
2390 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2392 if (wpa_s->conf->p2p_oper_reg_class &&
2393 wpa_s->conf->p2p_oper_channel) {
2394 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2395 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2396 p2p.cfg_op_channel = 1;
2397 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2398 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2401 p2p.op_reg_class = 81;
2403 * Use random operation channel from (1, 6, 11) if no other
2404 * preference is indicated.
2406 os_get_random((u8 *) &r, sizeof(r));
2407 p2p.op_channel = 1 + (r % 3) * 5;
2408 p2p.cfg_op_channel = 0;
2409 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2410 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2412 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2413 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2414 p2p.country[2] = 0x04;
2416 os_memcpy(p2p.country, "XX\x04", 3);
2418 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2419 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2424 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2427 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2428 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2429 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2431 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2432 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2434 p2p.max_peers = 100;
2436 if (wpa_s->conf->p2p_ssid_postfix) {
2437 p2p.ssid_postfix_len =
2438 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2439 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2440 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2441 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2442 p2p.ssid_postfix_len);
2445 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2447 global->p2p = p2p_init(&p2p);
2448 if (global->p2p == NULL)
2451 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2452 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2454 p2p_add_wps_vendor_extension(
2455 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2463 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2464 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2466 * This function deinitialize per-interface P2P data.
2468 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2470 if (wpa_s->driver && wpa_s->drv_priv)
2471 wpa_drv_probe_req_report(wpa_s, 0);
2473 if (wpa_s->go_params) {
2474 /* Clear any stored provisioning info */
2475 p2p_clear_provisioning_info(
2477 wpa_s->go_params->peer_device_addr);
2480 os_free(wpa_s->go_params);
2481 wpa_s->go_params = NULL;
2482 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2483 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2484 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2485 wpa_s->p2p_long_listen = 0;
2486 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2487 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2488 wpas_p2p_remove_pending_group_interface(wpa_s);
2490 /* TODO: remove group interface from the driver if this wpa_s instance
2491 * is on top of a P2P group interface */
2496 * wpas_p2p_deinit_global - Deinitialize global P2P module
2497 * @global: Pointer to global data from wpa_supplicant_init()
2499 * This function deinitializes the global (per device) P2P module.
2501 void wpas_p2p_deinit_global(struct wpa_global *global)
2503 struct wpa_supplicant *wpa_s, *tmp;
2505 if (global->p2p == NULL)
2508 /* Remove remaining P2P group interfaces */
2509 wpa_s = global->ifaces;
2511 wpas_p2p_service_flush(wpa_s);
2512 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2513 wpa_s = wpa_s->next;
2515 tmp = global->ifaces;
2518 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2523 /* Disconnect from the P2P group and deinit the interface */
2524 wpas_p2p_disconnect(tmp);
2528 * Deinit GO data on any possibly remaining interface (if main
2529 * interface is used as GO).
2531 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2532 if (wpa_s->ap_iface)
2533 wpas_p2p_group_deinit(wpa_s);
2536 p2p_deinit(global->p2p);
2541 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2543 if (wpa_s->drv_flags &
2544 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2545 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2546 return 1; /* P2P group requires a new interface in every case
2548 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2549 return 0; /* driver does not support concurrent operations */
2550 if (wpa_s->global->ifaces->next)
2551 return 1; /* more that one interface already in use */
2552 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2553 return 1; /* this interface is already in use */
2558 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2559 const u8 *peer_addr,
2560 enum p2p_wps_method wps_method,
2561 int go_intent, const u8 *own_interface_addr,
2562 unsigned int force_freq, int persistent_group)
2564 if (persistent_group && wpa_s->conf->persistent_reconnect)
2565 persistent_group = 2;
2567 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2568 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2569 go_intent, own_interface_addr,
2570 force_freq, persistent_group);
2573 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2574 go_intent, own_interface_addr, force_freq,
2579 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2580 const u8 *peer_addr,
2581 enum p2p_wps_method wps_method,
2582 int go_intent, const u8 *own_interface_addr,
2583 unsigned int force_freq, int persistent_group)
2585 if (persistent_group && wpa_s->conf->persistent_reconnect)
2586 persistent_group = 2;
2588 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2591 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2592 go_intent, own_interface_addr, force_freq,
2597 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2599 wpa_s->p2p_join_scan_count++;
2600 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2601 wpa_s->p2p_join_scan_count);
2602 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2603 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2604 " for join operationg - stop join attempt",
2605 MAC2STR(wpa_s->pending_join_iface_addr));
2606 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2607 if (wpa_s->p2p_auto_pd) {
2608 wpa_s->p2p_auto_pd = 0;
2609 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2610 " p2p_dev_addr=" MACSTR " status=N/A",
2611 MAC2STR(wpa_s->pending_join_dev_addr));
2614 wpa_msg(wpa_s->parent, MSG_INFO,
2615 P2P_EVENT_GROUP_FORMATION_FAILURE);
2620 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2622 struct wpa_supplicant *wpa_s = eloop_ctx;
2623 if (!wpa_s->pending_pd_before_join)
2626 * Provision Discovery Response may have been lost - try to connect
2627 * anyway since we do not need any information from this PD.
2629 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2630 "try to connect anyway");
2631 wpas_p2p_join_start(wpa_s);
2635 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2637 struct wpa_supplicant *iface;
2641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2644 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2645 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2647 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2649 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2650 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2651 shared_freq = iface->current_ssid->frequency;
2652 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2653 shared_freq = iface->assoc_freq;
2657 if (shared_freq && freq != shared_freq) {
2658 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2659 "connected on %d MHz - new connection on "
2660 "%d MHz", iface->ifname, shared_freq, freq);
2665 shared_freq = wpa_drv_shared_freq(wpa_s);
2666 if (shared_freq > 0 && shared_freq != freq) {
2667 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2668 "virtual interface connected on %d MHz - new "
2669 "connection on %d MHz", shared_freq, freq);
2677 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2678 const u8 *peer_dev_addr)
2680 struct wpa_bss *bss;
2683 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2686 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2687 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2692 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2693 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2694 "%ld.%06ld (%supdated in last scan)",
2695 bss->last_update.sec, bss->last_update.usec,
2696 updated ? "": "not ");
2702 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2703 struct wpa_scan_results *scan_res)
2705 struct wpa_bss *bss;
2707 u8 iface_addr[ETH_ALEN];
2709 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2711 if (wpa_s->global->p2p_disabled)
2714 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2715 scan_res ? (int) scan_res->num : -1,
2716 wpa_s->p2p_auto_join ? "auto_" : "");
2719 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2721 if (wpa_s->p2p_auto_pd) {
2722 int join = wpas_p2p_peer_go(wpa_s,
2723 wpa_s->pending_join_dev_addr);
2724 wpa_s->p2p_auto_pd = 0;
2725 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2726 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2727 MAC2STR(wpa_s->pending_join_dev_addr), join);
2728 if (p2p_prov_disc_req(wpa_s->global->p2p,
2729 wpa_s->pending_join_dev_addr,
2730 wpa_s->pending_pd_config_methods, join,
2732 wpa_s->p2p_auto_pd = 0;
2733 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2734 " p2p_dev_addr=" MACSTR " status=N/A",
2735 MAC2STR(wpa_s->pending_join_dev_addr));
2740 if (wpa_s->p2p_auto_join) {
2741 if (!wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr)) {
2742 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2743 "running a GO -> use GO Negotiation");
2744 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2745 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2746 wpa_s->p2p_persistent_group, 0, 0, 0,
2747 wpa_s->p2p_go_intent,
2748 wpa_s->p2p_connect_freq);
2752 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO -> try "
2753 "to join the group");
2756 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2757 wpa_s->pending_join_iface_addr);
2759 p2p_get_interface_addr(wpa_s->global->p2p,
2760 wpa_s->pending_join_dev_addr,
2762 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2764 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2765 "address for join from " MACSTR " to " MACSTR
2766 " based on newly discovered P2P peer entry",
2767 MAC2STR(wpa_s->pending_join_iface_addr),
2768 MAC2STR(iface_addr));
2769 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2772 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2773 wpa_s->pending_join_iface_addr);
2776 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2777 "from P2P peer table: %d MHz", freq);
2779 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2782 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2783 "from BSS table: %d MHz", freq);
2788 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2789 wpa_msg(wpa_s->parent, MSG_INFO,
2790 P2P_EVENT_GROUP_FORMATION_FAILURE
2791 "reason=FREQ_CONFLICT");
2795 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2796 "prior to joining an existing group (GO " MACSTR
2798 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2799 wpa_s->pending_pd_before_join = 1;
2801 switch (wpa_s->pending_join_wps_method) {
2802 case WPS_PIN_DISPLAY:
2803 method = WPS_CONFIG_KEYPAD;
2805 case WPS_PIN_KEYPAD:
2806 method = WPS_CONFIG_DISPLAY;
2809 method = WPS_CONFIG_PUSHBUTTON;
2816 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2817 wpa_s->pending_join_dev_addr) ==
2820 * We have already performed provision discovery for
2821 * joining the group. Proceed directly to join
2822 * operation without duplicated provision discovery. */
2823 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2824 "with " MACSTR " already done - proceed to "
2826 MAC2STR(wpa_s->pending_join_dev_addr));
2827 wpa_s->pending_pd_before_join = 0;
2831 if (p2p_prov_disc_req(wpa_s->global->p2p,
2832 wpa_s->pending_join_dev_addr, method, 1,
2834 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2835 "Discovery Request before joining an "
2837 wpa_s->pending_pd_before_join = 0;
2842 * Actual join operation will be started from the Action frame
2843 * TX status callback (if no ACK is received) or when the
2844 * Provision Discovery Response is received. Use a short
2845 * timeout as a backup mechanism should the Provision Discovery
2846 * Response be lost for any reason.
2848 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2850 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2855 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2856 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2857 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2858 wpas_p2p_check_join_scan_limit(wpa_s);
2862 /* Start join operation immediately */
2863 wpas_p2p_join_start(wpa_s);
2867 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2869 struct wpa_supplicant *wpa_s = eloop_ctx;
2871 struct wpa_driver_scan_params params;
2872 struct wpabuf *wps_ie, *ies;
2875 os_memset(¶ms, 0, sizeof(params));
2877 /* P2P Wildcard SSID */
2878 params.num_ssids = 1;
2879 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2880 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2882 wpa_s->wps->dev.p2p = 1;
2883 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
2884 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
2886 if (wps_ie == NULL) {
2887 wpas_p2p_scan_res_join(wpa_s, NULL);
2891 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2892 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
2894 wpabuf_free(wps_ie);
2895 wpas_p2p_scan_res_join(wpa_s, NULL);
2898 wpabuf_put_buf(ies, wps_ie);
2899 wpabuf_free(wps_ie);
2901 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
2903 params.p2p_probe = 1;
2904 params.extra_ies = wpabuf_head(ies);
2905 params.extra_ies_len = wpabuf_len(ies);
2908 * Run a scan to update BSS table and start Provision Discovery once
2909 * the new scan results become available.
2911 ret = wpa_drv_scan(wpa_s, ¶ms);
2913 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2918 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2920 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2921 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2922 wpas_p2p_check_join_scan_limit(wpa_s);
2927 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2928 const u8 *dev_addr, enum p2p_wps_method wps_method,
2931 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2932 MACSTR " dev " MACSTR ")%s",
2933 MAC2STR(iface_addr), MAC2STR(dev_addr),
2934 auto_join ? " (auto_join)" : "");
2936 wpa_s->p2p_auto_pd = 0;
2937 wpa_s->p2p_auto_join = !!auto_join;
2938 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2939 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2940 wpa_s->pending_join_wps_method = wps_method;
2942 /* Make sure we are not running find during connection establishment */
2943 wpas_p2p_stop_find(wpa_s);
2945 wpa_s->p2p_join_scan_count = 0;
2946 wpas_p2p_join_scan(wpa_s, NULL);
2951 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2953 struct wpa_supplicant *group;
2954 struct p2p_go_neg_results res;
2955 struct wpa_bss *bss;
2957 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2958 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2961 if (group != wpa_s) {
2962 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2963 sizeof(group->p2p_pin));
2964 group->p2p_wps_method = wpa_s->p2p_wps_method;
2967 group->p2p_in_provisioning = 1;
2969 os_memset(&res, 0, sizeof(res));
2970 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2972 res.wps_method = wpa_s->pending_join_wps_method;
2973 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2975 res.freq = bss->freq;
2976 res.ssid_len = bss->ssid_len;
2977 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
2980 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2981 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
2983 wpa_drv_cancel_remain_on_channel(wpa_s);
2984 wpa_s->off_channel_freq = 0;
2985 wpa_s->roc_waiting_drv_freq = 0;
2987 wpas_start_wps_enrollee(group, &res);
2990 * Allow a longer timeout for join-a-running-group than normal 15
2991 * second group formation timeout since the GO may not have authorized
2992 * our connection yet.
2994 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2995 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3003 * wpas_p2p_connect - Request P2P Group Formation to be started
3004 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3005 * @peer_addr: Address of the peer P2P Device
3006 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3007 * @persistent_group: Whether to create a persistent group
3008 * @auto_join: Whether to select join vs. GO Negotiation automatically
3009 * @join: Whether to join an existing group (as a client) instead of starting
3010 * Group Owner negotiation; @peer_addr is BSSID in that case
3011 * @auth: Whether to only authorize the connection instead of doing that and
3012 * initiating Group Owner negotiation
3013 * @go_intent: GO Intent or -1 to use default
3014 * @freq: Frequency for the group or 0 for auto-selection
3015 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3016 * failure, -2 on failure due to channel not currently available,
3017 * -3 if forced channel is not supported
3019 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3020 const char *pin, enum p2p_wps_method wps_method,
3021 int persistent_group, int auto_join, int join, int auth,
3022 int go_intent, int freq)
3024 int force_freq = 0, oper_freq = 0;
3027 enum wpa_driver_if_type iftype;
3030 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3034 go_intent = wpa_s->conf->p2p_go_intent;
3037 wpa_s->p2p_long_listen = 0;
3039 wpa_s->p2p_wps_method = wps_method;
3040 wpa_s->p2p_persistent_group = !!persistent_group;
3041 wpa_s->p2p_go_intent = go_intent;
3042 wpa_s->p2p_connect_freq = freq;
3045 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3046 else if (wps_method == WPS_PIN_DISPLAY) {
3047 ret = wps_generate_pin();
3048 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3050 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3053 wpa_s->p2p_pin[0] = '\0';
3055 if (join || auto_join) {
3056 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3058 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3059 "connect a running group from " MACSTR,
3060 MAC2STR(peer_addr));
3061 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3064 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3065 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3067 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3068 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3072 os_get_time(&wpa_s->p2p_auto_started);
3073 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3075 wpa_s->p2p_auto_started.sec,
3076 wpa_s->p2p_auto_started.usec);
3078 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3084 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3086 oper_freq = wpa_s->assoc_freq;
3088 oper_freq = wpa_drv_shared_freq(wpa_s);
3094 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3095 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3096 "(%u MHz) is not supported for P2P uses",
3101 if (oper_freq > 0 && freq != oper_freq &&
3102 !(wpa_s->drv_flags &
3103 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3104 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3105 "on %u MHz while connected on another "
3106 "channel (%u MHz)", freq, oper_freq);
3109 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3110 "requested channel (%u MHz)", freq);
3112 } else if (oper_freq > 0 &&
3113 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3114 if (!(wpa_s->drv_flags &
3115 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3116 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3117 "while connected on non-P2P supported "
3118 "channel (%u MHz)", oper_freq);
3121 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3122 "(%u MHz) not available for P2P - try to use "
3123 "another channel", oper_freq);
3125 } else if (oper_freq > 0) {
3126 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3127 "channel we are already using (%u MHz) on another "
3128 "interface", oper_freq);
3129 force_freq = oper_freq;
3132 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3134 if (wpa_s->create_p2p_iface) {
3135 /* Prepare to add a new interface for the group */
3136 iftype = WPA_IF_P2P_GROUP;
3137 if (go_intent == 15)
3138 iftype = WPA_IF_P2P_GO;
3139 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3140 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3141 "interface for the group");
3145 if_addr = wpa_s->pending_interface_addr;
3147 if_addr = wpa_s->own_addr;
3150 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3152 force_freq, persistent_group) < 0)
3157 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3158 go_intent, if_addr, force_freq,
3159 persistent_group) < 0) {
3160 if (wpa_s->create_p2p_iface)
3161 wpas_p2p_remove_pending_group_interface(wpa_s);
3169 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3170 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3171 * @freq: Frequency of the channel in MHz
3172 * @duration: Duration of the stay on the channel in milliseconds
3174 * This callback is called when the driver indicates that it has started the
3175 * requested remain-on-channel duration.
3177 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3178 unsigned int freq, unsigned int duration)
3180 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3182 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3183 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3184 wpa_s->pending_listen_duration);
3185 wpa_s->pending_listen_freq = 0;
3190 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3191 unsigned int timeout)
3193 /* Limit maximum Listen state time based on driver limitation. */
3194 if (timeout > wpa_s->max_remain_on_chan)
3195 timeout = wpa_s->max_remain_on_chan;
3197 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3198 return wpa_drv_p2p_listen(wpa_s, timeout);
3200 return p2p_listen(wpa_s->global->p2p, timeout);
3205 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3206 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3207 * @freq: Frequency of the channel in MHz
3209 * This callback is called when the driver indicates that a remain-on-channel
3210 * operation has been completed, i.e., the duration on the requested channel
3213 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3216 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3217 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3218 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3221 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3222 return; /* P2P module started a new operation */
3223 if (wpa_s->pending_action_tx)
3225 if (wpa_s->p2p_long_listen > 0)
3226 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3227 if (wpa_s->p2p_long_listen > 0) {
3228 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3229 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3235 * wpas_p2p_group_remove - Remove a P2P group
3236 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3237 * @ifname: Network interface name of the group interface or "*" to remove all
3239 * Returns: 0 on success, -1 on failure
3241 * This function is used to remove a P2P group. This can be used to disconnect
3242 * from a group in which the local end is a P2P Client or to end a P2P Group in
3243 * case the local end is the Group Owner. If a virtual network interface was
3244 * created for this group, that interface will be removed. Otherwise, only the
3245 * configured P2P group network will be removed from the interface.
3247 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3249 struct wpa_global *global = wpa_s->global;
3251 if (os_strcmp(ifname, "*") == 0) {
3252 struct wpa_supplicant *prev;
3253 wpa_s = global->ifaces;
3256 wpa_s = wpa_s->next;
3257 wpas_p2p_disconnect(prev);
3262 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3263 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3267 return wpas_p2p_disconnect(wpa_s);
3271 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3272 struct p2p_go_neg_results *params,
3278 os_memset(params, 0, sizeof(*params));
3279 params->role_go = 1;
3281 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3282 "frequency %d MHz", freq);
3283 params->freq = freq;
3284 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3285 wpa_s->conf->p2p_oper_channel >= 1 &&
3286 wpa_s->conf->p2p_oper_channel <= 11) {
3287 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3288 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3289 "frequency %d MHz", params->freq);
3290 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3291 wpa_s->conf->p2p_oper_reg_class == 124) {
3292 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3293 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3294 "frequency %d MHz", params->freq);
3295 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3296 wpa_s->best_overall_freq > 0 &&
3297 p2p_supported_freq(wpa_s->global->p2p,
3298 wpa_s->best_overall_freq)) {
3299 params->freq = wpa_s->best_overall_freq;
3300 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3301 "channel %d MHz", params->freq);
3302 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3303 wpa_s->best_24_freq > 0 &&
3304 p2p_supported_freq(wpa_s->global->p2p,
3305 wpa_s->best_24_freq)) {
3306 params->freq = wpa_s->best_24_freq;
3307 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3308 "channel %d MHz", params->freq);
3309 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3310 wpa_s->best_5_freq > 0 &&
3311 p2p_supported_freq(wpa_s->global->p2p,
3312 wpa_s->best_5_freq)) {
3313 params->freq = wpa_s->best_5_freq;
3314 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3315 "channel %d MHz", params->freq);
3318 for (chan = 0; chan < 11; chan++) {
3319 params->freq = 2412 + chan * 5;
3320 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3325 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3329 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3330 "known)", params->freq);
3333 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3334 wpa_s->assoc_freq && !freq) {
3335 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3337 params->freq = wpa_s->assoc_freq;
3340 res = wpa_drv_shared_freq(wpa_s);
3341 if (res > 0 && !freq) {
3342 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3343 "already using on a shared interface");
3345 } else if (res > 0 && freq != res &&
3346 !(wpa_s->drv_flags &
3347 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3348 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3349 "while connected on another channel (%u MHz)",
3358 static struct wpa_supplicant *
3359 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3362 struct wpa_supplicant *group_wpa_s;
3364 if (!wpas_p2p_create_iface(wpa_s))
3367 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3368 WPA_IF_P2P_CLIENT) < 0)
3370 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3371 if (group_wpa_s == NULL) {
3372 wpas_p2p_remove_pending_group_interface(wpa_s);
3381 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3382 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3383 * @persistent_group: Whether to create a persistent group
3384 * @freq: Frequency for the group or 0 to indicate no hardcoding
3385 * Returns: 0 on success, -1 on failure
3387 * This function creates a new P2P group with the local end as the Group Owner,
3388 * i.e., without using Group Owner Negotiation.
3390 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3393 struct p2p_go_neg_results params;
3396 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3399 /* Make sure we are not running find during connection establishment */
3400 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3401 wpas_p2p_stop_find(wpa_s);
3404 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3406 if (wpa_s->best_24_freq > 0 &&
3407 p2p_supported_freq(wpa_s->global->p2p,
3408 wpa_s->best_24_freq)) {
3409 freq = wpa_s->best_24_freq;
3410 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3411 "channel: %d MHz", freq);
3413 os_get_random((u8 *) &r, sizeof(r));
3414 freq = 2412 + (r % 3) * 25;
3415 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3416 "channel: %d MHz", freq);
3421 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3423 if (wpa_s->best_5_freq > 0 &&
3424 p2p_supported_freq(wpa_s->global->p2p,
3425 wpa_s->best_5_freq)) {
3426 freq = wpa_s->best_5_freq;
3427 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3428 "channel: %d MHz", freq);
3430 os_get_random((u8 *) &r, sizeof(r));
3431 freq = 5180 + (r % 4) * 20;
3432 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3433 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3434 "5 GHz channel for P2P group");
3437 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3438 "channel: %d MHz", freq);
3442 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3443 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3444 "(%u MHz) is not supported for P2P uses",
3449 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq))
3452 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3453 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3454 "(%u MHz) is not supported for P2P uses",
3458 p2p_go_params(wpa_s->global->p2p, ¶ms);
3459 params.persistent_group = persistent_group;
3461 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3464 wpas_start_wps_go(wpa_s, ¶ms, 0);
3470 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3471 struct wpa_ssid *params, int addr_allocated)
3473 struct wpa_ssid *ssid;
3475 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3479 wpa_supplicant_ap_deinit(wpa_s);
3481 ssid = wpa_config_add_network(wpa_s->conf);
3484 wpa_config_set_network_defaults(ssid);
3485 ssid->temporary = 1;
3486 ssid->proto = WPA_PROTO_RSN;
3487 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3488 ssid->group_cipher = WPA_CIPHER_CCMP;
3489 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3490 ssid->ssid = os_malloc(params->ssid_len);
3491 if (ssid->ssid == NULL) {
3492 wpa_config_remove_network(wpa_s->conf, ssid->id);
3495 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3496 ssid->ssid_len = params->ssid_len;
3497 ssid->p2p_group = 1;
3498 ssid->export_keys = 1;
3499 if (params->psk_set) {
3500 os_memcpy(ssid->psk, params->psk, 32);
3503 if (params->passphrase)
3504 ssid->passphrase = os_strdup(params->passphrase);
3506 wpa_supplicant_select_network(wpa_s, ssid);
3508 wpa_s->show_group_started = 1;
3514 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3515 struct wpa_ssid *ssid, int addr_allocated,
3518 struct p2p_go_neg_results params;
3521 if (ssid->disabled != 2 || ssid->ssid == NULL)
3524 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3525 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3526 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3531 /* Make sure we are not running find during connection establishment */
3532 wpas_p2p_stop_find(wpa_s);
3534 if (ssid->mode == WPAS_MODE_INFRA)
3535 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3537 if (ssid->mode != WPAS_MODE_P2P_GO)
3540 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq))
3544 if (ssid->passphrase == NULL ||
3545 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3546 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3550 os_strlcpy(params.passphrase, ssid->passphrase,
3551 sizeof(params.passphrase));
3552 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3553 params.ssid_len = ssid->ssid_len;
3554 params.persistent_group = 1;
3556 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3560 wpas_start_wps_go(wpa_s, ¶ms, 0);
3566 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3567 struct wpabuf *proberesp_ies)
3569 struct wpa_supplicant *wpa_s = ctx;
3570 if (wpa_s->ap_iface) {
3571 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3572 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3573 wpabuf_free(beacon_ies);
3574 wpabuf_free(proberesp_ies);
3578 wpabuf_free(hapd->p2p_beacon_ie);
3579 hapd->p2p_beacon_ie = beacon_ies;
3581 wpabuf_free(hapd->p2p_probe_resp_ie);
3582 hapd->p2p_probe_resp_ie = proberesp_ies;
3584 wpabuf_free(beacon_ies);
3585 wpabuf_free(proberesp_ies);
3587 wpa_supplicant_ap_update_beacon(wpa_s);
3591 static void wpas_p2p_idle_update(void *ctx, int idle)
3593 struct wpa_supplicant *wpa_s = ctx;
3594 if (!wpa_s->ap_iface)
3596 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3598 wpas_p2p_set_group_idle_timeout(wpa_s);
3600 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3604 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3605 int persistent_group,
3606 int group_formation)
3608 struct p2p_group *group;
3609 struct p2p_group_config *cfg;
3611 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3613 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3616 cfg = os_zalloc(sizeof(*cfg));
3620 if (persistent_group && wpa_s->conf->persistent_reconnect)
3621 cfg->persistent_group = 2;
3622 else if (persistent_group)
3623 cfg->persistent_group = 1;
3624 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3625 if (wpa_s->max_stations &&
3626 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3627 cfg->max_clients = wpa_s->max_stations;
3629 cfg->max_clients = wpa_s->conf->max_num_sta;
3630 cfg->cb_ctx = wpa_s;
3631 cfg->ie_update = wpas_p2p_ie_update;
3632 cfg->idle_update = wpas_p2p_idle_update;
3634 group = p2p_group_init(wpa_s->global->p2p, cfg);
3637 if (!group_formation)
3638 p2p_group_notif_formation_done(group);
3639 wpa_s->p2p_group = group;
3644 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3647 struct wpa_ssid *ssid = wpa_s->current_ssid;
3649 if (!wpa_s->p2p_in_provisioning) {
3650 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3651 "provisioning not in progress");
3655 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3656 u8 go_dev_addr[ETH_ALEN];
3657 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3658 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3660 /* Clear any stored provisioning info */
3661 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3664 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3666 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3668 * Use a separate timeout for initial data connection to
3669 * complete to allow the group to be removed automatically if
3670 * something goes wrong in this step before the P2P group idle
3671 * timeout mechanism is taken into use.
3673 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3674 wpas_p2p_group_formation_timeout,
3677 if (wpa_s->global->p2p)
3678 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3679 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3680 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3681 wpas_group_formation_completed(wpa_s, 1);
3685 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3686 struct wps_event_fail *fail)
3688 if (!wpa_s->p2p_in_provisioning) {
3689 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3690 "provisioning not in progress");
3694 if (wpa_s->go_params) {
3695 p2p_clear_provisioning_info(
3697 wpa_s->go_params->peer_device_addr);
3700 wpas_notify_p2p_wps_failed(wpa_s, fail);
3704 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3705 const char *config_method,
3706 enum wpas_p2p_prov_disc_use use)
3710 wpa_s->pending_pd_use = NORMAL_PD;
3711 if (os_strncmp(config_method, "display", 7) == 0)
3712 config_methods = WPS_CONFIG_DISPLAY;
3713 else if (os_strncmp(config_method, "keypad", 6) == 0)
3714 config_methods = WPS_CONFIG_KEYPAD;
3715 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3716 os_strncmp(config_method, "pushbutton", 10) == 0)
3717 config_methods = WPS_CONFIG_PUSHBUTTON;
3719 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
3723 if (use == WPAS_P2P_PD_AUTO) {
3724 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3725 wpa_s->pending_pd_config_methods = config_methods;
3726 wpa_s->p2p_auto_pd = 1;
3727 wpa_s->p2p_auto_join = 0;
3728 wpa_s->pending_pd_before_join = 0;
3729 wpas_p2p_stop_find(wpa_s);
3730 wpa_s->p2p_join_scan_count = 0;
3731 wpas_p2p_join_scan(wpa_s, NULL);
3735 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3736 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3738 use == WPAS_P2P_PD_FOR_JOIN);
3741 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3744 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3745 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3750 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3753 return p2p_scan_result_text(ies, ies_len, buf, end);
3757 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3759 if (!wpa_s->pending_action_tx)
3762 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3763 "operation request");
3764 wpabuf_free(wpa_s->pending_action_tx);
3765 wpa_s->pending_action_tx = NULL;
3769 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3770 enum p2p_discovery_type type,
3771 unsigned int num_req_dev_types, const u8 *req_dev_types,
3774 wpas_p2p_clear_pending_action_tx(wpa_s);
3775 wpa_s->p2p_long_listen = 0;
3777 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3778 return wpa_drv_p2p_find(wpa_s, timeout, type);
3780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3783 wpa_supplicant_cancel_sched_scan(wpa_s);
3785 return p2p_find(wpa_s->global->p2p, timeout, type,
3786 num_req_dev_types, req_dev_types, dev_id);
3790 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3792 wpas_p2p_clear_pending_action_tx(wpa_s);
3793 wpa_s->p2p_long_listen = 0;
3794 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3795 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3796 wpa_s->p2p_cb_on_scan_complete = 0;
3798 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3799 wpa_drv_p2p_stop_find(wpa_s);
3803 if (wpa_s->global->p2p)
3804 p2p_stop_find(wpa_s->global->p2p);
3806 wpas_p2p_remove_pending_group_interface(wpa_s);
3810 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3812 struct wpa_supplicant *wpa_s = eloop_ctx;
3813 wpa_s->p2p_long_listen = 0;
3817 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3821 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3824 wpa_supplicant_cancel_sched_scan(wpa_s);
3825 wpas_p2p_clear_pending_action_tx(wpa_s);
3829 * This is a request for unlimited Listen state. However, at
3830 * least for now, this is mapped to a Listen state for one
3835 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3836 wpa_s->p2p_long_listen = 0;
3839 * Stop previous find/listen operation to avoid trying to request a new
3840 * remain-on-channel operation while the driver is still running the
3843 if (wpa_s->global->p2p)
3844 p2p_stop_find(wpa_s->global->p2p);
3846 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3847 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3848 wpa_s->p2p_long_listen = timeout * 1000;
3849 eloop_register_timeout(timeout, 0,
3850 wpas_p2p_long_listen_timeout,
3858 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3859 u8 *buf, size_t len, int p2p_group)
3861 struct wpabuf *p2p_ie;
3864 if (wpa_s->global->p2p_disabled)
3866 if (wpa_s->global->p2p == NULL)
3871 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3872 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3874 wpabuf_free(p2p_ie);
3880 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3881 const u8 *dst, const u8 *bssid,
3882 const u8 *ie, size_t ie_len, int ssi_signal)
3884 if (wpa_s->global->p2p_disabled)
3886 if (wpa_s->global->p2p == NULL)
3889 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3891 case P2P_PREQ_NOT_P2P:
3892 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
3895 case P2P_PREQ_MALFORMED:
3896 case P2P_PREQ_NOT_LISTEN:
3897 case P2P_PREQ_NOT_PROCESSED:
3898 default: /* make gcc happy */
3900 case P2P_PREQ_PROCESSED:
3906 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3907 const u8 *sa, const u8 *bssid,
3908 u8 category, const u8 *data, size_t len, int freq)
3910 if (wpa_s->global->p2p_disabled)
3912 if (wpa_s->global->p2p == NULL)
3915 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3920 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3922 if (wpa_s->global->p2p_disabled)
3924 if (wpa_s->global->p2p == NULL)
3927 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3931 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3933 p2p_group_deinit(wpa_s->p2p_group);
3934 wpa_s->p2p_group = NULL;
3936 wpa_s->ap_configured_cb = NULL;
3937 wpa_s->ap_configured_cb_ctx = NULL;
3938 wpa_s->ap_configured_cb_data = NULL;
3939 wpa_s->connect_without_scan = NULL;
3943 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3945 wpa_s->p2p_long_listen = 0;
3947 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3948 return wpa_drv_p2p_reject(wpa_s, addr);
3950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3953 return p2p_reject(wpa_s->global->p2p, addr);
3957 /* Invite to reinvoke a persistent group */
3958 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3959 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3961 enum p2p_invite_role role;
3964 if (ssid->mode == WPAS_MODE_P2P_GO) {
3965 role = P2P_INVITE_ROLE_GO;
3966 if (peer_addr == NULL) {
3967 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3968 "address in invitation command");
3971 if (wpas_p2p_create_iface(wpa_s)) {
3972 if (wpas_p2p_add_group_interface(wpa_s,
3973 WPA_IF_P2P_GO) < 0) {
3974 wpa_printf(MSG_ERROR, "P2P: Failed to "
3975 "allocate a new interface for the "
3979 bssid = wpa_s->pending_interface_addr;
3981 bssid = wpa_s->own_addr;
3983 role = P2P_INVITE_ROLE_CLIENT;
3984 peer_addr = ssid->bssid;
3986 wpa_s->pending_invite_ssid_id = ssid->id;
3988 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3989 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3990 ssid->ssid, ssid->ssid_len,
3993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3996 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3997 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4001 /* Invite to join an active group */
4002 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4003 const u8 *peer_addr, const u8 *go_dev_addr)
4005 struct wpa_global *global = wpa_s->global;
4006 enum p2p_invite_role role;
4008 struct wpa_ssid *ssid;
4011 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4012 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4015 if (wpa_s == NULL) {
4016 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4020 ssid = wpa_s->current_ssid;
4022 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4027 persistent = ssid->p2p_persistent_group &&
4028 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4029 ssid->ssid, ssid->ssid_len);
4031 if (ssid->mode == WPAS_MODE_P2P_GO) {
4032 role = P2P_INVITE_ROLE_ACTIVE_GO;
4033 bssid = wpa_s->own_addr;
4034 if (go_dev_addr == NULL)
4035 go_dev_addr = wpa_s->global->p2p_dev_addr;
4037 role = P2P_INVITE_ROLE_CLIENT;
4038 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4039 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4040 "invite to current group");
4043 bssid = wpa_s->bssid;
4044 if (go_dev_addr == NULL &&
4045 !is_zero_ether_addr(wpa_s->go_dev_addr))
4046 go_dev_addr = wpa_s->go_dev_addr;
4048 wpa_s->parent->pending_invite_ssid_id = -1;
4050 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4051 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4052 ssid->ssid, ssid->ssid_len,
4053 go_dev_addr, persistent);
4055 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4058 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4059 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4060 go_dev_addr, persistent);
4064 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4066 struct wpa_ssid *ssid = wpa_s->current_ssid;
4067 const char *ssid_txt;
4068 u8 go_dev_addr[ETH_ALEN];
4069 int network_id = -1;
4073 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4076 if (!wpa_s->show_group_started || !ssid)
4079 wpa_s->show_group_started = 0;
4081 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4082 os_memset(go_dev_addr, 0, ETH_ALEN);
4083 if (ssid->bssid_set)
4084 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4085 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4087 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4089 if (wpa_s->global->p2p_group_formation == wpa_s)
4090 wpa_s->global->p2p_group_formation = NULL;
4092 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4093 (int) wpa_s->assoc_freq;
4094 if (ssid->passphrase == NULL && ssid->psk_set) {
4096 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4097 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4098 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4100 wpa_s->ifname, ssid_txt, freq, psk,
4101 MAC2STR(go_dev_addr),
4102 persistent ? " [PERSISTENT]" : "");
4104 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4105 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4106 "go_dev_addr=" MACSTR "%s",
4107 wpa_s->ifname, ssid_txt, freq,
4108 ssid->passphrase ? ssid->passphrase : "",
4109 MAC2STR(go_dev_addr),
4110 persistent ? " [PERSISTENT]" : "");
4114 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4117 network_id = ssid->id;
4118 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4122 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4123 u32 interval1, u32 duration2, u32 interval2)
4125 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4130 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4131 wpa_s->current_ssid == NULL ||
4132 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4135 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4136 wpa_s->own_addr, wpa_s->assoc_freq,
4137 duration1, interval1, duration2, interval2);
4141 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4142 unsigned int interval)
4144 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4147 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4150 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4154 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4156 return wpa_s->current_ssid != NULL &&
4157 wpa_s->current_ssid->p2p_group &&
4158 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4162 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4164 struct wpa_supplicant *wpa_s = eloop_ctx;
4166 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4167 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4172 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4174 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
4175 wpas_p2p_group_delete(wpa_s);
4179 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4183 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4184 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4186 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4189 timeout = wpa_s->conf->p2p_group_idle;
4190 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4191 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4192 timeout = P2P_MAX_CLIENT_IDLE;
4198 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4199 timeout = 0; /* special client mode no-timeout */
4204 if (wpa_s->p2p_in_provisioning) {
4206 * Use the normal group formation timeout during the
4207 * provisioning phase to avoid terminating this process too
4208 * early due to group idle timeout.
4210 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4211 "during provisioning");
4215 if (wpa_s->show_group_started) {
4217 * Use the normal group formation timeout between the end of
4218 * the provisioning phase and completion of 4-way handshake to
4219 * avoid terminating this process too early due to group idle
4222 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4223 "while waiting for initial 4-way handshake to "
4228 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4230 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4235 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4236 u16 reason_code, const u8 *ie, size_t ie_len,
4237 int locally_generated)
4239 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4244 if (!locally_generated)
4245 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4248 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4249 wpa_s->current_ssid &&
4250 wpa_s->current_ssid->p2p_group &&
4251 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4252 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4253 "session is ending");
4254 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
4255 wpas_p2p_group_delete(wpa_s);
4260 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4261 u16 reason_code, const u8 *ie, size_t ie_len,
4262 int locally_generated)
4264 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4266 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4269 if (!locally_generated)
4270 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4275 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4277 struct p2p_data *p2p = wpa_s->global->p2p;
4282 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4285 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4286 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4288 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4289 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4292 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4293 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4295 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4296 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4298 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4299 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4300 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4301 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4302 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4305 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4306 p2p_set_sec_dev_types(p2p,
4307 (void *) wpa_s->conf->sec_device_type,
4308 wpa_s->conf->num_sec_device_types);
4310 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4312 p2p_remove_wps_vendor_extensions(p2p);
4313 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4314 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4316 p2p_add_wps_vendor_extension(
4317 p2p, wpa_s->conf->wps_vendor_ext[i]);
4321 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4322 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4324 country[0] = wpa_s->conf->country[0];
4325 country[1] = wpa_s->conf->country[1];
4327 p2p_set_country(p2p, country);
4330 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4331 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4332 wpa_s->conf->p2p_ssid_postfix ?
4333 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4337 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4338 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4340 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4341 u8 reg_class, channel;
4344 if (wpa_s->conf->p2p_listen_reg_class &&
4345 wpa_s->conf->p2p_listen_channel) {
4346 reg_class = wpa_s->conf->p2p_listen_reg_class;
4347 channel = wpa_s->conf->p2p_listen_channel;
4351 * Pick one of the social channels randomly as the
4354 os_get_random((u8 *) &r, sizeof(r));
4355 channel = 1 + (r % 3) * 5;
4357 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4359 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4362 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4363 u8 op_reg_class, op_channel, cfg_op_channel;
4366 if (wpa_s->conf->p2p_oper_reg_class &&
4367 wpa_s->conf->p2p_oper_channel) {
4368 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4369 op_channel = wpa_s->conf->p2p_oper_channel;
4374 * Use random operation channel from (1, 6, 11)
4375 *if no other preference is indicated.
4377 os_get_random((u8 *) &r, sizeof(r));
4378 op_channel = 1 + (r % 3) * 5;
4381 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4384 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4388 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4389 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4390 wpa_s->conf->p2p_pref_chan) < 0) {
4391 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4398 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4401 if (!wpa_s->ap_iface)
4403 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4408 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4410 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4412 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4415 wpa_s->global->cross_connection = enabled;
4416 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4419 struct wpa_supplicant *iface;
4421 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4423 if (iface->cross_connect_enabled == 0)
4426 iface->cross_connect_enabled = 0;
4427 iface->cross_connect_in_use = 0;
4428 wpa_msg(iface->parent, MSG_INFO,
4429 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4430 iface->ifname, iface->cross_connect_uplink);
4438 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4440 struct wpa_supplicant *iface;
4442 if (!uplink->global->cross_connection)
4445 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4446 if (!iface->cross_connect_enabled)
4448 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4451 if (iface->ap_iface == NULL)
4453 if (iface->cross_connect_in_use)
4456 iface->cross_connect_in_use = 1;
4457 wpa_msg(iface->parent, MSG_INFO,
4458 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4459 iface->ifname, iface->cross_connect_uplink);
4464 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4466 struct wpa_supplicant *iface;
4468 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4469 if (!iface->cross_connect_enabled)
4471 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4474 if (!iface->cross_connect_in_use)
4477 wpa_msg(iface->parent, MSG_INFO,
4478 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4479 iface->ifname, iface->cross_connect_uplink);
4480 iface->cross_connect_in_use = 0;
4485 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4487 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4488 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4489 wpa_s->cross_connect_disallowed)
4490 wpas_p2p_disable_cross_connect(wpa_s);
4492 wpas_p2p_enable_cross_connect(wpa_s);
4493 if (!wpa_s->ap_iface &&
4494 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4495 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4499 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4501 wpas_p2p_disable_cross_connect(wpa_s);
4502 if (!wpa_s->ap_iface &&
4503 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4505 wpas_p2p_set_group_idle_timeout(wpa_s);
4509 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4511 struct wpa_supplicant *iface;
4513 if (!wpa_s->global->cross_connection)
4516 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4519 if (iface->drv_flags &
4520 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4522 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4525 wpa_s->cross_connect_enabled = 1;
4526 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4527 sizeof(wpa_s->cross_connect_uplink));
4528 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4529 "%s to %s whenever uplink is available",
4530 wpa_s->ifname, wpa_s->cross_connect_uplink);
4532 if (iface->ap_iface || iface->current_ssid == NULL ||
4533 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4534 iface->cross_connect_disallowed ||
4535 iface->wpa_state != WPA_COMPLETED)
4538 wpa_s->cross_connect_in_use = 1;
4539 wpa_msg(wpa_s->parent, MSG_INFO,
4540 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4541 wpa_s->ifname, wpa_s->cross_connect_uplink);
4547 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4549 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4550 !wpa_s->p2p_in_provisioning)
4551 return 0; /* not P2P client operation */
4553 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4555 if (wpa_s != wpa_s->parent)
4556 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4558 if (wpa_s->global->p2p)
4559 p2p_group_formation_failed(wpa_s->global->p2p);
4561 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4562 wpa_s->parent, NULL);
4564 wpas_group_formation_completed(wpa_s, 0);
4569 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4571 struct p2p_channels chan;
4573 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4576 os_memset(&chan, 0, sizeof(chan));
4577 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4578 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4583 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4587 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4589 struct wpa_global *global = wpa_s->global;
4593 if (global->p2p == NULL)
4596 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4598 if (wpa_s->pending_interface_name[0] &&
4599 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4602 peer = p2p_get_go_neg_peer(global->p2p);
4604 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4605 MACSTR, MAC2STR(peer));
4606 p2p_unauthorize(global->p2p, peer);
4610 wpas_p2p_stop_find(wpa_s);
4612 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4613 if (wpa_s == global->p2p_group_formation &&
4614 (wpa_s->p2p_in_provisioning ||
4615 wpa_s->parent->pending_interface_type ==
4616 WPA_IF_P2P_CLIENT)) {
4617 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4618 "formation found - cancelling",
4621 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4622 wpa_s->parent, NULL);
4623 wpas_p2p_group_delete(wpa_s);
4629 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4637 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4639 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4642 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4643 "being available anymore");
4644 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4645 wpas_p2p_group_delete(wpa_s);
4649 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4650 int freq_24, int freq_5, int freq_overall)
4652 struct p2p_data *p2p = wpa_s->global->p2p;
4653 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4655 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4659 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4662 struct p2p_data *p2p = wpa_s->global->p2p;
4664 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4667 if (hwaddr_aton(addr, peer))
4670 return p2p_unauthorize(p2p, peer);
4675 * wpas_p2p_disconnect - Disconnect from a P2P Group
4676 * @wpa_s: Pointer to wpa_supplicant data
4677 * Returns: 0 on success, -1 on failure
4679 * This can be used to disconnect from a group in which the local end is a P2P
4680 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4681 * virtual network interface was created for this group, that interface will be
4682 * removed. Otherwise, only the configured P2P group network will be removed
4683 * from the interface.
4685 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4691 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4692 wpas_p2p_group_delete(wpa_s);
4698 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4700 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4703 return p2p_in_progress(wpa_s->global->p2p);
4707 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4708 struct wpa_ssid *ssid)
4710 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4711 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4712 wpa_s->parent, NULL) > 0) {
4714 * Remove the network by scheduling the group formation
4715 * timeout to happen immediately. The teardown code
4716 * needs to be scheduled to run asynch later so that we
4717 * don't delete data from under ourselves unexpectedly.
4718 * Calling wpas_p2p_group_formation_timeout directly
4719 * causes a series of crashes in WPS failure scenarios.
4721 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4722 "P2P group network getting removed");
4723 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4724 wpa_s->parent, NULL);
4729 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
4730 const u8 *addr, const u8 *ssid,
4736 for (s = wpa_s->conf->ssid; s; s = s->next) {
4737 if (s->disabled != 2)
4740 (ssid_len != s->ssid_len ||
4741 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4743 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4744 return s; /* peer is GO in the persistent group */
4745 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4747 for (i = 0; i < s->num_p2p_clients; i++) {
4748 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4749 addr, ETH_ALEN) == 0)
4750 return s; /* peer is P2P client in persistent
4759 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4764 wpas_p2p_add_persistent_group_client(wpa_s, addr);