sp_common.c revision 1.2 1 1.2 pooka /* $NetBSD: sp_common.c,v 1.2 2010/11/05 14:23:45 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.2 pooka #include <sys/un.h>
38 1.1 pooka
39 1.1 pooka #include <arpa/inet.h>
40 1.1 pooka #include <netinet/in.h>
41 1.1 pooka #include <netinet/tcp.h>
42 1.1 pooka
43 1.1 pooka #include <assert.h>
44 1.1 pooka #include <errno.h>
45 1.1 pooka #include <fcntl.h>
46 1.1 pooka #include <poll.h>
47 1.1 pooka #include <stdarg.h>
48 1.1 pooka #include <stdio.h>
49 1.1 pooka #include <stdlib.h>
50 1.1 pooka #include <string.h>
51 1.1 pooka #include <unistd.h>
52 1.1 pooka
53 1.1 pooka //#define DEBUG
54 1.1 pooka #ifdef DEBUG
55 1.1 pooka #define DPRINTF(x) mydprintf x
56 1.1 pooka static void
57 1.1 pooka mydprintf(const char *fmt, ...)
58 1.1 pooka {
59 1.1 pooka va_list ap;
60 1.1 pooka
61 1.1 pooka va_start(ap, fmt);
62 1.1 pooka vfprintf(stderr, fmt, ap);
63 1.1 pooka va_end(ap);
64 1.1 pooka }
65 1.1 pooka #else
66 1.1 pooka #define DPRINTF(x)
67 1.1 pooka #endif
68 1.1 pooka
69 1.1 pooka /*
70 1.1 pooka * Bah, I hate writing on-off-wire conversions in C
71 1.1 pooka */
72 1.1 pooka
73 1.1 pooka enum {
74 1.1 pooka RUMPSP_SYSCALL_REQ, RUMPSP_SYSCALL_RESP,
75 1.1 pooka RUMPSP_COPYIN_REQ, RUMPSP_COPYIN_RESP,
76 1.1 pooka RUMPSP_COPYOUT_REQ, /* no copyout resp */
77 1.1 pooka RUMPSP_ANONMMAP_REQ, RUMPSP_ANONMMAP_RESP
78 1.1 pooka };
79 1.1 pooka
80 1.1 pooka struct rsp_hdr {
81 1.1 pooka uint64_t rsp_len;
82 1.1 pooka uint64_t rsp_reqno;
83 1.1 pooka uint32_t rsp_type;
84 1.1 pooka /*
85 1.1 pooka * We want this structure 64bit-aligned for typecast fun,
86 1.1 pooka * so might as well use the following for something.
87 1.1 pooka */
88 1.1 pooka uint32_t rsp_sysnum;
89 1.1 pooka };
90 1.1 pooka #define HDRSZ sizeof(struct rsp_hdr)
91 1.1 pooka
92 1.1 pooka /*
93 1.1 pooka * Data follows the header. We have two types of structured data.
94 1.1 pooka */
95 1.1 pooka
96 1.1 pooka /* copyin/copyout */
97 1.1 pooka struct rsp_copydata {
98 1.1 pooka size_t rcp_len;
99 1.1 pooka void *rcp_addr;
100 1.1 pooka uint8_t rcp_data[0];
101 1.1 pooka };
102 1.1 pooka
103 1.1 pooka /* syscall response */
104 1.1 pooka struct rsp_sysresp {
105 1.1 pooka int rsys_error;
106 1.1 pooka register_t rsys_retval[2];
107 1.1 pooka };
108 1.1 pooka
109 1.1 pooka
110 1.1 pooka struct spclient {
111 1.1 pooka int spc_fd;
112 1.1 pooka struct lwp *spc_lwp;
113 1.1 pooka
114 1.1 pooka /* incoming */
115 1.1 pooka struct rsp_hdr spc_hdr;
116 1.1 pooka uint8_t *spc_buf;
117 1.1 pooka size_t spc_off;
118 1.1 pooka
119 1.1 pooka #if 0
120 1.1 pooka /* outgoing */
121 1.1 pooka int spc_obusy;
122 1.1 pooka pthread_mutex_t spc_omtx;
123 1.1 pooka pthread_cond_t spc_cv;
124 1.1 pooka #endif
125 1.1 pooka };
126 1.1 pooka
127 1.1 pooka typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
128 1.1 pooka typedef int (*connecthook_fn)(int);
129 1.1 pooka
130 1.1 pooka static uint64_t nextreq;
131 1.1 pooka
132 1.1 pooka static int
133 1.1 pooka dosend(struct spclient *spc, const void *data, size_t dlen)
134 1.1 pooka {
135 1.1 pooka struct pollfd pfd;
136 1.1 pooka const uint8_t *sdata = data;
137 1.1 pooka ssize_t n;
138 1.1 pooka size_t sent;
139 1.1 pooka int fd = spc->spc_fd;
140 1.1 pooka
141 1.1 pooka pfd.fd = fd;
142 1.1 pooka pfd.events = POLLOUT;
143 1.1 pooka
144 1.1 pooka for (sent = 0, n = 0; sent < dlen; ) {
145 1.1 pooka if (n) {
146 1.1 pooka if (poll(&pfd, 1, INFTIM) == -1) {
147 1.1 pooka if (errno == EINTR)
148 1.1 pooka continue;
149 1.1 pooka return errno;
150 1.1 pooka }
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka n = write(fd, sdata + sent, dlen - sent);
154 1.1 pooka if (n == 0) {
155 1.1 pooka return EFAULT;
156 1.1 pooka }
157 1.1 pooka if (n == -1 && errno != EAGAIN) {
158 1.1 pooka return EFAULT;
159 1.1 pooka }
160 1.1 pooka sent += n;
161 1.1 pooka }
162 1.1 pooka
163 1.1 pooka return 0;
164 1.1 pooka }
165 1.1 pooka
166 1.1 pooka static int
167 1.1 pooka readframe(struct spclient *spc)
168 1.1 pooka {
169 1.1 pooka int fd = spc->spc_fd;
170 1.1 pooka size_t left;
171 1.1 pooka size_t framelen;
172 1.1 pooka ssize_t n;
173 1.1 pooka
174 1.1 pooka /* still reading header? */
175 1.1 pooka if (spc->spc_off < HDRSZ) {
176 1.1 pooka DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
177 1.1 pooka spc->spc_off));
178 1.1 pooka
179 1.1 pooka left = HDRSZ - spc->spc_off;
180 1.1 pooka /*LINTED: cast ok */
181 1.1 pooka n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
182 1.1 pooka if (n == 0) {
183 1.1 pooka return -1;
184 1.1 pooka }
185 1.1 pooka if (n == -1) {
186 1.1 pooka if (errno == EAGAIN)
187 1.1 pooka return 0;
188 1.1 pooka return -1;
189 1.1 pooka }
190 1.1 pooka
191 1.1 pooka spc->spc_off += n;
192 1.1 pooka if (spc->spc_off < HDRSZ)
193 1.1 pooka return -1;
194 1.1 pooka
195 1.1 pooka /*LINTED*/
196 1.1 pooka framelen = spc->spc_hdr.rsp_len;
197 1.1 pooka
198 1.1 pooka if (framelen < HDRSZ) {
199 1.1 pooka return -1;
200 1.1 pooka } else if (framelen == HDRSZ) {
201 1.1 pooka return 1;
202 1.1 pooka }
203 1.1 pooka
204 1.1 pooka spc->spc_buf = malloc(framelen - HDRSZ);
205 1.1 pooka if (spc->spc_buf == NULL) {
206 1.1 pooka return -1;
207 1.1 pooka }
208 1.1 pooka memset(spc->spc_buf, 0, framelen - HDRSZ);
209 1.1 pooka
210 1.1 pooka /* "fallthrough" */
211 1.1 pooka } else {
212 1.1 pooka /*LINTED*/
213 1.1 pooka framelen = spc->spc_hdr.rsp_len;
214 1.1 pooka }
215 1.1 pooka
216 1.1 pooka left = framelen - spc->spc_off;
217 1.1 pooka
218 1.1 pooka DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
219 1.1 pooka spc->spc_off, left));
220 1.1 pooka
221 1.1 pooka if (left == 0)
222 1.1 pooka return 1;
223 1.1 pooka n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
224 1.1 pooka if (n == 0) {
225 1.1 pooka return -1;
226 1.1 pooka }
227 1.1 pooka if (n == -1) {
228 1.1 pooka if (errno == EAGAIN)
229 1.1 pooka return 0;
230 1.1 pooka return -1;
231 1.1 pooka }
232 1.1 pooka spc->spc_off += n;
233 1.1 pooka left -= n;
234 1.1 pooka
235 1.1 pooka /* got everything? */
236 1.1 pooka if (left == 0)
237 1.1 pooka return 1;
238 1.1 pooka else
239 1.1 pooka return 0;
240 1.1 pooka }
241 1.1 pooka
242 1.1 pooka static int
243 1.1 pooka tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
244 1.1 pooka {
245 1.1 pooka struct sockaddr_in sin;
246 1.1 pooka char buf[64];
247 1.1 pooka const char *p;
248 1.1 pooka size_t l;
249 1.1 pooka int port;
250 1.1 pooka
251 1.1 pooka memset(&sin, 0, sizeof(sin));
252 1.1 pooka sin.sin_len = sizeof(sin);
253 1.1 pooka sin.sin_family = AF_INET;
254 1.1 pooka
255 1.1 pooka p = strchr(addr, ':');
256 1.1 pooka if (!p) {
257 1.1 pooka fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
258 1.1 pooka return EINVAL;
259 1.1 pooka }
260 1.1 pooka
261 1.1 pooka l = p - addr;
262 1.1 pooka if (l > sizeof(buf)-1) {
263 1.1 pooka fprintf(stderr, "rump_sp_tcp: address too long\n");
264 1.1 pooka return EINVAL;
265 1.1 pooka }
266 1.1 pooka strncpy(buf, addr, l);
267 1.1 pooka buf[l] = '\0';
268 1.1 pooka
269 1.1 pooka /* special INADDR_ANY treatment */
270 1.1 pooka if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
271 1.1 pooka sin.sin_addr.s_addr = INADDR_ANY;
272 1.1 pooka } else {
273 1.1 pooka switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
274 1.1 pooka case 1:
275 1.1 pooka break;
276 1.1 pooka case 0:
277 1.1 pooka fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
278 1.1 pooka return EINVAL;
279 1.1 pooka case -1:
280 1.1 pooka fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
281 1.1 pooka return errno;
282 1.1 pooka default:
283 1.1 pooka assert(/*CONSTCOND*/0);
284 1.1 pooka return EINVAL;
285 1.1 pooka }
286 1.1 pooka }
287 1.1 pooka
288 1.1 pooka if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
289 1.1 pooka fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
290 1.1 pooka return EINVAL;
291 1.1 pooka }
292 1.1 pooka
293 1.1 pooka /* advance to port number & parse */
294 1.1 pooka p++;
295 1.1 pooka l = strspn(p, "0123456789");
296 1.1 pooka if (l == 0) {
297 1.1 pooka fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
298 1.1 pooka return EINVAL;
299 1.1 pooka }
300 1.1 pooka strncpy(buf, p, l);
301 1.1 pooka buf[l] = '\0';
302 1.1 pooka
303 1.1 pooka if (*(p+l) != '/' && *(p+l) != '\0') {
304 1.1 pooka fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
305 1.1 pooka return EINVAL;
306 1.1 pooka }
307 1.1 pooka
308 1.1 pooka port = atoi(buf);
309 1.1 pooka if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
310 1.1 pooka fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
311 1.1 pooka return ERANGE;
312 1.1 pooka }
313 1.1 pooka sin.sin_port = htons(port);
314 1.1 pooka
315 1.1 pooka *sa = malloc(sizeof(sin));
316 1.1 pooka if (*sa == NULL)
317 1.1 pooka return errno;
318 1.1 pooka memcpy(*sa, &sin, sizeof(sin));
319 1.1 pooka return 0;
320 1.1 pooka }
321 1.1 pooka
322 1.1 pooka static int
323 1.1 pooka tcp_connecthook(int s)
324 1.1 pooka {
325 1.1 pooka int x;
326 1.1 pooka
327 1.1 pooka x = 1;
328 1.1 pooka setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
329 1.1 pooka
330 1.1 pooka return 0;
331 1.1 pooka }
332 1.1 pooka
333 1.2 pooka static int
334 1.2 pooka unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
335 1.2 pooka {
336 1.2 pooka struct sockaddr_un sun;
337 1.2 pooka
338 1.2 pooka if (strlen(addr) > sizeof(sun.sun_path))
339 1.2 pooka return ENAMETOOLONG;
340 1.2 pooka
341 1.2 pooka /*
342 1.2 pooka * The pathname can be all kinds of spaghetti elementals,
343 1.2 pooka * so meek and obidient we accept everything.
344 1.2 pooka */
345 1.2 pooka memset(&sun, 0, sizeof(sun));
346 1.2 pooka sun.sun_family = AF_LOCAL;
347 1.2 pooka strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
348 1.2 pooka sun.sun_len = SUN_LEN(&sun);
349 1.2 pooka
350 1.2 pooka *sa = malloc(sun.sun_len);
351 1.2 pooka if (*sa == NULL)
352 1.2 pooka return errno;
353 1.2 pooka memcpy(*sa, &sun, sun.sun_len);
354 1.2 pooka
355 1.2 pooka return 0;
356 1.2 pooka }
357 1.2 pooka
358 1.1 pooka /*ARGSUSED*/
359 1.1 pooka static int
360 1.1 pooka notsupp(void)
361 1.1 pooka {
362 1.1 pooka
363 1.1 pooka fprintf(stderr, "rump_sp: support not yet implemented\n");
364 1.1 pooka return EOPNOTSUPP;
365 1.1 pooka }
366 1.1 pooka
367 1.1 pooka static int
368 1.1 pooka success(void)
369 1.1 pooka {
370 1.1 pooka
371 1.1 pooka return 0;
372 1.1 pooka }
373 1.1 pooka
374 1.1 pooka struct {
375 1.1 pooka const char *id;
376 1.1 pooka int domain;
377 1.1 pooka addrparse_fn ap;
378 1.1 pooka connecthook_fn connhook;
379 1.1 pooka } parsetab[] = {
380 1.1 pooka { "tcp", PF_INET, tcp_parse, tcp_connecthook },
381 1.2 pooka { "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
382 1.1 pooka { "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
383 1.1 pooka };
384 1.1 pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
385 1.1 pooka
386 1.1 pooka static int
387 1.1 pooka parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
388 1.1 pooka int allow_wildcard)
389 1.1 pooka {
390 1.1 pooka char id[16];
391 1.1 pooka const char *p, *p2;
392 1.1 pooka size_t l;
393 1.1 pooka unsigned i;
394 1.1 pooka int error;
395 1.1 pooka
396 1.1 pooka /*
397 1.1 pooka * Parse the url
398 1.1 pooka */
399 1.1 pooka
400 1.1 pooka p = url;
401 1.1 pooka p2 = strstr(p, "://");
402 1.1 pooka if (!p2) {
403 1.1 pooka fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
404 1.1 pooka return EINVAL;
405 1.1 pooka }
406 1.1 pooka l = p2-p;
407 1.1 pooka if (l > sizeof(id)-1) {
408 1.1 pooka fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
409 1.1 pooka return EINVAL;
410 1.1 pooka }
411 1.1 pooka
412 1.1 pooka strncpy(id, p, l);
413 1.1 pooka id[l] = '\0';
414 1.1 pooka p2 += 3; /* beginning of address */
415 1.1 pooka
416 1.1 pooka for (i = 0; i < NPARSE; i++) {
417 1.1 pooka if (strcmp(id, parsetab[i].id) == 0) {
418 1.1 pooka error = parsetab[i].ap(p2, sap, allow_wildcard);
419 1.1 pooka if (error)
420 1.1 pooka return error;
421 1.1 pooka break;
422 1.1 pooka }
423 1.1 pooka }
424 1.1 pooka if (i == NPARSE) {
425 1.1 pooka fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
426 1.1 pooka return EINVAL;
427 1.1 pooka }
428 1.1 pooka
429 1.1 pooka *idxp = i;
430 1.1 pooka return 0;
431 1.1 pooka }
432