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
48 #define P2P_MAX_CLIENT_IDLE 20
50 #define P2P_MAX_CLIENT_IDLE 10
51 #endif /* ANDROID_P2P */
52 #endif /* P2P_MAX_CLIENT_IDLE */
55 static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
57 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
58 static struct wpa_supplicant *
59 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
61 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
62 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
63 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
64 const u8 *dev_addr, enum p2p_wps_method wps_method);
65 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
67 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
68 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
69 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
70 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
73 static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
75 struct wpa_supplicant *iface = NULL;
77 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
78 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
79 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
80 "defer P2P SEARCH", iface->ifname);
89 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
90 struct wpa_scan_results *scan_res)
94 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
97 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
100 for (i = 0; i < scan_res->num; i++) {
101 struct wpa_scan_res *bss = scan_res->res[i];
102 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
103 bss->freq, bss->level,
104 (const u8 *) (bss + 1),
109 p2p_scan_res_handled(wpa_s->global->p2p);
113 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
114 unsigned int num_req_dev_types,
115 const u8 *req_dev_types, const u8 *dev_id)
117 struct wpa_supplicant *wpa_s = ctx;
118 struct wpa_driver_scan_params params;
120 struct wpabuf *wps_ie, *ies;
121 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
128 os_memset(¶ms, 0, sizeof(params));
130 /* P2P Wildcard SSID */
131 params.num_ssids = 1;
132 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
133 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
135 wpa_s->wps->dev.p2p = 1;
136 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
138 num_req_dev_types, req_dev_types);
142 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
143 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
148 wpabuf_put_buf(ies, wps_ie);
151 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
153 params.p2p_probe = 1;
154 params.extra_ies = wpabuf_head(ies);
155 params.extra_ies_len = wpabuf_len(ies);
158 case P2P_SCAN_SOCIAL:
159 params.freqs = social_channels;
163 case P2P_SCAN_SPECIFIC:
164 social_channels[0] = freq;
165 social_channels[1] = 0;
166 params.freqs = social_channels;
168 case P2P_SCAN_SOCIAL_PLUS_ONE:
169 social_channels[3] = freq;
170 params.freqs = social_channels;
174 was_in_p2p_scan = wpa_s->scan_res_handler == wpas_p2p_scan_res_handler;
175 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
176 ret = wpa_drv_scan(wpa_s, ¶ms);
181 wpa_s->scan_res_handler = NULL;
183 if (wpa_s->scanning || was_in_p2p_scan || wpas_global_scan_in_progress(wpa_s)) {
185 if (wpa_s->scanning || was_in_p2p_scan) {
187 wpa_s->p2p_cb_on_scan_complete = 1;
196 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
198 switch (p2p_group_interface) {
199 case P2P_GROUP_INTERFACE_PENDING:
200 return WPA_IF_P2P_GROUP;
201 case P2P_GROUP_INTERFACE_GO:
202 return WPA_IF_P2P_GO;
203 case P2P_GROUP_INTERFACE_CLIENT:
204 return WPA_IF_P2P_CLIENT;
207 return WPA_IF_P2P_GROUP;
211 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
213 size_t ssid_len, int *go)
217 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
218 for (s = wpa_s->conf->ssid; s; s = s->next) {
219 if (s->disabled != 0 || !s->p2p_group ||
220 s->ssid_len != ssid_len ||
221 os_memcmp(ssid, s->ssid, ssid_len) != 0)
223 if (s->mode == WPAS_MODE_P2P_GO &&
224 s != wpa_s->current_ssid)
227 *go = s->mode == WPAS_MODE_P2P_GO;
236 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
238 struct wpa_ssid *ssid;
242 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
244 ssid = wpa_s->current_ssid;
247 * The current SSID was not known, but there may still be a
248 * pending P2P group interface waiting for provisioning.
250 ssid = wpa_s->conf->ssid;
252 if (ssid->p2p_group &&
253 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
254 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
259 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
261 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
262 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
263 wpa_s->reassociate = 0;
264 wpa_s->disconnected = 1;
265 wpa_supplicant_deauthenticate(wpa_s,
266 WLAN_REASON_DEAUTH_LEAVING);
270 if (wpa_s->cross_connect_in_use) {
271 wpa_s->cross_connect_in_use = 0;
272 wpa_msg(wpa_s->parent, MSG_INFO,
273 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
274 wpa_s->ifname, wpa_s->cross_connect_uplink);
276 switch (wpa_s->removal_reason) {
277 case P2P_GROUP_REMOVAL_REQUESTED:
278 reason = " reason=REQUESTED";
280 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
281 reason = " reason=IDLE";
283 case P2P_GROUP_REMOVAL_UNAVAILABLE:
284 reason = " reason=UNAVAILABLE";
287 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
288 reason = " reason=FREQ_CONFLICT";
295 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
296 wpa_s->ifname, gtype, reason);
299 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
301 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
302 struct wpa_global *global;
304 enum wpa_driver_if_type type;
305 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
307 global = wpa_s->global;
308 ifname = os_strdup(wpa_s->ifname);
309 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
310 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
311 wpa_s = global->ifaces;
313 wpa_drv_if_remove(wpa_s, type, ifname);
318 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
319 if (ssid && (ssid->p2p_group ||
320 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
321 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
323 if (ssid == wpa_s->current_ssid) {
324 wpa_sm_set_config(wpa_s->wpa, NULL);
325 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
326 wpa_s->current_ssid = NULL;
329 * Networks objects created during any P2P activities are not
330 * exposed out as they might/will confuse certain non-P2P aware
331 * applications since these network objects won't behave like
334 * Likewise, we don't send out network removed signals for such
337 wpa_config_remove_network(wpa_s->conf, id);
338 wpa_supplicant_clear_status(wpa_s);
339 wpa_supplicant_cancel_sched_scan(wpa_s);
341 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
344 wpa_supplicant_ap_deinit(wpa_s);
348 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
350 const u8 *ssid, size_t ssid_len)
358 if (wpa_s->go_params)
359 bssid = wpa_s->go_params->peer_interface_addr;
361 bssid = wpa_s->bssid;
363 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
365 u8 iface_addr[ETH_ALEN];
366 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
368 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
371 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
372 "group is persistent - BSS " MACSTR " not found",
377 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
379 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
380 "group is persistent - BSS " MACSTR
381 " did not include P2P IE", MAC2STR(bssid));
382 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
383 (u8 *) (bss + 1), bss->ie_len);
384 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
385 ((u8 *) bss + 1) + bss->ie_len,
390 group_capab = p2p_get_group_capab(p2p);
391 addr = p2p_get_go_dev_addr(p2p);
392 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
393 "group_capab=0x%x", group_capab);
395 os_memcpy(go_dev_addr, addr, ETH_ALEN);
396 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
399 os_memset(go_dev_addr, 0, ETH_ALEN);
402 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
403 "go_dev_addr=" MACSTR,
404 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
406 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
410 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
411 struct wpa_ssid *ssid,
412 const u8 *go_dev_addr)
417 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
418 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
419 for (s = wpa_s->conf->ssid; s; s = s->next) {
420 if (s->disabled == 2 &&
421 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
422 s->ssid_len == ssid->ssid_len &&
423 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
428 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
430 if (ssid->passphrase && !s->passphrase)
432 else if (ssid->passphrase && s->passphrase &&
433 os_strcmp(ssid->passphrase, s->passphrase) != 0)
436 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
439 s = wpa_config_add_network(wpa_s->conf);
444 * Instead of network_added we emit persistent_group_added
445 * notification. Also to keep the defense checks in
446 * persistent_group obj registration method, we set the
447 * relevant flags in s to designate it as a persistent group.
450 s->p2p_persistent_group = 1;
451 wpas_notify_persistent_group_added(wpa_s, s);
452 wpa_config_set_network_defaults(s);
456 s->p2p_persistent_group = 1;
459 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
460 s->mode = ssid->mode;
461 s->auth_alg = WPA_AUTH_ALG_OPEN;
462 s->key_mgmt = WPA_KEY_MGMT_PSK;
463 s->proto = WPA_PROTO_RSN;
464 s->pairwise_cipher = WPA_CIPHER_CCMP;
466 if (ssid->passphrase) {
467 os_free(s->passphrase);
468 s->passphrase = os_strdup(ssid->passphrase);
472 os_memcpy(s->psk, ssid->psk, 32);
474 if (s->passphrase && !s->psk_set)
475 wpa_config_update_psk(s);
476 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
478 s->ssid = os_malloc(ssid->ssid_len);
481 s->ssid_len = ssid->ssid_len;
482 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
485 #ifndef CONFIG_NO_CONFIG_WRITE
486 if (changed && wpa_s->conf->update_config &&
487 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
488 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
490 #endif /* CONFIG_NO_CONFIG_WRITE */
496 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
499 struct wpa_ssid *ssid, *s;
503 ssid = wpa_s->current_ssid;
504 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
505 !ssid->p2p_persistent_group)
508 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
509 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
512 if (s->ssid_len == ssid->ssid_len &&
513 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
520 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
521 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
523 return; /* already in list */
526 n = os_realloc(s->p2p_client_list,
527 (s->num_p2p_clients + 1) * ETH_ALEN);
530 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
531 s->p2p_client_list = n;
532 s->num_p2p_clients++;
534 #ifndef CONFIG_NO_CONFIG_WRITE
535 if (wpa_s->parent->conf->update_config &&
536 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
537 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
538 #endif /* CONFIG_NO_CONFIG_WRITE */
542 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
545 struct wpa_ssid *ssid;
546 const char *ssid_txt;
549 u8 go_dev_addr[ETH_ALEN];
553 * This callback is likely called for the main interface. Update wpa_s
554 * to use the group interface if a new interface was created for the
557 if (wpa_s->global->p2p_group_formation)
558 wpa_s = wpa_s->global->p2p_group_formation;
559 wpa_s->global->p2p_group_formation = NULL;
560 wpa_s->p2p_in_provisioning = 0;
563 wpa_msg(wpa_s->parent, MSG_INFO,
564 P2P_EVENT_GROUP_FORMATION_FAILURE);
565 wpas_p2p_group_delete(wpa_s);
569 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
571 ssid = wpa_s->current_ssid;
572 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
573 ssid->mode = WPAS_MODE_P2P_GO;
574 p2p_group_notif_formation_done(wpa_s->p2p_group);
575 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
580 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
581 client = ssid->mode == WPAS_MODE_INFRA;
582 if (ssid->mode == WPAS_MODE_P2P_GO) {
583 persistent = ssid->p2p_persistent_group;
584 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
587 persistent = wpas_p2p_persistent_group(wpa_s,
593 client = wpa_s->p2p_group_interface ==
594 P2P_GROUP_INTERFACE_CLIENT;
595 os_memset(go_dev_addr, 0, ETH_ALEN);
598 wpa_s->show_group_started = 0;
601 * Indicate event only after successfully completed 4-way
602 * handshake, i.e., when the interface is ready for data
605 wpa_s->show_group_started = 1;
606 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
608 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
609 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
610 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
612 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
613 MAC2STR(go_dev_addr),
614 persistent ? " [PERSISTENT]" : "");
615 wpas_p2p_cross_connect_setup(wpa_s);
616 wpas_p2p_set_group_idle_timeout(wpa_s);
618 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
619 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
620 "go_dev_addr=" MACSTR "%s",
621 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
622 ssid && ssid->passphrase ? ssid->passphrase : "",
623 MAC2STR(go_dev_addr),
624 persistent ? " [PERSISTENT]" : "");
625 wpas_p2p_cross_connect_setup(wpa_s);
626 wpas_p2p_set_group_idle_timeout(wpa_s);
630 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
632 if (network_id < 0 && ssid)
633 network_id = ssid->id;
635 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
639 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
641 const u8 *dst, const u8 *src,
643 const u8 *data, size_t data_len,
644 enum offchannel_send_action_result
647 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
649 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
651 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
655 case OFFCHANNEL_SEND_ACTION_SUCCESS:
656 res = P2P_SEND_ACTION_SUCCESS;
658 case OFFCHANNEL_SEND_ACTION_NO_ACK:
659 res = P2P_SEND_ACTION_NO_ACK;
661 case OFFCHANNEL_SEND_ACTION_FAILED:
662 res = P2P_SEND_ACTION_FAILED;
666 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
668 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
669 wpa_s->pending_pd_before_join &&
670 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
671 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
672 wpa_s->pending_pd_before_join = 0;
673 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
674 "join-existing-group operation (no ACK for PD "
676 wpas_p2p_join_start(wpa_s);
681 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
682 const u8 *src, const u8 *bssid, const u8 *buf,
683 size_t len, unsigned int wait_time)
685 struct wpa_supplicant *wpa_s = ctx;
686 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
688 wpas_p2p_send_action_tx_status, 1);
692 static void wpas_send_action_done(void *ctx)
694 struct wpa_supplicant *wpa_s = ctx;
695 offchannel_send_action_done(wpa_s);
699 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
700 struct p2p_go_neg_results *params)
702 if (wpa_s->go_params == NULL) {
703 wpa_s->go_params = os_malloc(sizeof(*params));
704 if (wpa_s->go_params == NULL)
707 os_memcpy(wpa_s->go_params, params, sizeof(*params));
712 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
713 struct p2p_go_neg_results *res)
715 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
716 MAC2STR(res->peer_interface_addr));
717 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
718 res->ssid, res->ssid_len);
719 wpa_supplicant_ap_deinit(wpa_s);
720 wpas_copy_go_neg_results(wpa_s, res);
721 if (res->wps_method == WPS_PBC)
722 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
724 u16 dev_pw_id = DEV_PW_DEFAULT;
725 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
726 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
727 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
728 wpa_s->p2p_pin, 1, dev_pw_id);
733 static void p2p_go_configured(void *ctx, void *data)
735 struct wpa_supplicant *wpa_s = ctx;
736 struct p2p_go_neg_results *params = data;
737 struct wpa_ssid *ssid;
740 ssid = wpa_s->current_ssid;
741 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
742 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
743 if (wpa_s->global->p2p_group_formation == wpa_s)
744 wpa_s->global->p2p_group_formation = NULL;
745 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
746 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
747 "go_dev_addr=" MACSTR "%s",
749 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
751 params->passphrase ? params->passphrase : "",
752 MAC2STR(wpa_s->global->p2p_dev_addr),
753 params->persistent_group ? " [PERSISTENT]" : "");
755 if (params->persistent_group)
756 network_id = wpas_p2p_store_persistent_group(
758 wpa_s->global->p2p_dev_addr);
760 network_id = ssid->id;
761 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
762 wpas_p2p_cross_connect_setup(wpa_s);
763 wpas_p2p_set_group_idle_timeout(wpa_s);
767 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
768 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
769 params->peer_interface_addr)) {
770 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
774 if (params->wps_method == WPS_PBC)
775 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
776 params->peer_device_addr);
777 else if (wpa_s->p2p_pin[0])
778 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
779 wpa_s->p2p_pin, NULL, 0);
780 os_free(wpa_s->go_params);
781 wpa_s->go_params = NULL;
785 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
786 struct p2p_go_neg_results *params,
789 struct wpa_ssid *ssid;
791 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
794 ssid = wpa_config_add_network(wpa_s->conf);
798 wpa_s->show_group_started = 0;
800 wpa_config_set_network_defaults(ssid);
803 ssid->p2p_persistent_group = params->persistent_group;
804 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
806 ssid->frequency = params->freq;
807 ssid->ssid = os_zalloc(params->ssid_len + 1);
809 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
810 ssid->ssid_len = params->ssid_len;
812 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
813 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
814 ssid->proto = WPA_PROTO_RSN;
815 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
816 ssid->passphrase = os_strdup(params->passphrase);
818 wpa_s->ap_configured_cb = p2p_go_configured;
819 wpa_s->ap_configured_cb_ctx = wpa_s;
820 wpa_s->ap_configured_cb_data = wpa_s->go_params;
821 wpa_s->connect_without_scan = ssid;
822 wpa_s->reassociate = 1;
823 wpa_s->disconnected = 0;
824 wpa_supplicant_req_scan(wpa_s, 0, 0);
828 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
829 const struct wpa_supplicant *src)
831 struct wpa_config *d;
832 const struct wpa_config *s;
837 #define C(n) if (s->n) d->n = os_strdup(s->n)
846 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
847 os_memcpy(d->sec_device_type, s->sec_device_type,
848 sizeof(d->sec_device_type));
849 d->num_sec_device_types = s->num_sec_device_types;
851 d->p2p_group_idle = s->p2p_group_idle;
852 d->p2p_intra_bss = s->p2p_intra_bss;
853 d->persistent_reconnect = s->persistent_reconnect;
857 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
858 enum wpa_driver_if_type type)
860 char ifname[120], force_ifname[120];
862 if (wpa_s->pending_interface_name[0]) {
863 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
864 "- skip creation of a new one");
865 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
866 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
867 "unknown?! ifname='%s'",
868 wpa_s->pending_interface_name);
874 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
875 wpa_s->p2p_group_idx);
876 if (os_strlen(ifname) >= IFNAMSIZ &&
877 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
878 /* Try to avoid going over the IFNAMSIZ length limit */
879 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
880 wpa_s->p2p_group_idx);
882 force_ifname[0] = '\0';
884 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
886 wpa_s->p2p_group_idx++;
888 wpa_s->pending_interface_type = type;
889 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
890 wpa_s->pending_interface_addr, NULL) < 0) {
891 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
896 if (force_ifname[0]) {
897 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
899 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
900 sizeof(wpa_s->pending_interface_name));
902 os_strlcpy(wpa_s->pending_interface_name, ifname,
903 sizeof(wpa_s->pending_interface_name));
904 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
905 MACSTR, wpa_s->pending_interface_name,
906 MAC2STR(wpa_s->pending_interface_addr));
912 static void wpas_p2p_remove_pending_group_interface(
913 struct wpa_supplicant *wpa_s)
915 if (!wpa_s->pending_interface_name[0] ||
916 is_zero_ether_addr(wpa_s->pending_interface_addr))
917 return; /* No pending virtual interface */
919 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
920 wpa_s->pending_interface_name);
921 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
922 wpa_s->pending_interface_name);
923 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
924 wpa_s->pending_interface_name[0] = '\0';
928 static struct wpa_supplicant *
929 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
931 struct wpa_interface iface;
932 struct wpa_supplicant *group_wpa_s;
934 if (!wpa_s->pending_interface_name[0]) {
935 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
936 if (!wpas_p2p_create_iface(wpa_s))
939 * Something has forced us to remove the pending interface; try
940 * to create a new one and hope for the best that we will get
941 * the same local address.
943 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
944 WPA_IF_P2P_CLIENT) < 0)
948 os_memset(&iface, 0, sizeof(iface));
949 iface.ifname = wpa_s->pending_interface_name;
950 iface.driver = wpa_s->driver->name;
951 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
952 iface.driver_param = wpa_s->conf->driver_param;
953 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
954 if (group_wpa_s == NULL) {
955 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
956 "wpa_supplicant interface");
959 wpa_s->pending_interface_name[0] = '\0';
960 group_wpa_s->parent = wpa_s;
961 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
962 P2P_GROUP_INTERFACE_CLIENT;
963 wpa_s->global->p2p_group_formation = group_wpa_s;
965 wpas_p2p_clone_config(group_wpa_s, wpa_s);
971 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
974 struct wpa_supplicant *wpa_s = eloop_ctx;
975 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
976 if (wpa_s->global->p2p)
977 p2p_group_formation_failed(wpa_s->global->p2p);
978 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
979 wpa_drv_p2p_group_formation_failed(wpa_s);
980 wpas_group_formation_completed(wpa_s, 0);
984 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
986 struct wpa_supplicant *wpa_s = ctx;
988 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
989 wpa_drv_cancel_remain_on_channel(wpa_s);
990 wpa_s->off_channel_freq = 0;
991 wpa_s->roc_waiting_drv_freq = 0;
995 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
997 wpas_notify_p2p_go_neg_completed(wpa_s, res);
998 wpas_p2p_remove_pending_group_interface(wpa_s);
1002 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1003 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1005 if (wpa_s->create_p2p_iface) {
1006 struct wpa_supplicant *group_wpa_s =
1007 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1008 if (group_wpa_s == NULL) {
1009 wpas_p2p_remove_pending_group_interface(wpa_s);
1012 if (group_wpa_s != wpa_s) {
1013 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1014 sizeof(group_wpa_s->p2p_pin));
1015 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1017 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1018 wpa_s->pending_interface_name[0] = '\0';
1019 group_wpa_s->p2p_in_provisioning = 1;
1022 wpas_start_wps_go(group_wpa_s, res, 1);
1024 wpas_start_wps_enrollee(group_wpa_s, res);
1026 wpa_s->p2p_in_provisioning = 1;
1027 wpa_s->global->p2p_group_formation = wpa_s;
1030 wpas_start_wps_go(wpa_s, res, 1);
1032 wpas_start_wps_enrollee(ctx, res);
1035 wpa_s->p2p_long_listen = 0;
1036 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1038 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1039 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1040 (res->peer_config_timeout % 100) * 10000,
1041 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1045 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1047 struct wpa_supplicant *wpa_s = ctx;
1048 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1049 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1051 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1055 void wpas_dev_found(void *ctx, const u8 *addr,
1056 const struct p2p_peer_info *info,
1059 #ifndef CONFIG_NO_STDOUT_DEBUG
1060 struct wpa_supplicant *wpa_s = ctx;
1061 char devtype[WPS_DEV_TYPE_BUFSIZE];
1063 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1064 " p2p_dev_addr=" MACSTR
1065 " pri_dev_type=%s name='%s' config_methods=0x%x "
1066 "dev_capab=0x%x group_capab=0x%x",
1067 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1068 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1070 info->device_name, info->config_methods,
1071 info->dev_capab, info->group_capab);
1072 #endif /* CONFIG_NO_STDOUT_DEBUG */
1074 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1078 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1080 struct wpa_supplicant *wpa_s = ctx;
1082 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1083 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1085 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1089 static int wpas_start_listen(void *ctx, unsigned int freq,
1090 unsigned int duration,
1091 const struct wpabuf *probe_resp_ie)
1093 struct wpa_supplicant *wpa_s = ctx;
1095 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1097 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1098 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1099 "report received Probe Request frames");
1103 wpa_s->pending_listen_freq = freq;
1104 wpa_s->pending_listen_duration = duration;
1106 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1107 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1108 "to remain on channel (%u MHz) for Listen "
1110 wpa_s->pending_listen_freq = 0;
1113 wpa_s->off_channel_freq = 0;
1114 wpa_s->roc_waiting_drv_freq = freq;
1120 static void wpas_stop_listen(void *ctx)
1122 struct wpa_supplicant *wpa_s = ctx;
1123 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1124 wpa_drv_cancel_remain_on_channel(wpa_s);
1125 wpa_s->off_channel_freq = 0;
1126 wpa_s->roc_waiting_drv_freq = 0;
1128 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1129 wpa_drv_probe_req_report(wpa_s, 0);
1133 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1135 struct wpa_supplicant *wpa_s = ctx;
1136 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1140 static struct p2p_srv_bonjour *
1141 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1142 const struct wpabuf *query)
1144 struct p2p_srv_bonjour *bsrv;
1147 len = wpabuf_len(query);
1148 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1149 struct p2p_srv_bonjour, list) {
1150 if (len == wpabuf_len(bsrv->query) &&
1151 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1159 static struct p2p_srv_upnp *
1160 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1161 const char *service)
1163 struct p2p_srv_upnp *usrv;
1165 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1166 struct p2p_srv_upnp, list) {
1167 if (version == usrv->version &&
1168 os_strcmp(service, usrv->service) == 0)
1175 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1180 if (wpabuf_tailroom(resp) < 5)
1183 /* Length (to be filled) */
1184 len_pos = wpabuf_put(resp, 2);
1185 wpabuf_put_u8(resp, srv_proto);
1186 wpabuf_put_u8(resp, srv_trans_id);
1188 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1189 /* Response Data: empty */
1190 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1194 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1195 struct wpabuf *resp, u8 srv_trans_id)
1197 struct p2p_srv_bonjour *bsrv;
1200 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1202 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1203 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1207 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1208 struct p2p_srv_bonjour, list) {
1209 if (wpabuf_tailroom(resp) <
1210 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1212 /* Length (to be filled) */
1213 len_pos = wpabuf_put(resp, 2);
1214 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1215 wpabuf_put_u8(resp, srv_trans_id);
1217 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1218 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1219 wpabuf_head(bsrv->resp),
1220 wpabuf_len(bsrv->resp));
1222 wpabuf_put_buf(resp, bsrv->query); /* Key */
1223 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1224 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1230 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1231 struct wpabuf *resp, u8 srv_trans_id,
1232 const u8 *query, size_t query_len)
1234 struct p2p_srv_bonjour *bsrv;
1238 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1240 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1241 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1242 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1247 if (query_len == 0) {
1248 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1252 if (wpabuf_tailroom(resp) < 5)
1254 /* Length (to be filled) */
1255 len_pos = wpabuf_put(resp, 2);
1256 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1257 wpabuf_put_u8(resp, srv_trans_id);
1259 wpabuf_set(&buf, query, query_len);
1260 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1262 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1266 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1267 /* Response Data: empty */
1268 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1274 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1275 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1276 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1278 if (wpabuf_tailroom(resp) >=
1279 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1281 wpabuf_put_buf(resp, bsrv->query); /* Key */
1282 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1284 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1288 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1289 struct wpabuf *resp, u8 srv_trans_id)
1291 struct p2p_srv_upnp *usrv;
1294 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1296 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1297 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1301 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1302 struct p2p_srv_upnp, list) {
1303 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1306 /* Length (to be filled) */
1307 len_pos = wpabuf_put(resp, 2);
1308 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1309 wpabuf_put_u8(resp, srv_trans_id);
1312 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1314 wpabuf_put_u8(resp, usrv->version);
1315 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1317 wpabuf_put_str(resp, usrv->service);
1318 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1324 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1325 struct wpabuf *resp, u8 srv_trans_id,
1326 const u8 *query, size_t query_len)
1328 struct p2p_srv_upnp *usrv;
1334 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1337 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1338 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1339 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1344 if (query_len == 0) {
1345 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1349 if (wpabuf_tailroom(resp) < 5)
1352 /* Length (to be filled) */
1353 len_pos = wpabuf_put(resp, 2);
1354 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1355 wpabuf_put_u8(resp, srv_trans_id);
1358 str = os_malloc(query_len);
1361 os_memcpy(str, query + 1, query_len - 1);
1362 str[query_len - 1] = '\0';
1364 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1365 struct p2p_srv_upnp, list) {
1366 if (version != usrv->version)
1369 if (os_strcmp(str, "ssdp:all") != 0 &&
1370 os_strstr(usrv->service, str) == NULL)
1373 if (wpabuf_tailroom(resp) < 2)
1377 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1379 wpabuf_put_u8(resp, version);
1381 wpabuf_put_u8(resp, ',');
1385 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1387 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1389 wpabuf_put_str(resp, usrv->service);
1394 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1397 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1398 /* Response Data: empty */
1401 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1405 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1406 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1408 struct wpa_supplicant *wpa_s = ctx;
1409 const u8 *pos = tlvs;
1410 const u8 *end = tlvs + tlvs_len;
1413 struct wpabuf *resp;
1414 u8 srv_proto, srv_trans_id;
1418 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1420 buf_len = 2 * tlvs_len + 1;
1421 buf = os_malloc(buf_len);
1423 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1424 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1426 freq, MAC2STR(sa), dialog_token, update_indic,
1431 if (wpa_s->p2p_sd_over_ctrl_iface) {
1432 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1433 update_indic, tlvs, tlvs_len);
1434 return; /* to be processed by an external program */
1437 resp = wpabuf_alloc(10000);
1441 while (pos + 1 < end) {
1442 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1443 slen = WPA_GET_LE16(pos);
1445 if (pos + slen > end || slen < 2) {
1446 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1451 tlv_end = pos + slen;
1454 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1456 srv_trans_id = *pos++;
1457 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1460 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1461 pos, tlv_end - pos);
1464 if (wpa_s->force_long_sd) {
1465 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1467 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1468 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1472 switch (srv_proto) {
1473 case P2P_SERV_ALL_SERVICES:
1474 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1475 "for all services");
1476 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1477 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1478 wpa_printf(MSG_DEBUG, "P2P: No service "
1479 "discovery protocols available");
1480 wpas_sd_add_proto_not_avail(
1481 resp, P2P_SERV_ALL_SERVICES,
1485 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1486 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1488 case P2P_SERV_BONJOUR:
1489 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1490 pos, tlv_end - pos);
1493 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1494 pos, tlv_end - pos);
1497 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1498 "protocol %u", srv_proto);
1499 wpas_sd_add_proto_not_avail(resp, srv_proto,
1508 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1509 update_indic, tlvs, tlvs_len);
1511 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1517 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1518 const u8 *tlvs, size_t tlvs_len)
1520 struct wpa_supplicant *wpa_s = ctx;
1521 const u8 *pos = tlvs;
1522 const u8 *end = tlvs + tlvs_len;
1528 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1530 if (tlvs_len > 1500) {
1531 /* TODO: better way for handling this */
1532 wpa_msg_ctrl(wpa_s, MSG_INFO,
1533 P2P_EVENT_SERV_DISC_RESP MACSTR
1534 " %u <long response: %u bytes>",
1535 MAC2STR(sa), update_indic,
1536 (unsigned int) tlvs_len);
1538 buf_len = 2 * tlvs_len + 1;
1539 buf = os_malloc(buf_len);
1541 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1542 wpa_msg_ctrl(wpa_s, MSG_INFO,
1543 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1544 MAC2STR(sa), update_indic, buf);
1550 u8 srv_proto, srv_trans_id, status;
1552 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1553 slen = WPA_GET_LE16(pos);
1555 if (pos + slen > end || slen < 3) {
1556 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1560 tlv_end = pos + slen;
1563 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1565 srv_trans_id = *pos++;
1566 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1569 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1572 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1573 pos, tlv_end - pos);
1578 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1582 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1583 const struct wpabuf *tlvs)
1585 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1586 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1587 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1589 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1593 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1594 u8 version, const char *query)
1596 struct wpabuf *tlvs;
1599 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1602 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1603 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1604 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1605 wpabuf_put_u8(tlvs, version);
1606 wpabuf_put_str(tlvs, query);
1607 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1613 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1615 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1616 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1617 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1619 return p2p_sd_cancel_request(wpa_s->global->p2p,
1620 (void *) (uintptr_t) req);
1624 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1625 const u8 *dst, u8 dialog_token,
1626 const struct wpabuf *resp_tlvs)
1628 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1629 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1633 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1635 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1640 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1642 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1643 wpa_drv_p2p_service_update(wpa_s);
1646 if (wpa_s->global->p2p)
1647 p2p_sd_service_update(wpa_s->global->p2p);
1651 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1653 dl_list_del(&bsrv->list);
1654 wpabuf_free(bsrv->query);
1655 wpabuf_free(bsrv->resp);
1660 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1662 dl_list_del(&usrv->list);
1663 os_free(usrv->service);
1668 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1670 struct p2p_srv_bonjour *bsrv, *bn;
1671 struct p2p_srv_upnp *usrv, *un;
1673 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1674 struct p2p_srv_bonjour, list)
1675 wpas_p2p_srv_bonjour_free(bsrv);
1677 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1678 struct p2p_srv_upnp, list)
1679 wpas_p2p_srv_upnp_free(usrv);
1681 wpas_p2p_sd_service_update(wpa_s);
1685 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1686 struct wpabuf *query, struct wpabuf *resp)
1688 struct p2p_srv_bonjour *bsrv;
1690 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1693 wpabuf_free(bsrv->resp);
1698 bsrv = os_zalloc(sizeof(*bsrv));
1701 bsrv->query = query;
1703 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1705 wpas_p2p_sd_service_update(wpa_s);
1710 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1711 const struct wpabuf *query)
1713 struct p2p_srv_bonjour *bsrv;
1715 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1718 wpas_p2p_srv_bonjour_free(bsrv);
1719 wpas_p2p_sd_service_update(wpa_s);
1724 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1725 const char *service)
1727 struct p2p_srv_upnp *usrv;
1729 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1730 return 0; /* Already listed */
1731 usrv = os_zalloc(sizeof(*usrv));
1734 usrv->version = version;
1735 usrv->service = os_strdup(service);
1736 if (usrv->service == NULL) {
1740 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1742 wpas_p2p_sd_service_update(wpa_s);
1747 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1748 const char *service)
1750 struct p2p_srv_upnp *usrv;
1752 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1755 wpas_p2p_srv_upnp_free(usrv);
1756 wpas_p2p_sd_service_update(wpa_s);
1761 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1762 const u8 *peer, const char *params,
1763 unsigned int generated_pin)
1765 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1766 MAC2STR(peer), generated_pin, params);
1770 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1771 const u8 *peer, const char *params)
1773 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1774 MAC2STR(peer), params);
1778 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1779 const u8 *dev_addr, const u8 *pri_dev_type,
1780 const char *dev_name, u16 supp_config_methods,
1781 u8 dev_capab, u8 group_capab, const u8 *group_id,
1782 size_t group_id_len)
1784 struct wpa_supplicant *wpa_s = ctx;
1785 char devtype[WPS_DEV_TYPE_BUFSIZE];
1787 u8 empty_dev_type[8];
1788 unsigned int generated_pin = 0;
1789 struct wpa_supplicant *group = NULL;
1792 for (group = wpa_s->global->ifaces; group; group = group->next)
1794 struct wpa_ssid *s = group->current_ssid;
1796 s->mode == WPAS_MODE_P2P_GO &&
1797 group_id_len - ETH_ALEN == s->ssid_len &&
1798 os_memcmp(group_id + ETH_ALEN, s->ssid,
1804 if (pri_dev_type == NULL) {
1805 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1806 pri_dev_type = empty_dev_type;
1808 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1809 " pri_dev_type=%s name='%s' config_methods=0x%x "
1810 "dev_capab=0x%x group_capab=0x%x%s%s",
1812 wps_dev_type_bin2str(pri_dev_type, devtype,
1814 dev_name, supp_config_methods, dev_capab, group_capab,
1815 group ? " group=" : "",
1816 group ? group->ifname : "");
1817 params[sizeof(params) - 1] = '\0';
1819 if (config_methods & WPS_CONFIG_DISPLAY) {
1820 generated_pin = wps_generate_pin();
1821 wpas_prov_disc_local_display(wpa_s, peer, params,
1823 } else if (config_methods & WPS_CONFIG_KEYPAD)
1824 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1825 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1826 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1827 "%s", MAC2STR(peer), params);
1829 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1830 P2P_PROV_DISC_SUCCESS,
1831 config_methods, generated_pin);
1835 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1837 struct wpa_supplicant *wpa_s = ctx;
1838 unsigned int generated_pin = 0;
1840 if (wpa_s->pending_pd_before_join &&
1841 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1842 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1843 wpa_s->pending_pd_before_join = 0;
1844 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1845 "join-existing-group operation");
1846 wpas_p2p_join_start(wpa_s);
1850 if (config_methods & WPS_CONFIG_DISPLAY)
1851 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1852 else if (config_methods & WPS_CONFIG_KEYPAD) {
1853 generated_pin = wps_generate_pin();
1854 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1855 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1856 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1859 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1860 P2P_PROV_DISC_SUCCESS,
1861 config_methods, generated_pin);
1865 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1866 enum p2p_prov_disc_status status)
1868 struct wpa_supplicant *wpa_s = ctx;
1870 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1875 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1876 const u8 *go_dev_addr, const u8 *ssid,
1877 size_t ssid_len, int *go, u8 *group_bssid,
1878 int *force_freq, int persistent_group)
1880 struct wpa_supplicant *wpa_s = ctx;
1882 u8 cur_bssid[ETH_ALEN];
1884 struct wpa_supplicant *grp;
1886 if (!persistent_group) {
1887 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1888 " to join an active group", MAC2STR(sa));
1889 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1890 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1892 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1893 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1894 "authorized invitation");
1898 * Do not accept the invitation automatically; notify user and
1901 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1904 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1906 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1907 "running persistent group");
1909 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1913 if (!wpa_s->conf->persistent_reconnect)
1914 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1916 for (s = wpa_s->conf->ssid; s; s = s->next) {
1917 if (s->disabled == 2 &&
1918 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1919 s->ssid_len == ssid_len &&
1920 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1925 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1926 " requested reinvocation of an unknown group",
1928 return P2P_SC_FAIL_UNKNOWN_GROUP;
1931 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1933 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1934 wpa_printf(MSG_DEBUG, "P2P: The only available "
1935 "interface is already in use - reject "
1937 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1939 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1940 } else if (s->mode == WPAS_MODE_P2P_GO) {
1942 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1944 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1945 "interface address for the group");
1946 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1948 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1953 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1954 wpa_s->assoc_freq) {
1955 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1956 "the channel we are already using");
1957 *force_freq = wpa_s->assoc_freq;
1960 res = wpa_drv_shared_freq(wpa_s);
1962 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1963 "with the channel we are already using on a "
1964 "shared interface");
1968 return P2P_SC_SUCCESS;
1972 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1973 const u8 *ssid, size_t ssid_len,
1974 const u8 *go_dev_addr, u8 status,
1977 struct wpa_supplicant *wpa_s = ctx;
1980 for (s = wpa_s->conf->ssid; s; s = s->next) {
1981 if (s->disabled == 2 &&
1982 s->ssid_len == ssid_len &&
1983 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1987 if (status == P2P_SC_SUCCESS) {
1988 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1989 " was accepted; op_freq=%d MHz",
1990 MAC2STR(sa), op_freq);
1992 wpas_p2p_group_add_persistent(
1993 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1995 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1996 wpa_s->p2p_wps_method);
2001 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2002 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2003 " was rejected (status %u)", MAC2STR(sa), status);
2009 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2010 "sa=" MACSTR " go_dev_addr=" MACSTR
2011 " bssid=" MACSTR " unknown-network",
2012 MAC2STR(sa), MAC2STR(go_dev_addr),
2015 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2016 "sa=" MACSTR " go_dev_addr=" MACSTR
2018 MAC2STR(sa), MAC2STR(go_dev_addr));
2023 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2024 " persistent=%d", MAC2STR(sa), s->id);
2028 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2030 struct wpa_supplicant *wpa_s = ctx;
2031 struct wpa_ssid *ssid;
2034 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2035 "status=%d " MACSTR,
2036 status, MAC2STR(bssid));
2038 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2039 "status=%d ", status);
2041 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2043 if (wpa_s->pending_invite_ssid_id == -1)
2044 return; /* Invitation to active group */
2046 if (status != P2P_SC_SUCCESS) {
2047 wpas_p2p_remove_pending_group_interface(wpa_s);
2051 ssid = wpa_config_get_network(wpa_s->conf,
2052 wpa_s->pending_invite_ssid_id);
2054 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2055 "data matching with invitation");
2059 wpas_p2p_group_add_persistent(wpa_s, ssid,
2060 ssid->mode == WPAS_MODE_P2P_GO, 0);
2064 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2065 struct p2p_channels *chan)
2069 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2072 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2073 chan->reg_class[cla].reg_class = 81;
2074 chan->reg_class[cla].channels = 11;
2075 for (i = 0; i < 11; i++)
2076 chan->reg_class[cla].channel[i] = i + 1;
2079 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2082 /* Operating class 115 - 5 GHz, channels 36-48 */
2083 chan->reg_class[cla].reg_class = 115;
2084 chan->reg_class[cla].channels = 4;
2085 chan->reg_class[cla].channel[0] = 36;
2086 chan->reg_class[cla].channel[1] = 40;
2087 chan->reg_class[cla].channel[2] = 44;
2088 chan->reg_class[cla].channel[3] = 48;
2091 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2094 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2095 chan->reg_class[cla].reg_class = 124;
2096 chan->reg_class[cla].channels = 4;
2097 chan->reg_class[cla].channel[0] = 149;
2098 chan->reg_class[cla].channel[1] = 153;
2099 chan->reg_class[cla].channel[2] = 157;
2100 chan->reg_class[cla].channel[3] = 161;
2103 chan->reg_classes = cla;
2108 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2110 enum hostapd_hw_mode mode)
2114 for (i = 0; i < num_modes; i++) {
2115 if (modes[i].mode == mode)
2123 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2127 for (i = 0; i < mode->num_channels; i++) {
2128 if (mode->channels[i].chan == chan) {
2130 *flags = mode->channels[i].flag;
2131 return !(mode->channels[i].flag &
2132 (HOSTAPD_CHAN_DISABLED |
2133 HOSTAPD_CHAN_PASSIVE_SCAN |
2134 HOSTAPD_CHAN_NO_IBSS |
2135 HOSTAPD_CHAN_RADAR));
2143 struct p2p_oper_class_map {
2144 enum hostapd_hw_mode mode;
2149 enum { BW20, BW40PLUS, BW40MINUS } bw;
2152 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2153 struct p2p_channels *chan)
2155 struct hostapd_hw_modes *mode;
2157 struct p2p_oper_class_map op_class[] = {
2158 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2159 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2160 #if 0 /* Do not enable HT40 on 2 GHz for now */
2161 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2162 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2164 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2165 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2166 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2167 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2168 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2169 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2170 { -1, 0, 0, 0, 0, BW20 }
2173 if (wpa_s->hw.modes == NULL) {
2174 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2175 "of all supported channels; assume dualband "
2177 return wpas_p2p_default_channels(wpa_s, chan);
2182 for (op = 0; op_class[op].op_class; op++) {
2183 struct p2p_oper_class_map *o = &op_class[op];
2185 struct p2p_reg_class *reg = NULL;
2187 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2190 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2192 if (!has_channel(mode, ch, &flag))
2194 if (o->bw == BW40MINUS &&
2195 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2196 !has_channel(mode, ch - 4, NULL)))
2198 if (o->bw == BW40PLUS &&
2199 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2200 !has_channel(mode, ch + 4, NULL)))
2203 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2204 "class %u", o->op_class);
2205 reg = &chan->reg_class[cla];
2207 reg->reg_class = o->op_class;
2209 reg->channel[reg->channels] = ch;
2213 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2214 reg->channel, reg->channels);
2218 chan->reg_classes = cla;
2224 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2227 struct wpa_supplicant *wpa_s = ctx;
2229 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2230 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2236 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2240 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2242 struct wpa_supplicant *wpa_s = ctx;
2244 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2245 struct wpa_ssid *ssid = wpa_s->current_ssid;
2248 if (ssid->mode != WPAS_MODE_INFRA)
2250 if (wpa_s->wpa_state != WPA_COMPLETED &&
2251 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2253 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2262 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2263 * @global: Pointer to global data from wpa_supplicant_init()
2264 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2265 * Returns: 0 on success, -1 on failure
2267 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2269 struct p2p_config p2p;
2273 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2279 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2280 struct p2p_params params;
2282 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2283 os_memset(¶ms, 0, sizeof(params));
2284 params.dev_name = wpa_s->conf->device_name;
2285 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2287 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2288 os_memcpy(params.sec_dev_type,
2289 wpa_s->conf->sec_device_type,
2290 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2292 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2298 os_memset(&p2p, 0, sizeof(p2p));
2299 p2p.msg_ctx = wpa_s;
2301 p2p.p2p_scan = wpas_p2p_scan;
2302 p2p.send_action = wpas_send_action;
2303 p2p.send_action_done = wpas_send_action_done;
2304 p2p.go_neg_completed = wpas_go_neg_completed;
2305 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2306 p2p.dev_found = wpas_dev_found;
2307 p2p.dev_lost = wpas_dev_lost;
2308 p2p.start_listen = wpas_start_listen;
2309 p2p.stop_listen = wpas_stop_listen;
2310 p2p.send_probe_resp = wpas_send_probe_resp;
2311 p2p.sd_request = wpas_sd_request;
2312 p2p.sd_response = wpas_sd_response;
2313 p2p.prov_disc_req = wpas_prov_disc_req;
2314 p2p.prov_disc_resp = wpas_prov_disc_resp;
2315 p2p.prov_disc_fail = wpas_prov_disc_fail;
2316 p2p.invitation_process = wpas_invitation_process;
2317 p2p.invitation_received = wpas_invitation_received;
2318 p2p.invitation_result = wpas_invitation_result;
2319 p2p.get_noa = wpas_get_noa;
2320 p2p.go_connected = wpas_go_connected;
2322 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2323 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2324 p2p.dev_name = wpa_s->conf->device_name;
2325 p2p.manufacturer = wpa_s->conf->manufacturer;
2326 p2p.model_name = wpa_s->conf->model_name;
2327 p2p.model_number = wpa_s->conf->model_number;
2328 p2p.serial_number = wpa_s->conf->serial_number;
2330 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2331 p2p.config_methods = wpa_s->wps->config_methods;
2334 if (wpa_s->conf->p2p_listen_reg_class &&
2335 wpa_s->conf->p2p_listen_channel) {
2336 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2337 p2p.channel = wpa_s->conf->p2p_listen_channel;
2341 * Pick one of the social channels randomly as the listen
2344 os_get_random((u8 *) &r, sizeof(r));
2345 p2p.channel = 1 + (r % 3) * 5;
2347 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2349 if (wpa_s->conf->p2p_oper_reg_class &&
2350 wpa_s->conf->p2p_oper_channel) {
2351 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2352 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2353 p2p.cfg_op_channel = 1;
2354 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2355 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2358 p2p.op_reg_class = 81;
2360 * Use random operation channel from (1, 6, 11) if no other
2361 * preference is indicated.
2363 os_get_random((u8 *) &r, sizeof(r));
2364 p2p.op_channel = 1 + (r % 3) * 5;
2365 p2p.cfg_op_channel = 0;
2366 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2367 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2369 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2370 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2371 p2p.country[2] = 0x04;
2373 os_memcpy(p2p.country, "XX\x04", 3);
2375 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2376 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2381 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2384 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2385 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2386 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2388 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2389 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2391 p2p.max_peers = 100;
2393 if (wpa_s->conf->p2p_ssid_postfix) {
2394 p2p.ssid_postfix_len =
2395 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2396 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2397 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2398 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2399 p2p.ssid_postfix_len);
2402 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2404 global->p2p = p2p_init(&p2p);
2405 if (global->p2p == NULL)
2408 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2409 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2411 p2p_add_wps_vendor_extension(
2412 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2420 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2421 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2423 * This function deinitialize per-interface P2P data.
2425 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2427 if (wpa_s->driver && wpa_s->drv_priv)
2428 wpa_drv_probe_req_report(wpa_s, 0);
2430 if (wpa_s->go_params) {
2431 /* Clear any stored provisioning info */
2432 p2p_clear_provisioning_info(
2434 wpa_s->go_params->peer_interface_addr);
2437 os_free(wpa_s->go_params);
2438 wpa_s->go_params = NULL;
2439 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2440 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2441 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2442 wpa_s->p2p_long_listen = 0;
2443 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2444 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2445 wpas_p2p_remove_pending_group_interface(wpa_s);
2447 /* TODO: remove group interface from the driver if this wpa_s instance
2448 * is on top of a P2P group interface */
2453 * wpas_p2p_deinit_global - Deinitialize global P2P module
2454 * @global: Pointer to global data from wpa_supplicant_init()
2456 * This function deinitializes the global (per device) P2P module.
2458 void wpas_p2p_deinit_global(struct wpa_global *global)
2460 struct wpa_supplicant *wpa_s, *tmp;
2463 if (global->p2p == NULL)
2466 /* Remove remaining P2P group interfaces */
2467 wpa_s = global->ifaces;
2469 wpas_p2p_service_flush(wpa_s);
2470 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2471 wpa_s = wpa_s->next;
2473 enum wpa_driver_if_type type;
2474 tmp = global->ifaces;
2477 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2482 ifname = os_strdup(tmp->ifname);
2483 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2484 wpa_supplicant_remove_iface(global, tmp, 0);
2486 wpa_drv_if_remove(wpa_s, type, ifname);
2491 * Deinit GO data on any possibly remaining interface (if main
2492 * interface is used as GO).
2494 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2495 if (wpa_s->ap_iface)
2496 wpas_p2p_group_deinit(wpa_s);
2499 p2p_deinit(global->p2p);
2504 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2506 if (wpa_s->drv_flags &
2507 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2508 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2509 return 1; /* P2P group requires a new interface in every case
2511 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2512 return 0; /* driver does not support concurrent operations */
2513 if (wpa_s->global->ifaces->next)
2514 return 1; /* more that one interface already in use */
2515 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2516 return 1; /* this interface is already in use */
2521 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2522 const u8 *peer_addr,
2523 enum p2p_wps_method wps_method,
2524 int go_intent, const u8 *own_interface_addr,
2525 unsigned int force_freq, int persistent_group)
2527 if (persistent_group && wpa_s->conf->persistent_reconnect)
2528 persistent_group = 2;
2530 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2531 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2532 go_intent, own_interface_addr,
2533 force_freq, persistent_group);
2536 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2537 go_intent, own_interface_addr, force_freq,
2542 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2543 const u8 *peer_addr,
2544 enum p2p_wps_method wps_method,
2545 int go_intent, const u8 *own_interface_addr,
2546 unsigned int force_freq, int persistent_group)
2548 if (persistent_group && wpa_s->conf->persistent_reconnect)
2549 persistent_group = 2;
2551 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2554 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2555 go_intent, own_interface_addr, force_freq,
2560 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2562 wpa_s->p2p_join_scan_count++;
2563 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2564 wpa_s->p2p_join_scan_count);
2565 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2566 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2567 " for join operationg - stop join attempt",
2568 MAC2STR(wpa_s->pending_join_iface_addr));
2569 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2570 wpa_msg(wpa_s->parent, MSG_INFO,
2571 P2P_EVENT_GROUP_FORMATION_FAILURE);
2576 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2578 struct wpa_supplicant *wpa_s = eloop_ctx;
2579 if (!wpa_s->pending_pd_before_join)
2582 * Provision Discovery Response may have been lost - try to connect
2583 * anyway since we do not need any information from this PD.
2585 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2586 "try to connect anyway");
2587 wpas_p2p_join_start(wpa_s);
2591 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2592 struct wpa_scan_results *scan_res)
2594 struct wpa_bss *bss;
2596 u8 iface_addr[ETH_ALEN];
2598 int shared_freq = 0;
2600 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2602 if (wpa_s->global->p2p_disabled)
2605 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2606 scan_res ? (int) scan_res->num : -1);
2609 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2611 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2612 wpa_s->pending_join_iface_addr);
2614 p2p_get_interface_addr(wpa_s->global->p2p,
2615 wpa_s->pending_join_dev_addr,
2617 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2619 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2620 "address for join from " MACSTR " to " MACSTR
2621 " based on newly discovered P2P peer entry",
2622 MAC2STR(wpa_s->pending_join_iface_addr),
2623 MAC2STR(iface_addr));
2624 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2627 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2628 wpa_s->pending_join_iface_addr);
2631 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2632 "from P2P peer table: %d MHz", freq);
2636 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
2637 ((shared_freq = wpa_drv_shared_freq(wpa_s)) > 0) && (shared_freq != freq)) {
2638 wpa_msg(wpa_s->parent, MSG_INFO,
2639 P2P_EVENT_GROUP_FORMATION_FAILURE "reason=FREQ_CONFLICT");
2644 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2647 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2648 "from BSS table: %d MHz", freq);
2653 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2654 "prior to joining an existing group (GO " MACSTR
2656 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2657 wpa_s->pending_pd_before_join = 1;
2659 switch (wpa_s->pending_join_wps_method) {
2660 case WPS_PIN_DISPLAY:
2661 method = WPS_CONFIG_KEYPAD;
2663 case WPS_PIN_KEYPAD:
2664 method = WPS_CONFIG_DISPLAY;
2667 method = WPS_CONFIG_PUSHBUTTON;
2674 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2675 wpa_s->pending_join_dev_addr) ==
2678 * We have already performed provision discovery for
2679 * joining the group. Proceed directly to join
2680 * operation without duplicated provision discovery. */
2681 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2682 "with " MACSTR " already done - proceed to "
2684 MAC2STR(wpa_s->pending_join_dev_addr));
2685 wpa_s->pending_pd_before_join = 0;
2689 if (p2p_prov_disc_req(wpa_s->global->p2p,
2690 wpa_s->pending_join_dev_addr, method, 1,
2692 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2693 "Discovery Request before joining an "
2695 wpa_s->pending_pd_before_join = 0;
2700 * Actual join operation will be started from the Action frame
2701 * TX status callback (if no ACK is received) or when the
2702 * Provision Discovery Response is received. Use a short
2703 * timeout as a backup mechanism should the Provision Discovery
2704 * Response be lost for any reason.
2706 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2708 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2713 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2714 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2715 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2716 wpas_p2p_check_join_scan_limit(wpa_s);
2720 /* Start join operation immediately */
2721 wpas_p2p_join_start(wpa_s);
2725 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2727 struct wpa_supplicant *wpa_s = eloop_ctx;
2729 struct wpa_driver_scan_params params;
2730 struct wpabuf *wps_ie, *ies;
2733 os_memset(¶ms, 0, sizeof(params));
2735 /* P2P Wildcard SSID */
2736 params.num_ssids = 1;
2737 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2738 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2740 wpa_s->wps->dev.p2p = 1;
2741 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2742 WPS_REQ_ENROLLEE, 0, NULL);
2743 if (wps_ie == NULL) {
2744 wpas_p2p_scan_res_join(wpa_s, NULL);
2748 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2749 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
2751 wpabuf_free(wps_ie);
2752 wpas_p2p_scan_res_join(wpa_s, NULL);
2755 wpabuf_put_buf(ies, wps_ie);
2756 wpabuf_free(wps_ie);
2758 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
2760 params.p2p_probe = 1;
2761 params.extra_ies = wpabuf_head(ies);
2762 params.extra_ies_len = wpabuf_len(ies);
2765 * Run a scan to update BSS table and start Provision Discovery once
2766 * the new scan results become available.
2768 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2769 ret = wpa_drv_scan(wpa_s, ¶ms);
2774 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2776 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2777 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2778 wpas_p2p_check_join_scan_limit(wpa_s);
2783 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2784 const u8 *dev_addr, enum p2p_wps_method wps_method)
2786 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2787 MACSTR " dev " MACSTR ")",
2788 MAC2STR(iface_addr), MAC2STR(dev_addr));
2790 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2791 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2792 wpa_s->pending_join_wps_method = wps_method;
2794 /* Make sure we are not running find during connection establishment */
2795 wpas_p2p_stop_find(wpa_s);
2797 wpa_s->p2p_join_scan_count = 0;
2798 wpas_p2p_join_scan(wpa_s, NULL);
2803 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2805 struct wpa_supplicant *group;
2806 struct p2p_go_neg_results res;
2808 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2809 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2812 if (group != wpa_s) {
2813 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2814 sizeof(group->p2p_pin));
2815 group->p2p_wps_method = wpa_s->p2p_wps_method;
2818 group->p2p_in_provisioning = 1;
2820 os_memset(&res, 0, sizeof(res));
2821 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2823 res.wps_method = wpa_s->pending_join_wps_method;
2824 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2825 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
2827 wpa_drv_cancel_remain_on_channel(wpa_s);
2828 wpa_s->off_channel_freq = 0;
2829 wpa_s->roc_waiting_drv_freq = 0;
2831 wpas_start_wps_enrollee(group, &res);
2834 * Allow a longer timeout for join-a-running-group than normal 15
2835 * second group formation timeout since the GO may not have authorized
2836 * our connection yet.
2838 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2839 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2847 * wpas_p2p_connect - Request P2P Group Formation to be started
2848 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2849 * @peer_addr: Address of the peer P2P Device
2850 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2851 * @persistent_group: Whether to create a persistent group
2852 * @join: Whether to join an existing group (as a client) instead of starting
2853 * Group Owner negotiation; @peer_addr is BSSID in that case
2854 * @auth: Whether to only authorize the connection instead of doing that and
2855 * initiating Group Owner negotiation
2856 * @go_intent: GO Intent or -1 to use default
2857 * @freq: Frequency for the group or 0 for auto-selection
2858 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2859 * failure, -2 on failure due to channel not currently available,
2860 * -3 if forced channel is not supported
2862 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2863 const char *pin, enum p2p_wps_method wps_method,
2864 int persistent_group, int join, int auth, int go_intent,
2867 int force_freq = 0, oper_freq = 0;
2870 enum wpa_driver_if_type iftype;
2873 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2877 go_intent = wpa_s->conf->p2p_go_intent;
2880 wpa_s->p2p_long_listen = 0;
2882 wpa_s->p2p_wps_method = wps_method;
2885 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2886 else if (wps_method == WPS_PIN_DISPLAY) {
2887 ret = wps_generate_pin();
2888 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2890 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2893 wpa_s->p2p_pin[0] = '\0';
2896 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2898 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2899 "connect a running group from " MACSTR,
2900 MAC2STR(peer_addr));
2901 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2904 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2905 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2907 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2908 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2911 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2917 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2919 oper_freq = wpa_s->assoc_freq;
2921 oper_freq = wpa_drv_shared_freq(wpa_s);
2927 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2928 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2929 "(%u MHz) is not supported for P2P uses",
2934 if (oper_freq > 0 && freq != oper_freq &&
2935 !(wpa_s->drv_flags &
2936 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2937 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2938 "on %u MHz while connected on another "
2939 "channel (%u MHz)", freq, oper_freq);
2942 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2943 "requested channel (%u MHz)", freq);
2945 } else if (oper_freq > 0 &&
2946 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2947 if (!(wpa_s->drv_flags &
2948 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2949 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2950 "while connected on non-P2P supported "
2951 "channel (%u MHz)", oper_freq);
2954 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2955 "(%u MHz) not available for P2P - try to use "
2956 "another channel", oper_freq);
2958 } else if (oper_freq > 0) {
2959 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2960 "channel we are already using (%u MHz) on another "
2961 "interface", oper_freq);
2962 force_freq = oper_freq;
2965 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2967 if (wpa_s->create_p2p_iface) {
2968 /* Prepare to add a new interface for the group */
2969 iftype = WPA_IF_P2P_GROUP;
2970 if (go_intent == 15)
2971 iftype = WPA_IF_P2P_GO;
2972 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2973 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2974 "interface for the group");
2978 if_addr = wpa_s->pending_interface_addr;
2980 if_addr = wpa_s->own_addr;
2983 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2985 force_freq, persistent_group) < 0)
2990 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2991 go_intent, if_addr, force_freq,
2992 persistent_group) < 0) {
2993 if (wpa_s->create_p2p_iface)
2994 wpas_p2p_remove_pending_group_interface(wpa_s);
3002 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3003 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3004 * @freq: Frequency of the channel in MHz
3005 * @duration: Duration of the stay on the channel in milliseconds
3007 * This callback is called when the driver indicates that it has started the
3008 * requested remain-on-channel duration.
3010 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3011 unsigned int freq, unsigned int duration)
3013 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3015 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3016 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3017 wpa_s->pending_listen_duration);
3018 wpa_s->pending_listen_freq = 0;
3023 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3024 unsigned int timeout)
3026 /* Limit maximum Listen state time based on driver limitation. */
3027 if (timeout > wpa_s->max_remain_on_chan)
3028 timeout = wpa_s->max_remain_on_chan;
3030 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3031 return wpa_drv_p2p_listen(wpa_s, timeout);
3033 return p2p_listen(wpa_s->global->p2p, timeout);
3038 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3039 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3040 * @freq: Frequency of the channel in MHz
3042 * This callback is called when the driver indicates that a remain-on-channel
3043 * operation has been completed, i.e., the duration on the requested channel
3046 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3049 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3050 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3051 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3052 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3054 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3055 return; /* P2P module started a new operation */
3056 if (wpa_s->pending_action_tx)
3058 if (wpa_s->p2p_long_listen > 0)
3059 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3060 if (wpa_s->p2p_long_listen > 0) {
3061 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3062 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3068 * wpas_p2p_group_remove - Remove a P2P group
3069 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3070 * @ifname: Network interface name of the group interface or "*" to remove all
3072 * Returns: 0 on success, -1 on failure
3074 * This function is used to remove a P2P group. This can be used to disconnect
3075 * from a group in which the local end is a P2P Client or to end a P2P Group in
3076 * case the local end is the Group Owner. If a virtual network interface was
3077 * created for this group, that interface will be removed. Otherwise, only the
3078 * configured P2P group network will be removed from the interface.
3080 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3082 struct wpa_global *global = wpa_s->global;
3084 if (os_strcmp(ifname, "*") == 0) {
3085 struct wpa_supplicant *prev;
3086 wpa_s = global->ifaces;
3089 wpa_s = wpa_s->next;
3090 wpas_p2p_disconnect(prev);
3095 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3096 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3100 return wpas_p2p_disconnect(wpa_s);
3104 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3105 struct p2p_go_neg_results *params,
3111 os_memset(params, 0, sizeof(*params));
3112 params->role_go = 1;
3114 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3115 "frequency %d MHz", freq);
3116 params->freq = freq;
3117 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3118 wpa_s->conf->p2p_oper_channel >= 1 &&
3119 wpa_s->conf->p2p_oper_channel <= 11) {
3120 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3121 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3122 "frequency %d MHz", params->freq);
3123 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3124 wpa_s->conf->p2p_oper_reg_class == 124) {
3125 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3126 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3127 "frequency %d MHz", params->freq);
3128 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3129 wpa_s->best_overall_freq > 0 &&
3130 p2p_supported_freq(wpa_s->global->p2p,
3131 wpa_s->best_overall_freq)) {
3132 params->freq = wpa_s->best_overall_freq;
3133 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3134 "channel %d MHz", params->freq);
3135 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3136 wpa_s->best_24_freq > 0 &&
3137 p2p_supported_freq(wpa_s->global->p2p,
3138 wpa_s->best_24_freq)) {
3139 params->freq = wpa_s->best_24_freq;
3140 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3141 "channel %d MHz", params->freq);
3142 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3143 wpa_s->best_5_freq > 0 &&
3144 p2p_supported_freq(wpa_s->global->p2p,
3145 wpa_s->best_5_freq)) {
3146 params->freq = wpa_s->best_5_freq;
3147 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3148 "channel %d MHz", params->freq);
3150 params->freq = 2412;
3151 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3152 "known)", params->freq);
3155 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3156 wpa_s->assoc_freq && !freq) {
3157 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3159 params->freq = wpa_s->assoc_freq;
3162 res = wpa_drv_shared_freq(wpa_s);
3163 if (res > 0 && !freq) {
3164 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3165 "already using on a shared interface");
3167 } else if (res > 0 && freq != res &&
3168 !(wpa_s->drv_flags &
3169 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3170 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3171 "while connected on another channel (%u MHz)",
3180 static struct wpa_supplicant *
3181 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3184 struct wpa_supplicant *group_wpa_s;
3186 if (!wpas_p2p_create_iface(wpa_s))
3189 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3190 WPA_IF_P2P_CLIENT) < 0)
3192 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3193 if (group_wpa_s == NULL) {
3194 wpas_p2p_remove_pending_group_interface(wpa_s);
3203 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3204 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3205 * @persistent_group: Whether to create a persistent group
3206 * @freq: Frequency for the group or 0 to indicate no hardcoding
3207 * Returns: 0 on success, -1 on failure
3209 * This function creates a new P2P group with the local end as the Group Owner,
3210 * i.e., without using Group Owner Negotiation.
3212 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3215 struct p2p_go_neg_results params;
3218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3221 /* Make sure we are not running find during connection establishment */
3222 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3223 wpas_p2p_stop_find(wpa_s);
3226 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3228 if (wpa_s->best_24_freq > 0 &&
3229 p2p_supported_freq(wpa_s->global->p2p,
3230 wpa_s->best_24_freq)) {
3231 freq = wpa_s->best_24_freq;
3232 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3233 "channel: %d MHz", freq);
3235 os_get_random((u8 *) &r, sizeof(r));
3236 freq = 2412 + (r % 3) * 25;
3237 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3238 "channel: %d MHz", freq);
3243 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3245 if (wpa_s->best_5_freq > 0 &&
3246 p2p_supported_freq(wpa_s->global->p2p,
3247 wpa_s->best_5_freq)) {
3248 freq = wpa_s->best_5_freq;
3249 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3250 "channel: %d MHz", freq);
3252 os_get_random((u8 *) &r, sizeof(r));
3253 freq = 5180 + (r % 4) * 20;
3254 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3255 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3256 "5 GHz channel for P2P group");
3259 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3260 "channel: %d MHz", freq);
3264 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3265 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3266 "(%u MHz) is not supported for P2P uses",
3271 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq))
3273 p2p_go_params(wpa_s->global->p2p, ¶ms);
3274 params.persistent_group = persistent_group;
3276 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3279 wpas_start_wps_go(wpa_s, ¶ms, 0);
3285 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3286 struct wpa_ssid *params, int addr_allocated)
3288 struct wpa_ssid *ssid;
3290 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3294 wpa_supplicant_ap_deinit(wpa_s);
3296 ssid = wpa_config_add_network(wpa_s->conf);
3299 wpa_config_set_network_defaults(ssid);
3300 ssid->temporary = 1;
3301 ssid->proto = WPA_PROTO_RSN;
3302 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3303 ssid->group_cipher = WPA_CIPHER_CCMP;
3304 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3305 ssid->ssid = os_malloc(params->ssid_len);
3306 if (ssid->ssid == NULL) {
3307 wpa_config_remove_network(wpa_s->conf, ssid->id);
3310 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3311 ssid->ssid_len = params->ssid_len;
3312 ssid->p2p_group = 1;
3313 ssid->export_keys = 1;
3314 if (params->psk_set) {
3315 os_memcpy(ssid->psk, params->psk, 32);
3318 if (params->passphrase)
3319 ssid->passphrase = os_strdup(params->passphrase);
3321 wpa_supplicant_select_network(wpa_s, ssid);
3323 wpa_s->show_group_started = 1;
3329 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3330 struct wpa_ssid *ssid, int addr_allocated,
3333 struct p2p_go_neg_results params;
3336 if (ssid->disabled != 2 || ssid->ssid == NULL)
3339 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3340 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3341 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3346 /* Make sure we are not running find during connection establishment */
3347 wpas_p2p_stop_find(wpa_s);
3349 if (ssid->mode == WPAS_MODE_INFRA)
3350 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3352 if (ssid->mode != WPAS_MODE_P2P_GO)
3355 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq))
3359 if (ssid->passphrase == NULL ||
3360 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3361 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3365 os_strlcpy(params.passphrase, ssid->passphrase,
3366 sizeof(params.passphrase));
3367 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3368 params.ssid_len = ssid->ssid_len;
3369 params.persistent_group = 1;
3371 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3375 wpas_start_wps_go(wpa_s, ¶ms, 0);
3381 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3382 struct wpabuf *proberesp_ies)
3384 struct wpa_supplicant *wpa_s = ctx;
3385 if (wpa_s->ap_iface) {
3386 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3387 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3388 wpabuf_free(beacon_ies);
3389 wpabuf_free(proberesp_ies);
3393 wpabuf_free(hapd->p2p_beacon_ie);
3394 hapd->p2p_beacon_ie = beacon_ies;
3396 wpabuf_free(hapd->p2p_probe_resp_ie);
3397 hapd->p2p_probe_resp_ie = proberesp_ies;
3399 wpabuf_free(beacon_ies);
3400 wpabuf_free(proberesp_ies);
3402 wpa_supplicant_ap_update_beacon(wpa_s);
3406 static void wpas_p2p_idle_update(void *ctx, int idle)
3408 struct wpa_supplicant *wpa_s = ctx;
3409 if (!wpa_s->ap_iface)
3411 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3413 wpa_printf(MSG_DEBUG,"Calling set group idle time out from idle_update");
3414 wpas_p2p_set_group_idle_timeout(wpa_s);
3417 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3421 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3422 int persistent_group,
3423 int group_formation)
3425 struct p2p_group *group;
3426 struct p2p_group_config *cfg;
3428 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3430 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3433 cfg = os_zalloc(sizeof(*cfg));
3437 if (persistent_group && wpa_s->conf->persistent_reconnect)
3438 cfg->persistent_group = 2;
3439 else if (persistent_group)
3440 cfg->persistent_group = 1;
3441 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3442 if (wpa_s->max_stations &&
3443 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3444 cfg->max_clients = wpa_s->max_stations;
3446 cfg->max_clients = wpa_s->conf->max_num_sta;
3447 cfg->cb_ctx = wpa_s;
3448 cfg->ie_update = wpas_p2p_ie_update;
3449 cfg->idle_update = wpas_p2p_idle_update;
3451 group = p2p_group_init(wpa_s->global->p2p, cfg);
3454 if (!group_formation)
3455 p2p_group_notif_formation_done(group);
3456 wpa_s->p2p_group = group;
3461 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3464 if (!wpa_s->p2p_in_provisioning) {
3465 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3466 "provisioning not in progress");
3470 /* Clear any stored provisioning info */
3471 p2p_clear_provisioning_info(wpa_s->global->p2p, peer_addr);
3473 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3475 if (wpa_s->global->p2p)
3476 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3477 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3478 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3479 wpas_group_formation_completed(wpa_s, 1);
3483 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3484 struct wps_event_fail *fail)
3486 if (!wpa_s->p2p_in_provisioning) {
3487 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3488 "provisioning not in progress");
3492 if (wpa_s->go_params) {
3493 p2p_clear_provisioning_info(
3495 wpa_s->go_params->peer_interface_addr);
3498 wpas_notify_p2p_wps_failed(wpa_s, fail);
3502 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3503 const char *config_method, int join)
3507 if (os_strncmp(config_method, "display", 7) == 0)
3508 config_methods = WPS_CONFIG_DISPLAY;
3509 else if (os_strncmp(config_method, "keypad", 6) == 0)
3510 config_methods = WPS_CONFIG_KEYPAD;
3511 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3512 os_strncmp(config_method, "pushbutton", 10) == 0)
3513 config_methods = WPS_CONFIG_PUSHBUTTON;
3515 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
3519 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3520 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3521 config_methods, join);
3524 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3527 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3528 config_methods, join, 0);
3532 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3535 return p2p_scan_result_text(ies, ies_len, buf, end);
3539 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3541 if (!wpa_s->pending_action_tx)
3544 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3545 "operation request");
3546 wpabuf_free(wpa_s->pending_action_tx);
3547 wpa_s->pending_action_tx = NULL;
3551 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3552 enum p2p_discovery_type type,
3553 unsigned int num_req_dev_types, const u8 *req_dev_types,
3556 wpas_p2p_clear_pending_action_tx(wpa_s);
3557 wpa_s->p2p_long_listen = 0;
3559 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3560 return wpa_drv_p2p_find(wpa_s, timeout, type);
3562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3565 wpa_supplicant_cancel_sched_scan(wpa_s);
3567 return p2p_find(wpa_s->global->p2p, timeout, type,
3568 num_req_dev_types, req_dev_types, dev_id);
3572 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3574 wpas_p2p_clear_pending_action_tx(wpa_s);
3575 wpa_s->p2p_long_listen = 0;
3576 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3577 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3578 wpa_s->p2p_cb_on_scan_complete = 0;
3580 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3581 wpa_drv_p2p_stop_find(wpa_s);
3585 if (wpa_s->global->p2p)
3586 p2p_stop_find(wpa_s->global->p2p);
3588 wpas_p2p_remove_pending_group_interface(wpa_s);
3592 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3594 struct wpa_supplicant *wpa_s = eloop_ctx;
3595 wpa_s->p2p_long_listen = 0;
3599 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3603 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3606 wpa_supplicant_cancel_sched_scan(wpa_s);
3607 wpas_p2p_clear_pending_action_tx(wpa_s);
3611 * This is a request for unlimited Listen state. However, at
3612 * least for now, this is mapped to a Listen state for one
3617 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3618 wpa_s->p2p_long_listen = 0;
3621 * Stop previous find/listen operation to avoid trying to request a new
3622 * remain-on-channel operation while the driver is still running the
3625 if (wpa_s->global->p2p)
3626 p2p_stop_find(wpa_s->global->p2p);
3628 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3629 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3630 wpa_s->p2p_long_listen = timeout * 1000;
3631 eloop_register_timeout(timeout, 0,
3632 wpas_p2p_long_listen_timeout,
3640 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3641 u8 *buf, size_t len, int p2p_group)
3643 struct wpabuf *p2p_ie;
3646 if (wpa_s->global->p2p_disabled)
3648 if (wpa_s->global->p2p == NULL)
3653 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3654 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3656 wpabuf_free(p2p_ie);
3662 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3663 const u8 *dst, const u8 *bssid,
3664 const u8 *ie, size_t ie_len)
3666 if (wpa_s->global->p2p_disabled)
3668 if (wpa_s->global->p2p == NULL)
3671 return p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3676 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3677 const u8 *sa, const u8 *bssid,
3678 u8 category, const u8 *data, size_t len, int freq)
3680 if (wpa_s->global->p2p_disabled)
3682 if (wpa_s->global->p2p == NULL)
3685 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3690 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3692 if (wpa_s->global->p2p_disabled)
3694 if (wpa_s->global->p2p == NULL)
3697 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3701 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3703 p2p_group_deinit(wpa_s->p2p_group);
3704 wpa_s->p2p_group = NULL;
3706 wpa_s->ap_configured_cb = NULL;
3707 wpa_s->ap_configured_cb_ctx = NULL;
3708 wpa_s->ap_configured_cb_data = NULL;
3709 wpa_s->connect_without_scan = NULL;
3713 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3715 wpa_s->p2p_long_listen = 0;
3717 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3718 return wpa_drv_p2p_reject(wpa_s, addr);
3720 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3723 return p2p_reject(wpa_s->global->p2p, addr);
3727 /* Invite to reinvoke a persistent group */
3728 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3729 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3731 enum p2p_invite_role role;
3734 if (ssid->mode == WPAS_MODE_P2P_GO) {
3735 role = P2P_INVITE_ROLE_GO;
3736 if (peer_addr == NULL) {
3737 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3738 "address in invitation command");
3741 if (wpas_p2p_create_iface(wpa_s)) {
3742 if (wpas_p2p_add_group_interface(wpa_s,
3743 WPA_IF_P2P_GO) < 0) {
3744 wpa_printf(MSG_ERROR, "P2P: Failed to "
3745 "allocate a new interface for the "
3749 bssid = wpa_s->pending_interface_addr;
3751 bssid = wpa_s->own_addr;
3753 role = P2P_INVITE_ROLE_CLIENT;
3754 peer_addr = ssid->bssid;
3756 wpa_s->pending_invite_ssid_id = ssid->id;
3758 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3759 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3760 ssid->ssid, ssid->ssid_len,
3763 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3766 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3767 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3771 /* Invite to join an active group */
3772 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3773 const u8 *peer_addr, const u8 *go_dev_addr)
3775 struct wpa_global *global = wpa_s->global;
3776 enum p2p_invite_role role;
3778 struct wpa_ssid *ssid;
3781 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3782 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3785 if (wpa_s == NULL) {
3786 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3790 ssid = wpa_s->current_ssid;
3792 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3797 persistent = ssid->p2p_persistent_group &&
3798 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
3799 ssid->ssid, ssid->ssid_len);
3801 if (ssid->mode == WPAS_MODE_P2P_GO) {
3802 role = P2P_INVITE_ROLE_ACTIVE_GO;
3803 bssid = wpa_s->own_addr;
3804 if (go_dev_addr == NULL)
3805 go_dev_addr = wpa_s->global->p2p_dev_addr;
3807 role = P2P_INVITE_ROLE_CLIENT;
3808 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3809 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3810 "invite to current group");
3813 bssid = wpa_s->bssid;
3814 if (go_dev_addr == NULL &&
3815 !is_zero_ether_addr(wpa_s->go_dev_addr))
3816 go_dev_addr = wpa_s->go_dev_addr;
3818 wpa_s->parent->pending_invite_ssid_id = -1;
3820 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3821 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3822 ssid->ssid, ssid->ssid_len,
3823 go_dev_addr, persistent);
3825 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3828 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3829 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3830 go_dev_addr, persistent);
3834 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3836 struct wpa_ssid *ssid = wpa_s->current_ssid;
3837 const char *ssid_txt;
3838 u8 go_dev_addr[ETH_ALEN];
3839 int network_id = -1;
3843 if (!wpa_s->show_group_started || !ssid)
3846 wpa_s->show_group_started = 0;
3848 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3849 os_memset(go_dev_addr, 0, ETH_ALEN);
3850 if (ssid->bssid_set)
3851 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3852 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3854 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3856 if (wpa_s->global->p2p_group_formation == wpa_s)
3857 wpa_s->global->p2p_group_formation = NULL;
3859 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
3860 (int) wpa_s->assoc_freq;
3861 if (ssid->passphrase == NULL && ssid->psk_set) {
3863 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3864 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3865 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3867 wpa_s->ifname, ssid_txt, freq, psk,
3868 MAC2STR(go_dev_addr),
3869 persistent ? " [PERSISTENT]" : "");
3871 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3872 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3873 "go_dev_addr=" MACSTR "%s",
3874 wpa_s->ifname, ssid_txt, freq,
3875 ssid->passphrase ? ssid->passphrase : "",
3876 MAC2STR(go_dev_addr),
3877 persistent ? " [PERSISTENT]" : "");
3881 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3884 network_id = ssid->id;
3885 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3889 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3890 u32 interval1, u32 duration2, u32 interval2)
3892 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3894 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3897 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3898 wpa_s->current_ssid == NULL ||
3899 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3902 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3903 wpa_s->own_addr, wpa_s->assoc_freq,
3904 duration1, interval1, duration2, interval2);
3908 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3909 unsigned int interval)
3911 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3914 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3917 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3921 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
3923 return wpa_s->current_ssid != NULL &&
3924 wpa_s->current_ssid->p2p_group &&
3925 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
3929 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3931 struct wpa_supplicant *wpa_s = eloop_ctx;
3933 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
3934 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3939 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate %d"
3940 "group",wpa_s->conf->p2p_group_idle);
3941 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3942 wpas_p2p_group_delete(wpa_s);
3946 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3948 unsigned int timeout;
3950 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3951 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3954 timeout = wpa_s->conf->p2p_group_idle;
3955 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
3956 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
3957 timeout = P2P_MAX_CLIENT_IDLE;
3962 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3964 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
3969 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3970 u16 reason_code, const u8 *ie, size_t ie_len)
3972 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3974 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3977 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3981 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3982 u16 reason_code, const u8 *ie, size_t ie_len)
3984 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3986 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3989 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3993 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3995 struct p2p_data *p2p = wpa_s->global->p2p;
4000 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4003 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4004 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4006 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4007 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4010 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4011 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4013 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4014 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4016 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4017 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4018 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4019 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4020 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4023 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4024 p2p_set_sec_dev_types(p2p,
4025 (void *) wpa_s->conf->sec_device_type,
4026 wpa_s->conf->num_sec_device_types);
4028 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4030 p2p_remove_wps_vendor_extensions(p2p);
4031 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4032 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4034 p2p_add_wps_vendor_extension(
4035 p2p, wpa_s->conf->wps_vendor_ext[i]);
4039 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4040 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4042 country[0] = wpa_s->conf->country[0];
4043 country[1] = wpa_s->conf->country[1];
4045 p2p_set_country(p2p, country);
4048 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4049 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4050 wpa_s->conf->p2p_ssid_postfix ?
4051 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4055 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4056 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4058 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4059 u8 reg_class, channel;
4062 if (wpa_s->conf->p2p_listen_reg_class &&
4063 wpa_s->conf->p2p_listen_channel) {
4064 reg_class = wpa_s->conf->p2p_listen_reg_class;
4065 channel = wpa_s->conf->p2p_listen_channel;
4069 * Pick one of the social channels randomly as the
4072 os_get_random((u8 *) &r, sizeof(r));
4073 channel = 1 + (r % 3) * 5;
4075 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4077 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4080 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4081 u8 op_reg_class, op_channel, cfg_op_channel;
4084 if (wpa_s->conf->p2p_oper_reg_class &&
4085 wpa_s->conf->p2p_oper_channel) {
4086 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4087 op_channel = wpa_s->conf->p2p_oper_channel;
4092 * Use random operation channel from (1, 6, 11)
4093 *if no other preference is indicated.
4095 os_get_random((u8 *) &r, sizeof(r));
4096 op_channel = 1 + (r % 3) * 5;
4099 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4102 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4108 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4111 if (!wpa_s->ap_iface)
4113 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4118 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4120 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4122 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4125 wpa_s->global->cross_connection = enabled;
4126 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4129 struct wpa_supplicant *iface;
4131 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4133 if (iface->cross_connect_enabled == 0)
4136 iface->cross_connect_enabled = 0;
4137 iface->cross_connect_in_use = 0;
4138 wpa_msg(iface->parent, MSG_INFO,
4139 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4140 iface->ifname, iface->cross_connect_uplink);
4148 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4150 struct wpa_supplicant *iface;
4152 if (!uplink->global->cross_connection)
4155 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4156 if (!iface->cross_connect_enabled)
4158 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4161 if (iface->ap_iface == NULL)
4163 if (iface->cross_connect_in_use)
4166 iface->cross_connect_in_use = 1;
4167 wpa_msg(iface->parent, MSG_INFO,
4168 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4169 iface->ifname, iface->cross_connect_uplink);
4174 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4176 struct wpa_supplicant *iface;
4178 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4179 if (!iface->cross_connect_enabled)
4181 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4184 if (!iface->cross_connect_in_use)
4187 wpa_msg(iface->parent, MSG_INFO,
4188 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4189 iface->ifname, iface->cross_connect_uplink);
4190 iface->cross_connect_in_use = 0;
4195 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4197 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4198 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4199 wpa_s->cross_connect_disallowed)
4200 wpas_p2p_disable_cross_connect(wpa_s);
4202 wpas_p2p_enable_cross_connect(wpa_s);
4203 if (!wpa_s->ap_iface)
4204 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4208 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4210 wpas_p2p_disable_cross_connect(wpa_s);
4211 if (!wpa_s->ap_iface &&
4212 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4215 wpa_printf(MSG_DEBUG,"Calling set grouple idle_timeout from notif_disconnected");
4216 wpas_p2p_set_group_idle_timeout(wpa_s);
4221 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4223 struct wpa_supplicant *iface;
4225 if (!wpa_s->global->cross_connection)
4228 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4231 if (iface->drv_flags &
4232 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4234 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4237 wpa_s->cross_connect_enabled = 1;
4238 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4239 sizeof(wpa_s->cross_connect_uplink));
4240 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4241 "%s to %s whenever uplink is available",
4242 wpa_s->ifname, wpa_s->cross_connect_uplink);
4244 if (iface->ap_iface || iface->current_ssid == NULL ||
4245 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4246 iface->cross_connect_disallowed ||
4247 iface->wpa_state != WPA_COMPLETED)
4250 wpa_s->cross_connect_in_use = 1;
4251 wpa_msg(wpa_s->parent, MSG_INFO,
4252 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4253 wpa_s->ifname, wpa_s->cross_connect_uplink);
4259 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4261 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4262 !wpa_s->p2p_in_provisioning)
4263 return 0; /* not P2P client operation */
4265 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4267 if (wpa_s != wpa_s->parent)
4268 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4270 if (wpa_s->global->p2p)
4271 p2p_group_formation_failed(wpa_s->global->p2p);
4273 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4274 wpa_s->parent, NULL);
4276 wpas_group_formation_completed(wpa_s, 0);
4281 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4283 struct p2p_channels chan;
4285 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4288 os_memset(&chan, 0, sizeof(chan));
4289 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4290 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4295 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4299 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4301 struct wpa_global *global = wpa_s->global;
4305 if (global->p2p == NULL)
4308 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4310 if (wpa_s->pending_interface_name[0] &&
4311 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4314 peer = p2p_get_go_neg_peer(global->p2p);
4316 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4317 MACSTR, MAC2STR(peer));
4318 p2p_unauthorize(global->p2p, peer);
4321 wpas_p2p_stop_find(wpa_s);
4323 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4324 if (wpa_s == global->p2p_group_formation &&
4325 (wpa_s->p2p_in_provisioning ||
4326 wpa_s->parent->pending_interface_type ==
4327 WPA_IF_P2P_CLIENT)) {
4328 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4329 "formation found - cancelling",
4332 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4333 wpa_s->parent, NULL);
4334 wpas_p2p_group_delete(wpa_s);
4340 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4348 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4350 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4353 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4354 "being available anymore");
4355 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4356 wpas_p2p_group_delete(wpa_s);
4360 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4361 int freq_24, int freq_5, int freq_overall)
4363 struct p2p_data *p2p = wpa_s->global->p2p;
4364 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4366 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4370 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4373 struct p2p_data *p2p = wpa_s->global->p2p;
4375 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4378 if (hwaddr_aton(addr, peer))
4381 return p2p_unauthorize(p2p, peer);
4386 * wpas_p2p_disconnect - Disconnect from a P2P Group
4387 * @wpa_s: Pointer to wpa_supplicant data
4388 * Returns: 0 on success, -1 on failure
4390 * This can be used to disconnect from a group in which the local end is a P2P
4391 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4392 * virtual network interface was created for this group, that interface will be
4393 * removed. Otherwise, only the configured P2P group network will be removed
4394 * from the interface.
4396 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4402 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4403 wpas_p2p_group_delete(wpa_s);
4409 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4411 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4414 return p2p_in_progress(wpa_s->global->p2p);
4418 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4419 struct wpa_ssid *ssid)
4422 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4423 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4424 wpa_s->parent, NULL) > 0) {
4425 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4426 "P2P group network getting removed");
4428 /* Give time for any Pending WPS Frame exchange */
4429 eloop_register_timeout(5, 0, wpas_p2p_group_formation_timeout,
4430 wpa_s->parent, NULL);
4432 wpas_p2p_group_formation_timeout(wpa_s->parent, NULL);
4438 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
4439 const u8 *addr, const u8 *ssid,
4445 for (s = wpa_s->conf->ssid; s; s = s->next) {
4446 if (s->disabled != 2)
4449 (ssid_len != s->ssid_len ||
4450 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4452 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4453 return s; /* peer is GO in the persistent group */
4454 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4456 for (i = 0; i < s->num_p2p_clients; i++) {
4457 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4458 addr, ETH_ALEN) == 0)
4459 return s; /* peer is P2P client in persistent
4468 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4473 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4477 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
4479 struct wpa_supplicant *iface = NULL;
4480 struct p2p_data *p2p = wpa_s->global->p2p;
4482 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4483 if((iface->p2p_group_interface) && (iface->current_ssid) &&
4484 (iface->current_ssid->frequency != freq)) {
4486 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
4487 /* Try to see whether we can move the GO. If it
4488 * is not possible, remove the GO interface
4490 if(wpa_drv_switch_channel(iface, freq) == 0) {
4491 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
4492 iface->current_ssid->frequency = freq;
4497 /* If GO cannot be moved or if the conflicting interface is a
4498 * P2P Client, remove the interface depending up on the connection
4500 if(!wpas_is_p2p_prioritized(wpa_s)) {
4501 /* STA connection has priority over existing
4502 * P2P connection. So remove the interface */
4503 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
4504 "concurrent mode frequency conflict");
4505 iface->removal_reason = P2P_GROUP_REMOVAL_FREQ_CONFLICT;
4506 wpas_p2p_group_delete(iface);
4508 /* Existing connection has the priority. Disable the newly
4509 * selected network and let the application know about it.