diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-05-11 22:54:27 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-05-11 22:54:27 +0200 |
commit | 9d93bc7e728b6fb098161a04160e7bd2798ffc01 (patch) | |
tree | ea025aace6c2336396990aa96abaad742582d590 | |
parent | 6b3e5c45c43c75484a8d731013b380a58e90ca54 (diff) |
WIP Use abstract unix sockets on Android
-rw-r--r-- | sysdep/unix/io.c | 10 | ||||
-rw-r--r-- | sysdep/unix/unix.h | 2 | ||||
-rw-r--r-- | sysdep/unix/wg_user.c | 27 | ||||
-rw-r--r-- | sysdep/unix/wg_user.h | 4 |
4 files changed, 31 insertions, 12 deletions
diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 3aba48d2..8e6ea5f8 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -1513,11 +1513,14 @@ sk_open_unix(sock *s, char *name) } int -sk_connect_unix(sock *s, char *name) +sk_connect_unix(sock *s, char *name, socklen_t namelen) { struct sockaddr_un sa; int fd; + if (namelen > sizeof(sa.sun_path)) + return -1; + /* We are sloppy during error (leak fd and not set s->err), but we die anyway */ fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -1528,10 +1531,11 @@ sk_connect_unix(sock *s, char *name) return -1; /* Path length checked in test_old_bird() */ + memset(&sa, 0, sizeof(sa)); sa.sun_family = AF_UNIX; - strcpy(sa.sun_path, name); + memcpy(sa.sun_path, name, namelen); - if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) < 0) + if (connect(fd, (struct sockaddr *) &sa, sizeof(sa_family_t) + namelen) < 0) return -1; s->fd = fd; diff --git a/sysdep/unix/unix.h b/sysdep/unix/unix.h index 746a3590..f5f896b4 100644 --- a/sysdep/unix/unix.h +++ b/sysdep/unix/unix.h @@ -107,7 +107,7 @@ void io_loop(void); void io_log_dump(void); int sk_open_unix(struct birdsock *s, char *name); struct rfile *rf_open(struct pool *, const char *name, const char *mode); -int sk_connect_unix(struct birdsock *s, char *name); +int sk_connect_unix(struct birdsock *s, char *name, socklen_t namelen); void *rf_file(struct rfile *f); int rf_fileno(struct rfile *f); void test_old_bird(char *path); diff --git a/sysdep/unix/wg_user.c b/sysdep/unix/wg_user.c index db92ea8e..2a7e78b1 100644 --- a/sysdep/unix/wg_user.c +++ b/sysdep/unix/wg_user.c @@ -12,18 +12,32 @@ #include "sysdep/linux/wireguard.h" static -char *get_socket_path(const char *ifname, char *buf, uint size) +socklen_t get_socket_path(const char *ifname, char *buf, uint size) { - bsnprintf(buf, size, SOCKET_PATH "%s.sock", ifname); - return buf; + int pos = 0; + + if (size < 1) + return 0; + +#ifdef ANDROID + /* Abstract socket */ + buf[pos++] = 0; +#end + bsnprintf(buf+pos, size-pos, SOCKET_PATH "%s.sock", ifname); + return pos + strlen(buf+pos); } bool wg_has_userspace(const char *ifname) { struct stat sb; char tmp[sizeof(struct sockaddr_un)]; + socklen_t tmplen = get_socket_path(ifname, tmp, sizeof(tmp)); - if (stat(get_socket_path(ifname, tmp, sizeof(tmp)), &sb) == 0) + if (tmplen > 0 && tmp[0] == 0) + /* System with abstract socket (Android) always use WireGuard's userspace + implementation. */ + return true; + else if (stat(tmp, &sb) == 0) return (sb.st_mode & S_IFMT) == S_IFSOCK; else { @@ -210,14 +224,13 @@ wg_user_set_device(struct pool *pool, struct wg_device *dev) { char path[sizeof(struct sockaddr_un)]; - - bsnprintf(path, sizeof(path), SOCKET_PATH "%s.sock", ifname); + socklen_t pathlen = get_socket_path(ifname, path, sizeof(path)); struct birdsock *sock = sk_new(pool); sock->rx_hook = user_rx_hook; sock->tx_hook = user_tx_hook; sock->err_hook = user_err_hook; - int res = sk_connect_unix(sock, path); + int res = sk_connect_unix(sock, path, pathlen); DBG(L_TRACE "WG: socket %d %d %s", res, sock->fd, path); if (res < 0) { diff --git a/sysdep/unix/wg_user.h b/sysdep/unix/wg_user.h index 6ecceecb..dc15fdf2 100644 --- a/sysdep/unix/wg_user.h +++ b/sysdep/unix/wg_user.h @@ -3,7 +3,9 @@ #include "sysdep/config.h" -#define SOCKET_PATH PATH_RUNSTATEDIR "/" +#ifndef SOCKET_PATH +#define SOCKET_PATH PATH_RUNSTATEDIR "/wireguard/" +#endif SOCKET_PATH struct pool; struct wg_device; |