3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
31 #include "config_ssid.h"
36 #include "offchannel.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
48 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
49 static struct wpa_supplicant *
50 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
52 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
53 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
54 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
55 const u8 *dev_addr, enum p2p_wps_method wps_method);
56 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
57 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
58 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
59 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
62 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
63 struct wpa_scan_results *scan_res)
67 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
70 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
73 for (i = 0; i < scan_res->num; i++) {
74 struct wpa_scan_res *bss = scan_res->res[i];
75 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
76 bss->freq, bss->level,
77 (const u8 *) (bss + 1),
82 p2p_scan_res_handled(wpa_s->global->p2p);
86 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
87 unsigned int num_req_dev_types,
88 const u8 *req_dev_types)
90 struct wpa_supplicant *wpa_s = ctx;
91 struct wpa_driver_scan_params params;
93 struct wpabuf *wps_ie, *ies;
94 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
97 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
100 os_memset(¶ms, 0, sizeof(params));
102 /* P2P Wildcard SSID */
103 params.num_ssids = 1;
104 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
105 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
107 wpa_s->wps->dev.p2p = 1;
108 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
110 num_req_dev_types, req_dev_types);
114 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
115 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
120 wpabuf_put_buf(ies, wps_ie);
123 p2p_scan_ie(wpa_s->global->p2p, ies);
125 params.p2p_probe = 1;
126 params.extra_ies = wpabuf_head(ies);
127 params.extra_ies_len = wpabuf_len(ies);
130 case P2P_SCAN_SOCIAL:
131 params.freqs = social_channels;
135 case P2P_SCAN_SPECIFIC:
136 social_channels[0] = freq;
137 social_channels[1] = 0;
138 params.freqs = social_channels;
140 case P2P_SCAN_SOCIAL_PLUS_ONE:
141 social_channels[3] = freq;
142 params.freqs = social_channels;
146 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
147 ret = wpa_drv_scan(wpa_s, ¶ms);
152 wpa_s->scan_res_handler = NULL;
158 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
160 switch (p2p_group_interface) {
161 case P2P_GROUP_INTERFACE_PENDING:
162 return WPA_IF_P2P_GROUP;
163 case P2P_GROUP_INTERFACE_GO:
164 return WPA_IF_P2P_GO;
165 case P2P_GROUP_INTERFACE_CLIENT:
166 return WPA_IF_P2P_CLIENT;
169 return WPA_IF_P2P_GROUP;
173 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
175 size_t ssid_len, int *go)
179 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
180 for (s = wpa_s->conf->ssid; s; s = s->next) {
181 if (s->disabled != 0 || !s->p2p_group ||
182 s->ssid_len != ssid_len ||
183 os_memcmp(ssid, s->ssid, ssid_len) != 0)
185 if (s->mode == WPAS_MODE_P2P_GO &&
186 s != wpa_s->current_ssid)
189 *go = s->mode == WPAS_MODE_P2P_GO;
198 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
200 struct wpa_ssid *ssid;
204 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
206 ssid = wpa_s->current_ssid;
209 * The current SSID was not known, but there may still be a
210 * pending P2P group interface waiting for provisioning.
212 ssid = wpa_s->conf->ssid;
214 if (ssid->p2p_group &&
215 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
216 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
221 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
223 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
224 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
225 wpa_s->reassociate = 0;
226 wpa_s->disconnected = 1;
227 wpa_supplicant_deauthenticate(wpa_s,
228 WLAN_REASON_DEAUTH_LEAVING);
232 if (wpa_s->cross_connect_in_use) {
233 wpa_s->cross_connect_in_use = 0;
234 wpa_msg(wpa_s->parent, MSG_INFO,
235 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
236 wpa_s->ifname, wpa_s->cross_connect_uplink);
238 switch (wpa_s->removal_reason) {
239 case P2P_GROUP_REMOVAL_REQUESTED:
240 reason = " reason=REQUESTED";
242 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
243 reason = " reason=IDLE";
245 case P2P_GROUP_REMOVAL_UNAVAILABLE:
246 reason = " reason=UNAVAILABLE";
252 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
253 wpa_s->ifname, gtype, reason);
256 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
258 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
259 struct wpa_global *global;
261 enum wpa_driver_if_type type;
262 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
264 global = wpa_s->global;
265 ifname = os_strdup(wpa_s->ifname);
266 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
267 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
268 wpa_s = global->ifaces;
270 wpa_drv_if_remove(wpa_s, type, ifname);
275 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
276 if (ssid && (ssid->p2p_group ||
277 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
278 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
280 if (ssid == wpa_s->current_ssid) {
281 wpa_sm_set_config(wpa_s->wpa, NULL);
282 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
283 wpa_s->current_ssid = NULL;
286 * Networks objects created during any P2P activities are not
287 * exposed out as they might/will confuse certain non-P2P aware
288 * applications since these network objects won't behave like
291 * Likewise, we don't send out network removed signals for such
294 wpa_config_remove_network(wpa_s->conf, id);
295 wpa_supplicant_clear_status(wpa_s);
297 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
300 wpa_supplicant_ap_deinit(wpa_s);
304 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
306 const u8 *ssid, size_t ssid_len)
314 if (wpa_s->go_params)
315 bssid = wpa_s->go_params->peer_interface_addr;
317 bssid = wpa_s->bssid;
319 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
321 u8 iface_addr[ETH_ALEN];
322 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
324 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
327 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
328 "group is persistent - BSS " MACSTR " not found",
333 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
335 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
336 "group is persistent - BSS " MACSTR
337 " did not include P2P IE", MAC2STR(bssid));
338 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
339 (u8 *) (bss + 1), bss->ie_len);
340 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
341 ((u8 *) bss + 1) + bss->ie_len,
346 group_capab = p2p_get_group_capab(p2p);
347 addr = p2p_get_go_dev_addr(p2p);
348 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
349 "group_capab=0x%x", group_capab);
351 os_memcpy(go_dev_addr, addr, ETH_ALEN);
352 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
355 os_memset(go_dev_addr, 0, ETH_ALEN);
358 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
359 "go_dev_addr=" MACSTR,
360 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
362 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
366 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
367 struct wpa_ssid *ssid,
368 const u8 *go_dev_addr)
373 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
374 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
375 for (s = wpa_s->conf->ssid; s; s = s->next) {
376 if (s->disabled == 2 &&
377 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
378 s->ssid_len == ssid->ssid_len &&
379 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
384 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
386 if (ssid->passphrase && !s->passphrase)
388 else if (ssid->passphrase && s->passphrase &&
389 os_strcmp(ssid->passphrase, s->passphrase) != 0)
392 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
395 s = wpa_config_add_network(wpa_s->conf);
400 * Instead of network_added we emit persistent_group_added
401 * notification. Also to keep the defense checks in
402 * persistent_group obj registration method, we set the
403 * relevant flags in s to designate it as a persistent group.
406 s->p2p_persistent_group = 1;
407 wpas_notify_persistent_group_added(wpa_s, s);
408 wpa_config_set_network_defaults(s);
412 s->p2p_persistent_group = 1;
415 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
416 s->mode = ssid->mode;
417 s->auth_alg = WPA_AUTH_ALG_OPEN;
418 s->key_mgmt = WPA_KEY_MGMT_PSK;
419 s->proto = WPA_PROTO_RSN;
420 s->pairwise_cipher = WPA_CIPHER_CCMP;
422 if (ssid->passphrase) {
423 os_free(s->passphrase);
424 s->passphrase = os_strdup(ssid->passphrase);
428 os_memcpy(s->psk, ssid->psk, 32);
430 if (s->passphrase && !s->psk_set)
431 wpa_config_update_psk(s);
432 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
434 s->ssid = os_malloc(ssid->ssid_len);
437 s->ssid_len = ssid->ssid_len;
438 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
441 #ifndef CONFIG_NO_CONFIG_WRITE
442 if (changed && wpa_s->conf->update_config &&
443 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
444 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
446 #endif /* CONFIG_NO_CONFIG_WRITE */
452 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
455 struct wpa_ssid *ssid;
456 const char *ssid_txt;
459 u8 go_dev_addr[ETH_ALEN];
463 * This callback is likely called for the main interface. Update wpa_s
464 * to use the group interface if a new interface was created for the
467 if (wpa_s->global->p2p_group_formation)
468 wpa_s = wpa_s->global->p2p_group_formation;
469 wpa_s->global->p2p_group_formation = NULL;
470 wpa_s->p2p_in_provisioning = 0;
473 wpa_msg(wpa_s->parent, MSG_INFO,
474 P2P_EVENT_GROUP_FORMATION_FAILURE);
475 wpas_p2p_group_delete(wpa_s);
479 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
481 ssid = wpa_s->current_ssid;
482 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
483 ssid->mode = WPAS_MODE_P2P_GO;
484 p2p_group_notif_formation_done(wpa_s->p2p_group);
485 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
490 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
491 client = ssid->mode == WPAS_MODE_INFRA;
492 if (ssid->mode == WPAS_MODE_P2P_GO) {
493 persistent = ssid->p2p_persistent_group;
494 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
497 persistent = wpas_p2p_persistent_group(wpa_s,
503 client = wpa_s->p2p_group_interface ==
504 P2P_GROUP_INTERFACE_CLIENT;
505 os_memset(go_dev_addr, 0, ETH_ALEN);
508 wpa_s->show_group_started = 0;
511 * Indicate event only after successfully completed 4-way
512 * handshake, i.e., when the interface is ready for data
515 wpa_s->show_group_started = 1;
516 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
518 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
519 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
520 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
522 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
523 MAC2STR(go_dev_addr),
524 persistent ? " [PERSISTENT]" : "");
525 wpas_p2p_cross_connect_setup(wpa_s);
526 wpas_p2p_set_group_idle_timeout(wpa_s);
528 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
529 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
530 "go_dev_addr=" MACSTR "%s",
531 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
532 ssid && ssid->passphrase ? ssid->passphrase : "",
533 MAC2STR(go_dev_addr),
534 persistent ? " [PERSISTENT]" : "");
535 wpas_p2p_cross_connect_setup(wpa_s);
536 wpas_p2p_set_group_idle_timeout(wpa_s);
540 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
542 if (network_id < 0 && ssid)
543 network_id = ssid->id;
545 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
549 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
551 const u8 *dst, const u8 *src,
553 const u8 *data, size_t data_len,
554 enum offchannel_send_action_result
557 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
559 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
561 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
565 case OFFCHANNEL_SEND_ACTION_SUCCESS:
566 res = P2P_SEND_ACTION_SUCCESS;
568 case OFFCHANNEL_SEND_ACTION_NO_ACK:
569 res = P2P_SEND_ACTION_NO_ACK;
571 case OFFCHANNEL_SEND_ACTION_FAILED:
572 res = P2P_SEND_ACTION_FAILED;
576 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
578 if (wpa_s->pending_pd_before_join &&
579 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
580 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
581 wpa_s->pending_pd_before_join = 0;
582 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
583 "join-existing-group operation");
584 wpas_p2p_join_start(wpa_s);
589 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
590 const u8 *src, const u8 *bssid, const u8 *buf,
591 size_t len, unsigned int wait_time)
593 struct wpa_supplicant *wpa_s = ctx;
594 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
596 wpas_p2p_send_action_tx_status);
600 static void wpas_send_action_done(void *ctx)
602 struct wpa_supplicant *wpa_s = ctx;
603 offchannel_send_action_done(wpa_s);
607 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
608 struct p2p_go_neg_results *params)
610 if (wpa_s->go_params == NULL) {
611 wpa_s->go_params = os_malloc(sizeof(*params));
612 if (wpa_s->go_params == NULL)
615 os_memcpy(wpa_s->go_params, params, sizeof(*params));
620 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
621 struct p2p_go_neg_results *res)
623 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
624 MAC2STR(res->peer_interface_addr));
625 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
626 res->ssid, res->ssid_len);
627 wpa_supplicant_ap_deinit(wpa_s);
628 wpas_copy_go_neg_results(wpa_s, res);
629 if (res->wps_method == WPS_PBC)
630 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
632 u16 dev_pw_id = DEV_PW_DEFAULT;
633 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
634 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
635 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
636 wpa_s->p2p_pin, 1, dev_pw_id);
641 static void p2p_go_configured(void *ctx, void *data)
643 struct wpa_supplicant *wpa_s = ctx;
644 struct p2p_go_neg_results *params = data;
645 struct wpa_ssid *ssid;
648 ssid = wpa_s->current_ssid;
649 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
650 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
651 if (wpa_s->global->p2p_group_formation == wpa_s)
652 wpa_s->global->p2p_group_formation = NULL;
653 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
654 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
655 "go_dev_addr=" MACSTR "%s",
657 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
659 params->passphrase ? params->passphrase : "",
660 MAC2STR(wpa_s->parent->own_addr),
661 params->persistent_group ? " [PERSISTENT]" : "");
663 if (params->persistent_group)
664 network_id = wpas_p2p_store_persistent_group(
666 wpa_s->parent->own_addr);
668 network_id = ssid->id;
669 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
670 wpas_p2p_cross_connect_setup(wpa_s);
671 wpas_p2p_set_group_idle_timeout(wpa_s);
675 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
676 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
677 params->peer_interface_addr)) {
678 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
682 if (params->wps_method == WPS_PBC)
683 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
685 else if (wpa_s->p2p_pin[0])
686 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
687 wpa_s->p2p_pin, NULL, 0);
688 os_free(wpa_s->go_params);
689 wpa_s->go_params = NULL;
693 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
694 struct p2p_go_neg_results *params,
697 struct wpa_ssid *ssid;
699 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
702 ssid = wpa_config_add_network(wpa_s->conf);
706 wpa_config_set_network_defaults(ssid);
709 ssid->p2p_persistent_group = params->persistent_group;
710 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
712 ssid->frequency = params->freq;
713 ssid->ssid = os_zalloc(params->ssid_len + 1);
715 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
716 ssid->ssid_len = params->ssid_len;
718 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
719 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
720 ssid->proto = WPA_PROTO_RSN;
721 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
722 ssid->passphrase = os_strdup(params->passphrase);
724 wpa_s->ap_configured_cb = p2p_go_configured;
725 wpa_s->ap_configured_cb_ctx = wpa_s;
726 wpa_s->ap_configured_cb_data = wpa_s->go_params;
727 wpa_s->connect_without_scan = ssid;
728 wpa_s->reassociate = 1;
729 wpa_s->disconnected = 0;
730 wpa_supplicant_req_scan(wpa_s, 0, 0);
734 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
735 const struct wpa_supplicant *src)
737 struct wpa_config *d;
738 const struct wpa_config *s;
743 #define C(n) if (s->n) d->n = os_strdup(s->n)
752 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
753 os_memcpy(d->sec_device_type, s->sec_device_type,
754 sizeof(d->sec_device_type));
755 d->num_sec_device_types = s->num_sec_device_types;
757 d->p2p_group_idle = s->p2p_group_idle;
758 d->p2p_intra_bss = s->p2p_intra_bss;
762 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
763 enum wpa_driver_if_type type)
765 char ifname[120], force_ifname[120];
767 if (wpa_s->pending_interface_name[0]) {
768 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
769 "- skip creation of a new one");
770 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
771 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
772 "unknown?! ifname='%s'",
773 wpa_s->pending_interface_name);
779 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
780 wpa_s->p2p_group_idx);
781 if (os_strlen(ifname) >= IFNAMSIZ &&
782 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
783 /* Try to avoid going over the IFNAMSIZ length limit */
784 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
785 wpa_s->p2p_group_idx);
787 force_ifname[0] = '\0';
789 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
791 wpa_s->p2p_group_idx++;
793 wpa_s->pending_interface_type = type;
794 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
795 wpa_s->pending_interface_addr, NULL) < 0) {
796 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
801 if (force_ifname[0]) {
802 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
804 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
805 sizeof(wpa_s->pending_interface_name));
807 os_strlcpy(wpa_s->pending_interface_name, ifname,
808 sizeof(wpa_s->pending_interface_name));
809 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
810 MACSTR, wpa_s->pending_interface_name,
811 MAC2STR(wpa_s->pending_interface_addr));
817 static void wpas_p2p_remove_pending_group_interface(
818 struct wpa_supplicant *wpa_s)
820 if (!wpa_s->pending_interface_name[0] ||
821 is_zero_ether_addr(wpa_s->pending_interface_addr))
822 return; /* No pending virtual interface */
824 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
825 wpa_s->pending_interface_name);
826 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
827 wpa_s->pending_interface_name);
828 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
829 wpa_s->pending_interface_name[0] = '\0';
833 static struct wpa_supplicant *
834 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
836 struct wpa_interface iface;
837 struct wpa_supplicant *group_wpa_s;
839 if (!wpa_s->pending_interface_name[0]) {
840 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
841 if (!wpas_p2p_create_iface(wpa_s))
844 * Something has forced us to remove the pending interface; try
845 * to create a new one and hope for the best that we will get
846 * the same local address.
848 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
849 WPA_IF_P2P_CLIENT) < 0)
853 os_memset(&iface, 0, sizeof(iface));
854 iface.ifname = wpa_s->pending_interface_name;
855 iface.driver = wpa_s->driver->name;
856 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
857 iface.driver_param = wpa_s->conf->driver_param;
858 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
859 if (group_wpa_s == NULL) {
860 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
861 "wpa_supplicant interface");
864 wpa_s->pending_interface_name[0] = '\0';
865 group_wpa_s->parent = wpa_s;
866 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
867 P2P_GROUP_INTERFACE_CLIENT;
868 wpa_s->global->p2p_group_formation = group_wpa_s;
870 wpas_p2p_clone_config(group_wpa_s, wpa_s);
876 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
879 struct wpa_supplicant *wpa_s = eloop_ctx;
880 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
881 if (wpa_s->global->p2p)
882 p2p_group_formation_failed(wpa_s->global->p2p);
883 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
884 wpa_drv_p2p_group_formation_failed(wpa_s);
885 wpas_group_formation_completed(wpa_s, 0);
889 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
891 struct wpa_supplicant *wpa_s = ctx;
893 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
894 wpa_drv_cancel_remain_on_channel(wpa_s);
895 wpa_s->off_channel_freq = 0;
896 wpa_s->roc_waiting_drv_freq = 0;
900 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
902 wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
903 wpas_p2p_remove_pending_group_interface(wpa_s);
907 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
908 wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
910 if (wpa_s->create_p2p_iface) {
911 struct wpa_supplicant *group_wpa_s =
912 wpas_p2p_init_group_interface(wpa_s, res->role_go);
913 if (group_wpa_s == NULL) {
914 wpas_p2p_remove_pending_group_interface(wpa_s);
917 if (group_wpa_s != wpa_s) {
918 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
919 sizeof(group_wpa_s->p2p_pin));
920 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
922 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
923 wpa_s->pending_interface_name[0] = '\0';
924 group_wpa_s->p2p_in_provisioning = 1;
927 wpas_start_wps_go(group_wpa_s, res, 1);
929 wpas_start_wps_enrollee(group_wpa_s, res);
931 wpa_s->p2p_in_provisioning = 1;
932 wpa_s->global->p2p_group_formation = wpa_s;
935 wpas_start_wps_go(wpa_s, res, 1);
937 wpas_start_wps_enrollee(ctx, res);
940 wpa_s->p2p_long_listen = 0;
941 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
943 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
944 eloop_register_timeout(15 + res->peer_config_timeout / 100,
945 (res->peer_config_timeout % 100) * 10000,
946 wpas_p2p_group_formation_timeout, wpa_s, NULL);
950 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
952 struct wpa_supplicant *wpa_s = ctx;
953 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
954 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
956 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
960 void wpas_dev_found(void *ctx, const u8 *addr,
961 const struct p2p_peer_info *info,
964 struct wpa_supplicant *wpa_s = ctx;
965 char devtype[WPS_DEV_TYPE_BUFSIZE];
967 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
968 " p2p_dev_addr=" MACSTR
969 " pri_dev_type=%s name='%s' config_methods=0x%x "
970 "dev_capab=0x%x group_capab=0x%x",
971 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
972 wps_dev_type_bin2str(info->pri_dev_type, devtype,
974 info->device_name, info->config_methods,
975 info->dev_capab, info->group_capab);
977 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
981 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
983 struct wpa_supplicant *wpa_s = ctx;
985 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
989 static int wpas_start_listen(void *ctx, unsigned int freq,
990 unsigned int duration,
991 const struct wpabuf *probe_resp_ie)
993 struct wpa_supplicant *wpa_s = ctx;
995 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
997 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
998 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
999 "report received Probe Request frames");
1003 wpa_s->pending_listen_freq = freq;
1004 wpa_s->pending_listen_duration = duration;
1006 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1007 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1008 "to remain on channel (%u MHz) for Listen "
1010 wpa_s->pending_listen_freq = 0;
1013 wpa_s->off_channel_freq = 0;
1014 wpa_s->roc_waiting_drv_freq = freq;
1020 static void wpas_stop_listen(void *ctx)
1022 struct wpa_supplicant *wpa_s = ctx;
1023 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1024 wpa_drv_cancel_remain_on_channel(wpa_s);
1025 wpa_s->off_channel_freq = 0;
1026 wpa_s->roc_waiting_drv_freq = 0;
1028 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1029 wpa_drv_probe_req_report(wpa_s, 0);
1033 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1035 struct wpa_supplicant *wpa_s = ctx;
1036 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1040 static struct p2p_srv_bonjour *
1041 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1042 const struct wpabuf *query)
1044 struct p2p_srv_bonjour *bsrv;
1047 len = wpabuf_len(query);
1048 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1049 struct p2p_srv_bonjour, list) {
1050 if (len == wpabuf_len(bsrv->query) &&
1051 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1059 static struct p2p_srv_upnp *
1060 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1061 const char *service)
1063 struct p2p_srv_upnp *usrv;
1065 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1066 struct p2p_srv_upnp, list) {
1067 if (version == usrv->version &&
1068 os_strcmp(service, usrv->service) == 0)
1075 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1080 if (wpabuf_tailroom(resp) < 5)
1083 /* Length (to be filled) */
1084 len_pos = wpabuf_put(resp, 2);
1085 wpabuf_put_u8(resp, srv_proto);
1086 wpabuf_put_u8(resp, srv_trans_id);
1088 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1089 /* Response Data: empty */
1090 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1094 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1095 struct wpabuf *resp, u8 srv_trans_id)
1097 struct p2p_srv_bonjour *bsrv;
1100 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1102 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1103 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1107 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1108 struct p2p_srv_bonjour, list) {
1109 if (wpabuf_tailroom(resp) <
1110 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1112 /* Length (to be filled) */
1113 len_pos = wpabuf_put(resp, 2);
1114 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1115 wpabuf_put_u8(resp, srv_trans_id);
1117 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1118 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1119 wpabuf_head(bsrv->resp),
1120 wpabuf_len(bsrv->resp));
1122 wpabuf_put_buf(resp, bsrv->query); /* Key */
1123 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1124 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1130 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1131 struct wpabuf *resp, u8 srv_trans_id,
1132 const u8 *query, size_t query_len)
1134 struct p2p_srv_bonjour *bsrv;
1138 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1140 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1141 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1142 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1147 if (query_len == 0) {
1148 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1152 if (wpabuf_tailroom(resp) < 5)
1154 /* Length (to be filled) */
1155 len_pos = wpabuf_put(resp, 2);
1156 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1157 wpabuf_put_u8(resp, srv_trans_id);
1159 wpabuf_set(&buf, query, query_len);
1160 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1162 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1166 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1167 /* Response Data: empty */
1168 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1174 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1175 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1176 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1178 if (wpabuf_tailroom(resp) >=
1179 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1181 wpabuf_put_buf(resp, bsrv->query); /* Key */
1182 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1184 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1188 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1189 struct wpabuf *resp, u8 srv_trans_id)
1191 struct p2p_srv_upnp *usrv;
1194 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1196 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1197 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1201 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1202 struct p2p_srv_upnp, list) {
1203 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1206 /* Length (to be filled) */
1207 len_pos = wpabuf_put(resp, 2);
1208 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1209 wpabuf_put_u8(resp, srv_trans_id);
1212 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1214 wpabuf_put_u8(resp, usrv->version);
1215 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1217 wpabuf_put_str(resp, usrv->service);
1218 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1224 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1225 struct wpabuf *resp, u8 srv_trans_id,
1226 const u8 *query, size_t query_len)
1228 struct p2p_srv_upnp *usrv;
1234 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1237 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1238 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1239 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1244 if (query_len == 0) {
1245 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1249 if (wpabuf_tailroom(resp) < 5)
1252 /* Length (to be filled) */
1253 len_pos = wpabuf_put(resp, 2);
1254 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1255 wpabuf_put_u8(resp, srv_trans_id);
1258 str = os_malloc(query_len);
1261 os_memcpy(str, query + 1, query_len - 1);
1262 str[query_len - 1] = '\0';
1264 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1265 struct p2p_srv_upnp, list) {
1266 if (version != usrv->version)
1269 if (os_strcmp(str, "ssdp:all") != 0 &&
1270 os_strstr(usrv->service, str) == NULL)
1273 if (wpabuf_tailroom(resp) < 2)
1277 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1279 wpabuf_put_u8(resp, version);
1281 wpabuf_put_u8(resp, ',');
1285 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1287 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1289 wpabuf_put_str(resp, usrv->service);
1294 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1297 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1298 /* Response Data: empty */
1301 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1305 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1306 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1308 struct wpa_supplicant *wpa_s = ctx;
1309 const u8 *pos = tlvs;
1310 const u8 *end = tlvs + tlvs_len;
1313 struct wpabuf *resp;
1314 u8 srv_proto, srv_trans_id;
1318 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1320 buf_len = 2 * tlvs_len + 1;
1321 buf = os_malloc(buf_len);
1323 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1324 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1326 freq, MAC2STR(sa), dialog_token, update_indic,
1331 if (wpa_s->p2p_sd_over_ctrl_iface)
1332 return; /* to be processed by an external program */
1334 resp = wpabuf_alloc(10000);
1338 while (pos + 1 < end) {
1339 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1340 slen = WPA_GET_LE16(pos);
1342 if (pos + slen > end || slen < 2) {
1343 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1348 tlv_end = pos + slen;
1351 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1353 srv_trans_id = *pos++;
1354 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1357 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1358 pos, tlv_end - pos);
1361 if (wpa_s->force_long_sd) {
1362 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1364 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1365 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1369 switch (srv_proto) {
1370 case P2P_SERV_ALL_SERVICES:
1371 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1372 "for all services");
1373 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1374 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1375 wpa_printf(MSG_DEBUG, "P2P: No service "
1376 "discovery protocols available");
1377 wpas_sd_add_proto_not_avail(
1378 resp, P2P_SERV_ALL_SERVICES,
1382 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1383 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1385 case P2P_SERV_BONJOUR:
1386 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1387 pos, tlv_end - pos);
1390 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1391 pos, tlv_end - pos);
1394 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1395 "protocol %u", srv_proto);
1396 wpas_sd_add_proto_not_avail(resp, srv_proto,
1405 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1406 update_indic, tlvs, tlvs_len);
1408 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1414 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1415 const u8 *tlvs, size_t tlvs_len)
1417 struct wpa_supplicant *wpa_s = ctx;
1418 const u8 *pos = tlvs;
1419 const u8 *end = tlvs + tlvs_len;
1425 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1427 if (tlvs_len > 1500) {
1428 /* TODO: better way for handling this */
1429 wpa_msg_ctrl(wpa_s, MSG_INFO,
1430 P2P_EVENT_SERV_DISC_RESP MACSTR
1431 " %u <long response: %u bytes>",
1432 MAC2STR(sa), update_indic,
1433 (unsigned int) tlvs_len);
1435 buf_len = 2 * tlvs_len + 1;
1436 buf = os_malloc(buf_len);
1438 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1439 wpa_msg_ctrl(wpa_s, MSG_INFO,
1440 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1441 MAC2STR(sa), update_indic, buf);
1447 u8 srv_proto, srv_trans_id, status;
1449 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1450 slen = WPA_GET_LE16(pos);
1452 if (pos + slen > end || slen < 3) {
1453 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1457 tlv_end = pos + slen;
1460 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1462 srv_trans_id = *pos++;
1463 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1466 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1469 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1470 pos, tlv_end - pos);
1475 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1479 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1480 const struct wpabuf *tlvs)
1482 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1483 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1484 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1486 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1490 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1491 u8 version, const char *query)
1493 struct wpabuf *tlvs;
1496 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1499 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1500 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1501 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1502 wpabuf_put_u8(tlvs, version);
1503 wpabuf_put_str(tlvs, query);
1504 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1510 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1512 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1513 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
1514 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1516 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1520 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1521 const u8 *dst, u8 dialog_token,
1522 const struct wpabuf *resp_tlvs)
1524 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1525 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1531 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1536 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1538 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1539 wpa_drv_p2p_service_update(wpa_s);
1542 if (wpa_s->global->p2p)
1543 p2p_sd_service_update(wpa_s->global->p2p);
1547 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1549 dl_list_del(&bsrv->list);
1550 wpabuf_free(bsrv->query);
1551 wpabuf_free(bsrv->resp);
1556 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1558 dl_list_del(&usrv->list);
1559 os_free(usrv->service);
1564 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1566 struct p2p_srv_bonjour *bsrv, *bn;
1567 struct p2p_srv_upnp *usrv, *un;
1569 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1570 struct p2p_srv_bonjour, list)
1571 wpas_p2p_srv_bonjour_free(bsrv);
1573 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1574 struct p2p_srv_upnp, list)
1575 wpas_p2p_srv_upnp_free(usrv);
1577 wpas_p2p_sd_service_update(wpa_s);
1581 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1582 struct wpabuf *query, struct wpabuf *resp)
1584 struct p2p_srv_bonjour *bsrv;
1586 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1589 wpabuf_free(bsrv->resp);
1594 bsrv = os_zalloc(sizeof(*bsrv));
1597 bsrv->query = query;
1599 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1601 wpas_p2p_sd_service_update(wpa_s);
1606 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1607 const struct wpabuf *query)
1609 struct p2p_srv_bonjour *bsrv;
1611 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1614 wpas_p2p_srv_bonjour_free(bsrv);
1615 wpas_p2p_sd_service_update(wpa_s);
1620 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1621 const char *service)
1623 struct p2p_srv_upnp *usrv;
1625 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1626 return 0; /* Already listed */
1627 usrv = os_zalloc(sizeof(*usrv));
1630 usrv->version = version;
1631 usrv->service = os_strdup(service);
1632 if (usrv->service == NULL) {
1636 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1638 wpas_p2p_sd_service_update(wpa_s);
1643 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1644 const char *service)
1646 struct p2p_srv_upnp *usrv;
1648 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1651 wpas_p2p_srv_upnp_free(usrv);
1652 wpas_p2p_sd_service_update(wpa_s);
1657 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1658 const u8 *peer, const char *params,
1659 unsigned int generated_pin)
1661 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1662 MAC2STR(peer), generated_pin, params);
1666 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1667 const u8 *peer, const char *params)
1669 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1670 MAC2STR(peer), params);
1674 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1675 const u8 *dev_addr, const u8 *pri_dev_type,
1676 const char *dev_name, u16 supp_config_methods,
1677 u8 dev_capab, u8 group_capab)
1679 struct wpa_supplicant *wpa_s = ctx;
1680 char devtype[WPS_DEV_TYPE_BUFSIZE];
1682 u8 empty_dev_type[8];
1683 unsigned int generated_pin = 0;
1685 if (pri_dev_type == NULL) {
1686 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1687 pri_dev_type = empty_dev_type;
1689 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1690 " pri_dev_type=%s name='%s' config_methods=0x%x "
1691 "dev_capab=0x%x group_capab=0x%x",
1693 wps_dev_type_bin2str(pri_dev_type, devtype,
1695 dev_name, supp_config_methods, dev_capab, group_capab);
1696 params[sizeof(params) - 1] = '\0';
1698 if (config_methods & WPS_CONFIG_DISPLAY) {
1699 generated_pin = wps_generate_pin();
1700 wpas_prov_disc_local_display(wpa_s, peer, params,
1702 } else if (config_methods & WPS_CONFIG_KEYPAD)
1703 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1704 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1705 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1706 "%s", MAC2STR(peer), params);
1708 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1709 P2P_PROV_DISC_SUCCESS,
1710 config_methods, generated_pin);
1714 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1716 struct wpa_supplicant *wpa_s = ctx;
1717 unsigned int generated_pin = 0;
1719 if (config_methods & WPS_CONFIG_DISPLAY)
1720 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1721 else if (config_methods & WPS_CONFIG_KEYPAD) {
1722 generated_pin = wps_generate_pin();
1723 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1724 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1725 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1728 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1729 P2P_PROV_DISC_SUCCESS,
1730 config_methods, generated_pin);
1732 if (wpa_s->pending_pd_before_join &&
1733 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1734 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1735 wpa_s->pending_pd_before_join = 0;
1736 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1737 "join-existing-group operation");
1738 wpas_p2p_join_start(wpa_s);
1743 void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1744 enum p2p_prov_disc_status status)
1746 struct wpa_supplicant *wpa_s = ctx;
1748 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1753 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1754 const u8 *go_dev_addr, const u8 *ssid,
1755 size_t ssid_len, int *go, u8 *group_bssid,
1756 int *force_freq, int persistent_group)
1758 struct wpa_supplicant *wpa_s = ctx;
1760 u8 cur_bssid[ETH_ALEN];
1762 struct wpa_supplicant *grp;
1764 if (!persistent_group) {
1765 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1766 " to join an active group", MAC2STR(sa));
1767 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1768 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1770 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1771 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1772 "authorized invitation");
1776 * Do not accept the invitation automatically; notify user and
1779 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1782 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1784 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1785 "running persistent group");
1787 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1791 if (!wpa_s->conf->persistent_reconnect)
1792 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1794 for (s = wpa_s->conf->ssid; s; s = s->next) {
1795 if (s->disabled == 2 &&
1796 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1797 s->ssid_len == ssid_len &&
1798 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1803 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1804 " requested reinvocation of an unknown group",
1806 return P2P_SC_FAIL_UNKNOWN_GROUP;
1809 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1811 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1812 wpa_printf(MSG_DEBUG, "P2P: The only available "
1813 "interface is already in use - reject "
1815 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1817 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1818 } else if (s->mode == WPAS_MODE_P2P_GO) {
1820 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1822 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1823 "interface address for the group");
1824 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1826 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1831 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1832 wpa_s->assoc_freq) {
1833 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1834 "the channel we are already using");
1835 *force_freq = wpa_s->assoc_freq;
1838 res = wpa_drv_shared_freq(wpa_s);
1840 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1841 "with the channel we are already using on a "
1842 "shared interface");
1846 return P2P_SC_SUCCESS;
1850 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1851 const u8 *ssid, size_t ssid_len,
1852 const u8 *go_dev_addr, u8 status,
1855 struct wpa_supplicant *wpa_s = ctx;
1858 for (s = wpa_s->conf->ssid; s; s = s->next) {
1859 if (s->disabled == 2 &&
1860 s->ssid_len == ssid_len &&
1861 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1865 if (status == P2P_SC_SUCCESS) {
1866 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1867 " was accepted; op_freq=%d MHz",
1868 MAC2STR(sa), op_freq);
1870 wpas_p2p_group_add_persistent(
1871 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1873 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1874 wpa_s->p2p_wps_method);
1879 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1880 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1881 " was rejected (status %u)", MAC2STR(sa), status);
1887 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1888 "sa=" MACSTR " go_dev_addr=" MACSTR
1889 " bssid=" MACSTR " unknown-network",
1890 MAC2STR(sa), MAC2STR(go_dev_addr),
1893 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1894 "sa=" MACSTR " go_dev_addr=" MACSTR
1896 MAC2STR(sa), MAC2STR(go_dev_addr));
1901 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1902 " persistent=%d", MAC2STR(sa), s->id);
1906 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1908 struct wpa_supplicant *wpa_s = ctx;
1909 struct wpa_ssid *ssid;
1912 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1913 "status=%d " MACSTR,
1914 status, MAC2STR(bssid));
1916 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1917 "status=%d ", status);
1919 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
1921 if (wpa_s->pending_invite_ssid_id == -1)
1922 return; /* Invitation to active group */
1924 if (status != P2P_SC_SUCCESS) {
1925 wpas_p2p_remove_pending_group_interface(wpa_s);
1929 ssid = wpa_config_get_network(wpa_s->conf,
1930 wpa_s->pending_invite_ssid_id);
1932 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1933 "data matching with invitation");
1937 wpas_p2p_group_add_persistent(wpa_s, ssid,
1938 ssid->mode == WPAS_MODE_P2P_GO, 0);
1942 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
1943 struct p2p_channels *chan)
1947 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
1950 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1951 chan->reg_class[cla].reg_class = 81;
1952 chan->reg_class[cla].channels = 11;
1953 for (i = 0; i < 11; i++)
1954 chan->reg_class[cla].channel[i] = i + 1;
1957 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
1960 /* Operating class 115 - 5 GHz, channels 36-48 */
1961 chan->reg_class[cla].reg_class = 115;
1962 chan->reg_class[cla].channels = 4;
1963 chan->reg_class[cla].channel[0] = 36;
1964 chan->reg_class[cla].channel[1] = 40;
1965 chan->reg_class[cla].channel[2] = 44;
1966 chan->reg_class[cla].channel[3] = 48;
1969 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
1972 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1973 chan->reg_class[cla].reg_class = 124;
1974 chan->reg_class[cla].channels = 4;
1975 chan->reg_class[cla].channel[0] = 149;
1976 chan->reg_class[cla].channel[1] = 153;
1977 chan->reg_class[cla].channel[2] = 157;
1978 chan->reg_class[cla].channel[3] = 161;
1981 chan->reg_classes = cla;
1986 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
1988 enum hostapd_hw_mode mode)
1992 for (i = 0; i < num_modes; i++) {
1993 if (modes[i].mode == mode)
2001 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2005 for (i = 0; i < mode->num_channels; i++) {
2006 if (mode->channels[i].chan == chan) {
2008 *flags = mode->channels[i].flag;
2009 return !(mode->channels[i].flag &
2010 (HOSTAPD_CHAN_DISABLED |
2011 HOSTAPD_CHAN_PASSIVE_SCAN |
2012 HOSTAPD_CHAN_NO_IBSS |
2013 HOSTAPD_CHAN_RADAR));
2021 struct p2p_oper_class_map {
2022 enum hostapd_hw_mode mode;
2027 enum { BW20, BW40PLUS, BW40MINUS } bw;
2030 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2031 struct p2p_channels *chan)
2033 struct hostapd_hw_modes *mode;
2035 struct p2p_oper_class_map op_class[] = {
2036 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2037 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2038 #if 0 /* Do not enable HT40 on 2 GHz for now */
2039 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2040 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2042 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2043 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2044 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2045 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2046 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2047 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2048 { -1, 0, 0, 0, 0, BW20 }
2051 if (wpa_s->hw.modes == NULL) {
2052 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2053 "of all supported channels; assume dualband "
2055 return wpas_p2p_default_channels(wpa_s, chan);
2060 for (op = 0; op_class[op].op_class; op++) {
2061 struct p2p_oper_class_map *o = &op_class[op];
2063 struct p2p_reg_class *reg = NULL;
2065 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2068 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2070 if (!has_channel(mode, ch, &flag))
2072 if (o->bw == BW40MINUS &&
2073 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2074 !has_channel(mode, ch - 4, NULL)))
2076 if (o->bw == BW40PLUS &&
2077 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2078 !has_channel(mode, ch + 4, NULL)))
2081 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2082 "class %u", o->op_class);
2083 reg = &chan->reg_class[cla];
2085 reg->reg_class = o->op_class;
2087 reg->channel[reg->channels] = ch;
2091 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2092 reg->channel, reg->channels);
2096 chan->reg_classes = cla;
2102 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2105 struct wpa_supplicant *wpa_s = ctx;
2107 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2108 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2114 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2119 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2120 * @global: Pointer to global data from wpa_supplicant_init()
2121 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2122 * Returns: 0 on success, -1 on failure
2124 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2126 struct p2p_config p2p;
2130 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2133 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2134 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2135 /* Continue anyway; this is not really a fatal error */
2141 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2142 struct p2p_params params;
2144 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2145 os_memset(¶ms, 0, sizeof(params));
2146 params.dev_name = wpa_s->conf->device_name;
2147 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2149 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2150 os_memcpy(params.sec_dev_type,
2151 wpa_s->conf->sec_device_type,
2152 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2154 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2160 os_memset(&p2p, 0, sizeof(p2p));
2161 p2p.msg_ctx = wpa_s;
2163 p2p.p2p_scan = wpas_p2p_scan;
2164 p2p.send_action = wpas_send_action;
2165 p2p.send_action_done = wpas_send_action_done;
2166 p2p.go_neg_completed = wpas_go_neg_completed;
2167 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2168 p2p.dev_found = wpas_dev_found;
2169 p2p.dev_lost = wpas_dev_lost;
2170 p2p.start_listen = wpas_start_listen;
2171 p2p.stop_listen = wpas_stop_listen;
2172 p2p.send_probe_resp = wpas_send_probe_resp;
2173 p2p.sd_request = wpas_sd_request;
2174 p2p.sd_response = wpas_sd_response;
2175 p2p.prov_disc_req = wpas_prov_disc_req;
2176 p2p.prov_disc_resp = wpas_prov_disc_resp;
2177 p2p.prov_disc_fail = wpas_prov_disc_fail;
2178 p2p.invitation_process = wpas_invitation_process;
2179 p2p.invitation_received = wpas_invitation_received;
2180 p2p.invitation_result = wpas_invitation_result;
2181 p2p.get_noa = wpas_get_noa;
2183 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2184 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2185 p2p.dev_name = wpa_s->conf->device_name;
2186 p2p.manufacturer = wpa_s->conf->manufacturer;
2187 p2p.model_name = wpa_s->conf->model_name;
2188 p2p.model_number = wpa_s->conf->model_number;
2189 p2p.serial_number = wpa_s->conf->serial_number;
2191 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2192 p2p.config_methods = wpa_s->wps->config_methods;
2195 if (wpa_s->conf->p2p_listen_reg_class &&
2196 wpa_s->conf->p2p_listen_channel) {
2197 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2198 p2p.channel = wpa_s->conf->p2p_listen_channel;
2202 * Pick one of the social channels randomly as the listen
2205 os_get_random((u8 *) &r, sizeof(r));
2206 p2p.channel = 1 + (r % 3) * 5;
2208 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2210 if (wpa_s->conf->p2p_oper_reg_class &&
2211 wpa_s->conf->p2p_oper_channel) {
2212 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2213 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2214 p2p.cfg_op_channel = 1;
2215 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2216 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2219 p2p.op_reg_class = 81;
2221 * Use random operation channel from (1, 6, 11) if no other
2222 * preference is indicated.
2224 os_get_random((u8 *) &r, sizeof(r));
2225 p2p.op_channel = 1 + (r % 3) * 5;
2226 p2p.cfg_op_channel = 0;
2227 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2228 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2230 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2231 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2232 p2p.country[2] = 0x04;
2234 os_memcpy(p2p.country, "XX\x04", 3);
2236 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2237 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2242 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2245 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2246 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2247 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2249 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2250 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2252 p2p.max_peers = 100;
2254 if (wpa_s->conf->p2p_ssid_postfix) {
2255 p2p.ssid_postfix_len =
2256 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2257 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2258 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2259 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2260 p2p.ssid_postfix_len);
2263 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2265 global->p2p = p2p_init(&p2p);
2266 if (global->p2p == NULL)
2269 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2270 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2272 p2p_add_wps_vendor_extension(
2273 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2281 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2282 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2284 * This function deinitialize per-interface P2P data.
2286 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2288 if (wpa_s->driver && wpa_s->drv_priv)
2289 wpa_drv_probe_req_report(wpa_s, 0);
2290 os_free(wpa_s->go_params);
2291 wpa_s->go_params = NULL;
2292 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2293 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2294 wpa_s->p2p_long_listen = 0;
2295 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2296 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2297 wpas_p2p_remove_pending_group_interface(wpa_s);
2299 /* TODO: remove group interface from the driver if this wpa_s instance
2300 * is on top of a P2P group interface */
2305 * wpas_p2p_deinit_global - Deinitialize global P2P module
2306 * @global: Pointer to global data from wpa_supplicant_init()
2308 * This function deinitializes the global (per device) P2P module.
2310 void wpas_p2p_deinit_global(struct wpa_global *global)
2312 struct wpa_supplicant *wpa_s, *tmp;
2315 if (global->p2p == NULL)
2318 /* Remove remaining P2P group interfaces */
2319 wpa_s = global->ifaces;
2321 wpas_p2p_service_flush(wpa_s);
2322 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2323 wpa_s = wpa_s->next;
2325 enum wpa_driver_if_type type;
2326 tmp = global->ifaces;
2329 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2334 ifname = os_strdup(tmp->ifname);
2335 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2336 wpa_supplicant_remove_iface(global, tmp);
2338 wpa_drv_if_remove(wpa_s, type, ifname);
2343 * Deinit GO data on any possibly remaining interface (if main
2344 * interface is used as GO).
2346 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2347 if (wpa_s->ap_iface)
2348 wpas_p2p_group_deinit(wpa_s);
2351 p2p_deinit(global->p2p);
2356 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2358 if (wpa_s->drv_flags &
2359 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2360 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2361 return 1; /* P2P group requires a new interface in every case
2363 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2364 return 0; /* driver does not support concurrent operations */
2365 if (wpa_s->global->ifaces->next)
2366 return 1; /* more that one interface already in use */
2367 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2368 return 1; /* this interface is already in use */
2373 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2374 const u8 *peer_addr,
2375 enum p2p_wps_method wps_method,
2376 int go_intent, const u8 *own_interface_addr,
2377 unsigned int force_freq, int persistent_group)
2379 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2380 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2381 go_intent, own_interface_addr,
2382 force_freq, persistent_group);
2385 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2386 go_intent, own_interface_addr, force_freq,
2391 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2392 const u8 *peer_addr,
2393 enum p2p_wps_method wps_method,
2394 int go_intent, const u8 *own_interface_addr,
2395 unsigned int force_freq, int persistent_group)
2397 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2400 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2401 go_intent, own_interface_addr, force_freq,
2406 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2408 wpa_s->p2p_join_scan_count++;
2409 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2410 wpa_s->p2p_join_scan_count);
2411 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2412 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2413 " for join operationg - stop join attempt",
2414 MAC2STR(wpa_s->pending_join_iface_addr));
2415 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2416 wpa_msg(wpa_s->parent, MSG_INFO,
2417 P2P_EVENT_GROUP_FORMATION_FAILURE);
2422 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2423 struct wpa_scan_results *scan_res)
2425 struct wpa_bss *bss;
2427 u8 iface_addr[ETH_ALEN];
2429 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2431 if (wpa_s->global->p2p_disabled)
2434 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2435 scan_res ? (int) scan_res->num : -1);
2438 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2440 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2441 wpa_s->pending_join_iface_addr);
2443 p2p_get_interface_addr(wpa_s->global->p2p,
2444 wpa_s->pending_join_dev_addr,
2446 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2448 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2449 "address for join from " MACSTR " to " MACSTR
2450 " based on newly discovered P2P peer entry",
2451 MAC2STR(wpa_s->pending_join_iface_addr),
2452 MAC2STR(iface_addr));
2453 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2456 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2457 wpa_s->pending_join_iface_addr);
2460 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2461 "from P2P peer table: %d MHz", freq);
2463 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2466 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2467 "from BSS table: %d MHz", freq);
2472 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2473 "prior to joining an existing group (GO " MACSTR
2475 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2476 wpa_s->pending_pd_before_join = 1;
2478 switch (wpa_s->pending_join_wps_method) {
2479 case WPS_PIN_DISPLAY:
2480 method = WPS_CONFIG_KEYPAD;
2482 case WPS_PIN_KEYPAD:
2483 method = WPS_CONFIG_DISPLAY;
2486 method = WPS_CONFIG_PUSHBUTTON;
2493 if (p2p_prov_disc_req(wpa_s->global->p2p,
2494 wpa_s->pending_join_dev_addr, method, 1)
2496 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2497 "Discovery Request before joining an "
2499 wpa_s->pending_pd_before_join = 0;
2504 * Actual join operation will be started from the Action frame
2505 * TX status callback.
2510 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2511 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2512 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2513 wpas_p2p_check_join_scan_limit(wpa_s);
2517 /* Start join operation immediately */
2518 wpas_p2p_join_start(wpa_s);
2522 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2524 struct wpa_supplicant *wpa_s = eloop_ctx;
2526 struct wpa_driver_scan_params params;
2527 struct wpabuf *wps_ie, *ies;
2530 os_memset(¶ms, 0, sizeof(params));
2532 /* P2P Wildcard SSID */
2533 params.num_ssids = 1;
2534 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2535 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2537 wpa_s->wps->dev.p2p = 1;
2538 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2539 WPS_REQ_ENROLLEE, 0, NULL);
2540 if (wps_ie == NULL) {
2541 wpas_p2p_scan_res_join(wpa_s, NULL);
2545 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2546 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
2548 wpabuf_free(wps_ie);
2549 wpas_p2p_scan_res_join(wpa_s, NULL);
2552 wpabuf_put_buf(ies, wps_ie);
2553 wpabuf_free(wps_ie);
2555 p2p_scan_ie(wpa_s->global->p2p, ies);
2557 params.p2p_probe = 1;
2558 params.extra_ies = wpabuf_head(ies);
2559 params.extra_ies_len = wpabuf_len(ies);
2562 * Run a scan to update BSS table and start Provision Discovery once
2563 * the new scan results become available.
2565 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2566 ret = wpa_drv_scan(wpa_s, ¶ms);
2571 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2573 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2574 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2575 wpas_p2p_check_join_scan_limit(wpa_s);
2580 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2581 const u8 *dev_addr, enum p2p_wps_method wps_method)
2583 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2584 MACSTR " dev " MACSTR ")",
2585 MAC2STR(iface_addr), MAC2STR(dev_addr));
2587 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2588 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2589 wpa_s->pending_join_wps_method = wps_method;
2591 /* Make sure we are not running find during connection establishment */
2592 wpas_p2p_stop_find(wpa_s);
2594 wpa_s->p2p_join_scan_count = 0;
2595 wpas_p2p_join_scan(wpa_s, NULL);
2600 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2602 struct wpa_supplicant *group;
2603 struct p2p_go_neg_results res;
2605 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2608 if (group != wpa_s) {
2609 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2610 sizeof(group->p2p_pin));
2611 group->p2p_wps_method = wpa_s->p2p_wps_method;
2614 group->p2p_in_provisioning = 1;
2616 os_memset(&res, 0, sizeof(res));
2617 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2619 res.wps_method = wpa_s->pending_join_wps_method;
2620 wpas_start_wps_enrollee(group, &res);
2623 * Allow a longer timeout for join-a-running-group than normal 15
2624 * second group formation timeout since the GO may not have authorized
2625 * our connection yet.
2627 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2628 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2636 * wpas_p2p_connect - Request P2P Group Formation to be started
2637 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2638 * @peer_addr: Address of the peer P2P Device
2639 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2640 * @persistent_group: Whether to create a persistent group
2641 * @join: Whether to join an existing group (as a client) instead of starting
2642 * Group Owner negotiation; @peer_addr is BSSID in that case
2643 * @auth: Whether to only authorize the connection instead of doing that and
2644 * initiating Group Owner negotiation
2645 * @go_intent: GO Intent or -1 to use default
2646 * @freq: Frequency for the group or 0 for auto-selection
2647 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2648 * failure, -2 on failure due to channel not currently available,
2649 * -3 if forced channel is not supported
2651 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2652 const char *pin, enum p2p_wps_method wps_method,
2653 int persistent_group, int join, int auth, int go_intent,
2656 int force_freq = 0, oper_freq = 0;
2659 enum wpa_driver_if_type iftype;
2661 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2665 go_intent = wpa_s->conf->p2p_go_intent;
2668 wpa_s->p2p_long_listen = 0;
2670 wpa_s->p2p_wps_method = wps_method;
2673 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2674 else if (wps_method == WPS_PIN_DISPLAY) {
2675 ret = wps_generate_pin();
2676 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2678 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2681 wpa_s->p2p_pin[0] = '\0';
2684 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2686 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2687 "connect a running group from " MACSTR,
2688 MAC2STR(peer_addr));
2689 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2692 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2693 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2695 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2696 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2699 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2705 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2707 oper_freq = wpa_s->assoc_freq;
2709 oper_freq = wpa_drv_shared_freq(wpa_s);
2715 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2716 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2717 "(%u MHz) is not supported for P2P uses",
2722 if (oper_freq > 0 && freq != oper_freq &&
2723 !(wpa_s->drv_flags &
2724 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2725 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2726 "on %u MHz while connected on another "
2727 "channel (%u MHz)", freq, oper_freq);
2730 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2731 "requested channel (%u MHz)", freq);
2733 } else if (oper_freq > 0 &&
2734 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2735 if (!(wpa_s->drv_flags &
2736 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2737 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2738 "while connected on non-P2P supported "
2739 "channel (%u MHz)", oper_freq);
2742 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2743 "(%u MHz) not available for P2P - try to use "
2744 "another channel", oper_freq);
2746 } else if (oper_freq > 0) {
2747 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2748 "channel we are already using (%u MHz) on another "
2749 "interface", oper_freq);
2750 force_freq = oper_freq;
2753 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2755 if (!wpa_s->create_p2p_iface) {
2757 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2758 go_intent, wpa_s->own_addr,
2759 force_freq, persistent_group)
2764 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2765 go_intent, wpa_s->own_addr,
2766 force_freq, persistent_group) < 0)
2771 /* Prepare to add a new interface for the group */
2772 iftype = WPA_IF_P2P_GROUP;
2773 if (go_intent == 15)
2774 iftype = WPA_IF_P2P_GO;
2775 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2776 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2777 "interface for the group");
2782 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2784 wpa_s->pending_interface_addr,
2785 force_freq, persistent_group) < 0)
2789 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2790 wpa_s->pending_interface_addr,
2791 force_freq, persistent_group) < 0) {
2792 wpas_p2p_remove_pending_group_interface(wpa_s);
2800 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2801 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2802 * @freq: Frequency of the channel in MHz
2803 * @duration: Duration of the stay on the channel in milliseconds
2805 * This callback is called when the driver indicates that it has started the
2806 * requested remain-on-channel duration.
2808 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2809 unsigned int freq, unsigned int duration)
2811 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2813 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2814 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2815 wpa_s->pending_listen_duration);
2816 wpa_s->pending_listen_freq = 0;
2821 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2822 unsigned int timeout)
2824 /* Limit maximum Listen state time based on driver limitation. */
2825 if (timeout > wpa_s->max_remain_on_chan)
2826 timeout = wpa_s->max_remain_on_chan;
2828 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2829 return wpa_drv_p2p_listen(wpa_s, timeout);
2831 return p2p_listen(wpa_s->global->p2p, timeout);
2836 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2837 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2838 * @freq: Frequency of the channel in MHz
2840 * This callback is called when the driver indicates that a remain-on-channel
2841 * operation has been completed, i.e., the duration on the requested channel
2844 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2847 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
2848 "(p2p_long_listen=%d ms pending_action_tx=%p)",
2849 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
2850 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2852 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2853 return; /* P2P module started a new operation */
2854 if (wpa_s->pending_action_tx)
2856 if (wpa_s->p2p_long_listen > 0)
2857 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
2858 if (wpa_s->p2p_long_listen > 0) {
2859 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2860 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
2866 * wpas_p2p_group_remove - Remove a P2P group
2867 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2868 * @ifname: Network interface name of the group interface or "*" to remove all
2870 * Returns: 0 on success, -1 on failure
2872 * This function is used to remove a P2P group. This can be used to disconnect
2873 * from a group in which the local end is a P2P Client or to end a P2P Group in
2874 * case the local end is the Group Owner. If a virtual network interface was
2875 * created for this group, that interface will be removed. Otherwise, only the
2876 * configured P2P group network will be removed from the interface.
2878 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2880 struct wpa_global *global = wpa_s->global;
2882 if (os_strcmp(ifname, "*") == 0) {
2883 struct wpa_supplicant *prev;
2884 wpa_s = global->ifaces;
2887 wpa_s = wpa_s->next;
2888 wpas_p2p_disconnect(prev);
2893 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2894 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2898 return wpas_p2p_disconnect(wpa_s);
2902 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2903 struct p2p_go_neg_results *params,
2909 os_memset(params, 0, sizeof(*params));
2910 params->role_go = 1;
2912 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
2913 "frequency %d MHz", freq);
2914 params->freq = freq;
2915 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2916 wpa_s->conf->p2p_oper_channel >= 1 &&
2917 wpa_s->conf->p2p_oper_channel <= 11) {
2918 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2919 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
2920 "frequency %d MHz", params->freq);
2921 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2922 wpa_s->conf->p2p_oper_reg_class == 124) {
2923 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2924 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
2925 "frequency %d MHz", params->freq);
2926 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2927 wpa_s->best_overall_freq > 0 &&
2928 p2p_supported_freq(wpa_s->global->p2p,
2929 wpa_s->best_overall_freq)) {
2930 params->freq = wpa_s->best_overall_freq;
2931 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
2932 "channel %d MHz", params->freq);
2933 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2934 wpa_s->best_24_freq > 0 &&
2935 p2p_supported_freq(wpa_s->global->p2p,
2936 wpa_s->best_24_freq)) {
2937 params->freq = wpa_s->best_24_freq;
2938 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
2939 "channel %d MHz", params->freq);
2940 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2941 wpa_s->best_5_freq > 0 &&
2942 p2p_supported_freq(wpa_s->global->p2p,
2943 wpa_s->best_5_freq)) {
2944 params->freq = wpa_s->best_5_freq;
2945 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
2946 "channel %d MHz", params->freq);
2948 params->freq = 2412;
2949 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
2950 "known)", params->freq);
2953 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2954 wpa_s->assoc_freq && !freq) {
2955 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2957 params->freq = wpa_s->assoc_freq;
2960 res = wpa_drv_shared_freq(wpa_s);
2961 if (res > 0 && !freq) {
2962 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2963 "already using on a shared interface");
2969 static struct wpa_supplicant *
2970 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2973 struct wpa_supplicant *group_wpa_s;
2975 if (!wpas_p2p_create_iface(wpa_s))
2978 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2979 WPA_IF_P2P_CLIENT) < 0)
2981 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2982 if (group_wpa_s == NULL) {
2983 wpas_p2p_remove_pending_group_interface(wpa_s);
2992 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2993 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2994 * @persistent_group: Whether to create a persistent group
2995 * @freq: Frequency for the group or 0 to indicate no hardcoding
2996 * Returns: 0 on success, -1 on failure
2998 * This function creates a new P2P group with the local end as the Group Owner,
2999 * i.e., without using Group Owner Negotiation.
3001 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3004 struct p2p_go_neg_results params;
3007 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3011 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3013 if (wpa_s->best_24_freq > 0 &&
3014 p2p_supported_freq(wpa_s->global->p2p,
3015 wpa_s->best_24_freq)) {
3016 freq = wpa_s->best_24_freq;
3017 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3018 "channel: %d MHz", freq);
3020 os_get_random((u8 *) &r, sizeof(r));
3021 freq = 2412 + (r % 3) * 25;
3022 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3023 "channel: %d MHz", freq);
3028 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3030 if (wpa_s->best_5_freq > 0 &&
3031 p2p_supported_freq(wpa_s->global->p2p,
3032 wpa_s->best_5_freq)) {
3033 freq = wpa_s->best_5_freq;
3034 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3035 "channel: %d MHz", freq);
3037 os_get_random((u8 *) &r, sizeof(r));
3038 freq = 5180 + (r % 4) * 20;
3039 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3040 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3041 "5 GHz channel for P2P group");
3044 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3045 "channel: %d MHz", freq);
3049 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3050 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3051 "(%u MHz) is not supported for P2P uses",
3056 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3057 p2p_go_params(wpa_s->global->p2p, ¶ms);
3058 params.persistent_group = persistent_group;
3060 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3063 wpas_start_wps_go(wpa_s, ¶ms, 0);
3069 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3070 struct wpa_ssid *params, int addr_allocated)
3072 struct wpa_ssid *ssid;
3074 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3078 wpa_supplicant_ap_deinit(wpa_s);
3080 ssid = wpa_config_add_network(wpa_s->conf);
3083 wpa_config_set_network_defaults(ssid);
3084 ssid->temporary = 1;
3085 ssid->proto = WPA_PROTO_RSN;
3086 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3087 ssid->group_cipher = WPA_CIPHER_CCMP;
3088 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3089 ssid->ssid = os_malloc(params->ssid_len);
3090 if (ssid->ssid == NULL) {
3091 wpa_config_remove_network(wpa_s->conf, ssid->id);
3094 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3095 ssid->ssid_len = params->ssid_len;
3096 ssid->p2p_group = 1;
3097 ssid->export_keys = 1;
3098 if (params->psk_set) {
3099 os_memcpy(ssid->psk, params->psk, 32);
3102 if (params->passphrase)
3103 ssid->passphrase = os_strdup(params->passphrase);
3105 wpa_supplicant_select_network(wpa_s, ssid);
3107 wpa_s->show_group_started = 1;
3113 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3114 struct wpa_ssid *ssid, int addr_allocated,
3117 struct p2p_go_neg_results params;
3120 if (ssid->disabled != 2 || ssid->ssid == NULL)
3123 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3124 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3125 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3130 /* Make sure we are not running find during connection establishment */
3131 wpas_p2p_stop_find(wpa_s);
3133 if (ssid->mode == WPAS_MODE_INFRA)
3134 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3136 if (ssid->mode != WPAS_MODE_P2P_GO)
3139 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3142 if (ssid->passphrase == NULL ||
3143 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3144 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3148 os_strlcpy(params.passphrase, ssid->passphrase,
3149 sizeof(params.passphrase));
3150 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3151 params.ssid_len = ssid->ssid_len;
3152 params.persistent_group = 1;
3154 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3158 wpas_start_wps_go(wpa_s, ¶ms, 0);
3164 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3165 struct wpabuf *proberesp_ies)
3167 struct wpa_supplicant *wpa_s = ctx;
3168 if (wpa_s->ap_iface) {
3169 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3171 wpabuf_free(hapd->p2p_beacon_ie);
3172 hapd->p2p_beacon_ie = beacon_ies;
3174 wpabuf_free(hapd->p2p_probe_resp_ie);
3175 hapd->p2p_probe_resp_ie = proberesp_ies;
3177 wpabuf_free(beacon_ies);
3178 wpabuf_free(proberesp_ies);
3180 wpa_supplicant_ap_update_beacon(wpa_s);
3184 static void wpas_p2p_idle_update(void *ctx, int idle)
3186 struct wpa_supplicant *wpa_s = ctx;
3187 if (!wpa_s->ap_iface)
3189 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3191 wpas_p2p_set_group_idle_timeout(wpa_s);
3193 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3197 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3198 int persistent_group,
3199 int group_formation)
3201 struct p2p_group *group;
3202 struct p2p_group_config *cfg;
3204 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3209 cfg = os_zalloc(sizeof(*cfg));
3213 cfg->persistent_group = persistent_group;
3214 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3215 if (wpa_s->max_stations &&
3216 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3217 cfg->max_clients = wpa_s->max_stations;
3219 cfg->max_clients = wpa_s->conf->max_num_sta;
3220 cfg->cb_ctx = wpa_s;
3221 cfg->ie_update = wpas_p2p_ie_update;
3222 cfg->idle_update = wpas_p2p_idle_update;
3224 group = p2p_group_init(wpa_s->global->p2p, cfg);
3227 if (!group_formation)
3228 p2p_group_notif_formation_done(group);
3229 wpa_s->p2p_group = group;
3234 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3237 if (!wpa_s->p2p_in_provisioning) {
3238 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3239 "provisioning not in progress");
3243 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3245 if (wpa_s->global->p2p)
3246 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3247 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3248 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3249 wpas_group_formation_completed(wpa_s, 1);
3253 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3254 struct wps_event_fail *fail)
3256 if (!wpa_s->p2p_in_provisioning) {
3257 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3258 "provisioning not in progress");
3261 wpas_notify_p2p_wps_failed(wpa_s, fail);
3265 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3266 const char *config_method)
3270 if (os_strcmp(config_method, "display") == 0)
3271 config_methods = WPS_CONFIG_DISPLAY;
3272 else if (os_strcmp(config_method, "keypad") == 0)
3273 config_methods = WPS_CONFIG_KEYPAD;
3274 else if (os_strcmp(config_method, "pbc") == 0 ||
3275 os_strcmp(config_method, "pushbutton") == 0)
3276 config_methods = WPS_CONFIG_PUSHBUTTON;
3280 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3281 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3285 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3288 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3293 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3296 return p2p_scan_result_text(ies, ies_len, buf, end);
3300 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3302 if (!wpa_s->pending_action_tx)
3305 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3306 "operation request");
3307 wpabuf_free(wpa_s->pending_action_tx);
3308 wpa_s->pending_action_tx = NULL;
3312 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3313 enum p2p_discovery_type type,
3314 unsigned int num_req_dev_types, const u8 *req_dev_types)
3316 wpas_p2p_clear_pending_action_tx(wpa_s);
3317 wpa_s->p2p_long_listen = 0;
3319 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3320 return wpa_drv_p2p_find(wpa_s, timeout, type);
3322 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3325 return p2p_find(wpa_s->global->p2p, timeout, type,
3326 num_req_dev_types, req_dev_types);
3330 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3332 wpas_p2p_clear_pending_action_tx(wpa_s);
3333 wpa_s->p2p_long_listen = 0;
3334 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3335 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3337 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3338 wpa_drv_p2p_stop_find(wpa_s);
3342 if (wpa_s->global->p2p)
3343 p2p_stop_find(wpa_s->global->p2p);
3345 wpas_p2p_remove_pending_group_interface(wpa_s);
3349 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3351 struct wpa_supplicant *wpa_s = eloop_ctx;
3352 wpa_s->p2p_long_listen = 0;
3356 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3360 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3363 wpas_p2p_clear_pending_action_tx(wpa_s);
3367 * This is a request for unlimited Listen state. However, at
3368 * least for now, this is mapped to a Listen state for one
3373 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3374 wpa_s->p2p_long_listen = 0;
3376 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3377 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3378 wpa_s->p2p_long_listen = timeout * 1000;
3379 eloop_register_timeout(timeout, 0,
3380 wpas_p2p_long_listen_timeout,
3388 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3389 u8 *buf, size_t len, int p2p_group)
3391 struct wpabuf *p2p_ie;
3394 if (wpa_s->global->p2p_disabled)
3396 if (wpa_s->global->p2p == NULL)
3401 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3402 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3404 wpabuf_free(p2p_ie);
3410 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3411 const u8 *dst, const u8 *bssid,
3412 const u8 *ie, size_t ie_len)
3414 if (wpa_s->global->p2p_disabled)
3416 if (wpa_s->global->p2p == NULL)
3419 return p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
3424 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3425 const u8 *sa, const u8 *bssid,
3426 u8 category, const u8 *data, size_t len, int freq)
3428 if (wpa_s->global->p2p_disabled)
3430 if (wpa_s->global->p2p == NULL)
3433 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3438 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3440 if (wpa_s->global->p2p_disabled)
3442 if (wpa_s->global->p2p == NULL)
3445 p2p_scan_ie(wpa_s->global->p2p, ies);
3449 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3451 p2p_group_deinit(wpa_s->p2p_group);
3452 wpa_s->p2p_group = NULL;
3454 wpa_s->ap_configured_cb = NULL;
3455 wpa_s->ap_configured_cb_ctx = NULL;
3456 wpa_s->ap_configured_cb_data = NULL;
3457 wpa_s->connect_without_scan = NULL;
3461 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3463 wpa_s->p2p_long_listen = 0;
3465 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3466 return wpa_drv_p2p_reject(wpa_s, addr);
3468 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3471 return p2p_reject(wpa_s->global->p2p, addr);
3475 /* Invite to reinvoke a persistent group */
3476 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3477 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3479 enum p2p_invite_role role;
3482 if (ssid->mode == WPAS_MODE_P2P_GO) {
3483 role = P2P_INVITE_ROLE_GO;
3484 if (peer_addr == NULL) {
3485 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3486 "address in invitation command");
3489 if (wpas_p2p_create_iface(wpa_s)) {
3490 if (wpas_p2p_add_group_interface(wpa_s,
3491 WPA_IF_P2P_GO) < 0) {
3492 wpa_printf(MSG_ERROR, "P2P: Failed to "
3493 "allocate a new interface for the "
3497 bssid = wpa_s->pending_interface_addr;
3499 bssid = wpa_s->own_addr;
3501 role = P2P_INVITE_ROLE_CLIENT;
3502 peer_addr = ssid->bssid;
3504 wpa_s->pending_invite_ssid_id = ssid->id;
3506 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3507 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3508 ssid->ssid, ssid->ssid_len,
3511 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3514 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3515 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3519 /* Invite to join an active group */
3520 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3521 const u8 *peer_addr, const u8 *go_dev_addr)
3523 struct wpa_global *global = wpa_s->global;
3524 enum p2p_invite_role role;
3526 struct wpa_ssid *ssid;
3528 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3529 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3532 if (wpa_s == NULL) {
3533 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3537 ssid = wpa_s->current_ssid;
3539 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3544 if (ssid->mode == WPAS_MODE_P2P_GO) {
3545 role = P2P_INVITE_ROLE_ACTIVE_GO;
3546 bssid = wpa_s->own_addr;
3547 if (go_dev_addr == NULL)
3548 go_dev_addr = wpa_s->parent->own_addr;
3550 role = P2P_INVITE_ROLE_CLIENT;
3551 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3552 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3553 "invite to current group");
3556 bssid = wpa_s->bssid;
3557 if (go_dev_addr == NULL &&
3558 !is_zero_ether_addr(wpa_s->go_dev_addr))
3559 go_dev_addr = wpa_s->go_dev_addr;
3561 wpa_s->parent->pending_invite_ssid_id = -1;
3563 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3564 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3565 ssid->ssid, ssid->ssid_len,
3568 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3571 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3572 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3577 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3579 struct wpa_ssid *ssid = wpa_s->current_ssid;
3580 const char *ssid_txt;
3581 u8 go_dev_addr[ETH_ALEN];
3582 int network_id = -1;
3585 if (!wpa_s->show_group_started || !ssid)
3588 wpa_s->show_group_started = 0;
3590 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3591 os_memset(go_dev_addr, 0, ETH_ALEN);
3592 if (ssid->bssid_set)
3593 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3594 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3596 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3598 if (wpa_s->global->p2p_group_formation == wpa_s)
3599 wpa_s->global->p2p_group_formation = NULL;
3601 if (ssid->passphrase == NULL && ssid->psk_set) {
3603 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3604 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3605 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3607 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3608 MAC2STR(go_dev_addr),
3609 persistent ? " [PERSISTENT]" : "");
3611 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3612 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3613 "go_dev_addr=" MACSTR "%s",
3614 wpa_s->ifname, ssid_txt, ssid->frequency,
3615 ssid->passphrase ? ssid->passphrase : "",
3616 MAC2STR(go_dev_addr),
3617 persistent ? " [PERSISTENT]" : "");
3621 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3624 network_id = ssid->id;
3625 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3629 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3630 u32 interval1, u32 duration2, u32 interval2)
3632 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3637 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3638 wpa_s->current_ssid == NULL ||
3639 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3642 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3643 wpa_s->own_addr, wpa_s->assoc_freq,
3644 duration1, interval1, duration2, interval2);
3648 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3649 unsigned int interval)
3651 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3654 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3657 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3661 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3663 struct wpa_supplicant *wpa_s = eloop_ctx;
3665 if (wpa_s->conf->p2p_group_idle == 0) {
3666 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3671 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3673 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3674 wpas_p2p_group_delete(wpa_s);
3678 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3680 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3681 if (wpa_s->conf->p2p_group_idle == 0)
3684 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3687 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3688 wpa_s->conf->p2p_group_idle);
3689 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3690 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3694 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3695 u16 reason_code, const u8 *ie, size_t ie_len)
3697 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3699 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3702 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3706 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3707 u16 reason_code, const u8 *ie, size_t ie_len)
3709 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3711 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3714 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3718 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3720 struct p2p_data *p2p = wpa_s->global->p2p;
3725 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3728 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3729 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3731 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
3732 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
3735 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
3736 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
3738 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
3739 p2p_set_uuid(p2p, wpa_s->wps->uuid);
3741 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
3742 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
3743 p2p_set_model_name(p2p, wpa_s->conf->model_name);
3744 p2p_set_model_number(p2p, wpa_s->conf->model_number);
3745 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
3748 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
3749 p2p_set_sec_dev_types(p2p,
3750 (void *) wpa_s->conf->sec_device_type,
3751 wpa_s->conf->num_sec_device_types);
3753 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
3755 p2p_remove_wps_vendor_extensions(p2p);
3756 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3757 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3759 p2p_add_wps_vendor_extension(
3760 p2p, wpa_s->conf->wps_vendor_ext[i]);
3764 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3765 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3767 country[0] = wpa_s->conf->country[0];
3768 country[1] = wpa_s->conf->country[1];
3770 p2p_set_country(p2p, country);
3773 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3774 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3775 wpa_s->conf->p2p_ssid_postfix ?
3776 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3780 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
3781 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
3783 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
3784 u8 reg_class, channel;
3787 if (wpa_s->conf->p2p_listen_reg_class &&
3788 wpa_s->conf->p2p_listen_channel) {
3789 reg_class = wpa_s->conf->p2p_listen_reg_class;
3790 channel = wpa_s->conf->p2p_listen_channel;
3794 * Pick one of the social channels randomly as the
3797 os_get_random((u8 *) &r, sizeof(r));
3798 channel = 1 + (r % 3) * 5;
3800 ret = p2p_set_listen_channel(p2p, reg_class, channel);
3802 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
3805 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
3806 u8 op_reg_class, op_channel, cfg_op_channel;
3809 if (wpa_s->conf->p2p_oper_reg_class &&
3810 wpa_s->conf->p2p_oper_channel) {
3811 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3812 op_channel = wpa_s->conf->p2p_oper_channel;
3817 * Use random operation channel from (1, 6, 11)
3818 *if no other preference is indicated.
3820 os_get_random((u8 *) &r, sizeof(r));
3821 op_channel = 1 + (r % 3) * 5;
3824 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
3827 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
3833 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
3836 if (!wpa_s->ap_iface)
3838 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
3843 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
3845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3847 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3850 wpa_s->global->cross_connection = enabled;
3851 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
3854 struct wpa_supplicant *iface;
3856 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
3858 if (iface->cross_connect_enabled == 0)
3861 iface->cross_connect_enabled = 0;
3862 iface->cross_connect_in_use = 0;
3863 wpa_msg(iface->parent, MSG_INFO,
3864 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3865 iface->ifname, iface->cross_connect_uplink);
3873 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
3875 struct wpa_supplicant *iface;
3877 if (!uplink->global->cross_connection)
3880 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3881 if (!iface->cross_connect_enabled)
3883 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3886 if (iface->ap_iface == NULL)
3888 if (iface->cross_connect_in_use)
3891 iface->cross_connect_in_use = 1;
3892 wpa_msg(iface->parent, MSG_INFO,
3893 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3894 iface->ifname, iface->cross_connect_uplink);
3899 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
3901 struct wpa_supplicant *iface;
3903 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3904 if (!iface->cross_connect_enabled)
3906 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3909 if (!iface->cross_connect_in_use)
3912 wpa_msg(iface->parent, MSG_INFO,
3913 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3914 iface->ifname, iface->cross_connect_uplink);
3915 iface->cross_connect_in_use = 0;
3920 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
3922 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
3923 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
3924 wpa_s->cross_connect_disallowed)
3925 wpas_p2p_disable_cross_connect(wpa_s);
3927 wpas_p2p_enable_cross_connect(wpa_s);
3928 if (!wpa_s->ap_iface)
3929 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3933 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
3935 wpas_p2p_disable_cross_connect(wpa_s);
3936 if (!wpa_s->ap_iface)
3937 wpas_p2p_set_group_idle_timeout(wpa_s);
3941 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
3943 struct wpa_supplicant *iface;
3945 if (!wpa_s->global->cross_connection)
3948 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3951 if (iface->drv_flags &
3952 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
3954 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
3957 wpa_s->cross_connect_enabled = 1;
3958 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
3959 sizeof(wpa_s->cross_connect_uplink));
3960 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
3961 "%s to %s whenever uplink is available",
3962 wpa_s->ifname, wpa_s->cross_connect_uplink);
3964 if (iface->ap_iface || iface->current_ssid == NULL ||
3965 iface->current_ssid->mode != WPAS_MODE_INFRA ||
3966 iface->cross_connect_disallowed ||
3967 iface->wpa_state != WPA_COMPLETED)
3970 wpa_s->cross_connect_in_use = 1;
3971 wpa_msg(wpa_s->parent, MSG_INFO,
3972 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3973 wpa_s->ifname, wpa_s->cross_connect_uplink);
3979 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
3981 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
3982 !wpa_s->p2p_in_provisioning)
3983 return 0; /* not P2P client operation */
3985 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
3987 if (wpa_s != wpa_s->parent)
3988 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
3990 if (wpa_s->global->p2p)
3991 p2p_group_formation_failed(wpa_s->global->p2p);
3993 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
3994 wpa_s->parent, NULL);
3996 wpas_group_formation_completed(wpa_s, 0);
4001 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4003 struct p2p_channels chan;
4005 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4008 os_memset(&chan, 0, sizeof(chan));
4009 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4010 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4015 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4019 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4021 struct wpa_global *global = wpa_s->global;
4025 if (global->p2p == NULL)
4028 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4030 if (wpa_s->pending_interface_name[0] &&
4031 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4034 peer = p2p_get_go_neg_peer(global->p2p);
4036 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4037 MACSTR, MAC2STR(peer));
4038 p2p_unauthorize(global->p2p, peer);
4041 wpas_p2p_stop_find(wpa_s);
4043 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4044 if (wpa_s == global->p2p_group_formation &&
4045 (wpa_s->p2p_in_provisioning ||
4046 wpa_s->parent->pending_interface_type ==
4047 WPA_IF_P2P_CLIENT)) {
4048 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4049 "formation found - cancelling",
4052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4053 wpa_s->parent, NULL);
4054 wpas_p2p_group_delete(wpa_s);
4060 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4068 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4070 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4073 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4074 "being available anymore");
4075 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4076 wpas_p2p_group_delete(wpa_s);
4080 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4081 int freq_24, int freq_5, int freq_overall)
4083 struct p2p_data *p2p = wpa_s->global->p2p;
4084 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4086 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4090 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4093 struct p2p_data *p2p = wpa_s->global->p2p;
4095 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4098 if (hwaddr_aton(addr, peer))
4101 return p2p_unauthorize(p2p, peer);
4106 * wpas_p2p_disconnect - Disconnect from a P2P Group
4107 * @wpa_s: Pointer to wpa_supplicant data
4108 * Returns: 0 on success, -1 on failure
4110 * This can be used to disconnect from a group in which the local end is a P2P
4111 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4112 * virtual network interface was created for this group, that interface will be
4113 * removed. Otherwise, only the configured P2P group network will be removed
4114 * from the interface.
4116 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4122 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4123 wpas_p2p_group_delete(wpa_s);
4129 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4131 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4134 return p2p_in_progress(wpa_s->global->p2p);