Home | History | Annotate | Line # | Download | only in librumpuser
sp_common.c revision 1.8
      1 /*      $NetBSD: sp_common.c,v 1.8 2010/11/24 17:00:10 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Common client/server sysproxy routines.  #included.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 #include <sys/types.h>
     35 #include <sys/mman.h>
     36 #include <sys/queue.h>
     37 #include <sys/socket.h>
     38 #include <sys/un.h>
     39 
     40 #include <arpa/inet.h>
     41 #include <netinet/in.h>
     42 #include <netinet/tcp.h>
     43 
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <poll.h>
     48 #include <pthread.h>
     49 #include <stdarg.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 //#define DEBUG
     56 #ifdef DEBUG
     57 #define DPRINTF(x) mydprintf x
     58 static void
     59 mydprintf(const char *fmt, ...)
     60 {
     61 	va_list ap;
     62 
     63 	va_start(ap, fmt);
     64 	vfprintf(stderr, fmt, ap);
     65 	va_end(ap);
     66 }
     67 #else
     68 #define DPRINTF(x)
     69 #endif
     70 
     71 /*
     72  * Bah, I hate writing on-off-wire conversions in C
     73  */
     74 
     75 enum { RUMPSP_REQ, RUMPSP_RESP };
     76 enum { RUMPSP_SYSCALL, RUMPSP_COPYIN, RUMPSP_COPYOUT, RUMPSP_ANONMMAP };
     77 
     78 struct rsp_hdr {
     79 	uint64_t rsp_len;
     80 	uint64_t rsp_reqno;
     81 	uint16_t rsp_class;
     82 	uint16_t rsp_type;
     83 	/*
     84 	 * We want this structure 64bit-aligned for typecast fun,
     85 	 * so might as well use the following for something.
     86 	 */
     87 	uint32_t rsp_sysnum;
     88 };
     89 #define HDRSZ sizeof(struct rsp_hdr)
     90 
     91 /*
     92  * Data follows the header.  We have two types of structured data.
     93  */
     94 
     95 /* copyin/copyout */
     96 struct rsp_copydata {
     97 	size_t rcp_len;
     98 	void *rcp_addr;
     99 	uint8_t rcp_data[0];
    100 };
    101 
    102 /* syscall response */
    103 struct rsp_sysresp {
    104 	int rsys_error;
    105 	register_t rsys_retval[2];
    106 };
    107 
    108 struct respwait {
    109 	uint64_t rw_reqno;
    110 	void *rw_data;
    111 	size_t rw_dlen;
    112 
    113 	pthread_cond_t rw_cv;
    114 
    115 	TAILQ_ENTRY(respwait) rw_entries;
    116 };
    117 
    118 struct spclient {
    119 	int spc_fd;
    120 	int spc_refcnt;
    121 	int spc_dying;
    122 
    123 	struct lwp *spc_mainlwp;
    124 	pid_t spc_pid;
    125 
    126 	struct pollfd *spc_pfd;
    127 
    128 	struct rsp_hdr spc_hdr;
    129 	uint8_t *spc_buf;
    130 	size_t spc_off;
    131 
    132 	pthread_mutex_t spc_mtx;
    133 	pthread_cond_t spc_cv;
    134 
    135 	uint64_t spc_nextreq;
    136 	int spc_ostatus, spc_istatus;
    137 
    138 	TAILQ_HEAD(, respwait) spc_respwait;
    139 };
    140 #define SPCSTATUS_FREE 0
    141 #define SPCSTATUS_BUSY 1
    142 #define SPCSTATUS_WANTED 2
    143 
    144 typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
    145 typedef int (*connecthook_fn)(int);
    146 
    147 static int readframe(struct spclient *);
    148 static void handlereq(struct spclient *);
    149 
    150 static void
    151 sendlock(struct spclient *spc)
    152 {
    153 
    154 	pthread_mutex_lock(&spc->spc_mtx);
    155 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    156 		spc->spc_ostatus = SPCSTATUS_WANTED;
    157 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    158 	}
    159 	spc->spc_ostatus = SPCSTATUS_BUSY;
    160 	pthread_mutex_unlock(&spc->spc_mtx);
    161 }
    162 
    163 static void
    164 sendunlock(struct spclient *spc)
    165 {
    166 
    167 	pthread_mutex_lock(&spc->spc_mtx);
    168 	if (spc->spc_ostatus == SPCSTATUS_WANTED)
    169 		pthread_cond_broadcast(&spc->spc_cv);
    170 	spc->spc_ostatus = SPCSTATUS_FREE;
    171 	pthread_mutex_unlock(&spc->spc_mtx);
    172 }
    173 
    174 static int
    175 dosend(struct spclient *spc, const void *data, size_t dlen)
    176 {
    177 	struct pollfd pfd;
    178 	const uint8_t *sdata = data;
    179 	ssize_t n;
    180 	size_t sent;
    181 	int fd = spc->spc_fd;
    182 
    183 	pfd.fd = fd;
    184 	pfd.events = POLLOUT;
    185 
    186 	for (sent = 0, n = 0; sent < dlen; ) {
    187 		if (n) {
    188 			if (poll(&pfd, 1, INFTIM) == -1) {
    189 				if (errno == EINTR)
    190 					continue;
    191 				return errno;
    192 			}
    193 		}
    194 
    195 		n = send(fd, sdata + sent, dlen - sent, MSG_NOSIGNAL);
    196 		if (n == 0) {
    197 			return EFAULT;
    198 		}
    199 		if (n == -1 && errno != EAGAIN) {
    200 			return EFAULT;
    201 		}
    202 		sent += n;
    203 	}
    204 
    205 	return 0;
    206 }
    207 
    208 static void
    209 putwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    210 {
    211 
    212 	rw->rw_data = NULL;
    213 	rw->rw_dlen = 0;
    214 	pthread_cond_init(&rw->rw_cv, NULL);
    215 
    216 	pthread_mutex_lock(&spc->spc_mtx);
    217 	rw->rw_reqno = rhdr->rsp_reqno = spc->spc_nextreq++;
    218 	TAILQ_INSERT_TAIL(&spc->spc_respwait, rw, rw_entries);
    219 }
    220 
    221 static void
    222 unputwait(struct spclient *spc, struct respwait *rw)
    223 {
    224 
    225 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    226 	pthread_mutex_unlock(&spc->spc_mtx);
    227 	pthread_cond_destroy(&rw->rw_cv);
    228 }
    229 
    230 static void
    231 kickwaiter(struct spclient *spc)
    232 {
    233 	struct respwait *rw;
    234 
    235 	pthread_mutex_lock(&spc->spc_mtx);
    236 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries) {
    237 		if (rw->rw_reqno == spc->spc_hdr.rsp_reqno)
    238 			break;
    239 	}
    240 	if (rw == NULL) {
    241 		printf("PANIC: no waiter\n");
    242 		abort();
    243 		return;
    244 	}
    245 	rw->rw_data = spc->spc_buf;
    246 	pthread_cond_signal(&rw->rw_cv);
    247 	pthread_mutex_unlock(&spc->spc_mtx);
    248 
    249 	spc->spc_buf = NULL;
    250 	spc->spc_off = 0;
    251 }
    252 
    253 static void
    254 kickall(struct spclient *spc)
    255 {
    256 	struct respwait *rw;
    257 
    258 	/* DIAGASSERT(mutex_owned(spc_lock)) */
    259 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries)
    260 		pthread_cond_signal(&rw->rw_cv);
    261 }
    262 
    263 static int
    264 waitresp(struct spclient *spc, struct respwait *rw)
    265 {
    266 	struct pollfd pfd;
    267 	int rv = 0;
    268 
    269 	while (rw->rw_data == NULL && spc->spc_dying == 0) {
    270 		/* are we free to receive? */
    271 		if (spc->spc_istatus == SPCSTATUS_FREE) {
    272 			int gotresp;
    273 
    274 			spc->spc_istatus = SPCSTATUS_BUSY;
    275 			pthread_mutex_unlock(&spc->spc_mtx);
    276 
    277 			pfd.fd = spc->spc_fd;
    278 			pfd.events = POLLIN;
    279 
    280 			for (gotresp = 0; !gotresp; ) {
    281 				rv = readframe(spc);
    282 				switch (rv) {
    283 				case 0:
    284 					poll(&pfd, 1, INFTIM);
    285 					continue;
    286 				case -1:
    287 					spc->spc_dying = 1;
    288 					break;
    289 				default:
    290 					break;
    291 				}
    292 
    293 				switch (spc->spc_hdr.rsp_class) {
    294 				case RUMPSP_RESP:
    295 					kickwaiter(spc);
    296 					gotresp = spc->spc_hdr.rsp_reqno ==
    297 					    rw->rw_reqno;
    298 					break;
    299 				case RUMPSP_REQ:
    300 					handlereq(spc);
    301 					break;
    302 				default:
    303 					/* panic */
    304 					break;
    305 				}
    306 			}
    307 			pthread_mutex_lock(&spc->spc_mtx);
    308 			if (spc->spc_istatus == SPCSTATUS_WANTED)
    309 				kickall(spc);
    310 			spc->spc_istatus = SPCSTATUS_FREE;
    311 		} else {
    312 			spc->spc_istatus = SPCSTATUS_WANTED;
    313 			pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
    314 		}
    315 	}
    316 
    317 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    318 	pthread_mutex_unlock(&spc->spc_mtx);
    319 
    320 	pthread_cond_destroy(&rw->rw_cv);
    321 	return rv;
    322 }
    323 
    324 static int
    325 readframe(struct spclient *spc)
    326 {
    327 	int fd = spc->spc_fd;
    328 	size_t left;
    329 	size_t framelen;
    330 	ssize_t n;
    331 
    332 	/* still reading header? */
    333 	if (spc->spc_off < HDRSZ) {
    334 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    335 		    spc->spc_off));
    336 
    337 		left = HDRSZ - spc->spc_off;
    338 		/*LINTED: cast ok */
    339 		n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
    340 		if (n == 0) {
    341 			return -1;
    342 		}
    343 		if (n == -1) {
    344 			if (errno == EAGAIN)
    345 				return 0;
    346 			return -1;
    347 		}
    348 
    349 		spc->spc_off += n;
    350 		if (spc->spc_off < HDRSZ)
    351 			return -1;
    352 
    353 		/*LINTED*/
    354 		framelen = spc->spc_hdr.rsp_len;
    355 
    356 		if (framelen < HDRSZ) {
    357 			return -1;
    358 		} else if (framelen == HDRSZ) {
    359 			return 1;
    360 		}
    361 
    362 		spc->spc_buf = malloc(framelen - HDRSZ);
    363 		if (spc->spc_buf == NULL) {
    364 			return -1;
    365 		}
    366 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    367 
    368 		/* "fallthrough" */
    369 	} else {
    370 		/*LINTED*/
    371 		framelen = spc->spc_hdr.rsp_len;
    372 	}
    373 
    374 	left = framelen - spc->spc_off;
    375 
    376 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    377 	    spc->spc_off, left));
    378 
    379 	if (left == 0)
    380 		return 1;
    381 	n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    382 	if (n == 0) {
    383 		return -1;
    384 	}
    385 	if (n == -1) {
    386 		if (errno == EAGAIN)
    387 			return 0;
    388 		return -1;
    389 	}
    390 	spc->spc_off += n;
    391 	left -= n;
    392 
    393 	/* got everything? */
    394 	if (left == 0)
    395 		return 1;
    396 	else
    397 		return 0;
    398 }
    399 
    400 static int
    401 tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    402 {
    403 	struct sockaddr_in sin;
    404 	char buf[64];
    405 	const char *p;
    406 	size_t l;
    407 	int port;
    408 
    409 	memset(&sin, 0, sizeof(sin));
    410 	sin.sin_len = sizeof(sin);
    411 	sin.sin_family = AF_INET;
    412 
    413 	p = strchr(addr, ':');
    414 	if (!p) {
    415 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    416 		return EINVAL;
    417 	}
    418 
    419 	l = p - addr;
    420 	if (l > sizeof(buf)-1) {
    421 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    422 		return EINVAL;
    423 	}
    424 	strncpy(buf, addr, l);
    425 	buf[l] = '\0';
    426 
    427 	/* special INADDR_ANY treatment */
    428 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    429 		sin.sin_addr.s_addr = INADDR_ANY;
    430 	} else {
    431 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    432 		case 1:
    433 			break;
    434 		case 0:
    435 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    436 			return EINVAL;
    437 		case -1:
    438 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    439 			return errno;
    440 		default:
    441 			assert(/*CONSTCOND*/0);
    442 			return EINVAL;
    443 		}
    444 	}
    445 
    446 	if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
    447 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    448 		return EINVAL;
    449 	}
    450 
    451 	/* advance to port number & parse */
    452 	p++;
    453 	l = strspn(p, "0123456789");
    454 	if (l == 0) {
    455 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    456 		return EINVAL;
    457 	}
    458 	strncpy(buf, p, l);
    459 	buf[l] = '\0';
    460 
    461 	if (*(p+l) != '/' && *(p+l) != '\0') {
    462 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    463 		return EINVAL;
    464 	}
    465 
    466 	port = atoi(buf);
    467 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    468 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    469 		return ERANGE;
    470 	}
    471 	sin.sin_port = htons(port);
    472 
    473 	*sa = malloc(sizeof(sin));
    474 	if (*sa == NULL)
    475 		return errno;
    476 	memcpy(*sa, &sin, sizeof(sin));
    477 	return 0;
    478 }
    479 
    480 static int
    481 tcp_connecthook(int s)
    482 {
    483 	int x;
    484 
    485 	x = 1;
    486 	setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    487 
    488 	return 0;
    489 }
    490 
    491 /*ARGSUSED*/
    492 static int
    493 unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    494 {
    495 	struct sockaddr_un sun;
    496 	size_t slen;
    497 
    498 	if (strlen(addr) > sizeof(sun.sun_path))
    499 		return ENAMETOOLONG;
    500 
    501 	/*
    502 	 * The pathname can be all kinds of spaghetti elementals,
    503 	 * so meek and obidient we accept everything.
    504 	 */
    505 	memset(&sun, 0, sizeof(sun));
    506 	sun.sun_family = AF_LOCAL;
    507 	strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
    508 	sun.sun_len = slen = SUN_LEN(&sun);
    509 
    510 	*sa = malloc(slen);
    511 	if (*sa == NULL)
    512 		return errno;
    513 	memcpy(*sa, &sun, slen);
    514 
    515 	return 0;
    516 }
    517 
    518 /*ARGSUSED*/
    519 static int
    520 notsupp(void)
    521 {
    522 
    523 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    524 	return EOPNOTSUPP;
    525 }
    526 
    527 static int
    528 success(void)
    529 {
    530 
    531 	return 0;
    532 }
    533 
    534 struct {
    535 	const char *id;
    536 	int domain;
    537 	addrparse_fn ap;
    538 	connecthook_fn connhook;
    539 } parsetab[] = {
    540 	{ "tcp", PF_INET, tcp_parse, tcp_connecthook },
    541 	{ "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
    542 	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
    543 };
    544 #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    545 
    546 static int
    547 parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
    548 	int allow_wildcard)
    549 {
    550 	char id[16];
    551 	const char *p, *p2;
    552 	size_t l;
    553 	unsigned i;
    554 	int error;
    555 
    556 	/*
    557 	 * Parse the url
    558 	 */
    559 
    560 	p = url;
    561 	p2 = strstr(p, "://");
    562 	if (!p2) {
    563 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    564 		return EINVAL;
    565 	}
    566 	l = p2-p;
    567 	if (l > sizeof(id)-1) {
    568 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    569 		return EINVAL;
    570 	}
    571 
    572 	strncpy(id, p, l);
    573 	id[l] = '\0';
    574 	p2 += 3; /* beginning of address */
    575 
    576 	for (i = 0; i < NPARSE; i++) {
    577 		if (strcmp(id, parsetab[i].id) == 0) {
    578 			error = parsetab[i].ap(p2, sap, allow_wildcard);
    579 			if (error)
    580 				return error;
    581 			break;
    582 		}
    583 	}
    584 	if (i == NPARSE) {
    585 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    586 		return EINVAL;
    587 	}
    588 
    589 	*idxp = i;
    590 	return 0;
    591 }
    592