unet-cli: strip initial newline in usage message
[project/unetd.git] / pex-msg.c
index 43a6960493ad8b991f449cbdaf5ab0e9d3b19d14..d7581b96e0807957a88a070d186da9ca0d977a9b 100644 (file)
--- a/pex-msg.c
+++ b/pex-msg.c
@@ -4,27 +4,56 @@
  */
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <libubox/list.h>
 #include <libubox/uloop.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip6.h>
-#include <netinet/udp.h>
+#include <libubox/usock.h>
 #include "pex-msg.h"
 #include "chacha20.h"
 #include "auth-data.h"
 
 static char pex_tx_buf[PEX_BUF_SIZE];
 static FILE *pex_urandom;
-static struct uloop_fd pex_fd;
+static struct uloop_fd pex_fd, pex_unix_fd;
 static LIST_HEAD(requests);
 static struct uloop_timeout gc_timer;
 static int pex_raw_v4_fd = -1, pex_raw_v6_fd = -1;
 
 static pex_recv_cb_t pex_recv_cb;
+static pex_recv_control_cb_t pex_control_cb;
+static int pex_unix_tx_fd = -1;
+
+int pex_socket(void)
+{
+       return pex_fd.fd;
+}
+
+int pex_raw_socket(int family)
+{
+       return family == AF_INET ? pex_raw_v4_fd : pex_raw_v6_fd;
+}
+
+static const void *
+get_mapped_sockaddr(const void *addr)
+{
+       static struct sockaddr_in6 sin6;
+       const struct sockaddr_in *sin = addr;
+
+       if (!sin || sin->sin_family != AF_INET)
+               return addr;
+
+       memset(&sin6, 0, sizeof(sin6));
+       sin6.sin6_family = AF_INET6;
+       sin6.sin6_addr.s6_addr[10] = 0xff;
+       sin6.sin6_addr.s6_addr[11] = 0xff;
+       memcpy(&sin6.sin6_addr.s6_addr[12], &sin->sin_addr, sizeof(struct in_addr));
+       sin6.sin6_port = sin->sin_port;
+
+       return &sin6;
+}
 
 struct pex_msg_update_recv_ctx {
        struct list_head list;
@@ -112,12 +141,19 @@ void *pex_msg_append(size_t len)
 static void
 pex_fd_cb(struct uloop_fd *fd, unsigned int events)
 {
-       struct sockaddr_in6 sin6;
-       static char buf[PEX_BUF_SIZE];
-       struct pex_hdr *hdr = (struct pex_hdr *)buf;
+       static struct sockaddr_in6 sin6;
+       static char buf[PEX_RX_BUF_SIZE];
        ssize_t len;
 
        while (1) {
+               static struct iovec iov[2] = {
+                       { .iov_base = &sin6 },
+                       { .iov_base = buf },
+               };
+               static struct msghdr msg = {
+                       .msg_iov = iov,
+                       .msg_iovlen = ARRAY_SIZE(iov),
+               };
                socklen_t slen = sizeof(sin6);
 
                len = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&sin6, &slen);
@@ -135,126 +171,123 @@ pex_fd_cb(struct uloop_fd *fd, unsigned int events)
                if (!len)
                        continue;
 
-               if (len < sizeof(*hdr) + sizeof(struct pex_ext_hdr))
-                       continue;
+               if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
+                       struct sockaddr_in *sin = (struct sockaddr_in *)&sin6;
+                       struct in_addr in = *(struct in_addr *)&sin6.sin6_addr.s6_addr[12];
+                       int port = sin6.sin6_port;
 
-               hdr->len = ntohs(hdr->len);
-               if (len - sizeof(hdr) - sizeof(struct pex_ext_hdr) < hdr->len)
-                       continue;
-
-               pex_recv_cb(hdr, &sin6);
-       }
-}
-
-static inline uint32_t
-csum_tcpudp_nofold(uint32_t saddr, uint32_t daddr, uint32_t len, uint8_t proto)
-{
-       uint64_t sum = 0;
-
-       sum += saddr;
-       sum += daddr;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-       sum += (proto + len) << 8;
-#else
-       sum += proto + len;
-#endif
-
-       sum = (sum & 0xffffffff) + (sum >> 32);
-       sum = (sum & 0xffffffff) + (sum >> 32);
+                       memset(&sin6, 0, sizeof(sin6));
+                       sin->sin_port = port;
+                       sin->sin_family = AF_INET;
+                       sin->sin_addr = in;
+                       slen = sizeof(*sin);
+               }
 
