2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)main.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/routed/main.c,v 1.11.2.1 2000/08/14 17:00:03 sheldonh Exp $
34 #include "pathnames.h"
41 naddr myaddr
; /* system address */
42 char myname
[MAXHOSTNAMELEN
+1];
46 int supplier
; /* supply or broadcast updates */
48 int ipforwarding
= 1; /* kernel forwarding on */
50 int default_gateway
; /* 1=advertise default */
52 int ridhosts
; /* 1=reduce host routes */
53 int mhome
; /* 1=want multi-homed host route */
54 int advertise_mhome
; /* 1=must continue advertising it */
55 int auth_ok
= 1; /* 1=ignore auth if we do not care */
57 struct timeval epoch
; /* when started */
58 struct timeval clk
, prev_clk
;
59 static int usec_fudge
;
60 struct timeval now
; /* current idea of time */
65 struct timeval next_bcast
; /* next general broadcast */
66 struct timeval no_flash
= { /* inhibit flash update */
67 EPOCH
+SUPPLY_INTERVAL
, 0
70 struct timeval flush_kern_timer
;
74 int rip_sock
= -1; /* RIP socket */
75 struct interface
*rip_sock_mcast
; /* current multicast interface */
76 int rt_sock
; /* routing socket */
80 static int get_rip_sock(naddr
, int);
81 static void timevalsub(struct timeval
*, struct timeval
*, struct timeval
*);
91 struct timeval wtime
, t2
;
95 struct interface
*ifp
;
100 /* Some shells are badly broken and send SIGHUP to backgrounded
103 signal(SIGHUP
, SIG_IGN
);
105 openlog("routed", LOG_PID
, LOG_DAEMON
);
108 gettimeofday(&clk
, 0);
111 epoch
.tv_sec
-= EPOCH
;
113 now_stale
= EPOCH
- STALE_TIME
;
114 now_expire
= EPOCH
- EXPIRE_TIME
;
115 now_garbage
= EPOCH
- GARBAGE_TIME
;
118 gethostname(myname
, sizeof(myname
)-1);
119 gethost(myname
, &myaddr
);
121 while ((n
= getopt(argc
, argv
, "sqdghmpAtvT:F:P:")) != -1) {
138 memset(&parm
, 0, sizeof(parm
));
139 parm
.parm_d_metric
= 1;
140 cp
= check_parms(&parm
);
142 msglog("bad -g: %s", cp
);
147 case 'h': /* suppress extra host routes */
151 case 'm': /* advertise host route */
152 mhome
= 1; /* on multi-homed hosts */
156 /* Ignore authentication if we do not care.
157 * Crazy as it is, that is what RFC 1723 requires.
170 case 'F': /* minimal routes for SLIP */
172 p
= strchr(optarg
,',');
173 if (p
&& *p
!= '\0') {
174 n
= (int)strtoul(p
+1, &q
, 0);
176 && n
<= HOPCNT_INFINITY
-1
180 if (!getnet(optarg
, &p_net
, &p_mask
)) {
181 msglog("bad network; \"-F %s\"",
185 memset(&parm
, 0, sizeof(parm
));
186 parm
.parm_net
= p_net
;
187 parm
.parm_mask
= p_mask
;
188 parm
.parm_d_metric
= n
;
189 cp
= check_parms(&parm
);
191 msglog("bad -F: %s", cp
);
195 /* handle arbitrary parameters.
198 cp
= parse_parms(q
, 0);
200 msglog("%s in \"-P %s\"", cp
, optarg
);
205 /* display version */
207 msglog("version 2.22");
217 if (tracename
== 0 && argc
>= 1) {
221 if (tracename
!= 0 && tracename
[0] == '\0')
225 logbad(0, "usage: routed [-sqdghmpAtv] [-T tracefile]"
226 " [-F net[,metric]] [-P parms]");
228 if (geteuid() != 0) {
231 logbad(0, "requires UID 0");
237 mib
[3] = IPCTL_FORWARDING
;
238 len
= sizeof(ipforwarding
);
239 if (sysctl(mib
, 4, &ipforwarding
, &len
, 0, 0) < 0)
240 LOGERR("sysctl(IPCTL_FORWARDING)");
244 msglog("-s incompatible with ipforwarding=0");
245 if (default_gateway
) {
246 msglog("-g incompatible with ipforwarding=0");
252 if (default_gateway
) {
253 if (supplier_set
&& !supplier
) {
254 msglog("-g and -q incompatible");
262 signal(SIGALRM
, sigalrm
);
264 signal(SIGHUP
, sigterm
); /* SIGHUP fatal during debugging */
265 signal(SIGTERM
, sigterm
);
266 signal(SIGINT
, sigterm
);
267 signal(SIGUSR1
, sigtrace_on
);
268 signal(SIGUSR2
, sigtrace_off
);
270 /* get into the background */
271 if (background
&& daemon(0, 1) < 0)
272 BADERR(0,"daemon()");
275 srandom((int)(clk
.tv_sec
^ clk
.tv_usec
^ mypid
));
277 /* prepare socket connected to the kernel.
279 rt_sock
= socket(AF_ROUTE
, SOCK_RAW
, 0);
281 BADERR(1,"rt_sock = socket()");
282 if (fcntl(rt_sock
, F_SETFL
, O_NONBLOCK
) == -1)
283 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno
));
285 if (setsockopt(rt_sock
, SOL_SOCKET
,SO_USELOOPBACK
,
286 &off
,sizeof(off
)) < 0)
287 LOGERR("setsockopt(SO_USELOOPBACK,0)");
292 if (tracename
!= 0) {
293 strncpy(inittracename
, tracename
, sizeof(inittracename
)-1);
294 set_tracefile(inittracename
, "%s", -1);
296 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
301 /* initialize radix tree */
304 /* Pick a random part of the second for our output to minimize
307 * Start broadcasting after hearing from other routers, and
308 * at a random time so a bunch of systems do not get synchronized
309 * after a power failure.
311 intvl_random(&next_bcast
, EPOCH
+MIN_WAITTIME
, EPOCH
+SUPPLY_INTERVAL
);
312 age_timer
.tv_usec
= next_bcast
.tv_usec
;
313 age_timer
.tv_sec
= EPOCH
+MIN_WAITTIME
;
314 rdisc_timer
= next_bcast
;
315 ifinit_timer
.tv_usec
= next_bcast
.tv_usec
;
317 /* Collect an initial view of the world by checking the interface
318 * configuration and the kludge file.
327 /* Now turn off stdio if not tracing */
328 if (new_tracelevel
== 0)
329 trace_close(background
);
331 /* Loop forever, listening and broadcasting.
335 gettimeofday(&clk
, 0);
336 if (prev_clk
.tv_sec
== clk
.tv_sec
337 && prev_clk
.tv_usec
== clk
.tv_usec
+usec_fudge
) {
338 /* Much of `routed` depends on time always advancing.
339 * On systems that do not guarantee that gettimeofday()
340 * produces unique timestamps even if called within
341 * a single tick, use trickery like that in classic
344 clk
.tv_usec
+= ++usec_fudge
;
349 timevalsub(&t2
, &clk
, &prev_clk
);
351 || t2
.tv_sec
> wtime
.tv_sec
+ 5) {
352 /* Deal with time changes before other
353 * housekeeping to keep everything straight.
358 trace_act("time changed by %d sec", (int)dt
);
362 timevalsub(&now
, &clk
, &epoch
);
363 now_stale
= now
.tv_sec
- STALE_TIME
;
364 now_expire
= now
.tv_sec
- EXPIRE_TIME
;
365 now_garbage
= now
.tv_sec
- GARBAGE_TIME
;
367 /* deal with signals that should affect tracing */
373 trace_off("exiting with signal %d", stopint
);
377 /* look for new or dead interfaces */
378 timevalsub(&wtime
, &ifinit_timer
, &now
);
379 if (wtime
.tv_sec
<= 0) {
386 /* Check the kernel table occassionally for mysteriously
389 timevalsub(&t2
, &flush_kern_timer
, &now
);
390 if (t2
.tv_sec
<= 0) {
392 flush_kern_timer
.tv_sec
= (now
.tv_sec
393 + CHECK_QUIET_INTERVAL
);
396 if (timercmp(&t2
, &wtime
, <))
399 /* If it is time, then broadcast our routes.
401 if (supplier
|| advertise_mhome
) {
402 timevalsub(&t2
, &next_bcast
, &now
);
403 if (t2
.tv_sec
<= 0) {
404 /* Synchronize the aging and broadcast
405 * timers to minimize awakenings
411 /* It is desirable to send routing updates
412 * regularly. So schedule the next update
413 * 30 seconds after the previous one was
414 * scheduled, instead of 30 seconds after
415 * the previous update was finished.
416 * Even if we just started after discovering
417 * a 2nd interface or were otherwise delayed,
418 * pick a 30-second aniversary of the
419 * original broadcast time.
421 n
= 1 + (0-t2
.tv_sec
)/SUPPLY_INTERVAL
;
422 next_bcast
.tv_sec
+= n
*SUPPLY_INTERVAL
;
427 if (timercmp(&t2
, &wtime
, <))
431 /* If we need a flash update, either do it now or
432 * set the delay to end when it is time.
434 * If we are within MIN_WAITTIME seconds of a full update,
439 && no_flash
.tv_sec
+MIN_WAITTIME
< next_bcast
.tv_sec
) {
440 /* accurate to the millisecond */
441 if (!timercmp(&no_flash
, &now
, >))
443 timevalsub(&t2
, &no_flash
, &now
);
444 if (timercmp(&t2
, &wtime
, <))
448 /* trigger the main aging timer.
450 timevalsub(&t2
, &age_timer
, &now
);
451 if (t2
.tv_sec
<= 0) {
455 if (timercmp(&t2
, &wtime
, <))
458 /* update the kernel routing table
460 timevalsub(&t2
, &need_kern
, &now
);
461 if (t2
.tv_sec
<= 0) {
465 if (timercmp(&t2
, &wtime
, <))
468 /* take care of router discovery,
469 * but do it in the correct the millisecond
471 if (!timercmp(&rdisc_timer
, &now
, >)) {
475 timevalsub(&t2
, &rdisc_timer
, &now
);
476 if (timercmp(&t2
, &wtime
, <))
480 /* wait for input or a timer to expire.
484 n
= select(sock_max
, &ibits
, 0, 0, &wtime
);
486 if (n
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
491 if (FD_ISSET(rt_sock
, &ibits
)) {
495 if (rdisc_sock
>= 0 && FD_ISSET(rdisc_sock
, &ibits
)) {
499 if (rip_sock
>= 0 && FD_ISSET(rip_sock
, &ibits
)) {
500 read_rip(rip_sock
, 0);
504 for (ifp
= ifnet
; n
> 0 && 0 != ifp
; ifp
= ifp
->int_next
) {
505 if (ifp
->int_rip_sock
>= 0
506 && FD_ISSET(ifp
->int_rip_sock
, &ibits
)) {
507 read_rip(ifp
->int_rip_sock
, ifp
);
517 sigalrm(__unused
int s
)
519 /* Historically, SIGALRM would cause the daemon to check for
520 * new and broken interfaces.
522 ifinit_timer
.tv_sec
= now
.tv_sec
;
523 trace_act("SIGALRM");
527 /* watch for fatal signals */
532 signal(sig
, SIG_DFL
); /* catch it only once */
539 struct interface
*ifp
;
545 FD_SET(rt_sock
, &fdbits
);
546 if (sock_max
<= rt_sock
)
547 sock_max
= rt_sock
+1;
549 FD_SET(rip_sock
, &fdbits
);
550 if (sock_max
<= rip_sock
)
551 sock_max
= rip_sock
+1;
553 for (ifp
= ifnet
; NULL
!= ifp
; ifp
= ifp
->int_next
) {
554 if (ifp
->int_rip_sock
>= 0) {
555 FD_SET(ifp
->int_rip_sock
, &fdbits
);
556 if (sock_max
<= ifp
->int_rip_sock
)
557 sock_max
= ifp
->int_rip_sock
+1;
560 if (rdisc_sock
>= 0) {
561 FD_SET(rdisc_sock
, &fdbits
);
562 if (sock_max
<= rdisc_sock
)
563 sock_max
= rdisc_sock
+1;
573 #define MIN_SOCKBUF (4*1024)
576 if (fcntl(sock
, F_SETFL
, O_NONBLOCK
) == -1)
577 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
578 name
, strerror(errno
));
580 if (setsockopt(sock
, SOL_SOCKET
,SO_BROADCAST
, &on
,sizeof(on
)) < 0)
581 msglog("setsockopt(%s,SO_BROADCAST): %s",
582 name
, strerror(errno
));
583 #ifdef USE_PASSIFNAME
585 if (setsockopt(sock
, SOL_SOCKET
, SO_PASSIFNAME
, &on
,sizeof(on
)) < 0)
586 msglog("setsockopt(%s,SO_PASSIFNAME): %s",
587 name
, strerror(errno
));
590 if (rbuf
>= MIN_SOCKBUF
) {
591 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
592 &rbuf
, sizeof(rbuf
)) < 0)
593 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
594 name
, rbuf
, strerror(errno
));
596 for (rbuf
= 60*1024; ; rbuf
-= 4096) {
597 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
598 &rbuf
, sizeof(rbuf
)) == 0) {
599 trace_act("RCVBUF=%d", rbuf
);
602 if (rbuf
< MIN_SOCKBUF
) {
603 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
604 name
, rbuf
, strerror(errno
));
614 static int /* <0 or file descriptor */
615 get_rip_sock(naddr addr
,
616 int serious
) /* 1=failure to bind is serious */
618 struct sockaddr_in in
;
623 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
624 BADERR(1,"rip_sock = socket()");
626 memset(&in
, 0, sizeof(in
));
628 in
.sin_len
= sizeof(in
);
630 in
.sin_family
= AF_INET
;
631 in
.sin_port
= htons(RIP_PORT
);
632 in
.sin_addr
.s_addr
= addr
;
633 if (bind(s
, (struct sockaddr
*)&in
, sizeof(in
)) < 0) {
635 BADERR(errno
!= EADDRINUSE
, "bind(rip_sock)");
638 fix_sock(s
,"rip_sock");
641 if (setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_TTL
,
642 &ttl
, sizeof(ttl
)) < 0)
643 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
649 /* turn off main RIP socket */
653 struct interface
*ifp
;
657 if (rip_sock
>= 0 && !mhome
) {
658 trace_act("turn off RIP");
663 /* get non-broadcast sockets to listen to queries.
665 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
666 if (ifp
->int_state
& IS_REMOTE
)
668 if (ifp
->int_rip_sock
< 0) {
669 addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
672 ifp
->int_rip_sock
= get_rip_sock(addr
, 0);
683 /* turn on RIP multicast input via an interface
686 rip_mcast_on(struct interface
*ifp
)
690 if (!IS_RIP_IN_OFF(ifp
->int_state
)
691 && (ifp
->int_if_flags
& IFF_MULTICAST
)
693 && !(ifp
->int_if_flags
& IFF_POINTOPOINT
)
695 && !(ifp
->int_state
& IS_ALIAS
)) {
696 m
.imr_multiaddr
.s_addr
= htonl(INADDR_RIP_GROUP
);
697 m
.imr_interface
.s_addr
= ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
700 if (setsockopt(rip_sock
,IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
702 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
707 /* Prepare socket used for RIP.
710 rip_on(struct interface
*ifp
)
712 /* If the main RIP socket is already alive, only start receiving
713 * multicasts for this interface.
721 /* If the main RIP socket is off and it makes sense to turn it on,
722 * then turn it on for all of the interfaces.
723 * It makes sense if either router discovery is off, or if
724 * router discover is on and at most one interface is doing RIP.
726 if (rip_interfaces
> 0 && (!rdisc_ok
|| rip_interfaces
> 1)) {
727 trace_act("turn on RIP");
729 /* Close all of the query sockets so that we can open
730 * the main socket. SO_REUSEPORT is not a solution,
731 * since that would let two daemons bind to the broadcast
734 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
735 if (ifp
->int_rip_sock
>= 0) {
736 close(ifp
->int_rip_sock
);
737 ifp
->int_rip_sock
= -1;
741 rip_sock
= get_rip_sock(INADDR_ANY
, 1);
742 rip_sock_mcast
= NULL
;
744 /* Do not advertise anything until we have heard something
746 if (next_bcast
.tv_sec
< now
.tv_sec
+MIN_WAITTIME
)
747 next_bcast
.tv_sec
= now
.tv_sec
+MIN_WAITTIME
;
749 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
750 ifp
->int_query_time
= NEVER
;
753 ifinit_timer
.tv_sec
= now
.tv_sec
;
755 } else if (ifp
!= NULL
756 && !(ifp
->int_state
& IS_REMOTE
)
757 && ifp
->int_rip_sock
< 0) {
758 /* RIP is off, so ensure there are sockets on which
759 * to listen for queries.
761 ifp
->int_rip_sock
= get_rip_sock(ifp
->int_addr
, 0);
768 /* die if malloc(3) fails
771 rtmalloc(size_t size
,
774 void *p
= malloc(size
);
776 logbad(1,"malloc(%lu) failed in %s", (u_long
)size
, msg
);
781 /* get a random instant in an interval
784 intvl_random(struct timeval
*tp
, /* put value here */
785 u_long lo
, /* value is after this second */
786 u_long hi
) /* and before this */
788 tp
->tv_sec
= (time_t)(hi
== lo
790 : (lo
+ random() % ((hi
- lo
))));
791 tp
->tv_usec
= random() % 1000000;
796 timevaladd(struct timeval
*t1
,
800 t1
->tv_sec
+= t2
->tv_sec
;
801 if ((t1
->tv_usec
+= t2
->tv_usec
) >= 1000000) {
803 t1
->tv_usec
-= 1000000;
811 timevalsub(struct timeval
*t1
,
815 t1
->tv_sec
= t2
->tv_sec
- t3
->tv_sec
;
816 if ((t1
->tv_usec
= t2
->tv_usec
- t3
->tv_usec
) < 0) {
818 t1
->tv_usec
+= 1000000;
823 /* put a message into the system log
826 msglog(const char *p
, ...)
833 vsyslog(LOG_ERR
, p
, args
);
836 if (ftrace
== stdout
)
837 fputs("routed: ", ftrace
);
838 vfprintf(ftrace
, p
, args
);
844 /* Put a message about a bad system into the system log if
845 * we have not complained about it recently.
847 * It is desirable to complain about all bad systems, but not too often.
848 * In the worst case, it is not practical to keep track of all bad systems.
849 * For example, there can be many systems with the wrong password.
852 msglim(struct msg_limit
*lim
, naddr addr
, const char *p
, ...)
856 struct msg_sub
*ms1
, *ms
;
861 /* look for the oldest slot in the table
862 * or the slot for the bad router.
864 ms
= ms1
= lim
->subs
;
865 for (i
= MSG_SUBJECT_N
; ; i
--, ms1
++) {
867 /* Reuse a slot at most once every 10 minutes.
869 if (lim
->reuse
> now
.tv_sec
) {
873 lim
->reuse
= now
.tv_sec
+ 10*60;
877 if (ms
->addr
== addr
) {
878 /* Repeat a complaint about a given system at
881 if (ms
->until
> now
.tv_sec
)
885 if (ms
->until
< ms1
->until
)
890 ms
->until
= now
.tv_sec
+ 60*60; /* 60 minutes */
893 for (p1
= p
; *p1
== ' '; p1
++)
895 vsyslog(LOG_ERR
, p1
, args
);
898 /* always display the message if tracing */
900 vfprintf(ftrace
, p
, args
);
907 logbad(int dump
, const char *p
, ...)
914 vsyslog(LOG_ERR
, p
, args
);
916 fputs("routed: ", stderr
);
917 vfprintf(stderr
, p
, args
);
918 fputs("; giving up\n",stderr
);