sp_common.c revision 1.9 1 /* $NetBSD: sp_common.c,v 1.9 2010/11/24 17:20:24 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 switch (readframe(spc)) {
282 case 0:
283 poll(&pfd, 1, INFTIM);
284 continue;
285 case -1:
286 rv = errno;
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
322 return rv;
323 }
324
325 static int
326 readframe(struct spclient *spc)
327 {
328 int fd = spc->spc_fd;
329 size_t left;
330 size_t framelen;
331 ssize_t n;
332
333 /* still reading header? */
334 if (spc->spc_off < HDRSZ) {
335 DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
336 spc->spc_off));
337
338 left = HDRSZ - spc->spc_off;
339 /*LINTED: cast ok */
340 n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
341 if (n == 0) {
342 return -1;
343 }
344 if (n == -1) {
345 if (errno == EAGAIN)
346 return 0;
347 return -1;
348 }
349
350 spc->spc_off += n;
351 if (spc->spc_off < HDRSZ)
352 return -1;
353
354 /*LINTED*/
355 framelen = spc->spc_hdr.rsp_len;
356
357 if (framelen < HDRSZ) {
358 return -1;
359 } else if (framelen == HDRSZ) {
360 return 1;
361 }
362
363 spc->spc_buf = malloc(framelen - HDRSZ);
364 if (spc->spc_buf == NULL) {
365 return -1;
366 }
367 memset(spc->spc_buf, 0, framelen - HDRSZ);
368
369 /* "fallthrough" */
370 } else {
371 /*LINTED*/
372 framelen = spc->spc_hdr.rsp_len;
373 }
374
375 left = framelen - spc->spc_off;
376
377 DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
378 spc->spc_off, left));
379
380 if (left == 0)
381 return 1;
382 n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
383 if (n == 0) {
384 return -1;
385 }
386 if (n == -1) {
387 if (errno == EAGAIN)
388 return 0;
389 return -1;
390 }
391 spc->spc_off += n;
392 left -= n;
393
394 /* got everything? */
395 if (left == 0)
396 return 1;
397 else
398 return 0;
399 }
400
401 static int
402 tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
403 {
404 struct sockaddr_in sin;
405 char buf[64];
406 const char *p;
407 size_t l;
408 int port;
409
410 memset(&sin, 0, sizeof(sin));
411 sin.sin_len = sizeof(sin);
412 sin.sin_family = AF_INET;
413
414 p = strchr(addr, ':');
415 if (!p) {
416 fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
417 return EINVAL;
418 }
419
420 l = p - addr;
421 if (l > sizeof(buf)-1) {
422 fprintf(stderr, "rump_sp_tcp: address too long\n");
423 return EINVAL;
424 }
425 strncpy(buf, addr, l);
426 buf[l] = '\0';
427
428 /* special INADDR_ANY treatment */
429 if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
430 sin.sin_addr.s_addr = INADDR_ANY;
431 } else {
432 switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
433 case 1:
434 break;
435 case 0:
436 fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
437 return EINVAL;
438 case -1:
439 fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
440 return errno;
441 default:
442 assert(/*CONSTCOND*/0);
443 return EINVAL;
444 }
445 }
446
447 if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
448 fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
449 return EINVAL;
450 }
451
452 /* advance to port number & parse */
453 p++;
454 l = strspn(p, "0123456789");
455 if (l == 0) {
456 fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
457 return EINVAL;
458 }
459 strncpy(buf, p, l);
460 buf[l] = '\0';
461
462 if (*(p+l) != '/' && *(p+l) != '\0') {
463 fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
464 return EINVAL;
465 }
466
467 port = atoi(buf);
468 if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
469 fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
470 return ERANGE;
471 }
472 sin.sin_port = htons(port);
473
474 *sa = malloc(sizeof(sin));
475 if (*sa == NULL)
476 return errno;
477 memcpy(*sa, &sin, sizeof(sin));
478 return 0;
479 }
480
481 static int
482 tcp_connecthook(int s)
483 {
484 int x;
485
486 x = 1;
487 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
488
489 return 0;
490 }
491
492 /*ARGSUSED*/
493 static int
494 unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
495 {
496 struct sockaddr_un sun;
497 size_t slen;
498
499 if (strlen(addr) > sizeof(sun.sun_path))
500 return ENAMETOOLONG;
501
502 /*
503 * The pathname can be all kinds of spaghetti elementals,
504 * so meek and obidient we accept everything.
505 */
506 memset(&sun, 0, sizeof(sun));
507 sun.sun_family = AF_LOCAL;
508 strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
509 sun.sun_len = slen = SUN_LEN(&sun);
510
511 *sa = malloc(slen);
512 if (*sa == NULL)
513 return errno;
514 memcpy(*sa, &sun, slen);
515
516 return 0;
517 }
518
519 /*ARGSUSED*/
520 static int
521 notsupp(void)
522 {
523
524 fprintf(stderr, "rump_sp: support not yet implemented\n");
525 return EOPNOTSUPP;
526 }
527
528 static int
529 success(void)
530 {
531
532 return 0;
533 }
534
535 struct {
536 const char *id;
537 int domain;
538 addrparse_fn ap;
539 connecthook_fn connhook;
540 } parsetab[] = {
541 { "tcp", PF_INET, tcp_parse, tcp_connecthook },
542 { "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
543 { "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
544 };
545 #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
546
547 static int
548 parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
549 int allow_wildcard)
550 {
551 char id[16];
552 const char *p, *p2;
553 size_t l;
554 unsigned i;
555 int error;
556
557 /*
558 * Parse the url
559 */
560
561 p = url;
562 p2 = strstr(p, "://");
563 if (!p2) {
564 fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
565 return EINVAL;
566 }
567 l = p2-p;
568 if (l > sizeof(id)-1) {
569 fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
570 return EINVAL;
571 }
572
573 strncpy(id, p, l);
574 id[l] = '\0';
575 p2 += 3; /* beginning of address */
576
577 for (i = 0; i < NPARSE; i++) {
578 if (strcmp(id, parsetab[i].id) == 0) {
579 error = parsetab[i].ap(p2, sap, allow_wildcard);
580 if (error)
581 return error;
582 break;
583 }
584 }
585 if (i == NPARSE) {
586 fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
587 return EINVAL;
588 }
589
590 *idxp = i;
591 return 0;
592 }
593