sp_common.c revision 1.5 1 /* $NetBSD: sp_common.c,v 1.5 2010/11/19 15:40:55 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 struct lwp *spc_lwp;
121
122 /* incoming */
123 struct rsp_hdr spc_hdr;
124 uint8_t *spc_buf;
125 size_t spc_off;
126
127 pthread_mutex_t spc_mtx;
128 pthread_cond_t spc_cv;
129
130 uint64_t spc_nextreq;
131 int spc_ostatus, spc_istatus;
132
133 TAILQ_HEAD(, respwait) spc_respwait;
134 };
135 #define SPCSTATUS_FREE 0
136 #define SPCSTATUS_BUSY 1
137 #define SPCSTATUS_WANTED 2
138
139 typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
140 typedef int (*connecthook_fn)(int);
141
142 static int readframe(struct spclient *);
143 static void handlereq(struct spclient *);
144
145 static void
146 sendlock(struct spclient *spc)
147 {
148
149 pthread_mutex_lock(&spc->spc_mtx);
150 while (spc->spc_ostatus != SPCSTATUS_FREE) {
151 spc->spc_ostatus = SPCSTATUS_WANTED;
152 pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
153 }
154 spc->spc_ostatus = SPCSTATUS_BUSY;
155 pthread_mutex_unlock(&spc->spc_mtx);
156 }
157
158 static void
159 sendunlock(struct spclient *spc)
160 {
161
162 pthread_mutex_lock(&spc->spc_mtx);
163 if (spc->spc_ostatus == SPCSTATUS_WANTED)
164 pthread_cond_broadcast(&spc->spc_cv);
165 spc->spc_ostatus = SPCSTATUS_FREE;
166 pthread_mutex_unlock(&spc->spc_mtx);
167 }
168
169 static int
170 dosend(struct spclient *spc, const void *data, size_t dlen)
171 {
172 struct pollfd pfd;
173 const uint8_t *sdata = data;
174 ssize_t n;
175 size_t sent;
176 int fd = spc->spc_fd;
177
178 pfd.fd = fd;
179 pfd.events = POLLOUT;
180
181 for (sent = 0, n = 0; sent < dlen; ) {
182 if (n) {
183 if (poll(&pfd, 1, INFTIM) == -1) {
184 if (errno == EINTR)
185 continue;
186 return errno;
187 }
188 }
189
190 n = send(fd, sdata + sent, dlen - sent, MSG_NOSIGNAL);
191 if (n == 0) {
192 return EFAULT;
193 }
194 if (n == -1 && errno != EAGAIN) {
195 return EFAULT;
196 }
197 sent += n;
198 }
199
200 return 0;
201 }
202
203 static void
204 putwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
205 {
206
207 rw->rw_data = NULL;
208 rw->rw_dlen = 0;
209 pthread_cond_init(&rw->rw_cv, NULL);
210
211 pthread_mutex_lock(&spc->spc_mtx);
212 rw->rw_reqno = rhdr->rsp_reqno = spc->spc_nextreq++;
213 TAILQ_INSERT_TAIL(&spc->spc_respwait, rw, rw_entries);
214 pthread_mutex_unlock(&spc->spc_mtx);
215 }
216
217 static void
218 kickwaiter(struct spclient *spc)
219 {
220 struct respwait *rw;
221
222 pthread_mutex_lock(&spc->spc_mtx);
223 TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries) {
224 if (rw->rw_reqno == spc->spc_hdr.rsp_reqno)
225 break;
226 }
227 if (rw == NULL) {
228 printf("PANIC: no waiter\n");
229 pthread_mutex_unlock(&spc->spc_mtx);
230 return;
231 }
232 rw->rw_data = spc->spc_buf;
233 TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
234 pthread_cond_signal(&rw->rw_cv);
235 pthread_mutex_unlock(&spc->spc_mtx);
236
237 spc->spc_buf = NULL;
238 spc->spc_off = 0;
239 }
240
241 static void
242 kickall(struct spclient *spc)
243 {
244 struct respwait *rw;
245
246 /* DIAGASSERT(mutex_owned(spc_lock)) */
247 TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries)
248 pthread_cond_signal(&rw->rw_cv);
249 }
250
251 static int
252 waitresp(struct spclient *spc, struct respwait *rw)
253 {
254 struct pollfd pfd;
255 int rv = 0;
256
257 pthread_mutex_lock(&spc->spc_mtx);
258 while (rw->rw_data == NULL) {
259 /* are we free to receive? */
260 if (spc->spc_istatus == SPCSTATUS_FREE) {
261 int gotresp;
262
263 spc->spc_istatus = SPCSTATUS_BUSY;
264 pthread_mutex_unlock(&spc->spc_mtx);
265
266 pfd.fd = spc->spc_fd;
267 pfd.events = POLLIN;
268
269 for (gotresp = 0; !gotresp; ) {
270 while (readframe(spc) < 1)
271 poll(&pfd, 1, INFTIM);
272
273 switch (spc->spc_hdr.rsp_class) {
274 case RUMPSP_RESP:
275 kickwaiter(spc);
276 gotresp = spc->spc_hdr.rsp_reqno ==
277 rw->rw_reqno;
278 break;
279 case RUMPSP_REQ:
280 handlereq(spc);
281 break;
282 default:
283 /* panic */
284 break;
285 }
286 }
287 pthread_mutex_lock(&spc->spc_mtx);
288 if (spc->spc_istatus == SPCSTATUS_WANTED)
289 kickall(spc);
290 spc->spc_istatus = SPCSTATUS_FREE;
291 pthread_mutex_unlock(&spc->spc_mtx);
292 } else {
293 spc->spc_istatus = SPCSTATUS_WANTED;
294 pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
295 }
296 }
297
298 TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
299 pthread_mutex_unlock(&spc->spc_mtx);
300
301 pthread_cond_destroy(&rw->rw_cv);
302 return rv;
303 }
304
305 static int
306 readframe(struct spclient *spc)
307 {
308 int fd = spc->spc_fd;
309 size_t left;
310 size_t framelen;
311 ssize_t n;
312
313 /* still reading header? */
314 if (spc->spc_off < HDRSZ) {
315 DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
316 spc->spc_off));
317
318 left = HDRSZ - spc->spc_off;
319 /*LINTED: cast ok */
320 n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
321 if (n == 0) {
322 return -1;
323 }
324 if (n == -1) {
325 if (errno == EAGAIN)
326 return 0;
327 return -1;
328 }
329
330 spc->spc_off += n;
331 if (spc->spc_off < HDRSZ)
332 return -1;
333
334 /*LINTED*/
335 framelen = spc->spc_hdr.rsp_len;
336
337 if (framelen < HDRSZ) {
338 return -1;
339 } else if (framelen == HDRSZ) {
340 return 1;
341 }
342
343 spc->spc_buf = malloc(framelen - HDRSZ);
344 if (spc->spc_buf == NULL) {
345 return -1;
346 }
347 memset(spc->spc_buf, 0, framelen - HDRSZ);
348
349 /* "fallthrough" */
350 } else {
351 /*LINTED*/
352 framelen = spc->spc_hdr.rsp_len;
353 }
354
355 left = framelen - spc->spc_off;
356
357 DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
358 spc->spc_off, left));
359
360 if (left == 0)
361 return 1;
362 n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
363 if (n == 0) {
364 return -1;
365 }
366 if (n == -1) {
367 if (errno == EAGAIN)
368 return 0;
369 return -1;
370 }
371 spc->spc_off += n;
372 left -= n;
373
374 /* got everything? */
375 if (left == 0)
376 return 1;
377 else
378 return 0;
379 }
380
381 static int
382 tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
383 {
384 struct sockaddr_in sin;
385 char buf[64];
386 const char *p;
387 size_t l;
388 int port;
389
390 memset(&sin, 0, sizeof(sin));
391 sin.sin_len = sizeof(sin);
392 sin.sin_family = AF_INET;
393
394 p = strchr(addr, ':');
395 if (!p) {
396 fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
397 return EINVAL;
398 }
399
400 l = p - addr;
401 if (l > sizeof(buf)-1) {
402 fprintf(stderr, "rump_sp_tcp: address too long\n");
403 return EINVAL;
404 }
405 strncpy(buf, addr, l);
406 buf[l] = '\0';
407
408 /* special INADDR_ANY treatment */
409 if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
410 sin.sin_addr.s_addr = INADDR_ANY;
411 } else {
412 switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
413 case 1:
414 break;
415 case 0:
416 fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
417 return EINVAL;
418 case -1:
419 fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
420 return errno;
421 default:
422 assert(/*CONSTCOND*/0);
423 return EINVAL;
424 }
425 }
426
427 if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
428 fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
429 return EINVAL;
430 }
431
432 /* advance to port number & parse */
433 p++;
434 l = strspn(p, "0123456789");
435 if (l == 0) {
436 fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
437 return EINVAL;
438 }
439 strncpy(buf, p, l);
440 buf[l] = '\0';
441
442 if (*(p+l) != '/' && *(p+l) != '\0') {
443 fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
444 return EINVAL;
445 }
446
447 port = atoi(buf);
448 if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
449 fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
450 return ERANGE;
451 }
452 sin.sin_port = htons(port);
453
454 *sa = malloc(sizeof(sin));
455 if (*sa == NULL)
456 return errno;
457 memcpy(*sa, &sin, sizeof(sin));
458 return 0;
459 }
460
461 static int
462 tcp_connecthook(int s)
463 {
464 int x;
465
466 x = 1;
467 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
468
469 return 0;
470 }
471
472 /*ARGSUSED*/
473 static int
474 unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
475 {
476 struct sockaddr_un sun;
477 size_t slen;
478
479 if (strlen(addr) > sizeof(sun.sun_path))
480 return ENAMETOOLONG;
481
482 /*
483 * The pathname can be all kinds of spaghetti elementals,
484 * so meek and obidient we accept everything.
485 */
486 memset(&sun, 0, sizeof(sun));
487 sun.sun_family = AF_LOCAL;
488 strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
489 sun.sun_len = slen = SUN_LEN(&sun);
490
491 *sa = malloc(slen);
492 if (*sa == NULL)
493 return errno;
494 memcpy(*sa, &sun, slen);
495
496 return 0;
497 }
498
499 /*ARGSUSED*/
500 static int
501 notsupp(void)
502 {
503
504 fprintf(stderr, "rump_sp: support not yet implemented\n");
505 return EOPNOTSUPP;
506 }
507
508 static int
509 success(void)
510 {
511
512 return 0;
513 }
514
515 struct {
516 const char *id;
517 int domain;
518 addrparse_fn ap;
519 connecthook_fn connhook;
520 } parsetab[] = {
521 { "tcp", PF_INET, tcp_parse, tcp_connecthook },
522 { "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
523 { "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
524 };
525 #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
526
527 static int
528 parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
529 int allow_wildcard)
530 {
531 char id[16];
532 const char *p, *p2;
533 size_t l;
534 unsigned i;
535 int error;
536
537 /*
538 * Parse the url
539 */
540
541 p = url;
542 p2 = strstr(p, "://");
543 if (!p2) {
544 fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
545 return EINVAL;
546 }
547 l = p2-p;
548 if (l > sizeof(id)-1) {
549 fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
550 return EINVAL;
551 }
552
553 strncpy(id, p, l);
554 id[l] = '\0';
555 p2 += 3; /* beginning of address */
556
557 for (i = 0; i < NPARSE; i++) {
558 if (strcmp(id, parsetab[i].id) == 0) {
559 error = parsetab[i].ap(p2, sap, allow_wildcard);
560 if (error)
561 return error;
562 break;
563 }
564 }
565 if (i == NPARSE) {
566 fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
567 return EINVAL;
568 }
569
570 *idxp = i;
571 return 0;
572 }
573