2008-02-28 02:34:43 +01:00
|
|
|
/*
|
|
|
|
* WPA Supplicant / UDP socket -based control interface
|
|
|
|
* Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-28 02:34:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "eapol_supp/eapol_supp_sm.h"
|
|
|
|
#include "wpa_supplicant_i.h"
|
|
|
|
#include "ctrl_iface.h"
|
2009-11-29 16:51:55 +01:00
|
|
|
#include "common/wpa_ctrl.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define COOKIE_LEN 8
|
|
|
|
|
|
|
|
/* Per-interface ctrl_iface */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct wpa_ctrl_dst - Internal data structure of control interface monitors
|
|
|
|
*
|
|
|
|
* This structure is used to store information about registered control
|
|
|
|
* interface monitors into struct wpa_supplicant. This data is private to
|
|
|
|
* ctrl_iface_udp.c and should not be touched directly from other files.
|
|
|
|
*/
|
|
|
|
struct wpa_ctrl_dst {
|
|
|
|
struct wpa_ctrl_dst *next;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
struct sockaddr_in addr;
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
int debug_level;
|
|
|
|
int errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ctrl_iface_priv {
|
|
|
|
struct wpa_supplicant *wpa_s;
|
|
|
|
int sock;
|
|
|
|
struct wpa_ctrl_dst *ctrl_dst;
|
|
|
|
u8 cookie[COOKIE_LEN];
|
|
|
|
};
|
|
|
|
|
2016-03-04 10:20:19 +01:00
|
|
|
struct ctrl_iface_global_priv {
|
|
|
|
int sock;
|
|
|
|
struct wpa_ctrl_dst *ctrl_dst;
|
|
|
|
u8 cookie[COOKIE_LEN];
|
|
|
|
};
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
|
|
|
|
const char *ifname, int sock,
|
|
|
|
struct wpa_ctrl_dst **head,
|
2008-02-28 02:34:43 +01:00
|
|
|
int level, const char *buf,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
|
2016-03-04 10:20:19 +01:00
|
|
|
static void wpas_ctrl_iface_free_dst(struct wpa_ctrl_dst *dst)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *prev;
|
|
|
|
|
|
|
|
while (dst) {
|
|
|
|
prev = dst;
|
|
|
|
dst = dst->next;
|
|
|
|
os_free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 *from,
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
struct sockaddr_in *from,
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
#endif /* CONFIG_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
dst = os_zalloc(sizeof(*dst));
|
|
|
|
if (dst == NULL)
|
|
|
|
return -1;
|
2014-02-24 13:22:16 +01:00
|
|
|
os_memcpy(&dst->addr, from, sizeof(*from));
|
2008-02-28 02:34:43 +01:00
|
|
|
dst->addrlen = fromlen;
|
|
|
|
dst->debug_level = MSG_INFO;
|
2016-03-04 10:20:18 +01:00
|
|
|
dst->next = *head;
|
|
|
|
*head = dst;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
|
|
|
|
inet_ntop(AF_INET6, &from->sin6_addr, addr, sizeof(*from)),
|
|
|
|
ntohs(from->sin6_port));
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
|
|
|
|
inet_ntoa(from->sin_addr), ntohs(from->sin_port));
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
static int wpa_supplicant_ctrl_iface_detach(struct wpa_ctrl_dst **head,
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 *from,
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
struct sockaddr_in *from,
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst, *prev = NULL;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
dst = *head;
|
2008-02-28 02:34:43 +01:00
|
|
|
while (dst) {
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
if (from->sin6_port == dst->addr.sin6_port &&
|
|
|
|
!os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
|
|
|
|
sizeof(from->sin6_addr))) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s:%d",
|
|
|
|
inet_ntop(AF_INET6, &from->sin6_addr, addr,
|
|
|
|
sizeof(*from)),
|
|
|
|
ntohs(from->sin6_port));
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
|
|
|
|
from->sin_port == dst->addr.sin_port) {
|
2013-08-24 22:31:06 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached "
|
|
|
|
"%s:%d", inet_ntoa(from->sin_addr),
|
|
|
|
ntohs(from->sin_port));
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (prev == NULL)
|
2016-03-04 10:20:18 +01:00
|
|
|
*head = dst->next;
|
2008-02-28 02:34:43 +01:00
|
|
|
else
|
|
|
|
prev->next = dst->next;
|
|
|
|
os_free(dst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
prev = dst;
|
|
|
|
dst = dst->next;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 *from,
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
struct sockaddr_in *from,
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen,
|
|
|
|
char *level)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
|
|
|
|
|
|
|
|
dst = priv->ctrl_dst;
|
|
|
|
while (dst) {
|
2014-02-24 13:22:16 +01:00
|
|
|
#if CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
if (from->sin6_port == dst->addr.sin6_port &&
|
|
|
|
!os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
|
|
|
|
sizeof(from->sin6_addr))) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level %s:%d",
|
|
|
|
inet_ntop(AF_INET6, &from->sin6_addr, addr,
|
|
|
|
sizeof(*from)),
|
|
|
|
ntohs(from->sin6_port));
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
|
|
|
|
from->sin_port == dst->addr.sin_port) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor "
|
|
|
|
"level %s:%d", inet_ntoa(from->sin_addr),
|
|
|
|
ntohs(from->sin_port));
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
dst->debug_level = atoi(level);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dst = dst->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
wpa_supplicant_ctrl_iface_get_cookie(struct ctrl_iface_priv *priv,
|
|
|
|
size_t *reply_len)
|
|
|
|
{
|
|
|
|
char *reply;
|
|
|
|
reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
|
|
|
|
if (reply == NULL) {
|
|
|
|
*reply_len = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memcpy(reply, "COOKIE=", 7);
|
|
|
|
wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
|
|
|
|
priv->cookie, COOKIE_LEN);
|
|
|
|
|
|
|
|
*reply_len = 7 + 2 * COOKIE_LEN;
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
|
|
|
|
void *sock_ctx)
|
|
|
|
{
|
|
|
|
struct wpa_supplicant *wpa_s = eloop_ctx;
|
|
|
|
struct ctrl_iface_priv *priv = sock_ctx;
|
|
|
|
char buf[256], *pos;
|
|
|
|
int res;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 from;
|
|
|
|
#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
struct sockaddr_in from;
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
char *reply = NULL;
|
|
|
|
size_t reply_len = 0;
|
|
|
|
int new_attached = 0;
|
|
|
|
u8 cookie[COOKIE_LEN];
|
|
|
|
|
|
|
|
res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
|
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (res < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-08-05 19:01:07 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, addr, sizeof(from));
|
|
|
|
if (os_strcmp(addr, "::1")) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected source %s",
|
|
|
|
addr);
|
|
|
|
}
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
|
|
|
|
/*
|
|
|
|
* The OS networking stack is expected to drop this kind of
|
|
|
|
* frames since the socket is bound to only localhost address.
|
|
|
|
* Just in case, drop the frame if it is coming from any other
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
|
|
|
|
"source %s", inet_ntoa(from.sin_addr));
|
|
|
|
return;
|
|
|
|
}
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2012-08-05 19:01:07 +02:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
buf[res] = '\0';
|
|
|
|
|
|
|
|
if (os_strcmp(buf, "GET_COOKIE") == 0) {
|
|
|
|
reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Require that the client includes a prefix with the 'cookie' value
|
|
|
|
* fetched with GET_COOKIE command. This is used to verify that the
|
|
|
|
* client has access to a bidirectional link over UDP in order to
|
|
|
|
* avoid attacks using forged localhost IP address even if the OS does
|
|
|
|
* not block such frames from remote destinations.
|
|
|
|
*/
|
|
|
|
if (os_strncmp(buf, "COOKIE=", 7) != 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
|
|
|
|
"drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
|
|
|
|
"request - drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
|
|
|
|
"drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buf + 7 + 2 * COOKIE_LEN;
|
|
|
|
while (*pos == ' ')
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
if (os_strcmp(pos, "ATTACH") == 0) {
|
2016-03-04 10:20:18 +01:00
|
|
|
if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
|
|
|
|
&from, fromlen))
|
2008-02-28 02:34:43 +01:00
|
|
|
reply_len = 1;
|
|
|
|
else {
|
|
|
|
new_attached = 1;
|
|
|
|
reply_len = 2;
|
|
|
|
}
|
|
|
|
} else if (os_strcmp(pos, "DETACH") == 0) {
|
2016-03-04 10:20:18 +01:00
|
|
|
if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst,
|
|
|
|
&from, fromlen))
|
2008-02-28 02:34:43 +01:00
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else if (os_strncmp(pos, "LEVEL ", 6) == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
|
|
|
|
pos + 6))
|
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else {
|
|
|
|
reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos,
|
|
|
|
&reply_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (reply) {
|
|
|
|
sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
|
|
|
os_free(reply);
|
|
|
|
} else if (reply_len == 1) {
|
|
|
|
sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
|
|
|
} else if (reply_len == 2) {
|
|
|
|
sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_attached)
|
|
|
|
eapol_sm_notify_ctrl_attached(wpa_s->eapol);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 11:08:28 +01:00
|
|
|
static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
|
|
|
|
enum wpa_msg_type type,
|
2008-02-28 02:34:43 +01:00
|
|
|
const char *txt, size_t len)
|
|
|
|
{
|
|
|
|
struct wpa_supplicant *wpa_s = ctx;
|
2016-03-04 10:20:19 +01:00
|
|
|
|
|
|
|
if (!wpa_s)
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
2016-03-04 10:20:19 +01:00
|
|
|
|
|
|
|
if (type != WPA_MSG_NO_GLOBAL && wpa_s->global->ctrl_iface) {
|
|
|
|
struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
|
|
|
|
|
|
|
|
if (priv->ctrl_dst) {
|
|
|
|
wpa_supplicant_ctrl_iface_send(
|
|
|
|
wpa_s,
|
|
|
|
type != WPA_MSG_PER_INTERFACE ?
|
|
|
|
NULL : wpa_s->ifname,
|
|
|
|
priv->sock, &priv->ctrl_dst, level, txt, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == WPA_MSG_ONLY_GLOBAL || !wpa_s->ctrl_iface)
|
|
|
|
return;
|
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
|
|
|
|
&wpa_s->ctrl_iface->ctrl_dst,
|
|
|
|
level, txt, len);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ctrl_iface_priv *
|
|
|
|
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_priv *priv;
|
2016-03-08 14:28:02 +01:00
|
|
|
char port_str[40];
|
2012-08-05 19:01:07 +02:00
|
|
|
int port = WPA_CTRL_IFACE_PORT;
|
2016-03-04 10:20:17 +01:00
|
|
|
char *pos;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
int domain = PF_INET6;
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int domain = PF_INET;
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
priv = os_zalloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
|
|
|
return NULL;
|
|
|
|
priv->wpa_s = wpa_s;
|
|
|
|
priv->sock = -1;
|
|
|
|
os_get_random(priv->cookie, COOKIE_LEN);
|
|
|
|
|
|
|
|
if (wpa_s->conf->ctrl_interface == NULL)
|
|
|
|
return priv;
|
|
|
|
|
2016-03-04 10:20:17 +01:00
|
|
|
pos = os_strstr(wpa_s->conf->ctrl_interface, "udp:");
|
|
|
|
if (pos) {
|
|
|
|
pos += 4;
|
|
|
|
port = atoi(pos);
|
|
|
|
if (port <= 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port: %s",
|
|
|
|
wpa_s->conf->ctrl_interface);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 13:22:16 +01:00
|
|
|
priv->sock = socket(domain, SOCK_DGRAM, 0);
|
2008-02-28 02:34:43 +01:00
|
|
|
if (priv->sock < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&addr, 0, sizeof(addr));
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
addr.sin6_family = AF_INET6;
|
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
addr.sin6_addr = in6addr_any;
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
inet_pton(AF_INET6, "::1", &addr.sin6_addr);
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
addr.sin_family = AF_INET;
|
2012-08-05 19:01:07 +02:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
2008-02-28 02:34:43 +01:00
|
|
|
addr.sin_addr.s_addr = htonl((127 << 24) | 1);
|
2012-08-05 19:01:07 +02:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2012-08-05 19:01:07 +02:00
|
|
|
try_again:
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
addr.sin6_port = htons(port);
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2012-08-05 19:01:07 +02:00
|
|
|
addr.sin_port = htons(port);
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2012-08-05 19:01:07 +02:00
|
|
|
port--;
|
2016-03-08 14:28:02 +01:00
|
|
|
if ((WPA_CTRL_IFACE_PORT - port) < WPA_CTRL_IFACE_PORT_LIMIT)
|
2012-08-05 19:01:07 +02:00
|
|
|
goto try_again;
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-03-08 14:28:02 +01:00
|
|
|
/* Update the ctrl_interface value to match the selected port */
|
|
|
|
os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
|
|
|
|
os_free(wpa_s->conf->ctrl_interface);
|
|
|
|
wpa_s->conf->ctrl_interface = os_strdup(port_str);
|
|
|
|
if (!wpa_s->conf->ctrl_interface) {
|
|
|
|
wpa_msg(wpa_s, MSG_ERROR, "Failed to malloc ctrl_interface");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-08-05 19:01:07 +02:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
wpa_msg(wpa_s, MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
|
|
|
|
wpa_s, priv);
|
|
|
|
wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
|
|
|
|
|
|
|
|
return priv;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (priv->sock >= 0)
|
|
|
|
close(priv->sock);
|
|
|
|
os_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
|
|
|
|
{
|
|
|
|
if (priv->sock > -1) {
|
|
|
|
eloop_unregister_read_sock(priv->sock);
|
|
|
|
if (priv->ctrl_dst) {
|
|
|
|
/*
|
2013-08-25 08:48:27 +02:00
|
|
|
* Wait before closing the control socket if
|
2008-02-28 02:34:43 +01:00
|
|
|
* there are any attached monitors in order to allow
|
|
|
|
* them to receive any pending messages.
|
|
|
|
*/
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
|
|
|
|
"monitors to receive messages");
|
2013-08-25 08:48:27 +02:00
|
|
|
os_sleep(0, 100000);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
close(priv->sock);
|
|
|
|
priv->sock = -1;
|
|
|
|
}
|
|
|
|
|
2016-03-04 10:20:19 +01:00
|
|
|
wpas_ctrl_iface_free_dst(priv->ctrl_dst);
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
|
|
|
|
const char *ifname, int sock,
|
|
|
|
struct wpa_ctrl_dst **head,
|
2008-02-28 02:34:43 +01:00
|
|
|
int level, const char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst, *next;
|
2016-03-04 10:20:18 +01:00
|
|
|
char levelstr[64];
|
2008-02-28 02:34:43 +01:00
|
|
|
int idx;
|
|
|
|
char *sbuf;
|
|
|
|
int llen;
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
dst = *head;
|
|
|
|
if (sock < 0 || dst == NULL)
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
|
2016-03-04 10:20:18 +01:00
|
|
|
if (ifname)
|
|
|
|
os_snprintf(levelstr, sizeof(levelstr), "IFACE=%s <%d>",
|
|
|
|
ifname, level);
|
|
|
|
else
|
|
|
|
os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
llen = os_strlen(levelstr);
|
|
|
|
sbuf = os_malloc(llen + len);
|
|
|
|
if (sbuf == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
os_memcpy(sbuf, levelstr, llen);
|
|
|
|
os_memcpy(sbuf + llen, buf, len);
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
while (dst) {
|
|
|
|
next = dst->next;
|
|
|
|
if (level >= dst->debug_level) {
|
2014-02-24 13:22:16 +01:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
|
|
|
|
inet_ntop(AF_INET6, &dst->addr.sin6_addr,
|
|
|
|
addr, sizeof(dst->addr)),
|
|
|
|
ntohs(dst->addr.sin6_port));
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2008-02-28 02:34:43 +01:00
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
|
|
|
|
inet_ntoa(dst->addr.sin_addr),
|
|
|
|
ntohs(dst->addr.sin_port));
|
2014-02-24 13:22:16 +01:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
|
2016-03-04 10:20:18 +01:00
|
|
|
if (sendto(sock, sbuf, llen + len, 0,
|
2008-02-28 02:34:43 +01:00
|
|
|
(struct sockaddr *) &dst->addr,
|
|
|
|
sizeof(dst->addr)) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"sendto(CTRL_IFACE monitor): %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
dst->errors++;
|
|
|
|
if (dst->errors > 10) {
|
|
|
|
wpa_supplicant_ctrl_iface_detach(
|
2016-03-04 10:20:18 +01:00
|
|
|
head, &dst->addr,
|
2008-02-28 02:34:43 +01:00
|
|
|
dst->addrlen);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
dst->errors = 0;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
dst = next;
|
|
|
|
}
|
|
|
|
os_free(sbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
|
|
|
|
priv->wpa_s->ifname);
|
|
|
|
eloop_wait_for_read_sock(priv->sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Global ctrl_iface */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
wpa_supplicant_global_get_cookie(struct ctrl_iface_global_priv *priv,
|
|
|
|
size_t *reply_len)
|
|
|
|
{
|
|
|
|
char *reply;
|
|
|
|
reply = os_malloc(7 + 2 * COOKIE_LEN + 1);
|
|
|
|
if (reply == NULL) {
|
|
|
|
*reply_len = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memcpy(reply, "COOKIE=", 7);
|
|
|
|
wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
|
|
|
|
priv->cookie, COOKIE_LEN);
|
|
|
|
|
|
|
|
*reply_len = 7 + 2 * COOKIE_LEN;
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
|
|
|
|
void *sock_ctx)
|
|
|
|
{
|
|
|
|
struct wpa_global *global = eloop_ctx;
|
|
|
|
struct ctrl_iface_global_priv *priv = sock_ctx;
|
|
|
|
char buf[256], *pos;
|
|
|
|
int res;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t fromlen = sizeof(from);
|
2016-03-04 10:20:19 +01:00
|
|
|
char *reply = NULL;
|
2008-02-28 02:34:43 +01:00
|
|
|
size_t reply_len;
|
|
|
|
u8 cookie[COOKIE_LEN];
|
|
|
|
|
|
|
|
res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
|
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (res < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-08-05 19:01:07 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
|
2008-02-28 02:34:43 +01:00
|
|
|
if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
|
|
|
|
/*
|
|
|
|
* The OS networking stack is expected to drop this kind of
|
|
|
|
* frames since the socket is bound to only localhost address.
|
|
|
|
* Just in case, drop the frame if it is coming from any other
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
|
|
|
|
"source %s", inet_ntoa(from.sin_addr));
|
|
|
|
return;
|
|
|
|
}
|
2012-08-05 19:01:07 +02:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
buf[res] = '\0';
|
|
|
|
|
|
|
|
if (os_strcmp(buf, "GET_COOKIE") == 0) {
|
|
|
|
reply = wpa_supplicant_global_get_cookie(priv, &reply_len);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os_strncmp(buf, "COOKIE=", 7) != 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - "
|
|
|
|
"drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the "
|
|
|
|
"request - drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - "
|
|
|
|
"drop request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buf + 7 + 2 * COOKIE_LEN;
|
|
|
|
while (*pos == ' ')
|
|
|
|
pos++;
|
|
|
|
|
2016-03-04 10:20:19 +01:00
|
|
|
if (os_strcmp(pos, "ATTACH") == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
|
|
|
|
&from, fromlen))
|
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else if (os_strcmp(pos, "DETACH") == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst,
|
|
|
|
&from, fromlen))
|
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else {
|
|
|
|
reply = wpa_supplicant_global_ctrl_iface_process(global, pos,
|
|
|
|
&reply_len);
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (reply) {
|
|
|
|
sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
|
|
|
os_free(reply);
|
2016-03-04 10:20:19 +01:00
|
|
|
} else if (reply_len == 1) {
|
2008-02-28 02:34:43 +01:00
|
|
|
sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
2016-03-04 10:20:19 +01:00
|
|
|
} else if (reply_len == 2) {
|
|
|
|
sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ctrl_iface_global_priv *
|
|
|
|
wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_global_priv *priv;
|
|
|
|
struct sockaddr_in addr;
|
2016-03-04 10:20:17 +01:00
|
|
|
char *pos;
|
2012-08-05 19:01:07 +02:00
|
|
|
int port = WPA_GLOBAL_CTRL_IFACE_PORT;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
priv = os_zalloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
|
|
|
return NULL;
|
|
|
|
priv->sock = -1;
|
|
|
|
os_get_random(priv->cookie, COOKIE_LEN);
|
|
|
|
|
|
|
|
if (global->params.ctrl_interface == NULL)
|
|
|
|
return priv;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "Global control interface '%s'",
|
|
|
|
global->params.ctrl_interface);
|
|
|
|
|
2016-03-04 10:20:17 +01:00
|
|
|
pos = os_strstr(global->params.ctrl_interface, "udp:");
|
|
|
|
if (pos) {
|
|
|
|
pos += 4;
|
|
|
|
port = atoi(pos);
|
|
|
|
if (port <= 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port %s",
|
|
|
|
global->params.ctrl_interface);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
priv->sock = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (priv->sock < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
2012-08-05 19:01:07 +02:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
2008-02-28 02:34:43 +01:00
|
|
|
addr.sin_addr.s_addr = htonl((127 << 24) | 1);
|
2012-08-05 19:01:07 +02:00
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
try_again:
|
|
|
|
addr.sin_port = htons(port);
|
2008-02-28 02:34:43 +01:00
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2012-08-05 19:01:07 +02:00
|
|
|
port++;
|
|
|
|
if ((port - WPA_GLOBAL_CTRL_IFACE_PORT) <
|
2016-03-04 10:20:17 +01:00
|
|
|
WPA_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
|
2012-08-05 19:01:07 +02:00
|
|
|
goto try_again;
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-08-05 19:01:07 +02:00
|
|
|
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
|
|
|
|
wpa_printf(MSG_DEBUG, "global_ctrl_iface_init UDP port: %d", port);
|
|
|
|
#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
eloop_register_read_sock(priv->sock,
|
|
|
|
wpa_supplicant_global_ctrl_iface_receive,
|
|
|
|
global, priv);
|
2016-03-04 10:20:19 +01:00
|
|
|
wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
return priv;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (priv->sock >= 0)
|
|
|
|
close(priv->sock);
|
|
|
|
os_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
|
|
|
|
{
|
|
|
|
if (priv->sock >= 0) {
|
|
|
|
eloop_unregister_read_sock(priv->sock);
|
|
|
|
close(priv->sock);
|
|
|
|
}
|
2016-03-04 10:20:19 +01:00
|
|
|
|
|
|
|
wpas_ctrl_iface_free_dst(priv->ctrl_dst);
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(priv);
|
|
|
|
}
|