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