Home | History | Annotate | Line # | Download | only in librumpuser
sp_common.c revision 1.1
      1  1.1  pooka /*      $NetBSD: sp_common.c,v 1.1 2010/11/04 20:54:07 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * Common client/server sysproxy routines.  #included.
     30  1.1  pooka  */
     31  1.1  pooka 
     32  1.1  pooka #include <sys/cdefs.h>
     33  1.1  pooka 
     34  1.1  pooka #include <sys/types.h>
     35  1.1  pooka #include <sys/mman.h>
     36  1.1  pooka #include <sys/socket.h>
     37  1.1  pooka 
     38  1.1  pooka #include <arpa/inet.h>
     39  1.1  pooka #include <netinet/in.h>
     40  1.1  pooka #include <netinet/tcp.h>
     41  1.1  pooka 
     42  1.1  pooka #include <assert.h>
     43  1.1  pooka #include <errno.h>
     44  1.1  pooka #include <fcntl.h>
     45  1.1  pooka #include <poll.h>
     46  1.1  pooka #include <stdarg.h>
     47  1.1  pooka #include <stdio.h>
     48  1.1  pooka #include <stdlib.h>
     49  1.1  pooka #include <string.h>
     50  1.1  pooka #include <unistd.h>
     51  1.1  pooka 
     52  1.1  pooka //#define DEBUG
     53  1.1  pooka #ifdef DEBUG
     54  1.1  pooka #define DPRINTF(x) mydprintf x
     55  1.1  pooka static void
     56  1.1  pooka mydprintf(const char *fmt, ...)
     57  1.1  pooka {
     58  1.1  pooka 	va_list ap;
     59  1.1  pooka 
     60  1.1  pooka 	va_start(ap, fmt);
     61  1.1  pooka 	vfprintf(stderr, fmt, ap);
     62  1.1  pooka 	va_end(ap);
     63  1.1  pooka }
     64  1.1  pooka #else
     65  1.1  pooka #define DPRINTF(x)
     66  1.1  pooka #endif
     67  1.1  pooka 
     68  1.1  pooka /*
     69  1.1  pooka  * Bah, I hate writing on-off-wire conversions in C
     70  1.1  pooka  */
     71  1.1  pooka 
     72  1.1  pooka enum {
     73  1.1  pooka 	RUMPSP_SYSCALL_REQ,	RUMPSP_SYSCALL_RESP,
     74  1.1  pooka 	RUMPSP_COPYIN_REQ,	RUMPSP_COPYIN_RESP,
     75  1.1  pooka 	RUMPSP_COPYOUT_REQ,	/* no copyout resp */
     76  1.1  pooka 	RUMPSP_ANONMMAP_REQ,	RUMPSP_ANONMMAP_RESP
     77  1.1  pooka };
     78  1.1  pooka 
     79  1.1  pooka struct rsp_hdr {
     80  1.1  pooka 	uint64_t rsp_len;
     81  1.1  pooka 	uint64_t rsp_reqno;
     82  1.1  pooka 	uint32_t rsp_type;
     83  1.1  pooka 	/*
     84  1.1  pooka 	 * We want this structure 64bit-aligned for typecast fun,
     85  1.1  pooka 	 * so might as well use the following for something.
     86  1.1  pooka 	 */
     87  1.1  pooka 	uint32_t rsp_sysnum;
     88  1.1  pooka };
     89  1.1  pooka #define HDRSZ sizeof(struct rsp_hdr)
     90  1.1  pooka 
     91  1.1  pooka /*
     92  1.1  pooka  * Data follows the header.  We have two types of structured data.
     93  1.1  pooka  */
     94  1.1  pooka 
     95  1.1  pooka /* copyin/copyout */
     96  1.1  pooka struct rsp_copydata {
     97  1.1  pooka 	size_t rcp_len;
     98  1.1  pooka 	void *rcp_addr;
     99  1.1  pooka 	uint8_t rcp_data[0];
    100  1.1  pooka };
    101  1.1  pooka 
    102  1.1  pooka /* syscall response */
    103  1.1  pooka struct rsp_sysresp {
    104  1.1  pooka 	int rsys_error;
    105  1.1  pooka 	register_t rsys_retval[2];
    106  1.1  pooka };
    107  1.1  pooka 
    108  1.1  pooka 
    109  1.1  pooka struct spclient {
    110  1.1  pooka 	int spc_fd;
    111  1.1  pooka 	struct lwp *spc_lwp;
    112  1.1  pooka 
    113  1.1  pooka 	/* incoming */
    114  1.1  pooka 	struct rsp_hdr spc_hdr;
    115  1.1  pooka 	uint8_t *spc_buf;
    116  1.1  pooka 	size_t spc_off;
    117  1.1  pooka 
    118  1.1  pooka #if 0
    119  1.1  pooka 	/* outgoing */
    120  1.1  pooka 	int spc_obusy;
    121  1.1  pooka 	pthread_mutex_t spc_omtx;
    122  1.1  pooka 	pthread_cond_t spc_cv;
    123  1.1  pooka #endif
    124  1.1  pooka };
    125  1.1  pooka 
    126  1.1  pooka typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
    127  1.1  pooka typedef int (*connecthook_fn)(int);
    128  1.1  pooka 
    129  1.1  pooka static uint64_t nextreq;
    130  1.1  pooka 
    131  1.1  pooka static int
    132  1.1  pooka dosend(struct spclient *spc, const void *data, size_t dlen)
    133  1.1  pooka {
    134  1.1  pooka 	struct pollfd pfd;
    135  1.1  pooka 	const uint8_t *sdata = data;
    136  1.1  pooka 	ssize_t n;
    137  1.1  pooka 	size_t sent;
    138  1.1  pooka 	int fd = spc->spc_fd;
    139  1.1  pooka 
    140  1.1  pooka 	pfd.fd = fd;
    141  1.1  pooka 	pfd.events = POLLOUT;
    142  1.1  pooka 
    143  1.1  pooka 	for (sent = 0, n = 0; sent < dlen; ) {
    144  1.1  pooka 		if (n) {
    145  1.1  pooka 			if (poll(&pfd, 1, INFTIM) == -1) {
    146  1.1  pooka 				if (errno == EINTR)
    147  1.1  pooka 					continue;
    148  1.1  pooka 				return errno;
    149  1.1  pooka 			}
    150  1.1  pooka 		}
    151  1.1  pooka 
    152  1.1  pooka 		n = write(fd, sdata + sent, dlen - sent);
    153  1.1  pooka 		if (n == 0) {
    154  1.1  pooka 			return EFAULT;
    155  1.1  pooka 		}
    156  1.1  pooka 		if (n == -1 && errno != EAGAIN) {
    157  1.1  pooka 			return EFAULT;
    158  1.1  pooka 		}
    159  1.1  pooka 		sent += n;
    160  1.1  pooka 	}
    161  1.1  pooka 
    162  1.1  pooka 	return 0;
    163  1.1  pooka }
    164  1.1  pooka 
    165  1.1  pooka static int
    166  1.1  pooka readframe(struct spclient *spc)
    167  1.1  pooka {
    168  1.1  pooka 	int fd = spc->spc_fd;
    169  1.1  pooka 	size_t left;
    170  1.1  pooka 	size_t framelen;
    171  1.1  pooka 	ssize_t n;
    172  1.1  pooka 
    173  1.1  pooka 	/* still reading header? */
    174  1.1  pooka 	if (spc->spc_off < HDRSZ) {
    175  1.1  pooka 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    176  1.1  pooka 		    spc->spc_off));
    177  1.1  pooka 
    178  1.1  pooka 		left = HDRSZ - spc->spc_off;
    179  1.1  pooka 		/*LINTED: cast ok */
    180  1.1  pooka 		n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
    181  1.1  pooka 		if (n == 0) {
    182  1.1  pooka 			return -1;
    183  1.1  pooka 		}
    184  1.1  pooka 		if (n == -1) {
    185  1.1  pooka 			if (errno == EAGAIN)
    186  1.1  pooka 				return 0;
    187  1.1  pooka 			return -1;
    188  1.1  pooka 		}
    189  1.1  pooka 
    190  1.1  pooka 		spc->spc_off += n;
    191  1.1  pooka 		if (spc->spc_off < HDRSZ)
    192  1.1  pooka 			return -1;
    193  1.1  pooka 
    194  1.1  pooka 		/*LINTED*/
    195  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    196  1.1  pooka 
    197  1.1  pooka 		if (framelen < HDRSZ) {
    198  1.1  pooka 			return -1;
    199  1.1  pooka 		} else if (framelen == HDRSZ) {
    200  1.1  pooka 			return 1;
    201  1.1  pooka 		}
    202  1.1  pooka 
    203  1.1  pooka 		spc->spc_buf = malloc(framelen - HDRSZ);
    204  1.1  pooka 		if (spc->spc_buf == NULL) {
    205  1.1  pooka 			return -1;
    206  1.1  pooka 		}
    207  1.1  pooka 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    208  1.1  pooka 
    209  1.1  pooka 		/* "fallthrough" */
    210  1.1  pooka 	} else {
    211  1.1  pooka 		/*LINTED*/
    212  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    213  1.1  pooka 	}
    214  1.1  pooka 
    215  1.1  pooka 	left = framelen - spc->spc_off;
    216  1.1  pooka 
    217  1.1  pooka 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    218  1.1  pooka 	    spc->spc_off, left));
    219  1.1  pooka 
    220  1.1  pooka 	if (left == 0)
    221  1.1  pooka 		return 1;
    222  1.1  pooka 	n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    223  1.1  pooka 	if (n == 0) {
    224  1.1  pooka 		return -1;
    225  1.1  pooka 	}
    226  1.1  pooka 	if (n == -1) {
    227  1.1  pooka 		if (errno == EAGAIN)
    228  1.1  pooka 			return 0;
    229  1.1  pooka 		return -1;
    230  1.1  pooka 	}
    231  1.1  pooka 	spc->spc_off += n;
    232  1.1  pooka 	left -= n;
    233  1.1  pooka 
    234  1.1  pooka 	/* got everything? */
    235  1.1  pooka 	if (left == 0)
    236  1.1  pooka 		return 1;
    237  1.1  pooka 	else
    238  1.1  pooka 		return 0;
    239  1.1  pooka }
    240  1.1  pooka 
    241  1.1  pooka static int
    242  1.1  pooka tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    243  1.1  pooka {
    244  1.1  pooka 	struct sockaddr_in sin;
    245  1.1  pooka 	char buf[64];
    246  1.1  pooka 	const char *p;
    247  1.1  pooka 	size_t l;
    248  1.1  pooka 	int port;
    249  1.1  pooka 
    250  1.1  pooka 	memset(&sin, 0, sizeof(sin));
    251  1.1  pooka 	sin.sin_len = sizeof(sin);
    252  1.1  pooka 	sin.sin_family = AF_INET;
    253  1.1  pooka 
    254  1.1  pooka 	p = strchr(addr, ':');
    255  1.1  pooka 	if (!p) {
    256  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    257  1.1  pooka 		return EINVAL;
    258  1.1  pooka 	}
    259  1.1  pooka 
    260  1.1  pooka 	l = p - addr;
    261  1.1  pooka 	if (l > sizeof(buf)-1) {
    262  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    263  1.1  pooka 		return EINVAL;
    264  1.1  pooka 	}
    265  1.1  pooka 	strncpy(buf, addr, l);
    266  1.1  pooka 	buf[l] = '\0';
    267  1.1  pooka 
    268  1.1  pooka 	/* special INADDR_ANY treatment */
    269  1.1  pooka 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    270  1.1  pooka 		sin.sin_addr.s_addr = INADDR_ANY;
    271  1.1  pooka 	} else {
    272  1.1  pooka 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    273  1.1  pooka 		case 1:
    274  1.1  pooka 			break;
    275  1.1  pooka 		case 0:
    276  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    277  1.1  pooka 			return EINVAL;
    278  1.1  pooka 		case -1:
    279  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    280  1.1  pooka 			return errno;
    281  1.1  pooka 		default:
    282  1.1  pooka 			assert(/*CONSTCOND*/0);
    283  1.1  pooka 			return EINVAL;
    284  1.1  pooka 		}
    285  1.1  pooka 	}
    286  1.1  pooka 
    287  1.1  pooka 	if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
    288  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    289  1.1  pooka 		return EINVAL;
    290  1.1  pooka 	}
    291  1.1  pooka 
    292  1.1  pooka 	/* advance to port number & parse */
    293  1.1  pooka 	p++;
    294  1.1  pooka 	l = strspn(p, "0123456789");
    295  1.1  pooka 	if (l == 0) {
    296  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    297  1.1  pooka 		return EINVAL;
    298  1.1  pooka 	}
    299  1.1  pooka 	strncpy(buf, p, l);
    300  1.1  pooka 	buf[l] = '\0';
    301  1.1  pooka 
    302  1.1  pooka 	if (*(p+l) != '/' && *(p+l) != '\0') {
    303  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    304  1.1  pooka 		return EINVAL;
    305  1.1  pooka 	}
    306  1.1  pooka 
    307  1.1  pooka 	port = atoi(buf);
    308  1.1  pooka 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    309  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    310  1.1  pooka 		return ERANGE;
    311  1.1  pooka 	}
    312  1.1  pooka 	sin.sin_port = htons(port);
    313  1.1  pooka 
    314  1.1  pooka 	*sa = malloc(sizeof(sin));
    315  1.1  pooka 	if (*sa == NULL)
    316  1.1  pooka 		return errno;
    317  1.1  pooka 	memcpy(*sa, &sin, sizeof(sin));
    318  1.1  pooka 	return 0;
    319  1.1  pooka }
    320  1.1  pooka 
    321  1.1  pooka static int
    322  1.1  pooka tcp_connecthook(int s)
    323  1.1  pooka {
    324  1.1  pooka 	int x;
    325  1.1  pooka 
    326  1.1  pooka 	x = 1;
    327  1.1  pooka 	setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    328  1.1  pooka 
    329  1.1  pooka 	return 0;
    330  1.1  pooka }
    331  1.1  pooka 
    332  1.1  pooka /*ARGSUSED*/
    333  1.1  pooka static int
    334  1.1  pooka notsupp(void)
    335  1.1  pooka {
    336  1.1  pooka 
    337  1.1  pooka 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    338  1.1  pooka 	return EOPNOTSUPP;
    339  1.1  pooka }
    340  1.1  pooka 
    341  1.1  pooka static int
    342  1.1  pooka success(void)
    343  1.1  pooka {
    344  1.1  pooka 
    345  1.1  pooka 	return 0;
    346  1.1  pooka }
    347  1.1  pooka 
    348  1.1  pooka struct {
    349  1.1  pooka 	const char *id;
    350  1.1  pooka 	int domain;
    351  1.1  pooka 	addrparse_fn ap;
    352  1.1  pooka 	connecthook_fn connhook;
    353  1.1  pooka } parsetab[] = {
    354  1.1  pooka 	{ "tcp", PF_INET, tcp_parse, tcp_connecthook },
    355  1.1  pooka 	{ "unix", PF_LOCAL, (addrparse_fn)notsupp, (connecthook_fn)success },
    356  1.1  pooka 	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
    357  1.1  pooka };
    358  1.1  pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    359  1.1  pooka 
    360  1.1  pooka static int
    361  1.1  pooka parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
    362  1.1  pooka 	int allow_wildcard)
    363  1.1  pooka {
    364  1.1  pooka 	char id[16];
    365  1.1  pooka 	const char *p, *p2;
    366  1.1  pooka 	size_t l;
    367  1.1  pooka 	unsigned i;
    368  1.1  pooka 	int error;
    369  1.1  pooka 
    370  1.1  pooka 	/*
    371  1.1  pooka 	 * Parse the url
    372  1.1  pooka 	 */
    373  1.1  pooka 
    374  1.1  pooka 	p = url;
    375  1.1  pooka 	p2 = strstr(p, "://");
    376  1.1  pooka 	if (!p2) {
    377  1.1  pooka 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    378  1.1  pooka 		return EINVAL;
    379  1.1  pooka 	}
    380  1.1  pooka 	l = p2-p;
    381  1.1  pooka 	if (l > sizeof(id)-1) {
    382  1.1  pooka 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    383  1.1  pooka 		return EINVAL;
    384  1.1  pooka 	}
    385  1.1  pooka 
    386  1.1  pooka 	strncpy(id, p, l);
    387  1.1  pooka 	id[l] = '\0';
    388  1.1  pooka 	p2 += 3; /* beginning of address */
    389  1.1  pooka 
    390  1.1  pooka 	for (i = 0; i < NPARSE; i++) {
    391  1.1  pooka 		if (strcmp(id, parsetab[i].id) == 0) {
    392  1.1  pooka 			error = parsetab[i].ap(p2, sap, allow_wildcard);
    393  1.1  pooka 			if (error)
    394  1.1  pooka 				return error;
    395  1.1  pooka 			break;
    396  1.1  pooka 		}
    397  1.1  pooka 	}
    398  1.1  pooka 	if (i == NPARSE) {
    399  1.1  pooka 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    400  1.1  pooka 		return EINVAL;
    401  1.1  pooka 	}
    402  1.1  pooka 
    403  1.1  pooka 	*idxp = i;
    404  1.1  pooka 	return 0;
    405  1.1  pooka }
    406