wg: Use seqpacket instead of dgram

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2016-07-22 20:30:34 +02:00
parent f304bc3199
commit ec890556e4
2 changed files with 6 additions and 35 deletions

View File

@ -80,21 +80,10 @@ static int add_next_to_inflatable_buffer(struct inflatable_buffer *buffer)
return 0; return 0;
} }
static void close_and_unlink(int fd)
{
struct sockaddr_un addr;
socklen_t len = sizeof(addr);
if (!getsockname(fd, (struct sockaddr *)&addr, &len))
unlink(addr.sun_path);
close(fd);
}
static int userspace_interface_fd(const char *interface) static int userspace_interface_fd(const char *interface)
{ {
struct stat sbuf; struct stat sbuf;
struct sockaddr_un addr = { .sun_family = AF_UNIX }, bind_addr = { .sun_family = AF_UNIX }; struct sockaddr_un addr = { .sun_family = AF_UNIX };
mode_t old_umask;
int fd = -1, ret; int fd = -1, ret;
ret = -EINVAL; ret = -EINVAL;
@ -103,10 +92,6 @@ static int userspace_interface_fd(const char *interface)
ret = snprintf(addr.sun_path, sizeof(addr.sun_path) - 1, SOCK_PATH "%s" SOCK_SUFFIX, interface); ret = snprintf(addr.sun_path, sizeof(addr.sun_path) - 1, SOCK_PATH "%s" SOCK_SUFFIX, interface);
if (ret < 0) if (ret < 0)
goto out; goto out;
ret = snprintf(bind_addr.sun_path, sizeof(bind_addr.sun_path) - 1, SOCK_PATH ".wg-tool-%s-%d.client", interface, getpid());
if (ret < 0)
goto out;
unlink(bind_addr.sun_path);
ret = stat(addr.sun_path, &sbuf); ret = stat(addr.sun_path, &sbuf);
if (ret < 0) if (ret < 0)
goto out; goto out;
@ -114,12 +99,7 @@ static int userspace_interface_fd(const char *interface)
if (!S_ISSOCK(sbuf.st_mode)) if (!S_ISSOCK(sbuf.st_mode))
goto out; goto out;
ret = fd = socket(AF_UNIX, SOCK_DGRAM, 0); ret = fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (ret < 0)
goto out;
old_umask = umask(0077);
ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
umask(old_umask);
if (ret < 0) if (ret < 0)
goto out; goto out;
@ -131,7 +111,7 @@ static int userspace_interface_fd(const char *interface)
} }
out: out:
if (ret && fd >= 0) if (ret && fd >= 0)
close_and_unlink(fd); close(fd);
if (!ret) if (!ret)
ret = fd; ret = fd;
return ret; return ret;
@ -142,7 +122,7 @@ static bool userspace_has_wireguard_interface(const char *interface)
int fd = userspace_interface_fd(interface); int fd = userspace_interface_fd(interface);
if (fd < 0) if (fd < 0)
return false; return false;
close_and_unlink(fd); close(fd);
return true; return true;
} }
@ -200,7 +180,7 @@ static int userspace_set_device(struct wgdevice *dev)
goto out; goto out;
ret = ret_code; ret = ret_code;
out: out:
close_and_unlink(fd); close(fd);
errno = -ret; errno = -ret;
return (int)ret; return (int)ret;
} }
@ -251,7 +231,7 @@ static int userspace_get_device(struct wgdevice **dev, const char *interface)
out: out:
if (*dev && ret) if (*dev && ret)
free(*dev); free(*dev);
close_and_unlink(fd); close(fd);
errno = -ret; errno = -ret;
return ret; return ret;
} }

View File

@ -12,13 +12,4 @@ int ipc_get_device(struct wgdevice **dev, const char *interface);
char *ipc_list_devices(void); char *ipc_list_devices(void);
bool ipc_has_device(const char *interface); bool ipc_has_device(const char *interface);
#define for_each_wgpeer(__dev, __peer, __i) for ((__i) = 0, (__peer) = (typeof(__peer))((uint8_t *)(__dev) + sizeof(struct wgdevice)); \
(__i) < (__dev)->num_peers; \
++(__i), (__peer) = (typeof(__peer))((uint8_t *)(__peer) + sizeof(struct wgpeer) + (sizeof(struct wgipmask) * (__peer)->num_ipmasks)))
#define for_each_wgipmask(__peer, __ipmask, __i) for ((__i) = 0, (__ipmask) = (typeof(__ipmask))((uint8_t *)(__peer) + sizeof(struct wgpeer)); \
(__i) < (__peer)->num_ipmasks; \
++(__i), (__ipmask) = (typeof(__ipmask))((uint8_t *)(__ipmask) + sizeof(struct wgipmask)))
#endif #endif