faithd.c revision 1.18 1 1.18 itojun /* $NetBSD: faithd.c,v 1.18 2001/03/20 01:13:46 itojun Exp $ */
2 1.18 itojun /* $KAME: faithd.c,v 1.38 2001/02/27 06:46:52 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.9 itojun #define HAVE_GETIFADDRS
40 1.1 itojun
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 #ifdef __FreeBSD__
50 1.1 itojun #include <libutil.h>
51 1.1 itojun #endif
52 1.1 itojun
53 1.1 itojun #include <stdio.h>
54 1.1 itojun #include <stdlib.h>
55 1.1 itojun #include <stdarg.h>
56 1.1 itojun #include <string.h>
57 1.1 itojun #include <syslog.h>
58 1.1 itojun #include <unistd.h>
59 1.1 itojun #include <errno.h>
60 1.1 itojun #include <signal.h>
61 1.1 itojun #include <fcntl.h>
62 1.1 itojun #include <termios.h>
63 1.1 itojun
64 1.1 itojun #include <net/if_types.h>
65 1.1 itojun #ifdef IFT_FAITH
66 1.1 itojun # define USE_ROUTE
67 1.1 itojun # include <net/if.h>
68 1.1 itojun # include <net/route.h>
69 1.1 itojun # include <net/if_dl.h>
70 1.1 itojun #endif
71 1.1 itojun
72 1.1 itojun #include <netinet/in.h>
73 1.1 itojun #include <arpa/inet.h>
74 1.1 itojun #include <netdb.h>
75 1.9 itojun #ifdef HAVE_GETIFADDRS
76 1.9 itojun #include <ifaddrs.h>
77 1.9 itojun #endif
78 1.1 itojun
79 1.1 itojun #ifdef FAITH4
80 1.1 itojun #include <resolv.h>
81 1.1 itojun #include <arpa/nameser.h>
82 1.1 itojun #ifndef FAITH_NS
83 1.1 itojun #define FAITH_NS "FAITH_NS"
84 1.1 itojun #endif
85 1.1 itojun #endif
86 1.1 itojun
87 1.1 itojun #include "faithd.h"
88 1.17 itojun #include "prefix.h"
89 1.1 itojun
90 1.1 itojun char *serverpath = NULL;
91 1.1 itojun char *serverarg[MAXARGV + 1];
92 1.1 itojun static char *faithdname = NULL;
93 1.1 itojun char logname[BUFSIZ];
94 1.7 itojun char procname[BUFSIZ];
95 1.1 itojun struct myaddrs {
96 1.1 itojun struct myaddrs *next;
97 1.1 itojun struct sockaddr *addr;
98 1.1 itojun };
99 1.1 itojun struct myaddrs *myaddrs = NULL;
100 1.1 itojun static char *service;
101 1.1 itojun #ifdef USE_ROUTE
102 1.1 itojun static int sockfd = 0;
103 1.1 itojun #endif
104 1.1 itojun int dflag = 0;
105 1.1 itojun static int pflag = 0;
106 1.12 itojun static int inetd = 0;
107 1.17 itojun static char *configfile = NULL;
108 1.1 itojun
109 1.1 itojun int main __P((int, char **));
110 1.12 itojun static int inetd_main __P((int, char **));
111 1.12 itojun static int daemon_main __P((int, char **));
112 1.1 itojun static void play_service __P((int));
113 1.1 itojun static void play_child __P((int, struct sockaddr *));
114 1.1 itojun static int faith_prefix __P((struct sockaddr *));
115 1.1 itojun static int map6to4 __P((struct sockaddr_in6 *, struct sockaddr_in *));
116 1.1 itojun #ifdef FAITH4
117 1.1 itojun static int map4to6 __P((struct sockaddr_in *, struct sockaddr_in6 *));
118 1.1 itojun #endif
119 1.1 itojun static void sig_child __P((int));
120 1.1 itojun static void sig_terminate __P((int));
121 1.1 itojun static void start_daemon __P((void));
122 1.15 itojun static void exit_stderr __P((const char *, ...))
123 1.15 itojun __attribute__((__format__(__printf__, 1, 2)));
124 1.9 itojun #ifndef HAVE_GETIFADDRS
125 1.1 itojun static unsigned int if_maxindex __P((void));
126 1.9 itojun #endif
127 1.1 itojun static void grab_myaddrs __P((void));
128 1.1 itojun static void free_myaddrs __P((void));
129 1.1 itojun static void update_myaddrs __P((void));
130 1.1 itojun static void usage __P((void));
131 1.1 itojun
132 1.1 itojun int
133 1.12 itojun main(int argc, char **argv)
134 1.1 itojun {
135 1.1 itojun
136 1.1 itojun /*
137 1.1 itojun * Initializing stuff
138 1.1 itojun */
139 1.1 itojun
140 1.1 itojun faithdname = strrchr(argv[0], '/');
141 1.1 itojun if (faithdname)
142 1.1 itojun faithdname++;
143 1.1 itojun else
144 1.1 itojun faithdname = argv[0];
145 1.1 itojun
146 1.12 itojun if (strcmp(faithdname, "faithd") != 0) {
147 1.12 itojun inetd = 1;
148 1.12 itojun return inetd_main(argc, argv);
149 1.12 itojun } else
150 1.12 itojun return daemon_main(argc, argv);
151 1.12 itojun }
152 1.12 itojun
153 1.12 itojun static int
154 1.12 itojun inetd_main(int argc, char **argv)
155 1.12 itojun {
156 1.12 itojun char path[MAXPATHLEN];
157 1.12 itojun struct sockaddr_storage me;
158 1.12 itojun struct sockaddr_storage from;
159 1.12 itojun int melen, fromlen;
160 1.12 itojun int i;
161 1.12 itojun int error;
162 1.12 itojun const int on = 1;
163 1.12 itojun char sbuf[NI_MAXSERV], snum[NI_MAXSERV];
164 1.12 itojun
165 1.17 itojun if (config_load(configfile) < 0 && configfile) {
166 1.17 itojun exit_failure("could not load config file");
167 1.17 itojun /*NOTREACHED*/
168 1.17 itojun }
169 1.17 itojun
170 1.12 itojun if (strrchr(argv[0], '/') == NULL)
171 1.12 itojun snprintf(path, sizeof(path), "%s/%s", DEFAULT_DIR, argv[0]);
172 1.12 itojun else
173 1.12 itojun snprintf(path, sizeof(path), "%s", argv[0]);
174 1.12 itojun
175 1.12 itojun #ifdef USE_ROUTE
176 1.12 itojun grab_myaddrs();
177 1.12 itojun
178 1.12 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
179 1.12 itojun if (sockfd < 0) {
180 1.14 itojun exit_failure("socket(PF_ROUTE): %s", ERRSTR);
181 1.12 itojun /*NOTREACHED*/
182 1.12 itojun }
183 1.12 itojun #endif
184 1.12 itojun
185 1.12 itojun melen = sizeof(me);
186 1.17 itojun if (getsockname(STDIN_FILENO, (struct sockaddr *)&me, &melen) < 0) {
187 1.17 itojun exit_failure("getsockname: %s", ERRSTR);
188 1.17 itojun /*NOTREACHED*/
189 1.17 itojun }
190 1.12 itojun fromlen = sizeof(from);
191 1.17 itojun if (getpeername(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) {
192 1.17 itojun exit_failure("getpeername: %s", ERRSTR);
193 1.17 itojun /*NOTREACHED*/
194 1.17 itojun }
195 1.12 itojun if (getnameinfo((struct sockaddr *)&me, melen, NULL, 0,
196 1.12 itojun sbuf, sizeof(sbuf), NI_NUMERICHOST) == 0)
197 1.12 itojun service = sbuf;
198 1.12 itojun else
199 1.12 itojun service = DEFAULT_PORT_NAME;
200 1.12 itojun if (getnameinfo((struct sockaddr *)&me, melen, NULL, 0,
201 1.12 itojun snum, sizeof(snum), NI_NUMERICHOST) != 0)
202 1.12 itojun snprintf(snum, sizeof(snum), "?");
203 1.12 itojun
204 1.12 itojun snprintf(logname, sizeof(logname), "faithd %s", snum);
205 1.12 itojun snprintf(procname, sizeof(procname), "accepting port %s", snum);
206 1.12 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
207 1.12 itojun
208 1.17 itojun if (argc >= MAXARGV) {
209 1.14 itojun exit_failure("too many arguments");
210 1.17 itojun /*NOTREACHED*/
211 1.17 itojun }
212 1.12 itojun serverarg[0] = serverpath = path;
213 1.12 itojun for (i = 1; i < argc; i++)
214 1.12 itojun serverarg[i] = argv[i];
215 1.12 itojun serverarg[i] = NULL;
216 1.12 itojun
217 1.12 itojun error = setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &on,
218 1.12 itojun sizeof(on));
219 1.17 itojun if (error < 0) {
220 1.14 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", ERRSTR);
221 1.17 itojun /*NOTREACHED*/
222 1.17 itojun }
223 1.12 itojun
224 1.12 itojun play_child(STDIN_FILENO, (struct sockaddr *)&from);
225 1.12 itojun exit_failure("should not reach here");
226 1.12 itojun return 0; /*dummy!*/
227 1.12 itojun }
228 1.12 itojun
229 1.12 itojun static int
230 1.12 itojun daemon_main(int argc, char **argv)
231 1.12 itojun {
232 1.12 itojun struct addrinfo hints, *res;
233 1.12 itojun int s_wld, error, i, serverargc, on = 1;
234 1.12 itojun int family = AF_INET6;
235 1.12 itojun int c;
236 1.12 itojun #ifdef FAITH_NS
237 1.12 itojun char *ns;
238 1.12 itojun #endif /* FAITH_NS */
239 1.12 itojun
240 1.17 itojun while ((c = getopt(argc, argv, "df:p46")) != -1) {
241 1.1 itojun switch (c) {
242 1.1 itojun case 'd':
243 1.1 itojun dflag++;
244 1.1 itojun break;
245 1.17 itojun case 'f':
246 1.17 itojun configfile = optarg;
247 1.17 itojun break;
248 1.1 itojun case 'p':
249 1.1 itojun pflag++;
250 1.1 itojun break;
251 1.1 itojun #ifdef FAITH4
252 1.1 itojun case '4':
253 1.1 itojun family = AF_INET;
254 1.1 itojun break;
255 1.1 itojun case '6':
256 1.1 itojun family = AF_INET6;
257 1.1 itojun break;
258 1.1 itojun #endif
259 1.1 itojun default:
260 1.1 itojun usage();
261 1.14 itojun /*NOTREACHED*/
262 1.1 itojun }
263 1.1 itojun }
264 1.1 itojun argc -= optind;
265 1.1 itojun argv += optind;
266 1.1 itojun
267 1.17 itojun if (config_load(configfile) < 0 && configfile) {
268 1.17 itojun exit_failure("could not load config file");
269 1.17 itojun /*NOTREACHED*/
270 1.17 itojun }
271 1.17 itojun
272 1.1 itojun #ifdef FAITH_NS
273 1.1 itojun if ((ns = getenv(FAITH_NS)) != NULL) {
274 1.1 itojun struct sockaddr_storage ss;
275 1.1 itojun struct addrinfo hints, *res;
276 1.1 itojun char serv[NI_MAXSERV];
277 1.1 itojun
278 1.1 itojun memset(&ss, 0, sizeof(ss));
279 1.1 itojun memset(&hints, 0, sizeof(hints));
280 1.7 itojun snprintf(serv, sizeof(serv), "%u", NAMESERVER_PORT);
281 1.1 itojun hints.ai_flags = AI_NUMERICHOST;
282 1.1 itojun if (getaddrinfo(ns, serv, &hints, &res) == 0) {
283 1.1 itojun res_init();
284 1.1 itojun memcpy(&_res_ext.nsaddr, res->ai_addr, res->ai_addrlen);
285 1.1 itojun _res.nscount = 1;
286 1.1 itojun }
287 1.1 itojun }
288 1.1 itojun #endif /* FAITH_NS */
289 1.1 itojun
290 1.1 itojun #ifdef USE_ROUTE
291 1.1 itojun grab_myaddrs();
292 1.1 itojun #endif
293 1.1 itojun
294 1.1 itojun switch (argc) {
295 1.1 itojun case 0:
296 1.14 itojun usage();
297 1.14 itojun /*NOTREACHED*/
298 1.1 itojun default:
299 1.1 itojun serverargc = argc - NUMARG;
300 1.12 itojun if (serverargc >= MAXARGV)
301 1.14 itojun exit_stderr("too many arguments");
302 1.1 itojun
303 1.10 itojun serverpath = malloc(strlen(argv[NUMPRG]) + 1);
304 1.1 itojun strcpy(serverpath, argv[NUMPRG]);
305 1.1 itojun for (i = 0; i < serverargc; i++) {
306 1.10 itojun serverarg[i] = malloc(strlen(argv[i + NUMARG]) + 1);
307 1.1 itojun strcpy(serverarg[i], argv[i + NUMARG]);
308 1.1 itojun }
309 1.1 itojun serverarg[i] = NULL;
310 1.1 itojun /* fall throuth */
311 1.1 itojun case 1: /* no local service */
312 1.1 itojun service = argv[NUMPRT];
313 1.1 itojun break;
314 1.1 itojun }
315 1.1 itojun
316 1.1 itojun /*
317 1.1 itojun * Opening wild card socket for this service.
318 1.1 itojun */
319 1.1 itojun
320 1.1 itojun memset(&hints, 0, sizeof(hints));
321 1.1 itojun hints.ai_flags = AI_PASSIVE;
322 1.1 itojun hints.ai_family = family;
323 1.1 itojun hints.ai_socktype = SOCK_STREAM;
324 1.1 itojun hints.ai_protocol = 0;
325 1.1 itojun error = getaddrinfo(NULL, service, &hints, &res);
326 1.10 itojun if (error)
327 1.14 itojun exit_stderr("getaddrinfo: %s", gai_strerror(error));
328 1.1 itojun
329 1.1 itojun s_wld = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
330 1.1 itojun if (s_wld == -1)
331 1.14 itojun exit_stderr("socket: %s", ERRSTR);
332 1.1 itojun
333 1.1 itojun #ifdef IPV6_FAITH
334 1.1 itojun if (res->ai_family == AF_INET6) {
335 1.1 itojun error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_FAITH, &on, sizeof(on));
336 1.1 itojun if (error == -1)
337 1.14 itojun exit_stderr("setsockopt(IPV6_FAITH): %s", ERRSTR);
338 1.1 itojun }
339 1.1 itojun #endif
340 1.1 itojun #ifdef FAITH4
341 1.1 itojun #ifdef IP_FAITH
342 1.1 itojun if (res->ai_family == AF_INET) {
343 1.1 itojun error = setsockopt(s_wld, IPPROTO_IP, IP_FAITH, &on, sizeof(on));
344 1.1 itojun if (error == -1)
345 1.14 itojun exit_stderr("setsockopt(IP_FAITH): %s", ERRSTR);
346 1.1 itojun }
347 1.1 itojun #endif
348 1.1 itojun #endif /* FAITH4 */
349 1.1 itojun
350 1.1 itojun error = setsockopt(s_wld, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
351 1.1 itojun if (error == -1)
352 1.14 itojun exit_stderr("setsockopt(SO_REUSEADDR): %s", ERRSTR);
353 1.1 itojun
354 1.1 itojun error = setsockopt(s_wld, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
355 1.1 itojun if (error == -1)
356 1.14 itojun exit_stderr("setsockopt(SO_OOBINLINE): %s", ERRSTR);
357 1.1 itojun
358 1.1 itojun error = bind(s_wld, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
359 1.1 itojun if (error == -1)
360 1.14 itojun exit_stderr("bind: %s", ERRSTR);
361 1.1 itojun
362 1.1 itojun error = listen(s_wld, 5);
363 1.1 itojun if (error == -1)
364 1.14 itojun exit_stderr("listen: %s", ERRSTR);
365 1.1 itojun
366 1.1 itojun #ifdef USE_ROUTE
367 1.1 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
368 1.1 itojun if (sockfd < 0) {
369 1.14 itojun exit_stderr("socket(PF_ROUTE): %s", ERRSTR);
370 1.1 itojun /*NOTREACHED*/
371 1.1 itojun }
372 1.1 itojun #endif
373 1.1 itojun
374 1.1 itojun /*
375 1.1 itojun * Everything is OK.
376 1.1 itojun */
377 1.1 itojun
378 1.1 itojun start_daemon();
379 1.1 itojun
380 1.7 itojun snprintf(logname, sizeof(logname), "faithd %s", service);
381 1.7 itojun snprintf(procname, sizeof(procname), "accepting port %s", service);
382 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
383 1.17 itojun syslog(LOG_INFO, "Staring faith daemon for %s port", service);
384 1.1 itojun
385 1.1 itojun play_service(s_wld);
386 1.14 itojun /* NOTREACHED */
387 1.1 itojun exit(1); /*pacify gcc*/
388 1.1 itojun }
389 1.1 itojun
390 1.1 itojun static void
391 1.1 itojun play_service(int s_wld)
392 1.1 itojun {
393 1.1 itojun struct sockaddr_storage srcaddr;
394 1.1 itojun int len;
395 1.1 itojun int s_src;
396 1.1 itojun pid_t child_pid;
397 1.1 itojun fd_set rfds;
398 1.1 itojun int error;
399 1.1 itojun int maxfd;
400 1.1 itojun
401 1.1 itojun /*
402 1.1 itojun * Wait, accept, fork, faith....
403 1.1 itojun */
404 1.1 itojun again:
405 1.13 itojun setproctitle("%s", procname);
406 1.1 itojun
407 1.1 itojun FD_ZERO(&rfds);
408 1.1 itojun FD_SET(s_wld, &rfds);
409 1.1 itojun maxfd = s_wld;
410 1.1 itojun #ifdef USE_ROUTE
411 1.1 itojun if (sockfd) {
412 1.1 itojun FD_SET(sockfd, &rfds);
413 1.1 itojun maxfd = (maxfd < sockfd) ? sockfd : maxfd;
414 1.1 itojun }
415 1.1 itojun #endif
416 1.1 itojun
417 1.1 itojun error = select(maxfd + 1, &rfds, NULL, NULL, NULL);
418 1.1 itojun if (error < 0) {
419 1.1 itojun if (errno == EINTR)
420 1.1 itojun goto again;
421 1.1 itojun exit_failure("select: %s", ERRSTR);
422 1.1 itojun /*NOTREACHED*/
423 1.1 itojun }
424 1.1 itojun
425 1.1 itojun #ifdef USE_ROUTE
426 1.1 itojun if (FD_ISSET(sockfd, &rfds)) {
427 1.1 itojun update_myaddrs();
428 1.1 itojun }
429 1.1 itojun #endif
430 1.1 itojun if (FD_ISSET(s_wld, &rfds)) {
431 1.1 itojun len = sizeof(srcaddr);
432 1.1 itojun s_src = accept(s_wld, (struct sockaddr *)&srcaddr,
433 1.1 itojun &len);
434 1.17 itojun if (s_src == -1) {
435 1.1 itojun exit_failure("socket: %s", ERRSTR);
436 1.17 itojun /*NOTREACHED*/
437 1.17 itojun }
438 1.1 itojun
439 1.1 itojun child_pid = fork();
440 1.1 itojun
441 1.1 itojun if (child_pid == 0) {
442 1.1 itojun /* child process */
443 1.1 itojun close(s_wld);
444 1.1 itojun closelog();
445 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
446 1.1 itojun play_child(s_src, (struct sockaddr *)&srcaddr);
447 1.1 itojun exit_failure("should never reach here");
448 1.17 itojun /*NOTREACHED*/
449 1.1 itojun } else {
450 1.1 itojun /* parent process */
451 1.1 itojun close(s_src);
452 1.1 itojun if (child_pid == -1)
453 1.1 itojun syslog(LOG_ERR, "can't fork");
454 1.1 itojun }
455 1.1 itojun }
456 1.1 itojun goto again;
457 1.1 itojun }
458 1.1 itojun
459 1.1 itojun static void
460 1.1 itojun play_child(int s_src, struct sockaddr *srcaddr)
461 1.1 itojun {
462 1.10 itojun struct sockaddr_storage dstaddr6;
463 1.1 itojun struct sockaddr_storage dstaddr4;
464 1.1 itojun char src[MAXHOSTNAMELEN];
465 1.1 itojun char dst6[MAXHOSTNAMELEN];
466 1.1 itojun char dst4[MAXHOSTNAMELEN];
467 1.1 itojun int len = sizeof(dstaddr6);
468 1.1 itojun int s_dst, error, hport, nresvport, on = 1;
469 1.1 itojun struct timeval tv;
470 1.2 itojun struct sockaddr *sa4;
471 1.17 itojun const struct config *conf;
472 1.1 itojun
473 1.1 itojun tv.tv_sec = 1;
474 1.1 itojun tv.tv_usec = 0;
475 1.1 itojun
476 1.1 itojun getnameinfo(srcaddr, srcaddr->sa_len,
477 1.1 itojun src, sizeof(src), NULL, 0, NI_NUMERICHOST);
478 1.1 itojun syslog(LOG_INFO, "accepted a client from %s", src);
479 1.1 itojun
480 1.1 itojun error = getsockname(s_src, (struct sockaddr *)&dstaddr6, &len);
481 1.17 itojun if (error == -1) {
482 1.1 itojun exit_failure("getsockname: %s", ERRSTR);
483 1.17 itojun /*NOTREACHED*/
484 1.17 itojun }
485 1.1 itojun
486 1.2 itojun getnameinfo((struct sockaddr *)&dstaddr6, len,
487 1.1 itojun dst6, sizeof(dst6), NULL, 0, NI_NUMERICHOST);
488 1.1 itojun syslog(LOG_INFO, "the client is connecting to %s", dst6);
489 1.1 itojun
490 1.1 itojun if (!faith_prefix((struct sockaddr *)&dstaddr6)) {
491 1.1 itojun if (serverpath) {
492 1.1 itojun /*
493 1.1 itojun * Local service
494 1.1 itojun */
495 1.1 itojun syslog(LOG_INFO, "executing local %s", serverpath);
496 1.12 itojun if (!inetd) {
497 1.12 itojun dup2(s_src, 0);
498 1.12 itojun close(s_src);
499 1.12 itojun dup2(0, 1);
500 1.12 itojun dup2(0, 2);
501 1.12 itojun }
502 1.1 itojun execv(serverpath, serverarg);
503 1.1 itojun syslog(LOG_ERR, "execv %s: %s", serverpath, ERRSTR);
504 1.1 itojun _exit(EXIT_FAILURE);
505 1.1 itojun } else {
506 1.1 itojun close(s_src);
507 1.1 itojun exit_success("no local service for %s", service);
508 1.1 itojun }
509 1.1 itojun }
510 1.1 itojun
511 1.1 itojun /*
512 1.1 itojun * Act as a translator
513 1.1 itojun */
514 1.1 itojun
515 1.2 itojun switch (((struct sockaddr *)&dstaddr6)->sa_family) {
516 1.2 itojun case AF_INET6:
517 1.1 itojun if (!map6to4((struct sockaddr_in6 *)&dstaddr6,
518 1.1 itojun (struct sockaddr_in *)&dstaddr4)) {
519 1.1 itojun close(s_src);
520 1.14 itojun exit_failure("map6to4 failed");
521 1.17 itojun /*NOTREACHED*/
522 1.1 itojun }
523 1.1 itojun syslog(LOG_INFO, "translating from v6 to v4");
524 1.2 itojun break;
525 1.1 itojun #ifdef FAITH4
526 1.2 itojun case AF_INET:
527 1.1 itojun if (!map4to6((struct sockaddr_in *)&dstaddr6,
528 1.1 itojun (struct sockaddr_in6 *)&dstaddr4)) {
529 1.1 itojun close(s_src);
530 1.14 itojun exit_failure("map4to6 failed");
531 1.17 itojun /*NOTREACHED*/
532 1.1 itojun }
533 1.1 itojun syslog(LOG_INFO, "translating from v4 to v6");
534 1.2 itojun break;
535 1.1 itojun #endif
536 1.2 itojun default:
537 1.1 itojun close(s_src);
538 1.14 itojun exit_failure("family not supported");
539 1.2 itojun /*NOTREACHED*/
540 1.1 itojun }
541 1.1 itojun
542 1.2 itojun sa4 = (struct sockaddr *)&dstaddr4;
543 1.2 itojun getnameinfo(sa4, sa4->sa_len,
544 1.1 itojun dst4, sizeof(dst4), NULL, 0, NI_NUMERICHOST);
545 1.17 itojun
546 1.17 itojun conf = config_match(srcaddr, sa4);
547 1.17 itojun if (!conf || !conf->permit) {
548 1.17 itojun close(s_src);
549 1.17 itojun exit_failure("translation to %s not permitted for %s", dst4,
550 1.17 itojun prefix_string(&conf->match));
551 1.17 itojun /*NOTREACHED*/
552 1.17 itojun }
553 1.17 itojun
554 1.1 itojun syslog(LOG_INFO, "the translator is connecting to %s", dst4);
555 1.1 itojun
556 1.1 itojun setproctitle("port %s, %s -> %s", service, src, dst4);
557 1.1 itojun
558 1.2 itojun if (sa4->sa_family == AF_INET6)
559 1.1 itojun hport = ntohs(((struct sockaddr_in6 *)&dstaddr4)->sin6_port);
560 1.1 itojun else /* AF_INET */
561 1.1 itojun hport = ntohs(((struct sockaddr_in *)&dstaddr4)->sin_port);
562 1.1 itojun
563 1.1 itojun switch (hport) {
564 1.1 itojun case RLOGIN_PORT:
565 1.1 itojun case RSH_PORT:
566 1.2 itojun s_dst = rresvport_af(&nresvport, sa4->sa_family);
567 1.1 itojun break;
568 1.1 itojun default:
569 1.1 itojun if (pflag)
570 1.2 itojun s_dst = rresvport_af(&nresvport, sa4->sa_family);
571 1.1 itojun else
572 1.2 itojun s_dst = socket(sa4->sa_family, SOCK_STREAM, 0);
573 1.1 itojun break;
574 1.1 itojun }
575 1.17 itojun if (s_dst < 0) {
576 1.1 itojun exit_failure("socket: %s", ERRSTR);
577 1.17 itojun /*NOTREACHED*/
578 1.17 itojun }
579 1.17 itojun
580 1.17 itojun if (conf->src.a.ss_family) {
581 1.17 itojun if (bind(s_dst, (struct sockaddr *)&conf->src.a,
582 1.17 itojun conf->src.a.ss_len) < 0) {
583 1.17 itojun exit_failure("bind: %s", ERRSTR);
584 1.17 itojun /*NOTREACHED*/
585 1.17 itojun }
586 1.17 itojun }
587 1.1 itojun
588 1.1 itojun error = setsockopt(s_dst, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
589 1.17 itojun if (error < 0) {
590 1.14 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", ERRSTR);
591 1.17 itojun /*NOTREACHED*/
592 1.17 itojun }
593 1.1 itojun
594 1.1 itojun error = setsockopt(s_src, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
595 1.17 itojun if (error < 0) {
596 1.14 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", ERRSTR);
597 1.17 itojun /*NOTREACHED*/
598 1.17 itojun }
599 1.1 itojun error = setsockopt(s_dst, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
600 1.17 itojun if (error < 0) {
601 1.14 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", ERRSTR);
602 1.17 itojun /*NOTREACHED*/
603 1.17 itojun }
604 1.1 itojun
605 1.8 itojun error = connect(s_dst, sa4, sa4->sa_len);
606 1.17 itojun if (error < 0) {
607 1.1 itojun exit_failure("connect: %s", ERRSTR);
608 1.17 itojun /*NOTREACHED*/
609 1.17 itojun }
610 1.1 itojun
611 1.1 itojun switch (hport) {
612 1.1 itojun case FTP_PORT:
613 1.1 itojun ftp_relay(s_src, s_dst);
614 1.1 itojun break;
615 1.1 itojun case RSH_PORT:
616 1.17 itojun syslog(LOG_WARNING,
617 1.17 itojun "WARINNG: it is insecure to relay rsh port");
618 1.1 itojun rsh_relay(s_src, s_dst);
619 1.1 itojun break;
620 1.17 itojun case RLOGIN_PORT:
621 1.17 itojun syslog(LOG_WARNING,
622 1.17 itojun "WARINNG: it is insecure to relay rlogin port");
623 1.17 itojun /*FALLTHROUGH*/
624 1.1 itojun default:
625 1.1 itojun tcp_relay(s_src, s_dst, service);
626 1.1 itojun break;
627 1.1 itojun }
628 1.1 itojun
629 1.1 itojun /* NOTREACHED */
630 1.1 itojun }
631 1.1 itojun
632 1.1 itojun /* 0: non faith, 1: faith */
633 1.1 itojun static int
634 1.1 itojun faith_prefix(struct sockaddr *dst)
635 1.1 itojun {
636 1.1 itojun #ifndef USE_ROUTE
637 1.1 itojun int mib[4], size;
638 1.1 itojun struct in6_addr faith_prefix;
639 1.1 itojun struct sockaddr_in6 *dst6 = (struct sockaddr_in *)dst;
640 1.1 itojun
641 1.1 itojun if (dst->sa_family != AF_INET6)
642 1.1 itojun return 0;
643 1.1 itojun
644 1.1 itojun mib[0] = CTL_NET;
645 1.1 itojun mib[1] = PF_INET6;
646 1.1 itojun mib[2] = IPPROTO_IPV6;
647 1.1 itojun mib[3] = IPV6CTL_FAITH_PREFIX;
648 1.1 itojun size = sizeof(struct in6_addr);
649 1.17 itojun if (sysctl(mib, 4, &faith_prefix, &size, NULL, 0) < 0) {
650 1.14 itojun exit_failure("sysctl: %s", ERRSTR);
651 1.17 itojun /*NOTREACHED*/
652 1.17 itojun }
653 1.1 itojun
654 1.2 itojun if (memcmp(dst, &faith_prefix,
655 1.2 itojun sizeof(struct in6_addr) - sizeof(struct in_addr) == 0) {
656 1.1 itojun return 1;
657 1.2 itojun }
658 1.1 itojun return 0;
659 1.1 itojun #else
660 1.1 itojun struct myaddrs *p;
661 1.1 itojun struct sockaddr_in6 *sin6;
662 1.1 itojun struct sockaddr_in *sin4;
663 1.3 itojun struct sockaddr_in6 *dst6;
664 1.3 itojun struct sockaddr_in *dst4;
665 1.3 itojun struct sockaddr_in dstmap;
666 1.3 itojun
667 1.4 itojun dst6 = (struct sockaddr_in6 *)dst;
668 1.3 itojun if (dst->sa_family == AF_INET6
669 1.3 itojun && IN6_IS_ADDR_V4MAPPED(&dst6->sin6_addr)) {
670 1.3 itojun /* ugly... */
671 1.3 itojun memset(&dstmap, 0, sizeof(dstmap));
672 1.3 itojun dstmap.sin_family = AF_INET;
673 1.3 itojun dstmap.sin_len = sizeof(dstmap);
674 1.3 itojun memcpy(&dstmap.sin_addr, &dst6->sin6_addr.s6_addr[12],
675 1.3 itojun sizeof(dstmap.sin_addr));
676 1.3 itojun dst = (struct sockaddr *)&dstmap;
677 1.3 itojun }
678 1.3 itojun
679 1.3 itojun dst6 = (struct sockaddr_in6 *)dst;
680 1.3 itojun dst4 = (struct sockaddr_in *)dst;
681 1.1 itojun
682 1.1 itojun for (p = myaddrs; p; p = p->next) {
683 1.1 itojun sin6 = (struct sockaddr_in6 *)p->addr;
684 1.1 itojun sin4 = (struct sockaddr_in *)p->addr;
685 1.1 itojun
686 1.3 itojun if (p->addr->sa_len != dst->sa_len
687 1.3 itojun || p->addr->sa_family != dst->sa_family)
688 1.3 itojun continue;
689 1.3 itojun
690 1.3 itojun switch (dst->sa_family) {
691 1.3 itojun case AF_INET6:
692 1.3 itojun if (sin6->sin6_scope_id == dst6->sin6_scope_id
693 1.5 itojun && IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &dst6->sin6_addr))
694 1.1 itojun return 0;
695 1.3 itojun break;
696 1.3 itojun case AF_INET:
697 1.3 itojun if (sin4->sin_addr.s_addr == dst4->sin_addr.s_addr)
698 1.6 itojun return 0;
699 1.3 itojun break;
700 1.1 itojun }
701 1.1 itojun }
702 1.1 itojun return 1;
703 1.1 itojun #endif
704 1.1 itojun }
705 1.1 itojun
706 1.1 itojun /* 0: non faith, 1: faith */
707 1.1 itojun static int
708 1.1 itojun map6to4(struct sockaddr_in6 *dst6, struct sockaddr_in *dst4)
709 1.1 itojun {
710 1.1 itojun memset(dst4, 0, sizeof(*dst4));
711 1.1 itojun dst4->sin_len = sizeof(*dst4);
712 1.1 itojun dst4->sin_family = AF_INET;
713 1.1 itojun dst4->sin_port = dst6->sin6_port;
714 1.2 itojun memcpy(&dst4->sin_addr, &dst6->sin6_addr.s6_addr[12],
715 1.2 itojun sizeof(dst4->sin_addr));
716 1.1 itojun
717 1.1 itojun if (dst4->sin_addr.s_addr == INADDR_ANY
718 1.1 itojun || dst4->sin_addr.s_addr == INADDR_BROADCAST
719 1.14 itojun || IN_MULTICAST(ntohl(dst4->sin_addr.s_addr)))
720 1.1 itojun return 0;
721 1.1 itojun
722 1.1 itojun return 1;
723 1.1 itojun }
724 1.1 itojun
725 1.1 itojun #ifdef FAITH4
726 1.1 itojun /* 0: non faith, 1: faith */
727 1.1 itojun static int
728 1.1 itojun map4to6(struct sockaddr_in *dst4, struct sockaddr_in6 *dst6)
729 1.1 itojun {
730 1.1 itojun char host[NI_MAXHOST];
731 1.1 itojun char serv[NI_MAXSERV];
732 1.1 itojun struct addrinfo hints, *res;
733 1.1 itojun int ai_errno;
734 1.1 itojun
735 1.1 itojun if (getnameinfo((struct sockaddr *)dst4, dst4->sin_len, host, sizeof(host),
736 1.1 itojun serv, sizeof(serv), NI_NAMEREQD|NI_NUMERICSERV) != 0)
737 1.1 itojun return 0;
738 1.1 itojun
739 1.1 itojun memset(&hints, 0, sizeof(hints));
740 1.1 itojun hints.ai_flags = 0;
741 1.1 itojun hints.ai_family = AF_INET6;
742 1.1 itojun hints.ai_socktype = SOCK_STREAM;
743 1.1 itojun hints.ai_protocol = 0;
744 1.1 itojun
745 1.1 itojun if ((ai_errno = getaddrinfo(host, serv, &hints, &res)) != 0) {
746 1.1 itojun syslog(LOG_INFO, "%s %s: %s", host, serv, gai_strerror(ai_errno));
747 1.1 itojun return 0;
748 1.1 itojun }
749 1.1 itojun
750 1.1 itojun memcpy(dst6, res->ai_addr, res->ai_addrlen);
751 1.1 itojun
752 1.1 itojun freeaddrinfo(res);
753 1.1 itojun
754 1.1 itojun return 1;
755 1.1 itojun }
756 1.1 itojun #endif /* FAITH4 */
757 1.1 itojun
758 1.1 itojun static void
759 1.1 itojun sig_child(int sig)
760 1.1 itojun {
761 1.1 itojun int status;
762 1.1 itojun pid_t pid;
763 1.1 itojun
764 1.1 itojun pid = wait3(&status, WNOHANG, (struct rusage *)0);
765 1.14 itojun if (pid && WEXITSTATUS(status))
766 1.1 itojun syslog(LOG_WARNING, "child %d exit status 0x%x", pid, status);
767 1.1 itojun }
768 1.1 itojun
769 1.1 itojun void
770 1.1 itojun sig_terminate(int sig)
771 1.1 itojun {
772 1.1 itojun syslog(LOG_INFO, "Terminating faith daemon");
773 1.1 itojun exit(EXIT_SUCCESS);
774 1.1 itojun }
775 1.1 itojun
776 1.1 itojun static void
777 1.1 itojun start_daemon(void)
778 1.1 itojun {
779 1.18 itojun #ifdef SA_NOCLDWAIT
780 1.18 itojun struct sigaction sa;
781 1.18 itojun #endif
782 1.18 itojun
783 1.1 itojun if (daemon(0, 0) == -1)
784 1.14 itojun exit_stderr("daemon: %s", ERRSTR);
785 1.1 itojun
786 1.18 itojun #ifdef SA_NOCLDWAIT
787 1.18 itojun memset(&sa, 0, sizeof(sa));
788 1.18 itojun sa.sa_handler = sig_child;
789 1.18 itojun sa.sa_flags = SA_NOCLDWAIT;
790 1.18 itojun sigemptyset(&sa.sa_mask);
791 1.18 itojun sigaction(SIGCHLD, &sa, (struct sigaction *)0);
792 1.18 itojun #else
793 1.17 itojun if (signal(SIGCHLD, sig_child) == SIG_ERR) {
794 1.1 itojun exit_failure("signal CHLD: %s", ERRSTR);
795 1.17 itojun /*NOTREACHED*/
796 1.17 itojun }
797 1.18 itojun #endif
798 1.1 itojun
799 1.17 itojun if (signal(SIGTERM, sig_terminate) == SIG_ERR) {
800 1.1 itojun exit_failure("signal TERM: %s", ERRSTR);
801 1.17 itojun /*NOTREACHED*/
802 1.17 itojun }
803 1.1 itojun }
804 1.1 itojun
805 1.14 itojun static void
806 1.14 itojun exit_stderr(const char *fmt, ...)
807 1.1 itojun {
808 1.1 itojun va_list ap;
809 1.1 itojun char buf[BUFSIZ];
810 1.1 itojun
811 1.1 itojun va_start(ap, fmt);
812 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
813 1.1 itojun va_end(ap);
814 1.17 itojun fprintf(stderr, "%s\n", buf);
815 1.1 itojun exit(EXIT_FAILURE);
816 1.1 itojun }
817 1.1 itojun
818 1.1 itojun void
819 1.1 itojun exit_failure(const char *fmt, ...)
820 1.1 itojun {
821 1.1 itojun va_list ap;
822 1.1 itojun char buf[BUFSIZ];
823 1.1 itojun
824 1.1 itojun va_start(ap, fmt);
825 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
826 1.1 itojun va_end(ap);
827 1.11 itojun syslog(LOG_ERR, "%s", buf);
828 1.1 itojun exit(EXIT_FAILURE);
829 1.1 itojun }
830 1.1 itojun
831 1.1 itojun void
832 1.1 itojun exit_success(const char *fmt, ...)
833 1.1 itojun {
834 1.1 itojun va_list ap;
835 1.1 itojun char buf[BUFSIZ];
836 1.1 itojun
837 1.1 itojun va_start(ap, fmt);
838 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
839 1.1 itojun va_end(ap);
840 1.11 itojun syslog(LOG_INFO, "%s", buf);
841 1.1 itojun exit(EXIT_SUCCESS);
842 1.1 itojun }
843 1.1 itojun
844 1.1 itojun #ifdef USE_ROUTE
845 1.9 itojun #ifndef HAVE_GETIFADDRS
846 1.1 itojun static unsigned int
847 1.1 itojun if_maxindex()
848 1.1 itojun {
849 1.1 itojun struct if_nameindex *p, *p0;
850 1.1 itojun unsigned int max = 0;
851 1.1 itojun
852 1.1 itojun p0 = if_nameindex();
853 1.1 itojun for (p = p0; p && p->if_index && p->if_name; p++) {
854 1.1 itojun if (max < p->if_index)
855 1.1 itojun max = p->if_index;
856 1.1 itojun }
857 1.1 itojun if_freenameindex(p0);
858 1.1 itojun return max;
859 1.1 itojun }
860 1.9 itojun #endif
861 1.1 itojun
862 1.1 itojun static void
863 1.1 itojun grab_myaddrs()
864 1.1 itojun {
865 1.9 itojun #ifdef HAVE_GETIFADDRS
866 1.9 itojun struct ifaddrs *ifap, *ifa;
867 1.9 itojun struct myaddrs *p;
868 1.9 itojun struct sockaddr_in6 *sin6;
869 1.9 itojun
870 1.9 itojun if (getifaddrs(&ifap) != 0) {
871 1.9 itojun exit_failure("getifaddrs");
872 1.9 itojun /*NOTREACHED*/
873 1.9 itojun }
874 1.9 itojun
875 1.9 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
876 1.9 itojun switch (ifa->ifa_addr->sa_family) {
877 1.9 itojun case AF_INET:
878 1.9 itojun case AF_INET6:
879 1.9 itojun break;
880 1.9 itojun default:
881 1.9 itojun continue;
882 1.9 itojun }
883 1.9 itojun
884 1.9 itojun p = (struct myaddrs *)malloc(sizeof(struct myaddrs) +
885 1.9 itojun ifa->ifa_addr->sa_len);
886 1.9 itojun if (!p) {
887 1.9 itojun exit_failure("not enough core");
888 1.9 itojun /*NOTREACHED*/
889 1.9 itojun }
890 1.9 itojun memcpy(p + 1, ifa->ifa_addr, ifa->ifa_addr->sa_len);
891 1.9 itojun p->next = myaddrs;
892 1.9 itojun p->addr = (struct sockaddr *)(p + 1);
893 1.9 itojun #ifdef __KAME__
894 1.9 itojun if (ifa->ifa_addr->sa_family == AF_INET6) {
895 1.9 itojun sin6 = (struct sockaddr_in6 *)p->addr;
896 1.9 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
897 1.9 itojun || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
898 1.9 itojun sin6->sin6_scope_id =
899 1.9 itojun ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
900 1.9 itojun sin6->sin6_addr.s6_addr[2] = 0;
901 1.9 itojun sin6->sin6_addr.s6_addr[3] = 0;
902 1.9 itojun }
903 1.9 itojun }
904 1.9 itojun #endif
905 1.9 itojun myaddrs = p;
906 1.9 itojun if (dflag) {
907 1.9 itojun char hbuf[NI_MAXHOST];
908 1.9 itojun getnameinfo(p->addr, p->addr->sa_len,
909 1.9 itojun hbuf, sizeof(hbuf), NULL, 0,
910 1.9 itojun NI_NUMERICHOST);
911 1.9 itojun syslog(LOG_INFO, "my interface: %s %s", hbuf,
912 1.9 itojun ifa->ifa_name);
913 1.9 itojun }
914 1.9 itojun }
915 1.9 itojun
916 1.9 itojun freeifaddrs(ifap);
917 1.9 itojun #else
918 1.1 itojun int s;
919 1.1 itojun unsigned int maxif;
920 1.1 itojun struct ifreq *iflist;
921 1.1 itojun struct ifconf ifconf;
922 1.9 itojun struct ifreq *ifr, *ifrp, *ifr_end;
923 1.1 itojun struct myaddrs *p;
924 1.1 itojun struct sockaddr_in6 *sin6;
925 1.9 itojun size_t siz;
926 1.9 itojun char ifrbuf[sizeof(struct ifreq) + 1024];
927 1.1 itojun
928 1.1 itojun maxif = if_maxindex() + 1;
929 1.1 itojun iflist = (struct ifreq *)malloc(maxif * BUFSIZ); /* XXX */
930 1.1 itojun if (!iflist) {
931 1.1 itojun exit_failure("not enough core");
932 1.1 itojun /*NOTREACHED*/
933 1.1 itojun }
934 1.1 itojun
935 1.1 itojun if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
936 1.1 itojun exit_failure("socket(SOCK_DGRAM)");
937 1.1 itojun /*NOTREACHED*/
938 1.1 itojun }
939 1.1 itojun memset(&ifconf, 0, sizeof(ifconf));
940 1.1 itojun ifconf.ifc_req = iflist;
941 1.1 itojun ifconf.ifc_len = maxif * BUFSIZ; /* XXX */
942 1.1 itojun if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) {
943 1.1 itojun exit_failure("ioctl(SIOCGIFCONF)");
944 1.1 itojun /*NOTREACHED*/
945 1.1 itojun }
946 1.1 itojun close(s);
947 1.1 itojun
948 1.1 itojun /* Look for this interface in the list */
949 1.1 itojun ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len);
950 1.9 itojun for (ifrp = ifconf.ifc_req;
951 1.9 itojun ifrp < ifr_end;
952 1.9 itojun ifrp = (struct ifreq *)((char *)ifrp + siz)) {
953 1.9 itojun memcpy(ifrbuf, ifrp, sizeof(*ifrp));
954 1.9 itojun ifr = (struct ifreq *)ifrbuf;
955 1.9 itojun siz = ifr->ifr_addr.sa_len;
956 1.9 itojun if (siz < sizeof(ifr->ifr_addr))
957 1.9 itojun siz = sizeof(ifr->ifr_addr);
958 1.9 itojun siz += (sizeof(*ifrp) - sizeof(ifr->ifr_addr));
959 1.9 itojun if (siz > sizeof(ifrbuf)) {
960 1.9 itojun /* ifr too big */
961 1.9 itojun break;
962 1.9 itojun }
963 1.9 itojun memcpy(ifrbuf, ifrp, siz);
964 1.9 itojun
965 1.1 itojun switch (ifr->ifr_addr.sa_family) {
966 1.1 itojun case AF_INET:
967 1.1 itojun case AF_INET6:
968 1.1 itojun p = (struct myaddrs *)malloc(sizeof(struct myaddrs)
969 1.1 itojun + ifr->ifr_addr.sa_len);
970 1.1 itojun if (!p) {
971 1.1 itojun exit_failure("not enough core");
972 1.1 itojun /*NOTREACHED*/
973 1.1 itojun }
974 1.1 itojun memcpy(p + 1, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
975 1.1 itojun p->next = myaddrs;
976 1.1 itojun p->addr = (struct sockaddr *)(p + 1);
977 1.1 itojun #ifdef __KAME__
978 1.1 itojun if (ifr->ifr_addr.sa_family == AF_INET6) {
979 1.1 itojun sin6 = (struct sockaddr_in6 *)p->addr;
980 1.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
981 1.1 itojun || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
982 1.1 itojun sin6->sin6_scope_id =
983 1.2 itojun ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
984 1.2 itojun sin6->sin6_addr.s6_addr[2] = 0;
985 1.2 itojun sin6->sin6_addr.s6_addr[3] = 0;
986 1.1 itojun }
987 1.1 itojun }
988 1.1 itojun #endif
989 1.1 itojun myaddrs = p;
990 1.1 itojun if (dflag) {
991 1.1 itojun char hbuf[NI_MAXHOST];
992 1.1 itojun getnameinfo(p->addr, p->addr->sa_len,
993 1.1 itojun hbuf, sizeof(hbuf), NULL, 0,
994 1.1 itojun NI_NUMERICHOST);
995 1.1 itojun syslog(LOG_INFO, "my interface: %s %s", hbuf, ifr->ifr_name);
996 1.1 itojun }
997 1.1 itojun break;
998 1.1 itojun default:
999 1.1 itojun break;
1000 1.1 itojun }
1001 1.1 itojun }
1002 1.1 itojun
1003 1.1 itojun free(iflist);
1004 1.9 itojun #endif
1005 1.1 itojun }
1006 1.1 itojun
1007 1.1 itojun static void
1008 1.1 itojun free_myaddrs()
1009 1.1 itojun {
1010 1.1 itojun struct myaddrs *p, *q;
1011 1.1 itojun
1012 1.1 itojun p = myaddrs;
1013 1.1 itojun while (p) {
1014 1.1 itojun q = p->next;
1015 1.1 itojun free(p);
1016 1.1 itojun p = q;
1017 1.1 itojun }
1018 1.1 itojun myaddrs = NULL;
1019 1.1 itojun }
1020 1.1 itojun
1021 1.1 itojun static void
1022 1.1 itojun update_myaddrs()
1023 1.1 itojun {
1024 1.1 itojun char msg[BUFSIZ];
1025 1.1 itojun int len;
1026 1.1 itojun struct rt_msghdr *rtm;
1027 1.1 itojun
1028 1.1 itojun len = read(sockfd, msg, sizeof(msg));
1029 1.1 itojun if (len < 0) {
1030 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) failed");
1031 1.1 itojun return;
1032 1.1 itojun }
1033 1.1 itojun rtm = (struct rt_msghdr *)msg;
1034 1.1 itojun if (len < 4 || len < rtm->rtm_msglen) {
1035 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) short read");
1036 1.1 itojun return;
1037 1.1 itojun }
1038 1.1 itojun if (rtm->rtm_version != RTM_VERSION) {
1039 1.1 itojun syslog(LOG_ERR, "routing socket version mismatch");
1040 1.1 itojun close(sockfd);
1041 1.1 itojun sockfd = 0;
1042 1.1 itojun return;
1043 1.1 itojun }
1044 1.1 itojun switch (rtm->rtm_type) {
1045 1.1 itojun case RTM_NEWADDR:
1046 1.1 itojun case RTM_DELADDR:
1047 1.1 itojun case RTM_IFINFO:
1048 1.1 itojun break;
1049 1.1 itojun default:
1050 1.1 itojun return;
1051 1.1 itojun }
1052 1.1 itojun /* XXX more filters here? */
1053 1.1 itojun
1054 1.1 itojun syslog(LOG_INFO, "update interface address list");
1055 1.1 itojun free_myaddrs();
1056 1.1 itojun grab_myaddrs();
1057 1.1 itojun }
1058 1.1 itojun #endif /*USE_ROUTE*/
1059 1.1 itojun
1060 1.1 itojun static void
1061 1.1 itojun usage()
1062 1.1 itojun {
1063 1.17 itojun fprintf(stderr, "usage: %s [-dp] [-f conf] service [serverpath [serverargs]]\n",
1064 1.1 itojun faithdname);
1065 1.1 itojun exit(0);
1066 1.1 itojun }
1067