2008-02-28 02:34:43 +01:00
|
|
|
/*
|
|
|
|
* WPA Supplicant / UNIX domain socket -based control interface
|
2020-05-02 20:58:59 +02:00
|
|
|
* Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
|
2008-02-28 02:34:43 +01:00
|
|
|
*
|
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 <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <grp.h>
|
2009-03-21 21:00:27 +01:00
|
|
|
#include <stddef.h>
|
2012-08-04 19:34:27 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2015-08-14 18:22:08 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif /* __linux__ */
|
2011-02-27 17:19:17 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#endif /* ANDROID */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2009-12-23 22:56:35 +01:00
|
|
|
#include "utils/common.h"
|
|
|
|
#include "utils/eloop.h"
|
|
|
|
#include "utils/list.h"
|
2016-03-04 10:20:21 +01:00
|
|
|
#include "common/ctrl_iface_common.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
#include "eapol_supp/eapol_supp_sm.h"
|
2009-12-23 22:56:35 +01:00
|
|
|
#include "config.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
#include "wpa_supplicant_i.h"
|
|
|
|
#include "ctrl_iface.h"
|
|
|
|
|
|
|
|
/* Per-interface ctrl_iface */
|
|
|
|
|
|
|
|
struct ctrl_iface_priv {
|
|
|
|
struct wpa_supplicant *wpa_s;
|
|
|
|
int sock;
|
2009-12-23 22:56:35 +01:00
|
|
|
struct dl_list ctrl_dst;
|
2014-11-25 11:11:28 +01:00
|
|
|
int android_control_socket;
|
2015-10-04 10:53:26 +02:00
|
|
|
struct dl_list msg_queue;
|
|
|
|
unsigned int throttle_count;
|
2008-02-28 02:34:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
struct ctrl_iface_global_priv {
|
|
|
|
struct wpa_global *global;
|
|
|
|
int sock;
|
|
|
|
struct dl_list ctrl_dst;
|
2014-11-25 11:11:28 +01:00
|
|
|
int android_control_socket;
|
2015-10-04 10:53:26 +02:00
|
|
|
struct dl_list msg_queue;
|
|
|
|
unsigned int throttle_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ctrl_iface_msg {
|
|
|
|
struct dl_list list;
|
|
|
|
struct wpa_supplicant *wpa_s;
|
|
|
|
int level;
|
|
|
|
enum wpa_msg_type type;
|
|
|
|
const char *txt;
|
|
|
|
size_t len;
|
2013-05-18 13:07:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
|
|
|
|
const char *ifname, int sock,
|
2013-05-18 13:07:28 +02:00
|
|
|
struct dl_list *ctrl_dst,
|
2008-02-28 02:34:43 +01:00
|
|
|
int level, const char *buf,
|
2013-09-25 15:23:11 +02:00
|
|
|
size_t len,
|
|
|
|
struct ctrl_iface_priv *priv,
|
|
|
|
struct ctrl_iface_global_priv *gp);
|
|
|
|
static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
|
|
|
|
struct ctrl_iface_priv *priv);
|
|
|
|
static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
|
|
|
|
struct ctrl_iface_global_priv *priv);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
2015-08-14 18:22:08 +02:00
|
|
|
static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
socklen_t optlen;
|
|
|
|
int sndbuf, outq;
|
2015-09-10 10:19:44 +02:00
|
|
|
int level = MSG_MSGDUMP;
|
2015-08-14 18:22:08 +02:00
|
|
|
|
|
|
|
if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0)
|
|
|
|
level = MSG_EXCESSIVE;
|
|
|
|
|
|
|
|
optlen = sizeof(sndbuf);
|
|
|
|
sndbuf = 0;
|
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
|
|
|
|
sndbuf = -1;
|
|
|
|
|
2016-03-26 10:40:35 +01:00
|
|
|
if (ioctl(sock, TIOCOUTQ, &outq) < 0)
|
2015-08-14 18:22:08 +02:00
|
|
|
outq = -1;
|
|
|
|
|
|
|
|
wpa_printf(level,
|
|
|
|
"CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d",
|
|
|
|
title, sock, sndbuf, outq, (int) len);
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage *from,
|
2015-01-18 15:23:43 +01:00
|
|
|
socklen_t fromlen, int global)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
2017-11-01 18:09:55 +01:00
|
|
|
return ctrl_iface_attach(ctrl_dst, from, fromlen, NULL);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage *from,
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen)
|
|
|
|
{
|
2016-03-04 10:20:21 +01:00
|
|
|
return ctrl_iface_detach(ctrl_dst, from, fromlen);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage *from,
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen,
|
|
|
|
char *level)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
|
|
|
|
|
2016-03-04 10:20:21 +01:00
|
|
|
return ctrl_iface_level(&priv->ctrl_dst, from, fromlen, level);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2020-05-02 20:58:59 +02:00
|
|
|
char *buf;
|
2008-02-28 02:34:43 +01:00
|
|
|
int res;
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage from;
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen = sizeof(from);
|
2013-09-23 16:52:10 +02:00
|
|
|
char *reply = NULL, *reply_buf = NULL;
|
2008-02-28 02:34:43 +01:00
|
|
|
size_t reply_len = 0;
|
|
|
|
int new_attached = 0;
|
|
|
|
|
2020-05-02 20:58:59 +02:00
|
|
|
buf = os_malloc(CTRL_IFACE_MAX_LEN + 1);
|
|
|
|
if (!buf)
|
|
|
|
return;
|
|
|
|
res = recvfrom(sock, buf, CTRL_IFACE_MAX_LEN, 0,
|
2008-02-28 02:34:43 +01:00
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (res < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
|
|
|
|
strerror(errno));
|
2020-05-02 20:58:59 +02:00
|
|
|
os_free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((size_t) res > CTRL_IFACE_MAX_LEN) {
|
|
|
|
wpa_printf(MSG_ERROR, "recvform(ctrl_iface): input truncated");
|
|
|
|
os_free(buf);
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[res] = '\0';
|
|
|
|
|
|
|
|
if (os_strcmp(buf, "ATTACH") == 0) {
|
2013-05-18 13:07:28 +02:00
|
|
|
if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
|
2015-01-18 15:23:43 +01:00
|
|
|
fromlen, 0))
|
2008-02-28 02:34:43 +01:00
|
|
|
reply_len = 1;
|
|
|
|
else {
|
|
|
|
new_attached = 1;
|
|
|
|
reply_len = 2;
|
|
|
|
}
|
|
|
|
} else if (os_strcmp(buf, "DETACH") == 0) {
|
2013-05-18 13:07:28 +02: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(buf, "LEVEL ", 6) == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
|
|
|
|
buf + 6))
|
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else {
|
2013-09-23 16:52:10 +02:00
|
|
|
reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
|
|
|
|
&reply_len);
|
|
|
|
reply = reply_buf;
|
2015-06-17 15:29:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There could be some password/key material in the command, so
|
|
|
|
* clear the buffer explicitly now that it is not needed
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
os_memset(buf, 0, res);
|
2013-09-23 16:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!reply && reply_len == 1) {
|
|
|
|
reply = "FAIL\n";
|
|
|
|
reply_len = 5;
|
|
|
|
} else if (!reply && reply_len == 2) {
|
|
|
|
reply = "OK\n";
|
|
|
|
reply_len = 3;
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reply) {
|
2015-08-14 18:22:08 +02:00
|
|
|
wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply,
|
|
|
|
reply_len);
|
2013-09-11 05:17:07 +02:00
|
|
|
if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen) < 0) {
|
2013-09-25 15:23:11 +02:00
|
|
|
int _errno = errno;
|
2013-09-11 05:17:07 +02:00
|
|
|
wpa_dbg(wpa_s, MSG_DEBUG,
|
2013-09-25 15:23:11 +02:00
|
|
|
"ctrl_iface sendto failed: %d - %s",
|
|
|
|
_errno, strerror(_errno));
|
|
|
|
if (_errno == ENOBUFS || _errno == EAGAIN) {
|
|
|
|
/*
|
|
|
|
* The socket send buffer could be full. This
|
|
|
|
* may happen if client programs are not
|
|
|
|
* receiving their pending messages. Close and
|
|
|
|
* reopen the socket as a workaround to avoid
|
|
|
|
* getting stuck being unable to send any new
|
|
|
|
* responses.
|
|
|
|
*/
|
|
|
|
sock = wpas_ctrl_iface_reinit(wpa_s, priv);
|
|
|
|
if (sock < 0) {
|
|
|
|
wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_attached) {
|
|
|
|
wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
|
|
|
|
new_attached = 0;
|
|
|
|
wpa_supplicant_ctrl_iface_detach(
|
|
|
|
&priv->ctrl_dst, &from, fromlen);
|
|
|
|
}
|
2013-09-11 05:17:07 +02:00
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
2013-09-23 16:52:10 +02:00
|
|
|
os_free(reply_buf);
|
2020-05-02 20:58:59 +02:00
|
|
|
os_free(buf);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (new_attached)
|
|
|
|
eapol_sm_notify_ctrl_attached(wpa_s->eapol);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
2014-03-02 14:52:10 +01:00
|
|
|
char *pbuf, *dir = NULL;
|
2008-02-28 02:34:43 +01:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (wpa_s->conf->ctrl_interface == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pbuf = os_strdup(wpa_s->conf->ctrl_interface);
|
|
|
|
if (pbuf == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (os_strncmp(pbuf, "DIR=", 4) == 0) {
|
2014-03-02 14:52:10 +01:00
|
|
|
char *gid_str;
|
2008-02-28 02:34:43 +01:00
|
|
|
dir = pbuf + 4;
|
|
|
|
gid_str = os_strstr(dir, " GROUP=");
|
2014-03-02 14:52:10 +01:00
|
|
|
if (gid_str)
|
2008-02-28 02:34:43 +01:00
|
|
|
*gid_str = '\0';
|
|
|
|
} else
|
|
|
|
dir = pbuf;
|
|
|
|
|
|
|
|
len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
|
|
|
|
buf = os_malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
os_free(pbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
|
Check os_snprintf() result more consistently - automatic 1
This converts os_snprintf() result validation cases to use
os_snprintf_error() where the exact rule used in os_snprintf_error() was
used. These changes were done automatically with spatch using the
following semantic patch:
@@
identifier E1;
expression E2,E3,E4,E5,E6;
statement S1;
@@
(
E1 = os_snprintf(E2, E3, ...);
|
int E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else
E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else if (E6)
E1 = os_snprintf(E2, E3, ...);
else
E1 = 0;
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else if (E6) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
E1 = os_snprintf(E2, E3, ...);
}
)
? os_free(E4);
- if (E1 < 0 || \( E1 >= E3 \| (size_t) E1 >= E3 \| (unsigned int) E1 >= E3 \| E1 >= (int) E3 \))
+ if (os_snprintf_error(E3, E1))
(
S1
|
{ ... }
)
Signed-off-by: Jouni Malinen <j@w1.fi>
2014-12-08 10:15:51 +01:00
|
|
|
if (os_snprintf_error(len, res)) {
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(pbuf);
|
|
|
|
os_free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
{
|
|
|
|
/* Windows/WinPcap uses interface names that are not suitable
|
|
|
|
* as a file name - convert invalid chars to underscores */
|
|
|
|
char *pos = buf;
|
|
|
|
while (*pos) {
|
|
|
|
if (*pos == '\\')
|
|
|
|
*pos = '_';
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* __CYGWIN__ */
|
|
|
|
os_free(pbuf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 10:53:26 +02:00
|
|
|
static int wpas_ctrl_iface_throttle(int sock)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
socklen_t optlen;
|
|
|
|
int sndbuf, outq;
|
|
|
|
|
|
|
|
optlen = sizeof(sndbuf);
|
|
|
|
sndbuf = 0;
|
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0 ||
|
2016-03-26 10:40:35 +01:00
|
|
|
ioctl(sock, TIOCOUTQ, &outq) < 0 ||
|
2015-10-04 10:53:26 +02:00
|
|
|
sndbuf <= 0 || outq < 0)
|
|
|
|
return 0;
|
|
|
|
return outq > sndbuf / 2;
|
|
|
|
#else /* __linux__ */
|
|
|
|
return 0;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpas_ctrl_msg_send_pending_global(struct wpa_global *global)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_global_priv *gpriv;
|
|
|
|
struct ctrl_iface_msg *msg;
|
|
|
|
|
|
|
|
gpriv = global->ctrl_iface;
|
|
|
|
while (gpriv && !dl_list_empty(&gpriv->msg_queue) &&
|
|
|
|
!wpas_ctrl_iface_throttle(gpriv->sock)) {
|
|
|
|
msg = dl_list_first(&gpriv->msg_queue, struct ctrl_iface_msg,
|
|
|
|
list);
|
|
|
|
if (!msg)
|
|
|
|
break;
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
wpa_supplicant_ctrl_iface_send(
|
|
|
|
msg->wpa_s,
|
|
|
|
msg->type != WPA_MSG_PER_INTERFACE ?
|
|
|
|
NULL : msg->wpa_s->ifname,
|
|
|
|
gpriv->sock, &gpriv->ctrl_dst, msg->level,
|
|
|
|
msg->txt, msg->len, NULL, gpriv);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpas_ctrl_msg_send_pending_iface(struct wpa_supplicant *wpa_s)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_priv *priv;
|
|
|
|
struct ctrl_iface_msg *msg;
|
|
|
|
|
|
|
|
priv = wpa_s->ctrl_iface;
|
|
|
|
while (priv && !dl_list_empty(&priv->msg_queue) &&
|
|
|
|
!wpas_ctrl_iface_throttle(priv->sock)) {
|
|
|
|
msg = dl_list_first(&priv->msg_queue, struct ctrl_iface_msg,
|
|
|
|
list);
|
|
|
|
if (!msg)
|
|
|
|
break;
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
|
|
|
|
&priv->ctrl_dst, msg->level,
|
|
|
|
msg->txt, msg->len, priv, NULL);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpas_ctrl_msg_queue_timeout(void *eloop_ctx, void *timeout_ctx)
|
|
|
|
{
|
|
|
|
struct wpa_supplicant *wpa_s = eloop_ctx;
|
|
|
|
struct ctrl_iface_priv *priv;
|
|
|
|
struct ctrl_iface_global_priv *gpriv;
|
|
|
|
int sock = -1, gsock = -1;
|
|
|
|
|
|
|
|
wpas_ctrl_msg_send_pending_global(wpa_s->global);
|
|
|
|
wpas_ctrl_msg_send_pending_iface(wpa_s);
|
|
|
|
|
|
|
|
priv = wpa_s->ctrl_iface;
|
|
|
|
if (priv && !dl_list_empty(&priv->msg_queue))
|
|
|
|
sock = priv->sock;
|
|
|
|
|
|
|
|
gpriv = wpa_s->global->ctrl_iface;
|
|
|
|
if (gpriv && !dl_list_empty(&gpriv->msg_queue))
|
|
|
|
gsock = gpriv->sock;
|
|
|
|
|
|
|
|
if (sock > -1 || gsock > -1) {
|
|
|
|
/* Continue pending message transmission from a timeout */
|
|
|
|
wpa_printf(MSG_MSGDUMP,
|
|
|
|
"CTRL: Had to throttle pending event message transmission for (sock %d gsock %d)",
|
|
|
|
sock, gsock);
|
|
|
|
eloop_register_timeout(0, 20000, wpas_ctrl_msg_queue_timeout,
|
|
|
|
wpa_s, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpas_ctrl_msg_queue(struct dl_list *queue,
|
|
|
|
struct wpa_supplicant *wpa_s, int level,
|
|
|
|
enum wpa_msg_type type,
|
|
|
|
const char *txt, size_t len)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_msg *msg;
|
|
|
|
|
|
|
|
msg = os_zalloc(sizeof(*msg) + len);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg->wpa_s = wpa_s;
|
|
|
|
msg->level = level;
|
|
|
|
msg->type = type;
|
|
|
|
os_memcpy(msg + 1, txt, len);
|
|
|
|
msg->txt = (const char *) (msg + 1);
|
|
|
|
msg->len = len;
|
|
|
|
dl_list_add_tail(queue, &msg->list);
|
|
|
|
eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
|
|
|
|
eloop_register_timeout(0, 0, wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpas_ctrl_msg_queue_limit(unsigned int throttle_count,
|
|
|
|
struct dl_list *queue)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_msg *msg;
|
|
|
|
|
|
|
|
if (throttle_count < 2000)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg = dl_list_first(queue, struct ctrl_iface_msg, list);
|
|
|
|
if (msg) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL: Dropped oldest pending message");
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2015-10-04 10:53:26 +02:00
|
|
|
struct ctrl_iface_priv *priv;
|
|
|
|
struct ctrl_iface_global_priv *gpriv;
|
2013-05-18 13:07:28 +02:00
|
|
|
|
|
|
|
if (wpa_s == NULL)
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
2013-05-18 13:07:28 +02:00
|
|
|
|
2015-10-04 10:53:26 +02:00
|
|
|
gpriv = wpa_s->global->ctrl_iface;
|
|
|
|
|
|
|
|
if (type != WPA_MSG_NO_GLOBAL && gpriv &&
|
|
|
|
!dl_list_empty(&gpriv->ctrl_dst)) {
|
|
|
|
if (!dl_list_empty(&gpriv->msg_queue) ||
|
|
|
|
wpas_ctrl_iface_throttle(gpriv->sock)) {
|
|
|
|
if (gpriv->throttle_count == 0) {
|
|
|
|
wpa_printf(MSG_MSGDUMP,
|
|
|
|
"CTRL: Had to throttle global event message for sock %d",
|
|
|
|
gpriv->sock);
|
|
|
|
}
|
|
|
|
gpriv->throttle_count++;
|
|
|
|
wpas_ctrl_msg_queue_limit(gpriv->throttle_count,
|
|
|
|
&gpriv->msg_queue);
|
|
|
|
wpas_ctrl_msg_queue(&gpriv->msg_queue, wpa_s, level,
|
|
|
|
type, txt, len);
|
|
|
|
} else {
|
|
|
|
if (gpriv->throttle_count) {
|
|
|
|
wpa_printf(MSG_MSGDUMP,
|
|
|
|
"CTRL: Had to throttle %u global event message(s) for sock %d",
|
|
|
|
gpriv->throttle_count, gpriv->sock);
|
|
|
|
}
|
|
|
|
gpriv->throttle_count = 0;
|
2015-02-06 11:08:28 +01:00
|
|
|
wpa_supplicant_ctrl_iface_send(
|
|
|
|
wpa_s,
|
2014-11-16 15:52:49 +01:00
|
|
|
type != WPA_MSG_PER_INTERFACE ?
|
|
|
|
NULL : wpa_s->ifname,
|
2015-10-04 10:53:26 +02:00
|
|
|
gpriv->sock, &gpriv->ctrl_dst, level,
|
|
|
|
txt, len, NULL, gpriv);
|
2013-05-18 13:07:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 10:53:26 +02:00
|
|
|
priv = wpa_s->ctrl_iface;
|
|
|
|
|
|
|
|
if (type != WPA_MSG_ONLY_GLOBAL && priv) {
|
|
|
|
if (!dl_list_empty(&priv->msg_queue) ||
|
|
|
|
wpas_ctrl_iface_throttle(priv->sock)) {
|
|
|
|
if (priv->throttle_count == 0) {
|
|
|
|
wpa_printf(MSG_MSGDUMP,
|
|
|
|
"CTRL: Had to throttle event message for sock %d",
|
|
|
|
priv->sock);
|
|
|
|
}
|
|
|
|
priv->throttle_count++;
|
|
|
|
wpas_ctrl_msg_queue_limit(priv->throttle_count,
|
|
|
|
&priv->msg_queue);
|
|
|
|
wpas_ctrl_msg_queue(&priv->msg_queue, wpa_s, level,
|
|
|
|
type, txt, len);
|
|
|
|
} else {
|
|
|
|
if (priv->throttle_count) {
|
|
|
|
wpa_printf(MSG_MSGDUMP,
|
|
|
|
"CTRL: Had to throttle %u event message(s) for sock %d",
|
|
|
|
priv->throttle_count, priv->sock);
|
|
|
|
}
|
|
|
|
priv->throttle_count = 0;
|
|
|
|
wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
|
|
|
|
&priv->ctrl_dst, level,
|
|
|
|
txt, len, priv, NULL);
|
|
|
|
}
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
|
|
|
|
struct ctrl_iface_priv *priv)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
char *fname = NULL;
|
|
|
|
gid_t gid = 0;
|
|
|
|
int gid_set = 0;
|
|
|
|
char *buf, *dir = NULL, *gid_str = NULL;
|
|
|
|
struct group *grp;
|
|
|
|
char *endp;
|
2012-08-04 19:34:27 +02:00
|
|
|
int flags;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
buf = os_strdup(wpa_s->conf->ctrl_interface);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto fail;
|
2011-02-27 17:19:17 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
|
|
|
|
wpa_s->conf->ctrl_interface);
|
|
|
|
priv->sock = android_get_control_socket(addr.sun_path);
|
2014-11-25 11:11:28 +01:00
|
|
|
if (priv->sock >= 0) {
|
|
|
|
priv->android_control_socket = 1;
|
2011-02-27 17:19:17 +01:00
|
|
|
goto havesock;
|
2014-11-25 11:11:28 +01:00
|
|
|
}
|
2011-02-27 17:19:17 +01:00
|
|
|
#endif /* ANDROID */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (os_strncmp(buf, "DIR=", 4) == 0) {
|
|
|
|
dir = buf + 4;
|
|
|
|
gid_str = os_strstr(dir, " GROUP=");
|
|
|
|
if (gid_str) {
|
|
|
|
*gid_str = '\0';
|
|
|
|
gid_str += 7;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dir = buf;
|
|
|
|
gid_str = wpa_s->conf->ctrl_interface_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
wpa_printf(MSG_DEBUG, "Using existing control "
|
|
|
|
"interface directory.");
|
|
|
|
} else {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
|
|
|
|
dir, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-23 16:05:47 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
/*
|
|
|
|
* wpa_supplicant is started from /init.*.rc on Android and that seems
|
|
|
|
* to be using umask 0077 which would leave the control interface
|
|
|
|
* directory without group access. This breaks things since Wi-Fi
|
|
|
|
* framework assumes that this directory can be accessed by other
|
|
|
|
* applications in the wifi group. Fix this by adding group access even
|
|
|
|
* if umask value would prevent this.
|
|
|
|
*/
|
|
|
|
if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
|
|
|
|
strerror(errno));
|
|
|
|
/* Try to continue anyway */
|
|
|
|
}
|
|
|
|
#endif /* ANDROID */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
if (gid_str) {
|
|
|
|
grp = getgrnam(gid_str);
|
|
|
|
if (grp) {
|
|
|
|
gid = grp->gr_gid;
|
|
|
|
gid_set = 1;
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
|
|
|
|
" (from group name '%s')",
|
|
|
|
(int) gid, gid_str);
|
|
|
|
} else {
|
|
|
|
/* Group name not found - try to parse this as gid */
|
|
|
|
gid = strtol(gid_str, &endp, 10);
|
|
|
|
if (*gid_str == '\0' || *endp != '\0') {
|
2008-03-06 21:49:46 +01:00
|
|
|
wpa_printf(MSG_ERROR, "CTRL: Invalid group "
|
2008-02-28 02:34:43 +01:00
|
|
|
"'%s'", gid_str);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
gid_set = 1;
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
|
|
|
|
(int) gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 19:28:04 +01:00
|
|
|
if (gid_set && lchown(dir, -1, gid) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "lchown[ctrl_interface=%s,gid=%d]: %s",
|
2013-08-26 10:46:21 +02:00
|
|
|
dir, (int) gid, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-11-16 15:43:25 +01:00
|
|
|
/* Make sure the group can enter and read the directory */
|
|
|
|
if (gid_set &&
|
|
|
|
chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
|
|
|
|
sizeof(addr.sun_path)) {
|
|
|
|
wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (priv->sock < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&addr, 0, sizeof(addr));
|
2010-03-06 09:16:47 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-03-21 21:00:27 +01:00
|
|
|
addr.sun_len = sizeof(addr);
|
|
|
|
#endif /* __FreeBSD__ */
|
2008-02-28 02:34:43 +01:00
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
fname = wpa_supplicant_ctrl_iface_path(wpa_s);
|
|
|
|
if (fname == NULL)
|
|
|
|
goto fail;
|
|
|
|
os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
|
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
if (connect(priv->sock, (struct sockaddr *) &addr,
|
|
|
|
sizeof(addr)) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
|
|
|
|
" allow connections - assuming it was left"
|
|
|
|
"over from forced program termination");
|
|
|
|
if (unlink(fname) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"Could not unlink existing ctrl_iface socket '%s': %s",
|
|
|
|
fname, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr,
|
|
|
|
sizeof(addr)) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
|
|
|
|
"ctrl_iface socket '%s'", fname);
|
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
|
|
|
|
"be in use - cannot override it");
|
|
|
|
wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
|
|
|
|
"not used anymore", fname);
|
|
|
|
os_free(fname);
|
|
|
|
fname = NULL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 19:28:04 +01:00
|
|
|
if (gid_set && lchown(fname, -1, gid) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "lchown[ctrl_interface=%s,gid=%d]: %s",
|
2013-08-26 10:46:21 +02:00
|
|
|
fname, (int) gid, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
|
|
|
|
fname, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
os_free(fname);
|
|
|
|
|
2011-02-27 17:19:17 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
havesock:
|
|
|
|
#endif /* ANDROID */
|
2012-08-04 19:34:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make socket non-blocking so that we don't hang forever if
|
|
|
|
* target dies unexpectedly.
|
|
|
|
*/
|
|
|
|
flags = fcntl(priv->sock, F_GETFL);
|
|
|
|
if (flags >= 0) {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(priv->sock, F_SETFL, flags) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
|
|
|
|
strerror(errno));
|
2012-08-04 19:34:27 +02:00
|
|
|
/* Not fatal, continue on.*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
os_free(buf);
|
2013-09-25 15:23:11 +02:00
|
|
|
return 0;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
fail:
|
2013-09-25 15:23:11 +02:00
|
|
|
if (priv->sock >= 0) {
|
2008-02-28 02:34:43 +01:00
|
|
|
close(priv->sock);
|
2013-09-25 15:23:11 +02:00
|
|
|
priv->sock = -1;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
if (fname) {
|
|
|
|
unlink(fname);
|
|
|
|
os_free(fname);
|
|
|
|
}
|
|
|
|
os_free(buf);
|
2013-09-25 15:23:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ctrl_iface_priv *
|
|
|
|
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_priv *priv;
|
|
|
|
|
|
|
|
priv = os_zalloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
|
|
|
return NULL;
|
|
|
|
dl_list_init(&priv->ctrl_dst);
|
2015-10-04 10:53:26 +02:00
|
|
|
dl_list_init(&priv->msg_queue);
|
2013-09-25 15:23:11 +02:00
|
|
|
priv->wpa_s = wpa_s;
|
|
|
|
priv->sock = -1;
|
|
|
|
|
|
|
|
if (wpa_s->conf->ctrl_interface == NULL)
|
|
|
|
return priv;
|
|
|
|
|
2015-09-23 17:51:49 +02:00
|
|
|
#ifdef ANDROID
|
|
|
|
if (wpa_s->global->params.ctrl_interface) {
|
|
|
|
int same = 0;
|
|
|
|
|
|
|
|
if (wpa_s->global->params.ctrl_interface[0] == '/') {
|
|
|
|
if (os_strcmp(wpa_s->global->params.ctrl_interface,
|
|
|
|
wpa_s->conf->ctrl_interface) == 0)
|
|
|
|
same = 1;
|
|
|
|
} else if (os_strncmp(wpa_s->global->params.ctrl_interface,
|
|
|
|
"@android:", 9) == 0 ||
|
|
|
|
os_strncmp(wpa_s->global->params.ctrl_interface,
|
|
|
|
"@abstract:", 10) == 0) {
|
|
|
|
char *pos;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently, Android uses @android:wpa_* as the naming
|
|
|
|
* convention for the global ctrl interface. This logic
|
|
|
|
* needs to be revisited if the above naming convention
|
|
|
|
* is modified.
|
|
|
|
*/
|
|
|
|
pos = os_strchr(wpa_s->global->params.ctrl_interface,
|
|
|
|
'_');
|
|
|
|
if (pos &&
|
|
|
|
os_strcmp(pos + 1,
|
|
|
|
wpa_s->conf->ctrl_interface) == 0)
|
|
|
|
same = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (same) {
|
|
|
|
/*
|
|
|
|
* The invalid configuration combination might be
|
|
|
|
* possible to hit in an Android OTA upgrade case, so
|
|
|
|
* instead of refusing to start the wpa_supplicant
|
|
|
|
* process, do not open the per-interface ctrl_iface
|
|
|
|
* and continue with the global control interface that
|
|
|
|
* was set from the command line since the Wi-Fi
|
|
|
|
* framework will use it for operations.
|
|
|
|
*/
|
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface",
|
|
|
|
wpa_s->global->params.ctrl_interface,
|
|
|
|
wpa_s->conf->ctrl_interface);
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ANDROID */
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
|
|
|
|
os_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
|
|
|
|
struct ctrl_iface_priv *priv)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (priv->sock <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-25 11:11:28 +01:00
|
|
|
/*
|
|
|
|
* On Android, the control socket being used may be the socket
|
|
|
|
* that is created when wpa_supplicant is started as a /init.*.rc
|
|
|
|
* service. Such a socket is maintained as a key-value pair in
|
|
|
|
* Android's environment. Closing this control socket would leave us
|
|
|
|
* in a bad state with an invalid socket descriptor.
|
|
|
|
*/
|
|
|
|
if (priv->android_control_socket)
|
|
|
|
return priv->sock;
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
eloop_unregister_read_sock(priv->sock);
|
|
|
|
close(priv->sock);
|
|
|
|
priv->sock = -1;
|
|
|
|
res = wpas_ctrl_iface_open_sock(wpa_s, priv);
|
|
|
|
if (res < 0)
|
|
|
|
return -1;
|
|
|
|
return priv->sock;
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
|
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst, *prev;
|
2015-10-04 10:53:26 +02:00
|
|
|
struct ctrl_iface_msg *msg, *prev_msg;
|
|
|
|
struct ctrl_iface_global_priv *gpriv;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (priv->sock > -1) {
|
|
|
|
char *fname;
|
2014-03-02 14:52:10 +01:00
|
|
|
char *buf, *dir = NULL;
|
2008-02-28 02:34:43 +01:00
|
|
|
eloop_unregister_read_sock(priv->sock);
|
2009-12-23 22:56:35 +01:00
|
|
|
if (!dl_list_empty(&priv->ctrl_dst)) {
|
2008-02-28 02:34:43 +01:00
|
|
|
/*
|
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;
|
|
|
|
fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
|
|
|
|
if (fname) {
|
|
|
|
unlink(fname);
|
|
|
|
os_free(fname);
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:30:20 +02:00
|
|
|
if (priv->wpa_s->conf->ctrl_interface == NULL)
|
|
|
|
goto free_dst;
|
2008-02-28 02:34:43 +01:00
|
|
|
buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto free_dst;
|
|
|
|
if (os_strncmp(buf, "DIR=", 4) == 0) {
|
2014-03-02 14:52:10 +01:00
|
|
|
char *gid_str;
|
2008-02-28 02:34:43 +01:00
|
|
|
dir = buf + 4;
|
|
|
|
gid_str = os_strstr(dir, " GROUP=");
|
2014-03-02 14:52:10 +01:00
|
|
|
if (gid_str)
|
2008-02-28 02:34:43 +01:00
|
|
|
*gid_str = '\0';
|
|
|
|
} else
|
|
|
|
dir = buf;
|
|
|
|
|
|
|
|
if (rmdir(dir) < 0) {
|
|
|
|
if (errno == ENOTEMPTY) {
|
|
|
|
wpa_printf(MSG_DEBUG, "Control interface "
|
|
|
|
"directory not empty - leaving it "
|
|
|
|
"behind");
|
|
|
|
} else {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"rmdir[ctrl_interface=%s]: %s",
|
|
|
|
dir, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
os_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_dst:
|
2009-12-23 22:56:35 +01:00
|
|
|
dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
|
2015-10-04 10:58:24 +02:00
|
|
|
list) {
|
|
|
|
dl_list_del(&dst->list);
|
2009-12-23 22:56:35 +01:00
|
|
|
os_free(dst);
|
2015-10-04 10:58:24 +02:00
|
|
|
}
|
2015-10-04 10:53:26 +02:00
|
|
|
dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
|
|
|
|
struct ctrl_iface_msg, list) {
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
|
|
|
gpriv = priv->wpa_s->global->ctrl_iface;
|
|
|
|
if (gpriv) {
|
|
|
|
dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue,
|
|
|
|
struct ctrl_iface_msg, list) {
|
|
|
|
if (msg->wpa_s == priv->wpa_s) {
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL);
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
|
2013-05-18 13:07:28 +02:00
|
|
|
* @ifname: Interface name for global control socket or %NULL
|
|
|
|
* @sock: Local socket fd
|
|
|
|
* @ctrl_dst: List of attached listeners
|
2008-02-28 02:34:43 +01:00
|
|
|
* @level: Priority level of the message
|
|
|
|
* @buf: Message data
|
|
|
|
* @len: Message length
|
|
|
|
*
|
|
|
|
* Send a packet to all monitor programs attached to the control interface.
|
|
|
|
*/
|
2013-09-25 15:23:11 +02:00
|
|
|
static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
|
|
|
|
const char *ifname, int sock,
|
2013-05-18 13:07:28 +02:00
|
|
|
struct dl_list *ctrl_dst,
|
2008-02-28 02:34:43 +01:00
|
|
|
int level, const char *buf,
|
2013-09-25 15:23:11 +02:00
|
|
|
size_t len,
|
|
|
|
struct ctrl_iface_priv *priv,
|
|
|
|
struct ctrl_iface_global_priv *gp)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct wpa_ctrl_dst *dst, *next;
|
|
|
|
char levelstr[10];
|
|
|
|
int idx, res;
|
|
|
|
struct msghdr msg;
|
2013-05-18 13:07:28 +02:00
|
|
|
struct iovec io[5];
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
if (sock < 0 || dl_list_empty(ctrl_dst))
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
|
Check os_snprintf() result more consistently - automatic 1
This converts os_snprintf() result validation cases to use
os_snprintf_error() where the exact rule used in os_snprintf_error() was
used. These changes were done automatically with spatch using the
following semantic patch:
@@
identifier E1;
expression E2,E3,E4,E5,E6;
statement S1;
@@
(
E1 = os_snprintf(E2, E3, ...);
|
int E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else
E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else if (E6)
E1 = os_snprintf(E2, E3, ...);
else
E1 = 0;
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else if (E6) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
E1 = os_snprintf(E2, E3, ...);
}
)
? os_free(E4);
- if (E1 < 0 || \( E1 >= E3 \| (size_t) E1 >= E3 \| (unsigned int) E1 >= E3 \| E1 >= (int) E3 \))
+ if (os_snprintf_error(E3, E1))
(
S1
|
{ ... }
)
Signed-off-by: Jouni Malinen <j@w1.fi>
2014-12-08 10:15:51 +01:00
|
|
|
if (os_snprintf_error(sizeof(levelstr), res))
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
2013-05-18 13:07:28 +02:00
|
|
|
idx = 0;
|
|
|
|
if (ifname) {
|
|
|
|
io[idx].iov_base = "IFNAME=";
|
|
|
|
io[idx].iov_len = 7;
|
|
|
|
idx++;
|
|
|
|
io[idx].iov_base = (char *) ifname;
|
|
|
|
io[idx].iov_len = os_strlen(ifname);
|
|
|
|
idx++;
|
|
|
|
io[idx].iov_base = " ";
|
|
|
|
io[idx].iov_len = 1;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
io[idx].iov_base = levelstr;
|
|
|
|
io[idx].iov_len = os_strlen(levelstr);
|
|
|
|
idx++;
|
|
|
|
io[idx].iov_base = (char *) buf;
|
|
|
|
io[idx].iov_len = len;
|
|
|
|
idx++;
|
2008-02-28 02:34:43 +01:00
|
|
|
os_memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_iov = io;
|
2013-05-18 13:07:28 +02:00
|
|
|
msg.msg_iovlen = idx;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
|
2013-09-25 15:23:11 +02:00
|
|
|
int _errno;
|
2016-03-04 10:20:23 +01:00
|
|
|
char txt[200];
|
2013-09-25 15:23:11 +02:00
|
|
|
|
|
|
|
if (level < dst->debug_level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
msg.msg_name = (void *) &dst->addr;
|
|
|
|
msg.msg_namelen = dst->addrlen;
|
2015-08-14 18:22:08 +02:00
|
|
|
wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
|
2013-09-25 15:23:11 +02:00
|
|
|
if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
|
2016-03-04 10:20:23 +01:00
|
|
|
sockaddr_print(MSG_MSGDUMP,
|
|
|
|
"CTRL_IFACE monitor sent successfully to",
|
|
|
|
&dst->addr, dst->addrlen);
|
2013-09-25 15:23:11 +02:00
|
|
|
dst->errors = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_errno = errno;
|
2016-03-04 10:20:23 +01:00
|
|
|
os_snprintf(txt, sizeof(txt), "CTRL_IFACE monitor: %d (%s) for",
|
|
|
|
_errno, strerror(_errno));
|
|
|
|
sockaddr_print(MSG_DEBUG, txt, &dst->addr, dst->addrlen);
|
2013-09-25 15:23:11 +02:00
|
|
|
dst->errors++;
|
|
|
|
|
|
|
|
if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
|
2016-03-04 10:20:23 +01:00
|
|
|
sockaddr_print(MSG_INFO, "CTRL_IFACE: Detach monitor that cannot receive messages:",
|
|
|
|
&dst->addr, dst->addrlen);
|
2013-09-25 15:23:11 +02:00
|
|
|
wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
|
|
|
|
dst->addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_errno == ENOBUFS || _errno == EAGAIN) {
|
|
|
|
/*
|
|
|
|
* The socket send buffer could be full. This may happen
|
|
|
|
* if client programs are not receiving their pending
|
|
|
|
* messages. Close and reopen the socket as a workaround
|
|
|
|
* to avoid getting stuck being unable to send any new
|
|
|
|
* responses.
|
|
|
|
*/
|
|
|
|
if (priv)
|
|
|
|
sock = wpas_ctrl_iface_reinit(wpa_s, priv);
|
|
|
|
else if (gp)
|
|
|
|
sock = wpas_ctrl_iface_global_reinit(
|
|
|
|
wpa_s->global, gp);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (sock < 0) {
|
|
|
|
wpa_dbg(wpa_s, MSG_DEBUG,
|
|
|
|
"Failed to reinitialize ctrl_iface socket");
|
2013-10-26 12:18:40 +02:00
|
|
|
break;
|
2013-09-25 15:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int res;
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage from;
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
|
2015-12-27 15:25:06 +01:00
|
|
|
if (priv->sock == -1)
|
|
|
|
return;
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
for (;;) {
|
|
|
|
wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
|
|
|
|
"attach", priv->wpa_s->ifname);
|
|
|
|
eloop_wait_for_read_sock(priv->sock);
|
|
|
|
|
|
|
|
res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
|
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (res < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buf[res] = '\0';
|
|
|
|
|
|
|
|
if (os_strcmp(buf, "ATTACH") == 0) {
|
|
|
|
/* handle ATTACH signal of first monitor interface */
|
2013-05-18 13:07:28 +02:00
|
|
|
if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
|
2015-01-18 15:23:43 +01:00
|
|
|
&from, fromlen,
|
|
|
|
0)) {
|
2013-09-11 05:17:07 +02:00
|
|
|
if (sendto(priv->sock, "OK\n", 3, 0,
|
|
|
|
(struct sockaddr *) &from, fromlen) <
|
|
|
|
0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
/* OK to continue */
|
|
|
|
return;
|
|
|
|
} else {
|
2013-09-11 05:17:07 +02:00
|
|
|
if (sendto(priv->sock, "FAIL\n", 5, 0,
|
|
|
|
(struct sockaddr *) &from, fromlen) <
|
|
|
|
0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* return FAIL for all other signals */
|
2013-09-11 05:17:07 +02:00
|
|
|
if (sendto(priv->sock, "FAIL\n", 5, 0,
|
|
|
|
(struct sockaddr *) &from, fromlen) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"ctrl_iface sendto failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Global ctrl_iface */
|
|
|
|
|
|
|
|
static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
|
|
|
|
void *sock_ctx)
|
|
|
|
{
|
|
|
|
struct wpa_global *global = eloop_ctx;
|
2013-05-18 13:07:28 +02:00
|
|
|
struct ctrl_iface_global_priv *priv = sock_ctx;
|
2020-05-02 20:58:59 +02:00
|
|
|
char *buf;
|
2008-02-28 02:34:43 +01:00
|
|
|
int res;
|
2016-03-04 10:20:23 +01:00
|
|
|
struct sockaddr_storage from;
|
2008-02-28 02:34:43 +01:00
|
|
|
socklen_t fromlen = sizeof(from);
|
2013-09-23 16:52:10 +02:00
|
|
|
char *reply = NULL, *reply_buf = NULL;
|
2008-02-28 02:34:43 +01:00
|
|
|
size_t reply_len;
|
|
|
|
|
2020-05-02 20:58:59 +02:00
|
|
|
buf = os_malloc(CTRL_IFACE_MAX_LEN + 1);
|
|
|
|
if (!buf)
|
|
|
|
return;
|
|
|
|
res = recvfrom(sock, buf, CTRL_IFACE_MAX_LEN, 0,
|
2008-02-28 02:34:43 +01:00
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (res < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
|
|
|
|
strerror(errno));
|
2020-05-02 20:58:59 +02:00
|
|
|
os_free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((size_t) res > CTRL_IFACE_MAX_LEN) {
|
|
|
|
wpa_printf(MSG_ERROR, "recvform(ctrl_iface): input truncated");
|
|
|
|
os_free(buf);
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[res] = '\0';
|
|
|
|
|
2013-05-18 13:07:28 +02:00
|
|
|
if (os_strcmp(buf, "ATTACH") == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
|
2015-01-18 15:23:43 +01:00
|
|
|
fromlen, 1))
|
2013-05-18 13:07:28 +02:00
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else if (os_strcmp(buf, "DETACH") == 0) {
|
|
|
|
if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
|
|
|
|
fromlen))
|
|
|
|
reply_len = 1;
|
|
|
|
else
|
|
|
|
reply_len = 2;
|
|
|
|
} else {
|
2013-09-23 16:52:10 +02:00
|
|
|
reply_buf = wpa_supplicant_global_ctrl_iface_process(
|
|
|
|
global, buf, &reply_len);
|
|
|
|
reply = reply_buf;
|
2015-06-17 15:29:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There could be some password/key material in the command, so
|
|
|
|
* clear the buffer explicitly now that it is not needed
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
os_memset(buf, 0, res);
|
2013-09-23 16:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!reply && reply_len == 1) {
|
|
|
|
reply = "FAIL\n";
|
|
|
|
reply_len = 5;
|
|
|
|
} else if (!reply && reply_len == 2) {
|
|
|
|
reply = "OK\n";
|
|
|
|
reply_len = 3;
|
2013-05-18 13:07:28 +02:00
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (reply) {
|
2015-08-14 18:22:08 +02:00
|
|
|
wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
|
|
|
|
sock, reply, reply_len);
|
2013-09-11 05:17:07 +02:00
|
|
|
if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
|
|
|
|
fromlen) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
2013-09-23 16:52:10 +02:00
|
|
|
os_free(reply_buf);
|
2020-05-02 20:58:59 +02:00
|
|
|
os_free(buf);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
|
|
|
|
struct ctrl_iface_global_priv *priv)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
2013-05-18 10:42:09 +02:00
|
|
|
const char *ctrl = global->params.ctrl_interface;
|
2013-08-24 19:31:04 +02:00
|
|
|
int flags;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2013-05-18 10:42:09 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
|
|
|
|
|
2011-02-27 17:19:17 +01:00
|
|
|
#ifdef ANDROID
|
2013-05-18 10:42:09 +02:00
|
|
|
if (os_strncmp(ctrl, "@android:", 9) == 0) {
|
|
|
|
priv->sock = android_get_control_socket(ctrl + 9);
|
|
|
|
if (priv->sock < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "Failed to open Android control "
|
|
|
|
"socket '%s'", ctrl + 9);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
|
|
|
|
ctrl + 9);
|
2014-11-25 11:11:28 +01:00
|
|
|
priv->android_control_socket = 1;
|
2011-02-27 17:19:17 +01:00
|
|
|
goto havesock;
|
2013-05-18 10:42:09 +02:00
|
|
|
}
|
2011-02-27 17:19:17 +01:00
|
|
|
|
2013-05-18 10:42:09 +02:00
|
|
|
if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
|
|
|
|
/*
|
|
|
|
* Backwards compatibility - try to open an Android control
|
|
|
|
* socket and if that fails, assume this was a UNIX domain
|
|
|
|
* socket instead.
|
|
|
|
*/
|
|
|
|
priv->sock = android_get_control_socket(ctrl);
|
|
|
|
if (priv->sock >= 0) {
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"Using Android control socket '%s'",
|
|
|
|
ctrl);
|
2014-11-25 11:11:28 +01:00
|
|
|
priv->android_control_socket = 1;
|
2013-05-18 10:42:09 +02:00
|
|
|
goto havesock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ANDROID */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (priv->sock < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&addr, 0, sizeof(addr));
|
2010-03-06 09:16:47 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-03-21 21:00:27 +01:00
|
|
|
addr.sun_len = sizeof(addr);
|
|
|
|
#endif /* __FreeBSD__ */
|
2008-02-28 02:34:43 +01:00
|
|
|
addr.sun_family = AF_UNIX;
|
2013-05-18 10:42:09 +02:00
|
|
|
|
|
|
|
if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
|
|
|
|
addr.sun_path[0] = '\0';
|
|
|
|
os_strlcpy(addr.sun_path + 1, ctrl + 10,
|
|
|
|
sizeof(addr.sun_path) - 1);
|
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
|
|
|
|
0) {
|
|
|
|
wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
|
2013-08-26 10:46:21 +02:00
|
|
|
"bind(PF_UNIX;%s) failed: %s",
|
|
|
|
ctrl, strerror(errno));
|
2013-05-18 10:42:09 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
|
|
|
|
ctrl + 10);
|
|
|
|
goto havesock;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
|
2008-02-28 02:34:43 +01:00
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
|
|
|
|
ctrl, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
if (connect(priv->sock, (struct sockaddr *) &addr,
|
|
|
|
sizeof(addr)) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
|
|
|
|
" allow connections - assuming it was left"
|
|
|
|
"over from forced program termination");
|
2013-05-18 10:42:09 +02:00
|
|
|
if (unlink(ctrl) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"Could not unlink existing ctrl_iface socket '%s': %s",
|
|
|
|
ctrl, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (bind(priv->sock, (struct sockaddr *) &addr,
|
|
|
|
sizeof(addr)) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
|
|
|
|
ctrl, strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
|
|
|
|
"ctrl_iface socket '%s'",
|
2013-05-18 10:42:09 +02:00
|
|
|
ctrl);
|
2008-02-28 02:34:43 +01:00
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
|
|
|
|
"be in use - cannot override it");
|
|
|
|
wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
|
|
|
|
"not used anymore",
|
2013-05-18 10:42:09 +02:00
|
|
|
ctrl);
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 10:42:09 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
|
|
|
|
|
2013-05-18 10:00:05 +02:00
|
|
|
if (global->params.ctrl_interface_group) {
|
|
|
|
char *gid_str = global->params.ctrl_interface_group;
|
|
|
|
gid_t gid = 0;
|
|
|
|
struct group *grp;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
grp = getgrnam(gid_str);
|
|
|
|
if (grp) {
|
|
|
|
gid = grp->gr_gid;
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
|
|
|
|
" (from group name '%s')",
|
|
|
|
(int) gid, gid_str);
|
|
|
|
} else {
|
|
|
|
/* Group name not found - try to parse this as gid */
|
|
|
|
gid = strtol(gid_str, &endp, 10);
|
|
|
|
if (*gid_str == '\0' || *endp != '\0') {
|
|
|
|
wpa_printf(MSG_ERROR, "CTRL: Invalid group "
|
|
|
|
"'%s'", gid_str);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
|
|
|
|
(int) gid);
|
|
|
|
}
|
2019-01-06 19:28:04 +01:00
|
|
|
if (lchown(ctrl, -1, gid) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR,
|
2019-01-06 19:28:04 +01:00
|
|
|
"lchown[global_ctrl_interface=%s,gid=%d]: %s",
|
2013-08-26 10:46:21 +02:00
|
|
|
ctrl, (int) gid, strerror(errno));
|
2013-05-18 10:00:05 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-05-18 10:42:09 +02:00
|
|
|
if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"chmod[global_ctrl_interface=%s]: %s",
|
|
|
|
ctrl, strerror(errno));
|
2013-05-18 10:00:05 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2013-05-18 10:42:09 +02:00
|
|
|
} else {
|
2014-06-14 00:20:18 +02:00
|
|
|
if (chmod(ctrl, S_IRWXU) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"chmod[global_ctrl_interface=%s](S_IRWXU): %s",
|
|
|
|
ctrl, strerror(errno));
|
|
|
|
/* continue anyway since group change was not required
|
|
|
|
*/
|
|
|
|
}
|
2013-05-18 10:00:05 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 17:19:17 +01:00
|
|
|
havesock:
|
2013-08-24 19:31:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make socket non-blocking so that we don't hang forever if
|
|
|
|
* target dies unexpectedly.
|
|
|
|
*/
|
|
|
|
flags = fcntl(priv->sock, F_GETFL);
|
|
|
|
if (flags >= 0) {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(priv->sock, F_SETFL, flags) < 0) {
|
2013-08-26 10:46:21 +02:00
|
|
|
wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
|
|
|
|
strerror(errno));
|
2013-08-24 19:31:04 +02:00
|
|
|
/* Not fatal, continue on.*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
eloop_register_read_sock(priv->sock,
|
|
|
|
wpa_supplicant_global_ctrl_iface_receive,
|
2013-05-18 13:07:28 +02:00
|
|
|
global, priv);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
return 0;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
fail:
|
2013-09-25 15:23:11 +02:00
|
|
|
if (priv->sock >= 0) {
|
2008-02-28 02:34:43 +01:00
|
|
|
close(priv->sock);
|
2013-09-25 15:23:11 +02:00
|
|
|
priv->sock = -1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ctrl_iface_global_priv *
|
|
|
|
wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
|
|
|
|
{
|
|
|
|
struct ctrl_iface_global_priv *priv;
|
|
|
|
|
|
|
|
priv = os_zalloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
|
|
|
return NULL;
|
|
|
|
dl_list_init(&priv->ctrl_dst);
|
2015-10-04 10:53:26 +02:00
|
|
|
dl_list_init(&priv->msg_queue);
|
2013-09-25 15:23:11 +02:00
|
|
|
priv->global = global;
|
|
|
|
priv->sock = -1;
|
|
|
|
|
|
|
|
if (global->params.ctrl_interface == NULL)
|
|
|
|
return priv;
|
|
|
|
|
|
|
|
if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
|
|
|
|
os_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-26 00:31:32 +02:00
|
|
|
wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
|
|
|
|
struct ctrl_iface_global_priv *priv)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (priv->sock <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-25 11:11:28 +01:00
|
|
|
/*
|
|
|
|
* On Android, the control socket being used may be the socket
|
|
|
|
* that is created when wpa_supplicant is started as a /init.*.rc
|
|
|
|
* service. Such a socket is maintained as a key-value pair in
|
|
|
|
* Android's environment. Closing this control socket would leave us
|
|
|
|
* in a bad state with an invalid socket descriptor.
|
|
|
|
*/
|
|
|
|
if (priv->android_control_socket)
|
|
|
|
return priv->sock;
|
|
|
|
|
2013-09-25 15:23:11 +02:00
|
|
|
eloop_unregister_read_sock(priv->sock);
|
|
|
|
close(priv->sock);
|
|
|
|
priv->sock = -1;
|
|
|
|
res = wpas_global_ctrl_iface_open_sock(global, priv);
|
|
|
|
if (res < 0)
|
|
|
|
return -1;
|
|
|
|
return priv->sock;
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
|
|
|
|
{
|
2013-05-18 13:07:28 +02:00
|
|
|
struct wpa_ctrl_dst *dst, *prev;
|
2015-10-04 10:53:26 +02:00
|
|
|
struct ctrl_iface_msg *msg, *prev_msg;
|
2013-05-18 13:07:28 +02:00
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
if (priv->sock >= 0) {
|
|
|
|
eloop_unregister_read_sock(priv->sock);
|
|
|
|
close(priv->sock);
|
|
|
|
}
|
|
|
|
if (priv->global->params.ctrl_interface)
|
|
|
|
unlink(priv->global->params.ctrl_interface);
|
2013-05-18 13:07:28 +02:00
|
|
|
dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
|
2015-10-04 10:58:24 +02:00
|
|
|
list) {
|
|
|
|
dl_list_del(&dst->list);
|
2013-05-18 13:07:28 +02:00
|
|
|
os_free(dst);
|
2015-10-04 10:58:24 +02:00
|
|
|
}
|
2015-10-04 10:53:26 +02:00
|
|
|
dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
|
|
|
|
struct ctrl_iface_msg, list) {
|
|
|
|
dl_list_del(&msg->list);
|
|
|
|
os_free(msg);
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(priv);
|
|
|
|
}
|