Home | History | Annotate | Line # | Download | only in faithd
faithd.c revision 1.8
      1  1.8  itojun /*	$NetBSD: faithd.c,v 1.8 1999/12/30 15:39:40 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.1  itojun 
     39  1.1  itojun #include <sys/param.h>
     40  1.1  itojun #include <sys/types.h>
     41  1.1  itojun #include <sys/sysctl.h>
     42  1.1  itojun #include <sys/socket.h>
     43  1.1  itojun #include <sys/wait.h>
     44  1.1  itojun #include <sys/stat.h>
     45  1.1  itojun #include <sys/time.h>
     46  1.1  itojun #include <sys/ioctl.h>
     47  1.1  itojun #ifdef __FreeBSD__
     48  1.1  itojun #include <libutil.h>
     49  1.1  itojun #endif
     50  1.1  itojun 
     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.1  itojun 
     74  1.1  itojun #ifdef FAITH4
     75  1.1  itojun #include <resolv.h>
     76  1.1  itojun #include <arpa/nameser.h>
     77  1.1  itojun #ifndef FAITH_NS
     78  1.1  itojun #define FAITH_NS "FAITH_NS"
     79  1.1  itojun #endif
     80  1.1  itojun #endif
     81  1.1  itojun 
     82  1.1  itojun #include "faithd.h"
     83  1.1  itojun 
     84  1.1  itojun char *serverpath = NULL;
     85  1.1  itojun char *serverarg[MAXARGV + 1];
     86  1.1  itojun static char *faithdname = NULL;
     87  1.1  itojun char logname[BUFSIZ];
     88  1.7  itojun char procname[BUFSIZ];
     89  1.1  itojun struct myaddrs {
     90  1.1  itojun 	struct myaddrs *next;
     91  1.1  itojun 	struct sockaddr *addr;
     92  1.1  itojun };
     93  1.1  itojun struct myaddrs *myaddrs = NULL;
     94  1.1  itojun static char *service;
     95  1.1  itojun #ifdef USE_ROUTE
     96  1.1  itojun static int sockfd = 0;
     97  1.1  itojun #endif
     98  1.1  itojun int dflag = 0;
     99  1.1  itojun static int pflag = 0;
    100  1.1  itojun 
    101  1.1  itojun int main __P((int, char **));
    102  1.1  itojun static void play_service __P((int));
    103  1.1  itojun static void play_child __P((int, struct sockaddr *));
    104  1.1  itojun static int faith_prefix __P((struct sockaddr *));
    105  1.1  itojun static int map6to4 __P((struct sockaddr_in6 *, struct sockaddr_in *));
    106  1.1  itojun #ifdef FAITH4
    107  1.1  itojun static int map4to6 __P((struct sockaddr_in *, struct sockaddr_in6 *));
    108  1.1  itojun #endif
    109  1.1  itojun static void sig_child __P((int));
    110  1.1  itojun static void sig_terminate __P((int));
    111  1.1  itojun static void start_daemon __P((void));
    112  1.1  itojun static unsigned int if_maxindex __P((void));
    113  1.1  itojun static void grab_myaddrs __P((void));
    114  1.1  itojun static void free_myaddrs __P((void));
    115  1.1  itojun static void update_myaddrs __P((void));
    116  1.1  itojun static void usage __P((void));
    117  1.1  itojun 
    118  1.1  itojun int
    119  1.1  itojun main(int argc, char *argv[])
    120  1.1  itojun {
    121  1.1  itojun 	struct addrinfo hints, *res;
    122  1.1  itojun 	int s_wld, error, i, serverargc, on = 1;
    123  1.1  itojun 	int family = AF_INET6;
    124  1.1  itojun 	int c;
    125  1.1  itojun #ifdef FAITH_NS
    126  1.1  itojun 	char *ns;
    127  1.1  itojun #endif /* FAITH_NS */
    128  1.1  itojun 	extern int optind;
    129  1.1  itojun 	extern char *optarg;
    130  1.1  itojun 
    131  1.1  itojun 	/*
    132  1.1  itojun 	 * Initializing stuff
    133  1.1  itojun 	 */
    134  1.1  itojun 
    135  1.1  itojun 	faithdname = strrchr(argv[0], '/');
    136  1.1  itojun 	if (faithdname)
    137  1.1  itojun 		faithdname++;
    138  1.1  itojun 	else
    139  1.1  itojun 		faithdname = argv[0];
    140  1.1  itojun 
    141  1.1  itojun 	while ((c = getopt(argc, argv, "dp46")) != -1) {
    142  1.1  itojun 		switch (c) {
    143  1.1  itojun 		case 'd':
    144  1.1  itojun 			dflag++;
    145  1.1  itojun 			break;
    146  1.1  itojun 		case 'p':
    147  1.1  itojun 			pflag++;
    148  1.1  itojun 			break;
    149  1.1  itojun #ifdef FAITH4
    150  1.1  itojun 		case '4':
    151  1.1  itojun 			family = AF_INET;
    152  1.1  itojun 			break;
    153  1.1  itojun 		case '6':
    154  1.1  itojun 			family = AF_INET6;
    155  1.1  itojun 			break;
    156  1.1  itojun #endif
    157  1.1  itojun 		default:
    158  1.1  itojun 			usage();
    159  1.1  itojun 			break;
    160  1.1  itojun 		}
    161  1.1  itojun 	}
    162  1.1  itojun 	argc -= optind;
    163  1.1  itojun 	argv += optind;
    164  1.1  itojun 
    165  1.1  itojun #ifdef FAITH_NS
    166  1.1  itojun 	if ((ns = getenv(FAITH_NS)) != NULL) {
    167  1.1  itojun 		struct sockaddr_storage ss;
    168  1.1  itojun 		struct addrinfo hints, *res;
    169  1.1  itojun 		char serv[NI_MAXSERV];
    170  1.1  itojun 
    171  1.1  itojun 		memset(&ss, 0, sizeof(ss));
    172  1.1  itojun 		memset(&hints, 0, sizeof(hints));
    173  1.7  itojun 		snprintf(serv, sizeof(serv), "%u", NAMESERVER_PORT);
    174  1.1  itojun 		hints.ai_flags = AI_NUMERICHOST;
    175  1.1  itojun 		if (getaddrinfo(ns, serv, &hints, &res) ==  0) {
    176  1.1  itojun 			res_init();
    177  1.1  itojun 			memcpy(&_res_ext.nsaddr, res->ai_addr, res->ai_addrlen);
    178  1.1  itojun 			_res.nscount = 1;
    179  1.1  itojun 		}
    180  1.1  itojun 	}
    181  1.1  itojun #endif /* FAITH_NS */
    182  1.1  itojun 
    183  1.1  itojun #ifdef USE_ROUTE
    184  1.1  itojun 	grab_myaddrs();
    185  1.1  itojun #endif
    186  1.1  itojun 
    187  1.1  itojun 	switch (argc) {
    188  1.1  itojun 	case 0:
    189  1.1  itojun 		serverpath = DEFAULT_PATH;
    190  1.1  itojun 		serverarg[0] = DEFAULT_NAME;
    191  1.1  itojun 		serverarg[1] = NULL;
    192  1.1  itojun 		service = DEFAULT_PORT_NAME;
    193  1.1  itojun 		break;
    194  1.1  itojun 	default:
    195  1.1  itojun 		serverargc = argc - NUMARG;
    196  1.1  itojun 		if (serverargc > MAXARGV)
    197  1.1  itojun 			exit_error("too many augments");
    198  1.1  itojun 
    199  1.1  itojun 		serverpath = malloc(strlen(argv[NUMPRG]));
    200  1.1  itojun 		strcpy(serverpath, argv[NUMPRG]);
    201  1.1  itojun 		for (i = 0; i < serverargc; i++) {
    202  1.1  itojun 			serverarg[i] = malloc(strlen(argv[i + NUMARG]));
    203  1.1  itojun 			strcpy(serverarg[i], argv[i + NUMARG]);
    204  1.1  itojun 		}
    205  1.1  itojun 		serverarg[i] = NULL;
    206  1.1  itojun 		/* fall throuth */
    207  1.1  itojun 	case 1:	/* no local service */
    208  1.1  itojun 		service = argv[NUMPRT];
    209  1.1  itojun 		break;
    210  1.1  itojun 	}
    211  1.1  itojun 
    212  1.1  itojun 	/*
    213  1.1  itojun 	 * Opening wild card socket for this service.
    214  1.1  itojun 	 */
    215  1.1  itojun 
    216  1.1  itojun 	memset(&hints, 0, sizeof(hints));
    217  1.1  itojun 	hints.ai_flags = AI_PASSIVE;
    218  1.1  itojun 	hints.ai_family = family;
    219  1.1  itojun 	hints.ai_socktype = SOCK_STREAM;
    220  1.1  itojun 	hints.ai_protocol = 0;
    221  1.1  itojun 	error = getaddrinfo(NULL, service, &hints, &res);
    222  1.1  itojun 	if (error)
    223  1.3  itojun 		exit_error("getaddrinfo: %s", gai_strerror(error));
    224  1.1  itojun 
    225  1.1  itojun 	s_wld = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    226  1.1  itojun 	if (s_wld == -1)
    227  1.1  itojun 		exit_error("socket: %s", ERRSTR);
    228  1.1  itojun 
    229  1.1  itojun #ifdef IPV6_FAITH
    230  1.1  itojun 	if (res->ai_family == AF_INET6) {
    231  1.1  itojun 		error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_FAITH, &on, sizeof(on));
    232  1.1  itojun 		if (error == -1)
    233  1.4  itojun 			exit_error("setsockopt(IPV6_FAITH): %s", ERRSTR);
    234  1.1  itojun 	}
    235  1.1  itojun #endif
    236  1.1  itojun #ifdef FAITH4
    237  1.1  itojun #ifdef IP_FAITH
    238  1.1  itojun 	if (res->ai_family == AF_INET) {
    239  1.1  itojun 		error = setsockopt(s_wld, IPPROTO_IP, IP_FAITH, &on, sizeof(on));
    240  1.1  itojun 		if (error == -1)
    241  1.4  itojun 			exit_error("setsockopt(IP_FAITH): %s", ERRSTR);
    242  1.1  itojun 	}
    243  1.1  itojun #endif
    244  1.1  itojun #endif /* FAITH4 */
    245  1.1  itojun 
    246  1.1  itojun 	error = setsockopt(s_wld, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    247  1.1  itojun 	if (error == -1)
    248  1.4  itojun 		exit_error("setsockopt(SO_REUSEADDR): %s", ERRSTR);
    249  1.1  itojun 
    250  1.1  itojun 	error = setsockopt(s_wld, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
    251  1.1  itojun 	if (error == -1)
    252  1.4  itojun 		exit_error("setsockopt(SO_OOBINLINE): %s", ERRSTR);
    253  1.1  itojun 
    254  1.1  itojun 	error = bind(s_wld, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
    255  1.1  itojun 	if (error == -1)
    256  1.1  itojun 		exit_error("bind: %s", ERRSTR);
    257  1.1  itojun 
    258  1.1  itojun 	error = listen(s_wld, 5);
    259  1.1  itojun 	if (error == -1)
    260  1.1  itojun 		exit_error("listen: %s", ERRSTR);
    261  1.1  itojun 
    262  1.1  itojun #ifdef USE_ROUTE
    263  1.1  itojun 	sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
    264  1.1  itojun 	if (sockfd < 0) {
    265  1.1  itojun 		exit_error("socket(PF_ROUTE): %s", ERRSTR);
    266  1.1  itojun 		/*NOTREACHED*/
    267  1.1  itojun 	}
    268  1.1  itojun #endif
    269  1.1  itojun 
    270  1.1  itojun 	/*
    271  1.1  itojun 	 * Everything is OK.
    272  1.1  itojun 	 */
    273  1.1  itojun 
    274  1.1  itojun 	start_daemon();
    275  1.1  itojun 
    276  1.7  itojun 	snprintf(logname, sizeof(logname), "faithd %s", service);
    277  1.7  itojun 	snprintf(procname, sizeof(procname), "accepting port %s", service);
    278  1.1  itojun 	openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    279  1.1  itojun 	syslog(LOG_INFO, "Staring faith daemon for %s port", service);
    280  1.1  itojun 
    281  1.1  itojun 	play_service(s_wld);
    282  1.1  itojun 	/*NOTRECHED*/
    283  1.1  itojun 	exit(1);	/*pacify gcc*/
    284  1.1  itojun }
    285  1.1  itojun 
    286  1.1  itojun static void
    287  1.1  itojun play_service(int s_wld)
    288  1.1  itojun {
    289  1.1  itojun 	struct sockaddr_storage srcaddr;
    290  1.1  itojun 	int len;
    291  1.1  itojun 	int s_src;
    292  1.1  itojun 	pid_t child_pid;
    293  1.1  itojun 	fd_set rfds;
    294  1.1  itojun 	int error;
    295  1.1  itojun 	int maxfd;
    296  1.1  itojun 
    297  1.1  itojun 	/*
    298  1.1  itojun 	 * Wait, accept, fork, faith....
    299  1.1  itojun 	 */
    300  1.1  itojun again:
    301  1.7  itojun 	setproctitle(procname);
    302  1.1  itojun 
    303  1.1  itojun 	FD_ZERO(&rfds);
    304  1.1  itojun 	FD_SET(s_wld, &rfds);
    305  1.1  itojun 	maxfd = s_wld;
    306  1.1  itojun #ifdef USE_ROUTE
    307  1.1  itojun 	if (sockfd) {
    308  1.1  itojun 		FD_SET(sockfd, &rfds);
    309  1.1  itojun 		maxfd = (maxfd < sockfd) ? sockfd : maxfd;
    310  1.1  itojun 	}
    311  1.1  itojun #endif
    312  1.1  itojun 
    313  1.1  itojun 	error = select(maxfd + 1, &rfds, NULL, NULL, NULL);
    314  1.1  itojun 	if (error < 0) {
    315  1.1  itojun 		if (errno == EINTR)
    316  1.1  itojun 			goto again;
    317  1.1  itojun 		exit_failure("select: %s", ERRSTR);
    318  1.1  itojun 		/*NOTREACHED*/
    319  1.1  itojun 	}
    320  1.1  itojun 
    321  1.1  itojun #ifdef USE_ROUTE
    322  1.1  itojun 	if (FD_ISSET(sockfd, &rfds)) {
    323  1.1  itojun 		update_myaddrs();
    324  1.1  itojun 	}
    325  1.1  itojun #endif
    326  1.1  itojun 	if (FD_ISSET(s_wld, &rfds)) {
    327  1.1  itojun 		len = sizeof(srcaddr);
    328  1.1  itojun 		s_src = accept(s_wld, (struct sockaddr *)&srcaddr,
    329  1.1  itojun 			&len);
    330  1.1  itojun 		if (s_src == -1)
    331  1.1  itojun 			exit_failure("socket: %s", ERRSTR);
    332  1.1  itojun 
    333  1.1  itojun 		child_pid = fork();
    334  1.1  itojun 
    335  1.1  itojun 		if (child_pid == 0) {
    336  1.1  itojun 			/* child process */
    337  1.1  itojun 			close(s_wld);
    338  1.1  itojun 			closelog();
    339  1.1  itojun 			openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    340  1.1  itojun 			play_child(s_src, (struct sockaddr *)&srcaddr);
    341  1.1  itojun 			exit_failure("should never reach here");
    342  1.1  itojun 		} else {
    343  1.1  itojun 			/* parent process */
    344  1.1  itojun 			close(s_src);
    345  1.1  itojun 			if (child_pid == -1)
    346  1.1  itojun 				syslog(LOG_ERR, "can't fork");
    347  1.1  itojun 		}
    348  1.1  itojun 	}
    349  1.1  itojun 	goto again;
    350  1.1  itojun }
    351  1.1  itojun 
    352  1.1  itojun static void
    353  1.1  itojun play_child(int s_src, struct sockaddr *srcaddr)
    354  1.1  itojun {
    355  1.1  itojun 	struct sockaddr_storage dstaddr6;
    356  1.1  itojun 	struct sockaddr_storage dstaddr4;
    357  1.1  itojun 	char src[MAXHOSTNAMELEN];
    358  1.1  itojun 	char dst6[MAXHOSTNAMELEN];
    359  1.1  itojun 	char dst4[MAXHOSTNAMELEN];
    360  1.1  itojun 	int len = sizeof(dstaddr6);
    361  1.1  itojun 	int s_dst, error, hport, nresvport, on = 1;
    362  1.1  itojun 	struct timeval tv;
    363  1.2  itojun 	struct sockaddr *sa4;
    364  1.1  itojun 
    365  1.1  itojun 	tv.tv_sec = 1;
    366  1.1  itojun 	tv.tv_usec = 0;
    367  1.1  itojun 
    368  1.1  itojun 	getnameinfo(srcaddr, srcaddr->sa_len,
    369  1.1  itojun 		src, sizeof(src), NULL, 0, NI_NUMERICHOST);
    370  1.1  itojun 	syslog(LOG_INFO, "accepted a client from %s", src);
    371  1.1  itojun 
    372  1.1  itojun 	error = getsockname(s_src, (struct sockaddr *)&dstaddr6, &len);
    373  1.1  itojun 	if (error == -1)
    374  1.1  itojun 		exit_failure("getsockname: %s", ERRSTR);
    375  1.1  itojun 
    376  1.2  itojun 	getnameinfo((struct sockaddr *)&dstaddr6, len,
    377  1.1  itojun 		dst6, sizeof(dst6), NULL, 0, NI_NUMERICHOST);
    378  1.1  itojun 	syslog(LOG_INFO, "the client is connecting to %s", dst6);
    379  1.1  itojun 
    380  1.1  itojun 	if (!faith_prefix((struct sockaddr *)&dstaddr6)) {
    381  1.1  itojun 		if (serverpath) {
    382  1.1  itojun 			/*
    383  1.1  itojun 			 * Local service
    384  1.1  itojun 			 */
    385  1.1  itojun 			syslog(LOG_INFO, "executing local %s", serverpath);
    386  1.1  itojun 			dup2(s_src, 0);
    387  1.1  itojun 			close(s_src);
    388  1.1  itojun 			dup2(0, 1);
    389  1.1  itojun 			dup2(0, 2);
    390  1.1  itojun 			execv(serverpath, serverarg);
    391  1.1  itojun 			syslog(LOG_ERR, "execv %s: %s", serverpath, ERRSTR);
    392  1.1  itojun 			_exit(EXIT_FAILURE);
    393  1.1  itojun 		} else {
    394  1.1  itojun 			close(s_src);
    395  1.1  itojun 			exit_success("no local service for %s", service);
    396  1.1  itojun 		}
    397  1.1  itojun 	}
    398  1.1  itojun 
    399  1.1  itojun 	/*
    400  1.1  itojun 	 * Act as a translator
    401  1.1  itojun 	 */
    402  1.1  itojun 
    403  1.2  itojun 	switch (((struct sockaddr *)&dstaddr6)->sa_family) {
    404  1.2  itojun 	case AF_INET6:
    405  1.1  itojun 		if (!map6to4((struct sockaddr_in6 *)&dstaddr6,
    406  1.1  itojun 		    (struct sockaddr_in *)&dstaddr4)) {
    407  1.1  itojun 			close(s_src);
    408  1.1  itojun 			exit_error("map6to4 failed");
    409  1.1  itojun 		}
    410  1.1  itojun 		syslog(LOG_INFO, "translating from v6 to v4");
    411  1.2  itojun 		break;
    412  1.1  itojun #ifdef FAITH4
    413  1.2  itojun 	case AF_INET:
    414  1.1  itojun 		if (!map4to6((struct sockaddr_in *)&dstaddr6,
    415  1.1  itojun 		    (struct sockaddr_in6 *)&dstaddr4)) {
    416  1.1  itojun 			close(s_src);
    417  1.1  itojun 			exit_error("map4to6 failed");
    418  1.1  itojun 		}
    419  1.1  itojun 		syslog(LOG_INFO, "translating from v4 to v6");
    420  1.2  itojun 		break;
    421  1.1  itojun #endif
    422  1.2  itojun 	default:
    423  1.1  itojun 		close(s_src);
    424  1.1  itojun 		exit_error("family not supported");
    425  1.2  itojun 		/*NOTREACHED*/
    426  1.1  itojun 	}
    427  1.1  itojun 
    428  1.2  itojun 	sa4 = (struct sockaddr *)&dstaddr4;
    429  1.2  itojun 	getnameinfo(sa4, sa4->sa_len,
    430  1.1  itojun 		dst4, sizeof(dst4), NULL, 0, NI_NUMERICHOST);
    431  1.1  itojun 	syslog(LOG_INFO, "the translator is connecting to %s", dst4);
    432  1.1  itojun 
    433  1.1  itojun 	setproctitle("port %s, %s -> %s", service, src, dst4);
    434  1.1  itojun 
    435  1.2  itojun 	if (sa4->sa_family == AF_INET6)
    436  1.1  itojun 		hport = ntohs(((struct sockaddr_in6 *)&dstaddr4)->sin6_port);
    437  1.1  itojun 	else /* AF_INET */
    438  1.1  itojun 		hport = ntohs(((struct sockaddr_in *)&dstaddr4)->sin_port);
    439  1.1  itojun 
    440  1.1  itojun 	switch (hport) {
    441  1.1  itojun 	case RLOGIN_PORT:
    442  1.1  itojun 	case RSH_PORT:
    443  1.2  itojun 		s_dst = rresvport_af(&nresvport, sa4->sa_family);
    444  1.1  itojun 		break;
    445  1.1  itojun 	default:
    446  1.1  itojun 		if (pflag)
    447  1.2  itojun 			s_dst = rresvport_af(&nresvport, sa4->sa_family);
    448  1.1  itojun 		else
    449  1.2  itojun 			s_dst = socket(sa4->sa_family, SOCK_STREAM, 0);
    450  1.1  itojun 		break;
    451  1.1  itojun 	}
    452  1.1  itojun 	if (s_dst == -1)
    453  1.1  itojun 		exit_failure("socket: %s", ERRSTR);
    454  1.1  itojun 
    455  1.1  itojun 	error = setsockopt(s_dst, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
    456  1.1  itojun 	if (error == -1)
    457  1.4  itojun 		exit_error("setsockopt(SO_OOBINLINE): %s", ERRSTR);
    458  1.1  itojun 
    459  1.1  itojun 	error = setsockopt(s_src, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
    460  1.1  itojun 	if (error == -1)
    461  1.4  itojun 		exit_error("setsockopt(SO_SNDTIMEO): %s", ERRSTR);
    462  1.1  itojun 	error = setsockopt(s_dst, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
    463  1.1  itojun 	if (error == -1)
    464  1.4  itojun 		exit_error("setsockopt(SO_SNDTIMEO): %s", ERRSTR);
    465  1.1  itojun 
    466  1.8  itojun 	error = connect(s_dst, sa4, sa4->sa_len);
    467  1.1  itojun 	if (error == -1)
    468  1.1  itojun 		exit_failure("connect: %s", ERRSTR);
    469  1.1  itojun 
    470  1.1  itojun 	switch (hport) {
    471  1.1  itojun 	case FTP_PORT:
    472  1.1  itojun 		ftp_relay(s_src, s_dst);
    473  1.1  itojun 		break;
    474  1.1  itojun 	case RSH_PORT:
    475  1.1  itojun 		rsh_relay(s_src, s_dst);
    476  1.1  itojun 		break;
    477  1.1  itojun 	default:
    478  1.1  itojun 		tcp_relay(s_src, s_dst, service);
    479  1.1  itojun 		break;
    480  1.1  itojun 	}
    481  1.1  itojun 
    482  1.1  itojun 	/* NOTREACHED */
    483  1.1  itojun }
    484  1.1  itojun 
    485  1.1  itojun /* 0: non faith, 1: faith */
    486  1.1  itojun static int
    487  1.1  itojun faith_prefix(struct sockaddr *dst)
    488  1.1  itojun {
    489  1.1  itojun #ifndef USE_ROUTE
    490  1.1  itojun 	int mib[4], size;
    491  1.1  itojun 	struct in6_addr faith_prefix;
    492  1.1  itojun 	struct sockaddr_in6 *dst6 = (struct sockaddr_in *)dst;
    493  1.1  itojun 
    494  1.1  itojun 	if (dst->sa_family != AF_INET6)
    495  1.1  itojun 		return 0;
    496  1.1  itojun 
    497  1.1  itojun 	mib[0] = CTL_NET;
    498  1.1  itojun 	mib[1] = PF_INET6;
    499  1.1  itojun 	mib[2] = IPPROTO_IPV6;
    500  1.1  itojun 	mib[3] = IPV6CTL_FAITH_PREFIX;
    501  1.1  itojun 	size = sizeof(struct in6_addr);
    502  1.1  itojun 	if (sysctl(mib, 4, &faith_prefix, &size, NULL, 0) < 0)
    503  1.1  itojun 		exit_error("sysctl: %s", ERRSTR);
    504  1.1  itojun 
    505  1.2  itojun 	if (memcmp(dst, &faith_prefix,
    506  1.2  itojun 			sizeof(struct in6_addr) - sizeof(struct in_addr) == 0) {
    507  1.1  itojun 		return 1;
    508  1.2  itojun 	}
    509  1.1  itojun 	return 0;
    510  1.1  itojun #else
    511  1.1  itojun 	struct myaddrs *p;
    512  1.1  itojun 	struct sockaddr_in6 *sin6;
    513  1.1  itojun 	struct sockaddr_in *sin4;
    514  1.3  itojun 	struct sockaddr_in6 *dst6;
    515  1.3  itojun 	struct sockaddr_in *dst4;
    516  1.3  itojun 	struct sockaddr_in dstmap;
    517  1.3  itojun 
    518  1.4  itojun 	dst6 = (struct sockaddr_in6 *)dst;
    519  1.3  itojun 	if (dst->sa_family == AF_INET6
    520  1.3  itojun 	 && IN6_IS_ADDR_V4MAPPED(&dst6->sin6_addr)) {
    521  1.3  itojun 		/* ugly... */
    522  1.3  itojun 		memset(&dstmap, 0, sizeof(dstmap));
    523  1.3  itojun 		dstmap.sin_family = AF_INET;
    524  1.3  itojun 		dstmap.sin_len = sizeof(dstmap);
    525  1.3  itojun 		memcpy(&dstmap.sin_addr, &dst6->sin6_addr.s6_addr[12],
    526  1.3  itojun 			sizeof(dstmap.sin_addr));
    527  1.3  itojun 		dst = (struct sockaddr *)&dstmap;
    528  1.3  itojun 	}
    529  1.3  itojun 
    530  1.3  itojun 	dst6 = (struct sockaddr_in6 *)dst;
    531  1.3  itojun 	dst4 = (struct sockaddr_in *)dst;
    532  1.1  itojun 
    533  1.1  itojun 	for (p = myaddrs; p; p = p->next) {
    534  1.1  itojun 		sin6 = (struct sockaddr_in6 *)p->addr;
    535  1.1  itojun 		sin4 = (struct sockaddr_in *)p->addr;
    536  1.1  itojun 
    537  1.3  itojun 		if (p->addr->sa_len != dst->sa_len
    538  1.3  itojun 		 || p->addr->sa_family != dst->sa_family)
    539  1.3  itojun 			continue;
    540  1.3  itojun 
    541  1.3  itojun 		switch (dst->sa_family) {
    542  1.3  itojun 		case AF_INET6:
    543  1.3  itojun 			if (sin6->sin6_scope_id == dst6->sin6_scope_id
    544  1.5  itojun 			 && IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &dst6->sin6_addr))
    545  1.1  itojun 				return 0;
    546  1.3  itojun 			break;
    547  1.3  itojun 		case AF_INET:
    548  1.3  itojun 			if (sin4->sin_addr.s_addr == dst4->sin_addr.s_addr)
    549  1.6  itojun 				return 0;
    550  1.3  itojun 			break;
    551  1.1  itojun 		}
    552  1.1  itojun 	}
    553  1.1  itojun 	return 1;
    554  1.1  itojun #endif
    555  1.1  itojun }
    556  1.1  itojun 
    557  1.1  itojun /* 0: non faith, 1: faith */
    558  1.1  itojun static int
    559  1.1  itojun map6to4(struct sockaddr_in6 *dst6, struct sockaddr_in *dst4)
    560  1.1  itojun {
    561  1.1  itojun 	memset(dst4, 0, sizeof(*dst4));
    562  1.1  itojun 	dst4->sin_len = sizeof(*dst4);
    563  1.1  itojun 	dst4->sin_family = AF_INET;
    564  1.1  itojun 	dst4->sin_port = dst6->sin6_port;
    565  1.2  itojun 	memcpy(&dst4->sin_addr, &dst6->sin6_addr.s6_addr[12],
    566  1.2  itojun 		sizeof(dst4->sin_addr));
    567  1.1  itojun 
    568  1.1  itojun 	if (dst4->sin_addr.s_addr == INADDR_ANY
    569  1.1  itojun 	 || dst4->sin_addr.s_addr == INADDR_BROADCAST
    570  1.1  itojun 	 || IN_MULTICAST(dst4->sin_addr.s_addr))
    571  1.1  itojun 		return 0;
    572  1.1  itojun 
    573  1.1  itojun 	return 1;
    574  1.1  itojun }
    575  1.1  itojun 
    576  1.1  itojun #ifdef FAITH4
    577  1.1  itojun /* 0: non faith, 1: faith */
    578  1.1  itojun static int
    579  1.1  itojun map4to6(struct sockaddr_in *dst4, struct sockaddr_in6 *dst6)
    580  1.1  itojun {
    581  1.1  itojun 	char host[NI_MAXHOST];
    582  1.1  itojun 	char serv[NI_MAXSERV];
    583  1.1  itojun 	struct addrinfo hints, *res;
    584  1.1  itojun 	int ai_errno;
    585  1.1  itojun 
    586  1.1  itojun 	if (getnameinfo((struct sockaddr *)dst4, dst4->sin_len, host, sizeof(host),
    587  1.1  itojun 			serv, sizeof(serv), NI_NAMEREQD|NI_NUMERICSERV) != 0)
    588  1.1  itojun 		return 0;
    589  1.1  itojun 
    590  1.1  itojun 	memset(&hints, 0, sizeof(hints));
    591  1.1  itojun 	hints.ai_flags = 0;
    592  1.1  itojun 	hints.ai_family = AF_INET6;
    593  1.1  itojun 	hints.ai_socktype = SOCK_STREAM;
    594  1.1  itojun 	hints.ai_protocol = 0;
    595  1.1  itojun 
    596  1.1  itojun 	if ((ai_errno = getaddrinfo(host, serv, &hints, &res)) != 0) {
    597  1.1  itojun 		syslog(LOG_INFO, "%s %s: %s", host, serv, gai_strerror(ai_errno));
    598  1.1  itojun 		return 0;
    599  1.1  itojun 	}
    600  1.1  itojun 
    601  1.1  itojun 	memcpy(dst6, res->ai_addr, res->ai_addrlen);
    602  1.1  itojun 
    603  1.1  itojun 	freeaddrinfo(res);
    604  1.1  itojun 
    605  1.1  itojun 	return 1;
    606  1.1  itojun }
    607  1.1  itojun #endif /* FAITH4 */
    608  1.1  itojun 
    609  1.1  itojun static void
    610  1.1  itojun sig_child(int sig)
    611  1.1  itojun {
    612  1.1  itojun 	int status;
    613  1.1  itojun 	pid_t pid;
    614  1.1  itojun 
    615  1.1  itojun 	pid = wait3(&status, WNOHANG, (struct rusage *)0);
    616  1.1  itojun 	if (pid && status)
    617  1.1  itojun 		syslog(LOG_WARNING, "child %d exit status 0x%x", pid, status);
    618  1.1  itojun }
    619  1.1  itojun 
    620  1.1  itojun void
    621  1.1  itojun sig_terminate(int sig)
    622  1.1  itojun {
    623  1.1  itojun 	syslog(LOG_INFO, "Terminating faith daemon");
    624  1.1  itojun 	exit(EXIT_SUCCESS);
    625  1.1  itojun }
    626  1.1  itojun 
    627  1.1  itojun static void
    628  1.1  itojun start_daemon(void)
    629  1.1  itojun {
    630  1.1  itojun 	if (daemon(0, 0) == -1)
    631  1.1  itojun 		exit_error("daemon: %s", ERRSTR);
    632  1.1  itojun 
    633  1.1  itojun 	if (signal(SIGCHLD, sig_child) == SIG_ERR)
    634  1.1  itojun 		exit_failure("signal CHLD: %s", ERRSTR);
    635  1.1  itojun 
    636  1.1  itojun 	if (signal(SIGTERM, sig_terminate) == SIG_ERR)
    637  1.1  itojun 		exit_failure("signal TERM: %s", ERRSTR);
    638  1.1  itojun }
    639  1.1  itojun 
    640  1.1  itojun void
    641  1.1  itojun exit_error(const char *fmt, ...)
    642  1.1  itojun {
    643  1.1  itojun 	va_list ap;
    644  1.1  itojun 	char buf[BUFSIZ];
    645  1.1  itojun 
    646  1.1  itojun 	va_start(ap, fmt);
    647  1.7  itojun 	vsnprintf(buf, sizeof(buf), fmt, ap);
    648  1.1  itojun 	va_end(ap);
    649  1.1  itojun 	fprintf(stderr, "%s\n", buf);
    650  1.1  itojun 	exit(EXIT_FAILURE);
    651  1.1  itojun }
    652  1.1  itojun 
    653  1.1  itojun void
    654  1.1  itojun exit_failure(const char *fmt, ...)
    655  1.1  itojun {
    656  1.1  itojun 	va_list ap;
    657  1.1  itojun 	char buf[BUFSIZ];
    658  1.1  itojun 
    659  1.1  itojun 	va_start(ap, fmt);
    660  1.7  itojun 	vsnprintf(buf, sizeof(buf), fmt, ap);
    661  1.1  itojun 	va_end(ap);
    662  1.1  itojun 	syslog(LOG_ERR, buf);
    663  1.1  itojun 	exit(EXIT_FAILURE);
    664  1.1  itojun }
    665  1.1  itojun 
    666  1.1  itojun void
    667  1.1  itojun exit_success(const char *fmt, ...)
    668  1.1  itojun {
    669  1.1  itojun 	va_list ap;
    670  1.1  itojun 	char buf[BUFSIZ];
    671  1.1  itojun 
    672  1.1  itojun 	va_start(ap, fmt);
    673  1.7  itojun 	vsnprintf(buf, sizeof(buf), fmt, ap);
    674  1.1  itojun 	va_end(ap);
    675  1.1  itojun 	syslog(LOG_INFO, buf);
    676  1.1  itojun 	exit(EXIT_SUCCESS);
    677  1.1  itojun }
    678  1.1  itojun 
    679  1.1  itojun #ifdef USE_ROUTE
    680  1.1  itojun static unsigned int
    681  1.1  itojun if_maxindex()
    682  1.1  itojun {
    683  1.1  itojun 	struct if_nameindex *p, *p0;
    684  1.1  itojun 	unsigned int max = 0;
    685  1.1  itojun 
    686  1.1  itojun 	p0 = if_nameindex();
    687  1.1  itojun 	for (p = p0; p && p->if_index && p->if_name; p++) {
    688  1.1  itojun 		if (max < p->if_index)
    689  1.1  itojun 			max = p->if_index;
    690  1.1  itojun 	}
    691  1.1  itojun 	if_freenameindex(p0);
    692  1.1  itojun 	return max;
    693  1.1  itojun }
    694  1.1  itojun 
    695  1.1  itojun static void
    696  1.1  itojun grab_myaddrs()
    697  1.1  itojun {
    698  1.1  itojun 	int s;
    699  1.1  itojun 	unsigned int maxif;
    700  1.1  itojun 	struct ifreq *iflist;
    701  1.1  itojun 	struct ifconf ifconf;
    702  1.1  itojun 	struct ifreq *ifr, *ifr_end;
    703  1.1  itojun 	struct myaddrs *p;
    704  1.1  itojun 	struct sockaddr_in6 *sin6;
    705  1.1  itojun 
    706  1.1  itojun 	maxif = if_maxindex() + 1;
    707  1.1  itojun 	iflist = (struct ifreq *)malloc(maxif * BUFSIZ);	/* XXX */
    708  1.1  itojun 	if (!iflist) {
    709  1.1  itojun 		exit_failure("not enough core");
    710  1.1  itojun 		/*NOTREACHED*/
    711  1.1  itojun 	}
    712  1.1  itojun 
    713  1.1  itojun 	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    714  1.1  itojun 		exit_failure("socket(SOCK_DGRAM)");
    715  1.1  itojun 		/*NOTREACHED*/
    716  1.1  itojun 	}
    717  1.1  itojun 	memset(&ifconf, 0, sizeof(ifconf));
    718  1.1  itojun 	ifconf.ifc_req = iflist;
    719  1.1  itojun 	ifconf.ifc_len = maxif * BUFSIZ;	/* XXX */
    720  1.1  itojun 	if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) {
    721  1.1  itojun 		exit_failure("ioctl(SIOCGIFCONF)");
    722  1.1  itojun 		/*NOTREACHED*/
    723  1.1  itojun 	}
    724  1.1  itojun 	close(s);
    725  1.1  itojun 
    726  1.1  itojun 	/* Look for this interface in the list */
    727  1.1  itojun 	ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len);
    728  1.1  itojun 	for (ifr = ifconf.ifc_req;
    729  1.1  itojun 	     ifr < ifr_end;
    730  1.1  itojun 	     ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
    731  1.1  itojun 				    + ifr->ifr_addr.sa_len)) {
    732  1.1  itojun 		switch (ifr->ifr_addr.sa_family) {
    733  1.1  itojun 		case AF_INET:
    734  1.1  itojun 		case AF_INET6:
    735  1.1  itojun 			p = (struct myaddrs *)malloc(sizeof(struct myaddrs)
    736  1.1  itojun 				+ ifr->ifr_addr.sa_len);
    737  1.1  itojun 			if (!p) {
    738  1.1  itojun 				exit_failure("not enough core");
    739  1.1  itojun 				/*NOTREACHED*/
    740  1.1  itojun 			}
    741  1.1  itojun 			memcpy(p + 1, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
    742  1.1  itojun 			p->next = myaddrs;
    743  1.1  itojun 			p->addr = (struct sockaddr *)(p + 1);
    744  1.1  itojun #ifdef __KAME__
    745  1.1  itojun 			if (ifr->ifr_addr.sa_family == AF_INET6) {
    746  1.1  itojun 				sin6 = (struct sockaddr_in6 *)p->addr;
    747  1.1  itojun 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
    748  1.1  itojun 				 || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
    749  1.1  itojun 					sin6->sin6_scope_id =
    750  1.2  itojun 						ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
    751  1.2  itojun 					sin6->sin6_addr.s6_addr[2] = 0;
    752  1.2  itojun 					sin6->sin6_addr.s6_addr[3] = 0;
    753  1.1  itojun 				}
    754  1.1  itojun 			}
    755  1.1  itojun #endif
    756  1.1  itojun 			myaddrs = p;
    757  1.1  itojun 			if (dflag) {
    758  1.1  itojun 				char hbuf[NI_MAXHOST];
    759  1.1  itojun 				getnameinfo(p->addr, p->addr->sa_len,
    760  1.1  itojun 					hbuf, sizeof(hbuf), NULL, 0,
    761  1.1  itojun 					NI_NUMERICHOST);
    762  1.1  itojun 				syslog(LOG_INFO, "my interface: %s %s", hbuf, ifr->ifr_name);
    763  1.1  itojun 			}
    764  1.1  itojun 			break;
    765  1.1  itojun 		default:
    766  1.1  itojun 			break;
    767  1.1  itojun 		}
    768  1.1  itojun 	}
    769  1.1  itojun 
    770  1.1  itojun 	free(iflist);
    771  1.1  itojun }
    772  1.1  itojun 
    773  1.1  itojun static void
    774  1.1  itojun free_myaddrs()
    775  1.1  itojun {
    776  1.1  itojun 	struct myaddrs *p, *q;
    777  1.1  itojun 
    778  1.1  itojun 	p = myaddrs;
    779  1.1  itojun 	while (p) {
    780  1.1  itojun 		q = p->next;
    781  1.1  itojun 		free(p);
    782  1.1  itojun 		p = q;
    783  1.1  itojun 	}
    784  1.1  itojun 	myaddrs = NULL;
    785  1.1  itojun }
    786  1.1  itojun 
    787  1.1  itojun static void
    788  1.1  itojun update_myaddrs()
    789  1.1  itojun {
    790  1.1  itojun 	char msg[BUFSIZ];
    791  1.1  itojun 	int len;
    792  1.1  itojun 	struct rt_msghdr *rtm;
    793  1.1  itojun 
    794  1.1  itojun 	len = read(sockfd, msg, sizeof(msg));
    795  1.1  itojun 	if (len < 0) {
    796  1.1  itojun 		syslog(LOG_ERR, "read(PF_ROUTE) failed");
    797  1.1  itojun 		return;
    798  1.1  itojun 	}
    799  1.1  itojun 	rtm = (struct rt_msghdr *)msg;
    800  1.1  itojun 	if (len < 4 || len < rtm->rtm_msglen) {
    801  1.1  itojun 		syslog(LOG_ERR, "read(PF_ROUTE) short read");
    802  1.1  itojun 		return;
    803  1.1  itojun 	}
    804  1.1  itojun 	if (rtm->rtm_version != RTM_VERSION) {
    805  1.1  itojun 		syslog(LOG_ERR, "routing socket version mismatch");
    806  1.1  itojun 		close(sockfd);
    807  1.1  itojun 		sockfd = 0;
    808  1.1  itojun 		return;
    809  1.1  itojun 	}
    810  1.1  itojun 	switch (rtm->rtm_type) {
    811  1.1  itojun 	case RTM_NEWADDR:
    812  1.1  itojun 	case RTM_DELADDR:
    813  1.1  itojun 	case RTM_IFINFO:
    814  1.1  itojun 		break;
    815  1.1  itojun 	default:
    816  1.1  itojun 		return;
    817  1.1  itojun 	}
    818  1.1  itojun 	/* XXX more filters here? */
    819  1.1  itojun 
    820  1.1  itojun 	syslog(LOG_INFO, "update interface address list");
    821  1.1  itojun 	free_myaddrs();
    822  1.1  itojun 	grab_myaddrs();
    823  1.1  itojun }
    824  1.1  itojun #endif /*USE_ROUTE*/
    825  1.1  itojun 
    826  1.1  itojun static void
    827  1.1  itojun usage()
    828  1.1  itojun {
    829  1.1  itojun 	fprintf(stderr, "usage: %s [-dp] [service [serverpath [serverargs]]]\n",
    830  1.1  itojun 		faithdname);
    831  1.1  itojun 	exit(0);
    832  1.1  itojun }
    833