faithd.c revision 1.33 1 1.33 christos /* $NetBSD: faithd.c,v 1.33 2010/11/26 18:58:43 christos Exp $ */
2 1.30 itojun /* $KAME: faithd.c,v 1.62 2003/08/19 21:20:33 itojun Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1997 and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.10 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.10 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun /*
34 1.1 itojun * User level translator from IPv6 to IPv4.
35 1.1 itojun *
36 1.1 itojun * Usage: faithd [<port> <progpath> <arg1(progname)> <arg2> ...]
37 1.1 itojun * e.g. faithd telnet /usr/local/v6/sbin/telnetd telnetd
38 1.1 itojun */
39 1.1 itojun
40 1.32 perry #include <sys/cdefs.h>
41 1.1 itojun #include <sys/param.h>
42 1.1 itojun #include <sys/types.h>
43 1.1 itojun #include <sys/sysctl.h>
44 1.1 itojun #include <sys/socket.h>
45 1.1 itojun #include <sys/wait.h>
46 1.1 itojun #include <sys/stat.h>
47 1.1 itojun #include <sys/time.h>
48 1.1 itojun #include <sys/ioctl.h>
49 1.1 itojun
50 1.30 itojun #include <poll.h>
51 1.1 itojun #include <stdio.h>
52 1.1 itojun #include <stdlib.h>
53 1.1 itojun #include <stdarg.h>
54 1.1 itojun #include <string.h>
55 1.1 itojun #include <syslog.h>
56 1.1 itojun #include <unistd.h>
57 1.1 itojun #include <errno.h>
58 1.1 itojun #include <signal.h>
59 1.1 itojun #include <fcntl.h>
60 1.1 itojun #include <termios.h>
61 1.1 itojun
62 1.1 itojun #include <net/if_types.h>
63 1.1 itojun #ifdef IFT_FAITH
64 1.1 itojun # define USE_ROUTE
65 1.1 itojun # include <net/if.h>
66 1.1 itojun # include <net/route.h>
67 1.1 itojun # include <net/if_dl.h>
68 1.1 itojun #endif
69 1.1 itojun
70 1.1 itojun #include <netinet/in.h>
71 1.1 itojun #include <arpa/inet.h>
72 1.1 itojun #include <netdb.h>
73 1.9 itojun #include <ifaddrs.h>
74 1.1 itojun
75 1.1 itojun #include "faithd.h"
76 1.17 itojun #include "prefix.h"
77 1.1 itojun
78 1.1 itojun char *serverpath = NULL;
79 1.1 itojun char *serverarg[MAXARGV + 1];
80 1.1 itojun char logname[BUFSIZ];
81 1.7 itojun char procname[BUFSIZ];
82 1.1 itojun struct myaddrs {
83 1.1 itojun struct myaddrs *next;
84 1.1 itojun struct sockaddr *addr;
85 1.1 itojun };
86 1.1 itojun struct myaddrs *myaddrs = NULL;
87 1.20 itojun static const char *service;
88 1.1 itojun #ifdef USE_ROUTE
89 1.1 itojun static int sockfd = 0;
90 1.1 itojun #endif
91 1.1 itojun int dflag = 0;
92 1.1 itojun static int pflag = 0;
93 1.12 itojun static int inetd = 0;
94 1.17 itojun static char *configfile = NULL;
95 1.1 itojun
96 1.33 christos static int inetd_main(int, char **);
97 1.33 christos static int daemon_main(int, char **);
98 1.33 christos static void play_service(int);
99 1.33 christos static void play_child(int, struct sockaddr *);
100 1.33 christos static int faith_prefix(struct sockaddr *);
101 1.33 christos static int map6to4(struct sockaddr_in6 *, struct sockaddr_in *);
102 1.33 christos static void sig_child(int);
103 1.33 christos static void sig_terminate(int);
104 1.33 christos static void start_daemon(void);
105 1.33 christos static void exit_stderr(const char *, ...)
106 1.15 itojun __attribute__((__format__(__printf__, 1, 2)));
107 1.33 christos static void grab_myaddrs(void);
108 1.33 christos static void free_myaddrs(void);
109 1.33 christos static void update_myaddrs(void);
110 1.33 christos static void usage(void) __attribute__((__noreturn__));
111 1.1 itojun
112 1.1 itojun int
113 1.12 itojun main(int argc, char **argv)
114 1.1 itojun {
115 1.1 itojun
116 1.1 itojun /*
117 1.1 itojun * Initializing stuff
118 1.1 itojun */
119 1.1 itojun
120 1.33 christos setprogname(argv[0]);
121 1.1 itojun
122 1.33 christos if (strcmp(getprogname(), "faithd") != 0) {
123 1.12 itojun inetd = 1;
124 1.12 itojun return inetd_main(argc, argv);
125 1.12 itojun } else
126 1.12 itojun return daemon_main(argc, argv);
127 1.12 itojun }
128 1.12 itojun
129 1.12 itojun static int
130 1.12 itojun inetd_main(int argc, char **argv)
131 1.12 itojun {
132 1.12 itojun char path[MAXPATHLEN];
133 1.12 itojun struct sockaddr_storage me;
134 1.12 itojun struct sockaddr_storage from;
135 1.28 itojun socklen_t melen, fromlen;
136 1.12 itojun int i;
137 1.12 itojun int error;
138 1.12 itojun const int on = 1;
139 1.12 itojun char sbuf[NI_MAXSERV], snum[NI_MAXSERV];
140 1.12 itojun
141 1.17 itojun if (config_load(configfile) < 0 && configfile) {
142 1.17 itojun exit_failure("could not load config file");
143 1.17 itojun /*NOTREACHED*/
144 1.17 itojun }
145 1.17 itojun
146 1.12 itojun if (strrchr(argv[0], '/') == NULL)
147 1.33 christos (void)snprintf(path, sizeof(path), "%s/%s", DEFAULT_DIR,
148 1.33 christos argv[0]);
149 1.12 itojun else
150 1.33 christos (void)snprintf(path, sizeof(path), "%s", argv[0]);
151 1.12 itojun
152 1.12 itojun #ifdef USE_ROUTE
153 1.12 itojun grab_myaddrs();
154 1.12 itojun
155 1.12 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
156 1.12 itojun if (sockfd < 0) {
157 1.20 itojun exit_failure("socket(PF_ROUTE): %s", strerror(errno));
158 1.12 itojun /*NOTREACHED*/
159 1.12 itojun }
160 1.12 itojun #endif
161 1.12 itojun
162 1.12 itojun melen = sizeof(me);
163 1.33 christos if (getsockname(STDIN_FILENO, (void *)&me, &melen) == -1) {
164 1.20 itojun exit_failure("getsockname: %s", strerror(errno));
165 1.17 itojun /*NOTREACHED*/
166 1.17 itojun }
167 1.12 itojun fromlen = sizeof(from);
168 1.33 christos if (getpeername(STDIN_FILENO, (void *)&from, &fromlen) == -1) {
169 1.20 itojun exit_failure("getpeername: %s", strerror(errno));
170 1.17 itojun /*NOTREACHED*/
171 1.17 itojun }
172 1.33 christos if (getnameinfo((void *)&me, melen, NULL, 0,
173 1.33 christos sbuf, (socklen_t)sizeof(sbuf), NI_NUMERICHOST) == 0)
174 1.12 itojun service = sbuf;
175 1.12 itojun else
176 1.12 itojun service = DEFAULT_PORT_NAME;
177 1.33 christos if (getnameinfo((void *)&me, melen, NULL, 0,
178 1.33 christos snum, (socklen_t)sizeof(snum), NI_NUMERICHOST) != 0)
179 1.33 christos (void)snprintf(snum, sizeof(snum), "?");
180 1.12 itojun
181 1.33 christos (void)snprintf(logname, sizeof(logname), "faithd %s", snum);
182 1.33 christos (void)snprintf(procname, sizeof(procname), "accepting port %s", snum);
183 1.12 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
184 1.12 itojun
185 1.17 itojun if (argc >= MAXARGV) {
186 1.14 itojun exit_failure("too many arguments");
187 1.17 itojun /*NOTREACHED*/
188 1.17 itojun }
189 1.12 itojun serverarg[0] = serverpath = path;
190 1.12 itojun for (i = 1; i < argc; i++)
191 1.12 itojun serverarg[i] = argv[i];
192 1.12 itojun serverarg[i] = NULL;
193 1.12 itojun
194 1.12 itojun error = setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &on,
195 1.33 christos (socklen_t)sizeof(on));
196 1.17 itojun if (error < 0) {
197 1.20 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
198 1.17 itojun /*NOTREACHED*/
199 1.17 itojun }
200 1.12 itojun
201 1.33 christos play_child(STDIN_FILENO, (void *)&from);
202 1.12 itojun exit_failure("should not reach here");
203 1.12 itojun return 0; /*dummy!*/
204 1.12 itojun }
205 1.12 itojun
206 1.12 itojun static int
207 1.12 itojun daemon_main(int argc, char **argv)
208 1.12 itojun {
209 1.12 itojun struct addrinfo hints, *res;
210 1.12 itojun int s_wld, error, i, serverargc, on = 1;
211 1.12 itojun int family = AF_INET6;
212 1.12 itojun int c;
213 1.12 itojun
214 1.26 itojun while ((c = getopt(argc, argv, "df:p")) != -1) {
215 1.1 itojun switch (c) {
216 1.1 itojun case 'd':
217 1.1 itojun dflag++;
218 1.1 itojun break;
219 1.17 itojun case 'f':
220 1.17 itojun configfile = optarg;
221 1.17 itojun break;
222 1.1 itojun case 'p':
223 1.1 itojun pflag++;
224 1.1 itojun break;
225 1.1 itojun default:
226 1.1 itojun usage();
227 1.14 itojun /*NOTREACHED*/
228 1.1 itojun }
229 1.1 itojun }
230 1.1 itojun argc -= optind;
231 1.1 itojun argv += optind;
232 1.1 itojun
233 1.17 itojun if (config_load(configfile) < 0 && configfile) {
234 1.17 itojun exit_failure("could not load config file");
235 1.17 itojun /*NOTREACHED*/
236 1.17 itojun }
237 1.17 itojun
238 1.1 itojun
239 1.1 itojun #ifdef USE_ROUTE
240 1.1 itojun grab_myaddrs();
241 1.1 itojun #endif
242 1.1 itojun
243 1.1 itojun switch (argc) {
244 1.1 itojun case 0:
245 1.14 itojun usage();
246 1.14 itojun /*NOTREACHED*/
247 1.1 itojun default:
248 1.1 itojun serverargc = argc - NUMARG;
249 1.12 itojun if (serverargc >= MAXARGV)
250 1.14 itojun exit_stderr("too many arguments");
251 1.1 itojun
252 1.29 itojun serverpath = strdup(argv[NUMPRG]);
253 1.21 itojun if (!serverpath)
254 1.21 itojun exit_stderr("not enough core");
255 1.1 itojun for (i = 0; i < serverargc; i++) {
256 1.29 itojun serverarg[i] = strdup(argv[i + NUMARG]);
257 1.21 itojun if (!serverarg[i])
258 1.21 itojun exit_stderr("not enough core");
259 1.1 itojun }
260 1.1 itojun serverarg[i] = NULL;
261 1.33 christos /*FALLTHROUGH*/
262 1.1 itojun case 1: /* no local service */
263 1.1 itojun service = argv[NUMPRT];
264 1.1 itojun break;
265 1.1 itojun }
266 1.1 itojun
267 1.23 itojun start_daemon();
268 1.23 itojun
269 1.1 itojun /*
270 1.1 itojun * Opening wild card socket for this service.
271 1.1 itojun */
272 1.1 itojun
273 1.1 itojun memset(&hints, 0, sizeof(hints));
274 1.1 itojun hints.ai_flags = AI_PASSIVE;
275 1.1 itojun hints.ai_family = family;
276 1.1 itojun hints.ai_socktype = SOCK_STREAM;
277 1.29 itojun hints.ai_protocol = IPPROTO_TCP; /* SCTP? */
278 1.1 itojun error = getaddrinfo(NULL, service, &hints, &res);
279 1.10 itojun if (error)
280 1.23 itojun exit_failure("getaddrinfo: %s", gai_strerror(error));
281 1.1 itojun
282 1.1 itojun s_wld = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
283 1.1 itojun if (s_wld == -1)
284 1.23 itojun exit_failure("socket: %s", strerror(errno));
285 1.1 itojun
286 1.1 itojun #ifdef IPV6_FAITH
287 1.1 itojun if (res->ai_family == AF_INET6) {
288 1.33 christos error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_FAITH, &on,
289 1.33 christos (socklen_t)sizeof(on));
290 1.1 itojun if (error == -1)
291 1.23 itojun exit_failure("setsockopt(IPV6_FAITH): %s",
292 1.20 itojun strerror(errno));
293 1.1 itojun }
294 1.1 itojun #endif
295 1.1 itojun
296 1.33 christos error = setsockopt(s_wld, SOL_SOCKET, SO_REUSEADDR, &on,
297 1.33 christos (socklen_t)sizeof(on));
298 1.1 itojun if (error == -1)
299 1.23 itojun exit_failure("setsockopt(SO_REUSEADDR): %s", strerror(errno));
300 1.1 itojun
301 1.33 christos error = setsockopt(s_wld, SOL_SOCKET, SO_OOBINLINE, &on,
302 1.33 christos (socklen_t)sizeof(on));
303 1.1 itojun if (error == -1)
304 1.23 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
305 1.1 itojun
306 1.27 itojun #ifdef IPV6_V6ONLY
307 1.33 christos error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_V6ONLY, &on,
308 1.33 christos (socklen_t)sizeof(on));
309 1.27 itojun if (error == -1)
310 1.27 itojun exit_failure("setsockopt(IPV6_V6ONLY): %s", strerror(errno));
311 1.27 itojun #endif
312 1.27 itojun
313 1.1 itojun error = bind(s_wld, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
314 1.1 itojun if (error == -1)
315 1.23 itojun exit_failure("bind: %s", strerror(errno));
316 1.1 itojun
317 1.1 itojun error = listen(s_wld, 5);
318 1.1 itojun if (error == -1)
319 1.23 itojun exit_failure("listen: %s", strerror(errno));
320 1.1 itojun
321 1.1 itojun #ifdef USE_ROUTE
322 1.1 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
323 1.1 itojun if (sockfd < 0) {
324 1.23 itojun exit_failure("socket(PF_ROUTE): %s", strerror(errno));
325 1.1 itojun /*NOTREACHED*/
326 1.1 itojun }
327 1.1 itojun #endif
328 1.1 itojun
329 1.1 itojun /*
330 1.1 itojun * Everything is OK.
331 1.1 itojun */
332 1.1 itojun
333 1.33 christos (void)snprintf(logname, sizeof(logname), "faithd %s", service);
334 1.33 christos (void)snprintf(procname, sizeof(procname), "accepting port %s", service);
335 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
336 1.17 itojun syslog(LOG_INFO, "Staring faith daemon for %s port", service);
337 1.1 itojun
338 1.1 itojun play_service(s_wld);
339 1.33 christos return 1;
340 1.1 itojun }
341 1.1 itojun
342 1.1 itojun static void
343 1.1 itojun play_service(int s_wld)
344 1.1 itojun {
345 1.1 itojun struct sockaddr_storage srcaddr;
346 1.28 itojun socklen_t len;
347 1.1 itojun int s_src;
348 1.1 itojun pid_t child_pid;
349 1.30 itojun struct pollfd pfd[2];
350 1.1 itojun int error;
351 1.1 itojun
352 1.1 itojun /*
353 1.1 itojun * Wait, accept, fork, faith....
354 1.1 itojun */
355 1.1 itojun again:
356 1.13 itojun setproctitle("%s", procname);
357 1.1 itojun
358 1.30 itojun pfd[0].fd = s_wld;
359 1.30 itojun pfd[0].events = POLLIN;
360 1.30 itojun pfd[1].fd = -1;
361 1.30 itojun pfd[1].revents = 0;
362 1.1 itojun #ifdef USE_ROUTE
363 1.1 itojun if (sockfd) {
364 1.30 itojun pfd[1].fd = sockfd;
365 1.30 itojun pfd[1].events = POLLIN;
366 1.1 itojun }
367 1.1 itojun #endif
368 1.1 itojun
369 1.33 christos error = poll(pfd, (unsigned int)(sizeof(pfd) / sizeof(pfd[0])), INFTIM);
370 1.1 itojun if (error < 0) {
371 1.1 itojun if (errno == EINTR)
372 1.1 itojun goto again;
373 1.20 itojun exit_failure("select: %s", strerror(errno));
374 1.1 itojun /*NOTREACHED*/
375 1.1 itojun }
376 1.1 itojun
377 1.1 itojun #ifdef USE_ROUTE
378 1.30 itojun if (pfd[1].revents & POLLIN)
379 1.30 itojun {
380 1.1 itojun update_myaddrs();
381 1.1 itojun }
382 1.1 itojun #endif
383 1.30 itojun if (pfd[0].revents & POLLIN)
384 1.30 itojun {
385 1.1 itojun len = sizeof(srcaddr);
386 1.33 christos s_src = accept(s_wld, (void *)&srcaddr, &len);
387 1.24 itojun if (s_src < 0) {
388 1.24 itojun if (errno == ECONNABORTED)
389 1.24 itojun goto again;
390 1.20 itojun exit_failure("socket: %s", strerror(errno));
391 1.17 itojun /*NOTREACHED*/
392 1.27 itojun }
393 1.27 itojun if (srcaddr.ss_family == AF_INET6 &&
394 1.33 christos IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)(void *)&srcaddr)->sin6_addr)) {
395 1.33 christos (void)close(s_src);
396 1.27 itojun syslog(LOG_ERR, "connection from IPv4 mapped address?");
397 1.27 itojun goto again;
398 1.17 itojun }
399 1.1 itojun
400 1.1 itojun child_pid = fork();
401 1.26 itojun
402 1.1 itojun if (child_pid == 0) {
403 1.1 itojun /* child process */
404 1.33 christos (void)close(s_wld);
405 1.1 itojun closelog();
406 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
407 1.33 christos play_child(s_src, (void *)&srcaddr);
408 1.1 itojun exit_failure("should never reach here");
409 1.17 itojun /*NOTREACHED*/
410 1.1 itojun } else {
411 1.1 itojun /* parent process */
412 1.33 christos (void)close(s_src);
413 1.1 itojun if (child_pid == -1)
414 1.1 itojun syslog(LOG_ERR, "can't fork");
415 1.1 itojun }
416 1.1 itojun }
417 1.1 itojun goto again;
418 1.1 itojun }
419 1.1 itojun
420 1.1 itojun static void
421 1.1 itojun play_child(int s_src, struct sockaddr *srcaddr)
422 1.1 itojun {
423 1.10 itojun struct sockaddr_storage dstaddr6;
424 1.1 itojun struct sockaddr_storage dstaddr4;
425 1.24 itojun char src[NI_MAXHOST];
426 1.24 itojun char dst6[NI_MAXHOST];
427 1.24 itojun char dst4[NI_MAXHOST];
428 1.28 itojun socklen_t len = sizeof(dstaddr6);
429 1.1 itojun int s_dst, error, hport, nresvport, on = 1;
430 1.1 itojun struct timeval tv;
431 1.2 itojun struct sockaddr *sa4;
432 1.17 itojun const struct config *conf;
433 1.1 itojun
434 1.1 itojun tv.tv_sec = 1;
435 1.1 itojun tv.tv_usec = 0;
436 1.1 itojun
437 1.33 christos (void)getnameinfo(srcaddr, (socklen_t)srcaddr->sa_len,
438 1.33 christos src, (socklen_t)sizeof(src), NULL, 0, NI_NUMERICHOST);
439 1.1 itojun syslog(LOG_INFO, "accepted a client from %s", src);
440 1.1 itojun
441 1.33 christos error = getsockname(s_src, (void *)&dstaddr6, &len);
442 1.17 itojun if (error == -1) {
443 1.20 itojun exit_failure("getsockname: %s", strerror(errno));
444 1.17 itojun /*NOTREACHED*/
445 1.17 itojun }
446 1.1 itojun
447 1.33 christos (void)getnameinfo((void *)&dstaddr6, len,
448 1.33 christos dst6, (socklen_t)sizeof(dst6), NULL, 0, NI_NUMERICHOST);
449 1.1 itojun syslog(LOG_INFO, "the client is connecting to %s", dst6);
450 1.1 itojun
451 1.33 christos if (!faith_prefix((void *)&dstaddr6)) {
452 1.1 itojun if (serverpath) {
453 1.1 itojun /*
454 1.1 itojun * Local service
455 1.1 itojun */
456 1.1 itojun syslog(LOG_INFO, "executing local %s", serverpath);
457 1.12 itojun if (!inetd) {
458 1.33 christos (void)dup2(s_src, 0);
459 1.33 christos (void)close(s_src);
460 1.33 christos (void)dup2(0, 1);
461 1.33 christos (void)dup2(0, 2);
462 1.12 itojun }
463 1.33 christos (void)execv(serverpath, serverarg);
464 1.20 itojun syslog(LOG_ERR, "execv %s: %s", serverpath,
465 1.20 itojun strerror(errno));
466 1.1 itojun _exit(EXIT_FAILURE);
467 1.1 itojun } else {
468 1.33 christos (void)close(s_src);
469 1.1 itojun exit_success("no local service for %s", service);
470 1.1 itojun }
471 1.1 itojun }
472 1.1 itojun
473 1.1 itojun /*
474 1.1 itojun * Act as a translator
475 1.1 itojun */
476 1.1 itojun
477 1.33 christos switch (((struct sockaddr *)(void *)&dstaddr6)->sa_family) {
478 1.2 itojun case AF_INET6:
479 1.33 christos if (!map6to4((struct sockaddr_in6 *)(void *)&dstaddr6,
480 1.33 christos (struct sockaddr_in *)(void *)&dstaddr4)) {
481 1.33 christos (void)close(s_src);
482 1.14 itojun exit_failure("map6to4 failed");
483 1.17 itojun /*NOTREACHED*/
484 1.1 itojun }
485 1.1 itojun syslog(LOG_INFO, "translating from v6 to v4");
486 1.2 itojun break;
487 1.2 itojun default:
488 1.33 christos (void)close(s_src);
489 1.14 itojun exit_failure("family not supported");
490 1.2 itojun /*NOTREACHED*/
491 1.1 itojun }
492 1.1 itojun
493 1.33 christos sa4 = (void *)&dstaddr4;
494 1.33 christos (void)getnameinfo(sa4, (socklen_t)sa4->sa_len,
495 1.33 christos dst4, (socklen_t)sizeof(dst4), NULL, 0, NI_NUMERICHOST);
496 1.17 itojun
497 1.17 itojun conf = config_match(srcaddr, sa4);
498 1.17 itojun if (!conf || !conf->permit) {
499 1.33 christos (void)close(s_src);
500 1.19 itojun if (conf) {
501 1.19 itojun exit_failure("translation to %s not permitted for %s",
502 1.19 itojun dst4, prefix_string(&conf->match));
503 1.19 itojun /*NOTREACHED*/
504 1.19 itojun } else {
505 1.19 itojun exit_failure("translation to %s not permitted", dst4);
506 1.19 itojun /*NOTREACHED*/
507 1.19 itojun }
508 1.17 itojun }
509 1.17 itojun
510 1.1 itojun syslog(LOG_INFO, "the translator is connecting to %s", dst4);
511 1.1 itojun
512 1.1 itojun setproctitle("port %s, %s -> %s", service, src, dst4);
513 1.1 itojun
514 1.2 itojun if (sa4->sa_family == AF_INET6)
515 1.33 christos hport = ntohs(((struct sockaddr_in6 *)(void *)&dstaddr4)->sin6_port);
516 1.1 itojun else /* AF_INET */
517 1.33 christos hport = ntohs(((struct sockaddr_in *)(void *)&dstaddr4)->sin_port);
518 1.1 itojun
519 1.25 itojun if (pflag)
520 1.2 itojun s_dst = rresvport_af(&nresvport, sa4->sa_family);
521 1.25 itojun else
522 1.25 itojun s_dst = socket(sa4->sa_family, SOCK_STREAM, 0);
523 1.17 itojun if (s_dst < 0) {
524 1.20 itojun exit_failure("socket: %s", strerror(errno));
525 1.17 itojun /*NOTREACHED*/
526 1.17 itojun }
527 1.17 itojun
528 1.17 itojun if (conf->src.a.ss_family) {
529 1.33 christos if (bind(s_dst, (const void*)&conf->src.a,
530 1.33 christos (socklen_t)conf->src.a.ss_len) < 0) {
531 1.20 itojun exit_failure("bind: %s", strerror(errno));
532 1.17 itojun /*NOTREACHED*/
533 1.17 itojun }
534 1.17 itojun }
535 1.1 itojun
536 1.33 christos error = setsockopt(s_dst, SOL_SOCKET, SO_OOBINLINE, &on,
537 1.33 christos (socklen_t)sizeof(on));
538 1.17 itojun if (error < 0) {
539 1.20 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
540 1.17 itojun /*NOTREACHED*/
541 1.17 itojun }
542 1.1 itojun
543 1.33 christos error = setsockopt(s_src, SOL_SOCKET, SO_SNDTIMEO, &tv,
544 1.33 christos (socklen_t)sizeof(tv));
545 1.17 itojun if (error < 0) {
546 1.20 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", strerror(errno));
547 1.17 itojun /*NOTREACHED*/
548 1.17 itojun }
549 1.33 christos error = setsockopt(s_dst, SOL_SOCKET, SO_SNDTIMEO, &tv,
550 1.33 christos (socklen_t)sizeof(tv));
551 1.17 itojun if (error < 0) {
552 1.20 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", strerror(errno));
553 1.17 itojun /*NOTREACHED*/
554 1.17 itojun }
555 1.1 itojun
556 1.33 christos error = connect(s_dst, sa4, (socklen_t)sa4->sa_len);
557 1.17 itojun if (error < 0) {
558 1.20 itojun exit_failure("connect: %s", strerror(errno));
559 1.17 itojun /*NOTREACHED*/
560 1.17 itojun }
561 1.1 itojun
562 1.1 itojun switch (hport) {
563 1.1 itojun case FTP_PORT:
564 1.1 itojun ftp_relay(s_src, s_dst);
565 1.1 itojun break;
566 1.1 itojun default:
567 1.1 itojun tcp_relay(s_src, s_dst, service);
568 1.1 itojun break;
569 1.1 itojun }
570 1.1 itojun
571 1.1 itojun /* NOTREACHED */
572 1.1 itojun }
573 1.1 itojun
574 1.1 itojun /* 0: non faith, 1: faith */
575 1.1 itojun static int
576 1.1 itojun faith_prefix(struct sockaddr *dst)
577 1.1 itojun {
578 1.1 itojun #ifndef USE_ROUTE
579 1.1 itojun int mib[4], size;
580 1.1 itojun struct in6_addr faith_prefix;
581 1.1 itojun struct sockaddr_in6 *dst6 = (struct sockaddr_in *)dst;
582 1.1 itojun
583 1.1 itojun if (dst->sa_family != AF_INET6)
584 1.1 itojun return 0;
585 1.1 itojun
586 1.1 itojun mib[0] = CTL_NET;
587 1.1 itojun mib[1] = PF_INET6;
588 1.1 itojun mib[2] = IPPROTO_IPV6;
589 1.1 itojun mib[3] = IPV6CTL_FAITH_PREFIX;
590 1.1 itojun size = sizeof(struct in6_addr);
591 1.17 itojun if (sysctl(mib, 4, &faith_prefix, &size, NULL, 0) < 0) {
592 1.20 itojun exit_failure("sysctl: %s", strerror(errno));
593 1.17 itojun /*NOTREACHED*/
594 1.17 itojun }
595 1.1 itojun
596 1.33 christos return memcmp(dst, &faith_prefix,
597 1.33 christos sizeof(struct in6_addr) - sizeof(struct in_addr)) == 0;
598 1.1 itojun #else
599 1.1 itojun struct myaddrs *p;
600 1.1 itojun struct sockaddr_in6 *sin6;
601 1.1 itojun struct sockaddr_in *sin4;
602 1.3 itojun struct sockaddr_in6 *dst6;
603 1.3 itojun struct sockaddr_in *dst4;
604 1.3 itojun struct sockaddr_in dstmap;
605 1.3 itojun
606 1.33 christos dst6 = (void *)dst;
607 1.3 itojun if (dst->sa_family == AF_INET6
608 1.3 itojun && IN6_IS_ADDR_V4MAPPED(&dst6->sin6_addr)) {
609 1.3 itojun /* ugly... */
610 1.3 itojun memset(&dstmap, 0, sizeof(dstmap));
611 1.3 itojun dstmap.sin_family = AF_INET;
612 1.3 itojun dstmap.sin_len = sizeof(dstmap);
613 1.3 itojun memcpy(&dstmap.sin_addr, &dst6->sin6_addr.s6_addr[12],
614 1.3 itojun sizeof(dstmap.sin_addr));
615 1.33 christos dst = (void *)&dstmap;
616 1.3 itojun }
617 1.3 itojun
618 1.33 christos dst6 = (void *)dst;
619 1.33 christos dst4 = (void *)dst;
620 1.1 itojun
621 1.1 itojun for (p = myaddrs; p; p = p->next) {
622 1.33 christos sin6 = (void *)p->addr;
623 1.33 christos sin4 = (void *)p->addr;
624 1.1 itojun
625 1.3 itojun if (p->addr->sa_len != dst->sa_len
626 1.3 itojun || p->addr->sa_family != dst->sa_family)
627 1.3 itojun continue;
628 1.3 itojun
629 1.3 itojun switch (dst->sa_family) {
630 1.3 itojun case AF_INET6:
631 1.3 itojun if (sin6->sin6_scope_id == dst6->sin6_scope_id
632 1.5 itojun && IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &dst6->sin6_addr))
633 1.1 itojun return 0;
634 1.3 itojun break;
635 1.3 itojun case AF_INET:
636 1.3 itojun if (sin4->sin_addr.s_addr == dst4->sin_addr.s_addr)
637 1.6 itojun return 0;
638 1.3 itojun break;
639 1.1 itojun }
640 1.1 itojun }
641 1.1 itojun return 1;
642 1.1 itojun #endif
643 1.1 itojun }
644 1.1 itojun
645 1.1 itojun /* 0: non faith, 1: faith */
646 1.1 itojun static int
647 1.1 itojun map6to4(struct sockaddr_in6 *dst6, struct sockaddr_in *dst4)
648 1.1 itojun {
649 1.1 itojun memset(dst4, 0, sizeof(*dst4));
650 1.1 itojun dst4->sin_len = sizeof(*dst4);
651 1.1 itojun dst4->sin_family = AF_INET;
652 1.1 itojun dst4->sin_port = dst6->sin6_port;
653 1.2 itojun memcpy(&dst4->sin_addr, &dst6->sin6_addr.s6_addr[12],
654 1.2 itojun sizeof(dst4->sin_addr));
655 1.1 itojun
656 1.1 itojun if (dst4->sin_addr.s_addr == INADDR_ANY
657 1.1 itojun || dst4->sin_addr.s_addr == INADDR_BROADCAST
658 1.14 itojun || IN_MULTICAST(ntohl(dst4->sin_addr.s_addr)))
659 1.1 itojun return 0;
660 1.1 itojun
661 1.1 itojun return 1;
662 1.1 itojun }
663 1.1 itojun
664 1.1 itojun
665 1.1 itojun static void
666 1.33 christos /*ARGSUSED*/
667 1.1 itojun sig_child(int sig)
668 1.1 itojun {
669 1.1 itojun int status;
670 1.1 itojun pid_t pid;
671 1.1 itojun
672 1.21 itojun while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0)
673 1.21 itojun if (WEXITSTATUS(status))
674 1.26 itojun syslog(LOG_WARNING, "child %ld exit status 0x%x",
675 1.26 itojun (long)pid, status);
676 1.1 itojun }
677 1.1 itojun
678 1.1 itojun void
679 1.33 christos /*ARGSUSED*/
680 1.1 itojun sig_terminate(int sig)
681 1.1 itojun {
682 1.1 itojun syslog(LOG_INFO, "Terminating faith daemon");
683 1.1 itojun exit(EXIT_SUCCESS);
684 1.1 itojun }
685 1.1 itojun
686 1.1 itojun static void
687 1.1 itojun start_daemon(void)
688 1.1 itojun {
689 1.18 itojun #ifdef SA_NOCLDWAIT
690 1.18 itojun struct sigaction sa;
691 1.18 itojun #endif
692 1.18 itojun
693 1.1 itojun if (daemon(0, 0) == -1)
694 1.20 itojun exit_stderr("daemon: %s", strerror(errno));
695 1.1 itojun
696 1.18 itojun #ifdef SA_NOCLDWAIT
697 1.33 christos (void)memset(&sa, 0, sizeof(sa));
698 1.18 itojun sa.sa_handler = sig_child;
699 1.18 itojun sa.sa_flags = SA_NOCLDWAIT;
700 1.33 christos (void)sigemptyset(&sa.sa_mask);
701 1.33 christos (void)sigaction(SIGCHLD, &sa, (struct sigaction *)0);
702 1.18 itojun #else
703 1.17 itojun if (signal(SIGCHLD, sig_child) == SIG_ERR) {
704 1.20 itojun exit_failure("signal CHLD: %s", strerror(errno));
705 1.17 itojun /*NOTREACHED*/
706 1.17 itojun }
707 1.18 itojun #endif
708 1.1 itojun
709 1.17 itojun if (signal(SIGTERM, sig_terminate) == SIG_ERR) {
710 1.20 itojun exit_failure("signal TERM: %s", strerror(errno));
711 1.17 itojun /*NOTREACHED*/
712 1.17 itojun }
713 1.1 itojun }
714 1.1 itojun
715 1.14 itojun static void
716 1.14 itojun exit_stderr(const char *fmt, ...)
717 1.1 itojun {
718 1.1 itojun va_list ap;
719 1.1 itojun
720 1.1 itojun va_start(ap, fmt);
721 1.33 christos (void)fprintf(stderr, "%s: ", getprogname());
722 1.33 christos (void)vfprintf(stderr, fmt, ap);
723 1.1 itojun va_end(ap);
724 1.1 itojun exit(EXIT_FAILURE);
725 1.1 itojun }
726 1.1 itojun
727 1.1 itojun void
728 1.1 itojun exit_failure(const char *fmt, ...)
729 1.1 itojun {
730 1.1 itojun va_list ap;
731 1.1 itojun
732 1.1 itojun va_start(ap, fmt);
733 1.33 christos vsyslog(LOG_ERR, fmt, ap);
734 1.1 itojun va_end(ap);
735 1.1 itojun exit(EXIT_FAILURE);
736 1.1 itojun }
737 1.1 itojun
738 1.1 itojun void
739 1.1 itojun exit_success(const char *fmt, ...)
740 1.1 itojun {
741 1.1 itojun va_list ap;
742 1.1 itojun
743 1.1 itojun va_start(ap, fmt);
744 1.33 christos vsyslog(LOG_INFO, fmt, ap);
745 1.1 itojun va_end(ap);
746 1.1 itojun exit(EXIT_SUCCESS);
747 1.1 itojun }
748 1.1 itojun
749 1.1 itojun #ifdef USE_ROUTE
750 1.1 itojun static void
751 1.33 christos grab_myaddrs(void)
752 1.1 itojun {
753 1.9 itojun struct ifaddrs *ifap, *ifa;
754 1.9 itojun struct myaddrs *p;
755 1.9 itojun struct sockaddr_in6 *sin6;
756 1.9 itojun
757 1.9 itojun if (getifaddrs(&ifap) != 0) {
758 1.9 itojun exit_failure("getifaddrs");
759 1.9 itojun /*NOTREACHED*/
760 1.9 itojun }
761 1.9 itojun
762 1.9 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
763 1.9 itojun switch (ifa->ifa_addr->sa_family) {
764 1.9 itojun case AF_INET:
765 1.9 itojun case AF_INET6:
766 1.9 itojun break;
767 1.9 itojun default:
768 1.9 itojun continue;
769 1.9 itojun }
770 1.9 itojun
771 1.9 itojun p = (struct myaddrs *)malloc(sizeof(struct myaddrs) +
772 1.9 itojun ifa->ifa_addr->sa_len);
773 1.9 itojun if (!p) {
774 1.9 itojun exit_failure("not enough core");
775 1.9 itojun /*NOTREACHED*/
776 1.9 itojun }
777 1.9 itojun memcpy(p + 1, ifa->ifa_addr, ifa->ifa_addr->sa_len);
778 1.9 itojun p->next = myaddrs;
779 1.33 christos p->addr = (void *)(p + 1);
780 1.9 itojun #ifdef __KAME__
781 1.9 itojun if (ifa->ifa_addr->sa_family == AF_INET6) {
782 1.33 christos sin6 = (void *)p->addr;
783 1.9 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
784 1.9 itojun || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
785 1.9 itojun sin6->sin6_scope_id =
786 1.33 christos ntohs(*(uint16_t *)(void *)
787 1.33 christos &sin6->sin6_addr.s6_addr[3]);
788 1.9 itojun sin6->sin6_addr.s6_addr[2] = 0;
789 1.9 itojun sin6->sin6_addr.s6_addr[3] = 0;
790 1.9 itojun }
791 1.9 itojun }
792 1.9 itojun #endif
793 1.9 itojun myaddrs = p;
794 1.9 itojun if (dflag) {
795 1.9 itojun char hbuf[NI_MAXHOST];
796 1.33 christos (void)getnameinfo(p->addr, (socklen_t)p->addr->sa_len,
797 1.33 christos hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
798 1.33 christos NI_NUMERICHOST);
799 1.9 itojun syslog(LOG_INFO, "my interface: %s %s", hbuf,
800 1.9 itojun ifa->ifa_name);
801 1.9 itojun }
802 1.9 itojun }
803 1.9 itojun
804 1.9 itojun freeifaddrs(ifap);
805 1.1 itojun }
806 1.1 itojun
807 1.1 itojun static void
808 1.33 christos free_myaddrs(void)
809 1.1 itojun {
810 1.1 itojun struct myaddrs *p, *q;
811 1.1 itojun
812 1.1 itojun p = myaddrs;
813 1.1 itojun while (p) {
814 1.1 itojun q = p->next;
815 1.1 itojun free(p);
816 1.1 itojun p = q;
817 1.1 itojun }
818 1.1 itojun myaddrs = NULL;
819 1.1 itojun }
820 1.1 itojun
821 1.1 itojun static void
822 1.33 christos update_myaddrs(void)
823 1.1 itojun {
824 1.1 itojun char msg[BUFSIZ];
825 1.33 christos ssize_t len;
826 1.1 itojun struct rt_msghdr *rtm;
827 1.1 itojun
828 1.1 itojun len = read(sockfd, msg, sizeof(msg));
829 1.1 itojun if (len < 0) {
830 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) failed");
831 1.1 itojun return;
832 1.1 itojun }
833 1.33 christos rtm = (void *)msg;
834 1.1 itojun if (len < 4 || len < rtm->rtm_msglen) {
835 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) short read");
836 1.1 itojun return;
837 1.1 itojun }
838 1.1 itojun if (rtm->rtm_version != RTM_VERSION) {
839 1.1 itojun syslog(LOG_ERR, "routing socket version mismatch");
840 1.33 christos (void)close(sockfd);
841 1.1 itojun sockfd = 0;
842 1.1 itojun return;
843 1.1 itojun }
844 1.1 itojun switch (rtm->rtm_type) {
845 1.1 itojun case RTM_NEWADDR:
846 1.1 itojun case RTM_DELADDR:
847 1.1 itojun case RTM_IFINFO:
848 1.1 itojun break;
849 1.1 itojun default:
850 1.1 itojun return;
851 1.1 itojun }
852 1.1 itojun /* XXX more filters here? */
853 1.1 itojun
854 1.1 itojun syslog(LOG_INFO, "update interface address list");
855 1.1 itojun free_myaddrs();
856 1.1 itojun grab_myaddrs();
857 1.1 itojun }
858 1.1 itojun #endif /*USE_ROUTE*/
859 1.1 itojun
860 1.1 itojun static void
861 1.33 christos usage(void)
862 1.1 itojun {
863 1.33 christos (void)fprintf(stderr,
864 1.33 christos "Usage: %s [-dp] [-f conf] service [serverpath [serverargs]]\n",
865 1.33 christos getprogname());
866 1.1 itojun exit(0);
867 1.1 itojun }
868