2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "pathnames.h"
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993 "
39 "The Regents of the University of California."
40 " All rights reserved.");
43 naddr myaddr; /* system address */
44 static char myname[MAXHOSTNAMELEN+1];
48 int supplier; /* supply or broadcast updates */
50 static int ipforwarding = 1; /* kernel forwarding on */
52 static int default_gateway; /* 1=advertise default */
53 static int background = 1;
54 int ridhosts; /* 1=reduce host routes */
55 int mhome; /* 1=want multi-homed host route */
56 int advertise_mhome; /* 1=must continue advertising it */
57 int auth_ok = 1; /* 1=ignore auth if we do not care */
58 int insecure; /* Reply to special queries or not */
60 struct timeval epoch; /* when started */
62 static struct timeval prev_clk;
63 static int usec_fudge;
64 struct timeval now; /* current idea of time */
69 static struct timeval next_bcast; /* next general broadcast */
70 struct timeval no_flash = { /* inhibit flash update */
71 EPOCH+SUPPLY_INTERVAL, 0
74 static struct timeval flush_kern_timer;
78 int rip_sock = -1; /* RIP socket */
79 const struct interface *rip_sock_mcast; /* current multicast interface */
80 int rt_sock; /* routing socket */
84 static int get_rip_sock(naddr, int);
85 static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
86 static void sigalrm(int s UNUSED);
87 static void sigterm(int sig);
97 struct timeval wtime, t2;
101 struct interface *ifp;
106 /* Some shells are badly broken and send SIGHUP to backgrounded
109 signal(SIGHUP, SIG_IGN);
111 openlog("routed", LOG_PID, LOG_DAEMON);
114 gettimeofday(&clk, 0);
117 epoch.tv_sec -= EPOCH;
119 now_stale = EPOCH - STALE_TIME;
120 now_expire = EPOCH - EXPIRE_TIME;
121 now_garbage = EPOCH - GARBAGE_TIME;
124 (void)gethostname(myname, sizeof(myname)-1);
125 (void)gethost(myname, &myaddr);
127 while ((n = getopt(argc, argv, "isqdghmAtvT:F:P:")) != -1) {
147 memset(&parm, 0, sizeof(parm));
148 parm.parm_d_metric = 1;
149 cp = check_parms(&parm);
151 msglog("bad -g: %s", cp);
156 case 'h': /* suppress extra host routes */
160 case 'm': /* advertise host route */
161 mhome = 1; /* on multi-homed hosts */
165 /* Ignore authentication if we do not care.
166 * Crazy as it is, that is what RFC 1723 requires.
179 case 'F': /* minimal routes for SLIP */
181 p = strchr(optarg,',');
182 if (p && *p != '\0') {
183 n = (int)strtoul(p+1, &q, 0);
185 && n <= HOPCNT_INFINITY-1
189 if (!getnet(optarg, &p_net, &p_mask)) {
190 msglog("bad network; \"-F %s\"",
194 memset(&parm, 0, sizeof(parm));
195 parm.parm_net = p_net;
196 parm.parm_mask = p_mask;
197 parm.parm_d_metric = n;
198 cp = check_parms(&parm);
200 msglog("bad -F: %s", cp);
204 /* handle arbitrary parameters.
207 cp = parse_parms(q, 0);
209 msglog("%s in \"-P %s\"", cp, optarg);
214 /* display version */
216 msglog("version 2.31");
226 if (tracename == 0 && argc >= 1) {
230 if (tracename != 0 && tracename[0] == '\0')
234 logbad(0, "usage: routed [-sqdghmAtv] [-T tracefile]"
235 " [-F net[,metric]] [-P parms]");
237 if (geteuid() != 0) {
240 logbad(0, "requires UID 0");
246 mib[3] = IPCTL_FORWARDING;
247 len = sizeof(ipforwarding);
248 if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
249 LOGERR("sysctl(IPCTL_FORWARDING)");
253 msglog("-s incompatible with ipforwarding=0");
254 if (default_gateway) {
255 msglog("-g incompatible with ipforwarding=0");
261 if (default_gateway) {
262 if (supplier_set && !supplier) {
263 msglog("-g and -q incompatible");
271 signal(SIGALRM, sigalrm);
273 signal(SIGHUP, sigterm); /* SIGHUP fatal during debugging */
274 signal(SIGTERM, sigterm);
275 signal(SIGINT, sigterm);
276 signal(SIGUSR1, sigtrace_on);
277 signal(SIGUSR2, sigtrace_off);
279 /* get into the background */
280 if (background && daemon(0, 1) < 0)
281 BADERR(0,"daemon()");
285 /* prepare socket connected to the kernel.
287 rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
289 BADERR(1,"rt_sock = socket()");
290 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
291 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
293 if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
294 &off,sizeof(off)) < 0)
295 LOGERR("setsockopt(SO_USELOOPBACK,0)");
300 if (tracename != 0) {
301 strncpy(inittracename, tracename, sizeof(inittracename)-1);
302 set_tracefile(inittracename, "%s", -1);
304 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
309 /* initialize radix tree */
312 /* Pick a random part of the second for our output to minimize
315 * Start broadcasting after hearing from other routers, and
316 * at a random time so a bunch of systems do not get synchronized
317 * after a power failure.
319 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
320 age_timer.tv_usec = next_bcast.tv_usec;
321 age_timer.tv_sec = EPOCH+MIN_WAITTIME;
322 rdisc_timer = next_bcast;
323 ifinit_timer.tv_usec = next_bcast.tv_usec;
325 /* Collect an initial view of the world by checking the interface
326 * configuration and the kludge file.
335 /* Now turn off stdio if not tracing */
336 if (new_tracelevel == 0)
337 trace_close(background);
339 /* Loop forever, listening and broadcasting.
343 gettimeofday(&clk, 0);
344 if (prev_clk.tv_sec == clk.tv_sec
345 && prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
346 /* Much of `routed` depends on time always advancing.
347 * On systems that do not guarantee that gettimeofday()
348 * produces unique timestamps even if called within
349 * a single tick, use trickery like that in classic
352 clk.tv_usec += ++usec_fudge;
357 timevalsub(&t2, &clk, &prev_clk);
359 || t2.tv_sec > wtime.tv_sec + 5) {
360 /* Deal with time changes before other
361 * housekeeping to keep everything straight.
366 trace_act("time changed by %d sec", (int)dt);
370 timevalsub(&now, &clk, &epoch);
371 now_stale = now.tv_sec - STALE_TIME;
372 now_expire = now.tv_sec - EXPIRE_TIME;
373 now_garbage = now.tv_sec - GARBAGE_TIME;
375 /* deal with signals that should affect tracing */
381 trace_off("exiting with signal %d", stopint);
385 /* look for new or dead interfaces */
386 timevalsub(&wtime, &ifinit_timer, &now);
387 if (wtime.tv_sec <= 0) {
394 /* Check the kernel table occasionally for mysteriously
397 timevalsub(&t2, &flush_kern_timer, &now);
398 if (t2.tv_sec <= 0) {
400 flush_kern_timer.tv_sec = (now.tv_sec
401 + CHECK_QUIET_INTERVAL);
404 if (timercmp(&t2, &wtime, <))
407 /* If it is time, then broadcast our routes.
409 if (supplier || advertise_mhome) {
410 timevalsub(&t2, &next_bcast, &now);
411 if (t2.tv_sec <= 0) {
412 /* Synchronize the aging and broadcast
413 * timers to minimize awakenings
419 /* It is desirable to send routing updates
420 * regularly. So schedule the next update
421 * 30 seconds after the previous one was
422 * scheduled, instead of 30 seconds after
423 * the previous update was finished.
424 * Even if we just started after discovering
425 * a 2nd interface or were otherwise delayed,
426 * pick a 30-second anniversary of the
427 * original broadcast time.
429 n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
430 next_bcast.tv_sec += n*SUPPLY_INTERVAL;
435 if (timercmp(&t2, &wtime, <))
439 /* If we need a flash update, either do it now or
440 * set the delay to end when it is time.
442 * If we are within MIN_WAITTIME seconds of a full update,
447 && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
448 /* accurate to the millisecond */
449 if (!timercmp(&no_flash, &now, >))
451 timevalsub(&t2, &no_flash, &now);
452 if (timercmp(&t2, &wtime, <))
456 /* trigger the main aging timer.
458 timevalsub(&t2, &age_timer, &now);
459 if (t2.tv_sec <= 0) {
463 if (timercmp(&t2, &wtime, <))
466 /* update the kernel routing table
468 timevalsub(&t2, &need_kern, &now);
469 if (t2.tv_sec <= 0) {
473 if (timercmp(&t2, &wtime, <))
476 /* take care of router discovery,
477 * but do it in the correct the millisecond
479 if (!timercmp(&rdisc_timer, &now, >)) {
483 timevalsub(&t2, &rdisc_timer, &now);
484 if (timercmp(&t2, &wtime, <))
488 /* wait for input or a timer to expire.
492 n = select(sock_max, &ibits, 0, 0, &wtime);
494 if (n < 0 && errno != EINTR && errno != EAGAIN)
499 if (FD_ISSET(rt_sock, &ibits)) {
503 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
507 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
508 read_rip(rip_sock, 0);
512 LIST_FOREACH(ifp, &ifnet, int_list) {
515 if (ifp->int_rip_sock >= 0
516 && FD_ISSET(ifp->int_rip_sock, &ibits)) {
517 read_rip(ifp->int_rip_sock, ifp);
527 sigalrm(int s UNUSED)
529 /* Historically, SIGALRM would cause the daemon to check for
530 * new and broken interfaces.
532 ifinit_timer.tv_sec = now.tv_sec;
533 trace_act("SIGALRM");
537 /* watch for fatal signals */
542 (void)signal(sig, SIG_DFL); /* catch it only once */
549 struct interface *ifp;
555 FD_SET(rt_sock, &fdbits);
556 if (sock_max <= rt_sock)
557 sock_max = rt_sock+1;
559 FD_SET(rip_sock, &fdbits);
560 if (sock_max <= rip_sock)
561 sock_max = rip_sock+1;
563 LIST_FOREACH(ifp, &ifnet, int_list) {
564 if (ifp->int_rip_sock >= 0) {
565 FD_SET(ifp->int_rip_sock, &fdbits);
566 if (sock_max <= ifp->int_rip_sock)
567 sock_max = ifp->int_rip_sock+1;
570 if (rdisc_sock >= 0) {
571 FD_SET(rdisc_sock, &fdbits);
572 if (sock_max <= rdisc_sock)
573 sock_max = rdisc_sock+1;
583 #define MIN_SOCKBUF (4*1024)
586 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
587 logbad(1, "fcntl(%s) O_NONBLOCK: %s",
588 name, strerror(errno));
590 if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0)
591 msglog("setsockopt(%s,SO_BROADCAST): %s",
592 name, strerror(errno));
593 #ifdef USE_PASSIFNAME
595 if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0)
596 msglog("setsockopt(%s,SO_PASSIFNAME): %s",
597 name, strerror(errno));
600 if (rbuf >= MIN_SOCKBUF) {
601 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
602 &rbuf, sizeof(rbuf)) < 0)
603 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
604 name, rbuf, strerror(errno));
606 for (rbuf = 60*1024; ; rbuf -= 4096) {
607 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
608 &rbuf, sizeof(rbuf)) == 0) {
609 trace_act("RCVBUF=%d", rbuf);
612 if (rbuf < MIN_SOCKBUF) {
613 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
614 name, rbuf, strerror(errno));
624 static int /* <0 or file descriptor */
625 get_rip_sock(naddr addr,
626 int serious) /* 1=failure to bind is serious */
628 struct sockaddr_in rsin;
633 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
634 BADERR(1,"rip_sock = socket()");
636 memset(&rsin, 0, sizeof(rsin));
638 rsin.sin_len = sizeof(rsin);
640 rsin.sin_family = AF_INET;
641 rsin.sin_port = htons(RIP_PORT);
642 rsin.sin_addr.s_addr = addr;
643 if (bind(s, (struct sockaddr *)&rsin, sizeof(rsin)) < 0) {
645 BADERR(errno != EADDRINUSE, "bind(rip_sock)");
649 fix_sock(s,"rip_sock");
652 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
653 &ttl, sizeof(ttl)) < 0)
654 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
660 /* turn off main RIP socket */
664 struct interface *ifp;
668 if (rip_sock >= 0 && !mhome) {
669 trace_act("turn off RIP");
671 (void)close(rip_sock);
674 /* get non-broadcast sockets to listen to queries.
676 LIST_FOREACH(ifp, &ifnet, int_list) {
677 if (ifp->int_state & IS_REMOTE)
679 if (ifp->int_rip_sock < 0) {
680 addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
683 ifp->int_rip_sock = get_rip_sock(addr, 0);
694 /* turn on RIP multicast input via an interface
697 rip_mcast_on(struct interface *ifp)
700 struct sockaddr_in *sin;
702 if (!IS_RIP_IN_OFF(ifp->int_state)
703 && (ifp->int_if_flags & IFF_MULTICAST)
704 && !(ifp->int_state & IS_ALIAS)) {
705 memset(&gr, 0, sizeof(gr));
706 gr.gr_interface = ifp->int_index;
707 sin = (struct sockaddr_in *)&gr.gr_group;
708 sin->sin_family = AF_INET;
710 sin->sin_len = sizeof(struct sockaddr_in);
712 sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
713 if (setsockopt(rip_sock, IPPROTO_IP, MCAST_JOIN_GROUP,
714 &gr, sizeof(gr)) < 0)
715 LOGERR("setsockopt(MCAST_JOIN_GROUP RIP)");
720 /* Prepare socket used for RIP.
723 rip_on(struct interface *ifp)
725 /* If the main RIP socket is already alive, only start receiving
726 * multicasts for this interface.
734 /* If the main RIP socket is off and it makes sense to turn it on,
735 * then turn it on for all of the interfaces.
736 * It makes sense if either router discovery is off, or if
737 * router discover is on and at most one interface is doing RIP.
739 if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
740 trace_act("turn on RIP");
742 /* Close all of the query sockets so that we can open
743 * the main socket. SO_REUSEPORT is not a solution,
744 * since that would let two daemons bind to the broadcast
747 LIST_FOREACH(ifp, &ifnet, int_list) {
748 if (ifp->int_rip_sock >= 0) {
749 (void)close(ifp->int_rip_sock);
750 ifp->int_rip_sock = -1;
754 rip_sock = get_rip_sock(INADDR_ANY, 1);
755 rip_sock_mcast = NULL;
757 /* Do not advertise anything until we have heard something
759 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
760 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
762 LIST_FOREACH(ifp, &ifnet, int_list) {
763 ifp->int_query_time = NEVER;
766 ifinit_timer.tv_sec = now.tv_sec;
768 } else if (ifp != NULL
769 && !(ifp->int_state & IS_REMOTE)
770 && ifp->int_rip_sock < 0) {
771 /* RIP is off, so ensure there are sockets on which
772 * to listen for queries.
774 ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
781 /* die if malloc(3) fails
784 rtmalloc(size_t size,
787 void *p = malloc(size);
789 logbad(1,"malloc(%lu) failed in %s", (u_long)size, msg);
794 /* get a random instant in an interval
797 intvl_random(struct timeval *tp, /* put value here */
798 u_long lo, /* value is after this second */
799 u_long hi) /* and before this */
801 tp->tv_sec = (time_t)(hi == lo
803 : (lo + arc4random_uniform(1 + hi - lo)));
804 tp->tv_usec = arc4random_uniform(1000000);
809 timevaladd(struct timeval *t1,
813 t1->tv_sec += t2->tv_sec;
814 if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
816 t1->tv_usec -= 1000000;
824 timevalsub(struct timeval *t1,
828 t1->tv_sec = t2->tv_sec - t3->tv_sec;
829 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
831 t1->tv_usec += 1000000;
836 /* put a message into the system log
839 msglog(const char *p, ...)
846 vsyslog(LOG_ERR, p, args);
848 if (ftrace != NULL) {
849 if (ftrace == stdout)
850 (void)fputs("routed: ", ftrace);
852 (void)vfprintf(ftrace, p, args);
854 (void)fputc('\n', ftrace);
859 /* Put a message about a bad system into the system log if
860 * we have not complained about it recently.
862 * It is desirable to complain about all bad systems, but not too often.
863 * In the worst case, it is not practical to keep track of all bad systems.
864 * For example, there can be many systems with the wrong password.
867 msglim(struct msg_limit *lim, naddr addr, const char *p, ...)
871 struct msg_sub *ms1, *ms;
874 /* look for the oldest slot in the table
875 * or the slot for the bad router.
877 ms = ms1 = lim->subs;
878 for (i = MSG_SUBJECT_N; ; i--, ms1++) {
880 /* Reuse a slot at most once every 10 minutes.
882 if (lim->reuse > now.tv_sec) {
886 lim->reuse = now.tv_sec + 10*60;
890 if (ms->addr == addr) {
891 /* Repeat a complaint about a given system at
894 if (ms->until > now.tv_sec)
898 if (ms->until < ms1->until)
903 ms->until = now.tv_sec + 60*60; /* 60 minutes */
906 for (p1 = p; *p1 == ' '; p1++)
909 vsyslog(LOG_ERR, p1, args);
913 /* always display the message if tracing */
914 if (ftrace != NULL) {
916 (void)vfprintf(ftrace, p, args);
918 (void)fputc('\n', ftrace);
924 logbad(int dump, const char *p, ...)
931 vsyslog(LOG_ERR, p, args);
933 (void)fputs("routed: ", stderr);
935 (void)vfprintf(stderr, p, args);
937 (void)fputs("; giving up\n",stderr);
938 (void)fflush(stderr);