-       return (uint32_t)sum;
-}
+retry:
+               if (pex_unix_tx_fd >= 0) {
+                       iov[0].iov_len = slen;
+                       iov[1].iov_len = len;
+                       if (sendmsg(pex_unix_tx_fd, &msg, 0) < 0) {
+                               switch (errno) {
+                               case EINTR:
+                                       goto retry;
+                               case EMSGSIZE:
+                               case ENOBUFS:
+                               case EAGAIN:
+                                       continue;
+                               default:
+                                       perror("sendmsg");
+                                       close(pex_unix_tx_fd);
+                                       pex_unix_tx_fd = -1;
+                                       break;
+                               }
+                       }
+               }
 
-static inline uint32_t csum_add(uint32_t sum, uint32_t addend)
-{
-       sum += addend;
-       return sum + (sum < addend);
+               pex_recv_cb(buf, len, &sin6);
+       }
 }
 
-static inline uint16_t csum_fold(uint32_t sum)
+static void
+pex_unix_cb(struct uloop_fd *fd, unsigned int events)
 {
-       sum = (sum & 0xffff) + (sum >> 16);
-       sum = (sum & 0xffff) + (sum >> 16);
+       static char buf[PEX_RX_BUF_SIZE];
+       static struct iovec iov = {
+               .iov_base = buf,
+               .iov_len = sizeof(buf),
+       };
+       ssize_t len;
 
-       return (uint16_t)~sum;
-}
+       while (1) {
+               const struct sockaddr *sa = (struct sockaddr *)buf;
+               uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
+               struct msghdr msg = {
+                       .msg_iov = &iov,
+                       .msg_iovlen = 1,
+                       .msg_control = fd_buf,
+                       .msg_controllen = CMSG_LEN(sizeof(int)),
+               };
+               struct cmsghdr *cmsg;
+               socklen_t slen;
+               int *pfd;
 
-static uint32_t csum_partial(const void *buf, int len)
-{
-       const uint16_t *data = buf;
-       uint32_t sum = 0;
+               cmsg = CMSG_FIRSTHDR(&msg);
+               cmsg->cmsg_type = SCM_RIGHTS;
+               cmsg->cmsg_level = SOL_SOCKET;
+               cmsg->cmsg_len = CMSG_LEN(sizeof(int));
 
-       while (len > 1) {
-               sum += *data++;
-               len -= 2;
-       }
+               pfd = (int *)CMSG_DATA(cmsg);
+               *pfd = -1;
 
-       if (len == 1)
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-               sum += *(uint8_t *)data;
-#else
-               sum += *(uint8_t *)data << 8;
-#endif
+               len = recvmsg(fd->fd, &msg, 0);
+               if (len < 0) {
+                       if (errno == EINTR)
+                               continue;
 
-       sum = (sum & 0xffff) + (sum >> 16);
-       sum = (sum & 0xffff) + (sum >> 16);
+                       if (errno == EAGAIN)
+                               break;
 
-       return sum;
-}
+                       pex_close();
+                       return;
+               }
 
-static void pex_fixup_udpv4(void *hdr, size_t hdrlen, const void *data, size_t len)
-{
-       struct ip *ip = hdr;
-       struct udphdr *udp = hdr + ip->ip_hl * 4;
-       uint16_t udp_len = sizeof(*udp) + len;
-       uint32_t sum;
+               if (*pfd >= 0) {
+                       if (pex_unix_tx_fd >= 0)
+                               close(pex_unix_tx_fd);
 
-       if ((void *)&udp[1] > hdr + hdrlen)
-               return;
+                       pex_unix_tx_fd = *pfd;
+               }
 
-       udp->uh_sum = 0;
-       udp->uh_ulen = htons(udp_len);
-       sum = csum_tcpudp_nofold(*(uint32_t *)&ip->ip_src, *(uint32_t *)&ip->ip_dst,
-                                ip->ip_p, udp_len);
-       sum = csum_add(sum, csum_partial(udp, sizeof(*udp)));
-       sum = csum_add(sum, csum_partial(data, len));
-       udp->uh_sum = csum_fold(sum);
+               if (!len)
+                       continue;
 
-       ip->ip_len = htons(hdrlen + len);
-       ip->ip_sum = 0;
-       ip->ip_sum = csum_fold(csum_partial(ip, sizeof(*ip)));
+               if (len < sizeof(*sa))
+                       continue;
 
-#ifdef __APPLE__
-       ip->ip_len = hdrlen + len;
-#endif
-}
+               if (sa->sa_family == AF_LOCAL) {
+                       slen = sizeof(struct sockaddr);
+                       len -= slen;
+                       if (len < sizeof(struct pex_msg_local_control))
+                               continue;
 
-static void pex_fixup_udpv6(void *hdr, size_t hdrlen, const void *data, size_t len)
-{
-       struct ip6_hdr *ip = hdr;
-       struct udphdr *udp = hdr + sizeof(*ip);
-       uint16_t udp_len = htons(sizeof(*udp) + len);
+                       if (pex_control_cb)
+                               pex_control_cb((struct pex_msg_local_control *)&buf[slen], len);
 
-       if ((void *)&udp[1] > hdr + hdrlen)
-               return;
+                       continue;
+               }
 
-       ip->ip6_plen = htons(sizeof(*udp) + len);
-       udp->uh_sum = 0;
-       udp->uh_ulen = udp_len;
-       udp->uh_sum = csum_fold(csum_partial(hdr, sizeof(*ip) + sizeof(*udp)));
+               if (sa->sa_family == AF_INET)
+                       slen = sizeof(struct sockaddr_in);
+               else if (sa->sa_family == AF_INET6)
+                       slen = sizeof(struct sockaddr_in6);
+               else
+                       continue;
 
-#ifdef __APPLE__
-       ip->ip6_plen = sizeof(*udp) + len;
-#endif
+               sa = get_mapped_sockaddr(sa);
+               sendto(pex_fd.fd, buf + slen, len - slen, 0, sa, sizeof(struct sockaddr_in6));
+       }
 }
 
