blob: b9ebd380f2d32430a544ca91ffcbafbf651fd9f8 [file] [log] [blame]
/*
* wpa_supplicant - P2P
* Copyright (c) 2009-2010, Atheros Communications
* Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#include "common.h"
#include "eloop.h"
#include "common/ieee802_11_common.h"
#include "common/ieee802_11_defs.h"
#include "common/wpa_ctrl.h"
#include "wps/wps_i.h"
#include "p2p/p2p.h"
#include "ap/hostapd.h"
#include "ap/ap_config.h"
#include "ap/sta_info.h"
#include "ap/ap_drv_ops.h"
#include "ap/wps_hostapd.h"
#include "ap/p2p_hostapd.h"
#include "ap/dfs.h"
#include "eapol_supp/eapol_supp_sm.h"
#include "rsn_supp/wpa.h"
#include "wpa_supplicant_i.h"
#include "driver_i.h"
#include "ap.h"
#include "config_ssid.h"
#include "config.h"
#include "notify.h"
#include "scan.h"
#include "bss.h"
#include "offchannel.h"
#include "wps_supplicant.h"
#include "p2p_supplicant.h"
#include "wifi_display.h"
/*
* How many times to try to scan to find the GO before giving up on join
* request.
*/
#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
#ifndef P2P_MAX_CLIENT_IDLE
/*
* How many seconds to try to reconnect to the GO when connection in P2P client
* role has been lost.
*/
#define P2P_MAX_CLIENT_IDLE 10
#endif /* P2P_MAX_CLIENT_IDLE */
#ifndef P2P_MAX_INITIAL_CONN_WAIT
/*
* How many seconds to wait for initial 4-way handshake to get completed after
* WPS provisioning step or after the re-invocation of a persistent group on a
* P2P Client.
*/
#define P2P_MAX_INITIAL_CONN_WAIT 10
#endif /* P2P_MAX_INITIAL_CONN_WAIT */
#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
/*
* How many seconds to wait for initial 4-way handshake to get completed after
* WPS provisioning step on the GO. This controls the extra time the P2P
* operation is considered to be in progress (e.g., to delay other scans) after
* WPS provisioning has been completed on the GO during group formation.
*/
#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
/*
* How many seconds to wait for initial 4-way handshake to get completed after
* re-invocation of a persistent group on the GO when the client is expected
* to connect automatically (no user interaction).
*/
#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
enum p2p_group_removal_reason {
P2P_GROUP_REMOVAL_UNKNOWN,
P2P_GROUP_REMOVAL_SILENT,
P2P_GROUP_REMOVAL_FORMATION_FAILED,
P2P_GROUP_REMOVAL_REQUESTED,
P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
P2P_GROUP_REMOVAL_UNAVAILABLE,
P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
P2P_GROUP_REMOVAL_PSK_FAILURE,
P2P_GROUP_REMOVAL_FREQ_CONFLICT
};
static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
static struct wpa_supplicant *
wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
int go);
static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
const u8 *ssid, size_t ssid_len);
static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
const u8 *ssid, size_t ssid_len);
static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
const u8 *dev_addr, enum p2p_wps_method wps_method,
int auto_join, int freq,
const u8 *ssid, size_t ssid_len);
static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
void *timeout_ctx);
static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
int group_added);
static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
static void wpas_stop_listen(void *ctx);
static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
enum wpa_driver_if_type type);
static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
int already_deleted);
/*
* Get the number of concurrent channels that the HW can operate, but that are
* currently not in use by any of the wpa_supplicant interfaces.
*/
static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
{
int *freqs;
int num, unused;
freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
if (!freqs)
return -1;
num = get_shared_radio_freqs(wpa_s, freqs,
wpa_s->num_multichan_concurrent);
os_free(freqs);
unused = wpa_s->num_multichan_concurrent - num;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
return unused;
}
/*
* Get the frequencies that are currently in use by one or more of the virtual
* interfaces, and that are also valid for P2P operation.
*/
static unsigned int
wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
struct wpa_used_freq_data *p2p_freqs,
unsigned int len)
{
struct wpa_used_freq_data *freqs;
unsigned int num, i, j;
freqs = os_calloc(wpa_s->num_multichan_concurrent,
sizeof(struct wpa_used_freq_data));
if (!freqs)
return 0;
num = get_shared_radio_freqs_data(wpa_s, freqs,
wpa_s->num_multichan_concurrent);
os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
for (i = 0, j = 0; i < num && j < len; i++) {
if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
p2p_freqs[j++] = freqs[i];
}
os_free(freqs);
dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
return j;
}
static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
int freq)
{
if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
return;
/* Use the wpa_s used to control the P2P Device operation */
wpa_s = wpa_s->global->p2p_init_wpa_s;
if (wpa_s->conf->p2p_ignore_shared_freq &&
freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
wpas_p2p_num_unused_channels(wpa_s) > 0) {
wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
freq);
freq = 0;
}
p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
}
static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
struct wpa_scan_results *scan_res)
{
size_t i;
if (wpa_s->p2p_scan_work) {
struct wpa_radio_work *work = wpa_s->p2p_scan_work;
wpa_s->p2p_scan_work = NULL;
radio_work_done(work);
}
if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
return;
wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
(int) scan_res->num);
for (i = 0; i < scan_res->num; i++) {
struct wpa_scan_res *bss = scan_res->res[i];
struct os_reltime time_tmp_age, entry_ts;
const u8 *ies;
size_t ies_len;
time_tmp_age.sec = bss->age / 1000;
time_tmp_age.usec = (bss->age % 1000) * 1000;
os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
ies = (const u8 *) (bss + 1);
ies_len = bss->ie_len;
if (bss->beacon_ie_len > 0 &&
!wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
MACSTR, MAC2STR(bss->bssid));
ies = ies + ies_len;
ies_len = bss->beacon_ie_len;
}
if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
bss->freq, &entry_ts, bss->level,
ies, ies_len) > 0)
break;
}
p2p_scan_res_handled(wpa_s->global->p2p);
}
static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
{
struct wpa_supplicant *wpa_s = work->wpa_s;
struct wpa_driver_scan_params *params = work->ctx;
int ret;
if (deinit) {
if (!work->started) {
wpa_scan_free_params(params);
return;
}
wpa_s->p2p_scan_work = NULL;
return;
}
ret = wpa_drv_scan(wpa_s, params);
wpa_scan_free_params(params);
work->ctx = NULL;
if (ret) {
radio_work_done(work);
p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
return;
}
p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
os_get_reltime(&wpa_s->scan_trigger_time);
wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
wpa_s->own_scan_requested = 1;
wpa_s->p2p_scan_work = work;
}
static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
int freq)
{
if (wpa_s->global->p2p_24ghz_social_channels &&
(freq == 2412 || freq == 2437 || freq == 2462)) {
/*
* Search all social channels regardless of whether these have
* been disabled for P2P operating channel use to avoid missing
* peers.
*/
return 1;
}
return p2p_supported_freq(wpa_s->global->p2p, freq);
}
static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
unsigned int num_req_dev_types,
const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
{
struct wpa_supplicant *wpa_s = ctx;
struct wpa_driver_scan_params *params = NULL;
struct wpabuf *wps_ie, *ies;
unsigned int num_channels = 0;
int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
size_t ielen;
u8 *n, i;
if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
return -1;
if (wpa_s->p2p_scan_work) {
wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
return -1;
}
params = os_zalloc(sizeof(*params));
if (params == NULL)
return -1;
/* P2P Wildcard SSID */
params->num_ssids = 1;
n = os_malloc(P2P_WILDCARD_SSID_LEN);
if (n == NULL)
goto fail;
os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
params->ssids[0].ssid = n;
params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
wpa_s->wps->dev.p2p = 1;
wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
num_req_dev_types, req_dev_types);
if (wps_ie == NULL)
goto fail;
ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
if (ies == NULL) {
wpabuf_free(wps_ie);
goto fail;
}
wpabuf_put_buf(ies, wps_ie);
wpabuf_free(wps_ie);
p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
params->p2p_probe = 1;
n = os_malloc(wpabuf_len(ies));
if (n == NULL) {
wpabuf_free(ies);
goto fail;
}
os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
params->extra_ies = n;
params->extra_ies_len = wpabuf_len(ies);
wpabuf_free(ies);
switch (type) {
case P2P_SCAN_SOCIAL:
params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
sizeof(int));
if (params->freqs == NULL)
goto fail;
for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
if (wpas_p2p_search_social_channel(
wpa_s, social_channels_freq[i]))
params->freqs[num_channels++] =
social_channels_freq[i];
}
params->freqs[num_channels++] = 0;
break;
case P2P_SCAN_FULL:
break;
case P2P_SCAN_SPECIFIC:
params->freqs = os_calloc(2, sizeof(int));
if (params->freqs == NULL)
goto fail;
params->freqs[0] = freq;
params->freqs[1] = 0;
break;
case P2P_SCAN_SOCIAL_PLUS_ONE:
params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
sizeof(int));
if (params->freqs == NULL)
goto fail;
for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
if (wpas_p2p_search_social_channel(
wpa_s, social_channels_freq[i]))
params->freqs[num_channels++] =
social_channels_freq[i];
}
if (p2p_supported_freq(wpa_s->global->p2p, freq))
params->freqs[num_channels++] = freq;
params->freqs[num_channels++] = 0;
break;
}
radio_remove_works(wpa_s, "p2p-scan", 0);
if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
params) < 0)
goto fail;
return 0;
fail:
wpa_scan_free_params(params);
return -1;
}
static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
{
switch (p2p_group_interface) {
case P2P_GROUP_INTERFACE_PENDING:
return WPA_IF_P2P_GROUP;
case P2P_GROUP_INTERFACE_GO:
return WPA_IF_P2P_GO;
case P2P_GROUP_INTERFACE_CLIENT:
return WPA_IF_P2P_CLIENT;
}
return WPA_IF_P2P_GROUP;
}
static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
const u8 *ssid,
size_t ssid_len, int *go)
{
struct wpa_ssid *s;
for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
for (s = wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled != 0 || !s->p2p_group ||
s->ssid_len != ssid_len ||
os_memcmp(ssid, s->ssid, ssid_len) != 0)
continue;
if (s->mode == WPAS_MODE_P2P_GO &&
s != wpa_s->current_ssid)
continue;
if (go)
*go = s->mode == WPAS_MODE_P2P_GO;
return wpa_s;
}
}
return NULL;
}
static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
{
struct wpa_supplicant *wpa_s = eloop_ctx;
wpa_printf(MSG_DEBUG,
"P2P: Complete previously requested removal of %s",
wpa_s->ifname);
wpas_p2p_disconnect(wpa_s);
}
static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
struct wpa_supplicant *calling_wpa_s)
{
if (calling_wpa_s == wpa_s && wpa_s &&
wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
/*
* The calling wpa_s instance is going to be removed. Do that
* from an eloop callback to keep the instance available until
* the caller has returned. This my be needed, e.g., to provide
* control interface responses on the per-interface socket.
*/
if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
wpa_s, NULL) < 0)
return -1;
return 0;
}
return wpas_p2p_disconnect(wpa_s);
}
/* Determine total number of clients in active groups where we are the GO */
static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
{
unsigned int count = 0;
struct wpa_ssid *s;
for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
for (s = wpa_s->conf->ssid; s; s = s->next) {
wpa_printf(MSG_DEBUG,
"P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
wpa_s, s, s->disabled, s->p2p_group,
s->mode);
if (!s->disabled && s->p2p_group &&
s->mode == WPAS_MODE_P2P_GO) {
count += p2p_get_group_num_members(
wpa_s->p2p_group);
}
}
}
return count;
}
/* Find an interface for a P2P group where we are the GO */
static struct wpa_supplicant *
wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
{
struct wpa_supplicant *save = NULL;
struct wpa_ssid *s;
if (!wpa_s)
return NULL;
for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
for (s = wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled || !s->p2p_group ||
s->mode != WPAS_MODE_P2P_GO)
continue;
/* Prefer a group with connected clients */
if (p2p_get_group_num_members(wpa_s->p2p_group))
return wpa_s;
save = wpa_s;
}
}
/* No group with connected clients, so pick the one without (if any) */
return save;
}
/* Find an active P2P group where we are the GO */
static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
u8 *bssid)
{
struct wpa_ssid *s, *empty = NULL;
if (!wpa_s)
return 0;
for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
for (s = wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled || !s->p2p_group ||
s->mode != WPAS_MODE_P2P_GO)
continue;
os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
if (p2p_get_group_num_members(wpa_s->p2p_group))
return s;
empty = s;
}
}
return empty;
}
/* Find a persistent group where we are the GO */
static struct wpa_ssid *
wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
{
struct wpa_ssid *s;
for (s = wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
return s;
}
return NULL;
}
static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
{
struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
struct wpa_ssid *s;
u8 conncap = P2PS_SETUP_NONE;
unsigned int owned_members = 0;
unsigned int owner = 0;
unsigned int client = 0;
struct wpa_supplicant *go_wpa_s;
struct wpa_ssid *persistent_go;
int p2p_no_group_iface;
wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
/*
* For non-concurrent capable devices:
* If persistent_go, then no new.
* If GO, then no client.
* If client, then no GO.
*/
go_wpa_s = wpas_p2p_get_go_group(wpa_s);
persistent_go = wpas_p2p_get_persistent_go(wpa_s);
p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
go_wpa_s, persistent_go);
for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
tmp_wpa_s = tmp_wpa_s->next) {
for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
wpa_printf(MSG_DEBUG,
"P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
tmp_wpa_s, s, s->disabled,
s->p2p_group, s->mode);
if (!s->disabled && s->p2p_group) {
if (s->mode == WPAS_MODE_P2P_GO) {
owned_members +=
p2p_get_group_num_members(
tmp_wpa_s->p2p_group);
owner++;
} else
client++;
}
}
}
/* If not concurrent, restrict our choices */
if (p2p_no_group_iface) {
wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
if (client)
return P2PS_SETUP_NONE;
if (go_wpa_s) {
if (role == P2PS_SETUP_CLIENT ||
incoming == P2PS_SETUP_GROUP_OWNER ||
p2p_client_limit_reached(go_wpa_s->p2p_group))
return P2PS_SETUP_NONE;
return P2PS_SETUP_GROUP_OWNER;
}
if (persistent_go) {
if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
if (!incoming)
return P2PS_SETUP_GROUP_OWNER |
P2PS_SETUP_CLIENT;
if (incoming == P2PS_SETUP_NEW) {
u8 r;
if (os_get_random(&r, sizeof(r)) < 0 ||
(r & 1))
return P2PS_SETUP_CLIENT;
return P2PS_SETUP_GROUP_OWNER;
}
}
}
}
/* If a required role has been specified, handle it here */
if (role && role != P2PS_SETUP_NEW) {
switch (incoming) {
case P2PS_SETUP_NONE:
case P2PS_SETUP_NEW:
case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
conncap = role;
goto grp_owner;
case P2PS_SETUP_GROUP_OWNER:
/*
* Must be a complimentary role - cannot be a client to
* more than one peer.
*/
if (incoming == role || client)
return P2PS_SETUP_NONE;
return P2PS_SETUP_CLIENT;
case P2PS_SETUP_CLIENT:
/* Must be a complimentary role */
if (incoming != role) {
conncap = P2PS_SETUP_GROUP_OWNER;
goto grp_owner;
}
default:
return P2PS_SETUP_NONE;
}
}
/*
* For now, we only will support ownership of one group, and being a
* client of one group. Therefore, if we have either an existing GO
* group, or an existing client group, we will not do a new GO
* negotiation, but rather try to re-use the existing groups.
*/
switch (incoming) {
case P2PS_SETUP_NONE:
case P2PS_SETUP_NEW:
if (client)
conncap = P2PS_SETUP_GROUP_OWNER;
else if (!owned_members)
conncap = P2PS_SETUP_NEW;
else if (incoming == P2PS_SETUP_NONE)
conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
else
conncap = P2PS_SETUP_CLIENT;
break;
case P2PS_SETUP_CLIENT:
conncap = P2PS_SETUP_GROUP_OWNER;
break;
case P2PS_SETUP_GROUP_OWNER:
if (!client)
conncap = P2PS_SETUP_CLIENT;
break;
case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
if (client)
conncap = P2PS_SETUP_GROUP_OWNER;
else {
u8 r;
if (os_get_random(&r, sizeof(r)) < 0 ||
(r & 1))
conncap = P2PS_SETUP_CLIENT;
else
conncap = P2PS_SETUP_GROUP_OWNER;
}
break;
default:
return P2PS_SETUP_NONE;
}
grp_owner:
if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
(!incoming && (conncap & P2PS_SETUP_NEW))) {
if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
conncap &= ~P2PS_SETUP_GROUP_OWNER;
wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
owner, owned_members, conncap);
s = wpas_p2p_get_persistent_go(wpa_s);
if (!s && !owner && p2p_no_group_iface) {
p2p_set_intended_addr(wpa_s->global->p2p,
wpa_s->own_addr);
} else if (!s && !owner) {
if (wpas_p2p_add_group_interface(wpa_s,
WPA_IF_P2P_GO) < 0) {
wpa_printf(MSG_ERROR,
"P2P: Failed to allocate a new interface for the group");
return P2PS_SETUP_NONE;
}
wpa_s->global->pending_group_iface_for_p2ps = 1;
p2p_set_intended_addr(wpa_s->global->p2p,
wpa_s->pending_interface_addr);
}
}
return conncap;
}
static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
enum p2p_group_removal_reason removal_reason)
{
struct wpa_ssid *ssid;
char *gtype;
const char *reason;
ssid = wpa_s->current_ssid;
if (ssid == NULL) {
/*
* The current SSID was not known, but there may still be a
* pending P2P group interface waiting for provisioning or a
* P2P group that is trying to reconnect.
*/
ssid = wpa_s->conf->ssid;
while (ssid) {
if (ssid->p2p_group && ssid->disabled != 2)
break;
ssid = ssid->next;
}
if (ssid == NULL &&
wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
{
wpa_printf(MSG_ERROR, "P2P: P2P group interface "
"not found");
return -1;
}
}
if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
gtype = "GO";
else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
(ssid && ssid->mode == WPAS_MODE_INFRA)) {
wpa_s->reassociate = 0;
wpa_s->disconnected = 1;
gtype = "client";
} else
gtype = "GO";
if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
if (os_strcmp(gtype, "client") == 0) {
wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
wpa_s, NULL)) {
wpa_printf(MSG_DEBUG,
"P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
wpa_s, NULL);
}
}
if (wpa_s->cross_connect_in_use) {
wpa_s->cross_connect_in_use = 0;
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
wpa_s->ifname, wpa_s->cross_connect_uplink);
}
switch (removal_reason) {
case P2P_GROUP_REMOVAL_REQUESTED:
reason = " reason=REQUESTED";
break;
case P2P_GROUP_REMOVAL_FORMATION_FAILED:
reason = " reason=FORMATION_FAILED";
break;
case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
reason = " reason=IDLE";
break;
case P2P_GROUP_REMOVAL_UNAVAILABLE:
reason = " reason=UNAVAILABLE";
break;
case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
reason = " reason=GO_ENDING_SESSION";
break;
case P2P_GROUP_REMOVAL_PSK_FAILURE:
reason = " reason=PSK_FAILURE";
break;
case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
reason = " reason=FREQ_CONFLICT";
break;
default:
reason = "";
break;
}
if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_GROUP_REMOVED "%s %s%s",
wpa_s->ifname, gtype, reason);
}
if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL) > 0) {
wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
"timeout");
wpa_s->p2p_in_provisioning = 0;
}
wpa_s->p2p_in_invitation = 0;
/*
* Make sure wait for the first client does not remain active after the
* group has been removed.
*/
wpa_s->global->p2p_go_wait_client.sec = 0;
if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
struct wpa_global *global;
char *ifname;
enum wpa_driver_if_type type;
wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
wpa_s->ifname);
global = wpa_s->global;
ifname = os_strdup(wpa_s->ifname);
type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
wpa_s = global->ifaces;
if (wpa_s && ifname)
wpa_drv_if_remove(wpa_s, type, ifname);
os_free(ifname);
return 1;
}
if (!wpa_s->p2p_go_group_formation_completed) {
wpa_s->global->p2p_group_formation = NULL;
wpa_s->p2p_in_provisioning = 0;
}
wpa_s->show_group_started = 0;
os_free(wpa_s->go_params);
wpa_s->go_params = NULL;
os_free(wpa_s->p2p_group_common_freqs);
wpa_s->p2p_group_common_freqs = NULL;
wpa_s->p2p_group_common_freqs_num = 0;
wpa_s->waiting_presence_resp = 0;
wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
if (ssid && (ssid->p2p_group ||
ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
(ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
int id = ssid->id;
if (ssid == wpa_s->current_ssid) {
wpa_sm_set_config(wpa_s->wpa, NULL);
eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
wpa_s->current_ssid = NULL;
}
/*
* Networks objects created during any P2P activities are not
* exposed out as they might/will confuse certain non-P2P aware
* applications since these network objects won't behave like
* regular ones.
*
* Likewise, we don't send out network removed signals for such
* network objects.
*/
wpa_config_remove_network(wpa_s->conf, id);
wpa_supplicant_clear_status(wpa_s);
wpa_supplicant_cancel_sched_scan(wpa_s);
} else {
wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
"found");
}
if (wpa_s->ap_iface)
wpa_supplicant_ap_deinit(wpa_s);
else
wpa_drv_deinit_p2p_cli(wpa_s);
return 0;
}
static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
u8 *go_dev_addr,
const u8 *ssid, size_t ssid_len)
{
struct wpa_bss *bss;
const u8 *bssid;
struct wpabuf *p2p;
u8 group_capab;
const u8 *addr;
if (wpa_s->go_params)
bssid = wpa_s->go_params->peer_interface_addr;
else
bssid = wpa_s->bssid;
bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
if (bss == NULL && wpa_s->go_params &&
!is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
bss = wpa_bss_get_p2p_dev_addr(
wpa_s, wpa_s->go_params->peer_device_addr);
if (bss == NULL) {
u8 iface_addr[ETH_ALEN];
if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
iface_addr) == 0)
bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
}
if (bss == NULL) {
wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
"group is persistent - BSS " MACSTR " not found",
MAC2STR(bssid));
return 0;
}
p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
if (p2p == NULL)
p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
P2P_IE_VENDOR_TYPE);
if (p2p == NULL) {
wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
"group is persistent - BSS " MACSTR
" did not include P2P IE", MAC2STR(bssid));
wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
(u8 *) (bss + 1), bss->ie_len);
wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
((u8 *) bss + 1) + bss->ie_len,
bss->beacon_ie_len);
return 0;
}
group_capab = p2p_get_group_capab(p2p);
addr = p2p_get_go_dev_addr(p2p);
wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
"group_capab=0x%x", group_capab);
if (addr) {
os_memcpy(go_dev_addr, addr, ETH_ALEN);
wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
MAC2STR(addr));
} else
os_memset(go_dev_addr, 0, ETH_ALEN);
wpabuf_free(p2p);
wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
"go_dev_addr=" MACSTR,
MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
}
static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
struct wpa_ssid *ssid,
const u8 *go_dev_addr)
{
struct wpa_ssid *s;
int changed = 0;
wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
"group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
for (s = wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled == 2 &&
os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
s->ssid_len == ssid->ssid_len &&
os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
break;
}
if (s) {
wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
"entry");
if (ssid->passphrase && !s->passphrase)
changed = 1;
else if (ssid->passphrase && s->passphrase &&
os_strcmp(ssid->passphrase, s->passphrase) != 0)
changed = 1;
} else {
wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
"entry");
changed = 1;
s = wpa_config_add_network(wpa_s->conf);
if (s == NULL)
return -1;
/*
* Instead of network_added we emit persistent_group_added
* notification. Also to keep the defense checks in
* persistent_group obj registration method, we set the
* relevant flags in s to designate it as a persistent group.
*/
s->p2p_group = 1;
s->p2p_persistent_group = 1;
wpas_notify_persistent_group_added(wpa_s, s);
wpa_config_set_network_defaults(s);
}
s->p2p_group = 1;
s->p2p_persistent_group = 1;
s->disabled = 2;
s->bssid_set = 1;
os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
s->mode = ssid->mode;
s->auth_alg = WPA_AUTH_ALG_OPEN;
s->key_mgmt = WPA_KEY_MGMT_PSK;
s->proto = WPA_PROTO_RSN;
s->pairwise_cipher = WPA_CIPHER_CCMP;
s->export_keys = 1;
if (ssid->passphrase) {
os_free(s->passphrase);
s->passphrase = os_strdup(ssid->passphrase);
}
if (ssid->psk_set) {
s->psk_set = 1;
os_memcpy(s->psk, ssid->psk, 32);
}
if (s->passphrase && !s->psk_set)
wpa_config_update_psk(s);
if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
os_free(s->ssid);
s->ssid = os_malloc(ssid->ssid_len);
}
if (s->ssid) {
s->ssid_len = ssid->ssid_len;
os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
}
if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
wpa_s->global->add_psk = NULL;
changed = 1;
}
if (changed && wpa_s->conf->update_config &&
wpa_config_write(wpa_s->confname, wpa_s->conf)) {
wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
}
return s->id;
}
static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
const u8 *addr)
{
struct wpa_ssid *ssid, *s;
u8 *n;
size_t i;
int found = 0;
struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
ssid = wpa_s->current_ssid;
if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
!ssid->p2p_persistent_group)
return;
for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
continue;
if (s->ssid_len == ssid->ssid_len &&
os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
break;
}
if (s == NULL)
return;
for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
ETH_ALEN) != 0)
continue;
if (i == s->num_p2p_clients - 1)
return; /* already the most recent entry */
/* move the entry to mark it most recent */
os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
(s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
os_memcpy(s->p2p_client_list +
(s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
ETH_ALEN);
os_memset(s->p2p_client_list +
(s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
0xff, ETH_ALEN);
found = 1;
break;
}
if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
n = os_realloc_array(s->p2p_client_list,
s->num_p2p_clients + 1, 2 * ETH_ALEN);
if (n == NULL)
return;
os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
ETH_ALEN);
os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
0xff, ETH_ALEN);
s->p2p_client_list = n;
s->num_p2p_clients++;
} else if (!found && s->p2p_client_list) {
/* Not enough room for an additional entry - drop the oldest
* entry */
os_memmove(s->p2p_client_list,
s->p2p_client_list + 2 * ETH_ALEN,
(s->num_p2p_clients - 1) * 2 * ETH_ALEN);
os_memcpy(s->p2p_client_list +
(s->num_p2p_clients - 1) * 2 * ETH_ALEN,
addr, ETH_ALEN);
os_memset(s->p2p_client_list +
(s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
0xff, ETH_ALEN);
}
if (p2p_wpa_s->conf->update_config &&
wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
}
static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
int go, struct wpa_ssid *ssid, int freq,
const u8 *psk, const char *passphrase,
const u8 *go_dev_addr, int persistent,
const char *extra)
{
const char *ssid_txt;
char psk_txt[65];
if (psk)
wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
else
psk_txt[0] = '\0';
if (ssid)
ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
else
ssid_txt = "";
if (passphrase && passphrase[0] == '\0')
passphrase = NULL;
/*
* Include PSK/passphrase only in the control interface message and
* leave it out from the debug log entry.
*/
wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
P2P_EVENT_GROUP_STARTED
"%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
MACSTR "%s%s",
wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
psk ? " psk=" : "", psk_txt,
passphrase ? " passphrase=\"" : "",
passphrase ? passphrase : "",
passphrase ? "\"" : "",
MAC2STR(go_dev_addr),
persistent ? " [PERSISTENT]" : "", extra);
wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
"%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
extra);
}
static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
int success, int already_deleted)
{
struct wpa_ssid *ssid;
int client;
int persistent;
u8 go_dev_addr[ETH_ALEN];
int network_id = -1;
/*
* This callback is likely called for the main interface. Update wpa_s
* to use the group interface if a new interface was created for the
* group.
*/
if (wpa_s->global->p2p_group_formation)
wpa_s = wpa_s->global->p2p_group_formation;
if (wpa_s->p2p_go_group_formation_completed) {
wpa_s->global->p2p_group_formation = NULL;
wpa_s->p2p_in_provisioning = 0;
}
wpa_s->p2p_in_invitation = 0;
wpa_s->group_formation_reported = 1;
if (!success) {
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_GROUP_FORMATION_FAILURE);
if (already_deleted)
return;
wpas_p2p_group_delete(wpa_s,
P2P_GROUP_REMOVAL_FORMATION_FAILED);
return;
}
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_GROUP_FORMATION_SUCCESS);
ssid = wpa_s->current_ssid;
if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
ssid->mode = WPAS_MODE_P2P_GO;
p2p_group_notif_formation_done(wpa_s->p2p_group);
wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
}
persistent = 0;
if (ssid) {
client = ssid->mode == WPAS_MODE_INFRA;
if (ssid->mode == WPAS_MODE_P2P_GO) {
persistent = ssid->p2p_persistent_group;
os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
ETH_ALEN);
} else
persistent = wpas_p2p_persistent_group(wpa_s,
go_dev_addr,
ssid->ssid,
ssid->ssid_len);
} else {
client = wpa_s->p2p_group_interface ==
P2P_GROUP_INTERFACE_CLIENT;
os_memset(go_dev_addr, 0, ETH_ALEN);
}
wpa_s->show_group_started = 0;
if (client) {
/*
* Indicate event only after successfully completed 4-way
* handshake, i.e., when the interface is ready for data
* packets.
*/
wpa_s->show_group_started = 1;
} else {
wpas_p2p_group_started(wpa_s, 1, ssid,
ssid ? ssid->frequency : 0,
ssid && ssid->passphrase == NULL &&
ssid->psk_set ? ssid->psk : NULL,
ssid ? ssid->passphrase : NULL,
go_dev_addr, persistent, "");
wpas_p2p_cross_connect_setup(wpa_s);
wpas_p2p_set_group_idle_timeout(wpa_s);
}
if (persistent)
network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
ssid, go_dev_addr);
else {
os_free(wpa_s->global->add_psk);
wpa_s->global->add_psk = NULL;
}
if (network_id < 0 && ssid)
network_id = ssid->id;
if (!client) {
wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
os_get_reltime(&wpa_s->global->p2p_go_wait_client);
}
}
struct send_action_work {
unsigned int freq;
u8 dst[ETH_ALEN];
u8 src[ETH_ALEN];
u8 bssid[ETH_ALEN];
size_t len;
unsigned int wait_time;
u8 buf[0];
};
static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
void *timeout_ctx)
{
struct wpa_supplicant *wpa_s = eloop_ctx;
if (!wpa_s->p2p_send_action_work)
return;
wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
os_free(wpa_s->p2p_send_action_work->ctx);
radio_work_done(wpa_s->p2p_send_action_work);
wpa_s->p2p_send_action_work = NULL;
}
static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
{
if (wpa_s->p2p_send_action_work) {
struct send_action_work *awork;
awork = wpa_s->p2p_send_action_work->ctx;
if (awork->wait_time == 0) {
os_free(awork);
radio_work_done(wpa_s->p2p_send_action_work);
wpa_s->p2p_send_action_work = NULL;
} else {
/*
* In theory, this should not be needed, but number of
* places in the P2P code is still using non-zero wait
* time for the last Action frame in the sequence and
* some of these do not call send_action_done().
*/
eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
wpa_s, NULL);
eloop_register_timeout(
0, awork->wait_time * 1000,
wpas_p2p_send_action_work_timeout,
wpa_s, NULL);
}
}
}
static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
unsigned int freq,
const u8 *dst, const u8 *src,
const u8 *bssid,
const u8 *data, size_t data_len,
enum offchannel_send_action_result
result)
{
enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
wpas_p2p_action_tx_clear(wpa_s);
if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
return;
switch (result) {
case OFFCHANNEL_SEND_ACTION_SUCCESS:
res = P2P_SEND_ACTION_SUCCESS;
break;
case OFFCHANNEL_SEND_ACTION_NO_ACK:
res = P2P_SEND_ACTION_NO_ACK;
break;
case OFFCHANNEL_SEND_ACTION_FAILED:
res = P2P_SEND_ACTION_FAILED;
break;
}
p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
wpa_s->pending_pd_before_join &&
(os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
wpa_s->p2p_fallback_to_go_neg) {
wpa_s->pending_pd_before_join = 0;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
"during p2p_connect-auto");
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_FALLBACK_TO_GO_NEG
"reason=no-ACK-to-PD-Req");
wpas_p2p_fallback_to_go_neg(wpa_s, 0);
return;
}
}
static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
{
struct wpa_supplicant *wpa_s = work->wpa_s;
struct send_action_work *awork = work->ctx;
if (deinit) {
if (work->started) {
eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
wpa_s, NULL);
wpa_s->p2p_send_action_work = NULL;
offchannel_send_action_done(wpa_s);
}
os_free(awork);
return;
}
if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
awork->bssid, awork->buf, awork->len,
awork->wait_time,
wpas_p2p_send_action_tx_status, 1) < 0) {
os_free(awork);
radio_work_done(work);
return;
}
wpa_s->p2p_send_action_work = work;
}
static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
unsigned int freq, const u8 *dst,
const u8 *src, const u8 *bssid, const u8 *buf,
size_t len, unsigned int wait_time)
{
struct send_action_work *awork;
if (wpa_s->p2p_send_action_work) {
wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
return -1;
}
awork = os_zalloc(sizeof(*awork) + len);
if (awork == NULL)
return -1;
awork->freq = freq;
os_memcpy(awork->dst, dst, ETH_ALEN);
os_memcpy(awork->src, src, ETH_ALEN);
os_memcpy(awork->bssid, bssid, ETH_ALEN);
awork->len = len;
awork->wait_time = wait_time;
os_memcpy(awork->buf, buf, len);
if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
wpas_send_action_cb, awork) < 0) {
os_free(awork);
return -1;
}
return 0;
}
static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
const u8 *src, const u8 *bssid, const u8 *buf,
size_t len, unsigned int wait_time)
{
struct wpa_supplicant *wpa_s = ctx;
int listen_freq = -1, send_freq = -1;
if (wpa_s->p2p_listen_work)
listen_freq = wpa_s->p2p_listen_work->freq;
if (wpa_s->p2p_send_action_work)
send_freq = wpa_s->p2p_send_action_work->freq;
if (listen_freq != (int) freq && send_freq != (int) freq) {
wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
listen_freq, send_freq);
return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
len, wait_time);
}
wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
wait_time,
wpas_p2p_send_action_tx_status, 1);
}
static void wpas_send_action_done(void *ctx)
{
struct wpa_supplicant *wpa_s = ctx;
if (wpa_s->p2p_send_action_work) {
eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
wpa_s, NULL);
os_free(wpa_s->p2p_send_action_work->ctx);
radio_work_done(wpa_s->p2p_send_action_work);
wpa_s->p2p_send_action_work = NULL;
}
offchannel_send_action_done(wpa_s);
}
static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
struct p2p_go_neg_results *params)
{
if (wpa_s->go_params == NULL) {
wpa_s->go_params = os_malloc(sizeof(*params));
if (wpa_s->go_params == NULL)
return -1;
}
os_memcpy(wpa_s->go_params, params, sizeof(*params));
return 0;
}
static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
struct p2p_go_neg_results *res)
{
wpa_s->group_formation_reported = 0;
wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
" dev_addr " MACSTR " wps_method %d",
MAC2STR(res->peer_interface_addr),
MAC2STR(res->peer_device_addr), res->wps_method);
wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
res->ssid, res->ssid_len);
wpa_supplicant_ap_deinit(wpa_s);
wpas_copy_go_neg_results(wpa_s, res);
if (res->wps_method == WPS_PBC) {
wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
#ifdef CONFIG_WPS_NFC
} else if (res->wps_method == WPS_NFC) {
wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
res->peer_interface_addr,
wpa_s->parent->p2p_oob_dev_pw,
wpa_s->parent->p2p_oob_dev_pw_id, 1,
wpa_s->parent->p2p_oob_dev_pw_id ==
DEV_PW_NFC_CONNECTION_HANDOVER ?
wpa_s->parent->p2p_peer_oob_pubkey_hash :
NULL,
NULL, 0, 0);
#endif /* CONFIG_WPS_NFC */
} else {
u16 dev_pw_id = DEV_PW_DEFAULT;
if (wpa_s->p2p_wps_method == WPS_P2PS)
dev_pw_id = DEV_PW_P2PS_DEFAULT;
if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
wpa_s->p2p_pin, 1, dev_pw_id);
}
}
static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
struct wpa_ssid *ssid)
{
struct wpa_ssid *persistent;
struct psk_list_entry *psk;
struct hostapd_data *hapd;
if (!wpa_s->ap_iface)
return;
persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
ssid->ssid_len);
if (persistent == NULL)
return;
hapd = wpa_s->ap_iface->bss[0];
dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
list) {
struct hostapd_wpa_psk *hpsk;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
MACSTR " psk=%d",
MAC2STR(psk->addr), psk->p2p);
hpsk = os_zalloc(sizeof(*hpsk));
if (hpsk == NULL)
break;
os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
if (psk->p2p)
os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
else
os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
hpsk->next = hapd->conf->ssid.wpa_psk;
hapd->conf->ssid.wpa_psk = hpsk;
}
}
static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
{
unsigned int i;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
wpa_s->p2p_group_common_freqs_num);
for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
i, wpa_s->p2p_group_common_freqs[i]);
}
static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
struct p2p_go_neg_results *params)
{
unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
wpa_s->p2p_group_common_freqs_num = 0;
os_free(wpa_s->p2p_group_common_freqs);
wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
if (!wpa_s->p2p_group_common_freqs)
return;
for (i = 0; i < len; i++) {
if (!wpa_s->go_params->freq_list[i])
break;
wpa_s->p2p_group_common_freqs[i] =
wpa_s->go_params->freq_list[i];
}
wpa_s->p2p_group_common_freqs_num = i;
}
static void p2p_config_write(struct wpa_supplicant *wpa_s)
{
#ifndef CONFIG_NO_CONFIG_WRITE
if (wpa_s->parent->conf->update_config &&
wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
#endif /* CONFIG_NO_CONFIG_WRITE */
}
static void p2p_go_configured(void *ctx, void *data)
{
struct wpa_supplicant *wpa_s = ctx;
struct p2p_go_neg_results *params = data;
struct wpa_ssid *ssid;
int network_id = -1;
p2p_go_save_group_common_freqs(wpa_s, params);
p2p_go_dump_common_freqs(wpa_s);
ssid = wpa_s->current_ssid;
if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
if (wpa_s->global->p2p_group_formation == wpa_s)
wpa_s->global->p2p_group_formation = NULL;
wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
params->passphrase[0] == '\0' ?
params->psk : NULL,
params->passphrase,
wpa_s->global->p2p_dev_addr,
params->persistent_group, "");
wpa_s->group_formation_reported = 1;
if (wpa_s->parent->p2ps_join_addr_valid) {
wpa_dbg(wpa_s, MSG_DEBUG,
"P2PS: Setting default PIN for " MACSTR,
MAC2STR(wpa_s->parent->p2ps_join_addr));
wpa_supplicant_ap_wps_pin(wpa_s,
wpa_s->parent->p2ps_join_addr,
"12345670", NULL, 0, 0);
wpa_s->parent->p2ps_join_addr_valid = 0;
}
os_get_reltime(&wpa_s->global->p2p_go_wait_client);
if (params->persistent_group) {
network_id = wpas_p2p_store_persistent_group(
wpa_s->parent, ssid,
wpa_s->global->p2p_dev_addr);
wpas_p2p_add_psk_list(wpa_s, ssid);
}
if (network_id < 0)
network_id = ssid->id;
wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
wpas_p2p_cross_connect_setup(wpa_s);
wpas_p2p_set_group_idle_timeout(wpa_s);
if (wpa_s->p2p_first_connection_timeout) {
wpa_dbg(wpa_s, MSG_DEBUG,
"P2P: Start group formation timeout of %d seconds until first data connection on GO",
wpa_s->p2p_first_connection_timeout);
wpa_s->p2p_go_group_formation_completed = 0;
wpa_s->global->p2p_group_formation = wpa_s;
eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
eloop_register_timeout(
wpa_s->p2p_first_connection_timeout, 0,
wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
}
return;
}
wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
params->peer_interface_addr)) {
wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
"filtering");
return;
}
if (params->wps_method == WPS_PBC) {
wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
params->peer_device_addr);
#ifdef CONFIG_WPS_NFC
} else if (params->wps_method == WPS_NFC) {
if (wpa_s->parent->p2p_oob_dev_pw_id !=
DEV_PW_NFC_CONNECTION_HANDOVER &&
!wpa_s->parent->p2p_oob_dev_pw) {
wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
return;
}
wpas_ap_wps_add_nfc_pw(
wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
wpa_s->parent->p2p_oob_dev_pw,
wpa_s->parent->p2p_peer_oob_pk_hash_known ?
wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
#endif /* CONFIG_WPS_NFC */
} else if (wpa_s->p2p_pin[0])
wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
wpa_s->p2p_pin, NULL, 0, 0);
os_free(wpa_s->go_params);
wpa_s->go_params = NULL;
}
static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
struct p2p_go_neg_results *params,
int group_formation)
{
struct wpa_ssid *ssid;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
"results");
return;
}
ssid = wpa_config_add_network(wpa_s->conf);
if (ssid == NULL) {
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
return;
}
wpa_s->show_group_started = 0;
wpa_s->p2p_go_group_formation_completed = 0;
wpa_s->group_formation_reported = 0;
wpa_config_set_network_defaults(ssid);
ssid->temporary = 1;
ssid->p2p_group = 1;
ssid->p2p_persistent_group = params->persistent_group;
ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
WPAS_MODE_P2P_GO;
ssid->frequency = params->freq;
ssid->ht40 = params->ht40;
ssid->vht = params->vht;
ssid->ssid = os_zalloc(params->ssid_len + 1);
if (ssid->ssid) {
os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
ssid->ssid_len = params->ssid_len;
}
ssid->auth_alg = WPA_AUTH_ALG_OPEN;
ssid->key_mgmt = WPA_KEY_MGMT_PSK;
ssid->proto = WPA_PROTO_RSN;
ssid->pairwise_cipher = WPA_CIPHER_CCMP;
ssid->group_cipher = WPA_CIPHER_CCMP;
if (params->freq > 56160) {
/*
* Enable GCMP instead of CCMP as pairwise_cipher and
* group_cipher in 60 GHz.
*/
ssid->pairwise_cipher = WPA_CIPHER_GCMP;
ssid->group_cipher = WPA_CIPHER_GCMP;
}
if (os_strlen(params->passphrase) > 0) {
ssid->passphrase = os_strdup(params->passphrase);
if (ssid->passphrase == NULL) {
wpa_msg_global(wpa_s, MSG_ERROR,
"P2P: Failed to copy passphrase for GO");
wpa_config_remove_network(wpa_s->conf, ssid->id);
return;
}
} else
ssid->passphrase = NULL;
ssid->psk_set = params->psk_set;
if (ssid->psk_set)
os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
else if (ssid->passphrase)
wpa_config_update_psk(ssid);
ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
wpa_s->ap_configured_cb = p2p_go_configured;
wpa_s->ap_configured_cb_ctx = wpa_s;
wpa_s->ap_configured_cb_data = wpa_s->go_params;
wpa_s->scan_req = NORMAL_SCAN_REQ;
wpa_s->connect_without_scan = ssid;
wpa_s->reassociate = 1;
wpa_s->disconnected = 0;
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
"start GO)");
wpa_supplicant_req_scan(wpa_s, 0, 0);
}
static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
const struct wpa_supplicant *src)
{
struct wpa_config *d;
const struct wpa_config *s;
d = dst->conf;
s = src->conf;
#define C(n) if (s->n) d->n = os_strdup(s->n)
C(device_name);
C(manufacturer);
C(model_name);
C(model_number);
C(serial_number);
C(config_methods);
#undef C
os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
os_memcpy(d->sec_device_type, s->sec_device_type,
sizeof(d->sec_device_type));
d->num_sec_device_types = s->num_sec_device_types;
d->p2p_group_idle = s->p2p_group_idle;
d->p2p_intra_bss = s->p2p_intra_bss;
d->persistent_reconnect = s->persistent_reconnect;
d->max_num_sta = s->max_num_sta;
d->pbc_in_m1 = s->pbc_in_m1;
d->ignore_old_scan_res = s->ignore_old_scan_res;
d->beacon_int = s->beacon_int;
d->dtim_period = s->dtim_period;
d->p2p_go_ctwindow = s->p2p_go_ctwindow;
d->disassoc_low_ack = s->disassoc_low_ack;
d->disable_scan_offload = s->disable_scan_offload;
d->passive_scan = s->passive_scan;
if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
}
d->p2p_cli_probe = s->p2p_cli_probe;
}
static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
char *ifname, size_t len)
{
char *ifname_ptr = wpa_s->ifname;
if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
}
os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
if (os_strlen(ifname) >= IFNAMSIZ &&
os_strlen(wpa_s->ifname) < IFNAMSIZ) {
int res;
/* Try to avoid going over the IFNAMSIZ length limit */
res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
if (os_snprintf_error(len, res) && len)
ifname[len - 1] = '\0';
}
}
static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
enum wpa_driver_if_type type)
{
char ifname[120], force_ifname[120];
if (wpa_s->pending_interface_name[0]) {
wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
"- skip creation of a new one");
if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
"unknown?! ifname='%s'",
wpa_s->pending_interface_name);
return -1;
}
return 0;
}
wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
force_ifname[0] = '\0';
wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
ifname);
wpa_s->p2p_group_idx++;
wpa_s->pending_interface_type = type;
if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
wpa_s->pending_interface_addr, NULL) < 0) {
wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
"interface");
return -1;
}
if (force_ifname[0]) {
wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
force_ifname);
os_strlcpy(wpa_s->pending_interface_name, force_ifname,
sizeof(wpa_s->pending_interface_name));
} else
os_strlcpy(wpa_s->pending_interface_name, ifname,
sizeof(wpa_s->pending_interface_name));
wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
MACSTR, wpa_s->pending_interface_name,
MAC2STR(wpa_s->pending_interface_addr));
return 0;
}
static void wpas_p2p_remove_pending_group_interface(
struct wpa_supplicant *wpa_s)
{
if (!wpa_s->pending_interface_name[0] ||
is_zero_ether_addr(wpa_s->pending_interface_addr))
return; /* No pending virtual interface */
wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
wpa_s->pending_interface_name);
wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
wpa_s->pending_interface_name);
os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
wpa_s->pending_interface_name[0] = '\0';
wpa_s->global->pending_group_iface_for_p2ps = 0;
}
static struct wpa_supplicant *
wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
{
struct wpa_interface iface;
struct wpa_supplicant *group_wpa_s;
if (!wpa_s->pending_interface_name[0]) {
wpa_printf(MSG_ERROR, "P2P: No pending group interface");
if (!wpas_p2p_create_iface(wpa_s))
return NULL;
/*
* Something has forced us to remove the pending interface; try
* to create a new one and hope for the best that we will get
* the same local address.
*/
if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
WPA_IF_P2P_CLIENT) < 0)
return NULL;
}
os_memset(&iface, 0, sizeof(iface));
iface.ifname = wpa_s->pending_interface_name;
iface.driver = wpa_s->driver->name;
if (wpa_s->conf->ctrl_interface == NULL &&
wpa_s->parent != wpa_s &&
wpa_s->p2p_mgmt &&
(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
else
iface.ctrl_interface = wpa_s->conf->ctrl_interface;
iface.driver_param = wpa_s->conf->driver_param;
group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
if (group_wpa_s == NULL) {
wpa_printf(MSG_ERROR, "P2P: Failed to create new "
"wpa_supplicant interface");
return NULL;
}
wpa_s->pending_interface_name[0] = '\0';
group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
P2P_GROUP_INTERFACE_CLIENT;
wpa_s->global->p2p_group_formation = group_wpa_s;
wpa_s->global->pending_group_iface_for_p2ps = 0;
wpas_p2p_clone_config(group_wpa_s, wpa_s);
return group_wpa_s;
}
static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
void *timeout_ctx)
{
struct wpa_supplicant *wpa_s = eloop_ctx;
wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
wpas_p2p_group_formation_failed(wpa_s, 0);
}
static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
int already_deleted)
{
eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
if (wpa_s->global->p2p)
p2p_group_formation_failed(wpa_s->global->p2p);
wpas_group_formation_completed(wpa_s, 0, already_deleted);
}
static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
{
wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
wpa_s->global->p2p_fail_on_wps_complete = 0;
}
void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
{
if (wpa_s->global->p2p_group_formation != wpa_s)
return;
/* Speed up group formation timeout since this cannot succeed */
eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
wpa_s->parent, NULL);
}
static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
{
struct wpa_supplicant *wpa_s = ctx;
if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
wpa_drv_cancel_remain_on_channel(wpa_s);
wpa_s->off_channel_freq = 0;
wpa_s->roc_waiting_drv_freq = 0;
}
if (res->status) {
wpa_msg_global(wpa_s, MSG_INFO,
P2P_EVENT_GO_NEG_FAILURE "status=%d",
res->status);
wpas_notify_p2p_go_neg_completed(wpa_s, res);
wpas_p2p_remove_pending_group_interface(wpa_s);
return;
}
if (wpa_s->p2p_go_ht40)
res->ht40 = 1;
if (wpa_s->p2p_go_vht)
res->vht = 1;
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
"freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
" wps_method=%s",
res->role_go ? "GO" : "client", res->freq, res->ht40,
MAC2STR(res->peer_device_addr),
MAC2STR(res->peer_interface_addr),
p2p_wps_method_text(res->wps_method));
wpas_notify_p2p_go_neg_completed(wpa_s, res);
if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
struct wpa_ssid *ssid;
ssid = wpa_config_get_network(wpa_s->conf,
wpa_s->p2p_persistent_id);
if (ssid && ssid->disabled == 2 &&
ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
size_t len = os_strlen(ssid->passphrase);
wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
"on requested persistent group");
os_memcpy(res->passphrase, ssid->passphrase, len);
res->passphrase[len] = '\0';
}
}
if (wpa_s->create_p2p_iface) {
struct wpa_supplicant *group_wpa_s =
wpas_p2p_init_group_interface(wpa_s, res->role_go);
if (group_wpa_s == NULL) {
wpas_p2p_remove_pending_group_interface(wpa_s);
eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
wpa_s, NULL);
wpas_p2p_group_formation_failed(wpa_s, 1);
return;
}
if (group_wpa_s != wpa_s) {
os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
sizeof(group_wpa_s->p2p_pin));
group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
}
os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
wpa_s->pending_interface_name[0] = '\0';
group_wpa_s->p2p_in_provisioning = 1;
if (res->role_go)
wpas_start_wps_go(group_wpa_s, res, 1);
else
wpas_start_wps_enrollee(group_wpa_s, res);
} else {
wpa_s->p2p_in_provisioning = 1;
wpa_s->global->p2p_group_formation = wpa_s;
if (res->role_go)
wpas_start_wps_go(wpa_s, res, 1);
else
wpas_start_wps_enrollee(ctx, res);
}
wpa_s->p2p_long_listen = 0;
eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
eloop_register_timeout(15 + res->peer_config_timeout / 100,
(res->peer_config_timeout % 100) * 10000,
wpas_p2p_group_formation_timeout, wpa_s, NULL);
}
static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
u8 go_intent)
{
struct wpa_supplicant *wpa_s = ctx;
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
" dev_passwd_id=%u go_intent=%u", MAC2STR(src),
dev_passwd_id, go_intent);
wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
}
static void wpas_dev_found(void *ctx, const u8 *addr,
const struct p2p_peer_info *info,
int new_device)
{
#ifndef CONFIG_NO_STDOUT_DEBUG
struct wpa_supplicant *wpa_s = ctx;
char devtype[WPS_DEV_TYPE_BUFSIZE];
char *wfd_dev_info_hex = NULL;
#ifdef CONFIG_WIFI_DISPLAY
wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
WFD_SUBELEM_DEVICE_INFO);
#endif /* CONFIG_WIFI_DISPLAY */
if (info->p2ps_instance) {
char str[256];
const u8 *buf = wpabuf_head(info->p2ps_instance);
size_t len = wpabuf_len(info->p2ps_instance);
while (len) {
u32 id;
u16 methods;
u8 str_len;
if (len < 4 + 2 + 1)
break;
id = WPA_GET_LE32(buf);
buf += sizeof(u32);
methods = WPA_GET_BE16(buf);
buf += sizeof(u16);
str_len = *buf++;
if (str_len > len - 4 - 2 - 1)
break;
os_memcpy(str, buf, str_len);
str[str_len] = '\0';
buf += str_len;
len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
wpa_msg_global(wpa_s, MSG_INFO,
P2P_EVENT_DEVICE_FOUND MACSTR
" p2p_dev_addr=" MACSTR
" pri_dev_type=%s name='%s'"
" config_methods=0x%x"
" dev_capab=0x%x"
" group_capab=0x%x"
" adv_id=%x asp_svc=%s%s",
MAC2STR(addr),
MAC2STR(info->p2p_device_addr),
wps_dev_type_bin2str(
info->pri_dev_type,
devtype, sizeof(devtype)),
info->device_name, methods,
info->dev_capab, info->group_capab,
id, str,
info->vendor_elems ?
" vendor_elems=1" : "");
}
goto done;
}
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
" p2p_dev_addr=" MACSTR
" pri_dev_type=%s name='%s' config_methods=0x%x "
"dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
MAC2STR(addr), MAC2STR(info->p2p_device_addr),
wps_dev_type_bin2str(info->pri_dev_type, devtype,
sizeof(devtype)),
info->device_name, info->config_methods,
info->dev_capab, info->group_capab,
wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
wfd_dev_info_hex ? wfd_dev_info_hex : "",
info->vendor_elems ? " vendor_elems=1" : "",
new_device);
done:
os_free(wfd_dev_info_hex);
#endif /* CONFIG_NO_STDOUT_DEBUG */
wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
}
static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
{
struct wpa_supplicant *wpa_s = ctx;
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
"p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
wpas_notify_p2p_device_lost(wpa_s, dev_addr);
}
static void wpas_find_stopped(void *ctx)
{
struct wpa_supplicant *wpa_s = ctx;
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
wpas_notify_p2p_find_stopped(wpa_s);
}
struct wpas_p2p_listen_work {
unsigned int freq;
unsigned int duration;
struct wpabuf *probe_resp_ie;
};
static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
{
if (lwork == NULL)
return;
wpabuf_free(lwork->probe_resp_ie);
os_free(lwork);
}
static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
{
struct wpas_p2p_listen_work *lwork;
if (!wpa_s->p2p_listen_work)
return;
lwork = wpa_s->p2p_listen_work->ctx;
wpas_p2p_listen_work_free(lwork);
radio_work_done(wpa_s->p2p_listen_work);
wpa_s->p2p_listen_work = NULL;
}
static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
{
struct wpa_supplicant *wpa_s = work->wpa_s;
struct wpas_p2p_listen_work *lwork = work->ctx;
unsigned int duration;
if (deinit) {
if (work->started) {
wpa_s->p2p_listen_work = NULL;
wpas_stop_listen(wpa_s);
}
wpas_p2p_listen_work_free(lwork);
return;
}
wpa_s->p2p_listen_work = work;
wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
"report received Probe Request frames");
wpas_p2p_listen_work_done(wpa_s);
return;
}
wpa_s->pending_listen_freq = lwork->freq;
wpa_s->pending_listen_duration = lwork->duration;
duration = lwork->duration;
#ifdef CONFIG_TESTING_OPTIONS
if (wpa_s->extra_roc_dur) {
wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
duration, duration + wpa_s->extra_roc_dur);
duration += wpa_s->extra_roc_dur;
}
#endif /* CONFIG_TESTING_OPTIONS */
if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
"to remain on channel (%u MHz) for Listen "
"state", lwork->freq);
wpas_p2p_listen_work_done(wpa_s);
wpa_s->pending_listen_freq = 0;
return;
}
wpa_s->off_channel_freq = 0;
wpa_s->roc_waiting_drv_freq = lwork->freq;
}
static int wpas_start_listen(void *ctx, unsigned int freq,
unsigned int duration,
const struct wpabuf *probe_resp_ie)
{
struct wpa_supplicant *wpa_s = ctx;
struct wpas_p2p_listen_work *lwork;
if (wpa_s->p2p_listen_work) {
wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
return -1;
}
lwork = os_zalloc(sizeof(*lwork));
if (lwork == NULL)
return -1;
lwork->freq = freq;
lwork->duration = duration;
if (probe_resp_ie) {
lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
if (lwork->probe_resp_ie == NULL) {
wpas_p2p_listen_work_free(lwork);
return -1;
}
}
if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
lwork) < 0) {
wpas_p2p_listen_work_free(lwork);
return -1;
}
return 0;
}
static void wpas_stop_listen(void *ctx)
{
struct wpa_supplicant *wpa_s = ctx;
if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
wpa_drv_cancel_remain_on_channel(wpa_s);
wpa_s->off_channel_freq = 0;
wpa_s->roc_waiting_drv_freq = 0;
}
wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
/*
* Don't cancel Probe Request RX reporting for a connected P2P Client
* handling Probe Request frames.
*/
if (!wpa_s->p2p_cli_probe)
wpa_drv_probe_req_report(wpa_s, 0);
wpas_p2p_listen_work_done(wpa_s);
}
static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
unsigned int freq)
{
struct wpa_supplicant *wpa_s = ctx;
return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
freq);
}
static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
const u8 *peer, const char *params,
unsigned int generated_pin)
{
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s", MAC2STR(peer), generated_pin, params);
}
static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
const u8 *peer, const char *params)
{
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s", MAC2STR(peer), params);
}
static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
const u8 *dev_addr, const u8 *pri_dev_type,
const char *dev_name, u16 supp_config_methods,
u8 dev_capab, u8 group_capab, const u8 *group_id,
size_t group_id_len)
{
struct wpa_supplicant *wpa_s = ctx;
char devtype[WPS_DEV_TYPE_BUFSIZE];
char params[300];
u8 empty_dev_type[8];
unsigned int generated_pin = 0;
struct wpa_supplicant *group = NULL;
int res;
if (group_id) {
for (group = wpa_s->global->ifaces; group; group = group->next)
{
struct wpa_ssid *s = group->current_ssid;
if (s != NULL &&
s->mode == WPAS_MODE_P2P_GO &&
group_id_len - ETH_ALEN == s->ssid_len &&
os_memcmp(group_id + ETH_ALEN, s->ssid,
s->ssid_len) == 0)
break;
}
}
if (pri_dev_type == NULL) {
os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
pri_dev_type = empty_dev_type;
}
res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
" pri_dev_type=%s name='%s' config_methods=0x%x "
"dev_capab=0x%x group_capab=0x%x%s%s",
MAC2STR(dev_addr),
wps_dev_type_bin2str(pri_dev_type, devtype,
sizeof(devtype)),
dev_name, supp_config_methods, dev_capab, group_capab,
group ? " group=" : "",
group ? group->ifname : "");
if (os_snprintf_error(sizeof(params), res))
wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
params[sizeof(params) - 1] = '\0';
if (config_methods & WPS_CONFIG_DISPLAY) {
generated_pin = wps_generate_pin();
wpas_prov_disc_local_display(wpa_s, peer, params,
generated_pin);
} else if (config_methods & WPS_CONFIG_KEYPAD)
wpas_prov_disc_local_keypad(wpa_s, peer, params);
else if (config_methods & WPS_CONFIG_PUSHBUTTON)
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
MACSTR "%s", MAC2STR(peer), params);
wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
P2P_PROV_DISC_SUCCESS,
config_methods, generated_pin);
}
static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
{
struct wpa_supplicant *wpa_s = ctx;
unsigned int generated_pin = 0;
char params[20];
if (wpa_s->pending_pd_before_join &&
(os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
wpa_s->pending_pd_before_join = 0;
wpa_printf(MSG_DEBUG, "P2P: Starting pending "
"join-existing-group operation");
wpas_p2p_join_start(wpa_s, 0, NULL, 0);
return;
}
if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
int res;
res = os_snprintf(params, sizeof(params), " peer_go=%d",
wpa_s->pending_pd_use == AUTO_PD_JOIN);
if (os_snprintf_error(sizeof(params), res))
params[sizeof(params) - 1] = '\0';
} else
params[0] = '\0';
if (config_methods & WPS_CONFIG_DISPLAY)
wpas_prov_disc_local_keypad(wpa_s, peer, params);
else if (config_methods & WPS_CONFIG_KEYPAD) {
generated_pin = wps_generate_pin();
wpas_prov_disc_local_display(wpa_s, peer, params,
generated_pin);
} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
MACSTR "%s", MAC2STR(peer), params);
wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
P2P_PROV_DISC_SUCCESS,
config_methods, generated_pin);
}
static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
enum p2p_prov_disc_status status,
u32 adv_id, const u8 *adv_mac,
const char *deferred_session_resp)
{
struct wpa_supplicant *wpa_s = ctx;
if (wpa_s->p2p_fallback_to_go_neg) {
wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
"failed - fall back to GO Negotiation");
wpa_msg_global(wpa_s->parent, MSG_INFO,
P2P_EVENT_FALLBACK_TO_GO_NEG
"reason=PD-failed");
wpas_p2p_fallback_to_go_neg(wpa_s, 0);
return;
}
if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
wpa_s->pending_pd_before_join = 0;
wpa_printf(MSG_DEBUG, "P2P: Starting pending "
"join-existing-group operation (no ACK for PD "
"Req attempts)");
wpas_p2p_join_start(wpa_s, 0, NULL, 0);
return;
}
if (adv_id && adv_mac && deferred_session_resp) {
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
" p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
" deferred_session_resp='%s'",
MAC2STR(peer), status, adv_id,
deferred_session_resp);
} else if (adv_id && adv_mac) {
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
" p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
MAC2STR(peer), status, adv_id);
} else {
wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
" p2p_dev_addr=" MACSTR " status=%d",
MAC2STR(peer), status);
}
wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
status, 0, 0);
}
static int freq_included(const struct p2p_channels *channels, unsigned int freq)
{
if (channels == NULL)
return 1; /* Assume no restrictions */
return p2p_channels_includes_freq(channels, freq);
}
/**
* Pick the best frequency to use from all the currently used frequencies.
*/
static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
struct wpa_used_freq_data *freqs,
unsigned int num)
{
unsigned int i, c;
/* find a candidate freq that is supported by P2P */
for (c = 0; c < num; c++)
if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
break;
if (c == num)
return 0;
/* once we have a candidate, try to find a 'better' one */
for (i = c + 1; i < num; i++) {
if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
continue;
/*
* 1. Infrastructure station interfaces have higher preference.
* 2. P2P Clients have higher preference.
* 3. All others.
*/
if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
c = i;
break;
}
if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
c = i;
}
return freqs[c].freq;
}
static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
const u8 *go_dev_addr, const u8 *ssid,
size_t ssid_len, int *go, u8 *group_bssid,
int *force_freq, int persistent_group,
const struct p2p_channels *channels,
int dev_pw_id)
{
struct wpa_supplicant *wpa_s = ctx;
struct wpa_ssid *s;
struct wpa_used_freq_data *freqs;
struct wpa_supplicant *grp;
int best_freq;
if (!persistent_group) {
wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
" to join an active group (SSID: %s)",
MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
(os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
== 0 ||
os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
wpa_printf(MSG_DEBUG, "P2P: Accept previously "
"authorized invitation");
goto accept_inv;
}
#ifdef CONFIG_WPS_NFC
if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
wpa_s->p2p_wps_method = WPS_NFC;
wpa_s->pending_join_wps_method = WPS_NFC;
os_memcpy(wpa_s->pending_join_dev_addr,
go_dev_addr, ETH_ALEN);
os_memcpy(wpa_s->pending_join_ifa