1 /* $NetBSD: main.c,v 1.40 2008/07/20 01:20:23 lukem Exp $ */
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "pathnames.h"
41 #if defined(__NetBSD__)
48 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
49 The Regents of the University of California. All rights reserved.");
51 __RCSID("$NetBSD: main.c,v 1.40 2008/07/20 01:20:23 lukem Exp $");
52 #elif defined(__FreeBSD__)
55 __RCSID("Revision: 2.27 ");
56 #ident "Revision: 2.27 "
61 naddr myaddr
; /* system address */
62 char myname
[MAXHOSTNAMELEN
+1];
66 int supplier
; /* supply or broadcast updates */
68 int ipforwarding
= 1; /* kernel forwarding on */
70 int default_gateway
; /* 1=advertise default */
72 int ridhosts
; /* 1=reduce host routes */
73 int mhome
; /* 1=want multi-homed host route */
74 int advertise_mhome
; /* 1=must continue advertising it */
75 int auth_ok
= 1; /* 1=ignore auth if we do not care */
77 struct timeval epoch
; /* when started */
78 struct timeval clk
, prev_clk
;
79 static int usec_fudge
;
80 struct timeval now
; /* current idea of time */
85 struct timeval next_bcast
; /* next general broadcast */
86 struct timeval no_flash
= { /* inhibit flash update */
87 EPOCH
+SUPPLY_INTERVAL
, 0
90 struct timeval flush_kern_timer
;
94 int rip_sock
= -1; /* RIP socket */
95 struct interface
*rip_sock_mcast
; /* current multicast interface */
96 int rt_sock
; /* routing socket */
100 static int get_rip_sock(naddr
, int);
101 static void timevalsub(struct timeval
*, struct timeval
*, struct timeval
*);
111 struct timeval wtime
, t2
;
113 fd_set
*ibitsp
= NULL
;
115 struct interface
*ifp
;
120 /* Some shells are badly broken and send SIGHUP to backgrounded
123 signal(SIGHUP
, SIG_IGN
);
125 openlog("routed", LOG_PID
, LOG_DAEMON
);
128 gettimeofday(&clk
, 0);
131 epoch
.tv_sec
-= EPOCH
;
133 now_stale
= EPOCH
- STALE_TIME
;
134 now_expire
= EPOCH
- EXPIRE_TIME
;
135 now_garbage
= EPOCH
- GARBAGE_TIME
;
138 (void)gethostname(myname
, sizeof(myname
) - 1);
139 (void)gethost(myname
, &myaddr
);
141 while ((n
= getopt(argc
, argv
, "sqdghmAtvT:F:P:")) != -1) {
158 memset(&parm
, 0, sizeof(parm
));
159 parm
.parm_d_metric
= 1;
160 cp
= check_parms(&parm
);
162 msglog("bad -g: %s", cp
);
167 case 'h': /* suppress extra host routes */
171 case 'm': /* advertise host route */
172 mhome
= 1; /* on multi-homed hosts */
176 /* Ignore authentication if we do not care.
177 * Crazy as it is, that is what RFC 1723 requires.
190 case 'F': /* minimal routes for SLIP */
192 p
= strchr(optarg
,',');
193 if (p
&& *p
!= '\0') {
194 n
= (int)strtoul(p
+1, &q
, 0);
196 && n
<= HOPCNT_INFINITY
-1
200 if (!getnet(optarg
, &p_net
, &p_mask
)) {
201 msglog("bad network; \"-F %s\"",
205 memset(&parm
, 0, sizeof(parm
));
206 parm
.parm_net
= p_net
;
207 parm
.parm_mask
= p_mask
;
208 parm
.parm_d_metric
= n
;
209 cp
= check_parms(&parm
);
211 msglog("bad -F: %s", cp
);
215 /* handle arbitrary parameters.
218 cp
= parse_parms(q
, 0);
220 msglog("%s in \"-P %s\"", cp
, optarg
);
225 /* display version */
227 msglog("version 2.32");
237 if (tracename
== 0 && argc
>= 1) {
241 if (tracename
!= 0 && tracename
[0] == '\0')
245 logbad(0, "usage: routed [-sqdghmAtv] [-T tracefile]"
246 " [-F net[/mask[,metric]]] [-P parms]");
248 if (geteuid() != 0) {
251 logbad(0, "requires UID 0");
257 mib
[3] = IPCTL_FORWARDING
;
258 len
= sizeof(ipforwarding
);
259 if (sysctl(mib
, 4, &ipforwarding
, &len
, 0, 0) < 0)
260 LOGERR("sysctl(IPCTL_FORWARDING)");
264 msglog("-s incompatible with ipforwarding=0");
265 if (default_gateway
) {
266 msglog("-g incompatible with ipforwarding=0");
272 if (default_gateway
) {
273 if (supplier_set
&& !supplier
) {
274 msglog("-g and -q incompatible");
282 signal(SIGALRM
, sigalrm
);
284 signal(SIGHUP
, sigterm
); /* SIGHUP fatal during debugging */
285 signal(SIGTERM
, sigterm
);
286 signal(SIGINT
, sigterm
);
287 signal(SIGUSR1
, sigtrace_on
);
288 signal(SIGUSR2
, sigtrace_off
);
290 /* get into the background */
292 if (0 > _daemonize(background
? 0 : (_DF_NOCHDIR
|_DF_NOFORK
),
293 STDIN_FILENO
, STDOUT_FILENO
, STDERR_FILENO
))
294 BADERR(0, "_daemonize()");
296 if (background
&& daemon(0, 1) < 0)
297 BADERR(0,"daemon()");
300 #if defined(__NetBSD__)
305 /* prepare socket connected to the kernel.
307 rt_sock
= socket(AF_ROUTE
, SOCK_RAW
, 0);
309 BADERR(1,"rt_sock = socket()");
310 if (fcntl(rt_sock
, F_SETFL
, O_NONBLOCK
) == -1)
311 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno
));
313 if (setsockopt(rt_sock
, SOL_SOCKET
,SO_USELOOPBACK
,
314 &off
,sizeof(off
)) < 0)
315 LOGERR("setsockopt(SO_USELOOPBACK,0)");
320 if (tracename
!= 0) {
321 strlcpy(inittracename
, tracename
, sizeof(inittracename
));
322 set_tracefile(inittracename
, "%s", -1);
324 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
329 /* initialize radix tree */
332 /* Pick a random part of the second for our output to minimize
335 * Start broadcasting after hearing from other routers, and
336 * at a random time so a bunch of systems do not get synchronized
337 * after a power failure.
339 intvl_random(&next_bcast
, EPOCH
+MIN_WAITTIME
, EPOCH
+SUPPLY_INTERVAL
);
340 age_timer
.tv_usec
= next_bcast
.tv_usec
;
341 age_timer
.tv_sec
= EPOCH
+MIN_WAITTIME
;
342 rdisc_timer
= next_bcast
;
343 ifinit_timer
.tv_usec
= next_bcast
.tv_usec
;
345 /* Collect an initial view of the world by checking the interface
346 * configuration and the kludge file.
355 /* Now turn off stdio if not tracing */
356 if (new_tracelevel
== 0)
357 trace_close(background
);
359 /* Loop forever, listening and broadcasting.
363 gettimeofday(&clk
, 0);
364 if (prev_clk
.tv_sec
== clk
.tv_sec
365 && prev_clk
.tv_usec
== clk
.tv_usec
+usec_fudge
) {
366 /* Much of `routed` depends on time always advancing.
367 * On systems that do not guarantee that gettimeofday()
368 * produces unique timestamps even if called within
369 * a single tick, use trickery like that in classic
372 clk
.tv_usec
+= ++usec_fudge
;
377 timevalsub(&t2
, &clk
, &prev_clk
);
379 || t2
.tv_sec
> wtime
.tv_sec
+ 5) {
380 /* Deal with time changes before other
381 * housekeeping to keep everything straight.
386 trace_act("time changed by %d sec", (int)dt
);
390 timevalsub(&now
, &clk
, &epoch
);
391 now_stale
= now
.tv_sec
- STALE_TIME
;
392 now_expire
= now
.tv_sec
- EXPIRE_TIME
;
393 now_garbage
= now
.tv_sec
- GARBAGE_TIME
;
395 /* deal with signals that should affect tracing */
401 trace_off("exiting with signal %d", stopint
);
405 /* look for new or dead interfaces */
406 timevalsub(&wtime
, &ifinit_timer
, &now
);
407 if (wtime
.tv_sec
<= 0) {
414 /* Check the kernel table occassionally for mysteriously
417 timevalsub(&t2
, &flush_kern_timer
, &now
);
418 if (t2
.tv_sec
<= 0) {
420 flush_kern_timer
.tv_sec
= (now
.tv_sec
421 + CHECK_QUIET_INTERVAL
);
424 if (timercmp(&t2
, &wtime
, <))
427 /* If it is time, then broadcast our routes.
429 if (supplier
|| advertise_mhome
) {
430 timevalsub(&t2
, &next_bcast
, &now
);
431 if (t2
.tv_sec
<= 0) {
432 /* Synchronize the aging and broadcast
433 * timers to minimize awakenings
439 /* It is desirable to send routing updates
440 * regularly. So schedule the next update
441 * 30 seconds after the previous one was
442 * scheduled, instead of 30 seconds after
443 * the previous update was finished.
444 * Even if we just started after discovering
445 * a 2nd interface or were otherwise delayed,
446 * pick a 30-second aniversary of the
447 * original broadcast time.
449 n
= 1 + (0-t2
.tv_sec
)/SUPPLY_INTERVAL
;
450 next_bcast
.tv_sec
+= n
*SUPPLY_INTERVAL
;
455 if (timercmp(&t2
, &wtime
, <))
459 /* If we need a flash update, either do it now or
460 * set the delay to end when it is time.
462 * If we are within MIN_WAITTIME seconds of a full update,
467 && no_flash
.tv_sec
+MIN_WAITTIME
< next_bcast
.tv_sec
) {
468 /* accurate to the millisecond */
469 if (!timercmp(&no_flash
, &now
, >))
471 timevalsub(&t2
, &no_flash
, &now
);
472 if (timercmp(&t2
, &wtime
, <))
476 /* trigger the main aging timer.
478 timevalsub(&t2
, &age_timer
, &now
);
479 if (t2
.tv_sec
<= 0) {
483 if (timercmp(&t2
, &wtime
, <))
486 /* update the kernel routing table
488 timevalsub(&t2
, &need_kern
, &now
);
489 if (t2
.tv_sec
<= 0) {
493 if (timercmp(&t2
, &wtime
, <))
496 /* take care of router discovery,
497 * but do it in the correct the millisecond
499 if (!timercmp(&rdisc_timer
, &now
, >)) {
503 timevalsub(&t2
, &rdisc_timer
, &now
);
504 if (timercmp(&t2
, &wtime
, <))
508 /* wait for input or a timer to expire.
513 ibitsp
= (fd_set
*)calloc(howmany(sock_max
, NFDBITS
),
517 memcpy(ibitsp
, fdbitsp
, howmany(sock_max
, NFDBITS
) *
519 n
= select(sock_max
, ibitsp
, 0, 0, &wtime
);
521 if (n
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
526 if (FD_ISSET(rt_sock
, ibitsp
)) {
530 if (rdisc_sock
>= 0 && FD_ISSET(rdisc_sock
, ibitsp
)) {
534 if (rip_sock
>= 0 && FD_ISSET(rip_sock
, ibitsp
)) {
535 read_rip(rip_sock
, 0);
539 for (ifp
= ifnet
; n
> 0 && 0 != ifp
; ifp
= ifp
->int_next
) {
540 if (ifp
->int_rip_sock
>= 0
541 && FD_ISSET(ifp
->int_rip_sock
, ibitsp
)) {
542 read_rip(ifp
->int_rip_sock
, ifp
);
552 sigalrm(int s UNUSED
)
554 /* Historically, SIGALRM would cause the daemon to check for
555 * new and broken interfaces.
557 ifinit_timer
.tv_sec
= now
.tv_sec
;
558 trace_act("SIGALRM");
562 /* watch for fatal signals */
567 (void)signal(sig
, SIG_DFL
); /* catch it only once */
574 struct interface
*ifp
;
578 if (sock_max
<= rt_sock
)
579 sock_max
= rt_sock
+ 1;
581 if (sock_max
<= rip_sock
)
582 sock_max
= rip_sock
+ 1;
583 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
584 if (ifp
->int_rip_sock
>= 0)
585 if (sock_max
<= ifp
->int_rip_sock
)
586 sock_max
= ifp
->int_rip_sock
+ 1;
589 if (sock_max
<= rdisc_sock
)
590 sock_max
= rdisc_sock
+ 1;
594 fdbitsp
= (fd_set
*)calloc(howmany(sock_max
, NFDBITS
),
599 FD_SET(rt_sock
, fdbitsp
);
601 FD_SET(rip_sock
, fdbitsp
);
602 for (ifp
= ifnet
; 0 != ifp
; ifp
= ifp
->int_next
) {
603 if (ifp
->int_rip_sock
>= 0)
604 FD_SET(ifp
->int_rip_sock
, fdbitsp
);
607 FD_SET(rdisc_sock
, fdbitsp
);
616 #define MIN_SOCKBUF (4*1024)
619 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) == -1)
620 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
621 name
, strerror(errno
));
623 if (setsockopt(sock
, SOL_SOCKET
,SO_BROADCAST
, &on
,sizeof(on
)) < 0)
624 msglog("setsockopt(%s,SO_BROADCAST): %s",
625 name
, strerror(errno
));
626 #ifdef USE_PASSIFNAME
628 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSIFNAME
, &on
,sizeof(on
)) < 0)
629 msglog("setsockopt(%s,SO_PASSIFNAME): %s",
630 name
, strerror(errno
));
633 if (rbuf
>= MIN_SOCKBUF
) {
634 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
635 &rbuf
, sizeof(rbuf
)) < 0)
636 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
637 name
, rbuf
, strerror(errno
));
639 for (rbuf
= 60*1024; ; rbuf
-= 4096) {
640 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
641 &rbuf
, sizeof(rbuf
)) == 0) {
642 trace_act("RCVBUF=%d", rbuf
);
645 if (rbuf
< MIN_SOCKBUF
) {
646 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
647 name
, rbuf
, strerror(errno
));
657 static int /* <0 or file descriptor */
658 get_rip_sock(naddr addr
,
659 int serious
) /* 1=failure to bind is serious */
661 struct sockaddr_in rsin
;
666 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
667 BADERR(1,"rip_sock = socket()");
669 memset(&rsin
, 0, sizeof(rsin
));
671 rsin
.sin_len
= sizeof(rsin
);
673 rsin
.sin_family
= AF_INET
;
674 rsin
.sin_port
= htons(RIP_PORT
);
675 rsin
.sin_addr
.s_addr
= addr
;
676 if (bind(s
, (struct sockaddr
*)&rsin
, sizeof(rsin
)) < 0) {
678 BADERR(errno
!= EADDRINUSE
, "bind(rip_sock)");
681 fix_sock(s
,"rip_sock");
684 if (setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
,
685 &ttl
, sizeof(ttl
)) < 0)
686 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
692 /* turn off main RIP socket */
696 struct interface
*ifp
;
700 if (rip_sock
>= 0 && !mhome
) {
701 trace_act("turn off RIP");
703 (void)close(rip_sock
);
706 /* get non-broadcast sockets to listen to queries.
708 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
709 if (ifp
->int_state
& IS_REMOTE
)
711 if (ifp
->int_rip_sock
< 0) {
712 addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
715 ifp
->int_rip_sock
= get_rip_sock(addr
, 0);
726 /* turn on RIP multicast input via an interface
729 rip_mcast_on(struct interface
*ifp
)
733 if (!IS_RIP_IN_OFF(ifp
->int_state
)
734 && (ifp
->int_if_flags
& IFF_MULTICAST
)
736 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
738 && !(ifp
->int_state
& IS_ALIAS
)) {
739 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
741 m
.imr_interface
.s_addr
= htonl(ifp
->int_index
);
743 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
747 if (setsockopt(rip_sock
,IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
749 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
754 /* Prepare socket used for RIP.
757 rip_on(struct interface
*ifp
)
759 /* If the main RIP socket is already alive, only start receiving
760 * multicasts for this interface.
768 /* If the main RIP socket is off and it makes sense to turn it on,
769 * then turn it on for all of the interfaces.
770 * It makes sense if either router discovery is off, or if
771 * router discover is on and at most one interface is doing RIP.
773 if (rip_interfaces
> 0 && (!rdisc_ok
|| rip_interfaces
> 1)) {
774 trace_act("turn on RIP");
776 /* Close all of the query sockets so that we can open
777 * the main socket. SO_REUSEPORT is not a solution,
778 * since that would let two daemons bind to the broadcast
781 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
782 if (ifp
->int_rip_sock
>= 0) {
783 (void)close(ifp
->int_rip_sock
);
784 ifp
->int_rip_sock
= -1;
788 rip_sock
= get_rip_sock(INADDR_ANY
, 1);
791 /* Do not advertise anything until we have heard something
793 if (next_bcast
.tv_sec
< now
.tv_sec
+MIN_WAITTIME
)
794 next_bcast
.tv_sec
= now
.tv_sec
+MIN_WAITTIME
;
796 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
797 ifp
->int_query_time
= NEVER
;
800 ifinit_timer
.tv_sec
= now
.tv_sec
;
803 && !(ifp
->int_state
& IS_REMOTE
)
804 && ifp
->int_rip_sock
< 0) {
805 /* RIP is off, so ensure there are sockets on which
806 * to listen for queries.
808 ifp
->int_rip_sock
= get_rip_sock(ifp
->int_addr
, 0);
815 /* die if malloc(3) fails
818 rtmalloc(size_t size
,
821 void *p
= malloc(size
);
823 logbad(1,"malloc(%lu) failed in %s", (u_long
)size
, msg
);
828 /* get a random instant in an interval
831 intvl_random(struct timeval
*tp
, /* put value here */
832 u_long lo
, /* value is after this second */
833 u_long hi
) /* and before this */
835 tp
->tv_sec
= (time_t)(hi
== lo
837 : (lo
+ arc4random() % ((hi
- lo
))));
838 tp
->tv_usec
= arc4random() % 1000000;
843 timevaladd(struct timeval
*t1
,
847 t1
->tv_sec
+= t2
->tv_sec
;
848 if ((t1
->tv_usec
+= t2
->tv_usec
) >= 1000000) {
850 t1
->tv_usec
-= 1000000;
858 timevalsub(struct timeval
*t1
,
862 t1
->tv_sec
= t2
->tv_sec
- t3
->tv_sec
;
863 if ((t1
->tv_usec
= t2
->tv_usec
- t3
->tv_usec
) < 0) {
865 t1
->tv_usec
+= 1000000;
870 /* put a message into the system log
873 msglog(const char *p
, ...)
880 vsyslog(LOG_ERR
, p
, args
);
884 if (ftrace
== stdout
)
885 (void)fputs("routed: ", ftrace
);
887 (void)vfprintf(ftrace
, p
, args
);
889 (void)fputc('\n', ftrace
);
894 /* Put a message about a bad system into the system log if
895 * we have not complained about it recently.
897 * It is desirable to complain about all bad systems, but not too often.
898 * In the worst case, it is not practical to keep track of all bad systems.
899 * For example, there can be many systems with the wrong password.
902 msglim(struct msg_limit
*lim
, naddr addr
, const char *p
, ...)
906 struct msg_sub
*ms1
, *ms
;
909 /* look for the oldest slot in the table
910 * or the slot for the bad router.
912 ms
= ms1
= lim
->subs
;
913 for (i
= MSG_SUBJECT_N
; ; i
--, ms1
++) {
915 /* Reuse a slot at most once every 10 minutes.
917 if (lim
->reuse
> now
.tv_sec
) {
921 lim
->reuse
= now
.tv_sec
+ 10*60;
925 if (ms
->addr
== addr
) {
926 /* Repeat a complaint about a given system at
929 if (ms
->until
> now
.tv_sec
)
933 if (ms
->until
< ms1
->until
)
938 ms
->until
= now
.tv_sec
+ 60*60; /* 60 minutes */
941 for (p1
= p
; *p1
== ' '; p1
++)
944 vsyslog(LOG_ERR
, p1
, args
);
948 /* always display the message if tracing */
951 (void)vfprintf(ftrace
, p
, args
);
952 (void)fputc('\n', ftrace
);
959 logbad(int dump
, const char *p
, ...)
966 vsyslog(LOG_ERR
, p
, args
);
969 (void)fputs("routed: ", stderr
);
971 (void)vfprintf(stderr
, p
, args
);
973 (void)fputs("; giving up\n",stderr
);
974 (void)fflush(stderr
);