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