-static void pex_fixup_header(void *hdr, size_t hdrlen, const void *data, size_t len)
-{
-       if (hdrlen >= sizeof(struct ip6_hdr) + sizeof(struct udphdr))
-               pex_fixup_udpv6(hdr, hdrlen, data, len);
-       else if (hdrlen >= sizeof(struct ip) + sizeof(struct udphdr))
-               pex_fixup_udpv4(hdr, hdrlen, data, len);
-}
 
 int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen)
 {
@@ -267,39 +300,28 @@ int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen)
        if (fd < 0) {
                hdr->len -= sizeof(struct pex_ext_hdr);
                if (ip_hdrlen)
-                       fd = sa->sa_family == AF_INET6 ? pex_raw_v6_fd : pex_raw_v4_fd;
-               else
+                       fd = pex_raw_socket(sa->sa_family);
+               else {
                        fd = pex_fd.fd;
+                       sa = addr = get_mapped_sockaddr(addr);
+               }
 
                if (fd < 0)
                        return -1;
        }
 
        hdr->len = htons(hdr->len);
-       if (addr) {
-               struct iovec iov[2] = {
-                       { .iov_base = (void *)ip_hdr, .iov_len = ip_hdrlen },
-                       { .iov_base = pex_tx_buf, .iov_len = tx_len }
-               };
-               struct msghdr msg = {
-                       .msg_name = (void *)addr,
-                       .msg_iov = iov,
-                       .msg_iovlen = ARRAY_SIZE(iov),
-               };
+       if (ip_hdr) {
+               ret = sendto_rawudp(fd, addr, ip_hdr, ip_hdrlen, pex_tx_buf, tx_len);
+       } else if (addr) {
+               socklen_t addr_len;
 
                if (sa->sa_family == AF_INET6)
-                       msg.msg_namelen = sizeof(struct sockaddr_in6);
+                       addr_len = sizeof(struct sockaddr_in6);
                else
-                       msg.msg_namelen = sizeof(struct sockaddr_in);
+                       addr_len = sizeof(struct sockaddr_in);
 
-               if (ip_hdrlen) {
-                       pex_fixup_header(ip_hdr, ip_hdrlen, pex_tx_buf, tx_len);
-               } else {
-                       msg.msg_iov++;
-                       msg.msg_iovlen--;
-               }
-
-               ret = sendmsg(fd, &msg, 0);
+               ret = sendto(fd, pex_tx_buf, tx_len, 0, addr, addr_len);
        } else {
                ret = send(fd, pex_tx_buf, tx_len, 0);
        }
@@ -343,7 +365,7 @@ void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
 
        res = pex_msg_append(sizeof(*res));
        res->req_id = req->req_id;
-       res->data_len = len;
+       res->data_len = cpu_to_be32(len);
 
        if (!fread(e_key_priv, sizeof(e_key_priv), 1, pex_urandom))
                return;
@@ -378,7 +400,7 @@ bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx)
 
        res_ext = pex_msg_append(sizeof(*res_ext));
        res_ext->req_id = ctx->req_id;
-       res_ext->offset = ctx->cur - ctx->data;
+       res_ext->offset = cpu_to_be32(ctx->cur - ctx->data);
        pex_msg_update_response_fill(ctx);
 
        return true;
@@ -402,8 +424,10 @@ pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
        memcpy(&ctx->addr, addr, sizeof(ctx->addr));
        memcpy(ctx->auth_key, auth_key, sizeof(ctx->auth_key));
        memcpy(ctx->priv_key, priv_key, sizeof(ctx->priv_key));
-       if (!fread(&ctx->req_id, sizeof(ctx->req_id), 1, pex_urandom))
+       if (!fread(&ctx->req_id, sizeof(ctx->req_id), 1, pex_urandom)) {
+               free(ctx);
                return NULL;
+       }
        list_add_tail(&ctx->list, &requests);
        if (!gc_timer.pending)
                uloop_timeout_set(&gc_timer, 1000);
@@ -458,13 +482,13 @@ void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op
 
                ctx = pex_msg_update_recv_ctx_get(res->req_id);
                if (!ctx || ctx->data_len || !res->data_len ||
-                   res->data_len > UNETD_NET_DATA_SIZE_MAX)
+                   be32_to_cpu(res->data_len) > UNETD_NET_DATA_SIZE_MAX)
                        return NULL;
 
                data += sizeof(*res);
                len -= sizeof(*res);
 
-               ctx->data_len = res->data_len;
+               ctx->data_len = be32_to_cpu(res->data_len);
                memcpy(ctx->e_key, res->e_key, sizeof(ctx->e_key));
                ctx->data = malloc(ctx->data_len);
        } else if (op == PEX_MSG_UPDATE_RESPONSE_DATA) {
@@ -474,7 +498,7 @@ void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op
                        return NULL;
 
                ctx = pex_msg_update_recv_ctx_get(res->req_id);
-               if (!ctx || ctx->data_ofs != res->offset)
+               if (!ctx || ctx->data_ofs != be32_to_cpu(res->offset))
                        return NULL;
 
                data += sizeof(*res);
@@ -520,6 +544,28 @@ error:
        return NULL;
 }
 
+struct pex_hdr *pex_rx_accept(void *data, size_t len, bool ext)
+{
+       struct pex_hdr *hdr = data;
+       uint16_t hdr_len;
+       size_t min_size;
+
+       min_size = sizeof(*hdr);
+       if (ext)
+               min_size += sizeof(struct pex_ext_hdr);
+
+       if (len < min_size)
+               return NULL;
+
+       hdr_len = ntohs(hdr->len);
+       if (len < min_size + hdr_len)
+               return NULL;
+
+       hdr->len = hdr_len;
+
+       return hdr;
+}
+
 static void
 pex_gc_cb(struct uloop_timeout *t)
 {
@@ -612,11 +658,29 @@ close_raw:
        return -1;
 }
 
-void pex_close(void)
+int pex_unix_open(const char *path, pex_recv_control_cb_t cb)
 {
-       if (!pex_fd.cb)
-               return;
+       mode_t prev_mask;
+       int fd;
+
+       pex_control_cb = cb;
+       unlink(path);
 
+       prev_mask = umask(0177);
+       fd = usock(USOCK_UDP | USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, path, NULL);
+       umask(prev_mask);
+       if (fd < 0)
+               return -1;
+
+       pex_unix_fd.cb = pex_unix_cb;
+       pex_unix_fd.fd = fd;
+       uloop_fd_add(&pex_unix_fd, ULOOP_READ);
+
+       return 0;
+}
+
+void pex_close(void)
+{
        if (pex_raw_v4_fd >= 0)
                close(pex_raw_v4_fd);
        if (pex_raw_v6_fd >= 0)
@@ -624,9 +688,20 @@ void pex_close(void)
        pex_raw_v4_fd = -1;
        pex_raw_v6_fd = -1;
 
-       fclose(pex_urandom);
-       uloop_fd_delete(&pex_fd);
-       close(pex_fd.fd);
+       if (pex_urandom)
+               fclose(pex_urandom);
+
+       if (pex_fd.cb) {
+               uloop_fd_delete(&pex_fd);
+               close(pex_fd.fd);
+       }
+
+       if (pex_unix_fd.cb) {
+               uloop_fd_delete(&pex_unix_fd);
+               close(pex_unix_fd.fd);
+       }
+
        pex_fd.cb = NULL;
+       pex_unix_fd.cb = NULL;
        pex_urandom = NULL;
 }